Home | History | Annotate | Line # | Download | only in rbootd
parseconf.c revision 1.7
      1 /*	$NetBSD: parseconf.c,v 1.7 1997/10/19 19:43:45 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1992 The University of Utah and the Center
      5  *	for Software Science (CSS).
      6  * Copyright (c) 1992, 1993
      7  *	The Regents of the University of California.  All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * the Center for Software Science of the University of Utah Computer
     11  * Science Department.  CSS requests users of this software to return
     12  * to css-dist (at) cs.utah.edu any improvements that they make and grant
     13  * CSS redistribution rights.
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  * 3. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *	This product includes software developed by the University of
     26  *	California, Berkeley and its contributors.
     27  * 4. Neither the name of the University nor the names of its contributors
     28  *    may be used to endorse or promote products derived from this software
     29  *    without specific prior written permission.
     30  *
     31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     41  * SUCH DAMAGE.
     42  *
     43  *	from: @(#)parseconf.c	8.1 (Berkeley) 6/4/93
     44  *
     45  * From: Utah Hdr: parseconf.c 3.1 92/07/06
     46  * Author: Jeff Forys, University of Utah CSS
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 #ifndef lint
     51 #if 0
     52 static char sccsid[] = "@(#)parseconf.c	8.1 (Berkeley) 6/4/93";
     53 #else
     54 __RCSID("$NetBSD: parseconf.c,v 1.7 1997/10/19 19:43:45 mycroft Exp $");
     55 #endif
     56 #endif /* not lint */
     57 
     58 #include <sys/param.h>
     59 #include <sys/stat.h>
     60 
     61 #include <ctype.h>
     62 #include <dirent.h>
     63 #include <fcntl.h>
     64 #include <signal.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <syslog.h>
     69 #include "defs.h"
     70 
     71 /*
     72 **  ParseConfig -- parse the config file into linked list of clients.
     73 **
     74 **	Parameters:
     75 **		None.
     76 **
     77 **	Returns:
     78 **		1 on success, 0 otherwise.
     79 **
     80 **	Side Effects:
     81 **		- Linked list of clients will be (re)allocated.
     82 **
     83 **	Warnings:
     84 **		- GetBootFiles() must be called before this routine
     85 **		  to create a linked list of default boot files.
     86 */
     87 int
     88 ParseConfig()
     89 {
     90 	FILE *fp;
     91 	CLIENT *client;
     92 	u_int8_t *addr;
     93 	char line[C_LINELEN];
     94 	char *cp, *bcp;
     95 	int i, j;
     96 	int omask, linecnt = 0;
     97 
     98 	if (BootAny)				/* ignore config file */
     99 		return(1);
    100 
    101 	FreeClients();				/* delete old list of clients */
    102 
    103 	if ((fp = fopen(ConfigFile, "r")) == NULL) {
    104 		syslog(LOG_ERR, "ParseConfig: can't open config file (%s)",
    105 		       ConfigFile);
    106 		return(0);
    107 	}
    108 
    109 	/*
    110 	 *  We've got to block SIGHUP to prevent reconfiguration while
    111 	 *  dealing with the linked list of Clients.  This can be done
    112 	 *  when actually linking the new client into the list, but
    113 	 *  this could have unexpected results if the server was HUP'd
    114 	 *  whilst reconfiguring.  Hence, it is done here.
    115 	 */
    116 	omask = sigblock(sigmask(SIGHUP));
    117 
    118 	/*
    119 	 *  GETSTR positions `bcp' at the start of the current token,
    120 	 *  and null terminates it.  `cp' is positioned at the start
    121 	 *  of the next token.  spaces & commas are separators.
    122 	 */
    123 #define GETSTR	while (isspace(*cp) || *cp == ',') cp++;	\
    124 		bcp = cp;					\
    125 		while (*cp && *cp!=',' && !isspace(*cp)) cp++;	\
    126 		if (*cp) *cp++ = '\0'
    127 
    128 	/*
    129 	 *  For each line, parse it into a new CLIENT struct.
    130 	 */
    131 	while (fgets(line, C_LINELEN, fp) != NULL) {
    132 		linecnt++;				/* line counter */
    133 
    134 		if (*line == '\0' || *line == '#')	/* ignore comment */
    135 			continue;
    136 
    137 		if ((cp = strchr(line,'#')) != NULL)	/* trash comments */
    138 			*cp = '\0';
    139 
    140 		cp = line;				/* init `cp' */
    141 		GETSTR;					/* get RMP addr */
    142 		if (bcp == cp)				/* all delimiters */
    143 			continue;
    144 
    145 		/*
    146 		 *  Get an RMP address from a string.  Abort on failure.
    147 		 */
    148 		if ((addr = ParseAddr(bcp)) == NULL) {
    149 			syslog(LOG_ERR,
    150 			       "ParseConfig: line %d: cant parse <%s>",
    151 			       linecnt, bcp);
    152 			continue;
    153 		}
    154 
    155 		if ((client = NewClient(addr)) == NULL)	/* alloc new client */
    156 			continue;
    157 
    158 		GETSTR;					/* get first file */
    159 
    160 		/*
    161 		 *  If no boot files are spec'd, use the default list.
    162 		 *  Otherwise, validate each file (`bcp') against the
    163 		 *  list of boot-able files.
    164 		 */
    165 		i = 0;
    166 		if (bcp == cp)				/* no files spec'd */
    167 			for (; i < C_MAXFILE && BootFiles[i] != NULL; i++)
    168 				client->files[i] = BootFiles[i];
    169 		else {
    170 			do {
    171 				/*
    172 				 *  For each boot file spec'd, make sure it's
    173 				 *  in our list.  If so, include a pointer to
    174 				 *  it in the CLIENT's list of boot files.
    175 				 */
    176 				for (j = 0; ; j++) {
    177 					if (j==C_MAXFILE||BootFiles[j]==NULL) {
    178 						syslog(LOG_ERR, "ParseConfig: line %d: no boot file (%s)",
    179 						       linecnt, bcp);
    180 						break;
    181 					}
    182 					if (STREQN(BootFiles[j], bcp)) {
    183 						if (i < C_MAXFILE)
    184 							client->files[i++] =
    185 							    BootFiles[j];
    186 						else
    187 							syslog(LOG_ERR, "ParseConfig: line %d: too many boot files (%s)",
    188 							       linecnt, bcp);
    189 						break;
    190 					}
    191 				}
    192 				GETSTR;			/* get next file */
    193 			} while (bcp != cp);
    194 
    195 			/*
    196 			 *  Restricted list of boot files were spec'd,
    197 			 *  however, none of them were found.  Since we
    198 			 *  apparently cant let them boot "just anything",
    199 			 *  the entire record is invalidated.
    200 			 */
    201 			if (i == 0) {
    202 				FreeClient(client);
    203 				continue;
    204 			}
    205 		}
    206 
    207 		/*
    208 		 *  Link this client into the linked list of clients.
    209 		 *  SIGHUP has already been blocked.
    210 		 */
    211 		if (Clients)
    212 			client->next = Clients;
    213 		Clients = client;
    214 	}
    215 
    216 	(void) fclose(fp);				/* close config file */
    217 
    218 	(void) sigsetmask(omask);			/* reset signal mask */
    219 
    220 	return(1);					/* return success */
    221 }
    222 
    223 /*
    224 **  ParseAddr -- Parse a string containing an RMP address.
    225 **
    226 **	This routine is fairly liberal at parsing an RMP address.  The
    227 **	address must contain 6 octets consisting of between 0 and 2 hex
    228 **	chars (upper/lower case) separated by colons.  If two colons are
    229 **	together (e.g. "::", the octet between them is recorded as being
    230 **	zero.  Hence, the following addrs are all valid and parse to the
    231 **	same thing:
    232 **
    233 **		08:00:09:00:66:ad	8::9:0:66:AD	8::9::66:aD
    234 **
    235 **	For clarity, an RMP address is really an Ethernet address, but
    236 **	since the HP boot code uses IEEE 802.3, it's really an IEEE
    237 **	802.3 address.  Of course, all of these are identical.
    238 **
    239 **	Parameters:
    240 **		str - string representation of an RMP address.
    241 **
    242 **	Returns:
    243 **		pointer to a static array of RMP_ADDRLEN bytes.
    244 **
    245 **	Side Effects:
    246 **		None.
    247 **
    248 **	Warnings:
    249 **		- The return value points to a static buffer; it must
    250 **		  be copied if it's to be saved.
    251 */
    252 u_int8_t *
    253 ParseAddr(str)
    254 	char *str;
    255 {
    256 	static u_int8_t addr[RMP_ADDRLEN];
    257 	char *cp;
    258 	unsigned i;
    259 	int part, subpart;
    260 
    261 	memset((char *)&addr[0], 0, RMP_ADDRLEN);	/* zero static buffer */
    262 
    263 	part = subpart = 0;
    264 	for (cp = str; *cp; cp++) {
    265 		/*
    266 		 *  A colon (`:') must be used to delimit each octet.
    267 		 */
    268 		if (*cp == ':') {
    269 			if (++part == RMP_ADDRLEN)	/* too many parts */
    270 				return(NULL);
    271 			subpart = 0;
    272 			continue;
    273 		}
    274 
    275 		/*
    276 		 *  Convert hex character to an integer.
    277 		 */
    278 		if (isdigit(*cp))
    279 			i = *cp - '0';
    280 		else {
    281 			i = (isupper(*cp)? tolower(*cp): *cp) - 'a' + 10;
    282 			if (i < 10 || i > 15)		/* not a hex char */
    283 				return(NULL);
    284 		}
    285 
    286 		if (subpart++) {
    287 			if (subpart > 2)		/* too many hex chars */
    288 				return(NULL);
    289 			addr[part] <<= 4;
    290 		}
    291 		addr[part] |= i;
    292 	}
    293 
    294 	if (part != (RMP_ADDRLEN-1))			/* too few parts */
    295 		return(NULL);
    296 
    297 	return(&addr[0]);
    298 }
    299 
    300 /*
    301 **  GetBootFiles -- record list of files in current (boot) directory.
    302 **
    303 **	Parameters:
    304 **		None.
    305 **
    306 **	Returns:
    307 **		Number of boot files on success, 0 on failure.
    308 **
    309 **	Side Effects:
    310 **		Strings in `BootFiles' are freed/allocated.
    311 **
    312 **	Warnings:
    313 **		- After this routine is called, ParseConfig() must be
    314 **		  called to re-order it's list of boot file pointers.
    315 */
    316 int
    317 GetBootFiles()
    318 {
    319 	DIR *dfd;
    320 	struct stat statb;
    321 	struct dirent *dp;
    322 	int i;
    323 
    324 	/*
    325 	 *  Free the current list of boot files.
    326 	 */
    327 	for (i = 0; i < C_MAXFILE && BootFiles[i] != NULL; i++) {
    328 		FreeStr(BootFiles[i]);
    329 		BootFiles[i] = NULL;
    330 	}
    331 
    332 	/*
    333 	 *  Open current directory to read boot file names.
    334 	 */
    335 	if ((dfd = opendir(".")) == NULL) {	/* open BootDir */
    336 		syslog(LOG_ERR, "GetBootFiles: can't open directory (%s)\n",
    337 		       BootDir);
    338 		return(0);
    339 	}
    340 
    341 	/*
    342 	 *  Read each boot file name and allocate space for it in the
    343 	 *  list of boot files (BootFiles).  All boot files read after
    344 	 *  C_MAXFILE will be ignored.
    345 	 */
    346 	i = 0;
    347 	for (dp = readdir(dfd); dp != NULL; dp = readdir(dfd)) {
    348 		if (stat(dp->d_name, &statb) < 0 || !S_ISREG(statb.st_mode))
    349 			continue;
    350 		if (i == C_MAXFILE)
    351 			syslog(LOG_ERR,
    352 			       "GetBootFiles: too many boot files (%s ignored)",
    353 			       dp->d_name);
    354 		else if ((BootFiles[i] = NewStr(dp->d_name)) != NULL)
    355 			i++;
    356 	}
    357 
    358 	(void) closedir(dfd);			/* close BootDir */
    359 
    360 	if (i == 0)				/* cant find any boot files */
    361 		syslog(LOG_ERR, "GetBootFiles: no boot files (%s)\n", BootDir);
    362 
    363 	return(i);
    364 }
    365