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