]>
Commit | Line | Data |
---|---|---|
bb4f798a | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
52ad194e MB |
2 | |
3 | #include "conf-files.h" | |
4 | #include "conf-parser.h" | |
bb4f798a | 5 | #include "def.h" |
52ad194e MB |
6 | #include "resolved-dnssd.h" |
7 | #include "resolved-dns-rr.h" | |
8 | #include "resolved-manager.h" | |
9 | #include "specifier.h" | |
10 | #include "strv.h" | |
11 | ||
bb4f798a | 12 | #define DNSSD_SERVICE_DIRS ((const char* const*) CONF_PATHS_STRV("systemd/dnssd")) |
52ad194e MB |
13 | |
14 | DnssdTxtData *dnssd_txtdata_free(DnssdTxtData *txt_data) { | |
15 | if (!txt_data) | |
16 | return NULL; | |
17 | ||
18 | dns_resource_record_unref(txt_data->rr); | |
19 | dns_txt_item_free_all(txt_data->txt); | |
20 | ||
21 | return mfree(txt_data); | |
22 | } | |
23 | ||
24 | DnssdTxtData *dnssd_txtdata_free_all(DnssdTxtData *txt_data) { | |
25 | DnssdTxtData *next; | |
26 | ||
27 | if (!txt_data) | |
28 | return NULL; | |
29 | ||
30 | next = txt_data->items_next; | |
31 | ||
32 | dnssd_txtdata_free(txt_data); | |
33 | ||
34 | return dnssd_txtdata_free_all(next); | |
35 | } | |
36 | ||
37 | DnssdService *dnssd_service_free(DnssdService *service) { | |
38 | if (!service) | |
39 | return NULL; | |
40 | ||
41 | if (service->manager) | |
42 | hashmap_remove(service->manager->dnssd_services, service->name); | |
43 | ||
44 | dns_resource_record_unref(service->ptr_rr); | |
45 | dns_resource_record_unref(service->srv_rr); | |
46 | ||
47 | dnssd_txtdata_free_all(service->txt_data_items); | |
48 | ||
49 | free(service->filename); | |
50 | free(service->name); | |
51 | free(service->type); | |
52 | free(service->name_template); | |
53 | ||
54 | return mfree(service); | |
55 | } | |
56 | ||
57 | static int dnssd_service_load(Manager *manager, const char *filename) { | |
58 | _cleanup_(dnssd_service_freep) DnssdService *service = NULL; | |
59 | _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL; | |
60 | char *d; | |
61 | const char *dropin_dirname; | |
62 | int r; | |
63 | ||
64 | assert(manager); | |
65 | assert(filename); | |
66 | ||
67 | service = new0(DnssdService, 1); | |
68 | if (!service) | |
69 | return log_oom(); | |
70 | ||
71 | service->filename = strdup(filename); | |
72 | if (!service->filename) | |
73 | return log_oom(); | |
74 | ||
75 | service->name = strdup(basename(filename)); | |
76 | if (!service->name) | |
77 | return log_oom(); | |
78 | ||
79 | d = endswith(service->name, ".dnssd"); | |
80 | if (!d) | |
81 | return -EINVAL; | |
82 | ||
83 | assert(streq(d, ".dnssd")); | |
84 | ||
85 | *d = '\0'; | |
86 | ||
87 | dropin_dirname = strjoina(service->name, ".dnssd.d"); | |
88 | ||
bb4f798a | 89 | r = config_parse_many(filename, DNSSD_SERVICE_DIRS, dropin_dirname, |
52ad194e MB |
90 | "Service\0", |
91 | config_item_perf_lookup, resolved_dnssd_gperf_lookup, | |
478ed938 | 92 | false, service, NULL); |
52ad194e MB |
93 | if (r < 0) |
94 | return r; | |
95 | ||
96 | if (!service->name_template) { | |
97 | log_error("%s doesn't define service instance name", service->name); | |
98 | return -EINVAL; | |
99 | } | |
100 | ||
101 | if (!service->type) { | |
102 | log_error("%s doesn't define service type", service->name); | |
103 | return -EINVAL; | |
104 | } | |
105 | ||
106 | if (LIST_IS_EMPTY(service->txt_data_items)) { | |
107 | txt_data = new0(DnssdTxtData, 1); | |
108 | if (!txt_data) | |
109 | return log_oom(); | |
110 | ||
111 | r = dns_txt_item_new_empty(&txt_data->txt); | |
112 | if (r < 0) | |
113 | return r; | |
114 | ||
115 | LIST_PREPEND(items, service->txt_data_items, txt_data); | |
116 | txt_data = NULL; | |
117 | } | |
118 | ||
119 | r = hashmap_ensure_allocated(&manager->dnssd_services, &string_hash_ops); | |
120 | if (r < 0) | |
121 | return r; | |
122 | ||
123 | r = hashmap_put(manager->dnssd_services, service->name, service); | |
124 | if (r < 0) | |
125 | return r; | |
126 | ||
127 | service->manager = manager; | |
128 | ||
129 | r = dnssd_update_rrs(service); | |
130 | if (r < 0) | |
131 | return r; | |
132 | ||
133 | service = NULL; | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
6e866b33 | 138 | static int specifier_dnssd_host_name(char specifier, const void *data, const void *userdata, char **ret) { |
52ad194e MB |
139 | DnssdService *s = (DnssdService *) userdata; |
140 | char *n; | |
141 | ||
142 | assert(s); | |
143 | assert(s->manager); | |
144 | assert(s->manager->llmnr_hostname); | |
145 | ||
146 | n = strdup(s->manager->llmnr_hostname); | |
147 | if (!n) | |
148 | return -ENOMEM; | |
149 | ||
150 | *ret = n; | |
151 | return 0; | |
152 | } | |
153 | ||
154 | int dnssd_render_instance_name(DnssdService *s, char **ret_name) { | |
155 | static const Specifier specifier_table[] = { | |
156 | { 'b', specifier_boot_id, NULL }, | |
157 | { 'H', specifier_dnssd_host_name, NULL }, | |
b012e921 | 158 | { 'm', specifier_machine_id, NULL }, |
52ad194e MB |
159 | { 'v', specifier_kernel_release, NULL }, |
160 | {} | |
161 | }; | |
162 | _cleanup_free_ char *name = NULL; | |
163 | int r; | |
164 | ||
165 | assert(s); | |
166 | assert(s->name_template); | |
167 | ||
168 | r = specifier_printf(s->name_template, specifier_table, s, &name); | |
169 | if (r < 0) | |
170 | return log_debug_errno(r, "Failed to replace specifiers: %m"); | |
171 | ||
6e866b33 MB |
172 | if (!dns_service_name_is_valid(name)) |
173 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), | |
174 | "Service instance name '%s' is invalid.", | |
175 | name); | |
52ad194e | 176 | |
b012e921 | 177 | *ret_name = TAKE_PTR(name); |
52ad194e MB |
178 | |
179 | return 0; | |
180 | } | |
181 | ||
182 | int dnssd_load(Manager *manager) { | |
183 | _cleanup_strv_free_ char **files = NULL; | |
184 | char **f; | |
185 | int r; | |
186 | ||
187 | assert(manager); | |
188 | ||
189 | if (manager->mdns_support != RESOLVE_SUPPORT_YES) | |
190 | return 0; | |
191 | ||
bb4f798a | 192 | r = conf_files_list_strv(&files, ".dnssd", NULL, 0, DNSSD_SERVICE_DIRS); |
52ad194e MB |
193 | if (r < 0) |
194 | return log_error_errno(r, "Failed to enumerate .dnssd files: %m"); | |
195 | ||
196 | STRV_FOREACH_BACKWARDS(f, files) { | |
197 | r = dnssd_service_load(manager, *f); | |
198 | if (r < 0) | |
199 | log_warning_errno(r, "Failed to load '%s': %m", *f);; | |
200 | } | |
201 | ||
202 | return 0; | |
203 | } | |
204 | ||
205 | int dnssd_update_rrs(DnssdService *s) { | |
206 | _cleanup_free_ char *n = NULL; | |
207 | _cleanup_free_ char *service_name = NULL; | |
208 | _cleanup_free_ char *full_name = NULL; | |
209 | DnssdTxtData *txt_data; | |
210 | int r; | |
211 | ||
212 | assert(s); | |
213 | assert(s->txt_data_items); | |
214 | assert(s->manager); | |
215 | ||
216 | s->ptr_rr = dns_resource_record_unref(s->ptr_rr); | |
217 | s->srv_rr = dns_resource_record_unref(s->srv_rr); | |
218 | LIST_FOREACH(items, txt_data, s->txt_data_items) | |
219 | txt_data->rr = dns_resource_record_unref(txt_data->rr); | |
220 | ||
221 | r = dnssd_render_instance_name(s, &n); | |
222 | if (r < 0) | |
223 | return r; | |
224 | ||
6e866b33 | 225 | r = dns_name_concat(s->type, "local", 0, &service_name); |
52ad194e MB |
226 | if (r < 0) |
227 | return r; | |
6e866b33 | 228 | r = dns_name_concat(n, service_name, 0, &full_name); |
52ad194e MB |
229 | if (r < 0) |
230 | return r; | |
231 | ||
232 | LIST_FOREACH(items, txt_data, s->txt_data_items) { | |
233 | txt_data->rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_TXT, | |
234 | full_name); | |
235 | if (!txt_data->rr) | |
236 | goto oom; | |
237 | ||
238 | txt_data->rr->ttl = MDNS_DEFAULT_TTL; | |
239 | txt_data->rr->txt.items = dns_txt_item_copy(txt_data->txt); | |
240 | if (!txt_data->rr->txt.items) | |
241 | goto oom; | |
242 | } | |
243 | ||
244 | s->ptr_rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, | |
245 | service_name); | |
246 | if (!s->ptr_rr) | |
247 | goto oom; | |
248 | ||
249 | s->ptr_rr->ttl = MDNS_DEFAULT_TTL; | |
250 | s->ptr_rr->ptr.name = strdup(full_name); | |
251 | if (!s->ptr_rr->ptr.name) | |
252 | goto oom; | |
253 | ||
254 | s->srv_rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SRV, | |
255 | full_name); | |
256 | if (!s->srv_rr) | |
257 | goto oom; | |
258 | ||
259 | s->srv_rr->ttl = MDNS_DEFAULT_TTL; | |
260 | s->srv_rr->srv.priority = s->priority; | |
261 | s->srv_rr->srv.weight = s->weight; | |
262 | s->srv_rr->srv.port = s->port; | |
263 | s->srv_rr->srv.name = strdup(s->manager->mdns_hostname); | |
264 | if (!s->srv_rr->srv.name) | |
265 | goto oom; | |
266 | ||
267 | return 0; | |
268 | ||
269 | oom: | |
270 | LIST_FOREACH(items, txt_data, s->txt_data_items) | |
271 | txt_data->rr = dns_resource_record_unref(txt_data->rr); | |
272 | s->ptr_rr = dns_resource_record_unref(s->ptr_rr); | |
273 | s->srv_rr = dns_resource_record_unref(s->srv_rr); | |
274 | return -ENOMEM; | |
275 | } | |
276 | ||
277 | int dnssd_txt_item_new_from_string(const char *key, const char *value, DnsTxtItem **ret_item) { | |
278 | size_t length; | |
279 | DnsTxtItem *i; | |
280 | ||
281 | length = strlen(key); | |
282 | ||
283 | if (!isempty(value)) | |
284 | length += strlen(value) + 1; /* length of value plus '=' */ | |
285 | ||
286 | i = malloc0(offsetof(DnsTxtItem, data) + length + 1); /* for safety reasons we add an extra NUL byte */ | |
287 | if (!i) | |
288 | return -ENOMEM; | |
289 | ||
290 | memcpy(i->data, key, strlen(key)); | |
291 | if (!isempty(value)) { | |
292 | memcpy(i->data + strlen(key), "=", 1); | |
293 | memcpy(i->data + strlen(key) + 1, value, strlen(value)); | |
294 | } | |
295 | i->length = length; | |
296 | ||
b012e921 | 297 | *ret_item = TAKE_PTR(i); |
52ad194e MB |
298 | |
299 | return 0; | |
300 | } | |
301 | ||
302 | int dnssd_txt_item_new_from_data(const char *key, const void *data, const size_t size, DnsTxtItem **ret_item) { | |
303 | size_t length; | |
304 | DnsTxtItem *i; | |
305 | ||
306 | length = strlen(key); | |
307 | ||
308 | if (size > 0) | |
309 | length += size + 1; /* size of date plus '=' */ | |
310 | ||
311 | i = malloc0(offsetof(DnsTxtItem, data) + length + 1); /* for safety reasons we add an extra NUL byte */ | |
312 | if (!i) | |
313 | return -ENOMEM; | |
314 | ||
315 | memcpy(i->data, key, strlen(key)); | |
316 | if (size > 0) { | |
317 | memcpy(i->data + strlen(key), "=", 1); | |
318 | memcpy(i->data + strlen(key) + 1, data, size); | |
319 | } | |
320 | i->length = length; | |
321 | ||
b012e921 | 322 | *ret_item = TAKE_PTR(i); |
52ad194e MB |
323 | |
324 | return 0; | |
325 | } | |
326 | ||
327 | void dnssd_signal_conflict(Manager *manager, const char *name) { | |
328 | Iterator i; | |
329 | DnssdService *s; | |
330 | int r; | |
331 | ||
332 | HASHMAP_FOREACH(s, manager->dnssd_services, i) { | |
333 | if (s->withdrawn) | |
334 | continue; | |
335 | ||
336 | if (dns_name_equal(dns_resource_key_name(s->srv_rr->key), name)) { | |
337 | _cleanup_free_ char *path = NULL; | |
338 | ||
339 | s->withdrawn = true; | |
340 | ||
341 | r = sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", s->name, &path); | |
342 | if (r < 0) { | |
343 | log_error_errno(r, "Can't get D-BUS object path: %m"); | |
344 | return; | |
345 | } | |
346 | ||
347 | r = sd_bus_emit_signal(manager->bus, | |
348 | path, | |
349 | "org.freedesktop.resolve1.DnssdService", | |
350 | "Conflicted", | |
351 | NULL); | |
352 | if (r < 0) { | |
353 | log_error_errno(r, "Cannot emit signal: %m"); | |
354 | return; | |
355 | } | |
356 | ||
357 | break; | |
358 | } | |
359 | } | |
360 | } |