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

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