blob: 16a05e8ba6587fafab4cbfdd8cc7b9b70f2dd576 [file] [log] [blame]
Simon Kelley59546082012-01-06 20:02:04 +00001/* dnsmasq is Copyright (c) 2000-2012 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"
41" <signal name=\"DhcpLeaseAdded\">\n"
42" <arg name=\"ipaddr\" type=\"s\"/>\n"
43" <arg name=\"hwaddr\" type=\"s\"/>\n"
44" <arg name=\"hostname\" type=\"s\"/>\n"
45" </signal>\n"
46" <signal name=\"DhcpLeaseDeleted\">\n"
47" <arg name=\"ipaddr\" type=\"s\"/>\n"
48" <arg name=\"hwaddr\" type=\"s\"/>\n"
49" <arg name=\"hostname\" type=\"s\"/>\n"
50" </signal>\n"
51" <signal name=\"DhcpLeaseUpdated\">\n"
52" <arg name=\"ipaddr\" type=\"s\"/>\n"
53" <arg name=\"hwaddr\" type=\"s\"/>\n"
54" <arg name=\"hostname\" type=\"s\"/>\n"
55" </signal>\n"
56" </interface>\n"
57"</node>\n";
58
Simon Kelleyad094272012-08-10 17:10:54 +010059static char *introspection_xml = NULL;
60
Simon Kelley3d8df262005-08-29 12:19:27 +010061struct watch {
62 DBusWatch *watch;
63 struct watch *next;
64};
65
66
67static dbus_bool_t add_watch(DBusWatch *watch, void *data)
68{
Simon Kelley3d8df262005-08-29 12:19:27 +010069 struct watch *w;
70
71 for (w = daemon->watches; w; w = w->next)
72 if (w->watch == watch)
73 return TRUE;
74
Simon Kelley5aabfc72007-08-29 11:24:47 +010075 if (!(w = whine_malloc(sizeof(struct watch))))
Simon Kelley3d8df262005-08-29 12:19:27 +010076 return FALSE;
77
78 w->watch = watch;
79 w->next = daemon->watches;
80 daemon->watches = w;
81
Simon Kelley5aabfc72007-08-29 11:24:47 +010082 w = data; /* no warning */
Simon Kelley3d8df262005-08-29 12:19:27 +010083 return TRUE;
84}
85
86static void remove_watch(DBusWatch *watch, void *data)
87{
Simon Kelley3d8df262005-08-29 12:19:27 +010088 struct watch **up, *w;
89
90 for (up = &(daemon->watches), w = daemon->watches; w; w = w->next)
91 if (w->watch == watch)
92 {
93 *up = w->next;
94 free(w);
95 }
96 else
97 up = &(w->next);
Simon Kelley5aabfc72007-08-29 11:24:47 +010098
99 w = data; /* no warning */
Simon Kelley3d8df262005-08-29 12:19:27 +0100100}
101
Simon Kelley5aabfc72007-08-29 11:24:47 +0100102static void dbus_read_servers(DBusMessage *message)
Simon Kelley3d8df262005-08-29 12:19:27 +0100103{
104 struct server *serv, *tmp, **up;
105 DBusMessageIter iter;
106 union mysockaddr addr, source_addr;
107 char *domain;
108
109 dbus_message_iter_init(message, &iter);
110
111 /* mark everything from DBUS */
112 for (serv = daemon->servers; serv; serv = serv->next)
113 if (serv->flags & SERV_FROM_DBUS)
114 serv->flags |= SERV_MARK;
115
116 while (1)
117 {
118 int skip = 0;
119
120 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32)
121 {
122 u32 a;
123
124 dbus_message_iter_get_basic(&iter, &a);
125 dbus_message_iter_next (&iter);
126
127#ifdef HAVE_SOCKADDR_SA_LEN
128 source_addr.in.sin_len = addr.in.sin_len = sizeof(struct sockaddr_in);
129#endif
130 addr.in.sin_addr.s_addr = ntohl(a);
131 source_addr.in.sin_family = addr.in.sin_family = AF_INET;
132 addr.in.sin_port = htons(NAMESERVER_PORT);
133 source_addr.in.sin_addr.s_addr = INADDR_ANY;
134 source_addr.in.sin_port = htons(daemon->query_port);
135 }
136 else if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_BYTE)
137 {
138 unsigned char p[sizeof(struct in6_addr)];
139 unsigned int i;
140
141 skip = 1;
142
143 for(i = 0; i < sizeof(struct in6_addr); i++)
144 {
145 dbus_message_iter_get_basic(&iter, &p[i]);
146 dbus_message_iter_next (&iter);
147 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BYTE)
148 break;
149 }
150
151#ifndef HAVE_IPV6
Simon Kelleyf2621c72007-04-29 19:47:21 +0100152 my_syslog(LOG_WARNING, _("attempt to set an IPv6 server address via DBus - no IPv6 support"));
Simon Kelley3d8df262005-08-29 12:19:27 +0100153#else
154 if (i == sizeof(struct in6_addr)-1)
155 {
Simon Kelley824af852008-02-12 20:43:05 +0000156 memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr));
Simon Kelley3d8df262005-08-29 12:19:27 +0100157#ifdef HAVE_SOCKADDR_SA_LEN
Simon Kelley9e038942008-05-30 20:06:34 +0100158 source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(struct sockaddr_in6);
Simon Kelley3d8df262005-08-29 12:19:27 +0100159#endif
160 source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6;
161 addr.in6.sin6_port = htons(NAMESERVER_PORT);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100162 source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0;
163 source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0;
Simon Kelley3d8df262005-08-29 12:19:27 +0100164 source_addr.in6.sin6_addr = in6addr_any;
165 source_addr.in6.sin6_port = htons(daemon->query_port);
166 skip = 0;
167 }
168#endif
169 }
170 else
171 /* At the end */
172 break;
173
174 do {
175 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
176 {
177 dbus_message_iter_get_basic(&iter, &domain);
178 dbus_message_iter_next (&iter);
179 }
180 else
181 domain = NULL;
182
183 if (!skip)
184 {
185 /* See if this is already there, and unmark */
186 for (serv = daemon->servers; serv; serv = serv->next)
187 if ((serv->flags & SERV_FROM_DBUS) &&
188 (serv->flags & SERV_MARK))
189 {
190 if (!(serv->flags & SERV_HAS_DOMAIN) && !domain)
191 {
192 serv->flags &= ~SERV_MARK;
193 break;
194 }
195 if ((serv->flags & SERV_HAS_DOMAIN) &&
196 domain &&
197 hostname_isequal(domain, serv->domain))
198 {
199 serv->flags &= ~SERV_MARK;
200 break;
201 }
202 }
203
Simon Kelley5aabfc72007-08-29 11:24:47 +0100204 if (!serv && (serv = whine_malloc(sizeof (struct server))))
Simon Kelley3d8df262005-08-29 12:19:27 +0100205 {
206 /* Not found, create a new one. */
Simon Kelley9e038942008-05-30 20:06:34 +0100207 memset(serv, 0, sizeof(struct server));
208
Simon Kelley3d8df262005-08-29 12:19:27 +0100209 if (domain)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100210 serv->domain = whine_malloc(strlen(domain)+1);
Simon Kelley9e038942008-05-30 20:06:34 +0100211
Simon Kelley3d8df262005-08-29 12:19:27 +0100212 if (domain && !serv->domain)
213 {
214 free(serv);
215 serv = NULL;
216 }
217 else
218 {
219 serv->next = daemon->servers;
220 daemon->servers = serv;
221 serv->flags = SERV_FROM_DBUS;
Simon Kelley3d8df262005-08-29 12:19:27 +0100222 if (domain)
223 {
224 strcpy(serv->domain, domain);
225 serv->flags |= SERV_HAS_DOMAIN;
226 }
227 }
228 }
229
230 if (serv)
231 {
232 if (source_addr.in.sin_family == AF_INET &&
233 addr.in.sin_addr.s_addr == 0 &&
234 serv->domain)
235 serv->flags |= SERV_NO_ADDR;
236 else
237 {
238 serv->flags &= ~SERV_NO_ADDR;
239 serv->addr = addr;
240 serv->source_addr = source_addr;
241 }
242 }
243 }
244 } while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING);
245 }
246
247 /* unlink and free anything still marked. */
248 for (serv = daemon->servers, up = &daemon->servers; serv; serv = tmp)
249 {
250 tmp = serv->next;
251 if (serv->flags & SERV_MARK)
252 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100253 server_gone(serv);
Simon Kelley3d8df262005-08-29 12:19:27 +0100254 *up = serv->next;
255 free(serv);
256 }
257 else
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100258 up = &serv->next;
Simon Kelley3d8df262005-08-29 12:19:27 +0100259 }
260
261}
262
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100263DBusHandlerResult message_handler(DBusConnection *connection,
264 DBusMessage *message,
265 void *user_data)
Simon Kelley3d8df262005-08-29 12:19:27 +0100266{
267 char *method = (char *)dbus_message_get_member(message);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100268
Simon Kelley73a08a22009-02-05 20:28:08 +0000269 if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
270 {
Simon Kelleyad094272012-08-10 17:10:54 +0100271 DBusMessage *reply;
Simon Kelley73a08a22009-02-05 20:28:08 +0000272
Simon Kelleyad094272012-08-10 17:10:54 +0100273 /* string length: "%s" provides space for termination zero */
274 if (!introspection_xml &&
275 (introspection_xml = whine_malloc(strlen(introspection_xml_template) + strlen(daemon->dbus_name))))
276 sprintf(introspection_xml, introspection_xml_template, daemon->dbus_name);
277
278 if (introspection_xml)
279 {
280 reply = dbus_message_new_method_return(message);
281
282 dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_xml, DBUS_TYPE_INVALID);
283 dbus_connection_send (connection, reply, NULL);
284 dbus_message_unref (reply);
285 }
Simon Kelley73a08a22009-02-05 20:28:08 +0000286 }
287 else if (strcmp(method, "GetVersion") == 0)
Simon Kelley3d8df262005-08-29 12:19:27 +0100288 {
289 char *v = VERSION;
290 DBusMessage *reply = dbus_message_new_method_return(message);
291
292 dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID);
293 dbus_connection_send (connection, reply, NULL);
294 dbus_message_unref (reply);
295 }
296 else if (strcmp(method, "SetServers") == 0)
297 {
Simon Kelleyf2621c72007-04-29 19:47:21 +0100298 my_syslog(LOG_INFO, _("setting upstream servers from DBus"));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100299 dbus_read_servers(message);
300 check_servers();
Simon Kelley3d8df262005-08-29 12:19:27 +0100301 }
302 else if (strcmp(method, "ClearCache") == 0)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100303 clear_cache_and_reload(dnsmasq_time());
Simon Kelley3d8df262005-08-29 12:19:27 +0100304 else
305 return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
306
Simon Kelley5aabfc72007-08-29 11:24:47 +0100307 method = user_data; /* no warning */
308
Simon Kelley3d8df262005-08-29 12:19:27 +0100309 return (DBUS_HANDLER_RESULT_HANDLED);
310
311}
312
313
314/* returns NULL or error message, may fail silently if dbus daemon not yet up. */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100315char *dbus_init(void)
Simon Kelley3d8df262005-08-29 12:19:27 +0100316{
317 DBusConnection *connection = NULL;
318 DBusObjectPathVTable dnsmasq_vtable = {NULL, &message_handler, NULL, NULL, NULL, NULL };
319 DBusError dbus_error;
320 DBusMessage *message;
321
322 dbus_error_init (&dbus_error);
323 if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error)))
324 return NULL;
Simon Kelley7cebd202006-05-06 14:13:33 +0100325
Simon Kelley3d8df262005-08-29 12:19:27 +0100326 dbus_connection_set_exit_on_disconnect(connection, FALSE);
327 dbus_connection_set_watch_functions(connection, add_watch, remove_watch,
Simon Kelley5aabfc72007-08-29 11:24:47 +0100328 NULL, NULL, NULL);
Simon Kelley3d8df262005-08-29 12:19:27 +0100329 dbus_error_init (&dbus_error);
Simon Kelleyad094272012-08-10 17:10:54 +0100330 dbus_bus_request_name (connection, daemon->dbus_name, 0, &dbus_error);
Simon Kelley3d8df262005-08-29 12:19:27 +0100331 if (dbus_error_is_set (&dbus_error))
332 return (char *)dbus_error.message;
333
334 if (!dbus_connection_register_object_path(connection, DNSMASQ_PATH,
Simon Kelley5aabfc72007-08-29 11:24:47 +0100335 &dnsmasq_vtable, NULL))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000336 return _("could not register a DBus message handler");
Simon Kelley3d8df262005-08-29 12:19:27 +0100337
338 daemon->dbus = connection;
339
Simon Kelleyad094272012-08-10 17:10:54 +0100340 if ((message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, "Up")))
Simon Kelley9009d742008-11-14 20:04:27 +0000341 {
342 dbus_connection_send(connection, message, NULL);
343 dbus_message_unref(message);
344 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100345
346 return NULL;
347}
348
349
Simon Kelley5aabfc72007-08-29 11:24:47 +0100350void set_dbus_listeners(int *maxfdp,
Simon Kelley16972692006-10-16 20:04:18 +0100351 fd_set *rset, fd_set *wset, fd_set *eset)
Simon Kelley3d8df262005-08-29 12:19:27 +0100352{
353 struct watch *w;
354
355 for (w = daemon->watches; w; w = w->next)
356 if (dbus_watch_get_enabled(w->watch))
357 {
358 unsigned int flags = dbus_watch_get_flags(w->watch);
Simon Kelley824af852008-02-12 20:43:05 +0000359 int fd = dbus_watch_get_unix_fd(w->watch);
Simon Kelley3d8df262005-08-29 12:19:27 +0100360
Simon Kelley16972692006-10-16 20:04:18 +0100361 bump_maxfd(fd, maxfdp);
Simon Kelley3d8df262005-08-29 12:19:27 +0100362
363 if (flags & DBUS_WATCH_READABLE)
364 FD_SET(fd, rset);
365
366 if (flags & DBUS_WATCH_WRITABLE)
367 FD_SET(fd, wset);
368
369 FD_SET(fd, eset);
370 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100371}
372
Simon Kelley5aabfc72007-08-29 11:24:47 +0100373void check_dbus_listeners(fd_set *rset, fd_set *wset, fd_set *eset)
Simon Kelley3d8df262005-08-29 12:19:27 +0100374{
375 DBusConnection *connection = (DBusConnection *)daemon->dbus;
376 struct watch *w;
377
378 for (w = daemon->watches; w; w = w->next)
379 if (dbus_watch_get_enabled(w->watch))
380 {
381 unsigned int flags = 0;
Simon Kelley824af852008-02-12 20:43:05 +0000382 int fd = dbus_watch_get_unix_fd(w->watch);
Simon Kelley3d8df262005-08-29 12:19:27 +0100383
384 if (FD_ISSET(fd, rset))
385 flags |= DBUS_WATCH_READABLE;
386
387 if (FD_ISSET(fd, wset))
388 flags |= DBUS_WATCH_WRITABLE;
389
390 if (FD_ISSET(fd, eset))
391 flags |= DBUS_WATCH_ERROR;
392
393 if (flags != 0)
394 dbus_watch_handle(w->watch, flags);
395 }
396
397 if (connection)
398 {
399 dbus_connection_ref (connection);
400 while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS);
401 dbus_connection_unref (connection);
402 }
403}
404
Simon Kelley316e2732010-01-22 20:16:09 +0000405#ifdef HAVE_DHCP
Simon Kelley1f15b812009-10-13 17:49:32 +0100406void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname)
Simon Kelley9009d742008-11-14 20:04:27 +0000407{
408 DBusConnection *connection = (DBusConnection *)daemon->dbus;
409 DBusMessage* message = NULL;
410 DBusMessageIter args;
Simon Kelley6ffeff82012-03-07 10:32:35 +0000411 char *action_str, *mac = daemon->namebuff;
Simon Kelley1f15b812009-10-13 17:49:32 +0100412 unsigned char *p;
413 int i;
Simon Kelley9009d742008-11-14 20:04:27 +0000414
415 if (!connection)
416 return;
Simon Kelley1f15b812009-10-13 17:49:32 +0100417
418 if (!hostname)
419 hostname = "";
420
Simon Kelley6ffeff82012-03-07 10:32:35 +0000421#ifdef HAVE_DHCP6
422 if (lease->flags & (LEASE_TA | LEASE_NA))
423 {
424 print_mac(mac, lease->clid, lease->clid_len);
425 inet_ntop(AF_INET6, lease->hwaddr, daemon->addrbuff, ADDRSTRLEN);
426 }
427 else
428#endif
429 {
430 p = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len,
431 lease->hwaddr, lease->clid_len, lease->clid, &i);
432 print_mac(mac, p, i);
433 inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN);
434 }
435
Simon Kelley9009d742008-11-14 20:04:27 +0000436 if (action == ACTION_DEL)
437 action_str = "DhcpLeaseDeleted";
438 else if (action == ACTION_ADD)
439 action_str = "DhcpLeaseAdded";
440 else if (action == ACTION_OLD)
441 action_str = "DhcpLeaseUpdated";
442 else
443 return;
444
Simon Kelleyad094272012-08-10 17:10:54 +0100445 if (!(message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, action_str)))
Simon Kelley9009d742008-11-14 20:04:27 +0000446 return;
447
448 dbus_message_iter_init_append(message, &args);
Simon Kelley6ffeff82012-03-07 10:32:35 +0000449
450 if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) &&
Simon Kelley9009d742008-11-14 20:04:27 +0000451 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) &&
452 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname))
453 dbus_connection_send(connection, message, NULL);
454
455 dbus_message_unref(message);
456}
Simon Kelley316e2732010-01-22 20:16:09 +0000457#endif
Simon Kelley9009d742008-11-14 20:04:27 +0000458
Simon Kelley3d8df262005-08-29 12:19:27 +0100459#endif