readnvram.c 8.7 KB

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