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