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

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.1       provos     70:        } else
                     71:                if (!strcmp(now, "newsalt")) {
1.2     ! provos     72:                        if( max < 10 )
        !            73:                                return 0;
1.1       provos     74:                        (void) srandom((int) time((time_t *) NULL));
                     75:                        salt[0] = _PASSWORD_EFMT1;
                     76:                        to64(&salt[1], (long) (29 * 25), 4);
                     77:                        to64(&salt[5], random(), 4);
1.2     ! provos     78:                        salt[9] = '\0';
1.1       provos     79:                } else
                     80:                        if (!strcmp(now, "blowfish")) {
                     81:                                int     rounds = atoi(next);
                     82:                                if (rounds < 4)
                     83:                                        rounds = 4;
                     84:                                strncpy(salt, bcrypt_gensalt(rounds), max - 1);
                     85:                                salt[max - 1] = 0;
                     86:                        } else {
                     87:                                strcpy(salt, ":");
                     88:                                warnx("Unkown option %s.", now);
                     89:                        }
1.2     ! provos     90:        return 1;
        !            91: }
        !            92:
        !            93: static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
        !            94:         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        !            95:
        !            96: void to64(s, v, n)
        !            97:         register char *s;
        !            98:         register long v;
        !            99:         register int n;
        !           100: {
        !           101:         while (--n >= 0) {
        !           102:                 *s++ = itoa64[v&0x3f];
        !           103:                 v >>= 6;
        !           104:         }
1.1       provos    105: }