#!/usr/bin/perl -w
#
# Copyright (c) 2005-2018 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
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()
{
    print(STDOUT "Usage: console [-d] pcXXX\n");
    exit(-1);
}
my $optlist  = "dp:A:";
my @opts = ();

#
# Configure variables
#
my $TB		= "/usr";
my $WRAPPER     = "$TB/bin/script_wrapper.py";
my $CONSOLEBIN  = "$TB/bin/console.bin";
my $aclfile;
my $uaclfile;

# 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();
}

# pass through select options
if (defined($options{"d"})) {
    push @opts, "-d";
}
if (defined($options{"p"})) {
    push @opts, "-p";
    push @opts, $options{"p"};
}
if (defined($options{"A"})) {
    if ($options{"A"} =~ /^(\S+)$/) {
	$uaclfile = $1;
    }
}

usage()
    if (@ARGV != 1);
my $node = $ARGV[0];

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

my $tempfile = `mktemp -t tipacl.XXXXXX`;
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");
}

if ($uaclfile) {
    print STDERR "Using ACl from $uaclfile...\n";
    if (system("cat $uaclfile >> $tempfile")) {
	unlink($tempfile);
	die("*** $0:\n".
	    "    Could not access acl file '$uaclfile'\n");
    }
}

#
# Ask the XMLRPC server for the tipacl. The current user has to have proper
# permission of course.
#
else {
    if (system("$WRAPPER tipacl $node >> $tempfile")) {
	unlink($tempfile);
	die("*** $0:\n".
	    "    $WRAPPER failed\n");
    }
}

#
# 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);
}
my @cmdargs = ($CONSOLEBIN, "-a", "$tempfile", @opts, "$node");
exec(@cmdargs);
die("*** $0:\n".
    "    Exec failure: '@cmdargs'\n");
