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