spewconlog.in 4.68 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2005-2016 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27 28 29 30 31
#
use English;
use Getopt::Std;

#
# Spew a console log from the tipserver that hosts it.
#
sub usage()
{
32
    print(STDOUT "Usage: spewconlog [-l linecount] [-k key [-r]] node\n");
33 34
    exit(-1);
}
35
my $optlist   = "l:k:r";
36
my $linecount = 0;
37
my $key;
38
my $reuseurl  = 0;
39
my $logdir    = "/var/log/tiplogs";
40
my $logname;
41
my $cmd;
42 43 44 45 46 47

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
48
my $ELABINELAB  = @ELABINELAB@;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
my $SSH		= "$TB/bin/sshtb";
my $SAVEUID	= $UID;

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

#
# We don't want to run this script unless its the real version.
#
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be setuid! Maybe its a development version?\n");
}

#
# This script is setuid, so please do not run it as root. Hard to track
# what has happened.
#
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root! Its already setuid!\n");
}

73 74 75 76
#
# Not yet ... need a xmlrpc hook.
#
if ($ELABINELAB) {
77 78 79
    print STDERR "*** $0:\n".
	"    Not supported in an inner Emulab, yet!\n";
    exit(0);
80 81
}

82 83 84 85 86 87 88 89 90
#
# Turn off line buffering on output. Very important for this script!
#
$| = 1; 

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libdb;
use libtestbed;
91
use Node;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

# Be careful not to exit on transient error
$libdb::DBQUERY_MAXTRIES = 30;

#
# Locals
# 
my $logfile;

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
109 110 111
if (defined($options{"k"})) {
    $key = $options{"k"};
}
112 113 114 115 116 117 118 119 120 121 122 123
if (defined($options{"l"})) {
    $linecount = $options{"l"};

    # Untaint of course.
    if ($linecount =~ /^([\d]+)$/) {
	$linecount = $1;
    }
    else {
	die("*** $0:\n".
	    "    Bad data in linecount: $linecount\n");
    }
}
124 125 126
if (defined($options{"r"})) {
    $reuseurl = 1;
}
127 128
usage()
    if (@ARGV != 1);
129
my $node_id = $ARGV[0];
130 131

# Untaint
132 133
if ($node_id =~ /^([-\w]+)$/) {
    $node_id = $1;
134 135
}
else {
136
    die("*** Tainted node name: $node_id\n");
137
}
138 139
my $node = Node->Lookup($node_id);
if (!defined($node)) {
140
    die("*** $0:\n".
141
	"    No such node $node_id!\n");
142 143 144 145 146 147
}

#
# Grab DB data. 
#
my $query_result =
148
    DBQueryFatal("select server from tiplines where node_id='$node_id'");
149 150 151

if (!$query_result->numrows) {
    die("*** $0:\n".
152
	"    No console log (tipline) available for $node_id!\n");
153 154 155
}
my ($tipserver) = $query_result->fetchrow_array();

156 157 158 159 160 161 162 163
#
# Check permission. Called as nobody if there is a key.
#
if (defined($key)) {
    if ($node->CheckTipAcl($key)) {
	die("*** $0:\n".
	    "    You not have permission to view console log for $node_id!\n");
    }
164 165 166
    if (!$reuseurl) {
	$node->ClrTipAclUrl();
    }
167 168 169 170 171 172 173
}
elsif (!TBAdmin($UID) &&
       (! TBNodeAccessCheck($UID, TB_NODEACCESS_READINFO, ($node_id)))) {
    die("*** $0:\n".
	"    You not have permission to view console log for $node_id!\n");
}

174 175 176
#
# Form the logfile name; admin users get the .log while users get .run.
#
177 178 179 180 181
if ($node->isvirtnode()) {
    $logname = "/var/emulab/vms/vminfo/$node_id/${node_id}.log";
}
elsif (defined($key) || !TBAdmin($UID)) {
    $logname = "$logdir/${node_id}.run";
182 183
}
else {
184
    $logname = "$logdir/${node_id}.log";
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
}

#
# Run an ssh command in a child process, protected by an alarm to
# ensure that the ssh is not hung up forever if the machine is in
# some funky state.
#
my $syspid = fork();

#
# Parent waits for ssh to complete.
# 
if ($syspid) {
    local $SIG{ALRM} = sub { kill("TERM", $syspid); };
    alarm 20;
    waitpid($syspid, 0);
    my $exitstatus = $?;
    alarm 0;

    if ($exitstatus) {
	exit(1);
    }
    exit(0);
}

#
# Now we want to ssh over and cat the file.
#
213 214 215 216 217 218 219
if ($linecount) {
    $cmd = "tail -${linecount} $logname | cat -v";
}
else {
    $cmd = "cat -v $logname";
}

220
$UID = 0;
221
system("$SSH -host $tipserver $cmd");
222 223
$UID = $SAVEUID;
exit(($? == 0 ? 0 : 1));