Home | History | Annotate | Line # | Download | only in skeyinit
skeyinit.c revision 1.17
      1  1.17     itohy /*	$NetBSD: skeyinit.c,v 1.17 2002/04/23 06:10:42 itohy Exp $	*/
      2   1.5       cgd 
      3   1.1   deraadt /* S/KEY v1.1b (skeyinit.c)
      4   1.1   deraadt  *
      5   1.1   deraadt  * Authors:
      6   1.1   deraadt  *          Neil M. Haller <nmh (at) thumper.bellcore.com>
      7   1.1   deraadt  *          Philip R. Karn <karn (at) chicago.qualcomm.com>
      8   1.1   deraadt  *          John S. Walden <jsw (at) thumper.bellcore.com>
      9   1.1   deraadt  *          Scott Chasin <chasin (at) crimelab.com>
     10   1.1   deraadt  *
     11  1.13       mjl  * Modifications:
     12  1.13       mjl  *          Todd C. Miller <Todd.Miller (at) courtesan.com>
     13  1.13       mjl  *
     14   1.1   deraadt  * S/KEY initialization and seed update
     15   1.1   deraadt  */
     16   1.1   deraadt 
     17   1.3       cgd #include <sys/param.h>
     18   1.3       cgd #include <sys/time.h>
     19   1.3       cgd #include <sys/resource.h>
     20   1.3       cgd 
     21  1.13       mjl #include <ctype.h>
     22  1.13       mjl #include <err.h>
     23  1.13       mjl #include <errno.h>
     24  1.13       mjl #include <fcntl.h>
     25  1.17     itohy #include <paths.h>
     26  1.13       mjl #include <pwd.h>
     27   1.1   deraadt #include <stdio.h>
     28   1.5       cgd #include <stdlib.h>
     29   1.1   deraadt #include <string.h>
     30  1.13       mjl #include <time.h>
     31   1.1   deraadt #include <unistd.h>
     32  1.13       mjl #include <utmp.h>
     33   1.3       cgd 
     34  1.13       mjl #include <skey.h>
     35   1.1   deraadt 
     36  1.13       mjl #ifndef SKEY_NAMELEN
     37  1.13       mjl #define SKEY_NAMELEN    4
     38  1.13       mjl #endif
     39   1.1   deraadt 
     40   1.8     lukem int main __P((int, char **));
     41   1.1   deraadt 
     42  1.13       mjl int main(int argc, char **argv)
     43   1.1   deraadt {
     44  1.13       mjl 	int     rval, nn, i, l;
     45  1.13       mjl 	int n = 0, defaultsetup = 1, zerokey = 0, hexmode = 0;
     46   1.1   deraadt 	time_t  now;
     47   1.9       mrg 	char	hostname[MAXHOSTNAMELEN + 1];
     48  1.13       mjl 	char    seed[SKEY_MAX_PW_LEN+2], key[SKEY_BINKEY_SIZE], defaultseed[SKEY_MAX_SEED_LEN+1];
     49  1.13       mjl 	char    passwd[SKEY_MAX_PW_LEN+2], passwd2[SKEY_MAX_PW_LEN+2], tbuf[27], buf[80];
     50  1.13       mjl 	char    lastc, me[UT_NAMESIZE+1], *p, *pw, *ht = NULL;
     51  1.11   mycroft 	const	char *salt;
     52  1.11   mycroft 	struct	skey skey;
     53  1.11   mycroft 	struct	passwd *pp;
     54  1.11   mycroft 	struct	tm *tm;
     55  1.13       mjl 	int c;
     56  1.17     itohy 
     57  1.17     itohy 	/*
     58  1.17     itohy 	 * Make sure using stdin/stdout/stderr is safe
     59  1.17     itohy 	 * after opening any file.
     60  1.17     itohy 	 */
     61  1.17     itohy 	i = open(_PATH_DEVNULL, O_RDWR);
     62  1.17     itohy 	while (i >= 0 && i < 2)
     63  1.17     itohy 		i = dup(i);
     64  1.17     itohy 	if (i > 2)
     65  1.17     itohy 		close(i);
     66   1.1   deraadt 
     67  1.13       mjl 	if (geteuid() != 0)
     68  1.13       mjl 		errx(1, "must be setuid root.");
     69   1.1   deraadt 
     70   1.2   deraadt 	if (gethostname(hostname, sizeof(hostname)) < 0)
     71   1.2   deraadt 		err(1, "gethostname");
     72  1.14   thorpej 
     73  1.14   thorpej 	/*
     74  1.14   thorpej 	 * Copy the hostname into the default seed, eliminating any
     75  1.14   thorpej 	 * non alpha-numeric characters.
     76  1.14   thorpej 	 */
     77  1.14   thorpej 	for (i = 0, l = 0; l < sizeof(defaultseed); i++) {
     78  1.14   thorpej 		if (hostname[i] == '\0') {
     79  1.14   thorpej 			defaultseed[l] = hostname[i];
     80  1.14   thorpej 			break;
     81  1.14   thorpej 		}
     82  1.14   thorpej 		if (isalnum(hostname[i]))
     83  1.14   thorpej 			defaultseed[l++] = hostname[i];
     84  1.14   thorpej 	}
     85  1.14   thorpej 
     86  1.13       mjl 	defaultseed[SKEY_NAMELEN] = '\0';
     87  1.13       mjl 	(void)time(&now);
     88  1.13       mjl 	(void)sprintf(tbuf, "%05ld", (long) (now % 100000));
     89  1.13       mjl 	(void)strncat(defaultseed, tbuf, sizeof(defaultseed) - 5);
     90   1.1   deraadt 
     91   1.1   deraadt 	if ((pp = getpwuid(getuid())) == NULL)
     92  1.12  christos 		err(1, "no user with uid %ld", (u_long)getuid());
     93   1.7       mrg 	(void)strncpy(me, pp->pw_name, sizeof(me) - 1);
     94  1.15  christos 	me[sizeof(me) - 1] = '\0';
     95   1.1   deraadt 
     96   1.1   deraadt 	if ((pp = getpwnam(me)) == NULL)
     97   1.1   deraadt 		err(1, "Who are you?");
     98  1.13       mjl 	salt = pp->pw_passwd;
     99   1.1   deraadt 
    100  1.13       mjl 	while((c = getopt(argc, argv, "n:t:sxz")) != -1) {
    101  1.13       mjl 		switch(c) {
    102  1.13       mjl 			case 'n':
    103  1.13       mjl 				n = atoi(optarg);
    104  1.13       mjl 				if(n < 1 || n > SKEY_MAX_SEQ)
    105  1.13       mjl 					errx(1, "count must be between 1 and %d", SKEY_MAX_SEQ);
    106  1.13       mjl 				break;
    107  1.13       mjl 			case 't':
    108  1.13       mjl 				if(skey_set_algorithm(optarg) == NULL)
    109  1.13       mjl 					errx(1, "Unknown hash algorithm %s", optarg);
    110  1.13       mjl 				ht = optarg;
    111  1.13       mjl 				break;
    112  1.13       mjl 			case 's':
    113  1.13       mjl 				defaultsetup = 0;
    114  1.13       mjl 				break;
    115  1.13       mjl 			case 'x':
    116  1.13       mjl 				hexmode = 1;
    117  1.13       mjl 				break;
    118  1.13       mjl 			case 'z':
    119  1.13       mjl 				zerokey = 1;
    120  1.13       mjl 				break;
    121  1.13       mjl 			default:
    122  1.13       mjl 				err(1, "Usage: %s [-n count] [-t md4|md5|sha1] [-s] [-x] [-z] [user]\n", argv[0]);
    123  1.13       mjl 		}
    124  1.13       mjl 	}
    125  1.13       mjl 
    126  1.13       mjl 	if(argc > optind) {
    127  1.13       mjl 		pp = getpwnam(argv[optind]);
    128  1.13       mjl 		if (pp == NULL)
    129  1.13       mjl 			errx(1, "User %s unknown", argv[optind]);
    130  1.13       mjl 		}
    131   1.1   deraadt 
    132   1.1   deraadt 	if (strcmp(pp->pw_name, me) != 0) {
    133   1.1   deraadt 		if (getuid() != 0) {
    134   1.1   deraadt 			/* Only root can change other's passwds */
    135  1.13       mjl 			errx(1, "Permission denied.");
    136   1.1   deraadt 		}
    137   1.1   deraadt 	}
    138   1.1   deraadt 
    139   1.1   deraadt 	if (getuid() != 0) {
    140   1.1   deraadt 		pw = getpass("Password:");
    141   1.1   deraadt 		p = crypt(pw, salt);
    142   1.1   deraadt 
    143  1.13       mjl 		if (strcmp(p, pp->pw_passwd)) {
    144  1.13       mjl 			errx(1, "Password incorrect.");
    145   1.1   deraadt 		}
    146   1.1   deraadt 	}
    147  1.13       mjl 
    148   1.1   deraadt 	rval = skeylookup(&skey, pp->pw_name);
    149   1.1   deraadt 	switch (rval) {
    150   1.1   deraadt 	case -1:
    151   1.1   deraadt 		err(1, "cannot open database");
    152   1.1   deraadt 	case 0:
    153  1.13       mjl 		/* comment out user if asked to */
    154  1.13       mjl 		if (zerokey)
    155  1.13       mjl 			exit(skeyzero(&skey, pp->pw_name));
    156  1.13       mjl 
    157   1.1   deraadt 		printf("[Updating %s]\n", pp->pw_name);
    158  1.13       mjl 		printf("Old key: [%s] %s\n", skey_get_algorithm(), skey.seed);
    159   1.1   deraadt 
    160   1.1   deraadt 		/*
    161   1.1   deraadt 		 * lets be nice if they have a skey.seed that
    162   1.1   deraadt 		 * ends in 0-8 just add one
    163   1.1   deraadt 		 */
    164   1.1   deraadt 		l = strlen(skey.seed);
    165   1.1   deraadt 		if (l > 0) {
    166   1.1   deraadt 			lastc = skey.seed[l - 1];
    167  1.12  christos 			if (isdigit((unsigned char)lastc) && lastc != '9') {
    168   1.7       mrg 				(void)strncpy(defaultseed, skey.seed,
    169   1.7       mrg 				    sizeof(defaultseed) - 1);
    170   1.1   deraadt 				defaultseed[l - 1] = lastc + 1;
    171   1.1   deraadt 			}
    172  1.12  christos 			if (isdigit((unsigned char)lastc) && lastc == '9' &&
    173  1.12  christos 			    l < 16) {
    174   1.7       mrg 				(void)strncpy(defaultseed, skey.seed,
    175   1.7       mrg 				    sizeof(defaultseed) - 1);
    176   1.1   deraadt 				defaultseed[l - 1] = '0';
    177   1.1   deraadt 				defaultseed[l] = '0';
    178   1.1   deraadt 				defaultseed[l + 1] = '\0';
    179   1.1   deraadt 			}
    180   1.1   deraadt 		}
    181   1.1   deraadt 		break;
    182   1.1   deraadt 	case 1:
    183  1.13       mjl 		if (zerokey)
    184  1.13       mjl 			errx(1, "You have no entry to zero.");
    185   1.1   deraadt 		printf("[Adding %s]\n", pp->pw_name);
    186   1.1   deraadt 		break;
    187   1.1   deraadt 	}
    188  1.13       mjl 
    189  1.13       mjl 	if(n==0)
    190  1.13       mjl 		n = 99;
    191  1.13       mjl 
    192  1.13       mjl 	/* Set hash type if asked to */
    193  1.13       mjl 	if (ht) {
    194  1.13       mjl 		/* Need to zero out old key when changing algorithm */
    195  1.13       mjl 		if (strcmp(ht, skey_get_algorithm()) && skey_set_algorithm(ht))
    196  1.13       mjl 			zerokey = 1;
    197  1.13       mjl 	}
    198   1.1   deraadt 
    199   1.1   deraadt 	if (!defaultsetup) {
    200  1.13       mjl 		printf("You need the 6 english words generated from the \"skey\" command.\n");
    201   1.1   deraadt 		for (i = 0;; i++) {
    202   1.1   deraadt 			if (i >= 2)
    203   1.1   deraadt 				exit(1);
    204  1.13       mjl 			printf("Enter sequence count from 1 to %d: ", SKEY_MAX_SEQ);
    205  1.13       mjl 			fgets(buf, sizeof(buf), stdin);
    206  1.13       mjl 			n = atoi(buf);
    207  1.13       mjl 			if (n > 0 && n < SKEY_MAX_SEQ)
    208   1.1   deraadt 				break;	/* Valid range */
    209  1.13       mjl 			printf("\nError: Count must be between 0 and %d\n", SKEY_MAX_SEQ);
    210  1.13       mjl 		}
    211  1.13       mjl 
    212  1.13       mjl 		for (i = 0;; i++) {
    213  1.13       mjl 			if (i >= 2)
    214  1.13       mjl 				exit(1);
    215  1.13       mjl 
    216  1.13       mjl 			printf("Enter new seed [default %s]: ", defaultseed);
    217  1.13       mjl 			fflush(stdout);
    218  1.13       mjl 			fgets(seed, sizeof(seed), stdin);
    219  1.13       mjl 			rip(seed);
    220  1.13       mjl 			for (p = seed; *p; p++) {
    221  1.13       mjl 				if (isalpha(*p)) {
    222  1.13       mjl 					if (isupper(*p))
    223  1.13       mjl 						*p = tolower(*p);
    224  1.13       mjl 				} else if (!isdigit(*p)) {
    225  1.13       mjl 					(void)puts("Error: seed may only contain alphanumeric characters");
    226  1.13       mjl 					break;
    227  1.13       mjl 				}
    228  1.13       mjl 			}
    229  1.13       mjl 			if (*p == '\0')
    230  1.13       mjl 				break;  /* Valid seed */
    231   1.1   deraadt 		}
    232  1.13       mjl 		if (strlen(seed) > SKEY_MAX_SEED_LEN) {
    233  1.13       mjl 			printf("Notice: Seed truncated to %d characters.\n", SKEY_MAX_SEED_LEN);
    234  1.13       mjl 			seed[SKEY_MAX_SEED_LEN] = '\0';
    235   1.1   deraadt 		}
    236   1.1   deraadt 		if (seed[0] == '\0')
    237  1.13       mjl 			(void)strcpy(seed, defaultseed);
    238   1.1   deraadt 
    239   1.1   deraadt 		for (i = 0;; i++) {
    240   1.1   deraadt 			if (i >= 2)
    241   1.1   deraadt 				exit(1);
    242   1.1   deraadt 
    243  1.13       mjl 			printf("otp-%s %d %s\ns/key access password: ",
    244  1.13       mjl 				skey_get_algorithm(), n, seed);
    245  1.13       mjl 			fgets(buf, sizeof(buf), stdin);
    246  1.13       mjl 			rip(buf);
    247  1.13       mjl 			backspace(buf);
    248   1.1   deraadt 
    249  1.13       mjl 			if (buf[0] == '?') {
    250  1.13       mjl 				puts("Enter 6 English words from secure S/Key calculation.");
    251   1.1   deraadt 				continue;
    252  1.13       mjl 			} else if (buf[0] == '\0') {
    253   1.1   deraadt 				exit(1);
    254   1.1   deraadt 			}
    255  1.13       mjl 			if (etob(key, buf) == 1 || atob8(key, buf) == 0)
    256   1.1   deraadt 				break;	/* Valid format */
    257  1.13       mjl 			(void)puts("Invalid format - try again with 6 English words.");
    258   1.1   deraadt 		}
    259   1.1   deraadt 	} else {
    260  1.13       mjl 	/* Get user's secret password */
    261  1.13       mjl 	puts("Reminder - Only use this method if you are directly connected\n"
    262  1.13       mjl 	      "           or have an encrypted channel. If you are using telnet\n"
    263  1.13       mjl 	      "           or rlogin, exit with no password and use skeyinit -s.\n");
    264  1.13       mjl 
    265  1.13       mjl 	for (i = 0;; i++) {
    266   1.1   deraadt 			if (i >= 2)
    267   1.1   deraadt 				exit(1);
    268   1.1   deraadt 
    269   1.1   deraadt 			printf("Enter secret password: ");
    270   1.1   deraadt 			readpass(passwd, sizeof(passwd));
    271   1.1   deraadt 			if (passwd[0] == '\0')
    272   1.1   deraadt 				exit(1);
    273   1.1   deraadt 
    274  1.13       mjl 			if (strlen(passwd) < SKEY_MIN_PW_LEN) {
    275  1.13       mjl 				(void)fprintf(stderr,
    276  1.13       mjl 				    "Your password must be at least %d characters long.\n", SKEY_MIN_PW_LEN);
    277  1.13       mjl 				continue;
    278  1.13       mjl 			} else if (strcmp(passwd, pp->pw_name) == 0) {
    279  1.13       mjl 				(void)fputs("Your password may not be the same as your user name.\n", stderr);
    280  1.13       mjl 				continue;
    281  1.13       mjl 			}
    282  1.13       mjl #if 0
    283  1.13       mjl 			else if (strspn(passwd, "abcdefghijklmnopqrstuvwxyz") == strlen(passwd)) {
    284  1.13       mjl 				(void)fputs("Your password must contain more than just lower case letters.\n"
    285  1.13       mjl 					    "Whitespace, numbers, and puctuation are suggested.\n", stderr);
    286  1.13       mjl 				continue;
    287  1.13       mjl 			}
    288  1.13       mjl #endif
    289   1.1   deraadt 			printf("Again secret password: ");
    290   1.1   deraadt 			readpass(passwd2, sizeof(passwd));
    291   1.1   deraadt 			if (passwd2[0] == '\0')
    292   1.1   deraadt 				exit(1);
    293   1.1   deraadt 
    294   1.1   deraadt 			if (strcmp(passwd, passwd2) == 0)
    295   1.1   deraadt 				break;
    296   1.1   deraadt 
    297  1.13       mjl 			puts("Passwords do not match.");
    298   1.1   deraadt 		}
    299   1.1   deraadt 
    300   1.1   deraadt 		/* Crunch seed and password into starting key */
    301  1.13       mjl 		(void)strcpy(seed, defaultseed);
    302   1.1   deraadt 		if (keycrunch(key, seed, passwd) != 0)
    303   1.1   deraadt 			err(2, "key crunch failed");
    304   1.1   deraadt 		nn = n;
    305   1.1   deraadt 		while (nn-- != 0)
    306   1.1   deraadt 			f(key);
    307   1.1   deraadt 	}
    308  1.13       mjl 	(void)time(&now);
    309   1.1   deraadt 	tm = localtime(&now);
    310  1.13       mjl 	(void)strftime(tbuf, sizeof(tbuf), " %b %d,%Y %T", tm);
    311   1.1   deraadt 
    312  1.13       mjl 	if ((skey.val = (char *)malloc(16 + 1)) == NULL)
    313  1.13       mjl 		err(1, "Can't allocate memory");
    314  1.13       mjl 
    315  1.16       wiz 	/* Zero out old key if necessary (entry would change size) */
    316  1.13       mjl 	if (zerokey) {
    317  1.13       mjl 		(void)skeyzero(&skey, pp->pw_name);
    318  1.13       mjl 		/* Re-open keys file and seek to the end */
    319  1.13       mjl 		if (skeylookup(&skey, pp->pw_name) == -1)
    320  1.13       mjl 			err(1, "cannot open database");
    321  1.13       mjl 	}
    322   1.1   deraadt 
    323   1.1   deraadt 	btoa8(skey.val, key);
    324   1.1   deraadt 
    325  1.13       mjl 	/*
    326  1.13       mjl 	 * Obtain an exclusive lock on the key file so we don't
    327  1.13       mjl 	 * clobber someone authenticating themselves at the same time.
    328  1.13       mjl 	 */
    329  1.13       mjl 	for (i = 0; i < 300; i++) {
    330  1.13       mjl 		if ((rval = flock(fileno(skey.keyfile), LOCK_EX|LOCK_NB)) == 0
    331  1.13       mjl 		    || errno != EWOULDBLOCK)
    332  1.13       mjl 			break;
    333  1.13       mjl 		usleep(100000);			/* Sleep for 0.1 seconds */
    334  1.13       mjl 	}
    335  1.13       mjl 	if (rval == -1)	{			/* Can't get exclusive lock */
    336  1.13       mjl 		errno = EAGAIN;
    337  1.13       mjl 		err(1, "cannot open database");
    338  1.13       mjl 	}
    339  1.13       mjl 
    340  1.13       mjl 	/* Don't save algorithm type for md4 (keep record length same) */
    341  1.13       mjl 	if (strcmp(skey_get_algorithm(), "md4") == 0)
    342  1.13       mjl 		(void)fprintf(skey.keyfile, "%s %04d %-16s %s %-21s\n",
    343  1.13       mjl 		    pp->pw_name, n, seed, skey.val, tbuf);
    344  1.13       mjl 	else
    345  1.13       mjl 		(void)fprintf(skey.keyfile, "%s %s %04d %-16s %s %-21s\n",
    346  1.13       mjl 		    pp->pw_name, skey_get_algorithm(), n, seed, skey.val, tbuf);
    347  1.13       mjl 
    348  1.13       mjl 	(void)fclose(skey.keyfile);
    349  1.13       mjl 
    350  1.13       mjl 	(void)printf("\nID %s skey is otp-%s %d %s\n", pp->pw_name,
    351  1.13       mjl 		     skey_get_algorithm(), n, seed);
    352  1.13       mjl 	(void)printf("Next login password: %s\n\n",
    353  1.13       mjl 		     hexmode ? put8(buf, key) : btoe(buf, key));
    354   1.1   deraadt 
    355  1.13       mjl 	return(0);
    356   1.1   deraadt }
    357