diff --git a/common/Makefile.am b/common/Makefile.am index 2be51f18a..6cbde0bc0 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -78,7 +78,6 @@ common_sources = \ exechelp.h \ signal.c \ audit.c audit.h \ - srv.h \ localename.c \ session-env.c session-env.h \ userids.c userids.h \ @@ -110,15 +109,9 @@ without_npth_sources = \ libcommon_a_SOURCES = $(common_sources) $(without_npth_sources) -if USE_DNS_SRV -libcommon_a_SOURCES += srv.c -endif libcommon_a_CFLAGS = $(AM_CFLAGS) $(LIBASSUAN_CFLAGS) -DWITHOUT_NPTH=1 libcommonpth_a_SOURCES = $(common_sources) -if USE_DNS_SRV -libcommonpth_a_SOURCES += srv.c -endif libcommonpth_a_CFLAGS = $(AM_CFLAGS) $(LIBASSUAN_CFLAGS) $(NPTH_CFLAGS) if !HAVE_W32CE_SYSTEM diff --git a/common/srv.c b/common/srv.c deleted file mode 100644 index 2107aa528..000000000 --- a/common/srv.c +++ /dev/null @@ -1,333 +0,0 @@ -/* srv.c - DNS SRV code - * Copyright (C) 2003, 2009 Free Software Foundation, Inc. - * - * This file is part of GnuPG. - * - * This file is free software; you can redistribute it and/or modify - * it under the terms of either - * - * - the GNU Lesser General Public License as published by the Free - * Software Foundation; either version 3 of the License, or (at - * your option) any later version. - * - * or - * - * - 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. - * - * or both in parallel, as here. - * - * This file 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. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, see . - */ - -#include -#include -#ifdef _WIN32 -# ifdef HAVE_WINSOCK2_H -# include -# endif -# include -#else -#include -#include -#include -#endif -#include -#include -#include -#include -#ifdef USE_ADNS -# include -#endif - -#include "util.h" -#include "host2net.h" -#include "srv.h" - -/* Not every installation has gotten around to supporting SRVs - yet... */ -#ifndef T_SRV -#define T_SRV 33 -#endif - -static int -priosort(const void *a,const void *b) -{ - const struct srventry *sa=a,*sb=b; - if(sa->priority>sb->priority) - return 1; - else if(sa->prioritypriority) - return -1; - else - return 0; -} - - -int -getsrv (const char *name,struct srventry **list) -{ - int srvcount=0; - u16 count; - int i, rc; - - *list = NULL; - -#ifdef USE_ADNS - { - adns_state state; - adns_answer *answer = NULL; - - rc = adns_init (&state, adns_if_noerrprint, NULL); - if (rc) - { - log_error ("error initializing adns: %s\n", strerror (errno)); - return -1; - } - - rc = adns_synchronous (state, name, adns_r_srv, adns_qf_quoteok_query, - &answer); - if (rc) - { - log_error ("DNS query failed: %s\n", strerror (errno)); - adns_finish (state); - return -1; - } - if (answer->status != adns_s_ok - || answer->type != adns_r_srv || !answer->nrrs) - { - log_error ("DNS query returned an error or no records: %s (%s)\n", - adns_strerror (answer->status), - adns_errabbrev (answer->status)); - adns_free (answer); - adns_finish (state); - return 0; - } - - for (count = 0; count < answer->nrrs; count++) - { - struct srventry *srv = NULL; - struct srventry *newlist; - - if (strlen (answer->rrs.srvha[count].ha.host) >= MAXDNAME) - { - log_info ("hostname in SRV record too long - skipped\n"); - continue; - } - - newlist = xtryrealloc (*list, (srvcount+1)*sizeof(struct srventry)); - if (!newlist) - goto fail; - *list = newlist; - memset (&(*list)[srvcount], 0, sizeof(struct srventry)); - srv = &(*list)[srvcount]; - srvcount++; - - srv->priority = answer->rrs.srvha[count].priority; - srv->weight = answer->rrs.srvha[count].weight; - srv->port = answer->rrs.srvha[count].port; - strcpy (srv->target, answer->rrs.srvha[count].ha.host); - } - - adns_free (answer); - adns_finish (state); - } -#else /*!USE_ADNS*/ - { - unsigned char answer[2048]; - HEADER *header = (HEADER *)answer; - unsigned char *pt, *emsg; - int r; - u16 dlen; - - r = res_query (name, C_IN, T_SRV, answer, sizeof answer); - if (r < sizeof (HEADER) || r > sizeof answer) - return -1; - if (header->rcode != NOERROR || !(count=ntohs (header->ancount))) - return 0; /* Error or no record found. */ - - emsg = &answer[r]; - pt = &answer[sizeof(HEADER)]; - - /* Skip over the query */ - rc = dn_skipname (pt, emsg); - if (rc == -1) - goto fail; - - pt += rc + QFIXEDSZ; - - while (count-- > 0 && pt < emsg) - { - struct srventry *srv=NULL; - u16 type,class; - struct srventry *newlist; - - newlist = xtryrealloc (*list, (srvcount+1)*sizeof(struct srventry)); - if (!newlist) - goto fail; - *list = newlist; - memset(&(*list)[srvcount],0,sizeof(struct srventry)); - srv=&(*list)[srvcount]; - srvcount++; - - rc = dn_skipname(pt,emsg); /* the name we just queried for */ - if (rc == -1) - goto fail; - pt+=rc; - - /* Truncated message? */ - if((emsg-pt)<16) - goto fail; - - type = buf16_to_u16 (pt); - pt += 2; - /* We asked for SRV and got something else !? */ - if(type!=T_SRV) - goto fail; - - class = buf16_to_u16 (pt); - pt += 2; - /* We asked for IN and got something else !? */ - if(class!=C_IN) - goto fail; - - pt += 4; /* ttl */ - dlen = buf16_to_u16 (pt); - pt += 2; - - srv->priority = buf16_to_ushort (pt); - pt += 2; - srv->weight = buf16_to_ushort (pt); - pt += 2; - srv->port = buf16_to_ushort (pt); - pt += 2; - - /* Get the name. 2782 doesn't allow name compression, but - dn_expand still works to pull the name out of the - packet. */ - rc = dn_expand(answer,emsg,pt,srv->target,MAXDNAME); - if (rc == 1 && srv->target[0] == 0) /* "." */ - { - xfree(*list); - *list = NULL; - return 0; - } - if (rc == -1) - goto fail; - pt += rc; - /* Corrupt packet? */ - if (dlen != rc+6) - goto fail; - } - } -#endif /*!USE_ADNS*/ - - /* Now we have an array of all the srv records. */ - - /* Order by priority */ - qsort(*list,srvcount,sizeof(struct srventry),priosort); - - /* For each priority, move the zero-weighted items first. */ - for (i=0; i < srvcount; i++) - { - int j; - - for (j=i;j < srvcount && (*list)[i].priority == (*list)[j].priority; j++) - { - if((*list)[j].weight==0) - { - /* Swap j with i */ - if(j!=i) - { - struct srventry temp; - - memcpy (&temp,&(*list)[j],sizeof(struct srventry)); - memcpy (&(*list)[j],&(*list)[i],sizeof(struct srventry)); - memcpy (&(*list)[i],&temp,sizeof(struct srventry)); - } - - break; - } - } - } - - /* Run the RFC-2782 weighting algorithm. We don't need very high - quality randomness for this, so regular libc srand/rand is - sufficient. Fixme: It is a bit questionaly to reinitalize srand - - better use a gnupg fucntion for this. */ - srand(time(NULL)*getpid()); - - for (i=0; i < srvcount; i++) - { - int j; - float prio_count=0,chose; - - for (j=i; j < srvcount && (*list)[i].priority == (*list)[j].priority; j++) - { - prio_count+=(*list)[j].weight; - (*list)[j].run_count=prio_count; - } - - chose=prio_count*rand()/RAND_MAX; - - for (j=i;j. - */ - -#ifndef GNUPG_COMMON_SRV_H -#define GNUPG_COMMON_SRV_H - -#ifdef USE_DNS_SRV -# ifdef _WIN32 -# ifdef HAVE_WINSOCK2_H -# include -# endif -# include -# else -# include -# include -# include -# endif /* !_WIN32 */ -#endif /* USE_DNS_SRV */ - - -#ifndef MAXDNAME -#define MAXDNAME 1025 -#endif - -struct srventry -{ - unsigned short priority; - unsigned short weight; - unsigned short port; - int run_count; - char target[MAXDNAME]; -}; - -int getsrv(const char *name,struct srventry **list); - -#endif /*GNUPG_COMMON_SRV_H*/ diff --git a/dirmngr/dns-stuff.c b/dirmngr/dns-stuff.c index 3fa6054d3..c500c34e7 100644 --- a/dirmngr/dns-stuff.c +++ b/dirmngr/dns-stuff.c @@ -1,5 +1,5 @@ /* dns-stuff.c - DNS related code including CERT RR (rfc-4398) - * Copyright (C) 2005, 2006, 2009 Free Software Foundation, Inc. + * Copyright (C) 2003, 2005, 2006, 2009 Free Software Foundation, Inc. * Copyright (C) 2005, 2006, 2009, 2015 Werner Koch * * This file is part of GnuPG. @@ -54,8 +54,11 @@ #include "host2net.h" #include "dns-stuff.h" -/* Not every installation has gotten around to supporting CERTs - yet... */ +/* Not every installation has gotten around to supporting SRVs or + CERTs yet... */ +#ifndef T_SRV +#define T_SRV 33 +#endif #ifndef T_CERT # define T_CERT 37 #endif @@ -588,3 +591,250 @@ get_dns_cert (const char *name, int want_certtype, return gpg_err_make (default_errsource, GPG_ERR_NOT_SUPPORTED); #endif } + +#ifdef USE_DNS_SRV +static int +priosort(const void *a,const void *b) +{ + const struct srventry *sa=a,*sb=b; + if(sa->priority>sb->priority) + return 1; + else if(sa->prioritypriority) + return -1; + else + return 0; +} + + +int +getsrv (const char *name,struct srventry **list) +{ + int srvcount=0; + u16 count; + int i, rc; + + *list = NULL; + +#ifdef USE_ADNS + { + adns_state state; + adns_answer *answer = NULL; + + rc = adns_init (&state, adns_if_noerrprint, NULL); + if (rc) + { + log_error ("error initializing adns: %s\n", strerror (errno)); + return -1; + } + + rc = adns_synchronous (state, name, adns_r_srv, adns_qf_quoteok_query, + &answer); + if (rc) + { + log_error ("DNS query failed: %s\n", strerror (errno)); + adns_finish (state); + return -1; + } + if (answer->status != adns_s_ok + || answer->type != adns_r_srv || !answer->nrrs) + { + log_error ("DNS query returned an error or no records: %s (%s)\n", + adns_strerror (answer->status), + adns_errabbrev (answer->status)); + adns_free (answer); + adns_finish (state); + return 0; + } + + for (count = 0; count < answer->nrrs; count++) + { + struct srventry *srv = NULL; + struct srventry *newlist; + + if (strlen (answer->rrs.srvha[count].ha.host) >= MAXDNAME) + { + log_info ("hostname in SRV record too long - skipped\n"); + continue; + } + + newlist = xtryrealloc (*list, (srvcount+1)*sizeof(struct srventry)); + if (!newlist) + goto fail; + *list = newlist; + memset (&(*list)[srvcount], 0, sizeof(struct srventry)); + srv = &(*list)[srvcount]; + srvcount++; + + srv->priority = answer->rrs.srvha[count].priority; + srv->weight = answer->rrs.srvha[count].weight; + srv->port = answer->rrs.srvha[count].port; + strcpy (srv->target, answer->rrs.srvha[count].ha.host); + } + + adns_free (answer); + adns_finish (state); + } +#else /*!USE_ADNS*/ + { + unsigned char answer[2048]; + HEADER *header = (HEADER *)answer; + unsigned char *pt, *emsg; + int r; + u16 dlen; + + r = res_query (name, C_IN, T_SRV, answer, sizeof answer); + if (r < sizeof (HEADER) || r > sizeof answer) + return -1; + if (header->rcode != NOERROR || !(count=ntohs (header->ancount))) + return 0; /* Error or no record found. */ + + emsg = &answer[r]; + pt = &answer[sizeof(HEADER)]; + + /* Skip over the query */ + rc = dn_skipname (pt, emsg); + if (rc == -1) + goto fail; + + pt += rc + QFIXEDSZ; + + while (count-- > 0 && pt < emsg) + { + struct srventry *srv=NULL; + u16 type,class; + struct srventry *newlist; + + newlist = xtryrealloc (*list, (srvcount+1)*sizeof(struct srventry)); + if (!newlist) + goto fail; + *list = newlist; + memset(&(*list)[srvcount],0,sizeof(struct srventry)); + srv=&(*list)[srvcount]; + srvcount++; + + rc = dn_skipname(pt,emsg); /* the name we just queried for */ + if (rc == -1) + goto fail; + pt+=rc; + + /* Truncated message? */ + if((emsg-pt)<16) + goto fail; + + type = buf16_to_u16 (pt); + pt += 2; + /* We asked for SRV and got something else !? */ + if(type!=T_SRV) + goto fail; + + class = buf16_to_u16 (pt); + pt += 2; + /* We asked for IN and got something else !? */ + if(class!=C_IN) + goto fail; + + pt += 4; /* ttl */ + dlen = buf16_to_u16 (pt); + pt += 2; + + srv->priority = buf16_to_ushort (pt); + pt += 2; + srv->weight = buf16_to_ushort (pt); + pt += 2; + srv->port = buf16_to_ushort (pt); + pt += 2; + + /* Get the name. 2782 doesn't allow name compression, but + dn_expand still works to pull the name out of the + packet. */ + rc = dn_expand(answer,emsg,pt,srv->target,MAXDNAME); + if (rc == 1 && srv->target[0] == 0) /* "." */ + { + xfree(*list); + *list = NULL; + return 0; + } + if (rc == -1) + goto fail; + pt += rc; + /* Corrupt packet? */ + if (dlen != rc+6) + goto fail; + } + } +#endif /*!USE_ADNS*/ + + /* Now we have an array of all the srv records. */ + + /* Order by priority */ + qsort(*list,srvcount,sizeof(struct srventry),priosort); + + /* For each priority, move the zero-weighted items first. */ + for (i=0; i < srvcount; i++) + { + int j; + + for (j=i;j < srvcount && (*list)[i].priority == (*list)[j].priority; j++) + { + if((*list)[j].weight==0) + { + /* Swap j with i */ + if(j!=i) + { + struct srventry temp; + + memcpy (&temp,&(*list)[j],sizeof(struct srventry)); + memcpy (&(*list)[j],&(*list)[i],sizeof(struct srventry)); + memcpy (&(*list)[i],&temp,sizeof(struct srventry)); + } + + break; + } + } + } + + /* Run the RFC-2782 weighting algorithm. We don't need very high + quality randomness for this, so regular libc srand/rand is + sufficient. Fixme: It is a bit questionaly to reinitalize srand + - better use a gnupg fucntion for this. */ + srand(time(NULL)*getpid()); + + for (i=0; i < srvcount; i++) + { + int j; + float prio_count=0,chose; + + for (j=i; j < srvcount && (*list)[i].priority == (*list)[j].priority; j++) + { + prio_count+=(*list)[j].weight; + (*list)[j].run_count=prio_count; + } + + chose=prio_count*rand()/RAND_MAX; + + for (j=i;j