155575Sbostic /* 255575Sbostic * Copyright (c) 1988, 1992 The University of Utah and the Center 355575Sbostic * for Software Science (CSS). 4*61446Sbostic * Copyright (c) 1992, 1993 5*61446Sbostic * The Regents of the University of California. All rights reserved. 655575Sbostic * 755575Sbostic * This code is derived from software contributed to Berkeley by 855575Sbostic * the Center for Software Science of the University of Utah Computer 955575Sbostic * Science Department. CSS requests users of this software to return 1055575Sbostic * to css-dist@cs.utah.edu any improvements that they make and grant 1155575Sbostic * CSS redistribution rights. 1255575Sbostic * 1355575Sbostic * %sccs.include.redist.c% 1455575Sbostic * 15*61446Sbostic * @(#)rbootd.c 8.1 (Berkeley) 06/04/93 1655575Sbostic * 1755575Sbostic * Utah $Hdr: rbootd.c 3.1 92/07/06$ 1855575Sbostic * Author: Jeff Forys, University of Utah CSS 1955575Sbostic */ 2055575Sbostic 2155575Sbostic #ifndef lint 22*61446Sbostic static char copyright[] = 23*61446Sbostic "@(#) Copyright (c) 1992, 1993\n\ 24*61446Sbostic The Regents of the University of California. All rights reserved.\n"; 2555575Sbostic #endif /* not lint */ 2655575Sbostic 2761445Sbostic #ifndef lint 28*61446Sbostic static char sccsid[] = "@(#)rbootd.c 8.1 (Berkeley) 06/04/93"; 2961445Sbostic #endif /* not lint */ 3061445Sbostic 3155600Sbostic #include <sys/param.h> 3255575Sbostic #include <sys/ioctl.h> 3355575Sbostic 3455600Sbostic #include <ctype.h> 3555575Sbostic #include <errno.h> 3655600Sbostic #include <fcntl.h> 3755600Sbostic #include <signal.h> 3855600Sbostic #include <stdio.h> 3955600Sbostic #include <stdlib.h> 4055600Sbostic #include <string.h> 4155575Sbostic #include <syslog.h> 4255600Sbostic #include <unistd.h> 4355600Sbostic #include "defs.h" 4455575Sbostic 4555600Sbostic 4655575Sbostic /* fd mask macros (backward compatibility with 4.2BSD) */ 4755575Sbostic #ifndef FD_SET 4855575Sbostic #ifdef notdef 4955575Sbostic typedef struct fd_set { /* this should already be in 4.2 */ 5055575Sbostic int fds_bits[1]; 5155575Sbostic } fd_set; 5255575Sbostic #endif 5355575Sbostic #define FD_ZERO(p) ((p)->fds_bits[0] = 0) 5455575Sbostic #define FD_SET(n, p) ((p)->fds_bits[0] |= (1 << (n))) 5555575Sbostic #define FD_CLR(n, p) ((p)->fds_bits[0] &= ~(1 << (n))) 5655575Sbostic #define FD_ISSET(n, p) ((p)->fds_bits[0] & (1 << (n))) 5755575Sbostic #endif 5855575Sbostic 5955600Sbostic int 6055575Sbostic main(argc, argv) 6155600Sbostic int argc; 6255600Sbostic char *argv[]; 6355575Sbostic { 6455575Sbostic int c, fd, omask, maxfds; 6555575Sbostic fd_set rset; 6655575Sbostic 6755575Sbostic /* 6855575Sbostic * Find what name we are running under. 6955575Sbostic */ 7055575Sbostic ProgName = (ProgName = rindex(argv[0],'/')) ? ++ProgName : *argv; 7155575Sbostic 7255575Sbostic /* 7355575Sbostic * Close any open file descriptors. 7455575Sbostic * Temporarily leave stdin & stdout open for `-d', 7555575Sbostic * and stderr open for any pre-syslog error messages. 7655575Sbostic */ 7755575Sbostic { 7855575Sbostic int i, nfds = getdtablesize(); 7955575Sbostic 8055575Sbostic for (i = 0; i < nfds; i++) 8155575Sbostic if (i != fileno(stdin) && i != fileno(stdout) && 8255575Sbostic i != fileno(stderr)) 8355575Sbostic (void) close(i); 8455575Sbostic } 8555575Sbostic 8655575Sbostic /* 8755575Sbostic * Parse any arguments. 8855575Sbostic */ 8955575Sbostic while ((c = getopt(argc, argv, "adi:")) != EOF) 9055575Sbostic switch(c) { 9155575Sbostic case 'a': 9255575Sbostic BootAny++; 9355575Sbostic break; 9455575Sbostic case 'd': 9555575Sbostic DebugFlg++; 9655575Sbostic break; 9755575Sbostic case 'i': 9855575Sbostic IntfName = optarg; 9955575Sbostic break; 10055575Sbostic } 10155575Sbostic for (; optind < argc; optind++) { 10255575Sbostic if (ConfigFile == NULL) 10355575Sbostic ConfigFile = argv[optind]; 10455575Sbostic else { 10555575Sbostic fprintf(stderr, 10655575Sbostic "%s: too many config files (`%s' ignored)\n", 10755575Sbostic ProgName, argv[optind]); 10855575Sbostic } 10955575Sbostic } 11055575Sbostic 11155575Sbostic if (ConfigFile == NULL) /* use default config file */ 11255575Sbostic ConfigFile = DfltConfig; 11355575Sbostic 11455575Sbostic if (DebugFlg) { 11555575Sbostic DbgFp = stdout; /* output to stdout */ 11655575Sbostic 11755575Sbostic (void) signal(SIGUSR1, SIG_IGN); /* dont muck w/DbgFp */ 11855575Sbostic (void) signal(SIGUSR2, SIG_IGN); 11955575Sbostic } else { 12055575Sbostic (void) fclose(stdin); /* dont need these */ 12155575Sbostic (void) fclose(stdout); 12255575Sbostic 12355575Sbostic /* 12455575Sbostic * Fork off a child to do the work & exit. 12555575Sbostic */ 12655575Sbostic switch(fork()) { 12755575Sbostic case -1: /* fork failed */ 12855575Sbostic fprintf(stderr, "%s: ", ProgName); 12955575Sbostic perror("fork"); 13055575Sbostic Exit(0); 13155575Sbostic case 0: /* this is the CHILD */ 13255575Sbostic break; 13355575Sbostic default: /* this is the PARENT */ 13455575Sbostic _exit(0); 13555575Sbostic } 13655575Sbostic 13755575Sbostic /* 13855575Sbostic * Try to disassociate from the current tty. 13955575Sbostic */ 14055575Sbostic { 14155575Sbostic char *devtty = "/dev/tty"; 14255575Sbostic int i; 14355575Sbostic 14455575Sbostic if ((i = open(devtty, O_RDWR)) < 0) { 14555575Sbostic /* probably already disassociated */ 14655575Sbostic if (setpgrp(0, 0) < 0) { 14755575Sbostic fprintf(stderr, "%s: ", ProgName); 14855575Sbostic perror("setpgrp"); 14955575Sbostic } 15055575Sbostic } else { 15155575Sbostic if (ioctl(i, (u_long)TIOCNOTTY, (char *)0) < 0){ 15255575Sbostic fprintf(stderr, "%s: ", ProgName); 15355575Sbostic perror("ioctl"); 15455575Sbostic } 15555575Sbostic (void) close(i); 15655575Sbostic } 15755575Sbostic } 15855575Sbostic 15955575Sbostic (void) signal(SIGUSR1, DebugOn); 16055575Sbostic (void) signal(SIGUSR2, DebugOff); 16155575Sbostic } 16255575Sbostic 16355575Sbostic (void) fclose(stderr); /* finished with it */ 16455575Sbostic 16555575Sbostic #ifdef SYSLOG4_2 16655575Sbostic openlog(ProgName, LOG_PID); 16755575Sbostic #else 16855575Sbostic openlog(ProgName, LOG_PID, LOG_DAEMON); 16955575Sbostic #endif 17055575Sbostic 17155575Sbostic /* 17255575Sbostic * If no interface was specified, get one now. 17355575Sbostic * 17455575Sbostic * This is convoluted because we want to get the default interface 17555575Sbostic * name for the syslog("restarted") message. If BpfGetIntfName() 17655575Sbostic * runs into an error, it will return a syslog-able error message 17755575Sbostic * (in `errmsg') which will be displayed here. 17855575Sbostic */ 17955575Sbostic if (IntfName == NULL) { 18055600Sbostic char *errmsg; 18155575Sbostic 18255575Sbostic if ((IntfName = BpfGetIntfName(&errmsg)) == NULL) { 18355575Sbostic syslog(LOG_NOTICE, "restarted (??)"); 18455575Sbostic syslog(LOG_ERR, errmsg); 18555575Sbostic Exit(0); 18655575Sbostic } 18755575Sbostic } 18855575Sbostic 18955575Sbostic syslog(LOG_NOTICE, "restarted (%s)", IntfName); 19055575Sbostic 19155575Sbostic (void) signal(SIGHUP, ReConfig); 19255575Sbostic (void) signal(SIGINT, Exit); 19355575Sbostic (void) signal(SIGTERM, Exit); 19455575Sbostic 19555575Sbostic /* 19655575Sbostic * Grab our host name and pid. 19755575Sbostic */ 19855575Sbostic if (gethostname(MyHost, MAXHOSTNAMELEN) < 0) { 19955575Sbostic syslog(LOG_ERR, "gethostname: %m"); 20055575Sbostic Exit(0); 20155575Sbostic } 20255575Sbostic MyHost[MAXHOSTNAMELEN] = '\0'; 20355575Sbostic 20455575Sbostic MyPid = getpid(); 20555575Sbostic 20655575Sbostic /* 20755575Sbostic * Write proc's pid to a file. 20855575Sbostic */ 20955575Sbostic { 21055575Sbostic FILE *fp; 21155575Sbostic 21255575Sbostic if ((fp = fopen(PidFile, "w")) != NULL) { 21355575Sbostic (void) fprintf(fp, "%d\n", MyPid); 21455575Sbostic (void) fclose(fp); 21555575Sbostic } else { 21655575Sbostic syslog(LOG_WARNING, "fopen: failed (%s)", PidFile); 21755575Sbostic } 21855575Sbostic } 21955575Sbostic 22055575Sbostic /* 22155575Sbostic * All boot files are relative to the boot directory, we might 22255575Sbostic * as well chdir() there to make life easier. 22355575Sbostic */ 22455575Sbostic if (chdir(BootDir) < 0) { 22555575Sbostic syslog(LOG_ERR, "chdir: %m (%s)", BootDir); 22655575Sbostic Exit(0); 22755575Sbostic } 22855575Sbostic 22955575Sbostic /* 23055575Sbostic * Initial configuration. 23155575Sbostic */ 23255575Sbostic omask = sigblock(sigmask(SIGHUP)); /* prevent reconfig's */ 23355575Sbostic if (GetBootFiles() == 0) /* get list of boot files */ 23455575Sbostic Exit(0); 23555575Sbostic if (ParseConfig() == 0) /* parse config file */ 23655575Sbostic Exit(0); 23755575Sbostic 23855575Sbostic /* 23955575Sbostic * Open and initialize a BPF device for the appropriate interface. 24055575Sbostic * If an error is encountered, a message is displayed and Exit() 24155575Sbostic * is called. 24255575Sbostic */ 24355575Sbostic fd = BpfOpen(); 24455575Sbostic 24555575Sbostic (void) sigsetmask(omask); /* allow reconfig's */ 24655575Sbostic 24755575Sbostic /* 24855575Sbostic * Main loop: receive a packet, determine where it came from, 24955575Sbostic * and if we service this host, call routine to handle request. 25055575Sbostic */ 25155575Sbostic maxfds = fd + 1; 25255575Sbostic FD_ZERO(&rset); 25355575Sbostic FD_SET(fd, &rset); 25455575Sbostic for (;;) { 25555575Sbostic struct timeval timeout; 25655575Sbostic fd_set r; 25755575Sbostic int nsel; 25855575Sbostic 25955575Sbostic r = rset; 26055575Sbostic 26155575Sbostic if (RmpConns == NULL) { /* timeout isnt necessary */ 26255575Sbostic nsel = select(maxfds, &r, (fd_set *)0, (fd_set *)0, 26355575Sbostic (struct timeval *)0); 26455575Sbostic } else { 26555575Sbostic timeout.tv_sec = RMP_TIMEOUT; 26655575Sbostic timeout.tv_usec = 0; 26755575Sbostic nsel = select(maxfds, &r, (fd_set *)0, (fd_set *)0, 26855575Sbostic &timeout); 26955575Sbostic } 27055575Sbostic 27155575Sbostic if (nsel < 0) { 27255575Sbostic if (errno == EINTR) 27355575Sbostic continue; 27455575Sbostic syslog(LOG_ERR, "select: %m"); 27555575Sbostic Exit(0); 27655575Sbostic } else if (nsel == 0) { /* timeout */ 27755575Sbostic DoTimeout(); /* clear stale conns */ 27855575Sbostic continue; 27955575Sbostic } 28055575Sbostic 28155575Sbostic if (FD_ISSET(fd, &r)) { 28255575Sbostic RMPCONN rconn; 28355575Sbostic CLIENT *client, *FindClient(); 28455575Sbostic int doread = 1; 28555575Sbostic 28655575Sbostic while (BpfRead(&rconn, doread)) { 28755575Sbostic doread = 0; 28855575Sbostic 28955575Sbostic if (DbgFp != NULL) /* display packet */ 29055575Sbostic DispPkt(&rconn,DIR_RCVD); 29155575Sbostic 29255575Sbostic omask = sigblock(sigmask(SIGHUP)); 29355575Sbostic 29455575Sbostic /* 29555575Sbostic * If we do not restrict service, set the 29655575Sbostic * client to NULL (ProcessPacket() handles 29755575Sbostic * this). Otherwise, check that we can 29855575Sbostic * service this host; if not, log a message 29955575Sbostic * and ignore the packet. 30055575Sbostic */ 30155575Sbostic if (BootAny) { 30255575Sbostic client = NULL; 30355575Sbostic } else if ((client=FindClient(&rconn))==NULL) { 30455575Sbostic syslog(LOG_INFO, 30555575Sbostic "%s: boot packet ignored", 30655575Sbostic EnetStr(&rconn)); 30755575Sbostic (void) sigsetmask(omask); 30855575Sbostic continue; 30955575Sbostic } 31055575Sbostic 31155575Sbostic ProcessPacket(&rconn,client); 31255575Sbostic 31355575Sbostic (void) sigsetmask(omask); 31455575Sbostic } 31555575Sbostic } 31655575Sbostic } 31755575Sbostic } 31855575Sbostic 31955575Sbostic /* 32055575Sbostic ** DoTimeout -- Free any connections that have timed out. 32155575Sbostic ** 32255575Sbostic ** Parameters: 32355575Sbostic ** None. 32455575Sbostic ** 32555575Sbostic ** Returns: 32655575Sbostic ** Nothing. 32755575Sbostic ** 32855575Sbostic ** Side Effects: 32955575Sbostic ** - Timed out connections in `RmpConns' will be freed. 33055575Sbostic */ 33155600Sbostic void 33255575Sbostic DoTimeout() 33355575Sbostic { 33455575Sbostic register RMPCONN *rtmp; 33555575Sbostic struct timeval now; 33655575Sbostic 33755575Sbostic (void) gettimeofday(&now, (struct timezone *)0); 33855575Sbostic 33955575Sbostic /* 34055575Sbostic * For each active connection, if RMP_TIMEOUT seconds have passed 34155575Sbostic * since the last packet was sent, delete the connection. 34255575Sbostic */ 34355575Sbostic for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next) 34455575Sbostic if ((rtmp->tstamp.tv_sec + RMP_TIMEOUT) < now.tv_sec) { 34555575Sbostic syslog(LOG_WARNING, "%s: connection timed out (%u)", 34655575Sbostic EnetStr(rtmp), rtmp->rmp.r_type); 34755575Sbostic RemoveConn(rtmp); 34855575Sbostic } 34955575Sbostic } 35055575Sbostic 35155575Sbostic /* 35255575Sbostic ** FindClient -- Find client associated with a packet. 35355575Sbostic ** 35455575Sbostic ** Parameters: 35555575Sbostic ** rconn - the new packet. 35655575Sbostic ** 35755575Sbostic ** Returns: 35855575Sbostic ** Pointer to client info if found, NULL otherwise. 35955575Sbostic ** 36055575Sbostic ** Side Effects: 36155575Sbostic ** None. 36255575Sbostic ** 36355575Sbostic ** Warnings: 36455575Sbostic ** - This routine must be called with SIGHUP blocked since 36555575Sbostic ** a reconfigure can invalidate the information returned. 36655575Sbostic */ 36755575Sbostic 36855575Sbostic CLIENT * 36955575Sbostic FindClient(rconn) 37055600Sbostic register RMPCONN *rconn; 37155575Sbostic { 37255575Sbostic register CLIENT *ctmp; 37355575Sbostic 37455575Sbostic for (ctmp = Clients; ctmp != NULL; ctmp = ctmp->next) 37555575Sbostic if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0], 37655575Sbostic (char *)&ctmp->addr[0], RMP_ADDRLEN) == 0) 37755575Sbostic break; 37855575Sbostic 37955575Sbostic return(ctmp); 38055575Sbostic } 38155575Sbostic 38255575Sbostic /* 38355575Sbostic ** Exit -- Log an error message and exit. 38455575Sbostic ** 38555575Sbostic ** Parameters: 38655575Sbostic ** sig - caught signal (or zero if not dying on a signal). 38755575Sbostic ** 38855575Sbostic ** Returns: 38955575Sbostic ** Does not return. 39055575Sbostic ** 39155575Sbostic ** Side Effects: 39255575Sbostic ** - This process ceases to exist. 39355575Sbostic */ 39455600Sbostic void 39555575Sbostic Exit(sig) 39655575Sbostic int sig; 39755575Sbostic { 39855575Sbostic if (sig > 0) 39955575Sbostic syslog(LOG_ERR, "going down on signal %d", sig); 40055575Sbostic else 40155575Sbostic syslog(LOG_ERR, "going down with fatal error"); 40255575Sbostic BpfClose(); 40355575Sbostic exit(1); 40455575Sbostic } 40555575Sbostic 40655575Sbostic /* 40755575Sbostic ** ReConfig -- Get new list of boot files and reread config files. 40855575Sbostic ** 40955575Sbostic ** Parameters: 41055575Sbostic ** None. 41155575Sbostic ** 41255575Sbostic ** Returns: 41355575Sbostic ** Nothing. 41455575Sbostic ** 41555575Sbostic ** Side Effects: 41655575Sbostic ** - All active connections are dropped. 41755575Sbostic ** - List of boot-able files is changed. 41855575Sbostic ** - List of clients is changed. 41955575Sbostic ** 42055575Sbostic ** Warnings: 42155575Sbostic ** - This routine must be called with SIGHUP blocked. 42255575Sbostic */ 42355600Sbostic void 42455600Sbostic ReConfig(signo) 42555600Sbostic int signo; 42655575Sbostic { 42755575Sbostic syslog(LOG_NOTICE, "reconfiguring boot server"); 42855575Sbostic 42955575Sbostic FreeConns(); 43055575Sbostic 43155575Sbostic if (GetBootFiles() == 0) 43255575Sbostic Exit(0); 43355575Sbostic 43455575Sbostic if (ParseConfig() == 0) 43555575Sbostic Exit(0); 43655575Sbostic } 43755575Sbostic 43855575Sbostic /* 43955575Sbostic ** DebugOff -- Turn off debugging. 44055575Sbostic ** 44155575Sbostic ** Parameters: 44255575Sbostic ** None. 44355575Sbostic ** 44455575Sbostic ** Returns: 44555575Sbostic ** Nothing. 44655575Sbostic ** 44755575Sbostic ** Side Effects: 44855575Sbostic ** - Debug file is closed. 44955575Sbostic */ 45055600Sbostic void 45155600Sbostic DebugOff(signo) 45255600Sbostic int signo; 45355575Sbostic { 45455575Sbostic if (DbgFp != NULL) 45555575Sbostic (void) fclose(DbgFp); 45655575Sbostic 45755575Sbostic DbgFp = NULL; 45855575Sbostic } 45955575Sbostic 46055575Sbostic /* 46155575Sbostic ** DebugOn -- Turn on debugging. 46255575Sbostic ** 46355575Sbostic ** Parameters: 46455575Sbostic ** None. 46555575Sbostic ** 46655575Sbostic ** Returns: 46755575Sbostic ** Nothing. 46855575Sbostic ** 46955575Sbostic ** Side Effects: 47055575Sbostic ** - Debug file is opened/truncated if not already opened, 47155575Sbostic ** otherwise do nothing. 47255575Sbostic */ 47355600Sbostic void 47455600Sbostic DebugOn(signo) 47555600Sbostic int signo; 47655575Sbostic { 47755575Sbostic if (DbgFp == NULL) { 47855575Sbostic if ((DbgFp = fopen(DbgFile, "w")) == NULL) 47955575Sbostic syslog(LOG_ERR, "can't open debug file (%s)", DbgFile); 48055575Sbostic } 48155575Sbostic } 482