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