console.in 2.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;
use POSIX qw(setsid);

#
# Wrapper for console program; grab tipacl from XMLRPC server, and feed it
# to the console binary.
#
sub usage()
{
17
    print(STDOUT "Usage: console [-d] pcXXX\n");
18 19
    exit(-1);
}
20
my $optlist  = "dp:";
21
my @opts = ();
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $WRAPPER     = "$TB/bin/script_wrapper.py";
my $CONSOLEBIN  = "$TB/bin/console.bin";
my $aclfile;

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

#
# Turn off line buffering on output. Very important for this script!
#
$| = 1; 

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
49 50 51 52 53 54 55 56 57 58

# pass through select options
if (defined($options{"d"})) {
    push @opts, "-d";
}
if (defined($options{"p"})) {
    push @opts, "-p";
    push @opts, $options{"p"};
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
usage()
    if (@ARGV != 1);
my $node = $ARGV[0];

#
# Make a temp file for the acl.
#
$ENV{'TMPDIR'} = "/tmp";

my $tempfile = `mktemp -t tipacl`;
if ($?) {
    die("*** $0:\n".
	"    Could not create a temporary file!\n");
}
if ($tempfile =~ /^([-\w\/\.]*)$/) {
    $tempfile = $1;
}
else {
    die("*** $0:\n".
	"    Bad data in tag: $tempfile\n");
}

#
# Ask the XMLRPC server for the tipacl. The current user has to have proper
# permission of course.
#
85 86 87 88 89
if (system("$WRAPPER tipacl $node >> $tempfile")) {
    unlink($tempfile);
    die("*** $0:\n".
	"    $WRAPPER failed\n");
}
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

#
# Do not want to leave the acl file around, and do not want to wait for
# the user to quit the program, so fork a child to wait a moment and remove
# the file. We have the child do it so as to avoid messing with the session
# and tty goo.
#
my $syspid = fork();

# Child delays a moment and exits. 
if (!$syspid) {
    sleep(1);
    unlink($tempfile);
    exit(0);
}
105
my @cmdargs = ($CONSOLEBIN, "-a", "$tempfile", @opts, "$node");
106 107 108
exec(@cmdargs);
die("*** $0:\n".
    "    Exec failure: '@cmdargs'\n");