From 5cdc7e233587eb80609471deb3fa029633e9a233 Mon Sep 17 00:00:00 2001 From: deraadt <> Date: Fri, 24 May 2002 21:27:38 +0000 Subject: [PATCH] strlcpy and KNF --- src/lib/libutil/passwd.c | 61 +++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/src/lib/libutil/passwd.c b/src/lib/libutil/passwd.c index b36620eb..fa1830d3 100644 --- a/src/lib/libutil/passwd.c +++ b/src/lib/libutil/passwd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: passwd.c,v 1.32 2002/04/10 10:11:03 mpech Exp $ */ +/* $OpenBSD: passwd.c,v 1.33 2002/05/24 21:27:38 deraadt Exp $ */ /* * Copyright (c) 1987, 1993, 1994, 1995 @@ -34,7 +34,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: passwd.c,v 1.32 2002/04/10 10:11:03 mpech Exp $"; +static char rcsid[] = "$OpenBSD: passwd.c,v 1.33 2002/05/24 21:27:38 deraadt Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -58,7 +58,7 @@ static char rcsid[] = "$OpenBSD: passwd.c,v 1.32 2002/04/10 10:11:03 mpech Exp $ #include "util.h" -#define NUM_OPTIONS 2 /* Number of hardcoded defaults */ +#define NUM_OPTIONS 2 /* Number of hardcoded defaults */ static void pw_cont(int sig); @@ -98,9 +98,10 @@ static int read_line(fp, line, max) FILE *fp; char *line; - int max; + int max; { char *p; + /* Read one line of config */ if (fgets(line, max, fp) == 0) return 0; @@ -123,7 +124,8 @@ static const char * pw_default(option) char *option; { - int i; + int i; + for (i = 0; i < NUM_OPTIONS; i++) if (!strcmp(options[i][0], option)) return options[i][1]; @@ -153,7 +155,7 @@ pw_file(nm) * Retrieve password information from the /etc/passwd.conf file, * at the moment this is only for choosing the cipher to use. * It could easily be used for other authentication methods as - * well. + * well. */ void pw_getconf(data, max, key, option) @@ -166,8 +168,8 @@ pw_getconf(data, max, key, option) char line[LINE_MAX]; static char result[LINE_MAX]; char *p; - int got = 0; - int found = 0; + int got = 0; + int found = 0; result[0] = '\0'; @@ -175,7 +177,7 @@ pw_getconf(data, max, key, option) if (!p || (fp = fopen(p, "r")) == NULL) { if (p) free(p); - if((p = (char *)pw_default(option))) { + if ((p = (char *)pw_default(option))) { strncpy(data, p, max - 1); data[max - 1] = '\0'; } else @@ -192,35 +194,36 @@ pw_getconf(data, max, key, option) /* Now we found our specified key */ while (read_line(fp, line, LINE_MAX)) { - char *p2; - /* Leaving key field */ - if (strchr(line, ':')) { - got = 1; - break; - } - p2 = line; - if (!(p = strsep(&p2, "=")) || p2 == NULL) - continue; - trim_whitespace(p); - if (!strncmp(p, option, strlen(option))) { - trim_whitespace(p2); - strcpy(result, p2); - found = 1; - break; - } + char *p2; + + /* Leaving key field */ + if (strchr(line, ':')) { + got = 1; + break; + } + p2 = line; + if (!(p = strsep(&p2, "=")) || p2 == NULL) + continue; + trim_whitespace(p); + if (!strncmp(p, option, strlen(option))) { + trim_whitespace(p2); + strlcpy(result, p2, sizeof result); + found = 1; + break; + } } } fclose(fp); - /* + /* * If we got no result and were looking for a default * value, try hard coded defaults. */ if (!strlen(result) && !strcmp(key,"default") && - (p=(char *)pw_default(option))) + (p = (char *)pw_default(option))) strncpy(data, p, max - 1); - else + else strncpy(data, result, max - 1); data[max - 1] = '\0'; } @@ -581,7 +584,7 @@ pw_scan(bp, pw, flags) break; } if (!strcmp(p, sh)) - break; + break; } endusershell(); }