| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "conf-files.h" |
| 7 |
#include "constants.h" |
| 8 |
#include "dns-answer.h" |
| 9 |
#include "dns-domain.h" |
| 10 |
#include "dns-question.h" |
| 11 |
#include "dns-rr.h" |
| 12 |
#include "errno-util.h" |
| 13 |
#include "hashmap.h" |
| 14 |
#include "json-util.h" |
| 15 |
#include "log.h" |
| 16 |
#include "resolved-manager.h" |
| 17 |
#include "resolved-static-records.h" |
| 18 |
#include "set.h" |
| 19 |
#include "stat-util.h" |
| 20 |
|
| 21 |
/* This implements a mechanism to extend what systemd-resolved resolves locally, via .rr drop-ins in |
| 22 |
* {/etc,/run,/usr/local/lib,/usr/lib}/systemd/resolve/static.d/. These files are in JSON format, and are RR |
| 23 |
* serializations, that match the usual way we serialize RRs to JSON. |
| 24 |
* |
| 25 |
* Note that this deliberately doesn't use the (probably more user-friendly) classic DNS zone file format, |
| 26 |
* to keep things a bit simpler, and symmetric to the places we currently already generate JSON |
| 27 |
* serializations of DNS RRs. Also note the semantics are different from DNS zone file format, for example |
| 28 |
* regarding delegation (i.e. the RRs defined here have no effect on subdomains), which is probably nicer for |
| 29 |
* one-off mappings of domains to specific resources. Or in other words, this is supposed to be a drop-in |
| 30 |
* based alternative to /etc/hosts, not one to DNS zone files. (The JSON format is also a lot more |
| 31 |
* extensible to us, for example we could teach it to map certain lookups to specific DNS errors, or extend |
| 32 |
* it so that subdomains always get NXDOMAIN or similar). |
| 33 |
* |
| 34 |
* (That said, if there's a good reason, we can also support *.zone files too one day). |
| 35 |
*/ |
| 36 |
|
| 37 |
/* Recheck static records at most once every 2s */ |
| 38 |
#define STATIC_RECORDS_RECHECK_USEC (2*USEC_PER_SEC) |
| 39 |
|
| 40 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 41 |
answer_by_name_hash_ops, |
| 42 |
char, |
| 43 |
dns_name_hash_func, |
| 44 |
dns_name_compare_func, |
| 45 |
DnsAnswer, |
| 46 |
dns_answer_unref); |
| 47 |
|
| 48 |
static int load_static_record_file_item(sd_json_variant *rj, Hashmap **records) { |
| 49 |
int r; |
| 50 |
|
| 51 |
assert(records); |
| 52 |
|
| 53 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 54 |
r = dns_resource_record_from_json(rj, &rr); |
| 55 |
if (r < 0) |
| 56 |
return log_error_errno(r, "Failed to parse DNS record from JSON: %m"); |
| 57 |
|
| 58 |
_cleanup_(dns_answer_unrefp) DnsAnswer *a = |
| 59 |
hashmap_remove(*records, dns_resource_key_name(rr->key)); |
| 60 |
|
| 61 |
r = dns_answer_add_extend_full(&a, rr, /* ifindex= */ 0, DNS_ANSWER_AUTHENTICATED, /* rrsig= */ NULL, /* until= */ USEC_INFINITY); |
| 62 |
if (r < 0) |
| 63 |
return log_error_errno(r, "Failed to append RR to DNS answer: %m"); |
| 64 |
|
| 65 |
DnsAnswerItem *item = ASSERT_PTR(ordered_set_first(a->items)); |
| 66 |
|
| 67 |
r = hashmap_ensure_put(records, &answer_by_name_hash_ops, dns_resource_key_name(item->rr->key), a); |
| 68 |
if (r < 0) |
| 69 |
return log_error_errno(r, "Failed to add RR to static record set: %m"); |
| 70 |
|
| 71 |
TAKE_PTR(a); |
| 72 |
|
| 73 |
log_debug("Added static resource record: %s", dns_resource_record_to_string(rr)); |
| 74 |
return 1; |
| 75 |
} |
| 76 |
|
| 77 |
static int load_static_record_file(const ConfFile *cf, Hashmap **records, Set **stats) { |
| 78 |
int r; |
| 79 |
|
| 80 |
assert(cf); |
| 81 |
assert(records); |
| 82 |
assert(stats); |
| 83 |
|
| 84 |
/* Have we seen this file before? Then we might as well skip loading it again, it wouldn't have any |
| 85 |
* additional effect anyway. (Note: masking/overriding has already been applied before we reach this |
| 86 |
* point, here everything is purely additive.) */ |
| 87 |
if (set_contains(*stats, &cf->st)) |
| 88 |
return 0; |
| 89 |
|
| 90 |
_cleanup_free_ struct stat *st_copy = memdup(&cf->st, sizeof(cf->st)); |
| 91 |
if (!st_copy) |
| 92 |
return log_oom(); |
| 93 |
|
| 94 |
if (set_ensure_consume(stats, &inode_unmodified_hash_ops, TAKE_PTR(st_copy)) < 0) |
| 95 |
return log_oom(); |
| 96 |
|
| 97 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; |
| 98 |
unsigned line = 0, column = 0; |
| 99 |
r = sd_json_parse_fd(cf->result, cf->fd, SD_JSON_PARSE_REOPEN_FD, &j, &line, &column); |
| 100 |
if (r < 0) { |
| 101 |
if (line > 0) |
| 102 |
log_syntax(/* unit= */ NULL, LOG_WARNING, cf->result, line, r, "Failed to parse JSON, skipping: %m"); |
| 103 |
else |
| 104 |
log_warning_errno(r, "Failed to parse JSON file '%s', skipping: %m", cf->result); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
if (sd_json_variant_is_array(j)) { |
| 109 |
sd_json_variant *i; |
| 110 |
int ret = 0; |
| 111 |
JSON_VARIANT_ARRAY_FOREACH(i, j) |
| 112 |
RET_GATHER(ret, load_static_record_file_item(i, records)); |
| 113 |
if (ret < 0) |
| 114 |
return ret; |
| 115 |
} else if (sd_json_variant_is_object(j)) { |
| 116 |
r = load_static_record_file_item(j, records); |
| 117 |
if (r < 0) |
| 118 |
return r; |
| 119 |
} else { |
| 120 |
log_warning("JSON file '%s' contains neither array nor object, skipping.", cf->result); |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
return 1; |
| 125 |
} |
| 126 |
|
| 127 |
static int manager_static_records_read(Manager *m) { |
| 128 |
int r; |
| 129 |
|
| 130 |
usec_t ts; |
| 131 |
assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &ts) >= 0); |
| 132 |
|
| 133 |
/* See if we checked the static records db recently already */ |
| 134 |
if (m->static_records_last != USEC_INFINITY && usec_add(m->static_records_last, STATIC_RECORDS_RECHECK_USEC) > ts) |
| 135 |
return 0; |
| 136 |
|
| 137 |
m->static_records_last = ts; |
| 138 |
|
| 139 |
ConfFile **files = NULL; |
| 140 |
size_t n_files = 0; |
| 141 |
CLEANUP_ARRAY(files, n_files, conf_file_free_array); |
| 142 |
|
| 143 |
r = conf_files_list_nulstr_full( |
| 144 |
".rr", |
| 145 |
/* root= */ NULL, |
| 146 |
CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED|CONF_FILES_WARN, |
| 147 |
CONF_PATHS_NULSTR("systemd/resolve/static.d/"), |
| 148 |
&files, |
| 149 |
&n_files); |
| 150 |
if (r < 0) |
| 151 |
return log_error_errno(r, "Failed to enumerate static record drop-ins: %m"); |
| 152 |
|
| 153 |
/* Let's suppress reloads if nothing changed. For that keep the set of inodes from the previous |
| 154 |
* reload around, and see if there are any changes on them. */ |
| 155 |
bool reload; |
| 156 |
if (set_size(m->static_records_stat) != n_files) |
| 157 |
reload = true; |
| 158 |
else { |
| 159 |
reload = false; |
| 160 |
FOREACH_ARRAY(f, files, n_files) |
| 161 |
if (!set_contains(m->static_records_stat, &(*f)->st)) { |
| 162 |
reload = true; |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if (!reload) { |
| 168 |
log_debug("No static record files changed, not re-reading."); |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
_cleanup_(hashmap_freep) Hashmap *records = NULL; |
| 173 |
_cleanup_(set_freep) Set *stats = NULL; |
| 174 |
FOREACH_ARRAY(f, files, n_files) |
| 175 |
(void) load_static_record_file(*f, &records, &stats); |
| 176 |
|
| 177 |
hashmap_free(m->static_records); |
| 178 |
m->static_records = TAKE_PTR(records); |
| 179 |
|
| 180 |
set_free(m->static_records_stat); |
| 181 |
m->static_records_stat = TAKE_PTR(stats); |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
int manager_static_records_lookup(Manager *m, DnsQuestion *q, DnsAnswer **answer) { |
| 187 |
int r; |
| 188 |
|
| 189 |
assert(m); |
| 190 |
assert(q); |
| 191 |
assert(answer); |
| 192 |
|
| 193 |
if (!m->read_static_records) |
| 194 |
return 0; |
| 195 |
|
| 196 |
(void) manager_static_records_read(m); |
| 197 |
|
| 198 |
const char *n = dns_question_first_name(q); |
| 199 |
if (!n) |
| 200 |
return 0; |
| 201 |
|
| 202 |
DnsAnswer *f = hashmap_get(m->static_records, n); |
| 203 |
if (!f) |
| 204 |
return 0; |
| 205 |
|
| 206 |
r = dns_answer_extend(answer, f); |
| 207 |
if (r < 0) |
| 208 |
return r; |
| 209 |
|
| 210 |
return 1; |
| 211 |
} |
| 212 |
|
| 213 |
void manager_static_records_flush(Manager *m) { |
| 214 |
assert(m); |
| 215 |
|
| 216 |
m->static_records = hashmap_free(m->static_records); |
| 217 |
m->static_records_stat = set_free(m->static_records_stat); |
| 218 |
} |