dirmngr: Use the new DNS wrapper for the HKP engine.

* dirmngr/ks-engine-hkp.c (my_getnameinfo): Change arg type to
dns_addrinfo_t.
(map_host): Replace getaddrinfo by resolve_dns_name.
--

Note that we still need to replace getnameinfo so that the PTR lookup
is either suppressed or also done via ADNS.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2015-10-21 17:46:21 +02:00
parent 8bccbf4778
commit afbe87fa2d
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
1 changed files with 30 additions and 25 deletions

View File

@ -38,6 +38,7 @@
#include "dirmngr.h" #include "dirmngr.h"
#include "misc.h" #include "misc.h"
#include "userids.h" #include "userids.h"
#include "dns-stuff.h"
#include "ks-engine.h" #include "ks-engine.h"
/* Substitutes for missing Mingw macro. The EAI_SYSTEM mechanism /* Substitutes for missing Mingw macro. The EAI_SYSTEM mechanism
@ -240,7 +241,7 @@ select_random_host (int *table)
0 on success or an EAI error code. True is stored at R_ISNUMERIC 0 on success or an EAI error code. True is stored at R_ISNUMERIC
if HOST has a numeric IP address. */ if HOST has a numeric IP address. */
static int static int
my_getnameinfo (struct addrinfo *ai, char *host, size_t hostlen, my_getnameinfo (dns_addrinfo_t ai, char *host, size_t hostlen,
int numeric, int *r_isnumeric) int numeric, int *r_isnumeric)
{ {
int ec; int ec;
@ -254,7 +255,7 @@ my_getnameinfo (struct addrinfo *ai, char *host, size_t hostlen,
if (numeric) if (numeric)
ec = EAI_NONAME; ec = EAI_NONAME;
else else
ec = getnameinfo (ai->ai_addr, ai->ai_addrlen, ec = getnameinfo (ai->addr, ai->addrlen,
host, hostlen, NULL, 0, NI_NAMEREQD); host, hostlen, NULL, 0, NI_NAMEREQD);
if (!ec && *host == '[') if (!ec && *host == '[')
@ -262,14 +263,14 @@ my_getnameinfo (struct addrinfo *ai, char *host, size_t hostlen,
else if (ec == EAI_NONAME) else if (ec == EAI_NONAME)
{ {
p = host; p = host;
if (ai->ai_family == AF_INET6) if (ai->family == AF_INET6)
{ {
*p++ = '['; *p++ = '[';
hostlen -= 2; hostlen -= 2;
} }
ec = getnameinfo (ai->ai_addr, ai->ai_addrlen, ec = getnameinfo (ai->addr, ai->addrlen,
p, hostlen, NULL, 0, NI_NUMERICHOST); p, hostlen, NULL, 0, NI_NUMERICHOST);
if (!ec && ai->ai_family == AF_INET6) if (!ec && ai->family == AF_INET6)
strcat (host, "]"); strcat (host, "]");
*r_isnumeric = 1; *r_isnumeric = 1;
@ -347,11 +348,12 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
if (idx == -1) if (idx == -1)
{ {
/* We never saw this host. Allocate a new entry. */ /* We never saw this host. Allocate a new entry. */
struct addrinfo hints, *aibuf, *ai; dns_addrinfo_t aibuf, ai;
int *reftbl; int *reftbl;
size_t reftblsize; size_t reftblsize;
int refidx; int refidx;
int is_pool = 0; int is_pool = 0;
char *cname;
reftblsize = 100; reftblsize = 100;
reftbl = xtrymalloc (reftblsize * sizeof *reftbl); reftbl = xtrymalloc (reftblsize * sizeof *reftbl);
@ -370,15 +372,13 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
/* Find all A records for this entry and put them into the pool /* Find all A records for this entry and put them into the pool
list - if any. */ list - if any. */
memset (&hints, 0, sizeof (hints)); err = resolve_dns_name (name, 0, 0, SOCK_STREAM, &aibuf, &cname);
hints.ai_family = AF_UNSPEC; if (err)
hints.ai_socktype = SOCK_STREAM; {
hints.ai_flags = AI_CANONNAME; log_error ("resolving '%s' failed: %s\n", name, gpg_strerror (err));
/* We can't use the the AI_IDN flag because that does the err = 0;
conversion using the current locale. However, GnuPG always }
used UTF-8. To support IDN we would need to make use of the else
libidn API. */
if (!getaddrinfo (name, NULL, &hints, &aibuf))
{ {
int n_v6, n_v4; int n_v6, n_v4;
@ -388,19 +388,22 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
with the IP addresses. If it is not a pool, we use the with the IP addresses. If it is not a pool, we use the
specified name. */ specified name. */
n_v6 = n_v4 = 0; n_v6 = n_v4 = 0;
for (ai = aibuf; ai; ai = ai->ai_next) for (ai = aibuf; ai; ai = ai->next)
{ {
if (ai->ai_family != AF_INET6) if (ai->family != AF_INET6)
n_v6++; n_v6++;
else if (ai->ai_family != AF_INET) else if (ai->family != AF_INET)
n_v4++; n_v4++;
} }
if (n_v6 > 1 || n_v4 > 1) if (n_v6 > 1 || n_v4 > 1)
is_pool = 1; is_pool = 1;
if (is_pool && aibuf->ai_canonname) if (is_pool && cname)
hi->cname = xtrystrdup (aibuf->ai_canonname); {
hi->cname = cname;
cname = NULL;
}
for (ai = aibuf; ai; ai = ai->ai_next) for (ai = aibuf; ai; ai = ai->next)
{ {
char tmphost[NI_MAXHOST + 2]; char tmphost[NI_MAXHOST + 2];
int tmpidx; int tmpidx;
@ -408,7 +411,7 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
int ec; int ec;
int i; int i;
if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) if (ai->family != AF_INET && ai->family != AF_INET6)
continue; continue;
dirmngr_tick (ctrl); dirmngr_tick (ctrl);
@ -474,13 +477,13 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
gai_strerror (ec)); gai_strerror (ec));
} }
if (ai->ai_family == AF_INET6) if (ai->family == AF_INET6)
{ {
hosttable[tmpidx]->v6 = 1; hosttable[tmpidx]->v6 = 1;
xfree (hosttable[tmpidx]->v6addr); xfree (hosttable[tmpidx]->v6addr);
hosttable[tmpidx]->v6addr = ipaddr; hosttable[tmpidx]->v6addr = ipaddr;
} }
else if (ai->ai_family == AF_INET) else if (ai->family == AF_INET)
{ {
hosttable[tmpidx]->v4 = 1; hosttable[tmpidx]->v4 = 1;
xfree (hosttable[tmpidx]->v4addr); xfree (hosttable[tmpidx]->v4addr);
@ -497,9 +500,11 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
} }
} }
} }
freeaddrinfo (aibuf);
} }
reftbl[refidx] = -1; reftbl[refidx] = -1;
xfree (cname);
free_dns_addrinfo (aibuf);
if (refidx && is_pool) if (refidx && is_pool)
{ {
assert (!hi->pool); assert (!hi->pool);