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