1 /* dns-stuff.c - DNS related code including CERT RR (rfc-4398)
2 * Copyright (C) 2003, 2005, 2006, 2009 Free Software Foundation, Inc.
3 * Copyright (C) 2005, 2006, 2009, 2015. 2016 Werner Koch
5 * This file is part of GnuPG.
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of either
10 * - the GNU Lesser General Public License as published by the Free
11 * Software Foundation; either version 3 of the License, or (at
12 * your option) any later version.
16 * - the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at
18 * your option) any later version.
20 * or both in parallel, as here.
22 * This file is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, see <https://www.gnu.org/licenses/>.
32 #include <sys/types.h>
33 #ifdef HAVE_W32_SYSTEM
34 # define WIN32_LEAN_AND_MEAN
35 # ifdef HAVE_WINSOCK2_H
36 # include <winsock2.h>
39 # include <iphlpapi.h>
41 # if HAVE_SYSTEM_RESOLVER
42 # include <netinet/in.h>
43 # include <arpa/nameser.h>
52 /* William Ahern's DNS library, included as a source copy. */
57 /* dns.c has a dns_p_free but it is not exported. We use our own
58 * wrapper here so that we do not accidentally use xfree which would
59 * be wrong for dns.c allocated data. */
60 #define dns_free(a) free ((a))
63 #ifdef WITHOUT_NPTH /* Give the Makefile a chance to build without Pth. */
70 #include "./dirmngr-err.h"
73 #include "dns-stuff.h"
76 # define my_unprotect() npth_unprotect ()
77 # define my_protect() npth_protect ()
79 # define my_unprotect() do { } while(0)
80 # define my_protect() do { } while(0)
83 /* We allow the use of 0 instead of AF_UNSPEC - check this assumption. */
85 # error AF_UNSPEC does not have the value 0
88 /* Windows does not support the AI_ADDRCONFIG flag - use zero instead. */
90 # define AI_ADDRCONFIG 0
93 /* Not every installation has gotten around to supporting SRVs or
102 /* The standard SOCKS and TOR ports. */
103 #define SOCKS_PORT 1080
104 #define TOR_PORT 9050
105 #define TOR_PORT2 9150 /* (Used by the Tor browser) */
108 /* The default nameserver used in Tor mode. */
109 #define DEFAULT_NAMESERVER "8.8.8.8"
111 /* The default timeout in seconds for libdns requests. */
112 #define DEFAULT_TIMEOUT 30
115 /* Two flags to enable verbose and debug mode. */
116 static int opt_verbose;
117 static int opt_debug;
119 /* The timeout in seconds for libdns requests. */
120 static int opt_timeout;
122 /* The flag to disable IPv4 access - right now this only skips
123 * returned A records. */
124 static int opt_disable_ipv4;
126 /* If set force the use of the standard resolver. */
127 static int standard_resolver;
129 /* If set use recursive resolver when available. */
130 static int recursive_resolver;
132 /* If set Tor mode shall be used. */
135 /* A string with the nameserver IP address used with Tor.
136 (40 should be sufficient for v6 but we add some extra for a scope.) */
137 static char tor_nameserver[40+20];
139 /* Two strings to hold the credentials presented to Tor. */
140 static char tor_socks_user[30];
141 static char tor_socks_password[20];
145 /* Libdns gobal data. */
148 struct dns_resolv_conf *resolv_conf;
149 struct dns_hosts *hosts;
150 struct dns_hints *hints;
152 struct sockaddr_storage socks_host;
155 /* If this flag is set, libdns shall be reinited for the next use. */
156 static int libdns_reinit_pending;
158 /* The Tor port to be used. */
159 static int libdns_tor_port;
161 #endif /*USE_LIBDNS*/
164 /* Calling this function with YES set to True forces the use of the
165 * standard resolver even if dirmngr has been built with support for
166 * an alternative resolver. */
168 enable_standard_resolver (int yes)
170 standard_resolver = yes;
174 /* Return true if the standard resolver is used. */
176 standard_resolver_p (void)
178 return standard_resolver;
182 /* Calling this function with YES switches libdns into recursive mode.
183 * It has no effect on the standard resolver. */
185 enable_recursive_resolver (int yes)
187 recursive_resolver = yes;
189 libdns_reinit_pending = 1;
194 /* Return true iff the recursive resolver is used. */
196 recursive_resolver_p (void)
199 return !standard_resolver && recursive_resolver;
206 /* Puts this module eternally into Tor mode. When called agained with
207 * NEW_CIRCUIT request a new TOR circuit for the next DNS query. */
209 enable_dns_tormode (int new_circuit)
211 if (!*tor_socks_user || new_circuit)
213 static unsigned int counter;
215 gpgrt_snprintf (tor_socks_user, sizeof tor_socks_user,
216 "dirmngr-%lu", (unsigned long)getpid ());
217 gpgrt_snprintf (tor_socks_password, sizeof tor_socks_password,
225 /* Disable tor mode. */
227 disable_dns_tormode (void)
233 /* Set verbosity and debug mode for this module. */
235 set_dns_verbose (int verbose, int debug)
237 opt_verbose = verbose;
242 /* Set the Disable-IPv4 flag so that the name resolver does not return
245 set_dns_disable_ipv4 (int yes)
247 opt_disable_ipv4 = !!yes;
251 /* Set the timeout for libdns requests to SECONDS. A value of 0 sets
252 * the default timeout and values are capped at 10 minutes. */
254 set_dns_timeout (int seconds)
257 seconds = DEFAULT_TIMEOUT;
258 else if (seconds < 1)
260 else if (seconds > 600)
263 opt_timeout = seconds;
267 /* Change the default IP address of the nameserver to IPADDR. The
268 address needs to be a numerical IP address and will be used for the
269 next DNS query. Note that this is only used in Tor mode. */
271 set_dns_nameserver (const char *ipaddr)
273 strncpy (tor_nameserver, ipaddr? ipaddr : DEFAULT_NAMESERVER,
274 sizeof tor_nameserver -1);
275 tor_nameserver[sizeof tor_nameserver -1] = 0;
277 libdns_reinit_pending = 1;
278 libdns_tor_port = 0; /* Start again with the default port. */
283 /* Free an addressinfo linked list as returned by resolve_dns_name. */
285 free_dns_addrinfo (dns_addrinfo_t ai)
289 dns_addrinfo_t next = ai->next;
296 #ifndef HAVE_W32_SYSTEM
297 /* Return H_ERRNO mapped to a gpg-error code. Will never return 0. */
299 get_h_errno_as_gpg_error (void)
305 case HOST_NOT_FOUND: ec = GPG_ERR_NO_NAME; break;
306 case TRY_AGAIN: ec = GPG_ERR_TRY_LATER; break;
307 case NO_RECOVERY: ec = GPG_ERR_SERVER_FAILED; break;
308 case NO_DATA: ec = GPG_ERR_NO_DATA; break;
309 default: ec = GPG_ERR_UNKNOWN_ERRNO; break;
311 return gpg_error (ec);
313 #endif /*!HAVE_W32_SYSTEM*/
316 map_eai_to_gpg_error (int ec)
322 case EAI_AGAIN: err = gpg_error (GPG_ERR_EAGAIN); break;
323 case EAI_BADFLAGS: err = gpg_error (GPG_ERR_INV_FLAG); break;
324 case EAI_FAIL: err = gpg_error (GPG_ERR_SERVER_FAILED); break;
325 case EAI_MEMORY: err = gpg_error (GPG_ERR_ENOMEM); break;
327 case EAI_NODATA: err = gpg_error (GPG_ERR_NO_DATA); break;
329 case EAI_NONAME: err = gpg_error (GPG_ERR_NO_NAME); break;
330 case EAI_SERVICE: err = gpg_error (GPG_ERR_NOT_SUPPORTED); break;
331 case EAI_FAMILY: err = gpg_error (GPG_ERR_EAFNOSUPPORT); break;
332 case EAI_SOCKTYPE: err = gpg_error (GPG_ERR_ESOCKTNOSUPPORT); break;
333 #ifndef HAVE_W32_SYSTEM
334 # ifdef EAI_ADDRFAMILY
335 case EAI_ADDRFAMILY:err = gpg_error (GPG_ERR_EADDRNOTAVAIL); break;
337 case EAI_SYSTEM: err = gpg_error_from_syserror (); break;
339 default: err = gpg_error (GPG_ERR_UNKNOWN_ERRNO); break;
347 libdns_error_to_gpg_error (int serr)
353 case 0: ec = 0; break;
355 case DNS_ENOBUFS: ec = GPG_ERR_BUFFER_TOO_SHORT; break;
356 case DNS_EILLEGAL: ec = GPG_ERR_INV_OBJ; break;
357 case DNS_EORDER: ec = GPG_ERR_INV_ORDER; break;
358 case DNS_ESECTION: ec = GPG_ERR_DNS_SECTION; break;
359 case DNS_EUNKNOWN: ec = GPG_ERR_DNS_UNKNOWN; break;
360 case DNS_EADDRESS: ec = GPG_ERR_DNS_ADDRESS; break;
361 case DNS_ENOQUERY: ec = GPG_ERR_DNS_NO_QUERY; break;
362 case DNS_ENOANSWER:ec = GPG_ERR_DNS_NO_ANSWER; break;
363 case DNS_EFETCHED: ec = GPG_ERR_ALREADY_FETCHED; break;
364 case DNS_ESERVICE: ec = GPG_ERR_NOT_SUPPORTED; break;
365 case DNS_ENONAME: ec = GPG_ERR_NO_NAME; break;
366 case DNS_EFAIL: ec = GPG_ERR_SERVER_FAILED; break;
367 case DNS_ECONNFIN: ec = GPG_ERR_DNS_CLOSED; break;
368 case DNS_EVERIFY: ec = GPG_ERR_DNS_VERIFY; break;
372 ec = gpg_err_code_from_errno (serr);
374 ec = GPG_ERR_DNS_UNKNOWN;
377 return gpg_error (ec);
379 #endif /*USE_LIBDNS*/
383 /* Initialize libdns. Returns 0 on success; prints a diagnostic and
384 * returns an error code on failure. */
393 if (libdns.resolv_conf)
394 return 0; /* Already initialized. */
396 memset (&ld, 0, sizeof ld);
398 ld.resolv_conf = dns_resconf_open (&derr);
401 err = libdns_error_to_gpg_error (derr);
402 log_error ("failed to allocate DNS resconf object: %s\n",
409 if (!*tor_nameserver)
410 set_dns_nameserver (NULL);
412 if (!libdns_tor_port)
413 libdns_tor_port = TOR_PORT;
415 cfgstr = xtryasprintf ("[%s]:53", tor_nameserver);
417 err = gpg_error_from_syserror ();
419 err = libdns_error_to_gpg_error
420 (dns_resconf_pton (&ld.resolv_conf->nameserver[0], cfgstr));
422 log_error ("failed to set nameserver '%s': %s\n",
423 cfgstr, gpg_strerror (err));
427 ld.resolv_conf->options.tcp = DNS_RESCONF_TCP_SOCKS;
430 cfgstr = xtryasprintf ("[%s]:%d", "127.0.0.1", libdns_tor_port);
432 err = gpg_error_from_syserror ();
434 err = libdns_error_to_gpg_error
435 (dns_resconf_pton (&ld.socks_host, cfgstr));
438 log_error ("failed to set socks server '%s': %s\n",
439 cfgstr, gpg_strerror (err));
445 #ifdef HAVE_W32_SYSTEM
452 ninfo = xtrymalloc (ninfo_len);
455 err = gpg_error_from_syserror ();
459 if (GetNetworkParams (ninfo, &ninfo_len))
461 log_error ("GetNetworkParms failed: %s\n", w32_strerror (-1));
462 err = gpg_error (GPG_ERR_GENERAL);
467 for (idx=0, pip = &(ninfo->DnsServerList);
468 pip && idx < DIM (ld.resolv_conf->nameserver);
472 log_debug ("dns: dnsserver[%d] '%s'\n", idx, pip->IpAddress.String);
473 err = libdns_error_to_gpg_error
474 (dns_resconf_pton (&ld.resolv_conf->nameserver[idx],
475 pip->IpAddress.String));
477 log_error ("failed to set nameserver[%d] '%s': %s\n",
478 idx, pip->IpAddress.String, gpg_strerror (err));
487 fname = "/etc/resolv.conf";
488 err = libdns_error_to_gpg_error
489 (dns_resconf_loadpath (ld.resolv_conf, fname));
492 log_error ("failed to load '%s': %s\n", fname, gpg_strerror (err));
496 fname = "/etc/nsswitch.conf";
497 err = libdns_error_to_gpg_error
498 (dns_nssconf_loadpath (ld.resolv_conf, fname));
501 log_error ("failed to load '%s': %s\n", fname, gpg_strerror (err));
502 /* not fatal, nsswitch.conf is not used on all systems; assume
503 * classic behavior instead. Our dns library states "bf" which tries
504 * DNS then Files, which is not classic; FreeBSD
505 * /usr/src/lib/libc/net/gethostnamadr.c defines default_src[] which
506 * is Files then DNS, which is. */
508 log_debug ("dns: fallback resolution order, files then DNS\n");
509 ld.resolv_conf->lookup[0] = 'f';
510 ld.resolv_conf->lookup[1] = 'b';
511 ld.resolv_conf->lookup[2] = '\0';
512 err = GPG_ERR_NO_ERROR;
518 ld.hosts = dns_hosts_open (&derr);
521 log_error ("failed to load hosts file: %s\n", gpg_strerror (err));
522 err = libdns_error_to_gpg_error (derr);
526 /* dns_hints_local for stub mode, dns_hints_root for recursive. */
527 ld.hints = (recursive_resolver
528 ? dns_hints_root (ld.resolv_conf, &derr)
529 : dns_hints_local (ld.resolv_conf, &derr));
532 log_error ("failed to load DNS hints: %s\n", gpg_strerror (err));
533 err = libdns_error_to_gpg_error (derr);
537 /* All fine. Make the data global. */
541 log_debug ("dns: libdns initialized%s\n", tor_mode?" (tor mode)":"");
547 #endif /*USE_LIBDNS*/
551 /* Deinitialize libdns. */
557 if (!libdns.resolv_conf)
558 return; /* Not initialized. */
561 memset (&libdns, 0, sizeof libdns);
562 dns_hints_close (ld.hints);
563 dns_hosts_close (ld.hosts);
564 dns_resconf_close (ld.resolv_conf);
566 #endif /*USE_LIBDNS*/
569 /* SIGHUP action handler for this module. With FORCE set objects are
570 * all immediately released. */
572 reload_dns_stuff (int force)
578 libdns_reinit_pending = 0;
582 libdns_reinit_pending = 1;
583 libdns_tor_port = 0; /* Start again with the default port. */
593 * Initialize libdns if needed and open a dns_resolver context.
594 * Returns 0 on success and stores the new context at R_RES. On
595 * failure an error code is returned and NULL stored at R_RES.
598 libdns_res_open (struct dns_resolver **r_res)
601 struct dns_resolver *res;
606 if (libdns_reinit_pending)
608 libdns_reinit_pending = 0;
612 err = libdns_init ();
619 res = dns_res_open (libdns.resolv_conf, libdns.hosts, libdns.hints, NULL,
620 dns_opts (.socks_host = &libdns.socks_host,
621 .socks_user = tor_socks_user,
622 .socks_password = tor_socks_password ),
625 return libdns_error_to_gpg_error (derr);
630 #endif /*USE_LIBDNS*/
634 /* Helper to test whether we need to try again after having switched
637 libdns_switch_port_p (gpg_error_t err)
639 if (tor_mode && gpg_err_code (err) == GPG_ERR_ECONNREFUSED
640 && libdns_tor_port == TOR_PORT)
642 /* Switch port and try again. */
644 log_debug ("dns: switching from SOCKS port %d to %d\n",
645 TOR_PORT, TOR_PORT2);
646 libdns_tor_port = TOR_PORT2;
647 libdns_reinit_pending = 1;
652 #endif /*USE_LIBDNS*/
656 /* Wrapper around dns_res_submit. */
658 libdns_res_submit (struct dns_resolver *res, const char *qname,
659 enum dns_type qtype, enum dns_class qclass)
661 return libdns_error_to_gpg_error (dns_res_submit (res, qname, qtype, qclass));
663 #endif /*USE_LIBDNS*/
667 /* Standard event handling loop. */
669 libdns_res_wait (struct dns_resolver *res)
673 while ((err = libdns_error_to_gpg_error (dns_res_check (res)))
674 && gpg_err_code (err) == GPG_ERR_EAGAIN)
676 if (dns_res_elapsed (res) > opt_timeout)
678 err = gpg_error (GPG_ERR_DNS_TIMEOUT);
683 dns_res_poll (res, 1);
689 #endif /*USE_LIBDNS*/
694 resolve_name_libdns (const char *name, unsigned short port,
695 int want_family, int want_socktype,
696 dns_addrinfo_t *r_dai, char **r_canonname)
699 dns_addrinfo_t daihead = NULL;
701 struct dns_resolver *res = NULL;
702 struct dns_addrinfo *ai = NULL;
703 struct addrinfo hints;
704 struct addrinfo *ent;
706 char *portstr = NULL;
713 memset (&hints, 0, sizeof hints);
714 hints.ai_family = want_family;
715 hints.ai_socktype = want_socktype;
716 hints.ai_flags = AI_ADDRCONFIG;
718 hints.ai_flags |= AI_CANONNAME;
719 if (is_ip_address (name))
720 hints.ai_flags |= AI_NUMERICHOST;
724 snprintf (portstr_, sizeof portstr_, "%hu", port);
728 err = libdns_res_open (&res);
732 ai = dns_ai_open (name, portstr, 0, &hints, res, &derr);
735 err = libdns_error_to_gpg_error (derr);
739 /* Loop over all records. */
742 err = libdns_error_to_gpg_error (dns_ai_nextent (&ent, ai));
743 if (gpg_err_code (err) == GPG_ERR_ENOENT)
746 err = 0; /* We got some results, we're good. */
749 if (gpg_err_code (err) == GPG_ERR_EAGAIN)
751 if (dns_ai_elapsed (ai) > opt_timeout)
753 err = gpg_error (GPG_ERR_DNS_TIMEOUT);
765 if (r_canonname && ! *r_canonname && ent && ent->ai_canonname)
767 *r_canonname = xtrystrdup (ent->ai_canonname);
770 err = gpg_error_from_syserror ();
773 /* Libdns appends the root zone part which is problematic
774 * for most other functions - strip it. */
775 if (**r_canonname && (*r_canonname)[strlen (*r_canonname)-1] == '.')
776 (*r_canonname)[strlen (*r_canonname)-1] = 0;
779 dai = xtrymalloc (sizeof *dai + ent->ai_addrlen -1);
782 err = gpg_error_from_syserror ();
786 dai->family = ent->ai_family;
787 dai->socktype = ent->ai_socktype;
788 dai->protocol = ent->ai_protocol;
789 dai->addrlen = ent->ai_addrlen;
790 memcpy (dai->addr, ent->ai_addr, ent->ai_addrlen);
805 xfree (*r_canonname);
808 free_dns_addrinfo (daihead);
815 #endif /*USE_LIBDNS*/
818 /* Resolve a name using the standard system function. */
820 resolve_name_standard (const char *name, unsigned short port,
821 int want_family, int want_socktype,
822 dns_addrinfo_t *r_dai, char **r_canonname)
825 dns_addrinfo_t daihead = NULL;
827 struct addrinfo *aibuf = NULL;
828 struct addrinfo hints, *ai;
836 memset (&hints, 0, sizeof hints);
837 hints.ai_family = want_family;
838 hints.ai_socktype = want_socktype;
839 hints.ai_flags = AI_ADDRCONFIG;
841 hints.ai_flags |= AI_CANONNAME;
842 if (is_ip_address (name))
843 hints.ai_flags |= AI_NUMERICHOST;
846 snprintf (portstr, sizeof portstr, "%hu", port);
850 /* We can't use the the AI_IDN flag because that does the conversion
851 using the current locale. However, GnuPG always used UTF-8. To
852 support IDN we would need to make use of the libidn API. */
853 ret = getaddrinfo (name, *portstr? portstr : NULL, &hints, &aibuf);
857 err = map_eai_to_gpg_error (ret);
858 if (gpg_err_code (err) == GPG_ERR_NO_NAME)
860 /* There seems to be a bug in the glibc getaddrinfo function
861 if the CNAME points to a long list of A and AAAA records
862 in which case the function return NO_NAME. Let's do the
863 CNAME redirection again. */
866 if (get_dns_cname (name, &cname))
867 goto leave; /* Still no success. */
869 ret = getaddrinfo (cname, *portstr? portstr : NULL, &hints, &aibuf);
874 err = map_eai_to_gpg_error (ret);
877 err = 0; /* Yep, now it worked. */
883 if (r_canonname && aibuf && aibuf->ai_canonname)
885 *r_canonname = xtrystrdup (aibuf->ai_canonname);
888 err = gpg_error_from_syserror ();
893 for (ai = aibuf; ai; ai = ai->ai_next)
895 if (ai->ai_family != AF_INET6 && ai->ai_family != AF_INET)
897 if (opt_disable_ipv4 && ai->ai_family == AF_INET)
900 dai = xtrymalloc (sizeof *dai + ai->ai_addrlen - 1);
901 dai->family = ai->ai_family;
902 dai->socktype = ai->ai_socktype;
903 dai->protocol = ai->ai_protocol;
904 dai->addrlen = ai->ai_addrlen;
905 memcpy (dai->addr, ai->ai_addr, ai->ai_addrlen);
912 freeaddrinfo (aibuf);
917 xfree (*r_canonname);
920 free_dns_addrinfo (daihead);
928 /* This a wrapper around getaddrinfo with slightly different semantics.
929 NAME is the name to resolve.
930 PORT is the requested port or 0.
931 WANT_FAMILY is either 0 (AF_UNSPEC), AF_INET6, or AF_INET4.
932 WANT_SOCKETTYPE is either SOCK_STREAM or SOCK_DGRAM.
934 On success the result is stored in a linked list with the head
935 stored at the address R_AI; the caller must call gpg_addrinfo_free
936 on this. If R_CANONNAME is not NULL the official name of the host
937 is stored there as a malloced string; if that name is not available
940 resolve_dns_name (const char *name, unsigned short port,
941 int want_family, int want_socktype,
942 dns_addrinfo_t *r_ai, char **r_canonname)
947 if (!standard_resolver)
949 err = resolve_name_libdns (name, port, want_family, want_socktype,
951 if (err && libdns_switch_port_p (err))
952 err = resolve_name_libdns (name, port, want_family, want_socktype,
956 #endif /*USE_LIBDNS*/
957 err = resolve_name_standard (name, port, want_family, want_socktype,
960 log_debug ("dns: resolve_dns_name(%s): %s\n", name, gpg_strerror (err));
966 /* Resolve an address using libdns. */
968 resolve_addr_libdns (const struct sockaddr *addr, int addrlen,
969 unsigned int flags, char **r_name)
972 char host[DNS_D_MAXNAME + 1];
973 struct dns_resolver *res = NULL;
974 struct dns_packet *ans = NULL;
980 /* First we turn ADDR into a DNS name (with ".arpa" suffix). */
982 if (addr->sa_family == AF_INET6)
984 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)addr;
985 if (!dns_aaaa_arpa (host, sizeof host, (void*)&a6->sin6_addr))
986 err = gpg_error (GPG_ERR_INV_OBJ);
988 else if (addr->sa_family == AF_INET)
990 const struct sockaddr_in *a4 = (const struct sockaddr_in *)addr;
991 if (!dns_a_arpa (host, sizeof host, (void*)&a4->sin_addr))
992 err = gpg_error (GPG_ERR_INV_OBJ);
995 err = gpg_error (GPG_ERR_EAFNOSUPPORT);
1000 err = libdns_res_open (&res);
1004 err = libdns_res_submit (res, host, DNS_T_PTR, DNS_C_IN);
1008 err = libdns_res_wait (res);
1012 ans = dns_res_fetch (res, &derr);
1015 err = libdns_error_to_gpg_error (derr);
1019 /* Check the rcode. */
1020 switch (dns_p_rcode (ans))
1022 case DNS_RC_NOERROR:
1024 case DNS_RC_NXDOMAIN:
1025 err = gpg_error (GPG_ERR_NO_NAME);
1028 err = GPG_ERR_SERVER_FAILED;
1032 /* Parse the result. */
1036 struct dns_rr_i rri;
1038 memset (&rri, 0, sizeof rri);
1039 dns_rr_i_init (&rri, ans);
1040 rri.section = DNS_S_ALL & ~DNS_S_QD;
1042 rri.type = DNS_T_PTR;
1044 if (!dns_rr_grep (&rr, 1, &rri, ans, &derr))
1046 err = gpg_error (GPG_ERR_NOT_FOUND);
1050 err = libdns_error_to_gpg_error (dns_ptr_parse (&ptr, &rr, ans));
1055 *r_name = xtrystrdup (ptr.host);
1058 err = gpg_error_from_syserror ();
1061 /* Libdns appends the root zone part which is problematic
1062 * for most other functions - strip it. */
1063 if (**r_name && (*r_name)[strlen (*r_name)-1] == '.')
1064 (*r_name)[strlen (*r_name)-1] = 0;
1066 else /* GPG_ERR_NO_NAME */
1073 buflen = sizeof ptr.host;
1076 if (addr->sa_family == AF_INET6 && (flags & DNS_WITHBRACKET))
1081 ec = getnameinfo (addr, addrlen, p, buflen, NULL, 0, NI_NUMERICHOST);
1084 err = map_eai_to_gpg_error (ec);
1087 if (addr->sa_family == AF_INET6 && (flags & DNS_WITHBRACKET))
1088 strcat (buffer, "]");
1093 dns_res_close (res);
1096 #endif /*USE_LIBDNS*/
1099 /* Resolve an address using the standard system function. */
1101 resolve_addr_standard (const struct sockaddr *addr, int addrlen,
1102 unsigned int flags, char **r_name)
1111 buflen = NI_MAXHOST;
1112 buffer = xtrymalloc (buflen + 2 + 1);
1114 return gpg_error_from_syserror ();
1116 if ((flags & DNS_NUMERICHOST) || tor_mode)
1119 ec = getnameinfo (addr, addrlen, buffer, buflen, NULL, 0, NI_NAMEREQD);
1121 if (!ec && *buffer == '[')
1122 ec = EAI_FAIL; /* A name may never start with a bracket. */
1123 else if (ec == EAI_NONAME)
1126 if (addr->sa_family == AF_INET6 && (flags & DNS_WITHBRACKET))
1131 ec = getnameinfo (addr, addrlen, p, buflen, NULL, 0, NI_NUMERICHOST);
1132 if (!ec && addr->sa_family == AF_INET6 && (flags & DNS_WITHBRACKET))
1133 strcat (buffer, "]");
1137 err = map_eai_to_gpg_error (ec);
1140 p = xtryrealloc (buffer, strlen (buffer)+1);
1142 err = gpg_error_from_syserror ();
1159 /* A wrapper around getnameinfo. */
1161 resolve_dns_addr (const struct sockaddr *addr, int addrlen,
1162 unsigned int flags, char **r_name)
1167 /* Note that we divert to the standard resolver for NUMERICHOST. */
1168 if (!standard_resolver && !(flags & DNS_NUMERICHOST))
1170 err = resolve_addr_libdns (addr, addrlen, flags, r_name);
1171 if (err && libdns_switch_port_p (err))
1172 err = resolve_addr_libdns (addr, addrlen, flags, r_name);
1175 #endif /*USE_LIBDNS*/
1176 err = resolve_addr_standard (addr, addrlen, flags, r_name);
1179 log_debug ("dns: resolve_dns_addr(): %s\n", gpg_strerror (err));
1184 /* Check whether NAME is an IP address. Returns a true if it is
1185 * either an IPv6 or a IPv4 numerical address. The actual return
1186 * values can also be used to identify whether it is v4 or v6: The
1187 * true value will surprisingly be 4 for IPv4 and 6 for IPv6. */
1189 is_ip_address (const char *name)
1192 int ndots, dblcol, n;
1195 return 6; /* yes: A legal DNS name may not contain this character;
1196 this mut be bracketed v6 address. */
1198 return 0; /* No. A leading dot is not a valid IP address. */
1200 /* Check whether this is a v6 address. */
1201 ndots = n = dblcol = 0;
1202 for (s=name; *s; s++)
1211 return 0; /* No: Only one "::" allowed. */
1220 else if (!strchr ("0123456789abcdefABCDEF", *s))
1221 return 0; /* No: Not a hex digit. */
1223 return 0; /* To many digits in a group. */
1226 return 0; /* No: Too many colons. */
1228 return 6; /* Yes: At least 2 colons indicate an v6 address. */
1231 /* Check whether it is legacy IP address. */
1233 for (s=name; *s; s++)
1238 return 0; /* No: Douple dot. */
1239 if (atoi (s+1) > 255)
1240 return 0; /* No: Ipv4 byte value too large. */
1244 else if (!strchr ("0123456789", *s))
1245 return 0; /* No: Not a digit. */
1247 return 0; /* No: More than 3 digits. */
1249 return (ndots == 3)? 4 : 0;
1253 /* Return true if NAME is an onion address. */
1255 is_onion_address (const char *name)
1259 len = name? strlen (name) : 0;
1260 if (len < 8 || strcmp (name + len - 6, ".onion"))
1262 /* Note that we require at least 2 characters before the suffix. */
1263 return 1; /* Yes. */
1267 /* libdns version of get_dns_cert. */
1270 get_dns_cert_libdns (const char *name, int want_certtype,
1271 void **r_key, size_t *r_keylen,
1272 unsigned char **r_fpr, size_t *r_fprlen, char **r_url)
1275 struct dns_resolver *res = NULL;
1276 struct dns_packet *ans = NULL;
1278 struct dns_rr_i rri;
1279 char host[DNS_D_MAXNAME + 1];
1283 /* Get the query type from WANT_CERTTYPE (which in general indicates
1284 * the subtype we want). */
1285 qtype = (want_certtype < DNS_CERTTYPE_RRBASE
1287 : (want_certtype - DNS_CERTTYPE_RRBASE));
1290 err = libdns_res_open (&res);
1294 if (dns_d_anchor (host, sizeof host, name, strlen (name)) >= sizeof host)
1296 err = gpg_error (GPG_ERR_ENAMETOOLONG);
1300 err = libdns_res_submit (res, name, qtype, DNS_C_IN);
1304 err = libdns_res_wait (res);
1308 ans = dns_res_fetch (res, &derr);
1311 err = libdns_error_to_gpg_error (derr);
1315 /* Check the rcode. */
1316 switch (dns_p_rcode (ans))
1318 case DNS_RC_NOERROR: break;
1319 case DNS_RC_NXDOMAIN: err = gpg_error (GPG_ERR_NO_NAME); break;
1320 default: err = GPG_ERR_SERVER_FAILED; break;
1325 memset (&rri, 0, sizeof rri);
1326 dns_rr_i_init (&rri, ans);
1327 rri.section = DNS_S_ALL & ~DNS_S_QD;
1331 err = gpg_error (GPG_ERR_NOT_FOUND);
1332 while (dns_rr_grep (&rr, 1, &rri, ans, &derr))
1334 unsigned char *rp = ans->data + rr.rd.p;
1335 unsigned short len = rr.rd.len;
1340 /* Definitely too short - skip. */
1342 else if (want_certtype >= DNS_CERTTYPE_RRBASE
1343 && rr.type == (want_certtype - DNS_CERTTYPE_RRBASE)
1346 *r_key = xtrymalloc (len);
1348 err = gpg_error_from_syserror ();
1351 memcpy (*r_key, rp, len);
1357 else if (want_certtype >= DNS_CERTTYPE_RRBASE)
1359 /* We did not found the requested RR - skip. */
1361 else if (rr.type == T_CERT && len > 5)
1363 /* We got a CERT type. */
1364 subtype = buf16_to_u16 (rp);
1367 /* Skip the CERT key tag and algo which we don't need. */
1370 if (want_certtype && want_certtype != subtype)
1371 ; /* Not the requested subtype - skip. */
1372 else if (subtype == DNS_CERTTYPE_PGP && len && r_key && r_keylen)
1375 *r_key = xtrymalloc (len);
1377 err = gpg_error_from_syserror ();
1380 memcpy (*r_key, rp, len);
1386 else if (subtype == DNS_CERTTYPE_IPGP
1387 && len && len < 1023 && len >= rp[0] + 1)
1393 *r_fpr = xtrymalloc (*r_fprlen);
1396 err = gpg_error_from_syserror ();
1399 memcpy (*r_fpr, rp+1, *r_fprlen);
1404 if (len > *r_fprlen + 1)
1406 *r_url = xtrymalloc (len - (*r_fprlen + 1) + 1);
1409 err = gpg_error_from_syserror ();
1414 memcpy (*r_url, rp + *r_fprlen + 1, len - (*r_fprlen + 1));
1415 (*r_url)[len - (*r_fprlen + 1)] = 0;
1425 /* Unknown subtype or record too short - skip. */
1430 /* Not a requested type - skip. */
1436 dns_res_close (res);
1439 #endif /*USE_LIBDNS*/
1442 /* Standard resolver version of get_dns_cert. */
1444 get_dns_cert_standard (const char *name, int want_certtype,
1445 void **r_key, size_t *r_keylen,
1446 unsigned char **r_fpr, size_t *r_fprlen, char **r_url)
1448 #ifdef HAVE_SYSTEM_RESOLVER
1450 unsigned char *answer;
1454 /* Allocate a 64k buffer which is the limit for an DNS response. */
1455 answer = xtrymalloc (65536);
1457 return gpg_error_from_syserror ();
1459 err = gpg_error (GPG_ERR_NOT_FOUND);
1460 r = res_query (name, C_IN,
1461 (want_certtype < DNS_CERTTYPE_RRBASE
1463 : (want_certtype - DNS_CERTTYPE_RRBASE)),
1465 /* Not too big, not too small, no errors and at least 1 answer. */
1466 if (r >= sizeof (HEADER) && r <= 65536
1467 && (((HEADER *)(void *) answer)->rcode) == NOERROR
1468 && (count = ntohs (((HEADER *)(void *) answer)->ancount)))
1471 unsigned char *pt, *emsg;
1475 pt = &answer[sizeof (HEADER)];
1477 /* Skip over the query */
1479 rc = dn_skipname (pt, emsg);
1482 err = gpg_error (GPG_ERR_INV_OBJ);
1485 pt += rc + QFIXEDSZ;
1487 /* There are several possible response types for a CERT request.
1488 We're interested in the PGP (a key) and IPGP (a URI) types.
1489 Skip all others. TODO: A key is better than a URI since
1490 we've gone through all this bother to fetch it, so favor that
1491 if we have both PGP and IPGP? */
1493 while (count-- > 0 && pt < emsg)
1495 u16 type, class, dlen, ctype;
1497 rc = dn_skipname (pt, emsg); /* the name we just queried for */
1500 err = gpg_error (GPG_ERR_INV_OBJ);
1506 /* Truncated message? 15 bytes takes us to the point where
1507 we start looking at the ctype. */
1508 if ((emsg - pt) < 15)
1511 type = buf16_to_u16 (pt);
1514 class = buf16_to_u16 (pt);
1524 dlen = buf16_to_u16 (pt);
1527 /* Check the type and parse. */
1528 if (want_certtype >= DNS_CERTTYPE_RRBASE
1529 && type == (want_certtype - DNS_CERTTYPE_RRBASE)
1532 *r_key = xtrymalloc (dlen);
1534 err = gpg_error_from_syserror ();
1537 memcpy (*r_key, pt, dlen);
1543 else if (want_certtype >= DNS_CERTTYPE_RRBASE)
1545 /* We did not found the requested RR. */
1548 else if (type == T_CERT)
1550 /* We got a CERT type. */
1551 ctype = buf16_to_u16 (pt);
1554 /* Skip the CERT key tag and algo which we don't need. */
1559 /* 15 bytes takes us to here */
1560 if (want_certtype && want_certtype != ctype)
1561 ; /* Not of the requested certtype. */
1562 else if (ctype == DNS_CERTTYPE_PGP && dlen && r_key && r_keylen)
1565 *r_key = xtrymalloc (dlen);
1567 err = gpg_error_from_syserror ();
1570 memcpy (*r_key, pt, dlen);
1576 else if (ctype == DNS_CERTTYPE_IPGP
1577 && dlen && dlen < 1023 && dlen >= pt[0] + 1)
1583 *r_fpr = xtrymalloc (*r_fprlen);
1586 err = gpg_error_from_syserror ();
1589 memcpy (*r_fpr, &pt[1], *r_fprlen);
1594 if (dlen > *r_fprlen + 1)
1596 *r_url = xtrymalloc (dlen - (*r_fprlen + 1) + 1);
1599 err = gpg_error_from_syserror ();
1604 memcpy (*r_url, &pt[*r_fprlen + 1],
1605 dlen - (*r_fprlen + 1));
1606 (*r_url)[dlen - (*r_fprlen + 1)] = '\0';
1615 /* No subtype matches, so continue with the next answer. */
1620 /* Not a requested type - might be a CNAME. Try next item. */
1630 #else /*!HAVE_SYSTEM_RESOLVER*/
1633 (void)want_certtype;
1639 return gpg_error (GPG_ERR_NOT_SUPPORTED);
1641 #endif /*!HAVE_SYSTEM_RESOLVER*/
1645 /* Returns 0 on success or an error code. If a PGP CERT record was
1646 found, the malloced data is returned at (R_KEY, R_KEYLEN) and
1647 the other return parameters are set to NULL/0. If an IPGP CERT
1648 record was found the fingerprint is stored as an allocated block at
1649 R_FPR and its length at R_FPRLEN; an URL is is allocated as a
1650 string and returned at R_URL. If WANT_CERTTYPE is 0 this function
1651 returns the first CERT found with a supported type; it is expected
1652 that only one CERT record is used. If WANT_CERTTYPE is one of the
1653 supported certtypes only records with this certtype are considered
1654 and the first found is returned. (R_KEY,R_KEYLEN) are optional. */
1656 get_dns_cert (const char *name, int want_certtype,
1657 void **r_key, size_t *r_keylen,
1658 unsigned char **r_fpr, size_t *r_fprlen, char **r_url)
1671 if (!standard_resolver)
1673 err = get_dns_cert_libdns (name, want_certtype, r_key, r_keylen,
1674 r_fpr, r_fprlen, r_url);
1675 if (err && libdns_switch_port_p (err))
1676 err = get_dns_cert_libdns (name, want_certtype, r_key, r_keylen,
1677 r_fpr, r_fprlen, r_url);
1680 #endif /*USE_LIBDNS*/
1681 err = get_dns_cert_standard (name, want_certtype, r_key, r_keylen,
1682 r_fpr, r_fprlen, r_url);
1685 log_debug ("dns: get_dns_cert(%s): %s\n", name, gpg_strerror (err));
1691 priosort(const void *a,const void *b)
1693 const struct srventry *sa=a,*sb=b;
1694 if(sa->priority>sb->priority)
1696 else if(sa->priority<sb->priority)
1703 /* Libdns based helper for getsrv. Note that it is expected that NULL
1704 * is stored at the address of LIST and 0 is stored at the address of
1708 getsrv_libdns (const char *name, struct srventry **list, unsigned int *r_count)
1711 struct dns_resolver *res = NULL;
1712 struct dns_packet *ans = NULL;
1714 struct dns_rr_i rri;
1715 char host[DNS_D_MAXNAME + 1];
1717 unsigned int srvcount = 0;
1719 err = libdns_res_open (&res);
1723 if (dns_d_anchor (host, sizeof host, name, strlen (name)) >= sizeof host)
1725 err = gpg_error (GPG_ERR_ENAMETOOLONG);
1729 err = libdns_res_submit (res, name, DNS_T_SRV, DNS_C_IN);
1733 err = libdns_res_wait (res);
1737 ans = dns_res_fetch (res, &derr);
1740 err = libdns_error_to_gpg_error (derr);
1744 /* Check the rcode. */
1745 switch (dns_p_rcode (ans))
1747 case DNS_RC_NOERROR: break;
1748 case DNS_RC_NXDOMAIN: err = gpg_error (GPG_ERR_NO_NAME); break;
1749 default: err = GPG_ERR_SERVER_FAILED; break;
1754 memset (&rri, 0, sizeof rri);
1755 dns_rr_i_init (&rri, ans);
1756 rri.section = DNS_S_ALL & ~DNS_S_QD;
1758 rri.type = DNS_T_SRV;
1760 while (dns_rr_grep (&rr, 1, &rri, ans, &derr))
1762 struct dns_srv dsrv;
1763 struct srventry *srv;
1764 struct srventry *newlist;
1766 err = libdns_error_to_gpg_error (dns_srv_parse(&dsrv, &rr, ans));
1770 newlist = xtryrealloc (*list, (srvcount+1)*sizeof(struct srventry));
1773 err = gpg_error_from_syserror ();
1777 memset (&(*list)[srvcount], 0, sizeof(struct srventry));
1778 srv = &(*list)[srvcount];
1780 srv->priority = dsrv.priority;
1781 srv->weight = dsrv.weight;
1782 srv->port = dsrv.port;
1783 mem2str (srv->target, dsrv.target, sizeof srv->target);
1784 /* Libdns appends the root zone part which is problematic for
1785 * most other functions - strip it. */
1786 if (*srv->target && (srv->target)[strlen (srv->target)-1] == '.')
1787 (srv->target)[strlen (srv->target)-1] = 0;
1790 *r_count = srvcount;
1799 dns_res_close (res);
1802 #endif /*USE_LIBDNS*/
1805 /* Standard resolver based helper for getsrv. Note that it is
1806 * expected that NULL is stored at the address of LIST and 0 is stored
1807 * at the address of R_COUNT. */
1809 getsrv_standard (const char *name,
1810 struct srventry **list, unsigned int *r_count)
1812 #ifdef HAVE_SYSTEM_RESOLVER
1814 unsigned char ans[2048];
1817 unsigned char *answer = res.ans;
1818 HEADER *header = res.header;
1819 unsigned char *pt, *emsg;
1822 unsigned int srvcount = 0;
1825 /* Do not allow a query using the standard resolver in Tor mode. */
1827 return gpg_error (GPG_ERR_NOT_ENABLED);
1830 r = res_query (name, C_IN, T_SRV, answer, sizeof res.ans);
1833 return get_h_errno_as_gpg_error ();
1834 if (r < sizeof (HEADER))
1835 return gpg_error (GPG_ERR_SERVER_FAILED);
1836 if (r > sizeof res.ans)
1837 return gpg_error (GPG_ERR_SYSTEM_BUG);
1838 if (header->rcode != NOERROR || !(count=ntohs (header->ancount)))
1839 return gpg_error (GPG_ERR_NO_NAME); /* Error or no record found. */
1842 pt = &answer[sizeof(HEADER)];
1844 /* Skip over the query */
1845 rc = dn_skipname (pt, emsg);
1849 pt += rc + QFIXEDSZ;
1851 while (count-- > 0 && pt < emsg)
1853 struct srventry *srv;
1855 struct srventry *newlist;
1857 newlist = xtryrealloc (*list, (srvcount+1)*sizeof(struct srventry));
1861 memset (&(*list)[srvcount], 0, sizeof(struct srventry));
1862 srv = &(*list)[srvcount];
1865 rc = dn_skipname (pt, emsg); /* The name we just queried for. */
1870 /* Truncated message? */
1874 type = buf16_to_u16 (pt);
1876 /* We asked for SRV and got something else !? */
1880 class = buf16_to_u16 (pt);
1882 /* We asked for IN and got something else !? */
1887 dlen = buf16_to_u16 (pt);
1890 srv->priority = buf16_to_ushort (pt);
1892 srv->weight = buf16_to_ushort (pt);
1894 srv->port = buf16_to_ushort (pt);
1897 /* Get the name. 2782 doesn't allow name compression, but
1898 * dn_expand still works to pull the name out of the packet. */
1899 rc = dn_expand (answer, emsg, pt, srv->target, sizeof srv->target);
1900 if (rc == 1 && srv->target[0] == 0) /* "." */
1909 /* Corrupt packet? */
1914 *r_count = srvcount;
1920 return gpg_error (GPG_ERR_GENERAL);
1922 #else /*!HAVE_SYSTEM_RESOLVER*/
1927 return gpg_error (GPG_ERR_NOT_SUPPORTED);
1929 #endif /*!HAVE_SYSTEM_RESOLVER*/
1933 /* Query a SRV record for SERVICE and PROTO for NAME. If SERVICE is
1934 * NULL, NAME is expected to contain the full query name. Note that
1935 * we do not return NONAME but simply store 0 at R_COUNT. On error an
1936 * error code is returned and 0 stored at R_COUNT. */
1938 get_dns_srv (const char *name, const char *service, const char *proto,
1939 struct srventry **list, unsigned int *r_count)
1942 char *namebuffer = NULL;
1943 unsigned int srvcount;
1950 /* If SERVICE is given construct the query from it and PROTO. */
1953 namebuffer = xtryasprintf ("_%s._%s.%s",
1954 service, proto? proto:"tcp", name);
1957 err = gpg_error_from_syserror ();
1965 if (!standard_resolver)
1967 err = getsrv_libdns (name, list, &srvcount);
1968 if (err && libdns_switch_port_p (err))
1969 err = getsrv_libdns (name, list, &srvcount);
1972 #endif /*USE_LIBDNS*/
1973 err = getsrv_standard (name, list, &srvcount);
1977 if (gpg_err_code (err) == GPG_ERR_NO_NAME)
1982 /* Now we have an array of all the srv records. */
1984 /* Order by priority */
1985 qsort(*list,srvcount,sizeof(struct srventry),priosort);
1987 /* For each priority, move the zero-weighted items first. */
1988 for (i=0; i < srvcount; i++)
1992 for (j=i;j < srvcount && (*list)[i].priority == (*list)[j].priority; j++)
1994 if((*list)[j].weight==0)
1999 struct srventry temp;
2001 memcpy (&temp,&(*list)[j],sizeof(struct srventry));
2002 memcpy (&(*list)[j],&(*list)[i],sizeof(struct srventry));
2003 memcpy (&(*list)[i],&temp,sizeof(struct srventry));
2011 /* Run the RFC-2782 weighting algorithm. We don't need very high
2012 quality randomness for this, so regular libc srand/rand is
2020 srand (time (NULL)*getpid());
2024 for (i=0; i < srvcount; i++)
2027 float prio_count=0,chose;
2029 for (j=i; j < srvcount && (*list)[i].priority == (*list)[j].priority; j++)
2031 prio_count+=(*list)[j].weight;
2032 (*list)[j].run_count=prio_count;
2035 chose=prio_count*rand()/RAND_MAX;
2037 for (j=i;j<srvcount && (*list)[i].priority==(*list)[j].priority;j++)
2039 if (chose<=(*list)[j].run_count)
2044 struct srventry temp;
2046 memcpy(&temp,&(*list)[j],sizeof(struct srventry));
2047 memcpy(&(*list)[j],&(*list)[i],sizeof(struct srventry));
2048 memcpy(&(*list)[i],&temp,sizeof(struct srventry));
2059 log_debug ("dns: getsrv(%s): %s\n", name, gpg_strerror (err));
2061 log_debug ("dns: getsrv(%s) -> %u records\n", name, srvcount);
2064 *r_count = srvcount;
2072 /* libdns version of get_dns_cname. */
2074 get_dns_cname_libdns (const char *name, char **r_cname)
2077 struct dns_resolver *res;
2078 struct dns_packet *ans = NULL;
2079 struct dns_cname cname;
2082 err = libdns_res_open (&res);
2086 err = libdns_res_submit (res, name, DNS_T_CNAME, DNS_C_IN);
2090 err = libdns_res_wait (res);
2094 ans = dns_res_fetch (res, &derr);
2097 err = libdns_error_to_gpg_error (derr);
2101 /* Check the rcode. */
2102 switch (dns_p_rcode (ans))
2104 case DNS_RC_NOERROR: break;
2105 case DNS_RC_NXDOMAIN: err = gpg_error (GPG_ERR_NO_NAME); break;
2106 default: err = GPG_ERR_SERVER_FAILED; break;
2111 /* Parse the result into CNAME. */
2112 err = libdns_error_to_gpg_error (dns_p_study (ans));
2116 if (!dns_d_cname (&cname, sizeof cname, name, strlen (name), ans, &derr))
2118 err = libdns_error_to_gpg_error (derr);
2123 *r_cname = xtrystrdup (cname.host);
2125 err = gpg_error_from_syserror ();
2128 /* Libdns appends the root zone part which is problematic
2129 * for most other functions - strip it. */
2130 if (**r_cname && (*r_cname)[strlen (*r_cname)-1] == '.')
2131 (*r_cname)[strlen (*r_cname)-1] = 0;
2136 dns_res_close (res);
2139 #endif /*USE_LIBDNS*/
2142 /* Standard resolver version of get_dns_cname. */
2144 get_dns_cname_standard (const char *name, char **r_cname)
2146 #ifdef HAVE_SYSTEM_RESOLVER
2150 unsigned char ans[2048];
2153 unsigned char *answer = res.ans;
2154 HEADER *header = res.header;
2155 unsigned char *pt, *emsg;
2158 int cnamesize = 1025;
2161 /* Do not allow a query using the standard resolver in Tor mode. */
2166 r = res_query (name, C_IN, T_CERT, answer, sizeof res.ans);
2169 return get_h_errno_as_gpg_error ();
2170 if (r < sizeof (HEADER))
2171 return gpg_error (GPG_ERR_SERVER_FAILED);
2172 if (r > sizeof res.ans)
2173 return gpg_error (GPG_ERR_SYSTEM_BUG);
2174 if (header->rcode != NOERROR || !(count=ntohs (header->ancount)))
2175 return gpg_error (GPG_ERR_NO_NAME); /* Error or no record found. */
2177 return gpg_error (GPG_ERR_SERVER_FAILED);
2180 pt = &answer[sizeof(HEADER)];
2181 rc = dn_skipname (pt, emsg);
2183 return gpg_error (GPG_ERR_SERVER_FAILED);
2185 pt += rc + QFIXEDSZ;
2187 return gpg_error (GPG_ERR_SERVER_FAILED);
2189 rc = dn_skipname (pt, emsg);
2191 return gpg_error (GPG_ERR_SERVER_FAILED);
2192 pt += rc + 2 + 2 + 4;
2194 return gpg_error (GPG_ERR_SERVER_FAILED);
2195 pt += 2; /* Skip rdlen */
2197 cname = xtrymalloc (cnamesize);
2199 return gpg_error_from_syserror ();
2201 rc = dn_expand (answer, emsg, pt, cname, cnamesize -1);
2205 return gpg_error (GPG_ERR_SERVER_FAILED);
2207 *r_cname = xtryrealloc (cname, strlen (cname)+1);
2210 err = gpg_error_from_syserror ();
2216 #else /*!HAVE_SYSTEM_RESOLVER*/
2220 return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
2222 #endif /*!HAVE_SYSTEM_RESOLVER*/
2227 get_dns_cname (const char *name, char **r_cname)
2234 if (!standard_resolver)
2236 err = get_dns_cname_libdns (name, r_cname);
2237 if (err && libdns_switch_port_p (err))
2238 err = get_dns_cname_libdns (name, r_cname);
2241 #endif /*USE_LIBDNS*/
2243 err = get_dns_cname_standard (name, r_cname);
2245 log_debug ("get_dns_cname(%s)%s%s\n", name,
2246 err ? ": " : " -> ",
2247 err ? gpg_strerror (err) : *r_cname);