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