Simon Kelley | d1ced3a | 2018-01-01 22:18:03 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2018 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" |
Simon Kelley | aaeea9f | 2014-08-12 18:30:44 +0100 | [diff] [blame] | 38 | #ifdef HAVE_LOOP |
| 39 | " <method name=\"GetLoopServers\">\n" |
| 40 | " <arg name=\"server\" direction=\"out\" type=\"as\"/>\n" |
| 41 | " </method>\n" |
| 42 | #endif |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 43 | " <method name=\"SetServers\">\n" |
| 44 | " <arg name=\"servers\" direction=\"in\" type=\"av\"/>\n" |
| 45 | " </method>\n" |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 46 | " <method name=\"SetDomainServers\">\n" |
| 47 | " <arg name=\"servers\" direction=\"in\" type=\"as\"/>\n" |
| 48 | " </method>\n" |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 49 | " <method name=\"SetServersEx\">\n" |
| 50 | " <arg name=\"servers\" direction=\"in\" type=\"aas\"/>\n" |
| 51 | " </method>\n" |
Daniel Collins | c4638f9 | 2014-06-07 21:21:44 +0100 | [diff] [blame] | 52 | " <method name=\"SetFilterWin2KOption\">\n" |
| 53 | " <arg name=\"filterwin2k\" direction=\"in\" type=\"b\"/>\n" |
| 54 | " </method>\n" |
| 55 | " <method name=\"SetBogusPrivOption\">\n" |
| 56 | " <arg name=\"boguspriv\" direction=\"in\" type=\"b\"/>\n" |
| 57 | " </method>\n" |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 58 | " <signal name=\"DhcpLeaseAdded\">\n" |
| 59 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 60 | " <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 61 | " <arg name=\"hostname\" type=\"s\"/>\n" |
| 62 | " </signal>\n" |
| 63 | " <signal name=\"DhcpLeaseDeleted\">\n" |
| 64 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 65 | " <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 66 | " <arg name=\"hostname\" type=\"s\"/>\n" |
| 67 | " </signal>\n" |
| 68 | " <signal name=\"DhcpLeaseUpdated\">\n" |
| 69 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 70 | " <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 71 | " <arg name=\"hostname\" type=\"s\"/>\n" |
| 72 | " </signal>\n" |
Nicolas Cavallari | c6d82c9 | 2015-06-09 20:42:20 +0100 | [diff] [blame] | 73 | #ifdef HAVE_DHCP |
| 74 | " <method name=\"AddDhcpLease\">\n" |
| 75 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 76 | " <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 77 | " <arg name=\"hostname\" type=\"ay\"/>\n" |
| 78 | " <arg name=\"clid\" type=\"ay\"/>\n" |
| 79 | " <arg name=\"lease_duration\" type=\"u\"/>\n" |
| 80 | " <arg name=\"ia_id\" type=\"u\"/>\n" |
| 81 | " <arg name=\"is_temporary\" type=\"b\"/>\n" |
| 82 | " </method>\n" |
| 83 | " <method name=\"DeleteDhcpLease\">\n" |
| 84 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 85 | " <arg name=\"success\" type=\"b\" direction=\"out\"/>\n" |
| 86 | " </method>\n" |
| 87 | #endif |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 88 | " </interface>\n" |
| 89 | "</node>\n"; |
| 90 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 91 | static char *introspection_xml = NULL; |
| 92 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 93 | struct watch { |
| 94 | DBusWatch *watch; |
| 95 | struct watch *next; |
| 96 | }; |
| 97 | |
| 98 | |
| 99 | static dbus_bool_t add_watch(DBusWatch *watch, void *data) |
| 100 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 101 | struct watch *w; |
| 102 | |
| 103 | for (w = daemon->watches; w; w = w->next) |
| 104 | if (w->watch == watch) |
| 105 | return TRUE; |
| 106 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 107 | if (!(w = whine_malloc(sizeof(struct watch)))) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 108 | return FALSE; |
| 109 | |
| 110 | w->watch = watch; |
| 111 | w->next = daemon->watches; |
| 112 | daemon->watches = w; |
| 113 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 114 | w = data; /* no warning */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 115 | return TRUE; |
| 116 | } |
| 117 | |
| 118 | static void remove_watch(DBusWatch *watch, void *data) |
| 119 | { |
Simon Kelley | 25c4198 | 2013-02-05 14:56:02 +0000 | [diff] [blame] | 120 | struct watch **up, *w, *tmp; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 121 | |
Simon Kelley | 25c4198 | 2013-02-05 14:56:02 +0000 | [diff] [blame] | 122 | for (up = &(daemon->watches), w = daemon->watches; w; w = tmp) |
| 123 | { |
| 124 | tmp = w->next; |
| 125 | if (w->watch == watch) |
| 126 | { |
| 127 | *up = tmp; |
| 128 | free(w); |
| 129 | } |
| 130 | else |
| 131 | up = &(w->next); |
| 132 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 133 | |
| 134 | w = data; /* no warning */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 137 | static void dbus_read_servers(DBusMessage *message) |
| 138 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 139 | DBusMessageIter iter; |
| 140 | union mysockaddr addr, source_addr; |
| 141 | char *domain; |
| 142 | |
| 143 | dbus_message_iter_init(message, &iter); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 144 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 145 | mark_servers(SERV_FROM_DBUS); |
| 146 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 147 | while (1) |
| 148 | { |
| 149 | int skip = 0; |
| 150 | |
| 151 | if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32) |
| 152 | { |
| 153 | u32 a; |
| 154 | |
| 155 | dbus_message_iter_get_basic(&iter, &a); |
| 156 | dbus_message_iter_next (&iter); |
| 157 | |
| 158 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 159 | source_addr.in.sin_len = addr.in.sin_len = sizeof(struct sockaddr_in); |
| 160 | #endif |
| 161 | addr.in.sin_addr.s_addr = ntohl(a); |
| 162 | source_addr.in.sin_family = addr.in.sin_family = AF_INET; |
| 163 | addr.in.sin_port = htons(NAMESERVER_PORT); |
| 164 | source_addr.in.sin_addr.s_addr = INADDR_ANY; |
| 165 | source_addr.in.sin_port = htons(daemon->query_port); |
| 166 | } |
| 167 | else if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_BYTE) |
| 168 | { |
| 169 | unsigned char p[sizeof(struct in6_addr)]; |
| 170 | unsigned int i; |
| 171 | |
| 172 | skip = 1; |
| 173 | |
| 174 | for(i = 0; i < sizeof(struct in6_addr); i++) |
| 175 | { |
| 176 | dbus_message_iter_get_basic(&iter, &p[i]); |
| 177 | dbus_message_iter_next (&iter); |
| 178 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BYTE) |
Simon Kelley | 006c162 | 2014-06-08 21:51:29 +0100 | [diff] [blame] | 179 | { |
| 180 | i++; |
| 181 | break; |
| 182 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | #ifndef HAVE_IPV6 |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 186 | 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] | 187 | #else |
Simon Kelley | 006c162 | 2014-06-08 21:51:29 +0100 | [diff] [blame] | 188 | if (i == sizeof(struct in6_addr)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 189 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 190 | memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr)); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 191 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 192 | 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] | 193 | #endif |
| 194 | source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6; |
| 195 | addr.in6.sin6_port = htons(NAMESERVER_PORT); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 196 | source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0; |
| 197 | source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 198 | source_addr.in6.sin6_addr = in6addr_any; |
| 199 | source_addr.in6.sin6_port = htons(daemon->query_port); |
| 200 | skip = 0; |
| 201 | } |
| 202 | #endif |
| 203 | } |
| 204 | else |
| 205 | /* At the end */ |
| 206 | break; |
| 207 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 208 | /* process each domain */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 209 | do { |
| 210 | if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) |
| 211 | { |
| 212 | dbus_message_iter_get_basic(&iter, &domain); |
| 213 | dbus_message_iter_next (&iter); |
| 214 | } |
| 215 | else |
| 216 | domain = NULL; |
| 217 | |
| 218 | if (!skip) |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 219 | add_update_server(SERV_FROM_DBUS, &addr, &source_addr, NULL, domain); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 220 | |
| 221 | } while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 222 | } |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 223 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 224 | /* unlink and free anything still marked. */ |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 225 | cleanup_servers(); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Simon Kelley | aaeea9f | 2014-08-12 18:30:44 +0100 | [diff] [blame] | 228 | #ifdef HAVE_LOOP |
| 229 | static DBusMessage *dbus_reply_server_loop(DBusMessage *message) |
| 230 | { |
| 231 | DBusMessageIter args, args_iter; |
| 232 | struct server *serv; |
| 233 | DBusMessage *reply = dbus_message_new_method_return(message); |
| 234 | |
| 235 | dbus_message_iter_init_append (reply, &args); |
| 236 | dbus_message_iter_open_container (&args, DBUS_TYPE_ARRAY,DBUS_TYPE_STRING_AS_STRING, &args_iter); |
| 237 | |
| 238 | for (serv = daemon->servers; serv; serv = serv->next) |
| 239 | if (serv->flags & SERV_LOOP) |
| 240 | { |
| 241 | prettyprint_addr(&serv->addr, daemon->addrbuff); |
| 242 | dbus_message_iter_append_basic (&args_iter, DBUS_TYPE_STRING, &daemon->addrbuff); |
| 243 | } |
| 244 | |
| 245 | dbus_message_iter_close_container (&args, &args_iter); |
| 246 | |
| 247 | return reply; |
| 248 | } |
| 249 | #endif |
| 250 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 251 | static DBusMessage* dbus_read_servers_ex(DBusMessage *message, int strings) |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 252 | { |
| 253 | DBusMessageIter iter, array_iter, string_iter; |
| 254 | DBusMessage *error = NULL; |
| 255 | const char *addr_err; |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 256 | char *dup = NULL; |
| 257 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 258 | if (!dbus_message_iter_init(message, &iter)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 259 | { |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 260 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 261 | "Failed to initialize dbus message iter"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 264 | /* check that the message contains an array of arrays */ |
| 265 | if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) || |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 266 | (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] | 267 | { |
| 268 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 269 | strings ? "Expected array of string" : "Expected array of string arrays"); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 272 | mark_servers(SERV_FROM_DBUS); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 273 | |
| 274 | /* array_iter points to each "as" element in the outer array */ |
| 275 | dbus_message_iter_recurse(&iter, &array_iter); |
| 276 | while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID) |
| 277 | { |
| 278 | const char *str = NULL; |
| 279 | union mysockaddr addr, source_addr; |
Simon Kelley | 7b1eae4 | 2014-02-20 13:43:28 +0000 | [diff] [blame] | 280 | int flags = 0; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 281 | char interface[IF_NAMESIZE]; |
Simon Kelley | d89fb4e | 2012-12-01 21:21:13 +0000 | [diff] [blame] | 282 | char *str_addr, *str_domain = NULL; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 283 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 284 | if (strings) |
| 285 | { |
| 286 | dbus_message_iter_get_basic(&array_iter, &str); |
| 287 | if (!str || !strlen (str)) |
| 288 | { |
| 289 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 290 | "Empty string"); |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | /* dup the string because it gets modified during parsing */ |
Simon Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 295 | if (dup) |
| 296 | free(dup); |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 297 | if (!(dup = str_domain = whine_malloc(strlen(str)+1))) |
| 298 | break; |
Simon Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 299 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 300 | strcpy(str_domain, str); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 301 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 302 | /* point to address part of old string for error message */ |
| 303 | if ((str_addr = strrchr(str, '/'))) |
| 304 | str = str_addr+1; |
| 305 | |
| 306 | if ((str_addr = strrchr(str_domain, '/'))) |
| 307 | { |
| 308 | if (*str_domain != '/' || str_addr == str_domain) |
| 309 | { |
| 310 | error = dbus_message_new_error_printf(message, |
| 311 | DBUS_ERROR_INVALID_ARGS, |
| 312 | "No domain terminator '%s'", |
| 313 | str); |
| 314 | break; |
| 315 | } |
| 316 | *str_addr++ = 0; |
| 317 | str_domain++; |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | str_addr = str_domain; |
| 322 | str_domain = NULL; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | /* check the types of the struct and its elements */ |
| 330 | if ((dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY) || |
| 331 | (dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_STRING)) |
| 332 | { |
| 333 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 334 | "Expected inner array of strings"); |
| 335 | break; |
| 336 | } |
| 337 | |
| 338 | /* string_iter points to each "s" element in the inner array */ |
| 339 | dbus_message_iter_recurse(&array_iter, &string_iter); |
| 340 | if (dbus_message_iter_get_arg_type(&string_iter) != DBUS_TYPE_STRING) |
| 341 | { |
| 342 | /* no IP address given */ |
| 343 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 344 | "Expected IP address"); |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | dbus_message_iter_get_basic(&string_iter, &str); |
| 349 | if (!str || !strlen (str)) |
| 350 | { |
| 351 | error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 352 | "Empty IP address"); |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | /* dup the string because it gets modified during parsing */ |
Simon Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 357 | if (dup) |
| 358 | free(dup); |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 359 | if (!(dup = str_addr = whine_malloc(strlen(str)+1))) |
| 360 | break; |
Simon Kelley | 9c4270b | 2013-02-04 22:07:57 +0000 | [diff] [blame] | 361 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 362 | strcpy(str_addr, str); |
| 363 | } |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 364 | |
| 365 | memset(&addr, 0, sizeof(addr)); |
| 366 | memset(&source_addr, 0, sizeof(source_addr)); |
| 367 | memset(&interface, 0, sizeof(interface)); |
| 368 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 369 | /* parse the IP address */ |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 370 | if ((addr_err = parse_server(str_addr, &addr, &source_addr, (char *) &interface, &flags))) |
| 371 | { |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 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 | } |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 377 | |
| 378 | /* 0.0.0.0 for server address == NULL, for Dbus */ |
| 379 | if (addr.in.sin_family == AF_INET && |
| 380 | addr.in.sin_addr.s_addr == 0) |
| 381 | flags |= SERV_NO_ADDR; |
| 382 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 383 | if (strings) |
| 384 | { |
| 385 | char *p; |
| 386 | |
| 387 | do { |
| 388 | if (str_domain) |
| 389 | { |
| 390 | if ((p = strchr(str_domain, '/'))) |
| 391 | *p++ = 0; |
| 392 | } |
| 393 | else |
| 394 | p = NULL; |
| 395 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 396 | add_update_server(flags | SERV_FROM_DBUS, &addr, &source_addr, interface, str_domain); |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 397 | } while ((str_domain = p)); |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | /* jump past the address to the domain list (if any) */ |
| 402 | dbus_message_iter_next (&string_iter); |
| 403 | |
| 404 | /* parse domains and add each server/domain pair to the list */ |
| 405 | do { |
| 406 | str = NULL; |
| 407 | if (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING) |
| 408 | dbus_message_iter_get_basic(&string_iter, &str); |
| 409 | dbus_message_iter_next (&string_iter); |
| 410 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 411 | add_update_server(flags | SERV_FROM_DBUS, &addr, &source_addr, interface, str); |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 412 | } while (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING); |
| 413 | } |
| 414 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 415 | /* jump to next element in outer array */ |
| 416 | dbus_message_iter_next(&array_iter); |
| 417 | } |
| 418 | |
Simon Kelley | d68c2ca | 2014-02-18 22:30:30 +0000 | [diff] [blame] | 419 | cleanup_servers(); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 420 | |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 421 | if (dup) |
| 422 | free(dup); |
| 423 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 424 | return error; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Daniel Collins | c4638f9 | 2014-06-07 21:21:44 +0100 | [diff] [blame] | 427 | static DBusMessage *dbus_set_bool(DBusMessage *message, int flag, char *name) |
| 428 | { |
| 429 | DBusMessageIter iter; |
| 430 | dbus_bool_t enabled; |
| 431 | |
| 432 | if (!dbus_message_iter_init(message, &iter) || dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) |
| 433 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, "Expected boolean argument"); |
| 434 | |
| 435 | dbus_message_iter_get_basic(&iter, &enabled); |
| 436 | |
| 437 | if (enabled) |
| 438 | { |
Simon Kelley | d91b1fd | 2015-06-09 20:45:07 +0100 | [diff] [blame] | 439 | my_syslog(LOG_INFO, _("Enabling --%s option from D-Bus"), name); |
Daniel Collins | c4638f9 | 2014-06-07 21:21:44 +0100 | [diff] [blame] | 440 | set_option_bool(flag); |
| 441 | } |
| 442 | else |
| 443 | { |
Simon Kelley | d91b1fd | 2015-06-09 20:45:07 +0100 | [diff] [blame] | 444 | my_syslog(LOG_INFO, _("Disabling --%s option from D-Bus"), name); |
Daniel Collins | c4638f9 | 2014-06-07 21:21:44 +0100 | [diff] [blame] | 445 | reset_option_bool(flag); |
| 446 | } |
| 447 | |
| 448 | return NULL; |
| 449 | } |
| 450 | |
Nicolas Cavallari | c6d82c9 | 2015-06-09 20:42:20 +0100 | [diff] [blame] | 451 | #ifdef HAVE_DHCP |
| 452 | static DBusMessage *dbus_add_lease(DBusMessage* message) |
| 453 | { |
| 454 | struct dhcp_lease *lease; |
| 455 | const char *ipaddr, *hwaddr, *hostname, *tmp; |
| 456 | const unsigned char* clid; |
| 457 | int clid_len, hostname_len, hw_len, hw_type; |
| 458 | dbus_uint32_t expires, ia_id; |
| 459 | dbus_bool_t is_temporary; |
| 460 | struct all_addr addr; |
| 461 | time_t now = dnsmasq_time(); |
| 462 | unsigned char dhcp_chaddr[DHCP_CHADDR_MAX]; |
| 463 | |
| 464 | DBusMessageIter iter, array_iter; |
| 465 | if (!dbus_message_iter_init(message, &iter)) |
| 466 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 467 | "Failed to initialize dbus message iter"); |
| 468 | |
| 469 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) |
| 470 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 471 | "Expected string as first argument"); |
| 472 | |
| 473 | dbus_message_iter_get_basic(&iter, &ipaddr); |
| 474 | dbus_message_iter_next(&iter); |
| 475 | |
| 476 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) |
| 477 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 478 | "Expected string as second argument"); |
| 479 | |
| 480 | dbus_message_iter_get_basic(&iter, &hwaddr); |
| 481 | dbus_message_iter_next(&iter); |
| 482 | |
| 483 | if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) || |
| 484 | (dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE)) |
| 485 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 486 | "Expected byte array as third argument"); |
| 487 | |
| 488 | dbus_message_iter_recurse(&iter, &array_iter); |
| 489 | dbus_message_iter_get_fixed_array(&array_iter, &hostname, &hostname_len); |
| 490 | tmp = memchr(hostname, '\0', hostname_len); |
| 491 | if (tmp) |
| 492 | { |
| 493 | if (tmp == &hostname[hostname_len - 1]) |
| 494 | hostname_len--; |
| 495 | else |
| 496 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 497 | "Hostname contains an embedded NUL character"); |
| 498 | } |
| 499 | dbus_message_iter_next(&iter); |
| 500 | |
| 501 | if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) || |
| 502 | (dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE)) |
| 503 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 504 | "Expected byte array as fourth argument"); |
| 505 | |
| 506 | dbus_message_iter_recurse(&iter, &array_iter); |
| 507 | dbus_message_iter_get_fixed_array(&array_iter, &clid, &clid_len); |
| 508 | dbus_message_iter_next(&iter); |
| 509 | |
| 510 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) |
| 511 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 512 | "Expected uint32 as fifth argument"); |
| 513 | |
| 514 | dbus_message_iter_get_basic(&iter, &expires); |
| 515 | dbus_message_iter_next(&iter); |
| 516 | |
| 517 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) |
| 518 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 519 | "Expected uint32 as sixth argument"); |
| 520 | |
| 521 | dbus_message_iter_get_basic(&iter, &ia_id); |
| 522 | dbus_message_iter_next(&iter); |
| 523 | |
| 524 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) |
| 525 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 526 | "Expected uint32 as sixth argument"); |
| 527 | |
| 528 | dbus_message_iter_get_basic(&iter, &is_temporary); |
| 529 | |
| 530 | if (inet_pton(AF_INET, ipaddr, &addr.addr.addr4)) |
| 531 | { |
| 532 | if (ia_id != 0 || is_temporary) |
| 533 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 534 | "ia_id and is_temporary must be zero for IPv4 lease"); |
| 535 | |
| 536 | if (!(lease = lease_find_by_addr(addr.addr.addr4))) |
| 537 | lease = lease4_allocate(addr.addr.addr4); |
| 538 | } |
| 539 | #ifdef HAVE_DHCP6 |
| 540 | else if (inet_pton(AF_INET6, ipaddr, &addr.addr.addr6)) |
| 541 | { |
| 542 | if (!(lease = lease6_find_by_addr(&addr.addr.addr6, 128, 0))) |
| 543 | lease = lease6_allocate(&addr.addr.addr6, |
| 544 | is_temporary ? LEASE_TA : LEASE_NA); |
| 545 | lease_set_iaid(lease, ia_id); |
| 546 | } |
| 547 | #endif |
| 548 | else |
| 549 | return dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 550 | "Invalid IP address '%s'", ipaddr); |
| 551 | |
Simon Kelley | 13dee6f | 2017-02-28 16:51:58 +0000 | [diff] [blame] | 552 | hw_len = parse_hex((char*)hwaddr, dhcp_chaddr, DHCP_CHADDR_MAX, NULL, &hw_type); |
Nicolas Cavallari | c6d82c9 | 2015-06-09 20:42:20 +0100 | [diff] [blame] | 553 | if (hw_type == 0 && hw_len != 0) |
| 554 | hw_type = ARPHRD_ETHER; |
Simon Kelley | 13dee6f | 2017-02-28 16:51:58 +0000 | [diff] [blame] | 555 | |
| 556 | lease_set_hwaddr(lease, dhcp_chaddr, clid, hw_len, hw_type, |
Nicolas Cavallari | c6d82c9 | 2015-06-09 20:42:20 +0100 | [diff] [blame] | 557 | clid_len, now, 0); |
| 558 | lease_set_expires(lease, expires, now); |
| 559 | if (hostname_len != 0) |
| 560 | lease_set_hostname(lease, hostname, 0, get_domain(lease->addr), NULL); |
Simon Kelley | 13dee6f | 2017-02-28 16:51:58 +0000 | [diff] [blame] | 561 | |
Nicolas Cavallari | c6d82c9 | 2015-06-09 20:42:20 +0100 | [diff] [blame] | 562 | lease_update_file(now); |
| 563 | lease_update_dns(0); |
| 564 | |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | static DBusMessage *dbus_del_lease(DBusMessage* message) |
| 569 | { |
| 570 | struct dhcp_lease *lease; |
| 571 | DBusMessageIter iter; |
| 572 | const char *ipaddr; |
| 573 | DBusMessage *reply; |
| 574 | struct all_addr addr; |
| 575 | dbus_bool_t ret = 1; |
| 576 | time_t now = dnsmasq_time(); |
| 577 | |
| 578 | if (!dbus_message_iter_init(message, &iter)) |
| 579 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 580 | "Failed to initialize dbus message iter"); |
| 581 | |
| 582 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) |
| 583 | return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 584 | "Expected string as first argument"); |
| 585 | |
| 586 | dbus_message_iter_get_basic(&iter, &ipaddr); |
| 587 | |
| 588 | if (inet_pton(AF_INET, ipaddr, &addr.addr.addr4)) |
| 589 | lease = lease_find_by_addr(addr.addr.addr4); |
| 590 | #ifdef HAVE_DHCP6 |
| 591 | else if (inet_pton(AF_INET6, ipaddr, &addr.addr.addr6)) |
| 592 | lease = lease6_find_by_addr(&addr.addr.addr6, 128, 0); |
| 593 | #endif |
| 594 | else |
| 595 | return dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 596 | "Invalid IP address '%s'", ipaddr); |
| 597 | |
| 598 | if (lease) |
| 599 | { |
| 600 | lease_prune(lease, now); |
| 601 | lease_update_file(now); |
| 602 | lease_update_dns(0); |
| 603 | } |
| 604 | else |
| 605 | ret = 0; |
| 606 | |
| 607 | if ((reply = dbus_message_new_method_return(message))) |
| 608 | dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &ret, |
| 609 | DBUS_TYPE_INVALID); |
| 610 | |
| 611 | |
| 612 | return reply; |
| 613 | } |
| 614 | #endif |
| 615 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 616 | DBusHandlerResult message_handler(DBusConnection *connection, |
| 617 | DBusMessage *message, |
| 618 | void *user_data) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 619 | { |
| 620 | char *method = (char *)dbus_message_get_member(message); |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 621 | DBusMessage *reply = NULL; |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 622 | int clear_cache = 0, new_servers = 0; |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 623 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 624 | if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) |
| 625 | { |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 626 | /* string length: "%s" provides space for termination zero */ |
| 627 | if (!introspection_xml && |
| 628 | (introspection_xml = whine_malloc(strlen(introspection_xml_template) + strlen(daemon->dbus_name)))) |
| 629 | sprintf(introspection_xml, introspection_xml_template, daemon->dbus_name); |
| 630 | |
| 631 | if (introspection_xml) |
| 632 | { |
| 633 | reply = dbus_message_new_method_return(message); |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 634 | 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] | 635 | } |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 636 | } |
| 637 | else if (strcmp(method, "GetVersion") == 0) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 638 | { |
| 639 | char *v = VERSION; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 640 | reply = dbus_message_new_method_return(message); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 641 | |
| 642 | dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 643 | } |
Simon Kelley | aaeea9f | 2014-08-12 18:30:44 +0100 | [diff] [blame] | 644 | #ifdef HAVE_LOOP |
| 645 | else if (strcmp(method, "GetLoopServers") == 0) |
| 646 | { |
| 647 | reply = dbus_reply_server_loop(message); |
| 648 | } |
| 649 | #endif |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 650 | else if (strcmp(method, "SetServers") == 0) |
| 651 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 652 | dbus_read_servers(message); |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 653 | new_servers = 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 654 | } |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 655 | else if (strcmp(method, "SetServersEx") == 0) |
| 656 | { |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 657 | reply = dbus_read_servers_ex(message, 0); |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 658 | new_servers = 1; |
Simon Kelley | 295a54e | 2012-12-01 21:02:15 +0000 | [diff] [blame] | 659 | } |
| 660 | else if (strcmp(method, "SetDomainServers") == 0) |
| 661 | { |
| 662 | reply = dbus_read_servers_ex(message, 1); |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 663 | new_servers = 1; |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 664 | } |
Daniel Collins | c4638f9 | 2014-06-07 21:21:44 +0100 | [diff] [blame] | 665 | else if (strcmp(method, "SetFilterWin2KOption") == 0) |
| 666 | { |
| 667 | reply = dbus_set_bool(message, OPT_FILTER, "filterwin2k"); |
| 668 | } |
| 669 | else if (strcmp(method, "SetBogusPrivOption") == 0) |
| 670 | { |
| 671 | reply = dbus_set_bool(message, OPT_BOGUSPRIV, "bogus-priv"); |
| 672 | } |
Nicolas Cavallari | c6d82c9 | 2015-06-09 20:42:20 +0100 | [diff] [blame] | 673 | #ifdef HAVE_DHCP |
| 674 | else if (strcmp(method, "AddDhcpLease") == 0) |
| 675 | { |
| 676 | reply = dbus_add_lease(message); |
| 677 | } |
| 678 | else if (strcmp(method, "DeleteDhcpLease") == 0) |
| 679 | { |
| 680 | reply = dbus_del_lease(message); |
| 681 | } |
| 682 | #endif |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 683 | else if (strcmp(method, "ClearCache") == 0) |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 684 | clear_cache = 1; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 685 | else |
| 686 | return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
Simon Kelley | d9fb0be | 2013-07-25 21:47:17 +0100 | [diff] [blame] | 687 | |
| 688 | if (new_servers) |
| 689 | { |
| 690 | my_syslog(LOG_INFO, _("setting upstream servers from DBus")); |
| 691 | check_servers(); |
| 692 | if (option_bool(OPT_RELOAD)) |
| 693 | clear_cache = 1; |
| 694 | } |
| 695 | |
| 696 | if (clear_cache) |
| 697 | clear_cache_and_reload(dnsmasq_time()); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 698 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 699 | method = user_data; /* no warning */ |
| 700 | |
Simon Kelley | faafb3f | 2012-09-20 14:17:39 +0100 | [diff] [blame] | 701 | /* If no reply or no error, return nothing */ |
| 702 | if (!reply) |
| 703 | reply = dbus_message_new_method_return(message); |
| 704 | |
| 705 | if (reply) |
| 706 | { |
| 707 | dbus_connection_send (connection, reply, NULL); |
| 708 | dbus_message_unref (reply); |
| 709 | } |
| 710 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 711 | return (DBUS_HANDLER_RESULT_HANDLED); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | |
| 715 | /* 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] | 716 | char *dbus_init(void) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 717 | { |
| 718 | DBusConnection *connection = NULL; |
| 719 | DBusObjectPathVTable dnsmasq_vtable = {NULL, &message_handler, NULL, NULL, NULL, NULL }; |
| 720 | DBusError dbus_error; |
| 721 | DBusMessage *message; |
| 722 | |
| 723 | dbus_error_init (&dbus_error); |
| 724 | if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error))) |
| 725 | return NULL; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 726 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 727 | dbus_connection_set_exit_on_disconnect(connection, FALSE); |
| 728 | dbus_connection_set_watch_functions(connection, add_watch, remove_watch, |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 729 | NULL, NULL, NULL); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 730 | dbus_error_init (&dbus_error); |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 731 | dbus_bus_request_name (connection, daemon->dbus_name, 0, &dbus_error); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 732 | if (dbus_error_is_set (&dbus_error)) |
| 733 | return (char *)dbus_error.message; |
| 734 | |
| 735 | if (!dbus_connection_register_object_path(connection, DNSMASQ_PATH, |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 736 | &dnsmasq_vtable, NULL)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 737 | return _("could not register a DBus message handler"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 738 | |
| 739 | daemon->dbus = connection; |
| 740 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 741 | if ((message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, "Up"))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 742 | { |
| 743 | dbus_connection_send(connection, message, NULL); |
| 744 | dbus_message_unref(message); |
| 745 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 746 | |
| 747 | return NULL; |
| 748 | } |
| 749 | |
| 750 | |
Simon Kelley | b842bc9 | 2015-07-12 21:09:11 +0100 | [diff] [blame] | 751 | void set_dbus_listeners(void) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 752 | { |
| 753 | struct watch *w; |
| 754 | |
| 755 | for (w = daemon->watches; w; w = w->next) |
| 756 | if (dbus_watch_get_enabled(w->watch)) |
| 757 | { |
| 758 | unsigned int flags = dbus_watch_get_flags(w->watch); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 759 | int fd = dbus_watch_get_unix_fd(w->watch); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 760 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 761 | if (flags & DBUS_WATCH_READABLE) |
Simon Kelley | b842bc9 | 2015-07-12 21:09:11 +0100 | [diff] [blame] | 762 | poll_listen(fd, POLLIN); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 763 | |
| 764 | if (flags & DBUS_WATCH_WRITABLE) |
Simon Kelley | b842bc9 | 2015-07-12 21:09:11 +0100 | [diff] [blame] | 765 | poll_listen(fd, POLLOUT); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 766 | |
Simon Kelley | b842bc9 | 2015-07-12 21:09:11 +0100 | [diff] [blame] | 767 | poll_listen(fd, POLLERR); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 768 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Simon Kelley | b842bc9 | 2015-07-12 21:09:11 +0100 | [diff] [blame] | 771 | void check_dbus_listeners() |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 772 | { |
| 773 | DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 774 | struct watch *w; |
| 775 | |
| 776 | for (w = daemon->watches; w; w = w->next) |
| 777 | if (dbus_watch_get_enabled(w->watch)) |
| 778 | { |
| 779 | unsigned int flags = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 780 | int fd = dbus_watch_get_unix_fd(w->watch); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 781 | |
Simon Kelley | b842bc9 | 2015-07-12 21:09:11 +0100 | [diff] [blame] | 782 | if (poll_check(fd, POLLIN)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 783 | flags |= DBUS_WATCH_READABLE; |
| 784 | |
Simon Kelley | b842bc9 | 2015-07-12 21:09:11 +0100 | [diff] [blame] | 785 | if (poll_check(fd, POLLOUT)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 786 | flags |= DBUS_WATCH_WRITABLE; |
| 787 | |
Simon Kelley | b842bc9 | 2015-07-12 21:09:11 +0100 | [diff] [blame] | 788 | if (poll_check(fd, POLLERR)) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 789 | flags |= DBUS_WATCH_ERROR; |
| 790 | |
| 791 | if (flags != 0) |
| 792 | dbus_watch_handle(w->watch, flags); |
| 793 | } |
| 794 | |
| 795 | if (connection) |
| 796 | { |
| 797 | dbus_connection_ref (connection); |
| 798 | while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS); |
| 799 | dbus_connection_unref (connection); |
| 800 | } |
| 801 | } |
| 802 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 803 | #ifdef HAVE_DHCP |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 804 | void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 805 | { |
| 806 | DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 807 | DBusMessage* message = NULL; |
| 808 | DBusMessageIter args; |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 809 | char *action_str, *mac = daemon->namebuff; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 810 | unsigned char *p; |
| 811 | int i; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 812 | |
| 813 | if (!connection) |
| 814 | return; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 815 | |
| 816 | if (!hostname) |
| 817 | hostname = ""; |
| 818 | |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 819 | #ifdef HAVE_DHCP6 |
| 820 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 821 | { |
| 822 | print_mac(mac, lease->clid, lease->clid_len); |
Simon Kelley | 2bb6f77 | 2014-08-06 10:16:32 +0100 | [diff] [blame] | 823 | inet_ntop(AF_INET6, &lease->addr6, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 824 | } |
| 825 | else |
| 826 | #endif |
| 827 | { |
| 828 | p = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len, |
| 829 | lease->hwaddr, lease->clid_len, lease->clid, &i); |
| 830 | print_mac(mac, p, i); |
| 831 | inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN); |
| 832 | } |
| 833 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 834 | if (action == ACTION_DEL) |
| 835 | action_str = "DhcpLeaseDeleted"; |
| 836 | else if (action == ACTION_ADD) |
| 837 | action_str = "DhcpLeaseAdded"; |
| 838 | else if (action == ACTION_OLD) |
| 839 | action_str = "DhcpLeaseUpdated"; |
| 840 | else |
| 841 | return; |
| 842 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 843 | 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] | 844 | return; |
| 845 | |
| 846 | dbus_message_iter_init_append(message, &args); |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 847 | |
| 848 | if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) && |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 849 | dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) && |
| 850 | dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname)) |
| 851 | dbus_connection_send(connection, message, NULL); |
| 852 | |
| 853 | dbus_message_unref(message); |
| 854 | } |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 855 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 856 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 857 | #endif |