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