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