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