Simon Kelley | 5954608 | 2012-01-06 20:02:04 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2012 Simon Kelley |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 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 |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 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. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 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/>. |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
| 19 | #ifdef HAVE_DBUS |
| 20 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 21 | #include <dbus/dbus.h> |
| 22 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 23 | const char* introspection_xml_template = |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 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" |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 32 | " <interface name=\"%s\">\n" |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 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" |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 41 | " <method name=\"SetServersEx\">\n" |
| 42 | " <arg name=\"servers\" direction=\"in\" type=\"aas\"/>\n" |
| 43 | " </method>\n" |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 44 | " <signal name=\"DhcpLeaseAdded\">\n" |
| 45 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 46 | " <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 47 | " <arg name=\"hostname\" type=\"s\"/>\n" |
| 48 | " </signal>\n" |
| 49 | " <signal name=\"DhcpLeaseDeleted\">\n" |
| 50 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 51 | " <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 52 | " <arg name=\"hostname\" type=\"s\"/>\n" |
| 53 | " </signal>\n" |
| 54 | " <signal name=\"DhcpLeaseUpdated\">\n" |
| 55 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 56 | " <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 57 | " <arg name=\"hostname\" type=\"s\"/>\n" |
| 58 | " </signal>\n" |
| 59 | " </interface>\n" |
| 60 | "</node>\n"; |
| 61 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 62 | static char *introspection_xml = NULL; |
| 63 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 64 | struct watch { |
| 65 | DBusWatch *watch; |
| 66 | struct watch *next; |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | static dbus_bool_t add_watch(DBusWatch *watch, void *data) |
| 71 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 72 | struct watch *w; |
| 73 | |
| 74 | for (w = daemon->watches; w; w = w->next) |
| 75 | if (w->watch == watch) |
| 76 | return TRUE; |
| 77 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 78 | if (!(w = whine_malloc(sizeof(struct watch)))) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 79 | return FALSE; |
| 80 | |
| 81 | w->watch = watch; |
| 82 | w->next = daemon->watches; |
| 83 | daemon->watches = w; |
| 84 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 85 | w = data; /* no warning */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 86 | return TRUE; |
| 87 | } |
| 88 | |
| 89 | static void remove_watch(DBusWatch *watch, void *data) |
| 90 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 91 | struct watch **up, *w; |
| 92 | |
| 93 | for (up = &(daemon->watches), w = daemon->watches; w; w = w->next) |
| 94 | if (w->watch == watch) |
| 95 | { |
| 96 | *up = w->next; |
| 97 | free(w); |
| 98 | } |
| 99 | else |
| 100 | up = &(w->next); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 101 | |
| 102 | w = data; /* no warning */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 105 | static void add_update_server(union mysockaddr *addr, |
| 106 | union mysockaddr *source_addr, |
| 107 | const char *interface, |
| 108 | const char *domain) |
| 109 | { |
| 110 | struct server *serv; |
| 111 | |
| 112 | /* See if there is a suitable candidate, and unmark */ |
| 113 | for (serv = daemon->servers; serv; serv = serv->next) |
| 114 | if ((serv->flags & SERV_FROM_DBUS) && |
| 115 | (serv->flags & SERV_MARK)) |
| 116 | { |
| 117 | if (domain) |
| 118 | { |
| 119 | if (!(serv->flags & SERV_HAS_DOMAIN) || !hostname_isequal(domain, serv->domain)) |
| 120 | continue; |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | if (serv->flags & SERV_HAS_DOMAIN) |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | serv->flags &= ~SERV_MARK; |
| 129 | |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | if (!serv && (serv = whine_malloc(sizeof (struct server)))) |
| 134 | { |
| 135 | /* Not found, create a new one. */ |
| 136 | memset(serv, 0, sizeof(struct server)); |
| 137 | |
| 138 | if (domain && !(serv->domain = whine_malloc(strlen(domain)+1))) |
| 139 | { |
| 140 | free(serv); |
| 141 | serv = NULL; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | serv->next = daemon->servers; |
| 146 | daemon->servers = serv; |
| 147 | serv->flags = SERV_FROM_DBUS; |
| 148 | if (domain) |
| 149 | { |
| 150 | strcpy(serv->domain, domain); |
| 151 | serv->flags |= SERV_HAS_DOMAIN; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (serv) |
| 157 | { |
| 158 | if (interface) |
| 159 | strcpy(serv->interface, interface); |
| 160 | else |
| 161 | serv->interface[0] = 0; |
| 162 | |
| 163 | if (source_addr->in.sin_family == AF_INET && |
| 164 | addr->in.sin_addr.s_addr == 0 && |
| 165 | serv->domain) |
| 166 | serv->flags |= SERV_NO_ADDR; |
| 167 | else |
| 168 | { |
| 169 | serv->flags &= ~SERV_NO_ADDR; |
| 170 | serv->addr = *addr; |
| 171 | serv->source_addr = *source_addr; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static void mark_dbus(void) |
| 177 | { |
| 178 | struct server *serv; |
| 179 | |
| 180 | /* mark everything from DBUS */ |
| 181 | for (serv = daemon->servers; serv; serv = serv->next) |
| 182 | if (serv->flags & SERV_FROM_DBUS) |
| 183 | serv->flags |= SERV_MARK; |
| 184 | } |
| 185 | |
| 186 | static void cleanup_dbus() |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 187 | { |
| 188 | struct server *serv, *tmp, **up; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 189 | |
| 190 | /* unlink and free anything still marked. */ |
| 191 | for (serv = daemon->servers, up = &daemon->servers; serv; serv = tmp) |
| 192 | { |
| 193 | tmp = serv->next; |
| 194 | if (serv->flags & SERV_MARK) |
| 195 | { |
| 196 | server_gone(serv); |
| 197 | *up = serv->next; |
| 198 | if (serv->domain) |
| 199 | free(serv->domain); |
| 200 | free(serv); |
| 201 | } |
| 202 | else |
| 203 | up = &serv->next; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static void dbus_read_servers(DBusMessage *message) |
| 208 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 209 | DBusMessageIter iter; |
| 210 | union mysockaddr addr, source_addr; |
| 211 | char *domain; |
| 212 | |
| 213 | dbus_message_iter_init(message, &iter); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 214 | |
| 215 | mark_dbus(); |
| 216 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 217 | while (1) |
| 218 | { |
| 219 | int skip = 0; |
| 220 | |
| 221 | if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32) |
| 222 | { |
| 223 | u32 a; |
| 224 | |
| 225 | dbus_message_iter_get_basic(&iter, &a); |
| 226 | dbus_message_iter_next (&iter); |
| 227 | |
| 228 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 229 | source_addr.in.sin_len = addr.in.sin_len = sizeof(struct sockaddr_in); |
| 230 | #endif |
| 231 | addr.in.sin_addr.s_addr = ntohl(a); |
| 232 | source_addr.in.sin_family = addr.in.sin_family = AF_INET; |
| 233 | addr.in.sin_port = htons(NAMESERVER_PORT); |
| 234 | source_addr.in.sin_addr.s_addr = INADDR_ANY; |
| 235 | source_addr.in.sin_port = htons(daemon->query_port); |
| 236 | } |
| 237 | else if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_BYTE) |
| 238 | { |
| 239 | unsigned char p[sizeof(struct in6_addr)]; |
| 240 | unsigned int i; |
| 241 | |
| 242 | skip = 1; |
| 243 | |
| 244 | for(i = 0; i < sizeof(struct in6_addr); i++) |
| 245 | { |
| 246 | dbus_message_iter_get_basic(&iter, &p[i]); |
| 247 | dbus_message_iter_next (&iter); |
| 248 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BYTE) |
| 249 | break; |
| 250 | } |
| 251 | |
| 252 | #ifndef HAVE_IPV6 |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 253 | my_syslog(LOG_WARNING, _("attempt to set an IPv6 server address via DBus - no IPv6 support")); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 254 | #else |
| 255 | if (i == sizeof(struct in6_addr)-1) |
| 256 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 257 | memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr)); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 258 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 259 | source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(struct sockaddr_in6); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 260 | #endif |
| 261 | source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6; |
| 262 | addr.in6.sin6_port = htons(NAMESERVER_PORT); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 263 | source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0; |
| 264 | source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 265 | source_addr.in6.sin6_addr = in6addr_any; |
| 266 | source_addr.in6.sin6_port = htons(daemon->query_port); |
| 267 | skip = 0; |
| 268 | } |
| 269 | #endif |
| 270 | } |
| 271 | else |
| 272 | /* At the end */ |
| 273 | break; |
| 274 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 275 | /* process each domain */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 276 | do { |
| 277 | if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) |
| 278 | { |
| 279 | dbus_message_iter_get_basic(&iter, &domain); |
| 280 | dbus_message_iter_next (&iter); |
| 281 | } |
| 282 | else |
| 283 | domain = NULL; |
| 284 | |
| 285 | if (!skip) |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 286 | add_update_server(&addr, &source_addr, NULL, domain); |
| 287 | |
| 288 | } while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 289 | } |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 290 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 291 | /* unlink and free anything still marked. */ |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 292 | cleanup_dbus(); |
| 293 | } |
| 294 | |
| 295 | static DBusMessage* dbus_read_servers_ex(DBusMessage *message) |
| 296 | { |
| 297 | DBusMessageIter iter, array_iter, string_iter; |
| 298 | DBusMessage *error = NULL; |
| 299 | const char *addr_err; |
| 300 | |
| 301 | if (!dbus_message_iter_init(message, &iter)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 302 | { |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 303 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 304 | "Failed to initialize dbus message iter"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 307 | /* check that the message contains an array of arrays */ |
| 308 | if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) || |
| 309 | (dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY)) |
| 310 | { |
| 311 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 312 | "Expected array of string arrays"); |
| 313 | } |
| 314 | |
| 315 | mark_dbus(); |
| 316 | |
| 317 | /* array_iter points to each "as" element in the outer array */ |
| 318 | dbus_message_iter_recurse(&iter, &array_iter); |
| 319 | while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID) |
| 320 | { |
| 321 | const char *str = NULL; |
| 322 | union mysockaddr addr, source_addr; |
| 323 | char interface[IF_NAMESIZE]; |
| 324 | char *str_addr; |
| 325 | |
| 326 | /* check the types of the struct and its elements */ |
| 327 | if ((dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY) || |
| 328 | (dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_STRING)) |
| 329 | { |
| 330 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 331 | "Expected inner array of strings"); |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | /* string_iter points to each "s" element in the inner array */ |
| 336 | dbus_message_iter_recurse(&array_iter, &string_iter); |
| 337 | if (dbus_message_iter_get_arg_type(&string_iter) != DBUS_TYPE_STRING) |
| 338 | { |
| 339 | /* no IP address given */ |
| 340 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 341 | "Expected IP address"); |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | dbus_message_iter_get_basic(&string_iter, &str); |
| 346 | if (!str || !strlen (str)) |
| 347 | { |
| 348 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 349 | "Empty IP address"); |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | memset(&addr, 0, sizeof(addr)); |
| 354 | memset(&source_addr, 0, sizeof(source_addr)); |
| 355 | memset(&interface, 0, sizeof(interface)); |
| 356 | |
| 357 | /* dup the string because it gets modified during parsing */ |
| 358 | str_addr = strdup(str); |
| 359 | if (!str_addr) |
| 360 | { |
| 361 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 362 | "Out of memory parsing IP address"); |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | /* parse the IP address */ |
| 367 | addr_err = parse_server(str_addr, &addr, &source_addr, &interface, NULL); |
| 368 | free(str_addr); |
| 369 | |
| 370 | if (addr_err) |
| 371 | { |
| 372 | error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 373 | "Invalid IP address '%s': %s", |
| 374 | str, addr_err); |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | /* jump past the address to the domain list (if any) */ |
| 379 | dbus_message_iter_next (&string_iter); |
| 380 | |
| 381 | /* parse domains and add each server/domain pair to the list */ |
| 382 | do { |
| 383 | str = NULL; |
| 384 | if (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING) |
| 385 | dbus_message_iter_get_basic(&string_iter, &str); |
| 386 | dbus_message_iter_next (&string_iter); |
| 387 | |
| 388 | add_update_server(&addr, &source_addr, interface, str); |
| 389 | } while (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING); |
| 390 | |
| 391 | /* jump to next element in outer array */ |
| 392 | dbus_message_iter_next(&array_iter); |
| 393 | } |
| 394 | |
| 395 | cleanup_dbus(); |
| 396 | |
| 397 | return error; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 400 | DBusHandlerResult message_handler(DBusConnection *connection, |
| 401 | DBusMessage *message, |
| 402 | void *user_data) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 403 | { |
| 404 | char *method = (char *)dbus_message_get_member(message); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 405 | DBusMessage *reply = NULL; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 406 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 407 | if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) |
| 408 | { |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 409 | /* string length: "%s" provides space for termination zero */ |
| 410 | if (!introspection_xml && |
| 411 | (introspection_xml = whine_malloc(strlen(introspection_xml_template) + strlen(daemon->dbus_name)))) |
| 412 | sprintf(introspection_xml, introspection_xml_template, daemon->dbus_name); |
| 413 | |
| 414 | if (introspection_xml) |
| 415 | { |
| 416 | reply = dbus_message_new_method_return(message); |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 417 | dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_xml, DBUS_TYPE_INVALID); |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 418 | } |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 419 | } |
| 420 | else if (strcmp(method, "GetVersion") == 0) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 421 | { |
| 422 | char *v = VERSION; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 423 | reply = dbus_message_new_method_return(message); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 424 | |
| 425 | dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 426 | } |
| 427 | else if (strcmp(method, "SetServers") == 0) |
| 428 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 429 | my_syslog(LOG_INFO, _("setting upstream servers from DBus")); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 430 | dbus_read_servers(message); |
| 431 | check_servers(); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 432 | } |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 433 | else if (strcmp(method, "SetServersEx") == 0) |
| 434 | { |
| 435 | my_syslog(LOG_INFO, _("setting upstream servers from DBus")); |
| 436 | reply = dbus_read_servers_ex(message); |
| 437 | check_servers(); |
| 438 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 439 | else if (strcmp(method, "ClearCache") == 0) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 440 | clear_cache_and_reload(dnsmasq_time()); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 441 | else |
| 442 | return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
| 443 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 444 | method = user_data; /* no warning */ |
| 445 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 446 | /* If no reply or no error, return nothing */ |
| 447 | if (!reply) |
| 448 | reply = dbus_message_new_method_return(message); |
| 449 | |
| 450 | if (reply) |
| 451 | { |
| 452 | dbus_connection_send (connection, reply, NULL); |
| 453 | dbus_message_unref (reply); |
| 454 | } |
| 455 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 456 | return (DBUS_HANDLER_RESULT_HANDLED); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
| 460 | /* returns NULL or error message, may fail silently if dbus daemon not yet up. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 461 | char *dbus_init(void) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 462 | { |
| 463 | DBusConnection *connection = NULL; |
| 464 | DBusObjectPathVTable dnsmasq_vtable = {NULL, &message_handler, NULL, NULL, NULL, NULL }; |
| 465 | DBusError dbus_error; |
| 466 | DBusMessage *message; |
| 467 | |
| 468 | dbus_error_init (&dbus_error); |
| 469 | if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error))) |
| 470 | return NULL; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 471 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 472 | dbus_connection_set_exit_on_disconnect(connection, FALSE); |
| 473 | dbus_connection_set_watch_functions(connection, add_watch, remove_watch, |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 474 | NULL, NULL, NULL); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 475 | dbus_error_init (&dbus_error); |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 476 | dbus_bus_request_name (connection, daemon->dbus_name, 0, &dbus_error); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 477 | if (dbus_error_is_set (&dbus_error)) |
| 478 | return (char *)dbus_error.message; |
| 479 | |
| 480 | if (!dbus_connection_register_object_path(connection, DNSMASQ_PATH, |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 481 | &dnsmasq_vtable, NULL)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 482 | return _("could not register a DBus message handler"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 483 | |
| 484 | daemon->dbus = connection; |
| 485 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 486 | if ((message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, "Up"))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 487 | { |
| 488 | dbus_connection_send(connection, message, NULL); |
| 489 | dbus_message_unref(message); |
| 490 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 491 | |
| 492 | return NULL; |
| 493 | } |
| 494 | |
| 495 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 496 | void set_dbus_listeners(int *maxfdp, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 497 | fd_set *rset, fd_set *wset, fd_set *eset) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 498 | { |
| 499 | struct watch *w; |
| 500 | |
| 501 | for (w = daemon->watches; w; w = w->next) |
| 502 | if (dbus_watch_get_enabled(w->watch)) |
| 503 | { |
| 504 | unsigned int flags = dbus_watch_get_flags(w->watch); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 505 | int fd = dbus_watch_get_unix_fd(w->watch); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 506 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 507 | bump_maxfd(fd, maxfdp); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 508 | |
| 509 | if (flags & DBUS_WATCH_READABLE) |
| 510 | FD_SET(fd, rset); |
| 511 | |
| 512 | if (flags & DBUS_WATCH_WRITABLE) |
| 513 | FD_SET(fd, wset); |
| 514 | |
| 515 | FD_SET(fd, eset); |
| 516 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 519 | void check_dbus_listeners(fd_set *rset, fd_set *wset, fd_set *eset) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 520 | { |
| 521 | DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 522 | struct watch *w; |
| 523 | |
| 524 | for (w = daemon->watches; w; w = w->next) |
| 525 | if (dbus_watch_get_enabled(w->watch)) |
| 526 | { |
| 527 | unsigned int flags = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 528 | int fd = dbus_watch_get_unix_fd(w->watch); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 529 | |
| 530 | if (FD_ISSET(fd, rset)) |
| 531 | flags |= DBUS_WATCH_READABLE; |
| 532 | |
| 533 | if (FD_ISSET(fd, wset)) |
| 534 | flags |= DBUS_WATCH_WRITABLE; |
| 535 | |
| 536 | if (FD_ISSET(fd, eset)) |
| 537 | flags |= DBUS_WATCH_ERROR; |
| 538 | |
| 539 | if (flags != 0) |
| 540 | dbus_watch_handle(w->watch, flags); |
| 541 | } |
| 542 | |
| 543 | if (connection) |
| 544 | { |
| 545 | dbus_connection_ref (connection); |
| 546 | while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS); |
| 547 | dbus_connection_unref (connection); |
| 548 | } |
| 549 | } |
| 550 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 551 | #ifdef HAVE_DHCP |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 552 | void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 553 | { |
| 554 | DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 555 | DBusMessage* message = NULL; |
| 556 | DBusMessageIter args; |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 557 | char *action_str, *mac = daemon->namebuff; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 558 | unsigned char *p; |
| 559 | int i; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 560 | |
| 561 | if (!connection) |
| 562 | return; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 563 | |
| 564 | if (!hostname) |
| 565 | hostname = ""; |
| 566 | |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 567 | #ifdef HAVE_DHCP6 |
| 568 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 569 | { |
| 570 | print_mac(mac, lease->clid, lease->clid_len); |
| 571 | inet_ntop(AF_INET6, lease->hwaddr, daemon->addrbuff, ADDRSTRLEN); |
| 572 | } |
| 573 | else |
| 574 | #endif |
| 575 | { |
| 576 | p = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len, |
| 577 | lease->hwaddr, lease->clid_len, lease->clid, &i); |
| 578 | print_mac(mac, p, i); |
| 579 | inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN); |
| 580 | } |
| 581 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 582 | if (action == ACTION_DEL) |
| 583 | action_str = "DhcpLeaseDeleted"; |
| 584 | else if (action == ACTION_ADD) |
| 585 | action_str = "DhcpLeaseAdded"; |
| 586 | else if (action == ACTION_OLD) |
| 587 | action_str = "DhcpLeaseUpdated"; |
| 588 | else |
| 589 | return; |
| 590 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 591 | if (!(message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, action_str))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 592 | return; |
| 593 | |
| 594 | dbus_message_iter_init_append(message, &args); |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 595 | |
| 596 | if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) && |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 597 | dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) && |
| 598 | dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname)) |
| 599 | dbus_connection_send(connection, message, NULL); |
| 600 | |
| 601 | dbus_message_unref(message); |
| 602 | } |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 603 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 604 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 605 | #endif |