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