readnvram.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. cputype = strdup("pc");
  134. fd = -1;
  135. safeoff = -1;
  136. safelen = -1;
  137. if(nvrfile != nil){
  138. /* accept device and device!file */
  139. i = gettokens(nvrfile, v, nelem(v), "!");
  140. fd = open(v[0], ORDWR);
  141. safelen = sizeof(Nvrsafe);
  142. if(strstr(v[0], "/9fat") == nil)
  143. safeoff = 0;
  144. nvrlen = getenv("nvrlen");
  145. if(nvrlen != nil)
  146. safelen = atoi(nvrlen);
  147. nvroff = getenv("nvroff");
  148. if(nvroff != nil)
  149. if(strcmp(nvroff, "dos") == 0)
  150. safeoff = -1;
  151. else
  152. safeoff = atoi(nvroff);
  153. if(safeoff < 0 && fd >= 0){
  154. safelen = 512;
  155. safeoff = finddosfile(fd, i == 2? v[1]: "plan9.nvr");
  156. if(safeoff < 0){ /* didn't find plan9.nvr? */
  157. close(fd);
  158. fd = -1;
  159. }
  160. }
  161. free(nvroff);
  162. free(nvrlen);
  163. free(nvrfile);
  164. }else
  165. for(i=0; i<nelem(nvtab); i++){
  166. if(strcmp(cputype, nvtab[i].cputype) != 0)
  167. continue;
  168. if((fd = open(nvtab[i].file, ORDWR)) < 0)
  169. continue;
  170. safeoff = nvtab[i].off;
  171. safelen = nvtab[i].len;
  172. if(safeoff == -1){
  173. safeoff = finddosfile(fd, "plan9.nvr");
  174. if(safeoff < 0){ /* didn't find plan9.nvr? */
  175. close(fd);
  176. fd = -1;
  177. continue;
  178. }
  179. }
  180. break;
  181. }
  182. free(cputype);
  183. locp->fd = fd;
  184. locp->safelen = safelen;
  185. locp->safeoff = safeoff;
  186. }
  187. /*
  188. * get key info out of nvram. since there isn't room in the PC's nvram use
  189. * a disk partition there.
  190. */
  191. int
  192. readnvram(Nvrsafe *safep, int flag)
  193. {
  194. int err;
  195. char buf[512], in[128]; /* 512 for floppy i/o */
  196. Nvrsafe *safe;
  197. Nvrwhere loc;
  198. err = 0;
  199. safe = (Nvrsafe*)buf;
  200. memset(&loc, 0, sizeof loc);
  201. findnvram(&loc);
  202. if (loc.safelen < 0)
  203. loc.safelen = sizeof *safe;
  204. else if (loc.safelen > sizeof buf)
  205. loc.safelen = sizeof buf;
  206. if (loc.safeoff < 0) {
  207. fprint(2, "readnvram: couldn't find nvram\n");
  208. if(!(flag&NVwritemem))
  209. memset(safep, 0, sizeof(*safep));
  210. safe = safep;
  211. /*
  212. * allow user to type the data for authentication,
  213. * even if there's no nvram to store it in.
  214. */
  215. }
  216. if(flag&NVwritemem)
  217. safe = safep;
  218. else {
  219. memset(safep, 0, sizeof(*safep));
  220. if(loc.fd < 0
  221. || seek(loc.fd, loc.safeoff, 0) < 0
  222. || read(loc.fd, buf, loc.safelen) != loc.safelen){
  223. err = 1;
  224. if(flag&(NVwrite|NVwriteonerr))
  225. fprint(2, "can't read nvram: %r\n");
  226. /* start from scratch */
  227. memset(safep, 0, sizeof(*safep));
  228. safe = safep;
  229. }else{
  230. *safep = *safe; /* overwrite arg with data read */
  231. safe = safep;
  232. /* verify data read */
  233. err |= check(safe->machkey, DESKEYLEN, safe->machsum,
  234. "bad nvram key");
  235. // err |= check(safe->config, CONFIGLEN, safe->configsum,
  236. // "bad secstore key");
  237. err |= check(safe->authid, ANAMELEN, safe->authidsum,
  238. "bad authentication id");
  239. err |= check(safe->authdom, DOMLEN, safe->authdomsum,
  240. "bad authentication domain");
  241. if(err == 0)
  242. if(safe->authid[0]==0 || safe->authdom[0]==0){
  243. fprint(2, "empty nvram authid or authdom\n");
  244. err = 1;
  245. }
  246. }
  247. }
  248. if((flag&(NVwrite|NVwritemem)) || (err && (flag&NVwriteonerr))){
  249. if (!(flag&NVwritemem)) {
  250. readcons("authid", nil, 0, safe->authid,
  251. sizeof safe->authid);
  252. readcons("authdom", nil, 0, safe->authdom,
  253. sizeof safe->authdom);
  254. readcons("secstore key", nil, 1, safe->config,
  255. sizeof safe->config);
  256. for(;;){
  257. if(readcons("password", nil, 1, in, sizeof in)
  258. == nil)
  259. goto Out;
  260. if(passtokey(safe->machkey, in))
  261. break;
  262. }
  263. }
  264. // safe->authsum = nvcsum(safe->authkey, DESKEYLEN);
  265. safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
  266. safe->configsum = nvcsum(safe->config, CONFIGLEN);
  267. safe->authidsum = nvcsum(safe->authid, sizeof safe->authid);
  268. safe->authdomsum = nvcsum(safe->authdom, sizeof safe->authdom);
  269. *(Nvrsafe*)buf = *safe;
  270. if(loc.fd < 0
  271. || seek(loc.fd, loc.safeoff, 0) < 0
  272. || write(loc.fd, buf, loc.safelen) != loc.safelen){
  273. fprint(2, "can't write key to nvram: %r\n");
  274. err = 1;
  275. }else
  276. err = 0;
  277. }
  278. Out:
  279. if (loc.fd >= 0)
  280. close(loc.fd);
  281. return err? -1: 0;
  282. }
  283. typedef struct Dosboot Dosboot;
  284. struct Dosboot{
  285. uchar magic[3]; /* really an xx86 JMP instruction */
  286. uchar version[8];
  287. uchar sectsize[2];
  288. uchar clustsize;
  289. uchar nresrv[2];
  290. uchar nfats;
  291. uchar rootsize[2];
  292. uchar volsize[2];
  293. uchar mediadesc;
  294. uchar fatsize[2];
  295. uchar trksize[2];
  296. uchar nheads[2];
  297. uchar nhidden[4];
  298. uchar bigvolsize[4];
  299. uchar driveno;
  300. uchar reserved0;
  301. uchar bootsig;
  302. uchar volid[4];
  303. uchar label[11];
  304. uchar type[8];
  305. };
  306. #define GETSHORT(p) (((p)[1]<<8) | (p)[0])
  307. #define GETLONG(p) ((GETSHORT((p)+2) << 16) | GETSHORT((p)))
  308. typedef struct Dosdir Dosdir;
  309. struct Dosdir
  310. {
  311. char name[8];
  312. char ext[3];
  313. uchar attr;
  314. uchar reserved[10];
  315. uchar time[2];
  316. uchar date[2];
  317. uchar start[2];
  318. uchar length[4];
  319. };
  320. static char*
  321. dosparse(char *from, char *to, int len)
  322. {
  323. char c;
  324. memset(to, ' ', len);
  325. if(from == 0)
  326. return 0;
  327. while(len-- > 0){
  328. c = *from++;
  329. if(c == '.')
  330. return from;
  331. if(c == 0)
  332. break;
  333. if(c >= 'a' && c <= 'z')
  334. *to++ = c + 'A' - 'a';
  335. else
  336. *to++ = c;
  337. }
  338. return 0;
  339. }
  340. /*
  341. * return offset of first file block
  342. *
  343. * This is a very simplistic dos file system. It only
  344. * works on floppies, only looks in the root, and only
  345. * returns a pointer to the first block of a file.
  346. *
  347. * This exists for cpu servers that have no hard disk
  348. * or nvram to store the key on.
  349. *
  350. * Please don't make this any smarter: it stays resident
  351. * and I'ld prefer not to waste the space on something that
  352. * runs only at boottime -- presotto.
  353. */
  354. static long
  355. finddosfile(int fd, char *file)
  356. {
  357. uchar secbuf[512];
  358. char name[8];
  359. char ext[3];
  360. Dosboot *b;
  361. Dosdir *root, *dp;
  362. int nroot, sectsize, rootoff, rootsects, n;
  363. /* dos'ize file name */
  364. file = dosparse(file, name, 8);
  365. dosparse(file, ext, 3);
  366. /* read boot block, check for sanity */
  367. b = (Dosboot*)secbuf;
  368. if(read(fd, secbuf, sizeof(secbuf)) != sizeof(secbuf))
  369. return -1;
  370. if(b->magic[0] != 0xEB || b->magic[1] != 0x3C || b->magic[2] != 0x90)
  371. return -1;
  372. sectsize = GETSHORT(b->sectsize);
  373. if(sectsize != 512)
  374. return -1;
  375. rootoff = (GETSHORT(b->nresrv) + b->nfats*GETSHORT(b->fatsize)) * sectsize;
  376. if(seek(fd, rootoff, 0) < 0)
  377. return -1;
  378. nroot = GETSHORT(b->rootsize);
  379. rootsects = (nroot*sizeof(Dosdir)+sectsize-1)/sectsize;
  380. if(rootsects <= 0 || rootsects > 64)
  381. return -1;
  382. /*
  383. * read root. it is contiguous to make stuff like
  384. * this easier
  385. */
  386. root = malloc(rootsects*sectsize);
  387. if(read(fd, root, rootsects*sectsize) != rootsects*sectsize)
  388. return -1;
  389. n = -1;
  390. for(dp = root; dp < &root[nroot]; dp++)
  391. if(memcmp(name, dp->name, 8) == 0 && memcmp(ext, dp->ext, 3) == 0){
  392. n = GETSHORT(dp->start);
  393. break;
  394. }
  395. free(root);
  396. if(n < 0)
  397. return -1;
  398. /*
  399. * dp->start is in cluster units, not sectors. The first
  400. * cluster is cluster 2 which starts immediately after the
  401. * root directory
  402. */
  403. return rootoff + rootsects*sectsize + (n-2)*sectsize*b->clustsize;
  404. }