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