1 /* $OpenBSD: rbootd.c,v 1.9 2001/12/01 23:27:23 miod Exp $ */ 2 /* $NetBSD: rbootd.c,v 1.5 1995/10/06 05:12:17 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. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * from: @(#)rbootd.c 8.1 (Berkeley) 6/4/93 45 * 46 * From: Utah Hdr: rbootd.c 3.1 92/07/06 47 * Author: Jeff Forys, University of Utah CSS 48 */ 49 50 #ifndef lint 51 static char copyright[] = 52 "@(#) Copyright (c) 1992, 1993\n\ 53 The Regents of the University of California. All rights reserved.\n"; 54 #endif /* not lint */ 55 56 #ifndef lint 57 /*static char sccsid[] = "@(#)rbootd.c 8.1 (Berkeley) 6/4/93";*/ 58 static char rcsid[] = "$OpenBSD: rbootd.c,v 1.9 2001/12/01 23:27:23 miod Exp $"; 59 #endif /* not lint */ 60 61 #include <sys/param.h> 62 #include <sys/time.h> 63 #include <ctype.h> 64 #include <err.h> 65 #include <errno.h> 66 #include <fcntl.h> 67 #include <signal.h> 68 #include <stdio.h> 69 #include <stdlib.h> 70 #include <string.h> 71 #include <syslog.h> 72 #include <unistd.h> 73 #include "defs.h" 74 75 extern char *__progname; /* from crt0.o */ 76 77 int 78 main(argc, argv) 79 int argc; 80 char *argv[]; 81 { 82 int c, fd, maxfds; 83 fd_set rset; 84 sigset_t hmask, omask; 85 86 /* 87 * Close any open file descriptors. 88 * Temporarily leave stdin & stdout open for `-d', 89 * and stderr open for any pre-syslog error messages. 90 */ 91 { 92 int i, nfds = getdtablesize(); 93 94 for (i = 0; i < nfds; i++) 95 if (i != fileno(stdin) && i != fileno(stdout) && 96 i != fileno(stderr)) 97 (void) close(i); 98 } 99 100 /* 101 * Parse any arguments. 102 */ 103 while ((c = getopt(argc, argv, "adi:")) != -1) 104 switch(c) { 105 case 'a': 106 BootAny++; 107 break; 108 case 'd': 109 DebugFlg++; 110 break; 111 case 'i': 112 IntfName = optarg; 113 break; 114 } 115 for (; optind < argc; optind++) { 116 if (ConfigFile == NULL) 117 ConfigFile = argv[optind]; 118 else { 119 warnx("too many config files (`%s' ignored)", 120 argv[optind]); 121 } 122 } 123 124 if (ConfigFile == NULL) /* use default config file */ 125 ConfigFile = DfltConfig; 126 127 if (DebugFlg) { 128 DbgFp = stdout; /* output to stdout */ 129 130 (void) signal(SIGUSR1, SIG_IGN); /* dont muck w/DbgFp */ 131 (void) signal(SIGUSR2, SIG_IGN); 132 (void) fclose(stderr); /* finished with it */ 133 } else { 134 if (daemon(0, 0)) 135 err(1, "can't detach from terminal"); 136 137 (void) signal(SIGUSR1, DebugOn); 138 (void) signal(SIGUSR2, DebugOff); 139 } 140 141 openlog(__progname, LOG_PID, LOG_DAEMON); 142 143 /* 144 * If no interface was specified, get one now. 145 * 146 * This is convoluted because we want to get the default interface 147 * name for the syslog("restarted") message. If BpfGetIntfName() 148 * runs into an error, it will return a syslog-able error message 149 * (in `errmsg') which will be displayed here. 150 */ 151 if (IntfName == NULL) { 152 char *errmsg; 153 154 if ((IntfName = BpfGetIntfName(&errmsg)) == NULL) { 155 syslog(LOG_NOTICE, "restarted (??)"); 156 /* BpfGetIntfName() returns safe names, using %m */ 157 syslog(LOG_ERR, "%s", errmsg); 158 Exit(0); 159 } 160 } 161 162 syslog(LOG_NOTICE, "restarted (%s)", IntfName); 163 164 (void) signal(SIGHUP, ReConfig); 165 (void) signal(SIGINT, Exit); 166 (void) signal(SIGTERM, Exit); 167 168 /* 169 * Grab our host name and pid. 170 */ 171 if (gethostname(MyHost, MAXHOSTNAMELEN) < 0) { 172 syslog(LOG_ERR, "gethostname: %m"); 173 Exit(0); 174 } 175 MyHost[MAXHOSTNAMELEN] = '\0'; 176 177 /* 178 * Write proc's pid to a file. 179 */ 180 if (pidfile(NULL) < 0) { 181 syslog(LOG_WARNING, "pidfile: failed"); 182 } 183 184 /* 185 * All boot files are relative to the boot directory, we might 186 * as well chdir() there to make life easier. 187 */ 188 if (chdir(BootDir) < 0) { 189 syslog(LOG_ERR, "chdir: %m (%s)", BootDir); 190 Exit(0); 191 } 192 193 /* 194 * Initial configuration. 195 */ 196 sigemptyset(&hmask); 197 sigaddset(&hmask, SIGHUP); 198 sigprocmask(SIG_BLOCK, &hmask, &omask); /* prevent reconfig's */ 199 if (GetBootFiles() == 0) /* get list of boot files */ 200 Exit(0); 201 if (ParseConfig() == 0) /* parse config file */ 202 Exit(0); 203 204 /* 205 * Open and initialize a BPF device for the appropriate interface. 206 * If an error is encountered, a message is displayed and Exit() 207 * is called. 208 */ 209 fd = BpfOpen(); 210 211 sigprocmask(SIG_SETMASK, &omask, NULL); /* allow reconfig's */ 212 213 /* 214 * Main loop: receive a packet, determine where it came from, 215 * and if we service this host, call routine to handle request. 216 */ 217 maxfds = fd + 1; 218 FD_ZERO(&rset); 219 FD_SET(fd, &rset); 220 for (;;) { 221 struct timeval timeout; 222 fd_set r; 223 int nsel; 224 225 r = rset; 226 227 if (RmpConns == NULL) { /* timeout isnt necessary */ 228 nsel = select(maxfds, &r, NULL, NULL, NULL); 229 } else { 230 timeout.tv_sec = RMP_TIMEOUT; 231 timeout.tv_usec = 0; 232 nsel = select(maxfds, &r, NULL, NULL, &timeout); 233 } 234 235 if (nsel < 0) { 236 if (errno == EINTR) 237 continue; 238 syslog(LOG_ERR, "select: %m"); 239 Exit(0); 240 } else if (nsel == 0) { /* timeout */ 241 DoTimeout(); /* clear stale conns */ 242 continue; 243 } 244 245 if (FD_ISSET(fd, &r)) { 246 RMPCONN rconn; 247 CLIENT *client, *FindClient(); 248 int doread = 1; 249 250 while (BpfRead(&rconn, doread)) { 251 doread = 0; 252 253 if (DbgFp != NULL) /* display packet */ 254 DispPkt(&rconn,DIR_RCVD); 255 256 sigprocmask(SIG_BLOCK, &hmask, &omask); 257 258 /* 259 * If we do not restrict service, set the 260 * client to NULL (ProcessPacket() handles 261 * this). Otherwise, check that we can 262 * service this host; if not, log a message 263 * and ignore the packet. 264 */ 265 if (BootAny) { 266 client = NULL; 267 } else if ((client=FindClient(&rconn))==NULL) { 268 syslog(LOG_INFO, 269 "%s: boot packet ignored", 270 EnetStr(&rconn)); 271 sigprocmask(SIG_SETMASK, &omask, NULL); 272 continue; 273 } 274 275 ProcessPacket(&rconn,client); 276 277 sigprocmask(SIG_SETMASK, &omask, NULL); 278 } 279 } 280 } 281 } 282 283 /* 284 ** DoTimeout -- Free any connections that have timed out. 285 ** 286 ** Parameters: 287 ** None. 288 ** 289 ** Returns: 290 ** Nothing. 291 ** 292 ** Side Effects: 293 ** - Timed out connections in `RmpConns' will be freed. 294 */ 295 void 296 DoTimeout() 297 { 298 register RMPCONN *rtmp; 299 struct timeval now; 300 301 (void) gettimeofday(&now, (struct timezone *)0); 302 303 /* 304 * For each active connection, if RMP_TIMEOUT seconds have passed 305 * since the last packet was sent, delete the connection. 306 */ 307 for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next) 308 if ((rtmp->tstamp.tv_sec + RMP_TIMEOUT) < now.tv_sec) { 309 syslog(LOG_WARNING, "%s: connection timed out (%u)", 310 EnetStr(rtmp), rtmp->rmp.r_type); 311 RemoveConn(rtmp); 312 } 313 } 314 315 /* 316 ** FindClient -- Find client associated with a packet. 317 ** 318 ** Parameters: 319 ** rconn - the new packet. 320 ** 321 ** Returns: 322 ** Pointer to client info if found, NULL otherwise. 323 ** 324 ** Side Effects: 325 ** None. 326 ** 327 ** Warnings: 328 ** - This routine must be called with SIGHUP blocked since 329 ** a reconfigure can invalidate the information returned. 330 */ 331 332 CLIENT * 333 FindClient(rconn) 334 register RMPCONN *rconn; 335 { 336 register CLIENT *ctmp; 337 338 for (ctmp = Clients; ctmp != NULL; ctmp = ctmp->next) 339 if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0], 340 (char *)&ctmp->addr[0], RMP_ADDRLEN) == 0) 341 break; 342 343 return(ctmp); 344 } 345 346 /* 347 ** Exit -- Log an error message and exit. 348 ** 349 ** Parameters: 350 ** sig - caught signal (or zero if not dying on a signal). 351 ** 352 ** Returns: 353 ** Does not return. 354 ** 355 ** Side Effects: 356 ** - This process ceases to exist. 357 */ 358 void 359 Exit(sig) 360 int sig; 361 { 362 /* XXX race */ 363 if (sig > 0) 364 syslog(LOG_ERR, "going down on signal %d", sig); 365 else 366 syslog(LOG_ERR, "going down with fatal error"); 367 BpfClose(); 368 exit(1); 369 } 370 371 /* 372 ** ReConfig -- Get new list of boot files and reread config files. 373 ** 374 ** Parameters: 375 ** None. 376 ** 377 ** Returns: 378 ** Nothing. 379 ** 380 ** Side Effects: 381 ** - All active connections are dropped. 382 ** - List of boot-able files is changed. 383 ** - List of clients is changed. 384 ** 385 ** Warnings: 386 ** - This routine must be called with SIGHUP blocked. 387 */ 388 void 389 ReConfig(signo) 390 int signo; 391 { 392 /* XXX race */ 393 syslog(LOG_NOTICE, "reconfiguring boot server"); 394 395 FreeConns(); 396 397 if (GetBootFiles() == 0) 398 Exit(0); 399 400 if (ParseConfig() == 0) 401 Exit(0); 402 } 403 404 /* 405 ** DebugOff -- Turn off debugging. 406 ** 407 ** Parameters: 408 ** None. 409 ** 410 ** Returns: 411 ** Nothing. 412 ** 413 ** Side Effects: 414 ** - Debug file is closed. 415 */ 416 void 417 DebugOff(signo) 418 int signo; 419 { 420 /* XXX race */ 421 422 if (DbgFp != NULL) 423 (void) fclose(DbgFp); 424 425 DbgFp = NULL; 426 } 427 428 /* 429 ** DebugOn -- Turn on debugging. 430 ** 431 ** Parameters: 432 ** None. 433 ** 434 ** Returns: 435 ** Nothing. 436 ** 437 ** Side Effects: 438 ** - Debug file is opened/truncated if not already opened, 439 ** otherwise do nothing. 440 */ 441 void 442 DebugOn(signo) 443 int signo; 444 { 445 /* XXX race */ 446 if (DbgFp == NULL) { 447 if ((DbgFp = fopen(DbgFile, "w")) == NULL) 448 syslog(LOG_ERR, "can't open debug file (%s)", DbgFile); 449 } 450 } 451