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