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