[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.18

1.18    ! millert     1: /*     $OpenBSD: pwd_gensalt.c,v 1.17 2003/07/02 21:04:10 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.2       provos     33: #include <sys/syslimits.h>
1.7       provos     34: #include <sys/types.h>
1.1       provos     35: #include <stdio.h>
1.4       weingart   36: #include <stdlib.h>
1.1       provos     37: #include <string.h>
                     38: #include <err.h>
1.7       provos     39: #include <grp.h>
1.1       provos     40: #include <pwd.h>
1.4       weingart   41: #include <time.h>
1.10      millert    42: #include <login_cap.h>
1.1       provos     43:
1.14      deraadt    44: void   to64(char *, int32_t, int n);
                     45: char   *bcrypt_gensalt(u_int8_t);
1.18    ! millert    46: int    pwd_gensalt(char *, int, login_cap_t *, char);
1.1       provos     47:
1.2       provos     48: int
1.18    ! millert    49: pwd_gensalt(char *salt, int saltlen, login_cap_t *lc, char type)
1.1       provos     50: {
1.14      deraadt    51:        char    option[LINE_MAX], *next, *now, *cipher;
                     52:
1.18    ! millert    53:        option[0] = '\0';
1.1       provos     54:        *salt = '\0';
                     55:
                     56:        switch (type) {
                     57:        case 'y':
1.14      deraadt    58:                cipher = "ypcipher";
1.1       provos     59:                break;
                     60:        case 'l':
                     61:        default:
1.14      deraadt    62:                cipher = "localcipher";
1.1       provos     63:                break;
1.7       provos     64:        }
                     65:
1.10      millert    66:        /*
1.18    ! millert    67:         * Check login.conf
1.10      millert    68:         */
                     69:        if ((next = login_getcapstr(lc, cipher, NULL, NULL)) != NULL) {
                     70:                strlcpy(option, next, sizeof(option));
                     71:                free(next);
1.18    ! millert    72:        }
1.10      millert    73:
1.18    ! millert    74:        if (*option == 0) {
        !            75:                if (type == 'l')
        !            76:                        strlcpy(option, "old", sizeof(option));
        !            77:                else
        !            78:                        strlcpy(option, "blowfish,6", sizeof(option));
1.1       provos     79:        }
                     80:
                     81:        next = option;
                     82:        now = strsep(&next, ",");
                     83:        if (!strcmp(now, "old")) {
1.14      deraadt    84:                if (saltlen < 3)
1.2       provos     85:                        return 0;
1.5       provos     86:                to64(&salt[0], arc4random(), 2);
1.2       provos     87:                salt[2] = '\0';
1.3       provos     88:        } else if (!strcmp(now, "newsalt")) {
1.8       provos     89:                u_int32_t rounds = atol(next);
1.14      deraadt    90:
                     91:                if (saltlen < 10)
1.3       provos     92:                        return 0;
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.8       provos     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.14      deraadt   103:                if (saltlen < 13)       /* $1$8salt$\0 */
1.3       provos    104:                        return 0;
1.14      deraadt   105:
                    106:                strlcpy(salt, "$1$", saltlen);
1.5       provos    107:                to64(&salt[3], arc4random(), 4);
                    108:                to64(&salt[7], arc4random(), 4);
1.16      deraadt   109:                strlcpy(&salt[11], "$", saltlen - 11);
1.3       provos    110:        } else if (!strcmp(now, "blowfish")) {
1.14      deraadt   111:                int rounds = atoi(next);
                    112:
1.3       provos    113:                if (rounds < 4)
                    114:                        rounds = 4;
1.14      deraadt   115:                strlcpy(salt, bcrypt_gensalt(rounds), saltlen);
1.3       provos    116:        } else {
1.14      deraadt   117:                strlcpy(salt, ":", saltlen);
1.15      henning   118:                warnx("Unknown option %s.", now);
1.3       provos    119:        }
1.2       provos    120:        return 1;
                    121: }
                    122:
1.6       deraadt   123: static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
                    124:        "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
1.2       provos    125:
1.6       deraadt   126: void
1.14      deraadt   127: to64(char *s, int32_t v, int n)
1.2       provos    128: {
1.6       deraadt   129:        while (--n >= 0) {
                    130:                *s++ = itoa64[v&0x3f];
                    131:                v >>= 6;
                    132:        }
1.1       provos    133: }