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