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