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 | |
| 308 | my_syslog(LOG_INFO, _("setting upstream servers from DBus")); |
| 309 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 310 | if (!dbus_message_iter_init(message, &iter)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 311 | { |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 312 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 313 | "Failed to initialize dbus message iter"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 316 | /* check that the message contains an array of arrays */ |
| 317 | if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) || |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 318 | (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] | 319 | { |
| 320 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 321 | strings ? "Expected array of string" : "Expected array of string arrays"); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | mark_dbus(); |
| 325 | |
| 326 | /* array_iter points to each "as" element in the outer array */ |
| 327 | dbus_message_iter_recurse(&iter, &array_iter); |
| 328 | while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID) |
| 329 | { |
| 330 | const char *str = NULL; |
| 331 | union mysockaddr addr, source_addr; |
| 332 | char interface[IF_NAMESIZE]; |
Simon Kelley | d89fb4e | 2012-12-01 21:21:13 +0000 | [diff] [blame] | 333 | char *str_addr, *str_domain = NULL; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 334 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 335 | if (strings) |
| 336 | { |
| 337 | dbus_message_iter_get_basic(&array_iter, &str); |
| 338 | if (!str || !strlen (str)) |
| 339 | { |
| 340 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 341 | "Empty string"); |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | /* dup the string because it gets modified during parsing */ |
Simon Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 346 | if (dup) |
| 347 | free(dup); |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 348 | if (!(dup = str_domain = whine_malloc(strlen(str)+1))) |
| 349 | break; |
Simon Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 350 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 351 | strcpy(str_domain, str); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 352 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 353 | /* point to address part of old string for error message */ |
| 354 | if ((str_addr = strrchr(str, '/'))) |
| 355 | str = str_addr+1; |
| 356 | |
| 357 | if ((str_addr = strrchr(str_domain, '/'))) |
| 358 | { |
| 359 | if (*str_domain != '/' || str_addr == str_domain) |
| 360 | { |
| 361 | error = dbus_message_new_error_printf(message, |
| 362 | DBUS_ERROR_INVALID_ARGS, |
| 363 | "No domain terminator '%s'", |
| 364 | str); |
| 365 | break; |
| 366 | } |
| 367 | *str_addr++ = 0; |
| 368 | str_domain++; |
| 369 | } |
| 370 | else |
| 371 | { |
| 372 | str_addr = str_domain; |
| 373 | str_domain = NULL; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | /* check the types of the struct and its elements */ |
| 381 | if ((dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY) || |
| 382 | (dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_STRING)) |
| 383 | { |
| 384 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 385 | "Expected inner array of strings"); |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | /* string_iter points to each "s" element in the inner array */ |
| 390 | dbus_message_iter_recurse(&array_iter, &string_iter); |
| 391 | if (dbus_message_iter_get_arg_type(&string_iter) != DBUS_TYPE_STRING) |
| 392 | { |
| 393 | /* no IP address given */ |
| 394 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 395 | "Expected IP address"); |
| 396 | break; |
| 397 | } |
| 398 | |
| 399 | dbus_message_iter_get_basic(&string_iter, &str); |
| 400 | if (!str || !strlen (str)) |
| 401 | { |
| 402 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 403 | "Empty IP address"); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | /* dup the string because it gets modified during parsing */ |
Simon Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 408 | if (dup) |
| 409 | free(dup); |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 410 | if (!(dup = str_addr = whine_malloc(strlen(str)+1))) |
| 411 | break; |
Simon Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 412 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 413 | strcpy(str_addr, str); |
| 414 | } |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 415 | |
| 416 | memset(&addr, 0, sizeof(addr)); |
| 417 | memset(&source_addr, 0, sizeof(source_addr)); |
| 418 | memset(&interface, 0, sizeof(interface)); |
| 419 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 420 | /* parse the IP address */ |
Simon Kelley | c99df93 | 2012-10-12 13:39:04 +0100 | [diff] [blame] | 421 | addr_err = parse_server(str_addr, &addr, &source_addr, (char *) &interface, NULL); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 422 | |
| 423 | if (addr_err) |
| 424 | { |
| 425 | error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 426 | "Invalid IP address '%s': %s", |
| 427 | str, addr_err); |
| 428 | break; |
| 429 | } |
| 430 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 431 | if (strings) |
| 432 | { |
| 433 | char *p; |
| 434 | |
| 435 | do { |
| 436 | if (str_domain) |
| 437 | { |
| 438 | if ((p = strchr(str_domain, '/'))) |
| 439 | *p++ = 0; |
| 440 | } |
| 441 | else |
| 442 | p = NULL; |
| 443 | |
| 444 | add_update_server(&addr, &source_addr, interface, str_domain); |
| 445 | } while ((str_domain = p)); |
| 446 | } |
| 447 | else |
| 448 | { |
| 449 | /* jump past the address to the domain list (if any) */ |
| 450 | dbus_message_iter_next (&string_iter); |
| 451 | |
| 452 | /* parse domains and add each server/domain pair to the list */ |
| 453 | do { |
| 454 | str = NULL; |
| 455 | if (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING) |
| 456 | dbus_message_iter_get_basic(&string_iter, &str); |
| 457 | dbus_message_iter_next (&string_iter); |
| 458 | |
| 459 | add_update_server(&addr, &source_addr, interface, str); |
| 460 | } while (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING); |
| 461 | } |
| 462 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 463 | /* jump to next element in outer array */ |
| 464 | dbus_message_iter_next(&array_iter); |
| 465 | } |
| 466 | |
| 467 | cleanup_dbus(); |
| 468 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 469 | if (dup) |
| 470 | free(dup); |
| 471 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 472 | return error; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 473 | } |
| 474 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 475 | DBusHandlerResult message_handler(DBusConnection *connection, |
| 476 | DBusMessage *message, |
| 477 | void *user_data) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 478 | { |
| 479 | char *method = (char *)dbus_message_get_member(message); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 480 | DBusMessage *reply = NULL; |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 481 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 482 | if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) |
| 483 | { |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 484 | /* string length: "%s" provides space for termination zero */ |
| 485 | if (!introspection_xml && |
| 486 | (introspection_xml = whine_malloc(strlen(introspection_xml_template) + strlen(daemon->dbus_name)))) |
| 487 | sprintf(introspection_xml, introspection_xml_template, daemon->dbus_name); |
| 488 | |
| 489 | if (introspection_xml) |
| 490 | { |
| 491 | reply = dbus_message_new_method_return(message); |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 492 | 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] | 493 | } |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 494 | } |
| 495 | else if (strcmp(method, "GetVersion") == 0) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 496 | { |
| 497 | char *v = VERSION; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 498 | reply = dbus_message_new_method_return(message); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 499 | |
| 500 | dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 501 | } |
| 502 | else if (strcmp(method, "SetServers") == 0) |
| 503 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 504 | my_syslog(LOG_INFO, _("setting upstream servers from DBus")); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 505 | dbus_read_servers(message); |
| 506 | check_servers(); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 507 | } |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 508 | else if (strcmp(method, "SetServersEx") == 0) |
| 509 | { |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 510 | reply = dbus_read_servers_ex(message, 0); |
| 511 | check_servers(); |
| 512 | } |
| 513 | else if (strcmp(method, "SetDomainServers") == 0) |
| 514 | { |
| 515 | reply = dbus_read_servers_ex(message, 1); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 516 | check_servers(); |
| 517 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 518 | else if (strcmp(method, "ClearCache") == 0) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 519 | clear_cache_and_reload(dnsmasq_time()); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 520 | else |
| 521 | return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
| 522 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 523 | method = user_data; /* no warning */ |
| 524 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 525 | /* If no reply or no error, return nothing */ |
| 526 | if (!reply) |
| 527 | reply = dbus_message_new_method_return(message); |
| 528 | |
| 529 | if (reply) |
| 530 | { |
| 531 | dbus_connection_send (connection, reply, NULL); |
| 532 | dbus_message_unref (reply); |
| 533 | } |
| 534 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 535 | return (DBUS_HANDLER_RESULT_HANDLED); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
| 539 | /* 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] | 540 | char *dbus_init(void) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 541 | { |
| 542 | DBusConnection *connection = NULL; |
| 543 | DBusObjectPathVTable dnsmasq_vtable = {NULL, &message_handler, NULL, NULL, NULL, NULL }; |
| 544 | DBusError dbus_error; |
| 545 | DBusMessage *message; |
| 546 | |
| 547 | dbus_error_init (&dbus_error); |
| 548 | if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error))) |
| 549 | return NULL; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 550 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 551 | dbus_connection_set_exit_on_disconnect(connection, FALSE); |
| 552 | dbus_connection_set_watch_functions(connection, add_watch, remove_watch, |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 553 | NULL, NULL, NULL); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 554 | dbus_error_init (&dbus_error); |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 555 | dbus_bus_request_name (connection, daemon->dbus_name, 0, &dbus_error); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 556 | if (dbus_error_is_set (&dbus_error)) |
| 557 | return (char *)dbus_error.message; |
| 558 | |
| 559 | if (!dbus_connection_register_object_path(connection, DNSMASQ_PATH, |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 560 | &dnsmasq_vtable, NULL)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 561 | return _("could not register a DBus message handler"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 562 | |
| 563 | daemon->dbus = connection; |
| 564 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 565 | if ((message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, "Up"))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 566 | { |
| 567 | dbus_connection_send(connection, message, NULL); |
| 568 | dbus_message_unref(message); |
| 569 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 570 | |
| 571 | return NULL; |
| 572 | } |
| 573 | |
| 574 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 575 | void set_dbus_listeners(int *maxfdp, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 576 | fd_set *rset, fd_set *wset, fd_set *eset) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 577 | { |
| 578 | struct watch *w; |
| 579 | |
| 580 | for (w = daemon->watches; w; w = w->next) |
| 581 | if (dbus_watch_get_enabled(w->watch)) |
| 582 | { |
| 583 | unsigned int flags = dbus_watch_get_flags(w->watch); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 584 | int fd = dbus_watch_get_unix_fd(w->watch); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 585 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 586 | bump_maxfd(fd, maxfdp); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 587 | |
| 588 | if (flags & DBUS_WATCH_READABLE) |
| 589 | FD_SET(fd, rset); |
| 590 | |
| 591 | if (flags & DBUS_WATCH_WRITABLE) |
| 592 | FD_SET(fd, wset); |
| 593 | |
| 594 | FD_SET(fd, eset); |
| 595 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 598 | 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] | 599 | { |
| 600 | DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 601 | struct watch *w; |
| 602 | |
| 603 | for (w = daemon->watches; w; w = w->next) |
| 604 | if (dbus_watch_get_enabled(w->watch)) |
| 605 | { |
| 606 | unsigned int flags = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 607 | int fd = dbus_watch_get_unix_fd(w->watch); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 608 | |
| 609 | if (FD_ISSET(fd, rset)) |
| 610 | flags |= DBUS_WATCH_READABLE; |
| 611 | |
| 612 | if (FD_ISSET(fd, wset)) |
| 613 | flags |= DBUS_WATCH_WRITABLE; |
| 614 | |
| 615 | if (FD_ISSET(fd, eset)) |
| 616 | flags |= DBUS_WATCH_ERROR; |
| 617 | |
| 618 | if (flags != 0) |
| 619 | dbus_watch_handle(w->watch, flags); |
| 620 | } |
| 621 | |
| 622 | if (connection) |
| 623 | { |
| 624 | dbus_connection_ref (connection); |
| 625 | while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS); |
| 626 | dbus_connection_unref (connection); |
| 627 | } |
| 628 | } |
| 629 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 630 | #ifdef HAVE_DHCP |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 631 | void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 632 | { |
| 633 | DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 634 | DBusMessage* message = NULL; |
| 635 | DBusMessageIter args; |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 636 | char *action_str, *mac = daemon->namebuff; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 637 | unsigned char *p; |
| 638 | int i; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 639 | |
| 640 | if (!connection) |
| 641 | return; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 642 | |
| 643 | if (!hostname) |
| 644 | hostname = ""; |
| 645 | |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 646 | #ifdef HAVE_DHCP6 |
| 647 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 648 | { |
| 649 | print_mac(mac, lease->clid, lease->clid_len); |
| 650 | inet_ntop(AF_INET6, lease->hwaddr, daemon->addrbuff, ADDRSTRLEN); |
| 651 | } |
| 652 | else |
| 653 | #endif |
| 654 | { |
| 655 | p = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len, |
| 656 | lease->hwaddr, lease->clid_len, lease->clid, &i); |
| 657 | print_mac(mac, p, i); |
| 658 | inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN); |
| 659 | } |
| 660 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 661 | if (action == ACTION_DEL) |
| 662 | action_str = "DhcpLeaseDeleted"; |
| 663 | else if (action == ACTION_ADD) |
| 664 | action_str = "DhcpLeaseAdded"; |
| 665 | else if (action == ACTION_OLD) |
| 666 | action_str = "DhcpLeaseUpdated"; |
| 667 | else |
| 668 | return; |
| 669 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 670 | 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] | 671 | return; |
| 672 | |
| 673 | dbus_message_iter_init_append(message, &args); |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 674 | |
| 675 | if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) && |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 676 | dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) && |
| 677 | dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname)) |
| 678 | dbus_connection_send(connection, message, NULL); |
| 679 | |
| 680 | dbus_message_unref(message); |
| 681 | } |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 682 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 683 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 684 | #endif |