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