Annotation of src/usr.bin/passwd/pwd_gensalt.c, Revision 1.3
1.1 provos 1: /*
2: * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
1.2 provos 15: * This product includes software developed by Niels Provos.
1.1 provos 16: * 4. The name of the author may not be used to endorse or promote products
17: * derived from this software without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29: */
30:
1.2 provos 31: #include <sys/syslimits.h>
1.1 provos 32: #include <stdio.h>
33: #include <string.h>
34: #include <err.h>
35: #include <pwd.h>
1.2 provos 36: #include <util.h>
1.1 provos 37:
1.2 provos 38: void to64( char *, long, int n);
1.1 provos 39:
1.2 provos 40: int
1.1 provos 41: pwd_gensalt(salt, max, pwd, type)
42: char *salt;
43: int max;
44: struct passwd *pwd;
45: char type;
46: {
47: char *bcrypt_gensalt __P((u_int8_t));
48: char option[LINE_MAX];
49: char *next, *now;
50: *salt = '\0';
51:
52: switch (type) {
53: case 'y':
1.2 provos 54: pw_getconf(option, LINE_MAX, pwd->pw_name, "ypcipher");
1.1 provos 55: break;
56: case 'l':
57: default:
1.2 provos 58: pw_getconf(option, LINE_MAX, pwd->pw_name, "localcipher");
1.1 provos 59: break;
60: }
61:
62: next = option;
63: now = strsep(&next, ",");
64: if (!strcmp(now, "old")) {
1.2 provos 65: if( max < 3 )
66: return 0;
1.1 provos 67: (void) srandom((int) time((time_t *) NULL));
68: to64(&salt[0], random(), 2);
1.2 provos 69: salt[2] = '\0';
1.3 ! provos 70: } else if (!strcmp(now, "newsalt")) {
! 71: if( max < 10 )
! 72: return 0;
! 73: (void) srandom((int) time((time_t *) NULL));
! 74: salt[0] = _PASSWORD_EFMT1;
! 75: to64(&salt[1], (long) (29 * 25), 4);
! 76: to64(&salt[5], random(), 4);
! 77: salt[9] = '\0';
! 78: } else if (!strcmp(now, "md5")) {
! 79: if( max < 13 ) /* $1$8salt$\0 */
! 80: return 0;
! 81: strcpy(salt, "$1$");
! 82: (void) srandom((int) time((time_t *) NULL));
! 83: to64(&salt[3], random(), 4);
! 84: to64(&salt[7], random(), 4);
! 85: strcpy(&salt[11], "$");
! 86: } else if (!strcmp(now, "blowfish")) {
! 87: int rounds = atoi(next);
! 88: if (rounds < 4)
! 89: rounds = 4;
! 90: strncpy(salt, bcrypt_gensalt(rounds), max - 1);
! 91: salt[max - 1] = 0;
! 92: } else {
! 93: strcpy(salt, ":");
! 94: warnx("Unkown option %s.", now);
! 95: }
1.2 provos 96: return 1;
97: }
98:
99: static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
100: "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
101:
102: void to64(s, v, n)
103: register char *s;
104: register long v;
105: register int n;
106: {
107: while (--n >= 0) {
108: *s++ = itoa64[v&0x3f];
109: v >>= 6;
110: }
1.1 provos 111: }