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