1 /* $NetBSD: parseconf.c,v 1.4 1995/10/06 05:12:16 thorpej Exp $ */ 2 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. All advertising materials mentioning features or use of this software 24 * must display the following acknowledgement: 25 * This product includes software developed by the University of 26 * California, Berkeley and its contributors. 27 * 4. Neither the name of the University nor the names of its contributors 28 * may be used to endorse or promote products derived from this software 29 * without specific prior written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41 * SUCH DAMAGE. 42 * 43 * from: @(#)parseconf.c 8.1 (Berkeley) 6/4/93 44 * 45 * From: Utah Hdr: parseconf.c 3.1 92/07/06 46 * Author: Jeff Forys, University of Utah CSS 47 */ 48 49 #ifndef lint 50 /*static char sccsid[] = "@(#)parseconf.c 8.1 (Berkeley) 6/4/93";*/ 51 static char rcsid[] = "$NetBSD: parseconf.c,v 1.4 1995/10/06 05:12:16 thorpej Exp $"; 52 #endif /* not lint */ 53 54 #include <sys/param.h> 55 #include <sys/stat.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() 85 { 86 FILE *fp; 87 CLIENT *client; 88 u_int8_t *addr; 89 char line[C_LINELEN]; 90 register char *cp, *bcp; 91 register 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: cant 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 cant 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(str) 250 char *str; 251 { 252 static u_int8_t addr[RMP_ADDRLEN]; 253 register char *cp; 254 register unsigned i; 255 register int part, subpart; 256 257 bzero((char *)&addr[0], RMP_ADDRLEN); /* zero static buffer */ 258 259 part = subpart = 0; 260 for (cp = str; *cp; cp++) { 261 /* 262 * A colon (`:') must be used to delimit each octet. 263 */ 264 if (*cp == ':') { 265 if (++part == RMP_ADDRLEN) /* too many parts */ 266 return(NULL); 267 subpart = 0; 268 continue; 269 } 270 271 /* 272 * Convert hex character to an integer. 273 */ 274 if (isdigit(*cp)) 275 i = *cp - '0'; 276 else { 277 i = (isupper(*cp)? tolower(*cp): *cp) - 'a' + 10; 278 if (i < 10 || i > 15) /* not a hex char */ 279 return(NULL); 280 } 281 282 if (subpart++) { 283 if (subpart > 2) /* too many hex chars */ 284 return(NULL); 285 addr[part] <<= 4; 286 } 287 addr[part] |= i; 288 } 289 290 if (part != (RMP_ADDRLEN-1)) /* too few parts */ 291 return(NULL); 292 293 return(&addr[0]); 294 } 295 296 /* 297 ** GetBootFiles -- record list of files in current (boot) directory. 298 ** 299 ** Parameters: 300 ** None. 301 ** 302 ** Returns: 303 ** Number of boot files on success, 0 on failure. 304 ** 305 ** Side Effects: 306 ** Strings in `BootFiles' are freed/allocated. 307 ** 308 ** Warnings: 309 ** - After this routine is called, ParseConfig() must be 310 ** called to re-order it's list of boot file pointers. 311 */ 312 int 313 GetBootFiles() 314 { 315 DIR *dfd; 316 struct stat statb; 317 register struct dirent *dp; 318 register int i; 319 320 /* 321 * Free the current list of boot files. 322 */ 323 for (i = 0; i < C_MAXFILE && BootFiles[i] != NULL; i++) { 324 FreeStr(BootFiles[i]); 325 BootFiles[i] = NULL; 326 } 327 328 /* 329 * Open current directory to read boot file names. 330 */ 331 if ((dfd = opendir(".")) == NULL) { /* open BootDir */ 332 syslog(LOG_ERR, "GetBootFiles: can't open directory (%s)", 333 BootDir); 334 return(0); 335 } 336 337 /* 338 * Read each boot file name and allocate space for it in the 339 * list of boot files (BootFiles). All boot files read after 340 * C_MAXFILE will be ignored. 341 */ 342 i = 0; 343 for (dp = readdir(dfd); dp != NULL; dp = readdir(dfd)) { 344 if (stat(dp->d_name, &statb) < 0 || 345 (statb.st_mode & S_IFMT) != S_IFREG) 346 continue; 347 if (i == C_MAXFILE) 348 syslog(LOG_ERR, 349 "GetBootFiles: too many boot files (%s ignored)", 350 dp->d_name); 351 else if ((BootFiles[i] = NewStr(dp->d_name)) != NULL) 352 i++; 353 } 354 355 (void) closedir(dfd); /* close BootDir */ 356 357 if (i == 0) /* cant find any boot files */ 358 syslog(LOG_ERR, "GetBootFiles: no boot files (%s)", BootDir); 359 360 return(i); 361 } 362