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,
32 #include <curl/curl.h>
34 #include "curl-shim.h"
36 #include "keyserver.h"
42 static FILE *input
,*output
,*console
;
44 static struct ks_options
*opt
;
50 char errorbuffer
[CURL_ERROR_SIZE
];
51 char request
[MAX_URL
];
52 struct curl_writer_ctx ctx
;
54 memset(&ctx
,0,sizeof(ctx
));
56 if(strncmp(getkey
,"0x",2)==0)
59 fprintf(output
,"KEY 0x%s BEGIN\n",getkey
);
61 sprintf(request
,"%s://%s%s%s%s",opt
->scheme
,opt
->host
,
62 opt
->port
?":":"",opt
->port
?opt
->port
:"",opt
->path
?opt
->path
:"/");
64 curl_easy_setopt(curl
,CURLOPT_URL
,request
);
65 curl_easy_setopt(curl
,CURLOPT_WRITEFUNCTION
,curl_writer
);
67 curl_easy_setopt(curl
,CURLOPT_FILE
,&ctx
);
68 curl_easy_setopt(curl
,CURLOPT_ERRORBUFFER
,errorbuffer
);
70 res
=curl_easy_perform(curl
);
73 fprintf(console
,"gpgkeys: %s fetch error %d: %s\n",opt
->scheme
,
75 fprintf(output
,"\nKEY 0x%s FAILED %d\n",getkey
,curl_err_to_gpg_err(res
));
79 curl_writer_finalize(&ctx
);
82 fprintf(console
,"gpgkeys: no key data found for %s\n",request
);
83 fprintf(output
,"\nKEY 0x%s FAILED %d\n",
84 getkey
,KEYSERVER_KEY_NOT_FOUND
);
87 fprintf(output
,"\nKEY 0x%s END\n",getkey
);
90 return curl_err_to_gpg_err(res
);
96 fprintf (fp
,"-h\thelp\n");
97 fprintf (fp
,"-V\tversion\n");
98 fprintf (fp
,"-o\toutput to this file\n");
102 main(int argc
,char *argv
[])
104 int arg
,ret
=KEYSERVER_INTERNAL_ERROR
;
107 long follow_redirects
=5;
112 /* Kludge to implement standard GNU options. */
113 if (argc
> 1 && !strcmp (argv
[1], "--version"))
115 fputs ("gpgkeys_curl (GnuPG) " VERSION
"\n", stdout
);
118 else if (argc
> 1 && !strcmp (argv
[1], "--help"))
124 while((arg
=getopt(argc
,argv
,"hVo:"))!=-1)
133 fprintf(stdout
,"%d\n%s\n",KEYSERVER_PROTO_VERSION
,VERSION
);
137 output
=fopen(optarg
,"wb");
140 fprintf(console
,"gpgkeys: Cannot open output file `%s': %s\n",
141 optarg
,strerror(errno
));
142 return KEYSERVER_INTERNAL_ERROR
;
150 input
=fopen(argv
[optind
],"r");
153 fprintf(console
,"gpgkeys: Cannot open input file `%s': %s\n",
154 argv
[optind
],strerror(errno
));
155 return KEYSERVER_INTERNAL_ERROR
;
165 opt
=init_ks_options();
167 return KEYSERVER_NO_MEMORY
;
169 /* Get the command and info block */
171 while(fgets(line
,MAX_LINE
,input
)!=NULL
)
174 char option
[MAX_OPTION
+1];
179 err
=parse_ks_options(line
,opt
);
188 if(sscanf(line
,"OPTION %" MKSTRING(MAX_OPTION
) "s\n",option
)==1)
191 char *start
=&option
[0];
193 option
[MAX_OPTION
]='\0';
195 if(strncasecmp(option
,"no-",3)==0)
201 if(strncasecmp(start
,"http-proxy",10)==0)
203 /* Safe to not check the return code of strdup() here.
204 If it fails, we simply won't use a proxy. */
210 else if(start
[10]=='=')
212 if(strlen(&start
[11])<MAX_PROXY
)
215 proxy
=strdup(&start
[11]);
219 else if(strncasecmp(start
,"follow-redirects",16)==0)
223 else if(start
[16]=='=')
224 follow_redirects
=atoi(&start
[17]);
225 else if(start
[16]=='\0')
235 fprintf(console
,"gpgkeys: no scheme supplied!\n");
236 ret
=KEYSERVER_SCHEME_NOT_FOUND
;
242 fprintf(console
,"gpgkeys: no keyserver host provided\n");
246 if(opt
->timeout
&& register_timeout()==-1)
248 fprintf(console
,"gpgkeys: unable to register timeout handler\n");
249 return KEYSERVER_INTERNAL_ERROR
;
252 curl_global_init(CURL_GLOBAL_DEFAULT
);
253 curl
=curl_easy_init();
256 fprintf(console
,"gpgkeys: unable to initialize curl\n");
257 ret
=KEYSERVER_INTERNAL_ERROR
;
263 curl_easy_setopt(curl
,CURLOPT_FOLLOWLOCATION
,1);
264 if(follow_redirects
>0)
265 curl_easy_setopt(curl
,CURLOPT_MAXREDIRS
,follow_redirects
);
269 curl_easy_setopt(curl
,CURLOPT_USERPWD
,opt
->auth
);
273 fprintf(console
,"gpgkeys: curl version = %s\n",curl_version());
274 curl_easy_setopt(curl
,CURLOPT_STDERR
,console
);
275 curl_easy_setopt(curl
,CURLOPT_VERBOSE
,1);
278 curl_easy_setopt(curl
,CURLOPT_SSL_VERIFYPEER
,opt
->flags
.check_cert
);
279 curl_easy_setopt(curl
,CURLOPT_CAINFO
,opt
->ca_cert_file
);
282 curl_easy_setopt(curl
,CURLOPT_PROXY
,proxy
);
284 /* If it's a GET or a SEARCH, the next thing to come in is the
285 keyids. If it's a SEND, then there are no keyids. */
287 if(opt
->action
==KS_GET
)
289 /* Eat the rest of the file */
292 if(fgets(line
,MAX_LINE
,input
)==NULL
)
296 if(line
[0]=='\n' || line
[0]=='\0')
304 fprintf(console
,"gpgkeys: out of memory while "
305 "building key list\n");
306 ret
=KEYSERVER_NO_MEMORY
;
310 /* Trim the trailing \n */
311 thekey
[strlen(line
)-1]='\0';
319 "gpgkeys: this keyserver type only supports key retrieval\n");
325 fprintf(console
,"gpgkeys: invalid keyserver instructions\n");
329 /* Send the response */
331 fprintf(output
,"VERSION %d\n",KEYSERVER_PROTO_VERSION
);
332 fprintf(output
,"PROGRAM %s\n\n",VERSION
);
336 fprintf(console
,"Scheme:\t\t%s\n",opt
->scheme
);
337 fprintf(console
,"Host:\t\t%s\n",opt
->host
);
339 fprintf(console
,"Port:\t\t%s\n",opt
->port
);
341 fprintf(console
,"Path:\t\t%s\n",opt
->path
);
342 fprintf(console
,"Command:\tGET\n");
345 set_timeout(opt
->timeout
);
359 free_ks_options(opt
);
362 curl_easy_cleanup(curl
);
366 curl_global_cleanup();