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