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