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