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