2006-08-16 12:47:53 +02:00
|
|
|
/* srv.c - DNS SRV code
|
2009-05-28 05:24:54 +02:00
|
|
|
* Copyright (C) 2003, 2009 Free Software Foundation, Inc.
|
2006-08-16 12:47:53 +02:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is part of GnuPG.
|
2006-08-16 12:47:53 +02:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2006-08-16 12:47:53 +02:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - 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,
|
2006-08-16 12:47:53 +02:00
|
|
|
* 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
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2006-08-16 12:47:53 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef _WIN32
|
2014-03-07 09:46:44 +01:00
|
|
|
# ifdef HAVE_WINSOCK2_H
|
|
|
|
# include <winsock2.h>
|
|
|
|
# endif
|
|
|
|
# include <windows.h>
|
2006-08-16 12:47:53 +02:00
|
|
|
#else
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
#endif
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2009-12-07 16:52:27 +01:00
|
|
|
#ifdef USE_ADNS
|
|
|
|
# include <adns.h>
|
|
|
|
#endif
|
2006-08-16 12:47:53 +02:00
|
|
|
|
|
|
|
#include "util.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->priority<sb->priority)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
|
2006-08-16 12:47:53 +02:00
|
|
|
int
|
2009-12-07 16:52:27 +01:00
|
|
|
getsrv (const char *name,struct srventry **list)
|
2006-08-16 12:47:53 +02:00
|
|
|
{
|
2009-12-07 16:52:27 +01:00
|
|
|
int srvcount=0;
|
|
|
|
u16 count;
|
|
|
|
int i, rc;
|
|
|
|
|
|
|
|
*list = NULL;
|
|
|
|
|
|
|
|
#ifdef USE_ADNS
|
|
|
|
{
|
|
|
|
adns_state state;
|
|
|
|
adns_answer *answer = NULL;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
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;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
if (answer->status != adns_s_ok
|
2009-12-07 16:52:27 +01:00
|
|
|
|| 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;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
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++;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
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;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
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. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
emsg = &answer[r];
|
|
|
|
pt = &answer[sizeof(HEADER)];
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
/* Skip over the query */
|
|
|
|
rc = dn_skipname (pt, emsg);
|
|
|
|
if (rc == -1)
|
|
|
|
goto fail;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
pt += rc + QFIXEDSZ;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
while (count-- > 0 && pt < emsg)
|
|
|
|
{
|
|
|
|
struct srventry *srv=NULL;
|
|
|
|
u16 type,class;
|
|
|
|
struct srventry *newlist;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
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++;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
rc = dn_skipname(pt,emsg); /* the name we just queried for */
|
|
|
|
if (rc == -1)
|
|
|
|
goto fail;
|
|
|
|
pt+=rc;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
/* Truncated message? */
|
|
|
|
if((emsg-pt)<16)
|
|
|
|
goto fail;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
type=*pt++ << 8;
|
|
|
|
type|=*pt++;
|
|
|
|
/* We asked for SRV and got something else !? */
|
|
|
|
if(type!=T_SRV)
|
|
|
|
goto fail;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
class=*pt++ << 8;
|
|
|
|
class|=*pt++;
|
|
|
|
/* We asked for IN and got something else !? */
|
|
|
|
if(class!=C_IN)
|
|
|
|
goto fail;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
pt+=4; /* ttl */
|
|
|
|
dlen=*pt++ << 8;
|
|
|
|
dlen|=*pt++;
|
|
|
|
srv->priority=*pt++ << 8;
|
|
|
|
srv->priority|=*pt++;
|
|
|
|
srv->weight=*pt++ << 8;
|
|
|
|
srv->weight|=*pt++;
|
|
|
|
srv->port=*pt++ << 8;
|
|
|
|
srv->port|=*pt++;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
/* 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*/
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
/* Now we have an array of all the srv records. */
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
/* Order by priority */
|
|
|
|
qsort(*list,srvcount,sizeof(struct srventry),priosort);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
/* For each priority, move the zero-weighted items first. */
|
|
|
|
for (i=0; i < srvcount; i++)
|
|
|
|
{
|
|
|
|
int j;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
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;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
memcpy (&temp,&(*list)[j],sizeof(struct srventry));
|
|
|
|
memcpy (&(*list)[j],&(*list)[i],sizeof(struct srventry));
|
|
|
|
memcpy (&(*list)[i],&temp,sizeof(struct srventry));
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-16 12:47:53 +02:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
/* 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());
|
2006-08-16 12:47:53 +02:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
for (i=0; i < srvcount; i++)
|
2006-08-16 12:47:53 +02:00
|
|
|
{
|
2009-12-07 16:52:27 +01:00
|
|
|
int j;
|
|
|
|
float prio_count=0,chose;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
for (j=i; j < srvcount && (*list)[i].priority == (*list)[j].priority; j++)
|
|
|
|
{
|
|
|
|
prio_count+=(*list)[j].weight;
|
|
|
|
(*list)[j].run_count=prio_count;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
chose=prio_count*rand()/RAND_MAX;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
for (j=i;j<srvcount && (*list)[i].priority==(*list)[j].priority;j++)
|
|
|
|
{
|
|
|
|
if (chose<=(*list)[j].run_count)
|
|
|
|
{
|
|
|
|
/* Swap j with i */
|
|
|
|
if(j!=i)
|
|
|
|
{
|
|
|
|
struct srventry temp;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2009-12-07 16:52:27 +01:00
|
|
|
memcpy(&temp,&(*list)[j],sizeof(struct srventry));
|
|
|
|
memcpy(&(*list)[j],&(*list)[i],sizeof(struct srventry));
|
|
|
|
memcpy(&(*list)[i],&temp,sizeof(struct srventry));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-08-16 12:47:53 +02:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2006-08-16 12:47:53 +02:00
|
|
|
return srvcount;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
xfree(*list);
|
|
|
|
*list=NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
int
|
|
|
|
main(int argc,char *argv[])
|
|
|
|
{
|
|
|
|
struct srventry *srv;
|
|
|
|
int rc,i;
|
|
|
|
|
|
|
|
rc=getsrv("_hkp._tcp.wwwkeys.pgp.net",&srv);
|
|
|
|
printf("Count=%d\n\n",rc);
|
|
|
|
for(i=0;i<rc;i++)
|
|
|
|
{
|
|
|
|
printf("priority=%hu\n",srv[i].priority);
|
|
|
|
printf("weight=%hu\n",srv[i].weight);
|
|
|
|
printf("port=%hu\n",srv[i].port);
|
|
|
|
printf("target=%s\n",srv[i].target);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
xfree(srv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* TEST */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Local Variables:
|
2012-08-24 09:29:56 +02:00
|
|
|
compile-command: "cc -DTEST -I.. -I../include -Wall -g -o srv srv.c -lresolv ../tools/no-libgcrypt.o ../common/libcommon.a"
|
2006-08-16 12:47:53 +02:00
|
|
|
End:
|
|
|
|
*/
|