-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauth.b
88 lines (78 loc) · 1.68 KB
/
auth.b
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
implement Authenticate, Xymodule;
include "sys.m";
sys: Sys;
include "draw.m";
include "xylib.m";
xylib: Xylib;
Value, Option: import xylib;
include "sh.m";
include "keyring.m";
keyring: Keyring;
include "security.m";
auth: Auth;
Authenticate: module {};
types(): string
{
return "ff-ks-Cs-v";
}
init()
{
sys = load Sys Sys->PATH;
xylib = load Xylib Xylib->PATH;
keyring = load Keyring Keyring->PATH;
auth = load Auth Auth->PATH;
auth->init();
}
After, Before, Create: con 1<<iota;
run(r: chan of ref Value, opts: list of Option, args: list of ref Value)
{
keyfile: string;
alg: string;
verbose: int;
reply :=<- r;
for(; opts != nil; opts = tl opts){
case (hd opts).opt {
'k' =>
keyfile = (hd (hd opts).args).gets();
if (keyfile != nil && ! (keyfile[0] == '/' || (len keyfile > 2 && keyfile[0:2] == "./")))
keyfile = "/usr/" + user() + "/keyring/" + keyfile;
'C' =>
alg = (hd (hd opts).args).gets();
'v' =>
verbose = 1;
}
}
if(keyfile == nil)
keyfile = "/usr/" + user() + "/keyring/default";
cert := keyring->readauthinfo(keyfile);
if (cert == nil) {
sys->fprint(sys->fildes(2), "auth: cannot read %q: %r", keyfile);
raise "fail:";
}
fd0 := (hd args).getfd();
eu: string;
(fd0, eu) = auth->client(alg, cert, fd0);
if(fd0 == nil){
sys->fprint(sys->fildes(2), "authentication failed: %s", eu);
reply.send(nil);
}
reply.send(ref Value.F(fd0));
}
user(): string
{
u := readfile("/dev/user");
if (u == nil)
return "nobody";
return u;
}
readfile(f: string): string
{
fd := sys->open(f, sys->OREAD);
if(fd == nil)
return nil;
buf := array[128] of byte;
n := sys->read(fd, buf, len buf);
if(n < 0)
return nil;
return string buf[0:n];
}