defs.php3.in 11.9 KB
Newer Older
1 2
<?php
#
3
# Standard definitions! 
4
#
5
$TBDIR          = "@prefix@/";
6
$OURDOMAIN      = "@OURDOMAIN@";
7
$BOSSNODE       = "@BOSSNODE@";
8
$USERNODE       = "@USERNODE@";
9
$TBADMINGROUP   = "@TBADMINGROUP@";
10 11 12 13 14 15 16
$WWWHOST	= "@WWWHOST@";
$WWW		= "@WWW@";
$TBAUTHDOMAIN	= "@TBAUTHDOMAIN@";
$TBBASE		= "@TBBASE@";
$TBDOCBASE	= "@TBDOCBASE@";
$TBWWW		= "@TBWWW@";
$THISHOMEBASE	= "@THISHOMEBASE@";
17

18 19 20 21 22 23
$TBMAILADDR_OPS		= "@TBOPSEMAIL_NOSLASH@";
$TBMAILADDR_WWW		= "@TBWWWEMAIL_NOSLASH@";
$TBMAILADDR_APPROVAL	= "@TBAPPROVALEMAIL_NOSLASH@";
$TBMAILADDR_LOGS	= "@TBLOGSEMAIL_NOSLASH@";
$TBMAILADDR_AUDIT	= "@TBAUDITEMAIL_NOSLASH@";

24 25 26 27 28
# Can override this in the defs file. 
$TBAUTHTIMEOUT  = "@TBAUTHTIMEOUT@";
$TBMAINSITE     = "@TBMAINSITE@";
$TBSECURECOOKIES= "@TBSECURECOOKIES@";
$TBCOOKIESUFFIX = "@TBCOOKIESUFFIX@";
29

Leigh B. Stoller's avatar
Leigh B. Stoller committed
30 31
$TBWWW_DIR	= "$TBDIR"."www/";
$TBBIN_DIR	= "$TBDIR"."bin/";
32
$TBETC_DIR	= "$TBDIR"."etc/";
33 34 35
$TBLIBEXEC_DIR	= "$TBDIR"."libexec/";
$TBSUEXEC_PATH  = "$TBLIBEXEC_DIR/suexec";
$TBCHKPASS_PATH = "$TBLIBEXEC_DIR/checkpass";
36
$TBCSLOGINS     = "$TBETC_DIR/cslogins";
37

38 39
#
# Hardcoded check against $WWWHOST, to prevent anyone from accidentally setting
40
# $TBMAINSITE when it should not be
41 42 43 44 45
#
if ($WWWHOST != "www.emulab.net") {
    $TBMAINSITE = 0;
}

46
$TBPROJ_DIR     = "/proj";
47
$TBUSER_DIR	= "/users";
48
$TBGROUP_DIR	= "/groups";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
49
$TBNSSUBDIR     = "nsdir";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50

51 52
$TBAUTHCOOKIE   = "HashCookie" . $TBCOOKIESUFFIX;
$TBNAMECOOKIE   = "MyUidCookie" . $TBCOOKIESUFFIX;
53
$TBLOGINCOOKIE  = "LoginCookie" . $TBCOOKIESUFFIX;
54

55 56
$HTTPTAG        = "http://";

57 58 59 60 61 62
$TBMAIL_OPS		= "Testbed Ops <$TBMAILADDR_OPS>";
$TBMAIL_WWW		= "Testbed WWW <$TBMAILADDR_WWW>";
$TBMAIL_APPROVAL	= "Testbed Approval <$TBMAILADDR_APPROVAL>";
$TBMAIL_LOGS		= "Testbed Logs <$TBMAILADDR_LOGS>";
$TBMAIL_AUDIT		= "Testbed Audit <$TBMAILADDR_AUDIT>";

63
#
64 65 66
# This just spits out an email address in a page, so it does not need
# to be configured per development tree. It could be though ...
# 
67 68
$TBMAILADDR     = "<a href=\"mailto:$TBMAILADDR_OPS\">
                      Testbed Operations ($TBMAILADDR_OPS)</a>";
69

70 71 72
# So subscripts always know ...
putenv("HTTP_SCRIPT=1");

73 74 75 76 77
#
# Database constants and the like.
#
include("dbdefs.php3");

78 79 80 81 82 83 84 85
#
# Control how error messages are returned to the user. If the session is
# not actually "interactive" then do not send any output to the browser.
# Just save it up and let the page deal with it. 
#
$session_interactive  = 1;
$session_errorhandler = 0;

86 87 88 89 90 91 92 93 94
#
# Wrap up the mail function so we can prepend a tag to the subject
# line that indicates what testbed. Useful when multiple testbed
# email to the same list.
#
# 
function TBMAIL($to, $subject, $message, $headers = 0)
{
    global $THISHOMEBASE;
95
    global $SCRIPT_NAME;
96 97 98

    $subject = strtoupper($THISHOMEBASE) . ": $subject";

99 100 101 102 103 104 105 106 107
    $tag = "X-NetBed: " . basename($SCRIPT_NAME);
    
    if ($headers) {
	$headers = "$headers\n" . $tag;
    }
    else {
	$headers = $tag;
    }
    return mail($to, $subject, $message, $headers);
108 109
}

110 111 112 113 114
#
# Internal errors should be reported back to the user simply. The actual 
# error information should be emailed to the list for action. The script
# should then terminate if required to do so.
#
115
function TBERROR ($message, $death, $xmp = 0) {
116 117 118
    global $TBMAIL_WWW, $TBMAIL_OPS, $TBMAILADDR, $TBMAILADDR_OPS;
    global $session_interactive, $session_errorhandler;
    $script = urldecode($_SERVER['REQUEST_URI']);
119

120 121
    TBMAIL($TBMAIL_OPS,
         "WEB ERROR REPORT",
122
         "\n".
123
	 "In $script\n\n".
124 125 126
         "$message\n\n".
         "Thanks,\n".
         "Testbed WWW\n",
127
         "From: $TBMAIL_OPS\n".
128
         "Errors-To: $TBMAIL_WWW");
129

130
    if ($death) {
131 132 133 134 135 136 137
	if ($session_interactive)
	    PAGEERROR("Could not continue. Please contact $TBMAILADDR");
	elseif ($session_errorhandler) {
	    $session_errorhandler("Could not continue. ".
				  "Please contact $TBMAILADDR_OPS", $death);
	}
	exit(1);
138 139 140
    }
    return 0;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
141 142 143 144 145

#
# General user errors should print something warm and fuzzy
#
function USERERROR($message, $death) {
146
    global $TBMAILADDR;
147 148 149 150 151 152 153 154 155 156 157 158
    global $session_interactive, $session_errorhandler;

    if (! $session_interactive) {
	if ($session_errorhandler)
	    $session_errorhandler($message, $death);
	else
	    echo "$message";

	if ($death)
	    exit(1);
	return;
    }
159

160
    $msg = "<font size=+1><br>
161
            $message
162
      	    </font>
163
            <br><br><br>
164 165 166
            <font size=-1>
            Please contact $TBMAILADDR if you feel this message is an error.
            </font>\n";
167

Leigh B. Stoller's avatar
Leigh B. Stoller committed
168
    if ($death) {
169
	PAGEERROR($msg);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
170
    }
171 172
    else
        echo "$msg\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
173 174
}

175 176 177 178 179 180 181 182
#
# A form error.
#
function FORMERROR($field) {
    USERERROR("Missing field; ".
              "Please go back and fill out the \"$field\" field!", 1);
}

183 184 185
#
# A page argument error. 
# 
186 187 188 189 190 191 192
function PAGEARGERROR($msg = 0) {
    $default = "Invalid page arguments: " . $_SERVER['REQUEST_URI'];

    if ($msg) {
	$default = "$default<br><br>$msg";
    }
    USERERROR($default, 1);
193 194
}

195
#
196
# SUEXEC stuff.
197
#
198 199 200 201
# Save this stuff so we can generate better error messages and such.
# 
$suexec_cmdandargs = "";
$suexec_retval     = 0;
202 203
$suexec_output     = "";
$suexec_output_array;
204

205 206 207 208 209 210 211
#
# Actions for suexec. 
#
define("SUEXEC_ACTION_CONTINUE",	0);
define("SUEXEC_ACTION_DIE",		1);
define("SUEXEC_ACTION_USERERROR",	2);
define("SUEXEC_ACTION_IGNORE",		3);
212

213 214 215 216 217
#
# An suexec error.
#
function SUEXECERROR($action)
{
218 219
    global $suexec_cmdandargs, $suexec_retval;
    global $suexec_output, $suexec_output_array;
220

221 222
    $foo  = "Shell Program Error. Exit status: $suexec_retval\n";
    $foo .= "  '$suexec_cmdandargs'\n";
223 224 225
    $foo .= "\n";
    $foo .= $suexec_output;

226 227
    switch ($action) {
    case SUEXEC_ACTION_CONTINUE:
228
	TBERROR($foo, 0, 1);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        break;
    case SUEXEC_ACTION_DIE:
	TBERROR($foo, 1, 1);
        break;
    case SUEXEC_ACTION_USERERROR:
	USERERROR("<XMP>$foo</XMP>", 1);
        break;
    case SUEXEC_ACTION_IGNORE:
	break;
    default:
	TBERROR($foo, 1, 1);
    }
}

#
# Run a program as a user.
#
function SUEXEC($uid, $gid, $cmdandargs, $action) {
    global $TBSUEXEC_PATH;
248 249
    global $suexec_cmdandargs, $suexec_retval;
    global $suexec_output, $suexec_output_array;
250 251 252

    ignore_user_abort(1);

253 254 255 256
    $suexec_cmdandargs   = "$uid $gid $cmdandargs";
    $suexec_output_array = array();
    $suexec_output       = "";
    $suexec_retval       = 0;
257
    
258 259 260 261 262 263 264 265 266 267 268 269 270
    exec("$TBSUEXEC_PATH $suexec_cmdandargs",
	 $suexec_output_array, $suexec_retval);

    # Yikes! Something is not doing integer conversion properly!
    if ($suexec_retval == 255) {
	$suexec_retval = -1;
    }

    if (count($suexec_output_array)) {
	for ($i = 0; $i < count($suexec_output_array); $i++) {
	    $suexec_output .= "$suexec_output_array[$i]\n";
	}
    }
271 272 273 274 275 276 277 278

    #
    # The output is still available of course, via $suexec_output.
    # 
    if ($suexec_retval == 0 || $action == SUEXEC_ACTION_IGNORE) {
	return $suexec_retval;
    }
    SUEXECERROR($action);
279 280
    # Must return the shell value!
    return $suexec_retval;
281 282
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
#
# Invoke the XMLRPC backend. We invoke the xmlrpc server, writing the request
# to its stdin. We then wait for the reply to came back on its stdout. We
# decode that reply, and return an error status and output to the caller.
# Note that we run the backend as the uid/gid, much like we do for plain
# scripts. This might change in the future. 
#
function XMLRPC($uid, $gid, $method, $arghash)
{
    global $TBSUEXEC_PATH;

    $xmlcode = xmlrpc_encode_request($method, array(0.1, $arghash));

    $descriptorspec = array(0 => array("pipe", "r"),
			    1 => array("pipe", "w"));

    $process = proc_open("$TBSUEXEC_PATH $uid $gid webxmlrpc",
			 $descriptorspec, $pipes);

    if (! is_resource($process)) {
	TBERROR("Could not invoke XMLRPC backend!\n".
		"$uid $gid $method\n".
		print_r($arghash, true), 1);
    }
    # $pipes now looks like this:
    # 0 => writeable handle connected to child stdin
    # 1 => readable handle connected to child stdout

    #
    # Write the request to the process, and then close the pipe so that
    # the other side sees the EOF. The sshxmlrpc protocol looks sorta
    # like a POST request, so we have to do that.
    #
    fwrite($pipes[0], "content-length: " . strlen($xmlcode) . "\r\n");
    fwrite($pipes[0], "\r\n");
    fwrite($pipes[0], "$xmlcode");
    fflush($pipes[0]);
    fclose($pipes[0]);

    #
    # Now read back the results into a string. We then convert the string
    # back into a PHP datatype. This datatype is defined outside this code
    # though. 
    #
    # Skip the headers, but we need the content-length.
    #
    $content_length = 0;
    while (!feof($pipes[1]) && ($foo = trim(fgets($pipes[1], 4096)))) {
        if (preg_match("/^content-length: (\d*)/", $foo, $matches)) {
	    $content_length = $matches[1];
	}
    }

    $output = "";
    while(!feof($pipes[1])) {
	$output .= fgets($pipes[1], 1024);
    }
    fclose($pipes[1]);

    # It is important that you close any pipes before calling
    # proc_close in order to avoid a deadlock.
    $return_value = proc_close($process);

    if (!$return_value && $content_length) {
	$decoded = xmlrpc_decode_request(substr($output, 0, $content_length),
					 $meth);
    }

    # On command error or on a "Fault" reply, send email and terminate the
    # script; something went really wrong. 
    if ($return_value || !$content_length ||
	(count($decoded) == 2 &&
	 array_key_exists("faultCode", $decoded) &&
	 array_key_exists("faultString", $decoded)) ||
	!(array_key_exists("code", $decoded) &&
	  array_key_exists("value", $decoded) &&
	  array_key_exists("output", $decoded))) {
	TBERROR("XMLRPC backend failure!\n".
		"$uid $gid $method returned $return_value\n".
		"Arg Hash:\n" .
		print_r($arghash, true) . "\n\n" .
		"XML:\n" .
		"$xmlcode\n\n" .
		"Output:\n" .
		"$output\n", 1);
    }
    return $decoded;
}

372 373 374
function ADDPUBKEY($uid, $cmdandargs) {
    global $TBSUEXEC_PATH;

375 376 377 378 379 380 381 382 383 384
    #
    # Complication. User might not have an actual account if setting or
    # changing his own pubkeys. webonly, unapproved, and unverified users
    # can still muck with their personal info. So, just invoke as user
    # nobody. We will get audit email in case we need to track what has
    # happened. 
    #
    if (! HASREALACCOUNT($uid)) {
	$uid = "nobody";
    }
385
    return SUEXEC($uid, "nobody", $cmdandargs, 0);
386 387
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
#
# Verify a URL.
#
function CHECKURL($url, &$error) {
    global $HTTPTAG;

    if (strlen($url)) {
	if (strstr($url, " ")) {
	    $error = "URL is malformed; spaces are not allowed!";
	    return 0;
	}
	
	if (strcmp($HTTPTAG, substr($url, 0, strlen($HTTPTAG)))) {
	    $error = "URL is malformed; must begin with $HTTPTAG!";
	    return 0;
	}
404

405 406
	$fp = @fopen($url, "r");
	if (! $fp) {
407
	    # Check to see if it was a redirect, in which case its OK
408 409 410 411 412 413 414 415 416 417 418
	    for ($i = 0; $i < count($http_response_header); $i++) {
		if (!strcmp("Location:", substr($http_response_header[$i],0,9))) {
		    $is_redirect = 1;
		}
	    }
	    if (!$is_redirect) {
		$error = "URL is not valid; Cannot be accessed!";
		return 0;
	    }
	} else {
	    fclose($fp);
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	}
    }
    return 1;
}

#
# Check a password.
#
function CHECKPASSWORD($uid, $password, $name, $email, &$error)
{
    global $TBCHKPASS_PATH;
    
    $mypipe =
	popen(escapeshellcmd("$TBCHKPASS_PATH $password $uid '$name:$email'"),
	      "w+");
    
    if ($mypipe) { 
        $retval=fgets($mypipe, 1024);
        if (strcmp($retval,"ok\n") != 0) {
	    $error = "$retval";
	    return 0;
	}
	return 1;
    }
    TBERROR("Checkpass Failure! Returned '$mypipe'.\n\n".
	    "$TBCHKPASS_PATH $password $uid '$name:$email'", 1);
}

447 448 449 450
function LASTNODELOGIN($node)
{
}

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
#
# A function to print the contents of an array (recursively).
# Mostly useful for debugging.
#
function ARRAY_PRINT($arr) {
  if (!is_array($arr)) { echo "non-array '$arr'\n"; }
  foreach ($arr as $i => $val) {
    echo("'$i' - '$val'\n");
    if (is_array($val)) {
      echo "Sub-array $i:\n";
      array_print($val);
      echo "End Sub-array $i.\n";
    }
  }
}

467 468 469 470
#
# Beware empty spaces (cookies)!
# 
require("tbauth.php3");
471 472 473 474 475

#
# Okay, this is what checks the login and spits out the menu.
#
require("menu.php3");
476
?>