| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-get-properties.h" |
| 7 |
#include "bus-object.h" |
| 8 |
#include "hashmap.h" |
| 9 |
#include "resolved-bus.h" |
| 10 |
#include "resolved-dns-delegate.h" |
| 11 |
#include "resolved-dns-delegate-bus.h" |
| 12 |
#include "resolved-dns-search-domain.h" |
| 13 |
#include "resolved-dns-server.h" /* IWYU pragma: keep */ |
| 14 |
#include "resolved-manager.h" |
| 15 |
#include "strv.h" |
| 16 |
|
| 17 |
static int property_get_dns( |
| 18 |
sd_bus *bus, |
| 19 |
const char *path, |
| 20 |
const char *interface, |
| 21 |
const char *property, |
| 22 |
sd_bus_message *reply, |
| 23 |
void *userdata, |
| 24 |
sd_bus_error *error) { |
| 25 |
|
| 26 |
DnsDelegate *d = ASSERT_PTR(userdata); |
| 27 |
int r; |
| 28 |
|
| 29 |
assert(reply); |
| 30 |
|
| 31 |
r = sd_bus_message_open_container(reply, 'a', "(iiayqs)"); |
| 32 |
if (r < 0) |
| 33 |
return r; |
| 34 |
|
| 35 |
LIST_FOREACH(servers, s, d->dns_servers) { |
| 36 |
r = bus_dns_server_append(reply, s, /* with_ifindex= */ true, /* extended= */ true); |
| 37 |
if (r < 0) |
| 38 |
return r; |
| 39 |
} |
| 40 |
|
| 41 |
return sd_bus_message_close_container(reply); |
| 42 |
} |
| 43 |
|
| 44 |
static int property_get_current_dns_server( |
| 45 |
sd_bus *bus, |
| 46 |
const char *path, |
| 47 |
const char *interface, |
| 48 |
const char *property, |
| 49 |
sd_bus_message *reply, |
| 50 |
void *userdata, |
| 51 |
sd_bus_error *error) { |
| 52 |
|
| 53 |
DnsDelegate *d = ASSERT_PTR(userdata); |
| 54 |
|
| 55 |
assert(reply); |
| 56 |
|
| 57 |
return bus_dns_server_append(reply, d->current_dns_server, /* with_ifindex= */ true, /* extended= */ true); |
| 58 |
} |
| 59 |
|
| 60 |
static int property_get_domains( |
| 61 |
sd_bus *bus, |
| 62 |
const char *path, |
| 63 |
const char *interface, |
| 64 |
const char *property, |
| 65 |
sd_bus_message *reply, |
| 66 |
void *userdata, |
| 67 |
sd_bus_error *error) { |
| 68 |
|
| 69 |
DnsDelegate *delegate = ASSERT_PTR(userdata); |
| 70 |
int r; |
| 71 |
|
| 72 |
assert(reply); |
| 73 |
|
| 74 |
r = sd_bus_message_open_container(reply, 'a', "(sb)"); |
| 75 |
if (r < 0) |
| 76 |
return r; |
| 77 |
|
| 78 |
LIST_FOREACH(domains, d, delegate->search_domains) { |
| 79 |
r = sd_bus_message_append(reply, "(sb)", d->name, d->route_only); |
| 80 |
if (r < 0) |
| 81 |
return r; |
| 82 |
} |
| 83 |
|
| 84 |
return sd_bus_message_close_container(reply); |
| 85 |
} |
| 86 |
|
| 87 |
static int dns_delegate_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
| 88 |
Manager *m = ASSERT_PTR(userdata); |
| 89 |
|
| 90 |
assert(bus); |
| 91 |
assert(path); |
| 92 |
assert(interface); |
| 93 |
assert(found); |
| 94 |
|
| 95 |
_cleanup_free_ char *e = NULL; |
| 96 |
if (sd_bus_path_decode(path, "/org/freedesktop/resolve1/dns_delegate", &e) <= 0) |
| 97 |
return 0; |
| 98 |
|
| 99 |
DnsDelegate *d = hashmap_get(m->delegates, e); |
| 100 |
if (!d) |
| 101 |
return 0; |
| 102 |
|
| 103 |
*found = d; |
| 104 |
return 1; |
| 105 |
} |
| 106 |
|
| 107 |
char* dns_delegate_bus_path(const DnsDelegate *d) { |
| 108 |
char *p; |
| 109 |
|
| 110 |
assert(d); |
| 111 |
|
| 112 |
if (sd_bus_path_encode("/org/freedesktop/resolve1/dns_delegate", d->id, &p) < 0) |
| 113 |
return NULL; |
| 114 |
|
| 115 |
return p; |
| 116 |
} |
| 117 |
|
| 118 |
static int dns_delegate_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { |
| 119 |
Manager *m = ASSERT_PTR(userdata); |
| 120 |
int r; |
| 121 |
|
| 122 |
assert(bus); |
| 123 |
assert(path); |
| 124 |
assert(nodes); |
| 125 |
|
| 126 |
_cleanup_strv_free_ char **l = NULL; |
| 127 |
DnsDelegate *d; |
| 128 |
HASHMAP_FOREACH(d, m->delegates) { |
| 129 |
_cleanup_free_ char *p = NULL; |
| 130 |
|
| 131 |
p = dns_delegate_bus_path(d); |
| 132 |
if (!p) |
| 133 |
return -ENOMEM; |
| 134 |
|
| 135 |
r = strv_consume(&l, TAKE_PTR(p)); |
| 136 |
if (r < 0) |
| 137 |
return r; |
| 138 |
} |
| 139 |
|
| 140 |
*nodes = TAKE_PTR(l); |
| 141 |
|
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
static const sd_bus_vtable dns_delegate_vtable[] = { |
| 146 |
SD_BUS_VTABLE_START(0), |
| 147 |
|
| 148 |
SD_BUS_PROPERTY("DNS", "a(iiayqs)", property_get_dns, 0, 0), |
| 149 |
SD_BUS_PROPERTY("CurrentDNSServer", "(iiayqs)", property_get_current_dns_server, 0, 0), |
| 150 |
SD_BUS_PROPERTY("Domains", "a(sb)", property_get_domains, 0, 0), |
| 151 |
SD_BUS_PROPERTY("DefaultRoute", "b", bus_property_get_tristate, offsetof(DnsDelegate, default_route), 0), |
| 152 |
|
| 153 |
SD_BUS_VTABLE_END |
| 154 |
}; |
| 155 |
|
| 156 |
const BusObjectImplementation dns_delegate_object = { |
| 157 |
"/org/freedesktop/resolve1/dns_delegate", |
| 158 |
"org.freedesktop.resolve1.DnsDelegate", |
| 159 |
.fallback_vtables = BUS_FALLBACK_VTABLES({dns_delegate_vtable, dns_delegate_object_find}), |
| 160 |
.node_enumerator = dns_delegate_node_enumerator, |
| 161 |
}; |