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