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