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