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

Annotation of src/usr.bin/skeyinit/skeyinit.c, Revision 1.58

1.58    ! deraadt     1: /*     $OpenBSD: skeyinit.c,v 1.57 2015/04/18 18:28:38 deraadt Exp $   */
1.1       deraadt     2:
1.28      millert     3: /* OpenBSD S/Key (skeyinit.c)
1.1       deraadt     4:  *
                      5:  * Authors:
                      6:  *          Neil M. Haller <nmh@thumper.bellcore.com>
                      7:  *          Philip R. Karn <karn@chicago.qualcomm.com>
                      8:  *          John S. Walden <jsw@thumper.bellcore.com>
                      9:  *          Scott Chasin <chasin@crimelab.com>
1.20      millert    10:  *          Todd C. Miller <Todd.Miller@courtesan.com>
                     11:  *
1.28      millert    12:  * S/Key initialization and seed update
1.1       deraadt    13:  */
                     14:
1.20      millert    15: #include <sys/file.h>
1.32      millert    16: #include <sys/resource.h>
                     17: #include <sys/stat.h>
1.1       deraadt    18: #include <sys/time.h>
                     19:
1.32      millert    20: #include <ctype.h>
1.19      millert    21: #include <err.h>
1.20      millert    22: #include <errno.h>
1.19      millert    23: #include <pwd.h>
1.28      millert    24: #include <readpassphrase.h>
1.1       deraadt    25: #include <stdio.h>
                     26: #include <stdlib.h>
                     27: #include <string.h>
1.19      millert    28: #include <syslog.h>
                     29: #include <time.h>
1.1       deraadt    30: #include <unistd.h>
1.56      deraadt    31: #include <limits.h>
1.6       millert    32: #include <utmp.h>
1.19      millert    33:
1.4       millert    34: #include <skey.h>
1.32      millert    35: #include <bsd_auth.h>
1.1       deraadt    36:
1.4       millert    37: #ifndef SKEY_NAMELEN
1.17      millert    38: #define SKEY_NAMELEN    4
1.4       millert    39: #endif
1.1       deraadt    40:
1.36      millert    41: void   usage(void);
1.44      deraadt    42: void   secure_mode(int *, char *, char *, size_t, char *, size_t);
1.38      millert    43: void   normal_mode(char *, int, char *, char *);
1.32      millert    44: void   convert_db(void);
                     45: void   enable_db(int);
1.8       millert    46:
1.1       deraadt    47: int
1.36      millert    48: main(int argc, char **argv)
1.1       deraadt    49: {
1.35      millert    50:        int     rval, i, l, n, defaultsetup, rmkey, hexmode, enable, convert;
1.56      deraadt    51:        char    hostname[HOST_NAME_MAX+1];
1.38      millert    52:        char    seed[SKEY_MAX_SEED_LEN + 1];
1.32      millert    53:        char    buf[256], key[SKEY_BINKEY_SIZE], filename[PATH_MAX], *ht;
                     54:        char    lastc, me[UT_NAMESIZE + 1], *p, *auth_type;
1.57      deraadt    55:        const char *errstr;
1.39      millert    56:        u_int32_t noise;
1.1       deraadt    57:        struct skey skey;
                     58:        struct passwd *pp;
                     59:
1.35      millert    60:        n = rmkey = hexmode = enable = convert = 0;
1.32      millert    61:        defaultsetup = 1;
                     62:        ht = auth_type = NULL;
1.4       millert    63:
1.39      millert    64:        /* Build up a default seed based on the hostname and some noise */
1.1       deraadt    65:        if (gethostname(hostname, sizeof(hostname)) < 0)
                     66:                err(1, "gethostname");
1.38      millert    67:        for (i = 0, p = seed; hostname[i] && i < SKEY_NAMELEN; i++) {
1.52      deraadt    68:                if (isalpha((unsigned char)hostname[i])) {
                     69:                        if (isupper((unsigned char)hostname[i]))
                     70:                                hostname[i] = tolower((unsigned char)hostname[i]);
1.26      millert    71:                        *p++ = hostname[i];
1.52      deraadt    72:                } else if (isdigit((unsigned char)hostname[i]))
1.25      millert    73:                        *p++ = hostname[i];
1.39      millert    74:        }
                     75:        noise = arc4random();
                     76:        for (i = 0; i < 5; i++) {
                     77:                *p++ = (noise % 10) + '0';
                     78:                noise /= 10;
1.25      millert    79:        }
                     80:        *p = '\0';
1.1       deraadt    81:
                     82:        if ((pp = getpwuid(getuid())) == NULL)
1.40      deraadt    83:                err(1, "no user with uid %u", getuid());
1.42      deraadt    84:        (void)strlcpy(me, pp->pw_name, sizeof me);
1.1       deraadt    85:
                     86:        if ((pp = getpwnam(me)) == NULL)
                     87:                err(1, "Who are you?");
                     88:
1.8       millert    89:        for (i = 1; i < argc && argv[i][0] == '-' && strcmp(argv[i], "--");) {
                     90:                if (argv[i][2] == '\0') {
                     91:                        /* Single character switch */
                     92:                        switch (argv[i][1]) {
1.32      millert    93:                        case 'a':
                     94:                                if (argv[++i] == NULL || argv[i][0] == '\0')
1.36      millert    95:                                        usage();
1.48      otto       96:                                auth_type = argv[i];
1.32      millert    97:                                break;
1.4       millert    98:                        case 's':
                     99:                                defaultsetup = 0;
1.48      otto      100:                                if (auth_type == NULL)
                    101:                                        auth_type = "skey";
1.4       millert   102:                                break;
                    103:                        case 'x':
                    104:                                hexmode = 1;
                    105:                                break;
1.35      millert   106:                        case 'r':
                    107:                                rmkey = 1;
1.4       millert   108:                                break;
1.17      millert   109:                        case 'n':
1.23      deraadt   110:                                if (argv[++i] == NULL || argv[i][0] == '\0')
1.36      millert   111:                                        usage();
1.57      deraadt   112:                                n = strtonum(argv[i], 1, SKEY_MAX_SEQ - 1, &errstr);
                    113:                                if (errstr)
1.17      millert   114:                                        errx(1, "count must be > 0 and < %d",
                    115:                                             SKEY_MAX_SEQ);
                    116:                                break;
1.32      millert   117:                        case 'C':
                    118:                                convert = 1;
                    119:                                break;
                    120:                        case 'D':
                    121:                                enable = -1;
                    122:                                break;
                    123:                        case 'E':
                    124:                                enable = 1;
                    125:                                break;
1.8       millert   126:                        default:
1.36      millert   127:                                usage();
1.8       millert   128:                        }
                    129:                } else {
                    130:                        /* Multi character switches are hash types */
                    131:                        if ((ht = skey_set_algorithm(&argv[i][1])) == NULL) {
                    132:                                warnx("Unknown hash algorithm %s", &argv[i][1]);
1.36      millert   133:                                usage();
1.8       millert   134:                        }
1.7       millert   135:                }
1.8       millert   136:                i++;
1.7       millert   137:        }
1.32      millert   138:        argv += i;
                    139:        argc -= i;
1.7       millert   140:
1.32      millert   141:        if (argc > 1 || (enable && convert) || (enable && argc) ||
                    142:            (convert && argc))
1.36      millert   143:                usage();
1.32      millert   144:
                    145:        /* Handle -C, -D, and -E */
1.34      millert   146:        if (convert || enable) {
                    147:                if (convert)
                    148:                        convert_db();
                    149:                else
                    150:                        enable_db(enable);
                    151:                exit(0);
                    152:        }
1.32      millert   153:
                    154:        /* Check for optional user string. */
                    155:        if (argc == 1) {
1.36      millert   156:                if ((pp = getpwnam(argv[0])) == NULL) {
1.16      millert   157:                        if (getuid() == 0) {
                    158:                                static struct passwd _pp;
                    159:
1.36      millert   160:                                _pp.pw_name = argv[0];
1.16      millert   161:                                pp = &_pp;
1.36      millert   162:                                warnx("Warning, user unknown: %s", argv[0]);
1.16      millert   163:                        } else {
1.36      millert   164:                                errx(1, "User unknown: %s", argv[0]);
1.16      millert   165:                        }
1.32      millert   166:                } else if (strcmp(pp->pw_name, me) != 0 && getuid() != 0) {
                    167:                        /* Only root can change other's S/Keys. */
                    168:                        errx(1, "Permission denied.");
1.1       deraadt   169:                }
                    170:        }
                    171:
1.41      millert   172:        switch (skey_haskey(pp->pw_name)) {
                    173:        case -1:
                    174:                if (errno == ENOENT || errno == EPERM)
                    175:                        errx(1, "S/Key disabled");
                    176:                else
                    177:                        err(1, "cannot open database");
                    178:                break;
                    179:        case 0:
                    180:                /* existing user */
                    181:                break;
                    182:        case 1:
1.48      otto      183:                if (!defaultsetup && strcmp(auth_type, "skey") == 0) {
1.41      millert   184:                        fprintf(stderr,
1.45      espie     185: "You must authenticate yourself before using S/Key for the first time.  In\n"
                    186: "secure mode this is normally done via an existing S/Key key.  However, since\n"
                    187: "you do not have an entry in the S/Key database you will have to specify an\n"
                    188: "alternate authentication type via the `-a' flag, e.g.\n"
1.54      ajacouto  189: "    \"skeyinit -s -a passwd\"\n\n"
1.45      espie     190: "Note that entering a plaintext password over a non-secure link defeats the\n"
                    191: "purpose of using S/Key in the fist place.\n");
1.41      millert   192:                        exit(1);
                    193:                }
                    194:                break;
                    195:        }
                    196:
1.28      millert   197:        if (defaultsetup)
1.32      millert   198:                fputs("Reminder - Only use this method if you are directly "
                    199:                    "connected\n           or have an encrypted channel.  If "
                    200:                    "you are using telnet,\n           hit return now and use "
                    201:                    "skeyinit -s.\n", stderr);
1.28      millert   202:
1.1       deraadt   203:        if (getuid() != 0) {
1.32      millert   204:                if ((pp = pw_dup(pp)) == NULL)
                    205:                        err(1, NULL);
                    206:                if (!auth_userokay(pp->pw_name, auth_type, NULL, NULL))
                    207:                        errx(1, "Password incorrect");
1.10      millert   208:        }
1.1       deraadt   209:
1.28      millert   210:        /*
                    211:         * Lookup and lock the record we are about to modify.
                    212:         * If this is a new entry this will prevent other users
                    213:         * from appending new entries (and clobbering ours).
                    214:         */
1.1       deraadt   215:        rval = skeylookup(&skey, pp->pw_name);
                    216:        switch (rval) {
1.4       millert   217:                case -1:
1.41      millert   218:                        err(1, "cannot open database");
1.21      millert   219:                        break;
1.4       millert   220:                case 0:
1.35      millert   221:                        /* remove user if asked to do so */
                    222:                        if (rmkey) {
                    223:                                if (snprintf(filename, sizeof(filename),
                    224:                                    "%s/%s", _PATH_SKEYDIR, pp->pw_name)
1.55      guenther  225:                                    >= sizeof(filename))
                    226:                                        errc(1, ENAMETOOLONG,
                    227:                                            "Cannot remove S/Key entry");
1.35      millert   228:                                if (unlink(filename) != 0)
                    229:                                        err(1, "Cannot remove S/Key entry");
                    230:                                printf("S/Key entry for %s removed.\n",
                    231:                                    pp->pw_name);
                    232:                                exit(0);
                    233:                        }
1.4       millert   234:
1.28      millert   235:                        (void)printf("[Updating %s with %s]\n", pp->pw_name,
                    236:                            ht ? ht : skey_get_algorithm());
                    237:                        (void)printf("Old seed: [%s] %s\n",
                    238:                                     skey_get_algorithm(), skey.seed);
1.4       millert   239:
                    240:                        /*
1.25      millert   241:                         * Sanity check old seed.
                    242:                         */
                    243:                        l = strlen(skey.seed);
                    244:                        for (p = skey.seed; *p; p++) {
1.52      deraadt   245:                                if (isalpha((unsigned char)*p)) {
                    246:                                        if (isupper((unsigned char)*p))
                    247:                                                *p = tolower((unsigned char)*p);
                    248:                                } else if (!isdigit((unsigned char)*p)) {
1.25      millert   249:                                        memmove(p, p + 1, l - (p - skey.seed));
                    250:                                        l--;
                    251:                                }
                    252:                        }
                    253:
1.28      millert   254:                        /* If the seed ends in 0-8 just add one.  */
1.4       millert   255:                        if (l > 0) {
                    256:                                lastc = skey.seed[l - 1];
1.52      deraadt   257:                                if (isdigit((unsigned char)lastc) &&
                    258:                                    lastc != '9') {
1.43      deraadt   259:                                        (void)strlcpy(seed, skey.seed,
                    260:                                            sizeof seed);
1.38      millert   261:                                        seed[l - 1] = lastc + 1;
1.4       millert   262:                                }
1.52      deraadt   263:                                if (isdigit((unsigned char)lastc) &&
                    264:                                    lastc == '9' && l < 16) {
1.43      deraadt   265:                                        (void)strlcpy(seed, skey.seed,
                    266:                                            sizeof seed);
1.38      millert   267:                                        seed[l - 1] = '0';
                    268:                                        seed[l] = '0';
                    269:                                        seed[l + 1] = '\0';
1.4       millert   270:                                }
1.1       deraadt   271:                        }
1.4       millert   272:                        break;
                    273:                case 1:
1.35      millert   274:                        if (rmkey)
                    275:                                errx(1, "You have no entry to remove.");
1.28      millert   276:                        (void)printf("[Adding %s with %s]\n", pp->pw_name,
                    277:                            ht ? ht : skey_get_algorithm());
1.32      millert   278:                        if (snprintf(filename, sizeof(filename), "%s/%s",
1.55      guenther  279:                            _PATH_SKEYDIR, pp->pw_name) >= sizeof(filename))
                    280:                                errc(1, ENAMETOOLONG,
                    281:                                    "Cannot create S/Key entry");
1.36      millert   282:                        if ((l = open(filename,
                    283:                            O_RDWR | O_NONBLOCK | O_CREAT | O_TRUNC |O_NOFOLLOW,
1.32      millert   284:                            S_IRUSR | S_IWUSR)) == -1 ||
                    285:                            flock(l, LOCK_EX) != 0 ||
                    286:                            (skey.keyfile = fdopen(l, "r+")) == NULL)
                    287:                                err(1, "Cannot create S/Key entry");
1.4       millert   288:                        break;
1.1       deraadt   289:        }
1.32      millert   290:        if (fchown(fileno(skey.keyfile), pp->pw_uid, -1) != 0 ||
                    291:            fchmod(fileno(skey.keyfile), S_IRUSR | S_IWUSR) != 0)
                    292:                err(1, "can't set owner/mode for %s", pp->pw_name);
1.17      millert   293:        if (n == 0)
1.47      otto      294:                n = 100;
1.1       deraadt   295:
1.8       millert   296:        /* Set hash type if asked to */
1.28      millert   297:        if (ht && strcmp(ht, skey_get_algorithm()) != 0)
                    298:                skey_set_algorithm(ht);
                    299:
                    300:        alarm(180);
                    301:        if (!defaultsetup)
1.44      deraadt   302:                secure_mode(&n, key, seed, sizeof seed, buf, sizeof(buf));
1.28      millert   303:        else
1.38      millert   304:                normal_mode(pp->pw_name, n, key, seed);
1.28      millert   305:        alarm(0);
1.4       millert   306:
1.32      millert   307:        /* XXX - why use malloc here? */
1.58    ! deraadt   308:        if ((skey.val = malloc(16 + 1)) == NULL)
1.28      millert   309:                err(1, "Can't allocate memory");
                    310:        btoa8(skey.val, key);
1.1       deraadt   311:
1.32      millert   312:        (void)fseek(skey.keyfile, 0L, SEEK_SET);
                    313:        (void)fprintf(skey.keyfile, "%s\n%s\n%04d\n%s\n%s\n",
                    314:            pp->pw_name, skey_get_algorithm(), n, seed, skey.val);
1.28      millert   315:        (void)fclose(skey.keyfile);
1.4       millert   316:
1.28      millert   317:        (void)printf("\nID %s skey is otp-%s %d %s\n", pp->pw_name,
1.38      millert   318:            skey_get_algorithm(), n, seed);
1.28      millert   319:        (void)printf("Next login password: %s\n\n",
                    320:            hexmode ? put8(buf, key) : btoe(buf, key));
                    321:        exit(0);
                    322: }
1.1       deraadt   323:
1.28      millert   324: void
1.44      deraadt   325: secure_mode(int *count, char *key, char *seed, size_t seedlen,
                    326:     char *buf, size_t bufsiz)
1.28      millert   327: {
1.38      millert   328:        char *p, newseed[SKEY_MAX_SEED_LEN + 2];
1.57      deraadt   329:        const char *errstr;
1.28      millert   330:        int i, n;
1.4       millert   331:
1.28      millert   332:        (void)puts("You need the 6 words generated from the \"skey\" command.");
                    333:        for (i = 0; ; i++) {
                    334:                if (i >= 2)
                    335:                        exit(1);
                    336:
                    337:                (void)printf("Enter sequence count from 1 to %d: ",
                    338:                    SKEY_MAX_SEQ);
                    339:                (void)fgets(buf, bufsiz, stdin);
                    340:                clearerr(stdin);
1.57      deraadt   341:                n = strtonum(buf, 1, SKEY_MAX_SEQ-1, &errstr);
                    342:                if (!errstr)
1.28      millert   343:                        break;  /* Valid range */
1.57      deraadt   344:                fprintf(stderr, "ERROR: Count must be between 1 and %d\n",
                    345:                    SKEY_MAX_SEQ - 1);
1.28      millert   346:        }
1.1       deraadt   347:
1.28      millert   348:        for (i = 0; ; i++) {
                    349:                if (i >= 2)
                    350:                        exit(1);
                    351:
1.38      millert   352:                (void)printf("Enter new seed [default %s]: ", seed);
                    353:                (void)fgets(newseed, sizeof(newseed), stdin); /* XXX */
1.28      millert   354:                clearerr(stdin);
1.38      millert   355:                rip(newseed);
                    356:                if (strlen(newseed) > SKEY_MAX_SEED_LEN) {
1.28      millert   357:                        (void)fprintf(stderr, "ERROR: Seed must be between 1 "
                    358:                            "and %d characters in length\n", SKEY_MAX_SEED_LEN);
                    359:                        continue;
                    360:                }
1.38      millert   361:                for (p = newseed; *p; p++) {
1.52      deraadt   362:                        if (isspace((unsigned char)*p)) {
1.28      millert   363:                                (void)fputs("ERROR: Seed must not contain "
                    364:                                    "any spaces\n", stderr);
1.1       deraadt   365:                                break;
1.52      deraadt   366:                        } else if (isalpha((unsigned char)*p)) {
                    367:                                if (isupper((unsigned char)*p))
                    368:                                        *p = tolower((unsigned char)*p);
                    369:                        } else if (!isdigit((unsigned char)*p)) {
1.28      millert   370:                                (void)fputs("ERROR: Seed must be purely "
                    371:                                    "alphanumeric\n", stderr);
                    372:                                break;
                    373:                        }
1.1       deraadt   374:                }
1.28      millert   375:                if (*p == '\0')
                    376:                        break;  /* Valid seed */
                    377:        }
1.38      millert   378:        if (newseed[0] != '\0')
1.44      deraadt   379:                (void)strlcpy(seed, newseed, seedlen);
1.1       deraadt   380:
1.28      millert   381:        for (i = 0; ; i++) {
                    382:                if (i >= 2)
                    383:                        exit(1);
                    384:
                    385:                (void)printf("otp-%s %d %s\nS/Key access password: ",
                    386:                             skey_get_algorithm(), n, seed);
                    387:                (void)fgets(buf, bufsiz, stdin);
                    388:                clearerr(stdin);
                    389:                rip(buf);
                    390:                backspace(buf);
                    391:
                    392:                if (buf[0] == '?') {
                    393:                        (void)puts("Enter 6 words from secure S/Key calculation.");
                    394:                        continue;
                    395:                } else if (buf[0] == '\0')
                    396:                        exit(1);
                    397:
                    398:                if (etob(key, buf) == 1 || atob8(key, buf) == 0)
                    399:                        break;  /* Valid format */
                    400:                (void)fputs("ERROR: Invalid format - try again with the 6 words.\n",
                    401:                    stderr);
1.1       deraadt   402:        }
1.28      millert   403:        *count= n;
                    404: }
1.1       deraadt   405:
1.28      millert   406: void
1.38      millert   407: normal_mode(char *username, int n, char *key, char *seed)
1.28      millert   408: {
                    409:        int i, nn;
1.38      millert   410:        char passwd[SKEY_MAX_PW_LEN+2], key2[SKEY_BINKEY_SIZE];
1.1       deraadt   411:
1.28      millert   412:        /* Get user's secret passphrase */
                    413:        for (i = 0; ; i++) {
                    414:                if (i > 2)
1.38      millert   415:                        errx(1, "S/Key entry not updated");
1.28      millert   416:
1.46      otto      417:                if (readpassphrase("Enter new secret passphrase: ", passwd,
1.28      millert   418:                    sizeof(passwd), 0) == NULL || passwd[0] == '\0')
                    419:                        exit(1);
                    420:
                    421:                if (strlen(passwd) < SKEY_MIN_PW_LEN) {
                    422:                        (void)fprintf(stderr,
                    423:                            "ERROR: Your passphrase must be at least %d "
                    424:                            "characters long.\n", SKEY_MIN_PW_LEN);
                    425:                        continue;
                    426:                } else if (strcmp(passwd, username) == 0) {
                    427:                        (void)fputs("ERROR: Your passphrase may not be the "
                    428:                            "same as your user name.\n", stderr);
                    429:                        continue;
1.50      deraadt   430:                } else if (strspn(passwd, "abcdefghijklmnopqrstuvwxyz") ==
1.28      millert   431:                    strlen(passwd)) {
                    432:                        (void)fputs("ERROR: Your passphrase must contain more "
                    433:                            "than just lower case letters.\nWhitespace, "
1.37      aaron     434:                            "numbers, and punctuation are suggested.\n",
                    435:                            stderr);
1.28      millert   436:                        continue;
                    437:                } else if (strlen(passwd) > 63) {
                    438:                        (void)fprintf(stderr, "WARNING: Your passphrase is "
                    439:                            "longer than the recommended maximum length of 63\n");
                    440:                }
                    441:                /* XXX - should check for passphrase that is really too long */
1.13      millert   442:
1.38      millert   443:                /* Crunch seed and passphrase into starting key */
                    444:                nn = keycrunch(key, seed, passwd);
                    445:                memset(passwd, 0, sizeof(passwd));
                    446:                if (nn != 0)
                    447:                        err(2, "key crunch failed");
                    448:
                    449:                if (readpassphrase("Again secret passphrase: ", passwd,
                    450:                    sizeof(passwd), 0) == NULL || passwd[0] == '\0')
                    451:                        exit(1);
                    452:
                    453:                /* Crunch seed and passphrase into starting key */
                    454:                nn = keycrunch(key2, seed, passwd);
                    455:                memset(passwd, 0, sizeof(passwd));
                    456:                if (nn != 0)
                    457:                        err(2, "key crunch failed");
                    458:
                    459:                if (memcmp(key, key2, sizeof(key2)) == 0)
1.28      millert   460:                        break;
1.1       deraadt   461:
1.28      millert   462:                (void)fputs("Passphrases do not match.\n", stderr);
1.20      millert   463:        }
1.28      millert   464:
                    465:        nn = n;
                    466:        while (nn-- != 0)
                    467:                f(key);
                    468: }
                    469:
1.32      millert   470: void
1.36      millert   471: enable_db(int op)
1.32      millert   472: {
                    473:        if (op == 1) {
                    474:                /* enable */
                    475:                if (mkdir(_PATH_SKEYDIR, 01730) != 0 && errno != EEXIST)
                    476:                        err(1, "can't mkdir %s", _PATH_SKEYDIR);
1.33      millert   477:                if (chown(_PATH_SKEYDIR, geteuid(), getegid()) != 0)
                    478:                        err(1, "can't chown %s", _PATH_SKEYDIR);
1.32      millert   479:                if (chmod(_PATH_SKEYDIR, 01730) != 0)
                    480:                        err(1, "can't chmod %s", _PATH_SKEYDIR);
                    481:        } else {
                    482:                /* disable */
                    483:                if (chmod(_PATH_SKEYDIR, 0) != 0 && errno != ENOENT)
                    484:                        err(1, "can't chmod %s", _PATH_SKEYDIR);
                    485:        }
                    486: }
                    487:
                    488: #define _PATH_SKEYKEYS "/etc/skeykeys"
                    489: void
                    490: convert_db(void)
                    491: {
                    492:        struct passwd *pp;
                    493:        uid_t uid;
                    494:        FILE *keyfile;
                    495:        FILE *newfile;
                    496:        char buf[256], *logname, *hashtype, *seed, *val, *cp;
                    497:        char filename[PATH_MAX];
1.57      deraadt   498:        const char *errstr;
1.32      millert   499:        int fd, n;
                    500:
                    501:        if ((keyfile = fopen(_PATH_SKEYKEYS, "r")) == NULL)
                    502:                err(1, "can't open %s", _PATH_SKEYKEYS);
                    503:        if (flock(fileno(keyfile), LOCK_EX) != 0)
                    504:                err(1, "can't lock %s", _PATH_SKEYKEYS);
1.34      millert   505:        enable_db(1);
1.32      millert   506:
                    507:        /*
                    508:         * Loop over each entry in _PATH_SKEYKEYS, creating a file
                    509:         * in _PATH_SKEYDIR for each one.
                    510:         */
                    511:        while (fgets(buf, sizeof(buf), keyfile) != NULL) {
                    512:                if (buf[0] == '#')
                    513:                        continue;
                    514:                if ((logname = strtok(buf, " \t")) == NULL)
                    515:                        continue;
                    516:                if ((cp = strtok(NULL, " \t")) == NULL)
                    517:                        continue;
1.53      naddy     518:                if (!isalpha((unsigned char)*cp))
                    519:                        continue;
                    520:                hashtype = cp;
                    521:                if ((cp = strtok(NULL, " \t")) == NULL)
                    522:                        continue;
1.57      deraadt   523:                n = strtonum(cp, 0, SKEY_MAX_SEQ, &errstr);
                    524:                if (errstr)
                    525:                        continue;
1.32      millert   526:                if ((seed = strtok(NULL, " \t")) == NULL)
                    527:                        continue;
                    528:                if ((val = strtok(NULL, " \t")) == NULL)
                    529:                        continue;
                    530:
                    531:                if ((pp = getpwnam(logname)) != NULL)
                    532:                        uid = pp->pw_uid;
                    533:                else
                    534:                        uid = 0;
                    535:
                    536:                /* Now write the new-style record. */
                    537:                if (snprintf(filename, sizeof(filename), "%s/%s", _PATH_SKEYDIR,
                    538:                    logname) >= sizeof(filename)) {
1.55      guenther  539:                        warnc(ENAMETOOLONG, "%s", logname);
1.32      millert   540:                        continue;
                    541:                }
                    542:                fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
                    543:                if (fd == -1 || flock(fd, LOCK_EX) != 0 ||
                    544:                    (newfile = fdopen(fd, "r+")) == NULL) {
                    545:                        warn("%s", logname);
                    546:                        continue;
                    547:                }
                    548:                (void)fprintf(newfile, "%s\n%s\n%04d\n%s\n%s\n", logname,
                    549:                            hashtype, n, seed, val);
                    550:                (void)fchown(fileno(newfile), uid, -1);
                    551:                (void)fclose(newfile);
                    552:        }
                    553:        printf("%s has been populated.  NOTE: %s has *not* been removed.\n"
                    554:            "It should be removed once you have verified that the new keys "
                    555:            "work.\n", _PATH_SKEYDIR, _PATH_SKEYKEYS);
1.8       millert   556: }
                    557:
                    558: void
1.36      millert   559: usage(void)
1.8       millert   560: {
1.36      millert   561:        extern char *__progname;
                    562:
1.51      jmc       563:        (void)fprintf(stderr, "usage: %s [-CDErsx] [-a auth-type] [-n count]"
1.53      naddy     564:            "\n\t[-md5 | -rmd160 | -sha1] [user]\n", __progname);
1.8       millert   565:        exit(1);
1.1       deraadt   566: }