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