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