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