blob: cbdce9ca82966ca84ca49598def8aecfbf36712e [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* dnsmasq is Copyright (c) 2000-2021 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#include "dnsmasq.h"
18
19#ifdef HAVE_DBUS
20
21#include <dbus/dbus.h>
22
23const char* introspection_xml_template =
24"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
25"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
26"<node name=\"" DNSMASQ_PATH "\">\n"
27" <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
28" <method name=\"Introspect\">\n"
29" <arg name=\"data\" direction=\"out\" type=\"s\"/>\n"
30" </method>\n"
31" </interface>\n"
32" <interface name=\"%s\">\n"
33" <method name=\"ClearCache\">\n"
34" </method>\n"
35" <method name=\"GetVersion\">\n"
36" <arg name=\"version\" direction=\"out\" type=\"s\"/>\n"
37" </method>\n"
38#ifdef HAVE_LOOP
39" <method name=\"GetLoopServers\">\n"
40" <arg name=\"server\" direction=\"out\" type=\"as\"/>\n"
41" </method>\n"
42#endif
43" <method name=\"SetServers\">\n"
44" <arg name=\"servers\" direction=\"in\" type=\"av\"/>\n"
45" </method>\n"
46" <method name=\"SetDomainServers\">\n"
47" <arg name=\"servers\" direction=\"in\" type=\"as\"/>\n"
48" </method>\n"
49" <method name=\"SetServersEx\">\n"
50" <arg name=\"servers\" direction=\"in\" type=\"aas\"/>\n"
51" </method>\n"
52" <method name=\"SetFilterWin2KOption\">\n"
53" <arg name=\"filterwin2k\" direction=\"in\" type=\"b\"/>\n"
54" </method>\n"
55" <method name=\"SetBogusPrivOption\">\n"
56" <arg name=\"boguspriv\" direction=\"in\" type=\"b\"/>\n"
57" </method>\n"
58" <signal name=\"DhcpLeaseAdded\">\n"
59" <arg name=\"ipaddr\" type=\"s\"/>\n"
60" <arg name=\"hwaddr\" type=\"s\"/>\n"
61" <arg name=\"hostname\" type=\"s\"/>\n"
62" </signal>\n"
63" <signal name=\"DhcpLeaseDeleted\">\n"
64" <arg name=\"ipaddr\" type=\"s\"/>\n"
65" <arg name=\"hwaddr\" type=\"s\"/>\n"
66" <arg name=\"hostname\" type=\"s\"/>\n"
67" </signal>\n"
68" <signal name=\"DhcpLeaseUpdated\">\n"
69" <arg name=\"ipaddr\" type=\"s\"/>\n"
70" <arg name=\"hwaddr\" type=\"s\"/>\n"
71" <arg name=\"hostname\" type=\"s\"/>\n"
72" </signal>\n"
73#ifdef HAVE_DHCP
74" <method name=\"AddDhcpLease\">\n"
75" <arg name=\"ipaddr\" type=\"s\"/>\n"
76" <arg name=\"hwaddr\" type=\"s\"/>\n"
77" <arg name=\"hostname\" type=\"ay\"/>\n"
78" <arg name=\"clid\" type=\"ay\"/>\n"
79" <arg name=\"lease_duration\" type=\"u\"/>\n"
80" <arg name=\"ia_id\" type=\"u\"/>\n"
81" <arg name=\"is_temporary\" type=\"b\"/>\n"
82" </method>\n"
83" <method name=\"DeleteDhcpLease\">\n"
84" <arg name=\"ipaddr\" type=\"s\"/>\n"
85" <arg name=\"success\" type=\"b\" direction=\"out\"/>\n"
86" </method>\n"
87#endif
88" <method name=\"GetMetrics\">\n"
89" <arg name=\"metrics\" direction=\"out\" type=\"a{su}\"/>\n"
90" </method>\n"
91" </interface>\n"
92"</node>\n";
93
94static char *introspection_xml = NULL;
95
96struct watch {
97 DBusWatch *watch;
98 struct watch *next;
99};
100
101
102static dbus_bool_t add_watch(DBusWatch *watch, void *data)
103{
104 struct watch *w;
105
106 for (w = daemon->watches; w; w = w->next)
107 if (w->watch == watch)
108 return TRUE;
109
110 if (!(w = whine_malloc(sizeof(struct watch))))
111 return FALSE;
112
113 w->watch = watch;
114 w->next = daemon->watches;
115 daemon->watches = w;
116
117 w = data; /* no warning */
118 return TRUE;
119}
120
121static void remove_watch(DBusWatch *watch, void *data)
122{
123 struct watch **up, *w, *tmp;
124
125 for (up = &(daemon->watches), w = daemon->watches; w; w = tmp)
126 {
127 tmp = w->next;
128 if (w->watch == watch)
129 {
130 *up = tmp;
131 free(w);
132 }
133 else
134 up = &(w->next);
135 }
136
137 w = data; /* no warning */
138}
139
140static void dbus_read_servers(DBusMessage *message)
141{
142 DBusMessageIter iter;
143 union mysockaddr addr, source_addr;
144 char *domain;
145
146 dbus_message_iter_init(message, &iter);
147
148 mark_servers(SERV_FROM_DBUS);
149
150 while (1)
151 {
152 int skip = 0;
153
154 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32)
155 {
156 u32 a;
157
158 dbus_message_iter_get_basic(&iter, &a);
159 dbus_message_iter_next (&iter);
160
161#ifdef HAVE_SOCKADDR_SA_LEN
162 source_addr.in.sin_len = addr.in.sin_len = sizeof(struct sockaddr_in);
163#endif
164 addr.in.sin_addr.s_addr = ntohl(a);
165 source_addr.in.sin_family = addr.in.sin_family = AF_INET;
166 addr.in.sin_port = htons(NAMESERVER_PORT);
167 source_addr.in.sin_addr.s_addr = INADDR_ANY;
168 source_addr.in.sin_port = htons(daemon->query_port);
169 }
170 else if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_BYTE)
171 {
172 unsigned char p[sizeof(struct in6_addr)];
173 unsigned int i;
174
175 skip = 1;
176
177 for(i = 0; i < sizeof(struct in6_addr); i++)
178 {
179 dbus_message_iter_get_basic(&iter, &p[i]);
180 dbus_message_iter_next (&iter);
181 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BYTE)
182 {
183 i++;
184 break;
185 }
186 }
187
188 if (i == sizeof(struct in6_addr))
189 {
190 memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr));
191#ifdef HAVE_SOCKADDR_SA_LEN
192 source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(struct sockaddr_in6);
193#endif
194 source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6;
195 addr.in6.sin6_port = htons(NAMESERVER_PORT);
196 source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0;
197 source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0;
198 source_addr.in6.sin6_addr = in6addr_any;
199 source_addr.in6.sin6_port = htons(daemon->query_port);
200 skip = 0;
201 }
202 }
203 else
204 /* At the end */
205 break;
206
207 /* process each domain */
208 do {
209 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
210 {
211 dbus_message_iter_get_basic(&iter, &domain);
212 dbus_message_iter_next (&iter);
213 }
214 else
215 domain = NULL;
216
217 if (!skip)
218 add_update_server(SERV_FROM_DBUS, &addr, &source_addr, NULL, domain, NULL);
219
220 } while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING);
221 }
222
223 /* unlink and free anything still marked. */
224 cleanup_servers();
225}
226
227#ifdef HAVE_LOOP
228static DBusMessage *dbus_reply_server_loop(DBusMessage *message)
229{
230 DBusMessageIter args, args_iter;
231 struct server *serv;
232 DBusMessage *reply = dbus_message_new_method_return(message);
233
234 dbus_message_iter_init_append (reply, &args);
235 dbus_message_iter_open_container (&args, DBUS_TYPE_ARRAY,DBUS_TYPE_STRING_AS_STRING, &args_iter);
236
237 for (serv = daemon->servers; serv; serv = serv->next)
238 if (serv->flags & SERV_LOOP)
239 {
240 (void)prettyprint_addr(&serv->addr, daemon->addrbuff);
241 dbus_message_iter_append_basic (&args_iter, DBUS_TYPE_STRING, &daemon->addrbuff);
242 }
243
244 dbus_message_iter_close_container (&args, &args_iter);
245
246 return reply;
247}
248#endif
249
250static DBusMessage* dbus_read_servers_ex(DBusMessage *message, int strings)
251{
252 DBusMessageIter iter, array_iter, string_iter;
253 DBusMessage *error = NULL;
254 const char *addr_err;
255 char *dup = NULL;
256
257 if (!dbus_message_iter_init(message, &iter))
258 {
259 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
260 "Failed to initialize dbus message iter");
261 }
262
263 /* check that the message contains an array of arrays */
264 if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) ||
265 (dbus_message_iter_get_element_type(&iter) != (strings ? DBUS_TYPE_STRING : DBUS_TYPE_ARRAY)))
266 {
267 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
268 strings ? "Expected array of string" : "Expected array of string arrays");
269 }
270
271 mark_servers(SERV_FROM_DBUS);
272
273 /* array_iter points to each "as" element in the outer array */
274 dbus_message_iter_recurse(&iter, &array_iter);
275 while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID)
276 {
277 const char *str = NULL;
278 union mysockaddr addr, source_addr;
279 u16 flags = 0;
280 char interface[IF_NAMESIZE];
281 char *str_addr, *str_domain = NULL;
282
283 if (strings)
284 {
285 dbus_message_iter_get_basic(&array_iter, &str);
286 if (!str || !strlen (str))
287 {
288 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
289 "Empty string");
290 break;
291 }
292
293 /* dup the string because it gets modified during parsing */
294 if (dup)
295 free(dup);
296 if (!(dup = str_domain = whine_malloc(strlen(str)+1)))
297 break;
298
299 strcpy(str_domain, str);
300
301 /* point to address part of old string for error message */
302 if ((str_addr = strrchr(str, '/')))
303 str = str_addr+1;
304
305 if ((str_addr = strrchr(str_domain, '/')))
306 {
307 if (*str_domain != '/' || str_addr == str_domain)
308 {
309 error = dbus_message_new_error_printf(message,
310 DBUS_ERROR_INVALID_ARGS,
311 "No domain terminator '%s'",
312 str);
313 break;
314 }
315 *str_addr++ = 0;
316 str_domain++;
317 }
318 else
319 {
320 str_addr = str_domain;
321 str_domain = NULL;
322 }
323
324
325 }
326 else
327 {
328 /* check the types of the struct and its elements */
329 if ((dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY) ||
330 (dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_STRING))
331 {
332 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
333 "Expected inner array of strings");
334 break;
335 }
336
337 /* string_iter points to each "s" element in the inner array */
338 dbus_message_iter_recurse(&array_iter, &string_iter);
339 if (dbus_message_iter_get_arg_type(&string_iter) != DBUS_TYPE_STRING)
340 {
341 /* no IP address given */
342 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
343 "Expected IP address");
344 break;
345 }
346
347 dbus_message_iter_get_basic(&string_iter, &str);
348 if (!str || !strlen (str))
349 {
350 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
351 "Empty IP address");
352 break;
353 }
354
355 /* dup the string because it gets modified during parsing */
356 if (dup)
357 free(dup);
358 if (!(dup = str_addr = whine_malloc(strlen(str)+1)))
359 break;
360
361 strcpy(str_addr, str);
362 }
363
364 /* parse the IP address */
365 if ((addr_err = parse_server(str_addr, &addr, &source_addr, (char *) &interface, &flags)))
366 {
367 error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
368 "Invalid IP address '%s': %s",
369 str, addr_err);
370 break;
371 }
372
373 /* 0.0.0.0 for server address == NULL, for Dbus */
374 if (addr.in.sin_family == AF_INET &&
375 addr.in.sin_addr.s_addr == 0)
376 flags |= SERV_LITERAL_ADDRESS;
377
378 if (strings)
379 {
380 char *p;
381
382 do {
383 if (str_domain)
384 {
385 if ((p = strchr(str_domain, '/')))
386 *p++ = 0;
387 }
388 else
389 p = NULL;
390
391 add_update_server(flags | SERV_FROM_DBUS, &addr, &source_addr, interface, str_domain, NULL);
392 } while ((str_domain = p));
393 }
394 else
395 {
396 /* jump past the address to the domain list (if any) */
397 dbus_message_iter_next (&string_iter);
398
399 /* parse domains and add each server/domain pair to the list */
400 do {
401 str = NULL;
402 if (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING)
403 dbus_message_iter_get_basic(&string_iter, &str);
404 dbus_message_iter_next (&string_iter);
405
406 add_update_server(flags | SERV_FROM_DBUS, &addr, &source_addr, interface, str, NULL);
407 } while (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING);
408 }
409
410 /* jump to next element in outer array */
411 dbus_message_iter_next(&array_iter);
412 }
413
414 cleanup_servers();
415
416 if (dup)
417 free(dup);
418
419 return error;
420}
421
422static DBusMessage *dbus_set_bool(DBusMessage *message, int flag, char *name)
423{
424 DBusMessageIter iter;
425 dbus_bool_t enabled;
426
427 if (!dbus_message_iter_init(message, &iter) || dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
428 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, "Expected boolean argument");
429
430 dbus_message_iter_get_basic(&iter, &enabled);
431
432 if (enabled)
433 {
434 my_syslog(LOG_INFO, _("Enabling --%s option from D-Bus"), name);
435 set_option_bool(flag);
436 }
437 else
438 {
439 my_syslog(LOG_INFO, _("Disabling --%s option from D-Bus"), name);
440 reset_option_bool(flag);
441 }
442
443 return NULL;
444}
445
446#ifdef HAVE_DHCP
447static DBusMessage *dbus_add_lease(DBusMessage* message)
448{
449 struct dhcp_lease *lease;
450 const char *ipaddr, *hwaddr, *hostname, *tmp;
451 const unsigned char* clid;
452 int clid_len, hostname_len, hw_len, hw_type;
453 dbus_uint32_t expires, ia_id;
454 dbus_bool_t is_temporary;
455 union all_addr addr;
456 time_t now = dnsmasq_time();
457 unsigned char dhcp_chaddr[DHCP_CHADDR_MAX];
458
459 DBusMessageIter iter, array_iter;
460 if (!dbus_message_iter_init(message, &iter))
461 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
462 "Failed to initialize dbus message iter");
463
464 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
465 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
466 "Expected string as first argument");
467
468 dbus_message_iter_get_basic(&iter, &ipaddr);
469 dbus_message_iter_next(&iter);
470
471 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
472 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
473 "Expected string as second argument");
474
475 dbus_message_iter_get_basic(&iter, &hwaddr);
476 dbus_message_iter_next(&iter);
477
478 if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) ||
479 (dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE))
480 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
481 "Expected byte array as third argument");
482
483 dbus_message_iter_recurse(&iter, &array_iter);
484 dbus_message_iter_get_fixed_array(&array_iter, &hostname, &hostname_len);
485 tmp = memchr(hostname, '\0', hostname_len);
486 if (tmp)
487 {
488 if (tmp == &hostname[hostname_len - 1])
489 hostname_len--;
490 else
491 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
492 "Hostname contains an embedded NUL character");
493 }
494 dbus_message_iter_next(&iter);
495
496 if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) ||
497 (dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE))
498 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
499 "Expected byte array as fourth argument");
500
501 dbus_message_iter_recurse(&iter, &array_iter);
502 dbus_message_iter_get_fixed_array(&array_iter, &clid, &clid_len);
503 dbus_message_iter_next(&iter);
504
505 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
506 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
507 "Expected uint32 as fifth argument");
508
509 dbus_message_iter_get_basic(&iter, &expires);
510 dbus_message_iter_next(&iter);
511
512 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
513 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
514 "Expected uint32 as sixth argument");
515
516 dbus_message_iter_get_basic(&iter, &ia_id);
517 dbus_message_iter_next(&iter);
518
519 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
520 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
521 "Expected uint32 as sixth argument");
522
523 dbus_message_iter_get_basic(&iter, &is_temporary);
524
525 if (inet_pton(AF_INET, ipaddr, &addr.addr4))
526 {
527 if (ia_id != 0 || is_temporary)
528 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
529 "ia_id and is_temporary must be zero for IPv4 lease");
530
531 if (!(lease = lease_find_by_addr(addr.addr4)))
532 lease = lease4_allocate(addr.addr4);
533 }
534#ifdef HAVE_DHCP6
535 else if (inet_pton(AF_INET6, ipaddr, &addr.addr6))
536 {
537 if (!(lease = lease6_find_by_addr(&addr.addr6, 128, 0)))
538 lease = lease6_allocate(&addr.addr6,
539 is_temporary ? LEASE_TA : LEASE_NA);
540 lease_set_iaid(lease, ia_id);
541 }
542#endif
543 else
544 return dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
545 "Invalid IP address '%s'", ipaddr);
546
547 hw_len = parse_hex((char*)hwaddr, dhcp_chaddr, DHCP_CHADDR_MAX, NULL, &hw_type);
548 if (hw_type == 0 && hw_len != 0)
549 hw_type = ARPHRD_ETHER;
550
551 lease_set_hwaddr(lease, dhcp_chaddr, clid, hw_len, hw_type,
552 clid_len, now, 0);
553 lease_set_expires(lease, expires, now);
554 if (hostname_len != 0)
555 lease_set_hostname(lease, hostname, 0, get_domain(lease->addr), NULL);
556
557 lease_update_file(now);
558 lease_update_dns(0);
559
560 return NULL;
561}
562
563static DBusMessage *dbus_del_lease(DBusMessage* message)
564{
565 struct dhcp_lease *lease;
566 DBusMessageIter iter;
567 const char *ipaddr;
568 DBusMessage *reply;
569 union all_addr addr;
570 dbus_bool_t ret = 1;
571 time_t now = dnsmasq_time();
572
573 if (!dbus_message_iter_init(message, &iter))
574 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
575 "Failed to initialize dbus message iter");
576
577 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
578 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
579 "Expected string as first argument");
580
581 dbus_message_iter_get_basic(&iter, &ipaddr);
582
583 if (inet_pton(AF_INET, ipaddr, &addr.addr4))
584 lease = lease_find_by_addr(addr.addr4);
585#ifdef HAVE_DHCP6
586 else if (inet_pton(AF_INET6, ipaddr, &addr.addr6))
587 lease = lease6_find_by_addr(&addr.addr6, 128, 0);
588#endif
589 else
590 return dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
591 "Invalid IP address '%s'", ipaddr);
592
593 if (lease)
594 {
595 lease_prune(lease, now);
596 lease_update_file(now);
597 lease_update_dns(0);
598 }
599 else
600 ret = 0;
601
602 if ((reply = dbus_message_new_method_return(message)))
603 dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &ret,
604 DBUS_TYPE_INVALID);
605
606
607 return reply;
608}
609#endif
610
611static DBusMessage *dbus_get_metrics(DBusMessage* message)
612{
613 DBusMessage *reply = dbus_message_new_method_return(message);
614 DBusMessageIter array, dict, iter;
615 int i;
616
617 dbus_message_iter_init_append(reply, &iter);
618 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{su}", &array);
619
620 for (i = 0; i < __METRIC_MAX; i++) {
621 const char *key = get_metric_name(i);
622 dbus_uint32_t value = daemon->metrics[i];
623
624 dbus_message_iter_open_container(&array, DBUS_TYPE_DICT_ENTRY, NULL, &dict);
625 dbus_message_iter_append_basic(&dict, DBUS_TYPE_STRING, &key);
626 dbus_message_iter_append_basic(&dict, DBUS_TYPE_UINT32, &value);
627 dbus_message_iter_close_container(&array, &dict);
628 }
629
630 dbus_message_iter_close_container(&iter, &array);
631
632 return reply;
633}
634
635DBusHandlerResult message_handler(DBusConnection *connection,
636 DBusMessage *message,
637 void *user_data)
638{
639 char *method = (char *)dbus_message_get_member(message);
640 DBusMessage *reply = NULL;
641 int clear_cache = 0, new_servers = 0;
642
643 if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
644 {
645 /* string length: "%s" provides space for termination zero */
646 if (!introspection_xml &&
647 (introspection_xml = whine_malloc(strlen(introspection_xml_template) + strlen(daemon->dbus_name))))
648 sprintf(introspection_xml, introspection_xml_template, daemon->dbus_name);
649
650 if (introspection_xml)
651 {
652 reply = dbus_message_new_method_return(message);
653 dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_xml, DBUS_TYPE_INVALID);
654 }
655 }
656 else if (strcmp(method, "GetVersion") == 0)
657 {
658 char *v = VERSION;
659 reply = dbus_message_new_method_return(message);
660
661 dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID);
662 }
663#ifdef HAVE_LOOP
664 else if (strcmp(method, "GetLoopServers") == 0)
665 {
666 reply = dbus_reply_server_loop(message);
667 }
668#endif
669 else if (strcmp(method, "SetServers") == 0)
670 {
671 dbus_read_servers(message);
672 new_servers = 1;
673 }
674 else if (strcmp(method, "SetServersEx") == 0)
675 {
676 reply = dbus_read_servers_ex(message, 0);
677 new_servers = 1;
678 }
679 else if (strcmp(method, "SetDomainServers") == 0)
680 {
681 reply = dbus_read_servers_ex(message, 1);
682 new_servers = 1;
683 }
684 else if (strcmp(method, "SetFilterWin2KOption") == 0)
685 {
686 reply = dbus_set_bool(message, OPT_FILTER, "filterwin2k");
687 }
688 else if (strcmp(method, "SetBogusPrivOption") == 0)
689 {
690 reply = dbus_set_bool(message, OPT_BOGUSPRIV, "bogus-priv");
691 }
692#ifdef HAVE_DHCP
693 else if (strcmp(method, "AddDhcpLease") == 0)
694 {
695 reply = dbus_add_lease(message);
696 }
697 else if (strcmp(method, "DeleteDhcpLease") == 0)
698 {
699 reply = dbus_del_lease(message);
700 }
701#endif
702 else if (strcmp(method, "GetMetrics") == 0)
703 {
704 reply = dbus_get_metrics(message);
705 }
706 else if (strcmp(method, "ClearCache") == 0)
707 clear_cache = 1;
708 else
709 return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
710
711 if (new_servers)
712 {
713 my_syslog(LOG_INFO, _("setting upstream servers from DBus"));
714 check_servers(0);
715 if (option_bool(OPT_RELOAD))
716 clear_cache = 1;
717 }
718
719 if (clear_cache)
720 clear_cache_and_reload(dnsmasq_time());
721
722 method = user_data; /* no warning */
723
724 /* If no reply or no error, return nothing */
725 if (!reply)
726 reply = dbus_message_new_method_return(message);
727
728 if (reply)
729 {
730 dbus_connection_send (connection, reply, NULL);
731 dbus_message_unref (reply);
732 }
733
734 return (DBUS_HANDLER_RESULT_HANDLED);
735}
736
737
738/* returns NULL or error message, may fail silently if dbus daemon not yet up. */
739char *dbus_init(void)
740{
741 DBusConnection *connection = NULL;
742 DBusObjectPathVTable dnsmasq_vtable = {NULL, &message_handler, NULL, NULL, NULL, NULL };
743 DBusError dbus_error;
744 DBusMessage *message;
745
746 dbus_error_init (&dbus_error);
747 if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error)))
748 return NULL;
749
750 dbus_connection_set_exit_on_disconnect(connection, FALSE);
751 dbus_connection_set_watch_functions(connection, add_watch, remove_watch,
752 NULL, NULL, NULL);
753 dbus_error_init (&dbus_error);
754 dbus_bus_request_name (connection, daemon->dbus_name, 0, &dbus_error);
755 if (dbus_error_is_set (&dbus_error))
756 return (char *)dbus_error.message;
757
758 if (!dbus_connection_register_object_path(connection, DNSMASQ_PATH,
759 &dnsmasq_vtable, NULL))
760 return _("could not register a DBus message handler");
761
762 daemon->dbus = connection;
763
764 if ((message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, "Up")))
765 {
766 dbus_connection_send(connection, message, NULL);
767 dbus_message_unref(message);
768 }
769
770 return NULL;
771}
772
773
774void set_dbus_listeners(void)
775{
776 struct watch *w;
777
778 for (w = daemon->watches; w; w = w->next)
779 if (dbus_watch_get_enabled(w->watch))
780 {
781 unsigned int flags = dbus_watch_get_flags(w->watch);
782 int fd = dbus_watch_get_unix_fd(w->watch);
783
784 if (flags & DBUS_WATCH_READABLE)
785 poll_listen(fd, POLLIN);
786
787 if (flags & DBUS_WATCH_WRITABLE)
788 poll_listen(fd, POLLOUT);
789
790 poll_listen(fd, POLLERR);
791 }
792}
793
794void check_dbus_listeners()
795{
796 DBusConnection *connection = (DBusConnection *)daemon->dbus;
797 struct watch *w;
798
799 for (w = daemon->watches; w; w = w->next)
800 if (dbus_watch_get_enabled(w->watch))
801 {
802 unsigned int flags = 0;
803 int fd = dbus_watch_get_unix_fd(w->watch);
804
805 if (poll_check(fd, POLLIN))
806 flags |= DBUS_WATCH_READABLE;
807
808 if (poll_check(fd, POLLOUT))
809 flags |= DBUS_WATCH_WRITABLE;
810
811 if (poll_check(fd, POLLERR))
812 flags |= DBUS_WATCH_ERROR;
813
814 if (flags != 0)
815 dbus_watch_handle(w->watch, flags);
816 }
817
818 if (connection)
819 {
820 dbus_connection_ref (connection);
821 while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS);
822 dbus_connection_unref (connection);
823 }
824}
825
826#ifdef HAVE_DHCP
827void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname)
828{
829 DBusConnection *connection = (DBusConnection *)daemon->dbus;
830 DBusMessage* message = NULL;
831 DBusMessageIter args;
832 char *action_str, *mac = daemon->namebuff;
833 unsigned char *p;
834 int i;
835
836 if (!connection)
837 return;
838
839 if (!hostname)
840 hostname = "";
841
842#ifdef HAVE_DHCP6
843 if (lease->flags & (LEASE_TA | LEASE_NA))
844 {
845 print_mac(mac, lease->clid, lease->clid_len);
846 inet_ntop(AF_INET6, &lease->addr6, daemon->addrbuff, ADDRSTRLEN);
847 }
848 else
849#endif
850 {
851 p = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len,
852 lease->hwaddr, lease->clid_len, lease->clid, &i);
853 print_mac(mac, p, i);
854 inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN);
855 }
856
857 if (action == ACTION_DEL)
858 action_str = "DhcpLeaseDeleted";
859 else if (action == ACTION_ADD)
860 action_str = "DhcpLeaseAdded";
861 else if (action == ACTION_OLD)
862 action_str = "DhcpLeaseUpdated";
863 else
864 return;
865
866 if (!(message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, action_str)))
867 return;
868
869 dbus_message_iter_init_append(message, &args);
870
871 if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) &&
872 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) &&
873 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname))
874 dbus_connection_send(connection, message, NULL);
875
876 dbus_message_unref(message);
877}
878#endif
879
880#endif