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