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