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
|
implement WmLogon;
#
# get a certificate to enable remote access.
#
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
Screen, Display, Image, Context, Point, Rect: import draw;
include "tk.m";
tk: Tk;
include "tkclient.m";
tkclient: Tkclient;
include "arg.m";
include "sh.m";
include "newns.m";
include "keyring.m";
keyring: Keyring;
include "security.m";
login: Login;
# XXX where to put the certificate: is the username already set to
# something appropriate, with a home directory and keyring directory in that?
# how do we find out the signer; presumably from the registry?
# should do that before signing on; if we can't get it, then prompt for it.
WmLogon: module {
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
cfg := array[] of {
"label .p -bitmap @/icons/inferno.bit -borderwidth 2 -relief raised",
"label .ul -text {User Name:} -anchor w",
"entry .ue -bg white",
"label .pl -text {Password:} -anchor w",
"entry .pe -bg white -show *",
"frame .f -borderwidth 2 -relief raised",
"grid .ul .ue -in .f",
"grid .pl .pe -in .f",
"pack .p .f -fill x",
"bind .ue <Key-\n> {focus next}",
"bind .ue {<Key-\t>} {focus next}",
"bind .pe <Key-\n> {send cmd ok}",
"bind .pe {<Key-\t>} {focus next}",
"focus .e",
};
init(ctxt: ref Draw->Context, args: list of string)
{
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
tk = load Tk Tk->PATH;
tkclient = load Tkclient Tkclient->PATH;
if(tkclient == nil){
sys->fprint(stderr(), "logon: cannot load %s: %r\n", Tkclient->PATH);
raise "fail:bad module";
}
login = load Login Login->PATH;
if(login == nil){
sys->fprint(stderr(), "logon: cannot load %s: %r\n", Login->PATH);
raise "fail:bad module";
}
keyring = load Keyring Keyring->PATH;
if(keyring == nil){
sys->fprint(stderr(), "logon: cannot load %s: %r\n", Keyring->PATH);
raise "fail:bad module";
}
sys->pctl(sys->NEWPGRP, nil);
tkclient->init();
(ctlwin, nil) := tkclient->toplevel(ctxt, nil, nil, Tkclient->Plain);
if(sys->fprint(ctlwin.ctxt.connfd, "request") == -1){
sys->fprint(stderr(), "logon: must be run as principal wm application\n");
raise "fail:lack of control";
}
addr: con "tcp!127.0.0.1!inflogin";
usr := "";
passwd := "";
arg := load Arg Arg->PATH;
if(arg != nil){
arg->init(args);
arg->setusage("usage: logon [-u user] [-p passwd] command [arg...]]\n");
while((opt := arg->opt()) != 0){
case opt{
'u' =>
usr = arg->earg();
'p' =>
passwd = arg->earg();
* =>
arg->usage();
}
}
args = arg->argv();
arg = nil;
} else
args = nil;
if(ctxt == nil)
sys->fprint(stderr(), "logon: must run under a window manager\n");
if (usr == nil || !logon(ctxt, usr, passwd, addr)) {
(panel, cmd) := makepanel(ctxt);
stop := chan of int;
spawn tkclient->handler(panel, stop);
for(;;) {
tk->cmd(panel, "focus .ue; update");
<-cmd;
usr = tk->cmd(panel, ".ue get");
if(usr == nil) {
notice(ctxt, "You must supply a user name to login");
continue;
}
passwd = tk->cmd(panel, ".pe get");
if(logon(ctxt, usr, passwd, addr)) {
panel = nil;
stop <-= 1;
break;
}
tk->cmd(panel, ".ue delete 0 end");
tk->cmd(panel, ".pe delete 0 end");
}
}
(ok, nil) := sys->stat("namespace");
if(ok >= 0) {
ns := load Newns Newns->PATH;
if(ns == nil)
notice(ctxt, "failed to load namespace builder");
else if ((nserr := ns->newns(nil, nil)) != nil)
notice(ctxt, "namespace error:\n"+nserr);
}
tkclient->wmctl(ctlwin, "endcontrol");
errch := chan of string;
spawn exec(ctxt, args, errch);
err := <-errch;
if (err != nil) {
sys->fprint(stderr(), "logon: %s\n", err);
raise "fail:exec failed";
}
}
makepanel(ctxt: ref Draw->Context): (ref Tk->Toplevel, chan of string)
{
(t, nil) := tkclient->toplevel(ctxt, "-bg silver", nil, Tkclient->Plain);
cmd := chan of string;
tk->namechan(t, cmd, "cmd");
for(i := 0; i < len cfg; i++)
tk->cmd(t, cfg[i]);
err := tk->cmd(t, "variable lasterr");
if(err != nil) {
sys->fprint(stderr(), "logon: tk error: %s\n", err);
raise "fail:config error";
}
tk->cmd(t, "update");
org: Point;
ir := tk->rect(t, ".", Tk->Border|Tk->Required);
org.x = t.screenr.dx() / 2 - ir.dx() / 2;
org.y = t.screenr.dy() / 3 - ir.dy() / 2;
if (org.y < 0)
org.y = 0;
tk->cmd(t, ". configure -x " + string org.x + " -y " + string org.y);
tkclient->startinput(t, "kbd" :: "ptr" :: nil);
tkclient->onscreen(t, "onscreen");
return (t, cmd);
}
exec(ctxt: ref Draw->Context, argv: list of string, errch: chan of string)
{
sys->pctl(sys->NEWFD, 0 :: 1 :: 2 :: nil);
if(argv == nil)
argv = "/dis/wm/toolbar.dis" :: nil;
else {
sh := load Sh Sh->PATH;
if(sh != nil){
sh->run(ctxt, "{$* &}" :: argv);
errch <-= nil;
exit;
}
}
{
cmd := load Command hd argv;
if (cmd == nil) {
errch <-= sys->sprint("cannot load %s: %r", hd argv);
} else {
errch <-= nil;
spawn cmd->init(ctxt, argv);
}
}exception{
"fail:*" =>
exit;
}
}
logon(ctxt: ref Draw->Context, uname, passwd, addr: string): int
{
(err, info) := login->login(uname, passwd, addr);
if(err != nil){
notice(ctxt, "Login failed:\n" + err);
return 0;
}
keys := "/usr/" + user() + "/keyring";
if(sys->bind("#s", keys, Sys->MBEFORE) == -1){
notice(ctxt, sys->sprint("Cannot access keyring: %r"));
return 0;
}
fio := sys->file2chan(keys, "default");
if(fio == nil){
notice(ctxt, sys->sprint("Cannot create key file: %r"));
return 0;
}
sync := chan of int;
spawn infofile(fio, sync);
<-sync;
if(keyring->writeauthinfo(keys + "/default", info) == -1){
notice(ctxt, sys->sprint("Cannot write key file: %r"));
return 0;
}
return 1;
}
notecmd := array[] of {
"frame .f",
"label .f.l -bitmap error -foreground red",
"button .b -text Continue -command {send cmd done}",
"focus .f",
"bind .f <Key-\n> {send cmd done}",
"pack .f.l .f.m -side left -expand 1",
"pack .f .b",
"pack propagate . 0",
};
centre(t: ref Tk->Toplevel)
{
sz := Point(int tk->cmd(t, ". cget -width"), int tk->cmd(t, ". cget -height"));
r := t.screenr;
if (sz.x > r.dx())
tk->cmd(t, ". configure -width " + string r.dx());
org: Point;
org.x = r.dx() / 2 - tk->rect(t, ".", 0).dx() / 2;
org.y = r.dy() / 3 - tk->rect(t, ".", 0).dy() / 2;
if (org.y < 0)
org.y = 0;
tk->cmd(t, ". configure -x " + string org.x + " -y " + string org.y);
}
notice(ctxt: ref Draw->Context, message: string)
{
(t, nil) := tkclient->toplevel(ctxt, "-borderwidth 2 -relief raised", nil, Tkclient->Plain);
cmd := chan of string;
tk->namechan(t, cmd, "cmd");
tk->cmd(t, "label .f.m -anchor nw -text '"+message);
for(i := 0; i < len notecmd; i++)
tk->cmd(t, notecmd[i]);
centre(t);
tkclient->onscreen(t, "onscreen");
tkclient->startinput(t, "kbd"::"ptr"::nil);
stop := chan of int;
spawn tkclient->handler(t, stop);
tk->cmd(t, "update; cursor -default");
<-cmd;
stop <-= 1;
}
stderr(): ref Sys->FD
{
return sys->fildes(2);
}
user(): string
{
fd := sys->open("/dev/user", Sys->OREAD);
buf := array[8192] of byte;
if((n := sys->read(fd, buf, len buf)) > 0)
return string buf[0:n];
return "none";
}
infofile(fileio: ref Sys->FileIO, sync: chan of int)
{
sys->pctl(Sys->NEWPGRP|Sys->NEWFD|Sys->NEWNS, nil);
sync <-= 1;
infodata: array of byte;
for(;;) alt {
(off, nbytes, fid, rc) := <-fileio.read =>
if(rc == nil)
break;
if(off > len infodata)
off = len infodata;
rc <-= (infodata[off:], nil);
(off, data, fid, wc) := <-fileio.write =>
if(wc == nil)
break;
if(off != len infodata){
wc <-= (0, "cannot be rewritten");
} else {
nid := array[len infodata+len data] of byte;
nid[0:] = infodata;
nid[len infodata:] = data;
infodata = nid;
wc <-= (len data, nil);
}
}
}
|