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