1 /* keyserver.c - generic keyserver code
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 * 2006 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 #include "keyserver-internal.h"
46 KEYDB_SEARCH_DESC desc
;
47 u32 createtime
,expiretime
;
54 enum ks_action
{KS_UNKNOWN
=0,KS_GET
,KS_GETNAME
,KS_SEND
,KS_SEARCH
};
56 static struct parse_options keyserver_opts
[]=
58 /* some of these options are not real - just for the help
60 {"max-cert-size",0,NULL
,NULL
},
61 {"include-revoked",0,NULL
,N_("include revoked keys in search results")},
62 {"include-subkeys",0,NULL
,N_("include subkeys when searching by key ID")},
63 {"use-temp-files",0,NULL
,
64 N_("use temporary files to pass data to keyserver helpers")},
65 {"keep-temp-files",KEYSERVER_KEEP_TEMP_FILES
,NULL
,
66 N_("do not delete temporary files after using them")},
67 {"refresh-add-fake-v3-keyids",KEYSERVER_ADD_FAKE_V3
,NULL
,
69 {"auto-key-retrieve",KEYSERVER_AUTO_KEY_RETRIEVE
,NULL
,
70 N_("automatically retrieve keys when verifying signatures")},
71 {"honor-keyserver-url",KEYSERVER_HONOR_KEYSERVER_URL
,NULL
,
72 N_("honor the preferred keyserver URL set on the key")},
73 {"honor-pka-record",KEYSERVER_HONOR_PKA_RECORD
,NULL
,
74 N_("honor the PKA record set on a key when retrieving keys")},
78 static int keyserver_work(enum ks_action action
,STRLIST list
,
79 KEYDB_SEARCH_DESC
*desc
,int count
,
80 unsigned char **fpr
,size_t *fpr_len
,
81 struct keyserver_spec
*keyserver
);
83 /* Reasonable guess */
84 #define DEFAULT_MAX_CERT_SIZE 16384
86 static size_t max_cert_size
=DEFAULT_MAX_CERT_SIZE
;
89 add_canonical_option(char *option
,STRLIST
*list
)
91 char *arg
=argsplit(option
);
97 joined
=xmalloc(strlen(option
)+1+strlen(arg
)+1);
98 /* Make a canonical name=value form with no spaces */
99 strcpy(joined
,option
);
102 append_to_strlist(list
,joined
);
106 append_to_strlist(list
,option
);
110 parse_keyserver_options(char *options
)
116 keyserver_opts
[0].value
=&max_cert
;
118 while((tok
=optsep(&options
)))
123 /* For backwards compatibility. 1.2.x used honor-http-proxy and
124 there are a good number of documents published that recommend
126 if(ascii_strcasecmp(tok
,"honor-http-proxy")==0)
128 else if(ascii_strcasecmp(tok
,"no-honor-http-proxy")==0)
131 /* We accept quite a few possible options here - some options to
132 handle specially, the keyserver_options list, and import and
133 export options that pertain to keyserver operations. Note
134 that you must use strncasecmp here as there might be an
135 =argument attached which will foil the use of strcasecmp. */
137 #ifdef EXEC_TEMPFILE_ONLY
138 if(ascii_strncasecmp(tok
,"use-temp-files",14)==0 ||
139 ascii_strncasecmp(tok
,"no-use-temp-files",17)==0)
140 log_info(_("WARNING: keyserver option `%s' is not used"
141 " on this platform\n"),tok
);
143 if(ascii_strncasecmp(tok
,"use-temp-files",14)==0)
144 opt
.keyserver_options
.options
|=KEYSERVER_USE_TEMP_FILES
;
145 else if(ascii_strncasecmp(tok
,"no-use-temp-files",17)==0)
146 opt
.keyserver_options
.options
&=~KEYSERVER_USE_TEMP_FILES
;
148 else if(!parse_options(tok
,&opt
.keyserver_options
.options
,
150 && !parse_import_options(tok
,
151 &opt
.keyserver_options
.import_options
,0)
152 && !parse_export_options(tok
,
153 &opt
.keyserver_options
.export_options
,0))
155 /* All of the standard options have failed, so the option is
156 destined for a keyserver plugin. */
157 add_canonical_option(tok
,&opt
.keyserver_options
.other
);
163 max_cert_size
=strtoul(max_cert
,(char **)NULL
,10);
166 max_cert_size
=DEFAULT_MAX_CERT_SIZE
;
173 free_keyserver_spec(struct keyserver_spec
*keyserver
)
175 xfree(keyserver
->uri
);
176 xfree(keyserver
->scheme
);
177 xfree(keyserver
->auth
);
178 xfree(keyserver
->host
);
179 xfree(keyserver
->port
);
180 xfree(keyserver
->path
);
181 xfree(keyserver
->opaque
);
182 free_strlist(keyserver
->options
);
186 /* Return 0 for match */
188 cmp_keyserver_spec(struct keyserver_spec
*one
,struct keyserver_spec
*two
)
190 if(ascii_strcasecmp(one
->scheme
,two
->scheme
)==0)
192 if(one
->host
&& two
->host
&& ascii_strcasecmp(one
->host
,two
->host
)==0)
194 if((one
->port
&& two
->port
195 && ascii_strcasecmp(one
->port
,two
->port
)==0)
196 || (!one
->port
&& !two
->port
))
199 else if(one
->opaque
&& two
->opaque
200 && ascii_strcasecmp(one
->opaque
,two
->opaque
)==0)
207 /* Try and match one of our keyservers. If we can, return that. If
208 we can't, return our input. */
209 struct keyserver_spec
*
210 keyserver_match(struct keyserver_spec
*spec
)
212 struct keyserver_spec
*ks
;
214 for(ks
=opt
.keyserver
;ks
;ks
=ks
->next
)
215 if(cmp_keyserver_spec(spec
,ks
)==0)
221 /* TODO: once we cut over to an all-curl world, we don't need this
222 parser any longer so it can be removed, or at least moved to
223 keyserver/ksutil.c for limited use in gpgkeys_ldap or the like. */
225 struct keyserver_spec
*
226 parse_keyserver_uri(const char *string
,int require_scheme
,
227 const char *configname
,unsigned int configlineno
)
230 struct keyserver_spec
*keyserver
;
235 assert(string
!=NULL
);
237 keyserver
=xmalloc_clear(sizeof(struct keyserver_spec
));
241 options
=strchr(uri
,' ');
249 while((tok
=optsep(&options
)))
250 add_canonical_option(tok
,&keyserver
->options
);
255 for(idx
=uri
,count
=0;*idx
&& *idx
!=':';idx
++)
259 /* Do we see the start of an RFC-2732 ipv6 address here? If so,
260 there clearly isn't a scheme so get out early. */
263 /* Was the '[' the first thing in the string? If not, we
264 have a mangled scheme with a [ in it so fail. */
275 if(*idx
=='\0' || *idx
=='[')
280 /* Assume HKP if there is no scheme */
282 keyserver
->scheme
=xstrdup("hkp");
284 keyserver
->uri
=xmalloc(strlen(keyserver
->scheme
)+3+strlen(uri
)+1);
285 strcpy(keyserver
->uri
,keyserver
->scheme
);
286 strcat(keyserver
->uri
,"://");
287 strcat(keyserver
->uri
,uri
);
293 keyserver
->uri
=xstrdup(uri
);
295 keyserver
->scheme
=xmalloc(count
+1);
297 /* Force to lowercase */
299 keyserver
->scheme
[i
]=ascii_tolower(uri
[i
]);
301 keyserver
->scheme
[i
]='\0';
303 /* Skip past the scheme and colon */
307 if(ascii_strcasecmp(keyserver
->scheme
,"x-broken-hkp")==0)
309 deprecated_warning(configname
,configlineno
,"x-broken-hkp",
310 "--keyserver-options ","broken-http-proxy");
311 xfree(keyserver
->scheme
);
312 keyserver
->scheme
=xstrdup("hkp");
313 append_to_strlist(&opt
.keyserver_options
.other
,"broken-http-proxy");
315 else if(ascii_strcasecmp(keyserver
->scheme
,"x-hkp")==0)
317 /* Canonicalize this to "hkp" so it works with both the internal
318 and external keyserver interface. */
319 xfree(keyserver
->scheme
);
320 keyserver
->scheme
=xstrdup("hkp");
323 if(assume_hkp
|| (uri
[0]=='/' && uri
[1]=='/'))
325 /* Two slashes means network path. */
327 /* Skip over the "//", if any */
331 /* Do we have userinfo auth data present? */
332 for(idx
=uri
,count
=0;*idx
&& *idx
!='@' && *idx
!='/';idx
++)
335 /* We found a @ before the slash, so that means everything
336 before the @ is auth data. */
342 keyserver
->auth
=xmalloc(count
+1);
343 strncpy(keyserver
->auth
,uri
,count
);
344 keyserver
->auth
[count
]='\0';
348 /* Is it an RFC-2732 ipv6 [literal address] ? */
351 for(idx
=uri
+1,count
=1;*idx
352 && ((isascii (*idx
) && isxdigit(*idx
))
353 || *idx
==':' || *idx
=='.');idx
++)
356 /* Is the ipv6 literal address terminated? */
363 for(idx
=uri
,count
=0;*idx
&& *idx
!=':' && *idx
!='/';idx
++)
369 keyserver
->host
=xmalloc(count
+1);
370 strncpy(keyserver
->host
,uri
,count
);
371 keyserver
->host
[count
]='\0';
373 /* Skip past the host */
378 /* It would seem to be reasonable to limit the range of the
379 ports to values between 1-65535, but RFC 1738 and 1808
380 imply there is no limit. Of course, the real world has
383 for(idx
=uri
+1,count
=0;*idx
&& *idx
!='/';idx
++)
387 /* Ports are digits only */
392 keyserver
->port
=xmalloc(count
+1);
393 strncpy(keyserver
->port
,uri
+1,count
);
394 keyserver
->port
[count
]='\0';
396 /* Skip past the colon and port number */
400 /* Everything else is the path */
402 keyserver
->path
=xstrdup(uri
);
404 keyserver
->path
=xstrdup("/");
406 if(keyserver
->path
[1])
407 keyserver
->flags
.direct_uri
=1;
411 /* No slash means opaque. Just record the opaque blob and get
413 keyserver
->opaque
=xstrdup(uri
);
417 /* One slash means absolute path. We don't need to support that
425 free_keyserver_spec(keyserver
);
430 struct keyserver_spec
*
431 parse_preferred_keyserver(PKT_signature
*sig
)
433 struct keyserver_spec
*spec
=NULL
;
437 p
=parse_sig_subpkt(sig
->hashed
,SIGSUBPKT_PREF_KS
,&plen
);
440 byte
*dupe
=xmalloc(plen
+1);
444 spec
=parse_keyserver_uri(dupe
,1,NULL
,0);
452 print_keyrec(int number
,struct keyrec
*keyrec
)
456 iobuf_writebyte(keyrec
->uidbuf
,0);
457 iobuf_flush_temp(keyrec
->uidbuf
);
458 printf("(%d)\t%s ",number
,iobuf_get_temp_buffer(keyrec
->uidbuf
));
461 printf("%d bit ",keyrec
->size
);
465 const char *str
=pubkey_algo_to_string(keyrec
->type
);
473 switch(keyrec
->desc
.mode
)
475 /* If the keyserver helper gave us a short keyid, we have no
476 choice but to use it. Do check --keyid-format to add a 0x if
478 case KEYDB_SEARCH_MODE_SHORT_KID
:
479 printf("key %s%08lX",
480 (opt
.keyid_format
==KF_0xSHORT
481 || opt
.keyid_format
==KF_0xLONG
)?"0x":"",
482 (ulong
)keyrec
->desc
.u
.kid
[1]);
485 /* However, if it gave us a long keyid, we can honor
487 case KEYDB_SEARCH_MODE_LONG_KID
:
488 printf("key %s",keystr(keyrec
->desc
.u
.kid
));
491 case KEYDB_SEARCH_MODE_FPR16
:
494 printf("%02X",keyrec
->desc
.u
.fpr
[i
]);
497 case KEYDB_SEARCH_MODE_FPR20
:
500 printf("%02X",keyrec
->desc
.u
.fpr
[i
]);
508 if(keyrec
->createtime
>0)
511 printf(_("created: %s"),strtimestamp(keyrec
->createtime
));
514 if(keyrec
->expiretime
>0)
517 printf(_("expires: %s"),strtimestamp(keyrec
->expiretime
));
521 printf(" (%s)",_("revoked"));
523 printf(" (%s)",_("disabled"));
525 printf(" (%s)",_("expired"));
530 /* Returns a keyrec (which must be freed) once a key is complete, and
531 NULL otherwise. Call with a NULL keystring once key parsing is
532 complete to return any unfinished keys. */
533 static struct keyrec
*
534 parse_keyrec(char *keystring
)
536 static struct keyrec
*work
=NULL
;
537 struct keyrec
*ret
=NULL
;
545 else if(work
->desc
.mode
==KEYDB_SEARCH_MODE_NONE
)
560 work
=xmalloc_clear(sizeof(struct keyrec
));
561 work
->uidbuf
=iobuf_temp();
564 /* Remove trailing whitespace */
565 for(i
=strlen(keystring
);i
>0;i
--)
566 if(ascii_isspace(keystring
[i
-1]))
571 if((record
=strsep(&keystring
,":"))==NULL
)
574 if(ascii_strcasecmp("pub",record
)==0)
581 work
=xmalloc_clear(sizeof(struct keyrec
));
582 work
->uidbuf
=iobuf_temp();
585 if((tok
=strsep(&keystring
,":"))==NULL
)
588 classify_user_id(tok
,&work
->desc
);
589 if(work
->desc
.mode
!=KEYDB_SEARCH_MODE_SHORT_KID
590 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_LONG_KID
591 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_FPR16
592 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_FPR20
)
594 work
->desc
.mode
=KEYDB_SEARCH_MODE_NONE
;
598 /* Note all items after this are optional. This allows us to
599 have a pub line as simple as pub:keyid and nothing else. */
603 if((tok
=strsep(&keystring
,":"))==NULL
)
606 work
->type
=atoi(tok
);
608 if((tok
=strsep(&keystring
,":"))==NULL
)
611 work
->size
=atoi(tok
);
613 if((tok
=strsep(&keystring
,":"))==NULL
)
619 work
->createtime
=atoi(tok
);
621 if((tok
=strsep(&keystring
,":"))==NULL
)
628 work
->expiretime
=atoi(tok
);
629 /* Force the 'e' flag on if this key is expired. */
630 if(work
->expiretime
<=make_timestamp())
634 if((tok
=strsep(&keystring
,":"))==NULL
)
656 else if(ascii_strcasecmp("uid",record
)==0 && work
->desc
.mode
)
658 char *userid
,*tok
,*decoded
;
660 if((tok
=strsep(&keystring
,":"))==NULL
)
668 /* By definition, de-%-encoding is always smaller than the
669 original string so we can decode in place. */
674 if(tok
[0]=='%' && tok
[1] && tok
[2])
676 if((userid
[i
]=hextobyte(&tok
[1]))==-1)
685 /* We don't care about the other info provided in the uid: line
686 since no keyserver supports marking userids with timestamps
687 or revoked/expired/disabled yet. */
689 /* No need to check for control characters, as utf8_to_native
692 decoded
=utf8_to_native(userid
,i
,0);
693 if(strlen(decoded
)>opt
.screen_columns
-10)
694 decoded
[opt
.screen_columns
-10]='\0';
695 iobuf_writestr(work
->uidbuf
,decoded
);
697 iobuf_writestr(work
->uidbuf
,"\n\t");
701 /* Ignore any records other than "pri" and "uid" for easy future
707 /* TODO: do this as a list sent to keyserver_work rather than calling
708 it once for each key to get the correct counts after the import
709 (cosmetics, really) and to better take advantage of the keyservers
710 that can do multiple fetches in one go (LDAP). */
712 show_prompt(KEYDB_SEARCH_DESC
*desc
,int numdesc
,int count
,const char *search
)
716 if(count
&& opt
.command_fd
==-1)
719 tty_printf("Keys %d-%d of %d for \"%s\". ",from
,numdesc
,count
,search
);
723 answer
=cpr_get_no_help("keysearch.prompt",
724 _("Enter number(s), N)ext, or Q)uit > "));
726 if(answer
[0]=='\x04')
732 if(answer
[0]=='q' || answer
[0]=='Q')
737 else if(atoi(answer
)>=1 && atoi(answer
)<=numdesc
)
739 char *split
=answer
,*num
;
741 while((num
=strsep(&split
," ,"))!=NULL
)
742 if(atoi(num
)>=1 && atoi(num
)<=numdesc
)
743 keyserver_work(KS_GET
,NULL
,&desc
[atoi(num
)-1],1,
744 NULL
,NULL
,opt
.keyserver
);
753 /* Count and searchstr are just for cosmetics. If the count is too
754 small, it will grow safely. If negative it disables the "Key x-y
755 of z" messages. searchstr should be UTF-8 (rather than native). */
757 keyserver_search_prompt(IOBUF buffer
,const char *searchstr
)
759 int i
=0,validcount
=0,started
=0,header
=0,count
=1;
760 unsigned int maxlen
,buflen
,numlines
=0;
761 KEYDB_SEARCH_DESC
*desc
;
766 localstr
=utf8_to_native(searchstr
,strlen(searchstr
),0);
768 desc
=xmalloc(count
*sizeof(KEYDB_SEARCH_DESC
));
772 struct keyrec
*keyrec
;
776 rl
=iobuf_read_line(buffer
,&line
,&buflen
,&maxlen
);
780 if(!header
&& ascii_strncasecmp("SEARCH ",line
,7)==0
781 && ascii_strncasecmp(" BEGIN",&line
[strlen(line
)-7],6)==0)
786 else if(ascii_strncasecmp("SEARCH ",line
,7)==0
787 && ascii_strncasecmp(" END",&line
[strlen(line
)-5],4)==0)
793 /* Look for an info: line. The only current info: values
794 defined are the version and key count. */
795 if(!started
&& rl
>0 && ascii_strncasecmp("info:",line
,5)==0)
797 char *tok
,*str
=&line
[5];
799 if((tok
=strsep(&str
,":"))!=NULL
)
803 if(sscanf(tok
,"%d",&version
)!=1)
808 log_error(_("invalid keyserver protocol "
809 "(us %d!=handler %d)\n"),1,version
);
814 if((tok
=strsep(&str
,":"))!=NULL
&& sscanf(tok
,"%d",&count
)==1)
823 desc
=xrealloc(desc
,count
*sizeof(KEYDB_SEARCH_DESC
));
832 keyrec
=parse_keyrec(NULL
);
847 if(show_prompt(desc
,i
,validcount
?count
:0,localstr
))
856 keyrec
=parse_keyrec(line
);
860 /* keyserver helper sent more keys than they claimed in the
863 desc
=xrealloc(desc
,count
*sizeof(KEYDB_SEARCH_DESC
));
869 desc
[i
]=keyrec
->desc
;
873 /* screen_lines - 1 for the prompt. */
874 if(numlines
+keyrec
->lines
>opt
.screen_lines
-1)
876 if(show_prompt(desc
,i
,validcount
?count
:0,localstr
))
882 print_keyrec(i
+1,keyrec
);
885 numlines
+=keyrec
->lines
;
886 iobuf_close(keyrec
->uidbuf
);
895 /* Leave this commented out or now, and perhaps for a very long
896 time. All HKPish servers return HTML error messages for
900 log_info(_("keyserver does not support searching\n"));
906 log_info(_("key \"%s\" not found on keyserver\n"),localstr
);
908 log_info(_("key not found on keyserver\n"));
916 /* We sometimes want to use a different gpgkeys_xxx for a given
917 protocol (for example, ldaps is handled by gpgkeys_ldap). Map
920 keyserver_typemap(const char *type
)
922 if(strcmp(type
,"ldaps")==0)
928 /* The PGP LDAP and the curl fetch-a-LDAP-object methodologies are
929 sufficiently different that we can't use curl to do LDAP. */
931 direct_uri_map(const char *scheme
,unsigned int is_direct
)
933 if(is_direct
&& strcmp(scheme
,"ldap")==0)
939 #define GPGKEYS_PREFIX "gpgkeys_"
940 #define GPGKEYS_CURL GPGKEYS_PREFIX "curl" EXEEXT
941 #define GPGKEYS_PREFIX_LEN (strlen(GPGKEYS_CURL))
942 #define KEYSERVER_ARGS_KEEP " -o \"%O\" \"%I\""
943 #define KEYSERVER_ARGS_NOKEEP " -o \"%o\" \"%i\""
946 keyserver_spawn(enum ks_action action
,STRLIST list
,KEYDB_SEARCH_DESC
*desc
,
947 int count
,int *prog
,unsigned char **fpr
,size_t *fpr_len
,
948 struct keyserver_spec
*keyserver
)
950 int ret
=0,i
,gotversion
=0,outofband
=0;
952 unsigned int maxlen
,buflen
;
953 char *command
,*end
,*searchstr
=NULL
;
955 struct exec_info
*spawn
;
957 const char *libexecdir
= get_libexecdir ();
961 #ifdef EXEC_TEMPFILE_ONLY
962 opt
.keyserver_options
.options
|=KEYSERVER_USE_TEMP_FILES
;
965 /* Build the filename for the helper to execute */
966 scheme
=keyserver_typemap(keyserver
->scheme
);
968 #ifdef DISABLE_KEYSERVER_PATH
969 /* Destroy any path we might have. This is a little tricky,
970 portability-wise. It's not correct to delete the PATH
971 environment variable, as that may fall back to a system built-in
972 PATH. Similarly, it is not correct to set PATH to the null
973 string (PATH="") since this actually deletes the PATH environment
974 variable under MinGW. The safest thing to do here is to force
975 PATH to be GNUPG_LIBEXECDIR. All this is not that meaningful on
976 Unix-like systems (since we're going to give a full path to
977 gpgkeys_foo), but on W32 it prevents loading any DLLs from
978 directories in %PATH%.
980 After some more thinking about this we came to the conclusion
981 that it is better to load the helpers from the directory where
982 the program of this process lives. Fortunately Windows provides
983 a way to retrieve this and our get_libexecdir function has been
984 modified to return just this. Setting the exec-path is not
986 set_exec_path(libexecdir);
989 if(opt
.exec_path_set
)
991 /* If exec-path was set, and DISABLE_KEYSERVER_PATH is
992 undefined, then don't specify a full path to gpgkeys_foo, so
993 that the PATH can work. */
994 command
=xmalloc(GPGKEYS_PREFIX_LEN
+strlen(scheme
)+3+strlen(EXEEXT
)+1);
1000 /* Specify a full path to gpgkeys_foo. */
1001 command
=xmalloc(strlen(libexecdir
)+strlen(DIRSEP_S
)+
1002 GPGKEYS_PREFIX_LEN
+strlen(scheme
)+3+strlen(EXEEXT
)+1);
1003 strcpy(command
,libexecdir
);
1004 strcat(command
,DIRSEP_S
);
1007 end
=command
+strlen(command
);
1009 /* Build a path for the keyserver helper. If it is direct_uri
1010 (i.e. an object fetch and not a keyserver), then add "_uri" to
1011 the end to distinguish the keyserver helper from an object
1012 fetcher that can speak that protocol (this is a problem for
1015 strcat(command
,GPGKEYS_PREFIX
);
1016 strcat(command
,scheme
);
1018 /* This "_uri" thing is in case we need to call a direct handler
1019 instead of the keyserver handler. This lets us use gpgkeys_curl
1020 or gpgkeys_ldap_uri (we don't provide it, but a user might)
1021 instead of gpgkeys_ldap to fetch things like
1022 ldap://keyserver.pgp.com/o=PGP%20keys?pgpkey?sub?pgpkeyid=99242560 */
1024 if(direct_uri_map(scheme
,keyserver
->flags
.direct_uri
))
1025 strcat(command
,"_uri");
1027 strcat(command
,EXEEXT
);
1029 /* Can we execute it? If not, try curl as our catchall. */
1030 if(path_access(command
,X_OK
)!=0)
1031 strcpy(end
,GPGKEYS_CURL
);
1033 if(opt
.keyserver_options
.options
&KEYSERVER_USE_TEMP_FILES
)
1035 if(opt
.keyserver_options
.options
&KEYSERVER_KEEP_TEMP_FILES
)
1037 command
=xrealloc(command
,strlen(command
)+
1038 strlen(KEYSERVER_ARGS_KEEP
)+1);
1039 strcat(command
,KEYSERVER_ARGS_KEEP
);
1043 command
=xrealloc(command
,strlen(command
)+
1044 strlen(KEYSERVER_ARGS_NOKEEP
)+1);
1045 strcat(command
,KEYSERVER_ARGS_NOKEEP
);
1048 ret
=exec_write(&spawn
,NULL
,command
,NULL
,0,0);
1051 ret
=exec_write(&spawn
,command
,NULL
,NULL
,0,0);
1058 fprintf(spawn
->tochild
,
1059 "# This is a GnuPG %s keyserver communications file\n",VERSION
);
1060 fprintf(spawn
->tochild
,"VERSION %d\n",KEYSERVER_PROTO_VERSION
);
1061 fprintf(spawn
->tochild
,"PROGRAM %s\n",VERSION
);
1062 fprintf(spawn
->tochild
,"SCHEME %s\n",keyserver
->scheme
);
1064 if(keyserver
->opaque
)
1065 fprintf(spawn
->tochild
,"OPAQUE %s\n",keyserver
->opaque
);
1069 fprintf(spawn
->tochild
,"AUTH %s\n",keyserver
->auth
);
1072 fprintf(spawn
->tochild
,"HOST %s\n",keyserver
->host
);
1075 fprintf(spawn
->tochild
,"PORT %s\n",keyserver
->port
);
1078 fprintf(spawn
->tochild
,"PATH %s\n",keyserver
->path
);
1081 /* Write global options */
1083 for(temp
=opt
.keyserver_options
.other
;temp
;temp
=temp
->next
)
1084 fprintf(spawn
->tochild
,"OPTION %s\n",temp
->d
);
1086 /* Write per-keyserver options */
1088 for(temp
=keyserver
->options
;temp
;temp
=temp
->next
)
1089 fprintf(spawn
->tochild
,"OPTION %s\n",temp
->d
);
1095 fprintf(spawn
->tochild
,"COMMAND GET\n\n");
1097 /* Which keys do we want? */
1099 for(i
=0;i
<count
;i
++)
1103 if(desc
[i
].mode
==KEYDB_SEARCH_MODE_FPR20
)
1107 fprintf(spawn
->tochild
,"0x");
1109 for(f
=0;f
<MAX_FINGERPRINT_LEN
;f
++)
1110 fprintf(spawn
->tochild
,"%02X",desc
[i
].u
.fpr
[f
]);
1112 fprintf(spawn
->tochild
,"\n");
1114 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_FPR16
)
1118 fprintf(spawn
->tochild
,"0x");
1121 fprintf(spawn
->tochild
,"%02X",desc
[i
].u
.fpr
[f
]);
1123 fprintf(spawn
->tochild
,"\n");
1125 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_LONG_KID
)
1126 fprintf(spawn
->tochild
,"0x%08lX%08lX\n",
1127 (ulong
)desc
[i
].u
.kid
[0],
1128 (ulong
)desc
[i
].u
.kid
[1]);
1129 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_SHORT_KID
)
1130 fprintf(spawn
->tochild
,"0x%08lX\n",
1131 (ulong
)desc
[i
].u
.kid
[1]);
1132 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_EXACT
)
1134 fprintf(spawn
->tochild
,"0x0000000000000000\n");
1137 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_NONE
)
1145 log_info(_("requesting key %s from %s server %s\n"),
1146 keystr_from_desc(&desc
[i
]),
1147 keyserver
->scheme
,keyserver
->host
);
1149 log_info(_("requesting key %s from %s\n"),
1150 keystr_from_desc(&desc
[i
]),keyserver
->uri
);
1154 fprintf(spawn
->tochild
,"\n");
1163 fprintf(spawn
->tochild
,"COMMAND GETNAME\n\n");
1165 /* Which names do we want? */
1167 for(key
=list
;key
!=NULL
;key
=key
->next
)
1168 fprintf(spawn
->tochild
,"%s\n",key
->d
);
1170 fprintf(spawn
->tochild
,"\n");
1173 log_info(_("searching for names from %s server %s\n"),
1174 keyserver
->scheme
,keyserver
->host
);
1176 log_info(_("searching for names from %s\n"),keyserver
->uri
);
1185 /* Note the extra \n here to send an empty keylist block */
1186 fprintf(spawn
->tochild
,"COMMAND SEND\n\n\n");
1188 for(key
=list
;key
!=NULL
;key
=key
->next
)
1190 armor_filter_context_t afx
;
1191 IOBUF buffer
=iobuf_temp();
1195 add_to_strlist(&temp
,key
->d
);
1197 memset(&afx
,0,sizeof(afx
));
1199 /* Tell the armor filter to use Unix-style \n line
1200 endings, since we're going to fprintf this to a file
1201 that (on Win32) is open in text mode. The win32 stdio
1202 will transform the \n to \r\n and we'll end up with the
1203 proper line endings on win32. This is a no-op on
1206 iobuf_push_filter(buffer
,armor_filter
,&afx
);
1208 /* TODO: Remove Comment: lines from keys exported this
1211 if(export_pubkeys_stream(buffer
,temp
,&block
,
1212 opt
.keyserver_options
.export_options
)==-1)
1213 iobuf_close(buffer
);
1218 iobuf_flush_temp(buffer
);
1220 merge_keys_and_selfsig(block
);
1222 fprintf(spawn
->tochild
,"INFO %08lX%08lX BEGIN\n",
1223 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[0],
1224 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[1]);
1226 for(node
=block
;node
;node
=node
->next
)
1228 switch(node
->pkt
->pkttype
)
1233 case PKT_PUBLIC_KEY
:
1234 case PKT_PUBLIC_SUBKEY
:
1236 PKT_public_key
*pk
=node
->pkt
->pkt
.public_key
;
1238 keyid_from_pk(pk
,NULL
);
1240 fprintf(spawn
->tochild
,"%sb:%08lX%08lX:%u:%u:%u:%u:",
1241 node
->pkt
->pkttype
==PKT_PUBLIC_KEY
?"pu":"su",
1242 (ulong
)pk
->keyid
[0],(ulong
)pk
->keyid
[1],
1249 fprintf(spawn
->tochild
,"r");
1251 fprintf(spawn
->tochild
,"e");
1253 fprintf(spawn
->tochild
,"\n");
1259 PKT_user_id
*uid
=node
->pkt
->pkt
.user_id
;
1262 if(uid
->attrib_data
)
1265 fprintf(spawn
->tochild
,"uid:");
1267 /* Quote ':', '%', and any 8-bit
1269 for(r
=0;r
<uid
->len
;r
++)
1271 if(uid
->name
[r
]==':' || uid
->name
[r
]=='%'
1272 || uid
->name
[r
]&0x80)
1273 fprintf(spawn
->tochild
,"%%%02X",
1274 (byte
)uid
->name
[r
]);
1276 fprintf(spawn
->tochild
,"%c",uid
->name
[r
]);
1279 fprintf(spawn
->tochild
,":%u:%u:",
1280 uid
->created
,uid
->expiredate
);
1283 fprintf(spawn
->tochild
,"r");
1285 fprintf(spawn
->tochild
,"e");
1287 fprintf(spawn
->tochild
,"\n");
1291 /* This bit is really for the benefit of
1292 people who store their keys in LDAP
1293 servers. It makes it easy to do queries
1294 for things like "all keys signed by
1298 PKT_signature
*sig
=node
->pkt
->pkt
.signature
;
1300 if(!IS_UID_SIG(sig
))
1303 fprintf(spawn
->tochild
,"sig:%08lX%08lX:%X:%u:%u\n",
1304 (ulong
)sig
->keyid
[0],(ulong
)sig
->keyid
[1],
1305 sig
->sig_class
,sig
->timestamp
,
1312 fprintf(spawn
->tochild
,"INFO %08lX%08lX END\n",
1313 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[0],
1314 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[1]);
1316 fprintf(spawn
->tochild
,"KEY %s BEGIN\n",key
->d
);
1317 fwrite(iobuf_get_temp_buffer(buffer
),
1318 iobuf_get_temp_length(buffer
),1,spawn
->tochild
);
1319 fprintf(spawn
->tochild
,"KEY %s END\n",key
->d
);
1321 iobuf_close(buffer
);
1324 log_info(_("sending key %s to %s server %s\n"),
1325 keystr(block
->pkt
->pkt
.public_key
->keyid
),
1326 keyserver
->scheme
,keyserver
->host
);
1328 log_info(_("sending key %s to %s\n"),
1329 keystr(block
->pkt
->pkt
.public_key
->keyid
),
1332 release_kbnode(block
);
1345 fprintf(spawn
->tochild
,"COMMAND SEARCH\n\n");
1347 /* Which keys do we want? Remember that the gpgkeys_ program
1348 is going to lump these together into a search string. */
1350 for(key
=list
;key
!=NULL
;key
=key
->next
)
1352 fprintf(spawn
->tochild
,"%s\n",key
->d
);
1355 searchstr
=xrealloc(searchstr
,
1356 strlen(searchstr
)+strlen(key
->d
)+2);
1357 strcat(searchstr
," ");
1361 searchstr
=xmalloc(strlen(key
->d
)+1);
1365 strcat(searchstr
,key
->d
);
1368 fprintf(spawn
->tochild
,"\n");
1371 log_info(_("searching for \"%s\" from %s server %s\n"),
1372 searchstr
,keyserver
->scheme
,keyserver
->host
);
1374 log_info(_("searching for \"%s\" from %s\n"),
1375 searchstr
,keyserver
->uri
);
1381 log_fatal(_("no keyserver action!\n"));
1385 /* Done sending, so start reading. */
1386 ret
=exec_read(spawn
);
1390 /* Now handle the response */
1398 if(iobuf_read_line(spawn
->fromchild
,&line
,&buflen
,&maxlen
)==0)
1400 ret
=G10ERR_READ_FILE
;
1401 goto fail
; /* i.e. EOF */
1406 /* remove trailing whitespace */
1408 while(plen
>0 && ascii_isspace(ptr
[plen
-1]))
1415 if(ascii_strncasecmp(ptr
,"VERSION ",8)==0)
1419 if(atoi(&ptr
[8])!=KEYSERVER_PROTO_VERSION
)
1421 log_error(_("invalid keyserver protocol (us %d!=handler %d)\n"),
1422 KEYSERVER_PROTO_VERSION
,atoi(&ptr
[8]));
1426 else if(ascii_strncasecmp(ptr
,"PROGRAM ",8)==0)
1428 if(ascii_strncasecmp(&ptr
[8],VERSION
,strlen(VERSION
))!=0)
1429 log_info(_("WARNING: keyserver handler from a different"
1430 " version of GnuPG (%s)\n"),&ptr
[8]);
1432 else if(ascii_strncasecmp(ptr
,"OPTION OUTOFBAND",16)==0)
1433 outofband
=1; /* Currently the only OPTION */
1438 log_error(_("keyserver did not send VERSION\n"));
1450 stats_handle
=import_new_stats_handle();
1452 /* Slurp up all the key data. In the future, it might be
1453 nice to look for KEY foo OUTOFBAND and FAILED indicators.
1454 It's harmless to ignore them, but ignoring them does make
1455 gpg complain about "no valid OpenPGP data found". One
1456 way to do this could be to continue parsing this
1457 line-by-line and make a temp iobuf for each key. */
1459 import_keys_stream(spawn
->fromchild
,stats_handle
,fpr
,fpr_len
,
1460 opt
.keyserver_options
.import_options
);
1462 import_print_stats(stats_handle
);
1463 import_release_stats_handle(stats_handle
);
1468 /* Nothing to do here */
1473 keyserver_search_prompt(spawn
->fromchild
,searchstr
);
1477 log_fatal(_("no keyserver action!\n"));
1486 *prog
=exec_finish(spawn
);
1492 keyserver_work(enum ks_action action
,STRLIST list
,KEYDB_SEARCH_DESC
*desc
,
1493 int count
,unsigned char **fpr
,size_t *fpr_len
,
1494 struct keyserver_spec
*keyserver
)
1500 log_error(_("no keyserver known (use option --keyserver)\n"));
1501 return G10ERR_BAD_URI
;
1504 #ifdef DISABLE_KEYSERVER_HELPERS
1506 log_error(_("external keyserver calls are not supported in this build\n"));
1507 return G10ERR_KEYSERVER
;
1510 /* Spawn a handler */
1512 rc
=keyserver_spawn(action
,list
,desc
,count
,&ret
,fpr
,fpr_len
,keyserver
);
1517 case KEYSERVER_SCHEME_NOT_FOUND
:
1518 log_error(_("no handler for keyserver scheme `%s'\n"),
1522 case KEYSERVER_NOT_SUPPORTED
:
1523 log_error(_("action `%s' not supported with keyserver "
1525 action
==KS_GET
?"get":action
==KS_SEND
?"send":
1526 action
==KS_SEARCH
?"search":"unknown",
1530 case KEYSERVER_VERSION_ERROR
:
1531 log_error(_(GPGKEYS_PREFIX
"%s does not support"
1532 " handler version %d\n"),
1533 keyserver_typemap(keyserver
->scheme
),
1534 KEYSERVER_PROTO_VERSION
);
1537 case KEYSERVER_TIMEOUT
:
1538 log_error(_("keyserver timed out\n"));
1541 case KEYSERVER_INTERNAL_ERROR
:
1543 log_error(_("keyserver internal error\n"));
1547 return G10ERR_KEYSERVER
;
1552 log_error(_("keyserver communications error: %s\n"),g10_errstr(rc
));
1558 #endif /* ! DISABLE_KEYSERVER_HELPERS*/
1562 keyserver_export(STRLIST users
)
1565 KEYDB_SEARCH_DESC desc
;
1568 /* Weed out descriptors that we don't support sending */
1569 for(;users
;users
=users
->next
)
1571 classify_user_id (users
->d
, &desc
);
1572 if(desc
.mode
!=KEYDB_SEARCH_MODE_SHORT_KID
&&
1573 desc
.mode
!=KEYDB_SEARCH_MODE_LONG_KID
&&
1574 desc
.mode
!=KEYDB_SEARCH_MODE_FPR16
&&
1575 desc
.mode
!=KEYDB_SEARCH_MODE_FPR20
)
1577 log_error(_("\"%s\" not a key ID: skipping\n"),users
->d
);
1581 append_to_strlist(&sl
,users
->d
);
1586 rc
=keyserver_work(KS_SEND
,sl
,NULL
,0,NULL
,NULL
,opt
.keyserver
);
1594 keyserver_import(STRLIST users
)
1596 KEYDB_SEARCH_DESC
*desc
;
1597 int num
=100,count
=0;
1600 /* Build a list of key ids */
1601 desc
=xmalloc(sizeof(KEYDB_SEARCH_DESC
)*num
);
1603 for(;users
;users
=users
->next
)
1605 classify_user_id (users
->d
, &desc
[count
]);
1606 if(desc
[count
].mode
!=KEYDB_SEARCH_MODE_SHORT_KID
&&
1607 desc
[count
].mode
!=KEYDB_SEARCH_MODE_LONG_KID
&&
1608 desc
[count
].mode
!=KEYDB_SEARCH_MODE_FPR16
&&
1609 desc
[count
].mode
!=KEYDB_SEARCH_MODE_FPR20
)
1611 log_error(_("\"%s\" not a key ID: skipping\n"),users
->d
);
1619 desc
=xrealloc(desc
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1624 rc
=keyserver_work(KS_GET
,NULL
,desc
,count
,NULL
,NULL
,opt
.keyserver
);
1632 keyserver_import_fprint(const byte
*fprint
,size_t fprint_len
,
1633 struct keyserver_spec
*keyserver
)
1635 KEYDB_SEARCH_DESC desc
;
1637 memset(&desc
,0,sizeof(desc
));
1640 desc
.mode
=KEYDB_SEARCH_MODE_FPR16
;
1641 else if(fprint_len
==20)
1642 desc
.mode
=KEYDB_SEARCH_MODE_FPR20
;
1646 memcpy(desc
.u
.fpr
,fprint
,fprint_len
);
1648 /* TODO: Warn here if the fingerprint we got doesn't match the one
1650 return keyserver_work(KS_GET
,NULL
,&desc
,1,NULL
,NULL
,keyserver
);
1654 keyserver_import_keyid(u32
*keyid
,struct keyserver_spec
*keyserver
)
1656 KEYDB_SEARCH_DESC desc
;
1658 memset(&desc
,0,sizeof(desc
));
1660 desc
.mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1661 desc
.u
.kid
[0]=keyid
[0];
1662 desc
.u
.kid
[1]=keyid
[1];
1664 return keyserver_work(KS_GET
,NULL
,&desc
,1,NULL
,NULL
,keyserver
);
1667 /* code mostly stolen from do_export_stream */
1669 keyidlist(STRLIST users
,KEYDB_SEARCH_DESC
**klist
,int *count
,int fakev3
)
1671 int rc
=0,ndesc
,num
=100;
1672 KBNODE keyblock
=NULL
,node
;
1674 KEYDB_SEARCH_DESC
*desc
;
1679 *klist
=xmalloc(sizeof(KEYDB_SEARCH_DESC
)*num
);
1686 desc
= xmalloc_clear ( ndesc
* sizeof *desc
);
1687 desc
[0].mode
= KEYDB_SEARCH_MODE_FIRST
;
1691 for (ndesc
=0, sl
=users
; sl
; sl
= sl
->next
, ndesc
++)
1693 desc
= xmalloc ( ndesc
* sizeof *desc
);
1695 for (ndesc
=0, sl
=users
; sl
; sl
= sl
->next
)
1697 if(classify_user_id (sl
->d
, desc
+ndesc
))
1700 log_error (_("key \"%s\" not found: %s\n"),
1701 sl
->d
, g10_errstr (G10ERR_INV_USER_ID
));
1705 while (!(rc
= keydb_search (kdbhd
, desc
, ndesc
)))
1708 desc
[0].mode
= KEYDB_SEARCH_MODE_NEXT
;
1710 /* read the keyblock */
1711 rc
= keydb_get_keyblock (kdbhd
, &keyblock
);
1714 log_error (_("error reading keyblock: %s\n"), g10_errstr(rc
) );
1718 if((node
=find_kbnode(keyblock
,PKT_PUBLIC_KEY
)))
1720 /* This is to work around a bug in some keyservers (pksd and
1721 OKS) that calculate v4 RSA keyids as if they were v3 RSA.
1722 The answer is to refresh both the correct v4 keyid
1723 (e.g. 99242560) and the fake v3 keyid (e.g. 68FDDBC7).
1724 This only happens for key refresh using the HKP scheme
1725 and if the refresh-add-fake-v3-keyids keyserver option is
1727 if(fakev3
&& is_RSA(node
->pkt
->pkt
.public_key
->pubkey_algo
) &&
1728 node
->pkt
->pkt
.public_key
->version
>=4)
1730 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1731 mpi_get_keyid(node
->pkt
->pkt
.public_key
->pkey
[0],
1732 (*klist
)[*count
].u
.kid
);
1738 *klist
=xrealloc(*klist
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1742 /* v4 keys get full fingerprints. v3 keys get long keyids.
1743 This is because it's easy to calculate any sort of keyid
1744 from a v4 fingerprint, but not a v3 fingerprint. */
1746 if(node
->pkt
->pkt
.public_key
->version
<4)
1748 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1749 keyid_from_pk(node
->pkt
->pkt
.public_key
,
1750 (*klist
)[*count
].u
.kid
);
1756 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_FPR20
;
1757 fingerprint_from_pk(node
->pkt
->pkt
.public_key
,
1758 (*klist
)[*count
].u
.fpr
,&dummy
);
1761 /* This is a little hackish, using the skipfncvalue as a
1762 void* pointer to the keyserver spec, but we don't need
1763 the skipfnc here, and it saves having an additional field
1764 for this (which would be wasted space most of the
1767 (*klist
)[*count
].skipfncvalue
=NULL
;
1769 /* Are we honoring preferred keyservers? */
1770 if(opt
.keyserver_options
.options
&KEYSERVER_HONOR_KEYSERVER_URL
)
1772 PKT_user_id
*uid
=NULL
;
1773 PKT_signature
*sig
=NULL
;
1775 merge_keys_and_selfsig(keyblock
);
1777 for(node
=node
->next
;node
;node
=node
->next
)
1779 if(node
->pkt
->pkttype
==PKT_USER_ID
1780 && node
->pkt
->pkt
.user_id
->is_primary
)
1781 uid
=node
->pkt
->pkt
.user_id
;
1782 else if(node
->pkt
->pkttype
==PKT_SIGNATURE
1783 && node
->pkt
->pkt
.signature
->
1784 flags
.chosen_selfsig
&& uid
)
1786 sig
=node
->pkt
->pkt
.signature
;
1791 /* Try and parse the keyserver URL. If it doesn't work,
1792 then we end up writing NULL which indicates we are
1793 the same as any other key. */
1795 (*klist
)[*count
].skipfncvalue
=parse_preferred_keyserver(sig
);
1803 *klist
=xrealloc(*klist
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1815 keydb_release(kdbhd
);
1816 release_kbnode(keyblock
);
1821 /* Note this is different than the original HKP refresh. It allows
1822 usernames to refresh only part of the keyring. */
1825 keyserver_refresh(STRLIST users
)
1827 int rc
,count
,numdesc
,fakev3
=0;
1828 KEYDB_SEARCH_DESC
*desc
;
1829 unsigned int options
=opt
.keyserver_options
.import_options
;
1831 /* We switch merge-only on during a refresh, as 'refresh' should
1832 never import new keys, even if their keyids match. */
1833 opt
.keyserver_options
.import_options
|=IMPORT_MERGE_ONLY
;
1835 /* Similarly, we switch on fast-import, since refresh may make
1836 multiple import sets (due to preferred keyserver URLs). We don't
1837 want each set to rebuild the trustdb. Instead we do it once at
1839 opt
.keyserver_options
.import_options
|=IMPORT_FAST
;
1841 /* If refresh_add_fake_v3_keyids is on and it's a HKP or MAILTO
1842 scheme, then enable fake v3 keyid generation. */
1843 if((opt
.keyserver_options
.options
&KEYSERVER_ADD_FAKE_V3
) && opt
.keyserver
1844 && (ascii_strcasecmp(opt
.keyserver
->scheme
,"hkp")==0 ||
1845 ascii_strcasecmp(opt
.keyserver
->scheme
,"mailto")==0))
1848 rc
=keyidlist(users
,&desc
,&numdesc
,fakev3
);
1857 /* Try to handle preferred keyserver keys first */
1858 for(i
=0;i
<numdesc
;i
++)
1860 if(desc
[i
].skipfncvalue
)
1862 struct keyserver_spec
*keyserver
=desc
[i
].skipfncvalue
;
1864 /* We use the keyserver structure we parsed out before.
1865 Note that a preferred keyserver without a scheme://
1866 will be interpreted as hkp:// */
1868 rc
=keyserver_work(KS_GET
,NULL
,&desc
[i
],1,NULL
,NULL
,keyserver
);
1870 log_info(_("WARNING: unable to refresh key %s"
1871 " via %s: %s\n"),keystr_from_desc(&desc
[i
]),
1872 keyserver
->uri
,g10_errstr(rc
));
1875 /* We got it, so mark it as NONE so we don't try and
1876 get it again from the regular keyserver. */
1878 desc
[i
].mode
=KEYDB_SEARCH_MODE_NONE
;
1882 free_keyserver_spec(keyserver
);
1892 log_info(_("refreshing 1 key from %s\n"),opt
.keyserver
->uri
);
1894 log_info(_("refreshing %d keys from %s\n"),
1895 count
,opt
.keyserver
->uri
);
1898 rc
=keyserver_work(KS_GET
,NULL
,desc
,numdesc
,NULL
,NULL
,opt
.keyserver
);
1903 opt
.keyserver_options
.import_options
=options
;
1905 /* If the original options didn't have fast import, and the trustdb
1906 is dirty, rebuild. */
1907 if(!(opt
.keyserver_options
.import_options
&IMPORT_FAST
))
1908 trustdb_check_or_update();
1914 keyserver_search(STRLIST tokens
)
1917 return keyserver_work(KS_SEARCH
,tokens
,NULL
,0,NULL
,NULL
,opt
.keyserver
);
1923 keyserver_fetch(STRLIST urilist
)
1925 KEYDB_SEARCH_DESC desc
;
1927 unsigned int options
=opt
.keyserver_options
.import_options
;
1929 /* Switch on fast-import, since fetch can handle more than one
1930 import and we don't want each set to rebuild the trustdb.
1931 Instead we do it once at the end. */
1932 opt
.keyserver_options
.import_options
|=IMPORT_FAST
;
1934 /* A dummy desc since we're not actually fetching a particular key
1936 memset(&desc
,0,sizeof(desc
));
1937 desc
.mode
=KEYDB_SEARCH_MODE_EXACT
;
1939 for(sl
=urilist
;sl
;sl
=sl
->next
)
1941 struct keyserver_spec
*spec
;
1943 spec
=parse_keyserver_uri(sl
->d
,1,NULL
,0);
1948 rc
=keyserver_work(KS_GET
,NULL
,&desc
,1,NULL
,NULL
,spec
);
1950 log_info (_("WARNING: unable to fetch URI %s: %s\n"),
1951 sl
->d
,g10_errstr(rc
));
1953 free_keyserver_spec(spec
);
1956 log_info (_("WARNING: unable to parse URI %s\n"),sl
->d
);
1959 opt
.keyserver_options
.import_options
=options
;
1961 /* If the original options didn't have fast import, and the trustdb
1962 is dirty, rebuild. */
1963 if(!(opt
.keyserver_options
.import_options
&IMPORT_FAST
))
1964 trustdb_check_or_update();
1969 /* Import key in a CERT or pointed to by a CERT */
1971 keyserver_import_cert(const char *name
,unsigned char **fpr
,size_t *fpr_len
)
1973 char *domain
,*look
,*url
;
1975 int type
,rc
=G10ERR_GENERAL
;
1979 domain
=strrchr(look
,'@');
1983 type
=get_cert(look
,max_cert_size
,&key
,fpr
,fpr_len
,&url
);
1986 int armor_status
=opt
.no_armor
;
1988 /* CERTs are always in binary format */
1991 rc
=import_keys_stream(key
,NULL
,fpr
,fpr_len
,
1992 opt
.keyserver_options
.import_options
);
1994 opt
.no_armor
=armor_status
;
1998 else if(type
==2 && *fpr
)
2000 /* We only consider the IPGP type if a fingerprint was provided.
2001 This lets us select the right key regardless of what a URL
2002 points to, or get the key from a keyserver. */
2005 struct keyserver_spec
*spec
;
2007 spec
=parse_keyserver_uri(url
,1,NULL
,0);
2012 add_to_strlist(&list
,url
);
2014 rc
=keyserver_fetch(list
);
2017 free_keyserver_spec(spec
);
2020 else if(opt
.keyserver
)
2022 /* If only a fingerprint is provided, try and fetch it from
2025 rc
=keyserver_import_fprint(*fpr
,*fpr_len
,opt
.keyserver
);
2028 log_info(_("no keyserver known (use option --keyserver)\n"));
2030 /* Give a better string here? "CERT fingerprint for \"%s\"
2031 found, but no keyserver" " known (use option
2032 --keyserver)\n" ? */
2042 /* Import key pointed to by a PKA record. Return the requested
2043 fingerprint in fpr. */
2045 keyserver_import_pka(const char *name
,unsigned char **fpr
,size_t *fpr_len
)
2053 uri
= get_pka_info (name
, *fpr
);
2056 struct keyserver_spec
*spec
;
2057 spec
= parse_keyserver_uri (uri
, 1, NULL
, 0);
2060 rc
=keyserver_import_fprint (*fpr
, 20, spec
);
2061 free_keyserver_spec (spec
);
2072 /* Import all keys that match name */
2074 keyserver_import_name(const char *name
,unsigned char **fpr
,size_t *fpr_len
,
2075 struct keyserver_spec
*keyserver
)
2080 append_to_strlist(&list
,name
);
2082 rc
=keyserver_work(KS_GETNAME
,list
,NULL
,0,fpr
,fpr_len
,keyserver
);
2089 /* Use the PGP Universal trick of asking ldap://keys.(maildomain) for
2092 keyserver_import_ldap(const char *name
,unsigned char **fpr
,size_t *fpr_len
)
2095 struct keyserver_spec
*keyserver
;
2099 append_to_strlist(&list
,name
);
2101 /* Parse out the domain */
2102 domain
=strrchr(name
,'@');
2104 return G10ERR_GENERAL
;
2108 keyserver
=xmalloc_clear(sizeof(struct keyserver_spec
));
2110 keyserver
->scheme
=xstrdup("ldap");
2111 keyserver
->host
=xmalloc(5+strlen(domain
)+1);
2112 strcpy(keyserver
->host
,"keys.");
2113 strcat(keyserver
->host
,domain
);
2114 keyserver
->uri
=xmalloc(strlen(keyserver
->scheme
)+
2115 3+strlen(keyserver
->host
)+1);
2116 strcpy(keyserver
->uri
,keyserver
->scheme
);
2117 strcat(keyserver
->uri
,"://");
2118 strcat(keyserver
->uri
,keyserver
->host
);
2120 rc
=keyserver_work(KS_GETNAME
,list
,NULL
,0,fpr
,fpr_len
,keyserver
);
2124 free_keyserver_spec(keyserver
);