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