Remove testing cruft.
[gnupg.git] / keyserver / gpgkeys_curl.c
blob6183556e726d9d2c19abbfaa31da7f4f1c57ddf0
1 /* gpgkeys_curl.c - fetch a key via libcurl
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 * In addition, as a special exception, the Free Software Foundation
20 * gives permission to link the code of the keyserver helper tools:
21 * gpgkeys_ldap, gpgkeys_curl and gpgkeys_hkp with the OpenSSL
22 * project's "OpenSSL" library (or with modified versions of it that
23 * use the same license as the "OpenSSL" library), and distribute the
24 * linked executables. You must obey the GNU General Public License
25 * in all respects for all of the code used other than "OpenSSL". If
26 * you modify this file, you may extend this exception to your version
27 * of the file, but you are not obligated to do so. If you do not
28 * wish to do so, delete this exception statement from your version.
31 #include <config.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #ifdef HAVE_GETOPT_H
38 #include <getopt.h>
39 #endif
40 #ifdef HAVE_LIBCURL
41 #include <curl/curl.h>
42 #else
43 #include "curl-shim.h"
44 #endif
45 #include "keyserver.h"
46 #include "ksutil.h"
48 extern char *optarg;
49 extern int optind;
51 static FILE *input,*output,*console;
52 static CURL *curl;
53 static struct ks_options *opt;
55 static int
56 get_key(char *getkey)
58 CURLcode res;
59 char errorbuffer[CURL_ERROR_SIZE];
60 char request[MAX_URL];
61 struct curl_writer_ctx ctx;
63 memset(&ctx,0,sizeof(ctx));
65 if(strncmp(getkey,"0x",2)==0)
66 getkey+=2;
68 fprintf(output,"KEY 0x%s BEGIN\n",getkey);
70 sprintf(request,"%s://%s%s%s%s",opt->scheme,opt->host,
71 opt->port?":":"",opt->port?opt->port:"",opt->path?opt->path:"/");
73 curl_easy_setopt(curl,CURLOPT_URL,request);
74 curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curl_writer);
75 ctx.stream=output;
76 curl_easy_setopt(curl,CURLOPT_FILE,&ctx);
77 curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errorbuffer);
79 res=curl_easy_perform(curl);
80 if(res!=CURLE_OK)
82 fprintf(console,"gpgkeys: %s fetch error %d: %s\n",opt->scheme,
83 res,errorbuffer);
84 fprintf(output,"\nKEY 0x%s FAILED %d\n",getkey,curl_err_to_gpg_err(res));
86 else
88 curl_writer_finalize(&ctx);
89 if(!ctx.flags.done)
91 fprintf(console,"gpgkeys: no key data found for %s\n",request);
92 fprintf(output,"\nKEY 0x%s FAILED %d\n",
93 getkey,KEYSERVER_KEY_NOT_FOUND);
95 else
96 fprintf(output,"\nKEY 0x%s END\n",getkey);
99 return curl_err_to_gpg_err(res);
102 static void
103 show_help (FILE *fp)
105 fprintf (fp,"-h, --help\thelp\n");
106 fprintf (fp,"-V\t\tmachine readable version\n");
107 fprintf (fp,"--version\thuman readable version\n");
108 fprintf (fp,"-o\t\toutput to this file\n");
112 main(int argc,char *argv[])
114 int arg,ret=KEYSERVER_INTERNAL_ERROR,i;
115 char line[MAX_LINE];
116 char *thekey=NULL;
117 long follow_redirects=5;
118 char *proxy=NULL;
119 curl_version_info_data *curldata;
121 console=stderr;
123 /* Kludge to implement standard GNU options. */
124 if (argc > 1 && !strcmp (argv[1], "--version"))
126 printf ("gpgkeys_curl (GnuPG) %s\n", VERSION);
127 printf ("Uses: %s\n", curl_version());
128 return 0;
130 else if (argc > 1 && !strcmp (argv[1], "--help"))
132 show_help (stdout);
133 return 0;
136 while((arg=getopt(argc,argv,"hVo:"))!=-1)
137 switch(arg)
139 default:
140 case 'h':
141 show_help (console);
142 return KEYSERVER_OK;
144 case 'V':
145 fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
146 return KEYSERVER_OK;
148 case 'o':
149 output=fopen(optarg,"wb");
150 if(output==NULL)
152 fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
153 optarg,strerror(errno));
154 return KEYSERVER_INTERNAL_ERROR;
157 break;
160 if(argc>optind)
162 input=fopen(argv[optind],"r");
163 if(input==NULL)
165 fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
166 argv[optind],strerror(errno));
167 return KEYSERVER_INTERNAL_ERROR;
171 if(input==NULL)
172 input=stdin;
174 if(output==NULL)
175 output=stdout;
177 opt=init_ks_options();
178 if(!opt)
179 return KEYSERVER_NO_MEMORY;
181 /* Get the command and info block */
183 while(fgets(line,MAX_LINE,input)!=NULL)
185 int err;
186 char option[MAX_OPTION+1];
188 if(line[0]=='\n')
189 break;
191 err=parse_ks_options(line,opt);
192 if(err>0)
194 ret=err;
195 goto fail;
197 else if(err==0)
198 continue;
200 if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1)
202 int no=0;
203 char *start=&option[0];
205 option[MAX_OPTION]='\0';
207 if(strncasecmp(option,"no-",3)==0)
209 no=1;
210 start=&option[3];
213 if(strncasecmp(start,"http-proxy",10)==0)
215 /* Safe to not check the return code of strdup() here.
216 If it fails, we simply won't use a proxy. */
217 if(no)
219 free(proxy);
220 proxy=strdup("");
222 else if(start[10]=='=')
224 if(strlen(&start[11])<MAX_PROXY)
226 free(proxy);
227 proxy=strdup(&start[11]);
231 else if(strncasecmp(start,"follow-redirects",16)==0)
233 if(no)
234 follow_redirects=0;
235 else if(start[16]=='=')
236 follow_redirects=atoi(&start[17]);
237 else if(start[16]=='\0')
238 follow_redirects=-1;
241 continue;
245 if(!opt->scheme)
247 fprintf(console,"gpgkeys: no scheme supplied!\n");
248 ret=KEYSERVER_SCHEME_NOT_FOUND;
249 goto fail;
252 if(!opt->host)
254 fprintf(console,"gpgkeys: no keyserver host provided\n");
255 goto fail;
258 if(opt->timeout && register_timeout()==-1)
260 fprintf(console,"gpgkeys: unable to register timeout handler\n");
261 return KEYSERVER_INTERNAL_ERROR;
264 curl_global_init(CURL_GLOBAL_DEFAULT);
266 curl=curl_easy_init();
267 if(!curl)
269 fprintf(console,"gpgkeys: unable to initialize curl\n");
270 ret=KEYSERVER_INTERNAL_ERROR;
271 goto fail;
274 /* Make sure we have the protocol the user is asking for so we can
275 print a nicer error message. */
276 curldata=curl_version_info(CURLVERSION_NOW);
277 for(i=0;curldata->protocols[i];i++)
278 if(strcasecmp(curldata->protocols[i],opt->scheme)==0)
279 break;
281 if(curldata->protocols[i]==NULL)
283 fprintf(console,"gpgkeys: protocol `%s' not supported\n",opt->scheme);
284 ret=KEYSERVER_SCHEME_NOT_FOUND;
285 goto fail;
288 if(follow_redirects)
290 curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
291 if(follow_redirects>0)
292 curl_easy_setopt(curl,CURLOPT_MAXREDIRS,follow_redirects);
295 if(opt->auth)
296 curl_easy_setopt(curl,CURLOPT_USERPWD,opt->auth);
298 if(opt->debug)
300 fprintf(console,"gpgkeys: curl version = %s\n",curl_version());
301 curl_easy_setopt(curl,CURLOPT_STDERR,console);
302 curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);
305 curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,(long)opt->flags.check_cert);
306 curl_easy_setopt(curl,CURLOPT_CAINFO,opt->ca_cert_file);
308 if(proxy)
309 curl_easy_setopt(curl,CURLOPT_PROXY,proxy);
311 /* If it's a GET or a SEARCH, the next thing to come in is the
312 keyids. If it's a SEND, then there are no keyids. */
314 if(opt->action==KS_GET)
316 /* Eat the rest of the file */
317 for(;;)
319 if(fgets(line,MAX_LINE,input)==NULL)
320 break;
321 else
323 if(line[0]=='\n' || line[0]=='\0')
324 break;
326 if(!thekey)
328 thekey=strdup(line);
329 if(!thekey)
331 fprintf(console,"gpgkeys: out of memory while "
332 "building key list\n");
333 ret=KEYSERVER_NO_MEMORY;
334 goto fail;
337 /* Trim the trailing \n */
338 thekey[strlen(line)-1]='\0';
343 else
345 fprintf(console,
346 "gpgkeys: this keyserver type only supports key retrieval\n");
347 goto fail;
350 if(!thekey)
352 fprintf(console,"gpgkeys: invalid keyserver instructions\n");
353 goto fail;
356 /* Send the response */
358 fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
359 fprintf(output,"PROGRAM %s\n\n",VERSION);
361 if(opt->verbose)
363 fprintf(console,"Scheme:\t\t%s\n",opt->scheme);
364 fprintf(console,"Host:\t\t%s\n",opt->host);
365 if(opt->port)
366 fprintf(console,"Port:\t\t%s\n",opt->port);
367 if(opt->path)
368 fprintf(console,"Path:\t\t%s\n",opt->path);
369 fprintf(console,"Command:\tGET\n");
372 set_timeout(opt->timeout);
374 ret=get_key(thekey);
376 fail:
378 free(thekey);
380 if(input!=stdin)
381 fclose(input);
383 if(output!=stdout)
384 fclose(output);
386 free_ks_options(opt);
388 if(curl)
389 curl_easy_cleanup(curl);
391 free(proxy);
393 curl_global_cleanup();
395 return ret;