* gpgkeys_curl.c (get_key, main): Don't try and be smart about what
[gnupg.git] / keyserver / gpgkeys_curl.c
1 /* gpgkeys_curl.c - fetch a key via libcurl
2  * Copyright (C) 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19  * USA.
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #ifdef HAVE_GETOPT_H
29 #include <getopt.h>
30 #endif
31 #ifdef FAKE_CURL
32 #include "curl-shim.h"
33 #else
34 #include <curl/curl.h>
35 #endif
36 #include "keyserver.h"
37 #include "ksutil.h"
38
39 extern char *optarg;
40 extern int optind;
41
42 static FILE *input,*output,*console;
43 static CURL *curl;
44 static struct ks_options *opt;
45
46 static int
47 get_key(char *getkey)
48 {
49   CURLcode res;
50   char errorbuffer[CURL_ERROR_SIZE];
51   char request[MAX_URL];
52   struct curl_writer_ctx ctx;
53
54   memset(&ctx,0,sizeof(ctx));
55
56   if(strncmp(getkey,"0x",2)==0)
57     getkey+=2;
58
59   fprintf(output,"KEY 0x%s BEGIN\n",getkey);
60
61   sprintf(request,"%s://%s%s%s%s",opt->scheme,opt->host,
62           opt->port?":":"",opt->port?opt->port:"",opt->path?opt->path:"/");
63
64   curl_easy_setopt(curl,CURLOPT_URL,request);
65   curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curl_writer);
66   ctx.stream=output;
67   curl_easy_setopt(curl,CURLOPT_FILE,&ctx);
68   curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errorbuffer);
69
70   res=curl_easy_perform(curl);
71   if(res!=CURLE_OK)
72     {
73       fprintf(console,"gpgkeys: %s fetch error %d: %s\n",opt->scheme,
74               res,errorbuffer);
75       fprintf(output,"\nKEY 0x%s FAILED %d\n",getkey,curl_err_to_gpg_err(res));
76     }
77   else
78     fprintf(output,"\nKEY 0x%s END\n",getkey);
79
80   return curl_err_to_gpg_err(res);
81 }
82
83 static void 
84 show_help (FILE *fp)
85 {
86   fprintf (fp,"-h\thelp\n");
87   fprintf (fp,"-V\tversion\n");
88   fprintf (fp,"-o\toutput to this file\n");
89 }
90
91 int
92 main(int argc,char *argv[])
93 {
94   int arg,ret=KEYSERVER_INTERNAL_ERROR;
95   char line[MAX_LINE];
96   char *thekey=NULL;
97   long follow_redirects=5;
98   char *proxy=NULL;
99
100   console=stderr;
101
102   /* Kludge to implement standard GNU options.  */
103   if (argc > 1 && !strcmp (argv[1], "--version"))
104     {
105       fputs ("gpgkeys_curl (GnuPG) " VERSION"\n", stdout);
106       return 0;
107     }
108   else if (argc > 1 && !strcmp (argv[1], "--help"))
109     {
110       show_help (stdout);
111       return 0;
112     }
113
114   while((arg=getopt(argc,argv,"hVo:"))!=-1)
115     switch(arg)
116       {
117       default:
118       case 'h':
119         show_help (console);
120         return KEYSERVER_OK;
121
122       case 'V':
123         fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
124         return KEYSERVER_OK;
125
126       case 'o':
127         output=fopen(optarg,"wb");
128         if(output==NULL)
129           {
130             fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
131                     optarg,strerror(errno));
132             return KEYSERVER_INTERNAL_ERROR;
133           }
134
135         break;
136       }
137
138   if(argc>optind)
139     {
140       input=fopen(argv[optind],"r");
141       if(input==NULL)
142         {
143           fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
144                   argv[optind],strerror(errno));
145           return KEYSERVER_INTERNAL_ERROR;
146         }
147     }
148
149   if(input==NULL)
150     input=stdin;
151
152   if(output==NULL)
153     output=stdout;
154
155   opt=init_ks_options();
156   if(!opt)
157     return KEYSERVER_NO_MEMORY;
158
159   /* Get the command and info block */
160
161   while(fgets(line,MAX_LINE,input)!=NULL)
162     {
163       int err;
164       char option[MAX_OPTION+1];
165
166       if(line[0]=='\n')
167         break;
168
169       err=parse_ks_options(line,opt);
170       if(err>0)
171         {
172           ret=err;
173           goto fail;
174         }
175       else if(err==0)
176         continue;
177
178       if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1)
179         {
180           int no=0;
181           char *start=&option[0];
182
183           option[MAX_OPTION]='\0';
184
185           if(strncasecmp(option,"no-",3)==0)
186             {
187               no=1;
188               start=&option[3];
189             }
190
191           if(strncasecmp(start,"http-proxy",10)==0)
192             {
193               /* Safe to not check the return code of strdup() here.
194                  If it fails, we simply won't use a proxy. */
195               if(no)
196                 {
197                   free(proxy);
198                   proxy=strdup("");
199                 }
200               else if(start[10]=='=')
201                 {
202                   if(strlen(&start[11])<MAX_PROXY)
203                     {
204                       free(proxy);
205                       proxy=strdup(&start[11]);
206                     }
207                 }
208             }
209           else if(strncasecmp(start,"follow-redirects",16)==0)
210             {
211               if(no)
212                 follow_redirects=0;
213               else if(start[16]=='=')
214                 follow_redirects=atoi(&start[17]);
215               else if(start[16]=='\0')
216                 follow_redirects=-1;
217             }
218
219           continue;
220         }
221     }
222
223   if(!opt->scheme)
224     {
225       fprintf(console,"gpgkeys: no scheme supplied!\n");
226       ret=KEYSERVER_SCHEME_NOT_FOUND;
227       goto fail;
228     }
229
230   if(!opt->host)
231     {
232       fprintf(console,"gpgkeys: no keyserver host provided\n");
233       goto fail;
234     }
235
236   if(opt->timeout && register_timeout()==-1)
237     {
238       fprintf(console,"gpgkeys: unable to register timeout handler\n");
239       return KEYSERVER_INTERNAL_ERROR;
240     }
241
242   curl_global_init(CURL_GLOBAL_DEFAULT);
243   curl=curl_easy_init();
244   if(!curl)
245     {
246       fprintf(console,"gpgkeys: unable to initialize curl\n");
247       ret=KEYSERVER_INTERNAL_ERROR;
248       goto fail;
249     }
250
251   if(follow_redirects)
252     {
253       curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
254       if(follow_redirects>0)
255         curl_easy_setopt(curl,CURLOPT_MAXREDIRS,follow_redirects);
256     }
257
258   if(opt->auth)
259     curl_easy_setopt(curl,CURLOPT_USERPWD,opt->auth);
260
261   if(opt->debug)
262     {
263       curl_easy_setopt(curl,CURLOPT_STDERR,console);
264       curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
265     }
266
267   curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,opt->flags.check_cert);
268   curl_easy_setopt(curl,CURLOPT_CAINFO,opt->ca_cert_file);
269
270   if(proxy)
271     curl_easy_setopt(curl,CURLOPT_PROXY,proxy);
272
273   /* If it's a GET or a SEARCH, the next thing to come in is the
274      keyids.  If it's a SEND, then there are no keyids. */
275
276   if(opt->action==KS_GET)
277     {
278       /* Eat the rest of the file */
279       for(;;)
280         {
281           if(fgets(line,MAX_LINE,input)==NULL)
282             break;
283           else
284             {
285               if(line[0]=='\n' || line[0]=='\0')
286                 break;
287
288               if(!thekey)
289                 {
290                   thekey=strdup(line);
291                   if(!thekey)
292                     {
293                       fprintf(console,"gpgkeys: out of memory while "
294                               "building key list\n");
295                       ret=KEYSERVER_NO_MEMORY;
296                       goto fail;
297                     }
298
299                   /* Trim the trailing \n */
300                   thekey[strlen(line)-1]='\0';
301                 }
302             }
303         }
304     }
305   else
306     {
307       fprintf(console,
308               "gpgkeys: this keyserver type only supports key retrieval\n");
309       goto fail;
310     }
311
312   if(!thekey)
313     {
314       fprintf(console,"gpgkeys: invalid keyserver instructions\n");
315       goto fail;
316     }
317
318   /* Send the response */
319
320   fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
321   fprintf(output,"PROGRAM %s\n\n",VERSION);
322
323   if(opt->verbose)
324     {
325       fprintf(console,"Scheme:\t\t%s\n",opt->scheme);
326       fprintf(console,"Host:\t\t%s\n",opt->host);
327       if(opt->port)
328         fprintf(console,"Port:\t\t%s\n",opt->port);
329       if(opt->path)
330         fprintf(console,"Path:\t\t%s\n",opt->path);
331       fprintf(console,"Command:\tGET\n");
332     }
333
334   set_timeout(opt->timeout);
335
336   ret=get_key(thekey);
337
338  fail:
339
340   free(thekey);
341
342   if(input!=stdin)
343     fclose(input);
344
345   if(output!=stdout)
346     fclose(output);
347
348   free_ks_options(opt);
349
350   if(curl)
351     curl_easy_cleanup(curl);
352
353   free(proxy);
354
355   curl_global_cleanup();
356
357   return ret;
358 }