blob: da28a280bfa4bc0d5fd7b91b6f200f7dbccb11dc [file] [log] [blame]
Simon Kelley61744352013-01-31 14:34:40 +00001/* dnsmasq is Copyright (c) 2000-2013 Simon Kelley
Simon Kelley3d8df262005-08-29 12:19:27 +01002
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
Simon Kelley824af852008-02-12 20:43:05 +00005 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
Simon Kelley3d8df262005-08-29 12:19:27 +01008 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.
Simon Kelley824af852008-02-12 20:43:05 +000012
Simon Kelley73a08a22009-02-05 20:28:08 +000013 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/>.
Simon Kelley3d8df262005-08-29 12:19:27 +010015*/
16
17#include "dnsmasq.h"
18
19#ifdef HAVE_DBUS
20
Simon Kelley3d8df262005-08-29 12:19:27 +010021#include <dbus/dbus.h>
22
Simon Kelleyad094272012-08-10 17:10:54 +010023const char* introspection_xml_template =
Simon Kelley73a08a22009-02-05 20:28:08 +000024"<!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"
Simon Kelleyad094272012-08-10 17:10:54 +010032" <interface name=\"%s\">\n"
Simon Kelley73a08a22009-02-05 20:28:08 +000033" <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" <method name=\"SetServers\">\n"
39" <arg name=\"servers\" direction=\"in\" type=\"av\"/>\n"
40" </method>\n"
Simon Kelley295a54e2012-12-01 21:02:15 +000041" <method name=\"SetDomainServers\">\n"
42" <arg name=\"servers\" direction=\"in\" type=\"as\"/>\n"
43" </method>\n"
Simon Kelleyfaafb3f2012-09-20 14:17:39 +010044" <method name=\"SetServersEx\">\n"
45" <arg name=\"servers\" direction=\"in\" type=\"aas\"/>\n"
46" </method>\n"
Simon Kelley73a08a22009-02-05 20:28:08 +000047" <signal name=\"DhcpLeaseAdded\">\n"
48" <arg name=\"ipaddr\" type=\"s\"/>\n"
49" <arg name=\"hwaddr\" type=\"s\"/>\n"
50" <arg name=\"hostname\" type=\"s\"/>\n"
51" </signal>\n"
52" <signal name=\"DhcpLeaseDeleted\">\n"
53" <arg name=\"ipaddr\" type=\"s\"/>\n"
54" <arg name=\"hwaddr\" type=\"s\"/>\n"
55" <arg name=\"hostname\" type=\"s\"/>\n"
56" </signal>\n"
57" <signal name=\"DhcpLeaseUpdated\">\n"
58" <arg name=\"ipaddr\" type=\"s\"/>\n"
59" <arg name=\"hwaddr\" type=\"s\"/>\n"
60" <arg name=\"hostname\" type=\"s\"/>\n"
61" </signal>\n"
62" </interface>\n"
63"</node>\n";
64
Simon Kelleyad094272012-08-10 17:10:54 +010065static char *introspection_xml = NULL;
66
Simon Kelley3d8df262005-08-29 12:19:27 +010067struct watch {
68 DBusWatch *watch;
69 struct watch *next;
70};
71
72
73static dbus_bool_t add_watch(DBusWatch *watch, void *data)
74{
Simon Kelley3d8df262005-08-29 12:19:27 +010075 struct watch *w;
76
77 for (w = daemon->watches; w; w = w->next)
78 if (w->watch == watch)
79 return TRUE;
80
Simon Kelley5aabfc72007-08-29 11:24:47 +010081 if (!(w = whine_malloc(sizeof(struct watch))))
Simon Kelley3d8df262005-08-29 12:19:27 +010082 return FALSE;
83
84 w->watch = watch;
85 w->next = daemon->watches;
86 daemon->watches = w;
87
Simon Kelley5aabfc72007-08-29 11:24:47 +010088 w = data; /* no warning */
Simon Kelley3d8df262005-08-29 12:19:27 +010089 return TRUE;
90}
91
92static void remove_watch(DBusWatch *watch, void *data)
93{
Simon Kelley25c41982013-02-05 14:56:02 +000094 struct watch **up, *w, *tmp;
Simon Kelley3d8df262005-08-29 12:19:27 +010095
Simon Kelley25c41982013-02-05 14:56:02 +000096 for (up = &(daemon->watches), w = daemon->watches; w; w = tmp)
97 {
98 tmp = w->next;
99 if (w->watch == watch)
100 {
101 *up = tmp;
102 free(w);
103 }
104 else
105 up = &(w->next);
106 }
Simon Kelley5aabfc72007-08-29 11:24:47 +0100107
108 w = data; /* no warning */
Simon Kelley3d8df262005-08-29 12:19:27 +0100109}
110
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100111static void add_update_server(union mysockaddr *addr,
112 union mysockaddr *source_addr,
113 const char *interface,
114 const char *domain)
115{
116 struct server *serv;
117
118 /* See if there is a suitable candidate, and unmark */
119 for (serv = daemon->servers; serv; serv = serv->next)
120 if ((serv->flags & SERV_FROM_DBUS) &&
121 (serv->flags & SERV_MARK))
122 {
123 if (domain)
124 {
125 if (!(serv->flags & SERV_HAS_DOMAIN) || !hostname_isequal(domain, serv->domain))
126 continue;
127 }
128 else
129 {
130 if (serv->flags & SERV_HAS_DOMAIN)
131 continue;
132 }
133
134 serv->flags &= ~SERV_MARK;
135
136 break;
137 }
138
139 if (!serv && (serv = whine_malloc(sizeof (struct server))))
140 {
141 /* Not found, create a new one. */
142 memset(serv, 0, sizeof(struct server));
143
144 if (domain && !(serv->domain = whine_malloc(strlen(domain)+1)))
145 {
146 free(serv);
147 serv = NULL;
148 }
149 else
150 {
151 serv->next = daemon->servers;
152 daemon->servers = serv;
153 serv->flags = SERV_FROM_DBUS;
154 if (domain)
155 {
156 strcpy(serv->domain, domain);
157 serv->flags |= SERV_HAS_DOMAIN;
158 }
159 }
160 }
161
162 if (serv)
163 {
164 if (interface)
165 strcpy(serv->interface, interface);
166 else
167 serv->interface[0] = 0;
168
169 if (source_addr->in.sin_family == AF_INET &&
170 addr->in.sin_addr.s_addr == 0 &&
171 serv->domain)
172 serv->flags |= SERV_NO_ADDR;
173 else
174 {
175 serv->flags &= ~SERV_NO_ADDR;
176 serv->addr = *addr;
177 serv->source_addr = *source_addr;
178 }
179 }
180}
181
182static void mark_dbus(void)
183{
184 struct server *serv;
185
186 /* mark everything from DBUS */
187 for (serv = daemon->servers; serv; serv = serv->next)
188 if (serv->flags & SERV_FROM_DBUS)
189 serv->flags |= SERV_MARK;
190}
191
192static void cleanup_dbus()
Simon Kelley3d8df262005-08-29 12:19:27 +0100193{
194 struct server *serv, *tmp, **up;
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100195
196 /* unlink and free anything still marked. */
197 for (serv = daemon->servers, up = &daemon->servers; serv; serv = tmp)
198 {
199 tmp = serv->next;
200 if (serv->flags & SERV_MARK)
201 {
202 server_gone(serv);
203 *up = serv->next;
204 if (serv->domain)
205 free(serv->domain);
206 free(serv);
207 }
208 else
209 up = &serv->next;
210 }
211}
212
213static void dbus_read_servers(DBusMessage *message)
214{
Simon Kelley3d8df262005-08-29 12:19:27 +0100215 DBusMessageIter iter;
216 union mysockaddr addr, source_addr;
217 char *domain;
218
219 dbus_message_iter_init(message, &iter);
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100220
221 mark_dbus();
222
Simon Kelley3d8df262005-08-29 12:19:27 +0100223 while (1)
224 {
225 int skip = 0;
226
227 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32)
228 {
229 u32 a;
230
231 dbus_message_iter_get_basic(&iter, &a);
232 dbus_message_iter_next (&iter);
233
234#ifdef HAVE_SOCKADDR_SA_LEN
235 source_addr.in.sin_len = addr.in.sin_len = sizeof(struct sockaddr_in);
236#endif
237 addr.in.sin_addr.s_addr = ntohl(a);
238 source_addr.in.sin_family = addr.in.sin_family = AF_INET;
239 addr.in.sin_port = htons(NAMESERVER_PORT);
240 source_addr.in.sin_addr.s_addr = INADDR_ANY;
241 source_addr.in.sin_port = htons(daemon->query_port);
242 }
243 else if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_BYTE)
244 {
245 unsigned char p[sizeof(struct in6_addr)];
246 unsigned int i;
247
248 skip = 1;
249
250 for(i = 0; i < sizeof(struct in6_addr); i++)
251 {
252 dbus_message_iter_get_basic(&iter, &p[i]);
253 dbus_message_iter_next (&iter);
254 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BYTE)
255 break;
256 }
257
258#ifndef HAVE_IPV6
Simon Kelleyf2621c72007-04-29 19:47:21 +0100259 my_syslog(LOG_WARNING, _("attempt to set an IPv6 server address via DBus - no IPv6 support"));
Simon Kelley3d8df262005-08-29 12:19:27 +0100260#else
261 if (i == sizeof(struct in6_addr)-1)
262 {
Simon Kelley824af852008-02-12 20:43:05 +0000263 memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr));
Simon Kelley3d8df262005-08-29 12:19:27 +0100264#ifdef HAVE_SOCKADDR_SA_LEN
Simon Kelley9e038942008-05-30 20:06:34 +0100265 source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(struct sockaddr_in6);
Simon Kelley3d8df262005-08-29 12:19:27 +0100266#endif
267 source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6;
268 addr.in6.sin6_port = htons(NAMESERVER_PORT);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100269 source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0;
270 source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0;
Simon Kelley3d8df262005-08-29 12:19:27 +0100271 source_addr.in6.sin6_addr = in6addr_any;
272 source_addr.in6.sin6_port = htons(daemon->query_port);
273 skip = 0;
274 }
275#endif
276 }
277 else
278 /* At the end */
279 break;
280
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100281 /* process each domain */
Simon Kelley3d8df262005-08-29 12:19:27 +0100282 do {
283 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
284 {
285 dbus_message_iter_get_basic(&iter, &domain);
286 dbus_message_iter_next (&iter);
287 }
288 else
289 domain = NULL;
290
291 if (!skip)
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100292 add_update_server(&addr, &source_addr, NULL, domain);
293
294 } while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING);
Simon Kelley3d8df262005-08-29 12:19:27 +0100295 }
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100296
Simon Kelley3d8df262005-08-29 12:19:27 +0100297 /* unlink and free anything still marked. */
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100298 cleanup_dbus();
299}
300
Simon Kelley295a54e2012-12-01 21:02:15 +0000301static DBusMessage* dbus_read_servers_ex(DBusMessage *message, int strings)
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100302{
303 DBusMessageIter iter, array_iter, string_iter;
304 DBusMessage *error = NULL;
305 const char *addr_err;
Simon Kelley295a54e2012-12-01 21:02:15 +0000306 char *dup = NULL;
307
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100308 if (!dbus_message_iter_init(message, &iter))
Simon Kelley3d8df262005-08-29 12:19:27 +0100309 {
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100310 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
311 "Failed to initialize dbus message iter");
Simon Kelley3d8df262005-08-29 12:19:27 +0100312 }
313
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100314 /* check that the message contains an array of arrays */
315 if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) ||
Simon Kelley295a54e2012-12-01 21:02:15 +0000316 (dbus_message_iter_get_element_type(&iter) != (strings ? DBUS_TYPE_STRING : DBUS_TYPE_ARRAY)))
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100317 {
318 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
Simon Kelley295a54e2012-12-01 21:02:15 +0000319 strings ? "Expected array of string" : "Expected array of string arrays");
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100320 }
321
322 mark_dbus();
323
324 /* array_iter points to each "as" element in the outer array */
325 dbus_message_iter_recurse(&iter, &array_iter);
326 while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID)
327 {
328 const char *str = NULL;
329 union mysockaddr addr, source_addr;
330 char interface[IF_NAMESIZE];
Simon Kelleyd89fb4e2012-12-01 21:21:13 +0000331 char *str_addr, *str_domain = NULL;
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100332
Simon Kelley295a54e2012-12-01 21:02:15 +0000333 if (strings)
334 {
335 dbus_message_iter_get_basic(&array_iter, &str);
336 if (!str || !strlen (str))
337 {
338 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
339 "Empty string");
340 break;
341 }
342
343 /* dup the string because it gets modified during parsing */
Simon Kelley9c4270b2013-02-04 22:07:57 +0000344 if (dup)
345 free(dup);
Simon Kelley295a54e2012-12-01 21:02:15 +0000346 if (!(dup = str_domain = whine_malloc(strlen(str)+1)))
347 break;
Simon Kelley9c4270b2013-02-04 22:07:57 +0000348
Simon Kelley295a54e2012-12-01 21:02:15 +0000349 strcpy(str_domain, str);
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100350
Simon Kelley295a54e2012-12-01 21:02:15 +0000351 /* point to address part of old string for error message */
352 if ((str_addr = strrchr(str, '/')))
353 str = str_addr+1;
354
355 if ((str_addr = strrchr(str_domain, '/')))
356 {
357 if (*str_domain != '/' || str_addr == str_domain)
358 {
359 error = dbus_message_new_error_printf(message,
360 DBUS_ERROR_INVALID_ARGS,
361 "No domain terminator '%s'",
362 str);
363 break;
364 }
365 *str_addr++ = 0;
366 str_domain++;
367 }
368 else
369 {
370 str_addr = str_domain;
371 str_domain = NULL;
372 }
373
374
375 }
376 else
377 {
378 /* check the types of the struct and its elements */
379 if ((dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY) ||
380 (dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_STRING))
381 {
382 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
383 "Expected inner array of strings");
384 break;
385 }
386
387 /* string_iter points to each "s" element in the inner array */
388 dbus_message_iter_recurse(&array_iter, &string_iter);
389 if (dbus_message_iter_get_arg_type(&string_iter) != DBUS_TYPE_STRING)
390 {
391 /* no IP address given */
392 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
393 "Expected IP address");
394 break;
395 }
396
397 dbus_message_iter_get_basic(&string_iter, &str);
398 if (!str || !strlen (str))
399 {
400 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
401 "Empty IP address");
402 break;
403 }
404
405 /* dup the string because it gets modified during parsing */
Simon Kelley9c4270b2013-02-04 22:07:57 +0000406 if (dup)
407 free(dup);
Simon Kelley295a54e2012-12-01 21:02:15 +0000408 if (!(dup = str_addr = whine_malloc(strlen(str)+1)))
409 break;
Simon Kelley9c4270b2013-02-04 22:07:57 +0000410
Simon Kelley295a54e2012-12-01 21:02:15 +0000411 strcpy(str_addr, str);
412 }
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100413
414 memset(&addr, 0, sizeof(addr));
415 memset(&source_addr, 0, sizeof(source_addr));
416 memset(&interface, 0, sizeof(interface));
417
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100418 /* parse the IP address */
Simon Kelleyc99df932012-10-12 13:39:04 +0100419 addr_err = parse_server(str_addr, &addr, &source_addr, (char *) &interface, NULL);
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100420
421 if (addr_err)
422 {
423 error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
424 "Invalid IP address '%s': %s",
425 str, addr_err);
426 break;
427 }
428
Simon Kelley295a54e2012-12-01 21:02:15 +0000429 if (strings)
430 {
431 char *p;
432
433 do {
434 if (str_domain)
435 {
436 if ((p = strchr(str_domain, '/')))
437 *p++ = 0;
438 }
439 else
440 p = NULL;
441
442 add_update_server(&addr, &source_addr, interface, str_domain);
443 } while ((str_domain = p));
444 }
445 else
446 {
447 /* jump past the address to the domain list (if any) */
448 dbus_message_iter_next (&string_iter);
449
450 /* parse domains and add each server/domain pair to the list */
451 do {
452 str = NULL;
453 if (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING)
454 dbus_message_iter_get_basic(&string_iter, &str);
455 dbus_message_iter_next (&string_iter);
456
457 add_update_server(&addr, &source_addr, interface, str);
458 } while (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING);
459 }
460
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100461 /* jump to next element in outer array */
462 dbus_message_iter_next(&array_iter);
463 }
464
465 cleanup_dbus();
466
Simon Kelley295a54e2012-12-01 21:02:15 +0000467 if (dup)
468 free(dup);
469
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100470 return error;
Simon Kelley3d8df262005-08-29 12:19:27 +0100471}
472
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100473DBusHandlerResult message_handler(DBusConnection *connection,
474 DBusMessage *message,
475 void *user_data)
Simon Kelley3d8df262005-08-29 12:19:27 +0100476{
477 char *method = (char *)dbus_message_get_member(message);
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100478 DBusMessage *reply = NULL;
Simon Kelleyd9fb0be2013-07-25 21:47:17 +0100479 int clear_cache = 0, new_servers = 0;
Simon Kelley295a54e2012-12-01 21:02:15 +0000480
Simon Kelley73a08a22009-02-05 20:28:08 +0000481 if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
482 {
Simon Kelleyad094272012-08-10 17:10:54 +0100483 /* string length: "%s" provides space for termination zero */
484 if (!introspection_xml &&
485 (introspection_xml = whine_malloc(strlen(introspection_xml_template) + strlen(daemon->dbus_name))))
486 sprintf(introspection_xml, introspection_xml_template, daemon->dbus_name);
487
488 if (introspection_xml)
489 {
490 reply = dbus_message_new_method_return(message);
Simon Kelleyad094272012-08-10 17:10:54 +0100491 dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_xml, DBUS_TYPE_INVALID);
Simon Kelleyad094272012-08-10 17:10:54 +0100492 }
Simon Kelley73a08a22009-02-05 20:28:08 +0000493 }
494 else if (strcmp(method, "GetVersion") == 0)
Simon Kelley3d8df262005-08-29 12:19:27 +0100495 {
496 char *v = VERSION;
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100497 reply = dbus_message_new_method_return(message);
Simon Kelley3d8df262005-08-29 12:19:27 +0100498
499 dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID);
Simon Kelley3d8df262005-08-29 12:19:27 +0100500 }
501 else if (strcmp(method, "SetServers") == 0)
502 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100503 dbus_read_servers(message);
Simon Kelleyd9fb0be2013-07-25 21:47:17 +0100504 new_servers = 1;
Simon Kelley3d8df262005-08-29 12:19:27 +0100505 }
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100506 else if (strcmp(method, "SetServersEx") == 0)
507 {
Simon Kelley295a54e2012-12-01 21:02:15 +0000508 reply = dbus_read_servers_ex(message, 0);
Simon Kelleyd9fb0be2013-07-25 21:47:17 +0100509 new_servers = 1;
Simon Kelley295a54e2012-12-01 21:02:15 +0000510 }
511 else if (strcmp(method, "SetDomainServers") == 0)
512 {
513 reply = dbus_read_servers_ex(message, 1);
Simon Kelleyd9fb0be2013-07-25 21:47:17 +0100514 new_servers = 1;
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100515 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100516 else if (strcmp(method, "ClearCache") == 0)
Simon Kelleyd9fb0be2013-07-25 21:47:17 +0100517 clear_cache = 1;
Simon Kelley3d8df262005-08-29 12:19:27 +0100518 else
519 return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
Simon Kelleyd9fb0be2013-07-25 21:47:17 +0100520
521 if (new_servers)
522 {
523 my_syslog(LOG_INFO, _("setting upstream servers from DBus"));
524 check_servers();
525 if (option_bool(OPT_RELOAD))
526 clear_cache = 1;
527 }
528
529 if (clear_cache)
530 clear_cache_and_reload(dnsmasq_time());
Simon Kelley3d8df262005-08-29 12:19:27 +0100531
Simon Kelley5aabfc72007-08-29 11:24:47 +0100532 method = user_data; /* no warning */
533
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100534 /* If no reply or no error, return nothing */
535 if (!reply)
536 reply = dbus_message_new_method_return(message);
537
538 if (reply)
539 {
540 dbus_connection_send (connection, reply, NULL);
541 dbus_message_unref (reply);
542 }
543
Simon Kelley3d8df262005-08-29 12:19:27 +0100544 return (DBUS_HANDLER_RESULT_HANDLED);
Simon Kelley3d8df262005-08-29 12:19:27 +0100545}
546
547
548/* returns NULL or error message, may fail silently if dbus daemon not yet up. */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100549char *dbus_init(void)
Simon Kelley3d8df262005-08-29 12:19:27 +0100550{
551 DBusConnection *connection = NULL;
552 DBusObjectPathVTable dnsmasq_vtable = {NULL, &message_handler, NULL, NULL, NULL, NULL };
553 DBusError dbus_error;
554 DBusMessage *message;
555
556 dbus_error_init (&dbus_error);
557 if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error)))
558 return NULL;
Simon Kelley7cebd202006-05-06 14:13:33 +0100559
Simon Kelley3d8df262005-08-29 12:19:27 +0100560 dbus_connection_set_exit_on_disconnect(connection, FALSE);
561 dbus_connection_set_watch_functions(connection, add_watch, remove_watch,
Simon Kelley5aabfc72007-08-29 11:24:47 +0100562 NULL, NULL, NULL);
Simon Kelley3d8df262005-08-29 12:19:27 +0100563 dbus_error_init (&dbus_error);
Simon Kelleyad094272012-08-10 17:10:54 +0100564 dbus_bus_request_name (connection, daemon->dbus_name, 0, &dbus_error);
Simon Kelley3d8df262005-08-29 12:19:27 +0100565 if (dbus_error_is_set (&dbus_error))
566 return (char *)dbus_error.message;
567
568 if (!dbus_connection_register_object_path(connection, DNSMASQ_PATH,
Simon Kelley5aabfc72007-08-29 11:24:47 +0100569 &dnsmasq_vtable, NULL))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000570 return _("could not register a DBus message handler");
Simon Kelley3d8df262005-08-29 12:19:27 +0100571
572 daemon->dbus = connection;
573
Simon Kelleyad094272012-08-10 17:10:54 +0100574 if ((message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, "Up")))
Simon Kelley9009d742008-11-14 20:04:27 +0000575 {
576 dbus_connection_send(connection, message, NULL);
577 dbus_message_unref(message);
578 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100579
580 return NULL;
581}
582
583
Simon Kelley5aabfc72007-08-29 11:24:47 +0100584void set_dbus_listeners(int *maxfdp,
Simon Kelley16972692006-10-16 20:04:18 +0100585 fd_set *rset, fd_set *wset, fd_set *eset)
Simon Kelley3d8df262005-08-29 12:19:27 +0100586{
587 struct watch *w;
588
589 for (w = daemon->watches; w; w = w->next)
590 if (dbus_watch_get_enabled(w->watch))
591 {
592 unsigned int flags = dbus_watch_get_flags(w->watch);
Simon Kelley824af852008-02-12 20:43:05 +0000593 int fd = dbus_watch_get_unix_fd(w->watch);
Simon Kelley3d8df262005-08-29 12:19:27 +0100594
Simon Kelley16972692006-10-16 20:04:18 +0100595 bump_maxfd(fd, maxfdp);
Simon Kelley3d8df262005-08-29 12:19:27 +0100596
597 if (flags & DBUS_WATCH_READABLE)
598 FD_SET(fd, rset);
599
600 if (flags & DBUS_WATCH_WRITABLE)
601 FD_SET(fd, wset);
602
603 FD_SET(fd, eset);
604 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100605}
606
Simon Kelley5aabfc72007-08-29 11:24:47 +0100607void check_dbus_listeners(fd_set *rset, fd_set *wset, fd_set *eset)
Simon Kelley3d8df262005-08-29 12:19:27 +0100608{
609 DBusConnection *connection = (DBusConnection *)daemon->dbus;
610 struct watch *w;
611
612 for (w = daemon->watches; w; w = w->next)
613 if (dbus_watch_get_enabled(w->watch))
614 {
615 unsigned int flags = 0;
Simon Kelley824af852008-02-12 20:43:05 +0000616 int fd = dbus_watch_get_unix_fd(w->watch);
Simon Kelley3d8df262005-08-29 12:19:27 +0100617
618 if (FD_ISSET(fd, rset))
619 flags |= DBUS_WATCH_READABLE;
620
621 if (FD_ISSET(fd, wset))
622 flags |= DBUS_WATCH_WRITABLE;
623
624 if (FD_ISSET(fd, eset))
625 flags |= DBUS_WATCH_ERROR;
626
627 if (flags != 0)
628 dbus_watch_handle(w->watch, flags);
629 }
630
631 if (connection)
632 {
633 dbus_connection_ref (connection);
634 while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS);
635 dbus_connection_unref (connection);
636 }
637}
638
Simon Kelley316e2732010-01-22 20:16:09 +0000639#ifdef HAVE_DHCP
Simon Kelley1f15b812009-10-13 17:49:32 +0100640void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname)
Simon Kelley9009d742008-11-14 20:04:27 +0000641{
642 DBusConnection *connection = (DBusConnection *)daemon->dbus;
643 DBusMessage* message = NULL;
644 DBusMessageIter args;
Simon Kelley6ffeff82012-03-07 10:32:35 +0000645 char *action_str, *mac = daemon->namebuff;
Simon Kelley1f15b812009-10-13 17:49:32 +0100646 unsigned char *p;
647 int i;
Simon Kelley9009d742008-11-14 20:04:27 +0000648
649 if (!connection)
650 return;
Simon Kelley1f15b812009-10-13 17:49:32 +0100651
652 if (!hostname)
653 hostname = "";
654
Simon Kelley6ffeff82012-03-07 10:32:35 +0000655#ifdef HAVE_DHCP6
656 if (lease->flags & (LEASE_TA | LEASE_NA))
657 {
658 print_mac(mac, lease->clid, lease->clid_len);
659 inet_ntop(AF_INET6, lease->hwaddr, daemon->addrbuff, ADDRSTRLEN);
660 }
661 else
662#endif
663 {
664 p = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len,
665 lease->hwaddr, lease->clid_len, lease->clid, &i);
666 print_mac(mac, p, i);
667 inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN);
668 }
669
Simon Kelley9009d742008-11-14 20:04:27 +0000670 if (action == ACTION_DEL)
671 action_str = "DhcpLeaseDeleted";
672 else if (action == ACTION_ADD)
673 action_str = "DhcpLeaseAdded";
674 else if (action == ACTION_OLD)
675 action_str = "DhcpLeaseUpdated";
676 else
677 return;
678
Simon Kelleyad094272012-08-10 17:10:54 +0100679 if (!(message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, action_str)))
Simon Kelley9009d742008-11-14 20:04:27 +0000680 return;
681
682 dbus_message_iter_init_append(message, &args);
Simon Kelley6ffeff82012-03-07 10:32:35 +0000683
684 if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) &&
Simon Kelley9009d742008-11-14 20:04:27 +0000685 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) &&
686 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname))
687 dbus_connection_send(connection, message, NULL);
688
689 dbus_message_unref(message);
690}
Simon Kelley316e2732010-01-22 20:16:09 +0000691#endif
Simon Kelley9009d742008-11-14 20:04:27 +0000692
Simon Kelley3d8df262005-08-29 12:19:27 +0100693#endif