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
|
implement Dial;
include "sys.m";
sys: Sys;
include "draw.m";
include "arg.m";
include "keyring.m";
keyring: Keyring;
include "security.m";
auth: Auth;
include "sh.m";
sh: Sh;
Context: import sh;
Dial: module {
init: fn(nil: ref Draw->Context, argv: list of string);
};
badmodule(p: string)
{
sys->fprint(stderr(), "dial: cannot load %s: %r\n", p);
raise "fail:bad module";
}
DEFAULTALG := "none";
verbose := 0;
init(drawctxt: ref Draw->Context, argv: list of string)
{
sys = load Sys Sys->PATH;
keyring = load Keyring Keyring->PATH;
auth = load Auth Auth->PATH;
if (auth == nil)
badmodule(Auth->PATH);
arg := load Arg Arg->PATH;
if (arg == nil)
badmodule(Arg->PATH);
sh = load Sh Sh->PATH;
if (sh == nil)
badmodule(Sh->PATH);
auth->init();
alg: string;
keyfile: string;
doauth := 1;
arg->init(argv);
arg->setusage("dial [-A] [-k keyfile] [-a alg] addr command [arg...]");
while ((opt := arg->opt()) != 0) {
case opt {
'A' =>
doauth = 0;
'a' =>
alg = arg->earg();
'f' or
'k' =>
keyfile = arg->earg();
if (! (keyfile[0] == '/' || (len keyfile > 2 && keyfile[0:2] == "./")))
keyfile = "/usr/" + user() + "/keyring/" + keyfile;
'v' =>
verbose = 1;
* =>
arg->usage();
}
}
argv = arg->argv();
if (len argv < 2)
arg->usage();
arg = nil;
(addr, shcmd) := (hd argv, tl argv);
if (doauth && alg == nil)
alg = DEFAULTALG;
if (alg != nil && keyfile == nil) {
kd := "/usr/" + user() + "/keyring/";
if (exists(kd + addr))
keyfile = kd + addr;
else
keyfile = kd + "default";
}
cert: ref Keyring->Authinfo;
if (alg != nil) {
cert = keyring->readauthinfo(keyfile);
if (cert == nil) {
sys->fprint(stderr(), "dial: cannot read %s: %r\n", keyfile);
raise "fail:bad keyfile";
}
}
(ok, c) := sys->dial(addr, nil);
if (ok == -1) {
sys->fprint(stderr(), "dial: cannot dial %s:: %r\n", addr);
raise "fail:errors";
}
user: string;
if (alg != nil) {
err: string;
(c.dfd, err) = auth->client(alg, cert, c.dfd);
if (c.dfd == nil) {
sys->fprint(stderr(), "dial: authentication failed: %s\n", err);
raise "fail:errors";
}
user = err;
}
sys->dup(c.dfd.fd, 0);
sys->dup(c.dfd.fd, 1);
c.dfd = c.cfd = nil;
ctxt := Context.new(drawctxt);
if (user != nil)
ctxt.set("user", sh->stringlist2list(user :: nil));
else
ctxt.set("user", nil);
ctxt.set("net", ref Sh->Listnode(nil, c.dir) :: nil);
ctxt.run(sh->stringlist2list(shcmd), 1);
}
exists(f: string): int
{
(ok, nil) := sys->stat(f);
return ok != -1;
}
stderr(): ref Sys->FD
{
return sys->fildes(2);
}
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];
}
|