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