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