* gpg.texi (GPG Esoteric Options): Tweak mention of Tempest font to
[gnupg.git] / keyserver / gpgkeys_curl.c
blobf14b4c9a77497fee27e0b85c8b426cd28b36fceb
1 /* gpgkeys_curl.c - fetch a key via libcurl
2 * Copyright (C) 2004, 2005, 2006, 2007 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\thelp\n");
106 fprintf (fp,"-V\tversion\n");
107 fprintf (fp,"-o\toutput to this file\n");
111 main(int argc,char *argv[])
113 int arg,ret=KEYSERVER_INTERNAL_ERROR,i;
114 char line[MAX_LINE];
115 char *thekey=NULL;
116 long follow_redirects=5;
117 char *proxy=NULL;
118 curl_version_info_data *curldata;
120 console=stderr;
122 /* Kludge to implement standard GNU options. */
123 if (argc > 1 && !strcmp (argv[1], "--version"))
125 fputs ("gpgkeys_curl (GnuPG) " VERSION"\n", stdout);
126 return 0;
128 else if (argc > 1 && !strcmp (argv[1], "--help"))
130 show_help (stdout);
131 return 0;
134 while((arg=getopt(argc,argv,"hVo:"))!=-1)
135 switch(arg)
137 default:
138 case 'h':
139 show_help (console);
140 return KEYSERVER_OK;
142 case 'V':
143 fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
144 return KEYSERVER_OK;
146 case 'o':
147 output=fopen(optarg,"wb");
148 if(output==NULL)
150 fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
151 optarg,strerror(errno));
152 return KEYSERVER_INTERNAL_ERROR;
155 break;
158 if(argc>optind)
160 input=fopen(argv[optind],"r");
161 if(input==NULL)
163 fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
164 argv[optind],strerror(errno));
165 return KEYSERVER_INTERNAL_ERROR;
169 if(input==NULL)
170 input=stdin;
172 if(output==NULL)
173 output=stdout;
175 opt=init_ks_options();
176 if(!opt)
177 return KEYSERVER_NO_MEMORY;
179 /* Get the command and info block */
181 while(fgets(line,MAX_LINE,input)!=NULL)
183 int err;
184 char option[MAX_OPTION+1];
186 if(line[0]=='\n')
187 break;
189 err=parse_ks_options(line,opt);
190 if(err>0)
192 ret=err;
193 goto fail;
195 else if(err==0)
196 continue;
198 if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1)
200 int no=0;
201 char *start=&option[0];
203 option[MAX_OPTION]='\0';
205 if(strncasecmp(option,"no-",3)==0)
207 no=1;
208 start=&option[3];
211 if(strncasecmp(start,"http-proxy",10)==0)
213 /* Safe to not check the return code of strdup() here.
214 If it fails, we simply won't use a proxy. */
215 if(no)
217 free(proxy);
218 proxy=strdup("");
220 else if(start[10]=='=')
222 if(strlen(&start[11])<MAX_PROXY)
224 free(proxy);
225 proxy=strdup(&start[11]);
229 else if(strncasecmp(start,"follow-redirects",16)==0)
231 if(no)
232 follow_redirects=0;
233 else if(start[16]=='=')
234 follow_redirects=atoi(&start[17]);
235 else if(start[16]=='\0')
236 follow_redirects=-1;
239 continue;
243 if(!opt->scheme)
245 fprintf(console,"gpgkeys: no scheme supplied!\n");
246 ret=KEYSERVER_SCHEME_NOT_FOUND;
247 goto fail;
250 if(!opt->host)
252 fprintf(console,"gpgkeys: no keyserver host provided\n");
253 goto fail;
256 if(opt->timeout && register_timeout()==-1)
258 fprintf(console,"gpgkeys: unable to register timeout handler\n");
259 return KEYSERVER_INTERNAL_ERROR;
262 curl_global_init(CURL_GLOBAL_DEFAULT);
264 curl=curl_easy_init();
265 if(!curl)
267 fprintf(console,"gpgkeys: unable to initialize curl\n");
268 ret=KEYSERVER_INTERNAL_ERROR;
269 goto fail;
272 /* Make sure we have the protocol the user is asking for so we can
273 print a nicer error message. */
274 curldata=curl_version_info(CURLVERSION_NOW);
275 for(i=0;curldata->protocols[i];i++)
276 if(strcasecmp(curldata->protocols[i],opt->scheme)==0)
277 break;
279 if(curldata->protocols[i]==NULL)
281 fprintf(console,"gpgkeys: protocol `%s' not supported\n",opt->scheme);
282 ret=KEYSERVER_SCHEME_NOT_FOUND;
283 goto fail;
286 if(follow_redirects)
288 curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
289 if(follow_redirects>0)
290 curl_easy_setopt(curl,CURLOPT_MAXREDIRS,follow_redirects);
293 if(opt->auth)
294 curl_easy_setopt(curl,CURLOPT_USERPWD,opt->auth);
296 if(opt->debug)
298 fprintf(console,"gpgkeys: curl version = %s\n",curl_version());
299 curl_easy_setopt(curl,CURLOPT_STDERR,console);
300 curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
303 curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,opt->flags.check_cert);
304 curl_easy_setopt(curl,CURLOPT_CAINFO,opt->ca_cert_file);
306 if(proxy)
307 curl_easy_setopt(curl,CURLOPT_PROXY,proxy);
309 /* If it's a GET or a SEARCH, the next thing to come in is the
310 keyids. If it's a SEND, then there are no keyids. */
312 if(opt->action==KS_GET)
314 /* Eat the rest of the file */
315 for(;;)
317 if(fgets(line,MAX_LINE,input)==NULL)
318 break;
319 else
321 if(line[0]=='\n' || line[0]=='\0')
322 break;
324 if(!thekey)
326 thekey=strdup(line);
327 if(!thekey)
329 fprintf(console,"gpgkeys: out of memory while "
330 "building key list\n");
331 ret=KEYSERVER_NO_MEMORY;
332 goto fail;
335 /* Trim the trailing \n */
336 thekey[strlen(line)-1]='\0';
341 else
343 fprintf(console,
344 "gpgkeys: this keyserver type only supports key retrieval\n");
345 goto fail;
348 if(!thekey)
350 fprintf(console,"gpgkeys: invalid keyserver instructions\n");
351 goto fail;
354 /* Send the response */
356 fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
357 fprintf(output,"PROGRAM %s\n\n",VERSION);
359 if(opt->verbose)
361 fprintf(console,"Scheme:\t\t%s\n",opt->scheme);
362 fprintf(console,"Host:\t\t%s\n",opt->host);
363 if(opt->port)
364 fprintf(console,"Port:\t\t%s\n",opt->port);
365 if(opt->path)
366 fprintf(console,"Path:\t\t%s\n",opt->path);
367 fprintf(console,"Command:\tGET\n");
370 set_timeout(opt->timeout);
372 ret=get_key(thekey);
374 fail:
376 free(thekey);
378 if(input!=stdin)
379 fclose(input);
381 if(output!=stdout)
382 fclose(output);
384 free_ks_options(opt);
386 if(curl)
387 curl_easy_cleanup(curl);
389 free(proxy);
391 curl_global_cleanup();
393 return ret;