summaryrefslogtreecommitdiffstats
path: root/g10/ringedit.c
blob: c7d1d8bb5b306af809781f54832f9515765e6ca4 (plain)
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
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
/* ringedit.c -  Function for key ring editing
 *	Copyright (C) 1998 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
 */


/****************
 * This module supplies function for:
 *
 *  - Search for a key block (pubkey and all other stuff) and return a
 *    handle for it.
 *
 *  - Lock/Unlock a key block
 *
 *  - Read a key block into a tree
 *
 *  - Update a key block
 *
 *  - Insert a new key block
 *
 *  - Delete a key block
 *
 */



#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> /* for truncate */
#include <assert.h>
#ifdef HAVE_LIBGDBM
  #include <gdbm.h>
#endif
#include "util.h"
#include "packet.h"
#include "memory.h"
#include "mpi.h"
#include "iobuf.h"
#include "keydb.h"
#include "host2net.h"
#include "options.h"
#include "main.h"
#include "i18n.h"


struct resource_table_struct {
    int used;
    int secret; /* this is a secret keyring */
    char *fname;
    IOBUF iobuf;
  #ifdef HAVE_LIBGDBM
    GDBM_FILE dbf;
  #endif
    enum resource_type rt;
};
typedef struct resource_table_struct RESTBL;

#define MAX_RESOURCES 10
static RESTBL resource_table[MAX_RESOURCES];

static int search( PACKET *pkt, KBPOS *kbpos, int secret );


static int keyring_search( PACKET *pkt, KBPOS *kbpos, IOBUF iobuf,
						const char *fname );
static int keyring_read( KBPOS *kbpos, KBNODE *ret_root );
static int keyring_enum( KBPOS *kbpos, KBNODE *ret_root, int skipsigs );
static int keyring_copy( KBPOS *kbpos, int mode, KBNODE root );

#ifdef HAVE_LIBGDBM
static int do_gdbm_store( KBPOS *kbpos, KBNODE root, int update );
static int do_gdbm_locate( GDBM_FILE dbf, KBPOS *kbpos,
					  const byte *fpr, int fprlen );
static int do_gdbm_locate_by_keyid( GDBM_FILE dbf, KBPOS *kbpos, u32 *keyid );
static int do_gdbm_read( KBPOS *kbpos, KBNODE *ret_root );
static int do_gdbm_enum( KBPOS *kbpos, KBNODE *ret_root );
#endif


static RESTBL *
check_pos( KBPOS *kbpos )
{
    if( kbpos->resno < 0 || kbpos->resno >= MAX_RESOURCES )
	return NULL;
    if( !resource_table[kbpos->resno].used )
	return NULL;
    return resource_table + kbpos->resno;
}

#ifdef HAVE_LIBGDBM
static void
fatal_gdbm_error( const char *string )
{
    log_fatal("gdbm failed: %s\n", string);
}

#endif /* HAVE_LIBGDBM */

/****************************************************************
 ****************** public functions ****************************
 ****************************************************************/

/****************
 * Get the name of the keyrings, start with a sequence number pointing to a 0.
 */
const char *
enum_keyblock_resources( int *sequence, int secret )
{
    int i = *sequence;
    const char *name = NULL;

    for(; i < MAX_RESOURCES; i++ )
	if( resource_table[i].used && !resource_table[i].secret == !secret ) {
	    if( resource_table[i].fname ) {
		name = resource_table[i].fname;
		break;
	    }
	}
    *sequence = ++i;
    return name;
}



/****************
 * Register a resource (which currently may only be a keyring file).
 * The first keyring which is added by this function is
 * created if it does not exist.
 * Note: this function may be called before secure memory is
 * available.
 */
int
add_keyblock_resource( const char *url, int force, int secret )
{
    static int any_secret, any_public;
    const char *resname = url;
    IOBUF iobuf = NULL;
    int i;
    char *filename = NULL;
    int rc = 0;
    enum resource_type rt = rt_UNKNOWN;

    /* Do we have an URL?
     *	gnupg-gdbm:filename  := this is a GDBM resource
     *	gnupg-ring:filename  := this is a plain keyring
     *	filename := See what is is, but create as plain keyring.
     */
    if( strlen( resname ) > 11 ) {
	if( !strncmp( resname, "gnupg-ring:", 11 ) ) {
	    rt = rt_RING;
	    resname += 11;
	}
	else if( !strncmp( resname, "gnupg-gdbm:", 11 ) ) {
	    rt = rt_GDBM;
	    resname += 11;
	}
      #ifndef __MINGW32__
	else if( strchr( resname, ':' ) ) {
	    log_error("%s: invalid URL\n", url );
	    rc = G10ERR_GENERAL;
	    goto leave;
	}
      #endif
    }

    if( *resname != '/' ) { /* do tilde expansion etc */
	if( strchr(resname, '/') )
	    filename = make_filename(resname, NULL);
	else
	    filename = make_filename(opt.homedir, resname, NULL);
    }
    else
	filename = m_strdup( resname );

    if( !force )
	force = secret? !any_secret : !any_public;

    for(i=0; i < MAX_RESOURCES; i++ )
	if( !resource_table[i].used )
	    break;
    if( i == MAX_RESOURCES ) {
	rc = G10ERR_RESOURCE_LIMIT;
	goto leave;
    }

    /* see whether we can determine the filetype */
    if( rt == rt_UNKNOWN ) {
	FILE *fp = fopen( filename, "rb" );

	if( fp ) {
	    u32 magic;

	    if( fread( &magic, 4, 1, fp) == 1 ) {
		if( magic == 0x13579ace )
		    rt = rt_GDBM;
		else if( magic == 0xce9a5713 )
		    log_error("%s: endianess does not match\n", url );
		else
		    rt = rt_RING;
	    }
	    else /* maybe empty: assume ring */
		rt = rt_RING;
	    fclose( fp );
	}
	else /* no file yet: create ring */
	    rt = rt_RING;
    }

    switch( rt ) {
      case rt_UNKNOWN:
	log_error("%s: unknown resource type\n", url );
	rc = G10ERR_GENERAL;
	goto leave;

      case rt_RING:
	iobuf = iobuf_fopen( filename, "rb" );
	if( !iobuf && !force ) {
	    rc = G10ERR_OPEN_FILE;
	    goto leave;
	}

	if( !iobuf ) {
	    char *last_slash_in_filename;

	    last_slash_in_filename = strrchr(filename, '/');
	    *last_slash_in_filename = 0;

	    if( access(filename, F_OK) ) {
		if( strlen(filename) >= 7
		    && !strcmp(filename+strlen(filename)-7, "/.gnupg") ) {
		  #if __MINGW32__
		    if( mkdir(filename) )
		  #else
		    if( mkdir(filename, S_IRUSR|S_IWUSR|S_IXUSR) )
		  #endif
		    {
			log_error( _("%s: can't create directory: %s\n"),
				  filename, strerror(errno));
			rc = G10ERR_OPEN_FILE;
			goto leave;
		    }
		    else
			log_info( _("%s: directory created\n"), filename );
		    copy_options_file( filename );
		}
		else
		{
		    rc = G10ERR_OPEN_FILE;
		    goto leave;
		}
	    }

	    *last_slash_in_filename = '/';

	    iobuf = iobuf_create( filename );
	    if( !iobuf ) {
		log_error("%s: can't create keyring: %s\n", filename, strerror(errno));
		rc = G10ERR_OPEN_FILE;
		goto leave;
	    }
	    else
		log_info("%s: keyring created\n", filename );
	}
      #ifdef __MINGW32__
	/* must close it again */
	iobuf_close( iobuf );
	iobuf = NULL;
      #endif
	break;

    #ifdef HAVE_LIBGDBM
      case rt_GDBM:
	resource_table[i].dbf = gdbm_open( filename, 0,
					   force? GDBM_WRCREAT : GDBM_WRITER,
					   S_IRUSR | S_IWUSR |
					   S_IRGRP | S_IWGRP | S_IROTH,
					   fatal_gdbm_error );
	if( !resource_table[i].dbf ) {
	    log_error("%s: can't open gdbm file: %s\n",
			    filename, gdbm_strerror(gdbm_errno));
	    rc = G10ERR_OPEN_FILE;
	    goto leave;
	}
	break;
    #endif

      default:
	log_error("%s: unsupported resource type\n", url );
	rc = G10ERR_GENERAL;
	goto leave;
    }

    resource_table[i].used = 1;
    resource_table[i].secret = !!secret;
    resource_table[i].fname = m_strdup(filename);
    resource_table[i].iobuf = iobuf;
    resource_table[i].rt    = rt;
  leave:
    if( rc )
	log_error("keyblock resource '%s': %s\n", filename, g10_errstr(rc) );
    else if( secret )
	any_secret = 1;
    else
	any_public = 1;
    m_free( filename );
    return rc;
}

/****************
 * Return the resource name of the keyblock associated with KBPOS.
 */
const char *
keyblock_resource_name( KBPOS *kbpos )
{
    RESTBL *rentry;

    if( !(rentry = check_pos( kbpos )) || !rentry->fname )
	log_bug("no name for keyblock resource %d\n", kbpos->resno );
    return rentry->fname;
}


/****************
 * Get a keyblock handle KBPOS from a filename. This can be used
 * to get a handle for insert_keyblock for a new keyblock.
 * Using a filename of NULL returns the default resource
 */
int
get_keyblock_handle( const char *filename, int secret, KBPOS *kbpos )
{
    int i;

    for(i=0; i < MAX_RESOURCES; i++ )
	if( resource_table[i].used && !resource_table[i].secret == !secret ) {
	    /* fixme: dos needs case insensitive file compare */
	    if( !filename || !strcmp( resource_table[i].fname, filename ) ) {
		memset( kbpos, 0, sizeof *kbpos );
		kbpos->resno = i;
		kbpos->rt = resource_table[i].rt;
		return 0;
	    }
	}
    return -1; /* not found */
}



/****************
 * Search a keyblock which starts with the given packet and puts all
 * information into KBPOS, which can be used later to access this key block.
 * This function looks into all registered keyblock sources.
 * PACKET must be a packet with either a secret_key or a public_key
 *
 * This function is intended to check whether a given certificate
 * is already in a keyring or to prepare it for editing.
 *
 * Returns: 0 if found, -1 if not found or an errorcode.
 */
static int
search( PACKET *pkt, KBPOS *kbpos, int secret )
{
    int i, rc, last_rc=-1;

    for(i=0; i < MAX_RESOURCES; i++ ) {
	if( resource_table[i].used && !resource_table[i].secret == !secret ) {
	    switch( resource_table[i].rt ) {
	      case rt_RING:
		rc = keyring_search( pkt, kbpos, resource_table[i].iobuf,
						 resource_table[i].fname );
		break;
	     #ifdef HAVE_LIBGDBM
	      case rt_GDBM: {
		    PKT_public_key *req_pk = pkt->pkt.public_key;
		    byte fpr[20];
		    size_t fprlen;

		    fingerprint_from_pk( req_pk, fpr, &fprlen );
		    rc = do_gdbm_locate( resource_table[i].dbf,
					 kbpos, fpr, fprlen );
		}
		break;
	     #endif
	      default: BUG();
	    }

	    kbpos->rt = resource_table[i].rt;
	    if( !rc ) {
		kbpos->resno = i;
		kbpos->fp = NULL;
		return 0;
	    }
	    if( rc != -1 ) {
		log_error("error searching resource %d: %s\n",
						  i, g10_errstr(rc));
		last_rc = rc;
	    }
	}
    }
    return last_rc;
}


/****************
 * Combined function to search for a username and get the position
 * of the keyblock.
 */
int
find_keyblock_byname( KBPOS *kbpos, const char *username )
{
    PACKET pkt;
    PKT_public_key *pk = m_alloc_clear( sizeof *pk );
    int rc;

    rc = get_pubkey_byname( pk, username );
    if( rc ) {
	free_public_key(pk);
	return rc;
    }

    init_packet( &pkt );
    pkt.pkttype = PKT_PUBLIC_KEY;
    pkt.pkt.public_key = pk;
    rc = search( &pkt, kbpos, 0 );
    free_public_key(pk);
    return rc;
}


/****************
 * Combined function to search for a key and get the position
 * of the keyblock.
 */
int
find_keyblock_bypk( KBPOS *kbpos, PKT_public_key *pk )
{
    PACKET pkt;
    int rc;

    init_packet( &pkt );
    pkt.pkttype = PKT_PUBLIC_KEY;
    pkt.pkt.public_key = pk;
    rc = search( &pkt, kbpos, 0 );
    return rc;
}


/****************
 * Combined function to search for a username and get the position
 * of the keyblock. This function does not unprotect the secret key.
 */
int
find_secret_keyblock_byname( KBPOS *kbpos, const char *username )
{
    PACKET pkt;
    PKT_secret_key *sk = m_alloc_clear( sizeof *sk );
    int rc;

    rc = get_seckey_byname( sk, username, 0 );
    if( rc ) {
	free_secret_key(sk);
	return rc;
    }

    init_packet( &pkt );
    pkt.pkttype = PKT_SECRET_KEY;
    pkt.pkt.secret_key = sk;
    rc = search( &pkt, kbpos, 1 );
    free_secret_key(sk);
    return rc;
}


/****************
 * Locate a keyblock in a database which is capable of direct access
 * Put all information into KBPOS, which can be later be to access this
 * key block.
 * This function looks into all registered keyblock sources.
 *
 * Returns: 0 if found,
 *	    -1 if not found
 *	    G10ERR_UNSUPPORTED if no resource is able to handle this
 *	    or another errorcode.
 */
int
locate_keyblock_by_fpr( KBPOS *kbpos, const byte *fpr, int fprlen, int secret )
{
    RESTBL *rentry;
    int i, rc, any=0, last_rc=-1;


    for(i=0, rentry = resource_table; i < MAX_RESOURCES; i++, rentry++ ) {
	if( rentry->used && !rentry->secret == !secret ) {
	    kbpos->rt = rentry->rt;
	    switch( rentry->rt ) {
	      case rt_GDBM:
		any = 1;
		rc = do_gdbm_locate( rentry->dbf, kbpos, fpr, fprlen );
		break;
	      default:
		rc = G10ERR_UNSUPPORTED;
		break;
	    }

	    if( !rc ) {
		kbpos->resno = i;
		kbpos->fp = NULL;
		return 0;
	    }
	    else if( rc != -1 && rc != G10ERR_UNSUPPORTED ) {
		log_error("error searching resource %d: %s\n",
						  i, g10_errstr(rc));
		last_rc = rc;
	    }
	}
    }

    return (last_rc == -1 && !any)? G10ERR_UNSUPPORTED : last_rc;
}


int
locate_keyblock_by_keyid( KBPOS *kbpos, u32 *keyid, int shortkid, int secret )
{
    RESTBL *rentry;
    int i, rc, any=0, last_rc=-1;

    if( shortkid )
	return G10ERR_UNSUPPORTED;

    for(i=0, rentry = resource_table; i < MAX_RESOURCES; i++, rentry++ ) {
	if( rentry->used && !rentry->secret == !secret ) {
	    kbpos->rt = rentry->rt;
	    switch( rentry->rt ) {
	      case rt_GDBM:
		any = 1;
		rc = do_gdbm_locate_by_keyid( rentry->dbf, kbpos, keyid );
		break;
	      default:
		rc = G10ERR_UNSUPPORTED;
		break;
	    }

	    if( !rc ) {
		kbpos->resno = i;
		kbpos->fp = NULL;
		return 0;
	    }
	    else if( rc != -1 && rc != G10ERR_UNSUPPORTED ) {
		log_error("error searching resource %d: %s\n",
						  i, g10_errstr(rc));
		last_rc = rc;
	    }
	}
    }

    return (last_rc == -1 && !any)? G10ERR_UNSUPPORTED : last_rc;
}




/****************
 * Lock the keyblock; wait until it's available
 * This function may change the internal data in kbpos, in cases
 * when the keyblock to be locked has been modified.
 * fixme: remove this function and add an option to search()?
 */
int
lock_keyblock( KBPOS *kbpos )
{
    if( !check_pos(kbpos) )
	return G10ERR_GENERAL;
    return 0;
}

/****************
 * Release a lock on a keyblock
 */
void
unlock_keyblock( KBPOS *kbpos )
{
    if( !check_pos(kbpos) )
	BUG();
}

/****************
 * Read a complete keyblock and return the root in ret_root.
 */
int
read_keyblock( KBPOS *kbpos, KBNODE *ret_root )
{
    if( !check_pos(kbpos) )
	return G10ERR_GENERAL;

    switch( kbpos->rt ) {
      case rt_RING:
	return keyring_read( kbpos, ret_root );
     #ifdef HAVE_LIBGDBM
      case rt_GDBM:
	return do_gdbm_read( kbpos, ret_root );
     #endif
      default: BUG();
    }
}


/****************
 * This functions can be used to read through a complete keyring.
 * Mode is: 0 = open
 *	    1 = read
 *	    2 = close
 *	    5 = open secret keyrings
 *	    11 = read but skip signature and comment packets.
 *	    all others are reserved!
 * Note that you do not need a search prior to this function,
 * only a handle is needed.
 * NOTE: It is not allowed to do an insert/update/delete with this
 *	 keyblock, if you want to do this, use search/read!
 */
int
enum_keyblocks( int mode, KBPOS *kbpos, KBNODE *ret_root )
{
    int rc = 0;
    RESTBL *rentry;

    if( !mode || mode == 5 || mode == 100 ) {
	int i;
	kbpos->fp = NULL;
	if( !mode ) {
	    kbpos->secret = 0;
	    i = 0;
	}
	else if( mode == 5 ) {
	    kbpos->secret = 1;
	    mode = 0;
	    i = 0;
	}
	else
	    i = kbpos->resno+1;
	for(; i < MAX_RESOURCES; i++ )
	    if( resource_table[i].used
		&& !resource_table[i].secret == !kbpos->secret )
		break;
	if( i == MAX_RESOURCES )
	    return -1; /* no resources */
	kbpos->resno = i;
	rentry = check_pos( kbpos );
	kbpos->rt = resource_table[i].rt;
	switch( kbpos->rt ) {
	  case rt_RING:
	    kbpos->fp = iobuf_fopen( rentry->fname, "rb" );
	    if( !kbpos->fp ) {
		log_error("can't open '%s'\n", rentry->fname );
		return G10ERR_OPEN_FILE;
	    }
	    break;
	 #ifdef HAVE_LIBGDBM
	  case rt_GDBM:
	    /* FIXME: make sure that there is only one enum at a time */
	    kbpos->offset = 0;
	    break;
	 #endif
	  default: BUG();
	}
	kbpos->pkt = NULL;
    }
    else if( mode == 1 || mode == 11 ) {
	int cont;
	do {
	    cont = 0;
	    switch( kbpos->rt ) {
	      case rt_RING:
		if( !kbpos->fp )
		    return G10ERR_GENERAL;
		rc = keyring_enum( kbpos, ret_root, mode == 11 );
		break;
	     #ifdef HAVE_LIBGDBM
	      case rt_GDBM:
		rc = do_gdbm_enum( kbpos, ret_root );
		break;
	     #endif
	      default: BUG();
	    }

	    if( rc == -1 ) {
		assert( !kbpos->pkt );
		rentry = check_pos( kbpos );
		assert(rentry);
		/* close */
		enum_keyblocks(2, kbpos, ret_root );
		/* and open the next one */
		rc = enum_keyblocks(100, kbpos, ret_root );
		if( !rc )
		    cont = 1;
	    }
	} while(cont);
    }
    else {
	switch( kbpos->rt ) {
	  case rt_RING:
	    if( kbpos->fp ) {
		iobuf_close( kbpos->fp );
		kbpos->fp = NULL;
	    }
	    break;
	  case rt_GDBM:
	    break;
	  default:
	    log_error("OOPS in close enum_keyblocks - ignored\n");
	    return rc;
	    break;
	}
	/* release pending packet */
	free_packet( kbpos->pkt );
	m_free( kbpos->pkt );
    }
    return rc;
}




/****************
 * Insert the keyblock described by ROOT into the keyring described
 * by KBPOS.  This actually appends the data to the keyfile.
 */
int
insert_keyblock( KBPOS *kbpos, KBNODE root )
{
    int rc;

    if( !check_pos(kbpos) )
	return G10ERR_GENERAL;

    switch( kbpos->rt ) {
      case rt_RING:
	rc = keyring_copy( kbpos, 1, root );
	break;
     #ifdef HAVE_LIBGDBM
      case rt_GDBM:
	rc = do_gdbm_store( kbpos, root, 0 );
	break;
     #endif
      default: BUG();
    }

    return rc;
}

/****************
 * Delete the keyblock described by KBPOS.
 * The current code simply changes the keyblock in the keyring
 * to packet of type 0 with the correct length.  To help detect errors,
 * zero bytes are written.
 */
int
delete_keyblock( KBPOS *kbpos )
{
    int rc;

    if( !check_pos(kbpos) )
	return G10ERR_GENERAL;

    switch( kbpos->rt ) {
      case rt_RING:
	rc = keyring_copy( kbpos, 2, NULL );
	break;
     #ifdef HAVE_LIBGDBM
      case rt_GDBM:
	log_debug("deleting gdbm keyblock is not yet implemented\n");
	rc = 0;
	break;
     #endif
      default: BUG();
    }

    return rc;
}


/****************
 * Update the keyblock at KBPOS with the one in ROOT.
 */
int
update_keyblock( KBPOS *kbpos, KBNODE root )
{
    int rc;

    if( !check_pos(kbpos) )
	return G10ERR_GENERAL;

    switch( kbpos->rt ) {
      case rt_RING:
	rc = keyring_copy( kbpos, 3, root );
	break;
     #ifdef HAVE_LIBGDBM
      case rt_GDBM:
	rc = do_gdbm_store( kbpos, root, 1 );
	break;
     #endif
      default: BUG();
    }

    return rc;
}



/****************************************************************
 ********** Functions which operates on regular keyrings ********
 ****************************************************************/

static int
cmp_seckey( PKT_secret_key *req_sk, PKT_secret_key *sk )
{
    int n,i;

    assert( req_sk->pubkey_algo == sk->pubkey_algo );

    n = pubkey_get_nskey( req_sk->pubkey_algo );
    for(i=0; i < n; i++ ) {
	if( mpi_cmp( req_sk->skey[i], sk->skey[i] ) )
	    return -1;
    }
    return 0;
}

static int
cmp_pubkey( PKT_public_key *req_pk, PKT_public_key *pk )
{
    int n, i;

    assert( req_pk->pubkey_algo == pk->pubkey_algo );

    n = pubkey_get_npkey( req_pk->pubkey_algo );
    for(i=0; i < n; i++ ) {
	if( mpi_cmp( req_pk->pkey[i], pk->pkey[i] )  )
	    return -1;
    }
    return 0;
}

/****************
 * search one keyring, return 0 if found, -1 if not found or an errorcode.
 */
static int
keyring_search( PACKET *req, KBPOS *kbpos, IOBUF iobuf, const char *fname )
{
    int rc;
    PACKET pkt;
    int save_mode;
    ulong offset;
    int pkttype = req->pkttype;
    PKT_public_key *req_pk = req->pkt.public_key;
    PKT_secret_key *req_sk = req->pkt.secret_key;

    init_packet(&pkt);
    save_mode = set_packet_list_mode(0);
    kbpos->rt = rt_RING;

  #if __MINGW32__
    assert(!iobuf);
    iobuf = iobuf_open( fname );
    if( !iobuf ) {
	log_error("%s: can't open keyring file\n", fname);
	rc = G10ERR_KEYRING_OPEN;
	goto leave;
    }
  #else
    if( iobuf_seek( iobuf, 0 ) ) {
	log_error("can't rewind keyring file\n");
	rc = G10ERR_KEYRING_OPEN;
	goto leave;
    }
  #endif

    while( !(rc=search_packet(iobuf, &pkt, pkttype, &offset)) ) {
	if( pkt.pkttype == PKT_SECRET_KEY ) {
	    PKT_secret_key *sk = pkt.pkt.secret_key;

	    if(   req_sk->timestamp == sk->timestamp
	       && req_sk->pubkey_algo == sk->pubkey_algo
	       && !cmp_seckey( req_sk, sk) )
		break; /* found */
	}
	else if( pkt.pkttype == PKT_PUBLIC_KEY ) {
	    PKT_public_key *pk = pkt.pkt.public_key;

	    if(   req_pk->timestamp == pk->timestamp
	       && req_pk->pubkey_algo == pk->pubkey_algo
	       && !cmp_pubkey( req_pk, pk ) )
		break; /* found */
	}
	else
	    BUG();
	free_packet(&pkt);
    }
    if( !rc )
	kbpos->offset = offset;

  leave:
    free_packet(&pkt);
    set_packet_list_mode(save_mode);
  #if __MINGW32__
    iobuf_close(iobuf);
  #endif
    return rc;
}


static int
keyring_read( KBPOS *kbpos, KBNODE *ret_root )
{
    PACKET *pkt;
    int rc;
    RESTBL *rentry;
    KBNODE root = NULL;
    IOBUF a;
    int in_cert = 0;

    if( !(rentry=check_pos(kbpos)) )
	return G10ERR_GENERAL;

    a = iobuf_fopen( rentry->fname, "rb" );
    if( !a ) {
	log_error("can't open '%s'\n", rentry->fname );
	return G10ERR_OPEN_FILE;
    }

    if( iobuf_seek( a, kbpos->offset ) ) {
	log_error("can't seek to %lu\n", kbpos->offset);
	iobuf_close(a);
	return G10ERR_KEYRING_OPEN;
    }

    pkt = m_alloc( sizeof *pkt );
    init_packet(pkt);
    kbpos->count=0;
    while( (rc=parse_packet(a, pkt)) != -1 ) {
	if( rc ) {  /* ignore errors */
	    if( rc != G10ERR_UNKNOWN_PACKET ) {
		log_error("read_keyblock: read error: %s\n", g10_errstr(rc) );
		rc = G10ERR_INV_KEYRING;
		goto ready;
	    }
	    kbpos->count++;
	    free_packet( pkt );
	    init_packet( pkt );
	    continue;
	}
	/* make a linked list of all packets */
	switch( pkt->pkttype ) {
	  case PKT_PUBLIC_KEY:
	  case PKT_SECRET_KEY:
	    if( in_cert )
		goto ready;
	    in_cert = 1;
	  default:
	    kbpos->count++;
	    if( !root )
		root = new_kbnode( pkt );
	    else
		add_kbnode( root, new_kbnode( pkt ) );
	    pkt = m_alloc( sizeof *pkt );
	    init_packet(pkt);
	    break;
	}
    }
  ready:
    if( rc == -1 && root )
	rc = 0;

    if( rc )
	release_kbnode( root );
    else
	*ret_root = root;
    free_packet( pkt );
    m_free( pkt );
    iobuf_close(a);
    return rc;
}


static int
keyring_enum( KBPOS *kbpos, KBNODE *ret_root, int skipsigs )
{
    PACKET *pkt;
    int rc;
    RESTBL *rentry;
    KBNODE root = NULL;

    if( !(rentry=check_pos(kbpos)) )
	return G10ERR_GENERAL;

    if( kbpos->pkt ) {
	root = new_kbnode( kbpos->pkt );
	kbpos->pkt = NULL;
    }

    pkt = m_alloc( sizeof *pkt );
    init_packet(pkt);
    while( (rc=parse_packet(kbpos->fp, pkt)) != -1 ) {
	if( rc ) {  /* ignore errors */
	    if( rc != G10ERR_UNKNOWN_PACKET ) {
		log_error("read_keyblock: read error: %s\n", g10_errstr(rc) );
		rc = G10ERR_INV_KEYRING;
		goto ready;
	    }
	    free_packet( pkt );
	    init_packet( pkt );
	    continue;
	}
	/* make a linked list of all packets */
	switch( pkt->pkttype ) {
	  case PKT_PUBLIC_KEY:
	  case PKT_SECRET_KEY:
	    if( root ) { /* store this packet */
		kbpos->pkt = pkt;
		pkt = NULL;
		goto ready;
	    }
	    root = new_kbnode( pkt );
	    pkt = m_alloc( sizeof *pkt );
	    init_packet(pkt);
	    break;

	  default:
	    /* skip pakets at the beginning of a keyring, until we find
	     * a start packet; issue a warning if it is not a comment */
	    if( !root && pkt->pkttype != PKT_COMMENT
		      && pkt->pkttype != PKT_OLD_COMMENT ) {
		log_info("keyring_enum: skipped packet of type %d\n",
			    pkt->pkttype );
		break;
	    }
	    if( !root || (skipsigs && ( pkt->pkttype == PKT_SIGNATURE
				      ||pkt->pkttype == PKT_COMMENT
				      ||pkt->pkttype == PKT_OLD_COMMENT )) ) {
		init_packet(pkt);
		break;
	    }
	    add_kbnode( root, new_kbnode( pkt ) );
	    pkt = m_alloc( sizeof *pkt );
	    init_packet(pkt);
	    break;
	}
    }
  ready:
    if( rc == -1 && root )
	rc = 0;

    if( rc )
	release_kbnode( root );
    else
	*ret_root = root;
    free_packet( pkt );
    m_free( pkt );

    return rc;
}


/****************
 * Perform insert/delete/update operation.
 * mode 1 = insert
 *	2 = delete
 *	3 = update
 */
static int
keyring_copy( KBPOS *kbpos, int mode, KBNODE root )
{
    RESTBL *rentry;
    IOBUF fp, newfp;
    int rc=0;
    char *bakfname = NULL;
    char *tmpfname = NULL;

    if( !(rentry = check_pos( kbpos )) )
	return G10ERR_GENERAL;
    if( kbpos->fp )
	BUG(); /* not allowed with such a handle */

    /* open the source file */
    fp = iobuf_fopen( rentry->fname, "rb" );
    if( mode == 1 && !fp && errno == ENOENT ) { /* no file yet */
	KBNODE kbctx, node;

	/* insert: create a new file */
	newfp = iobuf_create( rentry->fname );
	if( !newfp ) {
	    log_error("%s: can't create: %s\n", rentry->fname, strerror(errno));
	    return G10ERR_OPEN_FILE;
	}
	else
	    log_info("%s: keyring created\n", rentry->fname );

	kbctx=NULL;
	while( (node = walk_kbnode( root, &kbctx, 0 )) ) {
	    if( (rc = build_packet( newfp, node->pkt )) ) {
		log_error("build_packet(%d) failed: %s\n",
			    node->pkt->pkttype, g10_errstr(rc) );
		iobuf_cancel(newfp);
		return G10ERR_WRITE_FILE;
	    }
	}
	if( iobuf_close(newfp) ) {
	    log_error("%s: close failed: %s\n", rentry->fname, strerror(errno));
	    return G10ERR_CLOSE_FILE;
	}
	if( chmod( rentry->fname, S_IRUSR | S_IWUSR ) ) {
	    log_error("%s: chmod failed: %s\n",
				    rentry->fname, strerror(errno) );
	    return G10ERR_WRITE_FILE;
	}
	return 0;
    }
    if( !fp ) {
	log_error("%s: can't open: %s\n", rentry->fname, strerror(errno) );
	rc = G10ERR_OPEN_FILE;
	goto leave;
    }

    /* create the new file */
  #ifdef __MINGW32__
    /* Here is another Windoze bug?:
     * you cant rename("pubring.gpg.tmp", "pubring.gpg");
     * but	rename("pubring.gpg.tmp", "pubring.aaa");
     * works.  So we replace .gpg by .bak or .tmp
     */
    if( strlen(rentry->fname) > 4
	&& !strcmp(rentry->fname+strlen(rentry->fname)-4, ".gpg") ) {
	bakfname = m_alloc( strlen( rentry->fname ) + 1 );
	strcpy(bakfname,rentry->fname);
	strcpy(bakfname+strlen(rentry->fname)-4, ".bak");
	tmpfname = m_alloc( strlen( rentry->fname ) + 1 );
	strcpy(tmpfname,rentry->fname);
	strcpy(tmpfname+strlen(rentry->fname)-4, ".tmp");
    }
    else { /* file does not end with gpg; hmmm */
	bakfname = m_alloc( strlen( rentry->fname ) + 5 );
	strcpy(stpcpy(bakfname,rentry->fname),".bak");
	tmpfname = m_alloc( strlen( rentry->fname ) + 5 );
	strcpy(stpcpy(tmpfname,rentry->fname),".tmp");
    }
  #else
    bakfname = m_alloc( strlen( rentry->fname ) + 2 );
    strcpy(stpcpy(bakfname,rentry->fname),"~");
    tmpfname = m_alloc( strlen( rentry->fname ) + 5 );
    strcpy(stpcpy(tmpfname,rentry->fname),".tmp");
  #endif
    newfp = iobuf_create( tmpfname );
    if( !newfp ) {
	log_error("%s: can't create: %s\n", tmpfname, strerror(errno) );
	iobuf_close(fp);
	rc = G10ERR_OPEN_FILE;
	goto leave;
    }

    if( mode == 1 ) { /* insert */
	/* copy everything to the new file */
	rc = copy_all_packets( fp, newfp );
	if( rc != -1 ) {
	    log_error("%s: copy to %s failed: %s\n",
		      rentry->fname, tmpfname, g10_errstr(rc) );
	    iobuf_close(fp);
	    iobuf_cancel(newfp);
	    goto leave;
	}
	rc = 0;
    }

    if( mode == 2 || mode == 3 ) { /* delete or update */
	/* copy first part to the new file */
	rc = copy_some_packets( fp, newfp, kbpos->offset );
	if( rc ) { /* should never get EOF here */
	    log_error("%s: copy to %s failed: %s\n",
		      rentry->fname, tmpfname, g10_errstr(rc) );
	    iobuf_close(fp);
	    iobuf_cancel(newfp);
	    goto leave;
	}
	/* skip this keyblock */
	assert( kbpos->count );
	rc = skip_some_packets( fp, kbpos->count );
	if( rc ) {
	    log_error("%s: skipping %u packets failed: %s\n",
			    rentry->fname, kbpos->count, g10_errstr(rc));
	    iobuf_close(fp);
	    iobuf_cancel(newfp);
	    goto leave;
	}
    }

    if( mode == 1 || mode == 3 ) { /* insert or update */
	KBNODE kbctx, node;

	/* append the new data */
	kbctx=NULL;
	while( (node = walk_kbnode( root, &kbctx, 0 )) ) {
	    if( (rc = build_packet( newfp, node->pkt )) ) {
		log_error("build_packet(%d) failed: %s\n",
			    node->pkt->pkttype, g10_errstr(rc) );
		iobuf_close(fp);
		iobuf_cancel(newfp);
		rc = G10ERR_WRITE_FILE;
		goto leave;
	    }
	}
    }

    if( mode == 2 || mode == 3 ) { /* delete or update */
	/* copy the rest */
	rc = copy_all_packets( fp, newfp );
	if( rc != -1 ) {
	    log_error("%s: copy to %s failed: %s\n",
		      rentry->fname, tmpfname, g10_errstr(rc) );
	    iobuf_close(fp);
	    iobuf_cancel(newfp);
	    goto leave;
	}
	rc = 0;
    }

    /* close both files */
    if( iobuf_close(fp) ) {
	log_error("%s: close failed: %s\n", rentry->fname, strerror(errno) );
	rc = G10ERR_CLOSE_FILE;
	goto leave;
    }
    if( iobuf_close(newfp) ) {
	log_error("%s: close failed: %s\n", tmpfname, strerror(errno) );
	rc = G10ERR_CLOSE_FILE;
	goto leave;
    }
    /* if the new file is a secring, restrict the permissions */
  #ifndef __MINGW32__
    if( rentry->secret ) {
	if( chmod( tmpfname, S_IRUSR | S_IWUSR ) ) {
	    log_error("%s: chmod failed: %s\n",
				    tmpfname, strerror(errno) );
	    rc = G10ERR_WRITE_FILE;
	    goto leave;
	}
    }
  #endif

    /* rename and make backup file */
    if( !rentry->secret ) {  /* but not for secret keyrings */
      #ifdef __MINGW32__
	remove( bakfname );
      #endif
	if( rename( rentry->fname, bakfname ) ) {
	    log_error("%s: rename to %s failed: %s\n",
				    rentry->fname, bakfname, strerror(errno) );
	    rc = G10ERR_RENAME_FILE;
	    goto leave;
	}
    }
  #ifdef __MINGW32__
    remove( rentry->fname );
  #endif
    if( rename( tmpfname, rentry->fname ) ) {
	log_error("%s: rename to %s failed: %s\n",
			    tmpfname, rentry->fname,strerror(errno) );
	rc = G10ERR_RENAME_FILE;
	if( rentry->secret ) {
	    log_info(_(
		"Warning: 2 files with confidential information exists.\n"));
	    log_info(_("%s is the unchanged one\n"), rentry->fname );
	    log_info(_("%s is the new one\n"), tmpfname );
	    log_info(_("Please fix this possible security flaw\n"));
	}
	goto leave;
    }

  leave:
    m_free(bakfname);
    m_free(tmpfname);
    return rc;
}


#ifdef HAVE_LIBGDBM
/****************************************************************
 ********** Functions which operates on GDM files ***************
 ****************************************************************/

#if MAX_FINGERPRINT_LEN > 20
  #error A GDBM keyring assumes that fingerprints are less than 21
#endif

/****************
 * Insert the keyblock into the GDBM database
 */

static int
do_gdbm_store( KBPOS *kbpos, KBNODE root, int update )
{
    RESTBL *rentry;
    PKT_public_key *pk;
    KBNODE kbctx, node;
    IOBUF fp = NULL;
    byte fpr[20];
    byte contbuf[21];
    byte keybuf[21];
    size_t fprlen;
    datum key, content;
    int i, rc;

    if( !(rentry = check_pos( kbpos )) )
	return G10ERR_GENERAL;

    /* construct the fingerprint which is used as the primary key */
    node = find_kbnode( root, PKT_PUBLIC_KEY );
    if( !node )
	log_bug("a gdbm database can't store secret keys\n");
    pk = node->pkt->pkt.public_key;

    fingerprint_from_pk( pk, fpr, &fprlen );
    for(i=fprlen; i < DIM(fpr); i++ )
	fpr[i] = 0;

    /* build the keyblock */
    kbctx=NULL;
    fp = iobuf_temp();
    iobuf_put( fp, 1 ); /* data is a keyblock */
    while( (node = walk_kbnode( root, &kbctx, 0 )) ) {
	if( (rc = build_packet( fp, node->pkt )) ) {
	    log_error("build_packet(%d) failed: %s\n",
			node->pkt->pkttype, g10_errstr(rc) );
	    rc = G10ERR_WRITE_FILE;
	    goto leave;
	}
    }
    /* store data and key */
    *keybuf = 1;   /* key is a padded fingerprint */
    memcpy(keybuf+1, fpr, 20 );
    key.dptr  = keybuf;
    key.dsize = 21;
    content.dptr  = iobuf_get_temp_buffer( fp );
    content.dsize = iobuf_get_temp_length( fp );
    rc = gdbm_store( rentry->dbf, key, content,
				  update? GDBM_REPLACE : GDBM_INSERT );
    if( rc ) {
	log_error("%s: gdbm_store failed: %s\n", rentry->fname,
			    rc == 1 ? "already stored"
				    : gdbm_strerror(gdbm_errno) );
	rc = G10ERR_WRITE_FILE;
	goto leave;
    }
    /* now store all keyids */
    *contbuf = 2;  /* data is a list of fingerprints */
    memcpy(contbuf+1, fpr, 20 );
    content.dptr = contbuf;
    content.dsize= 21;
    kbctx=NULL;
    while( (node = walk_kbnode( root, &kbctx, 0 )) ) {
	if(    node->pkt->pkttype == PKT_PUBLIC_KEY
	    || node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
	    u32 aki[2];

	    keyid_from_pk( node->pkt->pkt.public_key, aki );
	    *keybuf = 2; /* key is a 8 byte keyid */
	    u32tobuf( keybuf+1	, aki[0] );
	    u32tobuf( keybuf+5, aki[1] );
	    key.dptr = keybuf;
	    key.dsize= 9;
	    /* fixme: must be more clever when a insert failed:
	     *	      build a list of fingerprints in this case */
	    rc = gdbm_store( rentry->dbf, key, content,
					  update? GDBM_REPLACE : GDBM_INSERT );
	    if( rc ) {
		log_info("%s: gdbm_store keyid failed: %s\n", rentry->fname,
				    rc == 1 ? "already stored"
					    : gdbm_strerror(gdbm_errno) );
		rc = 0;
	    }
	}
    }

  leave:
    iobuf_close(fp); /* don't need a cancel because it is a temp iobuf */
    return rc;
}



/****************
 * search one keybox, return 0 if found, -1 if not found or an errorcode.
 */
static int
do_gdbm_locate( GDBM_FILE dbf, KBPOS *kbpos, const byte *fpr, int fprlen )
{
    byte *keybuf = kbpos->keybuf;
    datum key;
    int i;

    *keybuf = 1;
    for(i=0; i < fprlen; i++ )
	keybuf[i+1] = fpr[i];
    for(; i < 20; i++ )
	keybuf[i+1] = 0;

    /* fetch the data */
    key.dptr  = keybuf;
    key.dsize = 21;
    if( !gdbm_exists( dbf, key ) )
	return -1; /* not found */
    return 0;
}

/****************
 * locate by keyid.
 * FIXME: we must have a way to enumerate thru the list opf fingerprints
 */
static int
do_gdbm_locate_by_keyid( GDBM_FILE dbf, KBPOS *kbpos, u32 *keyid )
{
    byte keybuf[9];
    datum key, content;
    int rc;

    /* construct the fingerprint which is used as the primary key */
    *keybuf = 2;
    u32tobuf( keybuf+1, keyid[0] );
    u32tobuf( keybuf+5, keyid[1] );

    /* fetch the data */
    key.dptr  = keybuf;
    key.dsize = 9;
    content = gdbm_fetch( dbf, key );
    if( !content.dptr )
	return -1;

    if( content.dsize < 2 ) {
	log_error("gdbm_fetch did not return enough data\n" );
	free( content.dptr ); /* can't use m_free() here */
	return G10ERR_INV_KEYRING;
    }
    if( *content.dptr != 2 ) {
	log_error("gdbm_fetch returned unexpected type %d\n",
		    *(byte*)content.dptr );
	free( content.dptr ); /* can't use m_free() here */
	return G10ERR_INV_KEYRING;
    }
    if( content.dsize < 21 ) {
	log_error("gdbm_fetch did not return a complete fingerprint\n" );
	free( content.dptr ); /* can't use m_free() here */
	return G10ERR_INV_KEYRING;
    }
    if( content.dsize > 21 )
	log_info("gdbm_fetch: warning: more than one fingerprint\n" );

    rc = do_gdbm_locate( dbf, kbpos, content.dptr+1, 20 );
    free( content.dptr ); /* can't use m_free() here */
    return rc;
}



static int
do_gdbm_read( KBPOS *kbpos, KBNODE *ret_root )
{
    PACKET *pkt;
    int rc;
    RESTBL *rentry;
    KBNODE root = NULL;
    IOBUF a;
    datum key, content;

    if( !(rentry=check_pos(kbpos)) )
	return G10ERR_GENERAL;

    key.dptr  = kbpos->keybuf;
    key.dsize = 21;
    content = gdbm_fetch( rentry->dbf, key );
    if( !content.dptr ) {
	log_error("gdbm_fetch failed: %s\n", gdbm_strerror(gdbm_errno) );
	return G10ERR_INV_KEYRING;
    }
    if( content.dsize < 2 ) {
	log_error("gdbm_fetch did not return enough data\n" );
	free( content.dptr ); /* can't use m_free() here */
	return G10ERR_INV_KEYRING;
    }
    if( *content.dptr != 1 ) {
	log_error("gdbm_fetch returned unexpected type %d\n",
		    *(byte*)content.dptr );
	free( content.dptr ); /* can't use m_free() here */
	return G10ERR_INV_KEYRING;
    }

    a = iobuf_temp_with_content( content.dptr+1, content.dsize-1 );
    free( content.dptr ); /* can't use m_free() here */

    pkt = m_alloc( sizeof *pkt );
    init_packet(pkt);
    kbpos->count=0;
    while( (rc=parse_packet(a, pkt)) != -1 ) {
	if( rc ) {  /* ignore errors */
	    if( rc != G10ERR_UNKNOWN_PACKET ) {
		log_error("read_keyblock: read error: %s\n", g10_errstr(rc) );
		rc = G10ERR_INV_KEYRING;
		break;
	    }
	    kbpos->count++;
	    free_packet( pkt );
	    init_packet( pkt );
	    continue;
	}
	/* make a linked list of all packets */
	kbpos->count++;
	if( !root )
	    root = new_kbnode( pkt );
	else
	    add_kbnode( root, new_kbnode( pkt ) );
	pkt = m_alloc( sizeof *pkt );
	init_packet(pkt);
    }
    if( rc == -1 && root )
	rc = 0;
    if( rc )
	release_kbnode( root );
    else
	*ret_root = root;
    free_packet( pkt );
    m_free( pkt );
    iobuf_close(a);
    return rc;
}


/****************
 * Enum over keyblok data
 */
static int
do_gdbm_enum( KBPOS *kbpos, KBNODE *ret_root )
{
    RESTBL *rentry;
    datum key, helpkey;

    if( !(rentry=check_pos(kbpos)) )
	return G10ERR_GENERAL;

    if( !kbpos->offset ) {
	kbpos->offset = 1;
	key = gdbm_firstkey( rentry->dbf );
    }
    else {
	helpkey.dptr = kbpos->keybuf;
	helpkey.dsize= 21;
	key = gdbm_nextkey( rentry->dbf, helpkey );
    }
    while( key.dptr && (!key.dsize || *key.dptr != 1) ) {
	helpkey = key;
	key = gdbm_nextkey( rentry->dbf, helpkey );
	free( helpkey.dptr ); /* free and not m_free() ! */
    }
    if( !key.dptr )
	return -1; /* eof */

    if( key.dsize < 21 ) {
	free( key.dptr ); /* free and not m_free() ! */
	log_error("do_gdm_enum: key is too short\n" );
	return G10ERR_INV_KEYRING;
    }
    memcpy( kbpos->keybuf, key.dptr, 21 );
    free( key.dptr ); /* free and not m_free() ! */
    return do_gdbm_read( kbpos, ret_root );
}

#endif /*HAVE_LIBGDBM*/