cdromcheckin.php3 13.5 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2013 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
#
require("defs.php3");
25
include_once("node_defs.php");
26 27 28 29 30 31 32 33 34 35

# These error codes must match whats in register.pl on the cd.
define("CDROMSTATUS_OKAY"	,	0);
define("CDROMSTATUS_MISSINGARGS",	100);
define("CDROMSTATUS_INVALIDARGS",	101);
define("CDROMSTATUS_BADCDKEY",		102);
define("CDROMSTATUS_BADPRIVKEY",	103);
define("CDROMSTATUS_BADIPADDR",		104);
define("CDROMSTATUS_BADREMOTEIP",	105);
define("CDROMSTATUS_IPADDRINUSE",	106);
36
define("CDROMSTATUS_UPGRADEERROR",	107);
37 38
define("CDROMSTATUS_OTHER",		199);

39
#
40
# Config setup.  This should not be hard-coded in the long term.
41 42 43 44 45 46

$slicexbase = "slicex_slice=3\nslicex_mount=/users\n";
$confslicex = "slicex_tarball=slicex.tar.gz
slicex_sig=http://${WWWHOST}/images/slicex-v4.tar.gz.sig
";

David G Andersen's avatar
David G Andersen committed
47
$version_tags[5] = "24a4f173a7716d47bbc047f8387c86af";
48 49 50 51 52 53 54 55
$confmaps["24a4f173a7716d47bbc047f8387c86af"] = "fdisk=image.fdisk
fdisk_sig=https://${WWWHOST}/images/image.fdisk.sig
slice1_image=slice1.ndz
slice1_alt_image=http://${WWWHOST}/images/slice1-v5.ndz
slice1_sig=https://${WWWHOST}/images/slice1-v5.ndz.sig
slice1_md5=24a4f173a7716d47bbc047f8387c86af
" . $slicexbase;

David G Andersen's avatar
David G Andersen committed
56
$version_tags[4] = "c9f8578517f5ebb0eca70b69dce144d";
57 58 59 60 61 62 63 64 65 66 67 68
$confmaps["c9f8578517f5ebb0eca70b69dce144d"] = "fdisk=image.fdisk
fdisk_sig=https://${WWWHOST}/images/image.fdisk.sig
slice1_image=slice1.ndz
slice1_alt_image=http://${WWWHOST}/images/slice1-v4.ndz
slice1_sig=https://${WWWHOST}/images/slice1-v4.ndz.sig
slice1_md5=c9f8578517f5ebb0eca70b69dce144db
" . $slicexbase;

$confmaps["d326a1f604489c43b488fa80a88221f4"] = "slice1_image=slice1.ndz
slice1_sig=slice1.ndz.sig
slice1_md5=d326a1f604489c43b488fa80a88221f4\n" . $slicexbase;

69 70 71 72 73 74 75 76 77
#
# Spit back a text message we can display to the user on the console
# of the node running the checkin. We could return an http error, but
# that would be of no help to the user on the other side.
# 
function SPITSTATUS($status)
{
    header("Content-Type: text/plain");
    echo "emulab_status=$status\n";
Leigh Stoller's avatar
Leigh Stoller committed
78

79
    if ($status) {
80 81
	    $REQUEST_URI = $_SERVER["REQUEST_URI"];
	    $REMOTE_ADDR = $_SERVER["REMOTE_ADDR"];
82 83 84
	    TBERROR("CDROM Checkin Error ($status) from $REMOTE_ADDR:\n\n".
		    "$REQUEST_URI\n", 0);
    }
85 86
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
# Shuffle stuff out of REQUEST to avoid a bunch of silly changes.
if (isset($_REQUEST["cdkey"])) {
    $cdkey = $_REQUEST["cdkey"];
}
if (isset($_REQUEST["needscript"])) {
    $needscript = $_REQUEST["needscript"];
}
if (isset($_REQUEST["privkey"])) {
    $privkey = $_REQUEST["privkey"];
}
if (isset($_REQUEST["IP"])) {
    $IP = $_REQUEST["IP"];
}
if (isset($_REQUEST["wahostname"])) {
    $wahostname = $_REQUEST["wahostname"];
}
if (isset($_REQUEST["roottag"])) {
    $roottag = $_REQUEST["roottag"];
}
if (isset($_REQUEST["updated"])) {
    $updated = $_REQUEST["updated"];
}

110 111 112
#
# This page is not intended to be invoked by humans!
#
Leigh Stoller's avatar
Leigh Stoller committed
113
if ((!isset($cdkey) || !strcmp($cdkey, ""))) {
114 115 116 117
    SPITSTATUS(CDROMSTATUS_MISSINGARGS);
    return;
}

Mike Hibler's avatar
Mike Hibler committed
118
if (!preg_match('/[0-9a-zA-Z]+/', $cdkey)) {
119 120 121 122
    SPITSTATUS(CDROMSTATUS_INVALIDARGS);
    return;
}

123 124 125 126 127 128 129 130 131
#
# Make sure this is a valid CDkey.
#
$query_result =
    DBQueryFatal("select * from cdroms where cdkey='$cdkey'");
if (! mysql_num_rows($query_result)) {
    SPITSTATUS(CDROMSTATUS_BADCDKEY);
    return;
}
132
$row    = mysql_fetch_array($query_result);
Leigh Stoller's avatar
Leigh Stoller committed
133
$cdvers = $row["version"];
134

Leigh Stoller's avatar
Leigh Stoller committed
135 136
#
# Node is requesting latest instructions. Give it the script. This could
137
# be on a per CDROM basis, but for now its hardwired.
Leigh Stoller's avatar
Leigh Stoller committed
138 139 140 141 142
# I know, the hardwired paths are bad; move into the cdroms DB table, even
# if duplicated. 
# 
if (isset($needscript)) {
    header("Content-Type: text/plain");
143 144 145 146 147

    if ($cdvers <= 3) {
	echo "MD5=5ed48bdf6def666d5a5d643cbc2ce4d0\n";
	echo "URL=https://${WWWHOST}/images/netbed-setup-v3.pl\n";
    }
148 149 150 151
    elseif ($cdvers == 69) {
	echo "URL=https://${WWWHOST}/images/netbed-setup-v69.pl\n";
	echo "SIG=https://${WWWHOST}/images/netbed-setup-v69.pl.sig\n";
    }
152 153 154 155
    elseif ($cdvers == 4) {
	echo "URL=https://${WWWHOST}/images/netbed-setup-v4.pl\n";
	echo "SIG=https://${WWWHOST}/images/netbed-setup-v4.pl.sig\n";
    }
156 157 158 159
    elseif ($cdvers == 5) {
	echo "URL=https://${WWWHOST}/images/netbed-setup-v5.pl\n";
	echo "SIG=https://${WWWHOST}/images/netbed-setup-v5.pl.sig\n";
    }
160
    else {
161 162
      SPITSTATUS(CDROMSTATUS_INVALIDARGS);
      return;
163
    }
Leigh Stoller's avatar
Leigh Stoller committed
164 165 166 167 168 169 170 171 172 173 174 175 176
    echo "emulab_status=0\n";
    return;
}

#
# Otherwise, move onto config instructions.
#
if ((!isset($privkey) || !strcmp($privkey, "")) ||
    (!isset($IP) || !strcmp($IP, ""))) {
    SPITSTATUS(CDROMSTATUS_MISSINGARGS);
    return;
}

Mike Hibler's avatar
Mike Hibler committed
177 178
if (!preg_match('/[0-9a-zA-Z ]+/', $privkey) ||
    !preg_match('/[0-9\.]+/', $IP)) {
Leigh Stoller's avatar
Leigh Stoller committed
179 180 181 182 183
    SPITSTATUS(CDROMSTATUS_INVALIDARGS);
    return;
}

if (isset($wahostname) &&
Mike Hibler's avatar
Mike Hibler committed
184
    !preg_match('/[-_0-9a-zA-Z\.]+/', $wahostname)) {
Leigh Stoller's avatar
Leigh Stoller committed
185 186 187 188
    SPITSTATUS(CDROMSTATUS_INVALIDARGS);
    return;
}

189 190 191 192 193 194 195 196 197 198 199 200 201
#
# Generate a nickname if given hostname.
#
unset($nickname);
if (isset($wahostname)) {
    if (strpos($wahostname, ".")) {
	$nickname = str_replace(".", "-", $wahostname);
    }
    else {
	$nickname = $wahostname;
    }
}

Leigh Stoller's avatar
Leigh Stoller committed
202
if (isset($roottag) &&
Mike Hibler's avatar
Mike Hibler committed
203
    !preg_match('/[0-9a-zA-Z]+/', $roottag)) {
Leigh Stoller's avatar
Leigh Stoller committed
204 205 206 207
    SPITSTATUS(CDROMSTATUS_INVALIDARGS);
    return;
}

208
#
209
# Grab the privkey record. First squeeze out any spaces.
210
#
211 212
$privkey = str_replace(" ", "", $privkey);

213 214 215 216 217 218 219 220
$query_result =
    DBQueryFatal("select * from widearea_privkeys where privkey='$privkey'");

if (!mysql_num_rows($query_result)) {
    SPITSTATUS(CDROMSTATUS_BADPRIVKEY);
    return;
}
$warow  = mysql_fetch_array($query_result);
Leigh Stoller's avatar
Leigh Stoller committed
221
$privIP = $warow["IP"];
222 223 224 225 226 227 228 229 230 231 232 233 234 235

#
# If the node is reporting that its finished updating, then make its
# current privkey its nextprivkey.
# 
if (isset($updated) && $updated == 1) {
    if (! strcmp($privIP, "1.1.1.1")) {
        #
        # If the node is brand new, then need to create several records.
	# Pass it off to a perl script.
        #
	DBQueryFatal("update widearea_privkeys ".
		     "set IP='$IP' ".
		     "where privkey='$privkey'");
236

237 238 239 240
	$newargs = "-w -i $IP ";
	if (isset($nickname)) {
	    $newargs .= " -n $nickname ";
	}
241

242 243
	$type = "pcwa";
	if (isset($wahostname)) {
244 245 246 247 248
            #
            # XXX Parse hostname to see if a ron node.
	    # Silly, but effective since we will never have anything else
	    # besides wa/ron.
            #
249 250
	    if (strstr($wahostname, ".ron.") ||
		strstr($wahostname, "roncluster")) {
251 252 253
		$type = "pcron";
	    }
	}
254 255
	$newargs .= " -t $type ";
	SUEXEC("nobody", $TBADMINGROUP, "webnewwanode $newargs", 0);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

	#
	# Send email to user reminding to register node.
	#
	$user_name = $warow[user_name];
	$user_email= $warow[user_email];
	$lockkey   = $warow[lockkey];

	TBMAIL("$user_name <$user_email>",
	   "Thanks for installing a NetBed CDROM!",
	   "It would be very helpful if you could please go to:\n".
	   "\n".
	   "    ${TBBASE}/widearea_register.php?cdkey=$lockkey&IP=$IP\n".
	   "\n".
	   "and tell us a few things about the node (processor, connection\n".
	   "type, geographical info, etc.). You can also register for a\n".
	   "local account on your node by providing this info to us.\n".
	   "\n".
	   "Thanks,\n".
	   "Testbed Operations\n",
	   "From: $TBMAIL_OPS\n".
	   "Errors-To: $TBMAIL_WWW");
278
    }
279

280 281
    DBQueryFatal("update widearea_privkeys ".
		 "set privkey=nextprivkey,updated=now(),nextprivkey=NULL ".
282
		 (isset($roottag) ? ",rootkey='$roottag'" : "") .
283 284
		 "where privkey='$privkey'");

285 286 287 288 289 290 291 292
    #
    # Check for image upgrade completed. Delete record. 
    #
    $query_result =
	DBQueryFatal("delete from widearea_updates ".
		     "where IP='$IP' and roottag='$roottag' and " .
		     "      update_started is not null");

293 294 295 296
    #
    # Update with new nickname. Nice if node changes its real hostname.
    #
    if (isset($nickname)) {
297 298 299
	if (($node = Node::LookupByIP($IP))) {
	    $nodeid = $node->node_id();
	    
300 301 302 303 304
	    DBQueryFatal("update reserved set vname='$nickname' ".
			 "where node_id='$nodeid'");
	}
    }

305 306 307 308 309 310 311 312 313 314 315 316 317
    SPITSTATUS(CDROMSTATUS_OKAY);
    return;
}

#
# If the record has a valid IP address and matches the node connecting
# then all we do is return a new private key.
#
if (strcmp($privIP, "1.1.1.1")) {
    if (strcmp($privIP, $IP)) {
	SPITSTATUS(CDROMSTATUS_BADIPADDR);
	return;
    }
318
    if (strcmp($_SERVER["REMOTE_ADDR"], $IP)) {
319 320 321
	SPITSTATUS(CDROMSTATUS_BADREMOTEIP);
	return;
    }
322

323
    $upgrade_instructions = 0;
324 325 326 327 328 329 330 331
    #
    # Check for image upgrade. Rather simplistic right now.
    #
    $query_result =
	DBQueryFatal("select * from widearea_updates where IP='$IP'");
    if (mysql_num_rows($query_result)) {
	$row     = mysql_fetch_array($query_result);
	$newroot = $row["roottag"];
332
	$force   = $row["force"];
333 334

	# Do not upgrade if the root tag already matches. ???
335
	if (!strcmp($force, "yes")|| !isset($roottag) || strcmp($roottag, $newroot)) {
336 337 338 339 340
	    $update  = 1;

	    DBQueryFatal("update widearea_updates set update_started=now() ".
			 "where IP='$IP'");

341
	    $upgrade_instructions = $confmaps[$newroot];
Leigh Stoller's avatar
Leigh Stoller committed
342
		
343
	    if (!$upgrade_instructions) {
344
		SPITSTATUS(CDROMSTATUS_UPGRADEERROR);
Leigh Stoller's avatar
Leigh Stoller committed
345
		return;
346 347
	    }
	}
348
    }
349 350

    #
351 352 353
    # Generate a new privkey and stash in the DB. Always stash the root
    # tag the node is running. If upgrading, it will be overwritten above
    # when it completes. This helps to figure out where things went wrong.
354 355 356
    #
    $newkey = GENHASH();
    DBQueryFatal("update widearea_privkeys ".
357
		 "set nextprivkey='$newkey',updated=now(),cdkey='$cdkey' ".
358
		 (isset($roottag) ? ",rootkey='$roottag'" : "") .
359 360 361 362
		 "where IP='$IP' and privkey='$privkey'");

    header("Content-Type: text/plain");
    echo "privkey=$newkey\n";
363 364
    if ($upgrade_instructions) 
	echo "$upgrade_instructions\n";
365 366
    echo "emulab_status=0\n";
    
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    return;
}

#
# We need to check for duplicate IPs. 
#
$query_result =
    DBQueryFatal("select * from widearea_privkeys where IP='$IP'");

if (mysql_num_rows($query_result)) {
    SPITSTATUS(CDROMSTATUS_IPADDRINUSE);
    return;
}

#
# Generate a new privkey and return the info. The DB is updated with the
# next key, but it does not become effective until it responds that it
# finished okay. This leaves a window open, but the eventual plan it to
# get rid of these priv keys anyway and go to per-node cert files we update
# on the fly.
# 
$newkey = GENHASH();
DBQueryFatal("update widearea_privkeys ".
390
	     "set nextprivkey='$newkey',updated=now(),cdkey='$cdkey'".
391 392
	     "where privkey='$privkey'");

393 394
header("Content-Type: text/plain");
echo "privkey=$newkey\n";
395
if ($cdvers == 1) {
396 397 398 399 400 401
    if (0) {
	# one shot setup for ISI (or anyone else who has a corrupt image)
	echo "fdisk=http://${WWWHOST}/images/image.fdisk\n";
	echo "slice1_image=http://${WWWHOST}/images/slice1-v1.ndz\n";
	echo "slice1_md5=2970e2cf045f5872c6728eeea3b51dae\n";
	echo "slicex_mount=/users\n";
402 403
	echo "slicex_tarball=http://${WWWHOST}/images/slicex-v2.tar.gz\n";
	echo "slicex_md5=a5274072a40ebf2fda8b8596a6e60e0d\n";
404 405 406 407 408
    } else {
	echo "fdisk=image.fdisk\n";
	echo "slice1_image=slice1.ndz\n";
	echo "slice1_md5=cb810b43f49d15b3ac4122ff42f8925d\n";
	echo "slicex_mount=/users\n";
409 410
	echo "slicex_tarball=http://${WWWHOST}/images/slicex.tar.gz\n";
	echo "slicex_md5=a5274072a40ebf2fda8b8596a6e60e0d\n";
411
    }
412
}
413
elseif ($cdvers == 2) {
414 415 416 417 418 419
    echo "fdisk=image.fdisk\n";
    echo "slice1_image=slice1.ndz\n";
    echo "slice1_md5=402f00f2e46d22507cef3d19846b48f8\n";
    echo "slicex_mount=/users\n";
    echo "slicex_tarball=slicex.tar.gz\n";
    echo "slicex_md5=a5274072a40ebf2fda8b8596a6e60e0d\n";
420
}
421
elseif ($cdvers == 3) {
422 423 424
    if (0) {
	echo "fdisk=http://${WWWHOST}/images/image.fdisk\n";
	echo "slice1_image=http://${WWWHOST}/images/slice1-v3.ndz\n";
425
	echo "slice1_md5=a9af4a08310a314c5b38e5e33a2b9c3e\n";
Leigh Stoller's avatar
Leigh Stoller committed
426
	echo "slicex_slice=3\n";
427 428 429 430 431 432 433
	echo "slicex_mount=/users\n";
	echo "slicex_tarball=http://${WWWHOST}/images/slicex-v3.tar.gz\n";
	echo "slicex_md5=0a3398cee6104850adaee7afbe75f008\n";
    }
    else {
	echo "fdisk=image.fdisk\n";
	echo "slice1_image=slice1.ndz\n";
434
	echo "slice1_md5=a9af4a08310a314c5b38e5e33a2b9c3e\n";
Leigh Stoller's avatar
Leigh Stoller committed
435
	echo "slicex_slice=3\n";
436 437 438
	echo "slicex_mount=/users\n";
	echo "slicex_tarball=slicex.tar.gz\n";
	echo "slicex_md5=0a3398cee6104850adaee7afbe75f008\n";
439 440 441
    }
}
elseif ($cdvers == 69) {
442
    if (!strcmp($_SERVER["REMOTE_ADDR"], "155.101.132.191")) {
443 444 445 446 447 448 449 450 451 452 453 454 455 456
	echo "fdisk=image.fdisk\n";
	echo "fdisk_sig=image.fdisk.sig\n";
	echo "slice1_image=slice1.ndz\n";
	echo "slice1_sig=slice1.ndz.sig\n";
	# Still return this for the root tag. Might change later.
	echo "slice1_md5=20d04a3ba96043788e2d31d52e7e7165\n";
    }
    else {
	echo "fdisk=http://${WWWHOST}/images/image.fdisk\n";
	echo "fdisk_sig=https://${WWWHOST}/images/image.fdisk.sig\n";
	echo "slice1_image=http://${WWWHOST}/images/slice1-v4.ndz\n";
	echo "slice1_sig=https://${WWWHOST}/images/slice1-v4.ndz.sig\n";
	# Still return this for the root tag. Might change later.
	echo "slice1_md5=20d04a3ba96043788e2d31d52e7e7165\n";
457 458
    }
}
459
else {
David G Andersen's avatar
David G Andersen committed
460 461
    if ($version_tags[$cdvers]) {
        echo $confmaps[$version_tags[$cdvers]];
462
	echo $confslicex;
463 464
    }
    else {
465 466
        SPITSTATUS(CDROMSTATUS_OTHER);
	return;
467 468
    }
}
469

470
echo "emulab_status=0\n";