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