[BACK]Return to pwd_gensalt.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / passwd

Annotation of src/usr.bin/passwd/pwd_gensalt.c, Revision 1.26

1.26    ! thib        1: /*     $OpenBSD: pwd_gensalt.c,v 1.25 2006/04/02 04:13:08 deraadt 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: char   *bcrypt_gensalt(u_int8_t);
                     45: int    pwd_gensalt(char *, int, login_cap_t *, char);
1.1       provos     46:
1.20      millert    47: #define        YPCIPHER_DEF            "old"
                     48: #define        LOCALCIPHER_DEF         "blowfish,6"
                     49:
1.2       provos     50: int
1.18      millert    51: pwd_gensalt(char *salt, int saltlen, login_cap_t *lc, char type)
1.1       provos     52: {
1.21      otto       53:        char *next, *now, *oldnext;
1.14      deraadt    54:
1.1       provos     55:        *salt = '\0';
                     56:
                     57:        switch (type) {
                     58:        case 'y':
1.21      otto       59:                next = login_getcapstr(lc, "ypcipher", NULL, NULL);
                     60:                if (next == NULL && (next = strdup(YPCIPHER_DEF)) == NULL) {
                     61:                        warn(NULL);
                     62:                        return 0;
                     63:                }
1.1       provos     64:                break;
                     65:        case 'l':
                     66:        default:
1.21      otto       67:                next = login_getcapstr(lc, "localcipher", NULL, NULL);
                     68:                if (next == NULL && (next = strdup(LOCALCIPHER_DEF)) == NULL) {
                     69:                        warn(NULL);
                     70:                        return 0;
                     71:                }
1.1       provos     72:                break;
                     73:        }
                     74:
1.21      otto       75:        oldnext = next;
1.1       provos     76:        now = strsep(&next, ",");
                     77:        if (!strcmp(now, "old")) {
1.21      otto       78:                if (saltlen < 3) {
                     79:                        free(oldnext);
1.2       provos     80:                        return 0;
1.21      otto       81:                }
1.5       provos     82:                to64(&salt[0], arc4random(), 2);
1.2       provos     83:                salt[2] = '\0';
1.3       provos     84:        } else if (!strcmp(now, "newsalt")) {
1.22      moritz     85:                u_int32_t rounds = 7250;
1.14      deraadt    86:
1.22      moritz     87:                if (next)
                     88:                        rounds = atol(next);
1.21      otto       89:                if (saltlen < 10) {
                     90:                        free(oldnext);
1.3       provos     91:                        return 0;
1.21      otto       92:                }
1.9       provos     93:                /* Check rounds, 24 bit is max */
                     94:                if (rounds < 7250)
                     95:                        rounds = 7250;
                     96:                else if (rounds > 0xffffff)
1.14      deraadt    97:                        rounds = 0xffffff;
1.3       provos     98:                salt[0] = _PASSWORD_EFMT1;
1.23      deraadt    99:                to64(&salt[1], (u_int32_t)rounds, 4);
1.5       provos    100:                to64(&salt[5], arc4random(), 4);
1.3       provos    101:                salt[9] = '\0';
                    102:        } else if (!strcmp(now, "md5")) {
1.21      otto      103:                if (saltlen < 13) {     /* $1$8salt$\0 */
                    104:                        free(oldnext);
1.3       provos    105:                        return 0;
1.21      otto      106:                }
1.14      deraadt   107:
                    108:                strlcpy(salt, "$1$", saltlen);
1.5       provos    109:                to64(&salt[3], arc4random(), 4);
                    110:                to64(&salt[7], arc4random(), 4);
1.16      deraadt   111:                strlcpy(&salt[11], "$", saltlen - 11);
1.3       provos    112:        } else if (!strcmp(now, "blowfish")) {
1.22      moritz    113:                int rounds = 6;
1.14      deraadt   114:
1.22      moritz    115:                if (next)
                    116:                        rounds = atoi(next);
1.3       provos    117:                if (rounds < 4)
                    118:                        rounds = 4;
1.22      moritz    119:                if (rounds > 31)
                    120:                        rounds = 31;
1.14      deraadt   121:                strlcpy(salt, bcrypt_gensalt(rounds), saltlen);
1.3       provos    122:        } else {
1.15      henning   123:                warnx("Unknown option %s.", now);
1.22      moritz    124:                free(oldnext);
                    125:                return 0;
1.3       provos    126:        }
1.21      otto      127:        free(oldnext);
1.2       provos    128:        return 1;
                    129: }
                    130:
1.6       deraadt   131: static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
                    132:        "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
1.2       provos    133:
1.25      deraadt   134: void
1.24      deraadt   135: to64(char *s, u_int32_t v, int n)
1.2       provos    136: {
1.6       deraadt   137:        while (--n >= 0) {
                    138:                *s++ = itoa64[v&0x3f];
1.24      deraadt   139:                v >>= 6;
1.6       deraadt   140:        }
1.1       provos    141: }