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