1 /* keyserver.c - generic keyserver code
2 * Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
40 #include "keyserver-internal.h"
49 KEYDB_SEARCH_DESC desc
;
50 time_t createtime
,expiretime
;
60 int tell
; /* tell remote process about this one */
64 {"include-revoked",1,&opt
.keyserver_options
.include_revoked
},
65 {"include-disabled",1,&opt
.keyserver_options
.include_disabled
},
66 {"include-subkeys",1,&opt
.keyserver_options
.include_subkeys
},
67 {"keep-temp-files",0,&opt
.keyserver_options
.keep_temp_files
},
68 {"honor-http-proxy",1,&opt
.keyserver_options
.honor_http_proxy
},
69 {"broken-http-proxy",1,&opt
.keyserver_options
.broken_http_proxy
},
70 {"refresh-add-fake-v3-keyids",0,&opt
.keyserver_options
.fake_v3_keyids
},
71 {"auto-key-retrieve",0,&opt
.keyserver_options
.auto_key_retrieve
},
72 {"try-dns-srv",1,&opt
.keyserver_options
.try_dns_srv
},
76 static int keyserver_work(int action
,STRLIST list
,
77 KEYDB_SEARCH_DESC
*desc
,int count
);
80 parse_keyserver_options(char *options
)
84 while((tok
=strsep(&options
," ,")))
91 for(i
=0;keyserver_opts
[i
].name
;i
++)
93 if(ascii_strcasecmp(tok
,keyserver_opts
[i
].name
)==0)
95 *(keyserver_opts
[i
].flag
)=1;
99 else if(ascii_strncasecmp("no-",tok
,3)==0 &&
100 ascii_strcasecmp(&tok
[3],keyserver_opts
[i
].name
)==0)
102 *(keyserver_opts
[i
].flag
)=0;
108 /* These options need more than just a flag */
111 if(ascii_strcasecmp(tok
,"verbose")==0)
112 opt
.keyserver_options
.verbose
++;
113 else if(ascii_strcasecmp(tok
,"no-verbose")==0)
114 opt
.keyserver_options
.verbose
--;
115 #ifdef EXEC_TEMPFILE_ONLY
116 else if(ascii_strcasecmp(tok
,"use-temp-files")==0 ||
117 ascii_strcasecmp(tok
,"no-use-temp-files")==0)
118 log_info(_("WARNING: keyserver option \"%s\" is not used "
119 "on this platform\n"),tok
);
121 else if(ascii_strcasecmp(tok
,"use-temp-files")==0)
122 opt
.keyserver_options
.use_temp_files
=1;
123 else if(ascii_strcasecmp(tok
,"no-use-temp-files")==0)
124 opt
.keyserver_options
.use_temp_files
=0;
127 if(!parse_import_options(tok
,
128 &opt
.keyserver_options
.import_options
) &&
129 !parse_export_options(tok
,
130 &opt
.keyserver_options
.export_options
))
131 add_to_strlist(&opt
.keyserver_options
.other
,tok
);
137 parse_keyserver_uri(char *uri
,const char *configname
,unsigned int configlineno
)
143 opt
.keyserver_host
=NULL
;
144 opt
.keyserver_port
=NULL
;
145 opt
.keyserver_opaque
=NULL
;
149 opt
.keyserver_scheme
=strsep(&uri
,":");
152 /* Assume HKP if there is no scheme */
154 uri
=opt
.keyserver_scheme
;
155 opt
.keyserver_scheme
="hkp";
159 /* Force to lowercase */
162 for(i
=opt
.keyserver_scheme
;*i
!='\0';i
++)
163 *i
=ascii_tolower(*i
);
166 if(ascii_strcasecmp(opt
.keyserver_scheme
,"x-broken-hkp")==0)
168 deprecated_warning(configname
,configlineno
,"x-broken-hkp",
169 "--keyserver-options ","broken-http-proxy");
170 opt
.keyserver_scheme
="hkp";
171 opt
.keyserver_options
.broken_http_proxy
=1;
173 else if(ascii_strcasecmp(opt
.keyserver_scheme
,"x-hkp")==0
174 || ascii_strcasecmp(opt
.keyserver_scheme
,"http")==0)
176 /* Canonicalize this to "hkp" so it works with both the internal
177 and external keyserver interface. */
178 opt
.keyserver_scheme
="hkp";
181 if(assume_hkp
|| (uri
[0]=='/' && uri
[1]=='/'))
183 /* Two slashes means network path. */
185 /* Skip over the "//", if any */
190 opt
.keyserver_host
=strsep(&uri
,":/");
191 if(opt
.keyserver_host
[0]=='\0')
192 return GPG_ERR_BAD_URI
;
194 if(uri
==NULL
|| uri
[0]=='\0')
195 opt
.keyserver_port
=NULL
;
201 opt
.keyserver_port
=strsep(&uri
,"/");
203 /* Ports are digits only */
204 ch
=opt
.keyserver_port
;
208 return GPG_ERR_BAD_URI
;
213 /* It would seem to be reasonable to limit the range of the
214 ports to values between 1-65535, but RFC 1738 and 1808
215 imply there is no limit. Of course, the real world has
219 /* (any path part of the URI is discarded for now as no keyserver
224 /* No slash means opaque. Just record the opaque blob and get
226 opt
.keyserver_opaque
=uri
;
231 /* One slash means absolute path. We don't need to support that
233 return GPG_ERR_BAD_URI
;
236 if(opt
.keyserver_scheme
[0]=='\0')
237 return GPG_ERR_BAD_URI
;
243 print_keyrec(int number
,struct keyrec
*keyrec
)
247 iobuf_writebyte(keyrec
->uidbuf
,0);
248 iobuf_flush_temp(keyrec
->uidbuf
);
249 printf("(%d)\t%s ",number
,iobuf_get_temp_buffer(keyrec
->uidbuf
));
252 printf("%d bit ",keyrec
->size
);
256 const char *str
= gcry_pk_algo_name (keyrec
->type
);
264 switch(keyrec
->desc
.mode
)
266 case KEYDB_SEARCH_MODE_SHORT_KID
:
267 printf("key %08lX",(ulong
)keyrec
->desc
.u
.kid
[1]);
270 case KEYDB_SEARCH_MODE_LONG_KID
:
271 printf("key %08lX%08lX",(ulong
)keyrec
->desc
.u
.kid
[0],
272 (ulong
)keyrec
->desc
.u
.kid
[1]);
275 case KEYDB_SEARCH_MODE_FPR16
:
278 printf("%02X",(unsigned char)keyrec
->desc
.u
.fpr
[i
]);
281 case KEYDB_SEARCH_MODE_FPR20
:
284 printf("%02X",(unsigned char)keyrec
->desc
.u
.fpr
[i
]);
292 if(keyrec
->createtime
>0)
293 printf(", created %s",strtimestamp(keyrec
->createtime
));
295 if(keyrec
->expiretime
>0)
296 printf(", expires %s",strtimestamp(keyrec
->expiretime
));
299 printf(" (%s)",("revoked"));
301 printf(" (%s)",("disabled"));
303 printf(" (%s)",("expired"));
308 /* Returns a keyrec (which must be freed) once a key is complete, and
309 NULL otherwise. Call with a NULL keystring once key parsing is
310 complete to return any unfinished keys. */
311 static struct keyrec
*
312 parse_keyrec(char *keystring
)
314 static struct keyrec
*work
=NULL
;
315 struct keyrec
*ret
=NULL
;
323 else if(work
->desc
.mode
==KEYDB_SEARCH_MODE_NONE
)
338 work
=xcalloc (1,sizeof(struct keyrec
));
339 work
->uidbuf
=iobuf_temp();
342 /* Remove trailing whitespace */
343 for(i
=strlen(keystring
);i
>0;i
--)
344 if(ascii_isspace(keystring
[i
-1]))
349 if((record
=strsep(&keystring
,":"))==NULL
)
352 if(ascii_strcasecmp("pub",record
)==0)
359 work
=xcalloc (1,sizeof(struct keyrec
));
360 work
->uidbuf
=iobuf_temp();
363 if((tok
=strsep(&keystring
,":"))==NULL
)
366 classify_user_id(tok
,&work
->desc
);
367 if(work
->desc
.mode
!=KEYDB_SEARCH_MODE_SHORT_KID
368 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_LONG_KID
369 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_FPR16
370 && work
->desc
.mode
!=KEYDB_SEARCH_MODE_FPR20
)
372 work
->desc
.mode
=KEYDB_SEARCH_MODE_NONE
;
376 /* Note all items after this are optional. This allows us to
377 have a pub line as simple as pub:keyid and nothing else. */
381 if((tok
=strsep(&keystring
,":"))==NULL
)
384 work
->type
=atoi(tok
);
386 if((tok
=strsep(&keystring
,":"))==NULL
)
389 work
->size
=atoi(tok
);
391 if((tok
=strsep(&keystring
,":"))==NULL
)
394 work
->createtime
=atoi(tok
);
396 if((tok
=strsep(&keystring
,":"))==NULL
)
399 work
->expiretime
=atoi(tok
);
401 if((tok
=strsep(&keystring
,":"))==NULL
)
423 if(work
->expiretime
&& work
->expiretime
<=make_timestamp())
426 else if(ascii_strcasecmp("uid",record
)==0 && work
->desc
.mode
)
428 char *userid
,*tok
,*decoded
;
430 if((tok
=strsep(&keystring
,":"))==NULL
)
438 /* By definition, de-%-encoding is always smaller than the
439 original string so we can decode in place. */
444 if(tok
[0]=='%' && tok
[1] && tok
[2])
446 if((userid
[i
]=hextobyte(&tok
[1]))==-1)
455 /* We don't care about the other info provided in the uid: line
456 since no keyserver supports marking userids with timestamps
457 or revoked/expired/disabled yet. */
459 /* No need to check for control characters, as utf8_to_native
462 decoded
=utf8_to_native(userid
,i
,0);
463 iobuf_writestr(work
->uidbuf
,decoded
);
465 iobuf_writestr(work
->uidbuf
,"\n\t");
469 /* Ignore any records other than "pri" and "uid" for easy future
475 /* TODO: do this as a list sent to keyserver_work rather than calling
476 it once for each key to get the correct counts after the import
477 (cosmetics, really) and to better take advantage of the keyservers
478 that can do multiple fetches in one go (LDAP). */
480 show_prompt(KEYDB_SEARCH_DESC
*desc
,int numdesc
,int count
,const char *search
)
484 if(count
&& opt
.command_fd
==-1)
487 tty_printf("Keys %d-%d of %d for \"%s\". ",from
,numdesc
,count
,search
);
491 answer
=cpr_get_no_help("keysearch.prompt",
492 _("Enter number(s), N)ext, or Q)uit > "));
494 if(answer
[0]=='\x04')
500 if(answer
[0]=='q' || answer
[0]=='Q')
505 else if(atoi(answer
)>=1 && atoi(answer
)<=numdesc
)
507 char *split
=answer
,*num
;
509 while((num
=strsep(&split
," ,"))!=NULL
)
510 if(atoi(num
)>=1 && atoi(num
)<=numdesc
)
511 keyserver_work(GET
,NULL
,&desc
[atoi(num
)-1],1);
520 /* Count and searchstr are just for cosmetics. If the count is too
521 small, it will grow safely. If negative it disables the "Key x-y
524 keyserver_search_prompt(iobuf_t buffer
,const char *searchstr
)
526 int i
=0,validcount
=0,started
=0,header
=0,count
=1;
527 unsigned int maxlen
,buflen
;
528 KEYDB_SEARCH_DESC
*desc
;
530 /* TODO: Something other than 23? That's 24-1 (the prompt). */
531 int maxlines
=23,numlines
=0;
533 desc
=xmalloc (count
*sizeof(KEYDB_SEARCH_DESC
));
537 struct keyrec
*keyrec
;
541 rl
=iobuf_read_line(buffer
,&line
,&buflen
,&maxlen
);
545 if(!header
&& ascii_strncasecmp("SEARCH ",line
,7)==0
546 && ascii_strncasecmp(" BEGIN",&line
[strlen(line
)-7],6)==0)
551 else if(ascii_strncasecmp("SEARCH ",line
,7)==0
552 && ascii_strncasecmp(" END",&line
[strlen(line
)-5],4)==0)
558 /* Look for an info: line. The only current info: values
559 defined are the version and key count. */
560 if(!started
&& rl
>0 && ascii_strncasecmp("info:",line
,5)==0)
562 char *tok
,*str
=&line
[5];
564 if((tok
=strsep(&str
,":"))!=NULL
)
568 if(sscanf(tok
,"%d",&version
)!=1)
573 log_error(_("invalid keyserver protocol "
574 "(us %d!=handler %d)\n"),1,version
);
579 if((tok
=strsep(&str
,":"))!=NULL
&& sscanf(tok
,"%d",&count
)==1)
588 desc
=xrealloc(desc
,count
*sizeof(KEYDB_SEARCH_DESC
));
597 keyrec
=parse_keyrec(NULL
);
612 if(show_prompt(desc
,i
,validcount
?count
:0,searchstr
))
621 keyrec
=parse_keyrec(line
);
625 /* keyserver helper sent more keys than they claimed in the
628 desc
=xrealloc(desc
,count
*sizeof(KEYDB_SEARCH_DESC
));
634 desc
[i
]=keyrec
->desc
;
638 if(numlines
+keyrec
->lines
>maxlines
)
640 if(show_prompt(desc
,i
,validcount
?count
:0,searchstr
))
646 print_keyrec(i
+1,keyrec
);
649 numlines
+=keyrec
->lines
;
650 iobuf_close(keyrec
->uidbuf
);
665 log_info(_("key \"%s\" not found on keyserver\n"),searchstr
);
667 log_info(_("key not found on keyserver\n"));
672 #define KEYSERVER_ARGS_KEEP " -o \"%O\" \"%I\""
673 #define KEYSERVER_ARGS_NOKEEP " -o \"%o\" \"%i\""
676 keyserver_spawn(int action
,STRLIST list
,
677 KEYDB_SEARCH_DESC
*desc
,int count
,int *prog
)
679 int ret
=0,i
,gotversion
=0,outofband
=0;
681 unsigned int maxlen
,buflen
;
682 char *command
=NULL
,*searchstr
=NULL
;
685 struct exec_info
*spawn
;
687 #ifdef EXEC_TEMPFILE_ONLY
688 opt
.keyserver_options
.use_temp_files
=1;
691 /* Push the libexecdir into path. If DISABLE_KEYSERVER_PATH is set,
692 use the 0 arg to replace the path. */
693 #ifdef DISABLE_KEYSERVER_PATH
694 set_exec_path(GNUPG_LIBEXECDIR
,0);
696 set_exec_path(GNUPG_LIBEXECDIR
,opt
.exec_path_set
);
699 /* Build the filename for the helper to execute */
700 command
=xmalloc (strlen("gpgkeys_")+strlen(opt
.keyserver_scheme
)+1);
701 strcpy(command
,"gpgkeys_");
702 strcat(command
,opt
.keyserver_scheme
);
704 if(opt
.keyserver_options
.use_temp_files
)
706 if(opt
.keyserver_options
.keep_temp_files
)
708 command
=xrealloc(command
,strlen(command
)+
709 strlen(KEYSERVER_ARGS_KEEP
)+1);
710 strcat(command
,KEYSERVER_ARGS_KEEP
);
714 command
=xrealloc(command
,strlen(command
)+
715 strlen(KEYSERVER_ARGS_NOKEEP
)+1);
716 strcat(command
,KEYSERVER_ARGS_NOKEEP
);
719 ret
=exec_write(&spawn
,NULL
,command
,NULL
,0,0);
722 ret
=exec_write(&spawn
,command
,NULL
,NULL
,0,0);
727 fprintf(spawn
->tochild
,"# This is a gpg keyserver communications file\n");
728 fprintf(spawn
->tochild
,"VERSION %d\n",KEYSERVER_PROTO_VERSION
);
729 fprintf(spawn
->tochild
,"PROGRAM %s\n",VERSION
);
731 if(opt
.keyserver_opaque
)
732 fprintf(spawn
->tochild
,"OPAQUE %s\n",opt
.keyserver_opaque
);
735 if(opt
.keyserver_host
)
736 fprintf(spawn
->tochild
,"HOST %s\n",opt
.keyserver_host
);
738 if(opt
.keyserver_port
)
739 fprintf(spawn
->tochild
,"PORT %s\n",opt
.keyserver_port
);
744 for(i
=0,kopts
=keyserver_opts
;kopts
[i
].name
;i
++)
745 if(*(kopts
[i
].flag
) && kopts
[i
].tell
)
746 fprintf(spawn
->tochild
,"OPTION %s\n",kopts
[i
].name
);
748 for(i
=0;i
<opt
.keyserver_options
.verbose
;i
++)
749 fprintf(spawn
->tochild
,"OPTION verbose\n");
751 temp
=opt
.keyserver_options
.other
;
753 for(;temp
;temp
=temp
->next
)
754 fprintf(spawn
->tochild
,"OPTION %s\n",temp
->d
);
760 fprintf(spawn
->tochild
,"COMMAND GET\n\n");
762 /* Which keys do we want? */
766 if(desc
[i
].mode
==KEYDB_SEARCH_MODE_FPR20
)
770 fprintf(spawn
->tochild
,"0x");
772 for(f
=0;f
<MAX_FINGERPRINT_LEN
;f
++)
773 fprintf(spawn
->tochild
,"%02X",(byte
)desc
[i
].u
.fpr
[f
]);
775 fprintf(spawn
->tochild
,"\n");
777 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_FPR16
)
781 fprintf(spawn
->tochild
,"0x");
784 fprintf(spawn
->tochild
,"%02X",(byte
)desc
[i
].u
.fpr
[f
]);
786 fprintf(spawn
->tochild
,"\n");
788 else if(desc
[i
].mode
==KEYDB_SEARCH_MODE_LONG_KID
)
789 fprintf(spawn
->tochild
,"0x%08lX%08lX\n",
790 (ulong
)desc
[i
].u
.kid
[0],
791 (ulong
)desc
[i
].u
.kid
[1]);
793 fprintf(spawn
->tochild
,"0x%08lX\n",
794 (ulong
)desc
[i
].u
.kid
[1]);
797 fprintf(spawn
->tochild
,"\n");
806 /* Note the extra \n here to send an empty keylist block */
807 fprintf(spawn
->tochild
,"COMMAND SEND\n\n\n");
809 for(key
=list
;key
!=NULL
;key
=key
->next
)
811 armor_filter_context_t afx
;
812 iobuf_t buffer
=iobuf_temp();
816 add_to_strlist(&temp
,key
->d
);
818 memset(&afx
,0,sizeof(afx
));
820 iobuf_push_filter(buffer
,armor_filter
,&afx
);
822 /* TODO: Don't use the keyblock hack here - instead,
823 output each key as a different ascii armored blob with
824 its own INFO section. */
826 if(export_pubkeys_stream(buffer
,temp
,&block
,
827 opt
.keyserver_options
.export_options
)==-1)
833 iobuf_flush_temp(buffer
);
835 merge_keys_and_selfsig(block
);
837 fprintf(spawn
->tochild
,"INFO %s BEGIN\n",key
->d
);
839 for(node
=block
;node
;node
=node
->next
)
841 switch(node
->pkt
->pkttype
)
847 case PKT_PUBLIC_SUBKEY
:
849 PKT_public_key
*pk
=node
->pkt
->pkt
.public_key
;
851 keyid_from_pk(pk
,NULL
);
853 fprintf(spawn
->tochild
,"%sb:%08lX%08lX:%u:%u:%u:%u:",
854 node
->pkt
->pkttype
==PKT_PUBLIC_KEY
?"pu":"su",
855 (ulong
)pk
->keyid
[0],(ulong
)pk
->keyid
[1],
862 fprintf(spawn
->tochild
,"r");
864 fprintf(spawn
->tochild
,"e");
866 fprintf(spawn
->tochild
,"\n");
873 PKT_user_id
*uid
=node
->pkt
->pkt
.user_id
;
879 fprintf(spawn
->tochild
,"uid:");
881 /* Quote ':', '%', and any 8-bit
883 for(r
=0;r
<uid
->len
;r
++)
885 if(uid
->name
[r
]==':' || uid
->name
[r
]=='%'
886 || uid
->name
[r
]&0x80)
887 fprintf(spawn
->tochild
,"%%%02X",uid
->name
[r
]);
889 fprintf(spawn
->tochild
,"%c",uid
->name
[r
]);
892 fprintf(spawn
->tochild
,":%u:%u:",
893 uid
->created
,uid
->expiredate
);
896 fprintf(spawn
->tochild
,"r");
898 fprintf(spawn
->tochild
,"e");
900 fprintf(spawn
->tochild
,"\n");
905 fprintf(spawn
->tochild
,"INFO %s END\n",key
->d
);
907 fprintf(spawn
->tochild
,"KEY %s BEGIN\n",key
->d
);
908 fwrite(iobuf_get_temp_buffer(buffer
),
909 iobuf_get_temp_length(buffer
),1,spawn
->tochild
);
910 fprintf(spawn
->tochild
,"KEY %s END\n",key
->d
);
913 release_kbnode(block
);
926 fprintf(spawn
->tochild
,"COMMAND SEARCH\n\n");
928 /* Which keys do we want? Remember that the gpgkeys_ program
929 is going to lump these together into a search string. */
931 for(key
=list
;key
!=NULL
;key
=key
->next
)
933 fprintf(spawn
->tochild
,"%s\n",key
->d
);
936 searchstr
=xrealloc(searchstr
,
937 strlen(searchstr
)+strlen(key
->d
)+2);
938 strcat(searchstr
," ");
942 searchstr
=xmalloc (strlen(key
->d
)+1);
946 strcat(searchstr
,key
->d
);
949 fprintf(spawn
->tochild
,"\n");
955 log_fatal(_("no keyserver action!\n"));
959 /* Done sending, so start reading. */
960 ret
=exec_read(spawn
);
964 /* Now handle the response */
972 if(iobuf_read_line(spawn
->fromchild
,&line
,&buflen
,&maxlen
)==0)
974 ret
= iobuf_error (spawn
->fromchild
);
975 goto fail
; /* i.e. EOF */
980 /* remove trailing whitespace */
982 while(plen
>0 && ascii_isspace(ptr
[plen
-1]))
989 if(ascii_strncasecmp(ptr
,"VERSION ",8)==0)
993 if(atoi(&ptr
[8])!=KEYSERVER_PROTO_VERSION
)
995 log_error(_("invalid keyserver protocol (us %d!=handler %d)\n"),
996 KEYSERVER_PROTO_VERSION
,atoi(&ptr
[8]));
1000 else if(ascii_strncasecmp(ptr
,"PROGRAM ",8)==0)
1002 if(ascii_strncasecmp(&ptr
[8],VERSION
,strlen(VERSION
))!=0)
1003 log_info(_("WARNING: keyserver handler from a different "
1004 "version of GnuPG (%s)\n"),&ptr
[8]);
1006 else if(ascii_strncasecmp(ptr
,"OPTION OUTOFBAND",16)==0)
1007 outofband
=1; /* Currently the only OPTION */
1012 log_error(_("keyserver did not send VERSION\n"));
1023 stats_handle
=import_new_stats_handle();
1025 /* Slurp up all the key data. In the future, it might be
1026 nice to look for KEY foo OUTOFBAND and FAILED indicators.
1027 It's harmless to ignore them, but ignoring them does make
1028 gpg complain about "no valid OpenPGP data found". One
1029 way to do this could be to continue parsing this
1030 line-by-line and make a temp iobuf for each key. */
1032 import_keys_stream(spawn
->fromchild
,stats_handle
,
1033 opt
.keyserver_options
.import_options
);
1035 import_print_stats(stats_handle
);
1036 import_release_stats_handle(stats_handle
);
1041 /* Nothing to do here */
1047 keyserver_search_prompt(spawn
->fromchild
,searchstr
);
1053 log_fatal(_("no keyserver action!\n"));
1060 *prog
=exec_finish(spawn
);
1066 keyserver_work(int action
,STRLIST list
,KEYDB_SEARCH_DESC
*desc
,int count
)
1070 if(opt
.keyserver_scheme
==NULL
)
1072 log_error(_("no keyserver known (use option --keyserver)\n"));
1073 return GPG_ERR_BAD_URI
;
1076 #ifdef DISABLE_KEYSERVER_HELPERS
1078 log_error(_("external keyserver calls are not supported in this build\n"));
1079 return GPG_ERR_KEYSERVER
;
1082 /* Spawn a handler */
1084 rc
=keyserver_spawn(action
,list
,desc
,count
,&ret
);
1089 case KEYSERVER_SCHEME_NOT_FOUND
:
1090 log_error(_("no handler for keyserver scheme \"%s\"\n"),
1091 opt
.keyserver_scheme
);
1094 case KEYSERVER_NOT_SUPPORTED
:
1095 log_error(_("action \"%s\" not supported with keyserver "
1097 action
==GET
?"get":action
==SEND
?"send":
1098 action
==SEARCH
?"search":"unknown",
1099 opt
.keyserver_scheme
);
1102 case KEYSERVER_VERSION_ERROR
:
1103 log_error(_("gpgkeys_%s does not support handler version %d\n"),
1104 opt
.keyserver_scheme
,KEYSERVER_PROTO_VERSION
);
1107 case KEYSERVER_INTERNAL_ERROR
:
1109 log_error(_("keyserver internal error\n"));
1113 return GPG_ERR_KEYSERVER
;
1118 log_error(_("keyserver communications error: %s\n"),gpg_strerror (rc
));
1124 #endif /* ! DISABLE_KEYSERVER_HELPERS*/
1128 keyserver_export(STRLIST users
)
1130 /* We better ask for confirmation when the user entered --send-keys
1131 without arguments. Sending all keys might not be the thing he
1133 if (users
|| opt
.batch
|| opt
.answer_yes
)
1135 else if ( !cpr_get_answer_is_yes
1136 ("keyserver_export.send_all",
1137 _("Do you really want to send all your "
1138 "public keys to the keyserver? (y/N) ")))
1141 return keyserver_work(SEND
,users
,NULL
,0);
1145 keyserver_import(STRLIST users
)
1147 KEYDB_SEARCH_DESC
*desc
;
1148 int num
=100,count
=0;
1151 /* Build a list of key ids */
1152 desc
=xmalloc (sizeof(KEYDB_SEARCH_DESC
)*num
);
1154 for(;users
;users
=users
->next
)
1156 classify_user_id (users
->d
, &desc
[count
]);
1157 if(desc
[count
].mode
!=KEYDB_SEARCH_MODE_SHORT_KID
&&
1158 desc
[count
].mode
!=KEYDB_SEARCH_MODE_LONG_KID
&&
1159 desc
[count
].mode
!=KEYDB_SEARCH_MODE_FPR16
&&
1160 desc
[count
].mode
!=KEYDB_SEARCH_MODE_FPR20
)
1162 log_error(_("skipping invalid key ID \"%s\"\n"),users
->d
);
1170 desc
=xrealloc(desc
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1175 rc
=keyserver_work(GET
,NULL
,desc
,count
);
1183 keyserver_import_fprint(const byte
*fprint
,size_t fprint_len
)
1185 KEYDB_SEARCH_DESC desc
;
1187 memset(&desc
,0,sizeof(desc
));
1190 desc
.mode
=KEYDB_SEARCH_MODE_FPR16
;
1191 else if(fprint_len
==20)
1192 desc
.mode
=KEYDB_SEARCH_MODE_FPR20
;
1196 memcpy(desc
.u
.fpr
,fprint
,fprint_len
);
1198 return keyserver_work(GET
,NULL
,&desc
,1);
1202 keyserver_import_keyid(u32
*keyid
)
1204 KEYDB_SEARCH_DESC desc
;
1206 memset(&desc
,0,sizeof(desc
));
1208 desc
.mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1209 desc
.u
.kid
[0]=keyid
[0];
1210 desc
.u
.kid
[1]=keyid
[1];
1212 return keyserver_work(GET
,NULL
,&desc
,1);
1215 /* code mostly stolen from do_export_stream */
1217 keyidlist(STRLIST users
,KEYDB_SEARCH_DESC
**klist
,int *count
,int fakev3
)
1219 int rc
=0,ndesc
,num
=100;
1220 KBNODE keyblock
=NULL
,node
;
1222 KEYDB_SEARCH_DESC
*desc
;
1227 *klist
=xmalloc (sizeof(KEYDB_SEARCH_DESC
)*num
);
1234 desc
= xcalloc (1, ndesc
* sizeof *desc
);
1235 desc
[0].mode
= KEYDB_SEARCH_MODE_FIRST
;
1239 for (ndesc
=0, sl
=users
; sl
; sl
= sl
->next
, ndesc
++)
1241 desc
= xmalloc ( ndesc
* sizeof *desc
);
1243 for (ndesc
=0, sl
=users
; sl
; sl
= sl
->next
)
1245 if(classify_user_id (sl
->d
, desc
+ndesc
))
1248 log_error (_("key `%s' not found: %s\n"),
1249 sl
->d
, gpg_strerror (GPG_ERR_INV_USER_ID
));
1253 while (!(rc
= keydb_search (kdbhd
, desc
, ndesc
)))
1256 desc
[0].mode
= KEYDB_SEARCH_MODE_NEXT
;
1258 /* read the keyblock */
1259 rc
= keydb_get_keyblock (kdbhd
, &keyblock
);
1262 log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc
) );
1266 if((node
=find_kbnode(keyblock
,PKT_PUBLIC_KEY
)))
1268 /* This is to work around a bug in some keyservers (pksd and
1269 OKS) that calculate v4 RSA keyids as if they were v3 RSA.
1270 The answer is to refresh both the correct v4 keyid
1271 (e.g. 99242560) and the fake v3 keyid (e.g. 68FDDBC7).
1272 This only happens for key refresh using the HKP scheme
1273 and if the refresh-add-fake-v3-keyids keyserver option is
1275 if(fakev3
&& is_RSA(node
->pkt
->pkt
.public_key
->pubkey_algo
) &&
1276 node
->pkt
->pkt
.public_key
->version
>=4)
1278 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1279 v3_keyid (node
->pkt
->pkt
.public_key
->pkey
[0],
1280 (*klist
)[*count
].u
.kid
);
1286 *klist
=xrealloc(*klist
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1290 /* v4 keys get full fingerprints. v3 keys get long keyids.
1291 This is because it's easy to calculate any sort of key id
1292 from a v4 fingerprint, but not a v3 fingerprint. */
1294 if(node
->pkt
->pkt
.public_key
->version
<4)
1296 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_LONG_KID
;
1297 keyid_from_pk(node
->pkt
->pkt
.public_key
,
1298 (*klist
)[*count
].u
.kid
);
1304 (*klist
)[*count
].mode
=KEYDB_SEARCH_MODE_FPR20
;
1305 fingerprint_from_pk(node
->pkt
->pkt
.public_key
,
1306 (*klist
)[*count
].u
.fpr
,&dummy
);
1314 *klist
=xrealloc(*klist
,sizeof(KEYDB_SEARCH_DESC
)*num
);
1324 keydb_release(kdbhd
);
1325 release_kbnode(keyblock
);
1330 /* Note this is different than the original HKP refresh. It allows
1331 usernames to refresh only part of the keyring. */
1334 keyserver_refresh(STRLIST users
)
1336 int rc
,count
,fakev3
=0;
1337 KEYDB_SEARCH_DESC
*desc
;
1339 /* We switch merge_only on during a refresh, as 'refresh' should
1340 never import new keys, even if their keyids match. Is it worth
1341 preserving the old merge_only value here? */
1344 /* If refresh_add_fake_v3_keyids is on and it's a HKP or MAILTO
1345 scheme, then enable fake v3 keyid generation. */
1346 if(opt
.keyserver_options
.fake_v3_keyids
&& opt
.keyserver_scheme
&&
1347 (ascii_strcasecmp(opt
.keyserver_scheme
,"hkp")==0 ||
1348 ascii_strcasecmp(opt
.keyserver_scheme
,"mailto")==0))
1351 rc
=keyidlist(users
,&desc
,&count
,fakev3
);
1357 if(opt
.keyserver_uri
)
1360 log_info(_("refreshing 1 key from %s\n"),opt
.keyserver_uri
);
1362 log_info(_("refreshing %d keys from %s\n"),
1363 count
,opt
.keyserver_uri
);
1366 rc
=keyserver_work(GET
,NULL
,desc
,count
);
1375 keyserver_search(STRLIST tokens
)
1378 return keyserver_work(SEARCH
,tokens
,NULL
,0);