8c750d1a4a01c46faec188081fb817ed658789d1
[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
35 extern char *optarg;
36 extern int optind;
37
38 #define GET    0
39 #define MAX_LINE 80
40
41 int verbose=0;
42 unsigned int http_flags=0;
43 char scheme[80]={'\0'},host[80]={'\0'},proxy[80]={'\0'},port[10]={'\0'},
44   path[1024]={'\0'};
45 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,gotit=0;
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   if(verbose)
69     fprintf(console,"gpgkeys: requesting key 0x%s from http://%s%s%s/%s\n",
70             getkey,host,port[0]?":":"",port[0]?port:"",path);
71
72   request=malloc(strlen(scheme)+3+strlen(host)+1+strlen(port)+strlen(path)+99);
73   if(!request)
74     {
75       fprintf(console,"gpgkeys: out of memory\n");
76       return KEYSERVER_NO_MEMORY;
77     }
78
79   sprintf(request,"%s://%s%s%s%s",scheme,host,
80           port[0]?":":"",port[0]?port:"",path);
81
82   if(verbose>2)
83     fprintf(console,"gpgkeys: HTTP URL is \"%s\"\n",request);
84
85   rc=http_open_document(&hd,request,http_flags,proxy[0]?proxy:NULL);
86   if(rc!=0)
87     {
88       fprintf(console,"gpgkeys: HTTP fetch error: %s\n",
89               rc==G10ERR_NETWORK?strerror(errno):g10_errstr(rc));
90       fprintf(output,"KEY 0x%s FAILED %d\n",getkey,
91             rc==G10ERR_NETWORK?KEYSERVER_UNREACHABLE:KEYSERVER_INTERNAL_ERROR);
92     }
93   else
94     {
95       unsigned int maxlen=1024,buflen;
96       byte *line=NULL;
97
98       while(iobuf_read_line(hd.fp_read,&line,&buflen,&maxlen))
99         {
100           maxlen=1024;
101
102           if(gotit)
103             {
104               fputs (line, output);
105               if(strncmp(line,END,strlen(END))==0)
106                 break;
107             }
108           else
109             if(strncmp(line,BEGIN,strlen(BEGIN))==0)
110               {
111                 fputs (line,output);
112                 gotit=1;
113               }
114         }
115
116       if(gotit)
117         fprintf(output,"KEY 0x%s END\n",getkey);
118       else
119         {
120           fprintf(console,"gpgkeys: key %s not found on keyserver\n",getkey);
121           fprintf(output,"KEY 0x%s FAILED %d\n",
122                   getkey,KEYSERVER_KEY_NOT_FOUND);
123         }
124
125       m_free(line);
126     }
127
128   free(request);
129
130   return KEYSERVER_OK;
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   int failed=0;
139   char *thekey=NULL;
140
141   console=stderr;
142
143   while((arg=getopt(argc,argv,"hVo:"))!=-1)
144     switch(arg)
145       {
146       default:
147       case 'h':
148         fprintf(console,"-h\thelp\n");
149         fprintf(console,"-V\tversion\n");
150         fprintf(console,"-o\toutput to this file\n");
151         return KEYSERVER_OK;
152
153       case 'V':
154         fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
155         return KEYSERVER_OK;
156
157       case 'o':
158         output=fopen(optarg,"w");
159         if(output==NULL)
160           {
161             fprintf(console,"gpgkeys: Cannot open output file \"%s\": %s\n",
162                     optarg,strerror(errno));
163             return KEYSERVER_INTERNAL_ERROR;
164           }
165
166         break;
167       }
168
169   if(argc>optind)
170     {
171       input=fopen(argv[optind],"r");
172       if(input==NULL)
173         {
174           fprintf(console,"gpgkeys: Cannot open input file \"%s\": %s\n",
175                   argv[optind],strerror(errno));
176           return KEYSERVER_INTERNAL_ERROR;
177         }
178     }
179
180   if(input==NULL)
181     input=stdin;
182
183   if(output==NULL)
184     output=stdout;
185
186   /* Get the command and info block */
187
188   while(fgets(line,MAX_LINE,input)!=NULL)
189     {
190       int version;
191       char commandstr[7];
192       char optionstr[110];
193       char hash;
194
195       if(line[0]=='\n')
196         break;
197
198       if(sscanf(line,"%c",&hash)==1 && hash=='#')
199         continue;
200
201       if(sscanf(line,"COMMAND %6s\n",commandstr)==1)
202         {
203           commandstr[6]='\0';
204
205           if(strcasecmp(commandstr,"get")==0)
206             action=GET;
207
208           continue;
209         }
210
211       if(sscanf(line,"SCHEME %79s\n",scheme)==1)
212         {
213           scheme[79]='\0';
214           continue;
215         }
216
217       if(sscanf(line,"HOST %79s\n",host)==1)
218         {
219           host[79]='\0';
220           continue;
221         }
222
223       if(sscanf(line,"PORT %9s\n",port)==1)
224         {
225           port[9]='\0';
226           continue;
227         }
228
229       if(sscanf(line,"PATH %1023s\n",path)==1)
230         {
231           path[1023]='\0';
232           continue;
233         }
234
235       if(sscanf(line,"VERSION %d\n",&version)==1)
236         {
237           if(version!=KEYSERVER_PROTO_VERSION)
238             {
239               ret=KEYSERVER_VERSION_ERROR;
240               goto fail;
241             }
242
243           continue;
244         }
245
246       if(sscanf(line,"OPTION %109s\n",optionstr)==1)
247         {
248           int no=0;
249           char *start=&optionstr[0];
250
251           optionstr[109]='\0';
252
253           if(strncasecmp(optionstr,"no-",3)==0)
254             {
255               no=1;
256               start=&optionstr[3];
257             }
258
259           if(strcasecmp(start,"verbose")==0)
260             {
261               if(no)
262                 verbose--;
263               else
264                 verbose++;
265             }
266           else if(strncasecmp(start,"http-proxy",10)==0)
267             {
268               if(no)
269                 proxy[0]='\0';
270               else if(start[10]=='=')
271                 {
272                   strncpy(proxy,&start[11],79);
273                   proxy[79]='\0';
274                 }
275               else if(start[10]=='\0')
276                 {
277                   char *http_proxy=getenv(HTTP_PROXY_ENV);
278                   if(http_proxy)
279                     {
280                       strncpy(proxy,http_proxy,79);
281                       proxy[79]='\0';
282                     }
283                 }
284             }
285           else if(strcasecmp(start,"broken-http-proxy")==0)
286             {
287               if(no)
288                 http_flags&=~HTTP_FLAG_NO_SHUTDOWN;
289               else
290                 http_flags|=HTTP_FLAG_NO_SHUTDOWN;
291             }
292           else if(strcasecmp(start,"try-dns-srv")==0)
293             {
294               if(no)
295                 http_flags&=~HTTP_FLAG_TRY_SRV;
296               else
297                 http_flags|=HTTP_FLAG_TRY_SRV;
298             }
299
300           continue;
301         }
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   /* Send the response */
349
350   fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
351   fprintf(output,"PROGRAM %s\n\n",VERSION);
352
353   if(verbose>1)
354     {
355       fprintf(console,"Scheme:\t\t%s\n",scheme);
356       fprintf(console,"Host:\t\t%s\n",host);
357       if(port[0])
358         fprintf(console,"Port:\t\t%s\n",port);
359       if(path[0])
360         fprintf(console,"Path:\t\t%s\n",path);
361       fprintf(console,"Command:\tGET\n");
362     }
363
364   if(get_key(thekey)!=KEYSERVER_OK)
365     failed++;
366
367   if(!failed)
368     ret=KEYSERVER_OK;
369
370  fail:
371
372   free(thekey);
373
374   if(input!=stdin)
375     fclose(input);
376
377   if(output!=stdout)
378     fclose(output);
379
380   return ret;
381 }