blob: 77e393377e0e0cb13cedb5e55a91ef26f28efba8 [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 Kelley73a08a22009-02-05 20:28:08 +000023const char* introspection_xml =
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=\"" DNSMASQ_SERVICE "\">\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" <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 Kelley3d8df262005-08-29 12:19:27 +010059struct watch {
60 DBusWatch *watch;
61 struct watch *next;
62};
63
64
65static dbus_bool_t add_watch(DBusWatch *watch, void *data)
66{
Simon Kelley3d8df262005-08-29 12:19:27 +010067 struct watch *w;
68
69 for (w = daemon->watches; w; w = w->next)
70 if (w->watch == watch)
71 return TRUE;
72
Simon Kelley5aabfc72007-08-29 11:24:47 +010073 if (!(w = whine_malloc(sizeof(struct watch))))
Simon Kelley3d8df262005-08-29 12:19:27 +010074 return FALSE;
75
76 w->watch = watch;
77 w->next = daemon->watches;
78 daemon->watches = w;
79
Simon Kelley5aabfc72007-08-29 11:24:47 +010080 w = data; /* no warning */
Simon Kelley3d8df262005-08-29 12:19:27 +010081 return TRUE;
82}
83
84static void remove_watch(DBusWatch *watch, void *data)
85{
Simon Kelley3d8df262005-08-29 12:19:27 +010086 struct watch **up, *w;
87
88 for (up = &(daemon->watches), w = daemon->watches; w; w = w->next)
89 if (w->watch == watch)
90 {
91 *up = w->next;
92 free(w);
93 }
94 else
95 up = &(w->next);
Simon Kelley5aabfc72007-08-29 11:24:47 +010096
97 w = data; /* no warning */
Simon Kelley3d8df262005-08-29 12:19:27 +010098}
99
Simon Kelley5aabfc72007-08-29 11:24:47 +0100100static void dbus_read_servers(DBusMessage *message)
Simon Kelley3d8df262005-08-29 12:19:27 +0100101{
102 struct server *serv, *tmp, **up;
103 DBusMessageIter iter;
104 union mysockaddr addr, source_addr;
105 char *domain;
106
107 dbus_message_iter_init(message, &iter);
108
109 /* mark everything from DBUS */
110 for (serv = daemon->servers; serv; serv = serv->next)
111 if (serv->flags & SERV_FROM_DBUS)
112 serv->flags |= SERV_MARK;
113
114 while (1)
115 {
116 int skip = 0;
117
118 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32)
119 {
120 u32 a;
121
122 dbus_message_iter_get_basic(&iter, &a);
123 dbus_message_iter_next (&iter);
124
125#ifdef HAVE_SOCKADDR_SA_LEN
126 source_addr.in.sin_len = addr.in.sin_len = sizeof(struct sockaddr_in);
127#endif
128 addr.in.sin_addr.s_addr = ntohl(a);
129 source_addr.in.sin_family = addr.in.sin_family = AF_INET;
130 addr.in.sin_port = htons(NAMESERVER_PORT);
131 source_addr.in.sin_addr.s_addr = INADDR_ANY;
132 source_addr.in.sin_port = htons(daemon->query_port);
133 }
134 else if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_BYTE)
135 {
136 unsigned char p[sizeof(struct in6_addr)];
137 unsigned int i;
138
139 skip = 1;
140
141 for(i = 0; i < sizeof(struct in6_addr); i++)
142 {
143 dbus_message_iter_get_basic(&iter, &p[i]);
144 dbus_message_iter_next (&iter);
145 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BYTE)
146 break;
147 }
148
149#ifndef HAVE_IPV6
Simon Kelleyf2621c72007-04-29 19:47:21 +0100150 my_syslog(LOG_WARNING, _("attempt to set an IPv6 server address via DBus - no IPv6 support"));
Simon Kelley3d8df262005-08-29 12:19:27 +0100151#else
152 if (i == sizeof(struct in6_addr)-1)
153 {
Simon Kelley824af852008-02-12 20:43:05 +0000154 memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr));
Simon Kelley3d8df262005-08-29 12:19:27 +0100155#ifdef HAVE_SOCKADDR_SA_LEN
Simon Kelley9e038942008-05-30 20:06:34 +0100156 source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(struct sockaddr_in6);
Simon Kelley3d8df262005-08-29 12:19:27 +0100157#endif
158 source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6;
159 addr.in6.sin6_port = htons(NAMESERVER_PORT);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100160 source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0;
161 source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0;
Simon Kelley3d8df262005-08-29 12:19:27 +0100162 source_addr.in6.sin6_addr = in6addr_any;
163 source_addr.in6.sin6_port = htons(daemon->query_port);
164 skip = 0;
165 }
166#endif
167 }
168 else
169 /* At the end */
170 break;
171
172 do {
173 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
174 {
175 dbus_message_iter_get_basic(&iter, &domain);
176 dbus_message_iter_next (&iter);
177 }
178 else
179 domain = NULL;
180
181 if (!skip)
182 {
183 /* See if this is already there, and unmark */
184 for (serv = daemon->servers; serv; serv = serv->next)
185 if ((serv->flags & SERV_FROM_DBUS) &&
186 (serv->flags & SERV_MARK))
187 {
188 if (!(serv->flags & SERV_HAS_DOMAIN) && !domain)
189 {
190 serv->flags &= ~SERV_MARK;
191 break;
192 }
193 if ((serv->flags & SERV_HAS_DOMAIN) &&
194 domain &&
195 hostname_isequal(domain, serv->domain))
196 {
197 serv->flags &= ~SERV_MARK;
198 break;
199 }
200 }
201
Simon Kelley5aabfc72007-08-29 11:24:47 +0100202 if (!serv && (serv = whine_malloc(sizeof (struct server))))
Simon Kelley3d8df262005-08-29 12:19:27 +0100203 {
204 /* Not found, create a new one. */
Simon Kelley9e038942008-05-30 20:06:34 +0100205 memset(serv, 0, sizeof(struct server));
206
Simon Kelley3d8df262005-08-29 12:19:27 +0100207 if (domain)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100208 serv->domain = whine_malloc(strlen(domain)+1);
Simon Kelley9e038942008-05-30 20:06:34 +0100209
Simon Kelley3d8df262005-08-29 12:19:27 +0100210 if (domain && !serv->domain)
211 {
212 free(serv);
213 serv = NULL;
214 }
215 else
216 {
217 serv->next = daemon->servers;
218 daemon->servers = serv;
219 serv->flags = SERV_FROM_DBUS;
Simon Kelley3d8df262005-08-29 12:19:27 +0100220 if (domain)
221 {
222 strcpy(serv->domain, domain);
223 serv->flags |= SERV_HAS_DOMAIN;
224 }
225 }
226 }
227
228 if (serv)
229 {
230 if (source_addr.in.sin_family == AF_INET &&
231 addr.in.sin_addr.s_addr == 0 &&
232 serv->domain)
233 serv->flags |= SERV_NO_ADDR;
234 else
235 {
236 serv->flags &= ~SERV_NO_ADDR;
237 serv->addr = addr;
238 serv->source_addr = source_addr;
239 }
240 }
241 }
242 } while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING);
243 }
244
245 /* unlink and free anything still marked. */
246 for (serv = daemon->servers, up = &daemon->servers; serv; serv = tmp)
247 {
248 tmp = serv->next;
249 if (serv->flags & SERV_MARK)
250 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100251 server_gone(serv);
Simon Kelley3d8df262005-08-29 12:19:27 +0100252 *up = serv->next;
253 free(serv);
254 }
255 else
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100256 up = &serv->next;
Simon Kelley3d8df262005-08-29 12:19:27 +0100257 }
258
259}
260
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100261DBusHandlerResult message_handler(DBusConnection *connection,
262 DBusMessage *message,
263 void *user_data)
Simon Kelley3d8df262005-08-29 12:19:27 +0100264{
265 char *method = (char *)dbus_message_get_member(message);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100266
Simon Kelley73a08a22009-02-05 20:28:08 +0000267 if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
268 {
269 DBusMessage *reply = dbus_message_new_method_return(message);
270
271 dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_xml, DBUS_TYPE_INVALID);
272 dbus_connection_send (connection, reply, NULL);
273 dbus_message_unref (reply);
274 }
275 else if (strcmp(method, "GetVersion") == 0)
Simon Kelley3d8df262005-08-29 12:19:27 +0100276 {
277 char *v = VERSION;
278 DBusMessage *reply = dbus_message_new_method_return(message);
279
280 dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID);
281 dbus_connection_send (connection, reply, NULL);
282 dbus_message_unref (reply);
283 }
284 else if (strcmp(method, "SetServers") == 0)
285 {
Simon Kelleyf2621c72007-04-29 19:47:21 +0100286 my_syslog(LOG_INFO, _("setting upstream servers from DBus"));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100287 dbus_read_servers(message);
288 check_servers();
Simon Kelley3d8df262005-08-29 12:19:27 +0100289 }
290 else if (strcmp(method, "ClearCache") == 0)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100291 clear_cache_and_reload(dnsmasq_time());
Simon Kelley3d8df262005-08-29 12:19:27 +0100292 else
293 return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
294
Simon Kelley5aabfc72007-08-29 11:24:47 +0100295 method = user_data; /* no warning */
296
Simon Kelley3d8df262005-08-29 12:19:27 +0100297 return (DBUS_HANDLER_RESULT_HANDLED);
298
299}
300
301
302/* returns NULL or error message, may fail silently if dbus daemon not yet up. */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100303char *dbus_init(void)
Simon Kelley3d8df262005-08-29 12:19:27 +0100304{
305 DBusConnection *connection = NULL;
306 DBusObjectPathVTable dnsmasq_vtable = {NULL, &message_handler, NULL, NULL, NULL, NULL };
307 DBusError dbus_error;
308 DBusMessage *message;
309
310 dbus_error_init (&dbus_error);
311 if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error)))
312 return NULL;
Simon Kelley7cebd202006-05-06 14:13:33 +0100313
Simon Kelley3d8df262005-08-29 12:19:27 +0100314 dbus_connection_set_exit_on_disconnect(connection, FALSE);
315 dbus_connection_set_watch_functions(connection, add_watch, remove_watch,
Simon Kelley5aabfc72007-08-29 11:24:47 +0100316 NULL, NULL, NULL);
Simon Kelley3d8df262005-08-29 12:19:27 +0100317 dbus_error_init (&dbus_error);
318 dbus_bus_request_name (connection, DNSMASQ_SERVICE, 0, &dbus_error);
319 if (dbus_error_is_set (&dbus_error))
320 return (char *)dbus_error.message;
321
322 if (!dbus_connection_register_object_path(connection, DNSMASQ_PATH,
Simon Kelley5aabfc72007-08-29 11:24:47 +0100323 &dnsmasq_vtable, NULL))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000324 return _("could not register a DBus message handler");
Simon Kelley3d8df262005-08-29 12:19:27 +0100325
326 daemon->dbus = connection;
327
328 if ((message = dbus_message_new_signal(DNSMASQ_PATH, DNSMASQ_SERVICE, "Up")))
Simon Kelley9009d742008-11-14 20:04:27 +0000329 {
330 dbus_connection_send(connection, message, NULL);
331 dbus_message_unref(message);
332 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100333
334 return NULL;
335}
336
337
Simon Kelley5aabfc72007-08-29 11:24:47 +0100338void set_dbus_listeners(int *maxfdp,
Simon Kelley16972692006-10-16 20:04:18 +0100339 fd_set *rset, fd_set *wset, fd_set *eset)
Simon Kelley3d8df262005-08-29 12:19:27 +0100340{
341 struct watch *w;
342
343 for (w = daemon->watches; w; w = w->next)
344 if (dbus_watch_get_enabled(w->watch))
345 {
346 unsigned int flags = dbus_watch_get_flags(w->watch);
Simon Kelley824af852008-02-12 20:43:05 +0000347 int fd = dbus_watch_get_unix_fd(w->watch);
Simon Kelley3d8df262005-08-29 12:19:27 +0100348
Simon Kelley16972692006-10-16 20:04:18 +0100349 bump_maxfd(fd, maxfdp);
Simon Kelley3d8df262005-08-29 12:19:27 +0100350
351 if (flags & DBUS_WATCH_READABLE)
352 FD_SET(fd, rset);
353
354 if (flags & DBUS_WATCH_WRITABLE)
355 FD_SET(fd, wset);
356
357 FD_SET(fd, eset);
358 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100359}
360
Simon Kelley5aabfc72007-08-29 11:24:47 +0100361void check_dbus_listeners(fd_set *rset, fd_set *wset, fd_set *eset)
Simon Kelley3d8df262005-08-29 12:19:27 +0100362{
363 DBusConnection *connection = (DBusConnection *)daemon->dbus;
364 struct watch *w;
365
366 for (w = daemon->watches; w; w = w->next)
367 if (dbus_watch_get_enabled(w->watch))
368 {
369 unsigned int flags = 0;
Simon Kelley824af852008-02-12 20:43:05 +0000370 int fd = dbus_watch_get_unix_fd(w->watch);
Simon Kelley3d8df262005-08-29 12:19:27 +0100371
372 if (FD_ISSET(fd, rset))
373 flags |= DBUS_WATCH_READABLE;
374
375 if (FD_ISSET(fd, wset))
376 flags |= DBUS_WATCH_WRITABLE;
377
378 if (FD_ISSET(fd, eset))
379 flags |= DBUS_WATCH_ERROR;
380
381 if (flags != 0)
382 dbus_watch_handle(w->watch, flags);
383 }
384
385 if (connection)
386 {
387 dbus_connection_ref (connection);
388 while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS);
389 dbus_connection_unref (connection);
390 }
391}
392
Simon Kelley316e2732010-01-22 20:16:09 +0000393#ifdef HAVE_DHCP
Simon Kelley1f15b812009-10-13 17:49:32 +0100394void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname)
Simon Kelley9009d742008-11-14 20:04:27 +0000395{
396 DBusConnection *connection = (DBusConnection *)daemon->dbus;
397 DBusMessage* message = NULL;
398 DBusMessageIter args;
Simon Kelley1f15b812009-10-13 17:49:32 +0100399 char *action_str, *addr, *mac = daemon->namebuff;
400 unsigned char *p;
401 int i;
Simon Kelley9009d742008-11-14 20:04:27 +0000402
403 if (!connection)
404 return;
Simon Kelley1f15b812009-10-13 17:49:32 +0100405
406 if (!hostname)
407 hostname = "";
408
409 p = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len,
410 lease->hwaddr, lease->clid_len, lease->clid, &i);
411 print_mac(mac, p, i);
412
Simon Kelley9009d742008-11-14 20:04:27 +0000413 if (action == ACTION_DEL)
414 action_str = "DhcpLeaseDeleted";
415 else if (action == ACTION_ADD)
416 action_str = "DhcpLeaseAdded";
417 else if (action == ACTION_OLD)
418 action_str = "DhcpLeaseUpdated";
419 else
420 return;
421
Simon Kelley1f15b812009-10-13 17:49:32 +0100422 addr = inet_ntoa(lease->addr);
423
Simon Kelley9009d742008-11-14 20:04:27 +0000424 if (!(message = dbus_message_new_signal(DNSMASQ_PATH, DNSMASQ_SERVICE, action_str)))
425 return;
426
427 dbus_message_iter_init_append(message, &args);
428
429 if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &addr) &&
430 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) &&
431 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname))
432 dbus_connection_send(connection, message, NULL);
433
434 dbus_message_unref(message);
435}
Simon Kelley316e2732010-01-22 20:16:09 +0000436#endif
Simon Kelley9009d742008-11-14 20:04:27 +0000437
Simon Kelley3d8df262005-08-29 12:19:27 +0100438#endif