1 #include <u.h>
2 #include <libc.h>
3 #include <authsrv.h>
4
5 static long finddosfile(int, char*);
6
7 static int nvramdebug;
8
9 static int
check(void * x,int len,uchar sum,char * msg)10 check(void *x, int len, uchar sum, char *msg)
11 {
12 if(nvcsum(x, len) == sum)
13 return 0;
14 memset(x, 0, len);
15 fprint(2, "%s\n", msg);
16 return 1;
17 }
18
19 /*
20 * get key info out of nvram. since there isn't room in the PC's nvram use
21 * a disk partition there.
22 */
23 static struct {
24 char *cputype;
25 char *file;
26 int off;
27 int len;
28 } nvtab[] = {
29 "sparc", "#r/nvram", 1024+850, sizeof(Nvrsafe),
30 "pc", "#S/sdC0/nvram", 0, sizeof(Nvrsafe),
31 "pc", "#S/sdC0/9fat", -1, sizeof(Nvrsafe),
32 "pc", "#S/sdC1/nvram", 0, sizeof(Nvrsafe),
33 "pc", "#S/sdC1/9fat", -1, sizeof(Nvrsafe),
34 "pc", "#S/sdD0/nvram", 0, sizeof(Nvrsafe),
35 "pc", "#S/sdD0/9fat", -1, sizeof(Nvrsafe),
36 "pc", "#S/sdE0/nvram", 0, sizeof(Nvrsafe),
37 "pc", "#S/sdE0/9fat", -1, sizeof(Nvrsafe),
38 "pc", "#S/sdF0/nvram", 0, sizeof(Nvrsafe),
39 "pc", "#S/sdF0/9fat", -1, sizeof(Nvrsafe),
40 "pc", "#S/sd00/nvram", 0, sizeof(Nvrsafe),
41 "pc", "#S/sd00/9fat", -1, sizeof(Nvrsafe),
42 "pc", "#S/sd01/nvram", 0, sizeof(Nvrsafe),
43 "pc", "#S/sd01/9fat", -1, sizeof(Nvrsafe),
44 "pc", "#S/sd10/nvram", 0, sizeof(Nvrsafe),
45 "pc", "#S/sd10/9fat", -1, sizeof(Nvrsafe),
46 "pc", "#f/fd0disk", -1, 512, /* 512: #f requires whole sector reads */
47 "pc", "#f/fd1disk", -1, 512,
48 "mips", "#r/nvram", 1024+900, sizeof(Nvrsafe),
49 "power", "#F/flash/flash0", 0x440000, sizeof(Nvrsafe),
50 "power", "#F/flash/flash", 0x440000, sizeof(Nvrsafe),
51 "power", "#r/nvram", 4352, sizeof(Nvrsafe), /* OK for MTX-604e */
52 "power", "/nvram", 0, sizeof(Nvrsafe), /* OK for Ucu */
53 "arm", "#F/flash/flash0", 0x100000, sizeof(Nvrsafe),
54 "arm", "#F/flash/flash", 0x100000, sizeof(Nvrsafe),
55 "debug", "/tmp/nvram", 0, sizeof(Nvrsafe),
56 };
57
58 static char*
readcons(char * prompt,char * def,int raw,char * buf,int nbuf)59 readcons(char *prompt, char *def, int raw, char *buf, int nbuf)
60 {
61 int fdin, fdout, ctl, n, m;
62 char line[10];
63
64 fdin = open("/dev/cons", OREAD);
65 if(fdin < 0)
66 fdin = 0;
67 fdout = open("/dev/cons", OWRITE);
68 if(fdout < 0)
69 fdout = 1;
70 if(def != nil)
71 fprint(fdout, "%s[%s]: ", prompt, def);
72 else
73 fprint(fdout, "%s: ", prompt);
74 if(raw){
75 ctl = open("/dev/consctl", OWRITE);
76 if(ctl >= 0)
77 write(ctl, "rawon", 5);
78 } else
79 ctl = -1;
80
81 m = 0;
82 for(;;){
83 n = read(fdin, line, 1);
84 if(n == 0){
85 close(ctl);
86 werrstr("readcons: EOF");
87 return nil;
88 }
89 if(n < 0){
90 close(ctl);
91 werrstr("can't read cons");
92 return nil;
93 }
94 if(line[0] == 0x7f)
95 exits(0);
96 if(n == 0 || line[0] == '\n' || line[0] == '\r'){
97 if(raw){
98 write(ctl, "rawoff", 6);
99 write(fdout, "\n", 1);
100 close(ctl);
101 }
102 buf[m] = '\0';
103 if(buf[0]=='\0' && def)
104 strcpy(buf, def);
105 return buf;
106 }
107 if(line[0] == '\b'){
108 if(m > 0)
109 m--;
110 }else if(line[0] == 0x15){ /* ^U: line kill */
111 m = 0;
112 if(def != nil)
113 fprint(fdout, "%s[%s]: ", prompt, def);
114 else
115 fprint(fdout, "%s: ", prompt);
116 }else{
117 if(m >= nbuf-1){
118 fprint(fdout, "line too long\n");
119 m = 0;
120 if(def != nil)
121 fprint(fdout, "%s[%s]: ", prompt, def);
122 else
123 fprint(fdout, "%s: ", prompt);
124 }else
125 buf[m++] = line[0];
126 }
127 }
128 }
129
130 typedef struct {
131 int fd;
132 int safeoff;
133 int safelen;
134 } Nvrwhere;
135
136 static char *nvrfile = nil, *cputype = nil;
137
138 static int
openrd(char * file)139 openrd(char *file)
140 {
141 int fd;
142
143 if(nvramdebug)
144 fprint(2, "nvram at %s?...", file);
145 fd = open(file, ORDWR);
146 if (fd < 0)
147 fd = open(file, OREAD);
148 if(nvramdebug)
149 fprint(2, "%s\n", fd >= 0? "yes": "no");
150 return fd;
151 }
152
153 /* returns with *locp filled in and locp->fd open, if possible */
154 static void
findnvram(Nvrwhere * locp)155 findnvram(Nvrwhere *locp)
156 {
157 char *nvrlen, *nvroff, *nvrcopy, *db, *v[2];
158 int fd, i, safeoff, safelen;
159
160 if (nvrfile == nil) {
161 nvrfile = getenv("nvram");
162 db = getenv("nvramdebug");
163 nvramdebug = db != nil;
164 free(db);
165 }
166 if (cputype == nil)
167 cputype = getenv("cputype");
168 if(cputype == nil)
169 cputype = strdup("mips");
170 if(strcmp(cputype, "386")==0 || strcmp(cputype, "amd64")==0 || strcmp(cputype, "alpha")==0) {
171 free(cputype);
172 cputype = strdup("pc");
173 }
174
175 fd = -1;
176 safeoff = -1;
177 safelen = -1;
178 if(nvrfile != nil && *nvrfile != '\0'){
179 /* accept device and device!file */
180 nvrcopy = strdup(nvrfile);
181 i = gettokens(nvrcopy, v, nelem(v), "!");
182 if (i < 1) {
183 i = 1;
184 v[0] = "";
185 v[1] = nil;
186 }
187 fd = openrd(v[0]);
188 safelen = sizeof(Nvrsafe);
189 if(strstr(v[0], "/9fat") == nil)
190 safeoff = 0;
191 nvrlen = getenv("nvrlen");
192 if(nvrlen != nil)
193 safelen = atoi(nvrlen);
194 nvroff = getenv("nvroff");
195 if(nvroff != nil)
196 if(strcmp(nvroff, "dos") == 0)
197 safeoff = -1;
198 else
199 safeoff = atoi(nvroff);
200 if(safeoff < 0 && fd >= 0){
201 safelen = 512;
202 safeoff = finddosfile(fd, i == 2? v[1]: "plan9.nvr");
203 if(safeoff < 0){ /* didn't find plan9.nvr? */
204 close(fd);
205 fd = -1;
206 }
207 }
208 free(nvrcopy);
209 free(nvroff);
210 free(nvrlen);
211 }else{
212 for(i=0; i<nelem(nvtab); i++){
213 if(strcmp(cputype, nvtab[i].cputype) != 0)
214 continue;
215 if((fd = openrd(nvtab[i].file)) < 0)
216 continue;
217 safeoff = nvtab[i].off;
218 safelen = nvtab[i].len;
219 if(safeoff == -1){
220 safeoff = finddosfile(fd, "plan9.nvr");
221 if(safeoff < 0){ /* didn't find plan9.nvr? */
222 close(fd);
223 fd = -1;
224 continue;
225 }
226 }
227 nvrfile = strdup(nvtab[i].file);
228 break;
229 }
230 if(i >= nelem(nvtab)) /* tried them all? */
231 werrstr(""); /* ignore failed opens */
232 }
233 locp->fd = fd;
234 locp->safelen = safelen;
235 locp->safeoff = safeoff;
236 }
237
238 /*
239 * get key info out of nvram. since there isn't room in the PC's nvram use
240 * a disk partition there.
241 */
242 int
readnvram(Nvrsafe * safep,int flag)243 readnvram(Nvrsafe *safep, int flag)
244 {
245 int err;
246 char buf[512], in[128]; /* 512 for floppy i/o */
247 Nvrsafe *safe;
248 Nvrwhere loc;
249
250 err = 0;
251 safe = (Nvrsafe*)buf;
252 memset(&loc, 0, sizeof loc);
253 findnvram(&loc);
254 if (loc.safelen < 0)
255 loc.safelen = sizeof *safe;
256 else if (loc.safelen > sizeof buf)
257 loc.safelen = sizeof buf;
258 if (loc.safeoff < 0) {
259 fprint(2, "readnvram: can't find nvram\n");
260 if(!(flag&NVwritemem))
261 memset(safep, 0, sizeof(*safep));
262 safe = safep;
263 /*
264 * allow user to type the data for authentication,
265 * even if there's no nvram to store it in.
266 */
267 }
268
269 if(flag&NVwritemem)
270 safe = safep;
271 else {
272 memset(safep, 0, sizeof(*safep));
273 if(loc.fd >= 0)
274 werrstr("");
275 if(loc.fd < 0
276 || seek(loc.fd, loc.safeoff, 0) < 0
277 || read(loc.fd, buf, loc.safelen) != loc.safelen){
278 err = 1;
279 if(flag&(NVwrite|NVwriteonerr))
280 if(loc.fd < 0 && nvrfile != nil)
281 fprint(2, "can't open %s: %r\n", nvrfile);
282 else if(loc.fd < 0){
283 /* this will have been printed above */
284 // fprint(2, "can't find nvram: %r\n");
285 }else if (seek(loc.fd, loc.safeoff, 0) < 0)
286 fprint(2, "can't seek %s to %d: %r\n",
287 nvrfile, loc.safeoff);
288 else
289 fprint(2, "can't read %d bytes from %s: %r\n",
290 loc.safelen, nvrfile);
291 /* start from scratch */
292 memset(safep, 0, sizeof(*safep));
293 safe = safep;
294 }else{
295 *safep = *safe; /* overwrite arg with data read */
296 safe = safep;
297
298 /* verify data read */
299 err |= check(safe->machkey, DESKEYLEN, safe->machsum,
300 "bad authentication password");
301 // err |= check(safe->config, CONFIGLEN, safe->configsum,
302 // "bad secstore password");
303 err |= check(safe->authid, ANAMELEN, safe->authidsum,
304 "bad authentication id");
305 err |= check(safe->authdom, DOMLEN, safe->authdomsum,
306 "bad authentication domain");
307 if(err == 0)
308 if(safe->authid[0]==0 || safe->authdom[0]==0){
309 fprint(2, "empty nvram authid or authdom\n");
310 err = 1;
311 }
312 }
313 }
314
315 if((flag&(NVwrite|NVwritemem)) || (err && (flag&NVwriteonerr))){
316 if (!(flag&NVwritemem)) {
317 readcons("authid", nil, 0, safe->authid,
318 sizeof safe->authid);
319 readcons("authdom", nil, 0, safe->authdom,
320 sizeof safe->authdom);
321 for(;;){
322 if(readcons("auth password", nil, 1, in,
323 sizeof in) == nil)
324 goto Out;
325 if(passtokey(safe->machkey, in))
326 break;
327 }
328 readcons("secstore password", nil, 1, safe->config,
329 sizeof safe->config);
330 }
331
332 // safe->authsum = nvcsum(safe->authkey, DESKEYLEN);
333 safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
334 safe->configsum = nvcsum(safe->config, CONFIGLEN);
335 safe->authidsum = nvcsum(safe->authid, sizeof safe->authid);
336 safe->authdomsum = nvcsum(safe->authdom, sizeof safe->authdom);
337
338 *(Nvrsafe*)buf = *safe;
339 if(loc.fd >= 0)
340 werrstr("");
341 if(loc.fd < 0
342 || seek(loc.fd, loc.safeoff, 0) < 0
343 || write(loc.fd, buf, loc.safelen) != loc.safelen){
344 fprint(2, "can't write key to nvram: %r\n");
345 err = 1;
346 }else
347 err = 0;
348 }
349 Out:
350 if (loc.fd >= 0)
351 close(loc.fd);
352 return err? -1: 0;
353 }
354
355 typedef struct Dosboot Dosboot;
356 struct Dosboot{
357 uchar magic[3]; /* really an xx86 JMP instruction */
358 uchar version[8];
359 uchar sectsize[2];
360 uchar clustsize;
361 uchar nresrv[2];
362 uchar nfats;
363 uchar rootsize[2];
364 uchar volsize[2];
365 uchar mediadesc;
366 uchar fatsize[2];
367 uchar trksize[2];
368 uchar nheads[2];
369 uchar nhidden[4];
370 uchar bigvolsize[4];
371 uchar driveno;
372 uchar reserved0;
373 uchar bootsig;
374 uchar volid[4];
375 uchar label[11];
376 uchar type[8];
377 };
378 #define GETSHORT(p) (((p)[1]<<8) | (p)[0])
379 #define GETLONG(p) ((GETSHORT((p)+2) << 16) | GETSHORT((p)))
380
381 typedef struct Dosdir Dosdir;
382 struct Dosdir
383 {
384 char name[8];
385 char ext[3];
386 uchar attr;
387 uchar reserved[10];
388 uchar time[2];
389 uchar date[2];
390 uchar start[2];
391 uchar length[4];
392 };
393
394 static char*
dosparse(char * from,char * to,int len)395 dosparse(char *from, char *to, int len)
396 {
397 char c;
398
399 memset(to, ' ', len);
400 if(from == 0)
401 return 0;
402 while(len-- > 0){
403 c = *from++;
404 if(c == '.')
405 return from;
406 if(c == 0)
407 break;
408 if(c >= 'a' && c <= 'z')
409 *to++ = c + 'A' - 'a';
410 else
411 *to++ = c;
412 }
413 return 0;
414 }
415
416 /*
417 * return offset of first file block
418 *
419 * This is a very simplistic dos file system. It only
420 * works on floppies, only looks in the root, and only
421 * returns a pointer to the first block of a file.
422 *
423 * This exists for cpu servers that have no hard disk
424 * or nvram to store the key on.
425 *
426 * Please don't make this any smarter: it stays resident
427 * and I'ld prefer not to waste the space on something that
428 * runs only at boottime -- presotto.
429 */
430 static long
finddosfile(int fd,char * file)431 finddosfile(int fd, char *file)
432 {
433 uchar secbuf[512];
434 char name[8];
435 char ext[3];
436 Dosboot *b;
437 Dosdir *root, *dp;
438 int nroot, sectsize, rootoff, rootsects, n;
439
440 /* dos'ize file name */
441 file = dosparse(file, name, 8);
442 dosparse(file, ext, 3);
443
444 /* read boot block, check for sanity */
445 b = (Dosboot*)secbuf;
446 if(read(fd, secbuf, sizeof(secbuf)) != sizeof(secbuf))
447 return -1;
448 if(b->magic[0] != 0xEB || b->magic[1] != 0x3C || b->magic[2] != 0x90)
449 return -1;
450 sectsize = GETSHORT(b->sectsize);
451 if(sectsize != 512)
452 return -1;
453 rootoff = (GETSHORT(b->nresrv) + b->nfats*GETSHORT(b->fatsize)) * sectsize;
454 if(seek(fd, rootoff, 0) < 0)
455 return -1;
456 nroot = GETSHORT(b->rootsize);
457 rootsects = (nroot*sizeof(Dosdir)+sectsize-1)/sectsize;
458 if(rootsects <= 0 || rootsects > 64)
459 return -1;
460
461 /*
462 * read root. it is contiguous to make stuff like
463 * this easier
464 */
465 root = malloc(rootsects*sectsize);
466 if(read(fd, root, rootsects*sectsize) != rootsects*sectsize)
467 return -1;
468 n = -1;
469 for(dp = root; dp < &root[nroot]; dp++)
470 if(memcmp(name, dp->name, 8) == 0 && memcmp(ext, dp->ext, 3) == 0){
471 n = GETSHORT(dp->start);
472 break;
473 }
474 free(root);
475
476 if(n < 0)
477 return -1;
478
479 /*
480 * dp->start is in cluster units, not sectors. The first
481 * cluster is cluster 2 which starts immediately after the
482 * root directory
483 */
484 return rootoff + rootsects*sectsize + (n-2)*sectsize*b->clustsize;
485 }
486
487