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