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

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