See ChangeLog: Tue Aug 31 17:20:44 CEST 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  10000  /* 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_[s]dir */
73 struct cmp_xdir_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 if( !opt.quiet )
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             if( !opt.quiet )
493                 log_info(_("%s: trustdb created\n"), db_name);
494
495             return 0;
496         }
497     }
498     m_free(db_name);
499     db_name = fname;
500     return 0;
501 }
502
503
504 const char *
505 tdbio_get_dbname()
506 {
507     return db_name;
508 }
509
510
511
512 static void
513 open_db()
514 {
515     TRUSTREC rec;
516     assert( db_fd == -1 );
517
518     if( !lockhandle )
519         lockhandle = create_dotlock( db_name );
520     if( !lockhandle )
521         log_fatal( _("%s: can't create lock\n"), db_name );
522   #ifdef HAVE_DOSISH_SYSTEM
523     db_fd = open( db_name, O_RDWR | O_BINARY );
524   #else
525     db_fd = open( db_name, O_RDWR );
526   #endif
527     if( db_fd == -1 )
528         log_fatal( _("%s: can't open: %s\n"), db_name, strerror(errno) );
529     if( tdbio_read_record( 0, &rec, RECTYPE_VER ) )
530         log_fatal( _("%s: invalid trustdb\n"), db_name );
531 }
532
533
534 /****************
535  * Make a hashtable: type 0 = key hash, 1 = sdir hash
536  */
537 static void
538 create_hashtable( TRUSTREC *vr, int type )
539 {
540     TRUSTREC rec;
541     off_t offset;
542     ulong recnum;
543     int i, n, rc;
544
545     offset = lseek( db_fd, 0, SEEK_END );
546     if( offset == -1 )
547         log_fatal("trustdb: lseek to end failed: %s\n", strerror(errno) );
548     recnum = offset / TRUST_RECORD_LEN;
549     assert(recnum); /* this is will never be the first record */
550
551     if( !type )
552         vr->r.ver.keyhashtbl = recnum;
553     else
554         vr->r.ver.sdirhashtbl = recnum;
555     /* Now write the records */
556     n = (256+ITEMS_PER_HTBL_RECORD-1) / ITEMS_PER_HTBL_RECORD;
557     for(i=0; i < n; i++, recnum++ ) {
558          memset( &rec, 0, sizeof rec );
559          rec.rectype = RECTYPE_HTBL;
560          rec.recnum = recnum;
561          rc = tdbio_write_record( &rec );
562          if( rc )
563              log_fatal( _("%s: failed to create hashtable: %s\n"),
564                                         db_name, g10_errstr(rc));
565     }
566     /* update the version record */
567     rc = tdbio_write_record( vr );
568     if( !rc )
569         rc = tdbio_sync();
570     if( rc )
571         log_fatal( _("%s: error updating version record: %s\n"),
572                                                   db_name, g10_errstr(rc));
573 }
574
575
576 int
577 tdbio_db_matches_options()
578 {
579     static int yes_no = -1;
580
581     if( yes_no == -1 ) {
582         TRUSTREC vr;
583         int rc;
584
585         rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
586         if( rc )
587             log_fatal( _("%s: error reading version record: %s\n"),
588                                                     db_name, g10_errstr(rc) );
589
590         if( !vr.r.ver.marginals && !vr.r.ver.completes
591                                 && !vr.r.ver.cert_depth )
592         {   /* special hack for trustdbs created by old versions of GnuPG */
593             vr.r.ver.marginals =  opt.marginals_needed;
594             vr.r.ver.completes =  opt.completes_needed;
595             vr.r.ver.cert_depth = opt.max_cert_depth;
596             rc = tdbio_write_record( &vr );
597             if( !rc && !in_transaction )
598                 rc = tdbio_sync();
599             if( rc )
600                 log_error( _("%s: error writing version record: %s\n"),
601                                                 db_name, g10_errstr(rc) );
602         }
603
604         yes_no = vr.r.ver.marginals == opt.marginals_needed
605                  && vr.r.ver.completes == opt.completes_needed
606                  && vr.r.ver.cert_depth == opt.max_cert_depth;
607     }
608     return yes_no;
609 }
610
611
612 /****************
613  * Return the modifiy stamp.
614  * if modify_down is true, the modify_down stamp will be
615  * returned, otherwise the modify_up stamp.
616  */
617 ulong
618 tdbio_read_modify_stamp( int modify_down )
619 {
620     TRUSTREC vr;
621     int rc;
622     ulong mod;
623
624     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
625     if( rc )
626         log_fatal( _("%s: error reading version record: %s\n"),
627                                                     db_name, g10_errstr(rc) );
628
629     mod = modify_down? vr.r.ver.mod_down : vr.r.ver.mod_up;
630
631     /* Always return at least 1 to make comparison easier;
632      * this is still far back in history (before Led Zeppelin III :-) */
633     return mod ? mod : 1;
634 }
635
636 void
637 tdbio_write_modify_stamp( int up, int down )
638 {
639     TRUSTREC vr;
640     int rc;
641     ulong stamp;
642
643     if( !(up || down) )
644         return;
645
646     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
647     if( rc )
648         log_fatal( _("%s: error reading version record: %s\n"),
649                                        db_name, g10_errstr(rc) );
650
651     stamp = make_timestamp();
652     if( down )
653         vr.r.ver.mod_down = stamp;
654     if( up )
655         vr.r.ver.mod_up = stamp;
656
657     rc = tdbio_write_record( &vr );
658     if( rc )
659         log_fatal( _("%s: error writing version record: %s\n"),
660                                        db_name, g10_errstr(rc) );
661 }
662
663
664 /****************
665  * Return the record number of the keyhash tbl or create a new one.
666  */
667 static ulong
668 get_keyhashrec(void)
669 {
670     static ulong keyhashtbl; /* record number of the key hashtable */
671
672     if( !keyhashtbl ) {
673         TRUSTREC vr;
674         int rc;
675
676         rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
677         if( rc )
678             log_fatal( _("%s: error reading version record: %s\n"),
679                                             db_name, g10_errstr(rc) );
680         if( !vr.r.ver.keyhashtbl )
681             create_hashtable( &vr, 0 );
682
683         keyhashtbl = vr.r.ver.keyhashtbl;
684     }
685     return keyhashtbl;
686 }
687
688 /****************
689  * Return the record number of the shadow direcory hash table
690  * or create a new one.
691  */
692 static ulong
693 get_sdirhashrec(void)
694 {
695     static ulong sdirhashtbl; /* record number of the hashtable */
696
697     if( !sdirhashtbl ) {
698         TRUSTREC vr;
699         int rc;
700
701         rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
702         if( rc )
703             log_fatal( _("%s: error reading version record: %s\n"),
704                                                     db_name, g10_errstr(rc) );
705         if( !vr.r.ver.sdirhashtbl )
706             create_hashtable( &vr, 1 );
707
708         sdirhashtbl = vr.r.ver.sdirhashtbl;
709     }
710     return sdirhashtbl;
711 }
712
713
714 /****************
715  * Update a hashtable.
716  * table gives the start of the table, key and keylen is the key,
717  * newrecnum is the record number to insert.
718  */
719 static int
720 upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
721 {
722     TRUSTREC lastrec, rec;
723     ulong hashrec, item;
724     int msb;
725     int level=0;
726     int rc, i;
727
728     hashrec = table;
729   next_level:
730     msb = key[level];
731     hashrec += msb / ITEMS_PER_HTBL_RECORD;
732     rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
733     if( rc ) {
734         log_error( db_name, "upd_hashtable: read failed: %s\n",
735                                                         g10_errstr(rc) );
736         return rc;
737     }
738
739     item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
740     if( !item ) { /* insert a new item into the hash table */
741         rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = newrecnum;
742         rc = tdbio_write_record( &rec );
743         if( rc ) {
744             log_error( db_name, "upd_hashtable: write htbl failed: %s\n",
745                                                             g10_errstr(rc) );
746             return rc;
747         }
748     }
749     else if( item != newrecnum ) {  /* must do an update */
750         lastrec = rec;
751         rc = tdbio_read_record( item, &rec, 0 );
752         if( rc ) {
753             log_error( "upd_hashtable: read item failed: %s\n",
754                                                             g10_errstr(rc) );
755             return rc;
756         }
757
758         if( rec.rectype == RECTYPE_HTBL ) {
759             hashrec = item;
760             level++;
761             if( level >= keylen ) {
762                 log_error( "hashtable has invalid indirections.\n");
763                 return G10ERR_TRUSTDB;
764             }
765             goto next_level;
766         }
767         else if( rec.rectype == RECTYPE_HLST ) { /* extend list */
768             /* see whether the key is already in this list */
769             for(;;) {
770                 for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
771                     if( rec.r.hlst.rnum[i] == newrecnum ) {
772                         return 0; /* okay, already in the list */
773                     }
774                 }
775                 if( rec.r.hlst.next ) {
776                     rc = tdbio_read_record( rec.r.hlst.next,
777                                                        &rec, RECTYPE_HLST);
778                     if( rc ) {
779                         log_error( "scan keyhashtbl read hlst failed: %s\n",
780                                                              g10_errstr(rc) );
781                         return rc;
782                     }
783                 }
784                 else
785                     break; /* not there */
786             }
787             /* find the next free entry and put it in */
788             for(;;) {
789                 for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
790                     if( !rec.r.hlst.rnum[i] ) {
791                         rec.r.hlst.rnum[i] = newrecnum;
792                         rc = tdbio_write_record( &rec );
793                         if( rc )
794                             log_error( "upd_hashtable: write hlst failed: %s\n",
795                                                               g10_errstr(rc) );
796                         return rc; /* done */
797                     }
798                 }
799                 if( rec.r.hlst.next ) {
800                     rc = tdbio_read_record( rec.r.hlst.next,
801                                                       &rec, RECTYPE_HLST );
802                     if( rc ) {
803                         log_error( "upd_hashtable: read hlst failed: %s\n",
804                                                              g10_errstr(rc) );
805                         return rc;
806                     }
807                 }
808                 else { /* add a new list record */
809                     rec.r.hlst.next = item = tdbio_new_recnum();
810                     rc = tdbio_write_record( &rec );
811                     if( rc ) {
812                         log_error( "upd_hashtable: write hlst failed: %s\n",
813                                                           g10_errstr(rc) );
814                         return rc;
815                     }
816                     memset( &rec, 0, sizeof rec );
817                     rec.rectype = RECTYPE_HLST;
818                     rec.recnum = item;
819                     rec.r.hlst.rnum[0] = newrecnum;
820                     rc = tdbio_write_record( &rec );
821                     if( rc )
822                         log_error( "upd_hashtable: write ext hlst failed: %s\n",
823                                                           g10_errstr(rc) );
824                     return rc; /* done */
825                 }
826             } /* end loop over hlst slots */
827         }
828         else if( rec.rectype == RECTYPE_KEY
829                  || rec.rectype == RECTYPE_DIR
830                  || rec.rectype == RECTYPE_SDIR ) { /* insert a list record */
831             if( rec.recnum == newrecnum ) {
832                 return 0;
833             }
834             item = rec.recnum; /* save number of key record */
835             memset( &rec, 0, sizeof rec );
836             rec.rectype = RECTYPE_HLST;
837             rec.recnum = tdbio_new_recnum();
838             rec.r.hlst.rnum[0] = item;       /* old keyrecord */
839             rec.r.hlst.rnum[1] = newrecnum; /* and new one */
840             rc = tdbio_write_record( &rec );
841             if( rc ) {
842                 log_error( "upd_hashtable: write new hlst failed: %s\n",
843                                                   g10_errstr(rc) );
844                 return rc;
845             }
846             /* update the hashtable record */
847             lastrec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = rec.recnum;
848             rc = tdbio_write_record( &lastrec );
849             if( rc )
850                 log_error( "upd_hashtable: update htbl failed: %s\n",
851                                                              g10_errstr(rc) );
852             return rc; /* ready */
853         }
854         else {
855             log_error( "hashtbl %lu: %lu/%d points to an invalid record %lu\n",
856                        table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
857             list_trustdb(NULL);
858             return G10ERR_TRUSTDB;
859         }
860     }
861
862     return 0;
863 }
864
865
866 /****************
867  * Drop an entry from a hashtable
868  * table gives the start of the table, key and keylen is the key,
869  */
870 static int
871 drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
872 {
873     TRUSTREC rec;
874     ulong hashrec, item;
875     int msb;
876     int level=0;
877     int rc, i;
878
879     hashrec = table;
880   next_level:
881     msb = key[level];
882     hashrec += msb / ITEMS_PER_HTBL_RECORD;
883     rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
884     if( rc ) {
885         log_error( db_name, "drop_from_hashtable: read failed: %s\n",
886                                                         g10_errstr(rc) );
887         return rc;
888     }
889
890     item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
891     if( !item )  /* not found - forget about it  */
892         return 0;
893
894     if( item == recnum ) {  /* tables points direct to the record */
895         rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = 0;
896         rc = tdbio_write_record( &rec );
897         if( rc )
898             log_error( db_name, "drop_from_hashtable: write htbl failed: %s\n",
899                                                             g10_errstr(rc) );
900         return rc;
901     }
902
903     rc = tdbio_read_record( item, &rec, 0 );
904     if( rc ) {
905         log_error( "drop_from_hashtable: read item failed: %s\n",
906                                                         g10_errstr(rc) );
907         return rc;
908     }
909
910     if( rec.rectype == RECTYPE_HTBL ) {
911         hashrec = item;
912         level++;
913         if( level >= keylen ) {
914             log_error( "hashtable has invalid indirections.\n");
915             return G10ERR_TRUSTDB;
916         }
917         goto next_level;
918     }
919
920     if( rec.rectype == RECTYPE_HLST ) {
921         for(;;) {
922             for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
923                 if( rec.r.hlst.rnum[i] == recnum ) {
924                     rec.r.hlst.rnum[i] = 0; /* drop */
925                     rc = tdbio_write_record( &rec );
926                     if( rc )
927                         log_error( db_name, "drop_from_hashtable: write htbl failed: %s\n",
928                                                                         g10_errstr(rc) );
929                     return rc;
930                 }
931             }
932             if( rec.r.hlst.next ) {
933                 rc = tdbio_read_record( rec.r.hlst.next,
934                                                    &rec, RECTYPE_HLST);
935                 if( rc ) {
936                     log_error( "scan keyhashtbl read hlst failed: %s\n",
937                                                          g10_errstr(rc) );
938                     return rc;
939                 }
940             }
941             else
942                 return 0; /* key not in table */
943         }
944     }
945
946     log_error( "hashtbl %lu: %lu/%d points to wrong record %lu\n",
947                     table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
948     return G10ERR_TRUSTDB;
949 }
950
951
952
953 /****************
954  * Lookup a record via the hashtable tablewith key/keylen and return the
955  * result in rec.  cmp() should return if the record is the desired one.
956  * Returns -1 if not found, 0 if found or another errocode
957  */
958 static int
959 lookup_hashtable( ulong table, const byte *key, size_t keylen,
960                   int (*cmpfnc)(void*, const TRUSTREC *), void *cmpdata,
961                                                 TRUSTREC *rec )
962 {
963     int rc;
964     ulong hashrec, item;
965     int msb;
966     int level=0;
967
968     hashrec = table;
969   next_level:
970     msb = key[level];
971     hashrec += msb / ITEMS_PER_HTBL_RECORD;
972     rc = tdbio_read_record( hashrec, rec, RECTYPE_HTBL );
973     if( rc ) {
974         log_error( db_name, "lookup_hashtable failed: %s\n", g10_errstr(rc) );
975         return rc;
976     }
977
978     item = rec->r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
979     if( !item )
980         return -1; /* not found */
981
982     rc = tdbio_read_record( item, rec, 0 );
983     if( rc ) {
984         log_error( db_name, "hashtable read failed: %s\n", g10_errstr(rc) );
985         return rc;
986     }
987     if( rec->rectype == RECTYPE_HTBL ) {
988         hashrec = item;
989         level++;
990         if( level >= keylen ) {
991             log_error( db_name, "hashtable has invalid indirections\n");
992             return G10ERR_TRUSTDB;
993         }
994         goto next_level;
995     }
996     else if( rec->rectype == RECTYPE_HLST ) {
997         for(;;) {
998             int i;
999
1000             for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
1001                 if( rec->r.hlst.rnum[i] ) {
1002                     TRUSTREC tmp;
1003
1004                     rc = tdbio_read_record( rec->r.hlst.rnum[i], &tmp, 0 );
1005                     if( rc ) {
1006                         log_error( "lookup_hashtable: read item failed: %s\n",
1007                                                               g10_errstr(rc) );
1008                         return rc;
1009                     }
1010                     if( (*cmpfnc)( cmpdata, &tmp ) ) {
1011                         *rec = tmp;
1012                         return 0;
1013                     }
1014                 }
1015             }
1016             if( rec->r.hlst.next ) {
1017                 rc = tdbio_read_record( rec->r.hlst.next, rec, RECTYPE_HLST );
1018                 if( rc ) {
1019                     log_error( "lookup_hashtable: read hlst failed: %s\n",
1020                                                          g10_errstr(rc) );
1021                     return rc;
1022                 }
1023             }
1024             else
1025                 return -1; /* not found */
1026         }
1027     }
1028
1029
1030     if( (*cmpfnc)( cmpdata, rec ) )
1031         return 0; /* really found */
1032
1033     return -1; /* no: not found */
1034 }
1035
1036
1037
1038
1039 /****************
1040  * Update the key hashtbl or create the table if it does not exist
1041  */
1042 static int
1043 update_keyhashtbl( TRUSTREC *kr )
1044 {
1045     return upd_hashtable( get_keyhashrec(),
1046                           kr->r.key.fingerprint,
1047                           kr->r.key.fingerprint_len, kr->recnum );
1048 }
1049
1050 /****************
1051  * Update the shadow dir hashtbl or create the table if it does not exist
1052  */
1053 static int
1054 update_sdirhashtbl( TRUSTREC *sr )
1055 {
1056     byte key[8];
1057
1058     u32tobuf( key   , sr->r.sdir.keyid[0] );
1059     u32tobuf( key+4 , sr->r.sdir.keyid[1] );
1060     return upd_hashtable( get_sdirhashrec(), key, 8, sr->recnum );
1061 }
1062
1063 /****************
1064  * Drop the records from the key-hashtbl
1065  */
1066 static int
1067 drop_from_keyhashtbl( TRUSTREC *kr )
1068 {
1069     return drop_from_hashtable( get_keyhashrec(),
1070                                 kr->r.key.fingerprint,
1071                                 kr->r.key.fingerprint_len, kr->recnum );
1072 }
1073
1074 /****************
1075  * Drop record drom the shadow dir hashtbl
1076  */
1077 static int
1078 drop_from_sdirhashtbl( TRUSTREC *sr )
1079 {
1080     byte key[8];
1081
1082     u32tobuf( key   , sr->r.sdir.keyid[0] );
1083     u32tobuf( key+4 , sr->r.sdir.keyid[1] );
1084     return drop_from_hashtable( get_sdirhashrec(), key, 8, sr->recnum );
1085 }
1086
1087
1088
1089
1090 void
1091 tdbio_dump_record( TRUSTREC *rec, FILE *fp  )
1092 {
1093     int i;
1094     ulong rnum = rec->recnum;
1095     byte *p;
1096
1097     fprintf(fp, "rec %5lu, ", rnum );
1098
1099     switch( rec->rectype ) {
1100       case 0: fprintf(fp, "blank\n");
1101         break;
1102       case RECTYPE_VER: fprintf(fp,
1103             "version, kd=%lu, sd=%lu, free=%lu, m/c/d=%d/%d/%d down=%s",
1104             rec->r.ver.keyhashtbl, rec->r.ver.sdirhashtbl,
1105                                    rec->r.ver.firstfree,
1106                                    rec->r.ver.marginals,
1107                                    rec->r.ver.completes,
1108                                    rec->r.ver.cert_depth,
1109                                    strtimestamp(rec->r.ver.mod_down) );
1110             fprintf(fp, ", up=%s\n", strtimestamp(rec->r.ver.mod_up) );
1111         break;
1112       case RECTYPE_FREE: fprintf(fp, "free, next=%lu\n", rec->r.free.next );
1113         break;
1114       case RECTYPE_DIR:
1115         fprintf(fp, "dir %lu, keys=%lu, uids=%lu, t=%02x",
1116                     rec->r.dir.lid,
1117                     rec->r.dir.keylist,
1118                     rec->r.dir.uidlist,
1119                     rec->r.dir.ownertrust );
1120         if( rec->r.dir.valcheck )
1121             fprintf( fp, ", v=%02x/%s", rec->r.dir.validity,
1122                                         strtimestamp(rec->r.dir.valcheck) );
1123         if( rec->r.dir.checkat )
1124             fprintf( fp, ", a=%s", strtimestamp(rec->r.dir.checkat) );
1125         if( rec->r.dir.dirflags & DIRF_CHECKED ) {
1126             if( rec->r.dir.dirflags & DIRF_VALID )
1127                 fputs(", valid", fp );
1128             if( rec->r.dir.dirflags & DIRF_EXPIRED )
1129                 fputs(", expired", fp );
1130             if( rec->r.dir.dirflags & DIRF_REVOKED )
1131                 fputs(", revoked", fp );
1132         }
1133         putc('\n', fp);
1134         break;
1135       case RECTYPE_KEY:
1136         fprintf(fp, "key %lu, n=%lu a=%d ",
1137                    rec->r.key.lid,
1138                    rec->r.key.next,
1139                    rec->r.key.pubkey_algo );
1140         for(i=0; i < rec->r.key.fingerprint_len; i++ )
1141             fprintf(fp, "%02X", rec->r.key.fingerprint[i] );
1142         if( rec->r.key.keyflags & KEYF_CHECKED ) {
1143             if( rec->r.key.keyflags & KEYF_VALID )
1144                 fputs(", valid", fp );
1145             if( rec->r.key.keyflags & KEYF_EXPIRED )
1146                 fputs(", expired", fp );
1147             if( rec->r.key.keyflags & KEYF_REVOKED )
1148                 fputs(", revoked", fp );
1149         }
1150         putc('\n', fp);
1151         break;
1152       case RECTYPE_UID:
1153         fprintf(fp, "uid %lu, next=%lu, pref=%lu, sig=%lu, hash=%02X%02X",
1154                     rec->r.uid.lid,
1155                     rec->r.uid.next,
1156                     rec->r.uid.prefrec,
1157                     rec->r.uid.siglist,
1158                     rec->r.uid.namehash[18], rec->r.uid.namehash[19]);
1159         fprintf( fp, ", v=%02x", rec->r.uid.validity );
1160         if( rec->r.uid.uidflags & UIDF_CHECKED ) {
1161             if( rec->r.uid.uidflags & UIDF_VALID )
1162                 fputs(", valid", fp );
1163             if( rec->r.uid.uidflags & UIDF_REVOKED )
1164                 fputs(", revoked", fp );
1165         }
1166         putc('\n', fp);
1167         break;
1168       case RECTYPE_PREF:
1169         fprintf(fp, "pref %lu, next=%lu,",
1170                     rec->r.pref.lid, rec->r.pref.next);
1171         for(i=0,p=rec->r.pref.data; i < ITEMS_PER_PREF_RECORD; i+=2,p+=2 ) {
1172             if( *p )
1173                 fprintf(fp, " %c%d", *p == PREFTYPE_SYM    ? 'S' :
1174                                      *p == PREFTYPE_HASH   ? 'H' :
1175                                      *p == PREFTYPE_COMPR  ? 'Z' : '?', p[1]);
1176         }
1177         putc('\n', fp);
1178         break;
1179       case RECTYPE_SIG:
1180         fprintf(fp, "sig %lu, next=%lu,",
1181                          rec->r.sig.lid, rec->r.sig.next );
1182         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1183             if( rec->r.sig.sig[i].lid ) {
1184                 fprintf(fp, " %lu:", rec->r.sig.sig[i].lid );
1185                 if( rec->r.sig.sig[i].flag & SIGF_CHECKED ) {
1186                     fprintf(fp,"%c%c%c",
1187                        (rec->r.sig.sig[i].flag & SIGF_VALID)   ? 'V':
1188                        (rec->r.sig.sig[i].flag & SIGF_IGNORED) ? 'I':'-',
1189                        (rec->r.sig.sig[i].flag & SIGF_EXPIRED) ? 'E':'-',
1190                        (rec->r.sig.sig[i].flag & SIGF_REVOKED) ? 'R':'-');
1191                 }
1192                 else if( rec->r.sig.sig[i].flag & SIGF_NOPUBKEY)
1193                     fputs("?--", fp);
1194                 else
1195                     fputs("---", fp);
1196             }
1197         }
1198         putc('\n', fp);
1199         break;
1200       case RECTYPE_SDIR:
1201         fprintf(fp, "sdir %lu, keyid=%08lX%08lX, algo=%d, hint=%lu\n",
1202                     rec->r.sdir.lid,
1203                     (ulong)rec->r.sdir.keyid[0],
1204                     (ulong)rec->r.sdir.keyid[1],
1205                     rec->r.sdir.pubkey_algo,
1206                     (ulong)rec->r.sdir.hintlist );
1207         break;
1208       case RECTYPE_CACH:
1209         fprintf(fp, "cach\n");
1210         break;
1211       case RECTYPE_HTBL:
1212         fprintf(fp, "htbl,");
1213         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ )
1214             fprintf(fp, " %lu", rec->r.htbl.item[i] );
1215         putc('\n', fp);
1216         break;
1217       case RECTYPE_HLST:
1218         fprintf(fp, "hlst, next=%lu,", rec->r.hlst.next );
1219         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ )
1220             fprintf(fp, " %lu", rec->r.hlst.rnum[i] );
1221         putc('\n', fp);
1222         break;
1223       default:
1224         fprintf(fp, "unknown type %d\n", rec->rectype );
1225         break;
1226     }
1227 }
1228
1229 /****************
1230  * read the record with number recnum
1231  * returns: -1 on error, 0 on success
1232  */
1233 int
1234 tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
1235 {
1236     byte readbuf[TRUST_RECORD_LEN];
1237     const byte *buf, *p;
1238     int rc = 0;
1239     int n, i;
1240
1241     if( db_fd == -1 )
1242         open_db();
1243     buf = get_record_from_cache( recnum );
1244     if( !buf ) {
1245         if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
1246             log_error(_("trustdb: lseek failed: %s\n"), strerror(errno) );
1247             return G10ERR_READ_FILE;
1248         }
1249         n = read( db_fd, readbuf, TRUST_RECORD_LEN);
1250         if( !n ) {
1251             return -1; /* eof */
1252         }
1253         else if( n != TRUST_RECORD_LEN ) {
1254             log_error(_("trustdb: read failed (n=%d): %s\n"), n,
1255                                                         strerror(errno) );
1256             return G10ERR_READ_FILE;
1257         }
1258         buf = readbuf;
1259     }
1260     rec->recnum = recnum;
1261     rec->dirty = 0;
1262     p = buf;
1263     rec->rectype = *p++;
1264     if( expected && rec->rectype != expected ) {
1265         log_error("%lu: read expected rec type %d, got %d\n",
1266                     recnum, expected, rec->rectype );
1267         return G10ERR_TRUSTDB;
1268     }
1269     p++;    /* skip reserved byte */
1270     switch( rec->rectype ) {
1271       case 0:  /* unused (free) record */
1272         break;
1273       case RECTYPE_VER: /* version record */
1274         if( memcmp(buf+1, "gpg", 3 ) ) {
1275             log_error( _("%s: not a trustdb file\n"), db_name );
1276             rc = G10ERR_TRUSTDB;
1277         }
1278         p += 2; /* skip "pgp" */
1279         rec->r.ver.version  = *p++;
1280         rec->r.ver.marginals = *p++;
1281         rec->r.ver.completes = *p++;
1282         rec->r.ver.cert_depth = *p++;
1283         p += 4; /* lock flags */
1284         rec->r.ver.created  = buftoulong(p); p += 4;
1285         rec->r.ver.mod_down = buftoulong(p); p += 4;
1286         rec->r.ver.mod_up   = buftoulong(p); p += 4;
1287         rec->r.ver.keyhashtbl=buftoulong(p); p += 4;
1288         rec->r.ver.firstfree =buftoulong(p); p += 4;
1289         rec->r.ver.sdirhashtbl =buftoulong(p); p += 4;
1290         if( recnum ) {
1291             log_error( _("%s: version record with recnum %lu\n"), db_name,
1292                                                              (ulong)recnum );
1293             rc = G10ERR_TRUSTDB;
1294         }
1295         else if( rec->r.ver.version != 2 ) {
1296             log_error( _("%s: invalid file version %d\n"), db_name,
1297                                                         rec->r.ver.version );
1298             rc = G10ERR_TRUSTDB;
1299         }
1300         break;
1301       case RECTYPE_FREE:
1302         rec->r.free.next  = buftoulong(p); p += 4;
1303         break;
1304       case RECTYPE_DIR:   /*directory record */
1305         rec->r.dir.lid      = buftoulong(p); p += 4;
1306         rec->r.dir.keylist  = buftoulong(p); p += 4;
1307         rec->r.dir.uidlist  = buftoulong(p); p += 4;
1308         rec->r.dir.cacherec = buftoulong(p); p += 4;
1309         rec->r.dir.ownertrust = *p++;
1310         rec->r.dir.dirflags   = *p++;
1311         rec->r.dir.validity   = *p++;
1312         rec->r.dir.valcheck   = buftoulong(p); p += 4;
1313         rec->r.dir.checkat    = buftoulong(p); p += 4;
1314         switch( rec->r.dir.validity ) {
1315           case 0:
1316           case TRUST_UNDEFINED:
1317           case TRUST_NEVER:
1318           case TRUST_MARGINAL:
1319           case TRUST_FULLY:
1320           case TRUST_ULTIMATE:
1321             break;
1322           default:
1323             log_info("lid %lu: invalid validity value - cleared\n", recnum);
1324         }
1325         if( rec->r.dir.lid != recnum ) {
1326             log_error( "%s: dir LID != recnum (%lu,%lu)\n",
1327                               db_name, rec->r.dir.lid, (ulong)recnum );
1328             rc = G10ERR_TRUSTDB;
1329         }
1330         break;
1331       case RECTYPE_KEY:   /* public key record */
1332         rec->r.key.lid      = buftoulong(p); p += 4;
1333         rec->r.key.next     = buftoulong(p); p += 4;
1334         p += 7;
1335         rec->r.key.keyflags = *p++;
1336         rec->r.key.pubkey_algo = *p++;
1337         rec->r.key.fingerprint_len = *p++;
1338         if( rec->r.key.fingerprint_len < 1 || rec->r.key.fingerprint_len > 20 )
1339             rec->r.key.fingerprint_len = 20;
1340         memcpy( rec->r.key.fingerprint, p, 20);
1341         break;
1342       case RECTYPE_UID:   /* user id record */
1343         rec->r.uid.lid      = buftoulong(p); p += 4;
1344         rec->r.uid.next     = buftoulong(p); p += 4;
1345         rec->r.uid.prefrec  = buftoulong(p); p += 4;
1346         rec->r.uid.siglist  = buftoulong(p); p += 4;
1347         rec->r.uid.uidflags = *p++;
1348         rec->r.uid.validity   = *p++;
1349         switch( rec->r.uid.validity ) {
1350           case 0:
1351           case TRUST_UNDEFINED:
1352           case TRUST_NEVER:
1353           case TRUST_MARGINAL:
1354           case TRUST_FULLY:
1355           case TRUST_ULTIMATE:
1356             break;
1357           default:
1358             log_info("lid %lu: invalid validity value - cleared\n", recnum);
1359         }
1360         memcpy( rec->r.uid.namehash, p, 20);
1361         break;
1362       case RECTYPE_PREF:  /* preference record */
1363         rec->r.pref.lid     = buftoulong(p); p += 4;
1364         rec->r.pref.next    = buftoulong(p); p += 4;
1365         memcpy( rec->r.pref.data, p, 30 );
1366         break;
1367       case RECTYPE_SIG:
1368         rec->r.sig.lid     = buftoulong(p); p += 4;
1369         rec->r.sig.next    = buftoulong(p); p += 4;
1370         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1371             rec->r.sig.sig[i].lid  = buftoulong(p); p += 4;
1372             rec->r.sig.sig[i].flag = *p++;
1373         }
1374         break;
1375       case RECTYPE_SDIR:   /* shadow directory record */
1376         rec->r.sdir.lid     = buftoulong(p); p += 4;
1377         rec->r.sdir.keyid[0]= buftou32(p); p += 4;
1378         rec->r.sdir.keyid[1]= buftou32(p); p += 4;
1379         rec->r.sdir.pubkey_algo = *p++;
1380         p += 3;
1381         rec->r.sdir.hintlist = buftoulong(p);
1382         if( rec->r.sdir.lid != recnum ) {
1383             log_error( "%s: sdir LID != recnum (%lu,%lu)\n",
1384                                db_name, rec->r.sdir.lid, (ulong)recnum );
1385             rc = G10ERR_TRUSTDB;
1386         }
1387         break;
1388       case RECTYPE_CACH:   /* cache record */
1389         rec->r.cache.lid    = buftoulong(p); p += 4;
1390         memcpy(rec->r.cache.blockhash, p, 20); p += 20;
1391         rec->r.cache.trustlevel = *p++;
1392         break;
1393       case RECTYPE_HTBL:
1394         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
1395             rec->r.htbl.item[i] = buftoulong(p); p += 4;
1396         }
1397         break;
1398       case RECTYPE_HLST:
1399         rec->r.hlst.next = buftoulong(p); p += 4;
1400         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
1401             rec->r.hlst.rnum[i] = buftoulong(p); p += 4;
1402         }
1403         break;
1404       default:
1405         log_error( "%s: invalid record type %d at recnum %lu\n",
1406                                    db_name, rec->rectype, (ulong)recnum );
1407         rc = G10ERR_TRUSTDB;
1408         break;
1409     }
1410
1411     return rc;
1412 }
1413
1414 /****************
1415  * Write the record at RECNUM
1416  */
1417 int
1418 tdbio_write_record( TRUSTREC *rec )
1419 {
1420     byte buf[TRUST_RECORD_LEN], *p;
1421     int rc = 0;
1422     int i;
1423     ulong recnum = rec->recnum;
1424
1425     if( db_fd == -1 )
1426         open_db();
1427
1428     memset(buf, 0, TRUST_RECORD_LEN);
1429     p = buf;
1430     *p++ = rec->rectype; p++;
1431     switch( rec->rectype ) {
1432       case 0:  /* unused record */
1433         break;
1434       case RECTYPE_VER: /* version record */
1435         if( recnum )
1436             BUG();
1437         memcpy(p-1, "gpg", 3 ); p += 2;
1438         *p++ = rec->r.ver.version;
1439         *p++ = rec->r.ver.marginals;
1440         *p++ = rec->r.ver.completes;
1441         *p++ = rec->r.ver.cert_depth;
1442         p += 4; /* skip lock flags */
1443         ulongtobuf(p, rec->r.ver.created); p += 4;
1444         ulongtobuf(p, rec->r.ver.mod_down); p += 4;
1445         ulongtobuf(p, rec->r.ver.mod_up); p += 4;
1446         ulongtobuf(p, rec->r.ver.keyhashtbl); p += 4;
1447         ulongtobuf(p, rec->r.ver.firstfree ); p += 4;
1448         ulongtobuf(p, rec->r.ver.sdirhashtbl ); p += 4;
1449         break;
1450
1451       case RECTYPE_FREE:
1452         ulongtobuf(p, rec->r.free.next); p += 4;
1453         break;
1454
1455       case RECTYPE_DIR:   /*directory record */
1456         ulongtobuf(p, rec->r.dir.lid); p += 4;
1457         ulongtobuf(p, rec->r.dir.keylist); p += 4;
1458         ulongtobuf(p, rec->r.dir.uidlist); p += 4;
1459         ulongtobuf(p, rec->r.dir.cacherec); p += 4;
1460         *p++ = rec->r.dir.ownertrust;
1461         *p++ = rec->r.dir.dirflags;
1462         *p++ = rec->r.dir.validity;
1463         ulongtobuf(p, rec->r.dir.valcheck); p += 4;
1464         ulongtobuf(p, rec->r.dir.checkat); p += 4;
1465         assert( rec->r.dir.lid == recnum );
1466         break;
1467
1468       case RECTYPE_KEY:
1469         ulongtobuf(p, rec->r.key.lid); p += 4;
1470         ulongtobuf(p, rec->r.key.next); p += 4;
1471         p += 7;
1472         *p++ = rec->r.key.keyflags;
1473         *p++ = rec->r.key.pubkey_algo;
1474         *p++ = rec->r.key.fingerprint_len;
1475         memcpy( p, rec->r.key.fingerprint, 20); p += 20;
1476         break;
1477
1478       case RECTYPE_UID:   /* user id record */
1479         ulongtobuf(p, rec->r.uid.lid); p += 4;
1480         ulongtobuf(p, rec->r.uid.next); p += 4;
1481         ulongtobuf(p, rec->r.uid.prefrec); p += 4;
1482         ulongtobuf(p, rec->r.uid.siglist); p += 4;
1483         *p++ = rec->r.uid.uidflags;
1484         *p++ = rec->r.uid.validity;
1485         memcpy( p, rec->r.uid.namehash, 20 ); p += 20;
1486         break;
1487
1488       case RECTYPE_PREF:
1489         ulongtobuf(p, rec->r.pref.lid); p += 4;
1490         ulongtobuf(p, rec->r.pref.next); p += 4;
1491         memcpy( p, rec->r.pref.data, 30 );
1492         break;
1493
1494       case RECTYPE_SIG:
1495         ulongtobuf(p, rec->r.sig.lid); p += 4;
1496         ulongtobuf(p, rec->r.sig.next); p += 4;
1497         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1498             ulongtobuf(p, rec->r.sig.sig[i].lid); p += 4;
1499             *p++ = rec->r.sig.sig[i].flag;
1500         }
1501         break;
1502
1503       case RECTYPE_SDIR:
1504         ulongtobuf( p, rec->r.sdir.lid); p += 4;
1505         u32tobuf( p, rec->r.sdir.keyid[0] ); p += 4;
1506         u32tobuf( p, rec->r.sdir.keyid[1] ); p += 4;
1507         *p++ = rec->r.sdir.pubkey_algo;
1508         p += 3;
1509         ulongtobuf( p, rec->r.sdir.hintlist );
1510         break;
1511
1512       case RECTYPE_CACH:
1513         ulongtobuf(p, rec->r.cache.lid); p += 4;
1514         memcpy(p, rec->r.cache.blockhash, 20); p += 20;
1515         *p++ = rec->r.cache.trustlevel;
1516         break;
1517
1518       case RECTYPE_HTBL:
1519         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
1520             ulongtobuf( p, rec->r.htbl.item[i]); p += 4;
1521         }
1522         break;
1523
1524       case RECTYPE_HLST:
1525         ulongtobuf( p, rec->r.hlst.next); p += 4;
1526         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
1527             ulongtobuf( p, rec->r.hlst.rnum[i]); p += 4;
1528         }
1529         break;
1530
1531       default:
1532         BUG();
1533     }
1534
1535     rc = put_record_into_cache( recnum, buf );
1536     if( rc )
1537         ;
1538     else if( rec->rectype == RECTYPE_KEY )
1539         rc = update_keyhashtbl( rec );
1540     else if( rec->rectype == RECTYPE_SDIR )
1541         rc = update_sdirhashtbl( rec );
1542
1543     return rc;
1544 }
1545
1546 int
1547 tdbio_delete_record( ulong recnum )
1548 {
1549     TRUSTREC vr, rec;
1550     int rc;
1551
1552     /* Must read the record fist, so we can drop it from the hash tables */
1553     rc = tdbio_read_record( recnum, &rec, 0 );
1554     if( rc )
1555         ;
1556     else if( rec.rectype == RECTYPE_KEY )
1557         rc = drop_from_keyhashtbl( &rec );
1558     else if( rec.rectype == RECTYPE_SDIR )
1559         rc = drop_from_sdirhashtbl( &rec );
1560
1561     if( rc )
1562         return rc;
1563
1564     /* now we can chnage it to a free record */
1565     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
1566     if( rc )
1567         log_fatal( _("%s: error reading version record: %s\n"),
1568                                        db_name, g10_errstr(rc) );
1569
1570     rec.recnum = recnum;
1571     rec.rectype = RECTYPE_FREE;
1572     rec.r.free.next = vr.r.ver.firstfree;
1573     vr.r.ver.firstfree = recnum;
1574     rc = tdbio_write_record( &rec );
1575     if( !rc )
1576         rc = tdbio_write_record( &vr );
1577     return rc;
1578 }
1579
1580 /****************
1581  * create a new record and return its record number
1582  */
1583 ulong
1584 tdbio_new_recnum()
1585 {
1586     off_t offset;
1587     ulong recnum;
1588     TRUSTREC vr, rec;
1589     int rc;
1590
1591     /* look for unused records */
1592     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
1593     if( rc )
1594         log_fatal( _("%s: error reading version record: %s\n"),
1595                                              db_name, g10_errstr(rc) );
1596     if( vr.r.ver.firstfree ) {
1597         recnum = vr.r.ver.firstfree;
1598         rc = tdbio_read_record( recnum, &rec, RECTYPE_FREE );
1599         if( rc ) {
1600             log_error( _("%s: error reading free record: %s\n"),
1601                                                   db_name,  g10_errstr(rc) );
1602             return rc;
1603         }
1604         /* update dir record */
1605         vr.r.ver.firstfree = rec.r.free.next;
1606         rc = tdbio_write_record( &vr );
1607         if( rc ) {
1608             log_error( _("%s: error writing dir record: %s\n"),
1609                                                      db_name, g10_errstr(rc) );
1610             return rc;
1611         }
1612         /*zero out the new record */
1613         memset( &rec, 0, sizeof rec );
1614         rec.rectype = 0; /* unused record */
1615         rec.recnum = recnum;
1616         rc = tdbio_write_record( &rec );
1617         if( rc )
1618             log_fatal(_("%s: failed to zero a record: %s\n"),
1619                                        db_name, g10_errstr(rc));
1620     }
1621     else { /* not found, append a new record */
1622         offset = lseek( db_fd, 0, SEEK_END );
1623         if( offset == -1 )
1624             log_fatal("trustdb: lseek to end failed: %s\n", strerror(errno) );
1625         recnum = offset / TRUST_RECORD_LEN;
1626         assert(recnum); /* this is will never be the first record */
1627         /* we must write a record, so that the next call to this function
1628          * returns another recnum */
1629         memset( &rec, 0, sizeof rec );
1630         rec.rectype = 0; /* unused record */
1631         rec.recnum = recnum;
1632         rc = 0;
1633         if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
1634             log_error(_("trustdb rec %lu: lseek failed: %s\n"),
1635                                                 recnum, strerror(errno) );
1636             rc = G10ERR_WRITE_FILE;
1637         }
1638         else {
1639             int n = write( db_fd, &rec, TRUST_RECORD_LEN);
1640             if( n != TRUST_RECORD_LEN ) {
1641                 log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
1642                                                  recnum, n, strerror(errno) );
1643                 rc = G10ERR_WRITE_FILE;
1644             }
1645         }
1646
1647         if( rc )
1648             log_fatal(_("%s: failed to append a record: %s\n"),
1649                                     db_name,    g10_errstr(rc));
1650     }
1651     return recnum ;
1652 }
1653
1654
1655
1656 /****************
1657  * Search the trustdb for a key which matches PK and return the dir record
1658  * The local_id of PK is set to the correct value
1659  */
1660 int
1661 tdbio_search_dir_bypk( PKT_public_key *pk, TRUSTREC *rec )
1662 {
1663     byte fingerprint[MAX_FINGERPRINT_LEN];
1664     size_t fingerlen;
1665     u32 keyid[2];
1666     int rc;
1667
1668     keyid_from_pk( pk, keyid );
1669     fingerprint_from_pk( pk, fingerprint, &fingerlen );
1670     rc = tdbio_search_dir_byfpr( fingerprint, fingerlen,
1671                                  pk->pubkey_algo, rec );
1672
1673     if( !rc ) {
1674         if( pk->local_id && pk->local_id != rec->recnum )
1675             log_error("%s: found record, but LID from memory does "
1676                        "not match recnum (%lu,%lu)\n",
1677                             db_name,  pk->local_id, rec->recnum );
1678         pk->local_id = rec->recnum;
1679     }
1680     return rc;
1681 }
1682
1683
1684 static int
1685 cmp_krec_fpr( void *dataptr, const TRUSTREC *rec )
1686 {
1687     const struct cmp_krec_fpr_struct *d = dataptr;
1688
1689     return rec->rectype == RECTYPE_KEY
1690            && ( !d->pubkey_algo || rec->r.key.pubkey_algo == d->pubkey_algo )
1691            && rec->r.key.fingerprint_len == d->fprlen
1692            && !memcmp( rec->r.key.fingerprint, d->fpr, d->fprlen );
1693 }
1694
1695 int
1696 tdbio_search_dir_byfpr( const byte *fingerprint, size_t fingerlen,
1697                         int pubkey_algo, TRUSTREC *rec )
1698 {
1699     struct cmp_krec_fpr_struct cmpdata;
1700     ulong recnum;
1701     int rc;
1702
1703     assert( fingerlen == 20 || fingerlen == 16 );
1704
1705     /* locate the key using the hash table */
1706     cmpdata.pubkey_algo = pubkey_algo;
1707     cmpdata.fpr = fingerprint;
1708     cmpdata.fprlen = fingerlen;
1709     rc = lookup_hashtable( get_keyhashrec(), fingerprint, fingerlen,
1710                            cmp_krec_fpr, &cmpdata, rec );
1711     if( !rc ) {
1712         recnum = rec->r.key.lid;
1713         /* Now read the dir record */
1714         rc = tdbio_read_record( recnum, rec, RECTYPE_DIR);
1715         if( rc )
1716             log_error("%s: can't read dirrec %lu: %s\n",
1717                                      db_name, recnum, g10_errstr(rc) );
1718     }
1719     return rc;
1720 }
1721
1722
1723
1724 static int
1725 cmp_sdir( void *dataptr, const TRUSTREC *rec )
1726 {
1727     const struct cmp_xdir_struct *d = dataptr;
1728
1729     return rec->rectype == RECTYPE_SDIR
1730            && ( !d->pubkey_algo || rec->r.sdir.pubkey_algo == d->pubkey_algo )
1731            && rec->r.sdir.keyid[0] == d->keyid[0]
1732            && rec->r.sdir.keyid[1] == d->keyid[1];
1733 }
1734
1735
1736 int
1737 tdbio_search_sdir( u32 *keyid, int pubkey_algo, TRUSTREC *rec )
1738 {
1739     struct cmp_xdir_struct cmpdata;
1740     int rc;
1741     byte key[8];
1742
1743     /* locate the shadow dir record using the hash table */
1744     u32tobuf( key   , keyid[0] );
1745     u32tobuf( key+4 , keyid[1] );
1746     cmpdata.pubkey_algo = pubkey_algo;
1747     cmpdata.keyid[0] = keyid[0];
1748     cmpdata.keyid[1] = keyid[1];
1749     rc = lookup_hashtable( get_sdirhashrec(), key, 8,
1750                            cmp_sdir, &cmpdata, rec );
1751     return rc;
1752 }
1753
1754
1755 void
1756 tdbio_invalid(void)
1757 {
1758     log_error(_(
1759         "the trustdb is corrupted; please run \"gpg --fix-trustdb\".\n") );
1760     g10_exit(2);
1761 }
1762
1763