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