See ChangeLog: Tue Mar 2 16:44:57 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
86 static void open_db(void);
87
88 \f
89 /*************************************
90  ************* record cache **********
91  *************************************/
92
93 /****************
94  * Get the data from therecord cache and return a
95  * pointer into that cache.  Caller should copy
96  * the return data.  NULL is returned on a cache miss.
97  */
98 static const char *
99 get_record_from_cache( ulong recno )
100 {
101     CACHE_CTRL r;
102
103     for( r = cache_list; r; r = r->next ) {
104         if( r->flags.used && r->recno == recno )
105             return r->data;
106     }
107     return NULL;
108 }
109
110
111 static int
112 write_cache_item( CACHE_CTRL r )
113 {
114     int n;
115
116     if( lseek( db_fd, r->recno * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
117         log_error(_("trustdb rec %lu: lseek failed: %s\n"),
118                                             r->recno, strerror(errno) );
119         return G10ERR_WRITE_FILE;
120     }
121     n = write( db_fd, r->data, TRUST_RECORD_LEN);
122     if( n != TRUST_RECORD_LEN ) {
123         log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
124                                             r->recno, n, strerror(errno) );
125         return G10ERR_WRITE_FILE;
126     }
127     r->flags.dirty = 0;
128     return 0;
129 }
130
131 /****************
132  * Put data into the cache.  This function may flush the
133  * some cache entries if there is not enough space available.
134  */
135 int
136 put_record_into_cache( ulong recno, const char *data )
137 {
138     CACHE_CTRL r, unused;
139     int dirty_count = 0;
140     int clean_count = 0;
141
142     /* see whether we already cached this one */
143     for( unused = NULL, r = cache_list; r; r = r->next ) {
144         if( !r->flags.used ) {
145             if( !unused )
146                 unused = r;
147         }
148         else if( r->recno == recno ) {
149             if( !r->flags.dirty ) {
150                 /* Hmmm: should we use a a copy and compare? */
151                 if( memcmp(r->data, data, TRUST_RECORD_LEN ) ) {
152                     r->flags.dirty = 1;
153                     cache_is_dirty = 1;
154                 }
155             }
156             memcpy( r->data, data, TRUST_RECORD_LEN );
157             return 0;
158         }
159         if( r->flags.used ) {
160             if( r->flags.dirty )
161                 dirty_count++;
162             else
163                 clean_count++;
164         }
165     }
166     /* not in the cache: add a new entry */
167     if( unused ) { /* reuse this entry */
168         r = unused;
169         r->flags.used = 1;
170         r->recno = recno;
171         memcpy( r->data, data, TRUST_RECORD_LEN );
172         r->flags.dirty = 1;
173         cache_is_dirty = 1;
174         cache_entries++;
175         return 0;
176     }
177     /* see whether we reached the limit */
178     if( cache_entries < MAX_CACHE_ENTRIES_SOFT ) { /* no */
179         r = m_alloc( sizeof *r );
180         r->flags.used = 1;
181         r->recno = recno;
182         memcpy( r->data, data, TRUST_RECORD_LEN );
183         r->flags.dirty = 1;
184         r->next = cache_list;
185         cache_list = r;
186         cache_is_dirty = 1;
187         cache_entries++;
188         return 0;
189     }
190     /* cache is full: discard some clean entries */
191     if( clean_count ) {
192         int n = clean_count / 3; /* discard a third of the clean entries */
193         if( !n )
194             n = 1;
195         for( unused = NULL, r = cache_list; r; r = r->next ) {
196             if( r->flags.used && !r->flags.dirty ) {
197                 if( !unused )
198                     unused = r;
199                 r->flags.used = 0;
200                 cache_entries--;
201                 if( !--n )
202                     break;
203             }
204         }
205         assert( unused );
206         r = unused;
207         r->flags.used = 1;
208         r->recno = recno;
209         memcpy( r->data, data, TRUST_RECORD_LEN );
210         r->flags.dirty = 1;
211         cache_is_dirty = 1;
212         cache_entries++;
213         return 0;
214     }
215     /* no clean entries: have to flush some dirty entries */
216     if( in_transaction ) {
217         /* but we can't do this while in a transaction
218          * we increase the cache size instead */
219         if( cache_entries < MAX_CACHE_ENTRIES_HARD ) { /* no */
220             if( opt.debug && !(cache_entries % 100) )
221                 log_debug("increasing tdbio cache size\n");
222             r = m_alloc( sizeof *r );
223             r->flags.used = 1;
224             r->recno = recno;
225             memcpy( r->data, data, TRUST_RECORD_LEN );
226             r->flags.dirty = 1;
227             r->next = cache_list;
228             cache_list = r;
229             cache_is_dirty = 1;
230             cache_entries++;
231             return 0;
232         }
233         log_info(_("trustdb transaction too large\n"));
234         return G10ERR_RESOURCE_LIMIT;
235     }
236     if( dirty_count ) {
237         int n = dirty_count / 5; /* discard some dirty entries */
238         if( !n )
239             n = 1;
240         if( !is_locked ) {
241             if( make_dotlock( lockhandle, -1 ) )
242                 log_fatal("can't acquire lock - giving up\n");
243             else
244                 is_locked = 1;
245         }
246         for( unused = NULL, r = cache_list; r; r = r->next ) {
247             if( r->flags.used && r->flags.dirty ) {
248                 int rc = write_cache_item( r );
249                 if( rc )
250                     return rc;
251                 if( !unused )
252                     unused = r;
253                 r->flags.used = 0;
254                 cache_entries--;
255                 if( !--n )
256                     break;
257             }
258         }
259         if( !opt.lock_once ) {
260             if( !release_dotlock( lockhandle ) )
261                 is_locked = 0;
262         }
263         assert( unused );
264         r = unused;
265         r->flags.used = 1;
266         r->recno = recno;
267         memcpy( r->data, data, TRUST_RECORD_LEN );
268         r->flags.dirty = 1;
269         cache_is_dirty = 1;
270         cache_entries++;
271         return 0;
272     }
273     BUG();
274 }
275
276
277 int
278 tdbio_is_dirty()
279 {
280     return cache_is_dirty;
281 }
282
283
284 /****************
285  * Flush the cache.  This cannot be used while in a transaction.
286  */
287 int
288 tdbio_sync()
289 {
290     CACHE_CTRL r;
291     int did_lock = 0;
292
293     if( db_fd == -1 )
294         open_db();
295     if( in_transaction )
296         log_bug("tdbio: syncing while in transaction\n");
297
298     if( !cache_is_dirty )
299         return 0;
300
301     if( !is_locked ) {
302         if( make_dotlock( lockhandle, -1 ) )
303             log_fatal("can't acquire lock - giving up\n");
304         else
305             is_locked = 1;
306         did_lock = 1;
307     }
308     for( r = cache_list; r; r = r->next ) {
309         if( r->flags.used && r->flags.dirty ) {
310             int rc = write_cache_item( r );
311             if( rc )
312                 return rc;
313         }
314     }
315     cache_is_dirty = 0;
316     if( did_lock && !opt.lock_once ) {
317         if( !release_dotlock( lockhandle ) )
318             is_locked = 0;
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 record number of the keyhash tbl or create a new one.
613  */
614 static ulong
615 get_keyhashrec(void)
616 {
617     static ulong keyhashtbl; /* record number of the key hashtable */
618
619     if( !keyhashtbl ) {
620         TRUSTREC vr;
621         int rc;
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         if( !vr.r.ver.keyhashtbl )
628             create_hashtable( &vr, 0 );
629
630         keyhashtbl = vr.r.ver.keyhashtbl;
631     }
632     return keyhashtbl;
633 }
634
635 /****************
636  * Return the record number of the shadow direcory hash table
637  * or create a new one.
638  */
639 static ulong
640 get_sdirhashrec(void)
641 {
642     static ulong sdirhashtbl; /* record number of the hashtable */
643
644     if( !sdirhashtbl ) {
645         TRUSTREC vr;
646         int rc;
647
648         rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
649         if( rc )
650             log_fatal( _("%s: error reading version record: %s\n"),
651                                                     db_name, g10_errstr(rc) );
652         if( !vr.r.ver.sdirhashtbl )
653             create_hashtable( &vr, 1 );
654
655         sdirhashtbl = vr.r.ver.sdirhashtbl;
656     }
657     return sdirhashtbl;
658 }
659
660
661 /****************
662  * Update a hashtable.
663  * table gives the start of the table, key and keylen is the key,
664  * newrecnum is the record number to insert.
665  */
666 static int
667 upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
668 {
669     TRUSTREC lastrec, rec;
670     ulong hashrec, item;
671     int msb;
672     int level=0;
673     int rc, i;
674
675     hashrec = table;
676   next_level:
677     msb = key[level];
678     hashrec += msb / ITEMS_PER_HTBL_RECORD;
679     rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
680     if( rc ) {
681         log_error( db_name, "upd_hashtable: read failed: %s\n",
682                                                         g10_errstr(rc) );
683         return rc;
684     }
685
686     item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
687     if( !item ) { /* insert a new item into the hash table */
688         rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = newrecnum;
689         rc = tdbio_write_record( &rec );
690         if( rc ) {
691             log_error( db_name, "upd_hashtable: write htbl failed: %s\n",
692                                                             g10_errstr(rc) );
693             return rc;
694         }
695     }
696     else if( item != newrecnum ) {  /* must do an update */
697         lastrec = rec;
698         rc = tdbio_read_record( item, &rec, 0 );
699         if( rc ) {
700             log_error( "upd_hashtable: read item failed: %s\n",
701                                                             g10_errstr(rc) );
702             return rc;
703         }
704
705         if( rec.rectype == RECTYPE_HTBL ) {
706             hashrec = item;
707             level++;
708             if( level >= keylen ) {
709                 log_error( "hashtable has invalid indirections.\n");
710                 return G10ERR_TRUSTDB;
711             }
712             goto next_level;
713         }
714         else if( rec.rectype == RECTYPE_HLST ) { /* extend list */
715             /* see whether the key is already in this list */
716             for(;;) {
717                 for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
718                     if( rec.r.hlst.rnum[i] == newrecnum ) {
719                         return 0; /* okay, already in the list */
720                     }
721                 }
722                 if( rec.r.hlst.next ) {
723                     rc = tdbio_read_record( rec.r.hlst.next,
724                                                        &rec, RECTYPE_HLST);
725                     if( rc ) {
726                         log_error( "scan keyhashtbl read hlst failed: %s\n",
727                                                              g10_errstr(rc) );
728                         return rc;
729                     }
730                 }
731                 else
732                     break; /* not there */
733             }
734             /* find the next free entry and put it in */
735             for(;;) {
736                 for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
737                     if( !rec.r.hlst.rnum[i] ) {
738                         rec.r.hlst.rnum[i] = newrecnum;
739                         rc = tdbio_write_record( &rec );
740                         if( rc )
741                             log_error( "upd_hashtable: write hlst failed: %s\n",
742                                                               g10_errstr(rc) );
743                         return rc; /* done */
744                     }
745                 }
746                 if( rec.r.hlst.next ) {
747                     rc = tdbio_read_record( rec.r.hlst.next,
748                                                       &rec, RECTYPE_HLST );
749                     if( rc ) {
750                         log_error( "upd_hashtable: read hlst failed: %s\n",
751                                                              g10_errstr(rc) );
752                         return rc;
753                     }
754                 }
755                 else { /* add a new list record */
756                     rec.r.hlst.next = item = tdbio_new_recnum();
757                     rc = tdbio_write_record( &rec );
758                     if( rc ) {
759                         log_error( "upd_hashtable: write hlst failed: %s\n",
760                                                           g10_errstr(rc) );
761                         return rc;
762                     }
763                     memset( &rec, 0, sizeof rec );
764                     rec.rectype = RECTYPE_HLST;
765                     rec.recnum = item;
766                     rec.r.hlst.rnum[0] = newrecnum;
767                     rc = tdbio_write_record( &rec );
768                     if( rc )
769                         log_error( "upd_hashtable: write ext hlst failed: %s\n",
770                                                           g10_errstr(rc) );
771                     return rc; /* done */
772                 }
773             } /* end loop over hlst slots */
774         }
775         else if( rec.rectype == RECTYPE_KEY
776                  || rec.rectype == RECTYPE_DIR
777                  || rec.rectype == RECTYPE_SDIR ) { /* insert a list record */
778             if( rec.recnum == newrecnum ) {
779                 return 0;
780             }
781             item = rec.recnum; /* save number of key record */
782             memset( &rec, 0, sizeof rec );
783             rec.rectype = RECTYPE_HLST;
784             rec.recnum = tdbio_new_recnum();
785             rec.r.hlst.rnum[0] = item;       /* old keyrecord */
786             rec.r.hlst.rnum[1] = newrecnum; /* and new one */
787             rc = tdbio_write_record( &rec );
788             if( rc ) {
789                 log_error( "upd_hashtable: write new hlst failed: %s\n",
790                                                   g10_errstr(rc) );
791                 return rc;
792             }
793             /* update the hashtable record */
794             lastrec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = rec.recnum;
795             rc = tdbio_write_record( &lastrec );
796             if( rc )
797                 log_error( "upd_hashtable: update htbl failed: %s\n",
798                                                              g10_errstr(rc) );
799             return rc; /* ready */
800         }
801         else {
802             log_error( "hashtbl %lu points to an invalid record\n",
803                                                                     item);
804             return G10ERR_TRUSTDB;
805         }
806     }
807
808     return 0;
809 }
810
811
812
813 /****************
814  * Lookup a record via the hashtable tablewith key/keylen and return the
815  * result in rec.  cmp() should return if the record is the desired one.
816  * Returns -1 if not found, 0 if found or another errocode
817  */
818 static int
819 lookup_hashtable( ulong table, const byte *key, size_t keylen,
820                   int (*cmpfnc)(void*, const TRUSTREC *), void *cmpdata,
821                                                 TRUSTREC *rec )
822 {
823     int rc;
824     ulong hashrec, item;
825     int msb;
826     int level=0;
827
828     hashrec = table;
829   next_level:
830     msb = key[level];
831     hashrec += msb / ITEMS_PER_HTBL_RECORD;
832     rc = tdbio_read_record( hashrec, rec, RECTYPE_HTBL );
833     if( rc ) {
834         log_error( db_name, "lookup_hashtable failed: %s\n", g10_errstr(rc) );
835         return rc;
836     }
837
838     item = rec->r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
839     if( !item )
840         return -1; /* not found */
841
842     rc = tdbio_read_record( item, rec, 0 );
843     if( rc ) {
844         log_error( db_name, "hashtable read failed: %s\n", g10_errstr(rc) );
845         return rc;
846     }
847     if( rec->rectype == RECTYPE_HTBL ) {
848         hashrec = item;
849         level++;
850         if( level >= keylen ) {
851             log_error( db_name, "hashtable has invalid indirections\n");
852             return G10ERR_TRUSTDB;
853         }
854         goto next_level;
855     }
856     else if( rec->rectype == RECTYPE_HLST ) {
857         for(;;) {
858             int i;
859
860             for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
861                 if( rec->r.hlst.rnum[i] ) {
862                     TRUSTREC tmp;
863
864                     rc = tdbio_read_record( rec->r.hlst.rnum[i], &tmp, 0 );
865                     if( rc ) {
866                         log_error( "lookup_hashtable: read item failed: %s\n",
867                                                               g10_errstr(rc) );
868                         return rc;
869                     }
870                     if( (*cmpfnc)( cmpdata, &tmp ) ) {
871                         *rec = tmp;
872                         return 0;
873                     }
874                 }
875             }
876             if( rec->r.hlst.next ) {
877                 rc = tdbio_read_record( rec->r.hlst.next, rec, RECTYPE_HLST );
878                 if( rc ) {
879                     log_error( "lookup_hashtable: read hlst failed: %s\n",
880                                                          g10_errstr(rc) );
881                     return rc;
882                 }
883             }
884             else
885                 return -1; /* not found */
886         }
887     }
888
889
890     if( (*cmpfnc)( cmpdata, rec ) )
891         return 0; /* really found */
892
893     return -1; /* no: not found */
894 }
895
896
897
898
899 /****************
900  * Update the key hashtbl or create the table if it does not exist
901  */
902 static int
903 update_keyhashtbl( TRUSTREC *kr )
904 {
905     return upd_hashtable( get_keyhashrec(),
906                           kr->r.key.fingerprint,
907                           kr->r.key.fingerprint_len, kr->recnum );
908 }
909
910 /****************
911  * Update the shadow dir hashtbl or create the table if it does not exist
912  */
913 static int
914 update_sdirhashtbl( TRUSTREC *sr )
915 {
916     byte key[8];
917
918     u32tobuf( key   , sr->r.sdir.keyid[0] );
919     u32tobuf( key+4 , sr->r.sdir.keyid[1] );
920     return upd_hashtable( get_sdirhashrec(), key, 8, sr->recnum );
921 }
922
923
924
925
926 void
927 tdbio_dump_record( TRUSTREC *rec, FILE *fp  )
928 {
929     int i;
930     ulong rnum = rec->recnum;
931     byte *p;
932
933     fprintf(fp, "rec %5lu, ", rnum );
934
935     switch( rec->rectype ) {
936       case 0: fprintf(fp, "blank\n");
937         break;
938       case RECTYPE_VER: fprintf(fp,
939             "version, kd=%lu, sd=%lu, free=%lu, m/c/d=%d/%d/%d\n",
940             rec->r.ver.keyhashtbl, rec->r.ver.sdirhashtbl,
941                                    rec->r.ver.firstfree,
942                                    rec->r.ver.marginals,
943                                    rec->r.ver.completes,
944                                    rec->r.ver.cert_depth );
945         break;
946       case RECTYPE_FREE: fprintf(fp, "free, next=%lu\n", rec->r.free.next );
947         break;
948       case RECTYPE_DIR:
949         fprintf(fp, "dir %lu, keys=%lu, uids=%lu, t=%02x",
950                     rec->r.dir.lid,
951                     rec->r.dir.keylist,
952                     rec->r.dir.uidlist,
953                     rec->r.dir.ownertrust );
954         if( rec->r.dir.dirflags & DIRF_VALVALID )
955             fprintf( fp, ", v=%02x", rec->r.dir.validity );
956         if( rec->r.dir.dirflags & DIRF_CHECKED ) {
957             if( rec->r.dir.dirflags & DIRF_VALID )
958                 fputs(", valid", fp );
959             if( rec->r.dir.dirflags & DIRF_EXPIRED )
960                 fputs(", expired", fp );
961             if( rec->r.dir.dirflags & DIRF_REVOKED )
962                 fputs(", revoked", fp );
963         }
964         putc('\n', fp);
965         break;
966       case RECTYPE_KEY:
967         fprintf(fp, "key %lu, n=%lu a=%d ",
968                    rec->r.key.lid,
969                    rec->r.key.next,
970                    rec->r.key.pubkey_algo );
971         for(i=0; i < rec->r.key.fingerprint_len; i++ )
972             fprintf(fp, "%02X", rec->r.key.fingerprint[i] );
973         if( rec->r.key.keyflags & KEYF_CHECKED ) {
974             if( rec->r.key.keyflags & KEYF_VALID )
975                 fputs(", valid", fp );
976             if( rec->r.key.keyflags & KEYF_EXPIRED )
977                 fputs(", expired", fp );
978             if( rec->r.key.keyflags & KEYF_REVOKED )
979                 fputs(", revoked", fp );
980         }
981         putc('\n', fp);
982         break;
983       case RECTYPE_UID:
984         fprintf(fp, "uid %lu, next=%lu, pref=%lu, sig=%lu, hash=%02X%02X",
985                     rec->r.uid.lid,
986                     rec->r.uid.next,
987                     rec->r.uid.prefrec,
988                     rec->r.uid.siglist,
989                     rec->r.uid.namehash[18], rec->r.uid.namehash[19]);
990         if( rec->r.uid.uidflags & UIDF_VALVALID )
991             fprintf( fp, ", v=%02x", rec->r.uid.validity );
992         if( rec->r.uid.uidflags & UIDF_CHECKED ) {
993             if( rec->r.uid.uidflags & UIDF_VALID )
994                 fputs(", valid", fp );
995             if( rec->r.uid.uidflags & UIDF_REVOKED )
996                 fputs(", revoked", fp );
997         }
998         putc('\n', fp);
999         break;
1000       case RECTYPE_PREF:
1001         fprintf(fp, "pref %lu, next=%lu,",
1002                     rec->r.pref.lid, rec->r.pref.next);
1003         for(i=0,p=rec->r.pref.data; i < ITEMS_PER_PREF_RECORD; i+=2,p+=2 ) {
1004             if( *p )
1005                 fprintf(fp, " %c%d", *p == PREFTYPE_SYM    ? 'S' :
1006                                      *p == PREFTYPE_HASH   ? 'H' :
1007                                      *p == PREFTYPE_COMPR  ? 'Z' : '?', p[1]);
1008         }
1009         putc('\n', fp);
1010         break;
1011       case RECTYPE_SIG:
1012         fprintf(fp, "sig %lu, next=%lu,",
1013                          rec->r.sig.lid, rec->r.sig.next );
1014         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1015             if( rec->r.sig.sig[i].lid ) {
1016                 fprintf(fp, " %lu:", rec->r.sig.sig[i].lid );
1017                 if( rec->r.sig.sig[i].flag & SIGF_CHECKED ) {
1018                     fprintf(fp,"%c%c%c",
1019                        (rec->r.sig.sig[i].flag & SIGF_VALID)   ? 'V':'-',
1020                        (rec->r.sig.sig[i].flag & SIGF_EXPIRED) ? 'E':'-',
1021                        (rec->r.sig.sig[i].flag & SIGF_REVOKED) ? 'R':'-');
1022                 }
1023                 else if( rec->r.sig.sig[i].flag & SIGF_NOPUBKEY)
1024                     fputs("?--", fp);
1025                 else
1026                     fputs("---", fp);
1027             }
1028         }
1029         putc('\n', fp);
1030         break;
1031       case RECTYPE_SDIR:
1032         fprintf(fp, "sdir %lu, keyid=%08lX%08lX, algo=%d, hint=%lu\n",
1033                     rec->r.sdir.lid,
1034                     (ulong)rec->r.sdir.keyid[0],
1035                     (ulong)rec->r.sdir.keyid[1],
1036                     rec->r.sdir.pubkey_algo,
1037                     (ulong)rec->r.sdir.hintlist );
1038         break;
1039       case RECTYPE_CACH:
1040         fprintf(fp, "cach\n");
1041         break;
1042       case RECTYPE_HTBL:
1043         fprintf(fp, "htbl,");
1044         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ )
1045             fprintf(fp, " %lu", rec->r.htbl.item[i] );
1046         putc('\n', fp);
1047         break;
1048       case RECTYPE_HLST:
1049         fprintf(fp, "hlst, next=%lu,", rec->r.hlst.next );
1050         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ )
1051             fprintf(fp, " %lu", rec->r.hlst.rnum[i] );
1052         putc('\n', fp);
1053         break;
1054       default:
1055         fprintf(fp, "unknown type %d\n", rec->rectype );
1056         break;
1057     }
1058 }
1059
1060 /****************
1061  * read the record with number recnum
1062  * returns: -1 on error, 0 on success
1063  */
1064 int
1065 tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
1066 {
1067     byte readbuf[TRUST_RECORD_LEN];
1068     const byte *buf, *p;
1069     int rc = 0;
1070     int n, i;
1071
1072     if( db_fd == -1 )
1073         open_db();
1074     buf = get_record_from_cache( recnum );
1075     if( !buf ) {
1076         if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
1077             log_error(_("trustdb: lseek failed: %s\n"), strerror(errno) );
1078             return G10ERR_READ_FILE;
1079         }
1080         n = read( db_fd, readbuf, TRUST_RECORD_LEN);
1081         if( !n ) {
1082             return -1; /* eof */
1083         }
1084         else if( n != TRUST_RECORD_LEN ) {
1085             log_error(_("trustdb: read failed (n=%d): %s\n"), n,
1086                                                         strerror(errno) );
1087             return G10ERR_READ_FILE;
1088         }
1089         buf = readbuf;
1090     }
1091     rec->recnum = recnum;
1092     rec->dirty = 0;
1093     p = buf;
1094     rec->rectype = *p++;
1095     if( expected && rec->rectype != expected ) {
1096         log_error("%lu: read expected rec type %d, got %d\n",
1097                     recnum, expected, rec->rectype );
1098         return G10ERR_TRUSTDB;
1099     }
1100     p++;    /* skip reserved byte */
1101     switch( rec->rectype ) {
1102       case 0:  /* unused (free) record */
1103         break;
1104       case RECTYPE_VER: /* version record */
1105         if( memcmp(buf+1, "gpg", 3 ) ) {
1106             log_error( _("%s: not a trustdb file\n"), db_name );
1107             rc = G10ERR_TRUSTDB;
1108         }
1109         p += 2; /* skip "pgp" */
1110         rec->r.ver.version  = *p++;
1111         rec->r.ver.marginals = *p++;
1112         rec->r.ver.completes = *p++;
1113         rec->r.ver.cert_depth = *p++;
1114         p += 4; /* lock flags */
1115         rec->r.ver.created  = buftoulong(p); p += 4;
1116         rec->r.ver.modified = buftoulong(p); p += 4;
1117         rec->r.ver.validated= buftoulong(p); p += 4;
1118         rec->r.ver.keyhashtbl=buftoulong(p); p += 4;
1119         rec->r.ver.firstfree =buftoulong(p); p += 4;
1120         rec->r.ver.sdirhashtbl =buftoulong(p); p += 4;
1121         if( recnum ) {
1122             log_error( _("%s: version record with recnum %lu\n"), db_name,
1123                                                              (ulong)recnum );
1124             rc = G10ERR_TRUSTDB;
1125         }
1126         else if( rec->r.ver.version != 2 ) {
1127             log_error( _("%s: invalid file version %d\n"), db_name,
1128                                                         rec->r.ver.version );
1129             rc = G10ERR_TRUSTDB;
1130         }
1131         break;
1132       case RECTYPE_FREE:
1133         rec->r.free.next  = buftoulong(p); p += 4;
1134         break;
1135       case RECTYPE_DIR:   /*directory record */
1136         rec->r.dir.lid      = buftoulong(p); p += 4;
1137         rec->r.dir.keylist  = buftoulong(p); p += 4;
1138         rec->r.dir.uidlist  = buftoulong(p); p += 4;
1139         rec->r.dir.cacherec = buftoulong(p); p += 4;
1140         rec->r.dir.ownertrust = *p++;
1141         rec->r.dir.dirflags   = *p++;
1142         rec->r.dir.validity   = *p++;
1143         switch( rec->r.dir.validity ) {
1144           case 0:
1145           case TRUST_UNDEFINED:
1146           case TRUST_NEVER:
1147           case TRUST_MARGINAL:
1148           case TRUST_FULLY:
1149           case TRUST_ULTIMATE:
1150             break;
1151           default:
1152             log_info("lid %lu: invalid validity value - cleared\n", recnum);
1153         }
1154         if( rec->r.dir.lid != recnum ) {
1155             log_error( "%s: dir LID != recnum (%lu,%lu)\n",
1156                               db_name, rec->r.dir.lid, (ulong)recnum );
1157             rc = G10ERR_TRUSTDB;
1158         }
1159         break;
1160       case RECTYPE_KEY:   /* public key record */
1161         rec->r.key.lid      = buftoulong(p); p += 4;
1162         rec->r.key.next     = buftoulong(p); p += 4;
1163         p += 7;
1164         rec->r.key.keyflags = *p++;
1165         rec->r.key.pubkey_algo = *p++;
1166         rec->r.key.fingerprint_len = *p++;
1167         if( rec->r.key.fingerprint_len < 1 || rec->r.key.fingerprint_len > 20 )
1168             rec->r.key.fingerprint_len = 20;
1169         memcpy( rec->r.key.fingerprint, p, 20);
1170         break;
1171       case RECTYPE_UID:   /* user id record */
1172         rec->r.uid.lid      = buftoulong(p); p += 4;
1173         rec->r.uid.next     = buftoulong(p); p += 4;
1174         rec->r.uid.prefrec  = buftoulong(p); p += 4;
1175         rec->r.uid.siglist  = buftoulong(p); p += 4;
1176         rec->r.uid.uidflags = *p++;
1177         rec->r.uid.validity   = *p++;
1178         switch( rec->r.uid.validity ) {
1179           case 0:
1180           case TRUST_UNDEFINED:
1181           case TRUST_NEVER:
1182           case TRUST_MARGINAL:
1183           case TRUST_FULLY:
1184           case TRUST_ULTIMATE:
1185             break;
1186           default:
1187             log_info("lid %lu: invalid validity value - cleared\n", recnum);
1188         }
1189         memcpy( rec->r.uid.namehash, p, 20);
1190         break;
1191       case RECTYPE_PREF:  /* preference record */
1192         rec->r.pref.lid     = buftoulong(p); p += 4;
1193         rec->r.pref.next    = buftoulong(p); p += 4;
1194         memcpy( rec->r.pref.data, p, 30 );
1195         break;
1196       case RECTYPE_SIG:
1197         rec->r.sig.lid     = buftoulong(p); p += 4;
1198         rec->r.sig.next    = buftoulong(p); p += 4;
1199         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1200             rec->r.sig.sig[i].lid  = buftoulong(p); p += 4;
1201             rec->r.sig.sig[i].flag = *p++;
1202         }
1203         break;
1204       case RECTYPE_SDIR:   /* shadow directory record */
1205         rec->r.sdir.lid     = buftoulong(p); p += 4;
1206         rec->r.sdir.keyid[0]= buftou32(p); p += 4;
1207         rec->r.sdir.keyid[1]= buftou32(p); p += 4;
1208         rec->r.sdir.pubkey_algo = *p++;
1209         p += 3;
1210         rec->r.sdir.hintlist = buftoulong(p);
1211         if( rec->r.sdir.lid != recnum ) {
1212             log_error( "%s: sdir LID != recnum (%lu,%lu)\n",
1213                                db_name, rec->r.sdir.lid, (ulong)recnum );
1214             rc = G10ERR_TRUSTDB;
1215         }
1216         break;
1217       case RECTYPE_CACH:   /* cache record */
1218         rec->r.cache.lid    = buftoulong(p); p += 4;
1219         memcpy(rec->r.cache.blockhash, p, 20); p += 20;
1220         rec->r.cache.trustlevel = *p++;
1221         break;
1222       case RECTYPE_HTBL:
1223         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
1224             rec->r.htbl.item[i] = buftoulong(p); p += 4;
1225         }
1226         break;
1227       case RECTYPE_HLST:
1228         rec->r.hlst.next = buftoulong(p); p += 4;
1229         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
1230             rec->r.hlst.rnum[i] = buftoulong(p); p += 4;
1231         }
1232         break;
1233       default:
1234         log_error( "%s: invalid record type %d at recnum %lu\n",
1235                                    db_name, rec->rectype, (ulong)recnum );
1236         rc = G10ERR_TRUSTDB;
1237         break;
1238     }
1239
1240     return rc;
1241 }
1242
1243 /****************
1244  * Write the record at RECNUM
1245  */
1246 int
1247 tdbio_write_record( TRUSTREC *rec )
1248 {
1249     byte buf[TRUST_RECORD_LEN], *p;
1250     int rc = 0;
1251     int i;
1252     ulong recnum = rec->recnum;
1253
1254     if( db_fd == -1 )
1255         open_db();
1256
1257     memset(buf, 0, TRUST_RECORD_LEN);
1258     p = buf;
1259     *p++ = rec->rectype; p++;
1260     switch( rec->rectype ) {
1261       case 0:  /* unused record */
1262         break;
1263       case RECTYPE_VER: /* version record */
1264         if( recnum )
1265             BUG();
1266         memcpy(p-1, "gpg", 3 ); p += 2;
1267         *p++ = rec->r.ver.version;
1268         *p++ = rec->r.ver.marginals;
1269         *p++ = rec->r.ver.completes;
1270         *p++ = rec->r.ver.cert_depth;
1271         p += 4; /* skip lock flags */
1272         ulongtobuf(p, rec->r.ver.created); p += 4;
1273         ulongtobuf(p, rec->r.ver.modified); p += 4;
1274         ulongtobuf(p, rec->r.ver.validated); p += 4;
1275         ulongtobuf(p, rec->r.ver.keyhashtbl); p += 4;
1276         ulongtobuf(p, rec->r.ver.firstfree ); p += 4;
1277         ulongtobuf(p, rec->r.ver.sdirhashtbl ); p += 4;
1278         break;
1279
1280       case RECTYPE_FREE:
1281         ulongtobuf(p, rec->r.free.next); p += 4;
1282         break;
1283
1284       case RECTYPE_DIR:   /*directory record */
1285         ulongtobuf(p, rec->r.dir.lid); p += 4;
1286         ulongtobuf(p, rec->r.dir.keylist); p += 4;
1287         ulongtobuf(p, rec->r.dir.uidlist); p += 4;
1288         ulongtobuf(p, rec->r.dir.cacherec); p += 4;
1289         *p++ = rec->r.dir.ownertrust;
1290         *p++ = rec->r.dir.dirflags;
1291         *p++ = rec->r.dir.validity;
1292         assert( rec->r.dir.lid == recnum );
1293         break;
1294
1295       case RECTYPE_KEY:
1296         ulongtobuf(p, rec->r.key.lid); p += 4;
1297         ulongtobuf(p, rec->r.key.next); p += 4;
1298         p += 7;
1299         *p++ = rec->r.key.keyflags;
1300         *p++ = rec->r.key.pubkey_algo;
1301         *p++ = rec->r.key.fingerprint_len;
1302         memcpy( p, rec->r.key.fingerprint, 20); p += 20;
1303         break;
1304
1305       case RECTYPE_UID:   /* user id record */
1306         ulongtobuf(p, rec->r.uid.lid); p += 4;
1307         ulongtobuf(p, rec->r.uid.next); p += 4;
1308         ulongtobuf(p, rec->r.uid.prefrec); p += 4;
1309         ulongtobuf(p, rec->r.uid.siglist); p += 4;
1310         *p++ = rec->r.uid.uidflags;
1311         *p++ = rec->r.uid.validity;
1312         memcpy( p, rec->r.uid.namehash, 20 ); p += 20;
1313         break;
1314
1315       case RECTYPE_PREF:
1316         ulongtobuf(p, rec->r.pref.lid); p += 4;
1317         ulongtobuf(p, rec->r.pref.next); p += 4;
1318         memcpy( p, rec->r.pref.data, 30 );
1319         break;
1320
1321       case RECTYPE_SIG:
1322         ulongtobuf(p, rec->r.sig.lid); p += 4;
1323         ulongtobuf(p, rec->r.sig.next); p += 4;
1324         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1325             ulongtobuf(p, rec->r.sig.sig[i].lid); p += 4;
1326             *p++ = rec->r.sig.sig[i].flag;
1327         }
1328         break;
1329
1330       case RECTYPE_SDIR:
1331         ulongtobuf( p, rec->r.sdir.lid); p += 4;
1332         u32tobuf( p, rec->r.sdir.keyid[0] ); p += 4;
1333         u32tobuf( p, rec->r.sdir.keyid[1] ); p += 4;
1334         *p++ = rec->r.sdir.pubkey_algo;
1335         p += 3;
1336         ulongtobuf( p, rec->r.sdir.hintlist );
1337         break;
1338
1339       case RECTYPE_CACH:
1340         ulongtobuf(p, rec->r.cache.lid); p += 4;
1341         memcpy(p, rec->r.cache.blockhash, 20); p += 20;
1342         *p++ = rec->r.cache.trustlevel;
1343         break;
1344
1345       case RECTYPE_HTBL:
1346         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
1347             ulongtobuf( p, rec->r.htbl.item[i]); p += 4;
1348         }
1349         break;
1350
1351       case RECTYPE_HLST:
1352         ulongtobuf( p, rec->r.hlst.next); p += 4;
1353         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
1354             ulongtobuf( p, rec->r.hlst.rnum[i]); p += 4;
1355         }
1356         break;
1357
1358       default:
1359         BUG();
1360     }
1361
1362     rc = put_record_into_cache( recnum, buf );
1363     if( rc )
1364         ;
1365     if( rec->rectype == RECTYPE_KEY )
1366         rc = update_keyhashtbl( rec );
1367     else if( rec->rectype == RECTYPE_SDIR )
1368         rc = update_sdirhashtbl( rec );
1369
1370     return rc;
1371 }
1372
1373 int
1374 tdbio_delete_record( ulong recnum )
1375 {
1376     TRUSTREC vr, rec;
1377     int rc;
1378
1379     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
1380     if( rc )
1381         log_fatal( _("%s: error reading version record: %s\n"),
1382                                        db_name, g10_errstr(rc) );
1383
1384     rec.recnum = recnum;
1385     rec.rectype = RECTYPE_FREE;
1386     rec.r.free.next = vr.r.ver.firstfree;
1387     vr.r.ver.firstfree = recnum;
1388     rc = tdbio_write_record( &rec );
1389     if( !rc )
1390         rc = tdbio_write_record( &vr );
1391     return rc;
1392 }
1393
1394 /****************
1395  * create a new record and return its record number
1396  */
1397 ulong
1398 tdbio_new_recnum()
1399 {
1400     off_t offset;
1401     ulong recnum;
1402     TRUSTREC vr, rec;
1403     int rc;
1404
1405     /* look for unused records */
1406     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
1407     if( rc )
1408         log_fatal( _("%s: error reading version record: %s\n"),
1409                                              db_name, g10_errstr(rc) );
1410     if( vr.r.ver.firstfree ) {
1411         recnum = vr.r.ver.firstfree;
1412         rc = tdbio_read_record( recnum, &rec, RECTYPE_FREE );
1413         if( rc ) {
1414             log_error( _("%s: error reading free record: %s\n"),
1415                                                   db_name,  g10_errstr(rc) );
1416             return rc;
1417         }
1418         /* update dir record */
1419         vr.r.ver.firstfree = rec.r.free.next;
1420         rc = tdbio_write_record( &vr );
1421         if( rc ) {
1422             log_error( _("%s: error writing dir record: %s\n"),
1423                                                      db_name, g10_errstr(rc) );
1424             return rc;
1425         }
1426         /*zero out the new record */
1427         memset( &rec, 0, sizeof rec );
1428         rec.rectype = 0; /* unused record */
1429         rec.recnum = recnum;
1430         rc = tdbio_write_record( &rec );
1431         if( rc )
1432             log_fatal(_("%s: failed to zero a record: %s\n"),
1433                                        db_name, g10_errstr(rc));
1434     }
1435     else { /* not found, append a new record */
1436         offset = lseek( db_fd, 0, SEEK_END );
1437         if( offset == -1 )
1438             log_fatal("trustdb: lseek to end failed: %s\n", strerror(errno) );
1439         recnum = offset / TRUST_RECORD_LEN;
1440         assert(recnum); /* this is will never be the first record */
1441         /* we must write a record, so that the next call to this function
1442          * returns another recnum */
1443         memset( &rec, 0, sizeof rec );
1444         rec.rectype = 0; /* unused record */
1445         rec.recnum = recnum;
1446         rc = 0;
1447         if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
1448             log_error(_("trustdb rec %lu: lseek failed: %s\n"),
1449                                                 recnum, strerror(errno) );
1450             rc = G10ERR_WRITE_FILE;
1451         }
1452         else {
1453             int n = write( db_fd, &rec, TRUST_RECORD_LEN);
1454             if( n != TRUST_RECORD_LEN ) {
1455                 log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
1456                                                  recnum, n, strerror(errno) );
1457                 rc = G10ERR_WRITE_FILE;
1458             }
1459         }
1460
1461         if( rc )
1462             log_fatal(_("%s: failed to append a record: %s\n"),
1463                                     db_name,    g10_errstr(rc));
1464     }
1465     return recnum ;
1466 }
1467
1468
1469
1470 /****************
1471  * Search the trustdb for a key which matches PK and return the dir record
1472  * The local_id of PK is set to the correct value
1473  */
1474 int
1475 tdbio_search_dir_bypk( PKT_public_key *pk, TRUSTREC *rec )
1476 {
1477     byte fingerprint[MAX_FINGERPRINT_LEN];
1478     size_t fingerlen;
1479     u32 keyid[2];
1480     int rc;
1481
1482     keyid_from_pk( pk, keyid );
1483     fingerprint_from_pk( pk, fingerprint, &fingerlen );
1484     rc = tdbio_search_dir_byfpr( fingerprint, fingerlen,
1485                                  pk->pubkey_algo, rec );
1486
1487     if( !rc ) {
1488         if( pk->local_id && pk->local_id != rec->recnum )
1489             log_error("%s: found record, but LID from memory does "
1490                        "not match recnum (%lu,%lu)\n",
1491                             db_name,  pk->local_id, rec->recnum );
1492         pk->local_id = rec->recnum;
1493     }
1494     return rc;
1495 }
1496
1497
1498 static int
1499 cmp_krec_fpr( void *dataptr, const TRUSTREC *rec )
1500 {
1501     const struct cmp_krec_fpr_struct *d = dataptr;
1502
1503     return rec->rectype == RECTYPE_KEY
1504            && ( !d->pubkey_algo || rec->r.key.pubkey_algo == d->pubkey_algo )
1505            && rec->r.key.fingerprint_len == d->fprlen
1506            && !memcmp( rec->r.key.fingerprint, d->fpr, d->fprlen );
1507 }
1508
1509 int
1510 tdbio_search_dir_byfpr( const byte *fingerprint, size_t fingerlen,
1511                         int pubkey_algo, TRUSTREC *rec )
1512 {
1513     struct cmp_krec_fpr_struct cmpdata;
1514     ulong recnum;
1515     int rc;
1516
1517     assert( fingerlen == 20 || fingerlen == 16 );
1518
1519     /* locate the key using the hash table */
1520     cmpdata.pubkey_algo = pubkey_algo;
1521     cmpdata.fpr = fingerprint;
1522     cmpdata.fprlen = fingerlen;
1523     rc = lookup_hashtable( get_keyhashrec(), fingerprint, fingerlen,
1524                            cmp_krec_fpr, &cmpdata, rec );
1525     if( !rc ) {
1526         recnum = rec->r.key.lid;
1527         /* Now read the dir record */
1528         rc = tdbio_read_record( recnum, rec, RECTYPE_DIR);
1529         if( rc )
1530             log_error("%s: can't read dirrec %lu: %s\n",
1531                                      db_name, recnum, g10_errstr(rc) );
1532     }
1533     return rc;
1534 }
1535
1536
1537
1538 static int
1539 cmp_sdir( void *dataptr, const TRUSTREC *rec )
1540 {
1541     const struct cmp_sdir_struct *d = dataptr;
1542
1543     return rec->rectype == RECTYPE_SDIR
1544            && ( !d->pubkey_algo || rec->r.sdir.pubkey_algo == d->pubkey_algo )
1545            && rec->r.sdir.keyid[0] == d->keyid[0]
1546            && rec->r.sdir.keyid[1] == d->keyid[1];
1547 }
1548
1549
1550 int
1551 tdbio_search_sdir( u32 *keyid, int pubkey_algo, TRUSTREC *rec )
1552 {
1553     struct cmp_sdir_struct cmpdata;
1554     int rc;
1555     byte key[8];
1556
1557     /* locate the shadow dir record using the hash table */
1558     u32tobuf( key   , keyid[0] );
1559     u32tobuf( key+4 , keyid[1] );
1560     cmpdata.pubkey_algo = pubkey_algo;
1561     cmpdata.keyid[0] = keyid[0];
1562     cmpdata.keyid[1] = keyid[1];
1563     rc = lookup_hashtable( get_sdirhashrec(), key, 8,
1564                            cmp_sdir, &cmpdata, rec );
1565     return rc;
1566 }
1567
1568