console_setup.in 1.99 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/perl -wT
use English;

#
# usage: console_setup node [node node ...]
#
7 8 9 10 11 12
sub usage()
{
    print STDOUT "Usage: console_setup node [node ...]\n";
    exit(-1);
}
my  $optlist = "";
13 14 15 16 17

#
# Configure variables
#
my $TB		= "@prefix@";
18 19 20 21 22 23 24 25 26

#
# Load the Testbed support stuff. 
#
push(@INC, "$TB/lib");
require libdb;

# Turn off line buffering on output
$| = 1; 
27

28
my $SSH		= "sshtb -n -q -l root plastic.emulab.net";
29
my $PROG	= "$TB/sbin/console_setup.proxy";
30 31 32 33 34 35 36 37 38
my $TBPID	= "flux";
my $cmdargs     = "";
my @row;

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
39 40 41 42 43 44 45 46 47
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV == 0) {
    usage();
48
}
49
my @nodes = @ARGV;
50 51 52 53 54 55

#
# Script must be setuid root. We don't worry about who called us or what
# nodes are specified since this script always does the right thing.
#
if ($EUID != 0) {
56
    die("Must be root! Maybe its a development version?");
57 58 59 60 61 62 63
}

#
# Build of a list of nodes/pid pairs and then send the command over to
# plastic.
# 
foreach my $node (@nodes) {
64 65
    my($db_result);
    
66 67 68 69 70 71
    #
    # Untaint the argument. 
    #
    if ($node =~ /^([-\@\w.]+)$/) {
	$node = $1;
    }
72 73 74
    else {
	die("Tainted node name: $node");
    }
75 76 77

    #
    # Need the project for the node since that is the group.
78 79 80 81 82 83
    #
    # HACK! If its a shark shelf, then need a wildcard query so we can
    # the nodes.
    #
    if ($node =~ /sh\d+/) {
	$db_result =
84 85
	    DBQueryFatal("select pid from reserved ".
			 "where node_id like '$node%'");
86 87 88
    }
    else {
	$db_result =
89
	    DBQueryFatal("select pid from reserved where node_id='$node'");
90 91
    }
    if ($db_result->numrows > 0) {
92 93 94 95 96 97
	@row = $db_result->fetchrow_array();
	$pid = $row[0];
    }
    else {
	$pid = $TBPID;
    }
98
	
99 100 101 102 103 104 105 106
    $cmdargs = "$cmdargs $node $pid";
}

$UID = 0;
system("$SSH $PROG $cmdargs") == 0 or
    die("Failed: $SSH $PROG $cmdargs: $?");

exit 0;