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