]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dnssd-bus.c
resolved: consult Polkit for privileges when manipulating DNS-SD
[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 Manager *m;
31 Iterator i;
32 Link *l;
33 int r;
34
35 assert(message);
36 assert(s);
37
38 m = s->manager;
39
40 r = bus_verify_polkit_async(message, CAP_SYS_ADMIN,
41 "org.freedesktop.resolve1.unregister-service",
42 NULL, false, s->originator,
43 &m->polkit_registry, error);
44 if (r < 0)
45 return r;
46 if (r == 0)
47 return 1; /* Polkit will call us back */
48
49 HASHMAP_FOREACH(l, m->links, i) {
50 if (l->mdns_ipv4_scope) {
51 r = dns_scope_announce(l->mdns_ipv4_scope, true);
52 if (r < 0)
53 log_warning_errno(r, "Failed to send goodbye messages in IPv4 scope: %m");
54
55 dns_zone_remove_rr(&l->mdns_ipv4_scope->zone, s->ptr_rr);
56 dns_zone_remove_rr(&l->mdns_ipv4_scope->zone, s->srv_rr);
57 dns_zone_remove_rr(&l->mdns_ipv4_scope->zone, s->txt_rr);
58 }
59
60 if (l->mdns_ipv6_scope) {
61 r = dns_scope_announce(l->mdns_ipv6_scope, true);
62 if (r < 0)
63 log_warning_errno(r, "Failed to send goodbye messages in IPv6 scope: %m");
64
65 dns_zone_remove_rr(&l->mdns_ipv6_scope->zone, s->ptr_rr);
66 dns_zone_remove_rr(&l->mdns_ipv6_scope->zone, s->srv_rr);
67 dns_zone_remove_rr(&l->mdns_ipv6_scope->zone, s->txt_rr);
68 }
69 }
70
71 dnssd_service_free(s);
72
73 manager_refresh_rrs(m);
74
75 return sd_bus_reply_method_return(message, NULL);
76 }
77
78 const sd_bus_vtable dnssd_vtable[] = {
79 SD_BUS_VTABLE_START(0),
80
81 SD_BUS_METHOD("Unregister", NULL, NULL, bus_dnssd_method_unregister, SD_BUS_VTABLE_UNPRIVILEGED),
82 SD_BUS_SIGNAL("Conflicted", NULL, 0),
83
84 SD_BUS_VTABLE_END
85 };
86
87 int dnssd_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
88 _cleanup_free_ char *name = NULL;
89 Manager *m = userdata;
90 DnssdService *service;
91 int r;
92
93 assert(bus);
94 assert(path);
95 assert(interface);
96 assert(found);
97 assert(m);
98
99 r = sd_bus_path_decode(path, "/org/freedesktop/resolve1/dnssd", &name);
100 if (r <= 0)
101 return 0;
102
103 service = hashmap_get(m->dnssd_services, name);
104 if (!service)
105 return 0;
106
107 *found = service;
108 return 1;
109 }
110
111 int dnssd_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
112 _cleanup_strv_free_ char **l = NULL;
113 Manager *m = userdata;
114 DnssdService *service;
115 Iterator i;
116 unsigned c = 0;
117 int r;
118
119 assert(bus);
120 assert(path);
121 assert(m);
122 assert(nodes);
123
124 l = new0(char*, hashmap_size(m->dnssd_services) + 1);
125 if (!l)
126 return -ENOMEM;
127
128 HASHMAP_FOREACH(service, m->dnssd_services, i) {
129 char *p;
130
131 r = sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service->name, &p);
132 if (r < 0)
133 return r;
134
135 l[c++] = p;
136 }
137
138 l[c] = NULL;
139 *nodes = l;
140 l = NULL;
141
142 return 1;
143 }