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,
43 #include "keyserver-internal.h"
51 KEYDB_SEARCH_DESC desc
;
52 u32 createtime
,expiretime
;
59 enum ks_action
{KS_UNKNOWN
=0,KS_GET
,KS_GETNAME
,KS_SEND
,KS_SEARCH
};
61 static struct parse_options keyserver_opts
[]=
63 /* some of these options are not real - just for the help
65 {"max-cert-size",0,NULL
,NULL
},
66 {"include-revoked",0,NULL
,N_("include revoked keys in search results")},
67 {"include-subkeys",0,NULL
,N_("include subkeys when searching by key ID")},
68 {"use-temp-files",0,NULL
,
69 N_("use temporary files to pass data to keyserver helpers")},
70 {"keep-temp-files",KEYSERVER_KEEP_TEMP_FILES
,NULL
,
71 N_("do not delete temporary files after using them")},
72 {"refresh-add-fake-v3-keyids",KEYSERVER_ADD_FAKE_V3
,NULL
,
74 {"auto-key-retrieve",KEYSERVER_AUTO_KEY_RETRIEVE
,NULL
,
75 N_("automatically retrieve keys when verifying signatures")},
76 {"honor-keyserver-url",KEYSERVER_HONOR_KEYSERVER_URL
,NULL
,
77 N_("honor the preferred keyserver URL set on the key")},
78 {"honor-pka-record",KEYSERVER_HONOR_PKA_RECORD
,NULL
,
79 N_("honor the PKA record set on a key when retrieving keys")},
83 static int keyserver_work(enum ks_action action
,STRLIST list
,
84 KEYDB_SEARCH_DESC
*desc
,int count
,
85 unsigned char **fpr
,size_t *fpr_len
,
86 struct keyserver_spec
*keyserver
);
88 /* Reasonable guess */
89 #define DEFAULT_MAX_CERT_SIZE 16384
91 static size_t max_cert_size
=DEFAULT_MAX_CERT_SIZE
;
94 add_canonical_option(char *option
,STRLIST
*list
)
96 char *arg
=argsplit(option
);
102 joined
=xmalloc(strlen(option
)+1+strlen(arg
)+1);
103 /* Make a canonical name=value form with no spaces */
104 strcpy(joined
,option
);
107 append_to_strlist(list
,joined
);
111 append_to_strlist(list
,option
);
115 parse_keyserver_options(char *options
)
121 keyserver_opts
[0].value
=&max_cert
;
123 while((tok
=optsep(&options
)))
128 /* For backwards compatibility. 1.2.x used honor-http-proxy and
129 there are a good number of documents published that recommend
131 if(ascii_strcasecmp(tok
,"honor-http-proxy")==0)
133 else if(ascii_strcasecmp(tok
,"no-honor-http-proxy")==0)
136 /* We accept quite a few possible options here - some options to
137 handle specially, the keyserver_options list, and import and
138 export options that pertain to keyserver operations. Note
139 that you must use strncasecmp here as there might be an
140 =argument attached which will foil the use of strcasecmp. */
142 #ifdef EXEC_TEMPFILE_ONLY
143 if(ascii_strncasecmp(tok
,"use-temp-files",14)==0 ||
144 ascii_strncasecmp(tok
,"no-use-temp-files",17)==0)
145 log_info(_("WARNING: keyserver option `%s' is not used"
146 " on this platform\n"),tok
);
148 if(ascii_strncasecmp(tok
,"use-temp-files",14)==0)
149 opt
.keyserver_options
.options
|=KEYSERVER_USE_TEMP_FILES
;
150 else if(ascii_strncasecmp(tok
,"no-use-temp-files",17)==0)
151 opt
.keyserver_options
.options
&=~KEYSERVER_USE_TEMP_FILES
;
153 else if(!parse_options(tok
,&opt
.keyserver_options
.options
,
155 && !parse_import_options(tok
,
156 &opt
.keyserver_options
.import_options
,0)
157 && !parse_export_options(tok
,
158 &opt
.keyserver_options
.export_options
,0))
160 /* All of the standard options have failed, so the option is
161 destined for a keyserver plugin. */
162 add_canonical_option(tok
,&opt
.keyserver_options
.other
);
168 max_cert_size
=strtoul(max_cert
,(char **)NULL
,10);
171 max_cert_size
=DEFAULT_MAX_CERT_SIZE
;
178 free_keyserver_spec(struct keyserver_spec
*keyserver
)
180 xfree(keyserver
->uri
);
181 xfree(keyserver
->scheme
);
182 xfree(keyserver
->auth
);
183 xfree(keyserver
->host
);
184 xfree(keyserver
->port
);
185 xfree(keyserver
->path
);
186 xfree(keyserver
->opaque
);
187 free_strlist(keyserver
->options
);
191 /* Return 0 for match */
193 cmp_keyserver_spec(struct keyserver_spec
*one
,struct keyserver_spec
*two
)
195 if(ascii_strcasecmp(one
->scheme
,two
->scheme
)==0)
197 if(one
->host
&& two
->host
&& ascii_strcasecmp(one
->host
,two
->host
)==0)
199 if((one
->port
&& two
->port
200 && ascii_strcasecmp(one
->port
,two
->port
)==0)
201 || (!one
->port
&& !two
->port
))
204 else if(one
->opaque
&& two
->opaque
205 && ascii_strcasecmp(one
->opaque
,two
->opaque
)==0)
212 /* Try and match one of our keyservers. If we can, return that. If
213 we can't, return our input. */
214 struct keyserver_spec
*
215 keyserver_match(struct keyserver_spec
*spec
)
217 struct keyserver_spec
*ks
;
219 for(ks
=opt
.keyserver
;ks
;ks
=ks
->next
)
220 if(cmp_keyserver_spec(spec
,ks
)==0)
226 /* TODO: once we cut over to an all-curl world, we don't need this
227 parser any longer so it can be removed, or at least moved to
228 keyserver/ksutil.c for limited use in gpgkeys_ldap or the like. */
230 struct keyserver_spec
*
231 parse_keyserver_uri(const char *string
,int require_scheme
,
232 const char *configname
,unsigned int configlineno
)
235 struct keyserver_spec
*keyserver
;
240 assert(string
!=NULL
);
242 keyserver
=xmalloc_clear(sizeof(struct keyserver_spec
));
246 options
=strchr(uri
,' ');
254 while((tok
=optsep(&options
)))
255 add_canonical_option(tok
,&keyserver
->options
);
260 for(idx
=uri
,count
=0;*idx
&& *idx
!=':';idx
++)
264 /* Do we see the start of an RFC-2732 ipv6 address here? If so,
265 there clearly isn't a scheme so get out early. */
268 /* Was the '[' the first thing in the string? If not, we
269 have a mangled scheme with a [ in it so fail. */
280 if(*idx
=='\0' || *idx
=='[')
285 /* Assume HKP if there is no scheme */
287 keyserver
->scheme
=xstrdup("hkp");
289 keyserver
->uri
=xmalloc(strlen(keyserver
->scheme
)+3+strlen(uri
)+1);
290 strcpy(keyserver
->uri
,keyserver
->scheme
);
291 strcat(keyserver
->uri
,"://");
292 strcat(keyserver
->uri
,uri
);
298 keyserver
->uri
=xstrdup(uri
);
300 keyserver
->scheme
=xmalloc(count
+1);
302 /* Force to lowercase */
304 keyserver
->scheme
[i
]=ascii_tolower(uri
[i
]);
306 keyserver
->scheme
[i
]='\0';
308 /* Skip past the scheme and colon */
312 if(ascii_strcasecmp(keyserver
->scheme
,"x-broken-hkp")==0)
314 deprecated_warning(configname
,configlineno
,"x-broken-hkp",
315 "--keyserver-options ","broken-http-proxy");
316 xfree(keyserver
->scheme
);
317 keyserver
->scheme
=xstrdup("hkp");
318 append_to_strlist(&opt
.keyserver_options
.other
,"broken-http-proxy");
320 else if(ascii_strcasecmp(keyserver
->scheme
,"x-hkp")==0)
322 /* Canonicalize this to "hkp" so it works with both the internal
323 and external keyserver interface. */
324 xfree(keyserver
->scheme
);
325 keyserver
->scheme
=xstrdup("hkp");
328 if(assume_hkp
|| (uri
[0]=='/' && uri
[1]=='/'))
330 /* Two slashes means network path. */
332 /* Skip over the "//", if any */
336 /* Do we have userinfo auth data present? */
337 for(idx
=uri
,count
=0;*idx
&& *idx
!='@' && *idx
!='/';idx
++)
340 /* We found a @ before the slash, so that means everything
341 before the @ is auth data. */
347 keyserver
->auth
=xmalloc(count
+1);
348 strncpy(keyserver
->auth
,uri
,count
);
349 keyserver
->auth
[count
]='\0';
353 /* Is it an RFC-2732 ipv6 [literal address] ? */
356 for(idx
=uri
+1,count
=1;*idx
357 && ((isascii (*idx
) && isxdigit(*idx
))
358 || *idx
==':' || *idx
=='.');idx
++)
361 /* Is the ipv6 literal address terminated? */
368 for(idx
=uri
,count
=0;*idx
&& *idx
!=':' && *idx
!='/';idx
++)
374 keyserver
->host
=xmalloc(count
+1);
375 strncpy(keyserver
->host
,uri
,count
);
376 keyserver
->host
[count
]='\0';
378 /* Skip past the host */
383 /* It would seem to be reasonable to limit the range of the
384 ports to values between 1-65535, but RFC 1738 and 1808
385 imply there is no limit. Of course, the real world has
388 for(idx
=uri
+1,count
=0;*idx
&& *idx
!='/';idx
++)
392 /* Ports are digits only */
397 keyserver
->port
=xmalloc(count
+1);
398 strncpy(keyserver
->port
,uri
+1,count
);
399 keyserver
->port
[count
]='\0';
401 /* Skip past the colon and port number */
405 /* Everything else is the path */
407 keyserver
->path
=xstrdup(uri
);
409 keyserver
->path
=xstrdup("/");
411 if(keyserver
->path
[1])
412 keyserver
->flags
.direct_uri
=1;
416 /* No slash means opaque. Just record the opaque blob and get
418 keyserver
->opaque
=xstrdup(uri
);
422 /* One slash means absolute path. We don't need to support that
430 free_keyserver_spec(keyserver
);
435 struct keyserver_spec
*
436 parse_preferred_keyserver(PKT_signature
*sig
)
438 struct keyserver_spec
*spec
=NULL
;
442 p
=parse_sig_subpkt(sig
->hashed
,SIGSUBPKT_PREF_KS
,&plen
);
445 byte
*dupe
=xmalloc(plen
+1);
449 spec
=parse_keyserver_uri(dupe
,1,NULL
,0);
457 print_keyrec(int number
,struct keyrec
*keyrec
)
461 iobuf_writebyte(keyrec
->uidbuf
,0);
462 iobuf_flush_temp(keyrec
->uidbuf
);
463 printf("(%d)\t%s ",number
,iobuf_get_temp_buffer(keyrec
->uidbuf
));
466 printf("%d bit ",keyrec
->size
);
470 const char *str
= gcry_pk_algo_name (keyrec
->type
);
478 switch(keyrec
->desc
.mode
)
480 /* If the keyserver helper gave us a short keyid, we have no
481 choice but to use it. Do check --keyid-format to add a 0x if
483 case KEYDB_SEARCH_MODE_SHORT_KID
:
484 printf("key %s%08lX",
485 (opt
.keyid_format
==KF_0xSHORT
486 || opt
.keyid_format
==KF_0xLONG
)?"0x":"",
487 (ulong
)keyrec
->desc
.u
.kid
[1]);
490 /* However, if it gave us a long keyid, we can honor
492 case KEYDB_SEARCH_MODE_LONG_KID
:
493 printf("key %s",keystr(keyrec
->desc
.u
.kid
));
496 case KEYDB_SEARCH_MODE_FPR16
:
499 printf("%02X",keyrec
->desc
.u
.fpr
[i
]);
502 case KEYDB_SEARCH_MODE_FPR20
:
505 printf("%02X",keyrec
->desc
.u
.fpr
[i
]);
513 if(keyrec
->createtime
>0)
516 printf(_("created: %s"),strtimestamp(keyrec
->createtime
));
519 if(keyrec
->expiretime
>0)
522 printf(_("expires: %s"),strtimestamp(keyrec
->expiretime
));
526 printf(" (%s)",_("revoked"));
528 printf(" (%s)",_("disabled"));
530 printf(" (%s)",_("expired"));
535 /* Returns a keyrec (which must be freed) once a key is complete, and
536 NULL otherwise. Call with a NULL keystring once key parsing is
537 complete to return any unfinished keys. */
538 static struct keyrec
*
539 parse_keyrec(char *keystring
)
541 static struct keyrec
*work
=NULL
;
542 struct keyrec
*ret
=NULL
;
550 else if(work
->desc
.mode
==KEYDB_SEARCH_MODE_NONE
)
565 work
=xmalloc_clear(sizeof(struct keyrec
));
566 work
->uidbuf
=iobuf_temp();
569 /* Remove trailing whitespace */
570 for(i
=strlen(keystring
);i
>0;i
--)
571 if(ascii_isspace(keystring
[i
-1]))
576 if((record
=strsep(&keystring
,":"))==NULL
)
579 if(ascii_strcasecmp("pub",record
)==0)
586 work
=xmalloc_clear(sizeof(struct keyrec
));
587 work
->uidbuf
=iobuf_temp();
590 if((tok
=strsep(&keystring
,":"))==NULL
)
593 classify_user_id(tok
,&work
->desc
);
594 if(work
->desc
.mode
!=KEYDB_SEARCH_MODE_SHORT_KID
595 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_LONG_KID
596 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_FPR16
597 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_FPR20
)
599 work
->desc
.mode
=KEYDB_SEARCH_MODE_NONE
;
603 /* Note all items after this are optional. This allows us to
604 have a pub line as simple as pub:keyid and nothing else. */
608 if((tok
=strsep(&keystring
,":"))==NULL
)
611 work
->type
=atoi(tok
);
613 if((tok
=strsep(&keystring
,":"))==NULL
)
616 work
->size
=atoi(tok
);
618 if((tok
=strsep(&keystring
,":"))==NULL
)
624 work
->createtime
=atoi(tok
);
626 if((tok
=strsep(&keystring
,":"))==NULL
)
633 work
->expiretime
=atoi(tok
);
634 /* Force the 'e' flag on if this key is expired. */
635 if(work
->expiretime
<=make_timestamp())
639 if((tok
=strsep(&keystring
,":"))==NULL
)
661 else if(ascii_strcasecmp("uid",record
)==0 && work
->desc
.mode
)
663 char *userid
,*tok
,*decoded
;
665 if((tok
=strsep(&keystring
,":"))==NULL
)
673 /* By definition, de-%-encoding is always smaller than the
674 original string so we can decode in place. */
679 if(tok
[0]=='%' && tok
[1] && tok
[2])
681 if((userid
[i
]=hextobyte(&tok
[1]))==-1)
690 /* We don't care about the other info provided in the uid: line
691 since no keyserver supports marking userids with timestamps
692 or revoked/expired/disabled yet. */
694 /* No need to check for control characters, as utf8_to_native
697 decoded
=utf8_to_native(userid
,i
,0);
698 if(strlen(decoded
)>opt
.screen_columns
-10)
699 decoded
[opt
.screen_columns
-10]='\0';
700 iobuf_writestr(work
->uidbuf
,decoded
);
702 iobuf_writestr(work
->uidbuf
,"\n\t");
706 /* Ignore any records other than "pri" and "uid" for easy future
712 /* TODO: do this as a list sent to keyserver_work rather than calling
713 it once for each key to get the correct counts after the import
714 (cosmetics, really) and to better take advantage of the keyservers
715 that can do multiple fetches in one go (LDAP). */
717 show_prompt(KEYDB_SEARCH_DESC
*desc
,int numdesc
,int count
,const char *search
)
721 if(count
&& opt
.command_fd
==-1)
724 tty_printf("Keys %d-%d of %d for \"%s\". ",from
,numdesc
,count
,search
);
728 answer
=cpr_get_no_help("keysearch.prompt",
729 _("Enter number(s), N)ext, or Q)uit > "));
731 if(answer
[0]=='\x04')
737 if(answer
[0]=='q' || answer
[0]=='Q')
742 else if(atoi(answer
)>=1 && atoi(answer
)<=numdesc
)
744 char *split
=answer
,*num
;
746 while((num
=strsep(&split
," ,"))!=NULL
)
747 if(atoi(num
)>=1 && atoi(num
)<=numdesc
)
748 keyserver_work(KS_GET
,NULL
,&desc
[atoi(num
)-1],1,
749 NULL
,NULL
,opt
.keyserver
);
758 /* Count and searchstr are just for cosmetics. If the count is too
759 small, it will grow safely. If negative it disables the "Key x-y
760 of z" messages. searchstr should be UTF-8 (rather than native). */
762 keyserver_search_prompt(IOBUF buffer
,const char *searchstr
)
764 int i
=0,validcount
=0,started
=0,header
=0,count
=1;
765 unsigned int maxlen
,buflen
,numlines
=0;
766 KEYDB_SEARCH_DESC
*desc
;
771 localstr
=utf8_to_native(searchstr
,strlen(searchstr
),0);
773 desc
=xmalloc(count
*sizeof(KEYDB_SEARCH_DESC
));
777 struct keyrec
*keyrec
;
781 rl
=iobuf_read_line(buffer
,&line
,&buflen
,&maxlen
);
785 if(!header
&& ascii_strncasecmp("SEARCH ",line
,7)==0
786 && ascii_strncasecmp(" BEGIN",&line
[strlen(line
)-7],6)==0)
791 else if(ascii_strncasecmp("SEARCH ",line
,7)==0
792 && ascii_strncasecmp(" END",&line
[strlen(line
)-5],4)==0)
798 /* Look for an info: line. The only current info: values
799 defined are the version and key count. */
800 if(!started
&& rl
>0 && ascii_strncasecmp("info:",line
,5)==0)
802 char *tok
,*str
=&line
[5];
804 if((tok
=strsep(&str
,":"))!=NULL
)
808 if(sscanf(tok
,"%d",&version
)!=1)
813 log_error(_("invalid keyserver protocol "
814 "(us %d!=handler %d)\n"),1,version
);
819 if((tok
=strsep(&str
,":"))!=NULL
&& sscanf(tok
,"%d",&count
)==1)
828 desc
=xrealloc(desc
,count
*sizeof(KEYDB_SEARCH_DESC
));
837 keyrec
=parse_keyrec(NULL
);
852 if(show_prompt(desc
,i
,validcount
?count
:0,localstr
))
861 keyrec
=parse_keyrec(line
);
865 /* keyserver helper sent more keys than they claimed in the
868 desc
=xrealloc(desc
,count
*sizeof(KEYDB_SEARCH_DESC
));
874 desc
[i
]=keyrec
->desc
;
878 /* screen_lines - 1 for the prompt. */
879 if(numlines
+keyrec
->lines
>opt
.screen_lines
-1)
881 if(show_prompt(desc
,i
,validcount
?count
:0,localstr
))
887 print_keyrec(i
+1,keyrec
);
890 numlines
+=keyrec
->lines
;
891 iobuf_close(keyrec
->uidbuf
);
900 /* Leave this commented out or now, and perhaps for a very long
901 time. All HKPish servers return HTML error messages for
905 log_info(_("keyserver does not support searching\n"));
911 log_info(_("key \"%s\" not found on keyserver\n"),localstr
);
913 log_info(_("key not found on keyserver\n"));
921 /* We sometimes want to use a different gpgkeys_xxx for a given
922 protocol (for example, ldaps is handled by gpgkeys_ldap). Map
925 keyserver_typemap(const char *type
)
927 if(strcmp(type
,"ldaps")==0)
933 /* The PGP LDAP and the curl fetch-a-LDAP-object methodologies are
934 sufficiently different that we can't use curl to do LDAP. */
936 direct_uri_map(const char *scheme
,unsigned int is_direct
)
938 if(is_direct
&& strcmp(scheme
,"ldap")==0)
944 #define GPGKEYS_PREFIX "gpgkeys_"
945 #define GPGKEYS_CURL GPGKEYS_PREFIX "curl" EXEEXT
946 #define GPGKEYS_PREFIX_LEN (strlen(GPGKEYS_CURL))
947 #define KEYSERVER_ARGS_KEEP " -o \"%O\" \"%I\""
948 #define KEYSERVER_ARGS_NOKEEP " -o \"%o\" \"%i\""
951 keyserver_spawn(enum ks_action action
,STRLIST list
,KEYDB_SEARCH_DESC
*desc
,
952 int count
,int *prog
,unsigned char **fpr
,size_t *fpr_len
,
953 struct keyserver_spec
*keyserver
)
955 int ret
=0,i
,gotversion
=0,outofband
=0;
957 unsigned int maxlen
,buflen
;
958 char *command
,*end
,*searchstr
=NULL
;
960 struct exec_info
*spawn
;
962 const char *libexecdir
= get_libexecdir ();
966 #ifdef EXEC_TEMPFILE_ONLY
967 opt
.keyserver_options
.options
|=KEYSERVER_USE_TEMP_FILES
;
970 /* Build the filename for the helper to execute */
971 scheme
=keyserver_typemap(keyserver
->scheme
);
973 #ifdef DISABLE_KEYSERVER_PATH
974 /* Destroy any path we might have. This is a little tricky,
975 portability-wise. It's not correct to delete the PATH
976 environment variable, as that may fall back to a system built-in
977 PATH. Similarly, it is not correct to set PATH to the null
978 string (PATH="") since this actually deletes the PATH environment
979 variable under MinGW. The safest thing to do here is to force
980 PATH to be GNUPG_LIBEXECDIR. All this is not that meaningful on
981 Unix-like systems (since we're going to give a full path to
982 gpgkeys_foo), but on W32 it prevents loading any DLLs from
983 directories in %PATH%.
985 After some more thinking about this we came to the conclusion
986 that it is better to load the helpers from the directory where
987 the program of this process lives. Fortunately Windows provides
988 a way to retrieve this and our get_libexecdir function has been
989 modified to return just this. Setting the exec-path is not
991 set_exec_path(libexecdir);
994 if(opt
.exec_path_set
)
996 /* If exec-path was set, and DISABLE_KEYSERVER_PATH is
997 undefined, then don't specify a full path to gpgkeys_foo, so
998 that the PATH can work. */
999 command
=xmalloc(GPGKEYS_PREFIX_LEN
+strlen(scheme
)+3+strlen(EXEEXT
)+1);
1005 /* Specify a full path to gpgkeys_foo. */
1006 command
=xmalloc(strlen(libexecdir
)+strlen(DIRSEP_S
)+
1007 GPGKEYS_PREFIX_LEN
+strlen(scheme
)+3+strlen(EXEEXT
)+1);
1008 strcpy(command
,libexecdir
);
1009 strcat(command
,DIRSEP_S
);
1012 end
=command
+strlen(command
);
1014 /* Build a path for the keyserver helper. If it is direct_uri
1015 (i.e. an object fetch and not a keyserver), then add "_uri" to
1016 the end to distinguish the keyserver helper from an object
1017 fetcher that can speak that protocol (this is a problem for
1020 strcat(command
,GPGKEYS_PREFIX
);
1021 strcat(command
,scheme
);
1023 /* This "_uri" thing is in case we need to call a direct handler
1024 instead of the keyserver handler. This lets us use gpgkeys_curl
1025 or gpgkeys_ldap_uri (we don't provide it, but a user might)
1026 instead of gpgkeys_ldap to fetch things like
1027 ldap://keyserver.pgp.com/o=PGP%20keys?pgpkey?sub?pgpkeyid=99242560 */
1029 if(direct_uri_map(scheme
,keyserver
->flags
.direct_uri
))
1030 strcat(command
,"_uri");
1032 strcat(command
,EXEEXT
);
1034 /* Can we execute it? If not, try curl as our catchall. */
1035 if(path_access(command
,X_OK
)!=0)
1036 strcpy(end
,GPGKEYS_CURL
);
1038 if(opt
.keyserver_options
.options
&KEYSERVER_USE_TEMP_FILES
)
1040 if(opt
.keyserver_options
.options
&KEYSERVER_KEEP_TEMP_FILES
)
1042 command
=xrealloc(command
,strlen(command
)+
1043 strlen(KEYSERVER_ARGS_KEEP
)+1);
1044 strcat(command
,KEYSERVER_ARGS_KEEP
);
1048 command
=xrealloc(command
,strlen(command
)+
1049 strlen(KEYSERVER_ARGS_NOKEEP
)+1);
1050 strcat(command
,KEYSERVER_ARGS_NOKEEP
);
1053 ret
=exec_write(&spawn
,NULL
,command
,NULL
,0,0);
1056 ret
=exec_write(&spawn
,command
,NULL
,NULL
,0,0);
1063 fprintf(spawn
->tochild
,
1064 "# This is a GnuPG %s keyserver communications file\n",VERSION
);
1065 fprintf(spawn
->tochild
,"VERSION %d\n",KEYSERVER_PROTO_VERSION
);
1066 fprintf(spawn
->tochild
,"PROGRAM %s\n",VERSION
);
1067 fprintf(spawn
->tochild
,"SCHEME %s\n",keyserver
->scheme
);
1069 if(keyserver
->opaque
)
1070 fprintf(spawn
->tochild
,"OPAQUE %s\n",keyserver
->opaque
);
1074 fprintf(spawn
->tochild
,"AUTH %s\n",keyserver
->auth
);
1077 fprintf(spawn
->tochild
,"HOST %s\n",keyserver
->host
);
1080 fprintf(spawn
->tochild
,"PORT %s\n",keyserver
->port
);
1083 fprintf(spawn
->tochild
,"PATH %s\n",keyserver
->path
);
1086 /* Write global options */
1088 for(temp
=opt
.keyserver_options
.other
;temp
;temp
=temp
->next
)
1089 fprintf(spawn
->tochild
,"OPTION %s\n",temp
->d
);
1091 /* Write per-keyserver options */
1093 for(temp
=keyserver
->options
;temp
;temp
=temp
->next
)
1094 fprintf(spawn
->tochild
,"OPTION %s\n",temp
->d
);
1100 fprintf(spawn
->tochild
,"COMMAND GET\n\n");
1102 /* Which keys do we want? */
1104 for(i
=0;i
<count
;i
++)
1108 if(desc
[i
].mode
==KEYDB_SEARCH_MODE_FPR20
)
1112 fprintf(spawn
->tochild
,"0x");
1114 for(f
=0;f
<MAX_FINGERPRINT_LEN
;f
++)
1115 fprintf(spawn
->tochild
,"%02X",desc
[i
].u
.fpr
[f
]);
1117 fprintf(spawn
->tochild
,"\n");
1119 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_FPR16
)
1123 fprintf(spawn
->tochild
,"0x");
1126 fprintf(spawn
->tochild
,"%02X",desc
[i
].u
.fpr
[f
]);
1128 fprintf(spawn
->tochild
,"\n");
1130 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_LONG_KID
)
1131 fprintf(spawn
->tochild
,"0x%08lX%08lX\n",
1132 (ulong
)desc
[i
].u
.kid
[0],
1133 (ulong
)desc
[i
].u
.kid
[1]);
1134 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_SHORT_KID
)
1135 fprintf(spawn
->tochild
,"0x%08lX\n",
1136 (ulong
)desc
[i
].u
.kid
[1]);
1137 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_EXACT
)
1139 fprintf(spawn
->tochild
,"0x0000000000000000\n");
1142 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_NONE
)
1150 log_info(_("requesting key %s from %s server %s\n"),
1151 keystr_from_desc(&desc
[i
]),
1152 keyserver
->scheme
,keyserver
->host
);
1154 log_info(_("requesting key %s from %s\n"),
1155 keystr_from_desc(&desc
[i
]),keyserver
->uri
);
1159 fprintf(spawn
->tochild
,"\n");
1168 fprintf(spawn
->tochild
,"COMMAND GETNAME\n\n");
1170 /* Which names do we want? */
1172 for(key
=list
;key
!=NULL
;key
=key
->next
)
1173 fprintf(spawn
->tochild
,"%s\n",key
->d
);
1175 fprintf(spawn
->tochild
,"\n");
1178 log_info(_("searching for names from %s server %s\n"),
1179 keyserver
->scheme
,keyserver
->host
);
1181 log_info(_("searching for names from %s\n"),keyserver
->uri
);
1190 /* Note the extra \n here to send an empty keylist block */
1191 fprintf(spawn
->tochild
,"COMMAND SEND\n\n\n");
1193 for(key
=list
;key
!=NULL
;key
=key
->next
)
1195 armor_filter_context_t afx
;
1196 IOBUF buffer
=iobuf_temp();
1200 add_to_strlist(&temp
,key
->d
);
1202 memset(&afx
,0,sizeof(afx
));
1204 /* Tell the armor filter to use Unix-style \n line
1205 endings, since we're going to fprintf this to a file
1206 that (on Win32) is open in text mode. The win32 stdio
1207 will transform the \n to \r\n and we'll end up with the
1208 proper line endings on win32. This is a no-op on
1211 iobuf_push_filter(buffer
,armor_filter
,&afx
);
1213 /* TODO: Remove Comment: lines from keys exported this
1216 if(export_pubkeys_stream(buffer
,temp
,&block
,
1217 opt
.keyserver_options
.export_options
)==-1)
1218 iobuf_close(buffer
);
1223 iobuf_flush_temp(buffer
);
1225 merge_keys_and_selfsig(block
);
1227 fprintf(spawn
->tochild
,"INFO %08lX%08lX BEGIN\n",
1228 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[0],
1229 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[1]);
1231 for(node
=block
;node
;node
=node
->next
)
1233 switch(node
->pkt
->pkttype
)
1238 case PKT_PUBLIC_KEY
:
1239 case PKT_PUBLIC_SUBKEY
:
1241 PKT_public_key
*pk
=node
->pkt
->pkt
.public_key
;
1243 keyid_from_pk(pk
,NULL
);
1245 fprintf(spawn
->tochild
,"%sb:%08lX%08lX:%u:%u:%u:%u:",
1246 node
->pkt
->pkttype
==PKT_PUBLIC_KEY
?"pu":"su",
1247 (ulong
)pk
->keyid
[0],(ulong
)pk
->keyid
[1],
1254 fprintf(spawn
->tochild
,"r");
1256 fprintf(spawn
->tochild
,"e");
1258 fprintf(spawn
->tochild
,"\n");
1264 PKT_user_id
*uid
=node
->pkt
->pkt
.user_id
;
1267 if(uid
->attrib_data
)
1270 fprintf(spawn
->tochild
,"uid:");
1272 /* Quote ':', '%', and any 8-bit
1274 for(r
=0;r
<uid
->len
;r
++)
1276 if(uid
->name
[r
]==':' || uid
->name
[r
]=='%'
1277 || uid
->name
[r
]&0x80)
1278 fprintf(spawn
->tochild
,"%%%02X",
1279 (byte
)uid
->name
[r
]);
1281 fprintf(spawn
->tochild
,"%c",uid
->name
[r
]);
1284 fprintf(spawn
->tochild
,":%u:%u:",
1285 uid
->created
,uid
->expiredate
);
1288 fprintf(spawn
->tochild
,"r");
1290 fprintf(spawn
->tochild
,"e");
1292 fprintf(spawn
->tochild
,"\n");
1296 /* This bit is really for the benefit of
1297 people who store their keys in LDAP
1298 servers. It makes it easy to do queries
1299 for things like "all keys signed by
1303 PKT_signature
*sig
=node
->pkt
->pkt
.signature
;
1305 if(!IS_UID_SIG(sig
))
1308 fprintf(spawn
->tochild
,"sig:%08lX%08lX:%X:%u:%u\n",
1309 (ulong
)sig
->keyid
[0],(ulong
)sig
->keyid
[1],
1310 sig
->sig_class
,sig
->timestamp
,
1317 fprintf(spawn
->tochild
,"INFO %08lX%08lX END\n",
1318 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[0],
1319 (ulong
)block
->pkt
->pkt
.public_key
->keyid
[1]);
1321 fprintf(spawn
->tochild
,"KEY %s BEGIN\n",key
->d
);
1322 fwrite(iobuf_get_temp_buffer(buffer
),
1323 iobuf_get_temp_length(buffer
),1,spawn
->tochild
);
1324 fprintf(spawn
->tochild
,"KEY %s END\n",key
->d
);
1326 iobuf_close(buffer
);
1329 log_info(_("sending key %s to %s server %s\n"),
1330 keystr(block
->pkt
->pkt
.public_key
->keyid
),
1331 keyserver
->scheme
,keyserver
->host
);
1333 log_info(_("sending key %s to %s\n"),
1334 keystr(block
->pkt
->pkt
.public_key
->keyid
),
1337 release_kbnode(block
);
1350 fprintf(spawn
->tochild
,"COMMAND SEARCH\n\n");
1352 /* Which keys do we want? Remember that the gpgkeys_ program
1353 is going to lump these together into a search string. */
1355 for(key
=list
;key
!=NULL
;key
=key
->next
)
1357 fprintf(spawn
->tochild
,"%s\n",key
->d
);
1360 searchstr
=xrealloc(searchstr
,
1361 strlen(searchstr
)+strlen(key
->d
)+2);
1362 strcat(searchstr
," ");
1366 searchstr
=xmalloc(strlen(key
->d
)+1);
1370 strcat(searchstr
,key
->d
);
1373 fprintf(spawn
->tochild
,"\n");
1376 log_info(_("searching for \"%s\" from %s server %s\n"),
1377 searchstr
,keyserver
->scheme
,keyserver
->host
);
1379 log_info(_("searching for \"%s\" from %s\n"),
1380 searchstr
,keyserver
->uri
);
1386 log_fatal(_("no keyserver action!\n"));
1390 /* Done sending, so start reading. */
1391 ret
=exec_read(spawn
);
1395 /* Now handle the response */
1403 if(iobuf_read_line(spawn
->fromchild
,&line
,&buflen
,&maxlen
)==0)
1405 ret
= gpg_error_from_errno (errno
);
1406 goto fail
; /* i.e. EOF */
1411 /* remove trailing whitespace */
1413 while(plen
>0 && ascii_isspace(ptr
[plen
-1]))
1420 if(ascii_strncasecmp(ptr
,"VERSION ",8)==0)
1424 if(atoi(&ptr
[8])!=KEYSERVER_PROTO_VERSION
)
1426 log_error(_("invalid keyserver protocol (us %d!=handler %d)\n"),
1427 KEYSERVER_PROTO_VERSION
,atoi(&ptr
[8]));
1431 else if(ascii_strncasecmp(ptr
,"PROGRAM ",8)==0)
1433 if(ascii_strncasecmp(&ptr
[8],VERSION
,strlen(VERSION
))!=0)
1434 log_info(_("WARNING: keyserver handler from a different"
1435 " version of GnuPG (%s)\n"),&ptr
[8]);
1437 else if(ascii_strncasecmp(ptr
,"OPTION OUTOFBAND",16)==0)
1438 outofband
=1; /* Currently the only OPTION */
1443 log_error(_("keyserver did not send VERSION\n"));
1455 stats_handle
=import_new_stats_handle();
1457 /* Slurp up all the key data. In the future, it might be
1458 nice to look for KEY foo OUTOFBAND and FAILED indicators.
1459 It's harmless to ignore them, but ignoring them does make
1460 gpg complain about "no valid OpenPGP data found". One
1461 way to do this could be to continue parsing this
1462 line-by-line and make a temp iobuf for each key. */
1464 import_keys_stream(spawn
->fromchild
,stats_handle
,fpr
,fpr_len
,
1465 opt
.keyserver_options
.import_options
);
1467 import_print_stats(stats_handle
);
1468 import_release_stats_handle(stats_handle
);
1473 /* Nothing to do here */
1478 keyserver_search_prompt(spawn
->fromchild
,searchstr
);
1482 log_fatal(_("no keyserver action!\n"));
1491 *prog
=exec_finish(spawn
);
1497 keyserver_work(enum ks_action action
,STRLIST list
,KEYDB_SEARCH_DESC
*desc
,
1498 int count
,unsigned char **fpr
,size_t *fpr_len
,
1499 struct keyserver_spec
*keyserver
)
1505 log_error(_("no keyserver known (use option --keyserver)\n"));
1506 return G10ERR_BAD_URI
;
1509 #ifdef DISABLE_KEYSERVER_HELPERS
1511 log_error(_("external keyserver calls are not supported in this build\n"));
1512 return G10ERR_KEYSERVER
;
1515 /* Spawn a handler */
1517 rc
=keyserver_spawn(action
,list
,desc
,count
,&ret
,fpr
,fpr_len
,keyserver
);
1522 case KEYSERVER_SCHEME_NOT_FOUND
:
1523 log_error(_("no handler for keyserver scheme `%s'\n"),
1527 case KEYSERVER_NOT_SUPPORTED
:
1528 log_error(_("action `%s' not supported with keyserver "
1530 action
==KS_GET
?"get":action
==KS_SEND
?"send":
1531 action
==KS_SEARCH
?"search":"unknown",
1535 case KEYSERVER_VERSION_ERROR
:
1536 log_error(_(GPGKEYS_PREFIX
"%s does not support"
1537 " handler version %d\n"),
1538 keyserver_typemap(keyserver
->scheme
),
1539 KEYSERVER_PROTO_VERSION
);
1542 case KEYSERVER_TIMEOUT
:
1543 log_error(_("keyserver timed out\n"));
1546 case KEYSERVER_INTERNAL_ERROR
:
1548 log_error(_("keyserver internal error\n"));
1552 return G10ERR_KEYSERVER
;
1557 log_error(_("keyserver communications error: %s\n"),g10_errstr(rc
));
1563 #endif /* ! DISABLE_KEYSERVER_HELPERS*/
1567 keyserver_export(STRLIST users
)
1570 KEYDB_SEARCH_DESC desc
;
1573 /* Weed out descriptors that we don't support sending */
1574 for(;users
;users
=users
->next
)
1576 classify_user_id (users
->d
, &desc
);
1577 if(desc
.mode
!=KEYDB_SEARCH_MODE_SHORT_KID
&&
1578 desc
.mode
!=KEYDB_SEARCH_MODE_LONG_KID
&&
1579 desc
.mode
!=KEYDB_SEARCH_MODE_FPR16
&&
1580 desc
.mode
!=KEYDB_SEARCH_MODE_FPR20
)
1582 log_error(_("\"%s\" not a key ID: skipping\n"),users
->d
);
1586 append_to_strlist(&sl
,users
->d
);
1591 rc
=keyserver_work(KS_SEND
,sl
,NULL
,0,NULL
,NULL
,opt
.keyserver
);
1599 keyserver_import(STRLIST users
)
1601 KEYDB_SEARCH_DESC
*desc
;
1602 int num
=100,count
=0;
1605 /* Build a list of key ids */
1606 desc
=xmalloc(sizeof(KEYDB_SEARCH_DESC
)*num
);
1608 for(;users
;users
=users
->next
)
1610 classify_user_id (users
->d
, &desc
[count
]);
1611 if(desc
[count
].mode
!=KEYDB_SEARCH_MODE_SHORT_KID
&&
1612 desc
[count
].mode
!=KEYDB_SEARCH_MODE_LONG_KID
&&
1613 desc
[count
].mode
!=KEYDB_SEARCH_MODE_FPR16
&&
1614 desc
[count
].mode
!=KEYDB_SEARCH_MODE_FPR20
)
1616 log_error(_("\"%s\" not a key ID: skipping\n"),users
->d
);
1624 desc
=xrealloc(desc
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1629 rc
=keyserver_work(KS_GET
,NULL
,desc
,count
,NULL
,NULL
,opt
.keyserver
);
1637 keyserver_import_fprint(const byte
*fprint
,size_t fprint_len
,
1638 struct keyserver_spec
*keyserver
)
1640 KEYDB_SEARCH_DESC desc
;
1642 memset(&desc
,0,sizeof(desc
));
1645 desc
.mode
=KEYDB_SEARCH_MODE_FPR16
;
1646 else if(fprint_len
==20)
1647 desc
.mode
=KEYDB_SEARCH_MODE_FPR20
;
1651 memcpy(desc
.u
.fpr
,fprint
,fprint_len
);
1653 /* TODO: Warn here if the fingerprint we got doesn't match the one
1655 return keyserver_work(KS_GET
,NULL
,&desc
,1,NULL
,NULL
,keyserver
);
1659 keyserver_import_keyid(u32
*keyid
,struct keyserver_spec
*keyserver
)
1661 KEYDB_SEARCH_DESC desc
;
1663 memset(&desc
,0,sizeof(desc
));
1665 desc
.mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1666 desc
.u
.kid
[0]=keyid
[0];
1667 desc
.u
.kid
[1]=keyid
[1];
1669 return keyserver_work(KS_GET
,NULL
,&desc
,1,NULL
,NULL
,keyserver
);
1672 /* code mostly stolen from do_export_stream */
1674 keyidlist(STRLIST users
,KEYDB_SEARCH_DESC
**klist
,int *count
,int fakev3
)
1676 int rc
=0,ndesc
,num
=100;
1677 KBNODE keyblock
=NULL
,node
;
1679 KEYDB_SEARCH_DESC
*desc
;
1684 *klist
=xmalloc(sizeof(KEYDB_SEARCH_DESC
)*num
);
1691 desc
= xmalloc_clear ( ndesc
* sizeof *desc
);
1692 desc
[0].mode
= KEYDB_SEARCH_MODE_FIRST
;
1696 for (ndesc
=0, sl
=users
; sl
; sl
= sl
->next
, ndesc
++)
1698 desc
= xmalloc ( ndesc
* sizeof *desc
);
1700 for (ndesc
=0, sl
=users
; sl
; sl
= sl
->next
)
1702 if(classify_user_id (sl
->d
, desc
+ndesc
))
1705 log_error (_("key \"%s\" not found: %s\n"),
1706 sl
->d
, g10_errstr (G10ERR_INV_USER_ID
));
1710 while (!(rc
= keydb_search (kdbhd
, desc
, ndesc
)))
1713 desc
[0].mode
= KEYDB_SEARCH_MODE_NEXT
;
1715 /* read the keyblock */
1716 rc
= keydb_get_keyblock (kdbhd
, &keyblock
);
1719 log_error (_("error reading keyblock: %s\n"), g10_errstr(rc
) );
1723 if((node
=find_kbnode(keyblock
,PKT_PUBLIC_KEY
)))
1725 /* This is to work around a bug in some keyservers (pksd and
1726 OKS) that calculate v4 RSA keyids as if they were v3 RSA.
1727 The answer is to refresh both the correct v4 keyid
1728 (e.g. 99242560) and the fake v3 keyid (e.g. 68FDDBC7).
1729 This only happens for key refresh using the HKP scheme
1730 and if the refresh-add-fake-v3-keyids keyserver option is
1732 if(fakev3
&& is_RSA(node
->pkt
->pkt
.public_key
->pubkey_algo
) &&
1733 node
->pkt
->pkt
.public_key
->version
>=4)
1735 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1736 v3_keyid (node
->pkt
->pkt
.public_key
->pkey
[0],
1737 (*klist
)[*count
].u
.kid
);
1743 *klist
=xrealloc(*klist
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1747 /* v4 keys get full fingerprints. v3 keys get long keyids.
1748 This is because it's easy to calculate any sort of keyid
1749 from a v4 fingerprint, but not a v3 fingerprint. */
1751 if(node
->pkt
->pkt
.public_key
->version
<4)
1753 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1754 keyid_from_pk(node
->pkt
->pkt
.public_key
,
1755 (*klist
)[*count
].u
.kid
);
1761 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_FPR20
;
1762 fingerprint_from_pk(node
->pkt
->pkt
.public_key
,
1763 (*klist
)[*count
].u
.fpr
,&dummy
);
1766 /* This is a little hackish, using the skipfncvalue as a
1767 void* pointer to the keyserver spec, but we don't need
1768 the skipfnc here, and it saves having an additional field
1769 for this (which would be wasted space most of the
1772 (*klist
)[*count
].skipfncvalue
=NULL
;
1774 /* Are we honoring preferred keyservers? */
1775 if(opt
.keyserver_options
.options
&KEYSERVER_HONOR_KEYSERVER_URL
)
1777 PKT_user_id
*uid
=NULL
;
1778 PKT_signature
*sig
=NULL
;
1780 merge_keys_and_selfsig(keyblock
);
1782 for(node
=node
->next
;node
;node
=node
->next
)
1784 if(node
->pkt
->pkttype
==PKT_USER_ID
1785 && node
->pkt
->pkt
.user_id
->is_primary
)
1786 uid
=node
->pkt
->pkt
.user_id
;
1787 else if(node
->pkt
->pkttype
==PKT_SIGNATURE
1788 && node
->pkt
->pkt
.signature
->
1789 flags
.chosen_selfsig
&& uid
)
1791 sig
=node
->pkt
->pkt
.signature
;
1796 /* Try and parse the keyserver URL. If it doesn't work,
1797 then we end up writing NULL which indicates we are
1798 the same as any other key. */
1800 (*klist
)[*count
].skipfncvalue
=parse_preferred_keyserver(sig
);
1808 *klist
=xrealloc(*klist
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1820 keydb_release(kdbhd
);
1821 release_kbnode(keyblock
);
1826 /* Note this is different than the original HKP refresh. It allows
1827 usernames to refresh only part of the keyring. */
1830 keyserver_refresh(STRLIST users
)
1832 int rc
,count
,numdesc
,fakev3
=0;
1833 KEYDB_SEARCH_DESC
*desc
;
1834 unsigned int options
=opt
.keyserver_options
.import_options
;
1836 /* We switch merge-only on during a refresh, as 'refresh' should
1837 never import new keys, even if their keyids match. */
1838 opt
.keyserver_options
.import_options
|=IMPORT_MERGE_ONLY
;
1840 /* Similarly, we switch on fast-import, since refresh may make
1841 multiple import sets (due to preferred keyserver URLs). We don't
1842 want each set to rebuild the trustdb. Instead we do it once at
1844 opt
.keyserver_options
.import_options
|=IMPORT_FAST
;
1846 /* If refresh_add_fake_v3_keyids is on and it's a HKP or MAILTO
1847 scheme, then enable fake v3 keyid generation. */
1848 if((opt
.keyserver_options
.options
&KEYSERVER_ADD_FAKE_V3
) && opt
.keyserver
1849 && (ascii_strcasecmp(opt
.keyserver
->scheme
,"hkp")==0 ||
1850 ascii_strcasecmp(opt
.keyserver
->scheme
,"mailto")==0))
1853 rc
=keyidlist(users
,&desc
,&numdesc
,fakev3
);
1862 /* Try to handle preferred keyserver keys first */
1863 for(i
=0;i
<numdesc
;i
++)
1865 if(desc
[i
].skipfncvalue
)
1867 struct keyserver_spec
*keyserver
=desc
[i
].skipfncvalue
;
1869 /* We use the keyserver structure we parsed out before.
1870 Note that a preferred keyserver without a scheme://
1871 will be interpreted as hkp:// */
1873 rc
=keyserver_work(KS_GET
,NULL
,&desc
[i
],1,NULL
,NULL
,keyserver
);
1875 log_info(_("WARNING: unable to refresh key %s"
1876 " via %s: %s\n"),keystr_from_desc(&desc
[i
]),
1877 keyserver
->uri
,g10_errstr(rc
));
1880 /* We got it, so mark it as NONE so we don't try and
1881 get it again from the regular keyserver. */
1883 desc
[i
].mode
=KEYDB_SEARCH_MODE_NONE
;
1887 free_keyserver_spec(keyserver
);
1897 log_info(_("refreshing 1 key from %s\n"),opt
.keyserver
->uri
);
1899 log_info(_("refreshing %d keys from %s\n"),
1900 count
,opt
.keyserver
->uri
);
1903 rc
=keyserver_work(KS_GET
,NULL
,desc
,numdesc
,NULL
,NULL
,opt
.keyserver
);
1908 opt
.keyserver_options
.import_options
=options
;
1910 /* If the original options didn't have fast import, and the trustdb
1911 is dirty, rebuild. */
1912 if(!(opt
.keyserver_options
.import_options
&IMPORT_FAST
))
1913 trustdb_check_or_update();
1919 keyserver_search(STRLIST tokens
)
1922 return keyserver_work(KS_SEARCH
,tokens
,NULL
,0,NULL
,NULL
,opt
.keyserver
);
1928 keyserver_fetch(STRLIST urilist
)
1930 KEYDB_SEARCH_DESC desc
;
1932 unsigned int options
=opt
.keyserver_options
.import_options
;
1934 /* Switch on fast-import, since fetch can handle more than one
1935 import and we don't want each set to rebuild the trustdb.
1936 Instead we do it once at the end. */
1937 opt
.keyserver_options
.import_options
|=IMPORT_FAST
;
1939 /* A dummy desc since we're not actually fetching a particular key
1941 memset(&desc
,0,sizeof(desc
));
1942 desc
.mode
=KEYDB_SEARCH_MODE_EXACT
;
1944 for(sl
=urilist
;sl
;sl
=sl
->next
)
1946 struct keyserver_spec
*spec
;
1948 spec
=parse_keyserver_uri(sl
->d
,1,NULL
,0);
1953 rc
=keyserver_work(KS_GET
,NULL
,&desc
,1,NULL
,NULL
,spec
);
1955 log_info (_("WARNING: unable to fetch URI %s: %s\n"),
1956 sl
->d
,g10_errstr(rc
));
1958 free_keyserver_spec(spec
);
1961 log_info (_("WARNING: unable to parse URI %s\n"),sl
->d
);
1964 opt
.keyserver_options
.import_options
=options
;
1966 /* If the original options didn't have fast import, and the trustdb
1967 is dirty, rebuild. */
1968 if(!(opt
.keyserver_options
.import_options
&IMPORT_FAST
))
1969 trustdb_check_or_update();
1974 /* Import key in a CERT or pointed to by a CERT */
1976 keyserver_import_cert(const char *name
,unsigned char **fpr
,size_t *fpr_len
)
1978 char *domain
,*look
,*url
;
1980 int type
,rc
=G10ERR_GENERAL
;
1984 domain
=strrchr(look
,'@');
1988 type
=get_dns_cert(look
,max_cert_size
,&key
,fpr
,fpr_len
,&url
);
1991 int armor_status
=opt
.no_armor
;
1993 /* CERTs are always in binary format */
1996 rc
=import_keys_stream(key
,NULL
,fpr
,fpr_len
,
1997 opt
.keyserver_options
.import_options
);
1999 opt
.no_armor
=armor_status
;
2003 else if(type
==2 && *fpr
)
2005 /* We only consider the IPGP type if a fingerprint was provided.
2006 This lets us select the right key regardless of what a URL
2007 points to, or get the key from a keyserver. */
2010 struct keyserver_spec
*spec
;
2012 spec
=parse_keyserver_uri(url
,1,NULL
,0);
2017 add_to_strlist(&list
,url
);
2019 rc
=keyserver_fetch(list
);
2022 free_keyserver_spec(spec
);
2025 else if(opt
.keyserver
)
2027 /* If only a fingerprint is provided, try and fetch it from
2030 rc
=keyserver_import_fprint(*fpr
,*fpr_len
,opt
.keyserver
);
2033 log_info(_("no keyserver known (use option --keyserver)\n"));
2035 /* Give a better string here? "CERT fingerprint for \"%s\"
2036 found, but no keyserver" " known (use option
2037 --keyserver)\n" ? */
2047 /* Import key pointed to by a PKA record. Return the requested
2048 fingerprint in fpr. */
2050 keyserver_import_pka(const char *name
,unsigned char **fpr
,size_t *fpr_len
)
2058 uri
= get_pka_info (name
, *fpr
);
2061 struct keyserver_spec
*spec
;
2062 spec
= parse_keyserver_uri (uri
, 1, NULL
, 0);
2065 rc
=keyserver_import_fprint (*fpr
, 20, spec
);
2066 free_keyserver_spec (spec
);
2077 /* Import all keys that match name */
2079 keyserver_import_name(const char *name
,unsigned char **fpr
,size_t *fpr_len
,
2080 struct keyserver_spec
*keyserver
)
2085 append_to_strlist(&list
,name
);
2087 rc
=keyserver_work(KS_GETNAME
,list
,NULL
,0,fpr
,fpr_len
,keyserver
);
2094 /* Use the PGP Universal trick of asking ldap://keys.(maildomain) for
2097 keyserver_import_ldap(const char *name
,unsigned char **fpr
,size_t *fpr_len
)
2100 struct keyserver_spec
*keyserver
;
2104 append_to_strlist(&list
,name
);
2106 /* Parse out the domain */
2107 domain
=strrchr(name
,'@');
2109 return G10ERR_GENERAL
;
2113 keyserver
=xmalloc_clear(sizeof(struct keyserver_spec
));
2115 keyserver
->scheme
=xstrdup("ldap");
2116 keyserver
->host
=xmalloc(5+strlen(domain
)+1);
2117 strcpy(keyserver
->host
,"keys.");
2118 strcat(keyserver
->host
,domain
);
2119 keyserver
->uri
=xmalloc(strlen(keyserver
->scheme
)+
2120 3+strlen(keyserver
->host
)+1);
2121 strcpy(keyserver
->uri
,keyserver
->scheme
);
2122 strcat(keyserver
->uri
,"://");
2123 strcat(keyserver
->uri
,keyserver
->host
);
2125 rc
=keyserver_work(KS_GETNAME
,list
,NULL
,0,fpr
,fpr_len
,keyserver
);
2129 free_keyserver_spec(keyserver
);