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