spewconlog.php3 3.25 KB
Newer Older
1 2
<?php
#
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
#
include("defs.php3");
25
include_once("node_defs.php");
26 27 28 29 30 31

#
# No PAGEHEADER since we spit out a Location header later. See below.
# 

#
32
# Verify page arguments.
33
#
34
$reqargs = RequiredPageArguments("node",      PAGEARG_NODE);
35 36
$optargs = OptionalPageArguments("linecount", PAGEARG_INTEGER,
                                 "key",       PAGEARG_STRING);
37
$node_id = $node->node_id();
38

39 40 41 42
if (isset($key)) {
    $safe_key = addslashes($key);
    
    $query_result =
43
	DBQueryFatal("select urlstamp, reuseurl from tiplines ".
44 45 46 47 48 49 50 51 52
		     "where node_id='$node_id' and urlhash='$safe_key' and ".
		     "      urlstamp!=0");
    
    if (mysql_num_rows($query_result) == 0) {
	USERERROR("Invalid node or invalid key", 1);
    } else {
	$row = mysql_fetch_array($query_result);
	$stamp = $row['urlstamp'];
	if ($stamp <= time()) {
53 54
	    DBQueryFatal("update tiplines set urlhash=NULL,urlstamp=0,".
			 "reuseurl=0 ".
55 56 57 58 59 60 61
	    		 "where node_id='$node_id'");
	    USERERROR("Key is no longer valid", 1);
	}
    }
    $uid     = "nobody";
    $isadmin = 0;
    $optarg  = "-k " . escapeshellarg($key);
62 63 64 65
    $reuse = $row['reuseurl'];
    if ($reuse == 1) {
      $optarg .= " -r";
    }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
}
else {
    #
    # Only known and logged in users can do this.
    #
    $this_user = CheckLoginOrDie();
    $uid       = $this_user->uid();
    $isadmin   = ISADMIN();
    $optarg    = "";
    
    if (!$isadmin &&
        !$node->AccessCheck($this_user, $TB_NODEACCESS_READINFO)) {
        USERERROR("You do not have permission to view the console log ".
                  "for $node_id!", 1);
    }
81 82 83 84 85 86 87 88 89
}

#
# Look for linecount argument
#
if (isset($linecount) && $linecount != "") {
    if (! TBvalid_integer($linecount)) {
	PAGEARGERROR("Illegal characters in linecount!");
    }
90
    $optarg .= " -l $linecount";
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
}

#
# A cleanup function to keep the child from becoming a zombie.
#
$fp = 0;

function SPEWCLEANUP()
{
    global $fp;

    if (connection_aborted() && $fp) {
	pclose($fp);
    }
    exit();
}
register_shutdown_function("SPEWCLEANUP");

$fp = popen("$TBSUEXEC_PATH $uid nobody webspewconlog $optarg $node_id", "r");
if (! $fp) {
    USERERROR("Spew console log failed!", 1);
}

114 115 116 117 118
header("Content-Type: text/plain; charset=us-ascii");
header("X-Content-Type-Options: nosniff");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
119 120 121 122 123 124 125 126 127 128
flush();
while (!feof($fp)) {
    $string = fgets($fp, 1024);
    echo "$string";
    flush();
}
pclose($fp);
$fp = 0;

?>