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