summaryrefslogtreecommitdiff
path: root/appl/lib/factotum.b
blob: 0b3ff1a8dfa0e4bdd7f8dd11caffafdcf404161a (plain)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
implement Factotum;

#
# client interface to factotum
#
# this is a near transliteration of Plan 9 code, subject to the Lucent Public License 1.02
#

include "sys.m";
	sys: Sys;

include "string.m";

include "factotum.m";

debug := 0;

init()
{
	sys = load Sys Sys->PATH;
}

setdebug(i: int)
{
	debug = i;
}

getaia(a: array of byte, n: int): (int, array of byte)
{
	if(len a - n < 2)
		return (-1, nil);
	c := (int a[n+1]<<8) | int a[n+0];
	n += 2;
	if(len a - n < c)
		return  (-1, nil);
	b := array[c] of byte;		# could avoid copy if known not to alias
	b[0:] = a[n: n+c];
	return (n+c, b);
}

getais(a: array of byte, n: int): (int, string)
{
	(n, a) = getaia(a, n);
	return (n, string a);
}

Authinfo.unpack(a: array of byte): (int, ref Authinfo)
{
	ai := ref Authinfo;
	n: int;
	(n, ai.cuid) = getais(a, 0);
	(n, ai.suid) = getais(a, n);
	(n, ai.cap) = getais(a, n);
	(n, ai.secret) = getaia(a, n);
	if(n < 0)
		return (-1, nil);
	return (n, ai);
}

mount(fd: ref Sys->FD, mnt: string, flags: int, aname: string, keyspec: string): (int, ref Authinfo)
{
	ai: ref Authinfo;
	afd := sys->fauth(fd, aname);
	if(afd != nil){
		ai = proxy(afd, sys->open("/mnt/factotum/rpc", Sys->ORDWR), "proto=p9any role=client "+keyspec);
		if(debug && ai == nil){
			sys->print("proxy failed: %r\n");
			return (-1, nil);
		}
	}
	return (sys->mount(fd, afd, mnt, flags, aname), ai);
}

dump(a: array of byte): string
{
	s := sys->sprint("[%d]", len a);
	for(i := 0; i < len a; i++){
		c := int a[i];
		if(c >= ' ' && c <= 16r7E)
			s += sys->sprint("%c", c);
		else
			s += sys->sprint("\\x%.2ux", c);
	}
	return s;
}

verbof(buf: array of byte): (string, array of byte)
{
	n := len buf;
	for(i:=0; i<n && buf[i] != byte ' '; i++)
		;
	s := string buf[0:i];
	if(i < n)
		i++;
	buf = buf[i:];
	case  s {
	"ok" or "error" or "done" or "phase" or
	"protocol" or "needkey" or "toosmall" or "internal" =>
		return (s, buf);
	* =>
		sys->werrstr(sys->sprint("malformed rpc response: %q", s));
		return ("rpc failure", buf);
	}
}

dorpc(fd: ref Sys->FD, verb: string, val: array of byte): (string, array of byte)
{
	(o, a) := rpc(fd, verb, val);
	if(o != "needkey" && o != "badkey")
		return (o, a);
	return ("no key", a);	# don't know how to get key
}

rpc(afd: ref Sys->FD, verb: string, a: array of byte): (string, array of byte)
{
	va := array of byte verb;
	l := len va;
	na := len a;
	if(na+l+1 > AuthRpcMax){
		sys->werrstr("rpc too big");
		return ("toobig", nil);
	}
	buf := array[na+l+1] of byte;
	buf[0:] = va;
	buf[l] = byte ' ';
	buf[l+1:] = a;
	if(debug)
		sys->print("rpc: ->%s %s\n", verb, dump(a));
	if((n:=sys->write(afd, buf, len buf)) != len buf){
		if(n >= 0)
			sys->werrstr("rpc short write");
		return ("rpc failure", nil);
	}
	buf = array[AuthRpcMax] of byte;
	if((n=sys->read(afd, buf, len buf)) < 0){
		if(debug)
			sys->print("<- (readerr) %r\n");
		return ("rpc failure", nil);
	}
	if(n < len buf)
		buf[n] = byte 0;
	buf = buf[0:n];

	#
	# Set error string for good default behavior.
	#
	s: string;
	(t, r) := verbof(buf);
	if(debug)
		sys->print("<- %s %#q\n", t, dump(r));
	case t {
	"ok" or
	"rpc failure" =>
		;	# don't touch
	"error" =>
		if(len r == 0)
			s = "unspecified rpc error";
		else
			s = sys->sprint("%s", string r);
	"needkey" =>
		s = sys->sprint("needkey %s", string r);
	"badkey" =>
		(nf, flds) := sys->tokenize(string r, "\n");
		if(nf < 2)
			s = sys->sprint("badkey %q", string r);
		else
			s = sys->sprint("badkey %q", hd tl flds);
		break;
	"phase" =>
		s = sys->sprint("phase error: %q", string r);
	* =>
		s = sys->sprint("unknown rpc type %q (bug in rpc.c)", t);
	}
	if(s != nil)
		sys->werrstr(s);
	return (t, r);
}

Authinfo.read(fd: ref Sys->FD): ref Authinfo
{
	(o, a) := rpc(fd, "authinfo", nil);
	if(o != "ok")
		return nil;
	(n, ai) := Authinfo.unpack(a);
	if(n <= 0)
		sys->werrstr("bad auth info from factotum");
	return ai;
}

proxy(fd: ref Sys->FD, afd: ref Sys->FD, params: string): ref Authinfo
{
	readc := chan of (array of byte, chan of (int, string));
	writec := chan of (array of byte, chan of (int, string));
	donec := chan of (ref Authinfo, string);
	spawn genproxy(readc, writec, donec, afd, params);
	for(;;)alt{
	(buf, reply) := <-readc =>
		n := sys->read(fd, buf, len buf);
		if(n == -1)
			reply <-= (-1, sys->sprint("%r"));
		else
			reply <-= (n, nil);
	(buf, reply) := <-writec =>
		n := sys->write(fd, buf, len buf);
		if(n == -1)
			reply <-= (-1, sys->sprint("%r"));
		else
			reply <-= (n, nil);
	(authinfo, err) := <-donec =>
		if(authinfo == nil)
			sys->werrstr(err);
		return authinfo;
	}
}

#
# do what factotum says
#
genproxy(
	readc: chan of (array of byte, chan of (int, string)),
	writec: chan of (array of byte, chan of (int, string)),
	donec: chan of (ref Authinfo, string),
	afd: ref Sys->FD,
	params: string)
{
	if(afd == nil){
		donec <-= (nil, "no authentication fd");
		return;
	}

	pa := array of byte params;
	(o, a) := dorpc(afd, "start", pa);
	if(o != "ok"){
		donec <-= (nil, sys->sprint("proxy start: %r"));
		return;
	}

	ai: ref Authinfo;
	err: string;
done:
	for(;;){
		(o, a) = dorpc(afd, "read", nil);
		case o {
		"done" =>
			if(len a > 0 && a[0] == byte 'h' && string a == "haveai")
				ai = Authinfo.read(afd);
			else
				ai = ref Authinfo;	# auth succeeded but empty authinfo
			break done;
		"ok" =>
			writec <-= (a[0:len a], reply := chan of (int, string));
			(n, e) := <-reply;
			if(n != len a){
				err = "proxy write fd: "+e;
				break done;
			}
		"phase" =>
			buf := array[AuthRpcMax] of {* => byte 0};
			n := 0;
			for(;;){
				(o, a) = dorpc(afd, "write", buf[0:n]);
				if(o != "toosmall")
					break;
				c := int string a;
				if(c > AuthRpcMax)
					break;
				readc <-= (buf[n:c], reply := chan of (int, string));
				(m, e) := <-reply;
				if(m <= 0){
					err = e;
					if(m == 0)
						err = sys->sprint("proxy short read");
					break done;
				}
				n += m;
			}
			if(o != "ok"){
				err = sys->sprint("proxy rpc write: %r");
				break done;
			}
		* =>
			err = sys->sprint("proxy rpc: %r");
			break done;
		}
	}
	donec <-= (ai, err);
}

getuserpasswd(keyspec: string): (string, string)
{
	str := load String String->PATH;
	if(str == nil)
		return (nil, nil);
	fd := sys->open("/mnt/factotum/rpc", Sys->ORDWR);
	if(fd == nil)
		return (nil, nil);
	if(((o, a) := dorpc(fd, "start", array of byte keyspec)).t0 != "ok" ||
	   ((o, a) = dorpc(fd, "read", nil)).t0 != "ok"){
		sys->werrstr("factotum: "+o);
		return (nil, nil);
	}
	flds := str->unquoted(string a);
	if(len flds != 2){
		sys->werrstr("odd response from factotum");
		return (nil, nil);
	}
	return (hd flds, hd tl flds);
}

parseattrs(s: string): list of ref Attr
{
	str := load String String->PATH;
	fld := str->unquoted(s);
	rfld := fld;
	for(fld = nil; rfld != nil; rfld = tl rfld)
		fld = (hd rfld) :: fld;
	attrs: list of ref Attr;
	for(; fld != nil; fld = tl fld){
		n := hd fld;
		a := "";
		tag := Aattr;
		for(i:=0; i<len n; i++)
			if(n[i] == '='){
				a = n[i+1:];
				n = n[0:i];
				tag = Aval;
			}
		if(len n == 0)
			continue;
		if(tag == Aattr && len n > 1 && n[len n-1] == '?'){
			tag = Aquery;
			n = n[0:len n-1];
		}
		attrs = ref Attr(tag, n, a) :: attrs;
	}
	# TO DO: eliminate answered queries
	return attrs;
}

Attr.text(a: self ref Attr): string
{
	case a.tag {
	Aattr =>
		return a.name;
	Aval =>
		return sys->sprint("%q=%q", a.name, a.val);
	Aquery =>
		return sys->sprint("%q?", a.name);
	* =>
		return "??";
	}
}

attrtext(attrs: list of ref Attr): string
{
	s := "";
	for(; attrs != nil; attrs = tl attrs){
		if(s != nil)
			s[len s] = ' ';
		s += (hd attrs).text();
	}
	return s;
}

findattr(attrs: list of ref Attr, n: string): ref Attr
{
	for(; attrs != nil; attrs = tl attrs)
		if((a := hd attrs).tag != Aquery && a.name == n)
			return a;
	return nil;
}

findattrval(attrs: list of ref Attr, n: string): string
{
	if((a := findattr(attrs, n)) != nil)
		return a.val;
	return nil;
}

delattr(l: list of ref Attr, n: string): list of ref Attr
{
	rl: list of ref Attr;
	for(; l != nil; l = tl l)
		if((hd l).name != n)
			rl = hd l :: rl;
	return rev(rl);
}

copyattrs(l: list of ref Attr): list of ref Attr
{
	rl: list of ref Attr;
	for(; l != nil; l = tl l)
		rl = hd l :: rl;
	return rev(rl);
}

takeattrs(l: list of ref Attr, names: list of string): list of ref Attr
{
	rl: list of ref Attr;
	for(; l != nil; l = tl l){
		n := (hd l).name;
		for(nl := names; nl != nil; nl = tl nl)
			if((hd nl) == n){
				rl = hd l :: rl;
				break;
			}
	}
	return rev(rl);
}

publicattrs(l: list of ref Attr): list of ref Attr
{
	rl: list of ref Attr;
	for(; l != nil; l = tl l){
		a := hd l;
		if(a.tag != Aquery || a.val == nil)
			rl = a :: rl;
	}
	return rev(rl);
}

rev[T](l: list of T): list of T
{
	rl: list of T;
	for(; l != nil; l = tl l)
		rl = hd l :: rl;
	return rl;
}