Subject: Re: PR11150 on login_getclass()
To: Martin J. Laubach <mjl@emsi.priv.at>
From: None <itojun@iijlab.net>
List: tech-userlevel
Date: 10/12/2000 09:15:25
>  As I mentioned in private mail, the above doesn't look all too good
>as solution. Something along OpenBSD's fix is much better I think.
>  While at it, you could nuke that classfiles vector, since we only
>support one config file anyway.

	something like this?

itojun


Index: login_cap.c
===================================================================
RCS file: /cvsroot/basesrc/lib/libutil/login_cap.c,v
retrieving revision 1.7
diff -u -r1.7 login_cap.c
--- login_cap.c	2000/09/21 10:15:32	1.7
+++ login_cap.c	2000/10/12 00:15:00
@@ -59,8 +59,6 @@
 #include <unistd.h>
 #include <util.h>
 
-static char	*classfiles[] = { _PATH_LOGIN_CONF, 0 };
-
 static void	setuserpath(login_cap_t *, char *);
 static u_quad_t	multiply(u_quad_t, u_quad_t);
 static u_quad_t	strtolimit(char *, char **, int);
@@ -72,12 +70,16 @@
 login_cap_t *
 login_getclass(char *class)
 {
+	char *classfiles[2];
 	login_cap_t *lc;
 	int res;
 
-	for (res = 0; classfiles[res]; ++res)
-		if (secure_path(classfiles[res]) < 0)
-			return (0);
+	if (secure_path(_PATH_LOGIN_CONF) == 0) {
+		classfiles[0] = _PATH_LOGIN_CONF;
+		classfiles[1] = NULL;
+	} else {
+		classfiles[0] = NULL;
+	}
 
 	if ((lc = malloc(sizeof(login_cap_t))) == NULL) {
 		syslog(LOG_ERR, "%s:%d malloc: %m", __FILE__, __LINE__);
@@ -95,8 +97,16 @@
 		free(lc);
 		return (0);
 	}
+
+	/*
+	 * Not having a login.conf file is not an error condition.
+	 * The individual routines deal reasonably with missing
+	 * capabilities and use default values.
+	 */
+	if (classfiles[0] == NULL)
+		return(lc);
 
-	if ((res = cgetent(&lc->lc_cap, classfiles, lc->lc_class)) != 0 ) {
+	if ((res = cgetent(&lc->lc_cap, classfiles, lc->lc_class)) != 0) {
 		lc->lc_cap = 0;
 		switch (res) {
 		case 1: 
@@ -431,7 +441,7 @@
 	char **res;
 	char *str = login_getcapstr(lc, "setenv", NULL, NULL);
 		  
-	if(str == NULL || *str == '\0')
+	if (str == NULL || *str == '\0')
 		return 0;
 	
 	/* count the sub-strings */
@@ -439,13 +449,13 @@
 		ptr += strcspn(ptr, stop);
 		if (*ptr)
 			ptr++;
-		}
+	}
 
 	/* allocate ptr array and string */
 	count = i;
-	res = malloc( count * sizeof(char *) + strlen(str) + 1 );
+	res = malloc(count * sizeof(char *) + strlen(str) + 1);
 
-	if(!res)
+	if (!res)
 		return -1;
 	
 	ptr = (char *)res + count * sizeof(char *);
@@ -457,7 +467,7 @@
 		ptr += strcspn(ptr, stop);
 		if (*ptr)
 			*ptr++ = '\0';
-		}
+	}
 	
 	res[i] = NULL;
 
@@ -723,7 +733,7 @@
 	};
 	const char **i;
 
-	for(i = infs; *i; i++) {
+	for (i = infs; *i; i++) {
 		if (!strcasecmp(s, *i))
 			return 1;
 	}
@@ -813,4 +823,3 @@
 
 	return (m);
 }
-