1 /* keyserver.c - generic keyserver code
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 * 2009 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 3 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, see <http://www.gnu.org/licenses/>.
41 #include "keyserver-internal.h"
46 #ifdef HAVE_W32_SYSTEM
47 /* It seems Vista doesn't grok X_OK and so fails access() tests.
48 Previous versions interpreted X_OK as F_OK anyway, so we'll just
52 #endif /* HAVE_W32_SYSTEM */
56 KEYDB_SEARCH_DESC desc
;
57 u32 createtime
,expiretime
;
64 enum ks_action
{KS_UNKNOWN
=0,KS_GET
,KS_GETNAME
,KS_SEND
,KS_SEARCH
};
66 static struct parse_options keyserver_opts
[]=
68 /* some of these options are not real - just for the help
70 {"max-cert-size",0,NULL
,NULL
},
71 {"include-revoked",0,NULL
,N_("include revoked keys in search results")},
72 {"include-subkeys",0,NULL
,N_("include subkeys when searching by key ID")},
73 {"use-temp-files",0,NULL
,
74 N_("use temporary files to pass data to keyserver helpers")},
75 {"keep-temp-files",KEYSERVER_KEEP_TEMP_FILES
,NULL
,
76 N_("do not delete temporary files after using them")},
77 {"refresh-add-fake-v3-keyids",KEYSERVER_ADD_FAKE_V3
,NULL
,
79 {"auto-key-retrieve",KEYSERVER_AUTO_KEY_RETRIEVE
,NULL
,
80 N_("automatically retrieve keys when verifying signatures")},
81 {"honor-keyserver-url",KEYSERVER_HONOR_KEYSERVER_URL
,NULL
,
82 N_("honor the preferred keyserver URL set on the key")},
83 {"honor-pka-record",KEYSERVER_HONOR_PKA_RECORD
,NULL
,
84 N_("honor the PKA record set on a key when retrieving keys")},
88 static int keyserver_work(enum ks_action action
,strlist_t list
,
89 KEYDB_SEARCH_DESC
*desc
,int count
,
90 unsigned char **fpr
,size_t *fpr_len
,
91 struct keyserver_spec
*keyserver
);
93 /* Reasonable guess */
94 #define DEFAULT_MAX_CERT_SIZE 16384
96 static size_t max_cert_size
=DEFAULT_MAX_CERT_SIZE
;
99 add_canonical_option(char *option
,strlist_t
*list
)
101 char *arg
=argsplit(option
);
107 joined
=xmalloc(strlen(option
)+1+strlen(arg
)+1);
108 /* Make a canonical name=value form with no spaces */
109 strcpy(joined
,option
);
112 append_to_strlist(list
,joined
);
116 append_to_strlist(list
,option
);
120 parse_keyserver_options(char *options
)
126 keyserver_opts
[0].value
=&max_cert
;
128 while((tok
=optsep(&options
)))
133 /* For backwards compatibility. 1.2.x used honor-http-proxy and
134 there are a good number of documents published that recommend
136 if(ascii_strcasecmp(tok
,"honor-http-proxy")==0)
138 else if(ascii_strcasecmp(tok
,"no-honor-http-proxy")==0)
141 /* We accept quite a few possible options here - some options to
142 handle specially, the keyserver_options list, and import and
143 export options that pertain to keyserver operations. Note
144 that you must use strncasecmp here as there might be an
145 =argument attached which will foil the use of strcasecmp. */
147 #ifdef EXEC_TEMPFILE_ONLY
148 if(ascii_strncasecmp(tok
,"use-temp-files",14)==0 ||
149 ascii_strncasecmp(tok
,"no-use-temp-files",17)==0)
150 log_info(_("WARNING: keyserver option `%s' is not used"
151 " on this platform\n"),tok
);
153 if(ascii_strncasecmp(tok
,"use-temp-files",14)==0)
154 opt
.keyserver_options
.options
|=KEYSERVER_USE_TEMP_FILES
;
155 else if(ascii_strncasecmp(tok
,"no-use-temp-files",17)==0)
156 opt
.keyserver_options
.options
&=~KEYSERVER_USE_TEMP_FILES
;
158 else if(!parse_options(tok
,&opt
.keyserver_options
.options
,
160 && !parse_import_options(tok
,
161 &opt
.keyserver_options
.import_options
,0)
162 && !parse_export_options(tok
,
163 &opt
.keyserver_options
.export_options
,0))
165 /* All of the standard options have failed, so the option is
166 destined for a keyserver plugin. */
167 add_canonical_option(tok
,&opt
.keyserver_options
.other
);
173 max_cert_size
=strtoul(max_cert
,(char **)NULL
,10);
176 max_cert_size
=DEFAULT_MAX_CERT_SIZE
;
183 free_keyserver_spec(struct keyserver_spec
*keyserver
)
185 xfree(keyserver
->uri
);
186 xfree(keyserver
->scheme
);
187 xfree(keyserver
->auth
);
188 xfree(keyserver
->host
);
189 xfree(keyserver
->port
);
190 xfree(keyserver
->path
);
191 xfree(keyserver
->opaque
);
192 free_strlist(keyserver
->options
);
196 /* Return 0 for match */
198 cmp_keyserver_spec(struct keyserver_spec
*one
,struct keyserver_spec
*two
)
200 if(ascii_strcasecmp(one
->scheme
,two
->scheme
)==0)
202 if(one
->host
&& two
->host
&& ascii_strcasecmp(one
->host
,two
->host
)==0)
204 if((one
->port
&& two
->port
205 && ascii_strcasecmp(one
->port
,two
->port
)==0)
206 || (!one
->port
&& !two
->port
))
209 else if(one
->opaque
&& two
->opaque
210 && ascii_strcasecmp(one
->opaque
,two
->opaque
)==0)
217 /* Try and match one of our keyservers. If we can, return that. If
218 we can't, return our input. */
219 struct keyserver_spec
*
220 keyserver_match(struct keyserver_spec
*spec
)
222 struct keyserver_spec
*ks
;
224 for(ks
=opt
.keyserver
;ks
;ks
=ks
->next
)
225 if(cmp_keyserver_spec(spec
,ks
)==0)
231 /* TODO: once we cut over to an all-curl world, we don't need this
232 parser any longer so it can be removed, or at least moved to
233 keyserver/ksutil.c for limited use in gpgkeys_ldap or the like. */
235 struct keyserver_spec
*
236 parse_keyserver_uri(const char *string
,int require_scheme
,
237 const char *configname
,unsigned int configlineno
)
240 struct keyserver_spec
*keyserver
;
245 assert(string
!=NULL
);
247 keyserver
=xmalloc_clear(sizeof(struct keyserver_spec
));
251 options
=strchr(uri
,' ');
259 while((tok
=optsep(&options
)))
260 add_canonical_option(tok
,&keyserver
->options
);
265 for(idx
=uri
,count
=0;*idx
&& *idx
!=':';idx
++)
269 /* Do we see the start of an RFC-2732 ipv6 address here? If so,
270 there clearly isn't a scheme so get out early. */
273 /* Was the '[' the first thing in the string? If not, we
274 have a mangled scheme with a [ in it so fail. */
285 if(*idx
=='\0' || *idx
=='[')
290 /* Assume HKP if there is no scheme */
292 keyserver
->scheme
=xstrdup("hkp");
294 keyserver
->uri
=xmalloc(strlen(keyserver
->scheme
)+3+strlen(uri
)+1);
295 strcpy(keyserver
->uri
,keyserver
->scheme
);
296 strcat(keyserver
->uri
,"://");
297 strcat(keyserver
->uri
,uri
);
303 keyserver
->uri
=xstrdup(uri
);
305 keyserver
->scheme
=xmalloc(count
+1);
307 /* Force to lowercase */
309 keyserver
->scheme
[i
]=ascii_tolower(uri
[i
]);
311 keyserver
->scheme
[i
]='\0';
313 /* Skip past the scheme and colon */
317 if(ascii_strcasecmp(keyserver
->scheme
,"x-broken-hkp")==0)
319 deprecated_warning(configname
,configlineno
,"x-broken-hkp",
320 "--keyserver-options ","broken-http-proxy");
321 xfree(keyserver
->scheme
);
322 keyserver
->scheme
=xstrdup("hkp");
323 append_to_strlist(&opt
.keyserver_options
.other
,"broken-http-proxy");
325 else if(ascii_strcasecmp(keyserver
->scheme
,"x-hkp")==0)
327 /* Canonicalize this to "hkp" so it works with both the internal
328 and external keyserver interface. */
329 xfree(keyserver
->scheme
);
330 keyserver
->scheme
=xstrdup("hkp");
333 if (uri
[0]=='/' && uri
[1]=='/' && uri
[2] == '/')
335 /* Three slashes means network path with a default host name.
336 This is a hack because it does not crok all possible
337 combiantions. We should better repalce all code bythe parser
339 keyserver
->path
= xstrdup (uri
+2);
341 else if(assume_hkp
|| (uri
[0]=='/' && uri
[1]=='/'))
343 /* Two slashes means network path. */
345 /* Skip over the "//", if any */
349 /* Do we have userinfo auth data present? */
350 for(idx
=uri
,count
=0;*idx
&& *idx
!='@' && *idx
!='/';idx
++)
353 /* We found a @ before the slash, so that means everything
354 before the @ is auth data. */
360 keyserver
->auth
=xmalloc(count
+1);
361 strncpy(keyserver
->auth
,uri
,count
);
362 keyserver
->auth
[count
]='\0';
366 /* Is it an RFC-2732 ipv6 [literal address] ? */
369 for(idx
=uri
+1,count
=1;*idx
370 && ((isascii (*idx
) && isxdigit(*idx
))
371 || *idx
==':' || *idx
=='.');idx
++)
374 /* Is the ipv6 literal address terminated? */
381 for(idx
=uri
,count
=0;*idx
&& *idx
!=':' && *idx
!='/';idx
++)
387 keyserver
->host
=xmalloc(count
+1);
388 strncpy(keyserver
->host
,uri
,count
);
389 keyserver
->host
[count
]='\0';
391 /* Skip past the host */
396 /* It would seem to be reasonable to limit the range of the
397 ports to values between 1-65535, but RFC 1738 and 1808
398 imply there is no limit. Of course, the real world has
401 for(idx
=uri
+1,count
=0;*idx
&& *idx
!='/';idx
++)
405 /* Ports are digits only */
410 keyserver
->port
=xmalloc(count
+1);
411 strncpy(keyserver
->port
,uri
+1,count
);
412 keyserver
->port
[count
]='\0';
414 /* Skip past the colon and port number */
418 /* Everything else is the path */
420 keyserver
->path
=xstrdup(uri
);
422 keyserver
->path
=xstrdup("/");
424 if(keyserver
->path
[1])
425 keyserver
->flags
.direct_uri
=1;
429 /* No slash means opaque. Just record the opaque blob and get
431 keyserver
->opaque
=xstrdup(uri
);
435 /* One slash means absolute path. We don't need to support that
443 free_keyserver_spec(keyserver
);
448 struct keyserver_spec
*
449 parse_preferred_keyserver(PKT_signature
*sig
)
451 struct keyserver_spec
*spec
=NULL
;
455 p
=parse_sig_subpkt(sig
->hashed
,SIGSUBPKT_PREF_KS
,&plen
);
458 byte
*dupe
=xmalloc(plen
+1);
462 spec
=parse_keyserver_uri(dupe
,1,NULL
,0);
470 print_keyrec(int number
,struct keyrec
*keyrec
)
474 iobuf_writebyte(keyrec
->uidbuf
,0);
475 iobuf_flush_temp(keyrec
->uidbuf
);
476 printf("(%d)\t%s ",number
,iobuf_get_temp_buffer(keyrec
->uidbuf
));
479 printf("%d bit ",keyrec
->size
);
483 const char *str
= gcry_pk_algo_name (keyrec
->type
);
491 switch(keyrec
->desc
.mode
)
493 /* If the keyserver helper gave us a short keyid, we have no
494 choice but to use it. Do check --keyid-format to add a 0x if
496 case KEYDB_SEARCH_MODE_SHORT_KID
:
497 printf("key %s%08lX",
498 (opt
.keyid_format
==KF_0xSHORT
499 || opt
.keyid_format
==KF_0xLONG
)?"0x":"",
500 (ulong
)keyrec
->desc
.u
.kid
[1]);
503 /* However, if it gave us a long keyid, we can honor
505 case KEYDB_SEARCH_MODE_LONG_KID
:
506 printf("key %s",keystr(keyrec
->desc
.u
.kid
));
509 case KEYDB_SEARCH_MODE_FPR16
:
512 printf("%02X",keyrec
->desc
.u
.fpr
[i
]);
515 case KEYDB_SEARCH_MODE_FPR20
:
518 printf("%02X",keyrec
->desc
.u
.fpr
[i
]);
526 if(keyrec
->createtime
>0)
529 printf(_("created: %s"),strtimestamp(keyrec
->createtime
));
532 if(keyrec
->expiretime
>0)
535 printf(_("expires: %s"),strtimestamp(keyrec
->expiretime
));
539 printf(" (%s)",_("revoked"));
541 printf(" (%s)",_("disabled"));
543 printf(" (%s)",_("expired"));
548 /* Returns a keyrec (which must be freed) once a key is complete, and
549 NULL otherwise. Call with a NULL keystring once key parsing is
550 complete to return any unfinished keys. */
551 static struct keyrec
*
552 parse_keyrec(char *keystring
)
554 static struct keyrec
*work
=NULL
;
555 struct keyrec
*ret
=NULL
;
563 else if(work
->desc
.mode
==KEYDB_SEARCH_MODE_NONE
)
578 work
=xmalloc_clear(sizeof(struct keyrec
));
579 work
->uidbuf
=iobuf_temp();
582 /* Remove trailing whitespace */
583 for(i
=strlen(keystring
);i
>0;i
--)
584 if(ascii_isspace(keystring
[i
-1]))
589 if((record
=strsep(&keystring
,":"))==NULL
)
592 if(ascii_strcasecmp("pub",record
)==0)
599 work
=xmalloc_clear(sizeof(struct keyrec
));
600 work
->uidbuf
=iobuf_temp();
603 if((tok
=strsep(&keystring
,":"))==NULL
)
606 classify_user_id(tok
,&work
->desc
);
607 if(work
->desc
.mode
!=KEYDB_SEARCH_MODE_SHORT_KID
608 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_LONG_KID
609 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_FPR16
610 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_FPR20
)
612 work
->desc
.mode
=KEYDB_SEARCH_MODE_NONE
;
616 /* Note all items after this are optional. This allows us to
617 have a pub line as simple as pub:keyid and nothing else. */
621 if((tok
=strsep(&keystring
,":"))==NULL
)
624 work
->type
=atoi(tok
);
626 if((tok
=strsep(&keystring
,":"))==NULL
)
629 work
->size
=atoi(tok
);
631 if((tok
=strsep(&keystring
,":"))==NULL
)
637 work
->createtime
=atoi(tok
);
639 if((tok
=strsep(&keystring
,":"))==NULL
)
646 work
->expiretime
=atoi(tok
);
647 /* Force the 'e' flag on if this key is expired. */
648 if(work
->expiretime
<=make_timestamp())
652 if((tok
=strsep(&keystring
,":"))==NULL
)
674 else if(ascii_strcasecmp("uid",record
)==0 && work
->desc
.mode
)
676 char *userid
,*tok
,*decoded
;
678 if((tok
=strsep(&keystring
,":"))==NULL
)
686 /* By definition, de-%-encoding is always smaller than the
687 original string so we can decode in place. */
692 if(tok
[0]=='%' && tok
[1] && tok
[2])
696 userid
[i
] = (c
=hextobyte(&tok
[1])) == -1 ? '?' : c
;
703 /* We don't care about the other info provided in the uid: line
704 since no keyserver supports marking userids with timestamps
705 or revoked/expired/disabled yet. */
707 /* No need to check for control characters, as utf8_to_native
710 decoded
=utf8_to_native(userid
,i
,0);
711 if(strlen(decoded
)>opt
.screen_columns
-10)
712 decoded
[opt
.screen_columns
-10]='\0';
713 iobuf_writestr(work
->uidbuf
,decoded
);
715 iobuf_writestr(work
->uidbuf
,"\n\t");
719 /* Ignore any records other than "pri" and "uid" for easy future
725 /* TODO: do this as a list sent to keyserver_work rather than calling
726 it once for each key to get the correct counts after the import
727 (cosmetics, really) and to better take advantage of the keyservers
728 that can do multiple fetches in one go (LDAP). */
730 show_prompt(KEYDB_SEARCH_DESC
*desc
,int numdesc
,int count
,const char *search
)
736 if(count
&& opt
.command_fd
==-1)
739 tty_printf("Keys %d-%d of %d for \"%s\". ",from
,numdesc
,count
,search
);
743 answer
=cpr_get_no_help("keysearch.prompt",
744 _("Enter number(s), N)ext, or Q)uit > "));
746 if(answer
[0]=='\x04')
752 if(answer
[0]=='q' || answer
[0]=='Q')
757 else if(atoi(answer
)>=1 && atoi(answer
)<=numdesc
)
759 char *split
=answer
,*num
;
761 while((num
=strsep(&split
," ,"))!=NULL
)
762 if(atoi(num
)>=1 && atoi(num
)<=numdesc
)
763 keyserver_work(KS_GET
,NULL
,&desc
[atoi(num
)-1],1,
764 NULL
,NULL
,opt
.keyserver
);
773 /* Count and searchstr are just for cosmetics. If the count is too
774 small, it will grow safely. If negative it disables the "Key x-y
775 of z" messages. searchstr should be UTF-8 (rather than native). */
777 keyserver_search_prompt(IOBUF buffer
,const char *searchstr
)
779 int i
=0,validcount
=0,started
=0,header
=0,count
=1;
780 unsigned int maxlen
,buflen
,numlines
=0;
781 KEYDB_SEARCH_DESC
*desc
;
786 localstr
=utf8_to_native(searchstr
,strlen(searchstr
),0);
788 desc
=xmalloc(count
*sizeof(KEYDB_SEARCH_DESC
));
792 struct keyrec
*keyrec
;
796 rl
=iobuf_read_line(buffer
,&line
,&buflen
,&maxlen
);
800 if(!header
&& ascii_strncasecmp("SEARCH ",line
,7)==0
801 && ascii_strncasecmp(" BEGIN",&line
[strlen(line
)-7],6)==0)
806 else if(ascii_strncasecmp("SEARCH ",line
,7)==0
807 && ascii_strncasecmp(" END",&line
[strlen(line
)-5],4)==0)
813 /* Look for an info: line. The only current info: values
814 defined are the version and key count. */
815 if(!started
&& rl
>0 && ascii_strncasecmp("info:",line
,5)==0)
817 char *tok
,*str
=&line
[5];
819 if((tok
=strsep(&str
,":"))!=NULL
)
823 if(sscanf(tok
,"%d",&version
)!=1)
828 log_error(_("invalid keyserver protocol "
829 "(us %d!=handler %d)\n"),1,version
);
834 if((tok
=strsep(&str
,":"))!=NULL
&& sscanf(tok
,"%d",&count
)==1)
843 desc
=xrealloc(desc
,count
*sizeof(KEYDB_SEARCH_DESC
));
852 keyrec
=parse_keyrec(NULL
);
865 if (opt
.with_colons
&& opt
.batch
)
870 if(show_prompt(desc
,i
,validcount
?count
:0,localstr
))
879 keyrec
=parse_keyrec(line
);
883 /* keyserver helper sent more keys than they claimed in the
886 desc
=xrealloc(desc
,count
*sizeof(KEYDB_SEARCH_DESC
));
892 desc
[i
]=keyrec
->desc
;
896 /* screen_lines - 1 for the prompt. */
897 if(numlines
+keyrec
->lines
>opt
.screen_lines
-1)
899 if(show_prompt(desc
,i
,validcount
?count
:0,localstr
))
905 print_keyrec(i
+1,keyrec
);
908 numlines
+=keyrec
->lines
;
909 iobuf_close(keyrec
->uidbuf
);
918 /* Leave this commented out or now, and perhaps for a very long
919 time. All HKPish servers return HTML error messages for
923 log_info(_("keyserver does not support searching\n"));
929 log_info(_("key \"%s\" not found on keyserver\n"),localstr
);
931 log_info(_("key not found on keyserver\n"));
939 /* We sometimes want to use a different gpgkeys_xxx for a given
940 protocol (for example, ldaps is handled by gpgkeys_ldap). Map
943 keyserver_typemap(const char *type
)
945 if(strcmp(type
,"ldaps")==0)
947 else if(strcmp(type
,"hkps")==0)
953 /* The PGP LDAP and the curl fetch-a-LDAP-object methodologies are
954 sufficiently different that we can't use curl to do LDAP. */
956 direct_uri_map(const char *scheme
,unsigned int is_direct
)
958 if(is_direct
&& strcmp(scheme
,"ldap")==0)
964 #if GNUPG_MAJOR_VERSION == 2
965 #define GPGKEYS_PREFIX "gpg2keys_"
967 #define GPGKEYS_PREFIX "gpgkeys_"
969 #define GPGKEYS_CURL GPGKEYS_PREFIX "curl" EXEEXT
970 #define GPGKEYS_PREFIX_LEN (strlen(GPGKEYS_CURL))
971 #define KEYSERVER_ARGS_KEEP " -o \"%O\" \"%I\""
972 #define KEYSERVER_ARGS_NOKEEP " -o \"%o\" \"%i\""
975 keyserver_spawn(enum ks_action action
,strlist_t list
,KEYDB_SEARCH_DESC
*desc
,
976 int count
,int *prog
,unsigned char **fpr
,size_t *fpr_len
,
977 struct keyserver_spec
*keyserver
)
979 int ret
=0,i
,gotversion
=0,outofband
=0;
981 unsigned int maxlen
,buflen
;
982 char *command
,*end
,*searchstr
=NULL
;
984 struct exec_info
*spawn
;
986 const char *libexecdir
= gnupg_libexecdir ();
990 #ifdef EXEC_TEMPFILE_ONLY
991 opt
.keyserver_options
.options
|=KEYSERVER_USE_TEMP_FILES
;
994 /* Build the filename for the helper to execute */
995 scheme
=keyserver_typemap(keyserver
->scheme
);
997 #ifdef DISABLE_KEYSERVER_PATH
998 /* Destroy any path we might have. This is a little tricky,
999 portability-wise. It's not correct to delete the PATH
1000 environment variable, as that may fall back to a system built-in
1001 PATH. Similarly, it is not correct to set PATH to the null
1002 string (PATH="") since this actually deletes the PATH environment
1003 variable under MinGW. The safest thing to do here is to force
1004 PATH to be GNUPG_LIBEXECDIR. All this is not that meaningful on
1005 Unix-like systems (since we're going to give a full path to
1006 gpgkeys_foo), but on W32 it prevents loading any DLLs from
1007 directories in %PATH%.
1009 After some more thinking about this we came to the conclusion
1010 that it is better to load the helpers from the directory where
1011 the program of this process lives. Fortunately Windows provides
1012 a way to retrieve this and our gnupg_libexecdir function has been
1013 modified to return just this. Setting the exec-path is not
1015 set_exec_path(libexecdir);
1018 if(opt
.exec_path_set
)
1020 /* If exec-path was set, and DISABLE_KEYSERVER_PATH is
1021 undefined, then don't specify a full path to gpgkeys_foo, so
1022 that the PATH can work. */
1023 command
=xmalloc(GPGKEYS_PREFIX_LEN
+strlen(scheme
)+3+strlen(EXEEXT
)+1);
1029 /* Specify a full path to gpgkeys_foo. */
1030 command
=xmalloc(strlen(libexecdir
)+strlen(DIRSEP_S
)+
1031 GPGKEYS_PREFIX_LEN
+strlen(scheme
)+3+strlen(EXEEXT
)+1);
1032 strcpy(command
,libexecdir
);
1033 strcat(command
,DIRSEP_S
);
1036 end
=command
+strlen(command
);
1038 /* Build a path for the keyserver helper. If it is direct_uri
1039 (i.e. an object fetch and not a keyserver), then add "_uri" to
1040 the end to distinguish the keyserver helper from an object
1041 fetcher that can speak that protocol (this is a problem for
1044 strcat(command
,GPGKEYS_PREFIX
);
1045 strcat(command
,scheme
);
1047 /* This "_uri" thing is in case we need to call a direct handler
1048 instead of the keyserver handler. This lets us use gpgkeys_curl
1049 or gpgkeys_ldap_uri (we don't provide it, but a user might)
1050 instead of gpgkeys_ldap to fetch things like
1051 ldap://keyserver.pgp.com/o=PGP%20keys?pgpkey?sub?pgpkeyid=99242560 */
1053 if(direct_uri_map(scheme
,keyserver
->flags
.direct_uri
))
1054 strcat(command
,"_uri");
1056 strcat(command
,EXEEXT
);
1058 /* Can we execute it? If not, try curl as our catchall. */
1059 if(path_access(command
,X_OK
)!=0)
1060 strcpy(end
,GPGKEYS_CURL
);
1062 if(opt
.keyserver_options
.options
&KEYSERVER_USE_TEMP_FILES
)
1064 if(opt
.keyserver_options
.options
&KEYSERVER_KEEP_TEMP_FILES
)
1066 command
=xrealloc(command
,strlen(command
)+
1067 strlen(KEYSERVER_ARGS_KEEP
)+1);
1068 strcat(command
,KEYSERVER_ARGS_KEEP
);
1072 command
=xrealloc(command
,strlen(command
)+
1073 strlen(KEYSERVER_ARGS_NOKEEP
)+1);
1074 strcat(command
,KEYSERVER_ARGS_NOKEEP
);
1077 ret
=exec_write(&spawn
,NULL
,command
,NULL
,0,0);
1080 ret
=exec_write(&spawn
,command
,NULL
,NULL
,0,0);
1087 fprintf(spawn
->tochild
,
1088 "# This is a GnuPG %s keyserver communications file\n",VERSION
);
1089 fprintf(spawn
->tochild
,"VERSION %d\n",KEYSERVER_PROTO_VERSION
);
1090 fprintf(spawn
->tochild
,"PROGRAM %s\n",VERSION
);
1091 fprintf(spawn
->tochild
,"SCHEME %s\n",keyserver
->scheme
);
1093 if(keyserver
->opaque
)
1094 fprintf(spawn
->tochild
,"OPAQUE %s\n",keyserver
->opaque
);
1098 fprintf(spawn
->tochild
,"AUTH %s\n",keyserver
->auth
);
1101 fprintf(spawn
->tochild
,"HOST %s\n",keyserver
->host
);
1104 fprintf(spawn
->tochild
,"PORT %s\n",keyserver
->port
);
1107 fprintf(spawn
->tochild
,"PATH %s\n",keyserver
->path
);
1110 /* Write global options */
1112 for(temp
=opt
.keyserver_options
.other
;temp
;temp
=temp
->next
)
1113 fprintf(spawn
->tochild
,"OPTION %s\n",temp
->d
);
1115 /* Write per-keyserver options */
1117 for(temp
=keyserver
->options
;temp
;temp
=temp
->next
)
1118 fprintf(spawn
->tochild
,"OPTION %s\n",temp
->d
);
1124 fprintf(spawn
->tochild
,"COMMAND GET\n\n");
1126 /* Which keys do we want? */
1128 for(i
=0;i
<count
;i
++)
1132 if(desc
[i
].mode
==KEYDB_SEARCH_MODE_FPR20
)
1136 fprintf(spawn
->tochild
,"0x");
1138 for(f
=0;f
<MAX_FINGERPRINT_LEN
;f
++)
1139 fprintf(spawn
->tochild
,"%02X",desc
[i
].u
.fpr
[f
]);
1141 fprintf(spawn
->tochild
,"\n");
1143 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_FPR16
)
1147 fprintf(spawn
->tochild
,"0x");
1150 fprintf(spawn
->tochild
,"%02X",desc
[i
].u
.fpr
[f
]);
1152 fprintf(spawn
->tochild
,"\n");
1154 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_LONG_KID
)
1155 fprintf(spawn
->tochild
,"0x%08lX%08lX\n",
1156 (ulong
)desc
[i
].u
.kid
[0],
1157 (ulong
)desc
[i
].u
.kid
[1]);
1158 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_SHORT_KID
)
1159 fprintf(spawn
->tochild
,"0x%08lX\n",
1160 (ulong
)desc
[i
].u
.kid
[1]);
1161 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_EXACT
)
1163 fprintf(spawn
->tochild
,"0x0000000000000000\n");
1166 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_NONE
)
1174 log_info(_("requesting key %s from %s server %s\n"),
1175 keystr_from_desc(&desc
[i
]),
1176 keyserver
->scheme
,keyserver
->host
);
1178 log_info(_("requesting key %s from %s\n"),
1179 keystr_from_desc(&desc
[i
]),keyserver
->uri
);
1183 fprintf(spawn
->tochild
,"\n");
1192 fprintf(spawn
->tochild
,"COMMAND GETNAME\n\n");
1194 /* Which names do we want? */
1196 for(key
=list
;key
!=NULL
;key
=key
->next
)
1197 fprintf(spawn
->tochild
,"%s\n",key
->d
);
1199 fprintf(spawn
->tochild
,"\n");
1202 log_info(_("searching for names from %s server %s\n"),
1203 keyserver
->scheme
,keyserver
->host
);
1205 log_info(_("searching for names from %s\n"),keyserver
->uri
);
1214 /* Note the extra \n here to send an empty keylist block */
1215 fprintf(spawn
->tochild
,"COMMAND SEND\n\n\n");
1217 for(key
=list
;key
!=NULL
;key
=key
->next
)
1219 armor_filter_context_t
*afx
;
1220 IOBUF buffer
= iobuf_temp ();
1224 add_to_strlist(&temp
,key
->d
);
1226 afx
= new_armor_context ();
1228 /* Tell the armor filter to use Unix-style \n line
1229 endings, since we're going to fprintf this to a file
1230 that (on Win32) is open in text mode. The win32 stdio
1231 will transform the \n to \r\n and we'll end up with the
1232 proper line endings on win32. This is a no-op on
1235 push_armor_filter (afx
, buffer
);
1236 release_armor_context (afx
);
1238 /* TODO: Remove Comment: lines from keys exported this
1241 if(export_pubkeys_stream(buffer
,temp
,&block
,
1242 opt
.keyserver_options
.export_options
)==-1)
1243 iobuf_close(buffer
);
1248 iobuf_flush_temp(buffer
);
1250 merge_keys_and_selfsig(block
);
1252 fprintf(spawn
->tochild
,"INFO %08lX%08lX BEGIN\n",
1253 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[0],
1254 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[1]);
1256 for(node
=block
;node
;node
=node
->next
)
1258 switch(node
->pkt
->pkttype
)
1263 case PKT_PUBLIC_KEY
:
1264 case PKT_PUBLIC_SUBKEY
:
1266 PKT_public_key
*pk
=node
->pkt
->pkt
.public_key
;
1268 keyid_from_pk(pk
,NULL
);
1270 fprintf(spawn
->tochild
,"%sb:%08lX%08lX:%u:%u:%u:%u:",
1271 node
->pkt
->pkttype
==PKT_PUBLIC_KEY
?"pu":"su",
1272 (ulong
)pk
->keyid
[0],(ulong
)pk
->keyid
[1],
1279 fprintf(spawn
->tochild
,"r");
1281 fprintf(spawn
->tochild
,"e");
1283 fprintf(spawn
->tochild
,"\n");
1289 PKT_user_id
*uid
=node
->pkt
->pkt
.user_id
;
1292 if(uid
->attrib_data
)
1295 fprintf(spawn
->tochild
,"uid:");
1297 /* Quote ':', '%', and any 8-bit
1299 for(r
=0;r
<uid
->len
;r
++)
1301 if(uid
->name
[r
]==':' || uid
->name
[r
]=='%'
1302 || uid
->name
[r
]&0x80)
1303 fprintf(spawn
->tochild
,"%%%02X",
1304 (byte
)uid
->name
[r
]);
1306 fprintf(spawn
->tochild
,"%c",uid
->name
[r
]);
1309 fprintf(spawn
->tochild
,":%u:%u:",
1310 uid
->created
,uid
->expiredate
);
1313 fprintf(spawn
->tochild
,"r");
1315 fprintf(spawn
->tochild
,"e");
1317 fprintf(spawn
->tochild
,"\n");
1321 /* This bit is really for the benefit of
1322 people who store their keys in LDAP
1323 servers. It makes it easy to do queries
1324 for things like "all keys signed by
1328 PKT_signature
*sig
=node
->pkt
->pkt
.signature
;
1330 if(!IS_UID_SIG(sig
))
1333 fprintf(spawn
->tochild
,"sig:%08lX%08lX:%X:%u:%u\n",
1334 (ulong
)sig
->keyid
[0],(ulong
)sig
->keyid
[1],
1335 sig
->sig_class
,sig
->timestamp
,
1342 fprintf(spawn
->tochild
,"INFO %08lX%08lX END\n",
1343 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[0],
1344 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[1]);
1346 fprintf(spawn
->tochild
,"KEY %08lX%08lX BEGIN\n",
1347 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[0],
1348 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[1]);
1349 fwrite(iobuf_get_temp_buffer(buffer
),
1350 iobuf_get_temp_length(buffer
),1,spawn
->tochild
);
1351 fprintf(spawn
->tochild
,"KEY %08lX%08lX END\n",
1352 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[0],
1353 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[1]);
1355 iobuf_close(buffer
);
1358 log_info(_("sending key %s to %s server %s\n"),
1359 keystr(block
->pkt
->pkt
.public_key
->keyid
),
1360 keyserver
->scheme
,keyserver
->host
);
1362 log_info(_("sending key %s to %s\n"),
1363 keystr(block
->pkt
->pkt
.public_key
->keyid
),
1366 release_kbnode(block
);
1379 fprintf(spawn
->tochild
,"COMMAND SEARCH\n\n");
1381 /* Which keys do we want? Remember that the gpgkeys_ program
1382 is going to lump these together into a search string. */
1384 for(key
=list
;key
!=NULL
;key
=key
->next
)
1386 fprintf(spawn
->tochild
,"%s\n",key
->d
);
1389 searchstr
=xrealloc(searchstr
,
1390 strlen(searchstr
)+strlen(key
->d
)+2);
1391 strcat(searchstr
," ");
1395 searchstr
=xmalloc(strlen(key
->d
)+1);
1399 strcat(searchstr
,key
->d
);
1402 fprintf(spawn
->tochild
,"\n");
1405 log_info(_("searching for \"%s\" from %s server %s\n"),
1406 searchstr
,keyserver
->scheme
,keyserver
->host
);
1408 log_info(_("searching for \"%s\" from %s\n"),
1409 searchstr
,keyserver
->uri
);
1415 log_fatal(_("no keyserver action!\n"));
1419 /* Done sending, so start reading. */
1420 ret
=exec_read(spawn
);
1424 /* Now handle the response */
1432 if(iobuf_read_line(spawn
->fromchild
,&line
,&buflen
,&maxlen
)==0)
1434 ret
= gpg_error_from_syserror ();
1435 goto fail
; /* i.e. EOF */
1440 /* remove trailing whitespace */
1442 while(plen
>0 && ascii_isspace(ptr
[plen
-1]))
1449 if(ascii_strncasecmp(ptr
,"VERSION ",8)==0)
1453 if(atoi(&ptr
[8])!=KEYSERVER_PROTO_VERSION
)
1455 log_error(_("invalid keyserver protocol (us %d!=handler %d)\n"),
1456 KEYSERVER_PROTO_VERSION
,atoi(&ptr
[8]));
1460 else if(ascii_strncasecmp(ptr
,"PROGRAM ",8)==0)
1462 if(ascii_strncasecmp(&ptr
[8],VERSION
,strlen(VERSION
))!=0)
1463 log_info(_("WARNING: keyserver handler from a different"
1464 " version of GnuPG (%s)\n"),&ptr
[8]);
1466 else if(ascii_strncasecmp(ptr
,"OPTION OUTOFBAND",16)==0)
1467 outofband
=1; /* Currently the only OPTION */
1472 log_error(_("keyserver did not send VERSION\n"));
1484 stats_handle
=import_new_stats_handle();
1486 /* Slurp up all the key data. In the future, it might be
1487 nice to look for KEY foo OUTOFBAND and FAILED indicators.
1488 It's harmless to ignore them, but ignoring them does make
1489 gpg complain about "no valid OpenPGP data found". One
1490 way to do this could be to continue parsing this
1491 line-by-line and make a temp iobuf for each key. */
1493 import_keys_stream(spawn
->fromchild
,stats_handle
,fpr
,fpr_len
,
1494 opt
.keyserver_options
.import_options
);
1496 import_print_stats(stats_handle
);
1497 import_release_stats_handle(stats_handle
);
1502 /* Nothing to do here */
1507 keyserver_search_prompt(spawn
->fromchild
,searchstr
);
1511 log_fatal(_("no keyserver action!\n"));
1520 *prog
=exec_finish(spawn
);
1526 keyserver_work(enum ks_action action
,strlist_t list
,KEYDB_SEARCH_DESC
*desc
,
1527 int count
,unsigned char **fpr
,size_t *fpr_len
,
1528 struct keyserver_spec
*keyserver
)
1534 log_error(_("no keyserver known (use option --keyserver)\n"));
1535 return G10ERR_BAD_URI
;
1538 #ifdef DISABLE_KEYSERVER_HELPERS
1540 log_error(_("external keyserver calls are not supported in this build\n"));
1541 return G10ERR_KEYSERVER
;
1544 /* Spawn a handler */
1546 rc
=keyserver_spawn(action
,list
,desc
,count
,&ret
,fpr
,fpr_len
,keyserver
);
1551 case KEYSERVER_SCHEME_NOT_FOUND
:
1552 log_error(_("no handler for keyserver scheme `%s'\n"),
1556 case KEYSERVER_NOT_SUPPORTED
:
1557 log_error(_("action `%s' not supported with keyserver "
1559 action
==KS_GET
?"get":action
==KS_SEND
?"send":
1560 action
==KS_SEARCH
?"search":"unknown",
1564 case KEYSERVER_VERSION_ERROR
:
1565 log_error(_(GPGKEYS_PREFIX
"%s does not support"
1566 " handler version %d\n"),
1567 keyserver_typemap(keyserver
->scheme
),
1568 KEYSERVER_PROTO_VERSION
);
1571 case KEYSERVER_TIMEOUT
:
1572 log_error(_("keyserver timed out\n"));
1575 case KEYSERVER_INTERNAL_ERROR
:
1577 log_error(_("keyserver internal error\n"));
1581 return G10ERR_KEYSERVER
;
1586 log_error(_("keyserver communications error: %s\n"),g10_errstr(rc
));
1592 #endif /* ! DISABLE_KEYSERVER_HELPERS*/
1596 keyserver_export(strlist_t users
)
1599 KEYDB_SEARCH_DESC desc
;
1602 /* Weed out descriptors that we don't support sending */
1603 for(;users
;users
=users
->next
)
1605 classify_user_id (users
->d
, &desc
);
1606 if(desc
.mode
!=KEYDB_SEARCH_MODE_SHORT_KID
&&
1607 desc
.mode
!=KEYDB_SEARCH_MODE_LONG_KID
&&
1608 desc
.mode
!=KEYDB_SEARCH_MODE_FPR16
&&
1609 desc
.mode
!=KEYDB_SEARCH_MODE_FPR20
)
1611 log_error(_("\"%s\" not a key ID: skipping\n"),users
->d
);
1615 append_to_strlist(&sl
,users
->d
);
1620 rc
=keyserver_work(KS_SEND
,sl
,NULL
,0,NULL
,NULL
,opt
.keyserver
);
1628 keyserver_import(strlist_t users
)
1630 KEYDB_SEARCH_DESC
*desc
;
1631 int num
=100,count
=0;
1634 /* Build a list of key ids */
1635 desc
=xmalloc(sizeof(KEYDB_SEARCH_DESC
)*num
);
1637 for(;users
;users
=users
->next
)
1639 classify_user_id (users
->d
, &desc
[count
]);
1640 if(desc
[count
].mode
!=KEYDB_SEARCH_MODE_SHORT_KID
&&
1641 desc
[count
].mode
!=KEYDB_SEARCH_MODE_LONG_KID
&&
1642 desc
[count
].mode
!=KEYDB_SEARCH_MODE_FPR16
&&
1643 desc
[count
].mode
!=KEYDB_SEARCH_MODE_FPR20
)
1645 log_error(_("\"%s\" not a key ID: skipping\n"),users
->d
);
1653 desc
=xrealloc(desc
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1658 rc
=keyserver_work(KS_GET
,NULL
,desc
,count
,NULL
,NULL
,opt
.keyserver
);
1666 keyserver_import_fprint(const byte
*fprint
,size_t fprint_len
,
1667 struct keyserver_spec
*keyserver
)
1669 KEYDB_SEARCH_DESC desc
;
1671 memset(&desc
,0,sizeof(desc
));
1674 desc
.mode
=KEYDB_SEARCH_MODE_FPR16
;
1675 else if(fprint_len
==20)
1676 desc
.mode
=KEYDB_SEARCH_MODE_FPR20
;
1680 memcpy(desc
.u
.fpr
,fprint
,fprint_len
);
1682 /* TODO: Warn here if the fingerprint we got doesn't match the one
1684 return keyserver_work(KS_GET
,NULL
,&desc
,1,NULL
,NULL
,keyserver
);
1688 keyserver_import_keyid(u32
*keyid
,struct keyserver_spec
*keyserver
)
1690 KEYDB_SEARCH_DESC desc
;
1692 memset(&desc
,0,sizeof(desc
));
1694 desc
.mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1695 desc
.u
.kid
[0]=keyid
[0];
1696 desc
.u
.kid
[1]=keyid
[1];
1698 return keyserver_work(KS_GET
,NULL
,&desc
,1,NULL
,NULL
,keyserver
);
1701 /* code mostly stolen from do_export_stream */
1703 keyidlist(strlist_t users
,KEYDB_SEARCH_DESC
**klist
,int *count
,int fakev3
)
1705 int rc
=0,ndesc
,num
=100;
1706 KBNODE keyblock
=NULL
,node
;
1708 KEYDB_SEARCH_DESC
*desc
;
1713 *klist
=xmalloc(sizeof(KEYDB_SEARCH_DESC
)*num
);
1720 desc
= xmalloc_clear ( ndesc
* sizeof *desc
);
1721 desc
[0].mode
= KEYDB_SEARCH_MODE_FIRST
;
1725 for (ndesc
=0, sl
=users
; sl
; sl
= sl
->next
, ndesc
++)
1727 desc
= xmalloc ( ndesc
* sizeof *desc
);
1729 for (ndesc
=0, sl
=users
; sl
; sl
= sl
->next
)
1731 if(classify_user_id (sl
->d
, desc
+ndesc
))
1734 log_error (_("key \"%s\" not found: %s\n"),
1735 sl
->d
, g10_errstr (G10ERR_INV_USER_ID
));
1739 while (!(rc
= keydb_search (kdbhd
, desc
, ndesc
)))
1742 desc
[0].mode
= KEYDB_SEARCH_MODE_NEXT
;
1744 /* read the keyblock */
1745 rc
= keydb_get_keyblock (kdbhd
, &keyblock
);
1748 log_error (_("error reading keyblock: %s\n"), g10_errstr(rc
) );
1752 if((node
=find_kbnode(keyblock
,PKT_PUBLIC_KEY
)))
1754 /* This is to work around a bug in some keyservers (pksd and
1755 OKS) that calculate v4 RSA keyids as if they were v3 RSA.
1756 The answer is to refresh both the correct v4 keyid
1757 (e.g. 99242560) and the fake v3 keyid (e.g. 68FDDBC7).
1758 This only happens for key refresh using the HKP scheme
1759 and if the refresh-add-fake-v3-keyids keyserver option is
1761 if(fakev3
&& is_RSA(node
->pkt
->pkt
.public_key
->pubkey_algo
) &&
1762 node
->pkt
->pkt
.public_key
->version
>=4)
1764 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1765 v3_keyid (node
->pkt
->pkt
.public_key
->pkey
[0],
1766 (*klist
)[*count
].u
.kid
);
1772 *klist
=xrealloc(*klist
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1776 /* v4 keys get full fingerprints. v3 keys get long keyids.
1777 This is because it's easy to calculate any sort of keyid
1778 from a v4 fingerprint, but not a v3 fingerprint. */
1780 if(node
->pkt
->pkt
.public_key
->version
<4)
1782 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1783 keyid_from_pk(node
->pkt
->pkt
.public_key
,
1784 (*klist
)[*count
].u
.kid
);
1790 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_FPR20
;
1791 fingerprint_from_pk(node
->pkt
->pkt
.public_key
,
1792 (*klist
)[*count
].u
.fpr
,&dummy
);
1795 /* This is a little hackish, using the skipfncvalue as a
1796 void* pointer to the keyserver spec, but we don't need
1797 the skipfnc here, and it saves having an additional field
1798 for this (which would be wasted space most of the
1801 (*klist
)[*count
].skipfncvalue
=NULL
;
1803 /* Are we honoring preferred keyservers? */
1804 if(opt
.keyserver_options
.options
&KEYSERVER_HONOR_KEYSERVER_URL
)
1806 PKT_user_id
*uid
=NULL
;
1807 PKT_signature
*sig
=NULL
;
1809 merge_keys_and_selfsig(keyblock
);
1811 for(node
=node
->next
;node
;node
=node
->next
)
1813 if(node
->pkt
->pkttype
==PKT_USER_ID
1814 && node
->pkt
->pkt
.user_id
->is_primary
)
1815 uid
=node
->pkt
->pkt
.user_id
;
1816 else if(node
->pkt
->pkttype
==PKT_SIGNATURE
1817 && node
->pkt
->pkt
.signature
->
1818 flags
.chosen_selfsig
&& uid
)
1820 sig
=node
->pkt
->pkt
.signature
;
1825 /* Try and parse the keyserver URL. If it doesn't work,
1826 then we end up writing NULL which indicates we are
1827 the same as any other key. */
1829 (*klist
)[*count
].skipfncvalue
=parse_preferred_keyserver(sig
);
1837 *klist
=xrealloc(*klist
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1849 keydb_release(kdbhd
);
1850 release_kbnode(keyblock
);
1855 /* Note this is different than the original HKP refresh. It allows
1856 usernames to refresh only part of the keyring. */
1859 keyserver_refresh(strlist_t users
)
1861 int rc
,count
,numdesc
,fakev3
=0;
1862 KEYDB_SEARCH_DESC
*desc
;
1863 unsigned int options
=opt
.keyserver_options
.import_options
;
1865 /* We switch merge-only on during a refresh, as 'refresh' should
1866 never import new keys, even if their keyids match. */
1867 opt
.keyserver_options
.import_options
|=IMPORT_MERGE_ONLY
;
1869 /* Similarly, we switch on fast-import, since refresh may make
1870 multiple import sets (due to preferred keyserver URLs). We don't
1871 want each set to rebuild the trustdb. Instead we do it once at
1873 opt
.keyserver_options
.import_options
|=IMPORT_FAST
;
1875 /* If refresh_add_fake_v3_keyids is on and it's a HKP or MAILTO
1876 scheme, then enable fake v3 keyid generation. */
1877 if((opt
.keyserver_options
.options
&KEYSERVER_ADD_FAKE_V3
) && opt
.keyserver
1878 && (ascii_strcasecmp(opt
.keyserver
->scheme
,"hkp")==0 ||
1879 ascii_strcasecmp(opt
.keyserver
->scheme
,"mailto")==0))
1882 rc
=keyidlist(users
,&desc
,&numdesc
,fakev3
);
1891 /* Try to handle preferred keyserver keys first */
1892 for(i
=0;i
<numdesc
;i
++)
1894 if(desc
[i
].skipfncvalue
)
1896 struct keyserver_spec
*keyserver
=desc
[i
].skipfncvalue
;
1898 /* We use the keyserver structure we parsed out before.
1899 Note that a preferred keyserver without a scheme://
1900 will be interpreted as hkp:// */
1902 rc
=keyserver_work(KS_GET
,NULL
,&desc
[i
],1,NULL
,NULL
,keyserver
);
1904 log_info(_("WARNING: unable to refresh key %s"
1905 " via %s: %s\n"),keystr_from_desc(&desc
[i
]),
1906 keyserver
->uri
,g10_errstr(rc
));
1909 /* We got it, so mark it as NONE so we don't try and
1910 get it again from the regular keyserver. */
1912 desc
[i
].mode
=KEYDB_SEARCH_MODE_NONE
;
1916 free_keyserver_spec(keyserver
);
1926 log_info(_("refreshing 1 key from %s\n"),opt
.keyserver
->uri
);
1928 log_info(_("refreshing %d keys from %s\n"),
1929 count
,opt
.keyserver
->uri
);
1932 rc
=keyserver_work(KS_GET
,NULL
,desc
,numdesc
,NULL
,NULL
,opt
.keyserver
);
1937 opt
.keyserver_options
.import_options
=options
;
1939 /* If the original options didn't have fast import, and the trustdb
1940 is dirty, rebuild. */
1941 if(!(opt
.keyserver_options
.import_options
&IMPORT_FAST
))
1942 trustdb_check_or_update();
1948 keyserver_search(strlist_t tokens
)
1951 return keyserver_work(KS_SEARCH
,tokens
,NULL
,0,NULL
,NULL
,opt
.keyserver
);
1957 keyserver_fetch(strlist_t urilist
)
1959 KEYDB_SEARCH_DESC desc
;
1961 unsigned int options
=opt
.keyserver_options
.import_options
;
1963 /* Switch on fast-import, since fetch can handle more than one
1964 import and we don't want each set to rebuild the trustdb.
1965 Instead we do it once at the end. */
1966 opt
.keyserver_options
.import_options
|=IMPORT_FAST
;
1968 /* A dummy desc since we're not actually fetching a particular key
1970 memset(&desc
,0,sizeof(desc
));
1971 desc
.mode
=KEYDB_SEARCH_MODE_EXACT
;
1973 for(sl
=urilist
;sl
;sl
=sl
->next
)
1975 struct keyserver_spec
*spec
;
1977 spec
=parse_keyserver_uri(sl
->d
,1,NULL
,0);
1982 rc
=keyserver_work(KS_GET
,NULL
,&desc
,1,NULL
,NULL
,spec
);
1984 log_info (_("WARNING: unable to fetch URI %s: %s\n"),
1985 sl
->d
,g10_errstr(rc
));
1987 free_keyserver_spec(spec
);
1990 log_info (_("WARNING: unable to parse URI %s\n"),sl
->d
);
1993 opt
.keyserver_options
.import_options
=options
;
1995 /* If the original options didn't have fast import, and the trustdb
1996 is dirty, rebuild. */
1997 if(!(opt
.keyserver_options
.import_options
&IMPORT_FAST
))
1998 trustdb_check_or_update();
2003 /* Import key in a CERT or pointed to by a CERT */
2005 keyserver_import_cert(const char *name
,unsigned char **fpr
,size_t *fpr_len
)
2007 char *domain
,*look
,*url
;
2009 int type
,rc
=G10ERR_GENERAL
;
2013 domain
=strrchr(look
,'@');
2017 type
=get_dns_cert(look
,max_cert_size
,&key
,fpr
,fpr_len
,&url
);
2018 if (!type
|| type
== -1)
2020 /* There might be an error in res_query which leads to an error
2021 return (-1) in the case that nothing was found. Thus we take
2022 all errors as key not found. */
2023 rc
= G10ERR_NO_PUBKEY
;
2027 int armor_status
=opt
.no_armor
;
2029 /* CERTs are always in binary format */
2032 rc
=import_keys_stream(key
,NULL
,fpr
,fpr_len
,
2033 opt
.keyserver_options
.import_options
);
2035 opt
.no_armor
=armor_status
;
2039 else if(type
==2 && *fpr
)
2041 /* We only consider the IPGP type if a fingerprint was provided.
2042 This lets us select the right key regardless of what a URL
2043 points to, or get the key from a keyserver. */
2046 struct keyserver_spec
*spec
;
2048 spec
=parse_keyserver_uri(url
,1,NULL
,0);
2051 rc
=keyserver_import_fprint(*fpr
,*fpr_len
,spec
);
2052 free_keyserver_spec(spec
);
2055 else if(opt
.keyserver
)
2057 /* If only a fingerprint is provided, try and fetch it from
2060 rc
=keyserver_import_fprint(*fpr
,*fpr_len
,opt
.keyserver
);
2063 log_info(_("no keyserver known (use option --keyserver)\n"));
2065 /* Give a better string here? "CERT fingerprint for \"%s\"
2066 found, but no keyserver" " known (use option
2067 --keyserver)\n" ? */
2077 /* Import key pointed to by a PKA record. Return the requested
2078 fingerprint in fpr. */
2080 keyserver_import_pka(const char *name
,unsigned char **fpr
,size_t *fpr_len
)
2083 int rc
= G10ERR_NO_PUBKEY
;
2085 *fpr
= xmalloc (20);
2088 uri
= get_pka_info (name
, *fpr
);
2091 /* An URI is available. Lookup the key. */
2092 struct keyserver_spec
*spec
;
2093 spec
= parse_keyserver_uri (uri
, 1, NULL
, 0);
2096 rc
= keyserver_import_fprint (*fpr
, 20, spec
);
2097 free_keyserver_spec (spec
);
2111 /* Import all keys that match name */
2113 keyserver_import_name(const char *name
,unsigned char **fpr
,size_t *fpr_len
,
2114 struct keyserver_spec
*keyserver
)
2116 strlist_t list
=NULL
;
2119 append_to_strlist(&list
,name
);
2121 rc
=keyserver_work(KS_GETNAME
,list
,NULL
,0,fpr
,fpr_len
,keyserver
);
2128 /* Use the PGP Universal trick of asking ldap://keys.(maildomain) for
2131 keyserver_import_ldap(const char *name
,unsigned char **fpr
,size_t *fpr_len
)
2134 struct keyserver_spec
*keyserver
;
2135 strlist_t list
=NULL
;
2138 append_to_strlist(&list
,name
);
2140 /* Parse out the domain */
2141 domain
=strrchr(name
,'@');
2143 return G10ERR_GENERAL
;
2147 keyserver
=xmalloc_clear(sizeof(struct keyserver_spec
));
2149 keyserver
->scheme
=xstrdup("ldap");
2150 keyserver
->host
=xmalloc(5+strlen(domain
)+1);
2151 strcpy(keyserver
->host
,"keys.");
2152 strcat(keyserver
->host
,domain
);
2153 keyserver
->uri
=xmalloc(strlen(keyserver
->scheme
)+
2154 3+strlen(keyserver
->host
)+1);
2155 strcpy(keyserver
->uri
,keyserver
->scheme
);
2156 strcat(keyserver
->uri
,"://");
2157 strcat(keyserver
->uri
,keyserver
->host
);
2159 rc
=keyserver_work(KS_GETNAME
,list
,NULL
,0,fpr
,fpr_len
,keyserver
);
2163 free_keyserver_spec(keyserver
);