xref: /netbsd-src/usr.sbin/rbootd/parseconf.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
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  *	from: @(#)parseconf.c	8.1 (Berkeley) 6/4/93
42  *	      $Id: parseconf.c,v 1.2 1994/01/11 16:41:33 brezak Exp $
43  *
44  * From: Utah Hdr: parseconf.c 3.1 92/07/06
45  * Author: Jeff Forys, University of Utah CSS
46  */
47 
48 #ifndef lint
49 /*static char sccsid[] = "@(#)parseconf.c	8.1 (Berkeley) 6/4/93";*/
50 static char rcsid[] = "$Id: parseconf.c,v 1.2 1994/01/11 16:41:33 brezak Exp $";
51 #endif /* not lint */
52 
53 #include <sys/param.h>
54 #include <sys/stat.h>
55 
56 #include <ctype.h>
57 #include <dirent.h>
58 #include <fcntl.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <syslog.h>
64 #include "defs.h"
65 
66 /*
67 **  ParseConfig -- parse the config file into linked list of clients.
68 **
69 **	Parameters:
70 **		None.
71 **
72 **	Returns:
73 **		1 on success, 0 otherwise.
74 **
75 **	Side Effects:
76 **		- Linked list of clients will be (re)allocated.
77 **
78 **	Warnings:
79 **		- GetBootFiles() must be called before this routine
80 **		  to create a linked list of default boot files.
81 */
82 int
83 ParseConfig()
84 {
85 	FILE *fp;
86 	CLIENT *client;
87 	u_char *addr;
88 	char line[C_LINELEN];
89 	register char *cp, *bcp;
90 	register int i, j;
91 	int omask, linecnt = 0;
92 
93 	if (BootAny)				/* ignore config file */
94 		return(1);
95 
96 	FreeClients();				/* delete old list of clients */
97 
98 	if ((fp = fopen(ConfigFile, "r")) == NULL) {
99 		syslog(LOG_ERR, "ParseConfig: can't open config file (%s)",
100 		       ConfigFile);
101 		return(0);
102 	}
103 
104 	/*
105 	 *  We've got to block SIGHUP to prevent reconfiguration while
106 	 *  dealing with the linked list of Clients.  This can be done
107 	 *  when actually linking the new client into the list, but
108 	 *  this could have unexpected results if the server was HUP'd
109 	 *  whilst reconfiguring.  Hence, it is done here.
110 	 */
111 	omask = sigblock(sigmask(SIGHUP));
112 
113 	/*
114 	 *  GETSTR positions `bcp' at the start of the current token,
115 	 *  and null terminates it.  `cp' is positioned at the start
116 	 *  of the next token.  spaces & commas are separators.
117 	 */
118 #define GETSTR	while (isspace(*cp) || *cp == ',') cp++;	\
119 		bcp = cp;					\
120 		while (*cp && *cp!=',' && !isspace(*cp)) cp++;	\
121 		if (*cp) *cp++ = '\0'
122 
123 	/*
124 	 *  For each line, parse it into a new CLIENT struct.
125 	 */
126 	while (fgets(line, C_LINELEN, fp) != NULL) {
127 		linecnt++;				/* line counter */
128 
129 		if (*line == '\0' || *line == '#')	/* ignore comment */
130 			continue;
131 
132 		if ((cp = index(line,'#')) != NULL)	/* trash comments */
133 			*cp = '\0';
134 
135 		cp = line;				/* init `cp' */
136 		GETSTR;					/* get RMP addr */
137 		if (bcp == cp)				/* all delimiters */
138 			continue;
139 
140 		/*
141 		 *  Get an RMP address from a string.  Abort on failure.
142 		 */
143 		if ((addr = ParseAddr(bcp)) == NULL) {
144 			syslog(LOG_ERR,
145 			       "ParseConfig: line %d: cant parse <%s>",
146 			       linecnt, bcp);
147 			continue;
148 		}
149 
150 		if ((client = NewClient(addr)) == NULL)	/* alloc new client */
151 			continue;
152 
153 		GETSTR;					/* get first file */
154 
155 		/*
156 		 *  If no boot files are spec'd, use the default list.
157 		 *  Otherwise, validate each file (`bcp') against the
158 		 *  list of boot-able files.
159 		 */
160 		i = 0;
161 		if (bcp == cp)				/* no files spec'd */
162 			for (; i < C_MAXFILE && BootFiles[i] != NULL; i++)
163 				client->files[i] = BootFiles[i];
164 		else {
165 			do {
166 				/*
167 				 *  For each boot file spec'd, make sure it's
168 				 *  in our list.  If so, include a pointer to
169 				 *  it in the CLIENT's list of boot files.
170 				 */
171 				for (j = 0; ; j++) {
172 					if (j==C_MAXFILE||BootFiles[j]==NULL) {
173 						syslog(LOG_ERR, "ParseConfig: line %d: no boot file (%s)",
174 						       linecnt, bcp);
175 						break;
176 					}
177 					if (STREQN(BootFiles[j], bcp)) {
178 						if (i < C_MAXFILE)
179 							client->files[i++] =
180 							    BootFiles[j];
181 						else
182 							syslog(LOG_ERR, "ParseConfig: line %d: too many boot files (%s)",
183 							       linecnt, bcp);
184 						break;
185 					}
186 				}
187 				GETSTR;			/* get next file */
188 			} while (bcp != cp);
189 
190 			/*
191 			 *  Restricted list of boot files were spec'd,
192 			 *  however, none of them were found.  Since we
193 			 *  apparently cant let them boot "just anything",
194 			 *  the entire record is invalidated.
195 			 */
196 			if (i == 0) {
197 				FreeClient(client);
198 				continue;
199 			}
200 		}
201 
202 		/*
203 		 *  Link this client into the linked list of clients.
204 		 *  SIGHUP has already been blocked.
205 		 */
206 		if (Clients)
207 			client->next = Clients;
208 		Clients = client;
209 	}
210 
211 	(void) fclose(fp);				/* close config file */
212 
213 	(void) sigsetmask(omask);			/* reset signal mask */
214 
215 	return(1);					/* return success */
216 }
217 
218 /*
219 **  ParseAddr -- Parse a string containing an RMP address.
220 **
221 **	This routine is fairly liberal at parsing an RMP address.  The
222 **	address must contain 6 octets consisting of between 0 and 2 hex
223 **	chars (upper/lower case) separated by colons.  If two colons are
224 **	together (e.g. "::", the octet between them is recorded as being
225 **	zero.  Hence, the following addrs are all valid and parse to the
226 **	same thing:
227 **
228 **		08:00:09:00:66:ad	8::9:0:66:AD	8::9::66:aD
229 **
230 **	For clarity, an RMP address is really an Ethernet address, but
231 **	since the HP boot code uses IEEE 802.3, it's really an IEEE
232 **	802.3 address.  Of course, all of these are identical.
233 **
234 **	Parameters:
235 **		str - string representation of an RMP address.
236 **
237 **	Returns:
238 **		pointer to a static array of RMP_ADDRLEN bytes.
239 **
240 **	Side Effects:
241 **		None.
242 **
243 **	Warnings:
244 **		- The return value points to a static buffer; it must
245 **		  be copied if it's to be saved.
246 **		- For speed, we assume a u_char consists of 8 bits.
247 */
248 u_char *
249 ParseAddr(str)
250 	char *str;
251 {
252 	static u_char addr[RMP_ADDRLEN];
253 	register char *cp;
254 	register unsigned i;
255 	register int part, subpart;
256 
257 	bzero((char *)&addr[0], RMP_ADDRLEN);	/* zero static buffer */
258 
259 	part = subpart = 0;
260 	for (cp = str; *cp; cp++) {
261 		/*
262 		 *  A colon (`:') must be used to delimit each octet.
263 		 */
264 		if (*cp == ':') {
265 			if (++part == RMP_ADDRLEN)	/* too many parts */
266 				return(NULL);
267 			subpart = 0;
268 			continue;
269 		}
270 
271 		/*
272 		 *  Convert hex character to an integer.
273 		 */
274 		if (isdigit(*cp))
275 			i = *cp - '0';
276 		else {
277 			i = (isupper(*cp)? tolower(*cp): *cp) - 'a' + 10;
278 			if (i < 10 || i > 15)		/* not a hex char */
279 				return(NULL);
280 		}
281 
282 		if (subpart++) {
283 			if (subpart > 2)		/* too many hex chars */
284 				return(NULL);
285 			addr[part] <<= 4;
286 		}
287 		addr[part] |= i;
288 	}
289 
290 	if (part != (RMP_ADDRLEN-1))			/* too few parts */
291 		return(NULL);
292 
293 	return(&addr[0]);
294 }
295 
296 /*
297 **  GetBootFiles -- record list of files in current (boot) directory.
298 **
299 **	Parameters:
300 **		None.
301 **
302 **	Returns:
303 **		Number of boot files on success, 0 on failure.
304 **
305 **	Side Effects:
306 **		Strings in `BootFiles' are freed/allocated.
307 **
308 **	Warnings:
309 **		- After this routine is called, ParseConfig() must be
310 **		  called to re-order it's list of boot file pointers.
311 */
312 int
313 GetBootFiles()
314 {
315 	DIR *dfd;
316 	struct stat statb;
317 	register struct dirent *dp;
318 	register int i;
319 
320 	/*
321 	 *  Free the current list of boot files.
322 	 */
323 	for (i = 0; i < C_MAXFILE && BootFiles[i] != NULL; i++) {
324 		FreeStr(BootFiles[i]);
325 		BootFiles[i] = NULL;
326 	}
327 
328 	/*
329 	 *  Open current directory to read boot file names.
330 	 */
331 	if ((dfd = opendir(".")) == NULL) {	/* open BootDir */
332 		syslog(LOG_ERR, "GetBootFiles: can't open directory (%s)\n",
333 		       BootDir);
334 		return(0);
335 	}
336 
337 	/*
338 	 *  Read each boot file name and allocate space for it in the
339 	 *  list of boot files (BootFiles).  All boot files read after
340 	 *  C_MAXFILE will be ignored.
341 	 */
342 	i = 0;
343 	for (dp = readdir(dfd); dp != NULL; dp = readdir(dfd)) {
344 		if (stat(dp->d_name, &statb) < 0 ||
345 		    (statb.st_mode & S_IFMT) != S_IFREG)
346 			continue;
347 		if (i == C_MAXFILE)
348 			syslog(LOG_ERR,
349 			       "GetBootFiles: too many boot files (%s ignored)",
350 			       dp->d_name);
351 		else if ((BootFiles[i] = NewStr(dp->d_name)) != NULL)
352 			i++;
353 	}
354 
355 	(void) closedir(dfd);			/* close BootDir */
356 
357 	if (i == 0)				/* cant find any boot files */
358 		syslog(LOG_ERR, "GetBootFiles: no boot files (%s)\n", BootDir);
359 
360 	return(i);
361 }
362