* gpgkeys_finger.c, gpgkeys_hkp.c, gpgkeys_http.c, gpgkeys_ldap.c: Part 1
[gnupg.git] / keyserver / gpgkeys_http.c
1 /* gpgkeys_http.c - fetch a key via HTTP
2  * Copyright (C) 2004 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #ifdef HAVE_GETOPT_H
28 #include <getopt.h>
29 #endif
30 #define INCLUDED_BY_MAIN_MODULE 1
31 #include "util.h"
32 #include "http.h"
33 #include "keyserver.h"
34 #include "ksutil.h"
35
36 extern char *optarg;
37 extern int optind;
38
39 #define GET    0
40 #define MAX_LINE 80
41 #define MAX_AUTH 128
42 #define MAX_HOST 80
43 #define MAX_PROXY 80
44 #define MAX_PORT 10
45 #define MAX_PATH 1024
46 #define MAX_COMMAND 7
47 #define MAX_OPTION 256
48
49 #define STRINGIFY(x) #x
50 #define MKSTRING(x) STRINGIFY(x)
51
52 static int verbose=0;
53 static unsigned int http_flags=0;
54 static char auth[MAX_AUTH+1]={'\0'},host[MAX_HOST+1]={'\0'},proxy[MAX_PROXY+1]={'\0'},port[MAX_PORT+1]={'\0'},path[MAX_PATH+1]={'\0'};
55 static FILE *input=NULL,*output=NULL,*console=NULL;
56
57 #define BEGIN "-----BEGIN PGP PUBLIC KEY BLOCK-----"
58 #define END   "-----END PGP PUBLIC KEY BLOCK-----"
59
60 #ifdef __riscos__
61 #define HTTP_PROXY_ENV           "GnuPG$HttpProxy"
62 #else
63 #define HTTP_PROXY_ENV           "http_proxy"
64 #endif
65
66 static int
67 get_key(char *getkey)
68 {
69   int rc;
70   char *request;
71   struct http_context hd;
72
73   if(strncmp(getkey,"0x",2)==0)
74     getkey+=2;
75
76   fprintf(output,"KEY 0x%s BEGIN\n",getkey);
77
78   request=malloc(4+3+strlen(host)+1+strlen(port)+1+strlen(path)+50);
79   if(!request)
80     {
81       fprintf(console,"gpgkeys: out of memory\n");
82       return KEYSERVER_NO_MEMORY;
83     }
84
85   sprintf(request,"http://%s%s%s%s%s%s%s",auth[0]?auth:"",auth[0]?"@":"",
86           host,port[0]?":":"",port[0]?port:"",path[0]?"":"/",path);
87
88   rc=http_open_document(&hd,request,http_flags,proxy[0]?proxy:NULL);
89   if(rc!=0)
90     {
91       fprintf(console,"gpgkeys: HTTP fetch error: %s\n",
92               rc==G10ERR_NETWORK?strerror(errno):g10_errstr(rc));
93       fprintf(output,"KEY 0x%s FAILED %d\n",getkey,
94             rc==G10ERR_NETWORK?KEYSERVER_UNREACHABLE:KEYSERVER_INTERNAL_ERROR);
95     }
96   else
97     {
98       unsigned int maxlen=1024,buflen,gotit=0;
99       byte *line=NULL;
100
101       while(iobuf_read_line(hd.fp_read,&line,&buflen,&maxlen))
102         {
103           maxlen=1024;
104
105           if(gotit)
106             {
107               fputs(line,output);
108               if(strncmp(line,END,strlen(END))==0)
109                 break;
110             }
111           else
112             if(strncmp(line,BEGIN,strlen(BEGIN))==0)
113               {
114                 fputs(line,output);
115                 gotit=1;
116               }
117         }
118
119       if(gotit)
120         fprintf(output,"KEY 0x%s END\n",getkey);
121       else
122         {
123           fprintf(console,"gpgkeys: key %s not found on keyserver\n",getkey);
124           fprintf(output,"KEY 0x%s FAILED %d\n",
125                   getkey,KEYSERVER_KEY_NOT_FOUND);
126         }
127
128       m_free(line);
129     }
130
131   free(request);
132
133   return KEYSERVER_OK;
134 }
135
136 static void 
137 show_help (FILE *fp)
138 {
139   fprintf (fp,"-h\thelp\n");
140   fprintf (fp,"-V\tversion\n");
141   fprintf (fp,"-o\toutput to this file\n");
142 }
143
144 int
145 main(int argc,char *argv[])
146 {
147   int arg,action=-1,ret=KEYSERVER_INTERNAL_ERROR;
148   char line[MAX_LINE];
149   char *thekey=NULL;
150   unsigned int timeout=DEFAULT_KEYSERVER_TIMEOUT;
151
152   console=stderr;
153
154   /* Kludge to implement standard GNU options.  */
155   if (argc > 1 && !strcmp (argv[1], "--version"))
156     {
157       fputs ("gpgkeys_http (GnuPG) " VERSION"\n", stdout);
158       return 0;
159     }
160   else if (argc > 1 && !strcmp (argv[1], "--help"))
161     {
162       show_help (stdout);
163       return 0;
164     }
165
166   while((arg=getopt(argc,argv,"hVo:"))!=-1)
167     switch(arg)
168       {
169       default:
170       case 'h':
171         show_help (console);
172         return KEYSERVER_OK;
173
174       case 'V':
175         fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
176         return KEYSERVER_OK;
177
178       case 'o':
179         output=fopen(optarg,"w");
180         if(output==NULL)
181           {
182             fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
183                     optarg,strerror(errno));
184             return KEYSERVER_INTERNAL_ERROR;
185           }
186
187         break;
188       }
189
190   if(argc>optind)
191     {
192       input=fopen(argv[optind],"r");
193       if(input==NULL)
194         {
195           fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
196                   argv[optind],strerror(errno));
197           return KEYSERVER_INTERNAL_ERROR;
198         }
199     }
200
201   if(input==NULL)
202     input=stdin;
203
204   if(output==NULL)
205     output=stdout;
206
207   /* Get the command and info block */
208
209   while(fgets(line,MAX_LINE,input)!=NULL)
210     {
211       int version;
212       char command[MAX_COMMAND+1];
213       char option[MAX_OPTION+1];
214       char hash;
215
216       if(line[0]=='\n')
217         break;
218
219       if(sscanf(line,"%c",&hash)==1 && hash=='#')
220         continue;
221
222       if(sscanf(line,"COMMAND %" MKSTRING(MAX_COMMAND) "s\n",command)==1)
223         {
224           command[MAX_COMMAND]='\0';
225
226           if(strcasecmp(command,"get")==0)
227             action=GET;
228
229           continue;
230         }
231
232       if(sscanf(line,"AUTH %" MKSTRING(MAX_AUTH) "s\n",auth)==1)
233         {
234           auth[MAX_AUTH]='\0';
235           continue;
236         }
237
238       if(sscanf(line,"HOST %" MKSTRING(MAX_HOST) "s\n",host)==1)
239         {
240           host[MAX_HOST]='\0';
241           continue;
242         }
243
244       if(sscanf(line,"PORT %" MKSTRING(MAX_PORT) "s\n",port)==1)
245         {
246           port[MAX_PORT]='\0';
247           continue;
248         }
249
250       if(sscanf(line,"PATH %" MKSTRING(MAX_PATH) "s\n",path)==1)
251         {
252           path[MAX_PATH]='\0';
253           continue;
254         }
255
256       if(sscanf(line,"VERSION %d\n",&version)==1)
257         {
258           if(version!=KEYSERVER_PROTO_VERSION)
259             {
260               ret=KEYSERVER_VERSION_ERROR;
261               goto fail;
262             }
263
264           continue;
265         }
266
267       if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1)
268         {
269           int no=0;
270           char *start=&option[0];
271
272           option[MAX_OPTION]='\0';
273
274           if(strncasecmp(option,"no-",3)==0)
275             {
276               no=1;
277               start=&option[3];
278             }
279
280           if(strcasecmp(start,"verbose")==0)
281             {
282               if(no)
283                 verbose--;
284               else
285                 verbose++;
286             }
287           else if(strncasecmp(start,"http-proxy",10)==0)
288             {
289               if(no)
290                 proxy[0]='\0';
291               else if(start[10]=='=')
292                 {
293                   strncpy(proxy,&start[11],79);
294                   proxy[79]='\0';
295                 }
296               else if(start[10]=='\0')
297                 {
298                   char *http_proxy=getenv(HTTP_PROXY_ENV);
299                   if(http_proxy)
300                     {
301                       strncpy(proxy,http_proxy,79);
302                       proxy[79]='\0';
303                     }
304                 }
305             }
306           else if(strcasecmp(start,"broken-http-proxy")==0)
307             {
308               if(no)
309                 http_flags&=~HTTP_FLAG_NO_SHUTDOWN;
310               else
311                 http_flags|=HTTP_FLAG_NO_SHUTDOWN;
312             }
313           else if(strcasecmp(start,"try-dns-srv")==0)
314             {
315               if(no)
316                 http_flags&=~HTTP_FLAG_TRY_SRV;
317               else
318                 http_flags|=HTTP_FLAG_TRY_SRV;
319             }
320           else if(strncasecmp(start,"timeout",7)==0)
321             {
322               if(no)
323                 timeout=0;
324               else if(start[7]=='=')
325                 timeout=atoi(&start[8]);
326               else if(start[7]=='\0')
327                 timeout=DEFAULT_KEYSERVER_TIMEOUT;
328             }
329
330           continue;
331         }
332     }
333
334   if(timeout && register_timeout()==-1)
335     {
336       fprintf(console,"gpgkeys: unable to register timeout handler\n");
337       return KEYSERVER_INTERNAL_ERROR;
338     }
339
340   /* By suggested convention, if the user gives a :port, then disable
341      SRV. */
342   if(port[0])
343     http_flags&=~HTTP_FLAG_TRY_SRV;
344
345   /* If it's a GET or a SEARCH, the next thing to come in is the
346      keyids.  If it's a SEND, then there are no keyids. */
347
348   if(action==GET)
349     {
350       /* Eat the rest of the file */
351       for(;;)
352         {
353           if(fgets(line,MAX_LINE,input)==NULL)
354             break;
355           else
356             {
357               if(line[0]=='\n' || line[0]=='\0')
358                 break;
359
360               if(!thekey)
361                 {
362                   thekey=strdup(line);
363                   if(!thekey)
364                     {
365                       fprintf(console,"gpgkeys: out of memory while "
366                               "building key list\n");
367                       ret=KEYSERVER_NO_MEMORY;
368                       goto fail;
369                     }
370
371                   /* Trim the trailing \n */
372                   thekey[strlen(line)-1]='\0';
373                 }
374             }
375         }
376     }
377   else
378     {
379       fprintf(console,
380               "gpgkeys: this keyserver type only supports key retrieval\n");
381       goto fail;
382     }
383
384   if(!thekey || !host[0])
385     {
386       fprintf(console,"gpgkeys: invalid keyserver instructions\n");
387       goto fail;
388     }
389
390   /* Send the response */
391
392   fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
393   fprintf(output,"PROGRAM %s\n\n",VERSION);
394
395   if(verbose>1)
396     {
397       fprintf(console,"Host:\t\t%s\n",host);
398       if(port[0])
399         fprintf(console,"Port:\t\t%s\n",port);
400       if(path[0])
401         fprintf(console,"Path:\t\t%s\n",path);
402       fprintf(console,"Command:\tGET\n");
403     }
404
405   set_timeout(timeout);
406
407   ret=get_key(thekey);
408
409  fail:
410
411   free(thekey);
412
413   if(input!=stdin)
414     fclose(input);
415
416   if(output!=stdout)
417     fclose(output);
418
419   return ret;
420 }