1
/* gpgkeys_curl.c - fetch a key via libcurl
2
* Copyright (C) 2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
30
#include <curl/curl.h>
31
#include "keyserver.h"
46
#define MAX_URL (MAX_SCHEME+1+3+MAX_AUTH+1+1+MAX_HOST+1+1+MAX_PORT+1+1+MAX_PATH+1+50)
48
#define STRINGIFY(x) #x
49
#define MKSTRING(x) STRINGIFY(x)
52
static char scheme[MAX_SCHEME+1],auth[MAX_AUTH+1],host[MAX_HOST+1]={'\0'},port[MAX_PORT+1]={'\0'},path[MAX_PATH+1]={'\0'},proxy[MAX_PROXY+1]={'\0'};
53
static FILE *input=NULL,*output=NULL,*console=NULL;
55
static char request[MAX_URL]={'\0'};
58
curl_err_to_gpg_err(CURLcode error)
62
case CURLE_FTP_COULDNT_RETR_FILE: return KEYSERVER_KEY_NOT_FOUND;
63
default: return KEYSERVER_INTERNAL_ERROR;
67
/* We wrap fwrite so to avoid DLL problems on Win32 (see curl faq for
70
writer(const void *ptr,size_t size,size_t nmemb,void *stream)
72
return fwrite(ptr,size,nmemb,stream);
79
char errorbuffer[CURL_ERROR_SIZE];
81
if(strncmp(getkey,"0x",2)==0)
84
fprintf(output,"KEY 0x%s BEGIN\n",getkey);
86
sprintf(request,"%s://%s%s%s%s%s%s%s",scheme,auth[0]?auth:"",auth[0]?"@":"",
87
host,port[0]?":":"",port[0]?port:"",path[0]?"":"/",path);
89
curl_easy_setopt(curl,CURLOPT_URL,request);
90
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writer);
91
curl_easy_setopt(curl,CURLOPT_FILE,output);
92
curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errorbuffer);
96
curl_easy_setopt(curl,CURLOPT_STDERR,console);
97
curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
100
res=curl_easy_perform(curl);
103
fprintf(console,"gpgkeys: %s fetch error %d: %s\n",scheme,
105
fprintf(output,"KEY 0x%s FAILED %d\n",getkey,curl_err_to_gpg_err(res));
108
fprintf(output,"KEY 0x%s END\n",getkey);
116
fprintf (fp,"-h\thelp\n");
117
fprintf (fp,"-V\tversion\n");
118
fprintf (fp,"-o\toutput to this file\n");
122
main(int argc,char *argv[])
124
int arg,action=-1,ret=KEYSERVER_INTERNAL_ERROR;
127
unsigned int timeout=DEFAULT_KEYSERVER_TIMEOUT;
128
long follow_redirects=5;
132
/* Kludge to implement standard GNU options. */
133
if (argc > 1 && !strcmp (argv[1], "--version"))
135
fputs ("gpgkeys_curl (GnuPG) " VERSION"\n", stdout);
138
else if (argc > 1 && !strcmp (argv[1], "--help"))
144
while((arg=getopt(argc,argv,"hVo:"))!=-1)
153
fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
157
output=fopen(optarg,"wb");
160
fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
161
optarg,strerror(errno));
162
return KEYSERVER_INTERNAL_ERROR;
170
input=fopen(argv[optind],"r");
173
fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
174
argv[optind],strerror(errno));
175
return KEYSERVER_INTERNAL_ERROR;
185
/* Get the command and info block */
187
while(fgets(line,MAX_LINE,input)!=NULL)
197
if(sscanf(line,"%c",&hash)==1 && hash=='#')
200
if(sscanf(line,"COMMAND %6s\n",commandstr)==1)
204
if(strcasecmp(commandstr,"get")==0)
210
if(sscanf(line,"SCHEME %" MKSTRING(MAX_SCHEME) "s\n",scheme)==1)
212
scheme[MAX_SCHEME]='\0';
216
if(sscanf(line,"AUTH %" MKSTRING(MAX_AUTH) "s\n",auth)==1)
222
if(sscanf(line,"HOST %" MKSTRING(MAX_HOST) "s\n",host)==1)
228
if(sscanf(line,"PORT %" MKSTRING(MAX_PORT) "s\n",port)==1)
234
if(sscanf(line,"PATH %" MKSTRING(MAX_PATH) "s\n",path)==1)
240
if(sscanf(line,"VERSION %d\n",&version)==1)
242
if(version!=KEYSERVER_PROTO_VERSION)
244
ret=KEYSERVER_VERSION_ERROR;
251
if(sscanf(line,"OPTION %255s\n",optionstr)==1)
254
char *start=&optionstr[0];
258
if(strncasecmp(optionstr,"no-",3)==0)
264
if(strcasecmp(start,"verbose")==0)
271
else if(strncasecmp(start,"http-proxy",10)==0)
275
else if(start[10]=='=')
277
strncpy(proxy,&start[11],MAX_PROXY);
278
proxy[MAX_PROXY]='\0';
281
else if(strncasecmp(start,"timeout",7)==0)
285
else if(start[7]=='=')
286
timeout=atoi(&start[8]);
287
else if(start[7]=='\0')
288
timeout=DEFAULT_KEYSERVER_TIMEOUT;
290
else if(strncasecmp(start,"follow-redirects",16)==0)
294
else if(start[16]=='=')
295
follow_redirects=atoi(&start[17]);
296
else if(start[16]=='\0')
306
fprintf(console,"gpgkeys: no scheme supplied!\n");
307
return KEYSERVER_SCHEME_NOT_FOUND;
309
#ifndef HTTP_VIA_LIBCURL
310
else if(strcasecmp(scheme,"http")==0)
312
fprintf(console,"gpgkeys: scheme `%s' not supported\n",scheme);
313
return KEYSERVER_SCHEME_NOT_FOUND;
315
#endif /* HTTP_VIA_LIBCURL */
316
#ifndef FTP_VIA_LIBCURL
317
else if(strcasecmp(scheme,"ftp")==0)
319
fprintf(console,"gpgkeys: scheme `%s' not supported\n",scheme);
320
return KEYSERVER_SCHEME_NOT_FOUND;
322
#endif /* FTP_VIA_LIBCURL */
324
if(timeout && register_timeout()==-1)
326
fprintf(console,"gpgkeys: unable to register timeout handler\n");
327
return KEYSERVER_INTERNAL_ERROR;
330
curl_global_init(CURL_GLOBAL_DEFAULT);
331
curl=curl_easy_init();
334
fprintf(console,"gpgkeys: unable to initialize curl\n");
335
ret=KEYSERVER_INTERNAL_ERROR;
341
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
342
if(follow_redirects>0)
343
curl_easy_setopt(curl,CURLOPT_MAXREDIRS,follow_redirects);
347
curl_easy_setopt(curl,CURLOPT_PROXY,proxy);
349
/* If it's a GET or a SEARCH, the next thing to come in is the
350
keyids. If it's a SEND, then there are no keyids. */
354
/* Eat the rest of the file */
357
if(fgets(line,MAX_LINE,input)==NULL)
361
if(line[0]=='\n' || line[0]=='\0')
369
fprintf(console,"gpgkeys: out of memory while "
370
"building key list\n");
371
ret=KEYSERVER_NO_MEMORY;
375
/* Trim the trailing \n */
376
thekey[strlen(line)-1]='\0';
384
"gpgkeys: this keyserver type only supports key retrieval\n");
388
if(!thekey || !host[0])
390
fprintf(console,"gpgkeys: invalid keyserver instructions\n");
394
/* Send the response */
396
fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
397
fprintf(output,"PROGRAM %s\n\n",VERSION);
401
fprintf(console,"Scheme:\t\t%s\n",scheme);
402
fprintf(console,"Host:\t\t%s\n",host);
404
fprintf(console,"Port:\t\t%s\n",port);
406
fprintf(console,"Path:\t\t%s\n",path);
407
fprintf(console,"Command:\tGET\n");
410
set_timeout(timeout);
414
curl_easy_cleanup(curl);
426
curl_global_cleanup();