* keyserver.c: Fix build problem with platforms that stick libcurl in
[gnupg.git] / g10 / keyserver.c
1 /* keyserver.c - generic keyserver code
2  * Copyright (C) 2001, 2002, 2003, 2004, 2005,
3  *               2006 Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
7  * GnuPG is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * GnuPG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20  * USA.
21  */
22
23 #include <config.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include "filter.h"
30 #include "keydb.h"
31 #include "status.h"
32 #include "exec.h"
33 #include "main.h"
34 #include "i18n.h"
35 #include "iobuf.h"
36 #include "memory.h"
37 #include "ttyio.h"
38 #include "options.h"
39 #include "packet.h"
40 #include "trustdb.h"
41 #include "keyserver-internal.h"
42 #include "util.h"
43
44 #define GPGKEYS_PREFIX "gpgkeys_"
45
46 #if defined(HAVE_LIBCURL) || defined(FAKE_CURL)
47 #define GPGKEYS_CURL "gpgkeys_curl"
48 #endif
49
50 #ifdef GPGKEYS_CURL
51 #define GPGKEYS_PREFIX_LEN (strlen(GPGKEYS_PREFIX)+strlen(GPGKEYS_CURL))
52 #else
53 #define GPGKEYS_PREFIX_LEN (strlen(GPGKEYS_PREFIX))
54 #endif
55
56 struct keyrec
57 {
58   KEYDB_SEARCH_DESC desc;
59   u32 createtime,expiretime;
60   int size,flags;
61   byte type;
62   IOBUF uidbuf;
63   unsigned int lines;
64 };
65
66 enum ks_action {KS_UNKNOWN=0,KS_GET,KS_GETNAME,KS_SEND,KS_SEARCH};
67
68 static struct parse_options keyserver_opts[]=
69   {
70     /* some of these options are not real - just for the help
71        message */
72     {"max-cert-size",0,NULL,NULL},
73     {"include-revoked",0,NULL,N_("include revoked keys in search results")},
74     {"include-subkeys",0,NULL,N_("include subkeys when searching by key ID")},
75     {"use-temp-files",0,NULL,
76      N_("use temporary files to pass data to keyserver helpers")},
77     {"keep-temp-files",KEYSERVER_KEEP_TEMP_FILES,NULL,
78      N_("do not delete temporary files after using them")},
79     {"refresh-add-fake-v3-keyids",KEYSERVER_ADD_FAKE_V3,NULL,
80      NULL},
81     {"auto-key-retrieve",KEYSERVER_AUTO_KEY_RETRIEVE,NULL,
82      N_("automatically retrieve keys when verifying signatures")},
83     {"honor-keyserver-url",KEYSERVER_HONOR_KEYSERVER_URL,NULL,
84      N_("honor the preferred keyserver URL set on the key")},
85     {"honor-pka-record",KEYSERVER_HONOR_PKA_RECORD,NULL,
86      N_("honor the PKA record set on a key when retrieving keys")},
87     {NULL,0,NULL,NULL}
88   };
89
90 static int keyserver_work(enum ks_action action,STRLIST list,
91                           KEYDB_SEARCH_DESC *desc,int count,
92                           unsigned char **fpr,size_t *fpr_len,
93                           struct keyserver_spec *keyserver);
94
95 /* Reasonable guess */
96 #define DEFAULT_MAX_CERT_SIZE 16384
97
98 static size_t max_cert_size=DEFAULT_MAX_CERT_SIZE;
99
100 static void
101 add_canonical_option(char *option,STRLIST *list)
102 {
103   char *arg=argsplit(option);
104
105   if(arg)
106     {
107       char *joined;
108
109       joined=xmalloc(strlen(option)+1+strlen(arg)+1);
110       /* Make a canonical name=value form with no spaces */
111       strcpy(joined,option);
112       strcat(joined,"=");
113       strcat(joined,arg);
114       append_to_strlist(list,joined);
115       xfree(joined);
116     }
117   else
118     append_to_strlist(list,option);
119 }
120
121 int
122 parse_keyserver_options(char *options)
123 {
124   int ret=1;
125   char *tok;
126   char *max_cert=NULL;
127
128   keyserver_opts[0].value=&max_cert;
129
130   while((tok=optsep(&options)))
131     {
132       if(tok[0]=='\0')
133         continue;
134
135       /* For backwards compatibility.  1.2.x used honor-http-proxy and
136          there are a good number of documents published that recommend
137          it. */
138       if(ascii_strcasecmp(tok,"honor-http-proxy")==0)
139         tok="http-proxy";
140       else if(ascii_strcasecmp(tok,"no-honor-http-proxy")==0)
141         tok="no-http-proxy";
142
143       /* We accept quite a few possible options here - some options to
144          handle specially, the keyserver_options list, and import and
145          export options that pertain to keyserver operations.  Note
146          that you must use strncasecmp here as there might be an
147          =argument attached which will foil the use of strcasecmp. */
148
149 #ifdef EXEC_TEMPFILE_ONLY
150       if(ascii_strncasecmp(tok,"use-temp-files",14)==0 ||
151               ascii_strncasecmp(tok,"no-use-temp-files",17)==0)
152         log_info(_("WARNING: keyserver option `%s' is not used"
153                    " on this platform\n"),tok);
154 #else
155       if(ascii_strncasecmp(tok,"use-temp-files",14)==0)
156         opt.keyserver_options.options|=KEYSERVER_USE_TEMP_FILES;
157       else if(ascii_strncasecmp(tok,"no-use-temp-files",17)==0)
158         opt.keyserver_options.options&=~KEYSERVER_USE_TEMP_FILES;
159 #endif
160       else if(!parse_options(tok,&opt.keyserver_options.options,
161                              keyserver_opts,0)
162          && !parse_import_options(tok,
163                                   &opt.keyserver_options.import_options,0)
164          && !parse_export_options(tok,
165                                   &opt.keyserver_options.export_options,0))
166         {
167           /* All of the standard options have failed, so the option is
168              destined for a keyserver plugin. */
169           add_canonical_option(tok,&opt.keyserver_options.other);
170         }
171     }
172
173   if(max_cert)
174     {
175       max_cert_size=strtoul(max_cert,(char **)NULL,10);
176
177       if(max_cert_size==0)
178         max_cert_size=DEFAULT_MAX_CERT_SIZE;
179     }
180
181   return ret;
182 }
183
184 void
185 free_keyserver_spec(struct keyserver_spec *keyserver)
186 {
187   xfree(keyserver->uri);
188   xfree(keyserver->scheme);
189   xfree(keyserver->auth);
190   xfree(keyserver->host);
191   xfree(keyserver->port);
192   xfree(keyserver->path);
193   xfree(keyserver->opaque);
194   free_strlist(keyserver->options);
195   xfree(keyserver);
196 }
197
198 /* Return 0 for match */
199 static int
200 cmp_keyserver_spec(struct keyserver_spec *one,struct keyserver_spec *two)
201 {
202   if(ascii_strcasecmp(one->scheme,two->scheme)==0)
203     {
204       if(one->host && two->host && ascii_strcasecmp(one->host,two->host)==0)
205         {
206           if((one->port && two->port
207               && ascii_strcasecmp(one->port,two->port)==0)
208              || (!one->port && !two->port))
209             return 0;
210         }
211       else if(one->opaque && two->opaque
212               && ascii_strcasecmp(one->opaque,two->opaque)==0)
213         return 0;
214     }
215
216   return 1;
217 }
218
219 /* Try and match one of our keyservers.  If we can, return that.  If
220    we can't, return our input. */
221 struct keyserver_spec *
222 keyserver_match(struct keyserver_spec *spec)
223 {
224   struct keyserver_spec *ks;
225
226   for(ks=opt.keyserver;ks;ks=ks->next)
227     if(cmp_keyserver_spec(spec,ks)==0)
228       return ks;
229
230   return spec;
231 }
232
233 /* TODO: once we cut over to an all-curl world, we don't need this
234    parser any longer so it can be removed, or at least moved to
235    keyserver/ksutil.c for limited use in gpgkeys_ldap or the like. */
236
237 struct keyserver_spec *
238 parse_keyserver_uri(const char *string,int require_scheme,
239                     const char *configname,unsigned int configlineno)
240 {
241   int assume_hkp=0;
242   struct keyserver_spec *keyserver;
243   const char *idx;
244   int count;
245   char *uri,*options;
246
247   assert(string!=NULL);
248
249   keyserver=xmalloc_clear(sizeof(struct keyserver_spec));
250
251   uri=xstrdup(string);
252
253   options=strchr(uri,' ');
254   if(options)
255     {
256       char *tok;
257
258       *options='\0';
259       options++;
260
261       while((tok=optsep(&options)))
262         add_canonical_option(tok,&keyserver->options);
263     }
264
265   /* Get the scheme */
266
267   for(idx=uri,count=0;*idx && *idx!=':';idx++)
268     {
269       count++;
270
271       /* Do we see the start of an RFC-2732 ipv6 address here?  If so,
272          there clearly isn't a scheme so get out early. */
273       if(*idx=='[')
274         {
275           /* Was the '[' the first thing in the string?  If not, we
276              have a mangled scheme with a [ in it so fail. */
277           if(count==1)
278             break;
279           else
280             goto fail;
281         }
282     }
283
284   if(count==0)
285     goto fail;
286
287   if(*idx=='\0' || *idx=='[')
288     {
289       if(require_scheme)
290         return NULL;
291
292       /* Assume HKP if there is no scheme */
293       assume_hkp=1;
294       keyserver->scheme=xstrdup("hkp");
295
296       keyserver->uri=xmalloc(strlen(keyserver->scheme)+3+strlen(uri)+1);
297       strcpy(keyserver->uri,keyserver->scheme);
298       strcat(keyserver->uri,"://");
299       strcat(keyserver->uri,uri);
300     }
301   else
302     {
303       int i;
304
305       keyserver->uri=xstrdup(uri);
306
307       keyserver->scheme=xmalloc(count+1);
308
309       /* Force to lowercase */
310       for(i=0;i<count;i++)
311         keyserver->scheme[i]=ascii_tolower(uri[i]);
312
313       keyserver->scheme[i]='\0';
314
315       /* Skip past the scheme and colon */
316       uri+=count+1;
317     }
318
319   if(ascii_strcasecmp(keyserver->scheme,"x-broken-hkp")==0)
320     {
321       deprecated_warning(configname,configlineno,"x-broken-hkp",
322                          "--keyserver-options ","broken-http-proxy");
323       xfree(keyserver->scheme);
324       keyserver->scheme=xstrdup("hkp");
325       append_to_strlist(&opt.keyserver_options.other,"broken-http-proxy");
326     }
327   else if(ascii_strcasecmp(keyserver->scheme,"x-hkp")==0)
328     {
329       /* Canonicalize this to "hkp" so it works with both the internal
330          and external keyserver interface. */
331       xfree(keyserver->scheme);
332       keyserver->scheme=xstrdup("hkp");
333     }
334
335   if(assume_hkp || (uri[0]=='/' && uri[1]=='/'))
336     {
337       /* Two slashes means network path. */
338
339       /* Skip over the "//", if any */
340       if(!assume_hkp)
341         uri+=2;
342
343       /* Do we have userinfo auth data present? */
344       for(idx=uri,count=0;*idx && *idx!='@' && *idx!='/';idx++)
345         count++;
346
347       /* We found a @ before the slash, so that means everything
348          before the @ is auth data. */
349       if(*idx=='@')
350         {
351           if(count==0)
352             goto fail;
353
354           keyserver->auth=xmalloc(count+1);
355           strncpy(keyserver->auth,uri,count);
356           keyserver->auth[count]='\0';
357           uri+=count+1;
358         }
359
360       /* Is it an RFC-2732 ipv6 [literal address] ? */
361       if(*uri=='[')
362         {
363           for(idx=uri+1,count=1;*idx
364                 && ((isascii (*idx) && isxdigit(*idx))
365                     || *idx==':' || *idx=='.');idx++)
366             count++;
367
368           /* Is the ipv6 literal address terminated? */
369           if(*idx==']')
370             count++;
371           else
372             goto fail;
373         }
374       else
375         for(idx=uri,count=0;*idx && *idx!=':' && *idx!='/';idx++)
376           count++;
377
378       if(count==0)
379         goto fail;
380
381       keyserver->host=xmalloc(count+1);
382       strncpy(keyserver->host,uri,count);
383       keyserver->host[count]='\0';
384
385       /* Skip past the host */
386       uri+=count;
387
388       if(*uri==':')
389         {
390           /* It would seem to be reasonable to limit the range of the
391              ports to values between 1-65535, but RFC 1738 and 1808
392              imply there is no limit.  Of course, the real world has
393              limits. */
394
395           for(idx=uri+1,count=0;*idx && *idx!='/';idx++)
396             {
397               count++;
398
399               /* Ports are digits only */
400               if(!digitp(idx))
401                 goto fail;
402             }
403
404           keyserver->port=xmalloc(count+1);
405           strncpy(keyserver->port,uri+1,count);
406           keyserver->port[count]='\0';
407
408           /* Skip past the colon and port number */
409           uri+=1+count;
410         }
411
412       /* Everything else is the path */
413       if(*uri)
414         keyserver->path=xstrdup(uri);
415       else
416         keyserver->path=xstrdup("/");
417
418       if(keyserver->path[1]!='\0')
419         keyserver->flags.direct_uri=1;
420     }
421   else if(uri[0]!='/')
422     {
423       /* No slash means opaque.  Just record the opaque blob and get
424          out. */
425       keyserver->opaque=xstrdup(uri);
426     }
427   else
428     {
429       /* One slash means absolute path.  We don't need to support that
430          yet. */
431       goto fail;
432     }
433
434   return keyserver;
435
436  fail:
437   free_keyserver_spec(keyserver);
438
439   return NULL;
440 }
441
442 struct keyserver_spec *
443 parse_preferred_keyserver(PKT_signature *sig)
444 {
445   struct keyserver_spec *spec=NULL;
446   const byte *p;
447   size_t plen;
448
449   p=parse_sig_subpkt(sig->hashed,SIGSUBPKT_PREF_KS,&plen);
450   if(p && plen)
451     {
452       byte *dupe=xmalloc(plen+1);
453
454       memcpy(dupe,p,plen);
455       dupe[plen]='\0';
456       spec=parse_keyserver_uri(dupe,1,NULL,0);
457       xfree(dupe);
458     }
459
460   return spec;
461 }
462
463 static void
464 print_keyrec(int number,struct keyrec *keyrec)
465 {
466   int i;
467
468   iobuf_writebyte(keyrec->uidbuf,0);
469   iobuf_flush_temp(keyrec->uidbuf);
470   printf("(%d)\t%s  ",number,iobuf_get_temp_buffer(keyrec->uidbuf));
471
472   if(keyrec->size>0)
473     printf("%d bit ",keyrec->size);
474
475   if(keyrec->type)
476     {
477       const char *str=pubkey_algo_to_string(keyrec->type);
478
479       if(str)
480         printf("%s ",str);
481       else
482         printf("unknown ");
483     }
484
485   switch(keyrec->desc.mode)
486     {
487       /* If the keyserver helper gave us a short keyid, we have no
488          choice but to use it.  Do check --keyid-format to add a 0x if
489          needed. */
490     case KEYDB_SEARCH_MODE_SHORT_KID:
491       printf("key %s%08lX",
492              (opt.keyid_format==KF_0xSHORT
493               || opt.keyid_format==KF_0xLONG)?"0x":"",
494              (ulong)keyrec->desc.u.kid[1]);
495       break;
496
497       /* However, if it gave us a long keyid, we can honor
498          --keyid-format */
499     case KEYDB_SEARCH_MODE_LONG_KID:
500       printf("key %s",keystr(keyrec->desc.u.kid));
501       break;
502
503     case KEYDB_SEARCH_MODE_FPR16:
504       printf("key ");
505       for(i=0;i<16;i++)
506         printf("%02X",keyrec->desc.u.fpr[i]);
507       break;
508
509     case KEYDB_SEARCH_MODE_FPR20:
510       printf("key ");
511       for(i=0;i<20;i++)
512         printf("%02X",keyrec->desc.u.fpr[i]);
513       break;
514
515     default:
516       BUG();
517       break;
518     }
519
520   if(keyrec->createtime>0)
521     {
522       printf(", ");
523       printf(_("created: %s"),strtimestamp(keyrec->createtime));
524     }
525
526   if(keyrec->expiretime>0)
527     {
528       printf(", ");
529       printf(_("expires: %s"),strtimestamp(keyrec->expiretime));
530     }
531
532   if(keyrec->flags&1)
533     printf(" (%s)",_("revoked"));
534   if(keyrec->flags&2)
535     printf(" (%s)",_("disabled"));
536   if(keyrec->flags&4)
537     printf(" (%s)",_("expired"));
538
539   printf("\n");
540 }
541
542 /* Returns a keyrec (which must be freed) once a key is complete, and
543    NULL otherwise.  Call with a NULL keystring once key parsing is
544    complete to return any unfinished keys. */
545 static struct keyrec *
546 parse_keyrec(char *keystring)
547 {
548   static struct keyrec *work=NULL;
549   struct keyrec *ret=NULL;
550   char *record;
551   int i;
552
553   if(keystring==NULL)
554     {
555       if(work==NULL)
556         return NULL;
557       else if(work->desc.mode==KEYDB_SEARCH_MODE_NONE)
558         {
559           xfree(work);
560           return NULL;
561         }
562       else
563         {
564           ret=work;
565           work=NULL;
566           return ret;
567         }
568     }
569
570   if(work==NULL)
571     {
572       work=xmalloc_clear(sizeof(struct keyrec));
573       work->uidbuf=iobuf_temp();
574     }
575
576   /* Remove trailing whitespace */
577   for(i=strlen(keystring);i>0;i--)
578     if(ascii_isspace(keystring[i-1]))
579       keystring[i-1]='\0';
580     else
581       break;
582
583   if((record=strsep(&keystring,":"))==NULL)
584     return ret;
585
586   if(ascii_strcasecmp("pub",record)==0)
587     {
588       char *tok;
589
590       if(work->desc.mode)
591         {
592           ret=work;
593           work=xmalloc_clear(sizeof(struct keyrec));
594           work->uidbuf=iobuf_temp();
595         }
596
597       if((tok=strsep(&keystring,":"))==NULL)
598         return ret;
599
600       classify_user_id(tok,&work->desc);
601       if(work->desc.mode!=KEYDB_SEARCH_MODE_SHORT_KID
602          && work->desc.mode!=KEYDB_SEARCH_MODE_LONG_KID
603          && work->desc.mode!=KEYDB_SEARCH_MODE_FPR16
604          && work->desc.mode!=KEYDB_SEARCH_MODE_FPR20)
605         {
606           work->desc.mode=KEYDB_SEARCH_MODE_NONE;
607           return ret;
608         }
609
610       /* Note all items after this are optional.  This allows us to
611          have a pub line as simple as pub:keyid and nothing else. */
612
613       work->lines++;
614
615       if((tok=strsep(&keystring,":"))==NULL)
616         return ret;
617
618       work->type=atoi(tok);
619
620       if((tok=strsep(&keystring,":"))==NULL)
621         return ret;
622
623       work->size=atoi(tok);
624
625       if((tok=strsep(&keystring,":"))==NULL)
626         return ret;
627
628       if(atoi(tok)<=0)
629         work->createtime=0;
630       else
631         work->createtime=atoi(tok);
632
633       if((tok=strsep(&keystring,":"))==NULL)
634         return ret;
635
636       if(atoi(tok)<=0)
637         work->expiretime=0;
638       else
639         {
640           work->expiretime=atoi(tok);
641           /* Force the 'e' flag on if this key is expired. */
642           if(work->expiretime<=make_timestamp())
643             work->flags|=4;
644         }
645
646       if((tok=strsep(&keystring,":"))==NULL)
647         return ret;
648
649       while(*tok)
650         switch(*tok++)
651           {
652           case 'r':
653           case 'R':
654             work->flags|=1;
655             break;
656             
657           case 'd':
658           case 'D':
659             work->flags|=2;
660             break;
661
662           case 'e':
663           case 'E':
664             work->flags|=4;
665             break;
666           }
667     }
668   else if(ascii_strcasecmp("uid",record)==0 && work->desc.mode)
669     {
670       char *userid,*tok,*decoded;
671
672       if((tok=strsep(&keystring,":"))==NULL)
673         return ret;
674
675       if(strlen(tok)==0)
676         return ret;
677
678       userid=tok;
679
680       /* By definition, de-%-encoding is always smaller than the
681          original string so we can decode in place. */
682
683       i=0;
684
685       while(*tok)
686         if(tok[0]=='%' && tok[1] && tok[2])
687           {
688             if((userid[i]=hextobyte(&tok[1]))==-1)
689               userid[i]='?';
690
691             i++;
692             tok+=3;
693           }
694         else
695           userid[i++]=*tok++;
696
697       /* We don't care about the other info provided in the uid: line
698          since no keyserver supports marking userids with timestamps
699          or revoked/expired/disabled yet. */
700
701       /* No need to check for control characters, as utf8_to_native
702          does this for us. */
703
704       decoded=utf8_to_native(userid,i,0);
705       if(strlen(decoded)>opt.screen_columns-10)
706         decoded[opt.screen_columns-10]='\0';
707       iobuf_writestr(work->uidbuf,decoded);
708       xfree(decoded);
709       iobuf_writestr(work->uidbuf,"\n\t");
710       work->lines++;
711     }
712
713   /* Ignore any records other than "pri" and "uid" for easy future
714      growth. */
715
716   return ret;
717 }
718
719 /* TODO: do this as a list sent to keyserver_work rather than calling
720    it once for each key to get the correct counts after the import
721    (cosmetics, really) and to better take advantage of the keyservers
722    that can do multiple fetches in one go (LDAP). */
723 static int
724 show_prompt(KEYDB_SEARCH_DESC *desc,int numdesc,int count,const char *search)
725 {
726   char *answer;
727
728   if(count && opt.command_fd==-1)
729     {
730       static int from=1;
731       tty_printf("Keys %d-%d of %d for \"%s\".  ",from,numdesc,count,search);
732       from=numdesc+1;
733     }
734
735   answer=cpr_get_no_help("keysearch.prompt",
736                          _("Enter number(s), N)ext, or Q)uit > "));
737   /* control-d */
738   if(answer[0]=='\x04')
739     {
740       printf("Q\n");
741       answer[0]='q';
742     }
743
744   if(answer[0]=='q' || answer[0]=='Q')
745     {
746       xfree(answer);
747       return 1;
748     }
749   else if(atoi(answer)>=1 && atoi(answer)<=numdesc)
750     {
751       char *split=answer,*num;
752
753       while((num=strsep(&split," ,"))!=NULL)
754         if(atoi(num)>=1 && atoi(num)<=numdesc)
755           keyserver_work(KS_GET,NULL,&desc[atoi(num)-1],1,
756                          NULL,NULL,opt.keyserver);
757
758       xfree(answer);
759       return 1;
760     }
761
762   return 0;
763 }
764
765 /* Count and searchstr are just for cosmetics.  If the count is too
766    small, it will grow safely.  If negative it disables the "Key x-y
767    of z" messages.  searchstr should be UTF-8 (rather than native). */
768 static void
769 keyserver_search_prompt(IOBUF buffer,const char *searchstr)
770 {
771   int i=0,validcount=0,started=0,header=0,count=1;
772   unsigned int maxlen,buflen,numlines=0;
773   KEYDB_SEARCH_DESC *desc;
774   byte *line=NULL;
775   char *localstr=NULL;
776
777   if(searchstr)
778     localstr=utf8_to_native(searchstr,strlen(searchstr),0);
779
780   desc=xmalloc(count*sizeof(KEYDB_SEARCH_DESC));
781
782   for(;;)
783     {
784       struct keyrec *keyrec;
785       int rl;
786
787       maxlen=1024;
788       rl=iobuf_read_line(buffer,&line,&buflen,&maxlen);
789
790       if(opt.with_colons)
791         {
792           if(!header && ascii_strncasecmp("SEARCH ",line,7)==0
793              && ascii_strncasecmp(" BEGIN",&line[strlen(line)-7],6)==0)
794             {
795               header=1;
796               continue;
797             }
798           else if(ascii_strncasecmp("SEARCH ",line,7)==0
799                   && ascii_strncasecmp(" END",&line[strlen(line)-5],4)==0)
800             continue;
801
802           printf("%s",line);
803         }
804
805       /* Look for an info: line.  The only current info: values
806          defined are the version and key count. */
807       if(!started && rl>0 && ascii_strncasecmp("info:",line,5)==0)
808         {
809           char *tok,*str=&line[5];
810
811           if((tok=strsep(&str,":"))!=NULL)
812             {
813               int version;
814
815               if(sscanf(tok,"%d",&version)!=1)
816                 version=1;
817
818               if(version!=1)
819                 {
820                   log_error(_("invalid keyserver protocol "
821                               "(us %d!=handler %d)\n"),1,version);
822                   break;
823                 }
824             }
825
826           if((tok=strsep(&str,":"))!=NULL && sscanf(tok,"%d",&count)==1)
827             {
828               if(count==0)
829                 goto notfound;
830               else if(count<0)
831                 count=10;
832               else
833                 validcount=1;
834
835               desc=xrealloc(desc,count*sizeof(KEYDB_SEARCH_DESC));
836             }
837
838           started=1;
839           continue;
840         }
841
842       if(rl==0)
843         {
844           keyrec=parse_keyrec(NULL);
845
846           if(keyrec==NULL)
847             {
848               if(i==0)
849                 {
850                   count=0;
851                   break;
852                 }
853
854               if(i!=count)
855                 validcount=0;
856
857               for(;;)
858                 {
859                   if(show_prompt(desc,i,validcount?count:0,localstr))
860                     break;
861                   validcount=0;
862                 }
863
864               break;
865             }
866         }
867       else
868         keyrec=parse_keyrec(line);
869
870       if(i==count)
871         {
872           /* keyserver helper sent more keys than they claimed in the
873              info: line. */
874           count+=10;
875           desc=xrealloc(desc,count*sizeof(KEYDB_SEARCH_DESC));
876           validcount=0;
877         }
878
879       if(keyrec)
880         {
881           desc[i]=keyrec->desc;
882
883           if(!opt.with_colons)
884             {
885               /* screen_lines - 1 for the prompt. */
886               if(numlines+keyrec->lines>opt.screen_lines-1)
887                 {
888                   if(show_prompt(desc,i,validcount?count:0,localstr))
889                     break;
890                   else
891                     numlines=0;
892                 }
893
894               print_keyrec(i+1,keyrec);
895             }
896
897           numlines+=keyrec->lines;
898           iobuf_close(keyrec->uidbuf);
899           xfree(keyrec);
900
901           started=1;
902           i++;
903         }
904     }
905
906  notfound:
907   /* Leave this commented out or now, and perhaps for a very long
908      time.  All HKPish servers return HTML error messages for
909      no-key-found. */
910   /* 
911      if(!started)
912      log_info(_("keyserver does not support searching\n"));
913      else
914   */
915   if(count==0)
916     {
917       if(localstr)
918         log_info(_("key \"%s\" not found on keyserver\n"),localstr);
919       else
920         log_info(_("key not found on keyserver\n"));
921     }
922
923   xfree(localstr);
924   xfree(desc);
925   xfree(line);
926 }
927
928 /* We sometimes want to use a different gpgkeys_xxx for a given
929    protocol (for example, ldaps is handled by gpgkeys_ldap).  Map
930    these here. */
931 static const char *
932 keyserver_typemap(const char *type)
933 {
934   if(strcmp(type,"ldaps")==0)
935     return "ldap";
936   else
937     return type;
938 }
939
940 #ifdef GPGKEYS_CURL
941 /* The PGP LDAP and the curl fetch-a-LDAP-object methodologies are
942    sufficiently different that we can't use curl to do LDAP. */
943 static int
944 curl_cant_handle(const char *scheme,unsigned int direct_uri)
945 {
946   if(!direct_uri && (strcmp(scheme,"ldap")==0 || strcmp(scheme,"ldaps")==0))
947     return 1;
948
949   return 0;
950 }
951 #endif
952
953 #define KEYSERVER_ARGS_KEEP " -o \"%O\" \"%I\""
954 #define KEYSERVER_ARGS_NOKEEP " -o \"%o\" \"%i\""
955
956 static int 
957 keyserver_spawn(enum ks_action action,STRLIST list,KEYDB_SEARCH_DESC *desc,
958                 int count,int *prog,unsigned char **fpr,size_t *fpr_len,
959                 struct keyserver_spec *keyserver)
960 {
961   int ret=0,i,gotversion=0,outofband=0;
962   STRLIST temp;
963   unsigned int maxlen,buflen;
964   char *command,*end,*searchstr=NULL;
965   byte *line=NULL;
966   struct exec_info *spawn;
967   const char *scheme;
968   const char *libexecdir = get_libexecdir ();
969
970   assert(keyserver);
971
972 #ifdef EXEC_TEMPFILE_ONLY
973   opt.keyserver_options.options|=KEYSERVER_USE_TEMP_FILES;
974 #endif
975
976   /* Build the filename for the helper to execute */
977   scheme=keyserver_typemap(keyserver->scheme);
978
979 #ifdef DISABLE_KEYSERVER_PATH
980   /* Destroy any path we might have.  This is a little tricky,
981      portability-wise.  It's not correct to delete the PATH
982      environment variable, as that may fall back to a system built-in
983      PATH.  Similarly, it is not correct to set PATH to the null
984      string (PATH="") since this actually deletes the PATH environment
985      variable under MinGW.  The safest thing to do here is to force
986      PATH to be GNUPG_LIBEXECDIR.  All this is not that meaningful on
987      Unix-like systems (since we're going to give a full path to
988      gpgkeys_foo), but on W32 it prevents loading any DLLs from
989      directories in %PATH%.
990
991      After some more thinking about this we came to the conclusion
992      that it is better to load the helpers from the directory where
993      the program of this process lives.  Fortunately Windows provides
994      a way to retrieve this and our get_libexecdir function has been
995      modified to return just this.  Setting the exec-path is not
996      anymore required.  
997        set_exec_path(libexecdir);
998  */
999 #else
1000   if(opt.exec_path_set)
1001     {
1002       /* If exec-path was set, and DISABLE_KEYSERVER_PATH is
1003          undefined, then don't specify a full path to gpgkeys_foo, so
1004          that the PATH can work. */
1005       command=xmalloc(GPGKEYS_PREFIX_LEN+strlen(scheme)+3+strlen(EXEEXT)+1);
1006       command[0]='\0';
1007     }
1008   else
1009 #endif
1010     {
1011       /* Specify a full path to gpgkeys_foo. */
1012       command=xmalloc(strlen(libexecdir)+strlen(DIRSEP_S)+
1013                       GPGKEYS_PREFIX_LEN+strlen(scheme)+3+strlen(EXEEXT)+1);
1014       strcpy(command,libexecdir);
1015       strcat(command,DIRSEP_S);
1016     }
1017
1018   end=command+strlen(command);
1019
1020   strcat(command,GPGKEYS_PREFIX); 
1021   strcat(command,scheme);
1022
1023   if(keyserver->flags.direct_uri)
1024     strcat(command,"uri");
1025
1026   strcat(command,EXEEXT);
1027
1028 #ifdef GPGKEYS_CURL
1029   if(!curl_cant_handle(scheme,keyserver->flags.direct_uri)
1030      && path_access(command,X_OK)!=0)
1031     strcpy(end,GPGKEYS_CURL);
1032 #endif
1033
1034   if(opt.keyserver_options.options&KEYSERVER_USE_TEMP_FILES)
1035     {
1036       if(opt.keyserver_options.options&KEYSERVER_KEEP_TEMP_FILES)
1037         {
1038           command=xrealloc(command,strlen(command)+
1039                             strlen(KEYSERVER_ARGS_KEEP)+1);
1040           strcat(command,KEYSERVER_ARGS_KEEP);
1041         }
1042       else
1043         {
1044           command=xrealloc(command,strlen(command)+
1045                             strlen(KEYSERVER_ARGS_NOKEEP)+1);
1046           strcat(command,KEYSERVER_ARGS_NOKEEP);  
1047         }
1048
1049       ret=exec_write(&spawn,NULL,command,NULL,0,0);
1050     }
1051   else
1052     ret=exec_write(&spawn,command,NULL,NULL,0,0);
1053
1054   xfree(command);
1055
1056   if(ret)
1057     return ret;
1058
1059   fprintf(spawn->tochild,
1060           "# This is a GnuPG %s keyserver communications file\n",VERSION);
1061   fprintf(spawn->tochild,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
1062   fprintf(spawn->tochild,"PROGRAM %s\n",VERSION);
1063   fprintf(spawn->tochild,"SCHEME %s\n",keyserver->scheme);
1064
1065   if(keyserver->opaque)
1066     fprintf(spawn->tochild,"OPAQUE %s\n",keyserver->opaque);
1067   else
1068     {
1069       if(keyserver->auth)
1070         fprintf(spawn->tochild,"AUTH %s\n",keyserver->auth);
1071
1072       if(keyserver->host)
1073         fprintf(spawn->tochild,"HOST %s\n",keyserver->host);
1074
1075       if(keyserver->port)
1076         fprintf(spawn->tochild,"PORT %s\n",keyserver->port);
1077
1078       if(keyserver->path)
1079         fprintf(spawn->tochild,"PATH %s\n",keyserver->path);
1080     }
1081
1082   /* Write global options */
1083
1084   for(temp=opt.keyserver_options.other;temp;temp=temp->next)
1085     fprintf(spawn->tochild,"OPTION %s\n",temp->d);
1086
1087   /* Write per-keyserver options */
1088
1089   for(temp=keyserver->options;temp;temp=temp->next)
1090     fprintf(spawn->tochild,"OPTION %s\n",temp->d);
1091
1092   switch(action)
1093     {
1094     case KS_GET:
1095       {
1096         fprintf(spawn->tochild,"COMMAND GET\n\n");
1097
1098         /* Which keys do we want? */
1099
1100         for(i=0;i<count;i++)
1101           {
1102             int quiet=0;
1103
1104             if(desc[i].mode==KEYDB_SEARCH_MODE_FPR20)
1105               {
1106                 int f;
1107
1108                 fprintf(spawn->tochild,"0x");
1109
1110                 for(f=0;f<MAX_FINGERPRINT_LEN;f++)
1111                   fprintf(spawn->tochild,"%02X",desc[i].u.fpr[f]);
1112
1113                 fprintf(spawn->tochild,"\n");
1114               }
1115             else if(desc[i].mode==KEYDB_SEARCH_MODE_FPR16)
1116               {
1117                 int f;
1118
1119                 fprintf(spawn->tochild,"0x");
1120
1121                 for(f=0;f<16;f++)
1122                   fprintf(spawn->tochild,"%02X",desc[i].u.fpr[f]);
1123
1124                 fprintf(spawn->tochild,"\n");
1125               }
1126             else if(desc[i].mode==KEYDB_SEARCH_MODE_LONG_KID)
1127               fprintf(spawn->tochild,"0x%08lX%08lX\n",
1128                       (ulong)desc[i].u.kid[0],
1129                       (ulong)desc[i].u.kid[1]);
1130             else if(desc[i].mode==KEYDB_SEARCH_MODE_SHORT_KID)
1131               fprintf(spawn->tochild,"0x%08lX\n",
1132                       (ulong)desc[i].u.kid[1]);
1133             else if(desc[i].mode==KEYDB_SEARCH_MODE_EXACT)
1134               {
1135                 fprintf(spawn->tochild,"0x0000000000000000\n");
1136                 quiet=1;
1137               }
1138             else if(desc[i].mode==KEYDB_SEARCH_MODE_NONE)
1139               continue;
1140             else
1141               BUG();
1142
1143             if(!quiet)
1144               {
1145                 if(keyserver->host)
1146                   log_info(_("requesting key %s from %s server %s\n"),
1147                            keystr_from_desc(&desc[i]),
1148                            keyserver->scheme,keyserver->host);
1149                 else
1150                   log_info(_("requesting key %s from %s\n"),
1151                            keystr_from_desc(&desc[i]),keyserver->uri);
1152               }
1153           }
1154
1155         fprintf(spawn->tochild,"\n");
1156
1157         break;
1158       }
1159
1160     case KS_GETNAME:
1161       {
1162         STRLIST key;
1163
1164         fprintf(spawn->tochild,"COMMAND GETNAME\n\n");
1165
1166         /* Which names do we want? */
1167
1168         for(key=list;key!=NULL;key=key->next)
1169           fprintf(spawn->tochild,"%s\n",key->d);
1170
1171         fprintf(spawn->tochild,"\n");
1172
1173         if(keyserver->host)
1174           log_info(_("searching for names from %s server %s\n"),
1175                    keyserver->scheme,keyserver->host);
1176         else
1177           log_info(_("searching for names from %s\n"),keyserver->uri);
1178
1179         break;
1180       }
1181
1182     case KS_SEND:
1183       {
1184         STRLIST key;
1185
1186         /* Note the extra \n here to send an empty keylist block */
1187         fprintf(spawn->tochild,"COMMAND SEND\n\n\n");
1188
1189         for(key=list;key!=NULL;key=key->next)
1190           {
1191             armor_filter_context_t afx;
1192             IOBUF buffer=iobuf_temp();
1193             KBNODE block;
1194
1195             temp=NULL;
1196             add_to_strlist(&temp,key->d);
1197
1198             memset(&afx,0,sizeof(afx));
1199             afx.what=1;
1200             /* Tell the armor filter to use Unix-style \n line
1201                endings, since we're going to fprintf this to a file
1202                that (on Win32) is open in text mode.  The win32 stdio
1203                will transform the \n to \r\n and we'll end up with the
1204                proper line endings on win32.  This is a no-op on
1205                Unix. */
1206             afx.eol[0]='\n';
1207             iobuf_push_filter(buffer,armor_filter,&afx);
1208
1209             /* TODO: Remove Comment: lines from keys exported this
1210                way? */
1211
1212             if(export_pubkeys_stream(buffer,temp,&block,
1213                                      opt.keyserver_options.export_options)==-1)
1214               iobuf_close(buffer);
1215             else
1216               {
1217                 KBNODE node;
1218
1219                 iobuf_flush_temp(buffer);
1220
1221                 merge_keys_and_selfsig(block);
1222
1223                 fprintf(spawn->tochild,"INFO %08lX%08lX BEGIN\n",
1224                         (ulong)block->pkt->pkt.public_key->keyid[0],
1225                         (ulong)block->pkt->pkt.public_key->keyid[1]);
1226
1227                 for(node=block;node;node=node->next)
1228                   {
1229                     switch(node->pkt->pkttype)
1230                       {
1231                       default:
1232                         continue;
1233
1234                       case PKT_PUBLIC_KEY:
1235                       case PKT_PUBLIC_SUBKEY:
1236                         {
1237                           PKT_public_key *pk=node->pkt->pkt.public_key;
1238
1239                           keyid_from_pk(pk,NULL);
1240
1241                           fprintf(spawn->tochild,"%sb:%08lX%08lX:%u:%u:%u:%u:",
1242                                   node->pkt->pkttype==PKT_PUBLIC_KEY?"pu":"su",
1243                                   (ulong)pk->keyid[0],(ulong)pk->keyid[1],
1244                                   pk->pubkey_algo,
1245                                   nbits_from_pk(pk),
1246                                   pk->timestamp,
1247                                   pk->expiredate);
1248
1249                           if(pk->is_revoked)
1250                             fprintf(spawn->tochild,"r");
1251                           if(pk->has_expired)
1252                             fprintf(spawn->tochild,"e");
1253
1254                           fprintf(spawn->tochild,"\n");
1255                         }
1256                         break;
1257
1258                       case PKT_USER_ID:
1259                         {
1260                           PKT_user_id *uid=node->pkt->pkt.user_id;
1261                           int r;
1262
1263                           if(uid->attrib_data)
1264                             continue;
1265
1266                           fprintf(spawn->tochild,"uid:");
1267
1268                           /* Quote ':', '%', and any 8-bit
1269                              characters */
1270                           for(r=0;r<uid->len;r++)
1271                             {
1272                               if(uid->name[r]==':' || uid->name[r]=='%'
1273                                  || uid->name[r]&0x80)
1274                                 fprintf(spawn->tochild,"%%%02X",
1275                                         (byte)uid->name[r]);
1276                               else
1277                                 fprintf(spawn->tochild,"%c",uid->name[r]);
1278                             }
1279
1280                           fprintf(spawn->tochild,":%u:%u:",
1281                                   uid->created,uid->expiredate);
1282
1283                           if(uid->is_revoked)
1284                             fprintf(spawn->tochild,"r");
1285                           if(uid->is_expired)
1286                             fprintf(spawn->tochild,"e");
1287
1288                           fprintf(spawn->tochild,"\n");
1289                         }
1290                         break;
1291
1292                         /* This bit is really for the benefit of
1293                            people who store their keys in LDAP
1294                            servers.  It makes it easy to do queries
1295                            for things like "all keys signed by
1296                            Isabella". */
1297                       case PKT_SIGNATURE:
1298                         {
1299                           PKT_signature *sig=node->pkt->pkt.signature;
1300
1301                           if(!IS_UID_SIG(sig))
1302                             continue;
1303
1304                           fprintf(spawn->tochild,"sig:%08lX%08lX:%X:%u:%u\n",
1305                                   (ulong)sig->keyid[0],(ulong)sig->keyid[1],
1306                                   sig->sig_class,sig->timestamp,
1307                                   sig->expiredate);
1308                         }
1309                         break;
1310                       }
1311                   }
1312
1313                 fprintf(spawn->tochild,"INFO %08lX%08lX END\n",
1314                         (ulong)block->pkt->pkt.public_key->keyid[0],
1315                         (ulong)block->pkt->pkt.public_key->keyid[1]);
1316
1317                 fprintf(spawn->tochild,"KEY %s BEGIN\n",key->d);
1318                 fwrite(iobuf_get_temp_buffer(buffer),
1319                        iobuf_get_temp_length(buffer),1,spawn->tochild);
1320                 fprintf(spawn->tochild,"KEY %s END\n",key->d);
1321
1322                 iobuf_close(buffer);
1323
1324                 if(keyserver->host)
1325                   log_info(_("sending key %s to %s server %s\n"),
1326                            keystr(block->pkt->pkt.public_key->keyid),
1327                            keyserver->scheme,keyserver->host);
1328                 else
1329                   log_info(_("sending key %s to %s\n"),
1330                            keystr(block->pkt->pkt.public_key->keyid),
1331                            keyserver->uri);
1332
1333                 release_kbnode(block);
1334               }
1335
1336             free_strlist(temp);
1337           }
1338
1339         break;
1340       }
1341
1342     case KS_SEARCH:
1343       {
1344         STRLIST key;
1345
1346         fprintf(spawn->tochild,"COMMAND SEARCH\n\n");
1347
1348         /* Which keys do we want?  Remember that the gpgkeys_ program
1349            is going to lump these together into a search string. */
1350
1351         for(key=list;key!=NULL;key=key->next)
1352           {
1353             fprintf(spawn->tochild,"%s\n",key->d);
1354             if(key!=list)
1355               {
1356                 searchstr=xrealloc(searchstr,
1357                                     strlen(searchstr)+strlen(key->d)+2);
1358                 strcat(searchstr," ");
1359               }
1360             else
1361               {
1362                 searchstr=xmalloc(strlen(key->d)+1);
1363                 searchstr[0]='\0';
1364               }
1365
1366             strcat(searchstr,key->d);
1367           }
1368
1369         fprintf(spawn->tochild,"\n");
1370
1371         if(keyserver->host)
1372           log_info(_("searching for \"%s\" from %s server %s\n"),
1373                    searchstr,keyserver->scheme,keyserver->host);
1374         else
1375           log_info(_("searching for \"%s\" from %s\n"),
1376                    searchstr,keyserver->uri);
1377
1378         break;
1379       }
1380
1381     default:
1382       log_fatal(_("no keyserver action!\n"));
1383       break;
1384     }
1385
1386   /* Done sending, so start reading. */
1387   ret=exec_read(spawn);
1388   if(ret)
1389     goto fail;
1390
1391   /* Now handle the response */
1392
1393   for(;;)
1394     {
1395       int plen;
1396       char *ptr;
1397
1398       maxlen=1024;
1399       if(iobuf_read_line(spawn->fromchild,&line,&buflen,&maxlen)==0)
1400         {
1401           ret=G10ERR_READ_FILE;
1402           goto fail; /* i.e. EOF */
1403         }
1404
1405       ptr=line;
1406
1407       /* remove trailing whitespace */
1408       plen=strlen(ptr);
1409       while(plen>0 && ascii_isspace(ptr[plen-1]))
1410         plen--;
1411       plen[ptr]='\0';
1412
1413       if(*ptr=='\0')
1414         break;
1415
1416       if(ascii_strncasecmp(ptr,"VERSION ",8)==0)
1417         {
1418           gotversion=1;
1419
1420           if(atoi(&ptr[8])!=KEYSERVER_PROTO_VERSION)
1421             {
1422               log_error(_("invalid keyserver protocol (us %d!=handler %d)\n"),
1423                         KEYSERVER_PROTO_VERSION,atoi(&ptr[8]));
1424               goto fail;
1425             }
1426         }
1427       else if(ascii_strncasecmp(ptr,"PROGRAM ",8)==0)
1428         {
1429           if(ascii_strncasecmp(&ptr[8],VERSION,strlen(VERSION))!=0)
1430             log_info(_("WARNING: keyserver handler from a different"
1431                        " version of GnuPG (%s)\n"),&ptr[8]);
1432         }
1433       else if(ascii_strncasecmp(ptr,"OPTION OUTOFBAND",16)==0)
1434         outofband=1; /* Currently the only OPTION */
1435     }
1436
1437   if(!gotversion)
1438     {
1439       log_error(_("keyserver did not send VERSION\n"));
1440       goto fail;
1441     }
1442
1443   if(!outofband)
1444     switch(action)
1445       {
1446       case KS_GET:
1447       case KS_GETNAME:
1448         {
1449           void *stats_handle;
1450
1451           stats_handle=import_new_stats_handle();
1452
1453           /* Slurp up all the key data.  In the future, it might be
1454              nice to look for KEY foo OUTOFBAND and FAILED indicators.
1455              It's harmless to ignore them, but ignoring them does make
1456              gpg complain about "no valid OpenPGP data found".  One
1457              way to do this could be to continue parsing this
1458              line-by-line and make a temp iobuf for each key. */
1459
1460           import_keys_stream(spawn->fromchild,stats_handle,fpr,fpr_len,
1461                              opt.keyserver_options.import_options);
1462
1463           import_print_stats(stats_handle);
1464           import_release_stats_handle(stats_handle);
1465
1466           break;
1467         }
1468
1469         /* Nothing to do here */
1470       case KS_SEND:
1471         break;
1472
1473       case KS_SEARCH:
1474         keyserver_search_prompt(spawn->fromchild,searchstr);
1475         break;
1476
1477       default:
1478         log_fatal(_("no keyserver action!\n"));
1479         break;
1480       }
1481
1482  fail:
1483   xfree(line);
1484   xfree(searchstr);
1485
1486
1487   *prog=exec_finish(spawn);
1488
1489   return ret;
1490 }
1491
1492 static int 
1493 keyserver_work(enum ks_action action,STRLIST list,KEYDB_SEARCH_DESC *desc,
1494                int count,unsigned char **fpr,size_t *fpr_len,
1495                struct keyserver_spec *keyserver)
1496 {
1497   int rc=0,ret=0;
1498
1499   if(!keyserver)
1500     {
1501       log_error(_("no keyserver known (use option --keyserver)\n"));
1502       return G10ERR_BAD_URI;
1503     }
1504
1505 #ifdef DISABLE_KEYSERVER_HELPERS
1506
1507   log_error(_("external keyserver calls are not supported in this build\n"));
1508   return G10ERR_KEYSERVER;
1509
1510 #else
1511   /* Spawn a handler */
1512
1513   rc=keyserver_spawn(action,list,desc,count,&ret,fpr,fpr_len,keyserver);
1514   if(ret)
1515     {
1516       switch(ret)
1517         {
1518         case KEYSERVER_SCHEME_NOT_FOUND:
1519           log_error(_("no handler for keyserver scheme `%s'\n"),
1520                     keyserver->scheme);
1521           break;
1522
1523         case KEYSERVER_NOT_SUPPORTED:
1524           log_error(_("action `%s' not supported with keyserver "
1525                       "scheme `%s'\n"),
1526                     action==KS_GET?"get":action==KS_SEND?"send":
1527                     action==KS_SEARCH?"search":"unknown",
1528                     keyserver->scheme);
1529           break;
1530
1531         case KEYSERVER_VERSION_ERROR:
1532           log_error(_(GPGKEYS_PREFIX "%s does not support"
1533                       " handler version %d\n"),
1534                     keyserver_typemap(keyserver->scheme),
1535                     KEYSERVER_PROTO_VERSION);
1536           break;
1537
1538         case KEYSERVER_TIMEOUT:
1539           log_error(_("keyserver timed out\n"));
1540           break;
1541
1542         case KEYSERVER_INTERNAL_ERROR:
1543         default:
1544           log_error(_("keyserver internal error\n"));
1545           break;
1546         }
1547
1548       return G10ERR_KEYSERVER;
1549     }
1550
1551   if(rc)
1552     {
1553       log_error(_("keyserver communications error: %s\n"),g10_errstr(rc));
1554
1555       return rc;
1556     }
1557
1558   return 0;
1559 #endif /* ! DISABLE_KEYSERVER_HELPERS*/
1560 }
1561
1562 int 
1563 keyserver_export(STRLIST users)
1564 {
1565   STRLIST sl=NULL;
1566   KEYDB_SEARCH_DESC desc;
1567   int rc=0;
1568
1569   /* Weed out descriptors that we don't support sending */
1570   for(;users;users=users->next)
1571     {
1572       classify_user_id (users->d, &desc);
1573       if(desc.mode!=KEYDB_SEARCH_MODE_SHORT_KID &&
1574          desc.mode!=KEYDB_SEARCH_MODE_LONG_KID &&
1575          desc.mode!=KEYDB_SEARCH_MODE_FPR16 &&
1576          desc.mode!=KEYDB_SEARCH_MODE_FPR20)
1577         {
1578           log_error(_("\"%s\" not a key ID: skipping\n"),users->d);
1579           continue;
1580         }
1581       else
1582         append_to_strlist(&sl,users->d);
1583     }
1584
1585   if(sl)
1586     {
1587       rc=keyserver_work(KS_SEND,sl,NULL,0,NULL,NULL,opt.keyserver);
1588       free_strlist(sl);
1589     }
1590
1591   return rc;
1592 }
1593
1594 int 
1595 keyserver_import(STRLIST users)
1596 {
1597   KEYDB_SEARCH_DESC *desc;
1598   int num=100,count=0;
1599   int rc=0;
1600
1601   /* Build a list of key ids */
1602   desc=xmalloc(sizeof(KEYDB_SEARCH_DESC)*num);
1603
1604   for(;users;users=users->next)
1605     {
1606       classify_user_id (users->d, &desc[count]);
1607       if(desc[count].mode!=KEYDB_SEARCH_MODE_SHORT_KID &&
1608          desc[count].mode!=KEYDB_SEARCH_MODE_LONG_KID &&
1609          desc[count].mode!=KEYDB_SEARCH_MODE_FPR16 &&
1610          desc[count].mode!=KEYDB_SEARCH_MODE_FPR20)
1611         {
1612           log_error(_("\"%s\" not a key ID: skipping\n"),users->d);
1613           continue;
1614         }
1615
1616       count++;
1617       if(count==num)
1618         {
1619           num+=100;
1620           desc=xrealloc(desc,sizeof(KEYDB_SEARCH_DESC)*num);
1621         }
1622     }
1623
1624   if(count>0)
1625     rc=keyserver_work(KS_GET,NULL,desc,count,NULL,NULL,opt.keyserver);
1626
1627   xfree(desc);
1628
1629   return rc;
1630 }
1631
1632 int
1633 keyserver_import_fprint(const byte *fprint,size_t fprint_len,
1634                         struct keyserver_spec *keyserver)
1635 {
1636   KEYDB_SEARCH_DESC desc;
1637
1638   memset(&desc,0,sizeof(desc));
1639
1640   if(fprint_len==16)
1641     desc.mode=KEYDB_SEARCH_MODE_FPR16;
1642   else if(fprint_len==20)
1643     desc.mode=KEYDB_SEARCH_MODE_FPR20;
1644   else
1645     return -1;
1646
1647   memcpy(desc.u.fpr,fprint,fprint_len);
1648
1649   /* TODO: Warn here if the fingerprint we got doesn't match the one
1650      we asked for? */
1651   return keyserver_work(KS_GET,NULL,&desc,1,NULL,NULL,keyserver);
1652 }
1653
1654 int 
1655 keyserver_import_keyid(u32 *keyid,struct keyserver_spec *keyserver)
1656 {
1657   KEYDB_SEARCH_DESC desc;
1658
1659   memset(&desc,0,sizeof(desc));
1660
1661   desc.mode=KEYDB_SEARCH_MODE_LONG_KID;
1662   desc.u.kid[0]=keyid[0];
1663   desc.u.kid[1]=keyid[1];
1664
1665   return keyserver_work(KS_GET,NULL,&desc,1,NULL,NULL,keyserver);
1666 }
1667
1668 /* code mostly stolen from do_export_stream */
1669 static int 
1670 keyidlist(STRLIST users,KEYDB_SEARCH_DESC **klist,int *count,int fakev3)
1671 {
1672   int rc=0,ndesc,num=100;
1673   KBNODE keyblock=NULL,node;
1674   KEYDB_HANDLE kdbhd;
1675   KEYDB_SEARCH_DESC *desc;
1676   STRLIST sl;
1677
1678   *count=0;
1679
1680   *klist=xmalloc(sizeof(KEYDB_SEARCH_DESC)*num);
1681
1682   kdbhd=keydb_new(0);
1683
1684   if(!users)
1685     {
1686       ndesc = 1;
1687       desc = xmalloc_clear ( ndesc * sizeof *desc);
1688       desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
1689     }
1690   else
1691     {
1692       for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++) 
1693         ;
1694       desc = xmalloc ( ndesc * sizeof *desc);
1695         
1696       for (ndesc=0, sl=users; sl; sl = sl->next)
1697         {
1698           if(classify_user_id (sl->d, desc+ndesc))
1699             ndesc++;
1700           else
1701             log_error (_("key \"%s\" not found: %s\n"),
1702                        sl->d, g10_errstr (G10ERR_INV_USER_ID));
1703         }
1704     }
1705
1706   while (!(rc = keydb_search (kdbhd, desc, ndesc)))
1707     {
1708       if (!users) 
1709         desc[0].mode = KEYDB_SEARCH_MODE_NEXT;
1710
1711       /* read the keyblock */
1712       rc = keydb_get_keyblock (kdbhd, &keyblock );
1713       if( rc )
1714         {
1715           log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) );
1716           goto leave;
1717         }
1718
1719       if((node=find_kbnode(keyblock,PKT_PUBLIC_KEY)))
1720         {
1721           /* This is to work around a bug in some keyservers (pksd and
1722              OKS) that calculate v4 RSA keyids as if they were v3 RSA.
1723              The answer is to refresh both the correct v4 keyid
1724              (e.g. 99242560) and the fake v3 keyid (e.g. 68FDDBC7).
1725              This only happens for key refresh using the HKP scheme
1726              and if the refresh-add-fake-v3-keyids keyserver option is
1727              set. */
1728           if(fakev3 && is_RSA(node->pkt->pkt.public_key->pubkey_algo) &&
1729              node->pkt->pkt.public_key->version>=4)
1730             {
1731               (*klist)[*count].mode=KEYDB_SEARCH_MODE_LONG_KID;
1732               mpi_get_keyid(node->pkt->pkt.public_key->pkey[0],
1733                             (*klist)[*count].u.kid);
1734               (*count)++;
1735
1736               if(*count==num)
1737                 {
1738                   num+=100;
1739                   *klist=xrealloc(*klist,sizeof(KEYDB_SEARCH_DESC)*num);
1740                 }
1741             }
1742
1743           /* v4 keys get full fingerprints.  v3 keys get long keyids.
1744              This is because it's easy to calculate any sort of keyid
1745              from a v4 fingerprint, but not a v3 fingerprint. */
1746
1747           if(node->pkt->pkt.public_key->version<4)
1748             {
1749               (*klist)[*count].mode=KEYDB_SEARCH_MODE_LONG_KID;
1750               keyid_from_pk(node->pkt->pkt.public_key,
1751                             (*klist)[*count].u.kid);
1752             }
1753           else
1754             {
1755               size_t dummy;
1756
1757               (*klist)[*count].mode=KEYDB_SEARCH_MODE_FPR20;
1758               fingerprint_from_pk(node->pkt->pkt.public_key,
1759                                   (*klist)[*count].u.fpr,&dummy);
1760             }
1761
1762           /* This is a little hackish, using the skipfncvalue as a
1763              void* pointer to the keyserver spec, but we don't need
1764              the skipfnc here, and it saves having an additional field
1765              for this (which would be wasted space most of the
1766              time). */
1767
1768           (*klist)[*count].skipfncvalue=NULL;
1769
1770           /* Are we honoring preferred keyservers? */
1771           if(opt.keyserver_options.options&KEYSERVER_HONOR_KEYSERVER_URL)
1772             {
1773               PKT_user_id *uid=NULL;
1774               PKT_signature *sig=NULL;
1775
1776               merge_keys_and_selfsig(keyblock);
1777
1778               for(node=node->next;node;node=node->next)
1779                 {
1780                   if(node->pkt->pkttype==PKT_USER_ID
1781                      && node->pkt->pkt.user_id->is_primary)
1782                     uid=node->pkt->pkt.user_id;
1783                   else if(node->pkt->pkttype==PKT_SIGNATURE
1784                           && node->pkt->pkt.signature->
1785                           flags.chosen_selfsig && uid)
1786                     {
1787                       sig=node->pkt->pkt.signature;
1788                       break;
1789                     }
1790                 }
1791
1792               /* Try and parse the keyserver URL.  If it doesn't work,
1793                  then we end up writing NULL which indicates we are
1794                  the same as any other key. */
1795               if(sig)
1796                 (*klist)[*count].skipfncvalue=parse_preferred_keyserver(sig);
1797             }
1798
1799           (*count)++;
1800
1801           if(*count==num)
1802             {
1803               num+=100;
1804               *klist=xrealloc(*klist,sizeof(KEYDB_SEARCH_DESC)*num);
1805             }
1806         }
1807     }
1808
1809   if(rc==-1)
1810     rc=0;
1811   
1812  leave:
1813   if(rc)
1814     xfree(*klist);
1815   xfree(desc);
1816   keydb_release(kdbhd);
1817   release_kbnode(keyblock);
1818
1819   return rc;
1820 }
1821
1822 /* Note this is different than the original HKP refresh.  It allows
1823    usernames to refresh only part of the keyring. */
1824
1825 int
1826 keyserver_refresh(STRLIST users)
1827 {
1828   int rc,count,numdesc,fakev3=0;
1829   KEYDB_SEARCH_DESC *desc;
1830   unsigned int options=opt.keyserver_options.import_options;
1831
1832   /* We switch merge-only on during a refresh, as 'refresh' should
1833      never import new keys, even if their keyids match. */
1834   opt.keyserver_options.import_options|=IMPORT_MERGE_ONLY;
1835
1836   /* Similarly, we switch on fast-import, since refresh may make
1837      multiple import sets (due to preferred keyserver URLs).  We don't
1838      want each set to rebuild the trustdb.  Instead we do it once at
1839      the end here. */
1840   opt.keyserver_options.import_options|=IMPORT_FAST;
1841
1842   /* If refresh_add_fake_v3_keyids is on and it's a HKP or MAILTO
1843      scheme, then enable fake v3 keyid generation. */
1844   if((opt.keyserver_options.options&KEYSERVER_ADD_FAKE_V3) && opt.keyserver
1845      && (ascii_strcasecmp(opt.keyserver->scheme,"hkp")==0 ||
1846          ascii_strcasecmp(opt.keyserver->scheme,"mailto")==0))
1847     fakev3=1;
1848
1849   rc=keyidlist(users,&desc,&numdesc,fakev3);
1850   if(rc)
1851     return rc;
1852
1853   count=numdesc;
1854   if(count>0)
1855     {
1856       int i;
1857
1858       /* Try to handle preferred keyserver keys first */
1859       for(i=0;i<numdesc;i++)
1860         {
1861           if(desc[i].skipfncvalue)
1862             {
1863               struct keyserver_spec *keyserver=desc[i].skipfncvalue;
1864
1865               /* We use the keyserver structure we parsed out before.
1866                  Note that a preferred keyserver without a scheme://
1867                  will be interpreted as hkp:// */
1868
1869               rc=keyserver_work(KS_GET,NULL,&desc[i],1,NULL,NULL,keyserver);
1870               if(rc)
1871                 log_info(_("WARNING: unable to refresh key %s"
1872                            " via %s: %s\n"),keystr_from_desc(&desc[i]),
1873                          keyserver->uri,g10_errstr(rc));
1874               else
1875                 {
1876                   /* We got it, so mark it as NONE so we don't try and
1877                      get it again from the regular keyserver. */
1878
1879                   desc[i].mode=KEYDB_SEARCH_MODE_NONE;
1880                   count--;
1881                 }
1882
1883               free_keyserver_spec(keyserver);
1884             }
1885         }
1886     }
1887
1888   if(count>0)
1889     {
1890       if(opt.keyserver)
1891         {
1892           if(count==1)
1893             log_info(_("refreshing 1 key from %s\n"),opt.keyserver->uri);
1894           else
1895             log_info(_("refreshing %d keys from %s\n"),
1896                      count,opt.keyserver->uri);
1897         }
1898
1899       rc=keyserver_work(KS_GET,NULL,desc,numdesc,NULL,NULL,opt.keyserver);
1900     }
1901
1902   xfree(desc);
1903
1904   opt.keyserver_options.import_options=options;
1905
1906   /* If the original options didn't have fast import, and the trustdb
1907      is dirty, rebuild. */
1908   if(!(opt.keyserver_options.import_options&IMPORT_FAST))
1909     trustdb_check_or_update();
1910
1911   return rc;
1912 }
1913
1914 int
1915 keyserver_search(STRLIST tokens)
1916 {
1917   if(tokens)
1918     return keyserver_work(KS_SEARCH,tokens,NULL,0,NULL,NULL,opt.keyserver);
1919   else
1920     return 0;
1921 }
1922
1923 int
1924 keyserver_fetch(STRLIST urilist)
1925 {
1926   KEYDB_SEARCH_DESC desc;
1927   STRLIST sl;
1928   unsigned int options=opt.keyserver_options.import_options;
1929
1930   /* Switch on fast-import, since fetch can handle more than one
1931      import and we don't want each set to rebuild the trustdb.
1932      Instead we do it once at the end. */
1933   opt.keyserver_options.import_options|=IMPORT_FAST;
1934
1935   /* A dummy desc since we're not actually fetching a particular key
1936      ID */
1937   memset(&desc,0,sizeof(desc));
1938   desc.mode=KEYDB_SEARCH_MODE_EXACT;
1939
1940   for(sl=urilist;sl;sl=sl->next)
1941     {
1942       struct keyserver_spec *spec;
1943
1944       spec=parse_keyserver_uri(sl->d,1,NULL,0);
1945       if(spec)
1946         {
1947           int rc;
1948
1949           /*
1950             Set the direct_uri flag so we know later to call a direct
1951             handler instead of the keyserver style.  This lets us use
1952             gpgkeys_curl or gpgkeys_ldapuri instead of gpgkeys_ldap to
1953             fetch things like
1954             ldap://keyserver.pgp.com/o=PGP%20keys?pgpkey?sub?pgpkeyid=99242560
1955           */
1956           spec->flags.direct_uri=1;
1957
1958           rc=keyserver_work(KS_GET,NULL,&desc,1,NULL,NULL,spec);
1959           if(rc)
1960             log_info (_("WARNING: unable to fetch URI %s: %s\n"),
1961                      sl->d,g10_errstr(rc));
1962
1963           free_keyserver_spec(spec);
1964         }
1965       else
1966         log_info (_("WARNING: unable to parse URI %s\n"),sl->d);
1967     }
1968
1969   opt.keyserver_options.import_options=options;
1970
1971   /* If the original options didn't have fast import, and the trustdb
1972      is dirty, rebuild. */
1973   if(!(opt.keyserver_options.import_options&IMPORT_FAST))
1974     trustdb_check_or_update();
1975
1976   return 0;
1977 }
1978
1979 /* Import key in a CERT or pointed to by a CERT */
1980 int
1981 keyserver_import_cert(const char *name,unsigned char **fpr,size_t *fpr_len)
1982 {
1983   char *domain,*look,*url;
1984   IOBUF key;
1985   int type,rc=G10ERR_GENERAL;
1986
1987   look=xstrdup(name);
1988
1989   domain=strrchr(look,'@');
1990   if(domain)
1991     *domain='.';
1992
1993   type=get_cert(look,max_cert_size,&key,fpr,fpr_len,&url);
1994   if(type==1)
1995     {
1996       int armor_status=opt.no_armor;
1997
1998       /* CERTs are always in binary format */
1999       opt.no_armor=1;
2000
2001       rc=import_keys_stream(key,NULL,fpr,fpr_len,
2002                             opt.keyserver_options.import_options);
2003
2004       opt.no_armor=armor_status;
2005
2006       iobuf_close(key);
2007     }
2008   else if(type==2 && *fpr)
2009     {
2010       /* We only consider the IPGP type if a fingerprint was provided.
2011          This lets us select the right key regardless of what a URL
2012          points to, or get the key from a keyserver. */
2013       if(url)
2014         {
2015           struct keyserver_spec *spec;
2016
2017           spec=parse_keyserver_uri(url,1,NULL,0);
2018           if(spec)
2019             {
2020               STRLIST list=NULL;
2021
2022               add_to_strlist(&list,url);
2023
2024               rc=keyserver_fetch(list);
2025
2026               free_strlist(list);
2027               free_keyserver_spec(spec);
2028             }
2029         }
2030       else if(opt.keyserver)
2031         {
2032           /* If only a fingerprint is provided, try and fetch it from
2033              our --keyserver */
2034
2035           rc=keyserver_import_fprint(*fpr,*fpr_len,opt.keyserver);
2036         }
2037
2038       xfree(url);
2039     }
2040
2041   xfree(look);
2042
2043   return rc;
2044 }
2045
2046 /* Import key pointed to by a PKA record. Return the requested
2047    fingerprint in fpr. */
2048 int
2049 keyserver_import_pka(const char *name,unsigned char **fpr,size_t *fpr_len)
2050 {
2051   char *uri;
2052   int rc=-1;
2053
2054   *fpr=xmalloc(20);
2055   *fpr_len=20;
2056
2057   uri = get_pka_info (name, *fpr);
2058   if (uri)
2059     {
2060       struct keyserver_spec *spec;
2061       spec = parse_keyserver_uri (uri, 1, NULL, 0);
2062       if (spec)
2063         {
2064           rc=keyserver_import_fprint (*fpr, 20, spec);
2065           free_keyserver_spec (spec);
2066         }
2067       xfree (uri);
2068     }
2069
2070   if(rc!=0)
2071     xfree(*fpr);
2072
2073   return rc;
2074 }
2075
2076 /* Import all keys that match name */
2077 int
2078 keyserver_import_name(const char *name,unsigned char **fpr,size_t *fpr_len,
2079                       struct keyserver_spec *keyserver)
2080 {
2081   STRLIST list=NULL;
2082   int rc;
2083
2084   append_to_strlist(&list,name);
2085
2086   rc=keyserver_work(KS_GETNAME,list,NULL,0,fpr,fpr_len,keyserver);
2087
2088   free_strlist(list);
2089
2090   return rc;
2091 }
2092
2093 /* Use the PGP Universal trick of asking ldap://keys.(maildomain) for
2094    the key. */
2095 int
2096 keyserver_import_ldap(const char *name,unsigned char **fpr,size_t *fpr_len)
2097 {
2098   char *domain;
2099   struct keyserver_spec *keyserver;
2100   STRLIST list=NULL;
2101   int rc;
2102
2103   append_to_strlist(&list,name);
2104
2105   /* Parse out the domain */
2106   domain=strrchr(name,'@');
2107   if(!domain)
2108     return G10ERR_GENERAL;
2109
2110   domain++;
2111
2112   keyserver=xmalloc_clear(sizeof(struct keyserver_spec));
2113
2114   keyserver->scheme=xstrdup("ldap");
2115   keyserver->host=xmalloc(5+strlen(domain)+1);
2116   strcpy(keyserver->host,"keys.");
2117   strcat(keyserver->host,domain);
2118   keyserver->uri=xmalloc(strlen(keyserver->scheme)+
2119                          3+strlen(keyserver->host)+1);
2120   strcpy(keyserver->uri,keyserver->scheme);
2121   strcat(keyserver->uri,"://");
2122   strcat(keyserver->uri,keyserver->host);
2123     
2124   rc=keyserver_work(KS_GETNAME,list,NULL,0,fpr,fpr_len,keyserver);
2125
2126   free_strlist(list);
2127
2128   free_keyserver_spec(keyserver);
2129
2130   return rc;
2131 }