Simon Kelley | 6174435 | 2013-01-31 14:34:40 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2013 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 | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 41 | " <method name=\"SetDomainServers\">\n" |
| 42 | " <arg name=\"servers\" direction=\"in\" type=\"as\"/>\n" |
| 43 | " </method>\n" |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 44 | " <method name=\"SetServersEx\">\n" |
| 45 | " <arg name=\"servers\" direction=\"in\" type=\"aas\"/>\n" |
| 46 | " </method>\n" |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 47 | " <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 Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 65 | static char *introspection_xml = NULL; |
| 66 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 67 | struct watch { |
| 68 | DBusWatch *watch; |
| 69 | struct watch *next; |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | static dbus_bool_t add_watch(DBusWatch *watch, void *data) |
| 74 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 75 | struct watch *w; |
| 76 | |
| 77 | for (w = daemon->watches; w; w = w->next) |
| 78 | if (w->watch == watch) |
| 79 | return TRUE; |
| 80 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 81 | if (!(w = whine_malloc(sizeof(struct watch)))) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 82 | return FALSE; |
| 83 | |
| 84 | w->watch = watch; |
| 85 | w->next = daemon->watches; |
| 86 | daemon->watches = w; |
| 87 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 88 | w = data; /* no warning */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 89 | return TRUE; |
| 90 | } |
| 91 | |
| 92 | static void remove_watch(DBusWatch *watch, void *data) |
| 93 | { |
Simon Kelley | 25c4198 | 2013-02-05 14:56:02 +0000 | [diff] [blame] | 94 | struct watch **up, *w, *tmp; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 95 | |
Simon Kelley | 25c4198 | 2013-02-05 14:56:02 +0000 | [diff] [blame] | 96 | 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 Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 107 | |
| 108 | w = data; /* no warning */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 111 | static 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 | |
| 182 | static 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 | |
| 192 | static void cleanup_dbus() |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 193 | { |
| 194 | struct server *serv, *tmp, **up; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 195 | |
| 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 | |
| 213 | static void dbus_read_servers(DBusMessage *message) |
| 214 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 215 | DBusMessageIter iter; |
| 216 | union mysockaddr addr, source_addr; |
| 217 | char *domain; |
| 218 | |
| 219 | dbus_message_iter_init(message, &iter); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 220 | |
| 221 | mark_dbus(); |
| 222 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 223 | 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 Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 259 | 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] | 260 | #else |
| 261 | if (i == sizeof(struct in6_addr)-1) |
| 262 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 263 | memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr)); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 264 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 265 | 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] | 266 | #endif |
| 267 | source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6; |
| 268 | addr.in6.sin6_port = htons(NAMESERVER_PORT); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 269 | source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0; |
| 270 | source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 271 | 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 Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 281 | /* process each domain */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 282 | 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 Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 292 | add_update_server(&addr, &source_addr, NULL, domain); |
| 293 | |
| 294 | } while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 295 | } |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 296 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 297 | /* unlink and free anything still marked. */ |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 298 | cleanup_dbus(); |
| 299 | } |
| 300 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 301 | static DBusMessage* dbus_read_servers_ex(DBusMessage *message, int strings) |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 302 | { |
| 303 | DBusMessageIter iter, array_iter, string_iter; |
| 304 | DBusMessage *error = NULL; |
| 305 | const char *addr_err; |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 306 | char *dup = NULL; |
| 307 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 308 | if (!dbus_message_iter_init(message, &iter)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 309 | { |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 310 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 311 | "Failed to initialize dbus message iter"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 314 | /* check that the message contains an array of arrays */ |
| 315 | if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) || |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 316 | (dbus_message_iter_get_element_type(&iter) != (strings ? DBUS_TYPE_STRING : DBUS_TYPE_ARRAY))) |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 317 | { |
| 318 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 319 | strings ? "Expected array of string" : "Expected array of string arrays"); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 320 | } |
| 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 Kelley | d89fb4e | 2012-12-01 21:21:13 +0000 | [diff] [blame] | 331 | char *str_addr, *str_domain = NULL; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 332 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 333 | 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 Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 344 | if (dup) |
| 345 | free(dup); |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 346 | if (!(dup = str_domain = whine_malloc(strlen(str)+1))) |
| 347 | break; |
Simon Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 348 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 349 | strcpy(str_domain, str); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 350 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 351 | /* 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 Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 406 | if (dup) |
| 407 | free(dup); |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 408 | if (!(dup = str_addr = whine_malloc(strlen(str)+1))) |
| 409 | break; |
Simon Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 410 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 411 | strcpy(str_addr, str); |
| 412 | } |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 413 | |
| 414 | memset(&addr, 0, sizeof(addr)); |
| 415 | memset(&source_addr, 0, sizeof(source_addr)); |
| 416 | memset(&interface, 0, sizeof(interface)); |
| 417 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 418 | /* parse the IP address */ |
Simon Kelley | c99df93 | 2012-10-12 13:39:04 +0100 | [diff] [blame] | 419 | addr_err = parse_server(str_addr, &addr, &source_addr, (char *) &interface, NULL); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 420 | |
| 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 Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 429 | 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 Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 461 | /* jump to next element in outer array */ |
| 462 | dbus_message_iter_next(&array_iter); |
| 463 | } |
| 464 | |
| 465 | cleanup_dbus(); |
| 466 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 467 | if (dup) |
| 468 | free(dup); |
| 469 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 470 | return error; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 473 | DBusHandlerResult message_handler(DBusConnection *connection, |
| 474 | DBusMessage *message, |
| 475 | void *user_data) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 476 | { |
| 477 | char *method = (char *)dbus_message_get_member(message); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 478 | DBusMessage *reply = NULL; |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 479 | int clear_cache = 0, new_servers = 0; |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 480 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 481 | if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) |
| 482 | { |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 483 | /* 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 Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 491 | 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] | 492 | } |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 493 | } |
| 494 | else if (strcmp(method, "GetVersion") == 0) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 495 | { |
| 496 | char *v = VERSION; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 497 | reply = dbus_message_new_method_return(message); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 498 | |
| 499 | dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 500 | } |
| 501 | else if (strcmp(method, "SetServers") == 0) |
| 502 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 503 | dbus_read_servers(message); |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 504 | new_servers = 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 505 | } |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 506 | else if (strcmp(method, "SetServersEx") == 0) |
| 507 | { |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 508 | reply = dbus_read_servers_ex(message, 0); |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 509 | new_servers = 1; |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 510 | } |
| 511 | else if (strcmp(method, "SetDomainServers") == 0) |
| 512 | { |
| 513 | reply = dbus_read_servers_ex(message, 1); |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 514 | new_servers = 1; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 515 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 516 | else if (strcmp(method, "ClearCache") == 0) |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 517 | clear_cache = 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 518 | else |
| 519 | return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 520 | |
| 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 Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 531 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 532 | method = user_data; /* no warning */ |
| 533 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 534 | /* 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 Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 544 | return (DBUS_HANDLER_RESULT_HANDLED); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | |
| 548 | /* 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] | 549 | char *dbus_init(void) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 550 | { |
| 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 Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 559 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 560 | dbus_connection_set_exit_on_disconnect(connection, FALSE); |
| 561 | dbus_connection_set_watch_functions(connection, add_watch, remove_watch, |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 562 | NULL, NULL, NULL); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 563 | dbus_error_init (&dbus_error); |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 564 | dbus_bus_request_name (connection, daemon->dbus_name, 0, &dbus_error); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 565 | 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 Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 569 | &dnsmasq_vtable, NULL)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 570 | return _("could not register a DBus message handler"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 571 | |
| 572 | daemon->dbus = connection; |
| 573 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 574 | if ((message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, "Up"))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 575 | { |
| 576 | dbus_connection_send(connection, message, NULL); |
| 577 | dbus_message_unref(message); |
| 578 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 579 | |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 584 | void set_dbus_listeners(int *maxfdp, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 585 | fd_set *rset, fd_set *wset, fd_set *eset) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 586 | { |
| 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 Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 593 | int fd = dbus_watch_get_unix_fd(w->watch); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 594 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 595 | bump_maxfd(fd, maxfdp); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 596 | |
| 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 Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 605 | } |
| 606 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 607 | 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] | 608 | { |
| 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 Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 616 | int fd = dbus_watch_get_unix_fd(w->watch); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 617 | |
| 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 Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 639 | #ifdef HAVE_DHCP |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 640 | void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 641 | { |
| 642 | DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 643 | DBusMessage* message = NULL; |
| 644 | DBusMessageIter args; |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 645 | char *action_str, *mac = daemon->namebuff; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 646 | unsigned char *p; |
| 647 | int i; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 648 | |
| 649 | if (!connection) |
| 650 | return; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 651 | |
| 652 | if (!hostname) |
| 653 | hostname = ""; |
| 654 | |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 655 | #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 Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 670 | 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 Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 679 | 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] | 680 | return; |
| 681 | |
| 682 | dbus_message_iter_init_append(message, &args); |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 683 | |
| 684 | if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) && |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 685 | 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 Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 691 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 692 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 693 | #endif |