See ChangeLog: Thu Mar 11 16:39:46 CET 1999 Werner Koch
[gnupg.git] / g10 / tdbio.c
1 /* tdbio.c
2  *      Copyright (C) 1998 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #include "errors.h"
33 #include "iobuf.h"
34 #include "memory.h"
35 #include "util.h"
36 #include "options.h"
37 #include "main.h"
38 #include "i18n.h"
39 #include "trustdb.h"
40 #include "tdbio.h"
41
42
43 /****************
44  * Yes, this is a very simple implementation. We should really
45  * use a page aligned buffer and read complete pages.
46  * To implement a simple trannsaction system, this is sufficient.
47  */
48 typedef struct cache_ctrl_struct *CACHE_CTRL;
49 struct cache_ctrl_struct {
50     CACHE_CTRL next;
51     struct {
52         unsigned used:1;
53         unsigned dirty:1;
54     } flags;
55     ulong recno;
56     char data[TRUST_RECORD_LEN];
57 };
58
59 #define MAX_CACHE_ENTRIES_SOFT  200   /* may be increased while in a */
60 #define MAX_CACHE_ENTRIES_HARD  1000  /* transaction to this one */
61 static CACHE_CTRL cache_list;
62 static int cache_entries;
63 static int cache_is_dirty;
64
65 /* a type used to pass infomation to cmp_krec_fpr */
66 struct cmp_krec_fpr_struct {
67     int pubkey_algo;
68     const char *fpr;
69     int fprlen;
70 };
71
72 /* a type used to pass infomation to cmp_sdir */
73 struct cmp_sdir_struct {
74     int pubkey_algo;
75     u32 keyid[2];
76 };
77
78
79 static char *db_name;
80 static DOTLOCK lockhandle;
81 static int is_locked;
82 static int  db_fd = -1;
83 static int in_transaction;
84
85 static void open_db(void);
86
87 \f
88 /*************************************
89  ************* record cache **********
90  *************************************/
91
92 /****************
93  * Get the data from therecord cache and return a
94  * pointer into that cache.  Caller should copy
95  * the return data.  NULL is returned on a cache miss.
96  */
97 static const char *
98 get_record_from_cache( ulong recno )
99 {
100     CACHE_CTRL r;
101
102     for( r = cache_list; r; r = r->next ) {
103         if( r->flags.used && r->recno == recno )
104             return r->data;
105     }
106     return NULL;
107 }
108
109
110 static int
111 write_cache_item( CACHE_CTRL r )
112 {
113     int n;
114
115     if( lseek( db_fd, r->recno * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
116         log_error(_("trustdb rec %lu: lseek failed: %s\n"),
117                                             r->recno, strerror(errno) );
118         return G10ERR_WRITE_FILE;
119     }
120     n = write( db_fd, r->data, TRUST_RECORD_LEN);
121     if( n != TRUST_RECORD_LEN ) {
122         log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
123                                             r->recno, n, strerror(errno) );
124         return G10ERR_WRITE_FILE;
125     }
126     r->flags.dirty = 0;
127     return 0;
128 }
129
130 /****************
131  * Put data into the cache.  This function may flush the
132  * some cache entries if there is not enough space available.
133  */
134 int
135 put_record_into_cache( ulong recno, const char *data )
136 {
137     CACHE_CTRL r, unused;
138     int dirty_count = 0;
139     int clean_count = 0;
140
141     /* see whether we already cached this one */
142     for( unused = NULL, r = cache_list; r; r = r->next ) {
143         if( !r->flags.used ) {
144             if( !unused )
145                 unused = r;
146         }
147         else if( r->recno == recno ) {
148             if( !r->flags.dirty ) {
149                 /* Hmmm: should we use a a copy and compare? */
150                 if( memcmp(r->data, data, TRUST_RECORD_LEN ) ) {
151                     r->flags.dirty = 1;
152                     cache_is_dirty = 1;
153                 }
154             }
155             memcpy( r->data, data, TRUST_RECORD_LEN );
156             return 0;
157         }
158         if( r->flags.used ) {
159             if( r->flags.dirty )
160                 dirty_count++;
161             else
162                 clean_count++;
163         }
164     }
165     /* not in the cache: add a new entry */
166     if( unused ) { /* reuse this entry */
167         r = unused;
168         r->flags.used = 1;
169         r->recno = recno;
170         memcpy( r->data, data, TRUST_RECORD_LEN );
171         r->flags.dirty = 1;
172         cache_is_dirty = 1;
173         cache_entries++;
174         return 0;
175     }
176     /* see whether we reached the limit */
177     if( cache_entries < MAX_CACHE_ENTRIES_SOFT ) { /* no */
178         r = m_alloc( sizeof *r );
179         r->flags.used = 1;
180         r->recno = recno;
181         memcpy( r->data, data, TRUST_RECORD_LEN );
182         r->flags.dirty = 1;
183         r->next = cache_list;
184         cache_list = r;
185         cache_is_dirty = 1;
186         cache_entries++;
187         return 0;
188     }
189     /* cache is full: discard some clean entries */
190     if( clean_count ) {
191         int n = clean_count / 3; /* discard a third of the clean entries */
192         if( !n )
193             n = 1;
194         for( unused = NULL, r = cache_list; r; r = r->next ) {
195             if( r->flags.used && !r->flags.dirty ) {
196                 if( !unused )
197                     unused = r;
198                 r->flags.used = 0;
199                 cache_entries--;
200                 if( !--n )
201                     break;
202             }
203         }
204         assert( unused );
205         r = unused;
206         r->flags.used = 1;
207         r->recno = recno;
208         memcpy( r->data, data, TRUST_RECORD_LEN );
209         r->flags.dirty = 1;
210         cache_is_dirty = 1;
211         cache_entries++;
212         return 0;
213     }
214     /* no clean entries: have to flush some dirty entries */
215     if( in_transaction ) {
216         /* but we can't do this while in a transaction
217          * we increase the cache size instead */
218         if( cache_entries < MAX_CACHE_ENTRIES_HARD ) { /* no */
219             if( opt.debug && !(cache_entries % 100) )
220                 log_debug("increasing tdbio cache size\n");
221             r = m_alloc( sizeof *r );
222             r->flags.used = 1;
223             r->recno = recno;
224             memcpy( r->data, data, TRUST_RECORD_LEN );
225             r->flags.dirty = 1;
226             r->next = cache_list;
227             cache_list = r;
228             cache_is_dirty = 1;
229             cache_entries++;
230             return 0;
231         }
232         log_info(_("trustdb transaction too large\n"));
233         return G10ERR_RESOURCE_LIMIT;
234     }
235     if( dirty_count ) {
236         int n = dirty_count / 5; /* discard some dirty entries */
237         if( !n )
238             n = 1;
239         if( !is_locked ) {
240             if( make_dotlock( lockhandle, -1 ) )
241                 log_fatal("can't acquire lock - giving up\n");
242             else
243                 is_locked = 1;
244         }
245         for( unused = NULL, r = cache_list; r; r = r->next ) {
246             if( r->flags.used && r->flags.dirty ) {
247                 int rc = write_cache_item( r );
248                 if( rc )
249                     return rc;
250                 if( !unused )
251                     unused = r;
252                 r->flags.used = 0;
253                 cache_entries--;
254                 if( !--n )
255                     break;
256             }
257         }
258         if( !opt.lock_once ) {
259             if( !release_dotlock( lockhandle ) )
260                 is_locked = 0;
261         }
262         assert( unused );
263         r = unused;
264         r->flags.used = 1;
265         r->recno = recno;
266         memcpy( r->data, data, TRUST_RECORD_LEN );
267         r->flags.dirty = 1;
268         cache_is_dirty = 1;
269         cache_entries++;
270         return 0;
271     }
272     BUG();
273 }
274
275
276 int
277 tdbio_is_dirty()
278 {
279     return cache_is_dirty;
280 }
281
282
283 /****************
284  * Flush the cache.  This cannot be used while in a transaction.
285  */
286 int
287 tdbio_sync()
288 {
289     CACHE_CTRL r;
290     int did_lock = 0;
291
292     if( db_fd == -1 )
293         open_db();
294     if( in_transaction )
295         log_bug("tdbio: syncing while in transaction\n");
296
297     if( !cache_is_dirty )
298         return 0;
299
300     if( !is_locked ) {
301         if( make_dotlock( lockhandle, -1 ) )
302             log_fatal("can't acquire lock - giving up\n");
303         else
304             is_locked = 1;
305         did_lock = 1;
306     }
307     for( r = cache_list; r; r = r->next ) {
308         if( r->flags.used && r->flags.dirty ) {
309             int rc = write_cache_item( r );
310             if( rc )
311                 return rc;
312         }
313     }
314     cache_is_dirty = 0;
315     if( did_lock && !opt.lock_once ) {
316         if( !release_dotlock( lockhandle ) )
317             is_locked = 0;
318     }
319
320     return 0;
321 }
322
323
324
325 /****************
326  * Simple transactions system:
327  * Everything between begin_transaction and end/cancel_transaction
328  * is not immediatly written but at the time of end_transaction.
329  *
330  */
331 int
332 tdbio_begin_transaction()
333 {
334     int rc;
335
336     if( in_transaction )
337         log_bug("tdbio: nested transactions\n");
338     /* flush everything out */
339     rc = tdbio_sync();
340     if( rc )
341         return rc;
342     in_transaction = 1;
343     return 0;
344 }
345
346 int
347 tdbio_end_transaction()
348 {
349     int rc;
350
351     if( !in_transaction )
352         log_bug("tdbio: no active transaction\n");
353     if( !is_locked ) {
354         if( make_dotlock( lockhandle, -1 ) )
355             log_fatal("can't acquire lock - giving up\n");
356         else
357             is_locked = 1;
358     }
359     block_all_signals();
360     in_transaction = 0;
361     rc = tdbio_sync();
362     unblock_all_signals();
363     if( !opt.lock_once ) {
364         if( !release_dotlock( lockhandle ) )
365             is_locked = 0;
366     }
367     return rc;
368 }
369
370 int
371 tdbio_cancel_transaction()
372 {
373     CACHE_CTRL r;
374
375     if( !in_transaction )
376         log_bug("tdbio: no active transaction\n");
377
378     /* remove all dirty marked entries, so that the original ones
379      * are read back the next time */
380     if( cache_is_dirty ) {
381         for( r = cache_list; r; r = r->next ) {
382             if( r->flags.used && r->flags.dirty ) {
383                 r->flags.used = 0;
384                 cache_entries--;
385             }
386         }
387         cache_is_dirty = 0;
388     }
389
390     in_transaction = 0;
391     return 0;
392 }
393
394
395 \f
396 /********************************************************
397  **************** cached I/O functions ******************
398  ********************************************************/
399
400 static void
401 cleanup(void)
402 {
403     if( is_locked ) {
404         if( !release_dotlock(lockhandle) )
405             is_locked = 0;
406     }
407 }
408
409 int
410 tdbio_set_dbname( const char *new_dbname, int create )
411 {
412     char *fname;
413     static int initialized = 0;
414
415     if( !initialized ) {
416         atexit( cleanup );
417         initialized = 1;
418     }
419     fname = new_dbname? m_strdup( new_dbname )
420                       : make_filename(opt.homedir, "trustdb.gpg", NULL );
421
422     if( access( fname, R_OK ) ) {
423         if( errno != ENOENT ) {
424             log_error( _("%s: can't access: %s\n"), fname, strerror(errno) );
425             m_free(fname);
426             return G10ERR_TRUSTDB;
427         }
428         if( create ) {
429             FILE *fp;
430             TRUSTREC rec;
431             int rc;
432             char *p = strrchr( fname, '/' );
433
434             assert(p);
435             *p = 0;
436             if( access( fname, F_OK ) ) {
437                 if( strlen(fname) >= 7
438                     && !strcmp(fname+strlen(fname)-7, "/.gnupg" ) ) {
439                   #if HAVE_DOSISH_SYSTEM
440                     if( mkdir( fname ) )
441                   #else
442                     if( mkdir( fname, S_IRUSR|S_IWUSR|S_IXUSR ) )
443                   #endif
444                         log_fatal( _("%s: can't create directory: %s\n"),
445                                                     fname,  strerror(errno) );
446                     else
447                         log_info( _("%s: directory created\n"), fname );
448                     copy_options_file( fname );
449                 }
450                 else
451                     log_fatal( _("%s: directory does not exist!\n"), fname );
452             }
453             *p = '/';
454
455             fp =fopen( fname, "wb" );
456             if( !fp )
457                 log_fatal( _("%s: can't create: %s\n"), fname, strerror(errno) );
458             fclose(fp);
459             m_free(db_name);
460             db_name = fname;
461           #ifdef HAVE_DOSISH_SYSTEM
462             db_fd = open( db_name, O_RDWR | O_BINARY );
463           #else
464             db_fd = open( db_name, O_RDWR );
465           #endif
466             if( db_fd == -1 )
467                 log_fatal( _("%s: can't open: %s\n"), db_name, strerror(errno) );
468
469             if( !lockhandle )
470                 lockhandle = create_dotlock( db_name );
471             if( !lockhandle )
472                 log_fatal( _("%s: can't create lock\n"), db_name );
473
474             memset( &rec, 0, sizeof rec );
475             rec.r.ver.version = 2;
476             rec.r.ver.created = make_timestamp();
477             rec.r.ver.marginals =  opt.marginals_needed;
478             rec.r.ver.completes =  opt.completes_needed;
479             rec.r.ver.cert_depth = opt.max_cert_depth;
480             rec.rectype = RECTYPE_VER;
481             rec.recnum = 0;
482             rc = tdbio_write_record( &rec );
483             if( !rc )
484                 tdbio_sync();
485             if( rc )
486                 log_fatal( _("%s: failed to create version record: %s"),
487                                                    fname, g10_errstr(rc));
488             /* and read again to check that we are okay */
489             if( tdbio_read_record( 0, &rec, RECTYPE_VER ) )
490                 log_fatal( _("%s: invalid trustdb created\n"), db_name );
491
492             log_info(_("%s: trustdb created\n"), db_name);
493
494             return 0;
495         }
496     }
497     m_free(db_name);
498     db_name = fname;
499     return 0;
500 }
501
502
503 const char *
504 tdbio_get_dbname()
505 {
506     return db_name;
507 }
508
509
510
511 static void
512 open_db()
513 {
514     TRUSTREC rec;
515     assert( db_fd == -1 );
516
517     if( !lockhandle )
518         lockhandle = create_dotlock( db_name );
519     if( !lockhandle )
520         log_fatal( _("%s: can't create lock\n"), db_name );
521   #ifdef HAVE_DOSISH_SYSTEM
522     db_fd = open( db_name, O_RDWR | O_BINARY );
523   #else
524     db_fd = open( db_name, O_RDWR );
525   #endif
526     if( db_fd == -1 )
527         log_fatal( _("%s: can't open: %s\n"), db_name, strerror(errno) );
528     if( tdbio_read_record( 0, &rec, RECTYPE_VER ) )
529         log_fatal( _("%s: invalid trustdb\n"), db_name );
530 }
531
532
533 /****************
534  * Make a hashtable: type 0 = key hash, 1 = sdir hash
535  */
536 static void
537 create_hashtable( TRUSTREC *vr, int type )
538 {
539     TRUSTREC rec;
540     off_t offset;
541     ulong recnum;
542     int i, n, rc;
543
544     offset = lseek( db_fd, 0, SEEK_END );
545     if( offset == -1 )
546         log_fatal("trustdb: lseek to end failed: %s\n", strerror(errno) );
547     recnum = offset / TRUST_RECORD_LEN;
548     assert(recnum); /* this is will never be the first record */
549
550     if( !type )
551         vr->r.ver.keyhashtbl = recnum;
552     else
553         vr->r.ver.sdirhashtbl = recnum;
554     /* Now write the records */
555     n = (256+ITEMS_PER_HTBL_RECORD-1) / ITEMS_PER_HTBL_RECORD;
556     for(i=0; i < n; i++, recnum++ ) {
557          memset( &rec, 0, sizeof rec );
558          rec.rectype = RECTYPE_HTBL;
559          rec.recnum = recnum;
560          rc = tdbio_write_record( &rec );
561          if( rc )
562              log_fatal( _("%s: failed to create hashtable: %s\n"),
563                                         db_name, g10_errstr(rc));
564     }
565     /* update the version record */
566     rc = tdbio_write_record( vr );
567     if( !rc )
568         rc = tdbio_sync();
569     if( rc )
570         log_fatal( _("%s: error updating version record: %s\n"),
571                                                   db_name, g10_errstr(rc));
572 }
573
574
575 int
576 tdbio_db_matches_options()
577 {
578     static int yes_no = -1;
579
580     if( yes_no == -1 ) {
581         TRUSTREC vr;
582         int rc;
583
584         rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
585         if( rc )
586             log_fatal( _("%s: error reading version record: %s\n"),
587                                                     db_name, g10_errstr(rc) );
588
589         if( !vr.r.ver.marginals && !vr.r.ver.completes
590                                 && !vr.r.ver.cert_depth )
591         {   /* special hack for trustdbs created by old versions of GnuPG */
592             vr.r.ver.marginals =  opt.marginals_needed;
593             vr.r.ver.completes =  opt.completes_needed;
594             vr.r.ver.cert_depth = opt.max_cert_depth;
595             rc = tdbio_write_record( &vr );
596             if( !rc && !in_transaction )
597                 rc = tdbio_sync();
598             if( rc )
599                 log_error( _("%s: error writing version record: %s\n"),
600                                                 db_name, g10_errstr(rc) );
601         }
602
603         yes_no = vr.r.ver.marginals == opt.marginals_needed
604                  && vr.r.ver.completes == opt.completes_needed
605                  && vr.r.ver.cert_depth == opt.max_cert_depth;
606     }
607     return yes_no;
608 }
609
610
611 /****************
612  * Return the modifiy stamp.
613  * if modify_down is true, the modify_down stamp will be
614  * returned, otherwise the modify_up stamp.
615  */
616 ulong
617 tdbio_read_modify_stamp( int modify_down )
618 {
619     TRUSTREC vr;
620     int rc;
621     ulong mod;
622
623     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
624     if( rc )
625         log_fatal( _("%s: error reading version record: %s\n"),
626                                                     db_name, g10_errstr(rc) );
627
628     mod = modify_down? vr.r.ver.mod_down : vr.r.ver.mod_up;
629
630     /* Always return at least 1 to make comparison easier;
631      * this is still far back in history (before Led Zeppelin III :-) */
632     return mod ? mod : 1;
633 }
634
635 void
636 tdbio_write_modify_stamp( int down, int up )
637 {
638     TRUSTREC vr;
639     int rc;
640     ulong stamp;
641
642     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
643     if( rc )
644         log_fatal( _("%s: error reading version record: %s\n"),
645                                        db_name, g10_errstr(rc) );
646
647     stamp = make_timestamp();
648     if( down )
649         vr.r.ver.mod_down = stamp;
650     if( up )
651         vr.r.ver.mod_up = stamp;
652
653     rc = tdbio_write_record( &vr );
654     if( !rc )
655         log_fatal( _("%s: error writing version record: %s\n"),
656                                        db_name, g10_errstr(rc) );
657 }
658
659
660 /****************
661  * Return the record number of the keyhash tbl or create a new one.
662  */
663 static ulong
664 get_keyhashrec(void)
665 {
666     static ulong keyhashtbl; /* record number of the key hashtable */
667
668     if( !keyhashtbl ) {
669         TRUSTREC vr;
670         int rc;
671
672         rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
673         if( rc )
674             log_fatal( _("%s: error reading version record: %s\n"),
675                                             db_name, g10_errstr(rc) );
676         if( !vr.r.ver.keyhashtbl )
677             create_hashtable( &vr, 0 );
678
679         keyhashtbl = vr.r.ver.keyhashtbl;
680     }
681     return keyhashtbl;
682 }
683
684 /****************
685  * Return the record number of the shadow direcory hash table
686  * or create a new one.
687  */
688 static ulong
689 get_sdirhashrec(void)
690 {
691     static ulong sdirhashtbl; /* record number of the hashtable */
692
693     if( !sdirhashtbl ) {
694         TRUSTREC vr;
695         int rc;
696
697         rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
698         if( rc )
699             log_fatal( _("%s: error reading version record: %s\n"),
700                                                     db_name, g10_errstr(rc) );
701         if( !vr.r.ver.sdirhashtbl )
702             create_hashtable( &vr, 1 );
703
704         sdirhashtbl = vr.r.ver.sdirhashtbl;
705     }
706     return sdirhashtbl;
707 }
708
709
710 /****************
711  * Update a hashtable.
712  * table gives the start of the table, key and keylen is the key,
713  * newrecnum is the record number to insert.
714  */
715 static int
716 upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
717 {
718     TRUSTREC lastrec, rec;
719     ulong hashrec, item;
720     int msb;
721     int level=0;
722     int rc, i;
723
724     hashrec = table;
725   next_level:
726     msb = key[level];
727     hashrec += msb / ITEMS_PER_HTBL_RECORD;
728     rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
729     if( rc ) {
730         log_error( db_name, "upd_hashtable: read failed: %s\n",
731                                                         g10_errstr(rc) );
732         return rc;
733     }
734
735     item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
736     if( !item ) { /* insert a new item into the hash table */
737         rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = newrecnum;
738         rc = tdbio_write_record( &rec );
739         if( rc ) {
740             log_error( db_name, "upd_hashtable: write htbl failed: %s\n",
741                                                             g10_errstr(rc) );
742             return rc;
743         }
744     }
745     else if( item != newrecnum ) {  /* must do an update */
746         lastrec = rec;
747         rc = tdbio_read_record( item, &rec, 0 );
748         if( rc ) {
749             log_error( "upd_hashtable: read item failed: %s\n",
750                                                             g10_errstr(rc) );
751             return rc;
752         }
753
754         if( rec.rectype == RECTYPE_HTBL ) {
755             hashrec = item;
756             level++;
757             if( level >= keylen ) {
758                 log_error( "hashtable has invalid indirections.\n");
759                 return G10ERR_TRUSTDB;
760             }
761             goto next_level;
762         }
763         else if( rec.rectype == RECTYPE_HLST ) { /* extend list */
764             /* see whether the key is already in this list */
765             for(;;) {
766                 for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
767                     if( rec.r.hlst.rnum[i] == newrecnum ) {
768                         return 0; /* okay, already in the list */
769                     }
770                 }
771                 if( rec.r.hlst.next ) {
772                     rc = tdbio_read_record( rec.r.hlst.next,
773                                                        &rec, RECTYPE_HLST);
774                     if( rc ) {
775                         log_error( "scan keyhashtbl read hlst failed: %s\n",
776                                                              g10_errstr(rc) );
777                         return rc;
778                     }
779                 }
780                 else
781                     break; /* not there */
782             }
783             /* find the next free entry and put it in */
784             for(;;) {
785                 for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
786                     if( !rec.r.hlst.rnum[i] ) {
787                         rec.r.hlst.rnum[i] = newrecnum;
788                         rc = tdbio_write_record( &rec );
789                         if( rc )
790                             log_error( "upd_hashtable: write hlst failed: %s\n",
791                                                               g10_errstr(rc) );
792                         return rc; /* done */
793                     }
794                 }
795                 if( rec.r.hlst.next ) {
796                     rc = tdbio_read_record( rec.r.hlst.next,
797                                                       &rec, RECTYPE_HLST );
798                     if( rc ) {
799                         log_error( "upd_hashtable: read hlst failed: %s\n",
800                                                              g10_errstr(rc) );
801                         return rc;
802                     }
803                 }
804                 else { /* add a new list record */
805                     rec.r.hlst.next = item = tdbio_new_recnum();
806                     rc = tdbio_write_record( &rec );
807                     if( rc ) {
808                         log_error( "upd_hashtable: write hlst failed: %s\n",
809                                                           g10_errstr(rc) );
810                         return rc;
811                     }
812                     memset( &rec, 0, sizeof rec );
813                     rec.rectype = RECTYPE_HLST;
814                     rec.recnum = item;
815                     rec.r.hlst.rnum[0] = newrecnum;
816                     rc = tdbio_write_record( &rec );
817                     if( rc )
818                         log_error( "upd_hashtable: write ext hlst failed: %s\n",
819                                                           g10_errstr(rc) );
820                     return rc; /* done */
821                 }
822             } /* end loop over hlst slots */
823         }
824         else if( rec.rectype == RECTYPE_KEY
825                  || rec.rectype == RECTYPE_DIR
826                  || rec.rectype == RECTYPE_SDIR ) { /* insert a list record */
827             if( rec.recnum == newrecnum ) {
828                 return 0;
829             }
830             item = rec.recnum; /* save number of key record */
831             memset( &rec, 0, sizeof rec );
832             rec.rectype = RECTYPE_HLST;
833             rec.recnum = tdbio_new_recnum();
834             rec.r.hlst.rnum[0] = item;       /* old keyrecord */
835             rec.r.hlst.rnum[1] = newrecnum; /* and new one */
836             rc = tdbio_write_record( &rec );
837             if( rc ) {
838                 log_error( "upd_hashtable: write new hlst failed: %s\n",
839                                                   g10_errstr(rc) );
840                 return rc;
841             }
842             /* update the hashtable record */
843             lastrec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = rec.recnum;
844             rc = tdbio_write_record( &lastrec );
845             if( rc )
846                 log_error( "upd_hashtable: update htbl failed: %s\n",
847                                                              g10_errstr(rc) );
848             return rc; /* ready */
849         }
850         else {
851             log_error( "hashtbl %lu points to an invalid record\n",
852                                                                     item);
853             return G10ERR_TRUSTDB;
854         }
855     }
856
857     return 0;
858 }
859
860
861
862 /****************
863  * Lookup a record via the hashtable tablewith key/keylen and return the
864  * result in rec.  cmp() should return if the record is the desired one.
865  * Returns -1 if not found, 0 if found or another errocode
866  */
867 static int
868 lookup_hashtable( ulong table, const byte *key, size_t keylen,
869                   int (*cmpfnc)(void*, const TRUSTREC *), void *cmpdata,
870                                                 TRUSTREC *rec )
871 {
872     int rc;
873     ulong hashrec, item;
874     int msb;
875     int level=0;
876
877     hashrec = table;
878   next_level:
879     msb = key[level];
880     hashrec += msb / ITEMS_PER_HTBL_RECORD;
881     rc = tdbio_read_record( hashrec, rec, RECTYPE_HTBL );
882     if( rc ) {
883         log_error( db_name, "lookup_hashtable failed: %s\n", g10_errstr(rc) );
884         return rc;
885     }
886
887     item = rec->r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
888     if( !item )
889         return -1; /* not found */
890
891     rc = tdbio_read_record( item, rec, 0 );
892     if( rc ) {
893         log_error( db_name, "hashtable read failed: %s\n", g10_errstr(rc) );
894         return rc;
895     }
896     if( rec->rectype == RECTYPE_HTBL ) {
897         hashrec = item;
898         level++;
899         if( level >= keylen ) {
900             log_error( db_name, "hashtable has invalid indirections\n");
901             return G10ERR_TRUSTDB;
902         }
903         goto next_level;
904     }
905     else if( rec->rectype == RECTYPE_HLST ) {
906         for(;;) {
907             int i;
908
909             for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
910                 if( rec->r.hlst.rnum[i] ) {
911                     TRUSTREC tmp;
912
913                     rc = tdbio_read_record( rec->r.hlst.rnum[i], &tmp, 0 );
914                     if( rc ) {
915                         log_error( "lookup_hashtable: read item failed: %s\n",
916                                                               g10_errstr(rc) );
917                         return rc;
918                     }
919                     if( (*cmpfnc)( cmpdata, &tmp ) ) {
920                         *rec = tmp;
921                         return 0;
922                     }
923                 }
924             }
925             if( rec->r.hlst.next ) {
926                 rc = tdbio_read_record( rec->r.hlst.next, rec, RECTYPE_HLST );
927                 if( rc ) {
928                     log_error( "lookup_hashtable: read hlst failed: %s\n",
929                                                          g10_errstr(rc) );
930                     return rc;
931                 }
932             }
933             else
934                 return -1; /* not found */
935         }
936     }
937
938
939     if( (*cmpfnc)( cmpdata, rec ) )
940         return 0; /* really found */
941
942     return -1; /* no: not found */
943 }
944
945
946
947
948 /****************
949  * Update the key hashtbl or create the table if it does not exist
950  */
951 static int
952 update_keyhashtbl( TRUSTREC *kr )
953 {
954     return upd_hashtable( get_keyhashrec(),
955                           kr->r.key.fingerprint,
956                           kr->r.key.fingerprint_len, kr->recnum );
957 }
958
959 /****************
960  * Update the shadow dir hashtbl or create the table if it does not exist
961  */
962 static int
963 update_sdirhashtbl( TRUSTREC *sr )
964 {
965     byte key[8];
966
967     u32tobuf( key   , sr->r.sdir.keyid[0] );
968     u32tobuf( key+4 , sr->r.sdir.keyid[1] );
969     return upd_hashtable( get_sdirhashrec(), key, 8, sr->recnum );
970 }
971
972
973
974
975 void
976 tdbio_dump_record( TRUSTREC *rec, FILE *fp  )
977 {
978     int i;
979     ulong rnum = rec->recnum;
980     byte *p;
981
982     fprintf(fp, "rec %5lu, ", rnum );
983
984     switch( rec->rectype ) {
985       case 0: fprintf(fp, "blank\n");
986         break;
987       case RECTYPE_VER: fprintf(fp,
988             "version, kd=%lu, sd=%lu, free=%lu, m/c/d=%d/%d/%d down=%s",
989             rec->r.ver.keyhashtbl, rec->r.ver.sdirhashtbl,
990                                    rec->r.ver.firstfree,
991                                    rec->r.ver.marginals,
992                                    rec->r.ver.completes,
993                                    rec->r.ver.cert_depth,
994                                    strtimestamp(rec->r.ver.mod_down) );
995             fprintf(fp, ", up=%s\n", strtimestamp(rec->r.ver.mod_up) );
996         break;
997       case RECTYPE_FREE: fprintf(fp, "free, next=%lu\n", rec->r.free.next );
998         break;
999       case RECTYPE_DIR:
1000         fprintf(fp, "dir %lu, keys=%lu, uids=%lu, t=%02x",
1001                     rec->r.dir.lid,
1002                     rec->r.dir.keylist,
1003                     rec->r.dir.uidlist,
1004                     rec->r.dir.ownertrust );
1005         if( rec->r.dir.valcheck )
1006             fprintf( fp, ", v=%02x/%s", rec->r.dir.validity,
1007                                         strtimestamp(rec->r.dir.valcheck) );
1008         if( rec->r.dir.dirflags & DIRF_CHECKED ) {
1009             if( rec->r.dir.dirflags & DIRF_VALID )
1010                 fputs(", valid", fp );
1011             if( rec->r.dir.dirflags & DIRF_EXPIRED )
1012                 fputs(", expired", fp );
1013             if( rec->r.dir.dirflags & DIRF_REVOKED )
1014                 fputs(", revoked", fp );
1015         }
1016         putc('\n', fp);
1017         break;
1018       case RECTYPE_KEY:
1019         fprintf(fp, "key %lu, n=%lu a=%d ",
1020                    rec->r.key.lid,
1021                    rec->r.key.next,
1022                    rec->r.key.pubkey_algo );
1023         for(i=0; i < rec->r.key.fingerprint_len; i++ )
1024             fprintf(fp, "%02X", rec->r.key.fingerprint[i] );
1025         if( rec->r.key.keyflags & KEYF_CHECKED ) {
1026             if( rec->r.key.keyflags & KEYF_VALID )
1027                 fputs(", valid", fp );
1028             if( rec->r.key.keyflags & KEYF_EXPIRED )
1029                 fputs(", expired", fp );
1030             if( rec->r.key.keyflags & KEYF_REVOKED )
1031                 fputs(", revoked", fp );
1032         }
1033         putc('\n', fp);
1034         break;
1035       case RECTYPE_UID:
1036         fprintf(fp, "uid %lu, next=%lu, pref=%lu, sig=%lu, hash=%02X%02X",
1037                     rec->r.uid.lid,
1038                     rec->r.uid.next,
1039                     rec->r.uid.prefrec,
1040                     rec->r.uid.siglist,
1041                     rec->r.uid.namehash[18], rec->r.uid.namehash[19]);
1042         fprintf( fp, ", v=%02x", rec->r.uid.validity );
1043         if( rec->r.uid.uidflags & UIDF_CHECKED ) {
1044             if( rec->r.uid.uidflags & UIDF_VALID )
1045                 fputs(", valid", fp );
1046             if( rec->r.uid.uidflags & UIDF_REVOKED )
1047                 fputs(", revoked", fp );
1048         }
1049         putc('\n', fp);
1050         break;
1051       case RECTYPE_PREF:
1052         fprintf(fp, "pref %lu, next=%lu,",
1053                     rec->r.pref.lid, rec->r.pref.next);
1054         for(i=0,p=rec->r.pref.data; i < ITEMS_PER_PREF_RECORD; i+=2,p+=2 ) {
1055             if( *p )
1056                 fprintf(fp, " %c%d", *p == PREFTYPE_SYM    ? 'S' :
1057                                      *p == PREFTYPE_HASH   ? 'H' :
1058                                      *p == PREFTYPE_COMPR  ? 'Z' : '?', p[1]);
1059         }
1060         putc('\n', fp);
1061         break;
1062       case RECTYPE_SIG:
1063         fprintf(fp, "sig %lu, next=%lu,",
1064                          rec->r.sig.lid, rec->r.sig.next );
1065         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1066             if( rec->r.sig.sig[i].lid ) {
1067                 fprintf(fp, " %lu:", rec->r.sig.sig[i].lid );
1068                 if( rec->r.sig.sig[i].flag & SIGF_CHECKED ) {
1069                     fprintf(fp,"%c%c%c",
1070                        (rec->r.sig.sig[i].flag & SIGF_VALID)   ? 'V':'-',
1071                        (rec->r.sig.sig[i].flag & SIGF_EXPIRED) ? 'E':'-',
1072                        (rec->r.sig.sig[i].flag & SIGF_REVOKED) ? 'R':'-');
1073                 }
1074                 else if( rec->r.sig.sig[i].flag & SIGF_NOPUBKEY)
1075                     fputs("?--", fp);
1076                 else
1077                     fputs("---", fp);
1078             }
1079         }
1080         putc('\n', fp);
1081         break;
1082       case RECTYPE_SDIR:
1083         fprintf(fp, "sdir %lu, keyid=%08lX%08lX, algo=%d, hint=%lu\n",
1084                     rec->r.sdir.lid,
1085                     (ulong)rec->r.sdir.keyid[0],
1086                     (ulong)rec->r.sdir.keyid[1],
1087                     rec->r.sdir.pubkey_algo,
1088                     (ulong)rec->r.sdir.hintlist );
1089         break;
1090       case RECTYPE_CACH:
1091         fprintf(fp, "cach\n");
1092         break;
1093       case RECTYPE_HTBL:
1094         fprintf(fp, "htbl,");
1095         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ )
1096             fprintf(fp, " %lu", rec->r.htbl.item[i] );
1097         putc('\n', fp);
1098         break;
1099       case RECTYPE_HLST:
1100         fprintf(fp, "hlst, next=%lu,", rec->r.hlst.next );
1101         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ )
1102             fprintf(fp, " %lu", rec->r.hlst.rnum[i] );
1103         putc('\n', fp);
1104         break;
1105       default:
1106         fprintf(fp, "unknown type %d\n", rec->rectype );
1107         break;
1108     }
1109 }
1110
1111 /****************
1112  * read the record with number recnum
1113  * returns: -1 on error, 0 on success
1114  */
1115 int
1116 tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
1117 {
1118     byte readbuf[TRUST_RECORD_LEN];
1119     const byte *buf, *p;
1120     int rc = 0;
1121     int n, i;
1122
1123     if( db_fd == -1 )
1124         open_db();
1125     buf = get_record_from_cache( recnum );
1126     if( !buf ) {
1127         if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
1128             log_error(_("trustdb: lseek failed: %s\n"), strerror(errno) );
1129             return G10ERR_READ_FILE;
1130         }
1131         n = read( db_fd, readbuf, TRUST_RECORD_LEN);
1132         if( !n ) {
1133             return -1; /* eof */
1134         }
1135         else if( n != TRUST_RECORD_LEN ) {
1136             log_error(_("trustdb: read failed (n=%d): %s\n"), n,
1137                                                         strerror(errno) );
1138             return G10ERR_READ_FILE;
1139         }
1140         buf = readbuf;
1141     }
1142     rec->recnum = recnum;
1143     rec->dirty = 0;
1144     p = buf;
1145     rec->rectype = *p++;
1146     if( expected && rec->rectype != expected ) {
1147         log_error("%lu: read expected rec type %d, got %d\n",
1148                     recnum, expected, rec->rectype );
1149         return G10ERR_TRUSTDB;
1150     }
1151     p++;    /* skip reserved byte */
1152     switch( rec->rectype ) {
1153       case 0:  /* unused (free) record */
1154         break;
1155       case RECTYPE_VER: /* version record */
1156         if( memcmp(buf+1, "gpg", 3 ) ) {
1157             log_error( _("%s: not a trustdb file\n"), db_name );
1158             rc = G10ERR_TRUSTDB;
1159         }
1160         p += 2; /* skip "pgp" */
1161         rec->r.ver.version  = *p++;
1162         rec->r.ver.marginals = *p++;
1163         rec->r.ver.completes = *p++;
1164         rec->r.ver.cert_depth = *p++;
1165         p += 4; /* lock flags */
1166         rec->r.ver.created  = buftoulong(p); p += 4;
1167         rec->r.ver.mod_down = buftoulong(p); p += 4;
1168         rec->r.ver.mod_up   = buftoulong(p); p += 4;
1169         rec->r.ver.keyhashtbl=buftoulong(p); p += 4;
1170         rec->r.ver.firstfree =buftoulong(p); p += 4;
1171         rec->r.ver.sdirhashtbl =buftoulong(p); p += 4;
1172         if( recnum ) {
1173             log_error( _("%s: version record with recnum %lu\n"), db_name,
1174                                                              (ulong)recnum );
1175             rc = G10ERR_TRUSTDB;
1176         }
1177         else if( rec->r.ver.version != 2 ) {
1178             log_error( _("%s: invalid file version %d\n"), db_name,
1179                                                         rec->r.ver.version );
1180             rc = G10ERR_TRUSTDB;
1181         }
1182         break;
1183       case RECTYPE_FREE:
1184         rec->r.free.next  = buftoulong(p); p += 4;
1185         break;
1186       case RECTYPE_DIR:   /*directory record */
1187         rec->r.dir.lid      = buftoulong(p); p += 4;
1188         rec->r.dir.keylist  = buftoulong(p); p += 4;
1189         rec->r.dir.uidlist  = buftoulong(p); p += 4;
1190         rec->r.dir.cacherec = buftoulong(p); p += 4;
1191         rec->r.dir.ownertrust = *p++;
1192         rec->r.dir.dirflags   = *p++;
1193         rec->r.dir.validity   = *p++;
1194         rec->r.dir.valcheck   = buftoulong(p); p += 4;
1195         switch( rec->r.dir.validity ) {
1196           case 0:
1197           case TRUST_UNDEFINED:
1198           case TRUST_NEVER:
1199           case TRUST_MARGINAL:
1200           case TRUST_FULLY:
1201           case TRUST_ULTIMATE:
1202             break;
1203           default:
1204             log_info("lid %lu: invalid validity value - cleared\n", recnum);
1205         }
1206         if( rec->r.dir.lid != recnum ) {
1207             log_error( "%s: dir LID != recnum (%lu,%lu)\n",
1208                               db_name, rec->r.dir.lid, (ulong)recnum );
1209             rc = G10ERR_TRUSTDB;
1210         }
1211         break;
1212       case RECTYPE_KEY:   /* public key record */
1213         rec->r.key.lid      = buftoulong(p); p += 4;
1214         rec->r.key.next     = buftoulong(p); p += 4;
1215         p += 7;
1216         rec->r.key.keyflags = *p++;
1217         rec->r.key.pubkey_algo = *p++;
1218         rec->r.key.fingerprint_len = *p++;
1219         if( rec->r.key.fingerprint_len < 1 || rec->r.key.fingerprint_len > 20 )
1220             rec->r.key.fingerprint_len = 20;
1221         memcpy( rec->r.key.fingerprint, p, 20);
1222         break;
1223       case RECTYPE_UID:   /* user id record */
1224         rec->r.uid.lid      = buftoulong(p); p += 4;
1225         rec->r.uid.next     = buftoulong(p); p += 4;
1226         rec->r.uid.prefrec  = buftoulong(p); p += 4;
1227         rec->r.uid.siglist  = buftoulong(p); p += 4;
1228         rec->r.uid.uidflags = *p++;
1229         rec->r.uid.validity   = *p++;
1230         switch( rec->r.uid.validity ) {
1231           case 0:
1232           case TRUST_UNDEFINED:
1233           case TRUST_NEVER:
1234           case TRUST_MARGINAL:
1235           case TRUST_FULLY:
1236           case TRUST_ULTIMATE:
1237             break;
1238           default:
1239             log_info("lid %lu: invalid validity value - cleared\n", recnum);
1240         }
1241         memcpy( rec->r.uid.namehash, p, 20);
1242         break;
1243       case RECTYPE_PREF:  /* preference record */
1244         rec->r.pref.lid     = buftoulong(p); p += 4;
1245         rec->r.pref.next    = buftoulong(p); p += 4;
1246         memcpy( rec->r.pref.data, p, 30 );
1247         break;
1248       case RECTYPE_SIG:
1249         rec->r.sig.lid     = buftoulong(p); p += 4;
1250         rec->r.sig.next    = buftoulong(p); p += 4;
1251         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1252             rec->r.sig.sig[i].lid  = buftoulong(p); p += 4;
1253             rec->r.sig.sig[i].flag = *p++;
1254         }
1255         break;
1256       case RECTYPE_SDIR:   /* shadow directory record */
1257         rec->r.sdir.lid     = buftoulong(p); p += 4;
1258         rec->r.sdir.keyid[0]= buftou32(p); p += 4;
1259         rec->r.sdir.keyid[1]= buftou32(p); p += 4;
1260         rec->r.sdir.pubkey_algo = *p++;
1261         p += 3;
1262         rec->r.sdir.hintlist = buftoulong(p);
1263         if( rec->r.sdir.lid != recnum ) {
1264             log_error( "%s: sdir LID != recnum (%lu,%lu)\n",
1265                                db_name, rec->r.sdir.lid, (ulong)recnum );
1266             rc = G10ERR_TRUSTDB;
1267         }
1268         break;
1269       case RECTYPE_CACH:   /* cache record */
1270         rec->r.cache.lid    = buftoulong(p); p += 4;
1271         memcpy(rec->r.cache.blockhash, p, 20); p += 20;
1272         rec->r.cache.trustlevel = *p++;
1273         break;
1274       case RECTYPE_HTBL:
1275         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
1276             rec->r.htbl.item[i] = buftoulong(p); p += 4;
1277         }
1278         break;
1279       case RECTYPE_HLST:
1280         rec->r.hlst.next = buftoulong(p); p += 4;
1281         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
1282             rec->r.hlst.rnum[i] = buftoulong(p); p += 4;
1283         }
1284         break;
1285       default:
1286         log_error( "%s: invalid record type %d at recnum %lu\n",
1287                                    db_name, rec->rectype, (ulong)recnum );
1288         rc = G10ERR_TRUSTDB;
1289         break;
1290     }
1291
1292     return rc;
1293 }
1294
1295 /****************
1296  * Write the record at RECNUM
1297  */
1298 int
1299 tdbio_write_record( TRUSTREC *rec )
1300 {
1301     byte buf[TRUST_RECORD_LEN], *p;
1302     int rc = 0;
1303     int i;
1304     ulong recnum = rec->recnum;
1305
1306     if( db_fd == -1 )
1307         open_db();
1308
1309     memset(buf, 0, TRUST_RECORD_LEN);
1310     p = buf;
1311     *p++ = rec->rectype; p++;
1312     switch( rec->rectype ) {
1313       case 0:  /* unused record */
1314         break;
1315       case RECTYPE_VER: /* version record */
1316         if( recnum )
1317             BUG();
1318         memcpy(p-1, "gpg", 3 ); p += 2;
1319         *p++ = rec->r.ver.version;
1320         *p++ = rec->r.ver.marginals;
1321         *p++ = rec->r.ver.completes;
1322         *p++ = rec->r.ver.cert_depth;
1323         p += 4; /* skip lock flags */
1324         ulongtobuf(p, rec->r.ver.created); p += 4;
1325         ulongtobuf(p, rec->r.ver.mod_down); p += 4;
1326         ulongtobuf(p, rec->r.ver.mod_up); p += 4;
1327         ulongtobuf(p, rec->r.ver.keyhashtbl); p += 4;
1328         ulongtobuf(p, rec->r.ver.firstfree ); p += 4;
1329         ulongtobuf(p, rec->r.ver.sdirhashtbl ); p += 4;
1330         break;
1331
1332       case RECTYPE_FREE:
1333         ulongtobuf(p, rec->r.free.next); p += 4;
1334         break;
1335
1336       case RECTYPE_DIR:   /*directory record */
1337         ulongtobuf(p, rec->r.dir.lid); p += 4;
1338         ulongtobuf(p, rec->r.dir.keylist); p += 4;
1339         ulongtobuf(p, rec->r.dir.uidlist); p += 4;
1340         ulongtobuf(p, rec->r.dir.cacherec); p += 4;
1341         *p++ = rec->r.dir.ownertrust;
1342         *p++ = rec->r.dir.dirflags;
1343         *p++ = rec->r.dir.validity;
1344         ulongtobuf(p, rec->r.dir.valcheck); p += 4;
1345         assert( rec->r.dir.lid == recnum );
1346         break;
1347
1348       case RECTYPE_KEY:
1349         ulongtobuf(p, rec->r.key.lid); p += 4;
1350         ulongtobuf(p, rec->r.key.next); p += 4;
1351         p += 7;
1352         *p++ = rec->r.key.keyflags;
1353         *p++ = rec->r.key.pubkey_algo;
1354         *p++ = rec->r.key.fingerprint_len;
1355         memcpy( p, rec->r.key.fingerprint, 20); p += 20;
1356         break;
1357
1358       case RECTYPE_UID:   /* user id record */
1359         ulongtobuf(p, rec->r.uid.lid); p += 4;
1360         ulongtobuf(p, rec->r.uid.next); p += 4;
1361         ulongtobuf(p, rec->r.uid.prefrec); p += 4;
1362         ulongtobuf(p, rec->r.uid.siglist); p += 4;
1363         *p++ = rec->r.uid.uidflags;
1364         *p++ = rec->r.uid.validity;
1365         memcpy( p, rec->r.uid.namehash, 20 ); p += 20;
1366         break;
1367
1368       case RECTYPE_PREF:
1369         ulongtobuf(p, rec->r.pref.lid); p += 4;
1370         ulongtobuf(p, rec->r.pref.next); p += 4;
1371         memcpy( p, rec->r.pref.data, 30 );
1372         break;
1373
1374       case RECTYPE_SIG:
1375         ulongtobuf(p, rec->r.sig.lid); p += 4;
1376         ulongtobuf(p, rec->r.sig.next); p += 4;
1377         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1378             ulongtobuf(p, rec->r.sig.sig[i].lid); p += 4;
1379             *p++ = rec->r.sig.sig[i].flag;
1380         }
1381         break;
1382
1383       case RECTYPE_SDIR:
1384         ulongtobuf( p, rec->r.sdir.lid); p += 4;
1385         u32tobuf( p, rec->r.sdir.keyid[0] ); p += 4;
1386         u32tobuf( p, rec->r.sdir.keyid[1] ); p += 4;
1387         *p++ = rec->r.sdir.pubkey_algo;
1388         p += 3;
1389         ulongtobuf( p, rec->r.sdir.hintlist );
1390         break;
1391
1392       case RECTYPE_CACH:
1393         ulongtobuf(p, rec->r.cache.lid); p += 4;
1394         memcpy(p, rec->r.cache.blockhash, 20); p += 20;
1395         *p++ = rec->r.cache.trustlevel;
1396         break;
1397
1398       case RECTYPE_HTBL:
1399         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
1400             ulongtobuf( p, rec->r.htbl.item[i]); p += 4;
1401         }
1402         break;
1403
1404       case RECTYPE_HLST:
1405         ulongtobuf( p, rec->r.hlst.next); p += 4;
1406         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
1407             ulongtobuf( p, rec->r.hlst.rnum[i]); p += 4;
1408         }
1409         break;
1410
1411       default:
1412         BUG();
1413     }
1414
1415     rc = put_record_into_cache( recnum, buf );
1416     if( rc )
1417         ;
1418     if( rec->rectype == RECTYPE_KEY )
1419         rc = update_keyhashtbl( rec );
1420     else if( rec->rectype == RECTYPE_SDIR )
1421         rc = update_sdirhashtbl( rec );
1422
1423     return rc;
1424 }
1425
1426 int
1427 tdbio_delete_record( ulong recnum )
1428 {
1429     TRUSTREC vr, rec;
1430     int rc;
1431
1432     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
1433     if( rc )
1434         log_fatal( _("%s: error reading version record: %s\n"),
1435                                        db_name, g10_errstr(rc) );
1436
1437     rec.recnum = recnum;
1438     rec.rectype = RECTYPE_FREE;
1439     rec.r.free.next = vr.r.ver.firstfree;
1440     vr.r.ver.firstfree = recnum;
1441     rc = tdbio_write_record( &rec );
1442     if( !rc )
1443         rc = tdbio_write_record( &vr );
1444     return rc;
1445 }
1446
1447 /****************
1448  * create a new record and return its record number
1449  */
1450 ulong
1451 tdbio_new_recnum()
1452 {
1453     off_t offset;
1454     ulong recnum;
1455     TRUSTREC vr, rec;
1456     int rc;
1457
1458     /* look for unused records */
1459     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
1460     if( rc )
1461         log_fatal( _("%s: error reading version record: %s\n"),
1462                                              db_name, g10_errstr(rc) );
1463     if( vr.r.ver.firstfree ) {
1464         recnum = vr.r.ver.firstfree;
1465         rc = tdbio_read_record( recnum, &rec, RECTYPE_FREE );
1466         if( rc ) {
1467             log_error( _("%s: error reading free record: %s\n"),
1468                                                   db_name,  g10_errstr(rc) );
1469             return rc;
1470         }
1471         /* update dir record */
1472         vr.r.ver.firstfree = rec.r.free.next;
1473         rc = tdbio_write_record( &vr );
1474         if( rc ) {
1475             log_error( _("%s: error writing dir record: %s\n"),
1476                                                      db_name, g10_errstr(rc) );
1477             return rc;
1478         }
1479         /*zero out the new record */
1480         memset( &rec, 0, sizeof rec );
1481         rec.rectype = 0; /* unused record */
1482         rec.recnum = recnum;
1483         rc = tdbio_write_record( &rec );
1484         if( rc )
1485             log_fatal(_("%s: failed to zero a record: %s\n"),
1486                                        db_name, g10_errstr(rc));
1487     }
1488     else { /* not found, append a new record */
1489         offset = lseek( db_fd, 0, SEEK_END );
1490         if( offset == -1 )
1491             log_fatal("trustdb: lseek to end failed: %s\n", strerror(errno) );
1492         recnum = offset / TRUST_RECORD_LEN;
1493         assert(recnum); /* this is will never be the first record */
1494         /* we must write a record, so that the next call to this function
1495          * returns another recnum */
1496         memset( &rec, 0, sizeof rec );
1497         rec.rectype = 0; /* unused record */
1498         rec.recnum = recnum;
1499         rc = 0;
1500         if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
1501             log_error(_("trustdb rec %lu: lseek failed: %s\n"),
1502                                                 recnum, strerror(errno) );
1503             rc = G10ERR_WRITE_FILE;
1504         }
1505         else {
1506             int n = write( db_fd, &rec, TRUST_RECORD_LEN);
1507             if( n != TRUST_RECORD_LEN ) {
1508                 log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
1509                                                  recnum, n, strerror(errno) );
1510                 rc = G10ERR_WRITE_FILE;
1511             }
1512         }
1513
1514         if( rc )
1515             log_fatal(_("%s: failed to append a record: %s\n"),
1516                                     db_name,    g10_errstr(rc));
1517     }
1518     return recnum ;
1519 }
1520
1521
1522
1523 /****************
1524  * Search the trustdb for a key which matches PK and return the dir record
1525  * The local_id of PK is set to the correct value
1526  */
1527 int
1528 tdbio_search_dir_bypk( PKT_public_key *pk, TRUSTREC *rec )
1529 {
1530     byte fingerprint[MAX_FINGERPRINT_LEN];
1531     size_t fingerlen;
1532     u32 keyid[2];
1533     int rc;
1534
1535     keyid_from_pk( pk, keyid );
1536     fingerprint_from_pk( pk, fingerprint, &fingerlen );
1537     rc = tdbio_search_dir_byfpr( fingerprint, fingerlen,
1538                                  pk->pubkey_algo, rec );
1539
1540     if( !rc ) {
1541         if( pk->local_id && pk->local_id != rec->recnum )
1542             log_error("%s: found record, but LID from memory does "
1543                        "not match recnum (%lu,%lu)\n",
1544                             db_name,  pk->local_id, rec->recnum );
1545         pk->local_id = rec->recnum;
1546     }
1547     return rc;
1548 }
1549
1550
1551 static int
1552 cmp_krec_fpr( void *dataptr, const TRUSTREC *rec )
1553 {
1554     const struct cmp_krec_fpr_struct *d = dataptr;
1555
1556     return rec->rectype == RECTYPE_KEY
1557            && ( !d->pubkey_algo || rec->r.key.pubkey_algo == d->pubkey_algo )
1558            && rec->r.key.fingerprint_len == d->fprlen
1559            && !memcmp( rec->r.key.fingerprint, d->fpr, d->fprlen );
1560 }
1561
1562 int
1563 tdbio_search_dir_byfpr( const byte *fingerprint, size_t fingerlen,
1564                         int pubkey_algo, TRUSTREC *rec )
1565 {
1566     struct cmp_krec_fpr_struct cmpdata;
1567     ulong recnum;
1568     int rc;
1569
1570     assert( fingerlen == 20 || fingerlen == 16 );
1571
1572     /* locate the key using the hash table */
1573     cmpdata.pubkey_algo = pubkey_algo;
1574     cmpdata.fpr = fingerprint;
1575     cmpdata.fprlen = fingerlen;
1576     rc = lookup_hashtable( get_keyhashrec(), fingerprint, fingerlen,
1577                            cmp_krec_fpr, &cmpdata, rec );
1578     if( !rc ) {
1579         recnum = rec->r.key.lid;
1580         /* Now read the dir record */
1581         rc = tdbio_read_record( recnum, rec, RECTYPE_DIR);
1582         if( rc )
1583             log_error("%s: can't read dirrec %lu: %s\n",
1584                                      db_name, recnum, g10_errstr(rc) );
1585     }
1586     return rc;
1587 }
1588
1589
1590
1591 static int
1592 cmp_sdir( void *dataptr, const TRUSTREC *rec )
1593 {
1594     const struct cmp_sdir_struct *d = dataptr;
1595
1596     return rec->rectype == RECTYPE_SDIR
1597            && ( !d->pubkey_algo || rec->r.sdir.pubkey_algo == d->pubkey_algo )
1598            && rec->r.sdir.keyid[0] == d->keyid[0]
1599            && rec->r.sdir.keyid[1] == d->keyid[1];
1600 }
1601
1602
1603 int
1604 tdbio_search_sdir( u32 *keyid, int pubkey_algo, TRUSTREC *rec )
1605 {
1606     struct cmp_sdir_struct cmpdata;
1607     int rc;
1608     byte key[8];
1609
1610     /* locate the shadow dir record using the hash table */
1611     u32tobuf( key   , keyid[0] );
1612     u32tobuf( key+4 , keyid[1] );
1613     cmpdata.pubkey_algo = pubkey_algo;
1614     cmpdata.keyid[0] = keyid[0];
1615     cmpdata.keyid[1] = keyid[1];
1616     rc = lookup_hashtable( get_sdirhashrec(), key, 8,
1617                            cmp_sdir, &cmpdata, rec );
1618     return rc;
1619 }
1620
1621
1622 void
1623 tdbio_invalid(void)
1624 {
1625     log_error(_(
1626         "The trustdb is corrupted; please run \"gpgm --fix-trustdb\".\n") );
1627     g10_exit(2);
1628 }
1629
1630