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