liblocsetup.pm 12 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2004 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5
6
7
# All rights reserved.
#

8
9
10
11
12
13
14
#
# Linux specific routines and constants for the client bootime setup stuff.
#
package liblocsetup;
use Exporter;
@ISA = "Exporter";
@EXPORT =
Mike Hibler's avatar
Mike Hibler committed
15
    qw ( $CP $EGREP $NFSMOUNT $UMOUNT $TMPASSWD $SFSSD $SFSCD $RPMCMD
16
	 os_account_cleanup os_ifconfig_line os_etchosts_line
17
	 os_setup os_groupadd os_useradd os_userdel os_usermod os_mkdir
Mike Hibler's avatar
Mike Hibler committed
18
	 os_ifconfig_veth
19
20
	 os_routing_enable_forward os_routing_enable_gated
	 os_routing_add_manual os_routing_del_manual os_homedirdel
21
	 os_groupdel os_getnfsmounts
22
23
24
25
26
       );

# Must come after package declaration!
use English;

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Load up the paths. Its conditionalized to be compatabile with older images.
# Note this file has probably already been loaded by the caller.
BEGIN
{
    if (-e "/etc/emulab/paths.pm") {
	require "/etc/emulab/paths.pm";
	import emulabpaths;
    }
    else {
	my $ETCDIR  = "/etc/rc.d/testbed";
	my $BINDIR  = "/etc/rc.d/testbed";
	my $VARDIR  = "/etc/rc.d/testbed";
	my $BOOTDIR = "/etc/rc.d/testbed";
    }
}
42
43
44
45
46
47

#
# Various programs and things specific to Linux and that we want to export.
# 
$CP		= "/bin/cp";
$EGREP		= "/bin/egrep -q";
48
$NFSMOUNT	= "/bin/mount";
49
$UMOUNT		= "/bin/umount";
50
$TMPASSWD	= "$ETCDIR/passwd";
51
52
$SFSSD		= "/usr/local/sbin/sfssd";
$SFSCD		= "/usr/local/sbin/sfscd";
Mike Hibler's avatar
Mike Hibler committed
53
$RPMCMD		= "/bin/rpm";
54
55
56
57

#
# These are not exported
#
58
59
60
my $TMGROUP	= "$ETCDIR/group";
my $TMSHADOW    = "$ETCDIR/shadow";
my $TMGSHADOW   = "$ETCDIR/gshadow";
61
62
63
64
my $USERADD     = "/usr/sbin/useradd";
my $USERDEL     = "/usr/sbin/userdel";
my $USERMOD     = "/usr/sbin/usermod";
my $GROUPADD	= "/usr/sbin/groupadd";
65
my $GROUPDEL	= "/usr/sbin/groupdel";
66
67
my $IFCONFIGBIN = "/sbin/ifconfig";
my $IFCONFIG    = "$IFCONFIGBIN %s inet %s netmask %s";
68
my $IFC_1000MBS  = "1000baseTx";
69
70
71
72
my $IFC_100MBS  = "100baseTx";
my $IFC_10MBS   = "10baseT";
my $IFC_FDUPLEX = "FD";
my $IFC_HDUPLEX = "HD";
73
my @LOCKFILES   = ("/etc/group.lock", "/etc/gshadow.lock");
74
my $MKDIR	= "/bin/mkdir";
75
76
my $GATED	= "/usr/sbin/gated";
my $ROUTE	= "/sbin/route";
77
78
my $SHELLS	= "/etc/shells";
my $DEFSHELL	= "/bin/tcsh";
79
80
81
82

#
# OS dependent part of cleanup node state.
# 
83
84
sub os_account_cleanup()
{
85
86
87
88
89
    unlink @LOCKFILES;

    printf STDOUT "Resetting passwd and group files\n";
    if (system("$CP -f $TMGROUP $TMPASSWD /etc") != 0) {
	print STDERR "Could not copy default group file into place: $!\n";
90
	return -1;
91
92
93
94
    }
    
    if (system("$CP -f $TMSHADOW $TMGSHADOW /etc") != 0) {
	print STDERR "Could not copy default passwd file into place: $!\n";
95
	return -1;
96
97
98
99
100
101
102
103
    }
    return 0;
}

#
# Generate and return an ifconfig line that is approriate for putting
# into a shell script (invoked at bootup).
#
104
sub os_ifconfig_line($$$$$$$;$$)
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
    my ($iface, $inet, $mask, $speed, $duplex, $aliases,
	$iface_type, $settings, $rtabid) = @_;
    my ($miirest, $miisleep, $miisetspd, $media);
    my ($uplines, $downlines);

    #
    # Special handling for new style interfaces (which have settings).
    # This should all move into per-type modules at some point. 
    #
    if (defined($settings) && exists($settings->{"protocol"}) &&
	$settings->{"protocol"} ne "ethernet") {

	#
	# Setting the protocol is special and appears to be card specific.
	# How stupid is that!
	#
	my $protocol = $settings->{"protocol"};
	my $privcmd  = "";
	
	if ($iface_type eq "ath") {
	    $privcmd = "/sbin/iwpriv $iface mode ";

	    SWITCH1: for ($protocol) {
		/^80211a$/ && do {
		    $privcmd .= "1";
		    last SWITCH1;
		};
		/^80211b$/ && do {
		    $privcmd .= "2";
		    last SWITCH1;
		};
		/^80211g$/ && do {
		    $privcmd .= "3";
		    last SWITCH1;
		};
	    }
	}
	else {
	    warn("*** WARNING: Unsupported interface type $iface_type!\n");
	    return undef;
	}
	 
	#
	# At the moment, we expect just the various flavors of 80211, and
	# we treat them all the same, configuring with iwconfig and iwpriv.
	#
	my $iwcmd = "/sbin/iwconfig $iface ";

	#
	# We demand to be given an ssid.
	#
	if (!exists($settings->{"ssid"})) {
	    warn("*** WARNING: No SSID provided for $iface!\n");
	    return undef;
	}
	$iwcmd .= "essid ". $settings->{"ssid"};

	# If we do not get a channel, pick one.
	if (exists($settings->{"channel"})) {
	    $iwcmd .= " channel " . $settings->{"channel"};
	}
	else {
	    $iwcmd .= " channel 3";
	}

	# txpower and rate default to auto if not specified.
	if (exists($settings->{"rate"})) {
	    $iwcmd .= " rate " . $settings->{"rate"};
	}
	else {
	    $iwcmd .= " rate auto";
	}
	if (exists($settings->{"txpower"})) {
	    $iwcmd .= " txpower " . $settings->{"txpower"};
	}
	else {
	    $iwcmd .= " txpower auto";
	}
	# Allow this too. 
	if (exists($settings->{"sensitivity"})) {
	    $iwcmd .= " sens " . $settings->{"sensitivity"};
	}

	#
	# We demand to be told if we are the master or a peon.
	# This needs to be last for some reason.
	#
	if (!exists($settings->{"accesspoint"})) {
	    warn("*** WARNING: No accesspoint provided for $iface!\n");
	    return undef;
	}
	my $accesspoint = $settings->{"accesspoint"};
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
	my $accesspointwdots;

	# Allow either dotted or undotted notation!
	if ($accesspoint =~ /^(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})(\w{2})$/) {
	    $accesspointwdots = "$1:$2:$3:$4:$5:$6";
	}
	elsif ($accesspoint =~
	       /^(\w{2}):(\w{2}):(\w{2}):(\w{2}):(\w{2}):(\w{2})$/) {
	    $accesspointwdots = $accesspoint;
	    $accesspoint      = "${1}${2}${3}${4}${5}${6}";
	}
	else {
	    warn("*** WARNING: Improper format for MAC ($accesspoint) ".
		 "provided for $iface!\n");
	    return undef;
	}
214
215
216
217
218
	    
	if (libsetup::findiface($accesspoint) eq $iface) {
	    $iwcmd .= " mode Master";
	}
	else {
219
	    $iwcmd .= " mode Managed ap $accesspointwdots";
220
221
222
223
224
225
226
227
	}

	$uplines   = sprintf($IFCONFIG, $iface, $inet, $mask) . "\n";
	$uplines  .= $privcmd . "\n";
	$uplines  .= $iwcmd;
	$downlines = "$IFCONFIGBIN $iface down";
	return ($uplines, $downlines);
    }
228
229
230
231
232
233
234
235
236
237
238
239

    #
    # Need to check units on the speed. Just in case.
    #
    if ($speed =~ /(\d*)([A-Za-z]*)/) {
	if ($2 eq "Mbps") {
	    $speed = $1;
	}
	elsif ($2 eq "Kbps") {
	    $speed = $1 / 1000;
	}
	else {
240
	    warn("*** Bad speed units $2 in ifconfig, default to 100Mbps\n");
241
242
	    $speed = 100;
	}
243
244
245
246
	if ($speed == 1000) {
	    $media = $IFC_1000MBS;
	}
	elsif ($speed == 100) {
247
248
249
250
251
252
	    $media = $IFC_100MBS;
	}
	elsif ($speed == 10) {
	    $media = $IFC_10MBS;
	}
	else {
253
254
	    warn("*** Bad Speed $speed in ifconfig, default to 100Mbps\n");
	    $speed = 100;
255
256
257
258
259
260
261
262
263
264
	    $media = $IFC_100MBS;
	}
    }
    if ($duplex eq "full") {
	$media = "$media-$IFC_FDUPLEX";
    }
    elsif ($duplex eq "half") {
	$media = "$media-$IFC_HDUPLEX";
    }
    else {
265
266
	warn("*** Bad duplex $duplex in ifconfig, default to full\n");
	$duplex = "full";
267
268
269
	$media = "$media-$IFC_FDUPLEX";
    }

270
271
272
273
274
275
276
277
    #
    # Linux is apparently changing from mii-tool to ethtool but some drivers
    # don't support the new interface (3c59x), some don't support the old
    # interface (e1000), and some (eepro100) support the new interface just
    # enough that they can report success but not actually do anything. Sweet!
    #
    if (-e "/usr/sbin/ethtool") {
	# this seems to work for returning an error on eepro100
278
	$uplines = "if /usr/sbin/ethtool $iface >/dev/null 2>&1; then\n    " .
279
280
281
282
283
	       "  /usr/sbin/ethtool -s $iface autoneg off speed $speed duplex $duplex\n    " .
	       "else\n    " .
	       "  /sbin/mii-tool --force=$media $iface\n    " .
		   "fi\n    ";
    } else {
284
	$uplines = "/sbin/mii-tool --force=$media $iface\n    ";
285
286
    }

287
288
    $uplines  .= sprintf($IFCONFIG, $iface, $inet, $mask);
    $downlines = "$IFCONFIGBIN $iface down";
289
    
290
    return ($uplines, $downlines);
291
292
}

293
294
295
#
# Specialized function for configing locally hacked veth devices.
#
296
sub os_ifconfig_veth($$$$$;$$)
297
298
299
300
{
    return "";
}

301
302
303
304
#
# Generate and return an string that is approriate for putting
# into /etc/hosts.
#
305
sub os_etchosts_line($$$)
306
{
307
    my ($name, $ip, $aliases) = @_;
308
    
309
    return sprintf("%s\t%s %s", $ip, $name, $aliases);
310
311
312
313
314
315
316
317
318
319
320
321
}

#
# Add a new group
# 
sub os_groupadd($$)
{
    my($group, $gid) = @_;

    return system("$GROUPADD -g $gid $group");
}

322
323
324
325
326
327
328
329
330
331
#
# Delete an old group
# 
sub os_groupdel($)
{
    my($group) = @_;

    return system("$GROUPDEL $group");
}

332
333
334
335
336
337
338
339
340
341
342
343
344
#
# Remove a user account.
# 
sub os_userdel($)
{
    my($login) = @_;

    return system("$USERDEL $login");
}

#
# Modify user group membership.
# 
345
sub os_usermod($$$$$$)
346
{
347
    my($login, $gid, $glist, $pswd, $root, $shell) = @_;
348
349
350
351
352
353
354

    if ($root) {
	$glist = join(',', split(/,/, $glist), "root");
    }
    if ($glist ne "") {
	$glist = "-G $glist";
    }
355
356
    # Map the shell into a full path.
    $shell = MapShell($shell);
357

358
    return system("$USERMOD -s $shell -g $gid $glist -p '$pswd' $login");
359
360
361
362
363
}

#
# Add a user.
# 
364
sub os_useradd($$$$$$$$$)
365
{
366
    my($login, $uid, $gid, $pswd, $glist, $homedir, $gcos, $root, $shell) = @_;
367
368
369
370
371
372
373

    if ($root) {
	$glist = join(',', split(/,/, $glist), "root");
    }
    if ($glist ne "") {
	$glist = "-G $glist";
    }
374
375
    # Map the shell into a full path.
    $shell = MapShell($shell);
376

377
    if (system("$USERADD -M -u $uid -g $gid $glist -p '$pswd' ".
378
	       "-d $homedir -s $shell -c \"$gcos\" $login") != 0) {
379
380
381
382
383
384
	warn "*** WARNING: $USERADD $login error.\n";
	return -1;
    }
    return 0;
}

385
386
387
388
389
390
391
392
#
# Remove a homedir. Might someday archive and ship back.
#
sub os_homedirdel($$)
{
    return 0;
}

393
394
395
396
397
398
399
400
401
402
403
404
405
#
# Create a directory including all intermediate directories.
#
sub os_mkdir($$)
{
    my ($dir, $mode) = @_;

    if (system("$MKDIR -p -m $mode $dir")) {
	return 0;
    }
    return 1;
}

406
407
408
409
410
411
412
413
#
# OS Dependent configuration. 
# 
sub os_setup()
{
    return 0;
}
    
414
415
416
417
418
419
420
421
422
423
424
#
# OS dependent, routing-related commands
#
sub os_routing_enable_forward()
{
    my $cmd;

    $cmd = "sysctl -w net.ipv4.conf.all.forwarding=1";
    return $cmd;
}

425
sub os_routing_enable_gated($)
426
{
427
    my ($conffile) = @_;
428
429
    my $cmd;

430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
    #
    # XXX hack to avoid gated dying with TCP/616 already in use.
    #
    # Apparently the port is used by something contacting ops's
    # portmapper (i.e., NFS mounts) and probably only happens when
    # there are a bazillion NFS mounts (i.e., an experiment in the
    # testbed project).
    #
    $cmd  = "for try in 1 2 3 4 5 6; do\n";
    $cmd .= "\tif `cat /proc/net/tcp | ".
	"grep -E -e '[0-9A-Z]{8}:0268 ' >/dev/null`; then\n";
    $cmd .= "\t\techo 'gated GII port in use, sleeping...';\n";
    $cmd .= "\t\tsleep 10;\n";
    $cmd .= "\telse\n";
    $cmd .= "\t\tbreak;\n";
    $cmd .= "\tfi\n";
    $cmd .= "    done\n";
    $cmd .= "    $GATED -f $conffile";
448
449
450
    return $cmd;
}

451
sub os_routing_add_manual($$$$$;$)
452
{
453
    my ($routetype, $destip, $destmask, $gate, $cost, $rtabid) = @_;
454
455
456
457
458
459
460
461
462
463
464
465
466
467
    my $cmd;

    if ($routetype eq "host") {
	$cmd = "$ROUTE add -host $destip gw $gate";
    } elsif ($routetype eq "net") {
	$cmd = "$ROUTE add -net $destip netmask $destmask gw $gate";
    } else {
	warn "*** WARNING: bad routing entry type: $routetype\n";
	$cmd = "";
    }

    return $cmd;
}

468
sub os_routing_del_manual($$$$$;$)
469
{
470
    my ($routetype, $destip, $destmask, $gate, $cost, $rtabid) = @_;
471
472
473
474
475
476
477
478
479
480
481
482
483
484
    my $cmd;

    if ($routetype eq "host") {
	$cmd = "$ROUTE delete -host $destip";
    } elsif ($routetype eq "net") {
	$cmd = "$ROUTE delete -net $destip netmask $destmask gw $gate";
    } else {
	warn "*** WARNING: bad routing entry type: $routetype\n";
	$cmd = "";
    }

    return $cmd;
}

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
# Map a shell name to a full path using /etc/shells
sub MapShell($)
{
   my ($shell) = @_;

   if ($shell eq "") {
       return $DEFSHELL;
   }

   my $fullpath = `grep '/${shell}\$' $SHELLS`;

   if ($?) {
       return $DEFSHELL;
   }

   # Sanity Check
   if ($fullpath =~ /^([-\w\/]*)$/) {
       $fullpath = $1;
   }
   else {
       $fullpath = $DEFSHELL;
   }
   return $fullpath;
}

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
sub os_getnfsmounts($)
{
    my ($rptr) = @_;
    my %mounted = ();

    #
    # Grab the output of the mount command and parse. 
    #
    if (! open(MOUNT, "/bin/mount|")) {
	print "os_getnfsmounts: Cannot run mount command\n";
	return -1;
    }
    while (<MOUNT>) {
	if ($_ =~ /^([-\w\.\/:\(\)]+) on ([-\w\.\/]+) type (\w+) .*$/) {
	    # Check type for nfs string.
	    if ($3 eq "nfs") {
		$mounted{$1} = $2;
	    }
	}
    }
    close(MOUNT);
    %$rptr = %mounted;
    return 0;
}

535
1;