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