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