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