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