Unbreak buildkernel.
[dragonfly.git] / libexec / rbootd / parseconf.c
blob3c807fcedca135889ee4f79f6589589113d9c6b4
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.
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.
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. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
37 * from: @(#)parseconf.c 8.1 (Berkeley) 6/4/93
39 * From: Utah Hdr: parseconf.c 3.1 92/07/06
40 * Author: Jeff Forys, University of Utah CSS
42 * @(#)parseconf.c 8.1 (Berkeley) 6/4/93
43 * $FreeBSD: src/libexec/rbootd/parseconf.c,v 1.9.2.1 2001/03/05 11:17:52 kris Exp $
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
50 #include <ctype.h>
51 #include <dirent.h>
52 #include <fcntl.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <syslog.h>
58 #include "defs.h"
61 ** ParseConfig -- parse the config file into linked list of clients.
63 ** Parameters:
64 ** None.
66 ** Returns:
67 ** 1 on success, 0 otherwise.
69 ** Side Effects:
70 ** - Linked list of clients will be (re)allocated.
72 ** Warnings:
73 ** - GetBootFiles() must be called before this routine
74 ** to create a linked list of default boot files.
76 int
77 ParseConfig(void)
79 FILE *fp;
80 CLIENT *client;
81 u_int8_t *addr;
82 char line[C_LINELEN];
83 char *cp, *bcp;
84 int i, j;
85 int omask, linecnt = 0;
87 if (BootAny) /* ignore config file */
88 return(1);
90 FreeClients(); /* delete old list of clients */
92 if ((fp = fopen(ConfigFile, "r")) == NULL) {
93 syslog(LOG_ERR, "ParseConfig: can't open config file (%s)",
94 ConfigFile);
95 return(0);
99 * We've got to block SIGHUP to prevent reconfiguration while
100 * dealing with the linked list of Clients. This can be done
101 * when actually linking the new client into the list, but
102 * this could have unexpected results if the server was HUP'd
103 * whilst reconfiguring. Hence, it is done here.
105 omask = sigblock(sigmask(SIGHUP));
108 * GETSTR positions `bcp' at the start of the current token,
109 * and null terminates it. `cp' is positioned at the start
110 * of the next token. spaces & commas are separators.
112 #define GETSTR while (isspace(*cp) || *cp == ',') cp++; \
113 bcp = cp; \
114 while (*cp && *cp!=',' && !isspace(*cp)) cp++; \
115 if (*cp) *cp++ = '\0'
118 * For each line, parse it into a new CLIENT struct.
120 while (fgets(line, C_LINELEN, fp) != NULL) {
121 linecnt++; /* line counter */
123 if (*line == '\0' || *line == '#') /* ignore comment */
124 continue;
126 if ((cp = strchr(line,'#')) != NULL) /* trash comments */
127 *cp = '\0';
129 cp = line; /* init `cp' */
130 GETSTR; /* get RMP addr */
131 if (bcp == cp) /* all delimiters */
132 continue;
135 * Get an RMP address from a string. Abort on failure.
137 if ((addr = ParseAddr(bcp)) == NULL) {
138 syslog(LOG_ERR,
139 "ParseConfig: line %d: can't parse <%s>",
140 linecnt, bcp);
141 continue;
144 if ((client = NewClient(addr)) == NULL) /* alloc new client */
145 continue;
147 GETSTR; /* get first file */
150 * If no boot files are spec'd, use the default list.
151 * Otherwise, validate each file (`bcp') against the
152 * list of boot-able files.
154 i = 0;
155 if (bcp == cp) /* no files spec'd */
156 for (; i < C_MAXFILE && BootFiles[i] != NULL; i++)
157 client->files[i] = BootFiles[i];
158 else {
159 do {
161 * For each boot file spec'd, make sure it's
162 * in our list. If so, include a pointer to
163 * it in the CLIENT's list of boot files.
165 for (j = 0; ; j++) {
166 if (j==C_MAXFILE||BootFiles[j]==NULL) {
167 syslog(LOG_ERR, "ParseConfig: line %d: no boot file (%s)",
168 linecnt, bcp);
169 break;
171 if (STREQN(BootFiles[j], bcp)) {
172 if (i < C_MAXFILE)
173 client->files[i++] =
174 BootFiles[j];
175 else
176 syslog(LOG_ERR, "ParseConfig: line %d: too many boot files (%s)",
177 linecnt, bcp);
178 break;
181 GETSTR; /* get next file */
182 } while (bcp != cp);
185 * Restricted list of boot files were spec'd,
186 * however, none of them were found. Since we
187 * apparently cant let them boot "just anything",
188 * the entire record is invalidated.
190 if (i == 0) {
191 FreeClient(client);
192 continue;
197 * Link this client into the linked list of clients.
198 * SIGHUP has already been blocked.
200 if (Clients)
201 client->next = Clients;
202 Clients = client;
205 (void) fclose(fp); /* close config file */
207 (void) sigsetmask(omask); /* reset signal mask */
209 return(1); /* return success */
213 ** ParseAddr -- Parse a string containing an RMP address.
215 ** This routine is fairly liberal at parsing an RMP address. The
216 ** address must contain 6 octets consisting of between 0 and 2 hex
217 ** chars (upper/lower case) separated by colons. If two colons are
218 ** together (e.g. "::", the octet between them is recorded as being
219 ** zero. Hence, the following addrs are all valid and parse to the
220 ** same thing:
222 ** 08:00:09:00:66:ad 8::9:0:66:AD 8::9::66:aD
224 ** For clarity, an RMP address is really an Ethernet address, but
225 ** since the HP boot code uses IEEE 802.3, it's really an IEEE
226 ** 802.3 address. Of course, all of these are identical.
228 ** Parameters:
229 ** str - string representation of an RMP address.
231 ** Returns:
232 ** pointer to a static array of RMP_ADDRLEN bytes.
234 ** Side Effects:
235 ** None.
237 ** Warnings:
238 ** - The return value points to a static buffer; it must
239 ** be copied if it's to be saved.
241 u_int8_t *
242 ParseAddr(char *str)
244 static u_int8_t addr[RMP_ADDRLEN];
245 char *cp;
246 unsigned i;
247 int part, subpart;
249 memset((char *)&addr[0], 0, RMP_ADDRLEN); /* zero static buffer */
251 part = subpart = 0;
252 for (cp = str; *cp; cp++) {
254 * A colon (`:') must be used to delimit each octet.
256 if (*cp == ':') {
257 if (++part == RMP_ADDRLEN) /* too many parts */
258 return(NULL);
259 subpart = 0;
260 continue;
264 * Convert hex character to an integer.
266 if (isdigit(*cp))
267 i = *cp - '0';
268 else {
269 i = (isupper(*cp)? tolower(*cp): *cp) - 'a' + 10;
270 if (i < 10 || i > 15) /* not a hex char */
271 return(NULL);
274 if (subpart++) {
275 if (subpart > 2) /* too many hex chars */
276 return(NULL);
277 addr[part] <<= 4;
279 addr[part] |= i;
282 if (part != (RMP_ADDRLEN-1)) /* too few parts */
283 return(NULL);
285 return(&addr[0]);
289 ** GetBootFiles -- record list of files in current (boot) directory.
291 ** Parameters:
292 ** None.
294 ** Returns:
295 ** Number of boot files on success, 0 on failure.
297 ** Side Effects:
298 ** Strings in `BootFiles' are freed/allocated.
300 ** Warnings:
301 ** - After this routine is called, ParseConfig() must be
302 ** called to re-order it's list of boot file pointers.
305 GetBootFiles(void)
307 DIR *dfd;
308 struct stat statb;
309 struct dirent *dp;
310 int i;
313 * Free the current list of boot files.
315 for (i = 0; i < C_MAXFILE && BootFiles[i] != NULL; i++) {
316 FreeStr(BootFiles[i]);
317 BootFiles[i] = NULL;
321 * Open current directory to read boot file names.
323 if ((dfd = opendir(".")) == NULL) { /* open BootDir */
324 syslog(LOG_ERR, "GetBootFiles: can't open directory (%s)\n",
325 BootDir);
326 return(0);
330 * Read each boot file name and allocate space for it in the
331 * list of boot files (BootFiles). All boot files read after
332 * C_MAXFILE will be ignored.
334 i = 0;
335 for (dp = readdir(dfd); dp != NULL; dp = readdir(dfd)) {
336 if (stat(dp->d_name, &statb) < 0 ||
337 (statb.st_mode & S_IFMT) != S_IFREG)
338 continue;
339 if (i == C_MAXFILE)
340 syslog(LOG_ERR,
341 "GetBootFiles: too many boot files (%s ignored)",
342 dp->d_name);
343 else if ((BootFiles[i] = NewStr(dp->d_name)) != NULL)
344 i++;
347 (void) closedir(dfd); /* close BootDir */
349 if (i == 0) /* cant find any boot files */
350 syslog(LOG_ERR, "GetBootFiles: no boot files (%s)\n", BootDir);
352 return(i);