Coverage Report

Created: 2025-06-13 06:25

/src/systemd/src/resolve/resolved-bus.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3
#include "sd-bus.h"
4
5
#include "alloc-util.h"
6
#include "bus-common-errors.h"
7
#include "bus-get-properties.h"
8
#include "bus-locator.h"
9
#include "bus-log-control-api.h"
10
#include "bus-message-util.h"
11
#include "bus-object.h"
12
#include "bus-polkit.h"
13
#include "bus-util.h"
14
#include "dns-domain.h"
15
#include "format-util.h"
16
#include "path-util.h"
17
#include "resolve-util.h"
18
#include "resolved-bus.h"
19
#include "resolved-def.h"
20
#include "resolved-dns-answer.h"
21
#include "resolved-dns-delegate.h"
22
#include "resolved-dns-delegate-bus.h"
23
#include "resolved-dns-dnssec.h"
24
#include "resolved-dns-packet.h"
25
#include "resolved-dns-query.h"
26
#include "resolved-dns-question.h"
27
#include "resolved-dns-rr.h"
28
#include "resolved-dns-scope.h"
29
#include "resolved-dns-search-domain.h"
30
#include "resolved-dns-server.h"
31
#include "resolved-dns-stream.h"
32
#include "resolved-dns-stub.h"
33
#include "resolved-dns-synthesize.h"
34
#include "resolved-dns-transaction.h"
35
#include "resolved-dnssd.h"
36
#include "resolved-dnssd-bus.h"
37
#include "resolved-link.h"
38
#include "resolved-link-bus.h"
39
#include "resolved-manager.h"
40
#include "resolved-resolv-conf.h"
41
#include "set.h"
42
#include "socket-netlink.h"
43
#include "string-util.h"
44
#include "utf8.h"
45
46
BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_resolve_support, resolve_support, ResolveSupport);
47
48
static int dns_query_new_for_bus(
49
                Manager *m,
50
                DnsQuery **ret,
51
                DnsQuestion *question_utf8,
52
                DnsQuestion *question_idna,
53
                DnsPacket *question_bypass,
54
                int ifindex,
55
                uint64_t flags,
56
0
                sd_bus_error *error) {
57
58
0
        int r;
59
60
0
        r = dns_query_new(m, ret, question_utf8, question_idna, question_bypass, ifindex, flags);
61
0
        if (r == -ENOANO)
62
0
                return sd_bus_error_set(error, BUS_ERROR_DNS_REFUSED, "DNS query type refused.");
63
0
        return r;
64
0
}
65
66
0
static int query_on_bus_track(sd_bus_track *t, void *userdata) {
67
0
        DnsQuery *q = ASSERT_PTR(userdata);
68
69
0
        assert(t);
70
71
0
        if (!DNS_TRANSACTION_IS_LIVE(q->state))
72
0
                return 0;
73
74
0
        log_debug("Client of active query vanished, aborting query.");
75
0
        dns_query_complete(q, DNS_TRANSACTION_ABORTED);
76
0
        return 0;
77
0
}
78
79
0
static int dns_query_bus_track(DnsQuery *q, sd_bus_message *m) {
80
0
        int r;
81
82
0
        assert(q);
83
0
        assert(m);
84
85
0
        if (!q->bus_track) {
86
0
                r = sd_bus_track_new(sd_bus_message_get_bus(m), &q->bus_track, query_on_bus_track, q);
87
0
                if (r < 0)
88
0
                        return r;
89
0
        }
90
91
0
        r = sd_bus_track_add_sender(q->bus_track, m);
92
0
        if (r < 0)
93
0
                return r;
94
95
0
        return 0;
96
0
}
97
98
0
static sd_bus_message *dns_query_steal_request(DnsQuery *q) {
99
0
        assert(q);
100
101
        /* Find the main query, it's the one that owns the message */
102
0
        while (q->auxiliary_for)
103
0
                q = q->auxiliary_for;
104
105
        /* Let's take the request message out of the DnsQuery object, so that we never send requests twice */
106
0
        return TAKE_PTR(q->bus_request);
107
0
}
108
109
_sd_printf_(3, 4) static int reply_method_errorf(
110
                DnsQuery *query,
111
                const char *error_name,
112
                const char *format,
113
0
                ...) {
114
115
0
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
116
0
        va_list ap;
117
0
        int r;
118
119
0
        assert(query);
120
0
        assert(format);
121
122
0
        req = dns_query_steal_request(query);
123
0
        if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
124
0
                return 0;
125
126
0
        va_start(ap, format);
127
0
        r = sd_bus_reply_method_errorfv(req, error_name, format, ap);
128
0
        va_end(ap);
129
130
0
        return r;
131
0
}
132
133
_sd_printf_(3, 4) static int reply_method_errnof(
134
                DnsQuery *query,
135
                int err,
136
                const char *format,
137
0
                ...) {
138
139
0
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
140
0
        int r;
141
142
0
        assert(query);
143
144
0
        req = dns_query_steal_request(query);
145
0
        if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
146
0
                return 0;
147
148
0
        if (format) {
149
0
                va_list ap;
150
151
0
                va_start(ap, format);
152
0
                r = sd_bus_reply_method_errnofv(req, err, format, ap);
153
0
                va_end(ap);
154
0
        } else
155
0
                r = sd_bus_reply_method_errno(req, err, NULL);
156
157
0
        return r;
158
0
}
159
160
0
static int reply_query_state(DnsQuery *q) {
161
0
        assert(q);
162
163
0
        switch (q->state) {
164
165
0
        case DNS_TRANSACTION_NO_SERVERS:
166
0
                return reply_method_errorf(q, BUS_ERROR_NO_NAME_SERVERS, "No appropriate name servers or networks for name found");
167
168
0
        case DNS_TRANSACTION_TIMEOUT:
169
0
                return reply_method_errorf(q, SD_BUS_ERROR_TIMEOUT, "Query timed out");
170
171
0
        case DNS_TRANSACTION_ATTEMPTS_MAX_REACHED:
172
0
                return reply_method_errorf(q, SD_BUS_ERROR_TIMEOUT, "All attempts to contact name servers or networks failed");
173
174
0
        case DNS_TRANSACTION_INVALID_REPLY:
175
0
                return reply_method_errorf(q, BUS_ERROR_INVALID_REPLY, "Received invalid reply");
176
177
0
        case DNS_TRANSACTION_ERRNO:
178
0
                return reply_method_errnof(q, q->answer_errno, "Lookup failed due to system error: %m");
179
180
0
        case DNS_TRANSACTION_ABORTED:
181
0
                return reply_method_errorf(q, BUS_ERROR_ABORTED, "Query aborted");
182
183
0
        case DNS_TRANSACTION_DNSSEC_FAILED:
184
0
                return reply_method_errorf(q, BUS_ERROR_DNSSEC_FAILED, "DNSSEC validation failed: %s%s%s%s%s%s",
185
0
                                           dnssec_result_to_string(q->answer_dnssec_result),
186
0
                                           q->answer_ede_rcode >= 0 ? " (" : "",
187
0
                                           q->answer_ede_rcode >= 0 ? FORMAT_DNS_EDE_RCODE(q->answer_ede_rcode) : "",
188
0
                                           (q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg)) ? ": " : "",
189
0
                                           q->answer_ede_rcode >= 0 ? strempty(q->answer_ede_msg) : "",
190
0
                                           q->answer_ede_rcode >= 0 ? ")" : "");
191
192
0
        case DNS_TRANSACTION_NO_TRUST_ANCHOR:
193
0
                return reply_method_errorf(q, BUS_ERROR_NO_TRUST_ANCHOR, "No suitable trust anchor known");
194
195
0
        case DNS_TRANSACTION_RR_TYPE_UNSUPPORTED:
196
0
                return reply_method_errorf(q, BUS_ERROR_RR_TYPE_UNSUPPORTED, "Server does not support requested resource record type");
197
198
0
        case DNS_TRANSACTION_NETWORK_DOWN:
199
0
                return reply_method_errorf(q, BUS_ERROR_NETWORK_DOWN, "Network is down");
200
201
0
        case DNS_TRANSACTION_NOT_FOUND:
202
                /* We return this as NXDOMAIN. This is only generated when a host doesn't implement LLMNR/TCP, and we
203
                 * thus quickly know that we cannot resolve an in-addr.arpa or ip6.arpa address. */
204
0
                return reply_method_errorf(q, BUS_ERROR_DNS_NXDOMAIN, "'%s' not found", dns_query_string(q));
205
206
0
        case DNS_TRANSACTION_NO_SOURCE:
207
0
                return reply_method_errorf(q, BUS_ERROR_NO_SOURCE, "All suitable resolution sources turned off");
208
209
0
        case DNS_TRANSACTION_STUB_LOOP:
210
0
                return reply_method_errorf(q, BUS_ERROR_STUB_LOOP, "Configured DNS server loops back to us");
211
212
0
        case DNS_TRANSACTION_RCODE_FAILURE: {
213
0
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
214
0
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *req = NULL;
215
216
0
                req = dns_query_steal_request(q);
217
0
                if (!req) /* No bus message set anymore? then we already replied already, let's not answer a second time */
218
0
                        return 0;
219
220
0
                if (q->answer_rcode == DNS_RCODE_NXDOMAIN)
221
0
                        sd_bus_error_setf(&error, BUS_ERROR_DNS_NXDOMAIN, "Name '%s' not found", dns_query_string(q));
222
0
                else {
223
0
                        const char *rc, *n;
224
225
0
                        rc = FORMAT_DNS_RCODE(q->answer_rcode);
226
0
                        n = strjoina(_BUS_ERROR_DNS, rc);
227
0
                        sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error: %s%s%s%s%s%s",
228
0
                                          dns_query_string(q), rc,
229
0
                                          q->answer_ede_rcode >= 0 ? " (" : "",
230
0
                                          q->answer_ede_rcode >= 0 ? FORMAT_DNS_EDE_RCODE(q->answer_ede_rcode) : "",
231
0
                                          (q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg)) ? ": " : "",
232
0
                                          q->answer_ede_rcode >= 0 ? strempty(q->answer_ede_msg) : "",
233
0
                                          q->answer_ede_rcode >= 0 ? ")" : "");
234
0
                }
235
236
0
                return sd_bus_reply_method_error(req, &error);
237
0
        }
238
239
0
        case DNS_TRANSACTION_NULL:
240
0
        case DNS_TRANSACTION_PENDING:
241
0
        case DNS_TRANSACTION_VALIDATING:
242
0
        case DNS_TRANSACTION_SUCCESS:
243
0
        default:
244
0
                assert_not_reached();
245
0
        }
246
0
}
247
248
0
static int append_address(sd_bus_message *reply, DnsResourceRecord *rr, int ifindex) {
249
0
        int r;
250
251
0
        assert(reply);
252
0
        assert(rr);
253
254
0
        r = sd_bus_message_open_container(reply, 'r', "iiay");
255
0
        if (r < 0)
256
0
                return r;
257
258
0
        r = sd_bus_message_append(reply, "i", ifindex);
259
0
        if (r < 0)
260
0
                return r;
261
262
0
        if (rr->key->type == DNS_TYPE_A) {
263
0
                r = sd_bus_message_append(reply, "i", AF_INET);
264
0
                if (r < 0)
265
0
                        return r;
266
267
0
                r = sd_bus_message_append_array(reply, 'y', &rr->a.in_addr, sizeof(struct in_addr));
268
269
0
        } else if (rr->key->type == DNS_TYPE_AAAA) {
270
0
                r = sd_bus_message_append(reply, "i", AF_INET6);
271
0
                if (r < 0)
272
0
                        return r;
273
274
0
                r = sd_bus_message_append_array(reply, 'y', &rr->aaaa.in6_addr, sizeof(struct in6_addr));
275
0
        } else
276
0
                return -EAFNOSUPPORT;
277
278
0
        if (r < 0)
279
0
                return r;
280
281
0
        r = sd_bus_message_close_container(reply);
282
0
        if (r < 0)
283
0
                return r;
284
285
0
        return 0;
286
0
}
287
288
0
static void bus_method_resolve_hostname_complete(DnsQuery *query) {
289
0
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
290
0
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
291
0
        _cleanup_(dns_query_freep) DnsQuery *q = query;
292
0
        _cleanup_free_ char *normalized = NULL;
293
0
        DnsQuestion *question;
294
0
        DnsResourceRecord *rr;
295
0
        unsigned added = 0;
296
0
        int ifindex, r;
297
298
0
        assert(q);
299
300
0
        if (q->state != DNS_TRANSACTION_SUCCESS) {
301
0
                r = reply_query_state(q);
302
0
                goto finish;
303
0
        }
304
305
0
        r = dns_query_process_cname_many(q);
306
0
        if (r == -ELOOP) {
307
0
                r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
308
0
                goto finish;
309
0
        }
310
0
        if (r < 0)
311
0
                goto finish;
312
0
        if (r == DNS_QUERY_CNAME) {
313
                /* This was a cname, and the query was restarted. */
314
0
                TAKE_PTR(q);
315
0
                return;
316
0
        }
317
318
0
        r = sd_bus_message_new_method_return(q->bus_request, &reply);
319
0
        if (r < 0)
320
0
                goto finish;
321
322
0
        r = sd_bus_message_open_container(reply, 'a', "(iiay)");
323
0
        if (r < 0)
324
0
                goto finish;
325
326
0
        question = dns_query_question_for_protocol(q, q->answer_protocol);
327
328
0
        DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
329
330
0
                r = dns_question_matches_rr(question, rr, DNS_SEARCH_DOMAIN_NAME(q->answer_search_domain));
331
0
                if (r < 0)
332
0
                        goto finish;
333
0
                if (r == 0)
334
0
                        continue;
335
336
0
                r = append_address(reply, rr, ifindex);
337
0
                if (r < 0)
338
0
                        goto finish;
339
340
0
                if (!canonical)
341
0
                        canonical = dns_resource_record_ref(rr);
342
343
0
                added++;
344
0
        }
345
346
0
        if (added <= 0) {
347
0
                r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
348
0
                goto finish;
349
0
        }
350
351
0
        r = sd_bus_message_close_container(reply);
352
0
        if (r < 0)
353
0
                goto finish;
354
355
        /* The key names are not necessarily normalized, make sure that they are when we return them to our
356
         * bus clients. */
357
0
        assert(canonical);
358
0
        r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
359
0
        if (r < 0)
360
0
                goto finish;
361
362
        /* Return the precise spelling and uppercasing and CNAME target reported by the server */
363
0
        r = sd_bus_message_append(
364
0
                        reply, "st",
365
0
                        normalized,
366
0
                        dns_query_reply_flags_make(q));
367
0
        if (r < 0)
368
0
                goto finish;
369
370
0
        q->bus_request = sd_bus_message_unref(q->bus_request);
371
0
        r = sd_bus_send(q->manager->bus, reply, NULL);
372
373
0
finish:
374
0
        if (r < 0) {
375
0
                log_error_errno(r, "Failed to send hostname reply: %m");
376
0
                (void) reply_method_errnof(q, r, NULL);
377
0
        }
378
0
}
379
380
0
static int parse_as_address(sd_bus_message *m, int ifindex, const char *hostname, int family, uint64_t flags) {
381
0
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
382
0
        _cleanup_free_ char *canonical = NULL;
383
0
        union in_addr_union parsed;
384
0
        int r, ff, parsed_ifindex = 0;
385
386
        /* Check if the hostname is actually already an IP address formatted as string. In that case just parse it,
387
         * let's not attempt to look it up. */
388
389
0
        r = in_addr_ifindex_from_string_auto(hostname, &ff, &parsed, &parsed_ifindex);
390
0
        if (r < 0) /* not an address */
391
0
                return 0;
392
393
0
        if (family != AF_UNSPEC && ff != family)
394
0
                return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address is not of the requested family.");
395
0
        if (ifindex > 0 && parsed_ifindex > 0 && parsed_ifindex != ifindex)
396
0
                return sd_bus_reply_method_errorf(m, BUS_ERROR_NO_SUCH_RR, "The specified address interface index does not match requested interface.");
397
398
0
        if (parsed_ifindex > 0)
399
0
                ifindex = parsed_ifindex;
400
401
0
        r = sd_bus_message_new_method_return(m, &reply);
402
0
        if (r < 0)
403
0
                return r;
404
405
0
        r = sd_bus_message_open_container(reply, 'a', "(iiay)");
406
0
        if (r < 0)
407
0
                return r;
408
409
0
        r = sd_bus_message_open_container(reply, 'r', "iiay");
410
0
        if (r < 0)
411
0
                return r;
412
413
0
        r = sd_bus_message_append(reply, "ii", ifindex, ff);
414
0
        if (r < 0)
415
0
                return r;
416
417
0
        r = sd_bus_message_append_array(reply, 'y', &parsed, FAMILY_ADDRESS_SIZE(ff));
418
0
        if (r < 0)
419
0
                return r;
420
421
0
        r = sd_bus_message_close_container(reply);
422
0
        if (r < 0)
423
0
                return r;
424
425
0
        r = sd_bus_message_close_container(reply);
426
0
        if (r < 0)
427
0
                return r;
428
429
        /* When an IP address is specified we just return it as canonical name, in order to avoid a DNS
430
         * look-up. However, we reformat it to make sure it's in a truly canonical form (i.e. on IPv6 the inner
431
         * omissions are always done the same way). */
432
0
        r = in_addr_ifindex_to_string(ff, &parsed, ifindex, &canonical);
433
0
        if (r < 0)
434
0
                return r;
435
436
0
        r = sd_bus_message_append(reply, "st", canonical,
437
0
                                  SD_RESOLVED_FLAGS_MAKE(dns_synthesize_protocol(flags), ff, true, true) |
438
0
                                  SD_RESOLVED_SYNTHETIC);
439
0
        if (r < 0)
440
0
                return r;
441
442
0
        return sd_bus_send(sd_bus_message_get_bus(m), reply, NULL);
443
0
}
444
445
0
void bus_client_log(sd_bus_message *m, const char *what) {
446
0
        _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
447
0
        const char *comm = NULL;
448
0
        uid_t uid = UID_INVALID;
449
0
        pid_t pid = 0;
450
0
        int r;
451
452
0
        assert(m);
453
0
        assert(what);
454
455
0
        if (!DEBUG_LOGGING)
456
0
                return;
457
458
0
        r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_COMM|SD_BUS_CREDS_AUGMENT, &creds);
459
0
        if (r < 0)
460
0
                return (void) log_debug_errno(r, "Failed to query client credentials, ignoring: %m");
461
462
0
        (void) sd_bus_creds_get_uid(creds, &uid);
463
0
        (void) sd_bus_creds_get_pid(creds, &pid);
464
0
        (void) sd_bus_creds_get_comm(creds, &comm);
465
466
0
        log_debug("D-Bus %s request from client PID " PID_FMT " (%s) with UID " UID_FMT,
467
0
                  what, pid, strna(comm), uid);
468
0
}
469
470
0
static int bus_method_resolve_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) {
471
0
        _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
472
0
        _cleanup_(dns_query_freep) DnsQuery *q = NULL;
473
0
        Manager *m = ASSERT_PTR(userdata);
474
0
        const char *hostname;
475
0
        int family, ifindex;
476
0
        uint64_t flags;
477
0
        int r;
478
479
0
        assert(message);
480
481
0
        assert_cc(sizeof(int) == sizeof(int32_t));
482
483
0
        r = sd_bus_message_read(message, "isit", &ifindex, &hostname, &family, &flags);
484
0
        if (r < 0)
485
0
                return r;
486
487
0
        if (ifindex < 0)
488
0
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
489
490
0
        if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
491
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
492
493
0
        if (validate_and_mangle_query_flags(m, &flags, hostname, SD_RESOLVED_NO_SEARCH) < 0)
494
0
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
495
496
0
        r = parse_as_address(message, ifindex, hostname, family, flags);
497
0
        if (r != 0)
498
0
                return r;
499
500
0
        r = dns_name_is_valid(hostname);
501
0
        if (r < 0)
502
0
                return r;
503
0
        if (r == 0)
504
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", hostname);
505
506
0
        r = dns_question_new_address(&question_utf8, family, hostname, false);
507
0
        if (r < 0)
508
0
                return r;
509
510
0
        r = dns_question_new_address(&question_idna, family, hostname, true);
511
0
        if (r < 0 && r != -EALREADY)
512
0
                return r;
513
514
0
        bus_client_log(message, "hostname resolution");
515
516
0
        r = dns_query_new_for_bus(m, &q, question_utf8, question_idna ?: question_utf8, NULL, ifindex, flags, error);
517
0
        if (r < 0)
518
0
                return r;
519
520
0
        q->bus_request = sd_bus_message_ref(message);
521
0
        q->request_family = family;
522
0
        q->complete = bus_method_resolve_hostname_complete;
523
524
0
        r = dns_query_bus_track(q, message);
525
0
        if (r < 0)
526
0
                return r;
527
528
0
        r = dns_query_go(q);
529
0
        if (r < 0)
530
0
                return r;
531
532
0
        TAKE_PTR(q);
533
0
        return 1;
534
0
}
535
536
0
static void bus_method_resolve_address_complete(DnsQuery *query) {
537
0
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
538
0
        _cleanup_(dns_query_freep) DnsQuery *q = query;
539
0
        DnsQuestion *question;
540
0
        DnsResourceRecord *rr;
541
0
        unsigned added = 0;
542
0
        int ifindex, r;
543
544
0
        assert(q);
545
546
0
        if (q->state != DNS_TRANSACTION_SUCCESS) {
547
0
                r = reply_query_state(q);
548
0
                goto finish;
549
0
        }
550
551
0
        r = dns_query_process_cname_many(q);
552
0
        if (r == -ELOOP) {
553
0
                r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
554
0
                goto finish;
555
0
        }
556
0
        if (r < 0)
557
0
                goto finish;
558
0
        if (r == DNS_QUERY_CNAME) {
559
                /* This was a cname, and the query was restarted. */
560
0
                TAKE_PTR(q);
561
0
                return;
562
0
        }
563
564
0
        r = sd_bus_message_new_method_return(q->bus_request, &reply);
565
0
        if (r < 0)
566
0
                goto finish;
567
568
0
        r = sd_bus_message_open_container(reply, 'a', "(is)");
569
0
        if (r < 0)
570
0
                goto finish;
571
572
0
        question = dns_query_question_for_protocol(q, q->answer_protocol);
573
574
0
        DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
575
0
                _cleanup_free_ char *normalized = NULL;
576
577
0
                r = dns_question_matches_rr(question, rr, NULL);
578
0
                if (r < 0)
579
0
                        goto finish;
580
0
                if (r == 0)
581
0
                        continue;
582
583
0
                r = dns_name_normalize(rr->ptr.name, 0, &normalized);
584
0
                if (r < 0)
585
0
                        goto finish;
586
587
0
                r = sd_bus_message_append(reply, "(is)", ifindex, normalized);
588
0
                if (r < 0)
589
0
                        goto finish;
590
591
0
                added++;
592
0
        }
593
594
0
        if (added <= 0) {
595
0
                r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR,
596
0
                                        "Address %s does not have any RR of requested type",
597
0
                                        IN_ADDR_TO_STRING(q->request_family, &q->request_address));
598
0
                goto finish;
599
0
        }
600
601
0
        r = sd_bus_message_close_container(reply);
602
0
        if (r < 0)
603
0
                goto finish;
604
605
0
        r = sd_bus_message_append(reply, "t", dns_query_reply_flags_make(q));
606
0
        if (r < 0)
607
0
                goto finish;
608
609
0
        q->bus_request = sd_bus_message_unref(q->bus_request);
610
0
        r = sd_bus_send(q->manager->bus, reply, NULL);
611
612
0
finish:
613
0
        if (r < 0) {
614
0
                log_error_errno(r, "Failed to send address reply: %m");
615
0
                (void) reply_method_errnof(q, r, NULL);
616
0
        }
617
0
}
618
619
0
static int bus_method_resolve_address(sd_bus_message *message, void *userdata, sd_bus_error *error) {
620
0
        _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
621
0
        _cleanup_(dns_query_freep) DnsQuery *q = NULL;
622
0
        Manager *m = ASSERT_PTR(userdata);
623
0
        union in_addr_union a;
624
0
        int family, ifindex;
625
0
        uint64_t flags;
626
0
        int r;
627
628
0
        assert(message);
629
630
0
        assert_cc(sizeof(int) == sizeof(int32_t));
631
632
0
        r = sd_bus_message_read(message, "i", &ifindex);
633
0
        if (r < 0)
634
0
                return r;
635
636
0
        r = bus_message_read_in_addr_auto(message, error, &family, &a);
637
0
        if (r < 0)
638
0
                return r;
639
640
0
        r = sd_bus_message_read(message, "t", &flags);
641
0
        if (r < 0)
642
0
                return r;
643
644
0
        if (ifindex < 0)
645
0
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
646
647
0
        if (validate_and_mangle_query_flags(m, &flags, /* name = */ NULL, /* ok = */ 0) < 0)
648
0
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
649
650
0
        r = dns_question_new_reverse(&question, family, &a);
651
0
        if (r < 0)
652
0
                return r;
653
654
0
        bus_client_log(message, "address resolution");
655
656
0
        r = dns_query_new_for_bus(m, &q, question, question, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH, error);
657
0
        if (r < 0)
658
0
                return r;
659
660
0
        q->bus_request = sd_bus_message_ref(message);
661
0
        q->request_family = family;
662
0
        q->request_address = a;
663
0
        q->complete = bus_method_resolve_address_complete;
664
665
0
        r = dns_query_bus_track(q, message);
666
0
        if (r < 0)
667
0
                return r;
668
669
0
        r = dns_query_go(q);
670
0
        if (r < 0)
671
0
                return r;
672
673
0
        TAKE_PTR(q);
674
0
        return 1;
675
0
}
676
677
0
static int bus_message_append_rr(sd_bus_message *m, DnsResourceRecord *rr, int ifindex) {
678
0
        int r;
679
680
0
        assert(m);
681
0
        assert(rr);
682
683
0
        r = sd_bus_message_open_container(m, 'r', "iqqay");
684
0
        if (r < 0)
685
0
                return r;
686
687
0
        r = sd_bus_message_append(m, "iqq",
688
0
                                  ifindex,
689
0
                                  rr->key->class,
690
0
                                  rr->key->type);
691
0
        if (r < 0)
692
0
                return r;
693
694
0
        r = dns_resource_record_to_wire_format(rr, false);
695
0
        if (r < 0)
696
0
                return r;
697
698
0
        r = sd_bus_message_append_array(m, 'y', rr->wire_format, rr->wire_format_size);
699
0
        if (r < 0)
700
0
                return r;
701
702
0
        return sd_bus_message_close_container(m);
703
0
}
704
705
0
static void bus_method_resolve_record_complete(DnsQuery *query) {
706
0
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
707
0
        _cleanup_(dns_query_freep) DnsQuery *q = query;
708
0
        DnsResourceRecord *rr;
709
0
        DnsQuestion *question;
710
0
        unsigned added = 0;
711
0
        int ifindex;
712
0
        int r;
713
714
0
        assert(q);
715
716
0
        if (q->state != DNS_TRANSACTION_SUCCESS) {
717
0
                r = reply_query_state(q);
718
0
                goto finish;
719
0
        }
720
721
0
        r = dns_query_process_cname_many(q);
722
0
        if (r == -ELOOP) {
723
0
                r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
724
0
                goto finish;
725
0
        }
726
0
        if (r < 0)
727
0
                goto finish;
728
0
        if (r == DNS_QUERY_CNAME) {
729
                /* This was a cname, and the query was restarted. */
730
0
                TAKE_PTR(q);
731
0
                return;
732
0
        }
733
734
0
        r = sd_bus_message_new_method_return(q->bus_request, &reply);
735
0
        if (r < 0)
736
0
                goto finish;
737
738
0
        r = sd_bus_message_open_container(reply, 'a', "(iqqay)");
739
0
        if (r < 0)
740
0
                goto finish;
741
742
0
        question = dns_query_question_for_protocol(q, q->answer_protocol);
743
744
0
        DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
745
0
                r = dns_question_matches_rr(question, rr, NULL);
746
0
                if (r < 0)
747
0
                        goto finish;
748
0
                if (r == 0)
749
0
                        continue;
750
751
0
                r = bus_message_append_rr(reply, rr, ifindex);
752
0
                if (r < 0)
753
0
                        goto finish;
754
755
0
                added++;
756
0
        }
757
758
0
        if (added <= 0) {
759
0
                r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "Name '%s' does not have any RR of the requested type", dns_query_string(q));
760
0
                goto finish;
761
0
        }
762
763
0
        r = sd_bus_message_close_container(reply);
764
0
        if (r < 0)
765
0
                goto finish;
766
767
0
        r = sd_bus_message_append(reply, "t", dns_query_reply_flags_make(q));
768
0
        if (r < 0)
769
0
                goto finish;
770
771
0
        q->bus_request = sd_bus_message_unref(q->bus_request);
772
0
        r = sd_bus_send(q->manager->bus, reply, NULL);
773
774
0
finish:
775
0
        if (r < 0) {
776
0
                log_error_errno(r, "Failed to send record reply: %m");
777
0
                (void) reply_method_errnof(q, r, NULL);
778
0
        }
779
0
}
780
781
0
static int bus_method_resolve_record(sd_bus_message *message, void *userdata, sd_bus_error *error) {
782
0
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
783
0
        _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
784
0
        _cleanup_(dns_query_freep) DnsQuery *q = NULL;
785
0
        Manager *m = ASSERT_PTR(userdata);
786
0
        uint16_t class, type;
787
0
        const char *name;
788
0
        int r, ifindex;
789
0
        uint64_t flags;
790
791
0
        assert(message);
792
793
0
        assert_cc(sizeof(int) == sizeof(int32_t));
794
795
0
        r = sd_bus_message_read(message, "isqqt", &ifindex, &name, &class, &type, &flags);
796
0
        if (r < 0)
797
0
                return r;
798
799
0
        if (ifindex < 0)
800
0
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
801
802
0
        r = dns_name_is_valid(name);
803
0
        if (r < 0)
804
0
                return r;
805
0
        if (r == 0)
806
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid name '%s'", name);
807
808
0
        if (!dns_type_is_valid_query(type))
809
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified resource record type %" PRIu16 " may not be used in a query.", type);
810
0
        if (dns_type_is_zone_transfer(type))
811
0
                return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Zone transfers not permitted via this programming interface.");
812
0
        if (dns_type_is_obsolete(type))
813
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Specified DNS resource record type %" PRIu16 " is obsolete.", type);
814
815
0
        if (validate_and_mangle_query_flags(m, &flags, name, SD_RESOLVED_NO_SEARCH) < 0)
816
0
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
817
818
0
        question = dns_question_new(1);
819
0
        if (!question)
820
0
                return -ENOMEM;
821
822
0
        key = dns_resource_key_new(class, type, name);
823
0
        if (!key)
824
0
                return -ENOMEM;
825
826
0
        r = dns_question_add(question, key, 0);
827
0
        if (r < 0)
828
0
                return r;
829
830
0
        bus_client_log(message, "resource record resolution");
831
832
        /* Setting SD_RESOLVED_CLAMP_TTL: let's request that the TTL is fixed up for locally cached entries,
833
         * after all we return it in the wire format blob. */
834
0
        r = dns_query_new_for_bus(m, &q, question, question, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH|SD_RESOLVED_CLAMP_TTL, error);
835
0
        if (r < 0)
836
0
                return r;
837
838
0
        q->bus_request = sd_bus_message_ref(message);
839
0
        q->complete = bus_method_resolve_record_complete;
840
841
0
        r = dns_query_bus_track(q, message);
842
0
        if (r < 0)
843
0
                return r;
844
845
0
        r = dns_query_go(q);
846
0
        if (r < 0)
847
0
                return r;
848
849
0
        TAKE_PTR(q);
850
0
        return 1;
851
0
}
852
853
0
static int append_srv(DnsQuery *q, sd_bus_message *reply, DnsResourceRecord *rr) {
854
0
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
855
0
        _cleanup_free_ char *normalized = NULL;
856
0
        int r;
857
858
0
        assert(q);
859
0
        assert(reply);
860
0
        assert(rr);
861
0
        assert(rr->key);
862
863
0
        if (rr->key->type != DNS_TYPE_SRV)
864
0
                return 0;
865
866
0
        if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
867
                /* First, let's see if we could find an appropriate A or AAAA
868
                 * record for the SRV record */
869
0
                LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
870
0
                        DnsResourceRecord *zz;
871
0
                        DnsQuestion *question;
872
873
0
                        if (aux->state != DNS_TRANSACTION_SUCCESS)
874
0
                                continue;
875
0
                        if (aux->auxiliary_result != 0)
876
0
                                continue;
877
878
0
                        question = dns_query_question_for_protocol(aux, aux->answer_protocol);
879
880
0
                        r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
881
0
                        if (r < 0)
882
0
                                return r;
883
0
                        if (r == 0)
884
0
                                continue;
885
886
0
                        DNS_ANSWER_FOREACH(zz, aux->answer) {
887
888
0
                                r = dns_question_matches_rr(question, zz, NULL);
889
0
                                if (r < 0)
890
0
                                        return r;
891
0
                                if (r == 0)
892
0
                                        continue;
893
894
0
                                canonical = dns_resource_record_ref(zz);
895
0
                                break;
896
0
                        }
897
898
0
                        if (canonical)
899
0
                                break;
900
0
                }
901
902
                /* Is there are successful A/AAAA lookup for this SRV RR? If not, don't add it */
903
0
                if (!canonical)
904
0
                        return 0;
905
0
        }
906
907
0
        r = sd_bus_message_open_container(reply, 'r', "qqqsa(iiay)s");
908
0
        if (r < 0)
909
0
                return r;
910
911
0
        r = dns_name_normalize(rr->srv.name, 0, &normalized);
912
0
        if (r < 0)
913
0
                return r;
914
915
0
        r = sd_bus_message_append(
916
0
                        reply,
917
0
                        "qqqs",
918
0
                        rr->srv.priority, rr->srv.weight, rr->srv.port, normalized);
919
0
        if (r < 0)
920
0
                return r;
921
922
0
        r = sd_bus_message_open_container(reply, 'a', "(iiay)");
923
0
        if (r < 0)
924
0
                return r;
925
926
0
        if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
927
0
                LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
928
0
                        DnsResourceRecord *zz;
929
0
                        DnsQuestion *question;
930
0
                        int ifindex;
931
932
0
                        if (aux->state != DNS_TRANSACTION_SUCCESS)
933
0
                                continue;
934
0
                        if (aux->auxiliary_result != 0)
935
0
                                continue;
936
937
0
                        question = dns_query_question_for_protocol(aux, aux->answer_protocol);
938
939
0
                        r = dns_name_equal(dns_question_first_name(question), rr->srv.name);
940
0
                        if (r < 0)
941
0
                                return r;
942
0
                        if (r == 0)
943
0
                                continue;
944
945
0
                        DNS_ANSWER_FOREACH_IFINDEX(zz, ifindex, aux->answer) {
946
947
0
                                r = dns_question_matches_rr(question, zz, NULL);
948
0
                                if (r < 0)
949
0
                                        return r;
950
0
                                if (r == 0)
951
0
                                        continue;
952
953
0
                                r = append_address(reply, zz, ifindex);
954
0
                                if (r < 0)
955
0
                                        return r;
956
0
                        }
957
0
                }
958
0
        }
959
960
0
        r = sd_bus_message_close_container(reply);
961
0
        if (r < 0)
962
0
                return r;
963
964
0
        if (canonical) {
965
0
                normalized = mfree(normalized);
966
967
0
                r = dns_name_normalize(dns_resource_key_name(canonical->key), 0, &normalized);
968
0
                if (r < 0)
969
0
                        return r;
970
0
        }
971
972
        /* Note that above we appended the hostname as encoded in the
973
         * SRV, and here the canonical hostname this maps to. */
974
0
        r = sd_bus_message_append(reply, "s", normalized);
975
0
        if (r < 0)
976
0
                return r;
977
978
0
        r = sd_bus_message_close_container(reply);
979
0
        if (r < 0)
980
0
                return r;
981
982
0
        return 1;
983
0
}
984
985
0
static int append_txt(sd_bus_message *reply, DnsResourceRecord *rr) {
986
0
        int r;
987
988
0
        assert(reply);
989
0
        assert(rr);
990
0
        assert(rr->key);
991
992
0
        if (rr->key->type != DNS_TYPE_TXT)
993
0
                return 0;
994
995
0
        LIST_FOREACH(items, i, rr->txt.items) {
996
997
0
                if (i->length <= 0)
998
0
                        continue;
999
1000
0
                r = sd_bus_message_append_array(reply, 'y', i->data, i->length);
1001
0
                if (r < 0)
1002
0
                        return r;
1003
0
        }
1004
1005
0
        return 1;
1006
0
}
1007
1008
0
static void resolve_service_all_complete(DnsQuery *query) {
1009
0
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *canonical = NULL;
1010
0
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1011
0
        _cleanup_free_ char *name = NULL, *type = NULL, *domain = NULL;
1012
0
        _cleanup_(dns_query_freep) DnsQuery *q = query;
1013
0
        DnsQuestion *question;
1014
0
        DnsResourceRecord *rr;
1015
0
        unsigned added = 0;
1016
0
        int r;
1017
1018
0
        assert(q);
1019
1020
0
        if (q->block_all_complete > 0) {
1021
0
                TAKE_PTR(q);
1022
0
                return;
1023
0
        }
1024
1025
0
        if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1026
0
                DnsQuery *bad = NULL;
1027
0
                bool have_success = false;
1028
1029
0
                LIST_FOREACH(auxiliary_queries, aux, q->auxiliary_queries) {
1030
1031
0
                        switch (aux->state) {
1032
1033
0
                        case DNS_TRANSACTION_PENDING:
1034
                                /* If an auxiliary query is still pending, let's wait */
1035
0
                                TAKE_PTR(q);
1036
0
                                return;
1037
1038
0
                        case DNS_TRANSACTION_SUCCESS:
1039
0
                                if (aux->auxiliary_result == 0)
1040
0
                                        have_success = true;
1041
0
                                else
1042
0
                                        bad = aux;
1043
0
                                break;
1044
1045
0
                        default:
1046
0
                                bad = aux;
1047
0
                        }
1048
0
                }
1049
1050
0
                if (!have_success) {
1051
                        /* We can only return one error, hence pick the last error we encountered */
1052
1053
0
                        assert(bad);
1054
1055
0
                        if (bad->state == DNS_TRANSACTION_SUCCESS) {
1056
0
                                assert(bad->auxiliary_result != 0);
1057
1058
0
                                if (bad->auxiliary_result == -ELOOP) {
1059
0
                                        r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(bad));
1060
0
                                        goto finish;
1061
0
                                }
1062
1063
0
                                assert(bad->auxiliary_result < 0);
1064
0
                                r = bad->auxiliary_result;
1065
0
                                goto finish;
1066
0
                        }
1067
1068
0
                        r = reply_query_state(bad);
1069
0
                        goto finish;
1070
0
                }
1071
0
        }
1072
1073
0
        r = sd_bus_message_new_method_return(q->bus_request, &reply);
1074
0
        if (r < 0)
1075
0
                goto finish;
1076
1077
0
        r = sd_bus_message_open_container(reply, 'a', "(qqqsa(iiay)s)");
1078
0
        if (r < 0)
1079
0
                goto finish;
1080
1081
0
        question = dns_query_question_for_protocol(q, q->answer_protocol);
1082
1083
0
        DNS_ANSWER_FOREACH(rr, q->answer) {
1084
0
                r = dns_question_matches_rr(question, rr, NULL);
1085
0
                if (r < 0)
1086
0
                        goto finish;
1087
0
                if (r == 0)
1088
0
                        continue;
1089
1090
0
                r = append_srv(q, reply, rr);
1091
0
                if (r < 0)
1092
0
                        goto finish;
1093
0
                if (r == 0) /* not an SRV record */
1094
0
                        continue;
1095
1096
0
                if (!canonical)
1097
0
                        canonical = dns_resource_record_ref(rr);
1098
1099
0
                added++;
1100
0
        }
1101
1102
0
        if (added <= 0) {
1103
0
                r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
1104
0
                goto finish;
1105
0
        }
1106
1107
0
        r = sd_bus_message_close_container(reply);
1108
0
        if (r < 0)
1109
0
                goto finish;
1110
1111
0
        r = sd_bus_message_open_container(reply, 'a', "ay");
1112
0
        if (r < 0)
1113
0
                goto finish;
1114
1115
0
        DNS_ANSWER_FOREACH(rr, q->answer) {
1116
0
                r = dns_question_matches_rr(question, rr, NULL);
1117
0
                if (r < 0)
1118
0
                        goto finish;
1119
0
                if (r == 0)
1120
0
                        continue;
1121
1122
0
                r = append_txt(reply, rr);
1123
0
                if (r < 0)
1124
0
                        goto finish;
1125
0
        }
1126
1127
0
        r = sd_bus_message_close_container(reply);
1128
0
        if (r < 0)
1129
0
                goto finish;
1130
1131
0
        assert(canonical);
1132
0
        r = dns_service_split(dns_resource_key_name(canonical->key), &name, &type, &domain);
1133
0
        if (r < 0)
1134
0
                goto finish;
1135
1136
0
        if (isempty(type)) {
1137
0
                r = reply_method_errorf(q, BUS_ERROR_INCONSISTENT_SERVICE_RECORDS, "'%s' does not provide a consistent set of service resource records", dns_query_string(q));
1138
0
                goto finish;
1139
0
        }
1140
1141
0
        r = sd_bus_message_append(
1142
0
                        reply,
1143
0
                        "ssst",
1144
0
                        name, type, domain,
1145
0
                        dns_query_reply_flags_make(q));
1146
0
        if (r < 0)
1147
0
                goto finish;
1148
1149
0
        q->bus_request = sd_bus_message_unref(q->bus_request);
1150
0
        r = sd_bus_send(q->manager->bus, reply, NULL);
1151
1152
0
finish:
1153
0
        if (r < 0) {
1154
0
                log_error_errno(r, "Failed to send service reply: %m");
1155
0
                (void) reply_method_errnof(q, r, NULL);
1156
0
        }
1157
0
}
1158
1159
0
static void resolve_service_hostname_complete(DnsQuery *q) {
1160
0
        int r;
1161
1162
0
        assert(q);
1163
0
        assert(q->auxiliary_for);
1164
1165
0
        if (q->state != DNS_TRANSACTION_SUCCESS) {
1166
0
                resolve_service_all_complete(q->auxiliary_for);
1167
0
                return;
1168
0
        }
1169
1170
0
        r = dns_query_process_cname_many(q);
1171
0
        if (r == DNS_QUERY_CNAME) /* This was a cname, and the query was restarted. */
1172
0
                return;
1173
1174
        /* This auxiliary lookup is finished or failed, let's see if all are finished now. */
1175
0
        q->auxiliary_result = r < 0 ? r : 0;
1176
0
        resolve_service_all_complete(q->auxiliary_for);
1177
0
}
1178
1179
0
static int resolve_service_hostname(DnsQuery *q, DnsResourceRecord *rr, int ifindex) {
1180
0
        _cleanup_(dns_question_unrefp) DnsQuestion *question = NULL;
1181
0
        _cleanup_(dns_query_freep) DnsQuery *aux = NULL;
1182
0
        int r;
1183
1184
0
        assert(q);
1185
0
        assert(rr);
1186
0
        assert(rr->key);
1187
0
        assert(rr->key->type == DNS_TYPE_SRV);
1188
1189
        /* OK, we found an SRV record for the service. Let's resolve
1190
         * the hostname included in it */
1191
1192
0
        r = dns_question_new_address(&question, q->request_family, rr->srv.name, false);
1193
0
        if (r < 0)
1194
0
                return r;
1195
1196
0
        r = dns_query_new(q->manager, &aux, question, question, NULL, ifindex, q->flags|SD_RESOLVED_NO_SEARCH);
1197
0
        if (r == -ENOANO)
1198
0
                return reply_method_errorf(q, BUS_ERROR_DNS_REFUSED, "DNS query type refused.");
1199
0
        if (r < 0)
1200
0
                return r;
1201
1202
0
        aux->request_family = q->request_family;
1203
0
        aux->complete = resolve_service_hostname_complete;
1204
1205
0
        r = dns_query_make_auxiliary(aux, q);
1206
0
        if (r == -EAGAIN)
1207
                /* Too many auxiliary lookups? If so, don't complain,
1208
                 * let's just not add this one, we already have more
1209
                 * than enough */
1210
0
                return 0;
1211
0
        if (r < 0)
1212
0
                return r;
1213
1214
        /* Note that auxiliary queries do not track the original bus
1215
         * client, only the primary request does that. */
1216
1217
0
        r = dns_query_go(aux);
1218
0
        if (r < 0)
1219
0
                return r;
1220
1221
0
        TAKE_PTR(aux);
1222
0
        return 1;
1223
0
}
1224
1225
0
static void bus_method_resolve_service_complete(DnsQuery *query) {
1226
0
        _cleanup_(dns_query_freep) DnsQuery *q = query;
1227
0
        bool has_root_domain = false;
1228
0
        DnsResourceRecord *rr;
1229
0
        DnsQuestion *question;
1230
0
        unsigned found = 0;
1231
0
        int ifindex, r;
1232
1233
0
        assert(q);
1234
1235
0
        if (q->state != DNS_TRANSACTION_SUCCESS) {
1236
0
                r = reply_query_state(q);
1237
0
                goto finish;
1238
0
        }
1239
1240
0
        r = dns_query_process_cname_many(q);
1241
0
        if (r == -ELOOP) {
1242
0
                r = reply_method_errorf(q, BUS_ERROR_CNAME_LOOP, "CNAME loop detected, or CNAME resolving disabled on '%s'", dns_query_string(q));
1243
0
                goto finish;
1244
0
        }
1245
0
        if (r < 0)
1246
0
                goto finish;
1247
0
        if (r == DNS_QUERY_CNAME) {
1248
                /* This was a cname, and the query was restarted. */
1249
0
                TAKE_PTR(q);
1250
0
                return;
1251
0
        }
1252
1253
0
        question = dns_query_question_for_protocol(q, q->answer_protocol);
1254
1255
0
        DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
1256
0
                r = dns_question_matches_rr(question, rr, NULL);
1257
0
                if (r < 0)
1258
0
                        goto finish;
1259
0
                if (r == 0)
1260
0
                        continue;
1261
1262
0
                if (rr->key->type != DNS_TYPE_SRV)
1263
0
                        continue;
1264
1265
0
                if (dns_name_is_root(rr->srv.name)) {
1266
0
                        has_root_domain = true;
1267
0
                        continue;
1268
0
                }
1269
1270
0
                if ((q->flags & SD_RESOLVED_NO_ADDRESS) == 0) {
1271
0
                        q->block_all_complete++;
1272
0
                        r = resolve_service_hostname(q, rr, ifindex);
1273
0
                        q->block_all_complete--;
1274
1275
0
                        if (r < 0)
1276
0
                                goto finish;
1277
0
                }
1278
1279
0
                found++;
1280
0
        }
1281
1282
0
        if (has_root_domain && found <= 0) {
1283
                /* If there's exactly one SRV RR and it uses the root domain as hostname, then the service is
1284
                 * explicitly not offered on the domain. Report this as a recognizable error. See RFC 2782,
1285
                 * Section "Usage Rules". */
1286
0
                r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_SERVICE, "'%s' does not provide the requested service", dns_query_string(q));
1287
0
                goto finish;
1288
0
        }
1289
1290
0
        if (found <= 0) {
1291
0
                r = reply_method_errorf(q, BUS_ERROR_NO_SUCH_RR, "'%s' does not have any RR of the requested type", dns_query_string(q));
1292
0
                goto finish;
1293
0
        }
1294
1295
        /* Maybe we are already finished? check now... */
1296
0
        resolve_service_all_complete(TAKE_PTR(q));
1297
0
        return;
1298
1299
0
finish:
1300
0
        if (r < 0) {
1301
0
                log_error_errno(r, "Failed to send service reply: %m");
1302
0
                (void) reply_method_errnof(q, r, NULL);
1303
0
        }
1304
0
}
1305
1306
0
static int bus_method_resolve_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1307
0
        _cleanup_(dns_question_unrefp) DnsQuestion *question_idna = NULL, *question_utf8 = NULL;
1308
0
        _cleanup_(dns_query_freep) DnsQuery *q = NULL;
1309
0
        const char *name, *type, *domain;
1310
0
        Manager *m = ASSERT_PTR(userdata);
1311
0
        int family, ifindex;
1312
0
        uint64_t flags;
1313
0
        int r;
1314
1315
0
        assert(message);
1316
1317
0
        assert_cc(sizeof(int) == sizeof(int32_t));
1318
1319
0
        r = sd_bus_message_read(message, "isssit", &ifindex, &name, &type, &domain, &family, &flags);
1320
0
        if (r < 0)
1321
0
                return r;
1322
1323
0
        if (ifindex < 0)
1324
0
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index");
1325
1326
0
        if (!IN_SET(family, AF_INET, AF_INET6, AF_UNSPEC))
1327
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family);
1328
1329
0
        if (isempty(name))
1330
0
                name = NULL;
1331
0
        else if (!dns_service_name_is_valid(name))
1332
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid service name '%s'", name);
1333
1334
0
        if (isempty(type))
1335
0
                type = NULL;
1336
0
        else if (!dns_srv_type_is_valid(type))
1337
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid SRV service type '%s'", type);
1338
1339
0
        r = dns_name_is_valid(domain);
1340
0
        if (r < 0)
1341
0
                return r;
1342
0
        if (r == 0)
1343
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid domain '%s'", domain);
1344
1345
0
        if (name && !type)
1346
0
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Service name cannot be specified without service type.");
1347
1348
0
        if (validate_and_mangle_query_flags(m, &flags, name, SD_RESOLVED_NO_TXT|SD_RESOLVED_NO_ADDRESS) < 0)
1349
0
                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter");
1350
1351
        /* Refuse the method if SRV is filtered. */
1352
0
        if (set_contains(m->refuse_record_types, INT_TO_PTR(DNS_TYPE_SRV)))
1353
0
                return sd_bus_error_set(error, BUS_ERROR_DNS_REFUSED, "DNS query type refused.");
1354
1355
0
        r = dns_question_new_service(&question_utf8, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), false);
1356
0
        if (r < 0)
1357
0
                return r;
1358
1359
0
        r = dns_question_new_service(&question_idna, name, type, domain, !(flags & SD_RESOLVED_NO_TXT), true);
1360
0
        if (r < 0)
1361
0
                return r;
1362
1363
0
        bus_client_log(message, "service resolution");
1364
1365
0
        r = dns_query_new_for_bus(m, &q, question_utf8, question_idna, NULL, ifindex, flags|SD_RESOLVED_NO_SEARCH, error);
1366
0
        if (r < 0)
1367
0
                return r;
1368
1369
0
        q->bus_request = sd_bus_message_ref(message);
1370
0
        q->request_family = family;
1371
0
        q->complete = bus_method_resolve_service_complete;
1372
1373
0
        r = dns_query_bus_track(q, message);
1374
0
        if (r < 0)
1375
0
                return r;
1376
1377
0
        r = dns_query_go(q);
1378
0
        if (r < 0)
1379
0
                return r;
1380
1381
0
        TAKE_PTR(q);
1382
0
        return 1;
1383
0
}
1384
1385
int bus_dns_server_append(
1386
                sd_bus_message *reply,
1387
                DnsServer *s,
1388
                bool with_ifindex, /* include "ifindex" field */
1389
0
                bool extended) {   /* also include port number and server name */
1390
0
        int r;
1391
1392
0
        assert(reply);
1393
1394
0
        if (!s) {
1395
0
                if (with_ifindex) {
1396
0
                        if (extended)
1397
0
                                return sd_bus_message_append(reply, "(iiayqs)", 0, AF_UNSPEC, 0, 0, NULL);
1398
0
                        else
1399
0
                                return sd_bus_message_append(reply, "(iiay)", 0, AF_UNSPEC, 0);
1400
0
                } else {
1401
0
                        if (extended)
1402
0
                                return sd_bus_message_append(reply, "(iayqs)", AF_UNSPEC, 0, 0, NULL);
1403
0
                        else
1404
0
                                return sd_bus_message_append(reply, "(iay)", AF_UNSPEC, 0);
1405
0
                }
1406
0
        }
1407
1408
0
        r = sd_bus_message_open_container(
1409
0
                        reply,
1410
0
                        'r',
1411
0
                        with_ifindex ? (extended ? "iiayqs" : "iiay") :
1412
0
                                       (extended ? "iayqs" : "iay"));
1413
0
        if (r < 0)
1414
0
                return r;
1415
1416
0
        if (with_ifindex) {
1417
0
                r = sd_bus_message_append(reply, "i", dns_server_ifindex(s));
1418
0
                if (r < 0)
1419
0
                        return r;
1420
0
        }
1421
1422
0
        r = sd_bus_message_append(reply, "i", s->family);
1423
0
        if (r < 0)
1424
0
                return r;
1425
1426
0
        r = sd_bus_message_append_array(reply, 'y', &s->address, FAMILY_ADDRESS_SIZE(s->family));
1427
0
        if (r < 0)
1428
0
                return r;
1429
1430
0
        if (extended) {
1431
0
                r = sd_bus_message_append(reply, "q", s->port);
1432
0
                if (r < 0)
1433
0
                        return r;
1434
1435
0
                r = sd_bus_message_append(reply, "s", s->server_name);
1436
0
                if (r < 0)
1437
0
                        return r;
1438
0
        }
1439
1440
0
        return sd_bus_message_close_container(reply);
1441
0
}
1442
1443
static int bus_property_get_dns_servers_internal(
1444
                sd_bus *bus,
1445
                const char *path,
1446
                const char *interface,
1447
                const char *property,
1448
                sd_bus_message *reply,
1449
                void *userdata,
1450
                sd_bus_error *error,
1451
0
                bool extended) {
1452
1453
0
        Manager *m = ASSERT_PTR(userdata);
1454
0
        Link *l;
1455
0
        int r;
1456
1457
0
        assert(reply);
1458
1459
0
        r = sd_bus_message_open_container(reply, 'a', extended ? "(iiayqs)" : "(iiay)");
1460
0
        if (r < 0)
1461
0
                return r;
1462
1463
0
        LIST_FOREACH(servers, s, m->dns_servers) {
1464
0
                r = bus_dns_server_append(reply, s, true, extended);
1465
0
                if (r < 0)
1466
0
                        return r;
1467
0
        }
1468
1469
0
        HASHMAP_FOREACH(l, m->links)
1470
0
                LIST_FOREACH(servers, s, l->dns_servers) {
1471
0
                        r = bus_dns_server_append(reply, s, true, extended);
1472
0
                        if (r < 0)
1473
0
                                return r;
1474
0
                }
1475
1476
0
        return sd_bus_message_close_container(reply);
1477
0
}
1478
1479
static int bus_property_get_dns_servers(
1480
                sd_bus *bus,
1481
                const char *path,
1482
                const char *interface,
1483
                const char *property,
1484
                sd_bus_message *reply,
1485
                void *userdata,
1486
0
                sd_bus_error *error) {
1487
0
        return bus_property_get_dns_servers_internal(bus, path, interface, property, reply, userdata, error, false);
1488
0
}
1489
1490
static int bus_property_get_dns_servers_ex(
1491
                sd_bus *bus,
1492
                const char *path,
1493
                const char *interface,
1494
                const char *property,
1495
                sd_bus_message *reply,
1496
                void *userdata,
1497
0
                sd_bus_error *error) {
1498
0
        return bus_property_get_dns_servers_internal(bus, path, interface, property, reply, userdata, error, true);
1499
0
}
1500
1501
static int bus_property_get_fallback_dns_servers_internal(
1502
                sd_bus *bus,
1503
                const char *path,
1504
                const char *interface,
1505
                const char *property,
1506
                sd_bus_message *reply,
1507
                void *userdata,
1508
                sd_bus_error *error,
1509
0
                bool extended) {
1510
1511
0
        DnsServer **f = ASSERT_PTR(userdata);
1512
0
        int r;
1513
1514
0
        assert(reply);
1515
1516
0
        r = sd_bus_message_open_container(reply, 'a', extended ? "(iiayqs)" : "(iiay)");
1517
0
        if (r < 0)
1518
0
                return r;
1519
1520
0
        LIST_FOREACH(servers, s, *f) {
1521
0
                r = bus_dns_server_append(reply, s, true, extended);
1522
0
                if (r < 0)
1523
0
                        return r;
1524
0
        }
1525
1526
0
        return sd_bus_message_close_container(reply);
1527
0
}
1528
1529
static int bus_property_get_fallback_dns_servers(
1530
                sd_bus *bus,
1531
                const char *path,
1532
                const char *interface,
1533
                const char *property,
1534
                sd_bus_message *reply,
1535
                void *userdata,
1536
0
                sd_bus_error *error) {
1537
0
        return bus_property_get_fallback_dns_servers_internal(bus, path, interface, property, reply, userdata, error, false);
1538
0
}
1539
1540
static int bus_property_get_fallback_dns_servers_ex(
1541
                sd_bus *bus,
1542
                const char *path,
1543
                const char *interface,
1544
                const char *property,
1545
                sd_bus_message *reply,
1546
                void *userdata,
1547
0
                sd_bus_error *error) {
1548
0
        return bus_property_get_fallback_dns_servers_internal(bus, path, interface, property, reply, userdata, error, true);
1549
0
}
1550
1551
static int bus_property_get_current_dns_server_internal(
1552
                sd_bus *bus,
1553
                const char *path,
1554
                const char *interface,
1555
                const char *property,
1556
                sd_bus_message *reply,
1557
                void *userdata,
1558
                sd_bus_error *error,
1559
0
                bool extended) {
1560
1561
0
        DnsServer *s;
1562
1563
0
        assert(reply);
1564
0
        assert(userdata);
1565
1566
0
        s = *(DnsServer **) userdata;
1567
1568
0
        return bus_dns_server_append(reply, s, true, extended);
1569
0
}
1570
1571
static int bus_property_get_current_dns_server(
1572
                sd_bus *bus,
1573
                const char *path,
1574
                const char *interface,
1575
                const char *property,
1576
                sd_bus_message *reply,
1577
                void *userdata,
1578
0
                sd_bus_error *error) {
1579
0
        return bus_property_get_current_dns_server_internal(bus, path, interface, property, reply, userdata, error, false);
1580
0
}
1581
1582
static int bus_property_get_current_dns_server_ex(
1583
                sd_bus *bus,
1584
                const char *path,
1585
                const char *interface,
1586
                const char *property,
1587
                sd_bus_message *reply,
1588
                void *userdata,
1589
0
                sd_bus_error *error) {
1590
0
        return bus_property_get_current_dns_server_internal(bus, path, interface, property, reply, userdata, error, true);
1591
0
}
1592
1593
static int bus_property_get_domains(
1594
                sd_bus *bus,
1595
                const char *path,
1596
                const char *interface,
1597
                const char *property,
1598
                sd_bus_message *reply,
1599
                void *userdata,
1600
0
                sd_bus_error *error) {
1601
1602
0
        Manager *m = ASSERT_PTR(userdata);
1603
0
        Link *l;
1604
0
        int r;
1605
1606
0
        assert(reply);
1607
1608
0
        r = sd_bus_message_open_container(reply, 'a', "(isb)");
1609
0
        if (r < 0)
1610
0
                return r;
1611
1612
0
        LIST_FOREACH(domains, d, m->search_domains) {
1613
0
                r = sd_bus_message_append(reply, "(isb)", 0, d->name, d->route_only);
1614
0
                if (r < 0)
1615
0
                        return r;
1616
0
        }
1617
1618
0
        HASHMAP_FOREACH(l, m->links) {
1619
0
                LIST_FOREACH(domains, d, l->search_domains) {
1620
0
                        r = sd_bus_message_append(reply, "(isb)", l->ifindex, d->name, d->route_only);
1621
0
                        if (r < 0)
1622
0
                                return r;
1623
0
                }
1624
0
        }
1625
1626
0
        return sd_bus_message_close_container(reply);
1627
0
}
1628
1629
static int bus_property_get_transaction_statistics(
1630
                sd_bus *bus,
1631
                const char *path,
1632
                const char *interface,
1633
                const char *property,
1634
                sd_bus_message *reply,
1635
                void *userdata,
1636
0
                sd_bus_error *error) {
1637
1638
0
        Manager *m = ASSERT_PTR(userdata);
1639
1640
0
        assert(reply);
1641
1642
0
        return sd_bus_message_append(reply, "(tt)",
1643
0
                                     (uint64_t) hashmap_size(m->dns_transactions),
1644
0
                                     (uint64_t) m->n_transactions_total);
1645
0
}
1646
1647
static int bus_property_get_cache_statistics(
1648
                sd_bus *bus,
1649
                const char *path,
1650
                const char *interface,
1651
                const char *property,
1652
                sd_bus_message *reply,
1653
                void *userdata,
1654
0
                sd_bus_error *error) {
1655
1656
0
        uint64_t size = 0, hit = 0, miss = 0;
1657
0
        Manager *m = ASSERT_PTR(userdata);
1658
1659
0
        assert(reply);
1660
1661
0
        LIST_FOREACH(scopes, s, m->dns_scopes) {
1662
0
                size += dns_cache_size(&s->cache);
1663
0
                hit += s->cache.n_hit;
1664
0
                miss += s->cache.n_miss;
1665
0
        }
1666
1667
0
        return sd_bus_message_append(reply, "(ttt)", size, hit, miss);
1668
0
}
1669
1670
static int bus_property_get_dnssec_statistics(
1671
                sd_bus *bus,
1672
                const char *path,
1673
                const char *interface,
1674
                const char *property,
1675
                sd_bus_message *reply,
1676
                void *userdata,
1677
0
                sd_bus_error *error) {
1678
1679
0
        Manager *m = ASSERT_PTR(userdata);
1680
1681
0
        assert(reply);
1682
1683
0
        return sd_bus_message_append(reply, "(tttt)",
1684
0
                                     (uint64_t) m->n_dnssec_verdict[DNSSEC_SECURE],
1685
0
                                     (uint64_t) m->n_dnssec_verdict[DNSSEC_INSECURE],
1686
0
                                     (uint64_t) m->n_dnssec_verdict[DNSSEC_BOGUS],
1687
0
                                     (uint64_t) m->n_dnssec_verdict[DNSSEC_INDETERMINATE]);
1688
0
}
1689
1690
static BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_dns_stub_listener_mode, dns_stub_listener_mode, DnsStubListenerMode);
1691
static BUS_DEFINE_PROPERTY_GET(bus_property_get_dnssec_supported, "b", Manager, manager_dnssec_supported);
1692
static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dnssec_mode, "s", Manager, manager_get_dnssec_mode, dnssec_mode_to_string);
1693
static BUS_DEFINE_PROPERTY_GET2(bus_property_get_dns_over_tls_mode, "s", Manager, manager_get_dns_over_tls_mode, dns_over_tls_mode_to_string);
1694
1695
static int bus_property_get_resolv_conf_mode(
1696
                sd_bus *bus,
1697
                const char *path,
1698
                const char *interface,
1699
                const char *property,
1700
                sd_bus_message *reply,
1701
                void *userdata,
1702
0
                sd_bus_error *error) {
1703
1704
0
        int r;
1705
1706
0
        assert(reply);
1707
1708
0
        r = resolv_conf_mode();
1709
0
        if (r < 0) {
1710
0
                log_warning_errno(r, "Failed to test /etc/resolv.conf mode, ignoring: %m");
1711
0
                return sd_bus_message_append(reply, "s", NULL);
1712
0
        }
1713
1714
0
        return sd_bus_message_append(reply, "s", resolv_conf_mode_to_string(r));
1715
0
}
1716
1717
0
static int bus_method_reset_statistics(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1718
0
        Manager *m = ASSERT_PTR(userdata);
1719
1720
0
        assert(message);
1721
1722
0
        bus_client_log(message, "statistics reset");
1723
1724
0
        dns_manager_reset_statistics(m);
1725
1726
0
        return sd_bus_reply_method_return(message, NULL);
1727
0
}
1728
1729
0
static int get_any_link(Manager *m, int ifindex, Link **ret, sd_bus_error *error) {
1730
0
        Link *l;
1731
1732
0
        assert(m);
1733
0
        assert(ret);
1734
1735
0
        l = hashmap_get(m->links, INT_TO_PTR(ifindex));
1736
0
        if (!l)
1737
0
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_LINK, "Link %i not known", ifindex);
1738
1739
0
        *ret = l;
1740
0
        return 0;
1741
0
}
1742
1743
0
static int call_link_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
1744
0
        int ifindex, r;
1745
0
        Link *l = NULL;  /* avoid false maybe-uninitialized warning */
1746
1747
0
        assert(m);
1748
0
        assert(message);
1749
0
        assert(handler);
1750
1751
0
        r = bus_message_read_ifindex(message, error, &ifindex);
1752
0
        if (r < 0)
1753
0
                return r;
1754
1755
0
        r = get_any_link(m, ifindex, &l, error);
1756
0
        if (r < 0)
1757
0
                return r;
1758
1759
0
        return handler(message, l, error);
1760
0
}
1761
1762
0
static int bus_method_set_link_dns_servers(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1763
0
        return call_link_method(userdata, message, bus_link_method_set_dns_servers, error);
1764
0
}
1765
1766
0
static int bus_method_set_link_dns_servers_ex(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1767
0
        return call_link_method(userdata, message, bus_link_method_set_dns_servers_ex, error);
1768
0
}
1769
1770
0
static int bus_method_set_link_domains(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1771
0
        return call_link_method(userdata, message, bus_link_method_set_domains, error);
1772
0
}
1773
1774
0
static int bus_method_set_link_default_route(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1775
0
        return call_link_method(userdata, message, bus_link_method_set_default_route, error);
1776
0
}
1777
1778
0
static int bus_method_set_link_llmnr(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1779
0
        return call_link_method(userdata, message, bus_link_method_set_llmnr, error);
1780
0
}
1781
1782
0
static int bus_method_set_link_mdns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1783
0
        return call_link_method(userdata, message, bus_link_method_set_mdns, error);
1784
0
}
1785
1786
0
static int bus_method_set_link_dns_over_tls(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1787
0
        return call_link_method(userdata, message, bus_link_method_set_dns_over_tls, error);
1788
0
}
1789
1790
0
static int bus_method_set_link_dnssec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1791
0
        return call_link_method(userdata, message, bus_link_method_set_dnssec, error);
1792
0
}
1793
1794
0
static int bus_method_set_link_dnssec_negative_trust_anchors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1795
0
        return call_link_method(userdata, message, bus_link_method_set_dnssec_negative_trust_anchors, error);
1796
0
}
1797
1798
0
static int bus_method_revert_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1799
0
        return call_link_method(userdata, message, bus_link_method_revert, error);
1800
0
}
1801
1802
0
static int bus_method_get_link(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1803
0
        _cleanup_free_ char *p = NULL;
1804
0
        Manager *m = ASSERT_PTR(userdata);
1805
0
        int r, ifindex;
1806
0
        Link *l = NULL;  /* avoid false maybe-uninitialized warning */
1807
1808
0
        assert(message);
1809
1810
0
        r = bus_message_read_ifindex(message, error, &ifindex);
1811
0
        if (r < 0)
1812
0
                return r;
1813
1814
0
        r = get_any_link(m, ifindex, &l, error);
1815
0
        if (r < 0)
1816
0
                return r;
1817
1818
0
        p = link_bus_path(l);
1819
0
        if (!p)
1820
0
                return -ENOMEM;
1821
1822
0
        return sd_bus_reply_method_return(message, "o", p);
1823
0
}
1824
1825
0
static int bus_method_flush_caches(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1826
0
        Manager *m = ASSERT_PTR(userdata);
1827
1828
0
        assert(message);
1829
1830
0
        bus_client_log(message, "cache flush");
1831
1832
0
        manager_flush_caches(m, LOG_INFO);
1833
1834
0
        return sd_bus_reply_method_return(message, NULL);
1835
0
}
1836
1837
0
static int bus_method_reset_server_features(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1838
0
        Manager *m = ASSERT_PTR(userdata);
1839
1840
0
        assert(message);
1841
1842
0
        bus_client_log(message, "server feature reset");
1843
1844
0
        (void) dns_stream_disconnect_all(m);
1845
0
        manager_reset_server_features(m);
1846
1847
0
        return sd_bus_reply_method_return(message, NULL);
1848
0
}
1849
1850
0
static int dnssd_service_on_bus_track(sd_bus_track *t, void *userdata) {
1851
0
        DnssdService *s = ASSERT_PTR(userdata);
1852
1853
0
        assert(t);
1854
1855
0
        log_debug("Client of active request vanished, destroying DNS-SD service.");
1856
0
        dnssd_service_free(s);
1857
1858
0
        return 0;
1859
0
}
1860
1861
0
static int bus_method_register_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1862
0
        _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1863
0
        _cleanup_(dnssd_service_freep) DnssdService *service = NULL;
1864
0
        _cleanup_(sd_bus_track_unrefp) sd_bus_track *bus_track = NULL;
1865
0
        const char *id, *name_template, *type;
1866
0
        _cleanup_free_ char *path = NULL;
1867
0
        DnssdService *s = NULL;
1868
0
        Manager *m = ASSERT_PTR(userdata);
1869
0
        uid_t euid;
1870
0
        int r;
1871
1872
0
        assert(message);
1873
1874
0
        if (m->mdns_support != RESOLVE_SUPPORT_YES)
1875
0
                return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Support for MulticastDNS is disabled");
1876
1877
0
        service = new0(DnssdService, 1);
1878
0
        if (!service)
1879
0
                return log_oom();
1880
1881
0
        r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1882
0
        if (r < 0)
1883
0
                return r;
1884
1885
0
        r = sd_bus_creds_get_euid(creds, &euid);
1886
0
        if (r < 0)
1887
0
                return r;
1888
0
        service->originator = euid;
1889
0
        service->config_source = RESOLVE_CONFIG_SOURCE_DBUS;
1890
1891
0
        r = sd_bus_message_read(message, "sssqqq", &id, &name_template, &type,
1892
0
                                &service->port, &service->priority,
1893
0
                                &service->weight);
1894
0
        if (r < 0)
1895
0
                return r;
1896
1897
0
        if (!filename_part_is_valid(id))
1898
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DNS-SD service identifier '%s' is invalid", id);
1899
1900
0
        if (!dnssd_srv_type_is_valid(type))
1901
0
                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "DNS-SD service type '%s' is invalid", type);
1902
1903
0
        s = hashmap_get(m->dnssd_services, id);
1904
0
        if (s)
1905
0
                return sd_bus_error_setf(error, BUS_ERROR_DNSSD_SERVICE_EXISTS, "DNS-SD service '%s' exists already", id);
1906
1907
0
        service->id = strdup(id);
1908
0
        if (!service->id)
1909
0
                return log_oom();
1910
1911
0
        service->name_template = strdup(name_template);
1912
0
        if (!service->name_template)
1913
0
                return log_oom();
1914
1915
0
        service->type = strdup(type);
1916
0
        if (!service->type)
1917
0
                return log_oom();
1918
1919
0
        r = dnssd_render_instance_name(m, service, NULL);
1920
0
        if (r < 0)
1921
0
                return r;
1922
1923
0
        r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "a{say}");
1924
0
        if (r < 0)
1925
0
                return r;
1926
1927
0
        while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "{say}")) > 0) {
1928
0
                _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL;
1929
0
                DnsTxtItem *last = NULL;
1930
1931
0
                txt_data = new0(DnssdTxtData, 1);
1932
0
                if (!txt_data)
1933
0
                        return log_oom();
1934
1935
0
                while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_DICT_ENTRY, "say")) > 0) {
1936
0
                        const char *key;
1937
0
                        const void *value;
1938
0
                        size_t size;
1939
0
                        DnsTxtItem *i;
1940
1941
0
                        r = sd_bus_message_read(message, "s", &key);
1942
0
                        if (r < 0)
1943
0
                                return r;
1944
1945
0
                        if (isempty(key))
1946
0
                                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Keys in DNS-SD TXT RRs can't be empty");
1947
1948
0
                        if (!ascii_is_valid(key))
1949
0
                                return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "TXT key '%s' contains non-ASCII symbols", key);
1950
1951
0
                        r = sd_bus_message_read_array(message, 'y', &value, &size);
1952
0
                        if (r < 0)
1953
0
                                return r;
1954
1955
0
                        r = dnssd_txt_item_new_from_data(key, value, size, &i);
1956
0
                        if (r < 0)
1957
0
                                return r;
1958
1959
0
                        LIST_INSERT_AFTER(items, txt_data->txts, last, i);
1960
0
                        last = i;
1961
1962
0
                        r = sd_bus_message_exit_container(message);
1963
0
                        if (r < 0)
1964
0
                                return r;
1965
1966
0
                }
1967
0
                if (r < 0)
1968
0
                        return r;
1969
1970
0
                r = sd_bus_message_exit_container(message);
1971
0
                if (r < 0)
1972
0
                        return r;
1973
1974
0
                if (txt_data->txts) {
1975
0
                        LIST_PREPEND(items, service->txt_data_items, txt_data);
1976
0
                        txt_data = NULL;
1977
0
                }
1978
0
        }
1979
0
        if (r < 0)
1980
0
                return r;
1981
1982
0
        r = sd_bus_message_exit_container(message);
1983
0
        if (r < 0)
1984
0
                return r;
1985
1986
0
        if (!service->txt_data_items) {
1987
0
                _cleanup_(dnssd_txtdata_freep) DnssdTxtData *txt_data = NULL;
1988
1989
0
                txt_data = new0(DnssdTxtData, 1);
1990
0
                if (!txt_data)
1991
0
                        return log_oom();
1992
1993
0
                r = dns_txt_item_new_empty(&txt_data->txts);
1994
0
                if (r < 0)
1995
0
                        return r;
1996
1997
0
                LIST_PREPEND(items, service->txt_data_items, txt_data);
1998
0
                txt_data = NULL;
1999
0
        }
2000
2001
0
        r = sd_bus_path_encode("/org/freedesktop/resolve1/dnssd", service->id, &path);
2002
0
        if (r < 0)
2003
0
                return r;
2004
2005
0
        r = bus_verify_polkit_async(
2006
0
                        message,
2007
0
                        "org.freedesktop.resolve1.register-service",
2008
                        /* details= */ NULL,
2009
0
                        &m->polkit_registry,
2010
0
                        error);
2011
0
        if (r < 0)
2012
0
                return r;
2013
0
        if (r == 0)
2014
0
                return 1; /* Polkit will call us back */
2015
2016
0
        r = hashmap_ensure_put(&m->dnssd_services, &string_hash_ops, service->id, service);
2017
0
        if (r < 0)
2018
0
                return r;
2019
2020
0
        r = sd_bus_track_new(sd_bus_message_get_bus(message), &bus_track, dnssd_service_on_bus_track, service);
2021
0
        if (r < 0)
2022
0
                return r;
2023
2024
0
        r = sd_bus_track_add_sender(bus_track, message);
2025
0
        if (r < 0)
2026
0
                return r;
2027
2028
0
        service->manager = m;
2029
2030
0
        service = NULL;
2031
2032
0
        manager_refresh_rrs(m);
2033
2034
0
        return sd_bus_reply_method_return(message, "o", path);
2035
0
}
2036
2037
0
static int call_dnssd_method(Manager *m, sd_bus_message *message, sd_bus_message_handler_t handler, sd_bus_error *error) {
2038
0
        _cleanup_free_ char *name = NULL;
2039
0
        DnssdService *s = NULL;
2040
0
        const char *path;
2041
0
        int r;
2042
2043
0
        assert(m);
2044
0
        assert(message);
2045
0
        assert(handler);
2046
2047
0
        r = sd_bus_message_read(message, "o", &path);
2048
0
        if (r < 0)
2049
0
                return r;
2050
2051
0
        r = sd_bus_path_decode(path, "/org/freedesktop/resolve1/dnssd", &name);
2052
0
        if (r == 0)
2053
0
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DNSSD_SERVICE, "DNS-SD service with object path '%s' does not exist", path);
2054
0
        if (r < 0)
2055
0
                return r;
2056
2057
0
        s = hashmap_get(m->dnssd_services, name);
2058
0
        if (!s)
2059
0
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DNSSD_SERVICE, "DNS-SD service '%s' not known", name);
2060
2061
0
        return handler(message, s, error);
2062
0
}
2063
2064
0
static int bus_method_unregister_service(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2065
0
        Manager *m = ASSERT_PTR(userdata);
2066
2067
0
        assert(message);
2068
2069
0
        return call_dnssd_method(m, message, bus_dnssd_method_unregister, error);
2070
0
}
2071
2072
0
static int bus_method_get_delegate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2073
0
        _cleanup_free_ char *p = NULL;
2074
0
        Manager *m = ASSERT_PTR(userdata);
2075
0
        int r;
2076
2077
0
        assert(message);
2078
2079
0
        const char *id;
2080
0
        r = sd_bus_message_read(message, "s", &id);
2081
0
        if (r < 0)
2082
0
                return r;
2083
2084
0
        DnsDelegate *d = hashmap_get(m->delegates, id);
2085
0
        if (!d)
2086
0
                return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DELEGATE, "Delegate '%s' not known", id);
2087
2088
0
        p = dns_delegate_bus_path(d);
2089
0
        if (!p)
2090
0
                return -ENOMEM;
2091
2092
0
        return sd_bus_reply_method_return(message, "o", p);
2093
0
}
2094
2095
0
static int bus_method_list_delegates(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2096
0
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2097
0
        Manager *m = ASSERT_PTR(userdata);
2098
0
        int r;
2099
2100
0
        assert(message);
2101
2102
0
        r = sd_bus_message_new_method_return(message, &reply);
2103
0
        if (r < 0)
2104
0
                return r;
2105
2106
0
        r = sd_bus_message_open_container(reply, 'a', "(so)");
2107
0
        if (r < 0)
2108
0
                return r;
2109
2110
0
        DnsDelegate *d;
2111
0
        HASHMAP_FOREACH(d, m->delegates) {
2112
0
                _cleanup_free_ char *p = NULL;
2113
2114
0
                p = dns_delegate_bus_path(d);
2115
0
                if (!p)
2116
0
                        return -ENOMEM;
2117
2118
0
                r = sd_bus_message_append(reply, "(so)", d->id, p);
2119
0
                if (r < 0)
2120
0
                        return r;
2121
0
        }
2122
2123
0
        r = sd_bus_message_close_container(reply);
2124
0
        if (r < 0)
2125
0
                return r;
2126
2127
0
        return sd_bus_message_send(reply);
2128
0
}
2129
2130
static const sd_bus_vtable resolve_vtable[] = {
2131
        SD_BUS_VTABLE_START(0),
2132
        SD_BUS_PROPERTY("LLMNRHostname", "s", NULL, offsetof(Manager, llmnr_hostname), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2133
        SD_BUS_PROPERTY("LLMNR", "s", bus_property_get_resolve_support, offsetof(Manager, llmnr_support), 0),
2134
        SD_BUS_PROPERTY("MulticastDNS", "s", bus_property_get_resolve_support, offsetof(Manager, mdns_support), 0),
2135
        SD_BUS_PROPERTY("DNSOverTLS", "s", bus_property_get_dns_over_tls_mode, 0, 0),
2136
        SD_BUS_PROPERTY("DNS", "a(iiay)", bus_property_get_dns_servers, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2137
        SD_BUS_PROPERTY("DNSEx", "a(iiayqs)", bus_property_get_dns_servers_ex, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2138
        SD_BUS_PROPERTY("FallbackDNS", "a(iiay)", bus_property_get_fallback_dns_servers, offsetof(Manager, fallback_dns_servers), SD_BUS_VTABLE_PROPERTY_CONST),
2139
        SD_BUS_PROPERTY("FallbackDNSEx", "a(iiayqs)", bus_property_get_fallback_dns_servers_ex, offsetof(Manager, fallback_dns_servers), SD_BUS_VTABLE_PROPERTY_CONST),
2140
        SD_BUS_PROPERTY("CurrentDNSServer", "(iiay)", bus_property_get_current_dns_server, offsetof(Manager, current_dns_server), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2141
        SD_BUS_PROPERTY("CurrentDNSServerEx", "(iiayqs)", bus_property_get_current_dns_server_ex, offsetof(Manager, current_dns_server), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2142
        SD_BUS_PROPERTY("Domains", "a(isb)", bus_property_get_domains, 0, 0),
2143
        SD_BUS_PROPERTY("TransactionStatistics", "(tt)", bus_property_get_transaction_statistics, 0, 0),
2144
        SD_BUS_PROPERTY("CacheStatistics", "(ttt)", bus_property_get_cache_statistics, 0, 0),
2145
        SD_BUS_PROPERTY("DNSSEC", "s", bus_property_get_dnssec_mode, 0, 0),
2146
        SD_BUS_PROPERTY("DNSSECStatistics", "(tttt)", bus_property_get_dnssec_statistics, 0, 0),
2147
        SD_BUS_PROPERTY("DNSSECSupported", "b", bus_property_get_dnssec_supported, 0, 0),
2148
        SD_BUS_PROPERTY("DNSSECNegativeTrustAnchors", "as", bus_property_get_string_set, offsetof(Manager, trust_anchor.negative_by_name), 0),
2149
        SD_BUS_PROPERTY("DNSStubListener", "s", bus_property_get_dns_stub_listener_mode, offsetof(Manager, dns_stub_listener_mode), 0),
2150
        SD_BUS_PROPERTY("ResolvConfMode", "s", bus_property_get_resolv_conf_mode, 0, 0),
2151
2152
        SD_BUS_METHOD_WITH_ARGS("ResolveHostname",
2153
                                SD_BUS_ARGS("i", ifindex, "s", name, "i", family, "t", flags),
2154
                                SD_BUS_RESULT("a(iiay)", addresses, "s", canonical, "t", flags),
2155
                                bus_method_resolve_hostname,
2156
                                SD_BUS_VTABLE_UNPRIVILEGED),
2157
        SD_BUS_METHOD_WITH_ARGS("ResolveAddress",
2158
                                SD_BUS_ARGS("i",  ifindex, "i", family, "ay", address, "t", flags),
2159
                                SD_BUS_RESULT("a(is)", names, "t", flags),
2160
                                bus_method_resolve_address,
2161
                                SD_BUS_VTABLE_UNPRIVILEGED),
2162
        SD_BUS_METHOD_WITH_ARGS("ResolveRecord",
2163
                                SD_BUS_ARGS("i", ifindex, "s", name, "q", class, "q", type, "t", flags),
2164
                                SD_BUS_RESULT("a(iqqay)", records, "t", flags),
2165
                                bus_method_resolve_record,
2166
                                SD_BUS_VTABLE_UNPRIVILEGED),
2167
        SD_BUS_METHOD_WITH_ARGS("ResolveService",
2168
                                SD_BUS_ARGS("i", ifindex,
2169
                                            "s", name,
2170
                                            "s", type,
2171
                                            "s", domain,
2172
                                            "i", family,
2173
                                            "t", flags),
2174
                                SD_BUS_RESULT("a(qqqsa(iiay)s)", srv_data,
2175
                                              "aay", txt_data,
2176
                                              "s", canonical_name,
2177
                                              "s", canonical_type,
2178
                                              "s", canonical_domain,
2179
                                              "t", flags),
2180
                                bus_method_resolve_service,
2181
                                SD_BUS_VTABLE_UNPRIVILEGED),
2182
        SD_BUS_METHOD_WITH_ARGS("GetLink",
2183
                                SD_BUS_ARGS("i", ifindex),
2184
                                SD_BUS_RESULT("o", path),
2185
                                bus_method_get_link,
2186
                                SD_BUS_VTABLE_UNPRIVILEGED),
2187
        SD_BUS_METHOD_WITH_ARGS("SetLinkDNS",
2188
                                SD_BUS_ARGS("i", ifindex, "a(iay)", addresses),
2189
                                SD_BUS_NO_RESULT,
2190
                                bus_method_set_link_dns_servers,
2191
                                SD_BUS_VTABLE_UNPRIVILEGED),
2192
        SD_BUS_METHOD_WITH_ARGS("SetLinkDNSEx",
2193
                                SD_BUS_ARGS("i", ifindex, "a(iayqs)", addresses),
2194
                                SD_BUS_NO_RESULT,
2195
                                bus_method_set_link_dns_servers_ex,
2196
                                SD_BUS_VTABLE_UNPRIVILEGED),
2197
        SD_BUS_METHOD_WITH_ARGS("SetLinkDomains",
2198
                                SD_BUS_ARGS("i", ifindex, "a(sb)", domains),
2199
                                SD_BUS_NO_RESULT,
2200
                                bus_method_set_link_domains,
2201
                                SD_BUS_VTABLE_UNPRIVILEGED),
2202
        SD_BUS_METHOD_WITH_ARGS("SetLinkDefaultRoute",
2203
                                SD_BUS_ARGS("i", ifindex, "b", enable),
2204
                                SD_BUS_NO_RESULT,
2205
                                bus_method_set_link_default_route,
2206
                                SD_BUS_VTABLE_UNPRIVILEGED),
2207
        SD_BUS_METHOD_WITH_ARGS("SetLinkLLMNR",
2208
                                SD_BUS_ARGS("i", ifindex, "s", mode),
2209
                                SD_BUS_NO_RESULT,
2210
                                bus_method_set_link_llmnr,
2211
                                SD_BUS_VTABLE_UNPRIVILEGED),
2212
        SD_BUS_METHOD_WITH_ARGS("SetLinkMulticastDNS",
2213
                                SD_BUS_ARGS("i", ifindex, "s", mode),
2214
                                SD_BUS_NO_RESULT,
2215
                                bus_method_set_link_mdns,
2216
                                SD_BUS_VTABLE_UNPRIVILEGED),
2217
        SD_BUS_METHOD_WITH_ARGS("SetLinkDNSOverTLS",
2218
                                SD_BUS_ARGS("i", ifindex, "s", mode),
2219
                                SD_BUS_NO_RESULT,
2220
                                bus_method_set_link_dns_over_tls,
2221
                                SD_BUS_VTABLE_UNPRIVILEGED),
2222
        SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSEC",
2223
                                SD_BUS_ARGS("i", ifindex, "s", mode),
2224
                                SD_BUS_NO_RESULT,
2225
                                bus_method_set_link_dnssec,
2226
                                SD_BUS_VTABLE_UNPRIVILEGED),
2227
        SD_BUS_METHOD_WITH_ARGS("SetLinkDNSSECNegativeTrustAnchors",
2228
                                SD_BUS_ARGS("i", ifindex, "as", names),
2229
                                SD_BUS_NO_RESULT,
2230
                                bus_method_set_link_dnssec_negative_trust_anchors,
2231
                                SD_BUS_VTABLE_UNPRIVILEGED),
2232
        SD_BUS_METHOD_WITH_ARGS("RevertLink",
2233
                                SD_BUS_ARGS("i", ifindex),
2234
                                SD_BUS_NO_RESULT,
2235
                                bus_method_revert_link,
2236
                                SD_BUS_VTABLE_UNPRIVILEGED),
2237
        SD_BUS_METHOD_WITH_ARGS("RegisterService",
2238
                                SD_BUS_ARGS("s", id,
2239
                                            "s", name_template,
2240
                                            "s", type,
2241
                                            "q", service_port,
2242
                                            "q", service_priority,
2243
                                            "q", service_weight,
2244
                                            "aa{say}", txt_datas),
2245
                                SD_BUS_RESULT("o", service_path),
2246
                                bus_method_register_service,
2247
                                SD_BUS_VTABLE_UNPRIVILEGED),
2248
        SD_BUS_METHOD_WITH_ARGS("UnregisterService",
2249
                                SD_BUS_ARGS("o", service_path),
2250
                                SD_BUS_NO_RESULT,
2251
                                bus_method_unregister_service,
2252
                                SD_BUS_VTABLE_UNPRIVILEGED),
2253
        SD_BUS_METHOD_WITH_ARGS("ResetStatistics",
2254
                                SD_BUS_NO_ARGS,
2255
                                SD_BUS_NO_RESULT,
2256
                                bus_method_reset_statistics,
2257
                                SD_BUS_VTABLE_UNPRIVILEGED),
2258
        SD_BUS_METHOD_WITH_ARGS("FlushCaches",
2259
                                SD_BUS_NO_ARGS,
2260
                                SD_BUS_NO_RESULT,
2261
                                bus_method_flush_caches,
2262
                                SD_BUS_VTABLE_UNPRIVILEGED),
2263
        SD_BUS_METHOD_WITH_ARGS("ResetServerFeatures",
2264
                                SD_BUS_NO_ARGS,
2265
                                SD_BUS_NO_RESULT,
2266
                                bus_method_reset_server_features,
2267
                                SD_BUS_VTABLE_UNPRIVILEGED),
2268
        SD_BUS_METHOD_WITH_ARGS("GetDelegate",
2269
                                SD_BUS_ARGS("s", id),
2270
                                SD_BUS_RESULT("o", path),
2271
                                bus_method_get_delegate,
2272
                                SD_BUS_VTABLE_UNPRIVILEGED),
2273
        SD_BUS_METHOD_WITH_ARGS("ListDelegates",
2274
                                SD_BUS_NO_ARGS,
2275
                                SD_BUS_RESULT("a(so)", delegates),
2276
                                bus_method_list_delegates,
2277
                                SD_BUS_VTABLE_UNPRIVILEGED),
2278
2279
        SD_BUS_VTABLE_END,
2280
};
2281
2282
const BusObjectImplementation manager_object = {
2283
        "/org/freedesktop/resolve1",
2284
        "org.freedesktop.resolve1.Manager",
2285
        .vtables = BUS_VTABLES(resolve_vtable),
2286
        .children = BUS_IMPLEMENTATIONS(&link_object,
2287
                                        &dnssd_object,
2288
                                        &dns_delegate_object),
2289
};
2290
2291
0
static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
2292
0
        Manager *m = ASSERT_PTR(userdata);
2293
0
        int b, r;
2294
2295
0
        assert(message);
2296
2297
0
        r = sd_bus_message_read(message, "b", &b);
2298
0
        if (r < 0) {
2299
0
                bus_log_parse_error(r);
2300
0
                return 0;
2301
0
        }
2302
2303
0
        if (b)
2304
0
                return 0;
2305
2306
0
        log_debug("Coming back from suspend, closing all TCP connections...");
2307
0
        (void) dns_stream_disconnect_all(m);
2308
2309
0
        log_debug("Coming back from suspend, resetting all probed server features...");
2310
0
        manager_reset_server_features(m);
2311
2312
0
        log_debug("Coming back from suspend, verifying all RRs...");
2313
0
        manager_verify_all(m);
2314
2315
0
        return 0;
2316
0
}
2317
2318
0
int manager_connect_bus(Manager *m) {
2319
0
        int r;
2320
2321
0
        assert(m);
2322
2323
0
        if (m->bus)
2324
0
                return 0;
2325
2326
0
        r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-resolve");
2327
0
        if (r < 0)
2328
0
                return log_error_errno(r, "Failed to connect to system bus: %m");
2329
2330
0
        r = bus_add_implementation(m->bus, &manager_object, m);
2331
0
        if (r < 0)
2332
0
                return r;
2333
2334
0
        r = bus_log_control_api_register(m->bus);
2335
0
        if (r < 0)
2336
0
                return r;
2337
2338
0
        r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.resolve1", 0, NULL, NULL);
2339
0
        if (r < 0)
2340
0
                return log_error_errno(r, "Failed to request name: %m");
2341
2342
0
        r = sd_bus_attach_event(m->bus, m->event, 0);
2343
0
        if (r < 0)
2344
0
                return log_error_errno(r, "Failed to attach bus to event loop: %m");
2345
2346
0
        r = bus_match_signal_async(
2347
0
                        m->bus,
2348
0
                        NULL,
2349
0
                        bus_login_mgr,
2350
0
                        "PrepareForSleep",
2351
0
                        match_prepare_for_sleep,
2352
0
                        NULL,
2353
0
                        m);
2354
0
        if (r < 0)
2355
0
                log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m");
2356
2357
0
        return 0;
2358
0
}
2359
2360
0
int manager_send_changed_strv(Manager *manager, char **properties) {
2361
0
        assert(manager);
2362
2363
0
        if (sd_bus_is_ready(manager->bus) <= 0)
2364
0
                return 0;
2365
2366
0
        int r = sd_bus_emit_properties_changed_strv(
2367
0
                        manager->bus,
2368
0
                        "/org/freedesktop/resolve1",
2369
0
                        "org.freedesktop.resolve1.Manager",
2370
0
                        properties);
2371
0
        if (r < 0)
2372
0
                log_notice_errno(r, "Failed to emit notification about changed properties: %m");
2373
2374
0
        return r;
2375
0
}