1 /* $OpenBSD: parseconf.c,v 1.12 2015/01/16 06:40:19 deraadt 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 #include <sys/stat.h> 47 48 #include <ctype.h> 49 #include <dirent.h> 50 #include <fcntl.h> 51 #include <signal.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <syslog.h> 56 #include "defs.h" 57 58 /* 59 ** ParseConfig -- parse the config file into linked list of clients. 60 ** 61 ** Parameters: 62 ** None. 63 ** 64 ** Returns: 65 ** 1 on success, 0 otherwise. 66 ** 67 ** Side Effects: 68 ** - Linked list of clients will be (re)allocated. 69 ** 70 ** Warnings: 71 ** - GetBootFiles() must be called before this routine 72 ** to create a linked list of default boot files. 73 */ 74 int 75 ParseConfig(void) 76 { 77 char line[C_LINELEN], *cp, *bcp; 78 int i, j, linecnt = 0; 79 sigset_t mask, omask; 80 u_int8_t *addr; 81 CLIENT *client; 82 FILE *fp; 83 84 if (BootAny) /* ignore config file */ 85 return(1); 86 87 FreeClients(); /* delete old list of clients */ 88 89 if ((fp = fopen(ConfigFile, "r")) == NULL) { 90 syslog(LOG_ERR, "ParseConfig: can't open config file (%s)", 91 ConfigFile); 92 return(0); 93 } 94 95 /* 96 * We've got to block SIGHUP to prevent reconfiguration while 97 * dealing with the linked list of Clients. This can be done 98 * when actually linking the new client into the list, but 99 * this could have unexpected results if the server was HUP'd 100 * whilst reconfiguring. Hence, it is done here. 101 */ 102 sigemptyset(&mask); 103 sigaddset(&mask, SIGHUP); 104 sigprocmask(SIG_BLOCK, &mask, &omask); 105 106 /* 107 * GETSTR positions `bcp' at the start of the current token, 108 * and null terminates it. `cp' is positioned at the start 109 * of the next token. spaces & commas are separators. 110 */ 111 #define GETSTR while (isspace((unsigned char)*cp) || *cp == ',') \ 112 cp++; \ 113 bcp = cp; \ 114 while (*cp && *cp!=',' && !isspace((unsigned char)*cp)) \ 115 cp++; \ 116 if (*cp) \ 117 *cp++ = '\0' 118 119 /* 120 * For each line, parse it into a new CLIENT struct. 121 */ 122 while (fgets(line, C_LINELEN, fp) != NULL) { 123 linecnt++; /* line counter */ 124 125 if (*line == '\0' || *line == '#') /* ignore comment */ 126 continue; 127 128 if ((cp = strchr(line,'#')) != NULL) /* trash comments */ 129 *cp = '\0'; 130 131 cp = line; /* init `cp' */ 132 GETSTR; /* get RMP addr */ 133 if (bcp == cp) /* all delimiters */ 134 continue; 135 136 /* 137 * Get an RMP address from a string. Abort on failure. 138 */ 139 if ((addr = ParseAddr(bcp)) == NULL) { 140 syslog(LOG_ERR, 141 "ParseConfig: line %d: cant parse <%s>", 142 linecnt, bcp); 143 continue; 144 } 145 146 if ((client = NewClient(addr)) == NULL) /* alloc new client */ 147 continue; 148 149 GETSTR; /* get first file */ 150 151 /* 152 * If no boot files are spec'd, use the default list. 153 * Otherwise, validate each file (`bcp') against the 154 * list of bootable files. 155 */ 156 i = 0; 157 if (bcp == cp) { /* no files spec'd */ 158 for (; i < C_MAXFILE && BootFiles[i] != NULL; i++) 159 client->files[i] = BootFiles[i]; 160 } else { 161 do { 162 /* 163 * For each boot file spec'd, make sure it's 164 * in our list. If so, include a pointer to 165 * it in the CLIENT's list of boot files. 166 */ 167 for (j = 0; ; j++) { 168 if (j==C_MAXFILE||BootFiles[j]==NULL) { 169 syslog(LOG_ERR, 170 "ParseConfig: line %d: no boot file (%s)", 171 linecnt, bcp); 172 break; 173 } 174 if (STREQN(BootFiles[j], bcp)) { 175 if (i < C_MAXFILE) 176 client->files[i++] = 177 BootFiles[j]; 178 else 179 syslog(LOG_ERR, "ParseConfig: line %d: too many boot files (%s)", 180 linecnt, bcp); 181 break; 182 } 183 } 184 GETSTR; /* get next file */ 185 } while (bcp != cp); 186 187 /* 188 * Restricted list of boot files were spec'd, 189 * however, none of them were found. Since we 190 * apparently cant let them boot "just anything", 191 * the entire record is invalidated. 192 */ 193 if (i == 0) { 194 FreeClient(client); 195 continue; 196 } 197 } 198 199 /* 200 * Link this client into the linked list of clients. 201 * SIGHUP has already been blocked. 202 */ 203 if (Clients) 204 client->next = Clients; 205 Clients = client; 206 } 207 208 (void) fclose(fp); /* close config file */ 209 sigprocmask(SIG_SETMASK, &omask, NULL); /* reset signal mask */ 210 return(1); /* return success */ 211 } 212 213 /* 214 ** ParseAddr -- Parse a string containing an RMP address. 215 ** 216 ** This routine is fairly liberal at parsing an RMP address. The 217 ** address must contain 6 octets consisting of between 0 and 2 hex 218 ** chars (upper/lower case) separated by colons. If two colons are 219 ** together (e.g. "::", the octet between them is recorded as being 220 ** zero. Hence, the following addrs are all valid and parse to the 221 ** same thing: 222 ** 223 ** 08:00:09:00:66:ad 8::9:0:66:AD 8::9::66:aD 224 ** 225 ** For clarity, an RMP address is really an Ethernet address, but 226 ** since the HP boot code uses IEEE 802.3, it's really an IEEE 227 ** 802.3 address. Of course, all of these are identical. 228 ** 229 ** Parameters: 230 ** str - string representation of an RMP address. 231 ** 232 ** Returns: 233 ** pointer to a static array of RMP_ADDRLEN bytes. 234 ** 235 ** Side Effects: 236 ** None. 237 ** 238 ** Warnings: 239 ** - The return value points to a static buffer; it must 240 ** be copied if it's to be saved. 241 */ 242 u_int8_t * 243 ParseAddr(char *str) 244 { 245 static u_int8_t addr[RMP_ADDRLEN]; 246 int part, subpart; 247 unsigned int i; 248 char *cp; 249 250 bzero((char *)&addr[0], RMP_ADDRLEN); /* zero static buffer */ 251 252 part = subpart = 0; 253 for (cp = str; *cp; cp++) { 254 /* 255 * A colon (`:') must be used to delimit each octet. 256 */ 257 if (*cp == ':') { 258 if (++part == RMP_ADDRLEN) /* too many parts */ 259 return(NULL); 260 subpart = 0; 261 continue; 262 } 263 264 /* 265 * Convert hex character to an integer. 266 */ 267 if (isdigit((unsigned char)*cp)) 268 i = *cp - '0'; 269 else { 270 i = (isupper((unsigned char)*cp) ? 271 tolower((unsigned char)*cp) : *cp) - 'a' + 10; 272 if (i < 10 || i > 15) /* not a hex char */ 273 return(NULL); 274 } 275 276 if (subpart++) { 277 if (subpart > 2) /* too many hex chars */ 278 return(NULL); 279 addr[part] <<= 4; 280 } 281 addr[part] |= i; 282 } 283 284 if (part != (RMP_ADDRLEN-1)) /* too few parts */ 285 return(NULL); 286 287 return(&addr[0]); 288 } 289 290 /* 291 ** GetBootFiles -- record list of files in current (boot) directory. 292 ** 293 ** Parameters: 294 ** None. 295 ** 296 ** Returns: 297 ** Number of boot files on success, 0 on failure. 298 ** 299 ** Side Effects: 300 ** Strings in `BootFiles' are freed/allocated. 301 ** 302 ** Warnings: 303 ** - After this routine is called, ParseConfig() must be 304 ** called to re-order it's list of boot file pointers. 305 */ 306 int 307 GetBootFiles(void) 308 { 309 struct stat statb; 310 struct dirent *dp; 311 DIR *dfd; 312 int i; 313 314 /* 315 * Free the current list of boot files. 316 */ 317 for (i = 0; i < C_MAXFILE && BootFiles[i] != NULL; i++) { 318 FreeStr(BootFiles[i]); 319 BootFiles[i] = NULL; 320 } 321 322 /* 323 * Open current directory to read boot file names. 324 */ 325 if ((dfd = opendir(".")) == NULL) { /* open BootDir */ 326 syslog(LOG_ERR, "GetBootFiles: can't open directory (%s)", 327 BootDir); 328 return(0); 329 } 330 331 /* 332 * Read each boot file name and allocate space for it in the 333 * list of boot files (BootFiles). All boot files read after 334 * C_MAXFILE will be ignored. 335 */ 336 i = 0; 337 for (dp = readdir(dfd); dp != NULL; dp = readdir(dfd)) { 338 if (stat(dp->d_name, &statb) < 0 || 339 (statb.st_mode & S_IFMT) != S_IFREG) 340 continue; 341 if (i == C_MAXFILE) 342 syslog(LOG_ERR, 343 "GetBootFiles: too many boot files (%s ignored)", 344 dp->d_name); 345 else if ((BootFiles[i] = NewStr(dp->d_name)) != NULL) 346 i++; 347 } 348 349 (void) closedir(dfd); /* close BootDir */ 350 351 if (i == 0) /* cant find any boot files */ 352 syslog(LOG_ERR, "GetBootFiles: no boot files (%s)", BootDir); 353 354 return(i); 355 } 356