config_emulab.c 25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 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 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 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 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 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 680 681 682 683 684 685 686 687 688 689 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 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
/*
 * Configuration "file" handling for Emulab.
 *
 * Uses info straight from the Emulab database.
 */ 

#ifdef USE_EMULAB_CONFIG
#include <sys/param.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <mysql/mysql.h>
#include "log.h"
#include "configdefs.h"

/* Emulab includes */
#include "tbdb.h"	/* the DB library */
#include "config.h"	/* the defs-* defines */

/* Extra info associated with a image information entry */
struct emulab_ii_extra_info {
	int DB_imageid;
};

/* Extra info associated with a host authentication entry */
struct emulab_ha_extra_info {
	char *pid;	/* project */
	char *gid;	/* group */
	char *eid;	/* experiment */
	char *suid;	/* swapper user name */
};

static char *MC_BASEADDR = FRISEBEEMCASTADDR;
static char *MC_BASEPORT = FRISEBEEMCASTPORT;
static char *SHAREDIR	 = SHAREROOT_DIR;
static char *PROJDIR	 = PROJROOT_DIR;
static char *GROUPSDIR	 = GROUPSROOT_DIR;
static char *USERSDIR	 = USERSROOT_DIR;
static char *SCRATCHDIR	 = SCRATCHROOT_DIR;

/* XXX should be autoconfiged as part of Emulab build */
static char *IMAGEDIR    = "/usr/testbed/images";

/* Emit aliases when dumping the config info; makes it smaller */
static int dump_doaliases = 1;

/* Multicast address/port base info */
static int mc_a, mc_b, mc_c, mc_port;

/* Memory alloc functions that abort when no memory */
static void *mymalloc(size_t size);
static void *myrealloc(void *ptr, size_t size);
static char *mystrdup(const char *str);

static int
emulab_init(void)
{
	static int called;
	char pathbuf[PATH_MAX], *path;

	if (!dbinit())
		return -1;

	if (called)
		return 0;
	called++;

	/* One time parsing of MC address info */
	if (sscanf(MC_BASEADDR, "%d.%d.%d", &mc_a, &mc_b, &mc_c) != 3) {
		error("emulab_init: MC_BASEADDR '%s' not in A.B.C format!",
		      MC_BASEADDR);
		return -1;
	}
	mc_port = atoi(MC_BASEPORT);

	if ((path = realpath(SHAREROOT_DIR, pathbuf)) == NULL) {
		error("emulab_init: could not resolve '%s'", SHAREROOT_DIR);
		return -1;
	}
	SHAREDIR = mystrdup(path);

	if ((path = realpath(PROJROOT_DIR, pathbuf)) == NULL) {
		error("emulab_init: could not resolve '%s'", PROJROOT_DIR);
		return -1;
	}
	PROJDIR = mystrdup(path);

	if ((path = realpath(GROUPSROOT_DIR, pathbuf)) == NULL) {
		error("emulab_init: could not resolve '%s'", GROUPSROOT_DIR);
		return -1;
	}
	GROUPSDIR = mystrdup(path);

	if ((path = realpath(USERSROOT_DIR, pathbuf)) == NULL) {
		error("emulab_init: could not resolve '%s'", USERSROOT_DIR);
		return -1;
	}
	USERSDIR = mystrdup(path);

	if (strlen(SCRATCHROOT_DIR) > 0) {
		if ((path = realpath(SCRATCHROOT_DIR, pathbuf)) == NULL) {
			error("emulab_init: could not resolve '%s'",
			      SCRATCHROOT_DIR);
			return -1;
		}
		SCRATCHDIR = mystrdup(path);
	} else
		SCRATCHDIR = NULL;

	return 0;
}

static void
emulab_deinit(void)
{
	dbclose();
}

static int
emulab_read(void)
{
	/* "Reading" the config file is a no-op. */
	return 0;
}

static void *
emulab_save(void)
{
	static int dummy;

	/* Just return non-zero value */
	return (void *)&dummy;
}

static int
emulab_restore(void *state)
{
	return 0;
}

static void
emulab_free(void *state)
{
}

/*
 * Set the allowed GET methods.
 * XXX for now, just multicast.
 */
void
set_get_methods(struct config_host_authinfo *ai, int ix)
{
	ai->imageinfo[ix].get_methods = CONFIG_IMAGE_MCAST;
}

/*
 * Set the GET options for a particular node/image.
 * XXX right now these are implied by various bits of state in the DB:
 *
 * running in elabinelab:      54Mb/sec, keepalive of 15 sec.
 * loading a "standard" image: 72Mb/sec
 * any other image:            54Mb/sec
 */
void
set_get_options(struct config_host_authinfo *ai, int ix)
{
	char str[256];

	strcpy(str, "");
#ifdef ELABINELAB
	strcat(str, " -W 54000000 -K 15");
#else
	if (!strncmp(ai->imageinfo[ix].path, IMAGEDIR, strlen(IMAGEDIR)))
		strcat(str, " -W 72000000");
	else
		strcat(str, " -W 54000000");
#endif

	/*
	 * We use a small server inactive timeout since we no longer have
	 * to start up a frisbeed well in advance of the client(s).
	 */
	strcat(str, " -T 60");

	ai->imageinfo[ix].get_options = mystrdup(str);
}

#define FREE(p) { if (p) free(p); }

/*
 * Free the dynamically allocated host_authinfo struct.
 */
static void
emulab_free_host_authinfo(struct config_host_authinfo *ai)
{
	int i;

	if (ai == NULL)
		return;

	FREE(ai->hostid);
	if (ai->imageinfo != NULL) {
		for (i = 0; i < ai->numimages; i++) {
			FREE(ai->imageinfo[i].imageid);
			FREE(ai->imageinfo[i].path);
			FREE(ai->imageinfo[i].get_options);
			FREE(ai->imageinfo[i].put_options);
			FREE(ai->imageinfo[i].extra);
		}
		free(ai->imageinfo);
	}
	if (ai->extra) {
		struct emulab_ha_extra_info *ei = ai->extra;

		FREE(ei->pid);
		FREE(ei->gid);
		FREE(ei->eid);
		FREE(ei->suid);
		free(ai->extra);
	}
	free(ai);
}

/*
 * Return the IP address/port to be used by the server/clients for
 * the image listed in ai->imageinfo[0].  Methods lists one or more transfer
 * methods that the client can handle, we return the method chosen.
 *
 * For Emulab, we use the frisbee_index from the DB along with the base
 * multicast address and port to compute a unique address/port.  Uses the
 * same logic that frisbeelauncher used to use.
 * XXX right now we only do multicast.
 *
 * Return zero on success, non-zero otherwise.
 */
static int
emulab_get_server_address(struct config_host_authinfo *ai, int methods,
			  in_addr_t *addrp, in_port_t *portp, int *methp)
{
	int	  idx;
	int	  a, b, c, d;

	assert(ai->numimages == 1 && ai->imageinfo != NULL);

	if ((methods & CONFIG_IMAGE_MCAST) == 0) {
		error("get_server_address: only support MCAST right now!");
		return 1;
	}

	if (!mydb_update("UPDATE emulab_indicies "
			" SET idx=LAST_INSERT_ID(idx+1) "
			" WHERE name='frisbee_index'")) {
		error("get_server_address: DB update error!");
		return 1;
	}
	idx = mydb_insertid();
	assert(idx > 0);

	a = mc_a;
	b = mc_b;
	c = mc_c;
	d = 1;

	d += idx;
	if (d > 254) {
		c += (d / 254);
		d = (d % 254) + 1;
	}
	if (c > 254) {
		b += (c / 254);
		c = (c % 254) + 1;
	}
	if (b > 254) {
		error("get_server_address: ran out of MC addresses!");
		return 1;
	}

	*methp = CONFIG_IMAGE_MCAST;
	*addrp = (a << 24) | (b << 16) | (c << 8) | d;
	*portp = mc_port + (((c << 8) | d) & 0x7FFFF);

	return 0;
}

/*
 * Return one if 'path' is in 'dir'; i.e., is 'dir' a prefix of 'path'?
 * Note: returns zero if path == dir.
 * Note: assumes realpath has been run on both.
 */
static int
isindir(char *dir, char *path)
{
	int len = dir ? strlen(dir) : 0;
	int rv = 1;

	if (dir == NULL || path == NULL || *dir == '\0' || *path == '\0' ||
	    strncmp(dir, path, len) || path[len] != '/')
		rv = 0;
	if (0)
		fprintf(stderr, "isindir(dir=%s, path=%s) => %d\n",
			dir ? dir : "", path ? path : "", rv);
	return rv;
}

/*
 * Check and see if the given image is in the set of standard directories
 * to which this node (actually pid/gid/eid/uid) has access.  The set is:
 *  - /share		      (GET only)
 *  - /proj/<pid>	      (GET only right now)
 *  - in /groups/<pid>/<gid>  (GET only right now)
 *  - in /users/<swapper-uid> (GET only right now)
 * and, if it exists:
 *  - in /scratch/<pid>	      (GET or PUT)
 *
 * We assume the get and put structures have been initialized, in particular
 * that they contain the "extra" info which has the pid/gid/eid/uid.
 */
static void
allow_stddirs(char *imageid, 
	      struct config_host_authinfo *get,
	      struct config_host_authinfo *put)
{
	char tpath[PATH_MAX];
	char *fpath, *shdir, *pdir, *gdir, *scdir, *udir;
	int doput = 0, doget = 0;
	struct emulab_ha_extra_info *ei;

	if (get == NULL && put == NULL)
		return;

	ei = get ? get->extra : put->extra;
	fpath = NULL;

	/*
	 * Construct the allowed directories for this pid/gid/eid/uid.
	 */
	shdir = SHAREDIR;
	snprintf(tpath, sizeof tpath, "%s/%s", PROJDIR, ei->pid);
	pdir = mystrdup(tpath);

	snprintf(tpath, sizeof tpath, "%s/%s/%s", GROUPSDIR, ei->pid, ei->gid);
	gdir = mystrdup(tpath);

	snprintf(tpath, sizeof tpath, "%s/%s", USERSDIR, ei->suid);
	udir = mystrdup(tpath);

	if (SCRATCHDIR) {
		snprintf(tpath, sizeof tpath, "%s/%s", SCRATCHDIR, ei->pid);
		scdir = mystrdup(tpath);
	} else
		scdir = NULL;

	/*
	 * No image specified, just return info about the directories
	 * that are accessible.
	 */
	if (imageid == NULL) {
		int ni, i;
		size_t ns;
		char *dirs[8];
		struct config_imageinfo *ci;

		/*
		 * Right now, only allow PUT to scratchdir if it exists.
		 */
		if (put != NULL && scdir) {
			dirs[0] = scdir;
			ni = put->numimages + 1;
			ns = ni * sizeof(struct config_imageinfo);
			if (put->imageinfo)
				put->imageinfo = myrealloc(put->imageinfo, ns);
			else
				put->imageinfo = mymalloc(ns);
			for (i = put->numimages; i < ni; i++) {
				ci = &put->imageinfo[i];
				ci->imageid = NULL;
				ci->path = mystrdup(dirs[i - put->numimages]);
				ci->flags = CONFIG_IMAGE_ISDIR;
				ci->get_options = NULL;
				ci->get_methods = 0;
				ci->put_options = NULL;
				ci->extra = NULL;
			}
			put->numimages = ni;
		}
		/*
		 * Get allows any of the above
		 */
		if (get != NULL) {
			dirs[0] = shdir;
			dirs[1] = pdir;
			dirs[2] = gdir;
			dirs[3] = udir;
			dirs[4] = scdir;
			ni = get->numimages + (scdir ? 5 : 4);
			ns = ni * sizeof(struct config_imageinfo);
			if (get->imageinfo)
				get->imageinfo = myrealloc(get->imageinfo, ns);
			else
				get->imageinfo = mymalloc(ns);
			for (i = get->numimages; i < ni; i++) {
				ci = &get->imageinfo[i];
				ci->imageid = NULL;
				ci->path = mystrdup(dirs[i - get->numimages]);
				ci->flags = CONFIG_IMAGE_ISDIR;
				set_get_options(get, i);
				set_get_methods(get, i);
				ci->put_options = NULL;
				ci->extra = NULL;
			}
			get->numimages = ni;
		}
		goto done;
	}

	/*
	 * Image was specified; find the real path for the targetted file.
	 * Don't want users symlinking to files outside their allowed space.
	 */
	if (realpath(imageid, tpath) == NULL)
		goto done;
	fpath = mystrdup(tpath);

	/*
	 * Make the appropriate access checks for get/put
	 */
	if (put != NULL &&
	    (isindir(scdir, fpath)))
		doput = 1;
	if (get != NULL &&
	    (isindir(shdir, fpath) ||
	     isindir(pdir, fpath) ||
	     isindir(gdir, fpath) ||
	     isindir(scdir, fpath) ||
	     isindir(udir, fpath)))
		doget = 1;

	if (doput) {
		assert(put->imageinfo == NULL);

		put->imageinfo = mymalloc(sizeof(struct config_imageinfo));
		put->numimages = 1;
		put->imageinfo[0].imageid = mystrdup(imageid);
		put->imageinfo[0].path = mystrdup(fpath);
		put->imageinfo[0].flags = CONFIG_IMAGE_ISFILE;
		put->imageinfo[0].get_options = NULL;
		put->imageinfo[0].get_methods = 0;
		put->imageinfo[0].put_options = NULL;
		put->imageinfo[0].extra = NULL;
	}
	if (doget) {
		assert(get->imageinfo == NULL);

		get->imageinfo = mymalloc(sizeof(struct config_imageinfo));
		get->numimages = 1;
		get->imageinfo[0].imageid = mystrdup(imageid);
		get->imageinfo[0].path = mystrdup(fpath);
		get->imageinfo[0].flags = CONFIG_IMAGE_ISFILE;
		set_get_options(get, 0);
		set_get_methods(get, 0);
		get->imageinfo[0].put_options = NULL;
		get->imageinfo[0].extra = NULL;
	}

 done:
	free(pdir);
	free(gdir);
	free(udir);
	if (scdir)
		free(scdir);
	if (fpath)
		free(fpath);
}

/*
 * Find all images (imageid==NULL) or a specific image (imageid!=NULL)
 * that a particular node can access for GET/PUT.  At any time, a node is
 * associated with a specific project and group.  These determine the
 * ability to GET/PUT an image.  For the all image case, the list of
 * accessible images will include:
 *
 * - all "global" images (GET only)
 * - all "shared" images in the project and any group (GET only)
 * - all images in the project and group of the node
 *
 * For a single image this will either return a single image or no image.
 * The desired image must be one of the images that would be returned in
 * the all images case.
 *
 * Imageids are constructed from "pid/imageid" which is the minimal unique
 * identifier (i.e., image name are a per-project namespace and not a
 * per-project, per-group namespace).
 *
 * Return zero on success, non-zero otherwise.
 */
static int
emulab_get_host_authinfo(struct in_addr *in, char *imageid,
			    struct config_host_authinfo **getp,
			    struct config_host_authinfo **putp)
{
	MYSQL_RES	*res;
	MYSQL_ROW	row;
	char		*node;
	int		i, nrows;
	char		*wantpid = NULL, *wantname = NULL;
	struct config_host_authinfo *get = NULL, *put = NULL;

	if (getp) {
		get = mymalloc(sizeof *get);
		memset(get, 0, sizeof *get);
	}
	if (putp) {
		put = mymalloc(sizeof *put);
		memset(put, 0, sizeof(*put));
	}
	/* Find the node name from its control net IP */
	res = mydb_query("SELECT node_id"
			 " FROM interfaces"
			 " WHERE IP='%s' AND role='ctrl'",
			 1, inet_ntoa(*in));
	assert(res != NULL);

	/* No such node */
	if (mysql_num_rows(res) == 0) {
		if (getp) *getp = get;
		if (putp) *putp = put;
		return 0;
	}

	row = mysql_fetch_row(res);
	if (!row[0]) {
		error("config_host_authinfo: null node_id!?");
		mysql_free_result(res);
		emulab_free_host_authinfo(get);
		emulab_free_host_authinfo(put);
		return 1;
	}
	node = mystrdup(row[0]);
	if (get != NULL)
		get->hostid = mystrdup(node);
	if (put != NULL)
		put->hostid = mystrdup(node);
	mysql_free_result(res);

	/* Find the pid/gid to which the node is currently assigned */
	res = mydb_query("SELECT e.pid,e.gid,r.eid,u.uid"
			 " FROM reserved AS r,experiments AS e,users AS u"
			 " WHERE r.pid=e.pid AND r.eid=e.eid"
			 "  AND e.swapper_idx=u.uid_idx"
			 "  AND r.node_id='%s'", 4, node);
	assert(res != NULL);

	/* Node is free */
	if (mysql_num_rows(res) == 0) {
		free(node);
		if (getp) *getp = get;
		if (putp) *putp = put;
		return 0;
	}

	row = mysql_fetch_row(res);
	if (!row[0] || !row[1] || !row[2] || !row[3]) {
		error("config_host_authinfo: null pid/gid/eid/uid!?");
		mysql_free_result(res);
		emulab_free_host_authinfo(get);
		emulab_free_host_authinfo(put);
		free(node);
		return 1;
	}

	if (get != NULL) {
		struct emulab_ha_extra_info *ei = mymalloc(sizeof *ei);
		ei->pid = mystrdup(row[0]);
		ei->gid = mystrdup(row[1]);
		ei->eid = mystrdup(row[2]);
		ei->suid = mystrdup(row[3]);
		get->extra = ei;
	}
	if (put != NULL) {
		struct emulab_ha_extra_info *ei = mymalloc(sizeof *ei);
		ei->pid = mystrdup(row[0]);
		ei->gid = mystrdup(row[1]);
		ei->eid = mystrdup(row[2]);
		ei->suid = mystrdup(row[3]);
		put->extra = ei;
	}
	mysql_free_result(res);

	/*
	 * If imageid is specified and starts with a '/', it is a path.
	 * In this case we compare that path to those accessible by
	 * the experiment swapper and pid/gid/eid.
	 */
	if (imageid && imageid[0] == '/') {
		allow_stddirs(imageid, get, put);
		goto done;
	}

	/*
	 * If we are interested in a specific image, extract the
	 * pid and imagename from the ID.
	 */
	if (imageid != NULL) {
		wantpid = mystrdup(imageid);
		wantname = index(wantpid, '/');
		if (wantname == NULL) {
			wantname = wantpid;
			wantpid = mystrdup("emulab-ops");
		} else {
			*wantname = '\0';
			wantname = mystrdup(wantname+1);
		}
	}

	if (put != NULL) {
		struct emulab_ha_extra_info *ei = put->extra;

		if (imageid != NULL) {
			/* Interested in a specific image */
			res = mydb_query("SELECT pid,gid,imagename,path,imageid"
					 " FROM images"
					 " WHERE pid='%s' AND gid='%s'"
					 "  AND pid='%s' AND imagename='%s'"
					 " ORDER BY pid,gid,imagename",
					 5, ei->pid, ei->gid,
					 wantpid, wantname);
		} else {
			/* Find all images that this pid/gid can PUT */
			res = mydb_query("SELECT pid,gid,imagename,path,imageid"
					 " FROM images"
					 " WHERE pid='%s' AND gid='%s'"
					 " ORDER BY pid,gid,imagename",
					 5, ei->pid, ei->gid);
		}

		/* Construct the list of "pid/imagename" imageids */
		nrows = mysql_num_rows(res);
		if (nrows)
			put->imageinfo =
				mymalloc(nrows *
					 sizeof(struct config_imageinfo));
		put->numimages = 0;
		for (i = 0; i < nrows; i++) {
			struct emulab_ii_extra_info *ii;
			char *imageid;

			row = mysql_fetch_row(res);
			/* XXX ignore rows with null or empty info */
			if (!row[0] || !row[0][0] ||
			    !row[1] || !row[1][0] ||
			    !row[2] || !row[2][0] ||
			    !row[3] || !row[3][0] ||
			    !row[4] || !row[4][0])
				continue;
			imageid = mymalloc(strlen(row[0])+strlen(row[2])+2);
			strcpy(imageid, row[0]);
			strcat(imageid, "/");
			strcat(imageid, row[2]);
			put->imageinfo[put->numimages].imageid = imageid;
			put->imageinfo[put->numimages].path = mystrdup(row[3]);
			put->imageinfo[put->numimages].flags =
				CONFIG_IMAGE_ISFILE;
			put->imageinfo[put->numimages].get_methods = 0;
			put->imageinfo[put->numimages].get_options = NULL;
			put->imageinfo[put->numimages].put_options = NULL;
			ii = mymalloc(sizeof *ii);
			ii->DB_imageid = atoi(row[4]);
			put->imageinfo[put->numimages].extra = ii;
			put->numimages++;
		}
		mysql_free_result(res);
	}

	if (get != NULL) {
		struct emulab_ha_extra_info *ei = get->extra;

		if (imageid != NULL) {
			/* Interested in a specific image */
			res = mydb_query("SELECT pid,gid,imagename,path,imageid"
					 " FROM images"
					 " WHERE (global=1"
					 "  OR (pid='%s' AND (gid='%s' OR shared=1)))"
					 "  AND pid='%s' AND imagename='%s'"
					 " ORDER BY pid,gid,imagename",
					 5, ei->pid, ei->gid,
					 wantpid, wantname);
		} else {
			/* Find all images that this pid/gid can GET */
			res = mydb_query("SELECT pid,gid,imagename,path,imageid"
					 " FROM images"
					 " WHERE (global=1"
					 "  OR (pid='%s' AND (gid='%s' OR shared=1)))"
					 " ORDER BY pid,gid,imagename",
					 5, ei->pid, ei->gid);
		}
		/* Construct the list of "pid/imagename" imageids */
		nrows = mysql_num_rows(res);
		if (nrows)
			get->imageinfo =
				mymalloc(nrows *
					 sizeof(struct config_imageinfo));
		get->numimages = 0;
		for (i = 0; i < nrows; i++) {
			struct emulab_ii_extra_info *ii;
			char *imageid;

			row = mysql_fetch_row(res);
			/* XXX ignore rows with null or empty info */
			if (!row[0] || !row[0][0] ||
			    !row[1] || !row[1][0] ||
			    !row[2] || !row[2][0] ||
			    !row[3] || !row[3][0] ||
			    !row[4] || !row[4][0])
				continue;
			imageid = mymalloc(strlen(row[0])+strlen(row[2])+2);
			strcpy(imageid, row[0]);
			strcat(imageid, "/");
			strcat(imageid, row[2]);
			get->imageinfo[get->numimages].imageid = imageid;
			get->imageinfo[get->numimages].path = mystrdup(row[3]);
			get->imageinfo[get->numimages].flags =
				CONFIG_IMAGE_ISFILE;
			set_get_methods(get, get->numimages);
			set_get_options(get, get->numimages);
			get->imageinfo[get->numimages].put_options = NULL;
			ii = mymalloc(sizeof *ii);
			ii->DB_imageid = atoi(row[4]);
			get->imageinfo[get->numimages].extra = ii;
			get->numimages++;
		}
		mysql_free_result(res);
	}

	/*
	 * Finally add on the standard directories that a node can access.
	 */
	allow_stddirs(NULL, get, put);

 done:
	free(node);
	if (wantpid)
		free(wantpid);
	if (wantname)
		free(wantname);
	if (getp) *getp = get;
	if (putp) *putp = put;
	return 0;
}

static void
dump_host_authinfo(FILE *fd, char *node, char *cmd,
		   struct config_host_authinfo *ai)
{
	struct emulab_ha_extra_info *ei = ai->extra;
	int i;

	if (strcmp(cmd, "GET") == 0)
		fprintf(fd, "# %s in %s/%s\n", node, ei->pid, ei->eid);
	fprintf(fd, "%s %s ", node, cmd);

	/*
	 * If we have defined image aliases, just dump the appropriate
	 * aliases here for each node.
	 */
	if (dump_doaliases) {
		if (strcmp(cmd, "GET") == 0)
			fprintf(fd, "global-images %s-images %s-%s-images ",
				ei->pid, ei->pid, ei->gid);
		else
			fprintf(fd, "%s-%s-images ", ei->pid, ei->gid);
	}
	/*
	 * Otherwise, dump the whole list of images for each node
	 */
	else {
		for (i = 0; i < ai->numimages; i++)
			if (ai->imageinfo[i].flags == CONFIG_IMAGE_ISFILE)
				fprintf(fd, "%s ", ai->imageinfo[i].imageid);
	}

	/*
	 * And dump any directories that can be accessed
	 */
	for (i = 0; i < ai->numimages; i++)
		if (ai->imageinfo[i].flags == CONFIG_IMAGE_ISDIR)
			fprintf(fd, "%s/* ", ai->imageinfo[i].path);

	fprintf(fd, "\n");
}

/*
 * Dump image alias lines for:
 * - global images (global=1)                     => "global-images"
 * - project-wide images (pid=<pid> and shared=1) => "<pid>-images"
 * - per-group images (pid=<pid> and gid=<gid>)   => "<pid>-<gid>-images"
 */
static void
dump_image_aliases(FILE *fd)
{
	MYSQL_RES	*res;
	MYSQL_ROW	row;
	int		i, nrows;
	char		*lastpid;

	/* First the global image alias */
	res = mydb_query("SELECT pid,imagename"
			 " FROM images"
			 " WHERE global=1"
			 " ORDER BY pid,imagename", 2);
	assert(res != NULL);

	nrows = mysql_num_rows(res);
	if (nrows) {
		fprintf(fd, "imagealias global-images ");
		for (i = 0; i < nrows; i++) {
			row = mysql_fetch_row(res);
			/* XXX ignore rows with null or empty info */
			if (!row[0] || !row[0][0] ||
			    !row[1] || !row[1][0])
				continue;
			fprintf(fd, "%s/%s ", row[0], row[1]);
		}
		fprintf(fd, "\n");
	}
	mysql_free_result(res);

	/* Only create aliases for pid/gids that have imageable nodes now */
	res = mydb_query("SELECT e.pid,e.gid"
			 " FROM nodes AS n,node_types AS t,"
			 "  node_type_attributes AS a,"
			 "  reserved AS r,experiments AS e"
			 " WHERE n.type=t.type AND n.type=a.type"
			 "  AND n.node_id=r.node_id"
			 "  AND r.pid=e.pid AND r.eid=e.eid"
			 "  AND n.role='testnode' AND t.class='pc'"
			 "  AND a.attrkey='imageable' AND a.attrvalue!='0'"
			 " GROUP by e.pid,e.gid", 2);
	assert(res != NULL);

	lastpid = strdup("NONE");
	nrows = mysql_num_rows(res);
	for (i = 0; i < nrows; i++) {
		MYSQL_RES	*res2;
		MYSQL_ROW	row2;
		int		i2, nrows2;

		row = mysql_fetch_row(res);
		/* XXX ignore rows with null or empty info */
		if (!row[0] || !row[0][0] ||
		    !row[1] || !row[1][0])
			continue;

		/* New pid, put out shared project image alias */
		if (strcmp(lastpid, row[0])) {
			free(lastpid);
			lastpid = strdup(row[0]);

			res2 = mydb_query("SELECT imagename"
					  " FROM images"
					  " WHERE pid='%s' AND shared=1"
					  " ORDER BY imagename",
					  1, lastpid);
			assert(res2 != NULL);

			nrows2 = mysql_num_rows(res2);
			if (nrows2) {
				fprintf(fd, "imagealias %s-images ", lastpid);
				for (i2 = 0; i2 < nrows2; i2++) {
					row2 = mysql_fetch_row(res2);
					if (!row2[0] || !row2[0][0])
						continue;
					fprintf(fd, "%s/%s ",
						lastpid, row2[0]);
				}
				fprintf(fd, "\n");
			}
			mysql_free_result(res2);
		}

		/* Put out pid/eid image alias */
		res2 = mydb_query("SELECT imagename"
				  " FROM images"
				  " WHERE pid='%s' AND gid='%s'"
				  " ORDER BY imagename",
				  1, row[0], row[1]);
		assert(res2 != NULL);

		nrows2 = mysql_num_rows(res2);
		if (nrows2) {
			fprintf(fd, "imagealias %s-%s-images ",
				row[0], row[1]);
			for (i2 = 0; i2 < nrows2; i2++) {
				row2 = mysql_fetch_row(res2);
				if (!row2[0] || !row2[0][0])
					continue;
				fprintf(fd, "%s/%s ", lastpid, row2[0]);
			}
			fprintf(fd, "\n");
		}
		mysql_free_result(res2);
	}
	mysql_free_result(res);
	free(lastpid);
}

static void
emulab_dump(FILE *fd)
{
	MYSQL_RES	*res;
	MYSQL_ROW	row;
	int		i, nrows;

	fprintf(fd, "Emulab master frisbee config:\n");

	if (dump_doaliases)
		dump_image_aliases(fd);

	res = mydb_query("SELECT n.node_id,i.IP"
			 " FROM nodes AS n,node_types AS t,"
			 "  node_type_attributes AS a,interfaces AS i"
			 " WHERE n.type=t.type AND n.type=a.type"
			 "  AND n.node_id=i.node_id AND i.role='ctrl'"
			 "  AND n.role='testnode' AND t.class='pc'"
			 "  AND a.attrkey='imageable' AND a.attrvalue!='0'"
			 " ORDER BY n.priority", 2);
	assert(res != NULL);

	nrows = mysql_num_rows(res);
	for (i = 0; i < nrows; i++) {
		row = mysql_fetch_row(res);
		if (row[0] && row[1]) {
			struct config_host_authinfo *get, *put;
			struct in_addr in;

			inet_aton(row[1], &in);
			if (emulab_get_host_authinfo(&in, NULL, &get, &put)) {
				warning("Could not get node authinfo for %s",
					row[0]);
			}
			if (strcmp(get->hostid, row[0])) {
				warning("node_id mismatch (%s != %s)",
					get->hostid, row[0]);
			}
			if (get->numimages)
				dump_host_authinfo(fd, row[0], "GET", get);
			emulab_free_host_authinfo(get);
			if (put->numimages)
				dump_host_authinfo(fd, row[0], "PUT", put);
			emulab_free_host_authinfo(put);
		}
	}
	mysql_free_result(res);
}

struct config emulab_config = {
	emulab_init,
	emulab_deinit,
	emulab_read,
	emulab_get_host_authinfo,
	emulab_free_host_authinfo,
	emulab_get_server_address,
	emulab_save,
	emulab_restore,
	emulab_free,
	emulab_dump
};

/*
 * XXX memory allocation functions that either return memory or abort.
 * We shouldn't run out of memory and don't want to check every return values.
 */
static void *
mymalloc(size_t size)
{
	void *ptr = malloc(size);
	if (ptr == NULL) {
		error("config_emulab: out of memory!");
		abort();
	}
	return ptr;
}

static void *
myrealloc(void *ptr, size_t size)
{
	void *nptr = realloc(ptr, size);
	if (nptr == NULL) {
		error("config_emulab: out of memory!");
		abort();
	}
	return nptr;
}

static char *
mystrdup(const char *str)
{
	char *nstr = strdup(str);
	if (nstr == NULL) {
		error("config_emulab: out of memory!");
		abort();
	}
	return nstr;
}

#if 0
/*
 * XXX Gak! Override some logging functions that the tbdb mysql code uses.
 * Otherwise we are going to have log messages going every which way.
 */
void
info(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	log_vwrite(LOG_INFO, fmt, args);
	va_end(args);
}

void
error(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	log_vwrite(LOG_ERR, fmt, args);
	va_end(args);
}

void
warning(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	log_vwrite(LOG_ERR, fmt, args);
	va_end(args);
}

#endif
#endif