* gpgkeys_hkp.c, gpgkeys_oldhkp.c: Add support for HKP servers that
[gnupg.git] / keyserver / gpgkeys_http.c
index 8c750d1..b119709 100644 (file)
@@ -1,5 +1,5 @@
 /* gpgkeys_http.c - fetch a key via HTTP
- * Copyright (C) 2004 Free Software Foundation, Inc.
+ * Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  *
  * This file is part of GnuPG.
  *
@@ -15,7 +15,8 @@
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
  */
 
 #include <config.h>
 #include "util.h"
 #include "http.h"
 #include "keyserver.h"
-
-extern char *optarg;
-extern int optind;
+#include "ksutil.h"
 
 #define GET    0
-#define MAX_LINE 80
 
-int verbose=0;
-unsigned int http_flags=0;
-char scheme[80]={'\0'},host[80]={'\0'},proxy[80]={'\0'},port[10]={'\0'},
-  path[1024]={'\0'};
-FILE *input=NULL,*output=NULL,*console=NULL;
+extern char *optarg;
+extern int optind;
 
-#define BEGIN "-----BEGIN PGP PUBLIC KEY BLOCK-----"
-#define END   "-----END PGP PUBLIC KEY BLOCK-----"
+static int verbose=0;
+static unsigned int http_flags=0;
+static char auth[MAX_AUTH+1];
+static char host[MAX_HOST+1];
+static char proxy[MAX_PROXY+1];
+static char port[MAX_PORT+1];
+static char path[URLMAX_PATH+1];
+static FILE *input,*output,*console;
 
 #ifdef __riscos__
 #define HTTP_PROXY_ENV           "GnuPG$HttpProxy"
@@ -56,7 +57,7 @@ FILE *input=NULL,*output=NULL,*console=NULL;
 static int
 get_key(char *getkey)
 {
-  int rc,gotit=0;
+  int rc;
   char *request;
   struct http_context hd;
 
@@ -65,22 +66,15 @@ get_key(char *getkey)
 
   fprintf(output,"KEY 0x%s BEGIN\n",getkey);
 
-  if(verbose)
-    fprintf(console,"gpgkeys: requesting key 0x%s from http://%s%s%s/%s\n",
-           getkey,host,port[0]?":":"",port[0]?port:"",path);
-
-  request=malloc(strlen(scheme)+3+strlen(host)+1+strlen(port)+strlen(path)+99);
+  request=malloc(4+3+strlen(host)+1+strlen(port)+1+strlen(path)+50);
   if(!request)
     {
       fprintf(console,"gpgkeys: out of memory\n");
       return KEYSERVER_NO_MEMORY;
     }
 
-  sprintf(request,"%s://%s%s%s%s",scheme,host,
-         port[0]?":":"",port[0]?port:"",path);
-
-  if(verbose>2)
-    fprintf(console,"gpgkeys: HTTP URL is \"%s\"\n",request);
+  sprintf(request,"http://%s%s%s%s%s%s%s",auth[0]?auth:"",auth[0]?"@":"",
+         host,port[0]?":":"",port[0]?port:"",path[0]?"":"/",path);
 
   rc=http_open_document(&hd,request,http_flags,proxy[0]?proxy:NULL);
   if(rc!=0)
@@ -92,7 +86,7 @@ get_key(char *getkey)
     }
   else
     {
-      unsigned int maxlen=1024,buflen;
+      unsigned int maxlen=1024,buflen,gotit=0;
       byte *line=NULL;
 
       while(iobuf_read_line(hd.fp_read,&line,&buflen,&maxlen))
@@ -101,14 +95,14 @@ get_key(char *getkey)
 
          if(gotit)
            {
-             fputs (line, output);
+             print_nocr(output,line);
              if(strncmp(line,END,strlen(END))==0)
                break;
            }
          else
            if(strncmp(line,BEGIN,strlen(BEGIN))==0)
              {
-               fputs (line,output);
+               print_nocr(output,line);
                gotit=1;
              }
        }
@@ -123,6 +117,7 @@ get_key(char *getkey)
        }
 
       m_free(line);
+      http_close(&hd);
     }
 
   free(request);
@@ -130,24 +125,42 @@ get_key(char *getkey)
   return KEYSERVER_OK;
 }
 
+static void 
+show_help (FILE *fp)
+{
+  fprintf (fp,"-h\thelp\n");
+  fprintf (fp,"-V\tversion\n");
+  fprintf (fp,"-o\toutput to this file\n");
+}
+
 int
 main(int argc,char *argv[])
 {
   int arg,action=-1,ret=KEYSERVER_INTERNAL_ERROR;
   char line[MAX_LINE];
-  int failed=0;
   char *thekey=NULL;
+  unsigned int timeout=DEFAULT_KEYSERVER_TIMEOUT;
 
   console=stderr;
 
+  /* Kludge to implement standard GNU options.  */
+  if (argc > 1 && !strcmp (argv[1], "--version"))
+    {
+      fputs ("gpgkeys_http (GnuPG) " VERSION"\n", stdout);
+      return 0;
+    }
+  else if (argc > 1 && !strcmp (argv[1], "--help"))
+    {
+      show_help (stdout);
+      return 0;
+    }
+
   while((arg=getopt(argc,argv,"hVo:"))!=-1)
     switch(arg)
       {
       default:
       case 'h':
-       fprintf(console,"-h\thelp\n");
-       fprintf(console,"-V\tversion\n");
-       fprintf(console,"-o\toutput to this file\n");
+        show_help (console);
        return KEYSERVER_OK;
 
       case 'V':
@@ -158,7 +171,7 @@ main(int argc,char *argv[])
        output=fopen(optarg,"w");
        if(output==NULL)
          {
-           fprintf(console,"gpgkeys: Cannot open output file \"%s\": %s\n",
+           fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
                    optarg,strerror(errno));
            return KEYSERVER_INTERNAL_ERROR;
          }
@@ -171,7 +184,7 @@ main(int argc,char *argv[])
       input=fopen(argv[optind],"r");
       if(input==NULL)
        {
-         fprintf(console,"gpgkeys: Cannot open input file \"%s\": %s\n",
+         fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
                  argv[optind],strerror(errno));
          return KEYSERVER_INTERNAL_ERROR;
        }
@@ -188,8 +201,8 @@ main(int argc,char *argv[])
   while(fgets(line,MAX_LINE,input)!=NULL)
     {
       int version;
-      char commandstr[7];
-      char optionstr[110];
+      char command[MAX_COMMAND+1];
+      char option[MAX_OPTION+1];
       char hash;
 
       if(line[0]=='\n')
@@ -198,37 +211,37 @@ main(int argc,char *argv[])
       if(sscanf(line,"%c",&hash)==1 && hash=='#')
        continue;
 
-      if(sscanf(line,"COMMAND %6s\n",commandstr)==1)
+      if(sscanf(line,"COMMAND %" MKSTRING(MAX_COMMAND) "s\n",command)==1)
        {
-         commandstr[6]='\0';
+         command[MAX_COMMAND]='\0';
 
-         if(strcasecmp(commandstr,"get")==0)
+         if(strcasecmp(command,"get")==0)
            action=GET;
 
          continue;
        }
 
-      if(sscanf(line,"SCHEME %79s\n",scheme)==1)
+      if(sscanf(line,"AUTH %" MKSTRING(MAX_AUTH) "s\n",auth)==1)
        {
-         scheme[79]='\0';
+         auth[MAX_AUTH]='\0';
          continue;
        }
 
-      if(sscanf(line,"HOST %79s\n",host)==1)
+      if(sscanf(line,"HOST %" MKSTRING(MAX_HOST) "s\n",host)==1)
        {
-         host[79]='\0';
+         host[MAX_HOST]='\0';
          continue;
        }
 
-      if(sscanf(line,"PORT %9s\n",port)==1)
+      if(sscanf(line,"PORT %" MKSTRING(MAX_PORT) "s\n",port)==1)
        {
-         port[9]='\0';
+         port[MAX_PORT]='\0';
          continue;
        }
 
-      if(sscanf(line,"PATH %1023s\n",path)==1)
+      if(sscanf(line,"PATH %" MKSTRING(URLMAX_PATH) "s\n",path)==1)
        {
-         path[1023]='\0';
+         path[URLMAX_PATH]='\0';
          continue;
        }
 
@@ -243,17 +256,17 @@ main(int argc,char *argv[])
          continue;
        }
 
-      if(sscanf(line,"OPTION %109s\n",optionstr)==1)
+      if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1)
        {
          int no=0;
-         char *start=&optionstr[0];
+         char *start=&option[0];
 
-         optionstr[109]='\0';
+         option[MAX_OPTION]='\0';
 
-         if(strncasecmp(optionstr,"no-",3)==0)
+         if(strncasecmp(option,"no-",3)==0)
            {
              no=1;
-             start=&optionstr[3];
+             start=&option[3];
            }
 
          if(strcasecmp(start,"verbose")==0)
@@ -296,11 +309,26 @@ main(int argc,char *argv[])
              else
                http_flags|=HTTP_FLAG_TRY_SRV;
            }
+         else if(strncasecmp(start,"timeout",7)==0)
+           {
+             if(no)
+               timeout=0;
+             else if(start[7]=='=')
+               timeout=atoi(&start[8]);
+             else if(start[7]=='\0')
+               timeout=DEFAULT_KEYSERVER_TIMEOUT;
+           }
 
          continue;
        }
     }
 
+  if(timeout && register_timeout()==-1)
+    {
+      fprintf(console,"gpgkeys: unable to register timeout handler\n");
+      return KEYSERVER_INTERNAL_ERROR;
+    }
+
   /* By suggested convention, if the user gives a :port, then disable
      SRV. */
   if(port[0])
@@ -345,6 +373,12 @@ main(int argc,char *argv[])
       goto fail;
     }
 
+  if(!thekey || !host[0])
+    {
+      fprintf(console,"gpgkeys: invalid keyserver instructions\n");
+      goto fail;
+    }
+
   /* Send the response */
 
   fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
@@ -352,7 +386,6 @@ main(int argc,char *argv[])
 
   if(verbose>1)
     {
-      fprintf(console,"Scheme:\t\t%s\n",scheme);
       fprintf(console,"Host:\t\t%s\n",host);
       if(port[0])
        fprintf(console,"Port:\t\t%s\n",port);
@@ -361,11 +394,9 @@ main(int argc,char *argv[])
       fprintf(console,"Command:\tGET\n");
     }
 
-  if(get_key(thekey)!=KEYSERVER_OK)
-    failed++;
+  set_timeout(timeout);
 
-  if(!failed)
-    ret=KEYSERVER_OK;
+  ret=get_key(thekey);
 
  fail: