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
|
/* $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>
*
* S/KEY initialization and seed update
*/
#include <sys/param.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <pwd.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
#include "skey.h"
#define NAMELEN 2
int skeylookup __ARGS((struct skey * mp, char *name));
int skeyzero __ARGS((struct skey * mp, char *name));
int
main(argc, argv)
int argc;
char *argv[];
{
int rval, n, nn, i, defaultsetup, l, zerokey = 0;
time_t now;
char hostname[MAXHOSTNAMELEN];
char seed[18], tmp[80], key[8], defaultseed[17];
char passwd[256], passwd2[256], tbuf[27], buf[60];
char lastc, me[80], user[8], *salt, *p, *pw;
struct skey skey;
struct passwd *pp;
struct tm *tm;
time(&now);
tm = localtime(&now);
strftime(tbuf, sizeof(tbuf), "%M%j", tm);
if (gethostname(hostname, sizeof(hostname)) < 0)
err(1, "gethostname");
strncpy(defaultseed, hostname, sizeof(defaultseed)- 1);
defaultseed[4] = '\0';
strncat(defaultseed, tbuf, sizeof(defaultseed) - 5);
if ((pp = getpwuid(getuid())) == NULL)
err(1, "no user with uid %d", getuid());
strcpy(me, pp->pw_name);
if ((pp = getpwnam(me)) == NULL)
err(1, "Who are you?");
defaultsetup = 1;
for (i=1; i < argc; i++) {
if (strcmp("-s", argv[i]) == 0)
defaultsetup = 0;
else if (strcmp("-z", argv[i]) == 0)
zerokey = 1;
else {
pp = getpwnam(argv[i]);
break;
}
}
if (pp == NULL) {
err(1, "User unknown");
}
if (strcmp(pp->pw_name, me) != 0) {
if (getuid() != 0) {
/* Only root can change other's passwds */
printf("Permission denied.\n");
exit(1);
}
}
salt = pp->pw_passwd;
setpriority(PRIO_PROCESS, 0, -4);
if (getuid() != 0) {
setpriority(PRIO_PROCESS, 0, -4);
pw = getpass("Password:");
p = crypt(pw, salt);
setpriority(PRIO_PROCESS, 0, 0);
if (pp && strcmp(p, pp->pw_passwd)) {
printf("Password incorrect.\n");
exit(1);
}
}
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));
printf("[Updating %s]\n", pp->pw_name);
printf("Old key: %s\n", skey.seed);
/*
* lets be nice if they have a 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') {
strcpy(defaultseed, skey.seed);
defaultseed[l - 1] = lastc + 1;
}
if (isdigit(lastc) && lastc == '9' && l < 16) {
strcpy(defaultseed, skey.seed);
defaultseed[l - 1] = '0';
defaultseed[l] = '0';
defaultseed[l + 1] = '\0';
}
}
break;
case 1:
if (zerokey) {
printf("You have no entry to zero.\n");
exit(1);
}
printf("[Adding %s]\n", pp->pw_name);
break;
}
n = 99;
if (!defaultsetup) {
printf("You need the 6 english words generated from the \"key\" command.\n");
for (i = 0;; i++) {
if (i >= 2)
exit(1);
printf("Enter sequence count from 1 to 10000: ");
fgets(tmp, sizeof(tmp), stdin);
n = atoi(tmp);
if (n > 0 && n < 10000)
break; /* Valid range */
printf("\n Error: Count must be > 0 and < 10000\n");
}
}
if (!defaultsetup) {
printf("Enter new key [default %s]: ", defaultseed);
fflush(stdout);
fgets(seed, sizeof(seed), stdin);
rip(seed);
if (strlen(seed) > 16) {
printf("Notice: Seed truncated to 16 characters.\n");
seed[16] = '\0';
}
if (seed[0] == '\0')
strcpy(seed, defaultseed);
for (i = 0;; i++) {
if (i >= 2)
exit(1);
printf("s/key %d %s\ns/key access password: ", n, seed);
fgets(tmp, sizeof(tmp), stdin);
rip(tmp);
backspace(tmp);
if (tmp[0] == '?') {
printf("Enter 6 English words from secure S/Key calculation.\n");
continue;
}
if (tmp[0] == '\0') {
exit(1);
}
if (etob(key, tmp) == 1 || atob8(key, tmp) == 0)
break; /* Valid format */
printf("Invalid format - try again with 6 English words.\n");
}
} else {
/* Get user's secret password */
for (i = 0;; i++) {
if (i >= 2)
exit(1);
printf("Enter secret password: ");
readpass(passwd, sizeof(passwd));
if (passwd[0] == '\0')
exit(1);
printf("Again secret password: ");
readpass(passwd2, sizeof(passwd));
if (passwd2[0] == '\0')
exit(1);
if (strlen(passwd) < 4 && strlen(passwd2) < 4)
err(1, "Your password must be longer");
if (strcmp(passwd, passwd2) == 0)
break;
printf("Passwords do not match.\n");
}
strcpy(seed, defaultseed);
/* Crunch seed and password into starting key */
if (keycrunch(key, seed, passwd) != 0)
err(2, "key crunch failed");
nn = n;
while (nn-- != 0)
f(key);
}
time(&now);
tm = localtime(&now);
strftime(tbuf, sizeof(tbuf), " %b %d,%Y %T", tm);
skey.val = (char *)malloc(16 + 1);
btoa8(skey.val, key);
fprintf(skey.keyfile, "%s %04d %-16s %s %-21s\n", pp->pw_name, n,
seed, skey.val, tbuf);
fclose(skey.keyfile);
printf("ID %s s/key is %d %s\n", pp->pw_name, n, seed);
printf("Next login password: %s\n", btoe(buf, key));
#ifdef HEXIN
printf("%s\n", put8(buf, key));
#endif
exit(1);
}
|