summaryrefslogtreecommitdiff
path: root/os/init/init.b
blob: 655b0cf3f4406e7f2b402fb9e5272ab1b3c14907 (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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
implement Init;

include "sys.m";
sys: Sys;
FD, Connection, sprint, Dir: import sys;
print, fprint, open, bind, mount, dial, sleep, read: import sys;

include "draw.m";
draw: Draw;
Context, Display, Font, Rect, Point, Image, Screen: import draw;

include "prefab.m";
prefab: Prefab;
Environ, Element, Compound, Style: import prefab;

include "mpeg.m";

include "ir.m";
tirc: chan of int;	# translated remote input (from irslave)
irstopc: chan of int;	# channel to irslave

include "keyring.m";
kr: Keyring;
IPint: import kr;

Init: module
{
	init:	fn();
};

Shell: module
{
	init:	fn(ctxt: ref Context, argv: list of string);
};

Signon: con "Dialing Local Service Provider\nWait a moment ...";
Login:  con "Connected to Service Provider";
Intro:	con "/mpeg/youwill2";
Garden:	con "The Garden of Delights\nHieronymus Bosch";

rootfs(server: string): int
{
	ok, n: int;
	c: Connection;
	err: string;

	(ok, c) = dial("tcp!" + server + "!6666", nil);
	if(ok < 0)
		return -1;

	if(kr != nil){
		ai := kr->readauthinfo("/nvfs/default");
		if(ai == nil){
			(ai, err) = register(server);
			if(err != nil){
				status("registration failed: "+err+"\nPress a key on your remote\ncontrol to continue.");
				# register() may have failed before Ir loaded.
				if(tirc!=nil){
					<-tirc;
					irstopc <-= 1;
				}
			}
			statusbox = nil;
		}
		(id_or_err, secret) := kr->auth(c.dfd, ai, 0);
		if(secret == nil){
			status("authentication failed: "+err);
			sys->sleep(2000);
			statusbox = nil;
			(ai, err) = register(server);
			if(err != nil){
				status("registration failed: "+err+"\nPress a key on your remote\ncontrol to continue.");
				# register() may have failed before Ir loaded.
				if(tirc!=nil){
					<-tirc;
					irstopc <-= 1;
				}
			}
			statusbox = nil;
		} else {
			# no line encryption
			algbuf := array of byte "none";
			kr->sendmsg(c.dfd, algbuf, len algbuf);
		}
	}

	c.cfd = nil;
	n = mount(c.dfd, nil, "/", sys->MREPL, "");
	if(n > 0)
		return 0;
	return -1;
}

ones: ref Image;
screen: ref Screen;
menuenv, tvenv: ref Environ;
Bootpreadlen: con 128;
textfont: ref Font;
disp: ref Display;
env: ref Environ;
statusbox: ref Compound;

init()
{
	shell: Shell;
	nr, ntok: int;
	c: ref Compound;
	ls: list of string;
	le, te, xe: ref Element;
	spec: string;

	sys = load Sys Sys->PATH;
	draw = load Draw Draw->PATH;
	prefab = load Prefab Prefab->PATH;
	kr = load Keyring Keyring->PATH;
	
	disp = Display.allocate(nil);
	ones = disp.ones;

	textfont = Font.open(disp, "*default*");
	screencolor := disp.rgb(161, 195, 209);

	menustyle := ref Style(
			textfont,			# titlefont
			textfont,			# textfont
			disp.color(16r55),		# elemcolor
			disp.color(draw->Black),	# edgecolor
			disp.color(draw->Yellow),	# titlecolor	
			disp.color(draw->Black),	# textcolor
			disp.color(draw->White));	# highlightcolor

	screen = Screen.allocate(disp.image, screencolor, 0);
	screen.image.draw(screen.image.r, screencolor, ones, (0, 0));
	menuenv = ref Environ(screen, menustyle);

	logo := disp.open("/lucent");
	phone := disp.open("/phone");
	if(phone == nil  || logo == nil) {
		print("open: /phone or /lucent: %r\n");
		exit;
	}

	#
	# Setup what we need to call a server and
	# Authenticate
	#
	bind("#l", "/net", sys->MREPL);
	bind("#I", "/net", sys->MAFTER);
	bind("#c", "/dev", sys->MAFTER);
	bind("#H", "/dev", sys->MAFTER);
	nvramfd := sys->open("#H/hd0nvram", sys->ORDWR);
	if(nvramfd != nil){
		spec = sys->sprint("#Fhd0nvram", nvramfd.fd);
		if(bind(spec, "/nvfs", sys->MAFTER|sys->MCREATE) < 0)
			print("init: bind %s: %r\n", spec);
	}

	setsysname();	# set up system name

	fd := open("/net/ipifc", sys->OWRITE);
	if(fd == nil) {
		print("init: open /net/ipifc: %r");
		exit;
	}
	fprint(fd, "bootp /net/ether0");

	fd = open("/net/bootp", sys->OREAD);
	if(fd == nil) {
		print("init: open /net/bootp: %r");
		exit;
	}

	buf := array[Bootpreadlen] of byte;
	nr = read(fd, buf, len buf);
	fd = nil;
	if(nr <= 0) {
		print("init: read /net/bootp: %r");
		exit;
	}

	(ntok, ls) = sys->tokenize(string buf, " \t\n");
	while(ls != nil) {
		if(hd ls == "fsip"){
			ls = tl ls;
			break;
		}
		ls = tl ls;
	}
	if(ls == nil) {
		print("init: server address not in bootp read");
		exit;
	}

	zr := Rect((0,0), (0,0));

	le = Element.icon(menuenv, logo.r, logo, ones);
	le = Element.elist(menuenv, le, Prefab->EVertical);
	xe = Element.icon(menuenv, phone.r, phone, ones);
	xe = Element.elist(menuenv, xe, Prefab->EHorizontal);
	te = Element.text(menuenv, Signon, zr, Prefab->EText);
	xe.append(te);
	xe.adjust(Prefab->Adjpack, Prefab->Adjleft);
	le.append(xe);
	le.adjust(Prefab->Adjpack, Prefab->Adjup);
	c = Compound.box(menuenv, (150, 100),
	Element.text(menuenv, "Inferno", zr, Prefab->ETitle), le);
	c.draw();

	while(rootfs(hd ls) < 0)
		sleep(1000);

	#
	# default namespace
	#
	bind("#c", "/dev", sys->MBEFORE);		# console
	bind("#H", "/dev", sys->MAFTER);
	if(spec != nil)
		bind(spec, "/nvfs", sys->MBEFORE|sys->MCREATE);	# our keys
	bind("#E", "/dev", sys->MBEFORE);		# mpeg
	bind("#l", "/net", sys->MBEFORE);		# ethernet
	bind("#I", "/net", sys->MBEFORE);		# TCP/IP
	bind("#V", "/dev", sys->MAFTER);		# hauppauge TV
	bind("#p", "/prog", sys->MREPL);		# prog device
	sys->bind("#d", "/fd", Sys->MREPL);

	setclock();

	le = Element.icon(menuenv, logo.r, logo, ones);
	le = Element.elist(menuenv, le, Prefab->EVertical);
	xe = Element.text(menuenv, Login, zr, Prefab->EText);
	le.append(xe);

	i := disp.newimage(Rect((0, 0), (320, 240)), 3, 0, 0);
	i.draw(i.r, menustyle.elemcolor, ones, i.r.min);
	xe = Element.icon(menuenv, i.r, i, ones);
	le.append(xe);

	le.adjust(Prefab->Adjpack, Prefab->Adjup);
	c = Compound.box(menuenv, (160, 50),
	Element.text(menuenv, "Inferno", zr, Prefab->ETitle), le);
	c.draw();

	xc: chan of string;
	mpeg := load Mpeg Mpeg->PATH;
	if(mpeg != nil) {
		xc = chan of string;
		r := (hd tl tl c.contents.kids).r;
		s := mpeg->play(disp, c.image, 1, r, Intro, xc);
		if(s != "") {
			print("mpeg: %s\n", s);
			xc = nil;
		}
	}

	i2 := disp.open("/icons/delight.bit");
	i.draw(i.r, i2, ones, i2.r.min);
	i2 = nil;
	if(xc != nil)
		<-xc;

	le.append(Element.text(menuenv, Garden, le.r, Prefab->EText));
	le.adjust(Prefab->Adjpack, Prefab->Adjup);
	c = Compound.box(menuenv, (160, 50),
	Element.text(menuenv, "Inferno", zr, Prefab->ETitle), le);
	c.draw();

	sleep(5000);

	# Do a bind to force applications to use IR module built
	# into the kernel.
	if(bind("#/./ir", Ir->PATH, sys->MREPL) < 0)
		print("init: bind ir: %r\n");
	# Uncomment the next line to load sh.dis.
#	shell = load Shell "/dis/sh.dis";
	dc : ref Context;
	# Comment the next 2 lines to load sh.dis.
	shell = load Shell "/dis/mux/mux.dis";
	dc = ref Context(screen, disp, nil, nil, nil, nil, nil);
	if(shell == nil) {
		print("init: load /dis/sh.dis: %r");
		exit;
	}
	shell->init(dc, nil);
}

setclock()
{
	(ok, dir) := sys->stat("/");
	if (ok < 0) {
		print("init: stat /: %r");
		return;
	}

	fd := sys->open("/dev/time", sys->OWRITE);
	if (fd == nil) {
		print("init: open /dev/time: %r");
		return;
	}

	# Time is kept as microsecs, atime is in secs
	b := array of byte sprint("%d000000", dir.atime);
	if (sys->write(fd, b, len b) != len b)
		print("init: write /dev/time: %r");
}

register(signer: string): (ref Keyring->Authinfo, string)
{

	# get box id
	fd := sys->open("/nvfs/ID", sys->OREAD);
	if(fd == nil){
		fd = sys->create("/nvfs/ID", sys->OWRITE, 8r664);
		if(fd == nil)
			return  (nil, "can't create /nvfs/ID");
		if(sys->fprint(fd, "LT%d", randomint()) < 0)
			return  (nil, "can't write /nvfs/ID");
		fd = sys->open("/nvfs/ID", sys->OREAD);
	}
	if(fd == nil)
		return  (nil, "can't open /nvfs/ID");

	buf := array[64] of byte;
	n := sys->read(fd, buf, (len buf) - 1);
	if(n <= 0)
		return (nil, "can't read /nvfs/ID");

	boxid := string buf[0:n];
	fd = nil;
	buf = nil;

	# Set-up for user input via remote control.
	tirc = chan of int;
	irstopc = chan of int;
	spawn irslave(tirc, irstopc);
	case dialogue("Register with your service provider?", "yes\nno") {
	0 =>
		;
	* =>
		return (nil, "registration not desired");
	}

	# a holder
	info := ref Keyring->Authinfo;

	# contact signer
#	status("looking for signer");
#	signer := virgil->virgil("$SIGNER");
#	if(signer == nil)
#		return (nil, "can't find signer");
	status("dialing tcp!"+signer+"!6671");
	(ok, c) := sys->dial("tcp!"+signer+"!6671", nil);
	if(!ok)
		return (nil, "can't contact signer");

	# get signer's public key and diffie helman parameters
	status("getting signer's key");
	spkbuf := kr->getmsg(c.dfd);
	if(spkbuf == nil)
		return (nil, "can't read signer's key");
	info.spk = kr->strtopk(string spkbuf);
	if(info.spk == nil)
		return (nil, "bad key from signer");
	alphabuf := kr->getmsg(c.dfd);
	if(alphabuf == nil)
		return (nil, "can't read dh alpha");
	info.alpha = IPint.b64toip(string alphabuf);
	pbuf := kr->getmsg(c.dfd);
	if(pbuf == nil)
		return (nil, "can't read dh mod");
	info.p = IPint.b64toip(string pbuf);

	# generate our key from system parameters
	status("generating our key");
	info.mysk = kr->genSKfromPK(info.spk, boxid);
	if(info.mysk == nil)
		return (nil, "can't generate our own key");
	info.mypk = kr->sktopk(info.mysk);

	# send signer our public key
	mypkbuf := array of byte kr->pktostr(info.mypk);
	kr->sendmsg(c.dfd, mypkbuf, len mypkbuf);

	# get blind certificate
	status("getting blinded certificate");
	certbuf := kr->getmsg(c.dfd);
	if(certbuf == nil)
		return (nil, "can't read signed key");

	# verify we've got the right stuff
	if(!verify(boxid, spkbuf, mypkbuf, certbuf))
		return (nil, "verification failed, try again");

	# contact counter signer
	status("dialing tcp!"+signer+"!6672");
	(ok, c) = sys->dial("tcp!"+signer+"!6672", nil);
	if(!ok)
		return (nil, "can't contact countersigner");

	# send boxid
	buf = array of byte boxid;
	kr->sendmsg(c.dfd, buf, len buf);

	# get blinding mask
	status("unblinding certificate");
	mask := kr->getmsg(c.dfd);
	if(len mask != len certbuf)
		return (nil, "bad mask length");
	for(i := 0; i < len mask; i++)
		certbuf[i] = certbuf[i] ^ mask[i];
	info.cert = kr->strtocert(string certbuf);

	status("verifying certificate");
	state := kr->sha(mypkbuf, len mypkbuf, nil, nil);
	if(kr->verify(info.spk, info.cert, state) == 0)
		return (nil, "bad certificate");

	status("storing keys");
	kr->writeauthinfo("/nvfs/default", info);
	
	status("Congratulations, you are registered.\nPress a key to continue.");
	<-tirc;
	irstopc <-= 1;

	return (info, nil);
}

dialogue(expl: string, selection: string): int
{
	c := Compound.textbox(menuenv, ((100, 100), (100, 100)), expl, selection);
	c.draw();
	for(;;){
		(key, index, nil) := c.select(c.contents, 0, tirc);
		case key {
		Ir->Select =>
			return index;
		Ir->Enter =>
			return -1;
		}
	}
}

status(expl: string)
{
#	title := Element.text(menuenv, "registration\nstatus", ((0,0),(0,0)), Prefab->ETitle);
#	msg := Element.text(menuenv, expl, ((0,0),(0,0)), Prefab->EText);
#	c := Compound.box(menuenv, (100, 100), title, msg);

	c := Compound.textbox(menuenv, ((100, 100),(100,100)), "Registration status", expl);
	c.draw();
	statusbox = c;
}

pro:= array[] of {
	"alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf",
	"hotel", "india", "juliet", "kilo", "lima", "mike", "nancy", "oscar",
	"poppa", "quebec", "romeo", "sierra", "tango", "uniform",
	"victor", "whiskey", "xray", "yankee", "zulu"
};

#
#  prompt for acceptance
#
verify(boxid: string, hispk, mypk, cert: array of byte): int
{
	s: string;

	# hash the string
	state := kr->md5(hispk, len hispk, nil, nil);
	kr->md5(mypk, len mypk, nil, state);
	digest := array[Keyring->MD5dlen] of byte;
	kr->md5(cert, len cert, digest, state);

	title := Element.elist(menuenv, nil, Prefab->EVertical);
	subtitle := Element.text(menuenv, "Telephone your service provider\n to register.  You will need\nthe following:\n", ((0,0),(0,0)), Prefab->ETitle);
	title.append(subtitle);

	line := Element.text(menuenv, "boxid is '"+boxid+"'.", ((0,0),(0,0)), Prefab->ETitle);
	title.append(line);
	for(i := 0; i < len digest; i++){
		line = Element.elist(menuenv, nil, Prefab->EHorizontal);
		s = (string (2*i)) + ": " + pro[((int digest[i])>>4)%len pro];
		line.append(Element.text(menuenv, s, ((0,0),(0,0)), Prefab->ETitle));

		s = (string (2*i+1)) + ": " + pro[(int digest[i])%len pro] + "\n";
		line.append(Element.text(menuenv, s, ((0,0),(200,0)), Prefab->ETitle));

		line.adjust(Prefab->Adjequal, Prefab->Adjleft);
		title.append(line);
	}
	title.adjust(Prefab->Adjpack, Prefab->Adjleft);

	le := Element.elist(menuenv, nil, Prefab->EHorizontal);
	le.append(Element.text(menuenv, " accept ", ((0, 0), (0, 0)), Prefab->EText));
	le.append(Element.text(menuenv, " reject ", ((0, 0), (0, 0)), Prefab->EText));
	le.adjust(Prefab->Adjpack, Prefab->Adjleft);

	c := Compound.box(menuenv, (50, 50), title, le);
	c.draw();

	for(;;){
		(key, index, nil) := c.select(c.contents, 0, tirc);
		case key {
		Ir->Select =>
			if(index == 0)
				return 1;
			return 0;
		Ir->Enter =>
			return 0;
		}
	}

	return 0;
}

randomint(): int
{
	fd := sys->open("/dev/random", sys->OREAD);
	if(fd == nil)
		return 0;
	buf := array[4] of byte;
	sys->read(fd, buf, 4);
	rand := 0;
	for(i := 0; i < 4; i++)
		rand = (rand<<8) | int buf[i];
	return rand;
}

# Reads real (if possible) or simulated remote, returns Ir events on irc.
# Must be a separate thread to be able to 1) read raw Ir input channel
# and 2) write translated Ir input data on output channel.
irslave(irc, stopc: chan of int)
{
	in, irpid: int;
	buf: list of int;
	outc: chan of int;

	irchan := chan of int;	# Untranslated Ir input channel.
	irpidch := chan of int;	# Ir reader pid channel.
	irmod := load Ir "#/./ir";	# Module built into kernel.

	if(irmod==nil){
		print("irslave: failed to load #/./ir");
		return;
	}
	if(irmod->init(irchan, irpidch)<0){
		print("irslave: failed to initialize ir");
		return;
	}
	irpid =<-irpidch;

	hdbuf := 0;
	dummy := chan of int;
	for(;;){
		if(buf == nil){
			outc = dummy;
		}else{
			outc = irc;
			hdbuf = hd buf;
		}
		alt{
		in = <-irchan =>
			buf = append(buf, in);
		outc <-= irmod->translate(hdbuf) =>
			buf = tl buf;
		<-stopc =>{
			killir(irpid);
			return;
			}
		}
	}
}

append(l: list of int, i: int): list of int
{
	if(l == nil)
		return i :: nil;
	return hd l :: append(tl l, i);
}

killir(irpid: int)
{
        pid := sys->sprint("%d", irpid);
        fd := sys->open("#p/"+pid+"/ctl", sys->OWRITE);
        if(fd==nil) {
                print("init: process %s: %r\n", pid);
                return;
        }
 
        msg := array of byte "kill";
        n := sys->write(fd, msg, len msg);
        if(n < 0) {
                print("init: message for %s: %r\n", pid);
                return;
        }
}

#
# Set system name from nvram
#
setsysname()
{
	fd := open("/nvfs/ID", sys->OREAD);
	if(fd == nil)
		return;
	fds := open("/dev/sysname", sys->OWRITE);
	if(fds == nil)
		return;
	buf := array[128] of byte;
	nr := sys->read(fd, buf, len buf);
	if(nr <= 0)
		return;
	sys->write(fds, buf, nr);
}