1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* Use libunbound to use DNSSEC supported resolving.
*
* Copyright (C) 2012 Paul Wouters <paul@libreswan.org>
* Copyright (C) 2017-2019 Paul Wouters <pwouters@redhat.com>
* Copyright (C) 2017 Antony Antony <antony@phenome.org>
* Copyright (C) 2019-2019 Stepan Broz <stepan@izitra.cz>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#ifndef USE_DNSSEC
# error this file should only be compiled when using DNSSEC
#endif
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <errno.h>
#include <event2/event.h> /* deb:libevent-dev */
#include <unbound.h> /* rpm:unbound-devel deb:libunbound-dev */
#include <unbound-event.h> /* ditto */
#include "lswglob.h"
#include "dnssec.h"
#include "constants.h"
#include "lswlog.h"
#include "ip_info.h"
static struct ub_ctx *dns_ctx = NULL;
void unbound_ctx_free(void)
{
if (dns_ctx != NULL) {
ub_ctx_delete(dns_ctx);
dns_ctx = NULL;
}
}
static void add_trust_anchors(unsigned count, char **files,
struct lswglob_context *context UNUSED,
struct logger *logger)
{
for (unsigned i = 0; i < count; i++) {
const char *file = files[i];
int ugh = ub_ctx_add_ta_file(dns_ctx, file);
if (ugh != 0) {
llog(RC_LOG, logger, "ignored trusted key file %s: %s",
file, ub_strerror(ugh));
} else {
ldbg(logger, "added contents of trusted key file %s to unbound resolver context", file);
}
}
}
static void unbound_ctx_config(bool do_dnssec, const char *rootfile,
const char *trusted,
struct logger *logger)
{
int ugh;
if (DBGP(DBG_BASE)) {
ub_ctx_debuglevel(dns_ctx, 5);
DBG_log("unbound context created - setting debug level to 5");
}
/* lookup from /etc/hosts before DNS lookups as people expect that */
ugh = ub_ctx_hosts(dns_ctx, "/etc/hosts");
if (ugh != 0) {
llog(RC_LOG, logger,
"error reading hosts: %s: %s",
ub_strerror(ugh), strerror(errno));
} else {
dbg("/etc/hosts lookups activated");
}
/*
* Use /etc/resolv.conf as forwarding cache - we expect people
* to reconfigure this file if they need to work around DHCP DNS
* obtained servers.
*/
/*
* ??? ub_ctx_resolvconf is not currently documented to set errno.
* Private communications with W.C.A. Wijngaards 2017 October:
* "Is errno is meaningful after a failed call to libunbound?"
* "Yes it is. Specifically for the error-to-read-file case.
* Not other cases (eg. socket errors happen too far away in the code)."
*/
errno = 0;
ugh = ub_ctx_resolvconf(dns_ctx, "/etc/resolv.conf");
if (ugh != 0) {
int e = errno; /* protect value from ub_strerror */
llog(RC_LOG, logger,
"error reading /etc/resolv.conf: %s: [errno: %s]",
ub_strerror(ugh), strerror(e));
} else {
dbg("/etc/resolv.conf usage activated");
}
/*
* Limit outgoing ports to those allowed by common SELinux policy
*/
errno = 0;
ugh = ub_ctx_set_option(dns_ctx, "outgoing-port-avoid:", "0-65535");
if (ugh != 0) {
llog(RC_LOG, logger,
"error setting outgoing-port-avoid: %s: %s",
ub_strerror(ugh), strerror(errno));
} else {
dbg("outgoing-port-avoid set 0-65535");
}
errno = 0;
ugh = ub_ctx_set_option(dns_ctx, "outgoing-port-permit:", "32768-60999");
if (ugh != 0) {
llog(RC_LOG, logger,
"error setting outgoing-port-permit: %s: %s",
ub_strerror(ugh), strerror(errno));
} else {
dbg("outgoing-port-permit set 32768-60999");
}
if (!do_dnssec) {
/* No DNSSEC - nothing more to configure */
dbg("dnssec validation disabled by configuration");
return;
}
/* Only DNSSEC related configuration from here */
if (rootfile == NULL) {
if (trusted == NULL) {
llog(RC_LOG, logger,
"dnssec-enable=yes but no dnssec-rootkey-file or trust anchors specified.");
llog(RC_LOG, logger,
"WARNING: DNSSEC validation disabled");
return;
} else {
llog(RC_LOG, logger,
"dnssec-enable=yes but no dnssec-rootkey-file specified. Additional trust anchor file MUST include a root trust anchor or DNSSEC validation will be disabled");
}
} else {
dbg("loading dnssec root key from:%s", rootfile);
errno = 0;
ugh = ub_ctx_add_ta_file(dns_ctx, rootfile);
if (ugh != 0) {
int e = errno; /* protect value from ub_strerror */
llog(RC_LOG, logger,
"error adding dnssec root key: %s [errno: %s]",
ub_strerror(ugh), strerror(e));
llog(RC_LOG, logger,
"WARNING: DNSSEC validation disabled");
}
}
if (trusted == NULL) {
dbg("no additional dnssec trust anchors defined via dnssec-trusted= option");
return;
}
if (!lswglob(trusted, "trusted anchor", add_trust_anchors,
/*lswglob_context*/NULL, logger)) {
llog(RC_LOG, logger, "no trust anchor files matched '%s'", trusted);
}
}
/*
* initialize a ub_ctx for asynchronous calls using libevent from pluto.
* only call once
*/
diag_t unbound_event_init(struct event_base *eb, bool do_dnssec,
const char *rootfile, const char *trusted,
struct logger *logger)
{
passert(dns_ctx == NULL); /* block re-entry to the function */
dns_ctx = ub_ctx_create_event(eb);
if (dns_ctx == NULL) {
return diag("failed to initialize unbound libevent ABI, please recompile libunbound with libevent support or recompile libreswan without USE_DNSSEC");
}
unbound_ctx_config(do_dnssec, rootfile, trusted, logger);
return NULL;
}
/*
* initialize a ub_ct for blocking dns calls. Do not call from pluto.
* Call this function once directly, such as addconn.
* dns_ctx is static in this file. call unbound_ctx_free() to free it.
*/
void unbound_sync_init(bool do_dnssec, const char *rootfile,
const char *trusted, struct logger *logger)
{
passert(dns_ctx == NULL); /* block re-entry to the function */
dns_ctx = ub_ctx_create();
passert(dns_ctx != NULL);
unbound_ctx_config(do_dnssec, rootfile, trusted, logger);
}
/*
* synchronous blocking resolving - simple replacement of ttoaddress_dns()
* src_len == 0 means "apply strlen"
* af == AF_UNSPEC means default to AF_INET(A/IPv4)
*/
bool unbound_resolve(char *src, const struct ip_info *afi,
ip_address *ipaddr, const struct logger *logger)
{
/* 28 = AAAA record, 1 = A record */
const int qtype = (afi == &ipv6_info) ? 28/*AAAA*/ : 1/*A*/;
passert(dns_ctx != NULL);
if (strlen(src) == 0) {
return diag("empty hostname in host lookup");
}
struct ub_result *result;
int ugh = ub_resolve(dns_ctx, src, qtype, 1 /* CLASS IN */, &result);
if (ugh != 0) {
llog(RC_LOG, logger, "unbound error: %s", ub_strerror(ugh));
ub_resolve_free(result);
return false;
}
if (result->bogus) {
llog_error(logger, 0/*no-errno*/,
"%s failed DNSSEC validation",
result->qname);
ub_resolve_free(result);
return false;
}
if (!result->havedata) {
if (result->secure) {
dbg("validated reply proves '%s' does not exist",
src);
} else {
dbg("failed to resolve '%s' (%s)", src,
result->bogus ? "BOGUS" : "insecure");
}
ub_resolve_free(result);
return false;
}
if (!result->secure) {
dbg("warning: %s lookup was not protected by DNSSEC!",
result->qname);
}
if (DBGP(DBG_TMI)) {
int i = 0;
DBG_log("The result has:");
DBG_log("qname: %s", result->qname);
DBG_log("qtype: %d", result->qtype);
DBG_log("qclass: %d", result->qclass);
if (result->canonname)
DBG_log("canonical name: %s", result->canonname);
DBG_log("DNS rcode: %d", result->rcode);
for (i = 0; result->data[i] != NULL; i++) {
DBG_log("result data element %d has length %d",
i, result->len[i]);
}
DBG_log("result has %d data element(s)", i);
}
/* XXX: for now pick the first one and return that */
passert(result->data != NULL);
passert(result->data[0] != NULL);
passert(result->len != NULL);
/*
* XXX: data_to_address() only requires the length >=
* address-length.
*/
diag_t diag = data_to_address(result->data[0], (size_t)result->len[0], afi, ipaddr);
if (diag != NULL) {
llog_pexpect(logger, HERE, "invalid dns address record: %s",
str_diag(diag));
pfree_diag(&diag);
ub_resolve_free(result);
return false;
}
ldbg(logger, "success for %s lookup", afi->ip_name);
return true;
}
struct ub_ctx * get_unbound_ctx(void)
{
return dns_ctx;
}
|