cdromcheckin.php3 10.4 KB
Newer Older
1
2
3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# All rights reserved.
#
require("defs.php3");

# 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);
define("CDROMSTATUS_OTHER",		199);

#
# 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)
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
27
28
    global $REMOTE_ADDR, $REQUEST_URI;
    
29
30
    header("Content-Type: text/plain");
    echo "emulab_status=$status\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31

32
33
34
35
    if ($status) {
	    TBERROR("CDROM Checkin Error ($status) from $REMOTE_ADDR:\n\n".
		    "$REQUEST_URI\n", 0);
    }
36
37
38
39
40
}

#
# This page is not intended to be invoked by humans!
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
if ((!isset($cdkey) || !strcmp($cdkey, ""))) {
42
43
44
45
    SPITSTATUS(CDROMSTATUS_MISSINGARGS);
    return;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
46
if (!ereg("[0-9a-zA-Z]+", $cdkey)) {
47
48
49
50
    SPITSTATUS(CDROMSTATUS_INVALIDARGS);
    return;
}

51
52
53
54
55
56
57
58
59
#
# 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;
}
60
61
$row    = mysql_fetch_array($query_result);
$cdvers = $row[version];
62

Leigh B. Stoller's avatar
Leigh B. Stoller committed
63
64
#
# Node is requesting latest instructions. Give it the script. This could
65
# be on a per CDROM basis, but for now its hardwired.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66
67
68
69
70
# I know, the hardwired paths are bad; move into the cdroms DB table, even
# if duplicated. 
# 
if (isset($needscript)) {
    header("Content-Type: text/plain");
71
72
73
74
75
76
77
78
79

    if ($cdvers <= 3) {
	echo "MD5=5ed48bdf6def666d5a5d643cbc2ce4d0\n";
	echo "URL=https://${WWWHOST}/images/netbed-setup-v3.pl\n";
    }
    else {
	echo "URL=https://${WWWHOST}/images/netbed-setup-v4.pl\n";
	echo "SIG=https://${WWWHOST}/images/netbed-setup-v4.pl.sig\n";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    echo "emulab_status=0\n";
    return;
}

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

if (!ereg("[0-9a-zA-Z ]+", $privkey) ||
    !ereg("[0-9\.]+", $IP)) {
    SPITSTATUS(CDROMSTATUS_INVALIDARGS);
    return;
}

if (isset($wahostname) &&
    !ereg("[-_0-9a-zA-Z\.]+", $wahostname)) {
    SPITSTATUS(CDROMSTATUS_INVALIDARGS);
    return;
}

105
106
107
108
109
110
111
112
113
114
115
116
117
#
# Generate a nickname if given hostname.
#
unset($nickname);
if (isset($wahostname)) {
    if (strpos($wahostname, ".")) {
	$nickname = str_replace(".", "-", $wahostname);
    }
    else {
	$nickname = $wahostname;
    }
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
118
119
120
121
122
123
if (isset($roottag) &&
    !ereg("[0-9a-zA-Z]+", $roottag)) {
    SPITSTATUS(CDROMSTATUS_INVALIDARGS);
    return;
}

124
#
125
# Grab the privkey record. First squeeze out any spaces.
126
#
127
128
$privkey = str_replace(" ", "", $privkey);

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
$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);
$privIP = $warow[IP];

#
# 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'");
152

153
154
155
156
	$newargs = "-w -i $IP ";
	if (isset($nickname)) {
	    $newargs .= " -n $nickname ";
	}
157

158
159
	$type = "pcwa";
	if (isset($wahostname)) {
160
161
162
163
164
            #
            # XXX Parse hostname to see if a ron node.
	    # Silly, but effective since we will never have anything else
	    # besides wa/ron.
            #
165
166
	    if (strstr($wahostname, ".ron.") ||
		strstr($wahostname, "roncluster")) {
167
168
169
		$type = "pcron";
	    }
	}
170
171
	$newargs .= " -t $type ";
	SUEXEC("nobody", $TBADMINGROUP, "webnewwanode $newargs", 0);
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

	#
	# 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");
194
    }
195
196
197
198
199
    $newroot = "";
    if (isset($roottag)) {
	    $newroot = ",rootkey='$roottag'";
    }

200
201
    DBQueryFatal("update widearea_privkeys ".
		 "set privkey=nextprivkey,updated=now(),nextprivkey=NULL ".
202
		 "    $newroot ".
203
204
		 "where privkey='$privkey'");

205
206
207
208
209
210
211
212
213
214
215
    #
    # Update with new nickname. Nice if node changes its real hostname.
    #
    if (isset($nickname)) {
	$nodeid = TBIPtoNodeID($IP);
	if ($nodeid) {
	    DBQueryFatal("update reserved set vname='$nickname' ".
			 "where node_id='$nodeid'");
	}
    }

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    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;
    }
    if (strcmp($REMOTE_ADDR, $IP)) {
	SPITSTATUS(CDROMSTATUS_BADREMOTEIP);
	return;
    }
233
234
235
236
    $newroot = "";
    if (isset($roottag)) {
	    $newroot = ",rootkey='$roottag'";
    }
237
238

    #
239
    # For now we just give them a new privkey. There is no real image upgrade
240
241
242
243
    # path in place yet.
    #
    $newkey = GENHASH();
    DBQueryFatal("update widearea_privkeys ".
244
		 "set nextprivkey='$newkey',updated=now(),cdkey='$cdkey' ".
245
		 "    $newroot ".
246
247
248
249
		 "where IP='$IP' and privkey='$privkey'");

    header("Content-Type: text/plain");
    echo "privkey=$newkey\n";
250

251
252
253
254
255
256
257
258
    if (0 && $cdvers == 69) {
	echo "slice1_image=http://${WWWHOST}/images/slice1-v4.ndz\n";
	echo "slice1_md5=d10baf117e269e3faa9cf4ee5d089a7f\n";
	echo "slice1_sig=https://${WWWHOST}/images/slice1-v4.ndz.sig\n";
#	echo "slicex_slice=3\n";
#	echo "slicex_mount=/users\n";
#	echo "slicex_tarball=http://${WWWHOST}/images/slicex-v4.tar.gz\n";
#	echo "slicex_sig=https://${WWWHOST}/images/slicex-v4.tar.gz.sig\n";
259
260
261
    }
    echo "emulab_status=0\n";
    
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    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 ".
285
	     "set nextprivkey='$newkey',updated=now(),cdkey='$cdkey'".
286
287
	     "where privkey='$privkey'");

288
289
header("Content-Type: text/plain");
echo "privkey=$newkey\n";
290
if ($cdvers == 1) {
Mike Hibler's avatar
Mike Hibler committed
291
292
293
294
295
296
    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";
297
298
	echo "slicex_tarball=http://${WWWHOST}/images/slicex-v2.tar.gz\n";
	echo "slicex_md5=a5274072a40ebf2fda8b8596a6e60e0d\n";
Mike Hibler's avatar
Mike Hibler committed
299
300
301
302
303
    } else {
	echo "fdisk=image.fdisk\n";
	echo "slice1_image=slice1.ndz\n";
	echo "slice1_md5=cb810b43f49d15b3ac4122ff42f8925d\n";
	echo "slicex_mount=/users\n";
304
305
	echo "slicex_tarball=http://${WWWHOST}/images/slicex.tar.gz\n";
	echo "slicex_md5=a5274072a40ebf2fda8b8596a6e60e0d\n";
Mike Hibler's avatar
Mike Hibler committed
306
    }
307
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
308
elseif ($cdvers == 2) {
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    if (0) {
	echo "fdisk=http://${WWWHOST}/images/image.fdisk\n";
	echo "slice1_image=http://${WWWHOST}/images/slice1-v2.ndz\n";
	echo "slice1_md5=402f00f2e46d22507cef3d19846b48f8\n";
	echo "slicex_mount=/users\n";
	echo "slicex_tarball=http://${WWWHOST}/images/slicex-v2.tar.gz\n";
	echo "slicex_md5=a5274072a40ebf2fda8b8596a6e60e0d\n";
    }
    else {
	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";
    }
325
}
326
elseif ($cdvers == 3) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
327
328
329
    if (0) {
	echo "fdisk=http://${WWWHOST}/images/image.fdisk\n";
	echo "slice1_image=http://${WWWHOST}/images/slice1-v3.ndz\n";
330
	echo "slice1_md5=a9af4a08310a314c5b38e5e33a2b9c3e\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
331
	echo "slicex_slice=3\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
332
333
334
335
336
337
338
	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";
339
	echo "slice1_md5=a9af4a08310a314c5b38e5e33a2b9c3e\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
340
	echo "slicex_slice=3\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
341
342
343
344
345
	echo "slicex_mount=/users\n";
	echo "slicex_tarball=slicex.tar.gz\n";
	echo "slicex_md5=0a3398cee6104850adaee7afbe75f008\n";
    }
}
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
else {
    if (0) {
	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=d326a1f604489c43b488fa80a88221f4\n";
	echo "slicex_slice=3\n";
	echo "slicex_mount=/users\n";
	echo "slicex_tarball=https://${WWWHOST}/images/slicex-v4.tar.gz\n";
	echo "slicex_sig=http://${WWWHOST}/images/slicex-v4.tar.gz.sig\n";
    }
    else {
	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=d326a1f604489c43b488fa80a88221f4\n";
	echo "slicex_slice=3\n";
	echo "slicex_mount=/users\n";
	echo "slicex_tarball=slicex.tar.gz\n";
	echo "slicex_sig=slicex.tar.gz.sig\n";
    }
}
372
echo "emulab_status=0\n";