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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
3
4
#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6
7
# All rights reserved.
#
8
# TODO: Signal handlers for protecting db files.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
9

10
11
12
13
14
15
16
#
# Common routines and constants for the client bootime setup stuff.
#
package libsetup;
use Exporter;
@ISA = "Exporter";
@EXPORT =
17
    qw ( libsetup_init libsetup_setvnodeid cleanup_node 
18
	 doifconfig dohostnames domounts dotunnels check_nickname
19
	 doaccounts dorpms dotarballs dostartupcmd install_deltas
20
	 bootsetup nodeupdate startcmdstatus whatsmynickname dosyncserver
21
22
	 TBBackGround TBForkCmd vnodejailsetup plabsetup vnodeplabsetup
	 dorouterconfig jailsetup dojailconfig JailedMounts findiface
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23
	 tmccdie tmcctimeout libsetup_getvnodeid dotrafficconfig
24

25
	 OPENTMCC CLOSETMCC RUNTMCC MFS REMOTE JAILED PLAB LOCALROOTFS
26

27
	 CONFDIR TMCC TMIFC TMDELAY TMRPM TMTARBALLS TMHOSTS TMJAILNAME
28
	 TMNICKNAME HOSTSFILE TMSTARTUPCMD FINDIF TMTUNNELCONFIG
29
	 TMTRAFFICCONFIG TMROUTECONFIG TMLINKDELAY TMDELMAP TMMOUNTDB
30
	 TMPROGAGENTS TMPASSDB TMGROUPDB TMGATEDCONFIG
31
32
	 TMCCCMD_REBOOT TMCCCMD_STATUS TMCCCMD_IFC TMCCCMD_ACCT TMCCCMD_DELAY
	 TMCCCMD_HOSTS TMCCCMD_RPM TMCCCMD_TARBALL TMCCCMD_STARTUP
33
	 TMCCCMD_DELTA TMCCCMD_STARTSTAT TMCCCMD_READY TMCCCMD_TRAFFIC
34
	 TMCCCMD_BOSSINFO TMCCCMD_VNODELIST TMCCCMD_ISALIVE TMCCCMD_LINKDELAYS
35
	 TMCCCMD_PROGRAMS
36
37
38
39
40
41
       );

# Must come after package declaration!
use English;

#
42
43
# For virtual (multiplexed nodes). If defined, tack onto tmcc command.
# and use in pathnames. Used in conjunction with jailed virtual nodes.
44
#
45
my $vnodeid;
46
47
48
49
50
51
52
53
sub libsetup_setvnodeid($)
{
    ($vnodeid) = @_;
}
sub libsetup_getvnodeid()
{
    return $vnodeid;
}
54

55
#
56
# True if running inside a jail. Set just below. 
57
58
59
# 
my $injail;

60
61
62
63
64
#
# True if running in a Plab vserver.
#
my $inplab;

65
66
67
# Load up the paths. Its conditionalized to be compatabile with older images.
# Note this file has probably already been loaded by the caller.
BEGIN
68
{
69
70
71
72
73
    if (! -e "/etc/emulab/paths.pm") {
	die("Yikes! Could not require /etc/emulab/paths.pm!\n");
    }
    require "/etc/emulab/paths.pm";
    import emulabpaths;
74

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    #
    # Determine if running inside a jail. This affects the paths below.
    #
    if (-e "$BOOTDIR/jailname") {
	open(VN, "$BOOTDIR/jailname");
	$vnodeid = <VN>;
	close(VN);

	if ($vnodeid =~ /^([-\w]+)$/) {
	    $vnodeid = $1;
	}
	else {
	    die("Bad data in vnodeid: $vnodeid");
	}
	$injail = 1;
    }

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    # Determine if running inside a Plab vserver.
    if (-e "$BOOTDIR/plabname") {
	open(VN, "$BOOTDIR/plabname");
	$vnodeid = <VN>;
	close(VN);

	if ($vnodeid =~ /^([-\w]+)$/) {
	    $vnodeid = $1;
	}
	else {
	    die("Bad data in vnodeid: $vnodeid");
	}
	$inplab = 1;
    }

107
108
109
110
111
112
113
    # Make sure these exist!
    if (! -e "$VARDIR/logs") {
	mkdir("$VARDIR", 0775);
	mkdir("$VARDIR/jails", 0775);
	mkdir("$VARDIR/db", 0755);
	mkdir("$VARDIR/logs", 0775);
	mkdir("$VARDIR/boot", 0775);
114
	mkdir("$VARDIR/lock", 0775);
115
    }
116
117
118
}

#
119
120
121
# The init routine. This is deprecated, but left behind in case an old
# liblocsetup is run against a new libsetup. Whenever a new libsetup
# is installed, better install the path module (see above) too!
122
#
123
124
125
126
127
128
129
130
131
sub libsetup_init($)
{
    my($path) = @_;

    $ETCDIR  = $path;
    $BINDIR  = $path;
    $VARDIR  = $path;
    $BOOTDIR = $path
}
132

133
#
134
# This "local" library provides the OS dependent part. 
135
#
136
use liblocsetup;
137

138
139
140
141
#
# These are the paths of various files and scripts that are part of the
# setup library.
#
142
143
144
sub TMCC()		{ "$BINDIR/tmcc"; }
sub TMHOSTS()		{ "$ETCDIR/hosts"; }
sub FINDIF()		{ "$BINDIR/findif"; }
145
sub HOSTSFILE()		{ "/etc/hosts"; }
146
#
147
# This path is valid only *outside* the jail when its setup.
148
# 
149
150
sub JAILDIR()		{ "$VARDIR/jails/$vnodeid"; }

151
152
153
154
155
#
# Also valid outside the jail, this is where we put local project storage.
#
sub LOCALROOTFS()	{ (REMOTE() ? "/users/local" : "$VARDIR/jails/local");}

156
157
158
#
# Okay, here is the path mess. There are three environments.
# 1. A local node where everything goes in one place ($VARDIR/boot).
159
# 2. A virtual node inside a jail or a Plab vserver ($VARDIR/boot).
160
161
162
163
164
165
# 3. A virtual node outside a jail (JAILDIR()).
#
# As for #3, whether setting up a old-style virtual node or a new style
# jailed node, the code that sets it up needs a different per-vnode path.
#
sub CONFDIR() {
166
    if ($injail || $inplab) {
167
168
169
170
171
172
	return $BOOTDIR;
    }
    if ($vnodeid) {
	return JAILDIR();
    }
    return $BOOTDIR;
173
}
174

175
176
177
178
179
180
181
182
183
184
185
186
187
#
# These go in /var/emulab. Good for all environments!
# 
sub TMMOUNTDB()		{ $VARDIR . "/db/mountdb"; }
sub TMSFSMOUNTDB()	{ $VARDIR . "/db/sfsmountdb"; }
sub TMPASSDB()		{ $VARDIR . "/db/passdb"; }
sub TMGROUPDB()		{ $VARDIR . "/db/groupdb"; }
#
# The rest of these depend on the environment running in (inside/outside jail).
# 
sub TMNICKNAME()	{ CONFDIR() . "/nickname";}
sub TMJAILNAME()	{ CONFDIR() . "/jailname";}
sub TMJAILCONFIG()	{ CONFDIR() . "/jailconfig";}
188
sub TMPLABCONFIG()	{ CONFDIR() . "/rc.plab";}
189
sub TMSTARTUPCMD()	{ CONFDIR() . "/startupcmd";}
190
sub TMPROGAGENTS()	{ CONFDIR() . "/progagents";}
191
192
193
194
sub TMIFC()		{ CONFDIR() . "/rc.ifc"; }
sub TMRPM()		{ CONFDIR() . "/rc.rpm";}
sub TMTARBALLS()	{ CONFDIR() . "/rc.tarballs";}
sub TMROUTECONFIG()     { CONFDIR() . "/rc.route";}
195
sub TMGATEDCONFIG()     { CONFDIR() . "/gated.conf";}
196
197
198
sub TMTRAFFICCONFIG()	{ CONFDIR() . "/rc.traffic";}
sub TMTUNNELCONFIG()	{ CONFDIR() . "/rc.tunnel";}
sub TMVTUNDCONFIG()	{ CONFDIR() . "/vtund.conf";}
199
200
sub TMDELAY()		{ CONFDIR() . "/rc.delay";}
sub TMLINKDELAY()	{ CONFDIR() . "/rc.linkdelay";}
201
sub TMDELMAP()		{ CONFDIR() . "/delay_mapping";}
202
203
sub TMSYNCSERVER()	{ CONFDIR() . "/syncserver";}
sub TMRCSYNCSERVER()	{ CONFDIR() . "/rc.syncserver";}
204

Austin Clements's avatar
Austin Clements committed
205
206
207
208
209
210
211
212
#
# 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;

213
#
214
215
216
217
218
# 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!
#
219
sub TMCD_VERSION()	{ 10; };
220
221
222

#
# These are the TMCC commands. 
223
224
225
226
227
228
#
sub TMCCCMD_REBOOT()	{ "reboot"; }
sub TMCCCMD_STATUS()	{ "status"; }
sub TMCCCMD_IFC()	{ "ifconfig"; }
sub TMCCCMD_ACCT()	{ "accounts"; }
sub TMCCCMD_DELAY()	{ "delay"; }
229
sub TMCCCMD_HOSTS()	{ "hostnames"; }
230
231
232
233
234
235
236
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"; }
237
sub TMCCCMD_ROUTING()	{ "routing"; }
238
239
sub TMCCCMD_TRAFFIC()	{ "trafgens"; }
sub TMCCCMD_BOSSINFO()	{ "bossinfo"; }
240
sub TMCCCMD_TUNNEL()	{ "tunnels"; }
241
sub TMCCCMD_NSECONFIGS(){ "nseconfigs"; }
242
sub TMCCCMD_VNODELIST() { "vnodelist"; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
243
sub TMCCCMD_ISALIVE()   { "isalive"; }
Austin Clements's avatar
Austin Clements committed
244
245
sub TMCCCMD_SFSHOSTID()	{ "sfshostid"; }
sub TMCCCMD_SFSMOUNTS() { "sfsmounts"; }
246
sub TMCCCMD_JAILCONFIG(){ "jailconfig"; }
247
sub TMCCCMD_PLABCONFIG(){ "plabconfig"; }
248
sub TMCCCMD_LINKDELAYS(){ "linkdelays"; }
249
sub TMCCCMD_PROGRAMS()  { "programs"; }
250
sub TMCCCMD_SYNCSERVER(){ "syncserver"; }
251
252
253
254

#
# Some things never change.
# 
255
my $TARINSTALL  = "/usr/local/bin/install-tarfile %s %s %s";
256
my $VTUND       = "/usr/local/sbin/vtund";
257
258
259
260

#
# This is a debugging thing for my home network.
# 
261
#my $NODE	= "-p 7778 REDIRECT=192.168.100.1";
262
my $NODE	= "-p 7778";
Austin Clements's avatar
Austin Clements committed
263
$NODE		= "";
264
265

# Locals
266
267
268
269
my $pid		= "";
my $eid		= "";
my $vname	= "";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
270
271
272
# Control tmcc error condition and timeout. Dynamic, not lexical!
$tmccdie        = 1; 
$tmcctimeout    = 0;
273
my $TMCCTIMEO   = 30;	# Default timeout on remote nodes. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
274

275
276
277
# 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.
278
sub MFS()	{ if (-e "$ETCDIR/ismfs") { return 1; } else { return 0; } }
279

280
281
282
#
# Same for a remote node.
#
283
284
sub REMOTE()	{ if (-e "$ETCDIR/isrem") { return 1; } else { return 0; } }

285
286
287
288
289
#
# Same for a control node.
#
sub CONTROL()	{ if (-e "$ETCDIR/isctrl") { return 1; } else { return 0; } }

290
291
292
293
#
# Are we jailed? See above.
#
sub JAILED()	{ if ($injail) { return $vnodeid; } else { return 0; } }
294

295
296
297
298
299
#
# Are we on plab?
#
sub PLAB()	{ if ($inplab) { return $vnodeid; } else { return 0; } }

300
301
302
303
304
305
306
307
#
# Do not try this on the MFS since it has such a wimpy perl installation.
#
if (!MFS()) {
    require Socket;
    import Socket;
}

308
309
310
311
#
# 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
312
# usage: OPENTMCC(char *command, char *args, char *options)
313
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
314
sub OPENTMCC($;$$)
315
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
316
    my($cmd, $args, $options) = @_;
317
    my $vn = "";
318
319
320
321
322
    local *TM;

    if (!defined($args)) {
	$args = "";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
323
324
325
    if (!defined($options)) {
	$options = "";
    }
326
    if (defined($vnodeid)) {
327
328
	$vn = "-n $vnodeid";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
329
330
331
    if ($tmcctimeout) {
	$options .= " -t $tmcctimeout";
    }
332

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
336
337
338
339
340
    if (!open(TM, $foo)) {
	print STDERR "Cannot start TMCC: $!\n";
	die("\n") if $tmccdie;
	return undef;
    }
341
342
343
    return (*TM);
}

344
345
346
347
348
349
#
# Close connection. Die on error.
# 
sub CLOSETMCC($) {
    my($TM) = @_;
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
350
351
352
353
354
355
356
357
358
359
360
    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;
361
362
}

363
364
365
#
# Run a TMCC command with the provided arguments.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
366
# usage: RUNTMCC(char *command, char *args, char *options)
367
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
368
sub RUNTMCC($;$$)
369
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
370
    my($cmd, $args, $options) = @_;
371
372
373
374
375
    my($TM);

    if (!defined($args)) {
	$args = "";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
376
377
378
    if (!defined($options)) {
	$options = "";
    }
379
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
380
    $TM = OPENTMCC($cmd, $args, $options);
381
382
383
384
385
386
387
388
389
390

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

#
# Reset to a moderately clean state.
#
391
392
393
sub cleanup_node ($) {
    my ($scrub) = @_;
    
394
    print STDOUT "Cleaning node; removing configuration files ...\n";
395
    unlink TMIFC, TMRPM, TMSTARTUPCMD, TMTARBALLS;
396
    unlink TMROUTECONFIG, TMTRAFFICCONFIG, TMTUNNELCONFIG;
397
    unlink TMDELAY, TMLINKDELAY, TMPROGAGENTS, TMSYNCSERVER, TMRCSYNCSERVER;
398
    unlink TMMOUNTDB . ".db";
399
    unlink TMSFSMOUNTDB . ".db";
400
    unlink "$VARDIR/db/rtabid";
401

402
403
404
405
406
    #
    # If scrubbing, remove the password/group file DBs so that we revert
    # to base set.
    # 
    if ($scrub) {
407
	unlink TMNICKNAME;
408
409
410
411
412
413
414
415
416
417
	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);
	}
418
419
    }

420
    return os_cleanup_node($scrub);
421
422
423
}

#
424
425
# Check node allocation. If the nickname file has been created, use
# that to avoid load on tmcd.
426
427
428
429
430
#
# Returns 0 if node is free. Returns list (pid/eid/vname) if allocated.
#
sub check_status ()
{
431
    my $TM = OPENTMCC(TMCCCMD_STATUS);
432
    $_  = <$TM>;
433
    CLOSETMCC($TM);
434
435

    if ($_ =~ /^FREE/) {
436
	unlink TMNICKNAME;
437
438
439
	return 0;
    }
    
440
    if ($_ =~ /ALLOCATED=([-\@\w]*)\/([-\@\w]*) NICKNAME=([-\@\w]*)/) {
441
442
443
444
445
446
447
448
	$pid   = $1;
	$eid   = $2;
	$vname = $3;
    }
    else {
	warn "*** WARNING: Error getting reservation status\n";
	return 0;
    }
449
450
451
    
    #
    # Stick our nickname in a file in case someone wants it.
452
453
    # Do not overwrite; we want to save the original info until later.
    # See bootsetup; indicates project change!
454
    #
455
    if (! -e TMNICKNAME()) {
456
457
	system("echo '$vname.$eid.$pid' > " . TMNICKNAME());
    }
458
    
459
460
461
462
    return ($pid, $eid, $vname);
}

#
463
464
465
466
# Check cached nickname. Its okay if we have been deallocated and the info
# is stale. The node will notice that later.
# 
sub check_nickname()
467
{
468
469
470
    if (-e TMNICKNAME) {
	my $nickfile = TMNICKNAME;
	my $nickinfo = `cat $nickfile`;
471

472
473
474
475
476
477
478
479
480
	if ($nickinfo =~ /([-\@\w]*)\.([-\@\w]*)\.([-\@\w]*)/) {
	    $vname = $1;
	    $eid   = $2;
	    $pid   = $3;

	    return ($pid, $eid, $vname);
	}
    }
    return check_status();
481
482
483
484
}

#
# Process mount directives from TMCD. We keep track of all the mounts we
485
# have added in here so that we delete just the mounts we added, when
486
487
488
489
490
491
492
493
494
# 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
495
496
497
    my %sfsmounts;
    my %sfsdeletes;

498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
    #
    # Update our SFS hostid first. If this fails, dosfshostid will
    # unset USESFS.
    # 
    if ($USESFS) {
	if (! MFS()) {
	    #
	    # Setup SFS hostid.
	    #
	    print STDOUT "Setting up for SFS ... \n";
	    dosfshostid();
	}
	else {
	    # No SFS on the MFS.
	    $USESFS = 0;
	}
    }

Austin Clements's avatar
Austin Clements committed
516
    $TM = OPENTMCC(TMCCCMD_MOUNTS, "USESFS=$USESFS");
517
518

    while (<$TM>) {
Austin Clements's avatar
Austin Clements committed
519
	if ($_ =~ /^REMOTE=([-:\@\w\.\/]+) LOCAL=([-\@\w\.\/]+)/) {
520
521
	    $mounts{$1} = $2;
	}
Austin Clements's avatar
Austin Clements committed
522
523
524
525
526
527
	elsif ($_ =~ /^SFS REMOTE=([-:\@\w\.\/]+) LOCAL=([-\@\w\.\/]+)/) {
	    $sfsmounts{$1} = $2;
	}
	else {
	    warn "*** WARNING: Malformed mount information: $_\n";
	}
528
    }
529
530
    CLOSETMCC($TM);
    
531
532
533
534
    #
    # The MFS version does not support (or need) this DB stuff. Just mount
    # them up.
    #
535
    if (MFS()) {
536
537
	while (($remote, $local) = each %mounts) {
	    if (! -e $local) {
538
		if (! os_mkdir($local, "0770")) {
539
540
541
542
543
544
		    warn "*** WARNING: Could not make directory $local: $!\n";
		    next;
		}
	    }
	
	    print STDOUT "  Mounting $remote on $local\n";
545
546
547
	    if (system("$NFSMOUNT $remote $local")) {
		warn "*** WARNING: Could not $NFSMOUNT ".
		    "$remote on $local: $!\n";
548
549
550
551
552
553
		next;
	    }
	}
	return 0;
    }

554
555
556
557
558
559
560
    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) {
561
	if (defined($MDB{$remote})) {
562
563
564
	    next;
	}

565
566
567
568
569
570
	if (! -d $local) {
	    # Leftover SFS link.
	    if (-l $local) {
		unlink($local) or
		    warn "*** WARNING: Could not unlink $local: $!\n";
	    }
571
	    if (! os_mkdir($local, "0770")) {
572
573
574
575
576
577
		warn "*** WARNING: Could not make directory $local: $!\n";
		next;
	    }
	}
	
	print STDOUT "  Mounting $remote on $local\n";
578
579
	if (system("$NFSMOUNT $remote $local")) {
	    warn "*** WARNING: Could not $NFSMOUNT $remote on $local: $!\n";
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
614
615
	    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;
	}

	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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
    #
    # 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";
637
		    if (! unlink($local)) {
Austin Clements's avatar
Austin Clements committed
638
639
640
641
642
			warn "*** WARNING: Could not unlink $local: $!\n";
			next;
		    }
		}
	    }
643
644
645
646
647
648
	    elsif (-d $local) {
		if (! rmdir($local)) {
		    warn "*** WARNING: Could not rmdir $local: $!\n";
		    next;
		}
	    }
Austin Clements's avatar
Austin Clements committed
649
650
651
	    
	    $dir = $local;
	    $dir =~ s/(.*)\/[^\/]*$/$1/;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
652
	    if ($dir ne "" && ! -e $dir) {
Austin Clements's avatar
Austin Clements committed
653
		print STDOUT "  Making directory $dir\n";
654
		if (! os_mkdir($dir, "0755")) {
Austin Clements's avatar
Austin Clements committed
655
656
657
658
659
		    warn "*** WARNING: Could not make directory $local: $!\n";
		    next;
		}
	    }
	    print STDOUT "  Symlinking $remote on $local\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
660
	    if (! symlink("/sfs/" . $remote, $local)) {
Austin Clements's avatar
Austin Clements committed
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
		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
706
	$USESFS = 0;
Austin Clements's avatar
Austin Clements committed
707
708
709
710
711
    }

    return 0;
}

712
#
713
714
715
# Aux function called from the mkjail code to do mounts outside
# of a jail, and return the list of mounts that were created. Can use
# either NFS or local loopback. Maybe SFS someday. Local only, of course.
716
# 
717
sub JailedMounts($$$)
718
{
719
    my ($vid, $rootpath, $usenfs) = @_;
720
    my @mountlist = ();
721
    my $mountstr;
722
723
724
725
726
727
728
729

    #
    # No NFS mounts on remote nodes.
    # 
    if (REMOTE()) {
	return ();
    }

730
731
732
733
734
735
    if ($usenfs) {
	$mountstr = $NFSMOUNT;
    } else {
	$mountstr = $LOOPBACKMOUNT;
    }

736
737
738
739
740
    #
    # Mount same set of existing mounts. A hack, but this whole NFS thing
    # is a serious hack inside jails.
    #
    dbmopen(%MDB, TMMOUNTDB, 0444);
741
    
742
    while (my ($remote, $path) = each %MDB) {
743
	$local = "$rootpath$path";
744
745
746
747
748
749
750
751
	    
	if (! -e $local) {
	    if (! os_mkdir($local, "0770")) {
		warn "*** WARNING: Could not make directory $local: $!\n";
		next;
	    }
	}
	
752
753
754
755
	if (! $usenfs) {
	    $remote = $path;
	}

756
	print STDOUT "  Mounting $remote on $local\n";
757
758
	if (system("$mountstr $remote $local")) {
	    warn "*** WARNING: Could not $mountstr $remote on $local: $!\n";
759
760
761
762
	    next;
	}
	push(@mountlist, $path);
    }
763
    dbmclose(%MDB);	   
764
765
    return @mountlist;
}
Austin Clements's avatar
Austin Clements committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#
# 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
784
785
786
787
788
789
    if (-d "/usr/local/lib/sfs-0.6") {
	$myhostid = `sfskey hostid - 2>/dev/null`;
    }
    else {
	$myhostid = `sfskey hostid -s authserv - 2>/dev/null`;
    }
790
    if (! $?) {
Austin Clements's avatar
Austin Clements committed
791
792
793
794
795
	if ( $myhostid =~ /^([-\.\w_]*:[a-z0-9]*)$/ ) {
	    $myhostid = $1;
	    print STDOUT "  Hostid: $myhostid\n";
	    RUNTMCC(TMCCCMD_SFSHOSTID, "$myhostid");
	}
796
797
798
799
800
	elsif ( $myhostid =~ /^(@[-\.\w_]*,[a-z0-9]*)$/ ) {
	    $myhostid = $1;
	    print STDOUT "  Hostid: $myhostid\n";
	    RUNTMCC(TMCCCMD_SFSHOSTID, "$myhostid");
	}
Austin Clements's avatar
Austin Clements committed
801
802
803
804
805
	else {
	    warn "*** WARNING: Invalid hostid\n";
	}
    }
    else {
806
	warn "*** WARNING: Could not retrieve this node's SFShostid!\n";
Austin Clements's avatar
Austin Clements committed
807
808
809
810
811
812
	$USESFS = 0;
    }

    return 0;
}

813
814
815
816
#
# Do interface configuration.    
# Write a file of ifconfig lines, which will get executed.
#
817
sub doifconfig (;$)
818
{
819
820
821
822
823
    my ($rtabid) = @_;
    my @ifaces = ();
    my $upcmds   = "";
    my $downcmds = "";

824
825
826
827
828
829
830
    #
    # Kinda ugly, but there is too much perl goo included by Socket to put it
    # on the MFS. 
    # 
    if (MFS()) {
	return 1;
    }
831

832
    my $TM = OPENTMCC(TMCCCMD_IFC);
833
    while (<$TM>) {
834
835
836
	push(@ifaces, $_);
    }
    CLOSETMCC($TM);
837

838
839
840
841
842
843
844
845
846
    #
    # Create the interface list file.
    # Control net is always first.
    #
    open(XIFS, ">$BOOTDIR/tmcc.ifs") or
	die "Cannot open file $BOOTDIR/tmcc.ifs: $!";

    print XIFS `control_interface`;

847
    if (! @ifaces) {
848
	close(XIFS);
849
850
851
852
853
854
855
856
	return 0;
    }

    my $ethpat  = q(IFACETYPE=(\w*) INET=([0-9.]*) MASK=([0-9.]*) MAC=(\w*) );
    $ethpat    .= q(SPEED=(\w*) DUPLEX=(\w*) IPALIASES="(.*)");

    my $vethpat = q(IFACETYPE=(\w*) INET=([0-9.]*) MASK=([0-9.]*) ID=(\d*) );
    $vethpat   .= q(VMAC=(\w*) PMAC=(\w*));
857

858
859
    foreach my $iface (@ifaces) {
	if ($iface =~ /$ethpat/) {
860
861
862
863
864
	    my $inet     = $2;
	    my $mask     = $3;
	    my $mac      = $4;
	    my $speed    = $5; 
	    my $duplex   = $6;
865
	    my $aliases  = $7;
866
	    my $routearg = inet_ntoa(inet_aton($inet) & inet_aton($mask));
867

868
	    if (my $iface = findiface($mac)) {
869
870
871
872
873
874

		print XIFS "$iface\n";
		if (JAILED()) {
		    next;
		}

875
		my ($upline, $downline) =
876
		    os_ifconfig_line($iface, $inet,
877
				     $mask, $speed, $duplex, $aliases,$rtabid);
878
		    
879
		$upcmds   .= "$upline\n    ";
880
881
		$upcmds   .= TMROUTECONFIG . " $routearg up\n";
		
882
883
		$downcmds .= TMROUTECONFIG . " $routearg down\n    ";
		$downcmds .= "$downline\n    "
884
		    if (defined($downline));
885
886
887
888
889
890

		# There could be routes for each alias.
		foreach my $alias (split(',', $aliases)) {
		    $routearg = inet_ntoa(inet_aton($alias) &
					  inet_aton($mask));
			
891
892
		    $upcmds   .= TMROUTECONFIG . " $routearg up\n";
		    $downcmds .= TMROUTECONFIG . " $routearg down\n";
893
		}
894
895
	    }
	    else {
896
		warn "*** WARNING: Bad MAC: $mac\n";
897
898
	    }
	}
899
900
901
902
903
904
905
906
907
	elsif ($iface =~ /$vethpat/) {
	    my $iface    = undef;
	    my $inet     = $2;
	    my $mask     = $3;
	    my $id       = $4;
	    my $vmac     = $5;
	    my $pmac     = $6; 
	    my $routearg = inet_ntoa(inet_aton($inet) & inet_aton($mask));

908
909
910
911
912
913
914
	    if (JAILED()) {
		if ($iface = findiface($vmac)) {
		    print XIFS "$iface\n";
		}
		next;
	    }

915
916
	    if ($pmac eq "none" ||
		($iface = findiface($pmac))) {
917
918
		print XIFS "$iface\n"
		    if (defined($iface));
919

920
921
922
		my ($upline, $downline) =
		    os_ifconfig_veth($iface, $inet, $mask, $id, $vmac,$rtabid);
		    
923
		$upcmds   .= "$upline\n    ";
924
925
		$upcmds   .= TMROUTECONFIG . " $routearg up\n";
		
926
927
		$downcmds .= TMROUTECONFIG . " $routearg down\n    ";
		$downcmds .= "$downline\n    "
928
929
930
931
932
933
		    if (defined($downline));
	    }
	    else {
		warn "*** WARNING: Bad PMAC: $pmac\n";
	    }
	}
934
935
936
937
	else {
	    warn "*** WARNING: Bad ifconfig line: $_";
	}
    }
938
939
940
941
942
    close(XIFS);
    if (JAILED()) {
	return 0;
    }

943
944
945
946
947
948
949
950
951
952
    #
    # 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";
    print IFC "# auto-generated by libsetup.pm, DO NOT EDIT\n";
    print IFC "if [ x\$1 = x ]; then action=enable; else action=\$1; fi\n";
    print IFC "case \"\$action\" in\n";
    print IFC "  enable)\n";
953
954
    print IFC "    $upcmds\n";
    print IFC "    ;;\n";
955
    print IFC "  disable)\n";
956
957
    print IFC "    $downcmds\n";
    print IFC "    ;;\n";
958
    print IFC "esac\n";
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
    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;
}

986
987
988
989
#
# Do router configuration stuff. This just writes a file for someone else
# to deal with.
#
990
sub dorouterconfig (;$)
991
{
992
993
994
995
996
    my ($rtabid) = @_;
    my @stuff    = ();
    my $routing  = 0;
    my %upmap    = ();
    my %downmap  = ();
997
998
999
1000
1001
1002
    my $TM;

    $TM = OPENTMCC(TMCCCMD_ROUTING);
    while (<$TM>) {
	push(@stuff, $_);
    }
1003
    CLOSETMCC($TM);
1004
1005
1006
1007
1008
1009

    if (! @stuff) {
	return 0;
    }

    #
1010
1011
    # Look for router type. If none, we still write the file since other
    # scripts expect this to exist.
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
    # 
    foreach my $line (@stuff) {
	if (($line =~ /ROUTERTYPE=(.+)/) && ($1 ne "none")) {
	    $routing = 1;
	    last;
	}
    }
    
    open(RC, ">" . TMROUTECONFIG)
	or die("Could not open " . TMROUTECONFIG . ": $!");

1023
1024
1025
    print RC "#!/bin/sh\n";
    print RC "# auto-generated by libsetup.pm, DO NOT EDIT\n";

1026
1027
1028
1029
1030
1031
1032
    if (! $routing) {
	print RC "true\n";
	close(RC);
	chmod(0755, TMROUTECONFIG);
	return 0;
    }

1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
    #
    # 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;
1049
    foreach my $line (@stuff) {
1050
1051
1052
1053
1054
	if ($line =~ /ROUTERTYPE=(gated|ospf)/) {
	    $usegated = 1;
	} elsif ($line =~ /ROUTERTYPE=(manual|static)/) {
	    $usemanual = 1;
	} elsif ($usemanual && $line =~ /$pat/) {
1055
	    my $dip   = $1;
1056
1057
	    my $rtype = $2;
	    my $dmask = $3;
1058
1059
1060
	    my $gate  = $4;
	    my $cost  = $5;
	    my $routearg = inet_ntoa(inet_aton($gate) & inet_aton($dmask));
1061

1062
1063
1064
1065
	    if (! defined($upmap{$routearg})) {
		$upmap{$routearg} = [];
		$downmap{$routearg} = [];
	    }
1066
1067
	    $rcline = os_routing_add_manual($rtype, $dip,
					    $dmask, $gate, $cost, $rtabid);
1068
	    push(@{$upmap{$routearg}}, $rcline);
1069
1070
	    $rcline = os_routing_del_manual($rtype, $dip,
					    $dmask, $gate, $cost, $rtabid);
1071
	    push(@{$downmap{$routearg}}, $rcline);
1072
1073
1074
1075
1076
	} else {
	    warn "*** WARNING: Bad routing line: $line\n";
	}
    }

1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
    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";

1102
1103
1104
1105
1106
1107
1108
1109
    #
    # 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) {
1110
	$rcline = gatedsetup();
1111
	print RC "    $rcline\n";
1112
    }
1113
1114
1115
    print RC "  ;;\n";
    print RC "esac\n";
    print RC "exit 0\n";
1116

1117
    close(RC);
1118
    chmod(0755, TMROUTECONFIG);
1119
1120
1121
1122

    return 0;
}

1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
sub gatedsetup ()
{
    my ($cnet, @xifs) = split('\n', `cat $BOOTDIR/tmcc.ifs`);

    open(IFS, ">" . TMGATEDCONFIG)
	or die("Could not open " . TMGATEDCONFIG . ": $!");

    print IFS "# auto-generated by libsetup.pm, DO NOT EDIT\n\n";
    #
    # XXX hack: in a jail, the control net is an IP alias with a host mask.
    # This blows gated out of the water, so we have to make the control
    # interface appear to have a subnet mask.
    #
    if (JAILED() && -e "$BOOTDIR/myip") {
	my $hostip = `cat $BOOTDIR/myip`;
	chomp($hostip);
	print IFS "interfaces {\n".
	    "\tdefine subnet local $hostip netmask 255.240.0.0;\n};\n";
    }
    print IFS "smux off;\nrip off;\nospf on {\n";
    print IFS "\tbackbone {\n\t\tinterface $cnet { passive; };\n\t};\n";
    print IFS "\tarea 0.0.0.2 {\n\t\tauthtype none;\n";

    foreach my $xif (@xifs) {
	print IFS "\t\tinterface $xif { priority 1; };\n";
    }

    print IFS "\t};\n};\n";
    close(IFS);

    return os_routing_enable_gated(TMGATEDCONFIG);
}

1156
1157
1158
1159
1160
1161
#
# Host names configuration (/etc/hosts). 
#
sub dohostnames ()
{
    my $TM;
1162
    my $HTEMP = HOSTSFILE . ".new";
1163
1164

    #
1165
1166
1167
    # Note, we no longer start with the 'prototype' file here, because we have
    # to make up a localhost line that's properly qualified.
    #
1168
1169
    $TM = OPENTMCC(TMCCCMD_HOSTS);

1170
1171
    open(HOSTS, ">$HTEMP")
	or die("Could not open $HTEMP: $!");
1172

1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
    my $localaliases = "loghost";

    #
    # Find out our domain name, so that we can qualify the localhost entry
    #
    my $hostname = `hostname`;
    if ($hostname =~ /[^.]+\.(.+)/) {
	$localaliases .= " localhost.$1";
    }
    
    #
    # First, write a localhost line into the hosts file - we have to know the
    # domain to use here
    #
1187
1188
    print HOSTS os_etchosts_line("localhost", "127.0.0.1",
				 $localaliases), "\n";
1189

1190
1191
    #
    # Now convert each hostname into hosts file representation and write
1192
1193
1194
1195
1196
    # 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\. ]*)\');
    
1197
    while (<$TM>) {
1198
1199
1200
1201
1202
1203
	if ($_ =~ /$pat/) {
	    my $name    = $1;
	    my $ip      = $2;
	    my $aliases = $3;
	    
	    my $hostline = os_etchosts_line($name, $ip, $aliases);
1204
1205
1206
1207
1208
1209
1210
	    
	    print HOSTS  "$hostline\n";
	}
	else {
	    warn "*** WARNING: Bad hosts line: $_";
	}
    }
1211
    CLOSETMCC($TM);
1212
1213
    close(HOSTS);
    system("mv -f $HTEMP " . HOSTSFILE) == 0 or
1214
	warn("*** Could not mv $HTEMP to ". HOSTSFILE . "!\n");
1215
1216
1217
1218

    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1219
sub doaccounts()
1220
1221
{
    my %newaccounts = ();
1222
    my %newgroups   = ();
1223
1224
    my %pubkeys1    = ();
    my %pubkeys2    = ();
Austin Clements's avatar
Austin Clements committed
1225
    my @sfskeys     = ();
1226
    my %deletes     = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1227
    my %lastmod     = ();
1228
1229
    my %PWDDB;
    my %GRPDB;
1230
1231
1232
1233

    my $TM = OPENTMCC(TMCCCMD_ACCT);

    #
1234
1235
1236
1237
1238
1239
    # 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.
1240
1241
1242
    #
    while (<$TM>) {
	if ($_ =~ /^ADDGROUP NAME=([-\@\w.]+) GID=([0-9]+)/) {
1243
1244
1245
	    #
	    # Group info goes in the hash table.
	    #
1246
1247
	    my $gname = "$1";
	    
1248
	    if (REMOTE() && !JAILED() && !PLAB()) {
1249
		$gname = "emu-${gname}";
1250
	    }
1251
	    $newgroups{"$gname"} = $2
1252
	}
1253
	elsif ($_ =~ /^ADDUSER LOGIN=([0-9A-Za-z]+)/) {
1254
1255
1256
1257
1258
1259
	    #
	    # Account info goes in the hash table.
	    # 
	    $newaccounts{$1} = $_;
	    next;
	}
1260
	elsif ($_ =~ /^PUBKEY LOGIN=([0-9A-Za-z]+) KEY="(.*)"/) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1261
1262
1263
	    #
	    # Keys go into hash as a list of keys.
	    #
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
	    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
1281
1282
1283
	    }
	    next;
	}
Austin Clements's avatar
Austin Clements committed
1284
1285
1286
1287
1288
1289
1290
	elsif ($_ =~ /^SFSKEY KEY="(.*)"/) {
	    #
	    # SFS key goes into the array.
	    #
	    push(@sfskeys, $1);
	    next;
	}
1291
	else {
1292