From: provos Date: Sun, 16 Feb 1997 20:08:56 +0000 (+0000) Subject: cleaned up, moved pw_getconf to libutil, removed _'s in option names X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=3d920e5d7aa66408cd4fa3359a8df1b951736e42;p=openbsd cleaned up, moved pw_getconf to libutil, removed _'s in option names --- diff --git a/usr.bin/passwd/local_passwd.c b/usr.bin/passwd/local_passwd.c index c367545794e..f54dc12ab6d 100644 --- a/usr.bin/passwd/local_passwd.c +++ b/usr.bin/passwd/local_passwd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: local_passwd.c,v 1.5 1997/02/14 23:27:28 provos Exp $ */ +/* $OpenBSD: local_passwd.c,v 1.6 1997/02/16 20:08:56 provos Exp $ */ /*- * Copyright (c) 1990 The Regents of the University of California. @@ -35,7 +35,7 @@ #ifndef lint /*static char sccsid[] = "from: @(#)local_passwd.c 5.5 (Berkeley) 5/6/91";*/ -static char rcsid[] = "$OpenBSD: local_passwd.c,v 1.5 1997/02/14 23:27:28 provos Exp $"; +static char rcsid[] = "$OpenBSD: local_passwd.c,v 1.6 1997/02/16 20:08:56 provos Exp $"; #endif /* not lint */ #include @@ -108,7 +108,7 @@ getnewpasswd(pw) register char *p, *t; int tries; char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN], *crypt(), *getpass(); - void pwd_gensalt __P(( char *, int, struct passwd *, char)); + int pwd_gensalt __P(( char *, int, struct passwd *, char)); (void)printf("Changing local password for %s.\n", pw->pw_name); @@ -139,20 +139,9 @@ getnewpasswd(pw) break; (void)printf("Mismatch; try again, EOF to quit.\n"); } - pwd_gensalt( salt, _PASSWORD_LEN, pw, 'l' ); - return(crypt(buf, salt)); -} - -static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ - "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - -to64(s, v, n) - register char *s; - register long v; - register int n; -{ - while (--n >= 0) { - *s++ = itoa64[v&0x3f]; - v >>= 6; + if( !pwd_gensalt( salt, _PASSWORD_LEN, pw, 'l' )) { + (void)printf("Couldn't generate salt.\n"); + pw_error(NULL, 0, 0); } + return(crypt(buf, salt)); } diff --git a/usr.bin/passwd/pwd_gensalt.c b/usr.bin/passwd/pwd_gensalt.c index 9f84b47e886..80dff3ba1ab 100644 --- a/usr.bin/passwd/pwd_gensalt.c +++ b/usr.bin/passwd/pwd_gensalt.c @@ -12,7 +12,7 @@ * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: - * This product includes software developed by Theo de Raadt. + * This product includes software developed by Niels Provos. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * @@ -28,131 +28,16 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include #include +#include +void to64( char *, long, int n); -#define NUM_OPTIONS 2 /* Number of hardcoded defaults */ -#define LINE_MAX 100 /* Max. length of one config file */ - -static const char options[NUM_OPTIONS][2][80] = -{ - {"local_cipher", "blowfish,4"}, - {"yp_cipher", "old"} -}; -/* Read lines and removes trailers. */ - -static int -read_line(fp, line, max) - FILE *fp; - char *line; - int max; -{ - char *p, *c; - /* Read one line of config */ - if (fgets(line, max, fp) == 0) - return 0; - if (!(p = strchr(line, '\n'))) { - warnx("line too long"); - return 0; - } - *p = '\0'; - - /* Remove comments */ - if ((p = strchr(line, '#'))) - *p = '\0'; - - /* Remove trailing spaces */ - p = line; - while (isspace(*p)) - p++; - memcpy(line, p, strlen(p) + 1); - - p = line + strlen(line) - 1; - while (isspace(*p)) - p--; - *(p + 1) = '\0'; - return 1; -} - - -static const char * -pwd_default(option) - char *option; -{ - int i; - for (i = 0; i < NUM_OPTIONS; i++) - if (!strcasecmp(options[i][0], option)) - return options[i][1]; - return NULL; -} - -void -pwd_gettype(data, max, key, option) - char *data; - int max; - char *key; - char *option; -{ - FILE *fp; - char line[LINE_MAX]; - static char result[LINE_MAX]; - int defaultw; - int keyw; - int got; - result[0] = '\0'; - if ((fp = fopen(_PATH_PASSWDCONF, "r")) == NULL) { - strncpy(data, pwd_default(option), max - 1); - data[max - 1] = '\0'; - return; - } - defaultw = 0; - keyw = 0; - got = 0; - while (!keyw && (got || read_line(fp, line, LINE_MAX))) { - got = 0; - if (!strcmp("default:", line)) - defaultw = 1; - if (!strncmp(key, line, strlen(key)) && - line[strlen(key)] == ':') - keyw = 1; - - /* Now we found default or specified key */ - if (defaultw || keyw) { - while (read_line(fp, line, LINE_MAX)) { - /* Leaving key field */ - if (strchr(line, ':')) { - got = 1; - break; - } - if (!strncmp(line, option, strlen(option)) && - line[strlen(option)] == '=') { - char *p; - p = line + strlen(option) + 1; - while (isspace(*p)) - p++; - strcpy(result, p); - break; - } - } - if (keyw) - break; - defaultw = 0; - } - } - fclose(fp); - if (!strlen(result)) { - strncpy(data, pwd_default(option), max - 1); - data[max - 1] = '\0'; - return; - } - strncpy(data, result, max - 1); - data[max - 1] = '\0'; -} - -void +int pwd_gensalt(salt, max, pwd, type) char *salt; int max; @@ -163,30 +48,34 @@ pwd_gensalt(salt, max, pwd, type) char option[LINE_MAX]; char *next, *now; *salt = '\0'; - if (max < 10) - return; switch (type) { case 'y': - pwd_gettype(option, LINE_MAX, pwd->pw_name, "yp_cipher"); + pw_getconf(option, LINE_MAX, pwd->pw_name, "ypcipher"); break; case 'l': default: - pwd_gettype(option, LINE_MAX, pwd->pw_name, "local_cipher"); + pw_getconf(option, LINE_MAX, pwd->pw_name, "localcipher"); break; } next = option; now = strsep(&next, ","); if (!strcmp(now, "old")) { + if( max < 3 ) + return 0; (void) srandom((int) time((time_t *) NULL)); to64(&salt[0], random(), 2); + salt[2] = '\0'; } else if (!strcmp(now, "newsalt")) { + if( max < 10 ) + return 0; (void) srandom((int) time((time_t *) NULL)); salt[0] = _PASSWORD_EFMT1; to64(&salt[1], (long) (29 * 25), 4); to64(&salt[5], random(), 4); + salt[9] = '\0'; } else if (!strcmp(now, "blowfish")) { int rounds = atoi(next); @@ -198,4 +87,19 @@ pwd_gensalt(salt, max, pwd, type) strcpy(salt, ":"); warnx("Unkown option %s.", now); } + return 1; +} + +static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ + "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + +void to64(s, v, n) + register char *s; + register long v; + register int n; +{ + while (--n >= 0) { + *s++ = itoa64[v&0x3f]; + v >>= 6; + } } diff --git a/usr.bin/passwd/yp_passwd.c b/usr.bin/passwd/yp_passwd.c index 84e563fabab..323b1dfc656 100644 --- a/usr.bin/passwd/yp_passwd.c +++ b/usr.bin/passwd/yp_passwd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: yp_passwd.c,v 1.5 1997/02/14 23:27:31 provos Exp $ */ +/* $OpenBSD: yp_passwd.c,v 1.6 1997/02/16 20:09:00 provos Exp $ */ /* * Copyright (c) 1988 The Regents of the University of California. @@ -34,7 +34,7 @@ */ #ifndef lint /*static char sccsid[] = "from: @(#)yp_passwd.c 1.0 2/2/93";*/ -static char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.5 1997/02/14 23:27:31 provos Exp $"; +static char rcsid[] = "$OpenBSD: yp_passwd.c,v 1.6 1997/02/16 20:09:00 provos Exp $"; #endif /* not lint */ #ifdef YP @@ -192,7 +192,7 @@ getnewpasswd(pw, old_pass) register char *p, *t; int tries; char salt[_PASSWORD_LEN], *crypt(), *getpass(); - void pwd_gensalt __P(( char *, int, struct passwd *, char)); + int pwd_gensalt __P(( char *, int, struct passwd *, char)); printf("Changing YP password for %s.\n", pw->pw_name); @@ -231,7 +231,10 @@ getnewpasswd(pw, old_pass) break; (void)printf("Mismatch; try again, EOF to quit.\n"); } - pwd_gensalt( salt, _PASSWORD_LEN, pw, 'y' ); + if( !pwd_gensalt( salt, _PASSWORD_LEN, pw, 'y' )) { + (void)printf("Couldn't generate salt.\n"); + pw_error(NULL, 0, 0); + } return(strdup(crypt(buf, salt))); }