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