1 4180d05b 2004-06-17 devnull #include <u.h>
2 4180d05b 2004-06-17 devnull #include <libc.h>
3 4180d05b 2004-06-17 devnull #include <authsrv.h>
5 4180d05b 2004-06-17 devnull static long finddosfile(int, char*);
7 4180d05b 2004-06-17 devnull static int
8 4180d05b 2004-06-17 devnull check(void *x, int len, uchar sum, char *msg)
10 4180d05b 2004-06-17 devnull if(nvcsum(x, len) == sum)
11 4180d05b 2004-06-17 devnull return 0;
12 4180d05b 2004-06-17 devnull memset(x, 0, len);
13 4180d05b 2004-06-17 devnull fprint(2, "%s\n", msg);
14 4180d05b 2004-06-17 devnull return 1;
18 4180d05b 2004-06-17 devnull * get key info out of nvram. since there isn't room in the PC's nvram use
19 4180d05b 2004-06-17 devnull * a disk partition there.
21 4180d05b 2004-06-17 devnull static struct {
22 4180d05b 2004-06-17 devnull char *cputype;
23 4180d05b 2004-06-17 devnull char *file;
26 4180d05b 2004-06-17 devnull } nvtab[] = {
27 4180d05b 2004-06-17 devnull "sparc", "#r/nvram", 1024+850, sizeof(Nvrsafe),
28 4180d05b 2004-06-17 devnull "pc", "#S/sdC0/nvram", 0, sizeof(Nvrsafe),
29 4180d05b 2004-06-17 devnull "pc", "#S/sdC0/9fat", -1, sizeof(Nvrsafe),
30 4180d05b 2004-06-17 devnull "pc", "#S/sdC1/nvram", 0, sizeof(Nvrsafe),
31 4180d05b 2004-06-17 devnull "pc", "#S/sdC1/9fat", -1, sizeof(Nvrsafe),
32 4180d05b 2004-06-17 devnull "pc", "#S/sd00/nvram", 0, sizeof(Nvrsafe),
33 4180d05b 2004-06-17 devnull "pc", "#S/sd00/9fat", -1, sizeof(Nvrsafe),
34 4180d05b 2004-06-17 devnull "pc", "#S/sd01/nvram", 0, sizeof(Nvrsafe),
35 4180d05b 2004-06-17 devnull "pc", "#S/sd01/9fat", -1, sizeof(Nvrsafe),
36 4180d05b 2004-06-17 devnull "pc", "#f/fd0disk", -1, 512, /* 512: #f requires whole sector reads */
37 4180d05b 2004-06-17 devnull "pc", "#f/fd1disk", -1, 512,
38 4180d05b 2004-06-17 devnull "mips", "#r/nvram", 1024+900, sizeof(Nvrsafe),
39 4180d05b 2004-06-17 devnull "power", "#F/flash/flash0", 0x440000, sizeof(Nvrsafe),
40 4180d05b 2004-06-17 devnull "power", "#r/nvram", 4352, sizeof(Nvrsafe), /* OK for MTX-604e */
41 4180d05b 2004-06-17 devnull "debug", "/tmp/nvram", 0, sizeof(Nvrsafe),
45 6c0209f6 2005-02-11 devnull xreadcons(char *prompt, char *def, int secret, char *buf, int nbuf)
49 6c0209f6 2005-02-11 devnull p = readcons(prompt, def, secret);
50 6c0209f6 2005-02-11 devnull if(p == nil)
51 6c0209f6 2005-02-11 devnull return nil;
52 6c0209f6 2005-02-11 devnull strecpy(buf, buf+nbuf, p);
53 6c0209f6 2005-02-11 devnull memset(p, 0, strlen(p));
55 6c0209f6 2005-02-11 devnull return buf;
59 4180d05b 2004-06-17 devnull * get key info out of nvram. since there isn't room in the PC's nvram use
60 4180d05b 2004-06-17 devnull * a disk partition there.
63 4180d05b 2004-06-17 devnull readnvram(Nvrsafe *safep, int flag)
65 4180d05b 2004-06-17 devnull char buf[1024], in[128], *cputype, *nvrfile, *nvrlen, *nvroff, *v[2];
66 4180d05b 2004-06-17 devnull int fd, err, i, safeoff, safelen;
67 4180d05b 2004-06-17 devnull Nvrsafe *safe;
70 4180d05b 2004-06-17 devnull memset(safep, 0, sizeof(*safep));
72 4180d05b 2004-06-17 devnull nvrfile = getenv("nvram");
73 4180d05b 2004-06-17 devnull cputype = getenv("cputype");
74 4180d05b 2004-06-17 devnull if(cputype == nil)
75 4180d05b 2004-06-17 devnull cputype = "mips";
76 4180d05b 2004-06-17 devnull if(strcmp(cputype, "386")==0 || strcmp(cputype, "alpha")==0)
77 4180d05b 2004-06-17 devnull cputype = "pc";
79 4180d05b 2004-06-17 devnull safe = (Nvrsafe*)buf;
82 4180d05b 2004-06-17 devnull safeoff = -1;
83 4180d05b 2004-06-17 devnull safelen = -1;
84 4180d05b 2004-06-17 devnull if(nvrfile != nil){
85 4180d05b 2004-06-17 devnull /* accept device and device!file */
86 4180d05b 2004-06-17 devnull i = gettokens(nvrfile, v, nelem(v), "!");
87 4180d05b 2004-06-17 devnull fd = open(v[0], ORDWR);
88 4180d05b 2004-06-17 devnull safelen = sizeof(Nvrsafe);
89 4180d05b 2004-06-17 devnull if(strstr(v[0], "/9fat") == nil)
90 4180d05b 2004-06-17 devnull safeoff = 0;
91 4180d05b 2004-06-17 devnull nvrlen = getenv("nvrlen");
92 4180d05b 2004-06-17 devnull if(nvrlen != nil)
93 4180d05b 2004-06-17 devnull safelen = atoi(nvrlen);
94 4180d05b 2004-06-17 devnull nvroff = getenv("nvroff");
95 4180d05b 2004-06-17 devnull if(nvroff != nil){
96 4180d05b 2004-06-17 devnull if(strcmp(nvroff, "dos") == 0)
97 4180d05b 2004-06-17 devnull safeoff = -1;
99 4180d05b 2004-06-17 devnull safeoff = atoi(nvroff);
101 4180d05b 2004-06-17 devnull if(safeoff < 0 && fd >= 0){
102 4180d05b 2004-06-17 devnull safelen = 512;
103 4180d05b 2004-06-17 devnull safeoff = finddosfile(fd, i == 2 ? v[1] : "plan9.nvr");
104 4180d05b 2004-06-17 devnull if(safeoff < 0){
105 4180d05b 2004-06-17 devnull close(fd);
106 4180d05b 2004-06-17 devnull fd = -1;
109 4180d05b 2004-06-17 devnull free(nvrfile);
110 4180d05b 2004-06-17 devnull if(nvrlen != nil)
111 4180d05b 2004-06-17 devnull free(nvrlen);
112 4180d05b 2004-06-17 devnull if(nvroff != nil)
113 4180d05b 2004-06-17 devnull free(nvroff);
115 4180d05b 2004-06-17 devnull for(i=0; i<nelem(nvtab); i++){
116 4180d05b 2004-06-17 devnull if(strcmp(cputype, nvtab[i].cputype) != 0)
117 4180d05b 2004-06-17 devnull continue;
118 4180d05b 2004-06-17 devnull if((fd = open(nvtab[i].file, ORDWR)) < 0)
119 4180d05b 2004-06-17 devnull continue;
120 4180d05b 2004-06-17 devnull safeoff = nvtab[i].off;
121 4180d05b 2004-06-17 devnull safelen = nvtab[i].len;
122 4180d05b 2004-06-17 devnull if(safeoff == -1){
123 4180d05b 2004-06-17 devnull safeoff = finddosfile(fd, "plan9.nvr");
124 4180d05b 2004-06-17 devnull if(safeoff < 0){
125 4180d05b 2004-06-17 devnull close(fd);
126 4180d05b 2004-06-17 devnull fd = -1;
127 4180d05b 2004-06-17 devnull continue;
134 4180d05b 2004-06-17 devnull if(fd < 0
135 4180d05b 2004-06-17 devnull || seek(fd, safeoff, 0) < 0
136 4180d05b 2004-06-17 devnull || read(fd, buf, safelen) != safelen){
137 4180d05b 2004-06-17 devnull err = 1;
138 4180d05b 2004-06-17 devnull if(flag&(NVwrite|NVwriteonerr))
139 4180d05b 2004-06-17 devnull fprint(2, "can't read nvram: %r\n");
140 4180d05b 2004-06-17 devnull memset(safep, 0, sizeof(*safep));
141 4180d05b 2004-06-17 devnull safe = safep;
143 4180d05b 2004-06-17 devnull *safep = *safe;
144 4180d05b 2004-06-17 devnull safe = safep;
146 4180d05b 2004-06-17 devnull err |= check(safe->machkey, DESKEYLEN, safe->machsum, "bad nvram key");
147 4180d05b 2004-06-17 devnull // err |= check(safe->config, CONFIGLEN, safe->configsum, "bad secstore key");
148 4180d05b 2004-06-17 devnull err |= check(safe->authid, ANAMELEN, safe->authidsum, "bad authentication id");
149 4180d05b 2004-06-17 devnull err |= check(safe->authdom, DOMLEN, safe->authdomsum, "bad authentication domain");
152 4180d05b 2004-06-17 devnull if((flag&NVwrite) || (err && (flag&NVwriteonerr))){
153 6c0209f6 2005-02-11 devnull xreadcons("authid", nil, 0, safe->authid, sizeof(safe->authid));
154 6c0209f6 2005-02-11 devnull xreadcons("authdom", nil, 0, safe->authdom, sizeof(safe->authdom));
155 6c0209f6 2005-02-11 devnull xreadcons("secstore key", nil, 1, safe->config, sizeof(safe->config));
156 4180d05b 2004-06-17 devnull for(;;){
157 6c0209f6 2005-02-11 devnull if(xreadcons("password", nil, 1, in, sizeof in) == nil)
158 4180d05b 2004-06-17 devnull goto Out;
159 4180d05b 2004-06-17 devnull if(passtokey(safe->machkey, in))
162 4180d05b 2004-06-17 devnull safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
163 4180d05b 2004-06-17 devnull safe->configsum = nvcsum(safe->config, CONFIGLEN);
164 4180d05b 2004-06-17 devnull safe->authidsum = nvcsum(safe->authid, sizeof(safe->authid));
165 4180d05b 2004-06-17 devnull safe->authdomsum = nvcsum(safe->authdom, sizeof(safe->authdom));
166 4180d05b 2004-06-17 devnull *(Nvrsafe*)buf = *safe;
167 4180d05b 2004-06-17 devnull if(seek(fd, safeoff, 0) < 0
168 4180d05b 2004-06-17 devnull || write(fd, buf, safelen) != safelen){
169 4180d05b 2004-06-17 devnull fprint(2, "can't write key to nvram: %r\n");
170 4180d05b 2004-06-17 devnull err = 1;
172 4180d05b 2004-06-17 devnull err = 0;
175 4180d05b 2004-06-17 devnull close(fd);
176 4180d05b 2004-06-17 devnull return err ? -1 : 0;
179 4180d05b 2004-06-17 devnull typedef struct Dosboot Dosboot;
180 4180d05b 2004-06-17 devnull struct Dosboot{
181 4180d05b 2004-06-17 devnull uchar magic[3]; /* really an xx86 JMP instruction */
182 4180d05b 2004-06-17 devnull uchar version[8];
183 4180d05b 2004-06-17 devnull uchar sectsize[2];
184 4180d05b 2004-06-17 devnull uchar clustsize;
185 4180d05b 2004-06-17 devnull uchar nresrv[2];
186 4180d05b 2004-06-17 devnull uchar nfats;
187 4180d05b 2004-06-17 devnull uchar rootsize[2];
188 4180d05b 2004-06-17 devnull uchar volsize[2];
189 4180d05b 2004-06-17 devnull uchar mediadesc;
190 4180d05b 2004-06-17 devnull uchar fatsize[2];
191 4180d05b 2004-06-17 devnull uchar trksize[2];
192 4180d05b 2004-06-17 devnull uchar nheads[2];
193 4180d05b 2004-06-17 devnull uchar nhidden[4];
194 4180d05b 2004-06-17 devnull uchar bigvolsize[4];
195 4180d05b 2004-06-17 devnull uchar driveno;
196 4180d05b 2004-06-17 devnull uchar reserved0;
197 4180d05b 2004-06-17 devnull uchar bootsig;
198 4180d05b 2004-06-17 devnull uchar volid[4];
199 4180d05b 2004-06-17 devnull uchar label[11];
200 4180d05b 2004-06-17 devnull uchar type[8];
202 4180d05b 2004-06-17 devnull #define GETSHORT(p) (((p)[1]<<8) | (p)[0])
203 4180d05b 2004-06-17 devnull #define GETLONG(p) ((GETSHORT((p)+2) << 16) | GETSHORT((p)))
205 4180d05b 2004-06-17 devnull typedef struct Dosdir Dosdir;
206 4180d05b 2004-06-17 devnull struct Dosdir
208 4180d05b 2004-06-17 devnull char name[8];
209 4180d05b 2004-06-17 devnull char ext[3];
210 4180d05b 2004-06-17 devnull uchar attr;
211 4180d05b 2004-06-17 devnull uchar reserved[10];
212 4180d05b 2004-06-17 devnull uchar time[2];
213 4180d05b 2004-06-17 devnull uchar date[2];
214 4180d05b 2004-06-17 devnull uchar start[2];
215 4180d05b 2004-06-17 devnull uchar length[4];
218 4180d05b 2004-06-17 devnull static char*
219 4180d05b 2004-06-17 devnull dosparse(char *from, char *to, int len)
223 4180d05b 2004-06-17 devnull memset(to, ' ', len);
224 4180d05b 2004-06-17 devnull if(from == 0)
225 4180d05b 2004-06-17 devnull return 0;
226 4180d05b 2004-06-17 devnull while(len-- > 0){
227 4180d05b 2004-06-17 devnull c = *from++;
228 4180d05b 2004-06-17 devnull if(c == '.')
229 4180d05b 2004-06-17 devnull return from;
230 4180d05b 2004-06-17 devnull if(c == 0)
232 4180d05b 2004-06-17 devnull if(c >= 'a' && c <= 'z')
233 4180d05b 2004-06-17 devnull *to++ = c + 'A' - 'a';
235 4180d05b 2004-06-17 devnull *to++ = c;
237 4180d05b 2004-06-17 devnull return 0;
241 4180d05b 2004-06-17 devnull * return offset of first file block
243 4180d05b 2004-06-17 devnull * This is a very simplistic dos file system. It only
244 4180d05b 2004-06-17 devnull * works on floppies, only looks in the root, and only
245 4180d05b 2004-06-17 devnull * returns a pointer to the first block of a file.
247 4180d05b 2004-06-17 devnull * This exists for cpu servers that have no hard disk
248 4180d05b 2004-06-17 devnull * or nvram to store the key on.
250 4180d05b 2004-06-17 devnull * Please don't make this any smarter: it stays resident
251 4180d05b 2004-06-17 devnull * and I'ld prefer not to waste the space on something that
252 4180d05b 2004-06-17 devnull * runs only at boottime -- presotto.
254 4180d05b 2004-06-17 devnull static long
255 4180d05b 2004-06-17 devnull finddosfile(int fd, char *file)
257 4180d05b 2004-06-17 devnull uchar secbuf[512];
258 4180d05b 2004-06-17 devnull char name[8];
259 4180d05b 2004-06-17 devnull char ext[3];
260 4180d05b 2004-06-17 devnull Dosboot *b;
261 4180d05b 2004-06-17 devnull Dosdir *root, *dp;
262 4180d05b 2004-06-17 devnull int nroot, sectsize, rootoff, rootsects, n;
264 4180d05b 2004-06-17 devnull /* dos'ize file name */
265 4180d05b 2004-06-17 devnull file = dosparse(file, name, 8);
266 4180d05b 2004-06-17 devnull dosparse(file, ext, 3);
268 4180d05b 2004-06-17 devnull /* read boot block, check for sanity */
269 4180d05b 2004-06-17 devnull b = (Dosboot*)secbuf;
270 4180d05b 2004-06-17 devnull if(read(fd, secbuf, sizeof(secbuf)) != sizeof(secbuf))
271 4180d05b 2004-06-17 devnull return -1;
272 4180d05b 2004-06-17 devnull if(b->magic[0] != 0xEB || b->magic[1] != 0x3C || b->magic[2] != 0x90)
273 4180d05b 2004-06-17 devnull return -1;
274 4180d05b 2004-06-17 devnull sectsize = GETSHORT(b->sectsize);
275 4180d05b 2004-06-17 devnull if(sectsize != 512)
276 4180d05b 2004-06-17 devnull return -1;
277 4180d05b 2004-06-17 devnull rootoff = (GETSHORT(b->nresrv) + b->nfats*GETSHORT(b->fatsize)) * sectsize;
278 4180d05b 2004-06-17 devnull if(seek(fd, rootoff, 0) < 0)
279 4180d05b 2004-06-17 devnull return -1;
280 4180d05b 2004-06-17 devnull nroot = GETSHORT(b->rootsize);
281 4180d05b 2004-06-17 devnull rootsects = (nroot*sizeof(Dosdir)+sectsize-1)/sectsize;
282 4180d05b 2004-06-17 devnull if(rootsects <= 0 || rootsects > 64)
283 4180d05b 2004-06-17 devnull return -1;
286 4180d05b 2004-06-17 devnull * read root. it is contiguous to make stuff like
287 4180d05b 2004-06-17 devnull * this easier
289 4180d05b 2004-06-17 devnull root = malloc(rootsects*sectsize);
290 4180d05b 2004-06-17 devnull if(read(fd, root, rootsects*sectsize) != rootsects*sectsize)
291 4180d05b 2004-06-17 devnull return -1;
293 4180d05b 2004-06-17 devnull for(dp = root; dp < &root[nroot]; dp++)
294 4180d05b 2004-06-17 devnull if(memcmp(name, dp->name, 8) == 0 && memcmp(ext, dp->ext, 3) == 0){
295 4180d05b 2004-06-17 devnull n = GETSHORT(dp->start);
298 4180d05b 2004-06-17 devnull free(root);
300 4180d05b 2004-06-17 devnull if(n < 0)
301 4180d05b 2004-06-17 devnull return -1;
304 4180d05b 2004-06-17 devnull * dp->start is in cluster units, not sectors. The first
305 4180d05b 2004-06-17 devnull * cluster is cluster 2 which starts immediately after the
306 4180d05b 2004-06-17 devnull * root directory
308 4180d05b 2004-06-17 devnull return rootoff + rootsects*sectsize + (n-2)*sectsize*b->clustsize;