]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dnssd-bus.c
macro: introduce TAKE_PTR() macro
[thirdparty/systemd.git] / src / resolve / resolved-dnssd-bus.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2017 Dmitry Rozhkov
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "bus-util.h"
22 #include "resolved-dnssd.h"
23 #include "resolved-dnssd-bus.h"
24 #include "resolved-link.h"
25 #include "strv.h"
26 #include "user-util.h"
27
28 int bus_dnssd_method_unregister(sd_bus_message *message, void *userdata, sd_bus_error *error) {
29 DnssdService *s = userdata;
30 DnssdTxtData *txt_data;
31 Manager *m;
32 Iterator i;
33 Link *l;
34 int r;
35
36 assert(message);
37 assert(s);
38
39 m = s->manager;
40
41 r = bus_verify_polkit_async(message, CAP_SYS_ADMIN,
42 "org.freedesktop.resolve1.unregister-service",
43 NULL, false, s->originator,
44 &m->polkit_registry, error);
45 if (r < 0)
46 return r;
47 if (r == 0)
48 return 1; /* Polkit will call us back */
49
50 HASHMAP_FOREACH(l, m->links, i) {
51 if (l->mdns_ipv4_scope) {
52 r = dns_scope_announce(l->mdns_ipv4_scope, true);
53 if (r < 0)
54 log_warning_errno(r, "Failed to send goodbye messages in IPv4 scope: %m");
55
56 dns_zone_remove_rr(&l->mdns_ipv4_scope->zone, s->ptr_rr);
57 dns_zone_remove_rr(&l->mdns_ipv4_scope->zone, s->srv_rr);
58 LIST_FOREACH(items, txt_data, s->txt_data_items)
59 dns_zone_remove_rr(&l->mdns_ipv4_scope->zone, txt_data->rr);
60 }
61
62 if (l->mdns_ipv6_scope) {
63 r = dns_scope_announce(l->mdns_ipv6_scope, true);
64 if (r < 0)
65 log_warning_errno(r, "Failed to send goodbye messages in IPv6 scope: %m");
66
67 dns_zone_remove_rr(&l->mdns_ipv6_scope->zone, s->ptr_rr);
68 dns_zone_remove_rr(&l->mdns_ipv6_scope->zone, s->srv_rr);
69 LIST_FOREACH(items, txt_data, s->txt_data_items)
70 dns_zone_remove_rr(&l->mdns_ipv6_scope->zone, txt_data->rr);
71 }
72 }
73
74 dnssd_service_free(s);
75
76 manager_refresh_rrs(m);
77
78 return sd_bus_reply_method_return(message, NULL);
79 }
80
81 const sd_bus_vtable dnssd_vtable[] = {
82 SD_BUS_VTABLE_START(0),
83
84 SD_BUS_METHOD("Unregister", NULL, NULL, bus_dnssd_method_unregister, SD_BUS_VTABLE_UNPRIVILEGED),
85 SD_BUS_SIGNAL("Conflicted", NULL, 0),
86
87 SD_BUS_VTABLE_END
88 };
89
90 int dnssd_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
91 _cleanup_free_ char *name = NULL;
92 Manager *m = userdata;
93 DnssdService *service;
94 int r;
95
96 assert(bus);
97 assert(path);
98 assert(interface);
99 assert(found);
100 assert(m);
101
102 r = sd_bus_path_decode(path, "/org/freedesktop/resolve1/dnssd", &name);
103 if (r <= 0)
104 return 0;
105
106 service = hashmap_get(m->dnssd_services, name);
107 if (!service)
108 return 0;
109
110 *found = service;
111 return 1;
112 }
113
114 int dnssd_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
115 _cleanup_strv_free_ char **l = NULL;
116 Manager *m = userdata;
117 DnssdService *service;
118 Iterator i;
119 unsigned c = 0;
120 int r;
121
122 assert(bus);
123 assert(path);
124 assert(m);
125 assert(nodes);
126
127 l = new0(char*, hashmap_size(m->dnssd_services) + 1);
128 if (!l)
129 return -ENOMEM;
130
131 HASHMAP_FOREACH(service, m->dnssd_services, i) {
132 char *p;
133
134 r = sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service->name, &p);
135 if (r < 0)
136 return r;
137
138 l[c++] = p;
139 }
140
141 l[c] = NULL;
142 *nodes = TAKE_PTR(l);
143
144 return 1;
145 }