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