xref: /original-bsd/libexec/rbootd/rbootd.c (revision e811aa08)
1 /*
2  * Copyright (c) 1988, 1992 The University of Utah and the Center
3  *	for Software Science (CSS).
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  * %sccs.include.redist.c%
14  *
15  *	@(#)rbootd.c	8.1 (Berkeley) 06/04/93
16  *
17  * Utah $Hdr: rbootd.c 3.1 92/07/06$
18  * Author: Jeff Forys, University of Utah CSS
19  */
20 
21 #ifndef lint
22 static char copyright[] =
23 "@(#) Copyright (c) 1992, 1993\n\
24 	The Regents of the University of California.  All rights reserved.\n";
25 #endif /* not lint */
26 
27 #ifndef lint
28 static char sccsid[] = "@(#)rbootd.c	8.1 (Berkeley) 06/04/93";
29 #endif /* not lint */
30 
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 
34 #include <ctype.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <unistd.h>
43 #include "defs.h"
44 
45 
46 /* fd mask macros (backward compatibility with 4.2BSD) */
47 #ifndef	FD_SET
48 #ifdef	notdef
49 typedef	struct fd_set {		/* this should already be in 4.2 */
50 	int fds_bits[1];
51 } fd_set;
52 #endif
53 #define	FD_ZERO(p)	((p)->fds_bits[0] = 0)
54 #define	FD_SET(n, p)	((p)->fds_bits[0] |= (1 << (n)))
55 #define	FD_CLR(n, p)	((p)->fds_bits[0] &= ~(1 << (n)))
56 #define	FD_ISSET(n, p)	((p)->fds_bits[0] & (1 << (n)))
57 #endif
58 
59 int
60 main(argc, argv)
61 	int argc;
62 	char *argv[];
63 {
64 	int c, fd, omask, maxfds;
65 	fd_set rset;
66 
67 	/*
68 	 *  Find what name we are running under.
69 	 */
70 	ProgName = (ProgName = rindex(argv[0],'/')) ? ++ProgName : *argv;
71 
72 	/*
73 	 *  Close any open file descriptors.
74 	 *  Temporarily leave stdin & stdout open for `-d',
75 	 *  and stderr open for any pre-syslog error messages.
76 	 */
77 	{
78 		int i, nfds = getdtablesize();
79 
80 		for (i = 0; i < nfds; i++)
81 			if (i != fileno(stdin) && i != fileno(stdout) &&
82 			    i != fileno(stderr))
83 				(void) close(i);
84 	}
85 
86 	/*
87 	 *  Parse any arguments.
88 	 */
89 	while ((c = getopt(argc, argv, "adi:")) != EOF)
90 		switch(c) {
91 		    case 'a':
92 			BootAny++;
93 			break;
94 		    case 'd':
95 			DebugFlg++;
96 			break;
97 		    case 'i':
98 			IntfName = optarg;
99 			break;
100 		}
101 	for (; optind < argc; optind++) {
102 		if (ConfigFile == NULL)
103 			ConfigFile = argv[optind];
104 		else {
105 			fprintf(stderr,
106 			        "%s: too many config files (`%s' ignored)\n",
107 			        ProgName, argv[optind]);
108 		}
109 	}
110 
111 	if (ConfigFile == NULL)			/* use default config file */
112 		ConfigFile = DfltConfig;
113 
114 	if (DebugFlg) {
115 		DbgFp = stdout;				/* output to stdout */
116 
117 		(void) signal(SIGUSR1, SIG_IGN);	/* dont muck w/DbgFp */
118 		(void) signal(SIGUSR2, SIG_IGN);
119 	} else {
120 		(void) fclose(stdin);			/* dont need these */
121 		(void) fclose(stdout);
122 
123 		/*
124 		 *  Fork off a child to do the work & exit.
125 		 */
126 		switch(fork()) {
127 			case -1:	/* fork failed */
128 				fprintf(stderr, "%s: ", ProgName);
129 				perror("fork");
130 				Exit(0);
131 			case 0:		/* this is the CHILD */
132 				break;
133 			default:	/* this is the PARENT */
134 				_exit(0);
135 		}
136 
137 		/*
138 		 *  Try to disassociate from the current tty.
139 		 */
140 		{
141 			char *devtty = "/dev/tty";
142 			int i;
143 
144 			if ((i = open(devtty, O_RDWR)) < 0) {
145 				/* probably already disassociated */
146 				if (setpgrp(0, 0) < 0) {
147 					fprintf(stderr, "%s: ", ProgName);
148 					perror("setpgrp");
149 				}
150 			} else {
151 				if (ioctl(i, (u_long)TIOCNOTTY, (char *)0) < 0){
152 					fprintf(stderr, "%s: ", ProgName);
153 					perror("ioctl");
154 				}
155 				(void) close(i);
156 			}
157 		}
158 
159 		(void) signal(SIGUSR1, DebugOn);
160 		(void) signal(SIGUSR2, DebugOff);
161 	}
162 
163 	(void) fclose(stderr);		/* finished with it */
164 
165 #ifdef SYSLOG4_2
166 	openlog(ProgName, LOG_PID);
167 #else
168 	openlog(ProgName, LOG_PID, LOG_DAEMON);
169 #endif
170 
171 	/*
172 	 *  If no interface was specified, get one now.
173 	 *
174 	 *  This is convoluted because we want to get the default interface
175 	 *  name for the syslog("restarted") message.  If BpfGetIntfName()
176 	 *  runs into an error, it will return a syslog-able error message
177 	 *  (in `errmsg') which will be displayed here.
178 	 */
179 	if (IntfName == NULL) {
180 		char *errmsg;
181 
182 		if ((IntfName = BpfGetIntfName(&errmsg)) == NULL) {
183 			syslog(LOG_NOTICE, "restarted (??)");
184 			syslog(LOG_ERR, errmsg);
185 			Exit(0);
186 		}
187 	}
188 
189 	syslog(LOG_NOTICE, "restarted (%s)", IntfName);
190 
191 	(void) signal(SIGHUP, ReConfig);
192 	(void) signal(SIGINT, Exit);
193 	(void) signal(SIGTERM, Exit);
194 
195 	/*
196 	 *  Grab our host name and pid.
197 	 */
198 	if (gethostname(MyHost, MAXHOSTNAMELEN) < 0) {
199 		syslog(LOG_ERR, "gethostname: %m");
200 		Exit(0);
201 	}
202 	MyHost[MAXHOSTNAMELEN] = '\0';
203 
204 	MyPid = getpid();
205 
206 	/*
207 	 *  Write proc's pid to a file.
208 	 */
209 	{
210 		FILE *fp;
211 
212 		if ((fp = fopen(PidFile, "w")) != NULL) {
213 			(void) fprintf(fp, "%d\n", MyPid);
214 			(void) fclose(fp);
215 		} else {
216 			syslog(LOG_WARNING, "fopen: failed (%s)", PidFile);
217 		}
218 	}
219 
220 	/*
221 	 *  All boot files are relative to the boot directory, we might
222 	 *  as well chdir() there to make life easier.
223 	 */
224 	if (chdir(BootDir) < 0) {
225 		syslog(LOG_ERR, "chdir: %m (%s)", BootDir);
226 		Exit(0);
227 	}
228 
229 	/*
230 	 *  Initial configuration.
231 	 */
232 	omask = sigblock(sigmask(SIGHUP));	/* prevent reconfig's */
233 	if (GetBootFiles() == 0)		/* get list of boot files */
234 		Exit(0);
235 	if (ParseConfig() == 0)			/* parse config file */
236 		Exit(0);
237 
238 	/*
239 	 *  Open and initialize a BPF device for the appropriate interface.
240 	 *  If an error is encountered, a message is displayed and Exit()
241 	 *  is called.
242 	 */
243 	fd = BpfOpen();
244 
245 	(void) sigsetmask(omask);		/* allow reconfig's */
246 
247 	/*
248 	 *  Main loop: receive a packet, determine where it came from,
249 	 *  and if we service this host, call routine to handle request.
250 	 */
251 	maxfds = fd + 1;
252 	FD_ZERO(&rset);
253 	FD_SET(fd, &rset);
254 	for (;;) {
255 		struct timeval timeout;
256 		fd_set r;
257 		int nsel;
258 
259 		r = rset;
260 
261 		if (RmpConns == NULL) {		/* timeout isnt necessary */
262 			nsel = select(maxfds, &r, (fd_set *)0, (fd_set *)0,
263 			              (struct timeval *)0);
264 		} else {
265 			timeout.tv_sec = RMP_TIMEOUT;
266 			timeout.tv_usec = 0;
267 			nsel = select(maxfds, &r, (fd_set *)0, (fd_set *)0,
268 			              &timeout);
269 		}
270 
271 		if (nsel < 0) {
272 			if (errno == EINTR)
273 				continue;
274 			syslog(LOG_ERR, "select: %m");
275 			Exit(0);
276 		} else if (nsel == 0) {		/* timeout */
277 			DoTimeout();			/* clear stale conns */
278 			continue;
279 		}
280 
281 		if (FD_ISSET(fd, &r)) {
282 			RMPCONN rconn;
283 			CLIENT *client, *FindClient();
284 			int doread = 1;
285 
286 			while (BpfRead(&rconn, doread)) {
287 				doread = 0;
288 
289 				if (DbgFp != NULL)	/* display packet */
290 					DispPkt(&rconn,DIR_RCVD);
291 
292 				omask = sigblock(sigmask(SIGHUP));
293 
294 				/*
295 				 *  If we do not restrict service, set the
296 				 *  client to NULL (ProcessPacket() handles
297 				 *  this).  Otherwise, check that we can
298 				 *  service this host; if not, log a message
299 				 *  and ignore the packet.
300 				 */
301 				if (BootAny) {
302 					client = NULL;
303 				} else if ((client=FindClient(&rconn))==NULL) {
304 					syslog(LOG_INFO,
305 					       "%s: boot packet ignored",
306 					       EnetStr(&rconn));
307 					(void) sigsetmask(omask);
308 					continue;
309 				}
310 
311 				ProcessPacket(&rconn,client);
312 
313 				(void) sigsetmask(omask);
314 			}
315 		}
316 	}
317 }
318 
319 /*
320 **  DoTimeout -- Free any connections that have timed out.
321 **
322 **	Parameters:
323 **		None.
324 **
325 **	Returns:
326 **		Nothing.
327 **
328 **	Side Effects:
329 **		- Timed out connections in `RmpConns' will be freed.
330 */
331 void
332 DoTimeout()
333 {
334 	register RMPCONN *rtmp;
335 	struct timeval now;
336 
337 	(void) gettimeofday(&now, (struct timezone *)0);
338 
339 	/*
340 	 *  For each active connection, if RMP_TIMEOUT seconds have passed
341 	 *  since the last packet was sent, delete the connection.
342 	 */
343 	for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next)
344 		if ((rtmp->tstamp.tv_sec + RMP_TIMEOUT) < now.tv_sec) {
345 			syslog(LOG_WARNING, "%s: connection timed out (%u)",
346 			       EnetStr(rtmp), rtmp->rmp.r_type);
347 			RemoveConn(rtmp);
348 		}
349 }
350 
351 /*
352 **  FindClient -- Find client associated with a packet.
353 **
354 **	Parameters:
355 **		rconn - the new packet.
356 **
357 **	Returns:
358 **		Pointer to client info if found, NULL otherwise.
359 **
360 **	Side Effects:
361 **		None.
362 **
363 **	Warnings:
364 **		- This routine must be called with SIGHUP blocked since
365 **		  a reconfigure can invalidate the information returned.
366 */
367 
368 CLIENT *
369 FindClient(rconn)
370 	register RMPCONN *rconn;
371 {
372 	register CLIENT *ctmp;
373 
374 	for (ctmp = Clients; ctmp != NULL; ctmp = ctmp->next)
375 		if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0],
376 		         (char *)&ctmp->addr[0], RMP_ADDRLEN) == 0)
377 			break;
378 
379 	return(ctmp);
380 }
381 
382 /*
383 **  Exit -- Log an error message and exit.
384 **
385 **	Parameters:
386 **		sig - caught signal (or zero if not dying on a signal).
387 **
388 **	Returns:
389 **		Does not return.
390 **
391 **	Side Effects:
392 **		- This process ceases to exist.
393 */
394 void
395 Exit(sig)
396 	int sig;
397 {
398 	if (sig > 0)
399 		syslog(LOG_ERR, "going down on signal %d", sig);
400 	else
401 		syslog(LOG_ERR, "going down with fatal error");
402 	BpfClose();
403 	exit(1);
404 }
405 
406 /*
407 **  ReConfig -- Get new list of boot files and reread config files.
408 **
409 **	Parameters:
410 **		None.
411 **
412 **	Returns:
413 **		Nothing.
414 **
415 **	Side Effects:
416 **		- All active connections are dropped.
417 **		- List of boot-able files is changed.
418 **		- List of clients is changed.
419 **
420 **	Warnings:
421 **		- This routine must be called with SIGHUP blocked.
422 */
423 void
424 ReConfig(signo)
425 	int signo;
426 {
427 	syslog(LOG_NOTICE, "reconfiguring boot server");
428 
429 	FreeConns();
430 
431 	if (GetBootFiles() == 0)
432 		Exit(0);
433 
434 	if (ParseConfig() == 0)
435 		Exit(0);
436 }
437 
438 /*
439 **  DebugOff -- Turn off debugging.
440 **
441 **	Parameters:
442 **		None.
443 **
444 **	Returns:
445 **		Nothing.
446 **
447 **	Side Effects:
448 **		- Debug file is closed.
449 */
450 void
451 DebugOff(signo)
452 	int signo;
453 {
454 	if (DbgFp != NULL)
455 		(void) fclose(DbgFp);
456 
457 	DbgFp = NULL;
458 }
459 
460 /*
461 **  DebugOn -- Turn on debugging.
462 **
463 **	Parameters:
464 **		None.
465 **
466 **	Returns:
467 **		Nothing.
468 **
469 **	Side Effects:
470 **		- Debug file is opened/truncated if not already opened,
471 **		  otherwise do nothing.
472 */
473 void
474 DebugOn(signo)
475 	int signo;
476 {
477 	if (DbgFp == NULL) {
478 		if ((DbgFp = fopen(DbgFile, "w")) == NULL)
479 			syslog(LOG_ERR, "can't open debug file (%s)", DbgFile);
480 	}
481 }
482