1 /* gpgkeys_curl.c - fetch a key via libcurl
2 * Copyright (C) 2004, 2005, 2006 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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * In addition, as a special exception, the Free Software Foundation
22 * gives permission to link the code of the keyserver helper tools:
23 * gpgkeys_ldap, gpgkeys_curl and gpgkeys_hkp with the OpenSSL
24 * project's "OpenSSL" library (or with modified versions of it that
25 * use the same license as the "OpenSSL" library), and distribute the
26 * linked executables. You must obey the GNU General Public License
27 * in all respects for all of the code used other than "OpenSSL". If
28 * you modify this file, you may extend this exception to your version
29 * of the file, but you are not obligated to do so. If you do not
30 * wish to do so, delete this exception statement from your version.
43 #include <curl/curl.h>
45 #include "curl-shim.h"
47 #include "keyserver.h"
53 static FILE *input
,*output
,*console
;
55 static struct ks_options
*opt
;
61 char errorbuffer
[CURL_ERROR_SIZE
];
62 char request
[MAX_URL
];
63 struct curl_writer_ctx ctx
;
65 memset(&ctx
,0,sizeof(ctx
));
67 if(strncmp(getkey
,"0x",2)==0)
70 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
72 sprintf(request
,"%s://%s%s%s%s",opt
->scheme
,opt
->host
,
73 opt
->port
?":":"",opt
->port
?opt
->port
:"",opt
->path
?opt
->path
:"/");
75 curl_easy_setopt(curl
,CURLOPT_URL
,request
);
76 curl_easy_setopt(curl
,CURLOPT_WRITEFUNCTION
,curl_writer
);
78 curl_easy_setopt(curl
,CURLOPT_FILE
,&ctx
);
79 curl_easy_setopt(curl
,CURLOPT_ERRORBUFFER
,errorbuffer
);
81 res
=curl_easy_perform(curl
);
84 fprintf(console
,"gpgkeys: %s fetch error %d: %s\n",opt
->scheme
,
86 fprintf(output
,"\nKEY 0x%s FAILED %d\n",getkey
,curl_err_to_gpg_err(res
));
90 curl_writer_finalize(&ctx
);
93 fprintf(console
,"gpgkeys: no key data found for %s\n",request
);
94 fprintf(output
,"\nKEY 0x%s FAILED %d\n",
95 getkey
,KEYSERVER_KEY_NOT_FOUND
);
98 fprintf(output
,"\nKEY 0x%s END\n",getkey
);
101 return curl_err_to_gpg_err(res
);
107 fprintf (fp
,"-h\thelp\n");
108 fprintf (fp
,"-V\tversion\n");
109 fprintf (fp
,"-o\toutput to this file\n");
113 main(int argc
,char *argv
[])
115 int arg
,ret
=KEYSERVER_INTERNAL_ERROR
;
118 long follow_redirects
=5;
123 /* Kludge to implement standard GNU options. */
124 if (argc
> 1 && !strcmp (argv
[1], "--version"))
126 fputs ("gpgkeys_curl (GnuPG) " VERSION
"\n", stdout
);
129 else if (argc
> 1 && !strcmp (argv
[1], "--help"))
135 while((arg
=getopt(argc
,argv
,"hVo:"))!=-1)
144 fprintf(stdout
,"%d\n%s\n",KEYSERVER_PROTO_VERSION
,VERSION
);
148 output
=fopen(optarg
,"wb");
151 fprintf(console
,"gpgkeys: Cannot open output file `%s': %s\n",
152 optarg
,strerror(errno
));
153 return KEYSERVER_INTERNAL_ERROR
;
161 input
=fopen(argv
[optind
],"r");
164 fprintf(console
,"gpgkeys: Cannot open input file `%s': %s\n",
165 argv
[optind
],strerror(errno
));
166 return KEYSERVER_INTERNAL_ERROR
;
176 opt
=init_ks_options();
178 return KEYSERVER_NO_MEMORY
;
180 /* Get the command and info block */
182 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
185 char option
[MAX_OPTION
+1];
190 err
=parse_ks_options(line
,opt
);
199 if(sscanf(line
,"OPTION %" MKSTRING(MAX_OPTION
) "s\n",option
)==1)
202 char *start
=&option
[0];
204 option
[MAX_OPTION
]='\0';
206 if(strncasecmp(option
,"no-",3)==0)
212 if(strncasecmp(start
,"http-proxy",10)==0)
214 /* Safe to not check the return code of strdup() here.
215 If it fails, we simply won't use a proxy. */
221 else if(start
[10]=='=')
223 if(strlen(&start
[11])<MAX_PROXY
)
226 proxy
=strdup(&start
[11]);
230 else if(strncasecmp(start
,"follow-redirects",16)==0)
234 else if(start
[16]=='=')
235 follow_redirects
=atoi(&start
[17]);
236 else if(start
[16]=='\0')
246 fprintf(console
,"gpgkeys: no scheme supplied!\n");
247 ret
=KEYSERVER_SCHEME_NOT_FOUND
;
253 fprintf(console
,"gpgkeys: no keyserver host provided\n");
257 if(opt
->timeout
&& register_timeout()==-1)
259 fprintf(console
,"gpgkeys: unable to register timeout handler\n");
260 return KEYSERVER_INTERNAL_ERROR
;
263 curl_global_init(CURL_GLOBAL_DEFAULT
);
264 curl
=curl_easy_init();
267 fprintf(console
,"gpgkeys: unable to initialize curl\n");
268 ret
=KEYSERVER_INTERNAL_ERROR
;
274 curl_easy_setopt(curl
,CURLOPT_FOLLOWLOCATION
,1);
275 if(follow_redirects
>0)
276 curl_easy_setopt(curl
,CURLOPT_MAXREDIRS
,follow_redirects
);
280 curl_easy_setopt(curl
,CURLOPT_USERPWD
,opt
->auth
);
284 fprintf(console
,"gpgkeys: curl version = %s\n",curl_version());
285 curl_easy_setopt(curl
,CURLOPT_STDERR
,console
);
286 curl_easy_setopt(curl
,CURLOPT_VERBOSE
,1);
289 curl_easy_setopt(curl
,CURLOPT_SSL_VERIFYPEER
,opt
->flags
.check_cert
);
290 curl_easy_setopt(curl
,CURLOPT_CAINFO
,opt
->ca_cert_file
);
293 curl_easy_setopt(curl
,CURLOPT_PROXY
,proxy
);
295 /* If it's a GET or a SEARCH, the next thing to come in is the
296 keyids. If it's a SEND, then there are no keyids. */
298 if(opt
->action
==KS_GET
)
300 /* Eat the rest of the file */
303 if(fgets(line
,MAX_LINE
,input
)==NULL
)
307 if(line
[0]=='\n' || line
[0]=='\0')
315 fprintf(console
,"gpgkeys: out of memory while "
316 "building key list\n");
317 ret
=KEYSERVER_NO_MEMORY
;
321 /* Trim the trailing \n */
322 thekey
[strlen(line
)-1]='\0';
330 "gpgkeys: this keyserver type only supports key retrieval\n");
336 fprintf(console
,"gpgkeys: invalid keyserver instructions\n");
340 /* Send the response */
342 fprintf(output
,"VERSION %d\n",KEYSERVER_PROTO_VERSION
);
343 fprintf(output
,"PROGRAM %s\n\n",VERSION
);
347 fprintf(console
,"Scheme:\t\t%s\n",opt
->scheme
);
348 fprintf(console
,"Host:\t\t%s\n",opt
->host
);
350 fprintf(console
,"Port:\t\t%s\n",opt
->port
);
352 fprintf(console
,"Path:\t\t%s\n",opt
->path
);
353 fprintf(console
,"Command:\tGET\n");
356 set_timeout(opt
->timeout
);
370 free_ks_options(opt
);
373 curl_easy_cleanup(curl
);
377 curl_global_cleanup();