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