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