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