/src/dnsmasq/src/dnsmasq.c
Line | Count | Source |
1 | | /* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
2 | | |
3 | | This program is free software; you can redistribute it and/or modify |
4 | | it under the terms of the GNU General Public License as published by |
5 | | the Free Software Foundation; version 2 dated June, 1991, or |
6 | | (at your option) version 3 dated 29 June, 2007. |
7 | | |
8 | | This program is distributed in the hope that it will be useful, |
9 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | | GNU General Public License for more details. |
12 | | |
13 | | You should have received a copy of the GNU General Public License |
14 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | | */ |
16 | | |
17 | | /* Declare static char *compiler_opts in config.h */ |
18 | | #define DNSMASQ_COMPILE_OPTS |
19 | | |
20 | | /* dnsmasq.h has to be included first as it sources config.h */ |
21 | | #include "dnsmasq.h" |
22 | | |
23 | | #if defined(HAVE_IDN) || defined(HAVE_LIBIDN2) || defined(LOCALEDIR) |
24 | | #include <locale.h> |
25 | | #endif |
26 | | |
27 | | struct daemon *daemon; |
28 | | |
29 | | static volatile pid_t pid = 0; |
30 | | static volatile int pipewrite; |
31 | | |
32 | | static void set_dns_listeners(void); |
33 | | #ifdef HAVE_TFTP |
34 | | static void set_tftp_listeners(void); |
35 | | #endif |
36 | | static void check_dns_listeners(time_t now); |
37 | | static void do_tcp_connection(struct listener *listener, time_t now, int slot); |
38 | | static void sig_handler(int sig); |
39 | | static void async_event(int pipe, time_t now); |
40 | | static void fatal_event(struct event_desc *ev, char *msg); |
41 | | static int read_event(int fd, struct event_desc *evp, char **msg); |
42 | | static void poll_resolv(int force, int do_reload, time_t now); |
43 | | |
44 | | int main2 (int argc, char **argv) |
45 | 0 | { |
46 | 0 | time_t now; |
47 | 0 | struct sigaction sigact; |
48 | 0 | struct iname *if_tmp; |
49 | 0 | int piperead, pipefd[2], err_pipe[2]; |
50 | 0 | struct passwd *ent_pw = NULL; |
51 | 0 | #if defined(HAVE_SCRIPT) |
52 | 0 | uid_t script_uid = 0; |
53 | 0 | gid_t script_gid = 0; |
54 | 0 | #endif |
55 | 0 | struct group *gp = NULL; |
56 | 0 | long i, max_fd = sysconf(_SC_OPEN_MAX); |
57 | 0 | char *baduser = NULL; |
58 | 0 | int log_err; |
59 | 0 | int chown_warn = 0; |
60 | 0 | #if defined(HAVE_LINUX_NETWORK) |
61 | 0 | cap_user_header_t hdr = NULL; |
62 | 0 | cap_user_data_t data = NULL; |
63 | 0 | int need_cap_net_admin = 0; |
64 | 0 | int need_cap_net_raw = 0; |
65 | 0 | int need_cap_net_bind_service = 0; |
66 | 0 | int have_cap_chown = 0; |
67 | 0 | # ifdef HAVE_DHCP |
68 | 0 | char *bound_device = NULL; |
69 | 0 | int did_bind = 0; |
70 | 0 | # endif |
71 | 0 | struct server *serv; |
72 | 0 | char *netlink_warn; |
73 | | #else |
74 | | int bind_fallback = 0; |
75 | | #endif |
76 | 0 | #if defined(HAVE_DHCP) |
77 | 0 | struct dhcp_context *context; |
78 | 0 | struct dhcp_relay *relay; |
79 | 0 | #endif |
80 | 0 | #ifdef HAVE_TFTP |
81 | 0 | int tftp_prefix_missing = 0; |
82 | 0 | #endif |
83 | |
|
84 | 0 | #ifdef HAVE_LINUX_NETWORK |
85 | 0 | (void)netlink_warn; |
86 | 0 | #endif |
87 | | |
88 | | #if defined(HAVE_IDN) || defined(HAVE_LIBIDN2) || defined(LOCALEDIR) |
89 | | setlocale(LC_ALL, ""); |
90 | | #endif |
91 | | #ifdef LOCALEDIR |
92 | | bindtextdomain("dnsmasq", LOCALEDIR); |
93 | | textdomain("dnsmasq"); |
94 | | #endif |
95 | |
|
96 | 0 | sigact.sa_handler = sig_handler; |
97 | 0 | sigact.sa_flags = 0; |
98 | 0 | sigemptyset(&sigact.sa_mask); |
99 | 0 | sigaction(SIGUSR1, &sigact, NULL); |
100 | 0 | sigaction(SIGUSR2, &sigact, NULL); |
101 | 0 | sigaction(SIGHUP, &sigact, NULL); |
102 | 0 | sigaction(SIGTERM, &sigact, NULL); |
103 | 0 | sigaction(SIGALRM, &sigact, NULL); |
104 | 0 | sigaction(SIGCHLD, &sigact, NULL); |
105 | 0 | sigaction(SIGINT, &sigact, NULL); |
106 | | |
107 | | /* ignore SIGPIPE */ |
108 | 0 | sigact.sa_handler = SIG_IGN; |
109 | 0 | sigaction(SIGPIPE, &sigact, NULL); |
110 | |
|
111 | 0 | umask(022); /* known umask, create leases and pid files as 0644 */ |
112 | |
|
113 | 0 | rand_init(); /* Must precede read_opts() */ |
114 | | |
115 | 0 | read_opts(argc, argv, compile_opts); |
116 | | |
117 | 0 | #ifdef HAVE_LINUX_NETWORK |
118 | 0 | daemon->kernel_version = kernel_version(); |
119 | 0 | #endif |
120 | |
|
121 | 0 | if (daemon->edns_pktsz < PACKETSZ) |
122 | 0 | daemon->edns_pktsz = PACKETSZ; |
123 | | |
124 | | /* Min buffer size: we check after adding each record, so there must be |
125 | | memory for the largest packet, and the largest record so the |
126 | | min for DNS is PACKETSZ+MAXDNAME+RRFIXEDSZ which is < 1000. |
127 | | This might be increased is EDNS packet size if greater than the minimum. */ |
128 | 0 | daemon->packet_buff_sz = daemon->edns_pktsz + MAXDNAME + RRFIXEDSZ; |
129 | 0 | daemon->packet = safe_malloc(daemon->packet_buff_sz); |
130 | 0 | daemon->pipe_to_parent = -1; |
131 | | |
132 | 0 | if (option_bool(OPT_EXTRALOG)) |
133 | 0 | daemon->addrbuff2 = safe_malloc(ADDRSTRLEN); |
134 | | |
135 | | #ifdef HAVE_DNSSEC |
136 | | if (option_bool(OPT_DNSSEC_VALID)) |
137 | | { |
138 | | daemon->keyname = safe_malloc(MAXDNAMESTR + 1); |
139 | | daemon->cname = safe_malloc(MAXDNAMESTR + 1); |
140 | | /* one char flag per possible RR in answer section (may get extended). */ |
141 | | daemon->rr_status_sz = 64; |
142 | | daemon->rr_status = safe_malloc(sizeof(*daemon->rr_status) * daemon->rr_status_sz); |
143 | | /* Increase TCP process slots by half, and reserve the extra for validation-to-tcp use only. */ |
144 | | daemon->dnssec_tcp_slot_reserved = daemon->max_procs/2; |
145 | | daemon->max_procs += daemon->dnssec_tcp_slot_reserved; |
146 | | } |
147 | | #endif |
148 | | |
149 | 0 | #ifdef HAVE_DHCP |
150 | 0 | if (!daemon->lease_file) |
151 | 0 | { |
152 | 0 | if (daemon->dhcp || daemon->dhcp6) |
153 | 0 | daemon->lease_file = LEASEFILE; |
154 | 0 | } |
155 | 0 | #endif |
156 | | |
157 | | /* Ensure that at least stdin, stdout and stderr (fd 0, 1, 2) exist, |
158 | | otherwise file descriptors we create can end up being 0, 1, or 2 |
159 | | and then get accidentally closed later when we make 0, 1, and 2 |
160 | | open to /dev/null. Normally we'll be started with 0, 1 and 2 open, |
161 | | but it's not guaranteed. By opening /dev/null three times, we |
162 | | ensure that we're not using those fds for real stuff. */ |
163 | 0 | for (i = 0; i < 3; i++) |
164 | 0 | open("/dev/null", O_RDWR); |
165 | | |
166 | | /* Close any file descriptors we inherited apart from std{in|out|err} */ |
167 | 0 | close_fds(max_fd, -1, -1, -1); |
168 | | |
169 | | #ifndef HAVE_LINUX_NETWORK |
170 | | # if !(defined(IP_RECVDSTADDR) && defined(IP_RECVIF) && defined(IP_SENDSRCADDR)) |
171 | | if (!option_bool(OPT_NOWILD)) |
172 | | { |
173 | | bind_fallback = 1; |
174 | | set_option_bool(OPT_NOWILD); |
175 | | } |
176 | | # endif |
177 | | |
178 | | /* -- bind-dynamic not supported on !Linux, fall back to --bind-interfaces */ |
179 | | if (option_bool(OPT_CLEVERBIND)) |
180 | | { |
181 | | bind_fallback = 1; |
182 | | set_option_bool(OPT_NOWILD); |
183 | | reset_option_bool(OPT_CLEVERBIND); |
184 | | } |
185 | | #endif |
186 | |
|
187 | | #ifndef HAVE_INOTIFY |
188 | | if (daemon->dynamic_dirs) |
189 | | die(_("dhcp-hostsdir, dhcp-optsdir and hostsdir are not supported on this platform"), NULL, EC_BADCONF); |
190 | | #endif |
191 | | |
192 | 0 | if (option_bool(OPT_DNSSEC_VALID)) |
193 | 0 | { |
194 | | #ifdef HAVE_DNSSEC |
195 | | struct ds_config *ds; |
196 | | |
197 | | /* Must have at least a root trust anchor, or the DNSSEC code |
198 | | can loop forever. */ |
199 | | for (ds = daemon->ds; ds; ds = ds->next) |
200 | | if (ds->name && ds->name[0] == 0) |
201 | | break; |
202 | | |
203 | | if (!ds) |
204 | | die(_("no root trust anchor provided for DNSSEC"), NULL, EC_BADCONF); |
205 | | |
206 | | if (daemon->cachesize < CACHESIZ) |
207 | | die(_("cannot reduce cache size from default when DNSSEC enabled"), NULL, EC_BADCONF); |
208 | | #else |
209 | 0 | die(_("DNSSEC not available: set HAVE_DNSSEC in src/config.h"), NULL, EC_BADCONF); |
210 | 0 | #endif |
211 | 0 | } |
212 | | |
213 | | #ifndef HAVE_TFTP |
214 | | if (option_bool(OPT_TFTP)) |
215 | | die(_("TFTP server not available: set HAVE_TFTP in src/config.h"), NULL, EC_BADCONF); |
216 | | #endif |
217 | | |
218 | | #ifdef HAVE_CONNTRACK |
219 | | if (option_bool(OPT_CONNTRACK)) |
220 | | { |
221 | | if (daemon->query_port != 0 || daemon->osport) |
222 | | die (_("cannot use --conntrack AND --query-port"), NULL, EC_BADCONF); |
223 | | |
224 | | need_cap_net_admin = 1; |
225 | | } |
226 | | #else |
227 | 0 | if (option_bool(OPT_CONNTRACK)) |
228 | 0 | die(_("conntrack support not available: set HAVE_CONNTRACK in src/config.h"), NULL, EC_BADCONF); |
229 | 0 | #endif |
230 | | |
231 | | #ifdef HAVE_SOLARIS_NETWORK |
232 | | if (daemon->max_logs != 0) |
233 | | die(_("asynchronous logging is not available under Solaris"), NULL, EC_BADCONF); |
234 | | #endif |
235 | | |
236 | | #ifdef __ANDROID__ |
237 | | if (daemon->max_logs != 0) |
238 | | die(_("asynchronous logging is not available under Android"), NULL, EC_BADCONF); |
239 | | #endif |
240 | | |
241 | | #ifndef HAVE_AUTH |
242 | | if (daemon->auth_zones) |
243 | | die(_("authoritative DNS not available: set HAVE_AUTH in src/config.h"), NULL, EC_BADCONF); |
244 | | #endif |
245 | | |
246 | | #ifndef HAVE_LOOP |
247 | | if (option_bool(OPT_LOOP_DETECT)) |
248 | | die(_("loop detection not available: set HAVE_LOOP in src/config.h"), NULL, EC_BADCONF); |
249 | | #endif |
250 | | |
251 | 0 | #ifndef HAVE_UBUS |
252 | 0 | if (option_bool(OPT_UBUS)) |
253 | 0 | die(_("Ubus not available: set HAVE_UBUS in src/config.h"), NULL, EC_BADCONF); |
254 | 0 | #endif |
255 | | |
256 | | /* Handle only one of min_port/max_port being set. */ |
257 | 0 | if (daemon->min_port != 0 && daemon->max_port == 0) |
258 | 0 | daemon->max_port = MAX_PORT; |
259 | | |
260 | 0 | if (daemon->max_port != 0 && daemon->min_port == 0) |
261 | 0 | daemon->min_port = MIN_PORT; |
262 | | |
263 | 0 | if (daemon->max_port < daemon->min_port) |
264 | 0 | die(_("max_port cannot be smaller than min_port"), NULL, EC_BADCONF); |
265 | | |
266 | 0 | if (daemon->max_port != 0 && |
267 | 0 | daemon->max_port - daemon->min_port + 1 < daemon->randport_limit) |
268 | 0 | die(_("port_limit must not be larger than available port range"), NULL, EC_BADCONF); |
269 | | |
270 | 0 | now = dnsmasq_time(); |
271 | |
|
272 | 0 | if (daemon->auth_zones) |
273 | 0 | { |
274 | 0 | if (!daemon->authserver) |
275 | 0 | die(_("--auth-server required when an auth zone is defined."), NULL, EC_BADCONF); |
276 | | |
277 | | /* Create a serial at startup if not configured. */ |
278 | | #ifdef HAVE_BROKEN_RTC |
279 | | if (daemon->soa_sn == 0) |
280 | | die(_("zone serial must be configured in --auth-soa"), NULL, EC_BADCONF); |
281 | | #else |
282 | 0 | if (daemon->soa_sn == 0) |
283 | 0 | daemon->soa_sn = now; |
284 | 0 | #endif |
285 | 0 | } |
286 | | |
287 | 0 | #ifdef HAVE_DHCP6 |
288 | 0 | if (daemon->dhcp6) |
289 | 0 | { |
290 | 0 | daemon->doing_ra = option_bool(OPT_RA); |
291 | | |
292 | 0 | for (context = daemon->dhcp6; context; context = context->next) |
293 | 0 | { |
294 | 0 | if (context->flags & CONTEXT_DHCP) |
295 | 0 | daemon->doing_dhcp6 = 1; |
296 | 0 | if (context->flags & CONTEXT_RA) |
297 | 0 | daemon->doing_ra = 1; |
298 | | #if !defined(HAVE_LINUX_NETWORK) && !defined(HAVE_BSD_NETWORK) |
299 | | if (context->flags & CONTEXT_TEMPLATE) |
300 | | die (_("dhcp-range constructor not available on this platform"), NULL, EC_BADCONF); |
301 | | #endif |
302 | 0 | } |
303 | 0 | } |
304 | 0 | #endif |
305 | | |
306 | 0 | #ifdef HAVE_DHCP |
307 | | /* Note that order matters here, we must call lease_init before |
308 | | creating any file descriptors which shouldn't be leaked |
309 | | to the lease-script init process. We need to call common_init |
310 | | before lease_init to allocate buffers it uses. |
311 | | The script subsystem relies on DHCP buffers, hence the last two |
312 | | conditions below. */ |
313 | 0 | if (daemon->dhcp || daemon->doing_dhcp6 || daemon->relay4 || |
314 | 0 | daemon->relay6 || option_bool(OPT_TFTP) || option_bool(OPT_SCRIPT_ARP)) |
315 | 0 | { |
316 | 0 | dhcp_common_init(); |
317 | 0 | if (daemon->dhcp || daemon->doing_dhcp6) |
318 | 0 | lease_init(now); |
319 | 0 | } |
320 | | |
321 | 0 | if (daemon->dhcp || daemon->relay4) |
322 | 0 | { |
323 | 0 | dhcp_init(); |
324 | 0 | # ifdef HAVE_LINUX_NETWORK |
325 | | /* Need NET_RAW to send ping. */ |
326 | 0 | if (!option_bool(OPT_NO_PING)) |
327 | 0 | need_cap_net_raw = 1; |
328 | | /* Need NET_ADMIN to change ARP cache if not always broadcasting. */ |
329 | 0 | if (daemon->force_broadcast == NULL || daemon->force_broadcast->list != NULL) |
330 | 0 | need_cap_net_admin = 1; |
331 | 0 | # endif |
332 | 0 | } |
333 | | |
334 | 0 | # ifdef HAVE_DHCP6 |
335 | 0 | if (daemon->doing_ra || daemon->doing_dhcp6 || daemon->relay6) |
336 | 0 | { |
337 | 0 | ra_init(now); |
338 | 0 | # ifdef HAVE_LINUX_NETWORK |
339 | 0 | need_cap_net_raw = 1; |
340 | 0 | need_cap_net_admin = 1; |
341 | 0 | # endif |
342 | 0 | } |
343 | | |
344 | 0 | if (daemon->doing_dhcp6 || daemon->relay6) |
345 | 0 | dhcp6_init(); |
346 | 0 | # endif |
347 | |
|
348 | 0 | #endif |
349 | |
|
350 | 0 | #ifdef HAVE_IPSET |
351 | 0 | if (daemon->ipsets) |
352 | 0 | { |
353 | 0 | ipset_init(); |
354 | 0 | # ifdef HAVE_LINUX_NETWORK |
355 | 0 | need_cap_net_admin = 1; |
356 | 0 | # endif |
357 | 0 | } |
358 | 0 | #endif |
359 | |
|
360 | | #ifdef HAVE_NFTSET |
361 | | if (daemon->nftsets) |
362 | | { |
363 | | nftset_init(); |
364 | | # ifdef HAVE_LINUX_NETWORK |
365 | | need_cap_net_admin = 1; |
366 | | # endif |
367 | | } |
368 | | #endif |
369 | |
|
370 | 0 | #if defined(HAVE_LINUX_NETWORK) |
371 | 0 | netlink_warn = netlink_init(); |
372 | | #elif defined(HAVE_BSD_NETWORK) |
373 | | route_init(); |
374 | | #endif |
375 | |
|
376 | 0 | if (option_bool(OPT_NOWILD) && option_bool(OPT_CLEVERBIND)) |
377 | 0 | die(_("cannot set --bind-interfaces and --bind-dynamic"), NULL, EC_BADCONF); |
378 | | |
379 | 0 | if (!enumerate_interfaces(1) || !enumerate_interfaces(0)) |
380 | 0 | die(_("failed to find list of interfaces: %s"), NULL, EC_MISC); |
381 | | |
382 | 0 | #ifdef HAVE_DHCP |
383 | | /* Determine lease FQDNs after enumerate_interfaces() call, since it needs |
384 | | to call get_domain and that's only valid for some domain configs once we |
385 | | have interface addresses. */ |
386 | 0 | lease_calc_fqdns(); |
387 | 0 | #endif |
388 | | |
389 | 0 | if (option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND)) |
390 | 0 | { |
391 | 0 | create_bound_listeners(1); |
392 | | |
393 | 0 | if (!option_bool(OPT_CLEVERBIND)) |
394 | 0 | for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next) |
395 | 0 | if (if_tmp->name && !(if_tmp->flags & INAME_USED)) |
396 | 0 | die(_("unknown interface %s"), if_tmp->name, EC_BADNET); |
397 | | |
398 | 0 | #if defined(HAVE_LINUX_NETWORK) && defined(HAVE_DHCP) |
399 | | /* after enumerate_interfaces() */ |
400 | 0 | bound_device = whichdevice(); |
401 | |
|
402 | 0 | if ((did_bind = bind_dhcp_devices(bound_device)) & 2) |
403 | 0 | die(_("failed to set SO_BINDTODEVICE on DHCP socket: %s"), NULL, EC_BADNET); |
404 | 0 | #endif |
405 | 0 | } |
406 | 0 | else |
407 | 0 | create_wildcard_listeners(); |
408 | | |
409 | 0 | #ifdef HAVE_DHCP6 |
410 | | /* after enumerate_interfaces() */ |
411 | 0 | if (daemon->doing_dhcp6 || daemon->relay6 || daemon->doing_ra) |
412 | 0 | join_multicast(1); |
413 | | |
414 | | /* After netlink_init() and before create_helper() */ |
415 | 0 | lease_make_duid(now); |
416 | 0 | #endif |
417 | | |
418 | 0 | if (daemon->port != 0) |
419 | 0 | { |
420 | 0 | cache_init(); |
421 | 0 | blockdata_init(); |
422 | | |
423 | | /* Scale random socket pool by ftabsize, but |
424 | | limit it based on available fds. */ |
425 | 0 | daemon->numrrand = daemon->ftabsize/2; |
426 | 0 | if (daemon->numrrand > max_fd/3) |
427 | 0 | daemon->numrrand = max_fd/3; |
428 | | /* safe_malloc returns zero'd memory */ |
429 | 0 | daemon->randomsocks = safe_malloc(daemon->numrrand * sizeof(struct randfd)); |
430 | |
|
431 | 0 | daemon->tcp_pids = safe_malloc(daemon->max_procs*sizeof(pid_t)); |
432 | 0 | daemon->tcp_pipes = safe_malloc(daemon->max_procs*sizeof(int)); |
433 | 0 | daemon->tcp_type = safe_malloc(daemon->max_procs); |
434 | | |
435 | 0 | for (i = 0; i < daemon->max_procs; i++) |
436 | 0 | daemon->tcp_pipes[i] = -1; |
437 | 0 | } |
438 | |
|
439 | 0 | if (daemon->dump_file) |
440 | 0 | #ifdef HAVE_DUMPFILE |
441 | 0 | dump_init(); |
442 | 0 | else |
443 | 0 | daemon->dumpfd = -1; |
444 | | #else |
445 | | die(_("Packet dumps not available: set HAVE_DUMP in src/config.h"), NULL, EC_BADCONF); |
446 | | #endif |
447 | | |
448 | 0 | if (option_bool(OPT_DBUS)) |
449 | | #ifdef HAVE_DBUS |
450 | | { |
451 | | char *err; |
452 | | if ((err = dbus_init())) |
453 | | die(_("DBus error: %s"), err, EC_MISC); |
454 | | } |
455 | | #else |
456 | 0 | die(_("DBus not available: set HAVE_DBUS in src/config.h"), NULL, EC_BADCONF); |
457 | 0 | #endif |
458 | | |
459 | 0 | if (option_bool(OPT_UBUS)) |
460 | | #ifdef HAVE_UBUS |
461 | | { |
462 | | char *err; |
463 | | if ((err = ubus_init())) |
464 | | die(_("UBus error: %s"), err, EC_MISC); |
465 | | } |
466 | | #else |
467 | 0 | die(_("UBus not available: set HAVE_UBUS in src/config.h"), NULL, EC_BADCONF); |
468 | 0 | #endif |
469 | | |
470 | 0 | if (daemon->port != 0) |
471 | 0 | pre_allocate_sfds(); |
472 | |
|
473 | 0 | #if defined(HAVE_SCRIPT) |
474 | | /* Note getpwnam returns static storage */ |
475 | 0 | if ((daemon->dhcp || daemon->dhcp6) && |
476 | 0 | daemon->scriptuser && |
477 | 0 | (daemon->lease_change_command || daemon->luascript)) |
478 | 0 | { |
479 | 0 | struct passwd *scr_pw; |
480 | | |
481 | 0 | if ((scr_pw = getpwnam(daemon->scriptuser))) |
482 | 0 | { |
483 | 0 | script_uid = scr_pw->pw_uid; |
484 | 0 | script_gid = scr_pw->pw_gid; |
485 | 0 | } |
486 | 0 | else |
487 | 0 | baduser = daemon->scriptuser; |
488 | 0 | } |
489 | 0 | #endif |
490 | | |
491 | 0 | if (daemon->username && !(ent_pw = getpwnam(daemon->username))) |
492 | 0 | baduser = daemon->username; |
493 | 0 | else if (daemon->groupname && !(gp = getgrnam(daemon->groupname))) |
494 | 0 | baduser = daemon->groupname; |
495 | |
|
496 | 0 | if (baduser) |
497 | 0 | die(_("unknown user or group: %s"), baduser, EC_BADCONF); |
498 | | |
499 | | /* implement group defaults, "dip" if available, or group associated with uid */ |
500 | 0 | if (!daemon->group_set && !gp) |
501 | 0 | { |
502 | 0 | if (!(gp = getgrnam(CHGRP)) && ent_pw) |
503 | 0 | gp = getgrgid(ent_pw->pw_gid); |
504 | | |
505 | | /* for error message */ |
506 | 0 | if (gp) |
507 | 0 | daemon->groupname = gp->gr_name; |
508 | 0 | } |
509 | |
|
510 | 0 | #if defined(HAVE_LINUX_NETWORK) |
511 | | /* We keep CAP_NETADMIN (for ARP-injection) and |
512 | | CAP_NET_RAW (for icmp) if we're doing dhcp, |
513 | | if we have yet to bind ports because of DAD, |
514 | | or we're doing it dynamically, we need CAP_NET_BIND_SERVICE. */ |
515 | 0 | if ((is_dad_listeners() || option_bool(OPT_CLEVERBIND)) && |
516 | 0 | (option_bool(OPT_TFTP) || (daemon->port != 0 && daemon->port <= 1024))) |
517 | 0 | need_cap_net_bind_service = 1; |
518 | | |
519 | | /* usptream servers which bind to an interface call SO_BINDTODEVICE |
520 | | for each TCP connection, so need CAP_NET_RAW */ |
521 | 0 | for (serv = daemon->servers; serv; serv = serv->next) |
522 | 0 | if (serv->interface[0] != 0) |
523 | 0 | need_cap_net_raw = 1; |
524 | | |
525 | | /* If we're doing Dbus or UBus, the above can be set dynamically, |
526 | | (as can ports) so always (potentially) needed. */ |
527 | | #ifdef HAVE_DBUS |
528 | | if (option_bool(OPT_DBUS)) |
529 | | { |
530 | | need_cap_net_bind_service = 1; |
531 | | need_cap_net_raw = 1; |
532 | | } |
533 | | #endif |
534 | |
|
535 | | #ifdef HAVE_UBUS |
536 | | if (option_bool(OPT_UBUS)) |
537 | | { |
538 | | need_cap_net_bind_service = 1; |
539 | | need_cap_net_raw = 1; |
540 | | } |
541 | | #endif |
542 | | |
543 | | /* determine capability API version here, while we can still |
544 | | call safe_malloc */ |
545 | 0 | int capsize = 1; /* for header version 1 */ |
546 | 0 | char *fail = NULL; |
547 | | |
548 | 0 | hdr = safe_malloc(sizeof(*hdr)); |
549 | | |
550 | | /* find version supported by kernel */ |
551 | 0 | memset(hdr, 0, sizeof(*hdr)); |
552 | 0 | capget(hdr, NULL); |
553 | | |
554 | 0 | if (hdr->version != LINUX_CAPABILITY_VERSION_1) |
555 | 0 | { |
556 | | /* if unknown version, use largest supported version (3) */ |
557 | 0 | if (hdr->version != LINUX_CAPABILITY_VERSION_2) |
558 | 0 | hdr->version = LINUX_CAPABILITY_VERSION_3; |
559 | 0 | capsize = 2; |
560 | 0 | } |
561 | | |
562 | 0 | data = safe_malloc(sizeof(*data) * capsize); |
563 | 0 | capget(hdr, data); /* Get current values, for verification */ |
564 | |
|
565 | 0 | have_cap_chown = data->permitted & (1 << CAP_CHOWN); |
566 | |
|
567 | 0 | if (need_cap_net_admin && !(data->permitted & (1 << CAP_NET_ADMIN))) |
568 | 0 | fail = "NET_ADMIN"; |
569 | 0 | else if (need_cap_net_raw && !(data->permitted & (1 << CAP_NET_RAW))) |
570 | 0 | fail = "NET_RAW"; |
571 | 0 | else if (need_cap_net_bind_service && !(data->permitted & (1 << CAP_NET_BIND_SERVICE))) |
572 | 0 | fail = "NET_BIND_SERVICE"; |
573 | | |
574 | 0 | if (fail) |
575 | 0 | die(_("process is missing required capability %s"), fail, EC_MISC); |
576 | | |
577 | | /* Now set bitmaps to set caps after daemonising */ |
578 | 0 | memset(data, 0, sizeof(*data) * capsize); |
579 | | |
580 | 0 | if (need_cap_net_admin) |
581 | 0 | data->effective |= (1 << CAP_NET_ADMIN); |
582 | 0 | if (need_cap_net_raw) |
583 | 0 | data->effective |= (1 << CAP_NET_RAW); |
584 | 0 | if (need_cap_net_bind_service) |
585 | 0 | data->effective |= (1 << CAP_NET_BIND_SERVICE); |
586 | | |
587 | 0 | data->permitted = data->effective; |
588 | 0 | #endif |
589 | | |
590 | | /* Use a pipe to carry signals and other events back to the event loop |
591 | | in a race-free manner and another to carry errors to daemon-invoking process */ |
592 | 0 | safe_pipe(pipefd, 1); |
593 | | |
594 | 0 | piperead = pipefd[0]; |
595 | 0 | pipewrite = pipefd[1]; |
596 | | /* prime the pipe to load stuff first time. */ |
597 | 0 | send_event(pipewrite, EVENT_INIT, 0, NULL); |
598 | |
|
599 | 0 | err_pipe[1] = -1; |
600 | | |
601 | 0 | if (!option_bool(OPT_DEBUG)) |
602 | 0 | { |
603 | | /* The following code "daemonizes" the process. |
604 | | See Stevens section 12.4 */ |
605 | | |
606 | 0 | if (chdir("/") != 0) |
607 | 0 | die(_("cannot chdir to filesystem root: %s"), NULL, EC_MISC); |
608 | | |
609 | 0 | if (!option_bool(OPT_NO_FORK)) |
610 | 0 | { |
611 | 0 | pid_t pid; |
612 | | |
613 | | /* pipe to carry errors back to original process. |
614 | | When startup is complete we close this and the process terminates. */ |
615 | 0 | safe_pipe(err_pipe, 0); |
616 | | |
617 | 0 | if ((pid = fork()) == -1) |
618 | | /* fd == -1 since we've not forked, never returns. */ |
619 | 0 | send_event(-1, EVENT_FORK_ERR, errno, NULL); |
620 | | |
621 | 0 | if (pid != 0) |
622 | 0 | { |
623 | 0 | struct event_desc ev; |
624 | 0 | char *msg; |
625 | | |
626 | | /* close our copy of write-end */ |
627 | 0 | close(err_pipe[1]); |
628 | | |
629 | | /* check for errors after the fork */ |
630 | 0 | if (read_event(err_pipe[0], &ev, &msg)) |
631 | 0 | fatal_event(&ev, msg); |
632 | | |
633 | 0 | _exit(EC_GOOD); |
634 | 0 | } |
635 | | |
636 | 0 | close(err_pipe[0]); |
637 | | |
638 | | /* NO calls to die() from here on. */ |
639 | | |
640 | 0 | setsid(); |
641 | | |
642 | 0 | if ((pid = fork()) == -1) |
643 | 0 | send_event(err_pipe[1], EVENT_FORK_ERR, errno, NULL); |
644 | | |
645 | 0 | if (pid != 0) |
646 | 0 | _exit(0); |
647 | 0 | } |
648 | | |
649 | | /* write pidfile _after_ forking ! */ |
650 | 0 | if (daemon->runfile) |
651 | 0 | { |
652 | 0 | int fd, err = 0; |
653 | |
|
654 | 0 | sprintf(daemon->namebuff, "%d\n", (int) getpid()); |
655 | | |
656 | | /* Explanation: Some installations of dnsmasq (eg Debian/Ubuntu) locate the pid-file |
657 | | in a directory which is writable by the non-privileged user that dnsmasq runs as. This |
658 | | allows the daemon to delete the file as part of its shutdown. This is a security hole to the |
659 | | extent that an attacker running as the unprivileged user could replace the pidfile with a |
660 | | symlink, and have the target of that symlink overwritten as root next time dnsmasq starts. |
661 | | |
662 | | The following code first deletes any existing file, and then opens it with the O_EXCL flag, |
663 | | ensuring that the open() fails should there be any existing file (because the unlink() failed, |
664 | | or an attacker exploited the race between unlink() and open()). This ensures that no symlink |
665 | | attack can succeed. |
666 | | |
667 | | Any compromise of the non-privileged user still theoretically allows the pid-file to be |
668 | | replaced whilst dnsmasq is running. The worst that could allow is that the usual |
669 | | "shutdown dnsmasq" shell command could be tricked into stopping any other process. |
670 | | |
671 | | Note that if dnsmasq is started as non-root (eg for testing) it silently ignores |
672 | | failure to write the pid-file. |
673 | | */ |
674 | |
|
675 | 0 | unlink(daemon->runfile); |
676 | | |
677 | 0 | if ((fd = open(daemon->runfile, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)) == -1) |
678 | 0 | { |
679 | | /* only complain if started as root */ |
680 | 0 | if (getuid() == 0) |
681 | 0 | err = 1; |
682 | 0 | } |
683 | 0 | else |
684 | 0 | { |
685 | | /* We're still running as root here. Change the ownership of the PID file |
686 | | to the user we will be running as. Note that this is not to allow |
687 | | us to delete the file, since that depends on the permissions |
688 | | of the directory containing the file. That directory will |
689 | | need to by owned by the dnsmasq user, and the ownership of the |
690 | | file has to match, to keep systemd >273 happy. */ |
691 | 0 | if (getuid() == 0 && ent_pw && ent_pw->pw_uid != 0 && fchown(fd, ent_pw->pw_uid, ent_pw->pw_gid) == -1) |
692 | 0 | chown_warn = errno; |
693 | |
|
694 | 0 | if (!read_write(fd, (unsigned char *)daemon->namebuff, strlen(daemon->namebuff), RW_WRITE)) |
695 | 0 | err = 1; |
696 | 0 | else |
697 | 0 | { |
698 | 0 | if (close(fd) == -1) |
699 | 0 | err = 1; |
700 | 0 | } |
701 | 0 | } |
702 | |
|
703 | 0 | if (err) |
704 | 0 | { |
705 | 0 | send_event(err_pipe[1], EVENT_PIDFILE, errno, daemon->runfile); |
706 | 0 | _exit(0); |
707 | 0 | } |
708 | 0 | } |
709 | 0 | } |
710 | | |
711 | 0 | log_err = log_start(ent_pw, err_pipe[1]); |
712 | |
|
713 | 0 | if (!option_bool(OPT_DEBUG)) |
714 | 0 | { |
715 | | /* open stdout etc to /dev/null */ |
716 | 0 | int nullfd = open("/dev/null", O_RDWR); |
717 | 0 | if (nullfd != -1) |
718 | 0 | { |
719 | 0 | dup2(nullfd, STDOUT_FILENO); |
720 | 0 | dup2(nullfd, STDERR_FILENO); |
721 | 0 | dup2(nullfd, STDIN_FILENO); |
722 | 0 | close(nullfd); |
723 | 0 | } |
724 | 0 | } |
725 | | |
726 | | /* if we are to run scripts, we need to fork a helper before dropping root. */ |
727 | 0 | daemon->helperfd = -1; |
728 | 0 | #ifdef HAVE_SCRIPT |
729 | 0 | if ((daemon->dhcp || |
730 | 0 | daemon->dhcp6 || |
731 | 0 | daemon->relay6 || |
732 | 0 | option_bool(OPT_TFTP) || |
733 | 0 | option_bool(OPT_SCRIPT_ARP)) && |
734 | 0 | (daemon->lease_change_command || daemon->luascript)) |
735 | 0 | daemon->helperfd = create_helper(pipewrite, err_pipe[1], script_uid, script_gid, max_fd); |
736 | 0 | #endif |
737 | |
|
738 | 0 | if (!option_bool(OPT_DEBUG) && getuid() == 0) |
739 | 0 | { |
740 | 0 | int bad_capabilities = 0; |
741 | 0 | gid_t dummy; |
742 | | |
743 | | /* remove all supplementary groups */ |
744 | 0 | if (gp && |
745 | 0 | (setgroups(0, &dummy) == -1 || |
746 | 0 | setgid(gp->gr_gid) == -1)) |
747 | 0 | { |
748 | 0 | send_event(err_pipe[1], EVENT_GROUP_ERR, errno, daemon->groupname); |
749 | 0 | _exit(0); |
750 | 0 | } |
751 | | |
752 | 0 | if (ent_pw && ent_pw->pw_uid != 0) |
753 | 0 | { |
754 | 0 | #if defined(HAVE_LINUX_NETWORK) |
755 | | /* Need to be able to drop root. */ |
756 | 0 | data->effective |= (1 << CAP_SETUID); |
757 | 0 | data->permitted |= (1 << CAP_SETUID); |
758 | | /* Tell kernel to not clear capabilities when dropping root */ |
759 | 0 | if (capset(hdr, data) == -1 || prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1) |
760 | 0 | bad_capabilities = errno; |
761 | | |
762 | | #elif defined(HAVE_SOLARIS_NETWORK) |
763 | | /* http://developers.sun.com/solaris/articles/program_privileges.html */ |
764 | | priv_set_t *priv_set; |
765 | | |
766 | | if (!(priv_set = priv_str_to_set("basic", ",", NULL)) || |
767 | | priv_addset(priv_set, PRIV_NET_ICMPACCESS) == -1 || |
768 | | priv_addset(priv_set, PRIV_SYS_NET_CONFIG) == -1) |
769 | | bad_capabilities = errno; |
770 | | |
771 | | if (priv_set && bad_capabilities == 0) |
772 | | { |
773 | | priv_inverse(priv_set); |
774 | | |
775 | | if (setppriv(PRIV_OFF, PRIV_LIMIT, priv_set) == -1) |
776 | | bad_capabilities = errno; |
777 | | } |
778 | | |
779 | | if (priv_set) |
780 | | priv_freeset(priv_set); |
781 | | |
782 | | #endif |
783 | |
|
784 | 0 | if (bad_capabilities != 0) |
785 | 0 | { |
786 | 0 | send_event(err_pipe[1], EVENT_CAP_ERR, bad_capabilities, NULL); |
787 | 0 | _exit(0); |
788 | 0 | } |
789 | | |
790 | | /* finally drop root */ |
791 | 0 | if (setuid(ent_pw->pw_uid) == -1) |
792 | 0 | { |
793 | 0 | send_event(err_pipe[1], EVENT_USER_ERR, errno, daemon->username); |
794 | 0 | _exit(0); |
795 | 0 | } |
796 | | |
797 | 0 | #ifdef HAVE_LINUX_NETWORK |
798 | 0 | data->effective &= ~(1 << CAP_SETUID); |
799 | 0 | data->permitted &= ~(1 << CAP_SETUID); |
800 | | |
801 | | /* lose the setuid capability */ |
802 | 0 | if (capset(hdr, data) == -1) |
803 | 0 | { |
804 | 0 | send_event(err_pipe[1], EVENT_CAP_ERR, errno, NULL); |
805 | 0 | _exit(0); |
806 | 0 | } |
807 | 0 | #endif |
808 | | |
809 | 0 | } |
810 | 0 | } |
811 | | |
812 | 0 | #ifdef HAVE_LINUX_NETWORK |
813 | 0 | free(hdr); |
814 | 0 | free(data); |
815 | 0 | if (option_bool(OPT_DEBUG)) |
816 | 0 | prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); |
817 | 0 | #endif |
818 | |
|
819 | 0 | #ifdef HAVE_TFTP |
820 | 0 | if (option_bool(OPT_TFTP)) |
821 | 0 | { |
822 | 0 | DIR *dir; |
823 | 0 | struct tftp_prefix *p; |
824 | | |
825 | 0 | if (daemon->tftp_prefix) |
826 | 0 | { |
827 | 0 | if (!((dir = opendir(daemon->tftp_prefix)))) |
828 | 0 | { |
829 | 0 | tftp_prefix_missing = 1; |
830 | 0 | if (!option_bool(OPT_TFTP_NO_FAIL)) |
831 | 0 | { |
832 | 0 | send_event(err_pipe[1], EVENT_TFTP_ERR, errno, daemon->tftp_prefix); |
833 | 0 | _exit(0); |
834 | 0 | } |
835 | 0 | } |
836 | 0 | else |
837 | 0 | closedir(dir); |
838 | 0 | } |
839 | | |
840 | 0 | for (p = daemon->if_prefix; p; p = p->next) |
841 | 0 | { |
842 | 0 | p->missing = 0; |
843 | 0 | if (!((dir = opendir(p->prefix)))) |
844 | 0 | { |
845 | 0 | p->missing = 1; |
846 | 0 | if (!option_bool(OPT_TFTP_NO_FAIL)) |
847 | 0 | { |
848 | 0 | send_event(err_pipe[1], EVENT_TFTP_ERR, errno, p->prefix); |
849 | 0 | _exit(0); |
850 | 0 | } |
851 | 0 | } |
852 | 0 | else |
853 | 0 | closedir(dir); |
854 | 0 | } |
855 | 0 | } |
856 | 0 | #endif |
857 | | |
858 | 0 | #ifdef HAVE_INOTIFY |
859 | 0 | if ((daemon->port != 0 && !option_bool(OPT_NO_RESOLV)) || |
860 | 0 | daemon->dynamic_dirs) |
861 | 0 | inotify_dnsmasq_init(err_pipe[1]); |
862 | 0 | else |
863 | 0 | daemon->inotifyfd = -1; |
864 | 0 | #endif |
865 | | |
866 | | /* Don't start logging malloc before logging is set up. */ |
867 | 0 | daemon->log_malloc = option_bool(OPT_LOG_MALLOC); |
868 | | |
869 | 0 | if (daemon->port == 0) |
870 | 0 | my_syslog(LOG_INFO, _("started, version %s DNS disabled"), VERSION); |
871 | 0 | else |
872 | 0 | { |
873 | 0 | if (daemon->cachesize != 0) |
874 | 0 | { |
875 | 0 | my_syslog(LOG_INFO, _("started, version %s cachesize %d"), VERSION, daemon->cachesize); |
876 | 0 | if (daemon->cachesize > 10000) |
877 | 0 | my_syslog(LOG_WARNING, _("cache size greater than 10000 may cause performance issues, and is unlikely to be useful.")); |
878 | 0 | } |
879 | 0 | else |
880 | 0 | my_syslog(LOG_INFO, _("started, version %s cache disabled"), VERSION); |
881 | |
|
882 | 0 | if (option_bool(OPT_LOCAL_SERVICE)) |
883 | 0 | my_syslog(LOG_INFO, _("DNS service limited to local subnets")); |
884 | 0 | else if (option_bool(OPT_LOCALHOST_SERVICE)) |
885 | 0 | my_syslog(LOG_INFO, _("DNS service limited to localhost")); |
886 | 0 | } |
887 | | |
888 | 0 | my_syslog(LOG_INFO, _("compile time options: %s"), compile_opts); |
889 | |
|
890 | 0 | if (chown_warn != 0) |
891 | 0 | { |
892 | 0 | #if defined(HAVE_LINUX_NETWORK) |
893 | 0 | if (chown_warn == EPERM && !have_cap_chown) |
894 | 0 | my_syslog(LOG_INFO, "chown of PID file %s failed: please add capability CAP_CHOWN", daemon->runfile); |
895 | 0 | else |
896 | 0 | #endif |
897 | 0 | my_syslog(LOG_WARNING, "chown of PID file %s failed: %s", daemon->runfile, strerror(chown_warn)); |
898 | 0 | } |
899 | | |
900 | | #ifdef HAVE_DBUS |
901 | | if (option_bool(OPT_DBUS)) |
902 | | { |
903 | | if (daemon->dbus) |
904 | | my_syslog(LOG_INFO, _("DBus support enabled: connected to system bus")); |
905 | | else |
906 | | my_syslog(LOG_INFO, _("DBus support enabled: bus connection pending")); |
907 | | } |
908 | | #endif |
909 | |
|
910 | | #ifdef HAVE_UBUS |
911 | | if (option_bool(OPT_UBUS)) |
912 | | { |
913 | | if (daemon->ubus) |
914 | | my_syslog(LOG_INFO, _("UBus support enabled: connected to system bus")); |
915 | | else |
916 | | my_syslog(LOG_INFO, _("UBus support enabled: bus connection pending")); |
917 | | } |
918 | | #endif |
919 | |
|
920 | | #ifdef HAVE_DNSSEC |
921 | | if (option_bool(OPT_DNSSEC_VALID)) |
922 | | { |
923 | | int rc; |
924 | | struct ds_config *ds; |
925 | | |
926 | | /* Delay creating the timestamp file until here, after we've changed user, so that |
927 | | it has the correct owner to allow updating the mtime later. |
928 | | This means we have to report fatal errors via the pipe. */ |
929 | | if ((rc = setup_timestamp()) == -1) |
930 | | { |
931 | | send_event(err_pipe[1], EVENT_TIME_ERR, errno, daemon->timestamp_file); |
932 | | _exit(0); |
933 | | } |
934 | | |
935 | | if (option_bool(OPT_DNSSEC_IGN_NS)) |
936 | | my_syslog(LOG_INFO, _("DNSSEC validation enabled but all unsigned answers are trusted")); |
937 | | else |
938 | | my_syslog(LOG_INFO, _("DNSSEC validation enabled")); |
939 | | |
940 | | daemon->dnssec_no_time_check = option_bool(OPT_DNSSEC_TIME); |
941 | | if (option_bool(OPT_DNSSEC_TIME) && !daemon->back_to_the_future) |
942 | | my_syslog(LOG_INFO, _("DNSSEC signature timestamps not checked until receipt of SIGINT")); |
943 | | |
944 | | if (rc == 1) |
945 | | my_syslog(LOG_INFO, _("DNSSEC signature timestamps not checked until system time valid")); |
946 | | |
947 | | for (ds = daemon->ds; ds; ds = ds->next) |
948 | | { |
949 | | struct ds_config *ds1; |
950 | | |
951 | | for (ds1 = ds->next; ds1; ds1 = ds1->next) |
952 | | if (strcmp(ds->name, ds1->name) == 0 && |
953 | | ds->digestlen == ds1->digestlen && |
954 | | (ds->digestlen == 0 || |
955 | | (ds->algo == ds1->algo && |
956 | | ds->keytag == ds1->keytag && |
957 | | ds->digest_type == ds1->digest_type && |
958 | | memcmp(ds->digest, ds1->digest, ds->digestlen) == 0))) |
959 | | { |
960 | | ds->name = NULL; /* Mark as duplicate */ |
961 | | break; |
962 | | } |
963 | | |
964 | | if (ds->name) |
965 | | my_syslog(LOG_INFO, |
966 | | ds->digestlen == 0 ? _("configured with negative trust anchor for %s") : _("configured with trust anchor for %s keytag %u"), |
967 | | ds->name[0] == 0 ? "<root>" : ds->name, ds->keytag); |
968 | | } |
969 | | } |
970 | | #endif |
971 | |
|
972 | 0 | if (log_err != 0) |
973 | 0 | my_syslog(LOG_WARNING, _("warning: failed to change owner of %s: %s"), |
974 | 0 | daemon->log_file, strerror(log_err)); |
975 | | |
976 | | #ifndef HAVE_LINUX_NETWORK |
977 | | if (bind_fallback) |
978 | | my_syslog(LOG_WARNING, _("setting --bind-interfaces option because of OS limitations")); |
979 | | #endif |
980 | |
|
981 | 0 | if (option_bool(OPT_NOWILD)) |
982 | 0 | warn_bound_listeners(); |
983 | 0 | else if (!option_bool(OPT_CLEVERBIND)) |
984 | 0 | warn_wild_labels(); |
985 | |
|
986 | 0 | warn_int_names(); |
987 | | |
988 | 0 | if (!option_bool(OPT_NOWILD)) |
989 | 0 | for (if_tmp = daemon->if_names; if_tmp; if_tmp = if_tmp->next) |
990 | 0 | if (if_tmp->name && !(if_tmp->flags & INAME_USED)) |
991 | 0 | my_syslog(LOG_WARNING, _("warning: interface %s does not currently exist"), if_tmp->name); |
992 | | |
993 | 0 | if (daemon->port != 0 && option_bool(OPT_NO_RESOLV)) |
994 | 0 | { |
995 | 0 | if (daemon->resolv_files && !daemon->resolv_files->is_default) |
996 | 0 | my_syslog(LOG_WARNING, _("warning: ignoring resolv-file flag because no-resolv is set")); |
997 | 0 | daemon->resolv_files = NULL; |
998 | 0 | if (!daemon->servers) |
999 | 0 | { |
1000 | | #ifdef HAVE_DBUS |
1001 | | if (option_bool(OPT_DBUS)) |
1002 | | my_syslog(LOG_INFO, _("no upstream servers configured - please set them from DBus")); |
1003 | | else |
1004 | | #endif |
1005 | 0 | my_syslog(LOG_WARNING, _("warning: no upstream servers configured")); |
1006 | 0 | } |
1007 | 0 | } |
1008 | |
|
1009 | 0 | if (daemon->max_logs != 0) |
1010 | 0 | my_syslog(LOG_INFO, _("asynchronous logging enabled, queue limit is %d messages"), daemon->max_logs); |
1011 | | |
1012 | |
|
1013 | 0 | #ifdef HAVE_DHCP |
1014 | 0 | for (context = daemon->dhcp; context; context = context->next) |
1015 | 0 | log_context(AF_INET, context); |
1016 | |
|
1017 | 0 | for (relay = daemon->relay4; relay; relay = relay->next) |
1018 | 0 | log_relay(AF_INET, relay); |
1019 | |
|
1020 | 0 | # ifdef HAVE_DHCP6 |
1021 | 0 | for (context = daemon->dhcp6; context; context = context->next) |
1022 | 0 | log_context(AF_INET6, context); |
1023 | |
|
1024 | 0 | for (relay = daemon->relay6; relay; relay = relay->next) |
1025 | 0 | log_relay(AF_INET6, relay); |
1026 | | |
1027 | 0 | if (daemon->doing_dhcp6 || daemon->doing_ra) |
1028 | 0 | dhcp_construct_contexts(now); |
1029 | | |
1030 | 0 | if (option_bool(OPT_RA)) |
1031 | 0 | my_syslog(MS_DHCP | LOG_INFO, _("IPv6 router advertisement enabled")); |
1032 | 0 | # endif |
1033 | |
|
1034 | 0 | # ifdef HAVE_LINUX_NETWORK |
1035 | 0 | if (did_bind) |
1036 | 0 | my_syslog(MS_DHCP | LOG_INFO, _("DHCP, sockets bound exclusively to interface %s"), bound_device); |
1037 | |
|
1038 | 0 | if (netlink_warn) |
1039 | 0 | my_syslog(LOG_WARNING, netlink_warn); |
1040 | 0 | # endif |
1041 | | |
1042 | | /* after dhcp_construct_contexts */ |
1043 | 0 | if (daemon->dhcp || daemon->doing_dhcp6) |
1044 | 0 | lease_find_interfaces(now); |
1045 | 0 | #endif |
1046 | |
|
1047 | 0 | #ifdef HAVE_TFTP |
1048 | 0 | if (option_bool(OPT_TFTP)) |
1049 | 0 | { |
1050 | 0 | struct tftp_prefix *p; |
1051 | |
|
1052 | 0 | my_syslog(MS_TFTP | LOG_INFO, "TFTP %s%s %s %s", |
1053 | 0 | daemon->tftp_prefix ? _("root is ") : _("enabled"), |
1054 | 0 | daemon->tftp_prefix ? daemon->tftp_prefix : "", |
1055 | 0 | option_bool(OPT_TFTP_SECURE) ? _("secure mode") : "", |
1056 | 0 | option_bool(OPT_SINGLE_PORT) ? _("single port mode") : ""); |
1057 | |
|
1058 | 0 | if (tftp_prefix_missing) |
1059 | 0 | my_syslog(MS_TFTP | LOG_WARNING, _("warning: %s inaccessible"), daemon->tftp_prefix); |
1060 | |
|
1061 | 0 | for (p = daemon->if_prefix; p; p = p->next) |
1062 | 0 | if (p->missing) |
1063 | 0 | my_syslog(MS_TFTP | LOG_WARNING, _("warning: TFTP directory %s inaccessible"), p->prefix); |
1064 | | |
1065 | | /* This is a guess, it assumes that for small limits, |
1066 | | disjoint files might be served, but for large limits, |
1067 | | a single file will be sent to may clients (the file only needs |
1068 | | one fd). */ |
1069 | |
|
1070 | 0 | max_fd -= 30 + daemon->numrrand; /* use other than TFTP */ |
1071 | | |
1072 | 0 | if (max_fd < 0) |
1073 | 0 | max_fd = 5; |
1074 | 0 | else if (max_fd < 100 && !option_bool(OPT_SINGLE_PORT)) |
1075 | 0 | max_fd = max_fd/2; |
1076 | 0 | else |
1077 | 0 | max_fd = max_fd - 20; |
1078 | | |
1079 | | /* if we have to use a limited range of ports, |
1080 | | that will limit the number of transfers */ |
1081 | 0 | if (daemon->start_tftp_port != 0 && |
1082 | 0 | daemon->end_tftp_port - daemon->start_tftp_port + 1 < max_fd) |
1083 | 0 | max_fd = daemon->end_tftp_port - daemon->start_tftp_port + 1; |
1084 | |
|
1085 | 0 | if (daemon->tftp_max > max_fd) |
1086 | 0 | { |
1087 | 0 | daemon->tftp_max = max_fd; |
1088 | 0 | my_syslog(MS_TFTP | LOG_WARNING, |
1089 | 0 | _("restricting maximum simultaneous TFTP transfers to %d"), |
1090 | 0 | daemon->tftp_max); |
1091 | 0 | } |
1092 | 0 | } |
1093 | 0 | #endif |
1094 | | |
1095 | | /* finished start-up - release original process */ |
1096 | 0 | if (err_pipe[1] != -1) |
1097 | 0 | close(err_pipe[1]); |
1098 | | |
1099 | 0 | if (daemon->port != 0) |
1100 | 0 | check_servers(0); |
1101 | | |
1102 | 0 | pid = getpid(); |
1103 | |
|
1104 | 0 | #ifdef HAVE_INOTIFY |
1105 | | /* Using inotify, have to select a resolv file at startup */ |
1106 | 0 | poll_resolv(1, 0, now); |
1107 | 0 | #endif |
1108 | | |
1109 | 0 | while (1) |
1110 | 0 | { |
1111 | 0 | int timeout = fast_retry(now); |
1112 | | |
1113 | 0 | poll_reset(); |
1114 | | |
1115 | | /* Whilst polling for the dbus, or doing a tftp transfer, wake every quarter second */ |
1116 | 0 | if ((daemon->tftp_trans || (option_bool(OPT_DBUS) && !daemon->dbus)) && |
1117 | 0 | (timeout == -1 || timeout > 250)) |
1118 | 0 | timeout = 250; |
1119 | | |
1120 | | /* Wake every second whilst waiting for DAD to complete */ |
1121 | 0 | else if (is_dad_listeners() && |
1122 | 0 | (timeout == -1 || timeout > 1000)) |
1123 | 0 | timeout = 1000; |
1124 | | |
1125 | 0 | if (daemon->port != 0) |
1126 | 0 | set_dns_listeners(); |
1127 | | |
1128 | 0 | #ifdef HAVE_TFTP |
1129 | 0 | set_tftp_listeners(); |
1130 | 0 | #endif |
1131 | |
|
1132 | | #ifdef HAVE_DBUS |
1133 | | if (option_bool(OPT_DBUS)) |
1134 | | set_dbus_listeners(); |
1135 | | #endif |
1136 | | |
1137 | | #ifdef HAVE_UBUS |
1138 | | if (option_bool(OPT_UBUS)) |
1139 | | set_ubus_listeners(); |
1140 | | #endif |
1141 | | |
1142 | 0 | #ifdef HAVE_DHCP |
1143 | 0 | # if defined(HAVE_LINUX_NETWORK) |
1144 | 0 | if (bind_dhcp_devices(bound_device) & 2) |
1145 | 0 | { |
1146 | 0 | static int warned = 0; |
1147 | 0 | if (!warned) |
1148 | 0 | { |
1149 | 0 | my_syslog(LOG_ERR, _("error binding DHCP socket to device %s"), bound_device); |
1150 | 0 | warned = 1; |
1151 | 0 | } |
1152 | 0 | } |
1153 | 0 | # endif |
1154 | 0 | if (daemon->dhcp || daemon->relay4) |
1155 | 0 | { |
1156 | 0 | poll_listen(daemon->dhcpfd, POLLIN); |
1157 | 0 | if (daemon->pxefd != -1) |
1158 | 0 | poll_listen(daemon->pxefd, POLLIN); |
1159 | 0 | } |
1160 | 0 | #endif |
1161 | |
|
1162 | 0 | #ifdef HAVE_DHCP6 |
1163 | 0 | if (daemon->doing_dhcp6 || daemon->relay6) |
1164 | 0 | poll_listen(daemon->dhcp6fd, POLLIN); |
1165 | | |
1166 | 0 | if (daemon->doing_ra) |
1167 | 0 | poll_listen(daemon->icmp6fd, POLLIN); |
1168 | 0 | #endif |
1169 | | |
1170 | 0 | #ifdef HAVE_INOTIFY |
1171 | 0 | if (daemon->inotifyfd != -1) |
1172 | 0 | poll_listen(daemon->inotifyfd, POLLIN); |
1173 | 0 | #endif |
1174 | |
|
1175 | 0 | #if defined(HAVE_LINUX_NETWORK) |
1176 | 0 | poll_listen(daemon->netlinkfd, POLLIN); |
1177 | | #elif defined(HAVE_BSD_NETWORK) |
1178 | | poll_listen(daemon->routefd, POLLIN); |
1179 | | #endif |
1180 | | |
1181 | 0 | poll_listen(piperead, POLLIN); |
1182 | |
|
1183 | 0 | #ifdef HAVE_SCRIPT |
1184 | 0 | # ifdef HAVE_DHCP |
1185 | 0 | while (helper_buf_empty() && do_script_run(now)); |
1186 | 0 | # endif |
1187 | | |
1188 | | /* Refresh cache */ |
1189 | 0 | if (option_bool(OPT_SCRIPT_ARP)) |
1190 | 0 | find_mac(NULL, NULL, 0, now); |
1191 | 0 | while (helper_buf_empty() && do_arp_script_run()); |
1192 | |
|
1193 | 0 | # ifdef HAVE_TFTP |
1194 | 0 | while (helper_buf_empty() && do_tftp_script_run()); |
1195 | 0 | # endif |
1196 | |
|
1197 | 0 | # ifdef HAVE_DHCP6 |
1198 | 0 | while (helper_buf_empty() && do_snoop_script_run()); |
1199 | 0 | # endif |
1200 | | |
1201 | 0 | if (!helper_buf_empty()) |
1202 | 0 | poll_listen(daemon->helperfd, POLLOUT); |
1203 | | #else |
1204 | | /* need this for other side-effects */ |
1205 | | # ifdef HAVE_DHCP |
1206 | | while (do_script_run(now)); |
1207 | | # endif |
1208 | | |
1209 | | while (do_arp_script_run()); |
1210 | | |
1211 | | # ifdef HAVE_TFTP |
1212 | | while (do_tftp_script_run()); |
1213 | | # endif |
1214 | | |
1215 | | #endif |
1216 | | |
1217 | | |
1218 | | /* must do this just before do_poll(), when we know no |
1219 | | more calls to my_syslog() can occur */ |
1220 | 0 | set_log_writer(); |
1221 | | |
1222 | 0 | if (do_poll(timeout) < 0) |
1223 | 0 | continue; |
1224 | | |
1225 | 0 | now = dnsmasq_time(); |
1226 | |
|
1227 | 0 | check_log_writer(0); |
1228 | | |
1229 | | /* prime. */ |
1230 | 0 | enumerate_interfaces(1); |
1231 | | |
1232 | | /* Check the interfaces to see if any have exited DAD state |
1233 | | and if so, bind the address. */ |
1234 | 0 | if (is_dad_listeners()) |
1235 | 0 | { |
1236 | 0 | enumerate_interfaces(0); |
1237 | | /* NB, is_dad_listeners() == 1 --> we're binding interfaces */ |
1238 | 0 | create_bound_listeners(0); |
1239 | 0 | warn_bound_listeners(); |
1240 | 0 | } |
1241 | |
|
1242 | 0 | #if defined(HAVE_LINUX_NETWORK) |
1243 | 0 | if (poll_check(daemon->netlinkfd, POLLIN)) |
1244 | 0 | netlink_multicast(); |
1245 | | #elif defined(HAVE_BSD_NETWORK) |
1246 | | if (poll_check(daemon->routefd, POLLIN)) |
1247 | | route_sock(); |
1248 | | #endif |
1249 | |
|
1250 | 0 | #ifdef HAVE_INOTIFY |
1251 | 0 | if (daemon->inotifyfd != -1 && poll_check(daemon->inotifyfd, POLLIN) && inotify_check(now)) |
1252 | 0 | { |
1253 | 0 | if (daemon->port != 0 && !option_bool(OPT_NO_POLL)) |
1254 | 0 | poll_resolv(1, 1, now); |
1255 | 0 | } |
1256 | | #else |
1257 | | /* Check for changes to resolv files once per second max. */ |
1258 | | /* Don't go silent for long periods if the clock goes backwards. */ |
1259 | | if (daemon->last_resolv == 0 || |
1260 | | difftime(now, daemon->last_resolv) > 1.0 || |
1261 | | difftime(now, daemon->last_resolv) < -1.0) |
1262 | | { |
1263 | | /* poll_resolv doesn't need to reload first time through, since |
1264 | | that's queued anyway. */ |
1265 | | |
1266 | | poll_resolv(0, daemon->last_resolv != 0, now); |
1267 | | daemon->last_resolv = now; |
1268 | | } |
1269 | | #endif |
1270 | |
|
1271 | 0 | if (poll_check(piperead, POLLIN)) |
1272 | 0 | async_event(piperead, now); |
1273 | | |
1274 | | #ifdef HAVE_DBUS |
1275 | | /* if we didn't create a DBus connection, retry now. */ |
1276 | | if (option_bool(OPT_DBUS)) |
1277 | | { |
1278 | | if (!daemon->dbus) |
1279 | | { |
1280 | | char *err = dbus_init(); |
1281 | | |
1282 | | if (daemon->dbus) |
1283 | | my_syslog(LOG_INFO, _("connected to system DBus")); |
1284 | | else if (err) |
1285 | | { |
1286 | | my_syslog(LOG_ERR, _("DBus error: %s"), err); |
1287 | | reset_option_bool(OPT_DBUS); /* fatal error, stop trying. */ |
1288 | | } |
1289 | | } |
1290 | | |
1291 | | check_dbus_listeners(); |
1292 | | } |
1293 | | #endif |
1294 | |
|
1295 | | #ifdef HAVE_UBUS |
1296 | | /* if we didn't create a UBus connection, retry now. */ |
1297 | | if (option_bool(OPT_UBUS)) |
1298 | | { |
1299 | | if (!daemon->ubus) |
1300 | | { |
1301 | | char *err = ubus_init(); |
1302 | | |
1303 | | if (daemon->ubus) |
1304 | | my_syslog(LOG_INFO, _("connected to system UBus")); |
1305 | | else if (err) |
1306 | | { |
1307 | | my_syslog(LOG_ERR, _("UBus error: %s"), err); |
1308 | | reset_option_bool(OPT_UBUS); /* fatal error, stop trying. */ |
1309 | | } |
1310 | | } |
1311 | | |
1312 | | check_ubus_listeners(); |
1313 | | } |
1314 | | #endif |
1315 | | |
1316 | 0 | if (daemon->port != 0) |
1317 | 0 | check_dns_listeners(now); |
1318 | |
|
1319 | 0 | #ifdef HAVE_TFTP |
1320 | 0 | check_tftp_listeners(now); |
1321 | 0 | #endif |
1322 | |
|
1323 | 0 | #ifdef HAVE_DHCP |
1324 | 0 | if (daemon->dhcp || daemon->relay4) |
1325 | 0 | { |
1326 | 0 | if (poll_check(daemon->dhcpfd, POLLIN)) |
1327 | 0 | dhcp_packet(now, 0); |
1328 | 0 | if (daemon->pxefd != -1 && poll_check(daemon->pxefd, POLLIN)) |
1329 | 0 | dhcp_packet(now, 1); |
1330 | 0 | } |
1331 | |
|
1332 | 0 | #ifdef HAVE_DHCP6 |
1333 | 0 | if ((daemon->doing_dhcp6 || daemon->relay6) && poll_check(daemon->dhcp6fd, POLLIN)) |
1334 | 0 | dhcp6_packet(now); |
1335 | |
|
1336 | 0 | if (daemon->doing_ra && poll_check(daemon->icmp6fd, POLLIN)) |
1337 | 0 | icmp6_packet(now); |
1338 | 0 | #endif |
1339 | |
|
1340 | 0 | # ifdef HAVE_SCRIPT |
1341 | 0 | if (daemon->helperfd != -1 && poll_check(daemon->helperfd, POLLOUT)) |
1342 | 0 | helper_write(); |
1343 | 0 | # endif |
1344 | 0 | #endif |
1345 | |
|
1346 | 0 | } |
1347 | 0 | } |
1348 | | |
1349 | | static void sig_handler(int sig) |
1350 | 0 | { |
1351 | 0 | if (pid == 0) |
1352 | 0 | { |
1353 | | /* ignore anything other than TERM during startup |
1354 | | and in helper proc. (helper ignore TERM too) */ |
1355 | 0 | if (sig == SIGTERM || sig == SIGINT) |
1356 | 0 | exit(EC_MISC); |
1357 | 0 | } |
1358 | 0 | else if (daemon->pipe_to_parent != -1) |
1359 | 0 | { |
1360 | | /* alarm is used to kill TCP children after a fixed time. */ |
1361 | 0 | if (sig == SIGALRM) |
1362 | 0 | { |
1363 | | #ifdef HAVE_DNSSEC |
1364 | | if (!daemon->forward_to_tcp) |
1365 | | #endif |
1366 | 0 | _exit(0); /* Normal TCP child */ |
1367 | | #ifdef HAVE_DNSSEC |
1368 | | else |
1369 | | { |
1370 | | /* udp_to_tcp transfer. |
1371 | | If daemon->header_to_tcp is NULL the waiting is over and |
1372 | | we can let things take their course, otherwise, send a failure |
1373 | | return down the pipe to unblock the UDP transaction and kill |
1374 | | the process. */ |
1375 | | if (daemon->header_to_tcp) |
1376 | | { |
1377 | | unsigned char op = PIPE_OP_KILLED; |
1378 | | int status = STAT_ABANDONED; |
1379 | | |
1380 | | read_write(daemon->pipe_to_parent, &op, sizeof(op), RW_WRITE); |
1381 | | read_write(daemon->pipe_to_parent, (unsigned char *)&status, sizeof(status), RW_WRITE); |
1382 | | read_write(daemon->pipe_to_parent, (unsigned char *)(&daemon->plen_to_tcp), sizeof(daemon->plen_to_tcp), RW_WRITE); |
1383 | | read_write(daemon->pipe_to_parent, (unsigned char *)(daemon->header_to_tcp), daemon->plen_to_tcp, RW_WRITE); |
1384 | | read_write(daemon->pipe_to_parent, (unsigned char *)(&daemon->forward_to_tcp), sizeof(daemon->forward_to_tcp), RW_WRITE); |
1385 | | read_write(daemon->pipe_to_parent, (unsigned char *)(&daemon->forward_to_tcp->uid), sizeof(daemon->forward_to_tcp->uid), RW_WRITE); |
1386 | | |
1387 | | _exit(0); |
1388 | | } |
1389 | | } |
1390 | | #endif |
1391 | 0 | } |
1392 | 0 | } |
1393 | 0 | else |
1394 | 0 | { |
1395 | | /* master process */ |
1396 | 0 | int event, errsave = errno; |
1397 | | |
1398 | 0 | if (sig == SIGHUP) |
1399 | 0 | event = EVENT_RELOAD; |
1400 | 0 | else if (sig == SIGCHLD) |
1401 | 0 | event = EVENT_CHILD; |
1402 | 0 | else if (sig == SIGALRM) |
1403 | 0 | event = EVENT_ALARM; |
1404 | 0 | else if (sig == SIGTERM) |
1405 | 0 | event = EVENT_TERM; |
1406 | 0 | else if (sig == SIGUSR1) |
1407 | 0 | event = EVENT_DUMP; |
1408 | 0 | else if (sig == SIGUSR2) |
1409 | 0 | event = EVENT_REOPEN; |
1410 | 0 | else if (sig == SIGINT) |
1411 | 0 | { |
1412 | | /* Handle SIGINT normally in debug mode, so |
1413 | | ctrl-c continues to operate. */ |
1414 | 0 | if (option_bool(OPT_DEBUG)) |
1415 | 0 | exit(EC_MISC); |
1416 | 0 | else |
1417 | 0 | event = EVENT_TIME; |
1418 | 0 | } |
1419 | 0 | else |
1420 | 0 | return; |
1421 | | |
1422 | 0 | send_event(pipewrite, event, 0, NULL); |
1423 | 0 | errno = errsave; |
1424 | 0 | } |
1425 | 0 | } |
1426 | | |
1427 | | /* now == 0 -> queue immediate callback */ |
1428 | | void send_alarm(time_t event, time_t now) |
1429 | 0 | { |
1430 | 0 | if (now == 0 || event != 0) |
1431 | 0 | { |
1432 | | /* alarm(0) or alarm(-ve) doesn't do what we want.... */ |
1433 | 0 | if ((now == 0 || difftime(event, now) <= 0.0)) |
1434 | 0 | send_event(pipewrite, EVENT_ALARM, 0, NULL); |
1435 | 0 | else |
1436 | 0 | alarm((unsigned)difftime(event, now)); |
1437 | 0 | } |
1438 | 0 | } |
1439 | | |
1440 | | void queue_event(int event) |
1441 | 0 | { |
1442 | 0 | send_event(pipewrite, event, 0, NULL); |
1443 | 0 | } |
1444 | | |
1445 | | void send_event(int fd, int event, int data, char *msg) |
1446 | 0 | { |
1447 | 0 | struct event_desc ev; |
1448 | 0 | struct iovec iov[2]; |
1449 | |
|
1450 | 0 | ev.event = event; |
1451 | 0 | ev.data = data; |
1452 | 0 | ev.msg_sz = msg ? strlen(msg) : 0; |
1453 | | |
1454 | 0 | iov[0].iov_base = &ev; |
1455 | 0 | iov[0].iov_len = sizeof(ev); |
1456 | 0 | iov[1].iov_base = msg; |
1457 | 0 | iov[1].iov_len = ev.msg_sz; |
1458 | | |
1459 | | /* error pipe, debug mode. */ |
1460 | 0 | if (fd == -1) |
1461 | 0 | fatal_event(&ev, msg); |
1462 | 0 | else |
1463 | | /* pipe is non-blocking and struct event_desc is smaller than |
1464 | | PIPE_BUF, so this either fails or writes everything */ |
1465 | 0 | while (writev(fd, iov, msg ? 2 : 1) == -1 && errno == EINTR); |
1466 | 0 | } |
1467 | | |
1468 | | static int read_event(int fd, struct event_desc *evp, char **msg) |
1469 | 0 | { |
1470 | 0 | char *buf; |
1471 | |
|
1472 | 0 | if (!read_write(fd, (unsigned char *)evp, sizeof(struct event_desc), RW_READ)) |
1473 | 0 | return 0; |
1474 | | |
1475 | 0 | *msg = NULL; |
1476 | | |
1477 | 0 | if (evp->msg_sz != 0) |
1478 | 0 | { |
1479 | 0 | if (!(buf = whine_malloc(evp->msg_sz + 1))) |
1480 | 0 | { |
1481 | 0 | int i; |
1482 | 0 | unsigned char a; |
1483 | | |
1484 | | /* Keep the stream synchronised if malloc fails. */ |
1485 | 0 | for (i = 0; i < evp->msg_sz; i++) |
1486 | 0 | read_write(fd, &a, 1, RW_READ); |
1487 | 0 | } |
1488 | 0 | else if (read_write(fd, (unsigned char *)buf, evp->msg_sz, RW_READ)) |
1489 | 0 | { |
1490 | 0 | buf[evp->msg_sz] = 0; |
1491 | 0 | *msg = buf; |
1492 | 0 | } |
1493 | 0 | } |
1494 | |
|
1495 | 0 | return 1; |
1496 | 0 | } |
1497 | | |
1498 | | static void fatal_event(struct event_desc *ev, char *msg) |
1499 | 0 | { |
1500 | 0 | errno = ev->data; |
1501 | | |
1502 | 0 | switch (ev->event) |
1503 | 0 | { |
1504 | 0 | case EVENT_DIE: |
1505 | 0 | exit(0); |
1506 | | |
1507 | 0 | case EVENT_FORK_ERR: |
1508 | 0 | die(_("cannot fork into background: %s"), NULL, EC_MISC); |
1509 | | |
1510 | | /* fall through */ |
1511 | 0 | case EVENT_PIPE_ERR: |
1512 | 0 | die(_("failed to create helper: %s"), NULL, EC_MISC); |
1513 | | |
1514 | | /* fall through */ |
1515 | 0 | case EVENT_CAP_ERR: |
1516 | 0 | die(_("setting capabilities failed: %s"), NULL, EC_MISC); |
1517 | | |
1518 | | /* fall through */ |
1519 | 0 | case EVENT_USER_ERR: |
1520 | 0 | die(_("failed to change user-id to %s: %s"), msg, EC_MISC); |
1521 | | |
1522 | | /* fall through */ |
1523 | 0 | case EVENT_GROUP_ERR: |
1524 | 0 | die(_("failed to change group-id to %s: %s"), msg, EC_MISC); |
1525 | | |
1526 | | /* fall through */ |
1527 | 0 | case EVENT_PIDFILE: |
1528 | 0 | die(_("failed to open pidfile %s: %s"), msg, EC_FILE); |
1529 | | |
1530 | | /* fall through */ |
1531 | 0 | case EVENT_LOG_ERR: |
1532 | 0 | die(_("cannot open log %s: %s"), msg, EC_FILE); |
1533 | | |
1534 | | /* fall through */ |
1535 | 0 | case EVENT_LUA_ERR: |
1536 | 0 | die(_("failed to load Lua script: %s"), msg, EC_MISC); |
1537 | | |
1538 | | /* fall through */ |
1539 | 0 | case EVENT_TFTP_ERR: |
1540 | 0 | die(_("TFTP directory %s inaccessible: %s"), msg, EC_FILE); |
1541 | | |
1542 | | /* fall through */ |
1543 | 0 | case EVENT_TIME_ERR: |
1544 | 0 | die(_("cannot create timestamp file %s: %s" ), msg, EC_BADCONF); |
1545 | | |
1546 | | /* fall through */ |
1547 | 0 | case EVENT_LINK_ERR: |
1548 | 0 | die(_("cannot access path %s: %s" ), msg, EC_MISC); |
1549 | | |
1550 | | /* fall through */ |
1551 | 0 | case EVENT_INOTFY_ERR: |
1552 | 0 | die(_("failed to create inotify: %s" ), NULL, EC_MISC); |
1553 | | |
1554 | | /* fall through */ |
1555 | 0 | case EVENT_TMSL_ERR: |
1556 | 0 | die(_("too many symlinks following %s"), msg, EC_MISC); |
1557 | | |
1558 | | /* fall through */ |
1559 | 0 | case EVENT_RESOLV_ERR: |
1560 | 0 | die(_("directory %s for resolv-file is missing, cannot poll"), msg, EC_MISC); |
1561 | | |
1562 | | /* fall through */ |
1563 | 0 | case EVENT_IFILE_ERR: |
1564 | 0 | die(_("failed to create inotify for %s: %s"), msg, EC_MISC); |
1565 | 0 | } |
1566 | 0 | } |
1567 | | |
1568 | | static void async_event(int pipe, time_t now) |
1569 | 0 | { |
1570 | 0 | pid_t p; |
1571 | 0 | struct event_desc ev; |
1572 | 0 | int wstatus, i, check = 0; |
1573 | 0 | char *msg; |
1574 | | |
1575 | 0 | if (read_event(pipe, &ev, &msg)) |
1576 | 0 | switch (ev.event) |
1577 | 0 | { |
1578 | 0 | case EVENT_RELOAD: |
1579 | 0 | daemon->soa_sn++; /* Bump zone serial, as it may have changed. */ |
1580 | | |
1581 | | /* fall through */ |
1582 | | |
1583 | 0 | case EVENT_INIT: |
1584 | 0 | clear_cache_and_reload(now); |
1585 | | |
1586 | 0 | if (daemon->port != 0) |
1587 | 0 | { |
1588 | 0 | if (daemon->resolv_files && option_bool(OPT_NO_POLL)) |
1589 | 0 | { |
1590 | 0 | reload_servers(daemon->resolv_files->name); |
1591 | 0 | check = 1; |
1592 | 0 | } |
1593 | |
|
1594 | 0 | if (daemon->servers_file) |
1595 | 0 | { |
1596 | 0 | read_servers_file(); |
1597 | 0 | check = 1; |
1598 | 0 | } |
1599 | |
|
1600 | 0 | if (check) |
1601 | 0 | check_servers(0); |
1602 | 0 | } |
1603 | |
|
1604 | 0 | #ifdef HAVE_DHCP |
1605 | 0 | rerun_scripts(); |
1606 | 0 | #endif |
1607 | 0 | break; |
1608 | | |
1609 | 0 | case EVENT_DUMP: |
1610 | 0 | if (daemon->port != 0) |
1611 | 0 | dump_cache(now); |
1612 | 0 | break; |
1613 | | |
1614 | 0 | case EVENT_ALARM: |
1615 | 0 | #ifdef HAVE_DHCP |
1616 | 0 | if (daemon->dhcp || daemon->doing_dhcp6) |
1617 | 0 | { |
1618 | 0 | lease_prune(NULL, now); |
1619 | 0 | lease_update_file(now); |
1620 | 0 | lease_update_dns(0); |
1621 | 0 | } |
1622 | 0 | #ifdef HAVE_DHCP6 |
1623 | 0 | else if (daemon->doing_ra) |
1624 | | /* Not doing DHCP, so no lease system, manage alarms for ra only */ |
1625 | 0 | send_alarm(periodic_ra(now), now); |
1626 | 0 | #endif |
1627 | 0 | #endif |
1628 | 0 | break; |
1629 | | |
1630 | 0 | case EVENT_CHILD: |
1631 | | /* See Stevens 5.10 */ |
1632 | 0 | while ((p = waitpid(-1, &wstatus, WNOHANG)) != 0) |
1633 | 0 | if (p == -1) |
1634 | 0 | { |
1635 | 0 | if (errno != EINTR) |
1636 | 0 | break; |
1637 | 0 | } |
1638 | 0 | else if (daemon->port != 0) |
1639 | 0 | for (i = 0 ; i < daemon->max_procs; i++) |
1640 | 0 | if (daemon->tcp_pids[i] == p) |
1641 | 0 | { |
1642 | 0 | daemon->tcp_pids[i] = 0; |
1643 | |
|
1644 | 0 | if (!WIFEXITED(wstatus)) |
1645 | 0 | { |
1646 | | /* If a helper process dies, (eg with SIGSEV) |
1647 | | log that and attempt to patch things up so that the |
1648 | | parent can continue to function. */ |
1649 | 0 | my_syslog(LOG_WARNING, _("TCP helper process %u died unexpectedly"), (unsigned int)p); |
1650 | 0 | if (daemon->tcp_pipes[i] != -1) |
1651 | 0 | { |
1652 | 0 | close(daemon->tcp_pipes[i]); |
1653 | 0 | daemon->tcp_pipes[i] = -1; |
1654 | 0 | } |
1655 | 0 | } |
1656 | | |
1657 | | /* tcp_pipes == -1 && tcp_pids == 0 required to free slot */ |
1658 | 0 | if (daemon->tcp_pipes[i] == -1) |
1659 | 0 | { |
1660 | 0 | if (daemon->tcp_type[i]) |
1661 | 0 | daemon->dnssec_tcp_slot_reserved++; |
1662 | 0 | daemon->metrics[METRIC_TCP_CONNECTIONS]--; |
1663 | 0 | } |
1664 | 0 | } |
1665 | 0 | break; |
1666 | | |
1667 | 0 | #if defined(HAVE_SCRIPT) |
1668 | 0 | case EVENT_KILLED: |
1669 | 0 | my_syslog(LOG_WARNING, _("script process killed by signal %d"), ev.data); |
1670 | 0 | break; |
1671 | | |
1672 | 0 | case EVENT_EXITED: |
1673 | 0 | my_syslog(LOG_WARNING, _("script process exited with status %d"), ev.data); |
1674 | 0 | break; |
1675 | | |
1676 | 0 | case EVENT_EXEC_ERR: |
1677 | 0 | my_syslog(LOG_ERR, _("failed to execute %s: %s"), |
1678 | 0 | daemon->lease_change_command, strerror(ev.data)); |
1679 | 0 | break; |
1680 | | |
1681 | 0 | case EVENT_SCRIPT_LOG: |
1682 | 0 | my_syslog(MS_SCRIPT | LOG_DEBUG, "%s", msg ? msg : ""); |
1683 | 0 | free(msg); |
1684 | 0 | break; |
1685 | | |
1686 | | /* necessary for fatal errors in helper */ |
1687 | 0 | case EVENT_USER_ERR: |
1688 | 0 | case EVENT_DIE: |
1689 | 0 | case EVENT_LUA_ERR: |
1690 | 0 | fatal_event(&ev, msg); |
1691 | 0 | break; |
1692 | 0 | #endif |
1693 | | |
1694 | 0 | case EVENT_REOPEN: |
1695 | | /* Note: this may leave TCP-handling processes with the old file still open. |
1696 | | Since any such process will die in CHILD_LIFETIME or probably much sooner, |
1697 | | we leave them logging to the old file. */ |
1698 | 0 | if (daemon->log_file != NULL) |
1699 | 0 | log_reopen(daemon->log_file); |
1700 | 0 | break; |
1701 | | |
1702 | 0 | case EVENT_NEWADDR: |
1703 | 0 | newaddress(now); |
1704 | 0 | break; |
1705 | | |
1706 | 0 | case EVENT_NEWROUTE: |
1707 | 0 | resend_query(); |
1708 | | /* Force re-reading resolv file right now, for luck. */ |
1709 | 0 | poll_resolv(0, 1, now); |
1710 | 0 | break; |
1711 | | |
1712 | 0 | case EVENT_TIME: |
1713 | | #ifdef HAVE_DNSSEC |
1714 | | if (daemon->dnssec_no_time_check && option_bool(OPT_DNSSEC_VALID) && option_bool(OPT_DNSSEC_TIME)) |
1715 | | { |
1716 | | my_syslog(LOG_INFO, _("now checking DNSSEC signature timestamps")); |
1717 | | daemon->dnssec_no_time_check = 0; |
1718 | | clear_cache_and_reload(now); |
1719 | | } |
1720 | | #endif |
1721 | 0 | break; |
1722 | | |
1723 | 0 | case EVENT_TERM: |
1724 | | /* Knock all our children on the head. */ |
1725 | 0 | if (daemon->port != 0) |
1726 | 0 | for (i = 0; i < daemon->max_procs; i++) |
1727 | 0 | if (daemon->tcp_pids[i] != 0) |
1728 | 0 | kill(daemon->tcp_pids[i], SIGALRM); |
1729 | | |
1730 | 0 | #if defined(HAVE_SCRIPT) && defined(HAVE_DHCP) |
1731 | | /* handle pending lease transitions */ |
1732 | 0 | if (daemon->helperfd != -1) |
1733 | 0 | { |
1734 | | /* block in writes until all done */ |
1735 | 0 | if ((i = fcntl(daemon->helperfd, F_GETFL)) != -1) |
1736 | 0 | while(retry_send(fcntl(daemon->helperfd, F_SETFL, i & ~O_NONBLOCK))); |
1737 | 0 | do { |
1738 | 0 | helper_write(); |
1739 | 0 | } while (!helper_buf_empty() || do_script_run(now)); |
1740 | 0 | close(daemon->helperfd); |
1741 | 0 | } |
1742 | 0 | #endif |
1743 | | |
1744 | 0 | if (daemon->lease_stream) |
1745 | 0 | fclose(daemon->lease_stream); |
1746 | |
|
1747 | | #ifdef HAVE_DNSSEC |
1748 | | /* update timestamp file on TERM if time is considered valid */ |
1749 | | if (daemon->back_to_the_future) |
1750 | | { |
1751 | | if (utimes(daemon->timestamp_file, NULL) == -1) |
1752 | | my_syslog(LOG_ERR, _("failed to update mtime on %s: %s"), daemon->timestamp_file, strerror(errno)); |
1753 | | } |
1754 | | #endif |
1755 | |
|
1756 | 0 | if (daemon->runfile) |
1757 | 0 | unlink(daemon->runfile); |
1758 | |
|
1759 | 0 | #ifdef HAVE_DUMPFILE |
1760 | 0 | if (daemon->dumpfd != -1) |
1761 | 0 | close(daemon->dumpfd); |
1762 | 0 | #endif |
1763 | | |
1764 | 0 | my_syslog(LOG_INFO, _("exiting on receipt of SIGTERM")); |
1765 | 0 | flush_log(); |
1766 | 0 | exit(EC_GOOD); |
1767 | 0 | } |
1768 | 0 | } |
1769 | | |
1770 | | static void poll_resolv(int force, int do_reload, time_t now) |
1771 | 0 | { |
1772 | 0 | struct resolvc *res, *latest; |
1773 | 0 | struct stat statbuf; |
1774 | 0 | time_t last_change = 0; |
1775 | | /* There may be more than one possible file. |
1776 | | Go through and find the one which changed _last_. |
1777 | | Warn of any which can't be read. */ |
1778 | |
|
1779 | 0 | if (daemon->port == 0 || option_bool(OPT_NO_POLL)) |
1780 | 0 | return; |
1781 | | |
1782 | 0 | for (latest = NULL, res = daemon->resolv_files; res; res = res->next) |
1783 | 0 | if (stat(res->name, &statbuf) == -1) |
1784 | 0 | { |
1785 | 0 | if (force) |
1786 | 0 | { |
1787 | 0 | res->mtime = 0; |
1788 | 0 | continue; |
1789 | 0 | } |
1790 | | |
1791 | 0 | if (!res->logged) |
1792 | 0 | my_syslog(LOG_WARNING, _("failed to access %s: %s"), res->name, strerror(errno)); |
1793 | 0 | res->logged = 1; |
1794 | | |
1795 | 0 | if (res->mtime != 0) |
1796 | 0 | { |
1797 | | /* existing file evaporated, force selection of the latest |
1798 | | file even if its mtime hasn't changed since we last looked */ |
1799 | 0 | poll_resolv(1, do_reload, now); |
1800 | 0 | return; |
1801 | 0 | } |
1802 | 0 | } |
1803 | 0 | else |
1804 | 0 | { |
1805 | 0 | res->logged = 0; |
1806 | 0 | if (force || (statbuf.st_mtime != res->mtime || statbuf.st_ino != res->ino)) |
1807 | 0 | { |
1808 | 0 | res->mtime = statbuf.st_mtime; |
1809 | 0 | res->ino = statbuf.st_ino; |
1810 | 0 | if (difftime(statbuf.st_mtime, last_change) > 0.0) |
1811 | 0 | { |
1812 | 0 | last_change = statbuf.st_mtime; |
1813 | 0 | latest = res; |
1814 | 0 | } |
1815 | 0 | } |
1816 | 0 | } |
1817 | | |
1818 | 0 | if (latest) |
1819 | 0 | { |
1820 | 0 | static int warned = 0; |
1821 | 0 | if (reload_servers(latest->name)) |
1822 | 0 | { |
1823 | 0 | my_syslog(LOG_INFO, _("reading %s"), latest->name); |
1824 | 0 | warned = 0; |
1825 | 0 | check_servers(0); |
1826 | 0 | if (option_bool(OPT_RELOAD) && do_reload) |
1827 | 0 | clear_cache_and_reload(now); |
1828 | 0 | } |
1829 | 0 | else |
1830 | 0 | { |
1831 | | /* If we're delaying things, we don't call check_servers(), but |
1832 | | reload_servers() may have deleted some servers, rendering the server_array |
1833 | | invalid, so just rebuild that here. Once reload_servers() succeeds, |
1834 | | we call check_servers() above, which calls build_server_array itself. */ |
1835 | 0 | build_server_array(); |
1836 | 0 | latest->mtime = 0; |
1837 | 0 | if (!warned) |
1838 | 0 | { |
1839 | 0 | my_syslog(LOG_WARNING, _("no servers found in %s, will retry"), latest->name); |
1840 | 0 | warned = 1; |
1841 | 0 | } |
1842 | 0 | } |
1843 | 0 | } |
1844 | 0 | } |
1845 | | |
1846 | | void clear_cache_and_reload(time_t now) |
1847 | 0 | { |
1848 | 0 | (void)now; |
1849 | |
|
1850 | 0 | if (daemon->port != 0) |
1851 | 0 | cache_reload(); |
1852 | | |
1853 | 0 | #ifdef HAVE_DHCP |
1854 | 0 | if (daemon->dhcp || daemon->doing_dhcp6) |
1855 | 0 | { |
1856 | 0 | reread_dhcp(); |
1857 | 0 | if (option_bool(OPT_ETHERS)) |
1858 | 0 | dhcp_read_ethers(); |
1859 | 0 | dhcp_update_configs(daemon->dhcp_conf); |
1860 | 0 | lease_update_from_configs(); |
1861 | 0 | lease_update_file(now); |
1862 | 0 | lease_update_dns(1); |
1863 | 0 | } |
1864 | 0 | #ifdef HAVE_DHCP6 |
1865 | 0 | else if (daemon->doing_ra) |
1866 | | /* Not doing DHCP, so no lease system, manage |
1867 | | alarms for ra only */ |
1868 | 0 | send_alarm(periodic_ra(now), now); |
1869 | 0 | #endif |
1870 | 0 | #endif |
1871 | 0 | } |
1872 | | |
1873 | | #ifdef HAVE_TFTP |
1874 | | static void set_tftp_listeners(void) |
1875 | 0 | { |
1876 | 0 | int tftp = 0; |
1877 | 0 | struct tftp_transfer *transfer; |
1878 | 0 | struct listener *listener; |
1879 | | |
1880 | 0 | if (!option_bool(OPT_SINGLE_PORT)) |
1881 | 0 | for (transfer = daemon->tftp_trans; transfer; transfer = transfer->next) |
1882 | 0 | { |
1883 | 0 | tftp++; |
1884 | 0 | poll_listen(transfer->sockfd, POLLIN); |
1885 | 0 | } |
1886 | |
|
1887 | 0 | for (listener = daemon->listeners; listener; listener = listener->next) |
1888 | | /* tftp == 0 in single-port mode. */ |
1889 | 0 | if (tftp <= daemon->tftp_max && listener->tftpfd != -1) |
1890 | 0 | poll_listen(listener->tftpfd, POLLIN); |
1891 | 0 | } |
1892 | | #endif |
1893 | | |
1894 | | static void set_dns_listeners(void) |
1895 | 0 | { |
1896 | 0 | struct serverfd *serverfdp; |
1897 | 0 | struct listener *listener; |
1898 | 0 | struct randfd_list *rfl; |
1899 | 0 | int i, j; |
1900 | | |
1901 | 0 | for (serverfdp = daemon->sfds; serverfdp; serverfdp = serverfdp->next) |
1902 | 0 | poll_listen(serverfdp->fd, POLLIN); |
1903 | | |
1904 | 0 | for (i = 0; i < daemon->numrrand; i++) |
1905 | 0 | if (daemon->randomsocks[i].refcount != 0) |
1906 | 0 | poll_listen(daemon->randomsocks[i].fd, POLLIN); |
1907 | | |
1908 | | /* Check overflow random sockets too. */ |
1909 | 0 | for (rfl = daemon->rfl_poll; rfl; rfl = rfl->next) |
1910 | 0 | poll_listen(rfl->rfd->fd, POLLIN); |
1911 | | |
1912 | | /* check to see if we have free tcp process slots. */ |
1913 | 0 | for (j = 0, i = daemon->max_procs - 1; i >= 0; i--) |
1914 | 0 | if (daemon->tcp_pids[i] == 0 && daemon->tcp_pipes[i] == -1 && ++j > daemon->dnssec_tcp_slot_reserved) |
1915 | 0 | break; |
1916 | |
|
1917 | 0 | for (listener = daemon->listeners; listener; listener = listener->next) |
1918 | 0 | { |
1919 | 0 | if (listener->fd != -1) |
1920 | 0 | poll_listen(listener->fd, POLLIN); |
1921 | | |
1922 | | /* Only listen for TCP connections when a process slot |
1923 | | is available. Death of a child goes through the select loop, so |
1924 | | we don't need to explicitly arrange to wake up here, |
1925 | | we'll be called again when a slot becomes available. */ |
1926 | 0 | if (listener->tcpfd != -1 && i >= 0) |
1927 | 0 | poll_listen(listener->tcpfd, POLLIN); |
1928 | 0 | } |
1929 | | |
1930 | 0 | if (!option_bool(OPT_DEBUG)) |
1931 | 0 | for (i = 0; i < daemon->max_procs; i++) |
1932 | 0 | if (daemon->tcp_pipes[i] != -1) |
1933 | 0 | poll_listen(daemon->tcp_pipes[i], POLLIN); |
1934 | 0 | } |
1935 | | |
1936 | | static void check_dns_listeners(time_t now) |
1937 | 0 | { |
1938 | 0 | struct serverfd *serverfdp; |
1939 | 0 | struct listener *listener; |
1940 | 0 | struct randfd_list *rfl; |
1941 | 0 | int i, j; |
1942 | | |
1943 | | /* Note that handling events here can create or destroy fds and |
1944 | | render the result of the last poll() call invalid. Once |
1945 | | we find an fd that needs service, do it, then return to go around the |
1946 | | poll() loop again. This avoid really, really, weird bugs. */ |
1947 | |
|
1948 | 0 | if (!option_bool(OPT_DEBUG)) |
1949 | 0 | for (i = 0; i < daemon->max_procs; i++) |
1950 | 0 | if (daemon->tcp_pipes[i] != -1 && |
1951 | 0 | poll_check(daemon->tcp_pipes[i], POLLIN | POLLHUP)) |
1952 | 0 | { |
1953 | | /* Races. The child process can die before we read all of the data from the |
1954 | | pipe, or vice versa. Therefore send tcp_pids to zero when we wait() the |
1955 | | process, and tcp_pipes to -1 and close the FD when we read the last |
1956 | | of the data - indicated by cache_recv_insert returning zero. |
1957 | | The order of these events is indeterminate, and both are needed |
1958 | | to free the process slot. Once the child process has gone, poll() |
1959 | | returns POLLHUP, not POLLIN, so have to check for both here. */ |
1960 | 0 | if (!cache_recv_insert(now, daemon->tcp_pipes[i])) |
1961 | 0 | { |
1962 | 0 | close(daemon->tcp_pipes[i]); |
1963 | 0 | daemon->tcp_pipes[i] = -1; |
1964 | | /* tcp_pipes == -1 && tcp_pids == 0 required to free slot */ |
1965 | 0 | if (daemon->tcp_pids[i] == 0) |
1966 | 0 | { |
1967 | 0 | daemon->metrics[METRIC_TCP_CONNECTIONS]--; |
1968 | 0 | if (daemon->tcp_type[i]) |
1969 | 0 | daemon->dnssec_tcp_slot_reserved++; |
1970 | 0 | } |
1971 | 0 | } |
1972 | 0 | return; |
1973 | 0 | } |
1974 | | |
1975 | 0 | for (serverfdp = daemon->sfds; serverfdp; serverfdp = serverfdp->next) |
1976 | 0 | if (poll_check(serverfdp->fd, POLLIN)) |
1977 | 0 | { |
1978 | 0 | reply_query(serverfdp->fd, now); |
1979 | 0 | return; |
1980 | 0 | } |
1981 | | |
1982 | 0 | for (i = 0; i < daemon->numrrand; i++) |
1983 | 0 | if (daemon->randomsocks[i].refcount != 0 && |
1984 | 0 | poll_check(daemon->randomsocks[i].fd, POLLIN)) |
1985 | 0 | { |
1986 | 0 | reply_query(daemon->randomsocks[i].fd, now); |
1987 | 0 | return; |
1988 | 0 | } |
1989 | | |
1990 | | /* Check overflow random sockets too. */ |
1991 | 0 | for (rfl = daemon->rfl_poll; rfl; rfl = rfl->next) |
1992 | 0 | if (poll_check(rfl->rfd->fd, POLLIN)) |
1993 | 0 | { |
1994 | 0 | reply_query(rfl->rfd->fd, now); |
1995 | 0 | return; |
1996 | 0 | } |
1997 | | |
1998 | 0 | for (listener = daemon->listeners; listener; listener = listener->next) |
1999 | 0 | if (listener->fd != -1 && poll_check(listener->fd, POLLIN)) |
2000 | 0 | { |
2001 | 0 | receive_query(listener, now); |
2002 | 0 | return; |
2003 | 0 | } |
2004 | | |
2005 | | /* check to see if we have a free tcp process slot. |
2006 | | Note that we can't assume that because we had |
2007 | | at least one a poll() time, that we still do. |
2008 | | There may be more waiting connections after |
2009 | | poll() returns then free process slots. */ |
2010 | 0 | for (j = 0,i = daemon->max_procs - 1; i >= 0; i--) |
2011 | 0 | if (daemon->tcp_pids[i] == 0 && daemon->tcp_pipes[i] == -1 && ++j > daemon->dnssec_tcp_slot_reserved) |
2012 | 0 | break; |
2013 | |
|
2014 | 0 | if (i >= 0) |
2015 | 0 | for (listener = daemon->listeners; listener; listener = listener->next) |
2016 | 0 | if (listener->tcpfd != -1 && poll_check(listener->tcpfd, POLLIN)) |
2017 | 0 | { |
2018 | 0 | do_tcp_connection(listener, now, i); |
2019 | 0 | return; |
2020 | 0 | } |
2021 | 0 | } |
2022 | | |
2023 | | static void do_tcp_connection(struct listener *listener, time_t now, int slot) |
2024 | 0 | { |
2025 | 0 | int confd; |
2026 | 0 | struct irec *iface = NULL; |
2027 | 0 | pid_t p; |
2028 | 0 | union mysockaddr tcp_addr; |
2029 | 0 | socklen_t tcp_len = sizeof(union mysockaddr); |
2030 | 0 | struct server *s; |
2031 | 0 | struct listener *l; |
2032 | 0 | int flags, auth_dns = 0; |
2033 | 0 | struct in_addr netmask; |
2034 | 0 | int pipefd[2]; |
2035 | 0 | struct iovec tcpbuff; |
2036 | 0 | unsigned char a = 0; |
2037 | 0 | netmask.s_addr = 0; |
2038 | | |
2039 | 0 | while ((confd = accept(listener->tcpfd, NULL, NULL)) == -1 && errno == EINTR); |
2040 | | |
2041 | 0 | if (confd == -1) |
2042 | 0 | { |
2043 | | /* Transient errors: just return and retry on next poll cycle. */ |
2044 | 0 | if (errno == EAGAIN || errno == ECONNABORTED || |
2045 | 0 | errno == EMFILE || errno == ENFILE || |
2046 | 0 | errno == ENOMEM || errno == ENOBUFS) |
2047 | 0 | return; |
2048 | | |
2049 | | /* Fatal error (EINVAL, EBADF, etc): socket is permanently broken. |
2050 | | Close it so poll() no longer selects it. In --bind-dynamic mode |
2051 | | the listener will be rebuilt on the next address change event. */ |
2052 | 0 | close(listener->tcpfd); |
2053 | 0 | listener->tcpfd = -1; |
2054 | 0 | return; |
2055 | 0 | } |
2056 | | |
2057 | 0 | if (getsockname(confd, (struct sockaddr *)&tcp_addr, &tcp_len) == -1) |
2058 | 0 | { |
2059 | 0 | closeconandreturn: |
2060 | 0 | shutdown(confd, SHUT_RDWR); |
2061 | 0 | close(confd); |
2062 | 0 | return; |
2063 | 0 | } |
2064 | | |
2065 | | /* Make sure that the interface list is up-to-date. |
2066 | | |
2067 | | We do this here as we may need the results below, and |
2068 | | the DNS code needs them for --interface-name stuff. |
2069 | | |
2070 | | Multiple calls to enumerate_interfaces() per select loop are |
2071 | | inhibited, so calls to it in the child process (which doesn't select()) |
2072 | | have no effect. This avoids two processes reading from the same |
2073 | | netlink fd and screwing the pooch entirely. |
2074 | | */ |
2075 | | |
2076 | 0 | enumerate_interfaces(0); |
2077 | | |
2078 | 0 | if (option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND)) |
2079 | 0 | { |
2080 | 0 | if ((iface = listener->iface)) |
2081 | 0 | { |
2082 | 0 | netmask = iface->netmask; |
2083 | 0 | auth_dns = iface->dns_auth; |
2084 | 0 | } |
2085 | 0 | } |
2086 | 0 | else |
2087 | 0 | { |
2088 | 0 | int if_index, got_index = 0; |
2089 | 0 | char intr_name[IF_NAMESIZE]; |
2090 | | |
2091 | | /* if we can find the arrival interface, check it's one that's allowed |
2092 | | tcp_interface() is not implemented on non-Linux platforms */ |
2093 | 0 | if ((if_index = tcp_interface(confd, tcp_addr.sa.sa_family)) != 0 && |
2094 | 0 | indextoname(listener->tcpfd, if_index, intr_name)) |
2095 | 0 | { |
2096 | 0 | union all_addr addr; |
2097 | |
|
2098 | 0 | got_index = 1; |
2099 | | |
2100 | 0 | if (tcp_addr.sa.sa_family == AF_INET6) |
2101 | 0 | addr.addr6 = tcp_addr.in6.sin6_addr; |
2102 | 0 | else |
2103 | 0 | addr.addr4 = tcp_addr.in.sin_addr; |
2104 | | |
2105 | 0 | if (!iface_check(tcp_addr.sa.sa_family, &addr, intr_name, &auth_dns) && |
2106 | 0 | !loopback_exception(listener->tcpfd, tcp_addr.sa.sa_family, &addr, intr_name)) |
2107 | 0 | goto closeconandreturn; |
2108 | 0 | } |
2109 | | |
2110 | | /* When binding the wildcard address, try and get the |
2111 | | netmask of the interface for localisation. */ |
2112 | 0 | for (iface = daemon->interfaces; iface; iface = iface->next) |
2113 | 0 | if (sockaddr_isequal(&iface->addr, &tcp_addr)) |
2114 | 0 | { |
2115 | 0 | netmask = iface->netmask; |
2116 | 0 | break; |
2117 | 0 | } |
2118 | | |
2119 | | /* On platforms where tcp_interface() doesn't work, we rely |
2120 | | of the presence of the local address of the connection in the |
2121 | | interface list to check if we're accepting this connection and |
2122 | | for its auth status. */ |
2123 | 0 | if (!got_index) |
2124 | 0 | { |
2125 | 0 | if (!iface) |
2126 | 0 | goto closeconandreturn; |
2127 | | |
2128 | 0 | auth_dns = iface->dns_auth; |
2129 | 0 | } |
2130 | 0 | } |
2131 | | |
2132 | 0 | if (!option_bool(OPT_DEBUG)) |
2133 | 0 | { |
2134 | | /* The code in edns0.c that decorates queries with the source MAC address depends |
2135 | | on the code in arp.c, which populates a cache with the contents of the ARP table |
2136 | | using netlink. Since the child process can't use netlink, we pre-populate |
2137 | | the cache with the ARP table entry for our source here, including a negative entry |
2138 | | if there is nothing for our address in the ARP table. |
2139 | | |
2140 | | When the edns0 code calls find_mac() in the child process, it will |
2141 | | get the correct answer from the cache inherited from the parent |
2142 | | without having to use netlink to consult the kernel ARP table. |
2143 | | |
2144 | | edns0_needs_mac() simply calls find_mac if any EDNS0 options |
2145 | | which need a MAC address are enabled. */ |
2146 | | |
2147 | 0 | edns0_needs_mac(&tcp_addr, now); |
2148 | |
|
2149 | 0 | if (pipe(pipefd) == -1) |
2150 | 0 | goto closeconandreturn; /* pipe failed */ |
2151 | | |
2152 | 0 | if ((p = fork()) == -1) |
2153 | 0 | { |
2154 | | /* fork failed */ |
2155 | 0 | close(pipefd[0]); |
2156 | 0 | close(pipefd[1]); |
2157 | 0 | goto closeconandreturn; |
2158 | 0 | } |
2159 | | |
2160 | 0 | if (p != 0) |
2161 | 0 | { |
2162 | | /* fork() done: parent side */ |
2163 | 0 | close(pipefd[1]); /* parent needs read pipe end. */ |
2164 | | |
2165 | | /* The child process inherits various sockets, |
2166 | | whose existence can mess up the function of |
2167 | | the parent. See below for details of which |
2168 | | sockets, and why they cause problems. The |
2169 | | child closes the sockets during startup, |
2170 | | but there's a nasty race, since the parent |
2171 | | may use them before the child has done the close. |
2172 | | |
2173 | | To avoid the race, the parent blocks here until a |
2174 | | single byte comes back up the pipe, which |
2175 | | is sent when the child has finished the close. */ |
2176 | 0 | read_write(pipefd[0], &a, 1, RW_READ); |
2177 | | |
2178 | 0 | daemon->tcp_pids[slot] = p; |
2179 | 0 | daemon->tcp_pipes[slot] = pipefd[0]; |
2180 | 0 | daemon->tcp_type[slot] = 0; |
2181 | 0 | daemon->metrics[METRIC_TCP_CONNECTIONS]++; |
2182 | 0 | if (daemon->metrics[METRIC_TCP_CONNECTIONS] > daemon->max_procs_used) |
2183 | 0 | daemon->max_procs_used = daemon->metrics[METRIC_TCP_CONNECTIONS]; |
2184 | | |
2185 | 0 | close(confd); |
2186 | | |
2187 | | /* The child can use up to TCP_MAX_QUERIES ids, so skip that many. */ |
2188 | 0 | daemon->log_id += TCP_MAX_QUERIES; |
2189 | | #ifdef HAVE_DNSSEC |
2190 | | /* It can do more if making DNSSEC queries too. */ |
2191 | | if (option_bool(OPT_DNSSEC_VALID)) |
2192 | | daemon->log_id += daemon->limit[LIMIT_WORK]; |
2193 | | #endif |
2194 | | |
2195 | 0 | return; |
2196 | 0 | } |
2197 | | |
2198 | 0 | #ifdef HAVE_LINUX_NETWORK |
2199 | | /* The child process inherits the netlink socket, |
2200 | | which it never uses, but when the parent |
2201 | | uses it in the future, the answer may go to the |
2202 | | child, resulting in the parent blocking |
2203 | | forever awaiting the result. To avoid this |
2204 | | close the netlink socket in the child. */ |
2205 | 0 | close(daemon->netlinkfd); |
2206 | | #elif defined(HAVE_BSD_NETWORK) |
2207 | | close(daemon->routefd); |
2208 | | #endif |
2209 | | |
2210 | | /* Close inherited listening sockets in the child process. |
2211 | | These are not needed here and holding them prevents the |
2212 | | parent from re-binding if an interface is removed and |
2213 | | re-added (the child's copy causes EADDRINUSE). */ |
2214 | 0 | for (l = daemon->listeners; l; l = l->next) |
2215 | 0 | { |
2216 | 0 | if (l->fd != -1) |
2217 | 0 | close(l->fd); |
2218 | 0 | if (l->tcpfd != -1) |
2219 | 0 | close(l->tcpfd); |
2220 | 0 | if (l->tftpfd != -1) |
2221 | 0 | close(l->tftpfd); |
2222 | 0 | } |
2223 | | |
2224 | | /* unblock parent now we're not holding sockets we shouldn't. */ |
2225 | 0 | read_write(pipefd[1], &a, 1, RW_WRITE); |
2226 | | |
2227 | | /* Arrange for SIGALRM after CHILD_LIFETIME seconds to |
2228 | | terminate the process. */ |
2229 | 0 | alarm(CHILD_LIFETIME); |
2230 | 0 | close(pipefd[0]); /* close read end in child. */ |
2231 | 0 | daemon->pipe_to_parent = pipefd[1]; |
2232 | |
|
2233 | 0 | } |
2234 | | |
2235 | | /* The connected socket inherits non-blocking |
2236 | | attribute from the listening socket. |
2237 | | Reset that here. */ |
2238 | 0 | if ((flags = fcntl(confd, F_GETFL, 0)) != -1) |
2239 | 0 | while(retry_send(fcntl(confd, F_SETFL, flags & ~O_NONBLOCK))); |
2240 | |
|
2241 | 0 | tcp_request(confd, now, &tcpbuff, &tcp_addr, netmask, auth_dns); |
2242 | 0 | free(tcpbuff.iov_base); |
2243 | | |
2244 | 0 | for (s = daemon->servers; s; s = s->next) |
2245 | 0 | if (s->tcpfd != -1) |
2246 | 0 | { |
2247 | 0 | shutdown(s->tcpfd, SHUT_RDWR); |
2248 | 0 | close(s->tcpfd); |
2249 | 0 | s->tcpfd = -1; |
2250 | 0 | } |
2251 | | |
2252 | 0 | if (!option_bool(OPT_DEBUG)) |
2253 | 0 | { |
2254 | | #ifdef HAVE_DNSSEC |
2255 | | cache_update_hwm(); /* Sneak out possibly updated crypto HWM values. */ |
2256 | | #endif |
2257 | |
|
2258 | 0 | close(daemon->pipe_to_parent); |
2259 | 0 | flush_log(); |
2260 | 0 | _exit(0); |
2261 | 0 | } |
2262 | 0 | } |
2263 | | |
2264 | | |
2265 | | #ifdef HAVE_DNSSEC |
2266 | | /* If a DNSSEC query over UDP returns a truncated answer, |
2267 | | we swap to the TCP path. This routine is responsible for forking |
2268 | | the required process, the child then calls tcp_key_recurse() and |
2269 | | returns the result of the validation through the pipe to the parent |
2270 | | (which has also primed the cache with the relevant DS and DNSKEY records). |
2271 | | If we're in debug mode, don't fork and return the result directly, otherwise |
2272 | | return STAT_ASYNC. The UDP validation process will restart when |
2273 | | cache_recv_insert() calls pop_and_retry_query() after the result |
2274 | | arrives via the pipe to the parent. */ |
2275 | | int swap_to_tcp(struct frec *forward, time_t now, int status, struct dns_header *header, |
2276 | | ssize_t *plen, char *name, int class, struct server *server, int *keycount, int *validatecount) |
2277 | | { |
2278 | | struct server *s; |
2279 | | struct listener *l; |
2280 | | |
2281 | | if (!option_bool(OPT_DEBUG)) |
2282 | | { |
2283 | | pid_t p; |
2284 | | int i, pipefd[2]; |
2285 | | unsigned char a = 0; |
2286 | | |
2287 | | /* check to see if we have a free tcp process slot. */ |
2288 | | for (i = daemon->max_procs - 1; i >= 0; i--) |
2289 | | if (daemon->tcp_pids[i] == 0 && daemon->tcp_pipes[i] == -1) |
2290 | | break; |
2291 | | |
2292 | | /* No slots or no pipe */ |
2293 | | if (i < 0 || pipe(pipefd) != 0) |
2294 | | return STAT_ABANDONED; |
2295 | | |
2296 | | if ((p = fork()) != 0) |
2297 | | { |
2298 | | close(pipefd[1]); /* parent needs read pipe end. */ |
2299 | | if (p == -1) |
2300 | | { |
2301 | | /* fork() failed */ |
2302 | | close(pipefd[0]); |
2303 | | return STAT_ABANDONED; |
2304 | | } |
2305 | | |
2306 | | /* The child process inherits various sockets, |
2307 | | whose existence can mess up the function of |
2308 | | the parent. See below for details of which |
2309 | | sockets, and why they cause problems. The |
2310 | | child closes the sockets during startup, |
2311 | | but there's a nasty race, since the parent |
2312 | | may use them before the child has done the close. |
2313 | | |
2314 | | To avoid the race, the parent blocks here until a |
2315 | | single byte comes back up the pipe, which |
2316 | | is sent when the child has finished the close. */ |
2317 | | read_write(pipefd[0], &a, 1, RW_READ); |
2318 | | |
2319 | | /* i holds index of free slot */ |
2320 | | daemon->tcp_pids[i] = p; |
2321 | | daemon->tcp_pipes[i] = pipefd[0]; |
2322 | | daemon->tcp_type[i] = 1; |
2323 | | daemon->dnssec_tcp_slot_reserved--; |
2324 | | daemon->metrics[METRIC_TCP_CONNECTIONS]++; |
2325 | | if (daemon->metrics[METRIC_TCP_CONNECTIONS] > daemon->max_procs_used) |
2326 | | daemon->max_procs_used = daemon->metrics[METRIC_TCP_CONNECTIONS]; |
2327 | | |
2328 | | /* child can use a maximum of this many log serials. */ |
2329 | | daemon->log_id += daemon->limit[LIMIT_WORK]; |
2330 | | |
2331 | | /* tell the caller we've forked. */ |
2332 | | return STAT_ASYNC; |
2333 | | } |
2334 | | else |
2335 | | { |
2336 | | /* child starts here. */ |
2337 | | #ifdef HAVE_LINUX_NETWORK |
2338 | | /* The child process inherits the netlink socket, |
2339 | | which it never uses, but when the parent |
2340 | | uses it in the future, the answer may go to the |
2341 | | child, resulting in the parent blocking |
2342 | | forever awaiting the result. To avoid this |
2343 | | close the netlink socket in the child. */ |
2344 | | close(daemon->netlinkfd); |
2345 | | #elif defined(HAVE_BSD_NETWORK) |
2346 | | close(daemon->routefd); |
2347 | | #endif |
2348 | | |
2349 | | /* Close inherited listening sockets in the child process. |
2350 | | These are not needed here and holding them prevents the |
2351 | | parent from re-binding if an interface is removed and |
2352 | | re-added (the child's copy causes EADDRINUSE). */ |
2353 | | for (l = daemon->listeners; l; l = l->next) |
2354 | | { |
2355 | | if (l->fd != -1) |
2356 | | close(l->fd); |
2357 | | if (l->tcpfd != -1) |
2358 | | close(l->tcpfd); |
2359 | | if (l->tftpfd != -1) |
2360 | | close(l->tftpfd); |
2361 | | } |
2362 | | |
2363 | | /* unblock parent now we're not holding sockets we shouldn't. */ |
2364 | | read_write(pipefd[1], &a, 1, RW_WRITE); |
2365 | | |
2366 | | alarm(CHILD_LIFETIME); |
2367 | | close(pipefd[0]); /* close read end in child. */ |
2368 | | daemon->pipe_to_parent = pipefd[1]; |
2369 | | daemon->forward_to_tcp = forward; |
2370 | | daemon->header_to_tcp = header; |
2371 | | daemon->plen_to_tcp = *plen; |
2372 | | } |
2373 | | } |
2374 | | |
2375 | | status = tcp_from_udp(now, status, header, plen, class, name, server, keycount, validatecount); |
2376 | | |
2377 | | /* close upstream connections. */ |
2378 | | for (s = daemon->servers; s; s = s->next) |
2379 | | if (s->tcpfd != -1) |
2380 | | { |
2381 | | shutdown(s->tcpfd, SHUT_RDWR); |
2382 | | close(s->tcpfd); |
2383 | | s->tcpfd = -1; |
2384 | | } |
2385 | | |
2386 | | if (!option_bool(OPT_DEBUG)) |
2387 | | { |
2388 | | unsigned char op = PIPE_OP_RESULT; |
2389 | | |
2390 | | /* tell our parent we're done, and what the result was then exit. */ |
2391 | | read_write(daemon->pipe_to_parent, &op, sizeof(op), RW_WRITE); |
2392 | | read_write(daemon->pipe_to_parent, (unsigned char *)&status, sizeof(status), RW_WRITE); |
2393 | | read_write(daemon->pipe_to_parent, (unsigned char *)plen, sizeof(*plen), RW_WRITE); |
2394 | | read_write(daemon->pipe_to_parent, (unsigned char *)header, *plen, RW_WRITE); |
2395 | | read_write(daemon->pipe_to_parent, (unsigned char *)&forward, sizeof(forward), RW_WRITE); |
2396 | | read_write(daemon->pipe_to_parent, (unsigned char *)&forward->uid, sizeof(forward->uid), RW_WRITE); |
2397 | | read_write(daemon->pipe_to_parent, (unsigned char *)keycount, sizeof(*keycount), RW_WRITE); |
2398 | | read_write(daemon->pipe_to_parent, (unsigned char *)&keycount, sizeof(keycount), RW_WRITE); |
2399 | | read_write(daemon->pipe_to_parent, (unsigned char *)validatecount, sizeof(*validatecount), RW_WRITE); |
2400 | | read_write(daemon->pipe_to_parent, (unsigned char *)&validatecount, sizeof(validatecount), RW_WRITE); |
2401 | | |
2402 | | cache_update_hwm(); /* Sneak out possibly updated crypto HWM values. */ |
2403 | | |
2404 | | close(daemon->pipe_to_parent); |
2405 | | |
2406 | | flush_log(); |
2407 | | _exit(0); |
2408 | | } |
2409 | | |
2410 | | /* path for debug mode. */ |
2411 | | return status; |
2412 | | } |
2413 | | #endif |
2414 | | |
2415 | | |
2416 | | #ifdef HAVE_DHCP |
2417 | | int make_icmp_sock(void) |
2418 | 0 | { |
2419 | 0 | int fd; |
2420 | 0 | int zeroopt = 0; |
2421 | |
|
2422 | 0 | if ((fd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP)) != -1) |
2423 | 0 | { |
2424 | 0 | if (!fix_fd(fd) || |
2425 | 0 | setsockopt(fd, SOL_SOCKET, SO_DONTROUTE, &zeroopt, sizeof(zeroopt)) == -1) |
2426 | 0 | { |
2427 | 0 | close(fd); |
2428 | 0 | fd = -1; |
2429 | 0 | } |
2430 | 0 | } |
2431 | |
|
2432 | 0 | return fd; |
2433 | 0 | } |
2434 | | |
2435 | | int icmp_ping(struct in_addr addr) |
2436 | 0 | { |
2437 | | /* Try and get an ICMP echo from a machine. */ |
2438 | |
|
2439 | 0 | int fd; |
2440 | 0 | struct sockaddr_in saddr; |
2441 | 0 | struct { |
2442 | 0 | struct ip ip; |
2443 | 0 | struct icmp icmp; |
2444 | 0 | } packet; |
2445 | 0 | unsigned short id = rand16(); |
2446 | 0 | unsigned int i, j; |
2447 | 0 | int gotreply = 0; |
2448 | |
|
2449 | 0 | #if defined(HAVE_LINUX_NETWORK) || defined (HAVE_SOLARIS_NETWORK) |
2450 | 0 | if ((fd = make_icmp_sock()) == -1) |
2451 | 0 | return 0; |
2452 | | #else |
2453 | | int opt = 2000; |
2454 | | fd = daemon->dhcp_icmp_fd; |
2455 | | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)); |
2456 | | #endif |
2457 | | |
2458 | 0 | saddr.sin_family = AF_INET; |
2459 | 0 | saddr.sin_port = 0; |
2460 | 0 | saddr.sin_addr = addr; |
2461 | | #ifdef HAVE_SOCKADDR_SA_LEN |
2462 | | saddr.sin_len = sizeof(struct sockaddr_in); |
2463 | | #endif |
2464 | | |
2465 | 0 | memset(&packet.icmp, 0, sizeof(packet.icmp)); |
2466 | 0 | packet.icmp.icmp_type = ICMP_ECHO; |
2467 | 0 | packet.icmp.icmp_id = id; |
2468 | 0 | for (j = 0, i = 0; i < sizeof(struct icmp) / 2; i++) |
2469 | 0 | j += ((u16 *)&packet.icmp)[i]; |
2470 | 0 | while (j>>16) |
2471 | 0 | j = (j & 0xffff) + (j >> 16); |
2472 | 0 | packet.icmp.icmp_cksum = (j == 0xffff) ? j : ~j; |
2473 | | |
2474 | 0 | while (retry_send(sendto(fd, (char *)&packet.icmp, sizeof(struct icmp), 0, |
2475 | 0 | (struct sockaddr *)&saddr, sizeof(saddr)))); |
2476 | | |
2477 | 0 | gotreply = delay_dhcp(dnsmasq_time(), PING_WAIT, fd, addr.s_addr, id); |
2478 | |
|
2479 | 0 | #if defined(HAVE_LINUX_NETWORK) || defined(HAVE_SOLARIS_NETWORK) |
2480 | 0 | close(fd); |
2481 | | #else |
2482 | | opt = 1; |
2483 | | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)); |
2484 | | #endif |
2485 | |
|
2486 | 0 | return gotreply; |
2487 | 0 | } |
2488 | | |
2489 | | int delay_dhcp(time_t start, int sec, int fd, uint32_t addr, unsigned short id) |
2490 | 0 | { |
2491 | | /* Delay processing DHCP packets for "sec" seconds counting from "start". |
2492 | | If "fd" is not -1 it will stop waiting if an ICMP echo reply is received |
2493 | | from "addr" with ICMP ID "id" and return 1 */ |
2494 | | |
2495 | | /* Note that whilst waiting, we check for |
2496 | | (and service) events on the DNS and TFTP sockets, (so doing that |
2497 | | better not use any resources our caller has in use...) |
2498 | | but we remain deaf to signals or further DHCP packets. */ |
2499 | | |
2500 | | /* There can be a problem using dnsmasq_time() to end the loop, since |
2501 | | it's not monotonic, and can go backwards if the system clock is |
2502 | | tweaked, leading to the code getting stuck in this loop and |
2503 | | ignoring DHCP requests. To fix this, we check to see if select returned |
2504 | | as a result of a timeout rather than a socket becoming available. We |
2505 | | only allow this to happen as many times as it takes to get to the wait time |
2506 | | in quarter-second chunks. This provides a fallback way to end loop. */ |
2507 | |
|
2508 | 0 | int rc, timeout_count; |
2509 | 0 | time_t now; |
2510 | |
|
2511 | 0 | for (now = dnsmasq_time(), timeout_count = 0; |
2512 | 0 | (difftime(now, start) <= (float)sec) && (timeout_count < sec * 4);) |
2513 | 0 | { |
2514 | 0 | poll_reset(); |
2515 | 0 | if (fd != -1) |
2516 | 0 | poll_listen(fd, POLLIN); |
2517 | 0 | if (daemon->port != 0) |
2518 | 0 | set_dns_listeners(); |
2519 | 0 | #ifdef HAVE_TFTP |
2520 | 0 | set_tftp_listeners(); |
2521 | 0 | #endif |
2522 | 0 | set_log_writer(); |
2523 | | |
2524 | 0 | #ifdef HAVE_DHCP6 |
2525 | 0 | if (daemon->doing_ra) |
2526 | 0 | poll_listen(daemon->icmp6fd, POLLIN); |
2527 | 0 | #endif |
2528 | | |
2529 | 0 | rc = do_poll(250); |
2530 | | |
2531 | 0 | if (rc < 0) |
2532 | 0 | continue; |
2533 | 0 | else if (rc == 0) |
2534 | 0 | timeout_count++; |
2535 | | |
2536 | 0 | now = dnsmasq_time(); |
2537 | | |
2538 | 0 | check_log_writer(0); |
2539 | 0 | if (daemon->port != 0) |
2540 | 0 | check_dns_listeners(now); |
2541 | | |
2542 | 0 | #ifdef HAVE_DHCP6 |
2543 | 0 | if (daemon->doing_ra && poll_check(daemon->icmp6fd, POLLIN)) |
2544 | 0 | icmp6_packet(now); |
2545 | 0 | #endif |
2546 | | |
2547 | 0 | #ifdef HAVE_TFTP |
2548 | 0 | check_tftp_listeners(now); |
2549 | 0 | #endif |
2550 | |
|
2551 | 0 | if (fd != -1) |
2552 | 0 | { |
2553 | 0 | struct { |
2554 | 0 | struct ip ip; |
2555 | 0 | struct icmp icmp; |
2556 | 0 | } packet; |
2557 | 0 | struct sockaddr_in faddr; |
2558 | 0 | socklen_t len = sizeof(faddr); |
2559 | | |
2560 | 0 | if (poll_check(fd, POLLIN) && |
2561 | 0 | recvfrom(fd, &packet, sizeof(packet), 0, (struct sockaddr *)&faddr, &len) == sizeof(packet) && |
2562 | 0 | addr == faddr.sin_addr.s_addr && |
2563 | 0 | packet.icmp.icmp_type == ICMP_ECHOREPLY && |
2564 | 0 | packet.icmp.icmp_seq == 0 && |
2565 | 0 | packet.icmp.icmp_id == id) |
2566 | 0 | return 1; |
2567 | 0 | } |
2568 | 0 | } |
2569 | | |
2570 | 0 | return 0; |
2571 | 0 | } |
2572 | | #endif /* HAVE_DHCP */ |
2573 | | |
2574 | | |