* gpgkeys_ldap.c (print_nocr): New. (get_key): Call it here to
[gnupg.git] / keyserver / gpgkeys_curl.c
1 /* gpgkeys_curl.c - fetch a key via libcurl
2  * Copyright (C) 2004, 2005 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 static int verbose=0;
38 static char scheme[MAX_SCHEME+1];
39 static char auth[MAX_AUTH+1];
40 static char host[MAX_HOST+1];
41 static char port[MAX_PORT+1];
42 static char path[URLMAX_PATH+1];
43 static char proxy[MAX_PROXY+1];
44 static FILE *input, *output, *console;
45 static CURL *curl;
46 static char request[MAX_URL];
47
48 static int
49 curl_err_to_gpg_err(CURLcode error)
50 {
51   switch(error)
52     {
53     case CURLE_FTP_COULDNT_RETR_FILE: return KEYSERVER_KEY_NOT_FOUND;
54     default: return KEYSERVER_INTERNAL_ERROR;
55     }
56 }
57
58 static size_t
59 writer(const void *ptr,size_t size,size_t nmemb,void *stream)
60 {
61   const char *buf=ptr;
62   size_t i;
63   static int markeridx=0,begun=0,done=0;
64   static const char *marker=BEGIN;
65
66   /* scan the incoming data for our marker */
67   for(i=0;!done && i<(size*nmemb);i++)
68     {
69       if(buf[i]==marker[markeridx])
70         {
71           markeridx++;
72           if(marker[markeridx]=='\0')
73             {
74               if(begun)
75                 done=1;
76               else
77                 {
78                   /* We've found the BEGIN marker, so now we're looking
79                      for the END marker. */
80                   begun=1;
81                   marker=END;
82                   markeridx=0;
83                   fprintf(output,BEGIN);
84                   continue;
85                 }
86             }
87         }
88       else
89         markeridx=0;
90
91       if(begun)
92         {
93           /* Canonicalize CRLF to just LF by stripping CRs.  This
94              actually makes sense, since on Unix-like machines LF is
95              correct, and on win32-like machines, our output buffer is
96              opened in textmode and will re-canonicalize line endings
97              back to CRLF.  Since we only need to handle armored keys,
98              we don't have to worry about odd cases like CRCRCR and
99              the like. */
100
101           if(buf[i]!='\r')
102             fputc(buf[i],output);
103         }
104     }
105
106   return size*nmemb;
107 }
108
109 static int
110 get_key(char *getkey)
111 {
112   CURLcode res;
113   char errorbuffer[CURL_ERROR_SIZE];
114
115   if(strncmp(getkey,"0x",2)==0)
116     getkey+=2;
117
118   fprintf(output,"KEY 0x%s BEGIN\n",getkey);
119
120   sprintf(request,"%s://%s%s%s%s%s%s%s",scheme,auth[0]?auth:"",auth[0]?"@":"",
121           host,port[0]?":":"",port[0]?port:"",path[0]?"":"/",path);
122
123   curl_easy_setopt(curl,CURLOPT_URL,request);
124   curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writer);
125   curl_easy_setopt(curl,CURLOPT_FILE,output);
126   curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errorbuffer);
127
128   res=curl_easy_perform(curl);
129   if(res!=0)
130     {
131       fprintf(console,"gpgkeys: %s fetch error %d: %s\n",scheme,
132               res,errorbuffer);
133       fprintf(output,"\nKEY 0x%s FAILED %d\n",getkey,curl_err_to_gpg_err(res));
134     }
135   else
136     fprintf(output,"\nKEY 0x%s END\n",getkey);
137
138   return KEYSERVER_OK;
139 }
140
141 static void 
142 show_help (FILE *fp)
143 {
144   fprintf (fp,"-h\thelp\n");
145   fprintf (fp,"-V\tversion\n");
146   fprintf (fp,"-o\toutput to this file\n");
147 }
148
149 int
150 main(int argc,char *argv[])
151 {
152   int arg,action=-1,ret=KEYSERVER_INTERNAL_ERROR;
153   char line[MAX_LINE];
154   char *thekey=NULL;
155   unsigned int timeout=DEFAULT_KEYSERVER_TIMEOUT;
156   long follow_redirects=5,debug=0,check_cert=1;
157
158   console=stderr;
159
160   /* Kludge to implement standard GNU options.  */
161   if (argc > 1 && !strcmp (argv[1], "--version"))
162     {
163       fputs ("gpgkeys_curl (GnuPG) " VERSION"\n", stdout);
164       return 0;
165     }
166   else if (argc > 1 && !strcmp (argv[1], "--help"))
167     {
168       show_help (stdout);
169       return 0;
170     }
171
172   while((arg=getopt(argc,argv,"hVo:"))!=-1)
173     switch(arg)
174       {
175       default:
176       case 'h':
177         show_help (console);
178         return KEYSERVER_OK;
179
180       case 'V':
181         fprintf(stdout,"%d\n%s\n",KEYSERVER_PROTO_VERSION,VERSION);
182         return KEYSERVER_OK;
183
184       case 'o':
185         output=fopen(optarg,"wb");
186         if(output==NULL)
187           {
188             fprintf(console,"gpgkeys: Cannot open output file `%s': %s\n",
189                     optarg,strerror(errno));
190             return KEYSERVER_INTERNAL_ERROR;
191           }
192
193         break;
194       }
195
196   if(argc>optind)
197     {
198       input=fopen(argv[optind],"r");
199       if(input==NULL)
200         {
201           fprintf(console,"gpgkeys: Cannot open input file `%s': %s\n",
202                   argv[optind],strerror(errno));
203           return KEYSERVER_INTERNAL_ERROR;
204         }
205     }
206
207   if(input==NULL)
208     input=stdin;
209
210   if(output==NULL)
211     output=stdout;
212
213   /* Get the command and info block */
214
215   while(fgets(line,MAX_LINE,input)!=NULL)
216     {
217       int version;
218       char command[MAX_COMMAND+1];
219       char option[MAX_OPTION+1];
220       char hash;
221
222       if(line[0]=='\n')
223         break;
224
225       if(sscanf(line,"%c",&hash)==1 && hash=='#')
226         continue;
227
228       if(sscanf(line,"COMMAND %" MKSTRING(MAX_COMMAND) "s\n",command)==1)
229         {
230           command[MAX_COMMAND]='\0';
231
232           if(strcasecmp(command,"get")==0)
233             action=GET;
234
235           continue;
236         }
237
238       if(sscanf(line,"SCHEME %" MKSTRING(MAX_SCHEME) "s\n",scheme)==1)
239         {
240           scheme[MAX_SCHEME]='\0';
241           continue;
242         }
243
244       if(sscanf(line,"AUTH %" MKSTRING(MAX_AUTH) "s\n",auth)==1)
245         {
246           auth[MAX_AUTH]='\0';
247           continue;
248         }
249
250       if(sscanf(line,"HOST %" MKSTRING(MAX_HOST) "s\n",host)==1)
251         {
252           host[MAX_HOST]='\0';
253           continue;
254         }
255
256       if(sscanf(line,"PORT %" MKSTRING(MAX_PORT) "s\n",port)==1)
257         {
258           port[MAX_PORT]='\0';
259           continue;
260         }
261
262       if(sscanf(line,"PATH %" MKSTRING(URLMAX_PATH) "s\n",path)==1)
263         {
264           path[URLMAX_PATH]='\0';
265           continue;
266         }
267
268       if(sscanf(line,"VERSION %d\n",&version)==1)
269         {
270           if(version!=KEYSERVER_PROTO_VERSION)
271             {
272               ret=KEYSERVER_VERSION_ERROR;
273               goto fail;
274             }
275
276           continue;
277         }
278
279       if(sscanf(line,"OPTION %" MKSTRING(MAX_OPTION) "s\n",option)==1)
280         {
281           int no=0;
282           char *start=&option[0];
283
284           option[MAX_OPTION]='\0';
285
286           if(strncasecmp(option,"no-",3)==0)
287             {
288               no=1;
289               start=&option[3];
290             }
291
292           if(strcasecmp(start,"verbose")==0)
293             {
294               if(no)
295                 verbose--;
296               else
297                 verbose++;
298             }
299           else if(strncasecmp(start,"http-proxy",10)==0)
300             {
301               if(no)
302                 proxy[0]='\0';
303               else if(start[10]=='=')
304                 {
305                   strncpy(proxy,&start[11],MAX_PROXY);
306                   proxy[MAX_PROXY]='\0';
307                 }
308             }
309           else if(strncasecmp(start,"timeout",7)==0)
310             {
311               if(no)
312                 timeout=0;
313               else if(start[7]=='=')
314                 timeout=atoi(&start[8]);
315               else if(start[7]=='\0')
316                 timeout=DEFAULT_KEYSERVER_TIMEOUT;
317             }
318           else if(strncasecmp(start,"follow-redirects",16)==0)
319             {
320               if(no)
321                 follow_redirects=0;
322               else if(start[16]=='=')
323                 follow_redirects=atoi(&start[17]);
324               else if(start[16]=='\0')
325                 follow_redirects=-1;
326             }
327           else if(strncasecmp(start,"debug",5)==0)
328             {
329               if(no)
330                 debug=0;
331               else if(start[5]=='=')
332                 debug=atoi(&start[6]);
333               else if(start[5]=='\0')
334                 debug=1;
335             }
336           else if(strcasecmp(start,"check-cert")==0)
337             {
338               if(no)
339                 check_cert=0;
340               else
341                 check_cert=1;
342             }
343
344           continue;
345         }
346     }
347
348   if(scheme[0]=='\0')
349     {
350       fprintf(console,"gpgkeys: no scheme supplied!\n");
351       return KEYSERVER_SCHEME_NOT_FOUND;
352     }
353 #ifdef HTTP_VIA_LIBCURL
354   else if(strcasecmp(scheme,"http")==0)
355     ;
356 #endif /* HTTP_VIA_LIBCURL */
357 #ifdef HTTPS_VIA_LIBCURL
358   else if(strcasecmp(scheme,"https")==0)
359     ;
360 #endif /* HTTP_VIA_LIBCURL */
361 #ifdef FTP_VIA_LIBCURL
362   else if(strcasecmp(scheme,"ftp")==0)
363     ;
364 #endif /* FTP_VIA_LIBCURL */
365 #ifdef FTPS_VIA_LIBCURL
366   else if(strcasecmp(scheme,"ftps")==0)
367     ;
368 #endif /* FTPS_VIA_LIBCURL */
369   else
370     {
371       fprintf(console,"gpgkeys: scheme `%s' not supported\n",scheme);
372       return KEYSERVER_SCHEME_NOT_FOUND;
373     }
374
375   if(timeout && register_timeout()==-1)
376     {
377       fprintf(console,"gpgkeys: unable to register timeout handler\n");
378       return KEYSERVER_INTERNAL_ERROR;
379     }
380
381   curl_global_init(CURL_GLOBAL_DEFAULT);
382   curl=curl_easy_init();
383   if(!curl)
384     {
385       fprintf(console,"gpgkeys: unable to initialize curl\n");
386       ret=KEYSERVER_INTERNAL_ERROR;
387       goto fail;
388     }
389
390   if(follow_redirects)
391     {
392       curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1);
393       if(follow_redirects>0)
394         curl_easy_setopt(curl,CURLOPT_MAXREDIRS,follow_redirects);
395     }
396
397   if(debug)
398     {
399       curl_easy_setopt(curl,CURLOPT_STDERR,console);
400       curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
401     }
402
403   curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,check_cert);
404
405   if(proxy[0])
406     curl_easy_setopt(curl,CURLOPT_PROXY,proxy);
407
408   /* If it's a GET or a SEARCH, the next thing to come in is the
409      keyids.  If it's a SEND, then there are no keyids. */
410
411   if(action==GET)
412     {
413       /* Eat the rest of the file */
414       for(;;)
415         {
416           if(fgets(line,MAX_LINE,input)==NULL)
417             break;
418           else
419             {
420               if(line[0]=='\n' || line[0]=='\0')
421                 break;
422
423               if(!thekey)
424                 {
425                   thekey=strdup(line);
426                   if(!thekey)
427                     {
428                       fprintf(console,"gpgkeys: out of memory while "
429                               "building key list\n");
430                       ret=KEYSERVER_NO_MEMORY;
431                       goto fail;
432                     }
433
434                   /* Trim the trailing \n */
435                   thekey[strlen(line)-1]='\0';
436                 }
437             }
438         }
439     }
440   else
441     {
442       fprintf(console,
443               "gpgkeys: this keyserver type only supports key retrieval\n");
444       goto fail;
445     }
446
447   if(!thekey || !host[0])
448     {
449       fprintf(console,"gpgkeys: invalid keyserver instructions\n");
450       goto fail;
451     }
452
453   /* Send the response */
454
455   fprintf(output,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
456   fprintf(output,"PROGRAM %s\n\n",VERSION);
457
458   if(verbose)
459     {
460       fprintf(console,"Scheme:\t\t%s\n",scheme);
461       fprintf(console,"Host:\t\t%s\n",host);
462       if(port[0])
463         fprintf(console,"Port:\t\t%s\n",port);
464       if(path[0])
465         fprintf(console,"Path:\t\t%s\n",path);
466       fprintf(console,"Command:\tGET\n");
467     }
468
469   set_timeout(timeout);
470
471   ret=get_key(thekey);
472
473   curl_easy_cleanup(curl);
474
475  fail:
476
477   free(thekey);
478
479   if(input!=stdin)
480     fclose(input);
481
482   if(output!=stdout)
483     fclose(output);
484
485   curl_global_cleanup();
486
487   return ret;
488 }