Annotation of src/usr.bin/skeyinit/skeyinit.c, Revision 1.53
1.53 ! naddy 1: /* $OpenBSD: skeyinit.c,v 1.52 2013/11/28 18:24:55 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.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"
187: " \"skeyinit -s -a krb5\" or \"skeyinit -s -a passwd\"\n\n"
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)
223: >= sizeof(filename)) {
224: errno = ENAMETOOLONG;
225: err(1, "Cannot remove S/Key entry");
226: }
227: if (unlink(filename) != 0)
228: err(1, "Cannot remove S/Key entry");
229: printf("S/Key entry for %s removed.\n",
230: pp->pw_name);
231: exit(0);
232: }
1.4 millert 233:
1.28 millert 234: (void)printf("[Updating %s with %s]\n", pp->pw_name,
235: ht ? ht : skey_get_algorithm());
236: (void)printf("Old seed: [%s] %s\n",
237: skey_get_algorithm(), skey.seed);
1.4 millert 238:
239: /*
1.25 millert 240: * Sanity check old seed.
241: */
242: l = strlen(skey.seed);
243: for (p = skey.seed; *p; p++) {
1.52 deraadt 244: if (isalpha((unsigned char)*p)) {
245: if (isupper((unsigned char)*p))
246: *p = tolower((unsigned char)*p);
247: } else if (!isdigit((unsigned char)*p)) {
1.25 millert 248: memmove(p, p + 1, l - (p - skey.seed));
249: l--;
250: }
251: }
252:
1.28 millert 253: /* If the seed ends in 0-8 just add one. */
1.4 millert 254: if (l > 0) {
255: lastc = skey.seed[l - 1];
1.52 deraadt 256: if (isdigit((unsigned char)lastc) &&
257: lastc != '9') {
1.43 deraadt 258: (void)strlcpy(seed, skey.seed,
259: sizeof seed);
1.38 millert 260: seed[l - 1] = lastc + 1;
1.4 millert 261: }
1.52 deraadt 262: if (isdigit((unsigned char)lastc) &&
263: lastc == '9' && l < 16) {
1.43 deraadt 264: (void)strlcpy(seed, skey.seed,
265: sizeof seed);
1.38 millert 266: seed[l - 1] = '0';
267: seed[l] = '0';
268: seed[l + 1] = '\0';
1.4 millert 269: }
1.1 deraadt 270: }
1.4 millert 271: break;
272: case 1:
1.35 millert 273: if (rmkey)
274: errx(1, "You have no entry to remove.");
1.28 millert 275: (void)printf("[Adding %s with %s]\n", pp->pw_name,
276: ht ? ht : skey_get_algorithm());
1.32 millert 277: if (snprintf(filename, sizeof(filename), "%s/%s",
278: _PATH_SKEYDIR, pp->pw_name) >= sizeof(filename)) {
279: errno = ENAMETOOLONG;
280: err(1, "Cannot create S/Key entry");
281: }
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.28 millert 308: if ((skey.val = (char *)malloc(16 + 1)) == NULL)
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.28 millert 329: int i, n;
1.4 millert 330:
1.28 millert 331: (void)puts("You need the 6 words generated from the \"skey\" command.");
332: for (i = 0; ; i++) {
333: if (i >= 2)
334: exit(1);
335:
336: (void)printf("Enter sequence count from 1 to %d: ",
337: SKEY_MAX_SEQ);
338: (void)fgets(buf, bufsiz, stdin);
339: clearerr(stdin);
340: n = atoi(buf);
341: if (n > 0 && n < SKEY_MAX_SEQ)
342: break; /* Valid range */
343: (void)fprintf(stderr, "ERROR: Count must be between 1 and %d\n",
344: SKEY_MAX_SEQ);
345: }
1.1 deraadt 346:
1.28 millert 347: for (i = 0; ; i++) {
348: if (i >= 2)
349: exit(1);
350:
1.38 millert 351: (void)printf("Enter new seed [default %s]: ", seed);
352: (void)fgets(newseed, sizeof(newseed), stdin); /* XXX */
1.28 millert 353: clearerr(stdin);
1.38 millert 354: rip(newseed);
355: if (strlen(newseed) > SKEY_MAX_SEED_LEN) {
1.28 millert 356: (void)fprintf(stderr, "ERROR: Seed must be between 1 "
357: "and %d characters in length\n", SKEY_MAX_SEED_LEN);
358: continue;
359: }
1.38 millert 360: for (p = newseed; *p; p++) {
1.52 deraadt 361: if (isspace((unsigned char)*p)) {
1.28 millert 362: (void)fputs("ERROR: Seed must not contain "
363: "any spaces\n", stderr);
1.1 deraadt 364: break;
1.52 deraadt 365: } else if (isalpha((unsigned char)*p)) {
366: if (isupper((unsigned char)*p))
367: *p = tolower((unsigned char)*p);
368: } else if (!isdigit((unsigned char)*p)) {
1.28 millert 369: (void)fputs("ERROR: Seed must be purely "
370: "alphanumeric\n", stderr);
371: break;
372: }
1.1 deraadt 373: }
1.28 millert 374: if (*p == '\0')
375: break; /* Valid seed */
376: }
1.38 millert 377: if (newseed[0] != '\0')
1.44 deraadt 378: (void)strlcpy(seed, newseed, seedlen);
1.1 deraadt 379:
1.28 millert 380: for (i = 0; ; i++) {
381: if (i >= 2)
382: exit(1);
383:
384: (void)printf("otp-%s %d %s\nS/Key access password: ",
385: skey_get_algorithm(), n, seed);
386: (void)fgets(buf, bufsiz, stdin);
387: clearerr(stdin);
388: rip(buf);
389: backspace(buf);
390:
391: if (buf[0] == '?') {
392: (void)puts("Enter 6 words from secure S/Key calculation.");
393: continue;
394: } else if (buf[0] == '\0')
395: exit(1);
396:
397: if (etob(key, buf) == 1 || atob8(key, buf) == 0)
398: break; /* Valid format */
399: (void)fputs("ERROR: Invalid format - try again with the 6 words.\n",
400: stderr);
1.1 deraadt 401: }
1.28 millert 402: *count= n;
403: }
1.1 deraadt 404:
1.28 millert 405: void
1.38 millert 406: normal_mode(char *username, int n, char *key, char *seed)
1.28 millert 407: {
408: int i, nn;
1.38 millert 409: char passwd[SKEY_MAX_PW_LEN+2], key2[SKEY_BINKEY_SIZE];
1.1 deraadt 410:
1.28 millert 411: /* Get user's secret passphrase */
412: for (i = 0; ; i++) {
413: if (i > 2)
1.38 millert 414: errx(1, "S/Key entry not updated");
1.28 millert 415:
1.46 otto 416: if (readpassphrase("Enter new secret passphrase: ", passwd,
1.28 millert 417: sizeof(passwd), 0) == NULL || passwd[0] == '\0')
418: exit(1);
419:
420: if (strlen(passwd) < SKEY_MIN_PW_LEN) {
421: (void)fprintf(stderr,
422: "ERROR: Your passphrase must be at least %d "
423: "characters long.\n", SKEY_MIN_PW_LEN);
424: continue;
425: } else if (strcmp(passwd, username) == 0) {
426: (void)fputs("ERROR: Your passphrase may not be the "
427: "same as your user name.\n", stderr);
428: continue;
1.50 deraadt 429: } else if (strspn(passwd, "abcdefghijklmnopqrstuvwxyz") ==
1.28 millert 430: strlen(passwd)) {
431: (void)fputs("ERROR: Your passphrase must contain more "
432: "than just lower case letters.\nWhitespace, "
1.37 aaron 433: "numbers, and punctuation are suggested.\n",
434: stderr);
1.28 millert 435: continue;
436: } else if (strlen(passwd) > 63) {
437: (void)fprintf(stderr, "WARNING: Your passphrase is "
438: "longer than the recommended maximum length of 63\n");
439: }
440: /* XXX - should check for passphrase that is really too long */
1.13 millert 441:
1.38 millert 442: /* Crunch seed and passphrase into starting key */
443: nn = keycrunch(key, seed, passwd);
444: memset(passwd, 0, sizeof(passwd));
445: if (nn != 0)
446: err(2, "key crunch failed");
447:
448: if (readpassphrase("Again secret passphrase: ", passwd,
449: sizeof(passwd), 0) == NULL || passwd[0] == '\0')
450: exit(1);
451:
452: /* Crunch seed and passphrase into starting key */
453: nn = keycrunch(key2, seed, passwd);
454: memset(passwd, 0, sizeof(passwd));
455: if (nn != 0)
456: err(2, "key crunch failed");
457:
458: if (memcmp(key, key2, sizeof(key2)) == 0)
1.28 millert 459: break;
1.1 deraadt 460:
1.28 millert 461: (void)fputs("Passphrases do not match.\n", stderr);
1.20 millert 462: }
1.28 millert 463:
464: nn = n;
465: while (nn-- != 0)
466: f(key);
467: }
468:
1.32 millert 469: void
1.36 millert 470: enable_db(int op)
1.32 millert 471: {
472: if (op == 1) {
473: /* enable */
474: if (mkdir(_PATH_SKEYDIR, 01730) != 0 && errno != EEXIST)
475: err(1, "can't mkdir %s", _PATH_SKEYDIR);
1.33 millert 476: if (chown(_PATH_SKEYDIR, geteuid(), getegid()) != 0)
477: err(1, "can't chown %s", _PATH_SKEYDIR);
1.32 millert 478: if (chmod(_PATH_SKEYDIR, 01730) != 0)
479: err(1, "can't chmod %s", _PATH_SKEYDIR);
480: } else {
481: /* disable */
482: if (chmod(_PATH_SKEYDIR, 0) != 0 && errno != ENOENT)
483: err(1, "can't chmod %s", _PATH_SKEYDIR);
484: }
485: }
486:
487: #define _PATH_SKEYKEYS "/etc/skeykeys"
488: void
489: convert_db(void)
490: {
491: struct passwd *pp;
492: uid_t uid;
493: FILE *keyfile;
494: FILE *newfile;
495: char buf[256], *logname, *hashtype, *seed, *val, *cp;
496: char filename[PATH_MAX];
497: int fd, n;
498:
499: if ((keyfile = fopen(_PATH_SKEYKEYS, "r")) == NULL)
500: err(1, "can't open %s", _PATH_SKEYKEYS);
501: if (flock(fileno(keyfile), LOCK_EX) != 0)
502: err(1, "can't lock %s", _PATH_SKEYKEYS);
1.34 millert 503: enable_db(1);
1.32 millert 504:
505: /*
506: * Loop over each entry in _PATH_SKEYKEYS, creating a file
507: * in _PATH_SKEYDIR for each one.
508: */
509: while (fgets(buf, sizeof(buf), keyfile) != NULL) {
510: if (buf[0] == '#')
511: continue;
512: if ((logname = strtok(buf, " \t")) == NULL)
513: continue;
514: if ((cp = strtok(NULL, " \t")) == NULL)
515: continue;
1.53 ! naddy 516: if (!isalpha((unsigned char)*cp))
! 517: continue;
! 518: hashtype = cp;
! 519: if ((cp = strtok(NULL, " \t")) == NULL)
! 520: continue;
1.32 millert 521: n = atoi(cp);
522: if ((seed = strtok(NULL, " \t")) == NULL)
523: continue;
524: if ((val = strtok(NULL, " \t")) == NULL)
525: continue;
526:
527: if ((pp = getpwnam(logname)) != NULL)
528: uid = pp->pw_uid;
529: else
530: uid = 0;
531:
532: /* Now write the new-style record. */
533: if (snprintf(filename, sizeof(filename), "%s/%s", _PATH_SKEYDIR,
534: logname) >= sizeof(filename)) {
535: errno = ENAMETOOLONG;
536: warn("%s", logname);
537: continue;
538: }
539: fd = open(filename, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
540: if (fd == -1 || flock(fd, LOCK_EX) != 0 ||
541: (newfile = fdopen(fd, "r+")) == NULL) {
542: warn("%s", logname);
543: continue;
544: }
545: (void)fprintf(newfile, "%s\n%s\n%04d\n%s\n%s\n", logname,
546: hashtype, n, seed, val);
547: (void)fchown(fileno(newfile), uid, -1);
548: (void)fclose(newfile);
549: }
550: printf("%s has been populated. NOTE: %s has *not* been removed.\n"
551: "It should be removed once you have verified that the new keys "
552: "work.\n", _PATH_SKEYDIR, _PATH_SKEYKEYS);
1.8 millert 553: }
554:
555: void
1.36 millert 556: usage(void)
1.8 millert 557: {
1.36 millert 558: extern char *__progname;
559:
1.51 jmc 560: (void)fprintf(stderr, "usage: %s [-CDErsx] [-a auth-type] [-n count]"
1.53 ! naddy 561: "\n\t[-md5 | -rmd160 | -sha1] [user]\n", __progname);
1.8 millert 562: exit(1);
1.1 deraadt 563: }