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 /* Set verbosity and debug mode for this module. */
227 set_dns_verbose (int verbose, int debug)
229 opt_verbose = verbose;
234 /* Set the Disable-IPv4 flag so that the name resolver does not return
237 set_dns_disable_ipv4 (int yes)
239 opt_disable_ipv4 = !!yes;
243 /* Set the timeout for libdns requests to SECONDS. A value of 0 sets
244 * the default timeout and values are capped at 10 minutes. */
246 set_dns_timeout (int seconds)
249 seconds = DEFAULT_TIMEOUT;
250 else if (seconds < 1)
252 else if (seconds > 600)
255 opt_timeout = seconds;
259 /* Change the default IP address of the nameserver to IPADDR. The
260 address needs to be a numerical IP address and will be used for the
261 next DNS query. Note that this is only used in Tor mode. */
263 set_dns_nameserver (const char *ipaddr)
265 strncpy (tor_nameserver, ipaddr? ipaddr : DEFAULT_NAMESERVER,
266 sizeof tor_nameserver -1);
267 tor_nameserver[sizeof tor_nameserver -1] = 0;
269 libdns_reinit_pending = 1;
270 libdns_tor_port = 0; /* Start again with the default port. */
275 /* Free an addressinfo linked list as returned by resolve_dns_name. */
277 free_dns_addrinfo (dns_addrinfo_t ai)
281 dns_addrinfo_t next = ai->next;
288 #ifndef HAVE_W32_SYSTEM
289 /* Return H_ERRNO mapped to a gpg-error code. Will never return 0. */
291 get_h_errno_as_gpg_error (void)
297 case HOST_NOT_FOUND: ec = GPG_ERR_NO_NAME; break;
298 case TRY_AGAIN: ec = GPG_ERR_TRY_LATER; break;
299 case NO_RECOVERY: ec = GPG_ERR_SERVER_FAILED; break;
300 case NO_DATA: ec = GPG_ERR_NO_DATA; break;
301 default: ec = GPG_ERR_UNKNOWN_ERRNO; break;
303 return gpg_error (ec);
305 #endif /*!HAVE_W32_SYSTEM*/
308 map_eai_to_gpg_error (int ec)
314 case EAI_AGAIN: err = gpg_error (GPG_ERR_EAGAIN); break;
315 case EAI_BADFLAGS: err = gpg_error (GPG_ERR_INV_FLAG); break;
316 case EAI_FAIL: err = gpg_error (GPG_ERR_SERVER_FAILED); break;
317 case EAI_MEMORY: err = gpg_error (GPG_ERR_ENOMEM); break;
319 case EAI_NODATA: err = gpg_error (GPG_ERR_NO_DATA); break;
321 case EAI_NONAME: err = gpg_error (GPG_ERR_NO_NAME); break;
322 case EAI_SERVICE: err = gpg_error (GPG_ERR_NOT_SUPPORTED); break;
323 case EAI_FAMILY: err = gpg_error (GPG_ERR_EAFNOSUPPORT); break;
324 case EAI_SOCKTYPE: err = gpg_error (GPG_ERR_ESOCKTNOSUPPORT); break;
325 #ifndef HAVE_W32_SYSTEM
326 # ifdef EAI_ADDRFAMILY
327 case EAI_ADDRFAMILY:err = gpg_error (GPG_ERR_EADDRNOTAVAIL); break;
329 case EAI_SYSTEM: err = gpg_error_from_syserror (); break;
331 default: err = gpg_error (GPG_ERR_UNKNOWN_ERRNO); break;
339 libdns_error_to_gpg_error (int serr)
345 case 0: ec = 0; break;
347 case DNS_ENOBUFS: ec = GPG_ERR_BUFFER_TOO_SHORT; break;
348 case DNS_EILLEGAL: ec = GPG_ERR_INV_OBJ; break;
349 case DNS_EORDER: ec = GPG_ERR_INV_ORDER; break;
350 case DNS_ESECTION: ec = GPG_ERR_DNS_SECTION; break;
351 case DNS_EUNKNOWN: ec = GPG_ERR_DNS_UNKNOWN; break;
352 case DNS_EADDRESS: ec = GPG_ERR_DNS_ADDRESS; break;
353 case DNS_ENOQUERY: ec = GPG_ERR_DNS_NO_QUERY; break;
354 case DNS_ENOANSWER:ec = GPG_ERR_DNS_NO_ANSWER; break;
355 case DNS_EFETCHED: ec = GPG_ERR_ALREADY_FETCHED; break;
356 case DNS_ESERVICE: ec = GPG_ERR_NOT_SUPPORTED; break;
357 case DNS_ENONAME: ec = GPG_ERR_NO_NAME; break;
358 case DNS_EFAIL: ec = GPG_ERR_SERVER_FAILED; break;
359 case DNS_ECONNFIN: ec = GPG_ERR_DNS_CLOSED; break;
360 case DNS_EVERIFY: ec = GPG_ERR_DNS_VERIFY; break;
364 ec = gpg_err_code_from_errno (serr);
366 ec = GPG_ERR_DNS_UNKNOWN;
369 return gpg_error (ec);
371 #endif /*USE_LIBDNS*/
375 /* Initialize libdns. Returns 0 on success; prints a diagnostic and
376 * returns an error code on failure. */
385 if (libdns.resolv_conf)
386 return 0; /* Already initialized. */
388 memset (&ld, 0, sizeof ld);
390 ld.resolv_conf = dns_resconf_open (&derr);
393 err = libdns_error_to_gpg_error (derr);
394 log_error ("failed to allocate DNS resconf object: %s\n",
401 if (!*tor_nameserver)
402 set_dns_nameserver (NULL);
404 if (!libdns_tor_port)
405 libdns_tor_port = TOR_PORT;
407 cfgstr = xtryasprintf ("[%s]:53", tor_nameserver);
409 err = gpg_error_from_syserror ();
411 err = libdns_error_to_gpg_error
412 (dns_resconf_pton (&ld.resolv_conf->nameserver[0], cfgstr));
414 log_error ("failed to set nameserver '%s': %s\n",
415 cfgstr, gpg_strerror (err));
419 ld.resolv_conf->options.tcp = DNS_RESCONF_TCP_SOCKS;
422 cfgstr = xtryasprintf ("[%s]:%d", "127.0.0.1", libdns_tor_port);
424 err = gpg_error_from_syserror ();
426 err = libdns_error_to_gpg_error
427 (dns_resconf_pton (&ld.socks_host, cfgstr));
430 log_error ("failed to set socks server '%s': %s\n",
431 cfgstr, gpg_strerror (err));
437 #ifdef HAVE_W32_SYSTEM
444 ninfo = xtrymalloc (ninfo_len);
447 err = gpg_error_from_syserror ();
451 if (GetNetworkParams (ninfo, &ninfo_len))
453 log_error ("GetNetworkParms failed: %s\n", w32_strerror (-1));
454 err = gpg_error (GPG_ERR_GENERAL);
459 for (idx=0, pip = &(ninfo->DnsServerList);
460 pip && idx < DIM (ld.resolv_conf->nameserver);
464 log_debug ("dns: dnsserver[%d] '%s'\n", idx, pip->IpAddress.String);
465 err = libdns_error_to_gpg_error
466 (dns_resconf_pton (&ld.resolv_conf->nameserver[idx],
467 pip->IpAddress.String));
469 log_error ("failed to set nameserver[%d] '%s': %s\n",
470 idx, pip->IpAddress.String, gpg_strerror (err));
479 fname = "/etc/resolv.conf";
480 err = libdns_error_to_gpg_error
481 (dns_resconf_loadpath (ld.resolv_conf, fname));
484 log_error ("failed to load '%s': %s\n", fname, gpg_strerror (err));
488 fname = "/etc/nsswitch.conf";
489 err = libdns_error_to_gpg_error
490 (dns_nssconf_loadpath (ld.resolv_conf, fname));
493 log_error ("failed to load '%s': %s\n", fname, gpg_strerror (err));
494 /* not fatal, nsswitch.conf is not used on all systems; assume
495 * classic behavior instead. Our dns library states "bf" which tries
496 * DNS then Files, which is not classic; FreeBSD
497 * /usr/src/lib/libc/net/gethostnamadr.c defines default_src[] which
498 * is Files then DNS, which is. */
500 log_debug ("dns: fallback resolution order, files then DNS\n");
501 ld.resolv_conf->lookup[0] = 'f';
502 ld.resolv_conf->lookup[1] = 'b';
503 ld.resolv_conf->lookup[2] = '\0';
504 err = GPG_ERR_NO_ERROR;
510 ld.hosts = dns_hosts_open (&derr);
513 log_error ("failed to load hosts file: %s\n", gpg_strerror (err));
514 err = libdns_error_to_gpg_error (derr);
518 /* dns_hints_local for stub mode, dns_hints_root for recursive. */
519 ld.hints = (recursive_resolver
520 ? dns_hints_root (ld.resolv_conf, &derr)
521 : dns_hints_local (ld.resolv_conf, &derr));
524 log_error ("failed to load DNS hints: %s\n", gpg_strerror (err));
525 err = libdns_error_to_gpg_error (derr);
529 /* All fine. Make the data global. */
533 log_debug ("dns: libdns initialized%s\n", tor_mode?" (tor mode)":"");
539 #endif /*USE_LIBDNS*/
543 /* Deinitialize libdns. */
549 if (!libdns.resolv_conf)
550 return; /* Not initialized. */
553 memset (&libdns, 0, sizeof libdns);
554 dns_hints_close (ld.hints);
555 dns_hosts_close (ld.hosts);
556 dns_resconf_close (ld.resolv_conf);
558 #endif /*USE_LIBDNS*/
561 /* SIGHUP action handler for this module. With FORCE set objects are
562 * all immediately released. */
564 reload_dns_stuff (int force)
570 libdns_reinit_pending = 0;
574 libdns_reinit_pending = 1;
575 libdns_tor_port = 0; /* Start again with the default port. */
585 * Initialize libdns if needed and open a dns_resolver context.
586 * Returns 0 on success and stores the new context at R_RES. On
587 * failure an error code is returned and NULL stored at R_RES.
590 libdns_res_open (struct dns_resolver **r_res)
593 struct dns_resolver *res;
598 if (libdns_reinit_pending)
600 libdns_reinit_pending = 0;
604 err = libdns_init ();
611 res = dns_res_open (libdns.resolv_conf, libdns.hosts, libdns.hints, NULL,
612 dns_opts (.socks_host = &libdns.socks_host,
613 .socks_user = tor_socks_user,
614 .socks_password = tor_socks_password ),
617 return libdns_error_to_gpg_error (derr);
622 #endif /*USE_LIBDNS*/
626 /* Helper to test whether we need to try again after having switched
629 libdns_switch_port_p (gpg_error_t err)
631 if (tor_mode && gpg_err_code (err) == GPG_ERR_ECONNREFUSED
632 && libdns_tor_port == TOR_PORT)
634 /* Switch port and try again. */
636 log_debug ("dns: switching from SOCKS port %d to %d\n",
637 TOR_PORT, TOR_PORT2);
638 libdns_tor_port = TOR_PORT2;
639 libdns_reinit_pending = 1;
644 #endif /*USE_LIBDNS*/
648 /* Wrapper around dns_res_submit. */
650 libdns_res_submit (struct dns_resolver *res, const char *qname,
651 enum dns_type qtype, enum dns_class qclass)
653 return libdns_error_to_gpg_error (dns_res_submit (res, qname, qtype, qclass));
655 #endif /*USE_LIBDNS*/
659 /* Standard event handling loop. */
661 libdns_res_wait (struct dns_resolver *res)
665 while ((err = libdns_error_to_gpg_error (dns_res_check (res)))
666 && gpg_err_code (err) == GPG_ERR_EAGAIN)
668 if (dns_res_elapsed (res) > opt_timeout)
670 err = gpg_error (GPG_ERR_DNS_TIMEOUT);
675 dns_res_poll (res, 1);
681 #endif /*USE_LIBDNS*/
686 resolve_name_libdns (const char *name, unsigned short port,
687 int want_family, int want_socktype,
688 dns_addrinfo_t *r_dai, char **r_canonname)
691 dns_addrinfo_t daihead = NULL;
693 struct dns_resolver *res = NULL;
694 struct dns_addrinfo *ai = NULL;
695 struct addrinfo hints;
696 struct addrinfo *ent;
698 char *portstr = NULL;
705 memset (&hints, 0, sizeof hints);
706 hints.ai_family = want_family;
707 hints.ai_socktype = want_socktype;
708 hints.ai_flags = AI_ADDRCONFIG;
710 hints.ai_flags |= AI_CANONNAME;
711 if (is_ip_address (name))
712 hints.ai_flags |= AI_NUMERICHOST;
716 snprintf (portstr_, sizeof portstr_, "%hu", port);
720 err = libdns_res_open (&res);
724 ai = dns_ai_open (name, portstr, 0, &hints, res, &derr);
727 err = libdns_error_to_gpg_error (derr);
731 /* Loop over all records. */
734 err = libdns_error_to_gpg_error (dns_ai_nextent (&ent, ai));
735 if (gpg_err_code (err) == GPG_ERR_ENOENT)
738 err = 0; /* We got some results, we're good. */
741 if (gpg_err_code (err) == GPG_ERR_EAGAIN)
743 if (dns_ai_elapsed (ai) > opt_timeout)
745 err = gpg_error (GPG_ERR_DNS_TIMEOUT);
757 if (r_canonname && ! *r_canonname && ent && ent->ai_canonname)
759 *r_canonname = xtrystrdup (ent->ai_canonname);
762 err = gpg_error_from_syserror ();
765 /* Libdns appends the root zone part which is problematic
766 * for most other functions - strip it. */
767 if (**r_canonname && (*r_canonname)[strlen (*r_canonname)-1] == '.')
768 (*r_canonname)[strlen (*r_canonname)-1] = 0;
771 dai = xtrymalloc (sizeof *dai + ent->ai_addrlen -1);
774 err = gpg_error_from_syserror ();
778 dai->family = ent->ai_family;
779 dai->socktype = ent->ai_socktype;
780 dai->protocol = ent->ai_protocol;
781 dai->addrlen = ent->ai_addrlen;
782 memcpy (dai->addr, ent->ai_addr, ent->ai_addrlen);
797 xfree (*r_canonname);
800 free_dns_addrinfo (daihead);
807 #endif /*USE_LIBDNS*/
810 /* Resolve a name using the standard system function. */
812 resolve_name_standard (const char *name, unsigned short port,
813 int want_family, int want_socktype,
814 dns_addrinfo_t *r_dai, char **r_canonname)
817 dns_addrinfo_t daihead = NULL;
819 struct addrinfo *aibuf = NULL;
820 struct addrinfo hints, *ai;
828 memset (&hints, 0, sizeof hints);
829 hints.ai_family = want_family;
830 hints.ai_socktype = want_socktype;
831 hints.ai_flags = AI_ADDRCONFIG;
833 hints.ai_flags |= AI_CANONNAME;
834 if (is_ip_address (name))
835 hints.ai_flags |= AI_NUMERICHOST;
838 snprintf (portstr, sizeof portstr, "%hu", port);
842 /* We can't use the the AI_IDN flag because that does the conversion
843 using the current locale. However, GnuPG always used UTF-8. To
844 support IDN we would need to make use of the libidn API. */
845 ret = getaddrinfo (name, *portstr? portstr : NULL, &hints, &aibuf);
849 err = map_eai_to_gpg_error (ret);
850 if (gpg_err_code (err) == GPG_ERR_NO_NAME)
852 /* There seems to be a bug in the glibc getaddrinfo function
853 if the CNAME points to a long list of A and AAAA records
854 in which case the function return NO_NAME. Let's do the
855 CNAME redirection again. */
858 if (get_dns_cname (name, &cname))
859 goto leave; /* Still no success. */
861 ret = getaddrinfo (cname, *portstr? portstr : NULL, &hints, &aibuf);
866 err = map_eai_to_gpg_error (ret);
869 err = 0; /* Yep, now it worked. */
875 if (r_canonname && aibuf && aibuf->ai_canonname)
877 *r_canonname = xtrystrdup (aibuf->ai_canonname);
880 err = gpg_error_from_syserror ();
885 for (ai = aibuf; ai; ai = ai->ai_next)
887 if (ai->ai_family != AF_INET6 && ai->ai_family != AF_INET)
889 if (opt_disable_ipv4 && ai->ai_family == AF_INET)
892 dai = xtrymalloc (sizeof *dai + ai->ai_addrlen - 1);
893 dai->family = ai->ai_family;
894 dai->socktype = ai->ai_socktype;
895 dai->protocol = ai->ai_protocol;
896 dai->addrlen = ai->ai_addrlen;
897 memcpy (dai->addr, ai->ai_addr, ai->ai_addrlen);
904 freeaddrinfo (aibuf);
909 xfree (*r_canonname);
912 free_dns_addrinfo (daihead);
920 /* This a wrapper around getaddrinfo with slightly different semantics.
921 NAME is the name to resolve.
922 PORT is the requested port or 0.
923 WANT_FAMILY is either 0 (AF_UNSPEC), AF_INET6, or AF_INET4.
924 WANT_SOCKETTYPE is either SOCK_STREAM or SOCK_DGRAM.
926 On success the result is stored in a linked list with the head
927 stored at the address R_AI; the caller must call gpg_addrinfo_free
928 on this. If R_CANONNAME is not NULL the official name of the host
929 is stored there as a malloced string; if that name is not available
932 resolve_dns_name (const char *name, unsigned short port,
933 int want_family, int want_socktype,
934 dns_addrinfo_t *r_ai, char **r_canonname)
939 if (!standard_resolver)
941 err = resolve_name_libdns (name, port, want_family, want_socktype,
943 if (err && libdns_switch_port_p (err))
944 err = resolve_name_libdns (name, port, want_family, want_socktype,
948 #endif /*USE_LIBDNS*/
949 err = resolve_name_standard (name, port, want_family, want_socktype,
952 log_debug ("dns: resolve_dns_name(%s): %s\n", name, gpg_strerror (err));
958 /* Resolve an address using libdns. */
960 resolve_addr_libdns (const struct sockaddr *addr, int addrlen,
961 unsigned int flags, char **r_name)
964 char host[DNS_D_MAXNAME + 1];
965 struct dns_resolver *res = NULL;
966 struct dns_packet *ans = NULL;
972 /* First we turn ADDR into a DNS name (with ".arpa" suffix). */
974 if (addr->sa_family == AF_INET6)
976 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)addr;
977 if (!dns_aaaa_arpa (host, sizeof host, (void*)&a6->sin6_addr))
978 err = gpg_error (GPG_ERR_INV_OBJ);
980 else if (addr->sa_family == AF_INET)
982 const struct sockaddr_in *a4 = (const struct sockaddr_in *)addr;
983 if (!dns_a_arpa (host, sizeof host, (void*)&a4->sin_addr))
984 err = gpg_error (GPG_ERR_INV_OBJ);
987 err = gpg_error (GPG_ERR_EAFNOSUPPORT);
992 err = libdns_res_open (&res);
996 err = libdns_res_submit (res, host, DNS_T_PTR, DNS_C_IN);
1000 err = libdns_res_wait (res);
1004 ans = dns_res_fetch (res, &derr);
1007 err = libdns_error_to_gpg_error (derr);
1011 /* Check the rcode. */
1012 switch (dns_p_rcode (ans))
1014 case DNS_RC_NOERROR:
1016 case DNS_RC_NXDOMAIN:
1017 err = gpg_error (GPG_ERR_NO_NAME);
1020 err = GPG_ERR_SERVER_FAILED;
1024 /* Parse the result. */
1028 struct dns_rr_i rri;
1030 memset (&rri, 0, sizeof rri);
1031 dns_rr_i_init (&rri, ans);
1032 rri.section = DNS_S_ALL & ~DNS_S_QD;
1034 rri.type = DNS_T_PTR;
1036 if (!dns_rr_grep (&rr, 1, &rri, ans, &derr))
1038 err = gpg_error (GPG_ERR_NOT_FOUND);
1042 err = libdns_error_to_gpg_error (dns_ptr_parse (&ptr, &rr, ans));
1047 *r_name = xtrystrdup (ptr.host);
1050 err = gpg_error_from_syserror ();
1053 /* Libdns appends the root zone part which is problematic
1054 * for most other functions - strip it. */
1055 if (**r_name && (*r_name)[strlen (*r_name)-1] == '.')
1056 (*r_name)[strlen (*r_name)-1] = 0;
1058 else /* GPG_ERR_NO_NAME */
1065 buflen = sizeof ptr.host;
1068 if (addr->sa_family == AF_INET6 && (flags & DNS_WITHBRACKET))
1073 ec = getnameinfo (addr, addrlen, p, buflen, NULL, 0, NI_NUMERICHOST);
1076 err = map_eai_to_gpg_error (ec);
1079 if (addr->sa_family == AF_INET6 && (flags & DNS_WITHBRACKET))
1080 strcat (buffer, "]");
1085 dns_res_close (res);
1088 #endif /*USE_LIBDNS*/
1091 /* Resolve an address using the standard system function. */
1093 resolve_addr_standard (const struct sockaddr *addr, int addrlen,
1094 unsigned int flags, char **r_name)
1103 buflen = NI_MAXHOST;
1104 buffer = xtrymalloc (buflen + 2 + 1);
1106 return gpg_error_from_syserror ();
1108 if ((flags & DNS_NUMERICHOST) || tor_mode)
1111 ec = getnameinfo (addr, addrlen, buffer, buflen, NULL, 0, NI_NAMEREQD);
1113 if (!ec && *buffer == '[')
1114 ec = EAI_FAIL; /* A name may never start with a bracket. */
1115 else if (ec == EAI_NONAME)
1118 if (addr->sa_family == AF_INET6 && (flags & DNS_WITHBRACKET))
1123 ec = getnameinfo (addr, addrlen, p, buflen, NULL, 0, NI_NUMERICHOST);
1124 if (!ec && addr->sa_family == AF_INET6 && (flags & DNS_WITHBRACKET))
1125 strcat (buffer, "]");
1129 err = map_eai_to_gpg_error (ec);
1132 p = xtryrealloc (buffer, strlen (buffer)+1);
1134 err = gpg_error_from_syserror ();
1151 /* A wrapper around getnameinfo. */
1153 resolve_dns_addr (const struct sockaddr *addr, int addrlen,
1154 unsigned int flags, char **r_name)
1159 /* Note that we divert to the standard resolver for NUMERICHOST. */
1160 if (!standard_resolver && !(flags & DNS_NUMERICHOST))
1162 err = resolve_addr_libdns (addr, addrlen, flags, r_name);
1163 if (err && libdns_switch_port_p (err))
1164 err = resolve_addr_libdns (addr, addrlen, flags, r_name);
1167 #endif /*USE_LIBDNS*/
1168 err = resolve_addr_standard (addr, addrlen, flags, r_name);
1171 log_debug ("dns: resolve_dns_addr(): %s\n", gpg_strerror (err));
1176 /* Check whether NAME is an IP address. Returns a true if it is
1177 * either an IPv6 or a IPv4 numerical address. The actual return
1178 * values can also be used to identify whether it is v4 or v6: The
1179 * true value will surprisingly be 4 for IPv4 and 6 for IPv6. */
1181 is_ip_address (const char *name)
1184 int ndots, dblcol, n;
1187 return 6; /* yes: A legal DNS name may not contain this character;
1188 this mut be bracketed v6 address. */
1190 return 0; /* No. A leading dot is not a valid IP address. */
1192 /* Check whether this is a v6 address. */
1193 ndots = n = dblcol = 0;
1194 for (s=name; *s; s++)
1203 return 0; /* No: Only one "::" allowed. */
1212 else if (!strchr ("0123456789abcdefABCDEF", *s))
1213 return 0; /* No: Not a hex digit. */
1215 return 0; /* To many digits in a group. */
1218 return 0; /* No: Too many colons. */
1220 return 6; /* Yes: At least 2 colons indicate an v6 address. */
1223 /* Check whether it is legacy IP address. */
1225 for (s=name; *s; s++)
1230 return 0; /* No: Douple dot. */
1231 if (atoi (s+1) > 255)
1232 return 0; /* No: Ipv4 byte value too large. */
1236 else if (!strchr ("0123456789", *s))
1237 return 0; /* No: Not a digit. */
1239 return 0; /* No: More than 3 digits. */
1241 return (ndots == 3)? 4 : 0;
1245 /* Return true if NAME is an onion address. */
1247 is_onion_address (const char *name)
1251 len = name? strlen (name) : 0;
1252 if (len < 8 || strcmp (name + len - 6, ".onion"))
1254 /* Note that we require at least 2 characters before the suffix. */
1255 return 1; /* Yes. */
1259 /* libdns version of get_dns_cert. */
1262 get_dns_cert_libdns (const char *name, int want_certtype,
1263 void **r_key, size_t *r_keylen,
1264 unsigned char **r_fpr, size_t *r_fprlen, char **r_url)
1267 struct dns_resolver *res = NULL;
1268 struct dns_packet *ans = NULL;
1270 struct dns_rr_i rri;
1271 char host[DNS_D_MAXNAME + 1];
1275 /* Get the query type from WANT_CERTTYPE (which in general indicates
1276 * the subtype we want). */
1277 qtype = (want_certtype < DNS_CERTTYPE_RRBASE
1279 : (want_certtype - DNS_CERTTYPE_RRBASE));
1282 err = libdns_res_open (&res);
1286 if (dns_d_anchor (host, sizeof host, name, strlen (name)) >= sizeof host)
1288 err = gpg_error (GPG_ERR_ENAMETOOLONG);
1292 err = libdns_res_submit (res, name, qtype, DNS_C_IN);
1296 err = libdns_res_wait (res);
1300 ans = dns_res_fetch (res, &derr);
1303 err = libdns_error_to_gpg_error (derr);
1307 /* Check the rcode. */
1308 switch (dns_p_rcode (ans))
1310 case DNS_RC_NOERROR: break;
1311 case DNS_RC_NXDOMAIN: err = gpg_error (GPG_ERR_NO_NAME); break;
1312 default: err = GPG_ERR_SERVER_FAILED; break;
1317 memset (&rri, 0, sizeof rri);
1318 dns_rr_i_init (&rri, ans);
1319 rri.section = DNS_S_ALL & ~DNS_S_QD;
1323 err = gpg_error (GPG_ERR_NOT_FOUND);
1324 while (dns_rr_grep (&rr, 1, &rri, ans, &derr))
1326 unsigned char *rp = ans->data + rr.rd.p;
1327 unsigned short len = rr.rd.len;
1332 /* Definitely too short - skip. */
1334 else if (want_certtype >= DNS_CERTTYPE_RRBASE
1335 && rr.type == (want_certtype - DNS_CERTTYPE_RRBASE)
1338 *r_key = xtrymalloc (len);
1340 err = gpg_error_from_syserror ();
1343 memcpy (*r_key, rp, len);
1349 else if (want_certtype >= DNS_CERTTYPE_RRBASE)
1351 /* We did not found the requested RR - skip. */
1353 else if (rr.type == T_CERT && len > 5)
1355 /* We got a CERT type. */
1356 subtype = buf16_to_u16 (rp);
1359 /* Skip the CERT key tag and algo which we don't need. */
1362 if (want_certtype && want_certtype != subtype)
1363 ; /* Not the requested subtype - skip. */
1364 else if (subtype == DNS_CERTTYPE_PGP && len && r_key && r_keylen)
1367 *r_key = xtrymalloc (len);
1369 err = gpg_error_from_syserror ();
1372 memcpy (*r_key, rp, len);
1378 else if (subtype == DNS_CERTTYPE_IPGP
1379 && len && len < 1023 && len >= rp[0] + 1)
1385 *r_fpr = xtrymalloc (*r_fprlen);
1388 err = gpg_error_from_syserror ();
1391 memcpy (*r_fpr, rp+1, *r_fprlen);
1396 if (len > *r_fprlen + 1)
1398 *r_url = xtrymalloc (len - (*r_fprlen + 1) + 1);
1401 err = gpg_error_from_syserror ();
1406 memcpy (*r_url, rp + *r_fprlen + 1, len - (*r_fprlen + 1));
1407 (*r_url)[len - (*r_fprlen + 1)] = 0;
1417 /* Unknown subtype or record too short - skip. */
1422 /* Not a requested type - skip. */
1428 dns_res_close (res);
1431 #endif /*USE_LIBDNS*/
1434 /* Standard resolver version of get_dns_cert. */
1436 get_dns_cert_standard (const char *name, int want_certtype,
1437 void **r_key, size_t *r_keylen,
1438 unsigned char **r_fpr, size_t *r_fprlen, char **r_url)
1440 #ifdef HAVE_SYSTEM_RESOLVER
1442 unsigned char *answer;
1446 /* Allocate a 64k buffer which is the limit for an DNS response. */
1447 answer = xtrymalloc (65536);
1449 return gpg_error_from_syserror ();
1451 err = gpg_error (GPG_ERR_NOT_FOUND);
1452 r = res_query (name, C_IN,
1453 (want_certtype < DNS_CERTTYPE_RRBASE
1455 : (want_certtype - DNS_CERTTYPE_RRBASE)),
1457 /* Not too big, not too small, no errors and at least 1 answer. */
1458 if (r >= sizeof (HEADER) && r <= 65536
1459 && (((HEADER *)(void *) answer)->rcode) == NOERROR
1460 && (count = ntohs (((HEADER *)(void *) answer)->ancount)))
1463 unsigned char *pt, *emsg;
1467 pt = &answer[sizeof (HEADER)];
1469 /* Skip over the query */
1471 rc = dn_skipname (pt, emsg);
1474 err = gpg_error (GPG_ERR_INV_OBJ);
1477 pt += rc + QFIXEDSZ;
1479 /* There are several possible response types for a CERT request.
1480 We're interested in the PGP (a key) and IPGP (a URI) types.
1481 Skip all others. TODO: A key is better than a URI since
1482 we've gone through all this bother to fetch it, so favor that
1483 if we have both PGP and IPGP? */
1485 while (count-- > 0 && pt < emsg)
1487 u16 type, class, dlen, ctype;
1489 rc = dn_skipname (pt, emsg); /* the name we just queried for */
1492 err = gpg_error (GPG_ERR_INV_OBJ);
1498 /* Truncated message? 15 bytes takes us to the point where
1499 we start looking at the ctype. */
1500 if ((emsg - pt) < 15)
1503 type = buf16_to_u16 (pt);
1506 class = buf16_to_u16 (pt);
1516 dlen = buf16_to_u16 (pt);
1519 /* Check the type and parse. */
1520 if (want_certtype >= DNS_CERTTYPE_RRBASE
1521 && type == (want_certtype - DNS_CERTTYPE_RRBASE)
1524 *r_key = xtrymalloc (dlen);
1526 err = gpg_error_from_syserror ();
1529 memcpy (*r_key, pt, dlen);
1535 else if (want_certtype >= DNS_CERTTYPE_RRBASE)
1537 /* We did not found the requested RR. */
1540 else if (type == T_CERT)
1542 /* We got a CERT type. */
1543 ctype = buf16_to_u16 (pt);
1546 /* Skip the CERT key tag and algo which we don't need. */
1551 /* 15 bytes takes us to here */
1552 if (want_certtype && want_certtype != ctype)
1553 ; /* Not of the requested certtype. */
1554 else if (ctype == DNS_CERTTYPE_PGP && dlen && r_key && r_keylen)
1557 *r_key = xtrymalloc (dlen);
1559 err = gpg_error_from_syserror ();
1562 memcpy (*r_key, pt, dlen);
1568 else if (ctype == DNS_CERTTYPE_IPGP
1569 && dlen && dlen < 1023 && dlen >= pt[0] + 1)
1575 *r_fpr = xtrymalloc (*r_fprlen);
1578 err = gpg_error_from_syserror ();
1581 memcpy (*r_fpr, &pt[1], *r_fprlen);
1586 if (dlen > *r_fprlen + 1)
1588 *r_url = xtrymalloc (dlen - (*r_fprlen + 1) + 1);
1591 err = gpg_error_from_syserror ();
1596 memcpy (*r_url, &pt[*r_fprlen + 1],
1597 dlen - (*r_fprlen + 1));
1598 (*r_url)[dlen - (*r_fprlen + 1)] = '\0';
1607 /* No subtype matches, so continue with the next answer. */
1612 /* Not a requested type - might be a CNAME. Try next item. */
1622 #else /*!HAVE_SYSTEM_RESOLVER*/
1625 (void)want_certtype;
1631 return gpg_error (GPG_ERR_NOT_SUPPORTED);
1633 #endif /*!HAVE_SYSTEM_RESOLVER*/
1637 /* Returns 0 on success or an error code. If a PGP CERT record was
1638 found, the malloced data is returned at (R_KEY, R_KEYLEN) and
1639 the other return parameters are set to NULL/0. If an IPGP CERT
1640 record was found the fingerprint is stored as an allocated block at
1641 R_FPR and its length at R_FPRLEN; an URL is is allocated as a
1642 string and returned at R_URL. If WANT_CERTTYPE is 0 this function
1643 returns the first CERT found with a supported type; it is expected
1644 that only one CERT record is used. If WANT_CERTTYPE is one of the
1645 supported certtypes only records with this certtype are considered
1646 and the first found is returned. (R_KEY,R_KEYLEN) are optional. */
1648 get_dns_cert (const char *name, int want_certtype,
1649 void **r_key, size_t *r_keylen,
1650 unsigned char **r_fpr, size_t *r_fprlen, char **r_url)
1663 if (!standard_resolver)
1665 err = get_dns_cert_libdns (name, want_certtype, r_key, r_keylen,
1666 r_fpr, r_fprlen, r_url);
1667 if (err && libdns_switch_port_p (err))
1668 err = get_dns_cert_libdns (name, want_certtype, r_key, r_keylen,
1669 r_fpr, r_fprlen, r_url);
1672 #endif /*USE_LIBDNS*/
1673 err = get_dns_cert_standard (name, want_certtype, r_key, r_keylen,
1674 r_fpr, r_fprlen, r_url);
1677 log_debug ("dns: get_dns_cert(%s): %s\n", name, gpg_strerror (err));
1683 priosort(const void *a,const void *b)
1685 const struct srventry *sa=a,*sb=b;
1686 if(sa->priority>sb->priority)
1688 else if(sa->priority<sb->priority)
1695 /* Libdns based helper for getsrv. Note that it is expected that NULL
1696 * is stored at the address of LIST and 0 is stored at the address of
1700 getsrv_libdns (const char *name, struct srventry **list, unsigned int *r_count)
1703 struct dns_resolver *res = NULL;
1704 struct dns_packet *ans = NULL;
1706 struct dns_rr_i rri;
1707 char host[DNS_D_MAXNAME + 1];
1709 unsigned int srvcount = 0;
1711 err = libdns_res_open (&res);
1715 if (dns_d_anchor (host, sizeof host, name, strlen (name)) >= sizeof host)
1717 err = gpg_error (GPG_ERR_ENAMETOOLONG);
1721 err = libdns_res_submit (res, name, DNS_T_SRV, DNS_C_IN);
1725 err = libdns_res_wait (res);
1729 ans = dns_res_fetch (res, &derr);
1732 err = libdns_error_to_gpg_error (derr);
1736 /* Check the rcode. */
1737 switch (dns_p_rcode (ans))
1739 case DNS_RC_NOERROR: break;
1740 case DNS_RC_NXDOMAIN: err = gpg_error (GPG_ERR_NO_NAME); break;
1741 default: err = GPG_ERR_SERVER_FAILED; break;
1746 memset (&rri, 0, sizeof rri);
1747 dns_rr_i_init (&rri, ans);
1748 rri.section = DNS_S_ALL & ~DNS_S_QD;
1750 rri.type = DNS_T_SRV;
1752 while (dns_rr_grep (&rr, 1, &rri, ans, &derr))
1754 struct dns_srv dsrv;
1755 struct srventry *srv;
1756 struct srventry *newlist;
1758 err = libdns_error_to_gpg_error (dns_srv_parse(&dsrv, &rr, ans));
1762 newlist = xtryrealloc (*list, (srvcount+1)*sizeof(struct srventry));
1765 err = gpg_error_from_syserror ();
1769 memset (&(*list)[srvcount], 0, sizeof(struct srventry));
1770 srv = &(*list)[srvcount];
1772 srv->priority = dsrv.priority;
1773 srv->weight = dsrv.weight;
1774 srv->port = dsrv.port;
1775 mem2str (srv->target, dsrv.target, sizeof srv->target);
1776 /* Libdns appends the root zone part which is problematic for
1777 * most other functions - strip it. */
1778 if (*srv->target && (srv->target)[strlen (srv->target)-1] == '.')
1779 (srv->target)[strlen (srv->target)-1] = 0;
1782 *r_count = srvcount;
1791 dns_res_close (res);
1794 #endif /*USE_LIBDNS*/
1797 /* Standard resolver based helper for getsrv. Note that it is
1798 * expected that NULL is stored at the address of LIST and 0 is stored
1799 * at the address of R_COUNT. */
1801 getsrv_standard (const char *name,
1802 struct srventry **list, unsigned int *r_count)
1804 #ifdef HAVE_SYSTEM_RESOLVER
1806 unsigned char ans[2048];
1809 unsigned char *answer = res.ans;
1810 HEADER *header = res.header;
1811 unsigned char *pt, *emsg;
1814 unsigned int srvcount = 0;
1817 /* Do not allow a query using the standard resolver in Tor mode. */
1819 return gpg_error (GPG_ERR_NOT_ENABLED);
1822 r = res_query (name, C_IN, T_SRV, answer, sizeof res.ans);
1825 return get_h_errno_as_gpg_error ();
1826 if (r < sizeof (HEADER))
1827 return gpg_error (GPG_ERR_SERVER_FAILED);
1828 if (r > sizeof res.ans)
1829 return gpg_error (GPG_ERR_SYSTEM_BUG);
1830 if (header->rcode != NOERROR || !(count=ntohs (header->ancount)))
1831 return gpg_error (GPG_ERR_NO_NAME); /* Error or no record found. */
1834 pt = &answer[sizeof(HEADER)];
1836 /* Skip over the query */
1837 rc = dn_skipname (pt, emsg);
1841 pt += rc + QFIXEDSZ;
1843 while (count-- > 0 && pt < emsg)
1845 struct srventry *srv;
1847 struct srventry *newlist;
1849 newlist = xtryrealloc (*list, (srvcount+1)*sizeof(struct srventry));
1853 memset (&(*list)[srvcount], 0, sizeof(struct srventry));
1854 srv = &(*list)[srvcount];
1857 rc = dn_skipname (pt, emsg); /* The name we just queried for. */
1862 /* Truncated message? */
1866 type = buf16_to_u16 (pt);
1868 /* We asked for SRV and got something else !? */
1872 class = buf16_to_u16 (pt);
1874 /* We asked for IN and got something else !? */
1879 dlen = buf16_to_u16 (pt);
1882 srv->priority = buf16_to_ushort (pt);
1884 srv->weight = buf16_to_ushort (pt);
1886 srv->port = buf16_to_ushort (pt);
1889 /* Get the name. 2782 doesn't allow name compression, but
1890 * dn_expand still works to pull the name out of the packet. */
1891 rc = dn_expand (answer, emsg, pt, srv->target, sizeof srv->target);
1892 if (rc == 1 && srv->target[0] == 0) /* "." */
1901 /* Corrupt packet? */
1906 *r_count = srvcount;
1912 return gpg_error (GPG_ERR_GENERAL);
1914 #else /*!HAVE_SYSTEM_RESOLVER*/
1919 return gpg_error (GPG_ERR_NOT_SUPPORTED);
1921 #endif /*!HAVE_SYSTEM_RESOLVER*/
1925 /* Query a SRV record for SERVICE and PROTO for NAME. If SERVICE is
1926 * NULL, NAME is expected to contain the full query name. Note that
1927 * we do not return NONAME but simply store 0 at R_COUNT. On error an
1928 * error code is returned and 0 stored at R_COUNT. */
1930 get_dns_srv (const char *name, const char *service, const char *proto,
1931 struct srventry **list, unsigned int *r_count)
1934 char *namebuffer = NULL;
1935 unsigned int srvcount;
1942 /* If SERVICE is given construct the query from it and PROTO. */
1945 namebuffer = xtryasprintf ("_%s._%s.%s",
1946 service, proto? proto:"tcp", name);
1949 err = gpg_error_from_syserror ();
1957 if (!standard_resolver)
1959 err = getsrv_libdns (name, list, &srvcount);
1960 if (err && libdns_switch_port_p (err))
1961 err = getsrv_libdns (name, list, &srvcount);
1964 #endif /*USE_LIBDNS*/
1965 err = getsrv_standard (name, list, &srvcount);
1969 if (gpg_err_code (err) == GPG_ERR_NO_NAME)
1974 /* Now we have an array of all the srv records. */
1976 /* Order by priority */
1977 qsort(*list,srvcount,sizeof(struct srventry),priosort);
1979 /* For each priority, move the zero-weighted items first. */
1980 for (i=0; i < srvcount; i++)
1984 for (j=i;j < srvcount && (*list)[i].priority == (*list)[j].priority; j++)
1986 if((*list)[j].weight==0)
1991 struct srventry temp;
1993 memcpy (&temp,&(*list)[j],sizeof(struct srventry));
1994 memcpy (&(*list)[j],&(*list)[i],sizeof(struct srventry));
1995 memcpy (&(*list)[i],&temp,sizeof(struct srventry));
2003 /* Run the RFC-2782 weighting algorithm. We don't need very high
2004 quality randomness for this, so regular libc srand/rand is
2012 srand (time (NULL)*getpid());
2016 for (i=0; i < srvcount; i++)
2019 float prio_count=0,chose;
2021 for (j=i; j < srvcount && (*list)[i].priority == (*list)[j].priority; j++)
2023 prio_count+=(*list)[j].weight;
2024 (*list)[j].run_count=prio_count;
2027 chose=prio_count*rand()/RAND_MAX;
2029 for (j=i;j<srvcount && (*list)[i].priority==(*list)[j].priority;j++)
2031 if (chose<=(*list)[j].run_count)
2036 struct srventry temp;
2038 memcpy(&temp,&(*list)[j],sizeof(struct srventry));
2039 memcpy(&(*list)[j],&(*list)[i],sizeof(struct srventry));
2040 memcpy(&(*list)[i],&temp,sizeof(struct srventry));
2051 log_debug ("dns: getsrv(%s): %s\n", name, gpg_strerror (err));
2053 log_debug ("dns: getsrv(%s) -> %u records\n", name, srvcount);
2056 *r_count = srvcount;
2064 /* libdns version of get_dns_cname. */
2066 get_dns_cname_libdns (const char *name, char **r_cname)
2069 struct dns_resolver *res;
2070 struct dns_packet *ans = NULL;
2071 struct dns_cname cname;
2074 err = libdns_res_open (&res);
2078 err = libdns_res_submit (res, name, DNS_T_CNAME, DNS_C_IN);
2082 err = libdns_res_wait (res);
2086 ans = dns_res_fetch (res, &derr);
2089 err = libdns_error_to_gpg_error (derr);
2093 /* Check the rcode. */
2094 switch (dns_p_rcode (ans))
2096 case DNS_RC_NOERROR: break;
2097 case DNS_RC_NXDOMAIN: err = gpg_error (GPG_ERR_NO_NAME); break;
2098 default: err = GPG_ERR_SERVER_FAILED; break;
2103 /* Parse the result into CNAME. */
2104 err = libdns_error_to_gpg_error (dns_p_study (ans));
2108 if (!dns_d_cname (&cname, sizeof cname, name, strlen (name), ans, &derr))
2110 err = libdns_error_to_gpg_error (derr);
2115 *r_cname = xtrystrdup (cname.host);
2117 err = gpg_error_from_syserror ();
2120 /* Libdns appends the root zone part which is problematic
2121 * for most other functions - strip it. */
2122 if (**r_cname && (*r_cname)[strlen (*r_cname)-1] == '.')
2123 (*r_cname)[strlen (*r_cname)-1] = 0;
2128 dns_res_close (res);
2131 #endif /*USE_LIBDNS*/
2134 /* Standard resolver version of get_dns_cname. */
2136 get_dns_cname_standard (const char *name, char **r_cname)
2138 #ifdef HAVE_SYSTEM_RESOLVER
2142 unsigned char ans[2048];
2145 unsigned char *answer = res.ans;
2146 HEADER *header = res.header;
2147 unsigned char *pt, *emsg;
2150 int cnamesize = 1025;
2153 /* Do not allow a query using the standard resolver in Tor mode. */
2158 r = res_query (name, C_IN, T_CERT, answer, sizeof res.ans);
2161 return get_h_errno_as_gpg_error ();
2162 if (r < sizeof (HEADER))
2163 return gpg_error (GPG_ERR_SERVER_FAILED);
2164 if (r > sizeof res.ans)
2165 return gpg_error (GPG_ERR_SYSTEM_BUG);
2166 if (header->rcode != NOERROR || !(count=ntohs (header->ancount)))
2167 return gpg_error (GPG_ERR_NO_NAME); /* Error or no record found. */
2169 return gpg_error (GPG_ERR_SERVER_FAILED);
2172 pt = &answer[sizeof(HEADER)];
2173 rc = dn_skipname (pt, emsg);
2175 return gpg_error (GPG_ERR_SERVER_FAILED);
2177 pt += rc + QFIXEDSZ;
2179 return gpg_error (GPG_ERR_SERVER_FAILED);
2181 rc = dn_skipname (pt, emsg);
2183 return gpg_error (GPG_ERR_SERVER_FAILED);
2184 pt += rc + 2 + 2 + 4;
2186 return gpg_error (GPG_ERR_SERVER_FAILED);
2187 pt += 2; /* Skip rdlen */
2189 cname = xtrymalloc (cnamesize);
2191 return gpg_error_from_syserror ();
2193 rc = dn_expand (answer, emsg, pt, cname, cnamesize -1);
2197 return gpg_error (GPG_ERR_SERVER_FAILED);
2199 *r_cname = xtryrealloc (cname, strlen (cname)+1);
2202 err = gpg_error_from_syserror ();
2208 #else /*!HAVE_SYSTEM_RESOLVER*/
2212 return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
2214 #endif /*!HAVE_SYSTEM_RESOLVER*/
2219 get_dns_cname (const char *name, char **r_cname)
2226 if (!standard_resolver)
2228 err = get_dns_cname_libdns (name, r_cname);
2229 if (err && libdns_switch_port_p (err))
2230 err = get_dns_cname_libdns (name, r_cname);
2233 #endif /*USE_LIBDNS*/
2235 err = get_dns_cname_standard (name, r_cname);
2237 log_debug ("get_dns_cname(%s)%s%s\n", name,
2238 err ? ": " : " -> ",
2239 err ? gpg_strerror (err) : *r_cname);