1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
|
/* keyserver.c - generic keyserver code
* Copyright (C) 2001, 2002 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GnuPG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <config.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "filter.h"
#include "keydb.h"
#include "status.h"
#include "exec.h"
#include "main.h"
#include "i18n.h"
#include "iobuf.h"
#include "memory.h"
#include "options.h"
#include "packet.h"
#include "keyserver-internal.h"
#include "util.h"
#define KEYSERVER_PROTO_VERSION 0
#define GET 0
#define SEND 1
#define SEARCH 2
struct kopts
{
char *name;
int tell; /* tell remote process about this one */
int *flag;
} keyserver_opts[]=
{
{"include-revoked",1,&opt.keyserver_options.include_revoked},
{"include-disabled",1,&opt.keyserver_options.include_disabled},
{"include-subkeys",1,&opt.keyserver_options.include_subkeys},
{"keep-temp-files",0,&opt.keyserver_options.keep_temp_files},
{"honor-http-proxy",1,&opt.keyserver_options.honor_http_proxy},
{"broken-http-proxy",1,&opt.keyserver_options.broken_http_proxy},
{"refresh-add-fake-v3-keyids",0,&opt.keyserver_options.fake_v3_keyids},
{"auto-key-retrieve",0,&opt.keyserver_options.auto_key_retrieve},
{NULL}
};
void
parse_keyserver_options(char *options)
{
char *tok;
while((tok=strsep(&options," ,")))
{
int i,hit=0;
if(tok[0]=='\0')
continue;
for(i=0;keyserver_opts[i].name;i++)
{
if(ascii_strcasecmp(tok,keyserver_opts[i].name)==0)
{
*(keyserver_opts[i].flag)=1;
hit=1;
break;
}
else if(ascii_strncasecmp("no-",tok,3)==0 &&
ascii_strcasecmp(&tok[3],keyserver_opts[i].name)==0)
{
*(keyserver_opts[i].flag)=0;
hit=1;
break;
}
}
/* These options need more than just a flag */
if(!hit)
{
if(ascii_strcasecmp(tok,"verbose")==0)
opt.keyserver_options.verbose++;
else if(ascii_strcasecmp(tok,"no-verbose")==0)
opt.keyserver_options.verbose--;
#ifdef EXEC_TEMPFILE_ONLY
else if(ascii_strcasecmp(tok,"use-temp-files")==0 ||
ascii_strcasecmp(tok,"no-use-temp-files")==0)
log_info(_("WARNING: keyserver option \"%s\" is not used "
"on this platform\n"),tok);
#else
else if(ascii_strcasecmp(tok,"use-temp-files")==0)
opt.keyserver_options.use_temp_files=1;
else if(ascii_strcasecmp(tok,"no-use-temp-files")==0)
opt.keyserver_options.use_temp_files=0;
#endif
else
if(!parse_import_options(tok,
&opt.keyserver_options.import_options) &&
!parse_export_options(tok,
&opt.keyserver_options.export_options))
add_to_strlist(&opt.keyserver_options.other,tok);
}
}
}
int
parse_keyserver_uri(char *uri,const char *configname,unsigned int configlineno)
{
int assume_hkp=0;
assert(uri!=NULL);
opt.keyserver_host=NULL;
opt.keyserver_port=NULL;
opt.keyserver_opaque=NULL;
/* Get the scheme */
opt.keyserver_scheme=strsep(&uri,":");
if(uri==NULL)
{
/* Assume HKP if there is no scheme */
assume_hkp=1;
uri=opt.keyserver_scheme;
opt.keyserver_scheme="hkp";
}
if(ascii_strcasecmp(opt.keyserver_scheme,"x-broken-hkp")==0)
{
deprecated_warning(configname,configlineno,"x-broken-hkp",
"--keyserver-options ","broken-http-proxy");
opt.keyserver_scheme="hkp";
opt.keyserver_options.broken_http_proxy=1;
}
else if(ascii_strcasecmp(opt.keyserver_scheme,"x-hkp")==0)
{
/* Canonicalize this to "hkp" so it works with both the internal
and external keyserver interface. */
opt.keyserver_scheme="hkp";
}
if(assume_hkp || (uri[0]=='/' && uri[1]=='/'))
{
/* Two slashes means network path. */
/* Skip over the "//", if any */
if(!assume_hkp)
uri+=2;
/* Get the host */
opt.keyserver_host=strsep(&uri,":/");
if(opt.keyserver_host[0]=='\0')
return G10ERR_BAD_URI;
if(uri==NULL || uri[0]=='\0')
opt.keyserver_port=NULL;
else
{
char *ch;
/* Get the port */
opt.keyserver_port=strsep(&uri,"/");
/* Ports are digits only */
ch=opt.keyserver_port;
while(*ch!='\0')
{
if(!isdigit(*ch))
return G10ERR_BAD_URI;
ch++;
}
}
/* (any path part of the URI is discarded for now as no keyserver
uses it yet) */
}
else if(uri[0]!='/')
{
/* No slash means opaque. Just record the opaque blob and get
out. */
opt.keyserver_opaque=uri;
return 0;
}
else
{
/* One slash means absolute path. We don't need to support that
yet. */
return G10ERR_BAD_URI;
}
if(opt.keyserver_scheme[0]=='\0')
return G10ERR_BAD_URI;
return 0;
}
/* Unquote only the delimiter character and backslashes (\x5C) */
static void
printunquoted(char *string,char delim)
{
char *ch=string;
while(*ch)
{
if(*ch=='\\')
{
int c;
sscanf(ch,"\\x%02x",&c);
if(c==delim)
{
printf("%c",c);
ch+=3;
}
else if(c=='\\')
{
fputc('\\',stdout);
ch+=3;
}
else
fputc(*ch,stdout);
}
else
fputc(*ch,stdout);
ch++;
}
}
static int
print_keyinfo(int count,char *keystring,KEYDB_SEARCH_DESC *desc)
{
char *certid,*userid,*keytype,*tok;
int flags,keysize=0;
time_t createtime=0,expiretime=0,modifytime=0;
if((certid=strsep(&keystring,":"))==NULL)
return -1;
classify_user_id (certid, desc);
if(desc->mode!=KEYDB_SEARCH_MODE_SHORT_KID &&
desc->mode!=KEYDB_SEARCH_MODE_LONG_KID &&
desc->mode!=KEYDB_SEARCH_MODE_FPR16 &&
desc->mode!=KEYDB_SEARCH_MODE_FPR20)
return -1;
if((tok=strsep(&keystring,":"))==NULL)
return -1;
userid=utf8_to_native(tok,strlen(tok),0);
if((tok=strsep(&keystring,":"))==NULL)
goto fail;
flags=atoi(tok);
if((tok=strsep(&keystring,":"))==NULL)
goto fail;
createtime=atoi(tok);
if((tok=strsep(&keystring,":"))==NULL)
goto fail;
expiretime=atoi(tok);
if((tok=strsep(&keystring,":"))==NULL)
goto fail;
modifytime=atoi(tok);
if((keytype=strsep(&keystring,":"))==NULL)
goto fail;
/* The last one */
if(keystring!=NULL)
keysize=atoi(keystring);
printf("(%d)\t",count);
/* No need to check for control characters, as utf8_to_native does
this for us. */
printunquoted(userid,':');
if(flags&1)
printf(" (revoked)");
if(flags&2)
printf(" (disabled)");
printf("\n\t ");
if(keysize>0)
printf("%d bit ",keysize);
if(keytype[0])
printf("%s ",keytype);
printf("key %s, created %s",certid,strtimestamp(createtime));
if(expiretime>0)
printf(", expires %s",strtimestamp(expiretime));
printf("\n");
return 0;
fail:
m_free (userid);
return -1;
}
#define KEYSERVER_ARGS_KEEP " -o \"%O\" \"%I\""
#define KEYSERVER_ARGS_NOKEEP " -o \"%o\" \"%i\""
static int
keyserver_spawn(int action,STRLIST list,
KEYDB_SEARCH_DESC *desc,int count,int *prog)
{
int ret=0,i,gotversion=0,outofband=0;
STRLIST temp;
unsigned int maxlen=256,buflen;
char *command=NULL,*searchstr=NULL;
byte *line=NULL;
struct kopts *kopts;
struct exec_info *spawn;
#ifdef EXEC_TEMPFILE_ONLY
opt.keyserver_options.use_temp_files=1;
#endif
/* Push the libexecdir into path. If DISABLE_KEYSERVER_PATH is set,
use the 0 arg to replace the path. */
#ifdef DISABLE_KEYSERVER_PATH
set_exec_path(GNUPG_LIBEXECDIR,0);
#else
set_exec_path(GNUPG_LIBEXECDIR,opt.exec_path_set);
#endif
/* Build the filename for the helper to execute */
command=m_alloc(strlen("gpgkeys_")+strlen(opt.keyserver_scheme)+1);
strcpy(command,"gpgkeys_");
strcat(command,opt.keyserver_scheme);
if(opt.keyserver_options.use_temp_files)
{
if(opt.keyserver_options.keep_temp_files)
{
command=m_realloc(command,strlen(command)+
strlen(KEYSERVER_ARGS_KEEP)+1);
strcat(command,KEYSERVER_ARGS_KEEP);
}
else
{
command=m_realloc(command,strlen(command)+
strlen(KEYSERVER_ARGS_NOKEEP)+1);
strcat(command,KEYSERVER_ARGS_NOKEEP);
}
ret=exec_write(&spawn,NULL,command,NULL,0,0);
}
else
ret=exec_write(&spawn,command,NULL,NULL,0,0);
if(ret)
return ret;
fprintf(spawn->tochild,"# This is a gpg keyserver communications file\n");
fprintf(spawn->tochild,"VERSION %d\n",KEYSERVER_PROTO_VERSION);
fprintf(spawn->tochild,"PROGRAM %s\n",VERSION);
if(opt.keyserver_opaque)
fprintf(spawn->tochild,"OPAQUE %s\n",opt.keyserver_opaque);
else
{
if(opt.keyserver_host)
fprintf(spawn->tochild,"HOST %s\n",opt.keyserver_host);
if(opt.keyserver_port)
fprintf(spawn->tochild,"PORT %s\n",opt.keyserver_port);
}
/* Write options */
for(i=0,kopts=keyserver_opts;kopts[i].name;i++)
if(*(kopts[i].flag) && kopts[i].tell)
fprintf(spawn->tochild,"OPTION %s\n",kopts[i].name);
for(i=0;i<opt.keyserver_options.verbose;i++)
fprintf(spawn->tochild,"OPTION verbose\n");
temp=opt.keyserver_options.other;
for(;temp;temp=temp->next)
fprintf(spawn->tochild,"OPTION %s\n",temp->d);
switch(action)
{
case GET:
{
fprintf(spawn->tochild,"COMMAND GET\n\n");
/* Which keys do we want? */
for(i=0;i<count;i++)
{
if(desc[i].mode==KEYDB_SEARCH_MODE_FPR20)
{
int f;
fprintf(spawn->tochild,"0x");
for(f=0;f<MAX_FINGERPRINT_LEN;f++)
fprintf(spawn->tochild,"%02X",(byte)desc[i].u.fpr[f]);
fprintf(spawn->tochild,"\n");
}
else if(desc[i].mode==KEYDB_SEARCH_MODE_FPR16)
{
int f;
fprintf(spawn->tochild,"0x");
for(f=0;f<16;f++)
fprintf(spawn->tochild,"%02X",(byte)desc[i].u.fpr[f]);
fprintf(spawn->tochild,"\n");
}
else if(desc[i].mode==KEYDB_SEARCH_MODE_LONG_KID)
fprintf(spawn->tochild,"0x%08lX%08lX\n",
(ulong)desc[i].u.kid[0],
(ulong)desc[i].u.kid[1]);
else
fprintf(spawn->tochild,"0x%08lX\n",
(ulong)desc[i].u.kid[1]);
}
fprintf(spawn->tochild,"\n");
break;
}
case SEND:
{
STRLIST key;
/* Note the extra \n here to send an empty keylist block */
fprintf(spawn->tochild,"COMMAND SEND\n\n\n");
for(key=list;key!=NULL;key=key->next)
{
armor_filter_context_t afx;
IOBUF buffer=iobuf_temp();
temp=NULL;
add_to_strlist(&temp,key->d);
memset(&afx,0,sizeof(afx));
afx.what=1;
iobuf_push_filter(buffer,armor_filter,&afx);
if(export_pubkeys_stream(buffer,temp,
opt.keyserver_options.export_options)==-1)
iobuf_close(buffer);
else
{
iobuf_flush_temp(buffer);
fprintf(spawn->tochild,"KEY %s BEGIN\n",key->d);
fwrite(iobuf_get_temp_buffer(buffer),
iobuf_get_temp_length(buffer),1,spawn->tochild);
fprintf(spawn->tochild,"KEY %s END\n",key->d);
iobuf_close(buffer);
}
free_strlist(temp);
}
break;
}
case SEARCH:
{
STRLIST key;
fprintf(spawn->tochild,"COMMAND SEARCH\n\n");
/* Which keys do we want? Remember that the gpgkeys_ program
is going to lump these together into a search string. */
for(key=list;key!=NULL;key=key->next)
{
fprintf(spawn->tochild,"%s\n",key->d);
if(key!=list)
{
searchstr=m_realloc(searchstr,
strlen(searchstr)+strlen(key->d)+2);
strcat(searchstr," ");
}
else
{
searchstr=m_alloc(strlen(key->d)+1);
searchstr[0]='\0';
}
strcat(searchstr,key->d);
}
fprintf(spawn->tochild,"\n");
break;
}
default:
log_fatal(_("no keyserver action!\n"));
break;
}
/* Done sending, so start reading. */
ret=exec_read(spawn);
if(ret)
goto fail;
/* Now handle the response */
for(;;)
{
char *ptr;
int plen;
if(iobuf_read_line(spawn->fromchild,&line,&buflen,&maxlen)==0)
{
ret=G10ERR_READ_FILE;
goto fail; /* i.e. EOF */
}
ptr=line;
/* remove trailing whitespace */
plen=strlen(ptr);
while(plen>0 && isspace(ptr[plen-1]))
plen--;
plen[ptr]='\0';
if(*ptr=='\0')
break;
if(ascii_strncasecmp(ptr,"VERSION ",8)==0)
{
gotversion=1;
if(atoi(&ptr[8])!=KEYSERVER_PROTO_VERSION)
{
log_error(_("invalid keyserver protocol (us %d!=handler %d)\n"),
KEYSERVER_PROTO_VERSION,atoi(&ptr[8]));
goto fail;
}
}
else if(ascii_strncasecmp(ptr,"PROGRAM ",8)==0)
{
if(ascii_strncasecmp(&ptr[8],VERSION,strlen(VERSION))!=0)
log_info(_("WARNING: keyserver handler from a different "
"version of GnuPG (%s)\n"),&ptr[8]);
}
else if(ascii_strncasecmp(ptr,"OPTION OUTOFBAND",16)==0)
outofband=1; /* Currently the only OPTION */
}
m_free(line);
if(!gotversion)
{
log_error(_("keyserver did not send VERSION\n"));
goto fail;
}
if(!outofband)
switch(action)
{
case GET:
{
void *stats_handle;
stats_handle=import_new_stats_handle();
/* Slurp up all the key data. In the future, it might be nice
to look for KEY foo OUTOFBAND and FAILED indicators. It's
harmless to ignore them, but ignoring them does make gpg
complain about "no valid OpenPGP data found". One way to
do this could be to continue parsing this line-by-line and
make a temp iobuf for each key. */
import_keys_stream(spawn->fromchild,0,stats_handle,
opt.keyserver_options.import_options);
import_print_stats(stats_handle);
import_release_stats_handle(stats_handle);
break;
}
/* Nothing to do here */
case SEND:
break;
case SEARCH:
{
line=NULL;
buflen = 0;
maxlen = 80;
/* Look for the COUNT line */
do
{
if(iobuf_read_line(spawn->fromchild,&line,&buflen,&maxlen)==0)
{
ret=G10ERR_READ_FILE;
goto fail; /* i.e. EOF */
}
}
while(sscanf(line,"COUNT %d\n",&i)!=1);
keyserver_search_prompt(spawn->fromchild,i,searchstr);
break;
}
default:
log_fatal(_("no keyserver action!\n"));
break;
}
fail:
*prog=exec_finish(spawn);
return ret;
}
static int
keyserver_work(int action,STRLIST list,KEYDB_SEARCH_DESC *desc,int count)
{
int rc=0,ret=0;
if(opt.keyserver_scheme==NULL)
{
log_error(_("no keyserver known (use option --keyserver)\n"));
return G10ERR_BAD_URI;
}
#ifdef DISABLE_KEYSERVER_HELPERS
log_error(_("external keyserver calls are not supported in this build\n"));
return G10ERR_KEYSERVER;
#else
/* Spawn a handler */
rc=keyserver_spawn(action,list,desc,count,&ret);
if(ret)
{
switch(ret)
{
case KEYSERVER_SCHEME_NOT_FOUND:
log_error(_("no handler for keyserver scheme \"%s\"\n"),
opt.keyserver_scheme);
break;
case KEYSERVER_NOT_SUPPORTED:
log_error(_("action \"%s\" not supported with keyserver "
"scheme \"%s\"\n"),
action==GET?"get":action==SEND?"send":
action==SEARCH?"search":"unknown",
opt.keyserver_scheme);
case KEYSERVER_INTERNAL_ERROR:
default:
log_error(_("keyserver internal error\n"));
break;
}
return G10ERR_KEYSERVER;
}
if(rc)
{
log_error(_("keyserver communications error: %s\n"),g10_errstr(rc));
return rc;
}
return 0;
#endif /* ! DISABLE_KEYSERVER_HELPERS*/
}
int
keyserver_export(STRLIST users)
{
/* We better ask for confirmation when the user entered --send-keys
without arguments. Sending all keys might not be the thing he
intended to do */
if (users || opt.batch || opt.answer_yes)
;
else if ( !cpr_get_answer_is_yes
("keyserver_export.send_all",
_("Do you really want to send all your "
"public keys to the keyserver? (y/N) ")))
return -1;
return keyserver_work(SEND,users,NULL,0);
}
int
keyserver_import(STRLIST users)
{
KEYDB_SEARCH_DESC *desc;
int num=100,count=0;
int rc=0;
/* Build a list of key ids */
desc=m_alloc(sizeof(KEYDB_SEARCH_DESC)*num);
for(;users;users=users->next)
{
classify_user_id (users->d, &desc[count]);
if(desc[count].mode!=KEYDB_SEARCH_MODE_SHORT_KID &&
desc[count].mode!=KEYDB_SEARCH_MODE_LONG_KID &&
desc[count].mode!=KEYDB_SEARCH_MODE_FPR16 &&
desc[count].mode!=KEYDB_SEARCH_MODE_FPR20)
{
log_error(_("skipping invalid key ID \"%s\"\n"),users->d);
continue;
}
count++;
if(count==num)
{
num+=100;
desc=m_realloc(desc,sizeof(KEYDB_SEARCH_DESC)*num);
}
}
if(count>0)
rc=keyserver_work(GET,NULL,desc,count);
m_free(desc);
return rc;
}
int
keyserver_import_fprint(const byte *fprint,size_t fprint_len)
{
KEYDB_SEARCH_DESC desc;
memset(&desc,0,sizeof(desc));
if(fprint_len==16)
desc.mode=KEYDB_SEARCH_MODE_FPR16;
else if(fprint_len==20)
desc.mode=KEYDB_SEARCH_MODE_FPR20;
else
return -1;
memcpy(desc.u.fpr,fprint,fprint_len);
return keyserver_work(GET,NULL,&desc,1);
}
int
keyserver_import_keyid(u32 *keyid)
{
KEYDB_SEARCH_DESC desc;
memset(&desc,0,sizeof(desc));
desc.mode=KEYDB_SEARCH_MODE_LONG_KID;
desc.u.kid[0]=keyid[0];
desc.u.kid[1]=keyid[1];
return keyserver_work(GET,NULL,&desc,1);
}
/* code mostly stolen from do_export_stream */
static int
keyidlist(STRLIST users,KEYDB_SEARCH_DESC **klist,int *count,int fakev3)
{
int rc=0,ndesc,num=100;
KBNODE keyblock=NULL,node;
KEYDB_HANDLE kdbhd;
KEYDB_SEARCH_DESC *desc;
STRLIST sl;
*count=0;
*klist=m_alloc(sizeof(KEYDB_SEARCH_DESC)*num);
kdbhd=keydb_new(0);
if(!users)
{
ndesc = 1;
desc = m_alloc_clear ( ndesc * sizeof *desc);
desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
}
else
{
for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++)
;
desc = m_alloc ( ndesc * sizeof *desc);
for (ndesc=0, sl=users; sl; sl = sl->next)
{
if(classify_user_id (sl->d, desc+ndesc))
ndesc++;
else
log_error (_("key `%s' not found: %s\n"),
sl->d, g10_errstr (G10ERR_INV_USER_ID));
}
}
while (!(rc = keydb_search (kdbhd, desc, ndesc)))
{
if (!users)
desc[0].mode = KEYDB_SEARCH_MODE_NEXT;
/* read the keyblock */
rc = keydb_get_keyblock (kdbhd, &keyblock );
if( rc )
{
log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) );
goto leave;
}
if((node=find_kbnode(keyblock,PKT_PUBLIC_KEY)))
{
/* This is to work around a bug in some keyservers (pksd and
OKS) that calculate v4 RSA keyids as if they were v3 RSA.
The answer is to refresh both the correct v4 keyid
(e.g. 99242560) and the fake v3 keyid (e.g. 68FDDBC7).
This only happens for key refresh using the HKP scheme
and if the refresh-add-fake-v3-keyids keyserver option is
set. */
if(fakev3 && is_RSA(node->pkt->pkt.public_key->pubkey_algo) &&
node->pkt->pkt.public_key->version>=4)
{
(*klist)[*count].mode=KEYDB_SEARCH_MODE_LONG_KID;
mpi_get_keyid(node->pkt->pkt.public_key->pkey[0],
(*klist)[*count].u.kid);
(*count)++;
if(*count==num)
{
num+=100;
*klist=m_realloc(*klist,sizeof(KEYDB_SEARCH_DESC)*num);
}
}
/* v4 keys get full fingerprints. v3 keys get long keyids.
This is because it's easy to calculate any sort of key id
from a v4 fingerprint, but not a v3 fingerprint. */
if(node->pkt->pkt.public_key->version<4)
{
(*klist)[*count].mode=KEYDB_SEARCH_MODE_LONG_KID;
keyid_from_pk(node->pkt->pkt.public_key,
(*klist)[*count].u.kid);
}
else
{
size_t dummy;
(*klist)[*count].mode=KEYDB_SEARCH_MODE_FPR20;
fingerprint_from_pk(node->pkt->pkt.public_key,
(*klist)[*count].u.fpr,&dummy);
}
(*count)++;
if(*count==num)
{
num+=100;
*klist=m_realloc(*klist,sizeof(KEYDB_SEARCH_DESC)*num);
}
}
}
if(rc==-1)
rc=0;
leave:
m_free(desc);
keydb_release(kdbhd);
release_kbnode(keyblock);
return rc;
}
/* Note this is different than the original HKP refresh. It allows
usernames to refresh only part of the keyring. */
int
keyserver_refresh(STRLIST users)
{
int rc,count,fakev3=0;
KEYDB_SEARCH_DESC *desc;
/* We switch merge_only on during a refresh, as 'refresh' should
never import new keys, even if their keyids match. Is it worth
preserving the old merge_only value here? */
opt.merge_only=1;
/* If refresh_add_fake_v3_keyids is on and it's a HKP or MAILTO
scheme, then enable fake v3 keyid generation. */
if(opt.keyserver_options.fake_v3_keyids && opt.keyserver_scheme &&
(ascii_strcasecmp(opt.keyserver_scheme,"hkp")==0 ||
ascii_strcasecmp(opt.keyserver_scheme,"mailto")==0))
fakev3=1;
rc=keyidlist(users,&desc,&count,fakev3);
if(rc)
return rc;
if(count==1)
log_info(_("refreshing 1 key from %s\n"),opt.keyserver_uri);
else
log_info(_("refreshing %d keys from %s\n"),count,opt.keyserver_uri);
if(count>0)
rc=keyserver_work(GET,NULL,desc,count);
m_free(desc);
return 0;
}
int
keyserver_search(STRLIST tokens)
{
if(tokens)
return keyserver_work(SEARCH,tokens,NULL,0);
else
return 0;
}
/* Count and searchstr are just for cosmetics. If the count is too
small, it will grow safely. If negative it disables the "Key x-y
of z" messages. */
/* TODO: do this as a list sent to keyserver_work rather than calling
it once for each key to get the correct counts after the import
(cosmetics, really) and to better take advantage of the keyservers
that can do multiple fetches in one go (LDAP). */
void
keyserver_search_prompt(IOBUF buffer,int count,const char *searchstr)
{
int i=0,validcount=1;
unsigned int maxlen=256,buflen=0;
KEYDB_SEARCH_DESC *desc;
byte *line=NULL;
char *answer;
if(count==0)
goto notfound;
if(count<0)
{
validcount=0;
count=10;
}
desc=m_alloc(count*sizeof(KEYDB_SEARCH_DESC));
/* Read each line and show it to the user */
for(;;)
{
int rl;
if(validcount && i%10==0)
{
printf("Keys %d-%d of %d",i+1,(i+10<count)?i+10:count,count);
if(searchstr)
printf(" for \"%s\"",searchstr);
printf("\n");
}
maxlen=1024;
rl=iobuf_read_line(buffer,&line,&buflen,&maxlen);
if(rl>0)
{
if(print_keyinfo(i+1,line,&desc[i])==0)
{
i++;
if(i==count)
{
count+=10;
desc=m_realloc(desc,count*sizeof(KEYDB_SEARCH_DESC));
validcount=0;
}
}
else
continue;
}
if(rl==0 && i==0)
{
count=0;
break;
}
if(i%10==0 || rl==0)
{
answer=cpr_get_no_help("keysearch.prompt",
_("Enter number(s), N)ext, or Q)uit > "));
/* control-d */
if(answer[0]=='\x04')
{
printf("Q\n");
answer[0]='q';
}
if(answer[0]=='q' || answer[0]=='Q')
{
m_free(answer);
break;
}
else if(atoi(answer)>=1 && atoi(answer)<=i)
{
char *split=answer,*num;
while((num=strsep(&split," ,"))!=NULL)
if(atoi(num)>=1 && atoi(num)<=i)
keyserver_work(GET,NULL,&desc[atoi(num)-1],1);
m_free(answer);
break;
}
}
}
m_free(desc);
m_free(line);
notfound:
if(count==0)
{
if(searchstr)
log_info(_("key \"%s\" not found on keyserver\n"),searchstr);
else
log_info(_("key not found on keyserver\n"));
return;
}
}
|