1 /* $OpenBSD: rbootd.c,v 1.23 2009/10/27 23:59:54 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. 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 62 #include "defs.h" 63 64 extern char *__progname; /* from crt0.o */ 65 66 volatile sig_atomic_t dodebugoff; 67 volatile sig_atomic_t dodebugon; 68 volatile sig_atomic_t doreconfig; 69 70 void DebugOff(int); 71 void DebugOn(int); 72 void ReConfig(int); 73 void Exit(int); 74 75 void DoDebugOff(void); 76 void DoDebugOn(void); 77 void DoReConfig(void); 78 79 void DoTimeout(void); 80 CLIENT *FindClient(RMPCONN *); 81 82 int 83 main(int argc, char *argv[]) 84 { 85 int c, fd, maxfds; 86 sigset_t hmask, omask; 87 struct passwd *pw; 88 fd_set rset; 89 90 closefrom(STDERR_FILENO + 1); 91 92 if ((pw = getpwnam("_rbootd")) == NULL) 93 err(1, "getpwnam"); 94 95 while ((c = getopt(argc, argv, "adi:")) != -1) 96 switch (c) { 97 case 'a': 98 BootAny++; 99 break; 100 case 'd': 101 DebugFlg++; 102 break; 103 case 'i': 104 IntfName = optarg; 105 break; 106 } 107 for (; optind < argc; optind++) { 108 if (ConfigFile == NULL) 109 ConfigFile = argv[optind]; 110 else { 111 warnx("too many config files (`%s' ignored)", 112 argv[optind]); 113 } 114 } 115 116 if (ConfigFile == NULL) /* use default config file */ 117 ConfigFile = DfltConfig; 118 119 if (DebugFlg) { 120 DbgFp = stdout; /* output to stdout */ 121 122 (void) signal(SIGUSR1, SIG_IGN); /* dont muck w/DbgFp */ 123 (void) signal(SIGUSR2, SIG_IGN); 124 (void) fclose(stderr); /* finished with it */ 125 } else { 126 if (daemon(0, 0)) 127 err(1, "can't detach from terminal"); 128 129 (void) signal(SIGUSR1, DebugOn); 130 (void) signal(SIGUSR2, DebugOff); 131 } 132 133 /* 134 * If no interface was specified, get one now. 135 * 136 * This is convoluted because we want to get the default interface 137 * name for the syslog("restarted") message. If BpfGetIntfName() 138 * runs into an error, it will return a syslog-able error message 139 * (in `errmsg') which will be displayed here. 140 */ 141 if (IntfName == NULL) { 142 char *errmsg; 143 144 if ((IntfName = BpfGetIntfName(&errmsg)) == NULL) { 145 syslog(LOG_NOTICE, "restarted (??)"); 146 /* BpfGetIntfName() returns safe names, using %m */ 147 syslog(LOG_ERR, "%s", errmsg); 148 DoExit(); 149 } 150 } 151 152 openlog(__progname, LOG_PID, LOG_DAEMON); 153 fd = BpfOpen(); 154 syslog(LOG_NOTICE, "restarted (%s)", IntfName); 155 156 (void) signal(SIGHUP, ReConfig); 157 (void) signal(SIGINT, Exit); 158 (void) signal(SIGTERM, Exit); 159 160 gethostname(MyHost, MAXHOSTNAMELEN); 161 162 if (pidfile(NULL) < 0) 163 syslog(LOG_WARNING, "pidfile: failed"); 164 165 /* 166 * All boot files are relative to the boot directory, we might 167 * as well chdir() there to make life easier. 168 */ 169 if (chdir(BootDir) < 0) { 170 syslog(LOG_ERR, "chdir: %m (%s)", BootDir); 171 DoExit(); 172 } 173 174 /* 175 * Initial configuration. 176 */ 177 sigemptyset(&hmask); 178 sigaddset(&hmask, SIGHUP); 179 sigprocmask(SIG_BLOCK, &hmask, &omask); /* prevent reconfig's */ 180 if (GetBootFiles() == 0) /* get list of boot files */ 181 DoExit(); 182 if (ParseConfig() == 0) /* parse config file */ 183 DoExit(); 184 185 if (chroot(BootDir) == -1) { 186 syslog(LOG_CRIT, "chroot %s: %m", BootDir); 187 exit(1); 188 } 189 if (chdir("/") == -1) { 190 syslog(LOG_CRIT, "chdir(\"/\"): %m"); 191 exit(1); 192 } 193 if (setgroups(1, &pw->pw_gid) || 194 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || 195 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) { 196 syslog(LOG_CRIT, "can't drop privileges: %m"); 197 exit(1); 198 } 199 endpwent(); 200 201 sigprocmask(SIG_SETMASK, &omask, NULL); /* allow reconfig's */ 202 203 /* 204 * Main loop: receive a packet, determine where it came from, 205 * and if we service this host, call routine to handle request. 206 */ 207 maxfds = fd + 1; 208 FD_ZERO(&rset); 209 FD_SET(fd, &rset); 210 for (;;) { 211 struct timeval timeout; 212 fd_set r; 213 int nsel; 214 215 /* 216 * Check pending actions 217 */ 218 if (dodebugoff) { 219 DoDebugOff(); 220 dodebugoff = 0; 221 } 222 if (dodebugon) { 223 DoDebugOn(); 224 dodebugon = 0; 225 } 226 if (doreconfig) { 227 DoReConfig(); 228 doreconfig = 0; 229 } 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 DoExit(); 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; 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 sigprocmask(SIG_BLOCK, &hmask, &omask); 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 sigprocmask(SIG_SETMASK, &omask, NULL); 278 continue; 279 } 280 281 ProcessPacket(&rconn,client); 282 283 sigprocmask(SIG_SETMASK, &omask, NULL); 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(void) 303 { 304 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 CLIENT * 338 FindClient(RMPCONN *rconn) 339 { 340 CLIENT *ctmp; 341 342 for (ctmp = Clients; ctmp != NULL; ctmp = ctmp->next) 343 if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0], 344 (char *)&ctmp->addr[0], RMP_ADDRLEN) == 0) 345 break; 346 347 return(ctmp); 348 } 349 350 /* 351 ** Exit -- Log an error message and exit. 352 ** 353 ** Parameters: 354 ** sig - caught signal (or zero if not dying on a signal). 355 ** 356 ** Returns: 357 ** Does not return. 358 ** 359 ** Side Effects: 360 ** - This process ceases to exist. 361 */ 362 void 363 Exit(int sig) 364 { 365 struct syslog_data sdata = SYSLOG_DATA_INIT; 366 367 syslog_r(LOG_ERR, &sdata, "going down on signal %d", sig); 368 _exit(1); 369 } 370 371 void 372 DoExit(void) 373 { 374 syslog(LOG_ERR, "going down on fatal error"); 375 exit(1); 376 } 377 378 /* 379 ** ReConfig -- Get new list of boot files and reread config files. 380 ** 381 ** Parameters: 382 ** None. 383 ** 384 ** Returns: 385 ** Nothing. 386 ** 387 ** Side Effects: 388 ** - All active connections are dropped. 389 ** - List of bootable files is changed. 390 ** - List of clients is changed. 391 ** 392 ** Warnings: 393 ** - This routine must be called with SIGHUP blocked. 394 */ 395 void 396 ReConfig(int signo) 397 { 398 doreconfig = 1; 399 } 400 401 void 402 DoReConfig(void) 403 { 404 syslog(LOG_NOTICE, "reconfiguring boot server"); 405 406 FreeConns(); 407 408 if (GetBootFiles() == 0) 409 DoExit(); 410 411 if (ParseConfig() == 0) 412 DoExit(); 413 } 414 415 /* 416 ** DebugOff -- Turn off debugging. 417 ** 418 ** Parameters: 419 ** None. 420 ** 421 ** Returns: 422 ** Nothing. 423 ** 424 ** Side Effects: 425 ** - Debug file is closed. 426 */ 427 void 428 DebugOff(int signo) 429 { 430 dodebugoff = 1; 431 } 432 433 void 434 DoDebugOff(void) 435 { 436 if (DbgFp != NULL) 437 (void) fclose(DbgFp); 438 439 DbgFp = NULL; 440 } 441 442 /* 443 ** DebugOn -- Turn on debugging. 444 ** 445 ** Parameters: 446 ** None. 447 ** 448 ** Returns: 449 ** Nothing. 450 ** 451 ** Side Effects: 452 ** - Debug file is opened/truncated if not already opened, 453 ** otherwise do nothing. 454 */ 455 void 456 DebugOn(int signo) 457 { 458 dodebugon = 1; 459 } 460 461 void 462 DoDebugOn(void) 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