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 * 41ea022d16SRodney W. Grimes * @(#)parseconf.c 8.1 (Berkeley) 6/4/93 42ea022d16SRodney W. Grimes * 43ea022d16SRodney W. Grimes * 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 48ea022d16SRodney W. Grimes static char sccsid[] = "@(#)parseconf.c 8.1 (Berkeley) 6/4/93"; 49ea022d16SRodney W. Grimes #endif /* not lint */ 50ea022d16SRodney W. Grimes 51ea022d16SRodney W. Grimes #include <sys/param.h> 52ea022d16SRodney W. Grimes #include <sys/stat.h> 53ea022d16SRodney W. Grimes 54ea022d16SRodney W. Grimes #include <ctype.h> 55ea022d16SRodney W. Grimes #include <dirent.h> 56ea022d16SRodney W. Grimes #include <fcntl.h> 57ea022d16SRodney W. Grimes #include <signal.h> 58ea022d16SRodney W. Grimes #include <stdio.h> 59ea022d16SRodney W. Grimes #include <stdlib.h> 60ea022d16SRodney W. Grimes #include <string.h> 61ea022d16SRodney W. Grimes #include <syslog.h> 62ea022d16SRodney W. Grimes #include "defs.h" 63ea022d16SRodney W. Grimes 64ea022d16SRodney W. Grimes /* 65ea022d16SRodney W. Grimes ** ParseConfig -- parse the config file into linked list of clients. 66ea022d16SRodney W. Grimes ** 67ea022d16SRodney W. Grimes ** Parameters: 68ea022d16SRodney W. Grimes ** None. 69ea022d16SRodney W. Grimes ** 70ea022d16SRodney W. Grimes ** Returns: 71ea022d16SRodney W. Grimes ** 1 on success, 0 otherwise. 72ea022d16SRodney W. Grimes ** 73ea022d16SRodney W. Grimes ** Side Effects: 74ea022d16SRodney W. Grimes ** - Linked list of clients will be (re)allocated. 75ea022d16SRodney W. Grimes ** 76ea022d16SRodney W. Grimes ** Warnings: 77ea022d16SRodney W. Grimes ** - GetBootFiles() must be called before this routine 78ea022d16SRodney W. Grimes ** to create a linked list of default boot files. 79ea022d16SRodney W. Grimes */ 80ea022d16SRodney W. Grimes int 81ea022d16SRodney W. Grimes ParseConfig() 82ea022d16SRodney W. Grimes { 83ea022d16SRodney W. Grimes FILE *fp; 84ea022d16SRodney W. Grimes CLIENT *client; 85ea022d16SRodney W. Grimes u_char *addr; 86ea022d16SRodney W. Grimes char line[C_LINELEN]; 87ea022d16SRodney W. Grimes register char *cp, *bcp; 88ea022d16SRodney W. Grimes register int i, j; 89ea022d16SRodney W. Grimes int omask, linecnt = 0; 90ea022d16SRodney W. Grimes 91ea022d16SRodney W. Grimes if (BootAny) /* ignore config file */ 92ea022d16SRodney W. Grimes return(1); 93ea022d16SRodney W. Grimes 94ea022d16SRodney W. Grimes FreeClients(); /* delete old list of clients */ 95ea022d16SRodney W. Grimes 96ea022d16SRodney W. Grimes if ((fp = fopen(ConfigFile, "r")) == NULL) { 97ea022d16SRodney W. Grimes syslog(LOG_ERR, "ParseConfig: can't open config file (%s)", 98ea022d16SRodney W. Grimes ConfigFile); 99ea022d16SRodney W. Grimes return(0); 100ea022d16SRodney W. Grimes } 101ea022d16SRodney W. Grimes 102ea022d16SRodney W. Grimes /* 103ea022d16SRodney W. Grimes * We've got to block SIGHUP to prevent reconfiguration while 104ea022d16SRodney W. Grimes * dealing with the linked list of Clients. This can be done 105ea022d16SRodney W. Grimes * when actually linking the new client into the list, but 106ea022d16SRodney W. Grimes * this could have unexpected results if the server was HUP'd 107ea022d16SRodney W. Grimes * whilst reconfiguring. Hence, it is done here. 108ea022d16SRodney W. Grimes */ 109ea022d16SRodney W. Grimes omask = sigblock(sigmask(SIGHUP)); 110ea022d16SRodney W. Grimes 111ea022d16SRodney W. Grimes /* 112ea022d16SRodney W. Grimes * GETSTR positions `bcp' at the start of the current token, 113ea022d16SRodney W. Grimes * and null terminates it. `cp' is positioned at the start 114ea022d16SRodney W. Grimes * of the next token. spaces & commas are separators. 115ea022d16SRodney W. Grimes */ 116ea022d16SRodney W. Grimes #define GETSTR while (isspace(*cp) || *cp == ',') cp++; \ 117ea022d16SRodney W. Grimes bcp = cp; \ 118ea022d16SRodney W. Grimes while (*cp && *cp!=',' && !isspace(*cp)) cp++; \ 119ea022d16SRodney W. Grimes if (*cp) *cp++ = '\0' 120ea022d16SRodney W. Grimes 121ea022d16SRodney W. Grimes /* 122ea022d16SRodney W. Grimes * For each line, parse it into a new CLIENT struct. 123ea022d16SRodney W. Grimes */ 124ea022d16SRodney W. Grimes while (fgets(line, C_LINELEN, fp) != NULL) { 125ea022d16SRodney W. Grimes linecnt++; /* line counter */ 126ea022d16SRodney W. Grimes 127ea022d16SRodney W. Grimes if (*line == '\0' || *line == '#') /* ignore comment */ 128ea022d16SRodney W. Grimes continue; 129ea022d16SRodney W. Grimes 130ea022d16SRodney W. Grimes if ((cp = index(line,'#')) != NULL) /* trash comments */ 131ea022d16SRodney W. Grimes *cp = '\0'; 132ea022d16SRodney W. Grimes 133ea022d16SRodney W. Grimes cp = line; /* init `cp' */ 134ea022d16SRodney W. Grimes GETSTR; /* get RMP addr */ 135ea022d16SRodney W. Grimes if (bcp == cp) /* all delimiters */ 136ea022d16SRodney W. Grimes continue; 137ea022d16SRodney W. Grimes 138ea022d16SRodney W. Grimes /* 139ea022d16SRodney W. Grimes * Get an RMP address from a string. Abort on failure. 140ea022d16SRodney W. Grimes */ 141ea022d16SRodney W. Grimes if ((addr = ParseAddr(bcp)) == NULL) { 142ea022d16SRodney W. Grimes syslog(LOG_ERR, 143ea022d16SRodney W. Grimes "ParseConfig: line %d: cant parse <%s>", 144ea022d16SRodney W. Grimes linecnt, bcp); 145ea022d16SRodney W. Grimes continue; 146ea022d16SRodney W. Grimes } 147ea022d16SRodney W. Grimes 148ea022d16SRodney W. Grimes if ((client = NewClient(addr)) == NULL) /* alloc new client */ 149ea022d16SRodney W. Grimes continue; 150ea022d16SRodney W. Grimes 151ea022d16SRodney W. Grimes GETSTR; /* get first file */ 152ea022d16SRodney W. Grimes 153ea022d16SRodney W. Grimes /* 154ea022d16SRodney W. Grimes * If no boot files are spec'd, use the default list. 155ea022d16SRodney W. Grimes * Otherwise, validate each file (`bcp') against the 156ea022d16SRodney W. Grimes * list of boot-able files. 157ea022d16SRodney W. Grimes */ 158ea022d16SRodney W. Grimes i = 0; 159ea022d16SRodney W. Grimes if (bcp == cp) /* no files spec'd */ 160ea022d16SRodney W. Grimes for (; i < C_MAXFILE && BootFiles[i] != NULL; i++) 161ea022d16SRodney W. Grimes client->files[i] = BootFiles[i]; 162ea022d16SRodney W. Grimes else { 163ea022d16SRodney W. Grimes do { 164ea022d16SRodney W. Grimes /* 165ea022d16SRodney W. Grimes * For each boot file spec'd, make sure it's 166ea022d16SRodney W. Grimes * in our list. If so, include a pointer to 167ea022d16SRodney W. Grimes * it in the CLIENT's list of boot files. 168ea022d16SRodney W. Grimes */ 169ea022d16SRodney W. Grimes for (j = 0; ; j++) { 170ea022d16SRodney W. Grimes if (j==C_MAXFILE||BootFiles[j]==NULL) { 171ea022d16SRodney W. Grimes syslog(LOG_ERR, "ParseConfig: line %d: no boot file (%s)", 172ea022d16SRodney W. Grimes linecnt, bcp); 173ea022d16SRodney W. Grimes break; 174ea022d16SRodney W. Grimes } 175ea022d16SRodney W. Grimes if (STREQN(BootFiles[j], bcp)) { 176ea022d16SRodney W. Grimes if (i < C_MAXFILE) 177ea022d16SRodney W. Grimes client->files[i++] = 178ea022d16SRodney W. Grimes BootFiles[j]; 179ea022d16SRodney W. Grimes else 180ea022d16SRodney W. Grimes syslog(LOG_ERR, "ParseConfig: line %d: too many boot files (%s)", 181ea022d16SRodney W. Grimes linecnt, bcp); 182ea022d16SRodney W. Grimes break; 183ea022d16SRodney W. Grimes } 184ea022d16SRodney W. Grimes } 185ea022d16SRodney W. Grimes GETSTR; /* get next file */ 186ea022d16SRodney W. Grimes } while (bcp != cp); 187ea022d16SRodney W. Grimes 188ea022d16SRodney W. Grimes /* 189ea022d16SRodney W. Grimes * Restricted list of boot files were spec'd, 190ea022d16SRodney W. Grimes * however, none of them were found. Since we 191ea022d16SRodney W. Grimes * apparently cant let them boot "just anything", 192ea022d16SRodney W. Grimes * the entire record is invalidated. 193ea022d16SRodney W. Grimes */ 194ea022d16SRodney W. Grimes if (i == 0) { 195ea022d16SRodney W. Grimes FreeClient(client); 196ea022d16SRodney W. Grimes continue; 197ea022d16SRodney W. Grimes } 198ea022d16SRodney W. Grimes } 199ea022d16SRodney W. Grimes 200ea022d16SRodney W. Grimes /* 201ea022d16SRodney W. Grimes * Link this client into the linked list of clients. 202ea022d16SRodney W. Grimes * SIGHUP has already been blocked. 203ea022d16SRodney W. Grimes */ 204ea022d16SRodney W. Grimes if (Clients) 205ea022d16SRodney W. Grimes client->next = Clients; 206ea022d16SRodney W. Grimes Clients = client; 207ea022d16SRodney W. Grimes } 208ea022d16SRodney W. Grimes 209ea022d16SRodney W. Grimes (void) fclose(fp); /* close config file */ 210ea022d16SRodney W. Grimes 211ea022d16SRodney W. Grimes (void) sigsetmask(omask); /* reset signal mask */ 212ea022d16SRodney W. Grimes 213ea022d16SRodney W. Grimes return(1); /* return success */ 214ea022d16SRodney W. Grimes } 215ea022d16SRodney W. Grimes 216ea022d16SRodney W. Grimes /* 217ea022d16SRodney W. Grimes ** ParseAddr -- Parse a string containing an RMP address. 218ea022d16SRodney W. Grimes ** 219ea022d16SRodney W. Grimes ** This routine is fairly liberal at parsing an RMP address. The 220ea022d16SRodney W. Grimes ** address must contain 6 octets consisting of between 0 and 2 hex 221ea022d16SRodney W. Grimes ** chars (upper/lower case) separated by colons. If two colons are 222ea022d16SRodney W. Grimes ** together (e.g. "::", the octet between them is recorded as being 223ea022d16SRodney W. Grimes ** zero. Hence, the following addrs are all valid and parse to the 224ea022d16SRodney W. Grimes ** same thing: 225ea022d16SRodney W. Grimes ** 226ea022d16SRodney W. Grimes ** 08:00:09:00:66:ad 8::9:0:66:AD 8::9::66:aD 227ea022d16SRodney W. Grimes ** 228ea022d16SRodney W. Grimes ** For clarity, an RMP address is really an Ethernet address, but 229ea022d16SRodney W. Grimes ** since the HP boot code uses IEEE 802.3, it's really an IEEE 230ea022d16SRodney W. Grimes ** 802.3 address. Of course, all of these are identical. 231ea022d16SRodney W. Grimes ** 232ea022d16SRodney W. Grimes ** Parameters: 233ea022d16SRodney W. Grimes ** str - string representation of an RMP address. 234ea022d16SRodney W. Grimes ** 235ea022d16SRodney W. Grimes ** Returns: 236ea022d16SRodney W. Grimes ** pointer to a static array of RMP_ADDRLEN bytes. 237ea022d16SRodney W. Grimes ** 238ea022d16SRodney W. Grimes ** Side Effects: 239ea022d16SRodney W. Grimes ** None. 240ea022d16SRodney W. Grimes ** 241ea022d16SRodney W. Grimes ** Warnings: 242ea022d16SRodney W. Grimes ** - The return value points to a static buffer; it must 243ea022d16SRodney W. Grimes ** be copied if it's to be saved. 244ea022d16SRodney W. Grimes ** - For speed, we assume a u_char consists of 8 bits. 245ea022d16SRodney W. Grimes */ 246ea022d16SRodney W. Grimes u_char * 247ea022d16SRodney W. Grimes ParseAddr(str) 248ea022d16SRodney W. Grimes char *str; 249ea022d16SRodney W. Grimes { 250ea022d16SRodney W. Grimes static u_char addr[RMP_ADDRLEN]; 251ea022d16SRodney W. Grimes register char *cp; 252ea022d16SRodney W. Grimes register unsigned i; 253ea022d16SRodney W. Grimes register int part, subpart; 254ea022d16SRodney W. Grimes 255ea022d16SRodney W. Grimes bzero((char *)&addr[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; 315ea022d16SRodney W. Grimes register struct dirent *dp; 316ea022d16SRodney W. Grimes register 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