aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/ChangeLog8
-rw-r--r--common/Makefile.am1
-rw-r--r--common/http.c7
-rw-r--r--common/keyserver.h44
-rw-r--r--common/srv.c257
-rw-r--r--common/srv.h51
6 files changed, 365 insertions, 3 deletions
diff --git a/common/ChangeLog b/common/ChangeLog
index 9e72c5d3e..c033b62d9 100644
--- a/common/ChangeLog
+++ b/common/ChangeLog
@@ -1,3 +1,11 @@
+2006-08-16 Werner Koch <[email protected]>
+
+ * keyserver.h: Moved from ../include to here.
+
+ * http.c: Include srv.h.
+
+ * srv.c, srv.h: New. Taken from GnuPG 1.4
+
2006-08-14 Werner Koch <[email protected]>
* http.h (struct http_context_s): Moved to implementation.
diff --git a/common/Makefile.am b/common/Makefile.am
index e9de870e4..7bf008caa 100644
--- a/common/Makefile.am
+++ b/common/Makefile.am
@@ -51,6 +51,7 @@ libcommon_a_SOURCES = \
signal.c \
dynload.h \
estream.c estream.h \
+ srv.c srv.h \
dns-cert.c dns-cert.h \
pka.c pka.h \
http.c http.h
diff --git a/common/http.c b/common/http.c
index 22ee5c23c..e77e1d11e 100644
--- a/common/http.c
+++ b/common/http.c
@@ -67,10 +67,11 @@ typedef gnutls_transport_ptr gnutls_transport_ptr_t;
#include "util.h"
#include "http.h"
-
+#ifdef USE_DNS_SRV
+#include "srv.h"
+#else /*!USE_DNS_SRV*/
/* If we are not compiling with SRV record support we provide stub
data structures. */
-#ifndef USE_DNS_SRV
#ifndef MAXDNAME
#define MAXDNAME 1025
#endif
@@ -1366,7 +1367,7 @@ connect_server (const char *server, unsigned short port,
{
char srvname[MAXDNAME];
- stprcpy (stpcpy (stpcpy (stpcpy (srvname,"_"), srvtag),
+ stpcpy (stpcpy (stpcpy (stpcpy (srvname,"_"), srvtag),
"._tcp."), server);
srvcount = getsrv (srvname, &serverlist);
}
diff --git a/common/keyserver.h b/common/keyserver.h
new file mode 100644
index 000000000..3a8f5f43c
--- /dev/null
+++ b/common/keyserver.h
@@ -0,0 +1,44 @@
+/* keyserver.h - Public definitions for gpg keyserver helpers.
+ * Copyright (C) 2001, 2002 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of 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.
+ *
+ * GnuPG 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+
+#ifndef GNUPG_COMMON_KEYSERVER_H
+#define GNUPG_COMMON_KEYSERVER_H
+
+#define KEYSERVER_PROTO_VERSION 1
+
+/* These are usable for return codes for the gpgkeys_ process, and
+ also KEY FAILED codes. */
+#define KEYSERVER_OK 0 /* not an error */
+#define KEYSERVER_INTERNAL_ERROR 1 /* gpgkeys_ internal error */
+#define KEYSERVER_NOT_SUPPORTED 2 /* operation not supported */
+#define KEYSERVER_VERSION_ERROR 3 /* VERSION mismatch */
+#define KEYSERVER_GENERAL_ERROR 4 /* keyserver internal error */
+#define KEYSERVER_NO_MEMORY 5 /* out of memory */
+#define KEYSERVER_KEY_NOT_FOUND 6 /* key not found */
+#define KEYSERVER_KEY_EXISTS 7 /* key already exists */
+#define KEYSERVER_KEY_INCOMPLETE 8 /* key incomplete (EOF) */
+#define KEYSERVER_UNREACHABLE 9 /* unable to contact keyserver */
+#define KEYSERVER_TIMEOUT 10 /* timeout while accessing keyserver */
+
+/* Must be 127 due to shell internal magic. */
+#define KEYSERVER_SCHEME_NOT_FOUND 127
+
+#endif /*GNUPG_COMMON_KEYSERVER_H*/
diff --git a/common/srv.c b/common/srv.c
new file mode 100644
index 000000000..386e5e459
--- /dev/null
+++ b/common/srv.c
@@ -0,0 +1,257 @@
+/* srv.c - DNS SRV code
+ * Copyright (C) 2003 Free Software Foundation, Inc.
+ *
+ * This file is part of GNUPG.
+ *
+ * GNUPG is free software; you can redistribute it and/or modify
+ * it under the terms of 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.
+ *
+ * GNUPG 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+
+#include <config.h>
+#include <sys/types.h>
+#ifdef _WIN32
+#include <windows.h>
+#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>
+
+#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;
+}
+
+int
+getsrv(const char *name,struct srventry **list)
+{
+ unsigned char answer[PACKETSZ];
+ int r,srvcount=0;
+ unsigned char *pt,*emsg;
+ u16 count,dlen;
+
+ *list=NULL;
+
+ r=res_query(name,C_IN,T_SRV,answer,PACKETSZ);
+ if(r<sizeof(HEADER) || r>PACKETSZ)
+ return -1;
+
+ if((((HEADER *)answer)->rcode)==NOERROR &&
+ (count=ntohs(((HEADER *)answer)->ancount)))
+ {
+ int i,rc;
+
+ 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;
+
+ *list=xrealloc(*list,(srvcount+1)*sizeof(struct srventry));
+ 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=*pt++ << 8;
+ type|=*pt++;
+ /* We asked for SRV and got something else !? */
+ if(type!=T_SRV)
+ goto fail;
+
+ class=*pt++ << 8;
+ class|=*pt++;
+ /* We asked for IN and got something else !? */
+ if(class!=C_IN)
+ goto fail;
+
+ 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++;
+
+ /* 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) /* "." */
+ goto noanswer;
+ if(rc==-1)
+ goto fail;
+ pt+=rc;
+ /* Corrupt packet? */
+ if(dlen!=rc+6)
+ goto fail;
+
+#if 0
+ printf("count=%d\n",srvcount);
+ printf("priority=%d\n",srv->priority);
+ printf("weight=%d\n",srv->weight);
+ printf("port=%d\n",srv->port);
+ printf("target=%s\n",srv->target);
+#endif
+ }
+
+ /* 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. */
+ 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<srvcount && (*list)[i].priority==(*list)[j].priority;j++)
+ {
+ if(chose<=(*list)[j].run_count)
+ {
+ /* 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;
+ }
+ }
+ }
+ }
+
+ return srvcount;
+
+ noanswer:
+ xfree(*list);
+ *list=NULL;
+ return 0;
+
+ 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:
+compile-command: "cc -DTEST -I.. -I../include -Wall -g -o srv srv.c -lresolv libutil.a"
+End:
+*/
diff --git a/common/srv.h b/common/srv.h
new file mode 100644
index 000000000..55906eb00
--- /dev/null
+++ b/common/srv.h
@@ -0,0 +1,51 @@
+/* srv.h
+ * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
+ *
+ * This file is part of GNUPG.
+ *
+ * GNUPG is free software; you can redistribute it and/or modify
+ * it under the terms of 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.
+ *
+ * GNUPG 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+
+#ifndef GNUPG_COMMON_SRV_H
+#define GNUPG_COMMON_SRV_H
+
+#ifdef USE_DNS_SRV
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <resolv.h>
+#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*/