readnvram.c 10 KB

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