| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 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 |
| 5 |
the Free Software Foundation; version 2 dated June, 1991, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 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. |
| 12 |
|
| 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/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "dnsmasq.h" |
| 18 |
|
| 19 |
#ifdef HAVE_DBUS |
| 20 |
|
| 21 |
#include <dbus/dbus.h> |
| 22 |
|
| 23 |
const char* introspection_xml_template = |
| 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" |
| 32 |
" <interface name=\"%s\">\n" |
| 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 |
#ifdef HAVE_LOOP |
| 39 |
" <method name=\"GetLoopServers\">\n" |
| 40 |
" <arg name=\"server\" direction=\"out\" type=\"as\"/>\n" |
| 41 |
" </method>\n" |
| 42 |
#endif |
| 43 |
" <method name=\"SetServers\">\n" |
| 44 |
" <arg name=\"servers\" direction=\"in\" type=\"av\"/>\n" |
| 45 |
" </method>\n" |
| 46 |
" <method name=\"SetDomainServers\">\n" |
| 47 |
" <arg name=\"servers\" direction=\"in\" type=\"as\"/>\n" |
| 48 |
" </method>\n" |
| 49 |
" <method name=\"SetServersEx\">\n" |
| 50 |
" <arg name=\"servers\" direction=\"in\" type=\"aas\"/>\n" |
| 51 |
" </method>\n" |
| 52 |
" <method name=\"SetFilterWin2KOption\">\n" |
| 53 |
" <arg name=\"filterwin2k\" direction=\"in\" type=\"b\"/>\n" |
| 54 |
" </method>\n" |
| 55 |
" <method name=\"SetFilterA\">\n" |
| 56 |
" <arg name=\"filter-a\" direction=\"in\" type=\"b\"/>\n" |
| 57 |
" </method>\n" |
| 58 |
" <method name=\"SetFilterAAAA\">\n" |
| 59 |
" <arg name=\"filter-aaaa\" direction=\"in\" type=\"b\"/>\n" |
| 60 |
" </method>\n" |
| 61 |
" <method name=\"SetLocaliseQueriesOption\">\n" |
| 62 |
" <arg name=\"localise-queries\" direction=\"in\" type=\"b\"/>\n" |
| 63 |
" </method>\n" |
| 64 |
" <method name=\"SetBogusPrivOption\">\n" |
| 65 |
" <arg name=\"boguspriv\" direction=\"in\" type=\"b\"/>\n" |
| 66 |
" </method>\n" |
| 67 |
" <signal name=\"DhcpLeaseAdded\">\n" |
| 68 |
" <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 69 |
" <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 70 |
" <arg name=\"hostname\" type=\"s\"/>\n" |
| 71 |
" </signal>\n" |
| 72 |
" <signal name=\"DhcpLeaseDeleted\">\n" |
| 73 |
" <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 74 |
" <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 75 |
" <arg name=\"hostname\" type=\"s\"/>\n" |
| 76 |
" </signal>\n" |
| 77 |
" <signal name=\"DhcpLeaseUpdated\">\n" |
| 78 |
" <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 79 |
" <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 80 |
" <arg name=\"hostname\" type=\"s\"/>\n" |
| 81 |
" </signal>\n" |
| 82 |
#ifdef HAVE_DHCP |
| 83 |
" <method name=\"AddDhcpLease\">\n" |
| 84 |
" <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 85 |
" <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 86 |
" <arg name=\"hostname\" type=\"ay\"/>\n" |
| 87 |
" <arg name=\"clid\" type=\"ay\"/>\n" |
| 88 |
" <arg name=\"lease_duration\" type=\"u\"/>\n" |
| 89 |
" <arg name=\"ia_id\" type=\"u\"/>\n" |
| 90 |
" <arg name=\"is_temporary\" type=\"b\"/>\n" |
| 91 |
" </method>\n" |
| 92 |
" <method name=\"DeleteDhcpLease\">\n" |
| 93 |
" <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 94 |
" <arg name=\"success\" type=\"b\" direction=\"out\"/>\n" |
| 95 |
" </method>\n" |
| 96 |
#endif |
| 97 |
" <method name=\"GetMetrics\">\n" |
| 98 |
" <arg name=\"metrics\" direction=\"out\" type=\"a{su}\"/>\n" |
| 99 |
" </method>\n" |
| 100 |
" <method name=\"GetServerMetrics\">\n" |
| 101 |
" <arg name=\"metrics\" direction=\"out\" type=\"a{ss}\"/>\n" |
| 102 |
" </method>\n" |
| 103 |
" <method name=\"ClearMetrics\">\n" |
| 104 |
" </method>\n" |
| 105 |
" </interface>\n" |
| 106 |
"</node>\n"; |
| 107 |
|
| 108 |
static char *introspection_xml = NULL; |
| 109 |
static int watches_modified = 0; |
| 110 |
|
| 111 |
struct watch { |
| 112 |
DBusWatch *watch; |
| 113 |
struct watch *next; |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
static dbus_bool_t add_watch(DBusWatch *watch, void *data) |
| 118 |
{ |
| 119 |
struct watch *w; |
| 120 |
|
| 121 |
for (w = daemon->watches; w; w = w->next) |
| 122 |
if (w->watch == watch) |
| 123 |
return TRUE; |
| 124 |
|
| 125 |
if (!(w = whine_malloc(sizeof(struct watch)))) |
| 126 |
return FALSE; |
| 127 |
|
| 128 |
w->watch = watch; |
| 129 |
w->next = daemon->watches; |
| 130 |
daemon->watches = w; |
| 131 |
watches_modified++; |
| 132 |
|
| 133 |
(void)data; /* no warning */ |
| 134 |
return TRUE; |
| 135 |
} |
| 136 |
|
| 137 |
static void remove_watch(DBusWatch *watch, void *data) |
| 138 |
{ |
| 139 |
struct watch **up, *w, *tmp; |
| 140 |
|
| 141 |
for (up = &(daemon->watches), w = daemon->watches; w; w = tmp) |
| 142 |
{ |
| 143 |
tmp = w->next; |
| 144 |
if (w->watch == watch) |
| 145 |
{ |
| 146 |
*up = tmp; |
| 147 |
free(w); |
| 148 |
watches_modified++; |
| 149 |
} |
| 150 |
else |
| 151 |
up = &(w->next); |
| 152 |
} |
| 153 |
|
| 154 |
(void)data; /* no warning */ |
| 155 |
} |
| 156 |
|
| 157 |
static DBusMessage* dbus_read_servers(DBusMessage *message) |
| 158 |
{ |
| 159 |
DBusMessageIter iter; |
| 160 |
union mysockaddr addr, source_addr; |
| 161 |
char *domain; |
| 162 |
|
| 163 |
if (!dbus_message_iter_init(message, &iter)) |
| 164 |
{ |
| 165 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 166 |
"Failed to initialize dbus message iter"); |
| 167 |
} |
| 168 |
|
| 169 |
mark_servers(SERV_FROM_DBUS); |
| 170 |
|
| 171 |
while (1) |
| 172 |
{ |
| 173 |
int skip = 0; |
| 174 |
|
| 175 |
if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32) |
| 176 |
{ |
| 177 |
u32 a; |
| 178 |
|
| 179 |
dbus_message_iter_get_basic(&iter, &a); |
| 180 |
dbus_message_iter_next (&iter); |
| 181 |
|
| 182 |
#ifdef HAVE_SOCKADDR_SA_LEN |
| 183 |
source_addr.in.sin_len = addr.in.sin_len = sizeof(struct sockaddr_in); |
| 184 |
#endif |
| 185 |
addr.in.sin_addr.s_addr = ntohl(a); |
| 186 |
source_addr.in.sin_family = addr.in.sin_family = AF_INET; |
| 187 |
addr.in.sin_port = htons(NAMESERVER_PORT); |
| 188 |
source_addr.in.sin_addr.s_addr = INADDR_ANY; |
| 189 |
source_addr.in.sin_port = htons(daemon->query_port); |
| 190 |
} |
| 191 |
else if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_BYTE) |
| 192 |
{ |
| 193 |
unsigned char p[sizeof(struct in6_addr)]; |
| 194 |
unsigned int i; |
| 195 |
|
| 196 |
skip = 1; |
| 197 |
|
| 198 |
for(i = 0; i < sizeof(struct in6_addr); i++) |
| 199 |
{ |
| 200 |
dbus_message_iter_get_basic(&iter, &p[i]); |
| 201 |
dbus_message_iter_next (&iter); |
| 202 |
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BYTE) |
| 203 |
{ |
| 204 |
i++; |
| 205 |
break; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
if (i == sizeof(struct in6_addr)) |
| 210 |
{ |
| 211 |
memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr)); |
| 212 |
#ifdef HAVE_SOCKADDR_SA_LEN |
| 213 |
source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(struct sockaddr_in6); |
| 214 |
#endif |
| 215 |
source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6; |
| 216 |
addr.in6.sin6_port = htons(NAMESERVER_PORT); |
| 217 |
source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0; |
| 218 |
source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0; |
| 219 |
source_addr.in6.sin6_addr = in6addr_any; |
| 220 |
source_addr.in6.sin6_port = htons(daemon->query_port); |
| 221 |
skip = 0; |
| 222 |
} |
| 223 |
} |
| 224 |
else |
| 225 |
/* At the end */ |
| 226 |
break; |
| 227 |
|
| 228 |
/* process each domain */ |
| 229 |
do { |
| 230 |
if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) |
| 231 |
{ |
| 232 |
dbus_message_iter_get_basic(&iter, &domain); |
| 233 |
dbus_message_iter_next (&iter); |
| 234 |
} |
| 235 |
else |
| 236 |
domain = NULL; |
| 237 |
|
| 238 |
if (!skip) |
| 239 |
add_update_server(SERV_FROM_DBUS, &addr, &source_addr, NULL, domain, NULL); |
| 240 |
|
| 241 |
} while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING); |
| 242 |
} |
| 243 |
|
| 244 |
/* unlink and free anything still marked. */ |
| 245 |
cleanup_servers(); |
| 246 |
return NULL; |
| 247 |
} |
| 248 |
|
| 249 |
#ifdef HAVE_LOOP |
| 250 |
static DBusMessage *dbus_reply_server_loop(DBusMessage *message) |
| 251 |
{ |
| 252 |
DBusMessageIter args, args_iter; |
| 253 |
struct server *serv; |
| 254 |
DBusMessage *reply = dbus_message_new_method_return(message); |
| 255 |
|
| 256 |
dbus_message_iter_init_append (reply, &args); |
| 257 |
dbus_message_iter_open_container (&args, DBUS_TYPE_ARRAY,DBUS_TYPE_STRING_AS_STRING, &args_iter); |
| 258 |
|
| 259 |
for (serv = daemon->servers; serv; serv = serv->next) |
| 260 |
if (serv->flags & SERV_LOOP) |
| 261 |
{ |
| 262 |
(void)prettyprint_addr(&serv->addr, daemon->addrbuff); |
| 263 |
dbus_message_iter_append_basic (&args_iter, DBUS_TYPE_STRING, &daemon->addrbuff); |
| 264 |
} |
| 265 |
|
| 266 |
dbus_message_iter_close_container (&args, &args_iter); |
| 267 |
|
| 268 |
return reply; |
| 269 |
} |
| 270 |
#endif |
| 271 |
|
| 272 |
static DBusMessage* dbus_read_servers_ex(DBusMessage *message, int strings) |
| 273 |
{ |
| 274 |
DBusMessageIter iter, array_iter, string_iter; |
| 275 |
DBusMessage *error = NULL; |
| 276 |
const char *addr_err; |
| 277 |
char *dup = NULL; |
| 278 |
|
| 279 |
if (!dbus_message_iter_init(message, &iter)) |
| 280 |
{ |
| 281 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 282 |
"Failed to initialize dbus message iter"); |
| 283 |
} |
| 284 |
|
| 285 |
/* check that the message contains an array of arrays */ |
| 286 |
if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) || |
| 287 |
(dbus_message_iter_get_element_type(&iter) != (strings ? DBUS_TYPE_STRING : DBUS_TYPE_ARRAY))) |
| 288 |
{ |
| 289 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 290 |
strings ? "Expected array of string" : "Expected array of string arrays"); |
| 291 |
} |
| 292 |
|
| 293 |
mark_servers(SERV_FROM_DBUS); |
| 294 |
|
| 295 |
/* array_iter points to each "as" element in the outer array */ |
| 296 |
dbus_message_iter_recurse(&iter, &array_iter); |
| 297 |
while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID) |
| 298 |
{ |
| 299 |
const char *str = NULL; |
| 300 |
union mysockaddr addr, source_addr; |
| 301 |
u16 flags = 0; |
| 302 |
char interface[IF_NAMESIZE]; |
| 303 |
char *str_addr, *str_domain = NULL; |
| 304 |
struct server_details sdetails = { 0 }; |
| 305 |
sdetails.addr = &addr; |
| 306 |
sdetails.source_addr = &source_addr; |
| 307 |
sdetails.interface = interface; |
| 308 |
sdetails.flags = &flags; |
| 309 |
|
| 310 |
if (strings) |
| 311 |
{ |
| 312 |
dbus_message_iter_get_basic(&array_iter, &str); |
| 313 |
if (!str || !strlen (str)) |
| 314 |
{ |
| 315 |
error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 316 |
"Empty string"); |
| 317 |
break; |
| 318 |
} |
| 319 |
|
| 320 |
/* dup the string because it gets modified during parsing */ |
| 321 |
if (dup) |
| 322 |
free(dup); |
| 323 |
if (!(dup = str_domain = whine_malloc(strlen(str)+1))) |
| 324 |
break; |
| 325 |
|
| 326 |
strcpy(str_domain, str); |
| 327 |
|
| 328 |
/* point to address part of old string for error message */ |
| 329 |
if ((str_addr = strrchr(str, '/'))) |
| 330 |
str = str_addr+1; |
| 331 |
|
| 332 |
if ((str_addr = strrchr(str_domain, '/'))) |
| 333 |
{ |
| 334 |
if (*str_domain != '/' || str_addr == str_domain) |
| 335 |
{ |
| 336 |
error = dbus_message_new_error_printf(message, |
| 337 |
DBUS_ERROR_INVALID_ARGS, |
| 338 |
"No domain terminator '%s'", |
| 339 |
str); |
| 340 |
break; |
| 341 |
} |
| 342 |
*str_addr++ = 0; |
| 343 |
str_domain++; |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
str_addr = str_domain; |
| 348 |
str_domain = NULL; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
} |
| 353 |
else |
| 354 |
{ |
| 355 |
/* check the types of the struct and its elements */ |
| 356 |
if ((dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY) || |
| 357 |
(dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_STRING)) |
| 358 |
{ |
| 359 |
error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 360 |
"Expected inner array of strings"); |
| 361 |
break; |
| 362 |
} |
| 363 |
|
| 364 |
/* string_iter points to each "s" element in the inner array */ |
| 365 |
dbus_message_iter_recurse(&array_iter, &string_iter); |
| 366 |
if (dbus_message_iter_get_arg_type(&string_iter) != DBUS_TYPE_STRING) |
| 367 |
{ |
| 368 |
/* no IP address given */ |
| 369 |
error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 370 |
"Expected IP address"); |
| 371 |
break; |
| 372 |
} |
| 373 |
|
| 374 |
dbus_message_iter_get_basic(&string_iter, &str); |
| 375 |
if (!str || !strlen (str)) |
| 376 |
{ |
| 377 |
error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 378 |
"Empty IP address"); |
| 379 |
break; |
| 380 |
} |
| 381 |
|
| 382 |
/* dup the string because it gets modified during parsing */ |
| 383 |
if (dup) |
| 384 |
free(dup); |
| 385 |
if (!(dup = str_addr = whine_malloc(strlen(str)+1))) |
| 386 |
break; |
| 387 |
|
| 388 |
strcpy(str_addr, str); |
| 389 |
} |
| 390 |
|
| 391 |
if (strings) |
| 392 |
{ |
| 393 |
char *p; |
| 394 |
|
| 395 |
do { |
| 396 |
if (str_domain) |
| 397 |
{ |
| 398 |
if ((p = strchr(str_domain, '/'))) |
| 399 |
*p++ = 0; |
| 400 |
} |
| 401 |
else |
| 402 |
p = NULL; |
| 403 |
|
| 404 |
if (strings && strlen(str_addr) == 0) |
| 405 |
add_update_server(SERV_LITERAL_ADDRESS | SERV_FROM_DBUS, &addr, &source_addr, interface, str_domain, NULL); |
| 406 |
else |
| 407 |
{ |
| 408 |
if ((addr_err = parse_server(str_addr, &sdetails))) |
| 409 |
{ |
| 410 |
error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 411 |
"Invalid IP address '%s': %s", |
| 412 |
str, addr_err); |
| 413 |
break; |
| 414 |
} |
| 415 |
|
| 416 |
while (parse_server_next(&sdetails)) |
| 417 |
{ |
| 418 |
if ((addr_err = parse_server_addr(&sdetails))) |
| 419 |
{ |
| 420 |
error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 421 |
"Invalid IP address '%s': %s", |
| 422 |
str, addr_err); |
| 423 |
break; |
| 424 |
} |
| 425 |
|
| 426 |
add_update_server(flags | SERV_FROM_DBUS, &addr, &source_addr, interface, str_domain, NULL); |
| 427 |
} |
| 428 |
} |
| 429 |
} while ((str_domain = p)); |
| 430 |
} |
| 431 |
else |
| 432 |
{ |
| 433 |
/* jump past the address to the domain list (if any) */ |
| 434 |
dbus_message_iter_next (&string_iter); |
| 435 |
|
| 436 |
/* parse domains and add each server/domain pair to the list */ |
| 437 |
do { |
| 438 |
str = NULL; |
| 439 |
if (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING) |
| 440 |
dbus_message_iter_get_basic(&string_iter, &str); |
| 441 |
dbus_message_iter_next (&string_iter); |
| 442 |
|
| 443 |
if ((addr_err = parse_server(str_addr, &sdetails))) |
| 444 |
{ |
| 445 |
error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 446 |
"Invalid IP address '%s': %s", |
| 447 |
str, addr_err); |
| 448 |
break; |
| 449 |
} |
| 450 |
|
| 451 |
while (parse_server_next(&sdetails)) |
| 452 |
{ |
| 453 |
if ((addr_err = parse_server_addr(&sdetails))) |
| 454 |
{ |
| 455 |
error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 456 |
"Invalid IP address '%s': %s", |
| 457 |
str, addr_err); |
| 458 |
break; |
| 459 |
} |
| 460 |
|
| 461 |
/* 0.0.0.0 for server address == NULL, for Dbus */ |
| 462 |
if (addr.in.sin_family == AF_INET && |
| 463 |
addr.in.sin_addr.s_addr == 0) |
| 464 |
flags |= SERV_LITERAL_ADDRESS; |
| 465 |
else |
| 466 |
flags &= ~SERV_LITERAL_ADDRESS; |
| 467 |
|
| 468 |
add_update_server(flags | SERV_FROM_DBUS, &addr, &source_addr, interface, str, NULL); |
| 469 |
} |
| 470 |
} while (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING); |
| 471 |
} |
| 472 |
|
| 473 |
if (sdetails.orig_hostinfo) |
| 474 |
freeaddrinfo(sdetails.orig_hostinfo); |
| 475 |
|
| 476 |
/* jump to next element in outer array */ |
| 477 |
dbus_message_iter_next(&array_iter); |
| 478 |
} |
| 479 |
|
| 480 |
cleanup_servers(); |
| 481 |
|
| 482 |
if (dup) |
| 483 |
free(dup); |
| 484 |
|
| 485 |
return error; |
| 486 |
} |
| 487 |
|
| 488 |
static DBusMessage *dbus_get_bool(DBusMessage *message, dbus_bool_t *enabled, char *name) |
| 489 |
{ |
| 490 |
DBusMessageIter iter; |
| 491 |
|
| 492 |
if (!dbus_message_iter_init(message, &iter) || dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) |
| 493 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, "Expected boolean argument"); |
| 494 |
|
| 495 |
dbus_message_iter_get_basic(&iter, enabled); |
| 496 |
|
| 497 |
if (*enabled) |
| 498 |
my_syslog(LOG_INFO, _("Enabling --%s option from D-Bus"), name); |
| 499 |
else |
| 500 |
my_syslog(LOG_INFO, _("Disabling --%s option from D-Bus"), name); |
| 501 |
|
| 502 |
return NULL; |
| 503 |
} |
| 504 |
|
| 505 |
static DBusMessage *dbus_set_bool(DBusMessage *message, int flag, char *name) |
| 506 |
{ |
| 507 |
dbus_bool_t val; |
| 508 |
DBusMessage *reply = dbus_get_bool(message, &val, name); |
| 509 |
|
| 510 |
if (!reply) |
| 511 |
{ |
| 512 |
if (val) |
| 513 |
set_option_bool(flag); |
| 514 |
else |
| 515 |
reset_option_bool(flag); |
| 516 |
} |
| 517 |
|
| 518 |
return reply; |
| 519 |
} |
| 520 |
|
| 521 |
#ifdef HAVE_DHCP |
| 522 |
static DBusMessage *dbus_add_lease(DBusMessage* message) |
| 523 |
{ |
| 524 |
struct dhcp_lease *lease; |
| 525 |
const char *ipaddr, *hwaddr, *hostname, *tmp; |
| 526 |
const unsigned char* clid; |
| 527 |
int clid_len, hostname_len, hw_len, hw_type; |
| 528 |
dbus_uint32_t expires, ia_id; |
| 529 |
dbus_bool_t is_temporary; |
| 530 |
union all_addr addr; |
| 531 |
time_t now = dnsmasq_time(); |
| 532 |
unsigned char dhcp_chaddr[DHCP_CHADDR_MAX]; |
| 533 |
DBusMessageIter iter, array_iter; |
| 534 |
|
| 535 |
if (!dbus_message_iter_init(message, &iter)) |
| 536 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 537 |
"Failed to initialize dbus message iter"); |
| 538 |
|
| 539 |
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) |
| 540 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 541 |
"Expected string as first argument"); |
| 542 |
|
| 543 |
dbus_message_iter_get_basic(&iter, &ipaddr); |
| 544 |
dbus_message_iter_next(&iter); |
| 545 |
|
| 546 |
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) |
| 547 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 548 |
"Expected string as second argument"); |
| 549 |
|
| 550 |
dbus_message_iter_get_basic(&iter, &hwaddr); |
| 551 |
dbus_message_iter_next(&iter); |
| 552 |
|
| 553 |
if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) || |
| 554 |
(dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE)) |
| 555 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 556 |
"Expected byte array as third argument"); |
| 557 |
|
| 558 |
dbus_message_iter_recurse(&iter, &array_iter); |
| 559 |
dbus_message_iter_get_fixed_array(&array_iter, &hostname, &hostname_len); |
| 560 |
tmp = memchr(hostname, '\0', hostname_len); |
| 561 |
if (tmp) |
| 562 |
{ |
| 563 |
if (tmp == &hostname[hostname_len - 1]) |
| 564 |
hostname_len--; |
| 565 |
else |
| 566 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 567 |
"Hostname contains an embedded NUL character"); |
| 568 |
} |
| 569 |
dbus_message_iter_next(&iter); |
| 570 |
|
| 571 |
if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) || |
| 572 |
(dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE)) |
| 573 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 574 |
"Expected byte array as fourth argument"); |
| 575 |
|
| 576 |
dbus_message_iter_recurse(&iter, &array_iter); |
| 577 |
dbus_message_iter_get_fixed_array(&array_iter, &clid, &clid_len); |
| 578 |
dbus_message_iter_next(&iter); |
| 579 |
|
| 580 |
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) |
| 581 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 582 |
"Expected uint32 as fifth argument"); |
| 583 |
|
| 584 |
dbus_message_iter_get_basic(&iter, &expires); |
| 585 |
dbus_message_iter_next(&iter); |
| 586 |
|
| 587 |
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) |
| 588 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 589 |
"Expected uint32 as sixth argument"); |
| 590 |
|
| 591 |
dbus_message_iter_get_basic(&iter, &ia_id); |
| 592 |
dbus_message_iter_next(&iter); |
| 593 |
|
| 594 |
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) |
| 595 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 596 |
"Expected uint32 as sixth argument"); |
| 597 |
|
| 598 |
dbus_message_iter_get_basic(&iter, &is_temporary); |
| 599 |
|
| 600 |
if (inet_pton(AF_INET, ipaddr, &addr.addr4)) |
| 601 |
{ |
| 602 |
if (!daemon->dhcp) |
| 603 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 604 |
"DHCPv4 not configured"); |
| 605 |
|
| 606 |
if (ia_id != 0 || is_temporary) |
| 607 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 608 |
"ia_id and is_temporary must be zero for IPv4 lease"); |
| 609 |
|
| 610 |
if (!(lease = lease_find_by_addr(addr.addr4))) |
| 611 |
lease = lease4_allocate(addr.addr4); |
| 612 |
} |
| 613 |
#ifdef HAVE_DHCP6 |
| 614 |
else if (inet_pton(AF_INET6, ipaddr, &addr.addr6)) |
| 615 |
{ |
| 616 |
if (!daemon->doing_dhcp6) |
| 617 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 618 |
"DHCPv6 not configured"); |
| 619 |
|
| 620 |
if (!(lease = lease6_find_by_addr(&addr.addr6, 128, 0))) |
| 621 |
lease = lease6_allocate(&addr.addr6, |
| 622 |
is_temporary ? LEASE_TA : LEASE_NA); |
| 623 |
if (lease) |
| 624 |
lease_set_iaid(lease, ia_id); |
| 625 |
} |
| 626 |
#endif |
| 627 |
else |
| 628 |
return dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 629 |
"Invalid IP address '%s'", ipaddr); |
| 630 |
|
| 631 |
if (!lease) |
| 632 |
return dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 633 |
"unable to allocate lease for IP address '%s'", ipaddr); |
| 634 |
|
| 635 |
hw_len = parse_hex((char*)hwaddr, dhcp_chaddr, DHCP_CHADDR_MAX, NULL, &hw_type); |
| 636 |
if (hw_len < 0) |
| 637 |
return dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 638 |
"Invalid HW address '%s'", hwaddr); |
| 639 |
|
| 640 |
if (hw_type == 0 && hw_len != 0) |
| 641 |
hw_type = ARPHRD_ETHER; |
| 642 |
|
| 643 |
lease_set_hwaddr(lease, dhcp_chaddr, clid, hw_len, hw_type, |
| 644 |
clid_len, now, 0); |
| 645 |
lease_set_expires(lease, expires, now); |
| 646 |
if (hostname_len != 0) |
| 647 |
lease_set_hostname(lease, hostname, 0, get_domain(lease->addr), NULL); |
| 648 |
|
| 649 |
lease_update_file(now); |
| 650 |
lease_update_dns(0); |
| 651 |
|
| 652 |
return NULL; |
| 653 |
} |
| 654 |
|
| 655 |
static DBusMessage *dbus_del_lease(DBusMessage* message) |
| 656 |
{ |
| 657 |
struct dhcp_lease *lease = NULL; |
| 658 |
DBusMessageIter iter; |
| 659 |
const char *ipaddr; |
| 660 |
DBusMessage *reply; |
| 661 |
union all_addr addr; |
| 662 |
dbus_bool_t ret = 1; |
| 663 |
time_t now = dnsmasq_time(); |
| 664 |
|
| 665 |
if (!dbus_message_iter_init(message, &iter)) |
| 666 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 667 |
"Failed to initialize dbus message iter"); |
| 668 |
|
| 669 |
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING) |
| 670 |
return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS, |
| 671 |
"Expected string as first argument"); |
| 672 |
|
| 673 |
dbus_message_iter_get_basic(&iter, &ipaddr); |
| 674 |
|
| 675 |
if (inet_pton(AF_INET, ipaddr, &addr.addr4) && daemon->dhcp) |
| 676 |
lease = lease_find_by_addr(addr.addr4); |
| 677 |
#ifdef HAVE_DHCP6 |
| 678 |
else if (inet_pton(AF_INET6, ipaddr, &addr.addr6) && daemon->doing_dhcp6) |
| 679 |
lease = lease6_find_by_addr(&addr.addr6, 128, 0); |
| 680 |
#endif |
| 681 |
else |
| 682 |
return dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, |
| 683 |
"Invalid IP address '%s'", ipaddr); |
| 684 |
|
| 685 |
if (lease) |
| 686 |
{ |
| 687 |
lease_prune(lease, now); |
| 688 |
lease_update_file(now); |
| 689 |
lease_update_dns(0); |
| 690 |
} |
| 691 |
else |
| 692 |
ret = 0; |
| 693 |
|
| 694 |
if ((reply = dbus_message_new_method_return(message))) |
| 695 |
dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &ret, |
| 696 |
DBUS_TYPE_INVALID); |
| 697 |
|
| 698 |
|
| 699 |
return reply; |
| 700 |
} |
| 701 |
#endif |
| 702 |
|
| 703 |
static DBusMessage *dbus_get_metrics(DBusMessage* message) |
| 704 |
{ |
| 705 |
DBusMessage *reply = dbus_message_new_method_return(message); |
| 706 |
DBusMessageIter array, dict, iter; |
| 707 |
int i; |
| 708 |
|
| 709 |
dbus_message_iter_init_append(reply, &iter); |
| 710 |
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{su}", &array); |
| 711 |
|
| 712 |
for (i = 0; i < __METRIC_MAX; i++) { |
| 713 |
const char *key = get_metric_name(i); |
| 714 |
dbus_uint32_t value = daemon->metrics[i]; |
| 715 |
|
| 716 |
dbus_message_iter_open_container(&array, DBUS_TYPE_DICT_ENTRY, NULL, &dict); |
| 717 |
dbus_message_iter_append_basic(&dict, DBUS_TYPE_STRING, &key); |
| 718 |
dbus_message_iter_append_basic(&dict, DBUS_TYPE_UINT32, &value); |
| 719 |
dbus_message_iter_close_container(&array, &dict); |
| 720 |
} |
| 721 |
|
| 722 |
dbus_message_iter_close_container(&iter, &array); |
| 723 |
|
| 724 |
return reply; |
| 725 |
} |
| 726 |
|
| 727 |
static void add_dict_entry(DBusMessageIter *container, const char *key, const char *val) |
| 728 |
{ |
| 729 |
DBusMessageIter dict; |
| 730 |
|
| 731 |
dbus_message_iter_open_container(container, DBUS_TYPE_DICT_ENTRY, NULL, &dict); |
| 732 |
dbus_message_iter_append_basic(&dict, DBUS_TYPE_STRING, &key); |
| 733 |
dbus_message_iter_append_basic(&dict, DBUS_TYPE_STRING, &val); |
| 734 |
dbus_message_iter_close_container(container, &dict); |
| 735 |
} |
| 736 |
|
| 737 |
static void add_dict_int(DBusMessageIter *container, const char *key, const unsigned int val) |
| 738 |
{ |
| 739 |
snprintf(daemon->namebuff, MAXDNAMESTR, "%u", val); |
| 740 |
|
| 741 |
add_dict_entry(container, key, daemon->namebuff); |
| 742 |
} |
| 743 |
|
| 744 |
static DBusMessage *dbus_get_server_metrics(DBusMessage* message) |
| 745 |
{ |
| 746 |
DBusMessage *reply = dbus_message_new_method_return(message); |
| 747 |
DBusMessageIter server_array, dict_array, server_iter; |
| 748 |
struct server *serv; |
| 749 |
|
| 750 |
dbus_message_iter_init_append(reply, &server_iter); |
| 751 |
dbus_message_iter_open_container(&server_iter, DBUS_TYPE_ARRAY, "a{ss}", &server_array); |
| 752 |
|
| 753 |
/* sum counts from different records for same server */ |
| 754 |
for (serv = daemon->servers; serv; serv = serv->next) |
| 755 |
serv->flags &= ~SERV_MARK; |
| 756 |
|
| 757 |
for (serv = daemon->servers; serv; serv = serv->next) |
| 758 |
if (!(serv->flags & SERV_MARK)) |
| 759 |
{ |
| 760 |
unsigned int port; |
| 761 |
unsigned int queries = 0, failed_queries = 0, nxdomain_replies = 0, retrys = 0; |
| 762 |
unsigned int sigma_latency = 0, count_latency = 0; |
| 763 |
|
| 764 |
struct server *serv1; |
| 765 |
|
| 766 |
for (serv1 = serv; serv1; serv1 = serv1->next) |
| 767 |
if (!(serv1->flags & SERV_MARK) && sockaddr_isequal(&serv->addr, &serv1->addr)) |
| 768 |
{ |
| 769 |
serv1->flags |= SERV_MARK; |
| 770 |
queries += serv1->queries; |
| 771 |
failed_queries += serv1->failed_queries; |
| 772 |
nxdomain_replies += serv1->nxdomain_replies; |
| 773 |
retrys += serv1->retrys; |
| 774 |
sigma_latency += serv1->query_latency; |
| 775 |
count_latency++; |
| 776 |
} |
| 777 |
|
| 778 |
dbus_message_iter_open_container(&server_array, DBUS_TYPE_ARRAY, "{ss}", &dict_array); |
| 779 |
|
| 780 |
port = prettyprint_addr(&serv->addr, daemon->namebuff); |
| 781 |
add_dict_entry(&dict_array, "address", daemon->namebuff); |
| 782 |
|
| 783 |
add_dict_int(&dict_array, "port", port); |
| 784 |
add_dict_int(&dict_array, "queries", queries); |
| 785 |
add_dict_int(&dict_array, "failed_queries", failed_queries); |
| 786 |
add_dict_int(&dict_array, "nxdomain", nxdomain_replies); |
| 787 |
add_dict_int(&dict_array, "retries", retrys); |
| 788 |
add_dict_int(&dict_array, "latency", sigma_latency/count_latency); |
| 789 |
|
| 790 |
dbus_message_iter_close_container(&server_array, &dict_array); |
| 791 |
} |
| 792 |
|
| 793 |
dbus_message_iter_close_container(&server_iter, &server_array); |
| 794 |
|
| 795 |
return reply; |
| 796 |
} |
| 797 |
|
| 798 |
DBusHandlerResult message_handler(DBusConnection *connection, |
| 799 |
DBusMessage *message, |
| 800 |
void *user_data) |
| 801 |
{ |
| 802 |
char *method = (char *)dbus_message_get_member(message); |
| 803 |
DBusMessage *reply = NULL; |
| 804 |
int clear_cache = 0, new_servers = 0; |
| 805 |
|
| 806 |
if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) |
| 807 |
{ |
| 808 |
/* string length: "%s" provides space for termination zero */ |
| 809 |
if (!introspection_xml && |
| 810 |
(introspection_xml = whine_malloc(strlen(introspection_xml_template) + strlen(daemon->dbus_name)))) |
| 811 |
sprintf(introspection_xml, introspection_xml_template, daemon->dbus_name); |
| 812 |
|
| 813 |
if (introspection_xml) |
| 814 |
{ |
| 815 |
reply = dbus_message_new_method_return(message); |
| 816 |
dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_xml, DBUS_TYPE_INVALID); |
| 817 |
} |
| 818 |
} |
| 819 |
else if (strcmp(method, "GetVersion") == 0) |
| 820 |
{ |
| 821 |
char *v = VERSION; |
| 822 |
reply = dbus_message_new_method_return(message); |
| 823 |
|
| 824 |
dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID); |
| 825 |
} |
| 826 |
#ifdef HAVE_LOOP |
| 827 |
else if (strcmp(method, "GetLoopServers") == 0) |
| 828 |
{ |
| 829 |
reply = dbus_reply_server_loop(message); |
| 830 |
} |
| 831 |
#endif |
| 832 |
else if (strcmp(method, "SetServers") == 0) |
| 833 |
{ |
| 834 |
reply = dbus_read_servers(message); |
| 835 |
new_servers = 1; |
| 836 |
} |
| 837 |
else if (strcmp(method, "SetServersEx") == 0) |
| 838 |
{ |
| 839 |
reply = dbus_read_servers_ex(message, 0); |
| 840 |
new_servers = 1; |
| 841 |
} |
| 842 |
else if (strcmp(method, "SetDomainServers") == 0) |
| 843 |
{ |
| 844 |
reply = dbus_read_servers_ex(message, 1); |
| 845 |
new_servers = 1; |
| 846 |
} |
| 847 |
else if (strcmp(method, "SetFilterWin2KOption") == 0) |
| 848 |
{ |
| 849 |
reply = dbus_set_bool(message, OPT_FILTER, "filterwin2k"); |
| 850 |
} |
| 851 |
else if (strcmp(method, "SetFilterA") == 0) |
| 852 |
{ |
| 853 |
static int done = 0; |
| 854 |
static struct rrlist list = { 0, NULL }; |
| 855 |
dbus_bool_t enabled; |
| 856 |
|
| 857 |
if (!(reply = dbus_get_bool(message, &enabled, "filter-A"))) |
| 858 |
{ |
| 859 |
if (!done) |
| 860 |
{ |
| 861 |
done = 1; |
| 862 |
list.next = daemon->filter_rr; |
| 863 |
daemon->filter_rr = &list; |
| 864 |
} |
| 865 |
|
| 866 |
list.rr = enabled ? T_A : 0; |
| 867 |
} |
| 868 |
} |
| 869 |
else if (strcmp(method, "SetFilterAAAA") == 0) |
| 870 |
{ |
| 871 |
static int done = 0; |
| 872 |
static struct rrlist list = { 0, NULL }; |
| 873 |
dbus_bool_t enabled; |
| 874 |
|
| 875 |
if (!(reply = dbus_get_bool(message, &enabled, "filter-AAAA"))) |
| 876 |
{ |
| 877 |
if (!done) |
| 878 |
{ |
| 879 |
done = 1; |
| 880 |
list.next = daemon->filter_rr; |
| 881 |
daemon->filter_rr = &list; |
| 882 |
} |
| 883 |
|
| 884 |
list.rr = enabled ? T_AAAA : 0; |
| 885 |
} |
| 886 |
} |
| 887 |
else if (strcmp(method, "SetLocaliseQueriesOption") == 0) |
| 888 |
{ |
| 889 |
reply = dbus_set_bool(message, OPT_LOCALISE, "localise-queries"); |
| 890 |
} |
| 891 |
else if (strcmp(method, "SetBogusPrivOption") == 0) |
| 892 |
{ |
| 893 |
reply = dbus_set_bool(message, OPT_BOGUSPRIV, "bogus-priv"); |
| 894 |
} |
| 895 |
#ifdef HAVE_DHCP |
| 896 |
else if (strcmp(method, "AddDhcpLease") == 0) |
| 897 |
{ |
| 898 |
reply = dbus_add_lease(message); |
| 899 |
} |
| 900 |
else if (strcmp(method, "DeleteDhcpLease") == 0) |
| 901 |
{ |
| 902 |
reply = dbus_del_lease(message); |
| 903 |
} |
| 904 |
#endif |
| 905 |
else if (strcmp(method, "GetMetrics") == 0) |
| 906 |
{ |
| 907 |
reply = dbus_get_metrics(message); |
| 908 |
} |
| 909 |
else if (strcmp(method, "GetServerMetrics") == 0) |
| 910 |
{ |
| 911 |
reply = dbus_get_server_metrics(message); |
| 912 |
} |
| 913 |
else if (strcmp(method, "ClearMetrics") == 0) |
| 914 |
{ |
| 915 |
clear_metrics(); |
| 916 |
} |
| 917 |
else if (strcmp(method, "ClearCache") == 0) |
| 918 |
clear_cache = 1; |
| 919 |
else |
| 920 |
return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
| 921 |
|
| 922 |
if (new_servers) |
| 923 |
{ |
| 924 |
my_syslog(LOG_INFO, _("setting upstream servers from DBus")); |
| 925 |
check_servers(0); |
| 926 |
if (option_bool(OPT_RELOAD)) |
| 927 |
clear_cache = 1; |
| 928 |
} |
| 929 |
|
| 930 |
if (clear_cache) |
| 931 |
clear_cache_and_reload(dnsmasq_time()); |
| 932 |
|
| 933 |
(void)user_data; /* no warning */ |
| 934 |
|
| 935 |
/* If no reply or no error, return nothing */ |
| 936 |
if (!reply) |
| 937 |
reply = dbus_message_new_method_return(message); |
| 938 |
|
| 939 |
if (reply) |
| 940 |
{ |
| 941 |
dbus_connection_send (connection, reply, NULL); |
| 942 |
dbus_message_unref (reply); |
| 943 |
} |
| 944 |
|
| 945 |
return (DBUS_HANDLER_RESULT_HANDLED); |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
/* returns NULL or error message, may fail silently if dbus daemon not yet up. */ |
| 950 |
char *dbus_init(void) |
| 951 |
{ |
| 952 |
DBusConnection *connection = NULL; |
| 953 |
DBusObjectPathVTable dnsmasq_vtable = {NULL, &message_handler, NULL, NULL, NULL, NULL }; |
| 954 |
DBusError dbus_error; |
| 955 |
DBusMessage *message; |
| 956 |
|
| 957 |
dbus_error_init (&dbus_error); |
| 958 |
if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error))) |
| 959 |
{ |
| 960 |
dbus_error_free(&dbus_error); |
| 961 |
return NULL; |
| 962 |
} |
| 963 |
|
| 964 |
dbus_connection_set_exit_on_disconnect(connection, FALSE); |
| 965 |
dbus_connection_set_watch_functions(connection, add_watch, remove_watch, |
| 966 |
NULL, NULL, NULL); |
| 967 |
dbus_error_init (&dbus_error); |
| 968 |
dbus_bus_request_name (connection, daemon->dbus_name, 0, &dbus_error); |
| 969 |
if (dbus_error_is_set (&dbus_error)) |
| 970 |
return (char *)dbus_error.message; |
| 971 |
|
| 972 |
if (!dbus_connection_register_object_path(connection, DNSMASQ_PATH, |
| 973 |
&dnsmasq_vtable, NULL)) |
| 974 |
return _("could not register a DBus message handler"); |
| 975 |
|
| 976 |
daemon->dbus = connection; |
| 977 |
|
| 978 |
if ((message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, "Up"))) |
| 979 |
{ |
| 980 |
dbus_connection_send(connection, message, NULL); |
| 981 |
dbus_message_unref(message); |
| 982 |
} |
| 983 |
|
| 984 |
return NULL; |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
void set_dbus_listeners(void) |
| 989 |
{ |
| 990 |
struct watch *w; |
| 991 |
|
| 992 |
for (w = daemon->watches; w; w = w->next) |
| 993 |
if (dbus_watch_get_enabled(w->watch)) |
| 994 |
{ |
| 995 |
unsigned int flags = dbus_watch_get_flags(w->watch); |
| 996 |
int fd = dbus_watch_get_unix_fd(w->watch); |
| 997 |
int poll_flags = POLLERR; |
| 998 |
|
| 999 |
if (flags & DBUS_WATCH_READABLE) |
| 1000 |
poll_flags |= POLLIN; |
| 1001 |
if (flags & DBUS_WATCH_WRITABLE) |
| 1002 |
poll_flags |= POLLOUT; |
| 1003 |
|
| 1004 |
poll_listen(fd, poll_flags); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
static int check_dbus_watches() |
| 1009 |
{ |
| 1010 |
struct watch *w; |
| 1011 |
|
| 1012 |
watches_modified = 0; |
| 1013 |
for (w = daemon->watches; w; w = w->next) |
| 1014 |
if (dbus_watch_get_enabled(w->watch)) |
| 1015 |
{ |
| 1016 |
unsigned int flags = 0; |
| 1017 |
int fd = dbus_watch_get_unix_fd(w->watch); |
| 1018 |
int poll_flags = poll_check(fd, POLLIN|POLLOUT|POLLERR); |
| 1019 |
|
| 1020 |
if ((poll_flags & POLLIN) != 0) |
| 1021 |
flags |= DBUS_WATCH_READABLE; |
| 1022 |
if ((poll_flags & POLLOUT) != 0) |
| 1023 |
flags |= DBUS_WATCH_WRITABLE; |
| 1024 |
if ((poll_flags & POLLERR) != 0) |
| 1025 |
flags |= DBUS_WATCH_ERROR; |
| 1026 |
|
| 1027 |
if (flags != 0) |
| 1028 |
{ |
| 1029 |
dbus_watch_handle(w->watch, flags); |
| 1030 |
if (watches_modified) |
| 1031 |
return 0; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
return 1; |
| 1036 |
} |
| 1037 |
|
| 1038 |
void check_dbus_listeners() |
| 1039 |
{ |
| 1040 |
DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 1041 |
|
| 1042 |
while (!check_dbus_watches()) ; |
| 1043 |
|
| 1044 |
if (connection) |
| 1045 |
{ |
| 1046 |
dbus_connection_ref (connection); |
| 1047 |
while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS); |
| 1048 |
dbus_connection_unref (connection); |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
#ifdef HAVE_DHCP |
| 1053 |
void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname) |
| 1054 |
{ |
| 1055 |
DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 1056 |
DBusMessage* message = NULL; |
| 1057 |
DBusMessageIter args; |
| 1058 |
char *action_str, *mac; |
| 1059 |
unsigned char *p; |
| 1060 |
int i; |
| 1061 |
|
| 1062 |
if (!connection) |
| 1063 |
return; |
| 1064 |
|
| 1065 |
if (!hostname) |
| 1066 |
hostname = ""; |
| 1067 |
|
| 1068 |
#ifdef HAVE_DHCP6 |
| 1069 |
if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 1070 |
{ |
| 1071 |
mac = print_mac(lease->clid, lease->clid_len); |
| 1072 |
inet_ntop(AF_INET6, &lease->addr6, daemon->addrbuff, ADDRSTRLEN); |
| 1073 |
} |
| 1074 |
else |
| 1075 |
#endif |
| 1076 |
{ |
| 1077 |
p = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len, |
| 1078 |
lease->hwaddr, lease->clid_len, lease->clid, &i); |
| 1079 |
mac = print_mac(p, i); |
| 1080 |
inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN); |
| 1081 |
} |
| 1082 |
|
| 1083 |
if (action == ACTION_DEL) |
| 1084 |
action_str = "DhcpLeaseDeleted"; |
| 1085 |
else if (action == ACTION_ADD) |
| 1086 |
action_str = "DhcpLeaseAdded"; |
| 1087 |
else if (action == ACTION_OLD) |
| 1088 |
action_str = "DhcpLeaseUpdated"; |
| 1089 |
else |
| 1090 |
return; |
| 1091 |
|
| 1092 |
if (!(message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, action_str))) |
| 1093 |
return; |
| 1094 |
|
| 1095 |
dbus_message_iter_init_append(message, &args); |
| 1096 |
|
| 1097 |
if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) && |
| 1098 |
dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) && |
| 1099 |
dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname)) |
| 1100 |
dbus_connection_send(connection, message, NULL); |
| 1101 |
|
| 1102 |
dbus_message_unref(message); |
| 1103 |
} |
| 1104 |
#endif |
| 1105 |
|
| 1106 |
#endif |