readnvram.c 10 KB

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