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