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