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