Annotation of src/usr.bin/passwd/pwd_gensalt.c, Revision 1.27
1.27 ! dhill 1: /* $OpenBSD: pwd_gensalt.c,v 1.26 2007/07/06 13:09:36 thib Exp $ */
1.17 deraadt 2:
1.1 provos 3: /*
4: * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
1.2 provos 17: * This product includes software developed by Niels Provos.
1.1 provos 18: * 4. The name of the author may not be used to endorse or promote products
19: * derived from this software without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31: */
32:
1.7 provos 33: #include <sys/types.h>
1.1 provos 34: #include <stdio.h>
1.4 weingart 35: #include <stdlib.h>
1.1 provos 36: #include <string.h>
37: #include <err.h>
1.7 provos 38: #include <grp.h>
1.1 provos 39: #include <pwd.h>
1.4 weingart 40: #include <time.h>
1.10 millert 41: #include <login_cap.h>
1.1 provos 42:
1.25 deraadt 43: void to64(char *, u_int32_t, int n);
44: int pwd_gensalt(char *, int, login_cap_t *, char);
1.1 provos 45:
1.20 millert 46: #define YPCIPHER_DEF "old"
47: #define LOCALCIPHER_DEF "blowfish,6"
48:
1.2 provos 49: int
1.18 millert 50: pwd_gensalt(char *salt, int saltlen, login_cap_t *lc, char type)
1.1 provos 51: {
1.21 otto 52: char *next, *now, *oldnext;
1.14 deraadt 53:
1.1 provos 54: *salt = '\0';
55:
56: switch (type) {
57: case 'y':
1.21 otto 58: next = login_getcapstr(lc, "ypcipher", NULL, NULL);
59: if (next == NULL && (next = strdup(YPCIPHER_DEF)) == NULL) {
60: warn(NULL);
61: return 0;
62: }
1.1 provos 63: break;
64: case 'l':
65: default:
1.21 otto 66: next = login_getcapstr(lc, "localcipher", NULL, NULL);
67: if (next == NULL && (next = strdup(LOCALCIPHER_DEF)) == NULL) {
68: warn(NULL);
69: return 0;
70: }
1.1 provos 71: break;
72: }
73:
1.21 otto 74: oldnext = next;
1.1 provos 75: now = strsep(&next, ",");
76: if (!strcmp(now, "old")) {
1.21 otto 77: if (saltlen < 3) {
78: free(oldnext);
1.2 provos 79: return 0;
1.21 otto 80: }
1.5 provos 81: to64(&salt[0], arc4random(), 2);
1.2 provos 82: salt[2] = '\0';
1.3 provos 83: } else if (!strcmp(now, "newsalt")) {
1.22 moritz 84: u_int32_t rounds = 7250;
1.14 deraadt 85:
1.22 moritz 86: if (next)
87: rounds = atol(next);
1.21 otto 88: if (saltlen < 10) {
89: free(oldnext);
1.3 provos 90: return 0;
1.21 otto 91: }
1.9 provos 92: /* Check rounds, 24 bit is max */
93: if (rounds < 7250)
94: rounds = 7250;
95: else if (rounds > 0xffffff)
1.14 deraadt 96: rounds = 0xffffff;
1.3 provos 97: salt[0] = _PASSWORD_EFMT1;
1.23 deraadt 98: to64(&salt[1], (u_int32_t)rounds, 4);
1.5 provos 99: to64(&salt[5], arc4random(), 4);
1.3 provos 100: salt[9] = '\0';
101: } else if (!strcmp(now, "md5")) {
1.21 otto 102: if (saltlen < 13) { /* $1$8salt$\0 */
103: free(oldnext);
1.3 provos 104: return 0;
1.21 otto 105: }
1.14 deraadt 106:
107: strlcpy(salt, "$1$", saltlen);
1.5 provos 108: to64(&salt[3], arc4random(), 4);
109: to64(&salt[7], arc4random(), 4);
1.16 deraadt 110: strlcpy(&salt[11], "$", saltlen - 11);
1.3 provos 111: } else if (!strcmp(now, "blowfish")) {
1.22 moritz 112: int rounds = 6;
1.14 deraadt 113:
1.22 moritz 114: if (next)
115: rounds = atoi(next);
1.3 provos 116: if (rounds < 4)
117: rounds = 4;
1.22 moritz 118: if (rounds > 31)
119: rounds = 31;
1.14 deraadt 120: strlcpy(salt, bcrypt_gensalt(rounds), saltlen);
1.3 provos 121: } else {
1.15 henning 122: warnx("Unknown option %s.", now);
1.22 moritz 123: free(oldnext);
124: return 0;
1.3 provos 125: }
1.21 otto 126: free(oldnext);
1.2 provos 127: return 1;
128: }
129:
1.6 deraadt 130: static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
131: "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
1.2 provos 132:
1.25 deraadt 133: void
1.24 deraadt 134: to64(char *s, u_int32_t v, int n)
1.2 provos 135: {
1.6 deraadt 136: while (--n >= 0) {
137: *s++ = itoa64[v&0x3f];
1.24 deraadt 138: v >>= 6;
1.6 deraadt 139: }
1.1 provos 140: }