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