libsetup.pm 47.4 KB
Newer Older
1
2
#!/usr/bin/perl -wT

Leigh B. Stoller's avatar
Leigh B. Stoller committed
3
4
5
6
7
8
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

9
10
11
12
13
14
15
16
17
18
19
#
# Common routines and constants for the client bootime setup stuff.
#
package libsetup;
use Exporter;
@ISA = "Exporter";
@EXPORT =
    qw ( libsetup_init inform_reboot cleanup_node check_status
	 create_nicknames doifconfig dohostnames
	 doaccounts dorpms dotarballs dostartupcmd install_deltas
	 bootsetup nodeupdate startcmdstatus whatsmynickname
20
	 TBBackGround TBForkCmd remotenodeupdate remotenodevnodesetup
21

Leigh B. Stoller's avatar
Leigh B. Stoller committed
22
	 OPENTMCC CLOSETMCC RUNTMCC MFS REMOTE
23
24

	 TMCC TMIFC TMDELAY TMRPM TMTARBALLS TMHOSTS
25
26
	 TMNICKNAME HOSTSFILE TMSTARTUPCMD FINDIF TMTUNNELCONFIG
	 TMTRAFFICCONFIG TMROUTECONFIG TMVNODEDIR
27
28
29

	 TMCCCMD_REBOOT TMCCCMD_STATUS TMCCCMD_IFC TMCCCMD_ACCT TMCCCMD_DELAY
	 TMCCCMD_HOSTS TMCCCMD_RPM TMCCCMD_TARBALL TMCCCMD_STARTUP
30
	 TMCCCMD_DELTA TMCCCMD_STARTSTAT TMCCCMD_READY TMCCCMD_TRAFFIC
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31
	 TMCCCMD_BOSSINFO TMCCCMD_VNODELIST TMCCCMD_ISALIVE
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

       );

# Must come after package declaration!
use English;

#
# This is the home of the setup library on the client machine. The including
# program has to tell us this by calling the init routine below. For example,
# it is /etc/testbed on FreeBSD and /etc/rc.d/testbed on Linux.
#
my $SETUPDIR;

sub libsetup_init($)
{
    my($path) = @_;

    $SETUPDIR = $path;
}

#
# This "local" library provides the OS dependent part. Must load this after
# defining the above function cause the local library invokes it to set the
# $SETUPDIR
#
use liblocsetup;

59
60
61
62
63
64
65
#
# For virtual (multiplexed nodes). If defined, tack onto tmcc command.
# and use in pathnames. Not sure how this will be used later with jailed
# virtual nodes, since they will run in their own environment, but without
# jail we have to share the same namespace.
#
my $vnodeid	= "";
66
my $vnodedir;
67

68
69
70
71
72
73
74
75
76
77
78
79
80
81
#
# These are the paths of various files and scripts that are part of the
# setup library.
#
sub TMCC()		{ "$SETUPDIR/tmcc"; }
sub TMIFC()		{ "$SETUPDIR/rc.ifc"; }
sub TMRPM()		{ "$SETUPDIR/rc.rpm"; }
sub TMTARBALLS()	{ "$SETUPDIR/rc.tarballs"; }
sub TMSTARTUPCMD()	{ "$SETUPDIR/startupcmd"; }
sub TMHOSTS()		{ "$SETUPDIR/hosts"; }
sub TMNICKNAME()	{ "$SETUPDIR/nickname"; }
sub FINDIF()		{ "$SETUPDIR/findif"; }
sub HOSTSFILE()		{ "/etc/hosts"; }
sub TMMOUNTDB()		{ "$SETUPDIR/mountdb"; }
Austin Clements's avatar
Austin Clements committed
82
sub TMSFSMOUNTDB()	{ "$SETUPDIR/sfsmountdb"; }
83
84
85
86
sub TMROUTECONFIG()     { ($vnodedir ? $vnodedir : $SETUPDIR) . "/rc.route";}
sub TMTRAFFICCONFIG()	{ ($vnodedir ? $vnodedir : $SETUPDIR) . "/rc.traffic";}
sub TMTUNNELCONFIG()	{ ($vnodedir ? $vnodedir : $SETUPDIR) . "/rc.tunnel";}
sub TMVTUNDCONFIG()	{ ($vnodedir ? $vnodedir : $SETUPDIR) . "/vtund.conf";}
87
88
sub TMPASSDB()		{ "$SETUPDIR/passdb"; }
sub TMGROUPDB()		{ "$SETUPDIR/groupdb"; }
89

Austin Clements's avatar
Austin Clements committed
90
91
92
93
94
95
96
97
#
# Whether or not to use SFS (the self-certifying file system).  If this
# is 0, fall back to NFS.  Note that it doesn't hurt to set this to 1
# even if TMCD is not serving out SFS mounts, or if this node is not
# running SFS.  It'll deal and fall back to NFS.
#
my $USESFS		= 1;

98
#
99
100
101
102
103
# This is the VERSION. We send it through to tmcd so it knows what version
# responses this file is expecting.
#
# BE SURE TO BUMP THIS AS INCOMPATIBILE CHANGES TO TMCD ARE MADE!
#
Austin Clements's avatar
Austin Clements committed
104
sub TMCD_VERSION()	{ 6; };
105
106
107

#
# These are the TMCC commands. 
108
109
110
111
112
113
#
sub TMCCCMD_REBOOT()	{ "reboot"; }
sub TMCCCMD_STATUS()	{ "status"; }
sub TMCCCMD_IFC()	{ "ifconfig"; }
sub TMCCCMD_ACCT()	{ "accounts"; }
sub TMCCCMD_DELAY()	{ "delay"; }
114
sub TMCCCMD_HOSTS()	{ "hostnames"; }
115
116
117
118
119
120
121
sub TMCCCMD_RPM()	{ "rpms"; }
sub TMCCCMD_TARBALL()	{ "tarballs"; }
sub TMCCCMD_STARTUP()	{ "startupcmd"; }
sub TMCCCMD_DELTA()	{ "deltas"; }
sub TMCCCMD_STARTSTAT()	{ "startstatus"; }
sub TMCCCMD_READY()	{ "ready"; }
sub TMCCCMD_MOUNTS()	{ "mounts"; }
122
sub TMCCCMD_ROUTING()	{ "routing"; }
123
124
sub TMCCCMD_TRAFFIC()	{ "trafgens"; }
sub TMCCCMD_BOSSINFO()	{ "bossinfo"; }
125
sub TMCCCMD_TUNNEL()	{ "tunnels"; }
126
sub TMCCCMD_NSECONFIGS(){ "nseconfigs"; }
127
sub TMCCCMD_VNODELIST() { "vnodelist"; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
128
sub TMCCCMD_ISALIVE()   { "isalive"; }
Austin Clements's avatar
Austin Clements committed
129
130
sub TMCCCMD_SFSHOSTID()	{ "sfshostid"; }
sub TMCCCMD_SFSMOUNTS() { "sfsmounts"; }
131
132
133
134
135
136

#
# Some things never change.
# 
my $TARINSTALL  = "/usr/local/bin/install-tarfile %s %s";
my $DELTAINSTALL= "/usr/local/bin/install-delta %s";
137
my $VTUND       = "/usr/local/sbin/vtund";
138
139
140
141

#
# This is a debugging thing for my home network.
# 
142
#my $NODE	= "-p 7778 REDIRECT=192.168.100.1";
Austin Clements's avatar
Austin Clements committed
143
144
#my $NODE	= "-p 7779";
$NODE		= "";
145
146

# Locals
147
148
149
150
my $pid		= "";
my $eid		= "";
my $vname	= "";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
151
152
153
154
# Control tmcc error condition and timeout. Dynamic, not lexical!
$tmccdie        = 1; 
$tmcctimeout    = 0;

155
156
157
158
# When on the MFS, we do a much smaller set of stuff.
# Cause of the way the packages are loaded (which I do not understand),
# this is computed on the fly instead of once.
sub MFS()	{ if (-e "$SETUPDIR/ismfs") { return 1; } else { return 0; } }
159

160
161
162
163
164
#
# Same for a remote node.
#
sub REMOTE()	{ if (-e "$SETUPDIR/isrem") { return 1; } else { return 0; } }

165
166
167
168
#
# Open a TMCC connection and return the "stream pointer". Caller is
# responsible for closing the stream and checking return value.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
169
# usage: OPENTMCC(char *command, char *args, char *options)
170
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
171
sub OPENTMCC($;$$)
172
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
173
    my($cmd, $args, $options) = @_;
174
    my $vn = "";
175
176
177
178
179
    local *TM;

    if (!defined($args)) {
	$args = "";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
180
181
182
    if (!defined($options)) {
	$options = "";
    }
183
184
185
    if ($vnodeid ne "") {
	$vn = "-n $vnodeid";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
186
187
188
    if ($tmcctimeout) {
	$options .= " -t $tmcctimeout";
    }
189

Leigh B. Stoller's avatar
Leigh B. Stoller committed
190
191
    my $foo = sprintf("%s -v %d $options $NODE $vn $cmd $args |",
		      TMCC, TMCD_VERSION);
192

Leigh B. Stoller's avatar
Leigh B. Stoller committed
193
194
195
196
197
    if (!open(TM, $foo)) {
	print STDERR "Cannot start TMCC: $!\n";
	die("\n") if $tmccdie;
	return undef;
    }
198
199
200
    return (*TM);
}

201
202
203
204
205
206
#
# Close connection. Die on error.
# 
sub CLOSETMCC($) {
    my($TM) = @_;
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
207
208
209
210
211
212
213
214
215
216
217
    if (! close($TM)) {
	if ($?) {
	    print STDERR "TMCC exited with status $?!\n";
	}
	else {
	    print STDERR "Error closing TMCC pipe: $!\n";
	}
	die("\n") if $tmccdie;
	return 0;
    }
    return 1;
218
219
}

220
221
222
#
# Run a TMCC command with the provided arguments.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
223
# usage: RUNTMCC(char *command, char *args, char *options)
224
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
225
sub RUNTMCC($;$$)
226
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
227
    my($cmd, $args, $options) = @_;
228
229
230
231
232
    my($TM);

    if (!defined($args)) {
	$args = "";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
233
234
235
    if (!defined($options)) {
	$options = "";
    }
236
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
237
    $TM = OPENTMCC($cmd, $args, $options);
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

    close($TM)
	or die $? ? "TMCC exited with status $?" : "Error closing pipe: $!";
    
    return 0;
}

#
# Inform the master we have rebooted.
#
sub inform_reboot()
{
    RUNTMCC(TMCCCMD_REBOOT);
    return 0;
}

#
# Reset to a moderately clean state.
#
257
258
259
sub cleanup_node ($) {
    my ($scrub) = @_;
    
260
261
    print STDOUT "Cleaning node; removing configuration files ...\n";
    unlink TMIFC, TMRPM, TMSTARTUPCMD, TMNICKNAME, TMTARBALLS;
262
    unlink TMROUTECONFIG, TMTRAFFICCONFIG, TMTUNNELCONFIG;
263
264
    unlink TMMOUNTDB . ".db";

265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
    #
    # If scrubbing, remove the password/group file DBs so that we revert
    # to base set.
    # 
    if ($scrub) {
	unlink TMPASSDB . ".db";
	unlink TMGROUPDB . ".db";
    }

    if (! REMOTE()) {
	printf STDOUT "Resetting %s file\n", HOSTSFILE;
	if (system($CP, "-f", TMHOSTS, HOSTSFILE) != 0) {
	    printf "Could not copy default %s into place: $!\n", HOSTSFILE;
	    exit(1);
	}
280
281
    }

282
    return os_cleanup_node($scrub);
283
284
285
286
287
288
289
290
291
292
293
294
295
}

#
# Check node allocation.
#
# Returns 0 if node is free. Returns list (pid/eid/vname) if allocated.
#
sub check_status ()
{
    my $TM;
    
    $TM = OPENTMCC(TMCCCMD_STATUS);
    $_  = <$TM>;
296
    CLOSETMCC($TM);
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

    if ($_ =~ /^FREE/) {
	return 0;
    }
    
    if ($_ =~ /ALLOCATED=([-\@\w.]*)\/([-\@\w.]*) NICKNAME=([-\@\w.]*)/) {
	$pid   = $1;
	$eid   = $2;
	$vname = $3;
    }
    else {
	warn "*** WARNING: Error getting reservation status\n";
	return 0;
    }
    return ($pid, $eid, $vname);
}

#
# Stick our nickname in a file in case someone wants it.
#
sub create_nicknames()
{
    open(NICK, ">" . TMNICKNAME)
	or die("Could not open nickname file: $!");
321
    print NICK "$vname.$eid.$pid\n";
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
    close(NICK);

    return 0;
}

#
# Process mount directives from TMCD. We keep track of all the mounts we
# have added in here so that we delete just the accounts we added, when
# project membership changes. Same goes for project directories on shared
# nodes. We use a simple perl DB for that.
#
sub domounts()
{
    my $TM;
    my %MDB;
    my %mounts;
    my %deletes;
Austin Clements's avatar
Austin Clements committed
339
340
341
342
    my %sfsmounts;
    my %sfsdeletes;

    $TM = OPENTMCC(TMCCCMD_MOUNTS, "USESFS=$USESFS");
343
344

    while (<$TM>) {
Austin Clements's avatar
Austin Clements committed
345
	if ($_ =~ /^REMOTE=([-:\@\w\.\/]+) LOCAL=([-\@\w\.\/]+)/) {
346
347
	    $mounts{$1} = $2;
	}
Austin Clements's avatar
Austin Clements committed
348
349
350
351
352
353
	elsif ($_ =~ /^SFS REMOTE=([-:\@\w\.\/]+) LOCAL=([-\@\w\.\/]+)/) {
	    $sfsmounts{$1} = $2;
	}
	else {
	    warn "*** WARNING: Malformed mount information: $_\n";
	}
354
    }
355
356
    CLOSETMCC($TM);
    
357
358
359
360
    #
    # The MFS version does not support (or need) this DB stuff. Just mount
    # them up.
    #
361
    if (MFS()) {
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
	while (($remote, $local) = each %mounts) {
	    if (! -e $local) {
		if (! os_mkdir($local, 0770)) {
		    warn "*** WARNING: Could not make directory $local: $!\n";
		    next;
		}
	    }
	
	    print STDOUT "  Mounting $remote on $local\n";
	    if (system("$MOUNT $remote $local")) {
		warn "*** WARNING: Could not $MOUNT $remote on $local: $!\n";
		next;
	    }
	}
	return 0;
    }

379
380
381
382
383
384
385
386
387
388
389
390
391
    dbmopen(%MDB, TMMOUNTDB, 0660);
    
    #
    # First mount all the mounts we are told to. For each one that is not
    # currently mounted, and can be mounted, add it to the DB.
    # 
    while (($remote, $local) = each %mounts) {
	if (system("$MOUNT | $EGREP ' $local '") == 0) {
	    $MDB{$remote} = $local;
	    next;
	}

	if (! -e $local) {
392
	    if (! os_mkdir($local, 0770)) {
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
		warn "*** WARNING: Could not make directory $local: $!\n";
		next;
	    }
	}
	
	print STDOUT "  Mounting $remote on $local\n";
	if (system("$MOUNT $remote $local")) {
	    warn "*** WARNING: Could not $MOUNT $remote on $local: $!\n";
	    next;
	}

	$MDB{$remote} = $local;
    }

    #
    # Now unmount the ones that we mounted previously, but are now no longer
    # in the mount set (as told to us by the TMCD). Note, we cannot delete 
    # them directly from MDB since that would mess up the foreach loop, so
    # just stick them in temp and postpass it.
    #
    while (($remote, $local) = each %MDB) {
	if (defined($mounts{$remote})) {
	    next;
	}

	if (system("$MOUNT | $EGREP ' $local '")) {
	    $deletes{$remote} = $local;
	    next;
	}

	print STDOUT "  Unmounting $local\n";
	if (system("$UMOUNT $local")) {
	    warn "*** WARNING: Could not unmount $local\n";
	    next;
	}
	
	#
	# Only delete from set if we can actually unmount it. This way
	# we can retry it later (or next time).
	# 
	$deletes{$remote} = $local;
    }
    while (($remote, $local) = each %deletes) {
	delete($MDB{$remote});
    }

    # Write the DB back out!
    dbmclose(%MDB);

Austin Clements's avatar
Austin Clements committed
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
    #
    # Now, do basically the same thing over again, but this time for
    # SFS mounted stuff
    #

    if (scalar(%sfsmounts)) {
	dbmopen(%MDB, TMSFSMOUNTDB, 0660);
	
	#
	# First symlink all the mounts we are told to. For each one
	# that is not currently symlinked, and can be, add it to the
	# DB.
	#
	while (($remote, $local) = each %sfsmounts) {
	    if (-l $local) {
		if (readlink($local) eq ("/sfs/" . $remote)) {
		    $MDB{$remote} = $local;
		    next;
		}
		if (readlink($local) ne ("/sfs/" . $remote)) {
		    print STDOUT "  Unlinking incorrect symlink $local\n";
		    if ( ! unlink($local)) {
			warn "*** WARNING: Could not unlink $local: $!\n";
			next;
		    }
		}
	    }
	    
	    $dir = $local;
	    $dir =~ s/(.*)\/[^\/]*$/$1/;
	    if (! -e $dir) {
		print STDOUT "  Making directory $dir\n";
		if (! os_mkdir($dir, 755)) {
		    warn "*** WARNING: Could not make directory $local: $!\n";
		    next;
		}
	    }
	    print STDOUT "  Symlinking $remote on $local\n";
	    if (! symlink("/sfs/" . $remote . "/", $local)) {
		warn "*** WARNING: Could not make symlink $local: $!\n";
		next;
	    }
	    
	    $MDB{$remote} = $local;
	}

	#
	# Now delete the ones that we symlinked previously, but are
	# now no longer in the mount set (as told to us by the TMCD).
	# Note, we cannot delete them directly from MDB since that
	# would mess up the foreach loop, so just stick them in temp
	# and postpass it.
	#
	while (($remote, $local) = each %MDB) {
	    if (defined($sfsmounts{$remote})) {
		next;
	    }
	    
	    if (! -e $local) {
		$sfsdeletes{$remote} = $local;
		next;
	    }
	    
	    print STDOUT "  Deleting symlink $local\n";
	    if (! unlink($local)) {
		warn "*** WARNING: Could not delete $local: $!\n";
		next;
	    }
	    
	    #
	    # Only delete from set if we can actually unlink it.  This way
	    # we can retry it later (or next time).
	    #
	    $sfsdeletes{$remote} = $local;
	}
	while (($remote, $local) = each %sfsdeletes) {
	    delete($MDB{$remote});
	}

	# Write the DB back out!
	dbmclose(%MDB);	
    }
    else {
	# There were no SFS mounts reported, so disable SFS
	$usesfs = 0;
    }

    return 0;
}

#
# Do SFS hostid setup.
# Creates an SFS host key for this node, if it doesn't already exist,
# and sends it to TMCD
#
sub dosfshostid ()
{
    my $TM;
    my $myhostid;

    # Do I already have a host key?
    if (! -e "/etc/sfs/sfs_host_key") {
	warn "*** This node does not have a host key, skipping SFS stuff\n";
	$USESFS = 0;
	return 1;
    }

    # Give hostid to TMCD
    open(SFSKEY, "sfskey hostid - |")
	or die "Cannot start sfskey";
    $myhostid = <SFSKEY>;
    close(SFSKEY);
    if (defined($myhostid)) {
	if ( $myhostid =~ /^([-\.\w_]*:[a-z0-9]*)$/ ) {
	    $myhostid = $1;
	    print STDOUT "  Hostid: $myhostid\n";
	    RUNTMCC(TMCCCMD_SFSHOSTID, "$myhostid");
	}
	else {
	    warn "*** WARNING: Invalid hostid\n";
	}
    }
    else {
	warn "*** WARNING: Could not retrieve this node's hostid (is sfssd running?)\n";
	$USESFS = 0;
    }

    return 0;
}

572
573
574
575
576
577
578
579
#
# Do interface configuration.    
# Write a file of ifconfig lines, which will get executed.
#
sub doifconfig ()
{
    my $TM;
    
580
581
582
583
584
585
586
587
588
589
    #
    # Kinda ugly, but there is too much perl goo included by Socket to put it
    # on the MFS. 
    # 
    if (MFS()) {
	return 1;
    }
    require Socket;
    import Socket;
    
590
591
592
593
594
595
596
597
598
599
600
    $TM = OPENTMCC(TMCCCMD_IFC);

    #
    # Open a connection to the TMCD, and then open a local file into which
    # we write ifconfig commands (as a shell script).
    # 
    open(IFC, ">" . TMIFC)
	or die("Could not open " . TMIFC . ": $!");
    print IFC "#!/bin/sh\n";
    
    while (<$TM>) {
601
602
603
604
605
606
607
608
609
	my $pat;

	#
	# Note that speed has a units spec: (K|M)bps
	# 
	$pat  = q(INTERFACE=(\d*) INET=([0-9.]*) MASK=([0-9.]*) MAC=(\w*) );
	$pat .= q(SPEED=(\w*) DUPLEX=(\w*));
	
	if ($_ =~ /$pat/) {
610
611
	    my $iface;

612
613
614
615
616
617
	    my $inet     = $2;
	    my $mask     = $3;
	    my $mac      = $4;
	    my $speed    = $5; 
	    my $duplex   = $6;
	    my $routearg = inet_ntoa(inet_aton($inet) & inet_aton($mask));
618
619
620
621

	    if ($iface = findiface($mac)) {
		my $ifline =
		    os_ifconfig_line($iface, $inet, $mask, $speed, $duplex);
622
623
624
		    
		print STDOUT "  $ifline\n";
		print IFC "$ifline\n";
625
		print IFC TMROUTECONFIG . " $routearg up\n";
626
627
	    }
	    else {
628
		warn "*** WARNING: Bad MAC: $mac\n";
629
630
631
632
633
634
	    }
	}
	else {
	    warn "*** WARNING: Bad ifconfig line: $_";
	}
    }
635
    CLOSETMCC($TM);
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
    close(IFC);
    chmod(0755, TMIFC);

    return 0;
}

#
# Convert from MAC to iface name (eth0/fxp0/etc) using little helper program.
# 
sub findiface($)
{
    my($mac) = @_;
    my($iface);

    open(FIF, FINDIF . " $mac |")
	or die "Cannot start " . FINDIF . ": $!";

    $iface = <FIF>;
    
    if (! close(FIF)) {
	return 0;
    }
    
    $iface =~ s/\n//g;
    return $iface;
}

663
664
665
666
667
668
669
670
#
# Do router configuration stuff. This just writes a file for someone else
# to deal with.
#
sub dorouterconfig ()
{
    my @stuff   = ();
    my $routing = 0;
671
672
    my %upmap   = ();
    my %downmap = ();
673
674
675
676
677
678
    my $TM;

    $TM = OPENTMCC(TMCCCMD_ROUTING);
    while (<$TM>) {
	push(@stuff, $_);
    }
679
    CLOSETMCC($TM);
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700

    if (! @stuff) {
	return 0;
    }

    #
    # Look for router type. If none, then do not bother to write this file.
    # 
    foreach my $line (@stuff) {
	if (($line =~ /ROUTERTYPE=(.+)/) && ($1 ne "none")) {
	    $routing = 1;
	    last;
	}
    }
    if (! $routing) {
	return 0;
    }
    
    open(RC, ">" . TMROUTECONFIG)
	or die("Could not open " . TMROUTECONFIG . ": $!");

701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
    print RC "#!/bin/sh\n";
    print RC "# auto-generated by libsetup.pm, DO NOT EDIT\n";

    #
    # Now convert static route info into OS route commands
    # Also check for use of gated and remember it.
    #
    my $usegated = 0;
    my $pat;

    #
    # ROUTERTYPE=manual
    # ROUTE DEST=192.168.2.3 DESTTYPE=host DESTMASK=255.255.255.0 \
    #	NEXTHOP=192.168.1.3 COST=0
    #
    $pat = q(ROUTE DEST=([0-9\.]*) DESTTYPE=(\w*) DESTMASK=([0-9\.]*) );
    $pat .= q(NEXTHOP=([0-9\.]*) COST=([0-9]*));

    my $usemanual = 0;
720
    foreach my $line (@stuff) {
721
722
723
724
725
	if ($line =~ /ROUTERTYPE=(gated|ospf)/) {
	    $usegated = 1;
	} elsif ($line =~ /ROUTERTYPE=(manual|static)/) {
	    $usemanual = 1;
	} elsif ($usemanual && $line =~ /$pat/) {
726
	    my $dip   = $1;
727
728
	    my $rtype = $2;
	    my $dmask = $3;
729
730
731
	    my $gate  = $4;
	    my $cost  = $5;
	    my $routearg = inet_ntoa(inet_aton($gate) & inet_aton($dmask));
732

733
734
735
736
737
738
739
740
	    if (! defined($upmap{$routearg})) {
		$upmap{$routearg} = [];
		$downmap{$routearg} = [];
	    }
	    $rcline = os_routing_add_manual($rtype, $dip, $dmask, $gate,$cost);
	    push(@{$upmap{$routearg}}, $rcline);
	    $rcline = os_routing_del_manual($rtype, $dip, $dmask, $gate,$cost);
	    push(@{$downmap{$routearg}}, $rcline);
741
742
743
744
745
	} else {
	    warn "*** WARNING: Bad routing line: $line\n";
	}
    }

746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
    print RC "case \"\$1\" in\n";
    foreach my $arg (keys(%upmap)) {
	print RC "  $arg)\n";
	print RC "    case \"\$2\" in\n";
	print RC "      up)\n";
	foreach my $rcline (@{$upmap{$arg}}) {
	    print RC "        $rcline\n";
	}
	print RC "      ;;\n";
	print RC "      down)\n";
	foreach my $rcline (@{$downmap{$arg}}) {
	    print RC "        $rcline\n";
	}
	print RC "      ;;\n";
	print RC "    esac\n";
	print RC "  ;;\n";
    }
    print RC "  enable)\n";

    #
    # Turn on IP forwarding
    #
    my $rcline = os_routing_enable_forward();
    print RC "    $rcline\n";

771
772
773
774
775
776
777
778
779
    #
    # Finally, enable gated if desired.
    #
    # Note that we allow both manually-specified static routes and gated
    # though more work may be needed on the gated config files to make
    # this work (i.e., to import existing kernel routes).
    #
    if ($usegated) {
	$rcline = os_routing_enable_gated();
780
	print RC "    $rcline\n";
781
    }
782
783
784
    print RC "  ;;\n";
    print RC "esac\n";
    print RC "exit 0\n";
785

786
    close(RC);
787
    chmod(0755, TMROUTECONFIG);
788
789
790
791

    return 0;
}

792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#
# Host names configuration (/etc/hosts). 
#
sub dohostnames ()
{
    my $TM;

    #
    # Start with fresh copy, since the hosts files is potentially updated
    # after the node boots via the update command.
    # 
    if (system($CP, "-f", TMHOSTS, HOSTSFILE) != 0) {
	printf STDERR "Could not copy default %s into place: $!\n", HOSTSFILE;
	return 1;
    }
    
    $TM = OPENTMCC(TMCCCMD_HOSTS);

    open(HOSTS, ">>" . HOSTSFILE)
	or die("Could not open $HOSTSFILE: $!");

    #
    # Now convert each hostname into hosts file representation and write
815
816
817
818
819
    # it to the hosts file. Note that ALIASES is for backwards compat.
    # Should go away at some point.
    #
    my $pat  = q(NAME=([-\w\.]+) IP=([0-9\.]*) ALIASES=\'([-\w\. ]*)\');
    
820
    while (<$TM>) {
821
822
823
824
825
826
	if ($_ =~ /$pat/) {
	    my $name    = $1;
	    my $ip      = $2;
	    my $aliases = $3;
	    
	    my $hostline = os_etchosts_line($name, $ip, $aliases);
827
828
829
830
831
832
833
834
	    
	    print STDOUT "  $hostline\n";
	    print HOSTS  "$hostline\n";
	}
	else {
	    warn "*** WARNING: Bad hosts line: $_";
	}
    }
835
    CLOSETMCC($TM);
836
837
838
839
840
    close(HOSTS);

    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
841
sub doaccounts()
842
843
{
    my %newaccounts = ();
844
    my %newgroups   = ();
845
846
    my %pubkeys1    = ();
    my %pubkeys2    = ();
Austin Clements's avatar
Austin Clements committed
847
    my @sfskeys     = ();
848
    my %deletes     = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
849
    my %lastmod     = ();
850
851
    my %PWDDB;
    my %GRPDB;
852
853
854
855

    my $TM = OPENTMCC(TMCCCMD_ACCT);

    #
856
857
858
859
860
861
    # The strategy is to keep a record of all the groups and accounts
    # added by the testbed system so that we know what to remove. We
    # use a vanilla perl dbm for that, one for the groups and one for
    # accounts. 
    #
    # First just get the current set of groups/accounts from tmcd.
862
863
864
    #
    while (<$TM>) {
	if ($_ =~ /^ADDGROUP NAME=([-\@\w.]+) GID=([0-9]+)/) {
865
866
867
	    #
	    # Group info goes in the hash table.
	    #
868
869
	    my $gname = "$1";
	    
870
	    if (REMOTE()) {
871
		$gname = "emu-${gname}";
872
	    }
873
	    $newgroups{"$gname"} = $2
874
875
876
877
878
879
880
881
	}
	elsif ($_ =~ /^ADDUSER LOGIN=([0-9a-z]+)/) {
	    #
	    # Account info goes in the hash table.
	    # 
	    $newaccounts{$1} = $_;
	    next;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
882
883
884
885
	elsif ($_ =~ /^PUBKEY LOGIN=([0-9a-z]+) KEY="(.*)"/) {
	    #
	    # Keys go into hash as a list of keys.
	    #
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
	    my $login = $1;
	    my $key   = $2;

	    #
	    # P1 or P2 key. Must be treated differently below.
	    #
	    if ($key =~ /^\d+\s+.*$/) {
		if (! defined($pubkeys1{$login})) {
		    $pubkeys1{$login} = [];
		}
		push(@{$pubkeys1{$login}}, $key);
	    }
	    else {
		if (! defined($pubkeys2{$login})) {
		    $pubkeys2{$login} = [];
		}
		push(@{$pubkeys2{$login}}, $key);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
903
904
905
	    }
	    next;
	}
Austin Clements's avatar
Austin Clements committed
906
907
908
909
910
911
912
	elsif ($_ =~ /^SFSKEY KEY="(.*)"/) {
	    #
	    # SFS key goes into the array.
	    #
	    push(@sfskeys, $1);
	    next;
	}
913
	else {
914
	    warn "*** WARNING: Bad accounts line: $_\n";
915
916
	}
    }
917
    CLOSETMCC($TM);
918

919
920
921
922
923
924
    dbmopen(%PWDDB, TMPASSDB, 0660) or
	die("Cannot open " . TMPASSDB . ": $!\n");
	
    dbmopen(%GRPDB, TMGROUPDB, 0660) or
	die("Cannot open " . TMGROUPDB . ": $!\n");

925
    #
926
927
    # Create any groups that do not currently exist. Add each to the
    # DB as we create it.
928
    #
929
930
931
932
933
934
    while (($group, $gid) = each %newgroups) {
	my ($exists,undef,$curgid) = getgrnam($group);
	
	if ($exists) {
	    if ($gid != $curgid) {
		warn "*** WARNING: $group/$gid mismatch with existing group\n";
935
	    }
936
	    next;
937
	}
938
939
940
941
942
943
944
945
946

	print "Adding group: $group/$gid\n";
	    
	if (os_groupadd($group, $gid)) {
	    warn "*** WARNING: Error adding new group $group/$gid\n";
	    next;
	}
	# Add to DB only if successful. 
	$GRPDB{$group} = $gid;
947
948
949
    }

    #
950
951
952
953
    # Now remove the ones that we created previously, but are now no longer
    # in the group set (as told to us by the TMCD). Note, we cannot delete 
    # them directly from the hash since that would mess up the foreach loop,
    # so just stick them in temp and postpass it.
954
    #
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
    while (($group, $gid) = each %GRPDB) {
	if (defined($newgroups{$group})) {
	    next;
	}

	print "Removing group: $group/$gid\n";
	
	if (os_groupdel($group)) {
	    warn "*** WARNING: Error removing group $group/$gid\n";
	    next;
	}
	# Delete from DB only if successful. 
	$deletes{$group} = $gid;
    }
    while (($group, $gid) = each %deletes) {
	delete($GRPDB{$group});
971
    }
972
973
974
975
    %deletes = ();

    # Write the DB back out!
    dbmclose(%GRPDB);
976
977

    #
978
979
980
    # Repeat the same sequence for accounts, except we remove old accounts
    # first. 
    # 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
981
982
983
984
985
986
987
988
989
990
991
992
993
    while (($login, $info) = each %PWDDB) {
	my $uid = $info;
	
	#
	# Split out the uid from the serial. Note that this was added later
	# so existing DBs might not have a serial yet. We save the serial
	# for later. 
	#
	if ($info =~ /(\d*):(\d*)/) {
	    $uid = $1;
	    $lastmod{$login} = $2;
	}
	
994
995
996
	if (defined($newaccounts{$login})) {
	    next;
	}
997

998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
	my ($exists,undef,$curuid,undef,
	    undef,undef,undef,$homedir) = getpwnam($login);

	#
	# If the account is gone, someone removed it by hand. Remove it
	# from the DB so we do not keep trying.
	#
	if (! defined($exists)) {
	    warn "*** WARNING: Account for $login was already removed!\n";
	    $deletes{$login} = $login;
	    next;
	}

	#
	# Check for mismatch, just in case. If there is a mismatch remove it
	# from the DB so we do not keep trying.
	#
	if ($uid != $curuid) {
	    warn "*** WARNING: ".
		 "Account uid for $login has changed ($uid/$curuid)!\n";
	    $deletes{$login} = $login;
	    next;
	}
	
	print "Removing user: $login\n";
	
	if (os_userdel($login) != 0) {
	    warn "*** WARNING: Error removing user $login\n";
	    next;
	}

	#
	# Remove the home dir. 
	#
	# Must ask for the current home dir in case it came from pw.conf.
	#
	if (defined($homedir) &&
	    index($homedir, "/${login}")) {
	    if (os_homedirdel($login, $homedir) != 0) {
	        warn "*** WARNING: Could not remove homedir $homedir.\n";
1038
1039
	    }
	}
1040
1041
1042
1043
1044
1045
1046
	
	# Delete from DB only if successful. 
	$deletes{$login} = $login;
    }
    
    while (($login, $foo) = each %deletes) {
	delete($PWDDB{$login});
1047
1048
    }

1049
    my $pat = q(ADDUSER LOGIN=([0-9a-z]+) PSWD=([^:]+) UID=(\d+) GID=(.*) );
1050
    $pat   .= q(ROOT=(\d) NAME="(.*)" HOMEDIR=(.*) GLIST="(.*)" );
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1051
    $pat   .= q(SERIAL=(\d+));
1052
1053

    while (($login, $info) = each %newaccounts) {
1054
	if ($info =~ /$pat/) {
1055
1056
1057
1058
1059
1060
1061
	    $pswd  = $2;
	    $uid   = $3;
	    $gid   = $4;
	    $root  = $5;
	    $name  = $6;
	    $hdir  = $7;
	    $glist = $8;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1062
	    $serial= $9;
1063
1064
1065
	    if ( $name =~ /^(([^:]+$|^))$/ ) {
		$name = $1;
	    }
1066

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1067
1068
1069
1070
1071
1072
1073
1074
1075
	    #
	    # See if update needed, based on the serial number we get.
	    # If its different, the account info has changed.
	    # 
	    my $doupdate = 0;
	    if (!defined($lastmod{$login}) || $lastmod{$login} != $serial) {
		$doupdate = 1;
	    }
	    
1076
	    my ($exists,undef,$curuid) = getpwnam($login);
1077
1078

	    if ($exists) {
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
		if (!defined($PWDDB{$login})) {
		    warn "*** WARNING: ".
			 "Skipping since $login existed before EmulabMan!\n";
		    next;
		}
		if ($curuid != $uid) {
		    warn "*** WARNING: ".
			 "$login/$uid uid mismatch with existing login.\n";
		    next;
		}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
		if ($doupdate) {
		    print "Updating: ".
			"$login/$uid/$gid/$root/$name/$hdir/$glist\n";
		    
		    os_usermod($login, $gid, "$glist", $pswd, $root);

		    #
		    # Note that we changed the info for next time.
		    # 
		    $PWDDB{$login} = "$uid:$serial";
		}
1100
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1101
1102
	    else {
		print "Adding: $login/$uid/$gid/$root/$name/$hdir/$glist\n";
1103

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
		if (os_useradd($login, $uid, $gid, $pswd, 
			       "$glist", $hdir, $name, $root)) {
		    warn "*** WARNING: Error adding new user $login\n";
		    next;
		}
		# Add to DB only if successful. 
		$PWDDB{$login} = "$uid:$serial";
	    }

	    #
	    # Skip ssh stuff if a local node or not updating (if the
	    # user did not exist, $doupdate will be true).
	    # 
	    if (!REMOTE() || !$doupdate) {
1118
1119
		next;
	    }
1120
1121
1122
1123
1124
1125
1126
1127
1128

	    #
	    # Create .ssh dir and populate it with an authkeys file.
	    # Must ask for the current home dir since we rely on pw.conf.
	    #
	    my (undef,undef,undef,undef,
		undef,undef,undef,$homedir) = getpwuid($uid);
	    my $sshdir = "$homedir/.ssh";
	    
1129
1130
	    TBNewsshKeyfile($sshdir, $uid, $gid, 1, @{$pubkeys1{$login}});
	    TBNewsshKeyfile($sshdir, $uid, $gid, 2, @{$pubkeys2{$login}});
1131
1132
1133
	}
	else {
	    warn("*** Bad accounts line: $info\n");
1134
1135
	}
    }
1136
1137
1138
    # Write the DB back out!
    dbmclose(%PWDDB);

Austin Clements's avatar
Austin Clements committed
1139
1140
1141
    #
    # Create sfs_users file and populate it with public SFS keys
    #
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
    if ($USESFS) {
	do {
	    if (!open(SFSKEYS, "> /etc/sfs/sfs_users.new")) {
		warn("*** WARNING: Could not open /etc/sfs/sfs_users.new: $!\n");
		next;
	    }
	    
	    print SFSKEYS "#\n";
	    print SFSKEYS "# DO NOT EDIT! This file auto generated by ".
		"Emulab.Net account software.\n";
	    print SFSKEYS "#\n";
	    print SFSKEYS "# Please use the web interface to edit your ".
		"SFS public key list.\n";
	    print SFSKEYS "#\n";
	    foreach my $key (@sfskeys) {
		print SFSKEYS "$key\n";
	    }
	    close(SFSKEYS);
	    
	    # Because sfs_users only contains public keys, sfs_users.pub is
	    # exactly the same
	    if (system("cp -p -f /etc/sfs/sfs_users.new ".
		       "/etc/sfs/sfs_users.pub.new")) {
		warn("*** Could not copy /etc/sfs/sfs_users.new to ".
		     "sfs_users.pub.new: $!\n");
		next;
	    }
	    
	    if (!chown(0, 0, "/etc/sfs/sfs_users.new")) {
		warn("*** WARNING: Could not chown /etc/sfs/sfs_users.new: $!\n");
		next;
	    }
	    if (!chmod(0600, "/etc/sfs/sfs_users.new")) {
		warn("*** WARNING: Could not chmod /etc/sfs/sfs_users.new: $!\n");
		next;
	    }
	    
	    if (!chown(0, 0, "/etc/sfs/sfs_users.pub.new")) {
		warn("*** WARNING: Could not chown /etc/sfs/sfs_users.pub.new: $!\n");
		next;
	    }
	    if (!chmod(0644, "/etc/sfs/sfs_users.pub.new")) {
		warn("*** WARNING: Could not chmod /etc/sfs/sfs_users.pub.new: $!\n");
		next;
	    }
	    
	    # Save off old key files and move in new ones
	    foreach my $keyfile ("/etc/sfs/sfs_users",
				 "/etc/sfs/sfs_users.pub") {
		if (-e $keyfile) {
		    if (system("cp -p -f $keyfile $keyfile.old")) {
			warn("*** Could not save off $keyfile: $!\n");
			next;
		    }
		    if (!chown(0, 0, "$keyfile")) {
			warn("*** Could not chown $keyfile: $!\n");
		    }
		    if (!chmod(0600, "$keyfile")) {
			warn("*** Could not chmod $keyfile: $!\n");
		    }
Austin Clements's avatar
Austin Clements committed
1202
		}
1203
1204
		if (system("mv -f $keyfile.new $keyfile")) {
		    warn("*** Could not mv $keyfile: ~!\n");
Austin Clements's avatar
Austin Clements committed
1205
1206
		}
	    }
1207
1208
	    
	    # The do-while is an easy way out in case of errors
Austin Clements's avatar
Austin Clements committed
1209
	}
1210
	while 0;
Austin Clements's avatar
Austin Clements committed
1211
1212
    }
    
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
    return 0;
}

#
# RPM configuration. 
#
sub dorpms ()
{
    my @rpms = ();
    
    my $TM = OPENTMCC(TMCCCMD_RPM);
    while (<$TM>) {
	push(@rpms, $_);
    }
1227
    CLOSETMCC($TM);
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237

    if (! @rpms) {
	return 0;
    }
    
    open(RPM, ">" . TMRPM)
	or die("Could not open " . TMRPM . ": $!");
    print RPM "#!/bin/sh\n";
    
    foreach my $rpm (@rpms) {
1238
	if ($rpm =~ /RPM=(.+)/) {
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
	    my $rpmline = os_rpminstall_line($1);
		    
	    print STDOUT "  $rpmline\n";
	    print RPM    "echo \"Installing RPM $1\"\n";
	    print RPM    "$rpmline\n";
	}
	else {
	    warn "*** WARNING: Bad RPMs line: $rpm";
	}
    }
    close(RPM);
    chmod(0755, TMRPM);

    return 0;
}

#
# TARBALL configuration. 
#
sub dotarballs ()
{
    my @tarballs = ();

    my $TM = OPENTMCC(TMCCCMD_TARBALL);
    while (<$TM>) {
	push(@tarballs, $_);
    }
1266
    CLOSETMCC($TM);
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276

    if (! @tarballs) {
	return 0;
    }
    
    open(TARBALL, ">" . TMTARBALLS)
	or die("Could not open " . TMTARBALLS . ": $!");
    print TARBALL "#!/bin/sh\n";
    
    foreach my $tarball (@tarballs) {
1277
	if ($tarball =~ /DIR=(.+)\s+TARBALL=(.+)/) {
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
	    my $tbline = sprintf($TARINSTALL, $1, $2);
		    
	    print STDOUT  "  $tbline\n";
	    print TARBALL "echo \"Installing Tarball $2 in dir $1 \"\n";
	    print TARBALL "$tbline\n";
	}
	else {
	    warn "*** WARNING: Bad Tarballs line: $tarball";
	}
    }
    close(TARBALL);
    chmod(0755, TMTARBALLS);

    return 0;
}

#
# Experiment startup Command.
#
sub dostartupcmd ()
{
    my $startupcmd;
    
    my $TM = OPENTMCC(TMCCCMD_STARTUP);
    $_ = <$TM>;
    if (defined($_)) {
	$startupcmd = $_;
    }
1306
    CLOSETMCC($TM);
1307
1308
1309
1310
1311
1312
1313
1314

    if (! $startupcmd) {
	return 0;
    }
    
    open(RUN, ">" . TMSTARTUPCMD)
	or die("Could not open $TMSTARTUPCMD: $!");
    
1315
    if ($startupcmd =~ /CMD=(\'.+\') UID=([0-9a-z]+)/) {
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
	print  STDOUT "  Will run $1 as $2\n";
	print  RUN    "$startupcmd";
    }
    else {
	warn "*** WARNING: Bad startupcmd line: $startupcmd";
    }

    close(RUN);
    chmod(0755, TMSTARTUPCMD);

    return 0;
}

1329
1330
1331
1332
1333
1334
sub dotrafficconfig()
{
    my $didopen = 0;
    my $pat;
    my $TM;
    my $boss;
1335
    my $startnse = 0;
1336
    my $nseconfig = "";
1337
    
1338
1339
1340
1341
1342
1343
1344
1345
1346
    #
    # Kinda ugly, but there is too much perl goo included by Socket to put it
    # on the MFS. 
    # 
    if (MFS()) {
	return 1;
    }
    require Socket;
    import Socket;
1347
1348
    
    $TM = OPENTMCC(TMCCCMD_BOSSINFO);
1349
1350
1351
1352
1353
1354
1355
    my $bossinfo = <$TM>;
    ($boss) = split(" ", $bossinfo);

    #
    # XXX hack: workaround for tmcc cmd failure inside TCL
    #     storing the output of a few tmcc commands in
    #     $SETUPDIR files for use by NSE
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1356
1357
1358
1359
1360
1361
1362
    #
    if (! REMOTE()) {
	open(BOSSINFCFG, ">$SETUPDIR/tmcc.bossinfo") or
	    die "Cannot open file $SETUPDIR/tmcc.bossinfo: $!";
	print BOSSINFCFG "$bossinfo";
	close(BOSSINFCFG);
    }
1363

1364
    CLOSETMCC($TM);
1365
1366
1367
1368
1369
1370
    my ($pid, $eid, $vname) = check_status();

    my $cmdline = "$SETUPDIR/trafgen -s $boss";
    if ($pid) {
	$cmdline .= " -E $pid/$eid";
    }
1371

1372
1373
1374
1375
1376
    #
    # XXX hack: workaround for tmcc cmd failure inside TCL
    #     storing the output of a few tmcc commands in
    #     $SETUPDIR files for use by NSE
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
    if (! REMOTE()) {
	my $record_sep;

	$record_sep = $/;
	undef($/);
	$TM = OPENTMCC(TMCCCMD_IFC);
	open(IFCFG, ">$SETUPDIR/tmcc.ifconfig") or
	    die "Cannot open file $SETUPDIR/tmcc.ifconfig: $!";
	print IFCFG <$TM>;
	close(IFCFG);
	CLOSETMCC($TM);
	$/ = $record_sep;
	
	open(TRAFCFG, ">$SETUPDIR/tmcc.trafgens") or
	    die "Cannot open file $SETUPDIR/tmcc.trafgens: $!";    
    }
1393

1394
1395
    $TM = OPENTMCC(TMCCCMD_TRAFFIC);

1396
    $pat  = q(TRAFGEN=([-\w.]+) MYNAME=([-\w.]+) MYPORT=(\d+) );
1397
    $pat .= q(PEERNAME=([-\w.]+) PEERPORT=(\d+) );
1398
    $pat .= q(PROTO=(\w+) ROLE=(\w+) GENERATOR=(\w+));
1399
1400

    while (<$TM>) {
1401

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1402
1403
1404
	if (! REMOTE()) {
	    print TRAFCFG "$_";
	}
1405
1406
1407
	if ($_ =~ /$pat/) {
	    #
	    # The following is specific to the modified TG traffic generator:
1408
1409
1410
1411
1412
	    #
	    #  trafgen [-s serverip] [-p serverport] [-l logfile] \
	    #	     [ -N name ] [-P proto] [-R role] [ -E pid/eid ] \
	    #	     [ -S srcip.srcport ] [ -T targetip.targetport ]
	    #
1413
1414
	    # N.B. serverport is not needed right now
	    #
1415
1416
1417
1418
1419
1420
1421
1422
	    my $name = $1;
	    my $ownaddr = inet_ntoa(my $ipaddr = gethostbyname($2));
	    my $ownport = $3;
	    my $peeraddr = inet_ntoa($ipaddr = gethostbyname($4));
	    my $peerport = $5;
	    my $proto = $6;
	    my $role = $7;
	    my $generator = $8;
1423
	    my $target;
1424
	    my $source;
1425

1426
1427
1428
	    # Skip if not specified as a TG generator. At some point
	    # work in Shashi's NSE work.
	    if ($generator ne "TG") {
1429
1430
1431
1432
1433
1434
1435
		$startnse = 1;
		if (! $didopen) {
		    open(RC, ">" . TMTRAFFICCONFIG)
			or die("Could not open " . TMTRAFFICCONFIG . ": $!");
		    print RC "#!/bin/sh\n";
		    $didopen = 1;
		}
1436
1437
1438
		next;
	    }

1439
1440
	    if ($role eq "sink") {
		$target = "$ownaddr.$ownport";
1441
		$source = "$peeraddr.$peerport";
1442
1443
1444
	    }
	    else {
		$target = "$peeraddr.$peerport";
1445
		$source = "$ownaddr.$ownport";
1446
1447
1448
1449
1450
1451
1452
1453
	    }

	    if (! $didopen) {
		open(RC, ">" . TMTRAFFICCONFIG)
		    or die("Could not open " . TMTRAFFICCONFIG . ": $!");
		print RC "#!/bin/sh\n";
		$didopen = 1;
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1454
1455
	    print RC "$cmdline -N $name -S $source -T $target -P $proto ".
		"-R $role >/tmp/${name}-${pid}-${eid}.debug 2>&1 &\n";
1456
1457
1458
1459
1460
	}
	else {
	    warn "*** WARNING: Bad traffic line: $_";
	}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1461
1462
1463
    if (! REMOTE()) {
	close(TRAFCFG);
    }
1464
1465
1466
1467

    if( $startnse ) {
	print RC "$SETUPDIR/startnse &\n";
    }
1468
    CLOSETMCC($TM);
1469

1470
1471
1472
1473
    #
    # XXX hack: workaround for tmcc cmd failure inside TCL
    #     storing the output of a few tmcc commands in
    #     $SETUPDIR files for use by NSE
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1474
1475
1476
1477
1478
1479
1480
    #
    if (! REMOTE()) {
	open(NSECFG, ">$SETUPDIR/tmcc.nseconfigs") or
	    die "Cannot open file $SETUPDIR/tmcc.nseconfigs: $!";
	$TM = OPENTMCC(TMCCCMD_NSECONFIGS);
	$record_sep = $/;
	undef($/);
1481
	$nseconfig = <$TM>;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1482
1483
1484
1485
1486
	$/ = $record_sep;
	print NSECFG $nseconfig;
	CLOSETMCC($TM);
	close(NSECFG);
    }
1487
	    
1488
1489
    # XXX hack: need a separate section for starting up NSE when we
    #           support simulated nodes
1490
    if( ! $startnse ) {
1491
1492
1493
	
	if( $nseconfig ) {

1494
	    # start NSE if 'tmcc nseconfigs' is not empty
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
	    if ( ! $didopen ) {
		open(RC, ">" . TMTRAFFICCONFIG)
		    or die("Could not open " . TMTRAFFICCONFIG . ": $!");
		print RC "#!/bin/sh\n";
		$didopen = 1;	
	    }
	    print RC "$SETUPDIR/startnse &\n";
	}
    }
    
1505
    if ($didopen) {
1506
	printf RC "%s %s\n", TMCC(), TMCCCMD_READY();
1507
1508
1509
1510
1511
1512
	close(RC);
	chmod(0755, TMTRAFFICCONFIG);
    }
    return 0;
}

1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
sub dotunnels()
{
    my @tunnels;
    my $pat;
    my $TM;
    my $didserver = 0;

    #
    # Kinda ugly, but there is too much perl goo included by Socket to put it
    # on the MFS. 
    # 
    if (MFS()) {
	return 1;
    }
    require Socket;
    import Socket;
    
    $TM = OPENTMCC(TMCCCMD_TUNNEL);
    while (<$TM>) {
	push(@tunnels, $_);
    }
1534
    CLOSETMCC($TM);
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627

    if (! @tunnels) {
	return 0;
    }
    my ($pid, $eid, $vname) = check_status();

    open(RC, ">" . TMTUNNELCONFIG)
	or die("Could not open " . TMTUNNELCONFIG . ": $!");
    print RC "#!/bin/sh\n";
    print RC "kldload if_tap\n";

    open(CONF, ">" . TMVTUNDCONFIG)
	or die("Could not open " . TMVTUNDCONFIG . ": $!");

    print(CONF
	  "options {\n".
	  "  ifconfig    /sbin/ifconfig;\n".
	  "  route       /sbin/route;\n".
	  "}\n".
	  "\n".
	  "default {\n".
	  "  persist     yes;\n".
	  "  stat        yes;\n".
	  "  keepalive   yes;\n".
	  "  type        ether;\n".
	  "}\n".
	  "\n");
    
    $pat  = q(TUNNEL=([-\w.]+) ISSERVER=(\d) PEERIP=([-\w.]+) );
    $pat .= q(PEERPORT=(\d+) PASSWORD=([-\w.]+) );
    $pat .= q(ENCRYPT=(\d) COMPRESS=(\d) INET=([-\w.]+) );
    $pat .= q(MASK=([-\w.]+) PROTO=([-\w.]+));

    foreach my $tunnel (@tunnels) {
	if ($tunnel =~ /$pat/) {
	    #
	    # The following is specific to vtund!
	    #
	    my $name     = $1;
	    my $isserver = $2;
	    my $peeraddr = $3;
	    my $peerport = $4;
	    my $password = $5;
	    my $encrypt  = ($6 ? "yes" : "no");
	    my $compress = ($7 ? "yes" : "no");
	    my $inetip   = $8;
	    my $mask     = $9;
	    my $proto    = $10;
	    my $routearg = inet_ntoa(inet_aton($inetip) & inet_aton($mask));

	    my $cmd = "$VTUND -n -P $peerport -f ". TMVTUNDCONFIG;

	    if ($isserver) {
		if (!$didserver) {
		    print RC
			"$cmd -s >/tmp/vtund-${pid}-${eid}.debug 2>&1 &\n";
		    $didserver = 1;
		}
	    }
	    else {
		print RC "$cmd $name $peeraddr ".
		    " >/tmp/vtun-${pid}-${eid}-${name}.debug 2>&1 &\n";
	    }
	    #
	    # Sheesh, vtund fails if it sees "//" in a path. 
	    #
	    my $config = TMROUTECONFIG;
	    $config =~ s/\/\//\//g;
	    
	    print(CONF
		  "$name {\n".
		  "  password      $password;\n".
		  "  compress      $compress;\n".
		  "  encrypt       $encrypt;\n".
		  "  proto         $proto;\n".
		  "\n".
		  "  up {\n".
		  "    # Connection is Up\n".
		  "    ifconfig \"%% $inetip netmask $mask\";\n".
		  "    program " . $config . " \"$routearg up\" wait;\n".
		  "  };\n".
		  "  down {\n".
		  "    # Connection is Down\n".
		  "    ifconfig \"%% down\";\n".
		  "    program " . $config . " \"$routearg down\" wait;\n".
		  "  };\n".
		  "}\n\n");
	}
	else {
	    warn "*** WARNING: Bad tunnel line: $tunnel";
	}
    }

1628
    close(CONF);
1629
1630
1631
1632
    close(RC);
    chmod(0755, TMTUNNELCONFIG);
    return 0;
}
1633

1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
#
# Boot Startup code. This is invoked from the setup OS dependent script,
# and this fires up all the stuff above.
#
sub bootsetup()
{
    #
    # Inform the master that we have rebooted.
    #
    inform_reboot();

    #
    # Check allocation. Exit now if not allocated.
    #
    print STDOUT "Checking Testbed reservation status ... \n";
    if (! check_status()) {
	print STDOUT "  Free!\n";
1651
	cleanup_node(1);
1652
1653
1654
1655
	return 0;
    }
    print STDOUT "  Allocated! $pid/$eid/$vname\n";

1656
1657
1658
1659
1660
    #
    # Cleanup node. Flag indicates to gently clean ...
    # 
    cleanup_node(0);

1661
1662
1663
1664
1665
    #
    # Setup a nicknames file. 
    #
    create_nicknames();

Austin Clements's avatar
Austin Clements committed
1666
1667
1668
1669
1670
1671
1672
1673
    if ($USESFS && ! MFS()) {
	#
	# Setup SFS hostid.
	#
	print STDOUT "Creating node SFS hostid ... \n";
	dosfshostid();
    }

1674
    #