Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/boot/key.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname boot/key.c 1992/0317
## diff -e /dev/null /n/bootesdump/1992/0317/sys/src/9/boot/key.c
0a
#include <u.h>
#include <libc.h>
#include <../boot/boot.h>

void
key(Method *mp)
{
	char password[20], key[7];
	int prompt, fd;

	USED(mp);

	prompt = kflag;
	fd = open("#r/nvram", ORDWR);
	if(fd < 0){
		prompt = 1;
		warning("can't open nvram");
	}
	if(prompt){
		do
			if(getpasswd(password, sizeof password) < 0){
				warning("can't read cons");
				return;
			}
		while(!passtokey(key, password, strlen(password)));
	}else if(seek(fd, 1024+900, 0) < 0 || read(fd, key, 7) != 7){
		close(fd);
		warning("can't read key from nvram");
	}
	if(kflag && seek(fd, 1024+900, 0) < 0 || write(fd, key, 7) != 7){
		close(fd);
		warning("can't write key to nvram");
	}
	close(fd);
	fd = open("#c/key", OWRITE);
	if(fd < 0)
		warning("can't open key");
	else if(write(fd, key, 7) != 7)
		warning("can't write key");
	close(fd);
}

.
## diffname boot/key.c 1992/0318
## diff -e /n/bootesdump/1992/0317/sys/src/9/boot/key.c /n/bootesdump/1992/0318/sys/src/9/boot/key.c
30c
	if(kflag && (seek(fd, 1024+900, 0) < 0 || write(fd, key, 7) != 7)){
.
21,24c
			getpasswd(password, sizeof password);
.
10a
	USED(islocal);
.
6c
key(int islocal, Method *mp)
.
## diffname boot/key.c 1992/0322
## diff -e /n/bootesdump/1992/0318/sys/src/9/boot/key.c /n/bootesdump/1992/0322/sys/src/9/boot/key.c
23c
		while(!passtokey(key, password));
.
2a
#include <auth.h>
.
## diffname boot/key.c 1992/0323
## diff -e /n/bootesdump/1992/0322/sys/src/9/boot/key.c /n/bootesdump/1992/0323/sys/src/9/boot/key.c
41d
36,38c
		warning("can't open #c/key");
	else if(write(fd, key, DESKEYLEN) != DESKEYLEN)
		warning("can't set #c/key");
.
29,31c
	nvsum = cksum(key);
	if(kflag){
		if(seek(fd, 1024+900, 0) < 0
		|| write(fd, key, DESKEYLEN) != DESKEYLEN
		|| write(fd, &nvsum, 1) != 1)
			warning("can't write key to nvram");
.
25,27c
	}else if(cksum(key) != nvsum){
		warning("bad nvram key; using password boofhead");
		passtokey(key, "boofhead");
.
20a
	if(seek(fd, 1024+900, 0) < 0
	|| read(fd, key, DESKEYLEN) != DESKEYLEN
	|| read(fd, &nvsum, 1) != 1)
		warning("can't read nvram key");

.
9c
	char password[20], key[DESKEYLEN];
	uchar nvsum;
.
5a
static uchar
cksum(char *key)
{
	int i, nvsum;

	nvsum = 0;
	for(i=0; i<DESKEYLEN; i++)
		nvsum += key[i];
	return nvsum & 0xff;
}

.
## diffname boot/key.c 1992/0325
## diff -e /n/bootesdump/1992/0323/sys/src/9/boot/key.c /n/bootesdump/1992/0325/sys/src/9/boot/key.c
57c
	else if(write(fd, safe.machkey, DESKEYLEN) != DESKEYLEN)
.
49,50c
		|| write(fd, &safe, sizeof safe) != sizeof safe)
.
46c
	safe.machsum = nvcsum(safe.machkey, DESKEYLEN);
.
41,44c
		while(!passtokey(safe.machkey, password));
	}else if(nvcsum(safe.machkey, DESKEYLEN) != safe.machsum){
		warning("bad nvram key; using default password");
		/* Just so its not plain text in the binary */
		for(i=0; i<sizeof(defpass); i++)
			defpass[i] = (defpass[i]-19)^(17*(i+3));
		passtokey(safe.machkey, defpass);
.
34,35c
	|| read(fd, &safe, sizeof safe) != sizeof safe)
.
20,22c
	Nvrsafe safe;
	char password[20];
	int prompt, fd, i;
.
11,16d
6,9c
char defpass[] = {0x64, 0x3e, 0x4d, 0x13, 0x32, 0x00, 0x0b, 0xe1, 0xce};
.
## diffname boot/key.c 1992/0724
## diff -e /n/bootesdump/1992/0325/sys/src/9/boot/key.c /n/bootesdump/1992/0724/sys/src/9/boot/key.c
33,37c
		warning("bad nvram key");
		prompt = 1;
		kflag = 1;
		goto getp;
.
27a
getp:
.
13c
	int prompt, fd;
.
6,7d
## diffname boot/key.c 1992/0725
## diff -e /n/bootesdump/1992/0724/sys/src/9/boot/key.c /n/bootesdump/1992/0725/sys/src/9/boot/key.c
46a
		return;
	}
.
45c
	if(fd < 0){
.
## diffname boot/key.c 1993/0330
## diff -e /n/bootesdump/1992/0725/sys/src/9/boot/key.c /n/bootesdump/1993/0330/sys/src/9/boot/key.c
44,51c

	/* set host's key */
	if(writefile("#c/key", safe.machkey, DESKEYLEN) < 0)
		fatal("#c/key");

	/* set host's owner (and uid of current process) */
	if(writefile("#c/hostowner", safe.authid, strlen(safe.authid)) < 0)
		fatal("#c/hostowner");

	/* set host's domain */
	if(writefile("#c/hostdomain", safe.authdom, strlen(safe.authdom)) < 0)
		fatal("#c/hostdomain");
.
31,38c
		outin("authid", safe.authid, sizeof(safe.authid));
		outin("authdom", safe.authdom, sizeof(safe.authdom));
		safe.machsum = nvcsum(safe.machkey, DESKEYLEN);
		safe.authidsum = nvcsum(safe.authid, sizeof(safe.authid));
		safe.authdomsum = nvcsum(safe.authdom, sizeof(safe.authdom));
.
22,27c
	if(getsafe(safe.machkey, DESKEYLEN, &safe.machsum, "#e/password", 1) < 0)
		warning("bad nvram key");
	if(getsafe(safe.authid, NAMELEN, &safe.authidsum, "#e/authid", 0) < 0)
		warning("bad authentication id");
	if(getsafe(safe.authdom, DOMLEN, &safe.authdomsum, "#e/authdom", 0) < 0)
		warning("bad authentication domain");
	if(kflag){
.
18,20c
	if(fd < 0
	|| seek(fd, 1024+900, 0) < 0
	|| read(fd, &safe, sizeof safe) != sizeof safe){
		memset(&safe, 0, sizeof(safe));
		warning("can't read nvram");
.
16d
11d
8a
	int fd;
.
5a
char *homsg = "can't set user name or key; please reboot";

getsafe(char *field, int len, uchar *sum, char *file, int pass)
{
	char buf[64];

	if(nvcsum(field, len) != *sum){
		if(readfile(file, buf, sizeof(buf)) < 0){
			kflag |= 1;
			return -1;
		}
		memset(field, 0, len);
		if(pass)
			passtokey(field, buf);
		else
			strncpy(field, buf, len-1);
	}
	return 0;
}

.
## diffname boot/key.c 1993/0416
## diff -e /n/bootesdump/1993/0330/sys/src/9/boot/key.c /n/bootesdump/1993/0416/sys/src/9/boot/key.c
58c
		if(seek(fd, safeoff, 0) < 0
.
38c
	|| seek(fd, safeoff, 0) < 0
.
35a
	if(strcmp(cputype, "sparc") == 0)
		safeoff = 1024+850;
	else
		safeoff = 1024+900;

.
29c
	int fd, safeoff;
.
## diffname boot/key.c 1993/0501
## diff -e /n/bootesdump/1993/0416/sys/src/9/boot/key.c /n/fornaxdump/1993/0501/sys/src/brazil/boot/key.c
63c
		if(seek(fd, 1024+900, 0) < 0
.
43c
	|| seek(fd, 1024+900, 0) < 0
.
36,40d
29c
	int fd;
.
## diffname boot/key.c 1993/0725
## diff -e /n/fornaxdump/1993/0501/sys/src/brazil/boot/key.c /n/fornaxdump/1993/0725/sys/src/brazil/boot/key.c
52a

.
49a

.
48a

.
## diffname boot/key.c 1993/0916
## diff -e /n/fornaxdump/1993/0725/sys/src/brazil/boot/key.c /n/fornaxdump/1993/0916/sys/src/brazil/boot/key.c
61c
		if(seek(fd, safeoff, 0) < 0
.
55d
51d
43,49c
	check(safe.machkey, DESKEYLEN, safe.machsum, "bad nvram key");
	check(safe.authid, NAMELEN, safe.authidsum, "bad authentication id");
	check(safe.authdom, DOMLEN, safe.authdomsum, "bad authentication domain");
.
38c
	|| seek(fd, safeoff, 0) < 0
.
36c
	if(strcmp(cputype, "sparc") == 0){
		fd = open("#r/nvram", ORDWR);
		safeoff = 1024+850;
	} else if(strcmp(cputype, "386") == 0){
		fd = open("#H/hd0nvram", ORDWR);
		if(fd < 0)
			fd = open("#w/sd0nvram", ORDWR);
		safeoff = 0x0;
	} else {
		fd = open("#r/nvram", ORDWR);
		safeoff = 1024+900;
	}

.
29c
	int fd, safeoff;
.
25a
/*
 *  get info out of nvram.  since there isn't room in the PC's nvram use
 *  a disk partition there.
 */
.
10,23c
	if(nvcsum(x, len) == sum)
		return;
	memset(x, 0, len);
	kflag = 1;
	warning(msg);
.
6,8c
static void
check(void *x, int len, uchar sum, char *msg)
.
## diffname boot/key.c 1997/0909
## diff -e /n/fornaxdump/1993/0916/sys/src/brazil/boot/key.c /n/emeliedump/1997/0909/sys/src/brazil/boot/key.c
76c
	if(writefile("#c/hostdomain", safe->authdom, strlen(safe->authdom)) < 0)
.
72c
	if(writefile("#c/hostowner", safe->authid, strlen(safe->authid)) < 0)
.
68c
	if(writefile("#c/key", safe->machkey, DESKEYLEN) < 0)
.
62c
		|| write(fd, buf, safelen) != safelen)
.
55,60c
		while(!passtokey(safe->machkey, password));
		outin("authid", safe->authid, sizeof(safe->authid));
		outin("authdom", safe->authdom, sizeof(safe->authdom));
		safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
		safe->authidsum = nvcsum(safe->authid, sizeof(safe->authid));
		safe->authdomsum = nvcsum(safe->authdom, sizeof(safe->authdom));
.
49,51c
	check(safe->machkey, DESKEYLEN, safe->machsum, "bad nvram key");
	check(safe->authid, NAMELEN, safe->authidsum, "bad authentication id");
	check(safe->authdom, DOMLEN, safe->authdomsum, "bad authentication domain");
.
45,46c
	|| read(fd, buf, safelen) != safelen){
		memset(safe, 0, sizeof(safe));
.
37c
		if(fd < 0){
			fd = open("#f/fd0disk", ORDWR);
			if(fd >= 0){
				if(dirfstat(fd, &d) >= 0){
					safeoff = d.length - 512;
					safelen = 512;
				} else {
					close(fd);
					fd = -1;
				}
			}
		}
.
29a
	safe = (Nvrsafe*)buf;
	safelen = sizeof(Nvrsafe);
	safeoff = 0;

.
25a
	Dir d;
.
23,24c
	int fd, safeoff, safelen;
	char buf[1024];
	Nvrsafe *safe;
.
## diffname boot/key.c 1997/0910
## diff -e /n/emeliedump/1997/0909/sys/src/brazil/boot/key.c /n/emeliedump/1997/0910/sys/src/brazil/boot/key.c
94a
}

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 = (1 + 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 > 20)
		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;
.
52a
				safelen = 512;
.
46,49c
				safeoff = finddosfile(fd, "plan9.nvr");
				if(safeoff < 0){
.
44a
			if(fd < 0)
				fd = open("#f/fd1disk", ORDWR);
.
27d
5a
static long	finddosfile(int, char*);

.
## diffname boot/key.c 1999/0618
## diff -e /n/emeliedump/1997/0910/sys/src/brazil/boot/key.c /n/emeliedump/1999/0618/sys/src/brazil/boot/key.c
85a
//fprint(2, "hostowner = %s\n", safe->authid);
//fprint(2, "hostdomain = %s\n", safe->authdom);

.
## diffname boot/key.c 2000/0401
## diff -e /n/emeliedump/1999/0618/sys/src/brazil/boot/key.c /n/emeliedump/2000/0401/sys/src/9/boot/key.c
85,87d
40c
	} else if(strcmp(cputype, "386") == 0 || strcmp(cputype, "alpha") == 0){
.
## diffname boot/key.c 2000/0518
## diff -e /n/emeliedump/2000/0401/sys/src/9/boot/key.c /n/emeliedump/2000/0518/sys/src/9/boot/key.c
43c
			fd = open("#S/sd00/nvram", ORDWR);
.
41c
		fd = open("#S/sdC0/nvram", ORDWR);
.
## diffname boot/key.c 2000/0708
## diff -e /n/emeliedump/2000/0518/sys/src/9/boot/key.c /n/emeliedump/2000/0708/sys/src/9/boot/key.c
94a
	if(*safe->authdom == 0)
		strcpy(safe->authdom, "plan9");
.
## diffname boot/key.c 2000/0722
## diff -e /n/emeliedump/2000/0708/sys/src/9/boot/key.c /n/emeliedump/2000/0722/sys/src/9/boot/key.c
98c
		warning("#c/hostdomain");
.
92c
		warning("#c/hostowner");
.
88c
		warning("#c/key");
.
## diffname boot/key.c 2000/0825
## diff -e /n/emeliedump/2000/0722/sys/src/9/boot/key.c /n/emeliedump/2000/0825/sys/src/9/boot/key.c
41a
		if(fd < 0){
			fd = open("#S/sdC0/9fat", ORDWR);
			if(fd >= 0){
				safeoff = finddosfile(fd, "plan9.nvr");
				if(safeoff < 0){
					close(fd);
					fd = -1;
				}
print("safeoff = %d\n", safeoff);
				safelen = 512;
			}
		}
.
## diffname boot/key.c 2000/0829
## diff -e /n/emeliedump/2000/0825/sys/src/9/boot/key.c /n/emeliedump/2000/0829/sys/src/9/boot/key.c
50d
## diffname boot/key.c 2000/0914
## diff -e /n/emeliedump/2000/0829/sys/src/9/boot/key.c /n/emeliedump/2000/0914/sys/src/9/boot/key.c
215c
	if(rootsects <= 0 || rootsects > 64)
.
210c
	rootoff = (GETSHORT(b->nresrv) + b->nfats*GETSHORT(b->fatsize)) * sectsize;
.
## diffname boot/key.c 2001/0527
## diff -e /n/emeliedump/2000/0914/sys/src/9/boot/key.c /n/emeliedump/2001/0527/sys/src/9/boot/key.c
80a
#undef NAMELEN
.
79a
#define NAMELEN 28
/*BUG: THIS IS TOTALLY BOGUS */
.
## diffname boot/key.c 2001/0924
## diff -e /n/emeliedump/2001/0527/sys/src/9/boot/key.c /n/emeliedump/2001/0924/sys/src/9/boot/key.c
80,83c
	check(safe->authid, ANAMELEN, safe->authidsum, "bad authentication id");
.
## diffname boot/key.c 2001/1107 # deleted
## diff -e /n/emeliedump/2001/0924/sys/src/9/boot/key.c /n/emeliedump/2001/1107/sys/src/9/boot/key.c
1,242d

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.