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