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