Home | History | Annotate | Line # | Download | only in skeyinit
skeyinit.c revision 1.7
      1  1.7      mrg /*	$NetBSD: skeyinit.c,v 1.7 1997/02/11 09:25:24 mrg 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.1  deraadt  * S/KEY initialization and seed update
     12  1.1  deraadt  */
     13  1.1  deraadt 
     14  1.3      cgd #include <sys/param.h>
     15  1.3      cgd #include <sys/time.h>
     16  1.3      cgd #include <sys/resource.h>
     17  1.3      cgd 
     18  1.1  deraadt #include <stdio.h>
     19  1.5      cgd #include <stdlib.h>
     20  1.1  deraadt #include <string.h>
     21  1.6       pk #include <err.h>
     22  1.1  deraadt #include <pwd.h>
     23  1.1  deraadt #include <unistd.h>
     24  1.1  deraadt #include <time.h>
     25  1.6       pk #include <ctype.h>
     26  1.3      cgd 
     27  1.1  deraadt #include "skey.h"
     28  1.1  deraadt 
     29  1.1  deraadt #define NAMELEN 2
     30  1.1  deraadt 
     31  1.1  deraadt int skeylookup __ARGS((struct skey * mp, char *name));
     32  1.1  deraadt 
     33  1.6       pk int
     34  1.1  deraadt main(argc, argv)
     35  1.1  deraadt 	int     argc;
     36  1.1  deraadt 	char   *argv[];
     37  1.1  deraadt {
     38  1.1  deraadt 	int     rval, n, nn, i, defaultsetup, l;
     39  1.1  deraadt 	time_t  now;
     40  1.2  deraadt 	char	hostname[MAXHOSTNAMELEN];
     41  1.1  deraadt 	char    seed[18], tmp[80], key[8], defaultseed[17];
     42  1.1  deraadt 	char    passwd[256], passwd2[256], tbuf[27], buf[60];
     43  1.1  deraadt 	char    lastc, me[80], user[8], *salt, *p, *pw;
     44  1.1  deraadt 	struct skey skey;
     45  1.1  deraadt 	struct passwd *pp;
     46  1.1  deraadt 	struct tm *tm;
     47  1.1  deraadt 
     48  1.1  deraadt 	time(&now);
     49  1.1  deraadt 	tm = localtime(&now);
     50  1.1  deraadt 	strftime(tbuf, sizeof(tbuf), "%M%j", tm);
     51  1.1  deraadt 
     52  1.2  deraadt 	if (gethostname(hostname, sizeof(hostname)) < 0)
     53  1.2  deraadt 		err(1, "gethostname");
     54  1.4  deraadt 	strncpy(defaultseed, hostname, sizeof(defaultseed)- 1);
     55  1.4  deraadt 	defaultseed[4] = '\0';
     56  1.4  deraadt 	strncat(defaultseed, tbuf, sizeof(defaultseed) - 5);
     57  1.1  deraadt 
     58  1.1  deraadt 	if ((pp = getpwuid(getuid())) == NULL)
     59  1.1  deraadt 		err(1, "no user with uid %d", getuid());
     60  1.7      mrg 	(void)strncpy(me, pp->pw_name, sizeof(me) - 1);
     61  1.1  deraadt 
     62  1.1  deraadt 	if ((pp = getpwnam(me)) == NULL)
     63  1.1  deraadt 		err(1, "Who are you?");
     64  1.1  deraadt 
     65  1.1  deraadt 	defaultsetup = 1;
     66  1.1  deraadt 	if (argc > 1) {
     67  1.1  deraadt 		if (strcmp("-s", argv[1]) == 0)
     68  1.1  deraadt 			defaultsetup = 0;
     69  1.1  deraadt 		else
     70  1.1  deraadt 			pp = getpwnam(argv[1]);
     71  1.1  deraadt 
     72  1.1  deraadt 		if (argc > 2)
     73  1.1  deraadt 			pp = getpwnam(argv[2]);
     74  1.1  deraadt 	}
     75  1.1  deraadt 	if (pp == NULL) {
     76  1.1  deraadt 		err(1, "User unknown");
     77  1.1  deraadt 	}
     78  1.1  deraadt 	if (strcmp(pp->pw_name, me) != 0) {
     79  1.1  deraadt 		if (getuid() != 0) {
     80  1.1  deraadt 			/* Only root can change other's passwds */
     81  1.1  deraadt 			printf("Permission denied.\n");
     82  1.1  deraadt 			exit(1);
     83  1.1  deraadt 		}
     84  1.1  deraadt 	}
     85  1.1  deraadt 	salt = pp->pw_passwd;
     86  1.1  deraadt 
     87  1.1  deraadt 	setpriority(PRIO_PROCESS, 0, -4);
     88  1.1  deraadt 
     89  1.1  deraadt 	if (getuid() != 0) {
     90  1.1  deraadt 		setpriority(PRIO_PROCESS, 0, -4);
     91  1.1  deraadt 
     92  1.1  deraadt 		pw = getpass("Password:");
     93  1.1  deraadt 		p = crypt(pw, salt);
     94  1.1  deraadt 
     95  1.1  deraadt 		setpriority(PRIO_PROCESS, 0, 0);
     96  1.1  deraadt 
     97  1.1  deraadt 		if (pp && strcmp(p, pp->pw_passwd)) {
     98  1.1  deraadt 			printf("Password incorrect.\n");
     99  1.1  deraadt 			exit(1);
    100  1.1  deraadt 		}
    101  1.1  deraadt 	}
    102  1.1  deraadt 	rval = skeylookup(&skey, pp->pw_name);
    103  1.1  deraadt 	switch (rval) {
    104  1.1  deraadt 	case -1:
    105  1.1  deraadt 		err(1, "cannot open database");
    106  1.1  deraadt 	case 0:
    107  1.1  deraadt 		printf("[Updating %s]\n", pp->pw_name);
    108  1.1  deraadt 		printf("Old key: %s\n", skey.seed);
    109  1.1  deraadt 
    110  1.1  deraadt 		/*
    111  1.1  deraadt 		 * lets be nice if they have a skey.seed that
    112  1.1  deraadt 		 * ends in 0-8 just add one
    113  1.1  deraadt 		 */
    114  1.1  deraadt 		l = strlen(skey.seed);
    115  1.1  deraadt 		if (l > 0) {
    116  1.1  deraadt 			lastc = skey.seed[l - 1];
    117  1.1  deraadt 			if (isdigit(lastc) && lastc != '9') {
    118  1.7      mrg 				(void)strncpy(defaultseed, skey.seed,
    119  1.7      mrg 				    sizeof(defaultseed) - 1);
    120  1.1  deraadt 				defaultseed[l - 1] = lastc + 1;
    121  1.1  deraadt 			}
    122  1.1  deraadt 			if (isdigit(lastc) && lastc == '9' && l < 16) {
    123  1.7      mrg 				(void)strncpy(defaultseed, skey.seed,
    124  1.7      mrg 				    sizeof(defaultseed) - 1);
    125  1.1  deraadt 				defaultseed[l - 1] = '0';
    126  1.1  deraadt 				defaultseed[l] = '0';
    127  1.1  deraadt 				defaultseed[l + 1] = '\0';
    128  1.1  deraadt 			}
    129  1.1  deraadt 		}
    130  1.1  deraadt 		break;
    131  1.1  deraadt 	case 1:
    132  1.1  deraadt 		printf("[Adding %s]\n", pp->pw_name);
    133  1.1  deraadt 		break;
    134  1.1  deraadt 	}
    135  1.1  deraadt 	n = 99;
    136  1.1  deraadt 
    137  1.1  deraadt 	if (!defaultsetup) {
    138  1.1  deraadt 		printf("You need the 6 english words generated from the \"key\" command.\n");
    139  1.1  deraadt 		for (i = 0;; i++) {
    140  1.1  deraadt 			if (i >= 2)
    141  1.1  deraadt 				exit(1);
    142  1.1  deraadt 			printf("Enter sequence count from 1 to 10000: ");
    143  1.1  deraadt 			fgets(tmp, sizeof(tmp), stdin);
    144  1.1  deraadt 			n = atoi(tmp);
    145  1.1  deraadt 			if (n > 0 && n < 10000)
    146  1.1  deraadt 				break;	/* Valid range */
    147  1.1  deraadt 			printf("\n Error: Count must be > 0 and < 10000\n");
    148  1.1  deraadt 		}
    149  1.1  deraadt 	}
    150  1.1  deraadt 	if (!defaultsetup) {
    151  1.1  deraadt 		printf("Enter new key [default %s]: ", defaultseed);
    152  1.1  deraadt 		fflush(stdout);
    153  1.1  deraadt 		fgets(seed, sizeof(seed), stdin);
    154  1.1  deraadt 		rip(seed);
    155  1.1  deraadt 		if (strlen(seed) > 16) {
    156  1.1  deraadt 			printf("Notice: Seed truncated to 16 characters.\n");
    157  1.1  deraadt 			seed[16] = '\0';
    158  1.1  deraadt 		}
    159  1.1  deraadt 		if (seed[0] == '\0')
    160  1.7      mrg 			(void)strncpy(seed, defaultseed, sizeof(seed) - 1);
    161  1.1  deraadt 
    162  1.1  deraadt 		for (i = 0;; i++) {
    163  1.1  deraadt 			if (i >= 2)
    164  1.1  deraadt 				exit(1);
    165  1.1  deraadt 
    166  1.1  deraadt 			printf("s/key %d %s\ns/key access password: ", n, seed);
    167  1.1  deraadt 			fgets(tmp, sizeof(tmp), stdin);
    168  1.1  deraadt 			rip(tmp);
    169  1.1  deraadt 			backspace(tmp);
    170  1.1  deraadt 
    171  1.1  deraadt 			if (tmp[0] == '?') {
    172  1.1  deraadt 				printf("Enter 6 English words from secure S/Key calculation.\n");
    173  1.1  deraadt 				continue;
    174  1.1  deraadt 			}
    175  1.1  deraadt 			if (tmp[0] == '\0') {
    176  1.1  deraadt 				exit(1);
    177  1.1  deraadt 			}
    178  1.1  deraadt 			if (etob(key, tmp) == 1 || atob8(key, tmp) == 0)
    179  1.1  deraadt 				break;	/* Valid format */
    180  1.1  deraadt 			printf("Invalid format - try again with 6 English words.\n");
    181  1.1  deraadt 		}
    182  1.1  deraadt 	} else {
    183  1.1  deraadt 		/* Get user's secret password */
    184  1.1  deraadt 		for (i = 0;; i++) {
    185  1.1  deraadt 			if (i >= 2)
    186  1.1  deraadt 				exit(1);
    187  1.1  deraadt 
    188  1.1  deraadt 			printf("Enter secret password: ");
    189  1.1  deraadt 			readpass(passwd, sizeof(passwd));
    190  1.1  deraadt 			if (passwd[0] == '\0')
    191  1.1  deraadt 				exit(1);
    192  1.1  deraadt 
    193  1.1  deraadt 			printf("Again secret password: ");
    194  1.1  deraadt 			readpass(passwd2, sizeof(passwd));
    195  1.1  deraadt 			if (passwd2[0] == '\0')
    196  1.1  deraadt 				exit(1);
    197  1.1  deraadt 
    198  1.1  deraadt 			if (strlen(passwd) < 4 && strlen(passwd2) < 4)
    199  1.1  deraadt 				err(1, "Your password must be longer");
    200  1.1  deraadt 			if (strcmp(passwd, passwd2) == 0)
    201  1.1  deraadt 				break;
    202  1.1  deraadt 
    203  1.1  deraadt 			printf("Passwords do not match.\n");
    204  1.1  deraadt 		}
    205  1.7      mrg 		(void)strncpy(seed, defaultseed, sizeof(seed) - 1);
    206  1.1  deraadt 
    207  1.1  deraadt 		/* Crunch seed and password into starting key */
    208  1.1  deraadt 		if (keycrunch(key, seed, passwd) != 0)
    209  1.1  deraadt 			err(2, "key crunch failed");
    210  1.1  deraadt 		nn = n;
    211  1.1  deraadt 		while (nn-- != 0)
    212  1.1  deraadt 			f(key);
    213  1.1  deraadt 	}
    214  1.1  deraadt 	time(&now);
    215  1.1  deraadt 	tm = localtime(&now);
    216  1.1  deraadt 	strftime(tbuf, sizeof(tbuf), " %b %d,%Y %T", tm);
    217  1.1  deraadt 
    218  1.1  deraadt 	skey.val = (char *)malloc(16 + 1);
    219  1.1  deraadt 
    220  1.1  deraadt 	btoa8(skey.val, key);
    221  1.1  deraadt 
    222  1.1  deraadt 	fprintf(skey.keyfile, "%s %04d %-16s %s %-21s\n", pp->pw_name, n,
    223  1.1  deraadt 	    seed, skey.val, tbuf);
    224  1.1  deraadt 	fclose(skey.keyfile);
    225  1.1  deraadt 	printf("ID %s s/key is %d %s\n", pp->pw_name, n, seed);
    226  1.1  deraadt 	printf("Next login password: %s\n", btoe(buf, key));
    227  1.1  deraadt #ifdef HEXIN
    228  1.1  deraadt 	printf("%s\n", put8(buf, key));
    229  1.1  deraadt #endif
    230  1.1  deraadt 
    231  1.1  deraadt 	exit(1);
    232  1.1  deraadt }
    233