* gpgkeys_curl.c (get_key): Newer versions of libcurl don't define TRUE.
[gnupg.git] / keyserver / gpgkeys_curl.c
1 /* gpgkeys_curl.c - fetch a key via libcurl
2  * Copyright (C) 2004 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #ifdef HAVE_GETOPT_H
28 #include <getopt.h>
29 #endif
30 #include <curl/curl.h>
31 #include "keyserver.h"
32 #include "ksutil.h"
33
34 extern char *optarg;
35 extern int optind;
36
37 #define GET           0
38 #define MAX_LINE     80
39
40 #define MAX_SCHEME   20
41 #define MAX_AUTH    128
42 #define MAX_HOST     80
43 #define MAX_PORT     10
44 #define MAX_PATH   1024
45 #define MAX_PROXY   128
46 #define MAX_URL (MAX_SCHEME+1+3+MAX_AUTH+1+1+MAX_HOST+1+1+MAX_PORT+1+1+MAX_PATH+1+50)
47
48 #define STRINGIFY(x) #x
49 #define MKSTRING(x) STRINGIFY(x)
50
51 static int verbose=0;
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;
54 static CURL *curl;
55 static char request[MAX_URL]={'\0'};
56
57 static int
58 curl_err_to_gpg_err(CURLcode error)
59 {
60   switch(error)
61     {
62     case CURLE_FTP_COULDNT_RETR_FILE: return KEYSERVER_KEY_NOT_FOUND;
63     default: return KEYSERVER_INTERNAL_ERROR;
64     }
65 }
66
67 /* We wrap fwrite so to avoid DLL problems on Win32 (see curl faq for
68    more). */
69 static size_t
70 writer(const void *ptr,size_t size,size_t nmemb,void *stream)
71 {
72   return fwrite(ptr,size,nmemb,stream);
73 }
74
75 static int
76 get_key(char *getkey)
77 {
78   CURLcode res;
79   char errorbuffer[CURL_ERROR_SIZE];
80
81   if(strncmp(getkey,"0x",2)==0)
82     getkey+=2;
83
84   fprintf(output,"KEY 0x%s BEGIN\n",getkey);
85
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);
88
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);
93
94   if(verbose>1)
95     {
96       curl_easy_setopt(curl,CURLOPT_STDERR,console);
97       curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
98     }
99
100   res=curl_easy_perform(curl);
101   if(res!=0)
102     {
103       fprintf(console,"gpgkeys: %s fetch error %d: %s\n",scheme,
104               res,errorbuffer);
105       fprintf(output,"KEY 0x%s FAILED %d\n",getkey,curl_err_to_gpg_err(res));
106     }
107   else
108     fprintf(output,"KEY 0x%s END\n",getkey);
109
110   return KEYSERVER_OK;
111 }
112
113 static void 
114 show_help (FILE *fp)
115 {
116   fprintf (fp,"-h\thelp\n");
117   fprintf (fp,"-V\tversion\n");
118   fprintf (fp,"-o\toutput to this file\n");
119 }
120
121 int
122 main(int argc,char *argv[])
123 {
124   int arg,action=-1,ret=KEYSERVER_INTERNAL_ERROR;
125   char line[MAX_LINE];
126   char *thekey=NULL;
127   unsigned int timeout=DEFAULT_KEYSERVER_TIMEOUT;
128   long follow_redirects=5;
129
130   console=stderr;
131
132   /* Kludge to implement standard GNU options.  */
133   if (argc > 1 && !strcmp (argv[1], "--version"))
134     {
135       fputs ("gpgkeys_curl (GnuPG) " VERSION"\n", stdout);
136       return 0;
137     }
138   else if (argc > 1 && !strcmp (argv[1], "--help"))
139     {
140       show_help (stdout);
141       return 0;
142     }
143
144   while((arg=getopt(argc,argv,"hVo:"))!=-1)
145     switch(arg)
146       {
147       default:
148       case 'h':
149         show_help (console);
150         return KEYSERVER_OK;
151
152       case 'V':
153         fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
154         return KEYSERVER_OK;
155
156       case 'o':
157         output=fopen(optarg,"wb");
158         if(output==NULL)
159           {
160             fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
161                     optarg,strerror(errno));
162             return KEYSERVER_INTERNAL_ERROR;
163           }
164
165         break;
166       }
167
168   if(argc>optind)
169     {
170       input=fopen(argv[optind],"r");
171       if(input==NULL)
172         {
173           fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
174                   argv[optind],strerror(errno));
175           return KEYSERVER_INTERNAL_ERROR;
176         }
177     }
178
179   if(input==NULL)
180     input=stdin;
181
182   if(output==NULL)
183     output=stdout;
184
185   /* Get the command and info block */
186
187   while(fgets(line,MAX_LINE,input)!=NULL)
188     {
189       int version;
190       char commandstr[7];
191       char optionstr[256];
192       char hash;
193
194       if(line[0]=='\n')
195         break;
196
197       if(sscanf(line,"%c",&hash)==1 && hash=='#')
198         continue;
199
200       if(sscanf(line,"COMMAND %6s\n",commandstr)==1)
201         {
202           commandstr[6]='\0';
203
204           if(strcasecmp(commandstr,"get")==0)
205             action=GET;
206
207           continue;
208         }
209
210       if(sscanf(line,"SCHEME %" MKSTRING(MAX_SCHEME) "s\n",scheme)==1)
211         {
212           scheme[MAX_SCHEME]='\0';
213           continue;
214         }
215
216       if(sscanf(line,"AUTH %" MKSTRING(MAX_AUTH) "s\n",auth)==1)
217         {
218           auth[MAX_AUTH]='\0';
219           continue;
220         }
221
222       if(sscanf(line,"HOST %" MKSTRING(MAX_HOST) "s\n",host)==1)
223         {
224           host[MAX_HOST]='\0';
225           continue;
226         }
227
228       if(sscanf(line,"PORT %" MKSTRING(MAX_PORT) "s\n",port)==1)
229         {
230           port[MAX_PORT]='\0';
231           continue;
232         }
233
234       if(sscanf(line,"PATH %" MKSTRING(MAX_PATH) "s\n",path)==1)
235         {
236           path[MAX_PATH]='\0';
237           continue;
238         }
239
240       if(sscanf(line,"VERSION %d\n",&version)==1)
241         {
242           if(version!=KEYSERVER_PROTO_VERSION)
243             {
244               ret=KEYSERVER_VERSION_ERROR;
245               goto fail;
246             }
247
248           continue;
249         }
250
251       if(sscanf(line,"OPTION %255s\n",optionstr)==1)
252         {
253           int no=0;
254           char *start=&optionstr[0];
255
256           optionstr[255]='\0';
257
258           if(strncasecmp(optionstr,"no-",3)==0)
259             {
260               no=1;
261               start=&optionstr[3];
262             }
263
264           if(strcasecmp(start,"verbose")==0)
265             {
266               if(no)
267                 verbose--;
268               else
269                 verbose++;
270             }
271           else if(strncasecmp(start,"http-proxy",10)==0)
272             {
273               if(no)
274                 proxy[0]='\0';
275               else if(start[10]=='=')
276                 {
277                   strncpy(proxy,&start[11],MAX_PROXY);
278                   proxy[MAX_PROXY]='\0';
279                 }
280             }
281           else if(strncasecmp(start,"timeout",7)==0)
282             {
283               if(no)
284                 timeout=0;
285               else if(start[7]=='=')
286                 timeout=atoi(&start[8]);
287               else if(start[7]=='\0')
288                 timeout=DEFAULT_KEYSERVER_TIMEOUT;
289             }
290           else if(strncasecmp(start,"follow-redirects",16)==0)
291             {
292               if(no)
293                 follow_redirects=0;
294               else if(start[16]=='=')
295                 follow_redirects=atoi(&start[17]);
296               else if(start[16]=='\0')
297                 follow_redirects=-1;
298             }
299
300           continue;
301         }
302     }
303
304   if(scheme[0]=='\0')
305     {
306       fprintf(console,"gpgkeys: no scheme supplied!\n");
307       return KEYSERVER_SCHEME_NOT_FOUND;
308     }
309 #ifndef HTTP_VIA_LIBCURL
310   else if(strcasecmp(scheme,"http")==0)
311     {
312       fprintf(console,"gpgkeys: scheme `%s' not supported\n",scheme);
313       return KEYSERVER_SCHEME_NOT_FOUND;
314     }
315 #endif /* HTTP_VIA_LIBCURL */
316 #ifndef FTP_VIA_LIBCURL
317   else if(strcasecmp(scheme,"ftp")==0)
318     {
319       fprintf(console,"gpgkeys: scheme `%s' not supported\n",scheme);
320       return KEYSERVER_SCHEME_NOT_FOUND;
321     }
322 #endif /* FTP_VIA_LIBCURL */
323
324   if(timeout && register_timeout()==-1)
325     {
326       fprintf(console,"gpgkeys: unable to register timeout handler\n");
327       return KEYSERVER_INTERNAL_ERROR;
328     }
329
330   curl_global_init(CURL_GLOBAL_DEFAULT);
331   curl=curl_easy_init();
332   if(!curl)
333     {
334       fprintf(console,"gpgkeys: unable to initialize curl\n");
335       ret=KEYSERVER_INTERNAL_ERROR;
336       goto fail;
337     }
338
339   if(follow_redirects)
340     {
341       curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
342       if(follow_redirects>0)
343         curl_easy_setopt(curl,CURLOPT_MAXREDIRS,follow_redirects);
344     }
345
346   if(proxy[0])
347     curl_easy_setopt(curl,CURLOPT_PROXY,proxy);
348
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. */
351
352   if(action==GET)
353     {
354       /* Eat the rest of the file */
355       for(;;)
356         {
357           if(fgets(line,MAX_LINE,input)==NULL)
358             break;
359           else
360             {
361               if(line[0]=='\n' || line[0]=='\0')
362                 break;
363
364               if(!thekey)
365                 {
366                   thekey=strdup(line);
367                   if(!thekey)
368                     {
369                       fprintf(console,"gpgkeys: out of memory while "
370                               "building key list\n");
371                       ret=KEYSERVER_NO_MEMORY;
372                       goto fail;
373                     }
374
375                   /* Trim the trailing \n */
376                   thekey[strlen(line)-1]='\0';
377                 }
378             }
379         }
380     }
381   else
382     {
383       fprintf(console,
384               "gpgkeys: this keyserver type only supports key retrieval\n");
385       goto fail;
386     }
387
388   if(!thekey || !host[0])
389     {
390       fprintf(console,"gpgkeys: invalid keyserver instructions\n");
391       goto fail;
392     }
393
394   /* Send the response */
395
396   fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
397   fprintf(output,"PROGRAM %s\n\n",VERSION);
398
399   if(verbose)
400     {
401       fprintf(console,"Scheme:\t\t%s\n",scheme);
402       fprintf(console,"Host:\t\t%s\n",host);
403       if(port[0])
404         fprintf(console,"Port:\t\t%s\n",port);
405       if(path[0])
406         fprintf(console,"Path:\t\t%s\n",path);
407       fprintf(console,"Command:\tGET\n");
408     }
409
410   set_timeout(timeout);
411
412   ret=get_key(thekey);
413
414   curl_easy_cleanup(curl);
415
416  fail:
417
418   free(thekey);
419
420   if(input!=stdin)
421     fclose(input);
422
423   if(output!=stdout)
424     fclose(output);
425
426   curl_global_cleanup();
427
428   return ret;
429 }