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