bootinfo_mysql.c 22.1 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1
/*
2
 * Copyright (c) 2000-2017 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{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/>.
 * 
 * }}}
Leigh Stoller's avatar
Leigh Stoller committed
22 23
 */

24 25
#include <sys/types.h>
#include <netinet/in.h>
26
#include <netdb.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <syslog.h>
30 31
#include <arpa/inet.h>
#include <string.h>
32
#include "config.h"
33 34
#include "log.h"
#include "tbdb.h"
35
#include "bootwhat.h"
36
#include "bootinfo.h"
37 38
#include <mysql/mysql.h>

39
/* XXX Should be configured in */
40
#define NEWNODEPID      "emulab-ops"
41 42
#define NEWNODEOSID	"NEWNODE-MFS"

43 44
#ifdef USE_MYSQL_DB

45
static int parse_multiboot_path(char *path, boot_what_t *info);
46
static void parse_features(char *path, boot_what_t *info);
47
static void parse_mfs_path(char *path, boot_what_t *info);
48
static int boot_newnode_mfs(struct in_addr, int, boot_what_t *);
49 50 51 52

int
open_bootinfo_db(void)
{
53 54 55
	if (!dbinit())
		return 1;

56 57 58
	return 0;
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72
int
findnode_bootinfo_db(struct in_addr ipaddr, int *events)
{
	MYSQL_RES	*res;
	MYSQL_ROW	row;

	res = mydb_query("select n.node_id,na.attrvalue,nt.attrvalue "
			 "from nodes as n "
			 "left join interfaces as i "
			 "  on n.node_id=i.node_id "
			 "left join node_attributes as na on "
			 "  n.node_id=na.node_id and na.attrkey='boot_method' "
			 "left join node_type_attributes as nt on "
			 "  n.type=nt.type and nt.attrkey='boot_method' "
73 74
			 "where i.IP='%s' and "
			 "      (i.role='ctrl' or i.role='outer_ctrl')",
75 76 77 78 79 80 81 82 83 84 85
			 3, inet_ntoa(ipaddr));
	if (!res) {
		error("Query failed for host %s\n", inet_ntoa(ipaddr));
		return 0;
	}
	if (!mysql_num_rows(res)) {
		mysql_free_result(res);
		return 0;
	}

	row = mysql_fetch_row(res);
86 87
	if ((row[1] && strcmp(row[1], "pxelinux") == 0) ||
	    (row[2] && strcmp(row[2], "pxelinux") == 0))
88 89 90 91 92 93 94 95
		*events = 0;
	else
		*events = 1;

	mysql_free_result(res);
	return 1;
}

96 97 98 99 100 101 102 103
/*
  WARNING!!!
  
  DO NOT change this function without making corresponding changes to
  the perl version of this code in db/libdb.pm . They MUST ALWAYS
  return exactly the same result given the same inputs.
*/

104
int
105 106
query_bootinfo_db(struct in_addr ipaddr, char *node_id, int version, 
		  boot_what_t *info, char* key)
107
{
108
	int		nrows, rval = 0;
109 110
	MYSQL_RES	*res, *res2;
	MYSQL_ROW	row, row2;
111
	char		ipstr[32];
112
	int		haskey=0;
113
	int             bootdisk_bios_id = 0;
114

115 116 117 118 119 120
	char		savedkey[HOSTKEY_LENGTH];
	if(key != NULL)
		{
		strncpy(savedkey, key, HOSTKEY_LENGTH);
		haskey=1;
		}
121 122 123 124 125 126 127 128
	info->cmdline[0] = 0;	/* Must zero first byte! */
	info->flags      = 0;
	strcpy(ipstr, inet_ntoa(ipaddr));

#define DEF_BOOT_OSID		0
#define DEF_BOOT_CMDLINE	1
#define DEF_BOOT_PATH		2
#define DEF_BOOT_MFS		3
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#define DEF_BOOT_FEATURES	4
#define DEF_BOOT_PARTITION	5
#define TEMP_BOOT_OSID		6
#define TEMP_BOOT_PATH		7
#define TEMP_BOOT_MFS		8
#define TEMP_BOOT_FEATURES	9
#define TEMP_BOOT_PARTITION	10
#define NEXT_BOOT_OSID		11
#define NEXT_BOOT_CMDLINE	12
#define NEXT_BOOT_PATH		13
#define NEXT_BOOT_MFS		14
#define NEXT_BOOT_FEATURES	15
#define NEXT_BOOT_PARTITION	16
#define PID			17
#define PXE_BOOT_PATH		18

#define NCOLS			19

147 148 149
#define DEFINED(x)		(row[(x)] != NULL && row[(x)][0] != '\0')
#define TOINT(x)		(atoi(row[(x)]))

150 151 152 153 154 155 156
	if (node_id) {
		/*
		 * Right now, this is ONLY used for checking bootinfo for
		 * imageable vnodes.  All bets off if you try it for something
		 * else!
		 */
		res = mydb_query("select n.def_boot_osid, n.def_boot_cmd_line, "
157
				 "        odef.path, odef.mfs, "
158
				 "        odef.osfeatures, pdef.`partition`, "
159
				 "       n.temp_boot_osid, "
160
				 "        otemp.path, otemp.mfs, "
161
				 "        otemp.osfeatures, ptemp.`partition`, "
162
				 "       n.next_boot_osid, n.next_boot_cmd_line, "
163
				 "        onext.path, onext.mfs, "
164
				 "        onext.osfeatures, pnext.`partition`, "
165 166 167
				 "       r.pid,n.pxe_boot_path "
				 " from nodes as n "
				 "left join reserved as r on n.node_id=r.node_id "
168
				 "left join `partitions` as pdef on "
169 170
				 "     n.node_id=pdef.node_id and "
				 "     n.def_boot_osid=pdef.osid "
171 172 173 174 175
				 "left join os_info as oidef on "
				 "     oidef.osid=n.def_boot_osid "
				 "left join os_info_versions as odef on "
				 "     odef.osid=oidef.osid and "
				 "     odef.vers=oidef.version "
176
				 "left join `partitions` as ptemp on "
177 178
				 "     n.node_id=ptemp.node_id and "
				 "     n.temp_boot_osid=ptemp.osid "
179 180 181 182 183
				 "left join os_info as oitemp on "
				 "     oitemp.osid=n.temp_boot_osid "
				 "left join os_info_versions as otemp on "
				 "     otemp.osid=oitemp.osid and "
				 "     otemp.vers=oitemp.version "
184
				 "left join `partitions` as pnext on "
185 186
				 "     n.node_id=pnext.node_id and "
				 "     n.next_boot_osid=pnext.osid "
187 188 189 190 191
				 "left join os_info as oinext on "
				 "     oinext.osid=n.next_boot_osid "
				 "left join os_info_versions as onext on "
				 "     onext.osid=oinext.osid and "
				 "     onext.vers=oinext.version "
192 193 194
				 "left outer join "
				 "  (select type,attrvalue from node_type_attributes "
				 "     where attrkey='nobootinfo' and attrvalue='1' "
195
				 "     group by type,attrvalue) as nobootinfo_types "
196 197 198
				 "  on n.type=nobootinfo_types.type "
				 "where n.node_id='%s' "
				 "  and nobootinfo_types.attrvalue is NULL",
199
				 NCOLS, node_id);
200 201
	}
	else if (! haskey) {
202
		res = mydb_query("select n.def_boot_osid, n.def_boot_cmd_line, "
203
				 "        odef.path, odef.mfs, "
204
				 "        odef.osfeatures, pdef.`partition`, "
205
				 "       n.temp_boot_osid, "
206
				 "        otemp.path, otemp.mfs, "
207
				 "        otemp.osfeatures, ptemp.`partition`, "
208
				 "       n.next_boot_osid, n.next_boot_cmd_line, "
209
				 "        onext.path, onext.mfs, "
210
				 "        onext.osfeatures, pnext.`partition`, "
211 212 213 214
				 "       r.pid,n.pxe_boot_path "
				 " from interfaces as i "
				 "left join nodes as n on i.node_id=n.node_id "
				 "left join reserved as r on i.node_id=r.node_id "
215
				 "left join `partitions` as pdef on "
216 217
				 "     n.node_id=pdef.node_id and "
				 "     n.def_boot_osid=pdef.osid "
218 219 220 221 222
				 "left join os_info as oidef on "
				 "     oidef.osid=n.def_boot_osid "
				 "left join os_info_versions as odef on "
				 "     odef.osid=oidef.osid and "
				 "     odef.vers=oidef.version "
223
				 "left join `partitions` as ptemp on "
224 225
				 "     n.node_id=ptemp.node_id and "
				 "     n.temp_boot_osid=ptemp.osid "
226 227 228 229 230
				 "left join os_info as oitemp on "
				 "     oitemp.osid=n.temp_boot_osid "
				 "left join os_info_versions as otemp on "
				 "     otemp.osid=oitemp.osid and "
				 "     otemp.vers=oitemp.version "
231
				 "left join `partitions` as pnext on "
232 233
				 "     n.node_id=pnext.node_id and "
				 "     n.next_boot_osid=pnext.osid "
234 235 236 237 238
				 "left join os_info as oinext on "
				 "     oinext.osid=n.next_boot_osid "
				 "left join os_info_versions as onext on "
				 "     onext.osid=oinext.osid and "
				 "     onext.vers=oinext.version "
239 240 241
				 "left outer join "
				 "  (select type,attrvalue from node_type_attributes "
				 "     where attrkey='nobootinfo' and attrvalue='1' "
242
				 "     group by type,attrvalue) as nobootinfo_types "
243 244 245
				 "  on n.type=nobootinfo_types.type "
				 "where i.IP='%s' "
				 "  and nobootinfo_types.attrvalue is NULL",
246
				 NCOLS, inet_ntoa(ipaddr));
247 248

		/* Get boot drive from DB */
249
		res2 = mydb_query("select attrvalue from node_attributes as a, nodes as n, interfaces as i where "
Ryan Jackson's avatar
Ryan Jackson committed
250
				  "i.IP = '%s' and i.node_id = n.node_id and n.node_id = a.node_id and a.attrkey = 'bootdisk_bios_id';",
251 252 253 254 255 256 257 258
				  1, inet_ntoa(ipaddr));

		if (!res2) {
			error("Query failed for host %s\n", node_id ? node_id : ipstr);
			/* XXX Wrong. Should fail so client can request again later */
			return 0;
		}

259 260 261 262 263 264 265 266 267 268 269 270 271
		if (!mysql_num_rows(res2)) {
			mysql_free_result(res2);
			res2 = mydb_query("select attrvalue from node_type_attributes as a, nodes as n, interfaces as i where "
					  "i.IP = '%s' and i.node_id = n.node_id and n.type = a.type and a.attrkey = 'bootdisk_bios_id';",
					  1, inet_ntoa(ipaddr));

			if (!res2) {
				error("Query failed for host %s\n", node_id ? node_id : ipstr);
				/* XXX Wrong. Should fail so client can request again later */
				return 0;
			}
		}

272 273 274 275
		if (mysql_num_rows(res2)) {
			row2 = mysql_fetch_row(res2);
			bootdisk_bios_id = atoi(row2[0]);
		}
276 277
		
		mysql_free_result(res2);
278 279 280 281
	}
	else { /* User provided a widearea hostkey, so they don't have a necessarily-unique IP address. */
		/* This is meant to be similar to the above, but queries on the wideareanodekey instead. */
		res = mydb_query("SELECT n.def_boot_osid, n.def_boot_cmd_line, "
282
				 " odef.path, odef.mfs, "
283
				 " odef.osfeatures, pdef.`partition`, "
284
				 "n.temp_boot_osid, "
285
				 " otemp.path, otemp.mfs, "
286
				 " otemp.osfeatures, ptemp.`partition`, "
287
				 "n.next_boot_osid, n.next_boot_cmd_line, "
288
				 " onext.path, onext.mfs, "
289
				 " onext.osfeatures, pnext.`partition`, "
290 291
				 "r.pid,n.pxe_boot_path "
				 "FROM nodes AS n "
292 293
				 "LEFT JOIN reserved AS r ON "
				 "     n.node_id=r.node_id "
294
				 "LEFT JOIN `partitions` AS pdef ON "
295 296 297 298 299 300 301
				 "     n.node_id=pdef.node_id AND "
				 "     n.def_boot_osid=pdef.osid "
				 "LEFT JOIN os_info AS oidef ON "
				 "     oidef.osid=n.def_boot_osid "
				 "left join os_info_versions as odef on "
				 "     odef.osid=oidef.osid and "
				 "     odef.vers=oidef.version "
302
				 "LEFT JOIN `partitions` AS ptemp ON"
303 304 305 306 307 308 309
				 "     n.node_id=ptemp.node_id AND "
				 "     n.temp_boot_osid=ptemp.osid "
				 "LEFT JOIN os_info AS oitemp ON "
				 "     oitemp.osid=n.temp_boot_osid "
				 "left join os_info_versions as otemp on "
				 "     otemp.osid=oitemp.osid and "
				 "     otemp.vers=oitemp.version "
310
				 "LEFT JOIN `partitions` AS pnext ON "
311 312 313 314 315 316 317
				 "     n.node_id=pnext.node_id AND "
				 "     n.next_boot_osid=pnext.osid "
				 "LEFT JOIN os_info AS oinext ON "
				 "     oinext.osid=n.next_boot_osid "
				 "left join os_info_versions as onext on "
				 "     onext.osid=oinext.osid and "
				 "     onext.vers=oinext.version "
318
				 "LEFT OUTER JOIN "
319 320 321
					"(SELECT type,attrvalue FROM "
				 "         node_type_attributes WHERE "
				 "           attrkey='nobootinfo' AND "
322 323
				 "           attrvalue='1' "
				 "          GROUP BY type,attrvalue) "
324 325
				 	"AS nobootinfo_types ON "
				 "         n.type=nobootinfo_types.type "
326
				 "WHERE n.node_id IN "
327 328 329 330
				 "    (SELECT node_id FROM widearea_nodeinfo "
				 "     WHERE privkey='%s') "
				 "AND nobootinfo_types.attrvalue IS NULL;",
				 NCOLS, savedkey);
331 332
	}
	
333
	if (!res) {
334
		error("Query failed for host %s\n", node_id ? node_id : ipstr);
335 336
		/* XXX Wrong. Should fail so client can request again later */
		return 0;
337
	}
338
	nrows = mysql_num_rows(res);
339 340 341 342

	switch (nrows) {
	case 0:
		mysql_free_result(res);
343
		return boot_newnode_mfs(ipaddr, version, info);
344 345 346
	case 1:
		break;
	default:
347 348
		error("%d entries for host %s\n",
		      nrows, node_id ? node_id : ipstr);
349 350 351
		break;
	}
	row = mysql_fetch_row(res);
Mike Hibler's avatar
Mike Hibler committed
352 353

	/*
354 355
	 * Version >=1 supports wait if not allocated. Client will recontact
	 * us later.
Mike Hibler's avatar
Mike Hibler committed
356
	 */
357 358 359
	if (version >= 1 && row[PID] == (char *) NULL) {
		info->type = BIBOOTWHAT_TYPE_WAIT;
		goto done;
Mike Hibler's avatar
Mike Hibler committed
360 361
	}

362 363
	/*
	 * If we received a query from a node whose PXE boot program is
364 365 366
	 * not an "Emulab pxeboot", then the node may be coming out of PXEWAIT
	 * and we need to tell it to reboot again to pick up the new PXE boot
	 * program.  An "Emulab pxeboot" is one that speaks bootinfo.
367
	 *
368 369 370
	 * XXX note that an "Emulab pxeboot" is currently identified by
	 * its not being the default pxeboot and its path containing the
	 * string "pxeboot" anywhere.
371 372 373 374 375 376 377
	 */
	if (DEFINED(PXE_BOOT_PATH) &&
	    strstr(row[PXE_BOOT_PATH], "pxeboot") == NULL) {
		info->type = BIBOOTWHAT_TYPE_REBOOT;
		goto done;
	}

378
	/*
379 380 381
	 * Check next_boot_osid. It overrides the others. It should be
	 * the case that partition and path/mfs are mutually exclusive.
	 * mfs might be set when path is set.  
382
	 */
383 384 385 386
	if (DEFINED(NEXT_BOOT_OSID)) {
		if (DEFINED(NEXT_BOOT_PATH)) {
			if (DEFINED(NEXT_BOOT_MFS) && TOINT(NEXT_BOOT_MFS) == 1){
				info->type = BIBOOTWHAT_TYPE_MFS;
387
				parse_features(row[NEXT_BOOT_FEATURES], info);
388
				parse_mfs_path(row[NEXT_BOOT_PATH], info);
389 390 391 392 393 394 395
			}
			else {
				info->type = BIBOOTWHAT_TYPE_MB;
				parse_multiboot_path(row[NEXT_BOOT_PATH], info);
			}
		}
		else if (DEFINED(NEXT_BOOT_PARTITION)) {
396 397 398 399 400 401 402 403
			if (bootdisk_bios_id) {
				info->type = BIBOOTWHAT_TYPE_DISKPART;
				info->what.dp.disk = bootdisk_bios_id;
				info->what.dp.partition = TOINT(NEXT_BOOT_PARTITION);
			} else {
				info->type = BIBOOTWHAT_TYPE_PART;
				info->what.partition = TOINT(NEXT_BOOT_PARTITION);
			}
404 405
		}
		else {
406 407
			error("Invalid NEXT_BOOT entry for host %s\n",
			      node_id ? node_id : ipstr);
408 409 410
			rval = 1;
		}
		if (DEFINED(NEXT_BOOT_CMDLINE)) {
411 412 413 414
			/*
			 * XXX note that this will override any cmdline
			 * specified in the osid path.  Should append instead?
			 */
415 416 417 418
			strncpy(info->cmdline,
				row[NEXT_BOOT_CMDLINE], MAX_BOOT_CMDLINE-1);
		}
		goto done;
419
	}
420 421 422 423 424 425 426 427

	/*
	 * Check temp_boot_osid. It overrides def_boot but not next_boot.
	 */
	if (DEFINED(TEMP_BOOT_OSID)) {
		if (DEFINED(TEMP_BOOT_PATH)) {
			if (DEFINED(TEMP_BOOT_MFS) && TOINT(TEMP_BOOT_MFS) == 1){
				info->type = BIBOOTWHAT_TYPE_MFS;
428
				parse_features(row[TEMP_BOOT_FEATURES], info);
429
				parse_mfs_path(row[TEMP_BOOT_PATH], info);
430 431 432 433 434 435 436
			}
			else {
				info->type = BIBOOTWHAT_TYPE_MB;
				parse_multiboot_path(row[TEMP_BOOT_PATH], info);
			}
		}
		else if (DEFINED(TEMP_BOOT_PARTITION)) {
437 438 439 440 441 442 443 444
			if (bootdisk_bios_id) {
				info->type = BIBOOTWHAT_TYPE_DISKPART;
				info->what.dp.disk = bootdisk_bios_id;
				info->what.dp.partition = TOINT(TEMP_BOOT_PARTITION);
			} else {
				info->type = BIBOOTWHAT_TYPE_PART;
				info->what.partition = TOINT(TEMP_BOOT_PARTITION);
			}
445 446
		}
		else {
447 448
			error("Invalid TEMP_BOOT entry for host %s\n",
			      node_id ? node_id : ipstr);
449 450 451
			rval = 1;
		}
		goto done;
452
	}
453

454
	/*
455
	 * Lastly, def_boot.
456
	 */
457 458 459 460
	if (DEFINED(DEF_BOOT_OSID)) {
		if (DEFINED(DEF_BOOT_PATH)) {
			if (DEFINED(DEF_BOOT_MFS) && TOINT(DEF_BOOT_MFS) == 1) {
				info->type = BIBOOTWHAT_TYPE_MFS;
461
				parse_features(row[DEF_BOOT_FEATURES], info);
462
				parse_mfs_path(row[DEF_BOOT_PATH], info);
463 464 465 466 467 468 469
			}
			else {
				info->type = BIBOOTWHAT_TYPE_MB;
				parse_multiboot_path(row[DEF_BOOT_PATH], info);
			}
		}
		else if (DEFINED(DEF_BOOT_PARTITION)) {
470 471 472 473 474 475 476 477
			if (bootdisk_bios_id) {
				info->type = BIBOOTWHAT_TYPE_DISKPART;
				info->what.dp.disk = bootdisk_bios_id;
				info->what.dp.partition = TOINT(DEF_BOOT_PARTITION);
			} else {
				info->type = BIBOOTWHAT_TYPE_PART;
				info->what.partition = TOINT(DEF_BOOT_PARTITION);
			}
478 479
		}
		else {
480 481
			error("Invalid DEF_BOOT entry for host %s\n",
			      node_id ? node_id : ipstr);
482 483 484
			rval = 1;
		}
		if (DEFINED(DEF_BOOT_CMDLINE)) {
485 486 487 488
			/*
			 * XXX note that this will override any cmdline
			 * specified in the osid path.  Should append instead?
			 */
489 490 491 492 493
			strncpy(info->cmdline,
				row[DEF_BOOT_CMDLINE], MAX_BOOT_CMDLINE-1);
		}
		goto done;
	}
494 495 496 497
	/*
	 * If we get here, there is no bootinfo to give the client.
	 * New PXE boot clients get PXEWAIT, but older ones get an error.
	 */
498
	error("No OSIDs set for host %s\n", node_id ? node_id : ipstr);
499 500 501 502 503
	if (version >= 1) {
		info->type = BIBOOTWHAT_TYPE_WAIT;
		goto done;
	}
	rval = 1;
504
 done:
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529

	/*
	 * XXX horrific state-specific hack.
	 *
	 * Currently, for nodes that use the gPXE boot dongle, they exit
	 * secure mode (do a TPM "sign-off") before entering PXEWAIT when
	 * freed. When such a node is allocated and it has to reload its
	 * disk, we must reboot the node so that the TPM sign off PCR is
	 * cleared and the secure boot path is cleanly restarted.
	 *
	 * For reasons too bogus to talk about, we cannot currently do
	 * this from stated. So here we detect a wakeup from PXEWAIT for
	 * the purposes of reloading the disk via the "secure MFS".
	 */
	if (!rval && !node_id && info->type == BIBOOTWHAT_TYPE_MFS &&
	    (info->flags & BIBOOTWHAT_FLAGS_SECURE) != 0) {
		res2 = mydb_query("select op_mode,eventstate from "
				  " nodes as n, interfaces as i "
				  " where n.node_id=i.node_id and i.IP='%s'",
				  2, ipstr);
		if (res2) {
			if (mysql_num_rows(res2)) {
				row2 = mysql_fetch_row(res2);
				error("Secure booting node %s in %s/%s\n",
				      ipstr, row2[0], row2[1]);
530
				if (strcmp(row2[0], "PXEKERNEL") == 0) {
531 532 533 534 535 536 537 538
					info->type = BIBOOTWHAT_TYPE_REBOOT;
					error("Forcing reboot of %s\n", ipstr);
				}
			}
			mysql_free_result(res2);
		}
	}

539
	mysql_free_result(res);
540
	return rval;
541 542
}

543 544 545
int
close_bootinfo_db(void)
{
546
	dbclose();
547 548 549
	return 0;
}

550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
/*
 * Split a multiboot path into the IP and Path.
 */
static int
parse_multiboot_path(char *path, boot_what_t *info)
{
	char		*p  = path;
	struct hostent	*he;

	info->type = BIBOOTWHAT_TYPE_MB;
	info->what.mb.tftp_ip.s_addr = 0;

	strsep(&p, ":");
	if (p) {
		he = gethostbyname(path);
		path = p;
	}
	else {
		he = gethostbyname("users.emulab.net");
	}
	if (he) {
		memcpy((char *)&info->what.mb.tftp_ip,
		       he->h_addr, sizeof(info->what.mb.tftp_ip));
	}

	strncpy(info->what.mb.filename, path, MAX_BOOT_PATH-1);

	return 0;
}

580 581 582 583 584
/*
 * Arrange to boot the special newnode kernel.
 */
static int
boot_newnode_mfs(struct in_addr ipaddr, int version, boot_what_t *info)
585
{
586 587 588 589
	int		nrows;
	MYSQL_RES	*res;
	MYSQL_ROW	row;

590 591
	error("%s: nonexistent IP, booting '%s,%s'\n",
	      inet_ntoa(ipaddr), NEWNODEPID, NEWNODEOSID);
592

593 594
#define MFS_PATH	0

595
	res = mydb_query("select v.path from os_info as o "
596 597 598 599
			 "left join os_info_versions as v on "
			 "     v.osid=o.osid and v.vers=o.version "
			 "where o.pid='%s' and o.osname='%s' and v.mfs=1 ",
			 1, NEWNODEPID, NEWNODEOSID);
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

	if (!res) {
		error("Query failed\n");
		/* XXX Wrong. Should fail so client can request again later */
		return 0;
	}
	nrows = mysql_num_rows(res);

	switch (nrows) {
	case 0:
		error("No DB entry for OSID %s\n", NEWNODEOSID);
		mysql_free_result(res);
		return 1;
	case 1:
		break;
	default:
		error("Too many DB entries for OSID %s\n", NEWNODEOSID);
617
		mysql_free_result(res);
618 619 620 621 622 623
		return 1;
	}
	row = mysql_fetch_row(res);

	if (row[MFS_PATH] != 0 && row[MFS_PATH][0] != '\0') {
		info->type = BIBOOTWHAT_TYPE_MFS;
624
		parse_mfs_path(row[MFS_PATH], info);
625 626 627
		mysql_free_result(res);
		return 0;
	}
628
	mysql_free_result(res);
629 630 631
	error("No path info for OSID %s\n", NEWNODEOSID);
	return 1;
#undef  MFS_PATH
632 633
}

634 635 636 637 638
void
parse_mfs_path(char *str, boot_what_t *info)
{
	struct hostent *he;
	struct in_addr hip;
639 640 641 642 643 644 645 646
	char *path, *args;

	/* treat anything after a space as the command line */
	args = strchr(str, ' ');
	if (args != NULL) {
		*args++ = '\0';
		strncpy(info->cmdline, args, MAX_BOOT_CMDLINE-1);
	}
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679

	/* no hostname, just copy string as is */
	path = strchr(str, ':');
	if (path == NULL) {
		strncpy(info->what.mfs, str, sizeof(info->what.mfs));
		return;
	}
	*path = '\0';

	/* hostname is a valid IP addr, copy as is */
	if (inet_addr(str) != INADDR_NONE) {
		*path = ':';
		strncpy(info->what.mfs, str, sizeof(info->what.mfs));
		return;
	}

	/* not a valid hostname, whine and copy it as is */
	he = gethostbyname(str);
	if (he == NULL) {
		*path = ':';
		error("Invalid hostname in MFS path '%s', passing anyway\n",
		      str);
		strncpy(info->what.mfs, str, sizeof(info->what.mfs));
		return;
	}
	*path = ':';

	/* valid hostname, translate to IP and replace in string */
	memcpy((char *)&hip, he->h_addr, he->h_length);
	strcpy(info->what.mfs, inet_ntoa(hip));
	strncat(info->what.mfs, path,
		sizeof(info->what.mfs)-strlen(info->what.mfs));
}
680

681 682 683
static void
parse_features(char *str, boot_what_t *info)
{
684 685
	/* Note that features might be a null string; no features */
	if (str && strstr(str, "ontrustedboot")) {
686 687 688 689
		info->flags |= BIBOOTWHAT_FLAGS_SECURE;
	}
}

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
/*
 * ElabinElab hack. Its really terrible!
 */
#ifdef ELABINELAB
int
elabinelab_hackcheck(struct sockaddr_in *target)
{
	int		nrows;
	MYSQL_RES	*res;
	MYSQL_ROW	row;

	res = mydb_query("select i2.IP from interfaces as i1 "
			 "left join interfaces as i2 on i2.node_id=i1.node_id "
			 "     and i2.role='outer_ctrl' "
			 "where i1.IP='%s'", 1, inet_ntoa(target->sin_addr));
	if (!res) {
		error("elabinelab_hackcheck failed for host %s\n",
		      inet_ntoa(target->sin_addr));
		/* XXX Wrong. Should fail so client can request again later */
		return 0;
	}

	nrows = mysql_num_rows(res);

	switch (nrows) {
	case 0:
		/* No hack interface */
		mysql_free_result(res);
		return 0;
	case 1:
		break;
	default:
		error("elabinelab_hackcheck: Too many DB entries %s\n",
		      inet_ntoa(target->sin_addr));
		mysql_free_result(res);
		return 1;
	}
	row = mysql_fetch_row(res);

	if (row[0] != 0 && row[0][0] != '\0') {
		inet_aton(row[0], &(target->sin_addr));
731
		mysql_free_result(res);
732 733 734 735 736 737 738 739 740
		return 0;
	}
	mysql_free_result(res);
	error("elabinelab_hackcheck: No IP address %s\n",
	      inet_ntoa(target->sin_addr));
	return 1;
}
#endif

741 742 743
#ifdef TEST
#include <stdarg.h>

Mike Hibler's avatar
Mike Hibler committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
static void
print_bootwhat(boot_what_t *bootinfo)
{
	switch (bootinfo->type) {
	case BIBOOTWHAT_TYPE_PART:
		printf("boot from partition %d\n",
		       bootinfo->what.partition);
		break;
	case BIBOOTWHAT_TYPE_SYSID:
		printf("boot from partition with sysid %d\n",
		       bootinfo->what.sysid);
		break;
	case BIBOOTWHAT_TYPE_MB:
		printf("boot multiboot image %s:%s\n",
		       inet_ntoa(bootinfo->what.mb.tftp_ip),
		       bootinfo->what.mb.filename);
		break;
761 762 763 764 765 766
	case BIBOOTWHAT_TYPE_WAIT:
		printf("No boot; waiting till allocated\n");
		break;
	case BIBOOTWHAT_TYPE_MFS:
		printf("boot from MFS %s\n", bootinfo->what.mfs);
		break;
Mike Hibler's avatar
Mike Hibler committed
767
	}
768 769 770
	if (bootinfo->cmdline[0])
		printf("Command line %s\n", bootinfo->cmdline);
		
Mike Hibler's avatar
Mike Hibler committed
771 772
}

773
int
774 775 776 777 778 779
main(int argc, char **argv)
{
	struct in_addr ipaddr;
	boot_info_t boot_info;
	boot_what_t *boot_whatp = (boot_what_t *)&boot_info.data;

780
	open_bootinfo_db();
781 782
	while (--argc > 0) {
		if (inet_aton(*++argv, &ipaddr))
783
			if (query_bootinfo_db(ipaddr, 1, boot_whatp) == 0) {
Mike Hibler's avatar
Mike Hibler committed
784 785 786
				printf("%s: ", *argv);
				print_bootwhat(boot_whatp);
			} else
787
				printf("%s: failed\n", *argv);
788 789 790
		else
			printf("bogus IP address `%s'\n", *argv);
	}
791
	close_bootinfo_db();
792 793 794 795
	exit(0);
}
#endif
#endif