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