xref: /openbsd/usr.sbin/rbootd/parseconf.c (revision 0ac0d02e)
1 /*	$OpenBSD: parseconf.c,v 1.6 2002/03/14 16:44:25 mpech Exp $	*/
2 /*	$NetBSD: parseconf.c,v 1.4 1995/10/06 05:12:16 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1988, 1992 The University of Utah and the Center
6  *	for Software Science (CSS).
7  * Copyright (c) 1992, 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * the Center for Software Science of the University of Utah Computer
12  * Science Department.  CSS requests users of this software to return
13  * to css-dist@cs.utah.edu any improvements that they make and grant
14  * CSS redistribution rights.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	from: @(#)parseconf.c	8.1 (Berkeley) 6/4/93
45  *
46  * From: Utah Hdr: parseconf.c 3.1 92/07/06
47  * Author: Jeff Forys, University of Utah CSS
48  */
49 
50 #ifndef lint
51 /*static char sccsid[] = "@(#)parseconf.c	8.1 (Berkeley) 6/4/93";*/
52 static char rcsid[] = "$OpenBSD: parseconf.c,v 1.6 2002/03/14 16:44:25 mpech Exp $";
53 #endif /* not lint */
54 
55 #include <sys/param.h>
56 #include <sys/stat.h>
57 
58 #include <ctype.h>
59 #include <dirent.h>
60 #include <fcntl.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <syslog.h>
66 #include "defs.h"
67 
68 /*
69 **  ParseConfig -- parse the config file into linked list of clients.
70 **
71 **	Parameters:
72 **		None.
73 **
74 **	Returns:
75 **		1 on success, 0 otherwise.
76 **
77 **	Side Effects:
78 **		- Linked list of clients will be (re)allocated.
79 **
80 **	Warnings:
81 **		- GetBootFiles() must be called before this routine
82 **		  to create a linked list of default boot files.
83 */
84 int
85 ParseConfig()
86 {
87 	FILE *fp;
88 	CLIENT *client;
89 	u_int8_t *addr;
90 	char line[C_LINELEN];
91 	char *cp, *bcp;
92 	int i, j;
93 	int linecnt = 0;
94 	sigset_t mask, omask;
95 
96 	if (BootAny)				/* ignore config file */
97 		return(1);
98 
99 	FreeClients();				/* delete old list of clients */
100 
101 	if ((fp = fopen(ConfigFile, "r")) == NULL) {
102 		syslog(LOG_ERR, "ParseConfig: can't open config file (%s)",
103 		       ConfigFile);
104 		return(0);
105 	}
106 
107 	/*
108 	 *  We've got to block SIGHUP to prevent reconfiguration while
109 	 *  dealing with the linked list of Clients.  This can be done
110 	 *  when actually linking the new client into the list, but
111 	 *  this could have unexpected results if the server was HUP'd
112 	 *  whilst reconfiguring.  Hence, it is done here.
113 	 */
114 	sigemptyset(&mask);
115 	sigaddset(&mask, SIGHUP);
116 	sigprocmask(SIG_BLOCK, &mask, &omask);
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 	sigprocmask(SIG_SETMASK, &omask, NULL);		/* 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 int i;
259 	int part, subpart;
260 
261 	bzero((char *)&addr[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)",
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 ||
349 		    (statb.st_mode & S_IFMT) != S_IFREG)
350 			continue;
351 		if (i == C_MAXFILE)
352 			syslog(LOG_ERR,
353 			       "GetBootFiles: too many boot files (%s ignored)",
354 			       dp->d_name);
355 		else if ((BootFiles[i] = NewStr(dp->d_name)) != NULL)
356 			i++;
357 	}
358 
359 	(void) closedir(dfd);			/* close BootDir */
360 
361 	if (i == 0)				/* cant find any boot files */
362 		syslog(LOG_ERR, "GetBootFiles: no boot files (%s)", BootDir);
363 
364 	return(i);
365 }
366