bootinfo_cfile.c 5.57 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1
/*
2
 * Copyright (c) 2000-2014 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 26 27 28 29 30
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <syslog.h>
31
#include "log.h"
32
#include "bootwhat.h"
33
#include "bootinfo.h"
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

/*
 * Trivial config file format.
 * Swiped from proxydhcp.c.
 */
#ifdef USE_CFILE_DB

#define CFILE	"bootinfo.conf"

struct config {
	struct in_addr	client;
	int		bootinfolen;
	boot_what_t	bootinfo;
	/* bootinfo is variable length */
};

#define MAX_CONFIGS	256
static int		numconfigs;
static struct config	*configurations[MAX_CONFIGS];
static struct config	*find_config(struct in_addr addr);
static int		parse_configs(char *filename);
#ifdef TEST
static void		print_bootwhat(boot_what_t *bootinfo);
#endif

int
open_bootinfo_db(void)
{
62 63 64 65 66 67 68 69 70
	char	cfile[BUFSIZ];
	
#ifdef TEST
	strcpy(cfile, CFILE);
#else
	strcpy(cfile, CONFPATH);
	strcat(cfile, CFILE);
#endif
	return parse_configs(cfile);
71 72 73
}

int
74
query_bootinfo_db(struct in_addr ipaddr, int version, boot_what_t *info, char *key)
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
{
	struct config *configp;

	if ((configp = find_config(ipaddr)) == 0)
		return 1;

	memcpy(info, &configp->bootinfo, configp->bootinfolen);
	return 0;
}

int
close_bootinfo_db(void)
{
	int i;

	for (i = 0; i < numconfigs; i++)
		if (configurations[i])
			free(configurations[i]);
	numconfigs = 0;
	return 0;
}

static int
parse_host(char *name)
{
	struct hostent *he;

	if (!isdigit(name[0])) {
		he = gethostbyname(name);
		if (he == 0) {
105
			error("%s: unknown host\n", name);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
			return 0;
		}
		return *(int *)he->h_addr;
	}
	return inet_addr(name);
}


static int
parse_configs(char *filename)
{
	FILE	*fp;
	int	i;
	char	buf[BUFSIZ], *bp, *cix, client[256], action[256];
	struct  config *configp;
	int	ipaddr;

	if ((fp = fopen(filename, "r")) == NULL) {
124
		error("%s: cannot open\n", filename);
125 126 127 128 129 130 131 132 133 134 135
		return 1;
	}

	while (1) {
		if ((bp = fgets(buf, BUFSIZ, fp)) == NULL)
			break;

		if (*bp == '\n' || *bp == '#')
			continue;

		if (numconfigs >= MAX_CONFIGS) {
136
			error("%s: too many lines\n", filename);
137 138 139 140 141 142 143 144 145
			fclose(fp);
			return 1;
		}

		sscanf(bp, "%s %s\n", client, action);

		configp = (struct config *) calloc(sizeof *configp, 1);
		if (!configp) {
		bad:
146
			error("%s: parse error\n", filename);
147 148 149 150 151 152 153 154 155 156
			fclose(fp);
			close_bootinfo_db();
			return 1;
		}
		configurations[numconfigs++] = configp;
		if ((ipaddr = parse_host(client)) == 0)
			goto bad;

		configp->client.s_addr = ipaddr;
		configp->bootinfolen = sizeof *configp;
157
		configp->bootinfo.flags = 0;
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
		if (strncmp(action, "sysid=", 6) == 0) {
			configp->bootinfo.type = BIBOOTWHAT_TYPE_SYSID;
			configp->bootinfo.what.sysid = atoi(&action[6]);
		} else if (strncmp(action, "part=", 5) == 0) {
			configp->bootinfo.type = BIBOOTWHAT_TYPE_PART;
			configp->bootinfo.what.partition = atoi(&action[5]);
		} else if (strncmp(action, "file=", 5) == 0) {
			if ((cix = index(action, ':')) == 0)
				goto bad;
			configp->bootinfo.type = BIBOOTWHAT_TYPE_MB;
			*cix++ = 0;
			if ((ipaddr = parse_host(&action[5])) == 0)
				goto bad;
			configp->bootinfo.what.mb.tftp_ip.s_addr = ipaddr;
			configp = realloc(configp,
					  sizeof *configp + strlen(cix) + 1);
			if (configp == 0)
				goto bad;
				
			configurations[numconfigs-1] = configp;
			configp->bootinfolen += strlen(cix) + 1;
			strcpy(configp->bootinfo.what.mb.filename, cix);
		} else
			goto bad;
	}
	fclose(fp);

#ifdef TEST
	printf("==== %s ====\n", filename);
	for (i = 0; i < numconfigs; i++) {
		configp = configurations[i];
		printf("%s: ", inet_ntoa(configp->client));
		print_bootwhat(&configp->bootinfo);
	}
	printf("====\n\n");
#endif

	return 0;
}

static struct config *
find_config(struct in_addr addr)
{
	int		i;
	struct config	*configp;
	
	for (i = 0; i < numconfigs; i++)
		if ((configp = configurations[i]) != NULL &&
		    addr.s_addr == configp->client.s_addr)
			return configp;

	return 0;
}

#ifdef TEST
#include <stdarg.h>

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;
	}
}

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;

	if (open_bootinfo_db() != 0) {
		printf("bad config file\n");
		exit(1);
	}

	while (--argc > 0) {
		if (inet_aton(*++argv, &ipaddr)) {
			if (query_bootinfo_db(ipaddr, boot_whatp) == 0) {
				printf("%s: ", *argv);
				print_bootwhat(boot_whatp);
			} else
				printf("failed\n");
		} else
			printf("bogus IP address `%s'\n", *argv);
	}
	close_bootinfo_db();
	exit(0);
}
#endif
#endif