* gpgkeys_finger.c (get_key), gpgkeys_curl.c (get_key): Better language
[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 if(!ctx.done)
78     {
79       fprintf(console,"gpgkeys: no key data found for %s\n",request);
80       fprintf(output,"\nKEY 0x%s FAILED %d\n",getkey,KEYSERVER_KEY_NOT_FOUND);
81     }
82   else
83     fprintf(output,"\nKEY 0x%s END\n",getkey);
84
85   return curl_err_to_gpg_err(res);
86 }
87
88 static void 
89 show_help (FILE *fp)
90 {
91   fprintf (fp,"-h\thelp\n");
92   fprintf (fp,"-V\tversion\n");
93   fprintf (fp,"-o\toutput to this file\n");
94 }
95
96 int
97 main(int argc,char *argv[])
98 {
99   int arg,ret=KEYSERVER_INTERNAL_ERROR;
100   char line[MAX_LINE];
101   char *thekey=NULL;
102   long follow_redirects=5;
103   char *proxy=NULL;
104
105   console=stderr;
106
107   /* Kludge to implement standard GNU options.  */
108   if (argc > 1 && !strcmp (argv[1], "--version"))
109     {
110       fputs ("gpgkeys_curl (GnuPG) " VERSION"\n", stdout);
111       return 0;
112     }
113   else if (argc > 1 && !strcmp (argv[1], "--help"))
114     {
115       show_help (stdout);
116       return 0;
117     }
118
119   while((arg=getopt(argc,argv,"hVo:"))!=-1)
120     switch(arg)
121       {
122       default:
123       case 'h':
124         show_help (console);
125         return KEYSERVER_OK;
126
127       case 'V':
128         fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
129         return KEYSERVER_OK;
130
131       case 'o':
132         output=fopen(optarg,"wb");
133         if(output==NULL)
134           {
135             fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
136                     optarg,strerror(errno));
137             return KEYSERVER_INTERNAL_ERROR;
138           }
139
140         break;
141       }
142
143   if(argc>optind)
144     {
145       input=fopen(argv[optind],"r");
146       if(input==NULL)
147         {
148           fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
149                   argv[optind],strerror(errno));
150           return KEYSERVER_INTERNAL_ERROR;
151         }
152     }
153
154   if(input==NULL)
155     input=stdin;
156
157   if(output==NULL)
158     output=stdout;
159
160   opt=init_ks_options();
161   if(!opt)
162     return KEYSERVER_NO_MEMORY;
163
164   /* Get the command and info block */
165
166   while(fgets(line,MAX_LINE,input)!=NULL)
167     {
168       int err;
169       char option[MAX_OPTION+1];
170
171       if(line[0]=='\n')
172         break;
173
174       err=parse_ks_options(line,opt);
175       if(err>0)
176         {
177           ret=err;
178           goto fail;
179         }
180       else if(err==0)
181         continue;
182
183       if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1)
184         {
185           int no=0;
186           char *start=&option[0];
187
188           option[MAX_OPTION]='\0';
189
190           if(strncasecmp(option,"no-",3)==0)
191             {
192               no=1;
193               start=&option[3];
194             }
195
196           if(strncasecmp(start,"http-proxy",10)==0)
197             {
198               /* Safe to not check the return code of strdup() here.
199                  If it fails, we simply won't use a proxy. */
200               if(no)
201                 {
202                   free(proxy);
203                   proxy=strdup("");
204                 }
205               else if(start[10]=='=')
206                 {
207                   if(strlen(&start[11])<MAX_PROXY)
208                     {
209                       free(proxy);
210                       proxy=strdup(&start[11]);
211                     }
212                 }
213             }
214           else if(strncasecmp(start,"follow-redirects",16)==0)
215             {
216               if(no)
217                 follow_redirects=0;
218               else if(start[16]=='=')
219                 follow_redirects=atoi(&start[17]);
220               else if(start[16]=='\0')
221                 follow_redirects=-1;
222             }
223
224           continue;
225         }
226     }
227
228   if(!opt->scheme)
229     {
230       fprintf(console,"gpgkeys: no scheme supplied!\n");
231       ret=KEYSERVER_SCHEME_NOT_FOUND;
232       goto fail;
233     }
234
235   if(!opt->host)
236     {
237       fprintf(console,"gpgkeys: no keyserver host provided\n");
238       goto fail;
239     }
240
241   if(opt->timeout && register_timeout()==-1)
242     {
243       fprintf(console,"gpgkeys: unable to register timeout handler\n");
244       return KEYSERVER_INTERNAL_ERROR;
245     }
246
247   curl_global_init(CURL_GLOBAL_DEFAULT);
248   curl=curl_easy_init();
249   if(!curl)
250     {
251       fprintf(console,"gpgkeys: unable to initialize curl\n");
252       ret=KEYSERVER_INTERNAL_ERROR;
253       goto fail;
254     }
255
256   if(follow_redirects)
257     {
258       curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
259       if(follow_redirects>0)
260         curl_easy_setopt(curl,CURLOPT_MAXREDIRS,follow_redirects);
261     }
262
263   if(opt->auth)
264     curl_easy_setopt(curl,CURLOPT_USERPWD,opt->auth);
265
266   if(opt->debug)
267     {
268       fprintf(console,"gpgkeys: curl version = %s\n",curl_version());
269       curl_easy_setopt(curl,CURLOPT_STDERR,console);
270       curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
271     }
272
273   curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,opt->flags.check_cert);
274   curl_easy_setopt(curl,CURLOPT_CAINFO,opt->ca_cert_file);
275
276   if(proxy)
277     curl_easy_setopt(curl,CURLOPT_PROXY,proxy);
278
279   /* If it's a GET or a SEARCH, the next thing to come in is the
280      keyids.  If it's a SEND, then there are no keyids. */
281
282   if(opt->action==KS_GET)
283     {
284       /* Eat the rest of the file */
285       for(;;)
286         {
287           if(fgets(line,MAX_LINE,input)==NULL)
288             break;
289           else
290             {
291               if(line[0]=='\n' || line[0]=='\0')
292                 break;
293
294               if(!thekey)
295                 {
296                   thekey=strdup(line);
297                   if(!thekey)
298                     {
299                       fprintf(console,"gpgkeys: out of memory while "
300                               "building key list\n");
301                       ret=KEYSERVER_NO_MEMORY;
302                       goto fail;
303                     }
304
305                   /* Trim the trailing \n */
306                   thekey[strlen(line)-1]='\0';
307                 }
308             }
309         }
310     }
311   else
312     {
313       fprintf(console,
314               "gpgkeys: this keyserver type only supports key retrieval\n");
315       goto fail;
316     }
317
318   if(!thekey)
319     {
320       fprintf(console,"gpgkeys: invalid keyserver instructions\n");
321       goto fail;
322     }
323
324   /* Send the response */
325
326   fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
327   fprintf(output,"PROGRAM %s\n\n",VERSION);
328
329   if(opt->verbose)
330     {
331       fprintf(console,"Scheme:\t\t%s\n",opt->scheme);
332       fprintf(console,"Host:\t\t%s\n",opt->host);
333       if(opt->port)
334         fprintf(console,"Port:\t\t%s\n",opt->port);
335       if(opt->path)
336         fprintf(console,"Path:\t\t%s\n",opt->path);
337       fprintf(console,"Command:\tGET\n");
338     }
339
340   set_timeout(opt->timeout);
341
342   ret=get_key(thekey);
343
344  fail:
345
346   free(thekey);
347
348   if(input!=stdin)
349     fclose(input);
350
351   if(output!=stdout)
352     fclose(output);
353
354   free_ks_options(opt);
355
356   if(curl)
357     curl_easy_cleanup(curl);
358
359   free(proxy);
360
361   curl_global_cleanup();
362
363   return ret;
364 }