~kris/9p

9hist

ref: daea032cda793d2290ff3c68fd2aac6439f4bedd 9hist/boot/key.c -rw-r--r-- 1.0 KiB
daea032c — David du Colombier Plan 9 from Bell Labs 1993-03-17 33 years ago
                                                                                
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
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <../boot/boot.h>

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

	USED(islocal);
	USED(mp);

	prompt = kflag;
	fd = open("#r/nvram", ORDWR);
	if(fd < 0){
		prompt = 1;
		warning("can't open nvram");
	}
	if(seek(fd, 1024+900, 0) < 0
	|| read(fd, &safe, sizeof safe) != sizeof safe)
		warning("can't read nvram key");

getp:
	if(prompt){
		do
			getpasswd(password, sizeof password);
		while(!passtokey(safe.machkey, password));
	}else if(nvcsum(safe.machkey, DESKEYLEN) != safe.machsum){
		warning("bad nvram key");
		prompt = 1;
		kflag = 1;
		goto getp;
	}
	safe.machsum = nvcsum(safe.machkey, DESKEYLEN);
	if(kflag){
		if(seek(fd, 1024+900, 0) < 0
		|| write(fd, &safe, sizeof safe) != sizeof safe)
			warning("can't write key to nvram");
	}
	close(fd);
	fd = open("#c/key", OWRITE);
	if(fd < 0){
		warning("can't open #c/key");
		return;
	}
	else if(write(fd, safe.machkey, DESKEYLEN) != DESKEYLEN)
		warning("can't set #c/key");
	close(fd);
}