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