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
|
implement CPU;
include "sys.m";
sys: Sys;
stderr: ref Sys->FD;
include "draw.m";
include "string.m";
str: String;
include "arg.m";
include "keyring.m";
include "security.m";
include "dial.m";
DEFCMD: con "/dis/sh";
CPU: module
{
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
badmodule(p: string)
{
sys->fprint(stderr, "cpu: cannot load %s: %r\n", p);
raise "fail:bad module";
}
usage()
{
sys->fprint(stderr, "Usage: cpu [-C cryptoalg] mach command args...\n");
raise "fail:usage";
}
# The default level of security is NOSSL, unless
# the keyring directory doesn't exist, in which case
# it's disallowed.
init(nil: ref Draw->Context, argv: list of string)
{
sys = load Sys Sys->PATH;
stderr = sys->fildes(2);
arg := load Arg Arg->PATH;
if (arg == nil) badmodule(Arg->PATH);
str = load String String->PATH;
if (str == nil) badmodule(String->PATH);
au := load Auth Auth->PATH;
if (au == nil) badmodule(Auth->PATH);
kr := load Keyring Keyring->PATH;
if (kr == nil) badmodule(Keyring->PATH);
dial := load Dial Dial->PATH;
arg->init(argv);
alg := "";
while ((opt := arg->opt()) != 0) {
if (opt == 'C') {
alg = arg->arg();
} else
usage();
}
argv = arg->argv();
args := "auxi/cpuslave";
# if(ctxt != nil && ctxt.screen != nil)
# args += " -s" + string ctxt.screen.id;
# else
args += " --";
mach: string;
case len argv {
0 =>
usage();
1 =>
mach = hd argv;
args += " " + DEFCMD;
* =>
mach = hd argv;
args += " " + str->quoted(tl argv);
}
user := getuser();
kd := "/usr/" + user + "/keyring/";
cert := kd + dial->netmkaddr(mach, "tcp", "");
if (!exists(cert)) {
cert = kd + "default";
if (!exists(cert)) {
sys->fprint(stderr, "cpu: cannot find certificate in %s; use getauthinfo\n", kd);
raise "fail:no certificate";
}
}
c := dial->dial(dial->netmkaddr(mach, "net", "rstyx"), nil);
if(c == nil){
sys->fprint(stderr, "cpu: can't dial %s: %r\n", mach);
raise "fail:dial";
}
ai := kr->readauthinfo(cert);
if (alg == nil)
alg = "none";
err := au->init();
if(err != nil) {
sys->fprint(stderr, "cpu: cannot initialise auth module: %s\n", err);
raise "fail:auth init failed";
}
fd := ref Sys->FD;
#sys->fprint(stderr, "cpu: authenticating using alg '%s'\n", alg);
(fd, err) = au->client(alg, ai, c.dfd);
if(fd == nil) {
sys->fprint(stderr, "cpu: authentication failed: %s\n", err);
raise "fail:authentication failure";
}
t := array of byte sys->sprint("%d\n%s\n", len (array of byte args)+1, args);
if(sys->write(fd, t, len t) != len t){
sys->fprint(stderr, "cpu: export args write error: %r\n");
raise "fail:write error";
}
if(sys->export(fd, "/", sys->EXPWAIT) < 0){
sys->fprint(stderr, "cpu: export failed: %r\n");
raise "fail:export error";
}
}
exists(file: string): int
{
(ok, nil) := sys->stat(file);
return ok != -1;
}
getuser(): string
{
fd := sys->open("/dev/user", sys->OREAD);
if(fd == nil){
sys->fprint(stderr, "cpu: cannot open /dev/user: %r\n");
raise "fail:no user id";
}
buf := array[50] of byte;
n := sys->read(fd, buf, len buf);
if(n < 0){
sys->fprint(stderr, "cpu: cannot read /dev/user: %r\n");
raise "fail:no user id";
}
return string buf[0:n];
}
|