5d1864c3de42c2870984f066c0ce2cb33bd6913a
[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 const char *lockname;
81 static int  db_fd = -1;
82 static int in_transaction;
83
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 to 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( !lockname )
240             lockname = make_dotlock( db_name, -1 );
241         if( !lockname )
242             log_fatal("can't get a lock - giving up\n");
243         for( unused = NULL, r = cache_list; r; r = r->next ) {
244             if( r->flags.used && r->flags.dirty ) {
245                 int rc = write_cache_item( r );
246                 if( rc )
247                     return rc;
248                 if( !unused )
249                     unused = r;
250                 r->flags.used = 0;
251                 cache_entries--;
252                 if( !--n )
253                     break;
254             }
255         }
256         if( !opt.lock_once ) {
257             release_dotlock( lockname );
258             lockname=NULL;
259         }
260         assert( unused );
261         r = unused;
262         r->flags.used = 1;
263         r->recno = recno;
264         memcpy( r->data, data, TRUST_RECORD_LEN );
265         r->flags.dirty = 1;
266         cache_is_dirty = 1;
267         cache_entries++;
268         return 0;
269     }
270     BUG();
271 }
272
273
274 int
275 tdbio_is_dirty()
276 {
277     return cache_is_dirty;
278 }
279
280
281 /****************
282  * Flush the cache.  This cannot be used while in a transaction.
283  */
284 int
285 tdbio_sync()
286 {
287     CACHE_CTRL r;
288     int did_lock = 0;
289
290     if( in_transaction )
291         log_bug("tdbio: syncing while in transaction\n");
292
293     if( !cache_is_dirty )
294         return 0;
295
296     if( !lockname ) {
297         lockname = make_dotlock( db_name, -1 );
298         did_lock = 1;
299         if( !lockname )
300             log_fatal("can't get a lock - giving up\n");
301     }
302     for( r = cache_list; r; r = r->next ) {
303         if( r->flags.used && r->flags.dirty ) {
304             int rc = write_cache_item( r );
305             if( rc )
306                 return rc;
307         }
308     }
309     cache_is_dirty = 0;
310     if( did_lock && !opt.lock_once ) {
311         release_dotlock( lockname );
312         lockname=NULL;
313     }
314     return 0;
315 }
316
317
318
319 /****************
320  * Simple transactions system:
321  * Everything between begin_transaction and end/cancel_transaction
322  * is not immediatly written but at the time of end_transaction.
323  *
324  */
325 int
326 tdbio_begin_transaction()
327 {
328     int rc;
329
330     if( in_transaction )
331         log_bug("tdbio: nested transactions\n");
332     /* flush everything out */
333     rc = tdbio_sync();
334     if( rc )
335         return rc;
336     in_transaction = 1;
337     return 0;
338 }
339
340 int
341 tdbio_end_transaction()
342 {
343     int rc;
344
345     if( !in_transaction )
346         log_bug("tdbio: no active transaction\n");
347     if( !lockname )
348         lockname = make_dotlock( db_name, -1 );
349     if( !lockname )
350         log_fatal("can't get a lock - giving up\n");
351     block_all_signals();
352     in_transaction = 0;
353     rc = tdbio_sync();
354     unblock_all_signals();
355     if( !opt.lock_once ) {
356         release_dotlock( lockname );
357         lockname=NULL;
358     }
359     return rc;
360 }
361
362 int
363 tdbio_cancel_transaction()
364 {
365     CACHE_CTRL r;
366
367     if( !in_transaction )
368         log_bug("tdbio: no active transaction\n");
369
370     /* remove all dirty marked entries, so that the original ones
371      * are read back the next time */
372     if( cache_is_dirty ) {
373         for( r = cache_list; r; r = r->next ) {
374             if( r->flags.used && r->flags.dirty ) {
375                 r->flags.used = 0;
376                 cache_entries--;
377             }
378         }
379         cache_is_dirty = 0;
380     }
381
382     in_transaction = 0;
383     return 0;
384 }
385
386
387 \f
388 /********************************************************
389  **************** cached I/O functions ******************
390  ********************************************************/
391
392 int
393 tdbio_set_dbname( const char *new_dbname, int create )
394 {
395     char *fname;
396
397     fname = new_dbname? m_strdup( new_dbname )
398                       : make_filename(opt.homedir, "trustdb.gpg", NULL );
399
400     if( access( fname, R_OK ) ) {
401         if( errno != ENOENT ) {
402             log_error( _("%s: can't access: %s\n"), fname, strerror(errno) );
403             m_free(fname);
404             return G10ERR_TRUSTDB;
405         }
406         if( create ) {
407             FILE *fp;
408             TRUSTREC rec;
409             int rc;
410             char *p = strrchr( fname, '/' );
411
412             assert(p);
413             *p = 0;
414             if( access( fname, F_OK ) ) {
415                 if( strlen(fname) >= 7
416                     && !strcmp(fname+strlen(fname)-7, "/.gnupg" ) ) {
417                   #if __MINGW32__
418                     if( mkdir( fname ) )
419                   #else
420                     if( mkdir( fname, S_IRUSR|S_IWUSR|S_IXUSR ) )
421                   #endif
422                         log_fatal( _("%s: can't create directory: %s\n"),
423                                                     fname,  strerror(errno) );
424                     else
425                         log_info( _("%s: directory created\n"), fname );
426                     copy_options_file( fname );
427                 }
428                 else
429                     log_fatal( _("%s: directory does not exist!\n"), fname );
430             }
431             *p = '/';
432
433             fp =fopen( fname, "wb" );
434             if( !fp )
435                 log_fatal( _("%s: can't create: %s\n"), fname, strerror(errno) );
436             fclose(fp);
437             m_free(db_name);
438             db_name = fname;
439           #ifdef __MINGW32__
440             db_fd = open( db_name, O_RDWR | O_BINARY );
441           #else
442             db_fd = open( db_name, O_RDWR );
443           #endif
444             if( db_fd == -1 )
445                 log_fatal( _("%s: can't open: %s\n"), db_name, strerror(errno) );
446
447             memset( &rec, 0, sizeof rec );
448             rec.r.ver.version = 2;
449             rec.r.ver.created = make_timestamp();
450             rec.r.ver.marginals =  opt.marginals_needed;
451             rec.r.ver.completes =  opt.completes_needed;
452             rec.r.ver.cert_depth = opt.max_cert_depth;
453             rec.rectype = RECTYPE_VER;
454             rec.recnum = 0;
455             rc = tdbio_write_record( &rec );
456             if( !rc )
457                 tdbio_sync();
458             if( rc )
459                 log_fatal( _("%s: failed to create version record: %s"),
460                                                    fname, g10_errstr(rc));
461             /* and read again to check that we are okay */
462             if( tdbio_read_record( 0, &rec, RECTYPE_VER ) )
463                 log_fatal( _("%s: invalid trust-db created\n"), db_name );
464
465             log_info(_("%s: trust-db created\n"), db_name);
466
467             return 0;
468         }
469     }
470     m_free(db_name);
471     db_name = fname;
472     return 0;
473 }
474
475
476 const char *
477 tdbio_get_dbname()
478 {
479     return db_name;
480 }
481
482
483 static void
484 cleanup(void)
485 {
486     if( lockname ) {
487         release_dotlock(lockname);
488         lockname = NULL;
489     }
490 }
491
492 static void
493 open_db()
494 {
495     TRUSTREC rec;
496     assert( db_fd == -1 );
497
498   #ifdef __MINGW32__
499     db_fd = open( db_name, O_RDWR | O_BINARY );
500   #else
501     db_fd = open( db_name, O_RDWR );
502   #endif
503     if( db_fd == -1 )
504         log_fatal( _("%s: can't open: %s\n"), db_name, strerror(errno) );
505     if( tdbio_read_record( 0, &rec, RECTYPE_VER ) )
506         log_fatal( _("%s: invalid trust-db\n"), db_name );
507     atexit( cleanup );
508 }
509
510
511 /****************
512  * Make a hashtable: type 0 = key hash, 1 = sdir hash
513  */
514 static void
515 create_hashtable( TRUSTREC *vr, int type )
516 {
517     TRUSTREC rec;
518     off_t offset;
519     ulong recnum;
520     int i, n, rc;
521
522     offset = lseek( db_fd, 0, SEEK_END );
523     if( offset == -1 )
524         log_fatal("trustdb: lseek to end failed: %s\n", strerror(errno) );
525     recnum = offset / TRUST_RECORD_LEN;
526     assert(recnum); /* this is will never be the first record */
527
528     if( !type )
529         vr->r.ver.keyhashtbl = recnum;
530     else
531         vr->r.ver.sdirhashtbl = recnum;
532     /* Now write the records */
533     n = (256+ITEMS_PER_HTBL_RECORD-1) / ITEMS_PER_HTBL_RECORD;
534     for(i=0; i < n; i++, recnum++ ) {
535          memset( &rec, 0, sizeof rec );
536          rec.rectype = RECTYPE_HTBL;
537          rec.recnum = recnum;
538          rc = tdbio_write_record( &rec );
539          if( rc )
540              log_fatal( _("%s: failed to create hashtable: %s\n"),
541                                         db_name, g10_errstr(rc));
542     }
543     /* update the version record */
544     rc = tdbio_write_record( vr );
545     if( !rc )
546         rc = tdbio_sync();
547     if( rc )
548         log_fatal( _("%s: error updating version record: %s\n"),
549                                                   db_name, g10_errstr(rc));
550 }
551
552
553 int
554 tdbio_db_matches_options()
555 {
556     static int yes_no = -1;
557
558     if( yes_no == -1 ) {
559         TRUSTREC vr;
560         int rc;
561
562         rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
563         if( rc )
564             log_fatal( _("%s: error reading version record: %s\n"),
565                                                     db_name, g10_errstr(rc) );
566
567         if( !vr.r.ver.marginals && !vr.r.ver.completes
568                                 && !vr.r.ver.cert_depth )
569         {   /* special hack for trustdbs created by old versions of GnuPG */
570             vr.r.ver.marginals =  opt.marginals_needed;
571             vr.r.ver.completes =  opt.completes_needed;
572             vr.r.ver.cert_depth = opt.max_cert_depth;
573             rc = tdbio_write_record( &vr );
574             if( !rc && !in_transaction )
575                 rc = tdbio_sync();
576             if( rc )
577                 log_error( _("%s: error writing version record: %s\n"),
578                                                 db_name, g10_errstr(rc) );
579         }
580
581         yes_no = vr.r.ver.marginals == opt.marginals_needed
582                  && vr.r.ver.completes == opt.completes_needed
583                  && vr.r.ver.cert_depth == opt.max_cert_depth;
584     }
585     return yes_no;
586 }
587
588
589 /****************
590  * Return the record number of the keyhash tbl or create a new one.
591  */
592 static ulong
593 get_keyhashrec()
594 {
595     static ulong keyhashtbl; /* record number of the key hashtable */
596
597     if( !keyhashtbl ) {
598         TRUSTREC vr;
599         int rc;
600
601         rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
602         if( rc )
603             log_fatal( _("%s: error reading version record: %s\n"),
604                                             db_name, g10_errstr(rc) );
605         if( !vr.r.ver.keyhashtbl )
606             create_hashtable( &vr, 0 );
607
608         keyhashtbl = vr.r.ver.keyhashtbl;
609     }
610     return keyhashtbl;
611 }
612
613 /****************
614  * Return the record number of the shadow direcory hash table
615  * or create a new one.
616  */
617 static ulong
618 get_sdirhashrec()
619 {
620     static ulong sdirhashtbl; /* record number of the hashtable */
621
622     if( !sdirhashtbl ) {
623         TRUSTREC vr;
624         int rc;
625
626         rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
627         if( rc )
628             log_fatal( _("%s: error reading version record: %s\n"),
629                                                     db_name, g10_errstr(rc) );
630         if( !vr.r.ver.sdirhashtbl )
631             create_hashtable( &vr, 1 );
632
633         sdirhashtbl = vr.r.ver.sdirhashtbl;
634     }
635     return sdirhashtbl;
636 }
637
638
639 /****************
640  * Update a hashtable.
641  * table gives the start of the table, key and keylen is the key,
642  * newrecnum is the record number to insert.
643  */
644 static int
645 upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
646 {
647     TRUSTREC lastrec, rec;
648     ulong hashrec, item;
649     int msb;
650     int level=0;
651     int rc, i;
652
653     hashrec = table;
654   next_level:
655     msb = key[level];
656     hashrec += msb / ITEMS_PER_HTBL_RECORD;
657     rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
658     if( rc ) {
659         log_error( db_name, "upd_hashtable: read failed: %s\n",
660                                                         g10_errstr(rc) );
661         return rc;
662     }
663
664     item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
665     if( !item ) { /* insert a new item into the hash table */
666         rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = newrecnum;
667         rc = tdbio_write_record( &rec );
668         if( rc ) {
669             log_error( db_name, "upd_hashtable: write htbl failed: %s\n",
670                                                             g10_errstr(rc) );
671             return rc;
672         }
673     }
674     else if( item != newrecnum ) {  /* must do an update */
675         lastrec = rec;
676         rc = tdbio_read_record( item, &rec, 0 );
677         if( rc ) {
678             log_error( "upd_hashtable: read item failed: %s\n",
679                                                             g10_errstr(rc) );
680             return rc;
681         }
682
683         if( rec.rectype == RECTYPE_HTBL ) {
684             hashrec = item;
685             level++;
686             if( level >= keylen ) {
687                 log_error( "hashtable has invalid indirections.\n");
688                 return G10ERR_TRUSTDB;
689             }
690             goto next_level;
691         }
692         else if( rec.rectype == RECTYPE_HLST ) { /* extend list */
693             /* see whether the key is already in this list */
694             for(;;) {
695                 for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
696                     if( rec.r.hlst.rnum[i] == newrecnum ) {
697                         return 0; /* okay, already in the list */
698                     }
699                 }
700                 if( rec.r.hlst.next ) {
701                     rc = tdbio_read_record( rec.r.hlst.next,
702                                                        &rec, RECTYPE_HLST);
703                     if( rc ) {
704                         log_error( "scan keyhashtbl read hlst failed: %s\n",
705                                                              g10_errstr(rc) );
706                         return rc;
707                     }
708                 }
709                 else
710                     break; /* not there */
711             }
712             /* find the next free entry and put it in */
713             for(;;) {
714                 for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
715                     if( !rec.r.hlst.rnum[i] ) {
716                         rec.r.hlst.rnum[i] = newrecnum;
717                         rc = tdbio_write_record( &rec );
718                         if( rc )
719                             log_error( "upd_hashtable: write hlst failed: %s\n",
720                                                               g10_errstr(rc) );
721                         return rc; /* done */
722                     }
723                 }
724                 if( rec.r.hlst.next ) {
725                     rc = tdbio_read_record( rec.r.hlst.next,
726                                                       &rec, RECTYPE_HLST );
727                     if( rc ) {
728                         log_error( "upd_hashtable: read hlst failed: %s\n",
729                                                              g10_errstr(rc) );
730                         return rc;
731                     }
732                 }
733                 else { /* add a new list record */
734                     rec.r.hlst.next = item = tdbio_new_recnum();
735                     rc = tdbio_write_record( &rec );
736                     if( rc ) {
737                         log_error( "upd_hashtable: write hlst failed: %s\n",
738                                                           g10_errstr(rc) );
739                         return rc;
740                     }
741                     memset( &rec, 0, sizeof rec );
742                     rec.rectype = RECTYPE_HLST;
743                     rec.recnum = item;
744                     rec.r.hlst.rnum[0] = newrecnum;
745                     rc = tdbio_write_record( &rec );
746                     if( rc )
747                         log_error( "upd_hashtable: write ext hlst failed: %s\n",
748                                                           g10_errstr(rc) );
749                     return rc; /* done */
750                 }
751             } /* end loop over hlst slots */
752         }
753         else if( rec.rectype == RECTYPE_KEY
754                  || rec.rectype == RECTYPE_DIR
755                  || rec.rectype == RECTYPE_SDIR ) { /* insert a list record */
756             if( rec.recnum == newrecnum ) {
757                 return 0;
758             }
759             item = rec.recnum; /* save number of key record */
760             memset( &rec, 0, sizeof rec );
761             rec.rectype = RECTYPE_HLST;
762             rec.recnum = tdbio_new_recnum();
763             rec.r.hlst.rnum[0] = item;       /* old keyrecord */
764             rec.r.hlst.rnum[1] = newrecnum; /* and new one */
765             rc = tdbio_write_record( &rec );
766             if( rc ) {
767                 log_error( "upd_hashtable: write new hlst failed: %s\n",
768                                                   g10_errstr(rc) );
769                 return rc;
770             }
771             /* update the hashtable record */
772             lastrec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = rec.recnum;
773             rc = tdbio_write_record( &lastrec );
774             if( rc )
775                 log_error( "upd_hashtable: update htbl failed: %s\n",
776                                                              g10_errstr(rc) );
777             return rc; /* ready */
778         }
779         else {
780             log_error( "hashtbl %lu points to an invalid record\n",
781                                                                     item);
782             return G10ERR_TRUSTDB;
783         }
784     }
785
786     return 0;
787 }
788
789
790
791 /****************
792  * Lookup a record via the hashtable tablewith key/keylen and return the
793  * result in rec.  cmp() should return if the record is the desired one.
794  * Returns -1 if not found, 0 if found or another errocode
795  */
796 static int
797 lookup_hashtable( ulong table, const byte *key, size_t keylen,
798                   int (*cmpfnc)(void*, const TRUSTREC *), void *cmpdata,
799                                                 TRUSTREC *rec )
800 {
801     int rc;
802     ulong hashrec, item;
803     int msb;
804     int level=0;
805
806     hashrec = table;
807   next_level:
808     msb = key[level];
809     hashrec += msb / ITEMS_PER_HTBL_RECORD;
810     rc = tdbio_read_record( hashrec, rec, RECTYPE_HTBL );
811     if( rc ) {
812         log_error( db_name, "lookup_hashtable failed: %s\n", g10_errstr(rc) );
813         return rc;
814     }
815
816     item = rec->r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
817     if( !item )
818         return -1; /* not found */
819
820     rc = tdbio_read_record( item, rec, 0 );
821     if( rc ) {
822         log_error( db_name, "hashtable read failed: %s\n", g10_errstr(rc) );
823         return rc;
824     }
825     if( rec->rectype == RECTYPE_HTBL ) {
826         hashrec = item;
827         level++;
828         if( level >= keylen ) {
829             log_error( db_name, "hashtable has invalid indirections\n");
830             return G10ERR_TRUSTDB;
831         }
832         goto next_level;
833     }
834     else if( rec->rectype == RECTYPE_HLST ) {
835         for(;;) {
836             int i;
837
838             for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
839                 if( rec->r.hlst.rnum[i] ) {
840                     TRUSTREC tmp;
841
842                     rc = tdbio_read_record( rec->r.hlst.rnum[i], &tmp, 0 );
843                     if( rc ) {
844                         log_error( "lookup_hashtable: read item failed: %s\n",
845                                                               g10_errstr(rc) );
846                         return rc;
847                     }
848                     if( (*cmpfnc)( cmpdata, &tmp ) ) {
849                         *rec = tmp;
850                         return 0;
851                     }
852                 }
853             }
854             if( rec->r.hlst.next ) {
855                 rc = tdbio_read_record( rec->r.hlst.next, rec, RECTYPE_HLST );
856                 if( rc ) {
857                     log_error( "lookup_hashtable: read hlst failed: %s\n",
858                                                          g10_errstr(rc) );
859                     return rc;
860                 }
861             }
862             else
863                 return -1; /* not found */
864         }
865     }
866
867
868     if( (*cmpfnc)( cmpdata, rec ) )
869         return 0; /* really found */
870
871     return -1; /* no: not found */
872 }
873
874
875
876
877 /****************
878  * Update the key hashtbl or create the table if it does not exist
879  */
880 static int
881 update_keyhashtbl( TRUSTREC *kr )
882 {
883     return upd_hashtable( get_keyhashrec(),
884                           kr->r.key.fingerprint,
885                           kr->r.key.fingerprint_len, kr->recnum );
886 }
887
888 /****************
889  * Update the shadow dir hashtbl or create the table if it does not exist
890  */
891 static int
892 update_sdirhashtbl( TRUSTREC *sr )
893 {
894     byte key[8];
895
896     u32tobuf( key   , sr->r.sdir.keyid[0] );
897     u32tobuf( key+4 , sr->r.sdir.keyid[1] );
898     return upd_hashtable( get_sdirhashrec(), key, 8, sr->recnum );
899 }
900
901
902
903
904 void
905 tdbio_dump_record( TRUSTREC *rec, FILE *fp  )
906 {
907     int i;
908     ulong rnum = rec->recnum;
909     byte *p;
910
911     fprintf(fp, "rec %5lu, ", rnum );
912
913     switch( rec->rectype ) {
914       case 0: fprintf(fp, "blank\n");
915         break;
916       case RECTYPE_VER: fprintf(fp,
917             "version, kd=%lu, sd=%lu, free=%lu, m/c/d=%d/%d/%d\n",
918             rec->r.ver.keyhashtbl, rec->r.ver.sdirhashtbl,
919                                    rec->r.ver.firstfree,
920                                    rec->r.ver.marginals,
921                                    rec->r.ver.completes,
922                                    rec->r.ver.cert_depth );
923         break;
924       case RECTYPE_FREE: fprintf(fp, "free, next=%lu\n", rec->r.free.next );
925         break;
926       case RECTYPE_DIR:
927         fprintf(fp, "dir %lu, keys=%lu, uids=%lu, t=%02x",
928                     rec->r.dir.lid,
929                     rec->r.dir.keylist,
930                     rec->r.dir.uidlist,
931                     rec->r.dir.ownertrust );
932         if( rec->r.dir.dirflags & DIRF_VALVALID )
933             fprintf( fp, ", v=%02x", rec->r.dir.validity );
934         if( rec->r.dir.dirflags & DIRF_CHECKED ) {
935             if( rec->r.dir.dirflags & DIRF_VALID )
936                 fputs(", valid", fp );
937             if( rec->r.dir.dirflags & DIRF_EXPIRED )
938                 fputs(", expired", fp );
939             if( rec->r.dir.dirflags & DIRF_REVOKED )
940                 fputs(", revoked", fp );
941         }
942         putc('\n', fp);
943         break;
944       case RECTYPE_KEY:
945         fprintf(fp, "key %lu, n=%lu a=%d ",
946                    rec->r.key.lid,
947                    rec->r.key.next,
948                    rec->r.key.pubkey_algo );
949         for(i=0; i < rec->r.key.fingerprint_len; i++ )
950             fprintf(fp, "%02X", rec->r.key.fingerprint[i] );
951         if( rec->r.key.keyflags & KEYF_CHECKED ) {
952             if( rec->r.key.keyflags & KEYF_VALID )
953                 fputs(", valid", fp );
954             if( rec->r.key.keyflags & KEYF_EXPIRED )
955                 fputs(", expired", fp );
956             if( rec->r.key.keyflags & KEYF_REVOKED )
957                 fputs(", revoked", fp );
958         }
959         putc('\n', fp);
960         break;
961       case RECTYPE_UID:
962         fprintf(fp, "uid %lu, next=%lu, pref=%lu, sig=%lu, hash=%02X%02X",
963                     rec->r.uid.lid,
964                     rec->r.uid.next,
965                     rec->r.uid.prefrec,
966                     rec->r.uid.siglist,
967                     rec->r.uid.namehash[18], rec->r.uid.namehash[19]);
968         if( rec->r.uid.uidflags & UIDF_CHECKED ) {
969             if( rec->r.uid.uidflags & UIDF_VALID )
970                 fputs(", valid", fp );
971             if( rec->r.uid.uidflags & UIDF_REVOKED )
972                 fputs(", revoked", fp );
973         }
974         putc('\n', fp);
975         break;
976       case RECTYPE_PREF:
977         fprintf(fp, "pref %lu, next=%lu,",
978                     rec->r.pref.lid, rec->r.pref.next);
979         for(i=0,p=rec->r.pref.data; i < ITEMS_PER_PREF_RECORD; i+=2,p+=2 ) {
980             if( *p )
981                 fprintf(fp, " %c%d", *p == PREFTYPE_SYM    ? 'S' :
982                                      *p == PREFTYPE_HASH   ? 'H' :
983                                      *p == PREFTYPE_COMPR  ? 'Z' : '?', p[1]);
984         }
985         putc('\n', fp);
986         break;
987       case RECTYPE_SIG:
988         fprintf(fp, "sig %lu, next=%lu,",
989                          rec->r.sig.lid, rec->r.sig.next );
990         for(i=0; i < SIGS_PER_RECORD; i++ ) {
991             if( rec->r.sig.sig[i].lid ) {
992                 fprintf(fp, " %lu:", rec->r.sig.sig[i].lid );
993                 if( rec->r.sig.sig[i].flag & SIGF_CHECKED ) {
994                     fprintf(fp,"%c%c%c",
995                        (rec->r.sig.sig[i].flag & SIGF_VALID)   ? 'V':'-',
996                        (rec->r.sig.sig[i].flag & SIGF_EXPIRED) ? 'E':'-',
997                        (rec->r.sig.sig[i].flag & SIGF_REVOKED) ? 'R':'-');
998                 }
999                 else if( rec->r.sig.sig[i].flag & SIGF_NOPUBKEY)
1000                     fputs("?--", fp);
1001                 else
1002                     fputs("---", fp);
1003             }
1004         }
1005         putc('\n', fp);
1006         break;
1007       case RECTYPE_SDIR:
1008         fprintf(fp, "sdir %lu, keyid=%08lX%08lX, algo=%d, hint=%lu\n",
1009                     rec->r.sdir.lid,
1010                     (ulong)rec->r.sdir.keyid[0],
1011                     (ulong)rec->r.sdir.keyid[1],
1012                     rec->r.sdir.pubkey_algo,
1013                     (ulong)rec->r.sdir.hintlist );
1014         break;
1015       case RECTYPE_CACH:
1016         fprintf(fp, "cach\n");
1017         break;
1018       case RECTYPE_HTBL:
1019         fprintf(fp, "htbl,");
1020         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ )
1021             fprintf(fp, " %lu", rec->r.htbl.item[i] );
1022         putc('\n', fp);
1023         break;
1024       case RECTYPE_HLST:
1025         fprintf(fp, "hlst, next=%lu,", rec->r.hlst.next );
1026         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ )
1027             fprintf(fp, " %lu", rec->r.hlst.rnum[i] );
1028         putc('\n', fp);
1029         break;
1030       default:
1031         fprintf(fp, "unknown type %d\n", rec->rectype );
1032         break;
1033     }
1034 }
1035
1036 /****************
1037  * read the record with number recnum
1038  * returns: -1 on error, 0 on success
1039  */
1040 int
1041 tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
1042 {
1043     byte readbuf[TRUST_RECORD_LEN];
1044     const byte *buf, *p;
1045     int rc = 0;
1046     int n, i;
1047
1048     if( db_fd == -1 )
1049         open_db();
1050     buf = get_record_from_cache( recnum );
1051     if( !buf ) {
1052         if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
1053             log_error(_("trustdb: lseek failed: %s\n"), strerror(errno) );
1054             return G10ERR_READ_FILE;
1055         }
1056         n = read( db_fd, readbuf, TRUST_RECORD_LEN);
1057         if( !n ) {
1058             return -1; /* eof */
1059         }
1060         else if( n != TRUST_RECORD_LEN ) {
1061             log_error(_("trustdb: read failed (n=%d): %s\n"), n,
1062                                                         strerror(errno) );
1063             return G10ERR_READ_FILE;
1064         }
1065         buf = readbuf;
1066     }
1067     rec->recnum = recnum;
1068     rec->dirty = 0;
1069     p = buf;
1070     rec->rectype = *p++;
1071     if( expected && rec->rectype != expected ) {
1072         log_error("%lu: read expected rec type %d, got %d\n",
1073                     recnum, expected, rec->rectype );
1074         return G10ERR_TRUSTDB;
1075     }
1076     p++;    /* skip reserved byte */
1077     switch( rec->rectype ) {
1078       case 0:  /* unused (free) record */
1079         break;
1080       case RECTYPE_VER: /* version record */
1081         if( memcmp(buf+1, "gpg", 3 ) ) {
1082             log_error( _("%s: not a trustdb file\n"), db_name );
1083             rc = G10ERR_TRUSTDB;
1084         }
1085         p += 2; /* skip "pgp" */
1086         rec->r.ver.version  = *p++;
1087         rec->r.ver.marginals = *p++;
1088         rec->r.ver.completes = *p++;
1089         rec->r.ver.cert_depth = *p++;
1090         p += 4; /* lock flags */
1091         rec->r.ver.created  = buftoulong(p); p += 4;
1092         rec->r.ver.modified = buftoulong(p); p += 4;
1093         rec->r.ver.validated= buftoulong(p); p += 4;
1094         rec->r.ver.keyhashtbl=buftoulong(p); p += 4;
1095         rec->r.ver.firstfree =buftoulong(p); p += 4;
1096         rec->r.ver.sdirhashtbl =buftoulong(p); p += 4;
1097         if( recnum ) {
1098             log_error( _("%s: version record with recnum %lu\n"), db_name,
1099                                                              (ulong)recnum );
1100             rc = G10ERR_TRUSTDB;
1101         }
1102         else if( rec->r.ver.version != 2 ) {
1103             log_error( _("%s: invalid file version %d\n"), db_name,
1104                                                         rec->r.ver.version );
1105             rc = G10ERR_TRUSTDB;
1106         }
1107         break;
1108       case RECTYPE_FREE:
1109         rec->r.free.next  = buftoulong(p); p += 4;
1110         break;
1111       case RECTYPE_DIR:   /*directory record */
1112         rec->r.dir.lid      = buftoulong(p); p += 4;
1113         rec->r.dir.keylist  = buftoulong(p); p += 4;
1114         rec->r.dir.uidlist  = buftoulong(p); p += 4;
1115         rec->r.dir.cacherec = buftoulong(p); p += 4;
1116         rec->r.dir.ownertrust = *p++;
1117         rec->r.dir.dirflags   = *p++;
1118         rec->r.dir.validity   = *p++;
1119         switch( rec->r.dir.validity ) {
1120           case 0:
1121           case TRUST_UNDEFINED:
1122           case TRUST_NEVER:
1123           case TRUST_MARGINAL:
1124           case TRUST_FULLY:
1125           case TRUST_ULTIMATE:
1126             break;
1127           default:
1128             log_info("lid %lu: invalid validity value - cleared\n", recnum);
1129         }
1130         if( rec->r.dir.lid != recnum ) {
1131             log_error( "%s: dir LID != recnum (%lu,%lu)\n",
1132                               db_name, rec->r.dir.lid, (ulong)recnum );
1133             rc = G10ERR_TRUSTDB;
1134         }
1135         break;
1136       case RECTYPE_KEY:   /* public key record */
1137         rec->r.key.lid      = buftoulong(p); p += 4;
1138         rec->r.key.next     = buftoulong(p); p += 4;
1139         p += 7;
1140         rec->r.key.keyflags = *p++;
1141         rec->r.key.pubkey_algo = *p++;
1142         rec->r.key.fingerprint_len = *p++;
1143         if( rec->r.key.fingerprint_len < 1 || rec->r.key.fingerprint_len > 20 )
1144             rec->r.key.fingerprint_len = 20;
1145         memcpy( rec->r.key.fingerprint, p, 20);
1146         break;
1147       case RECTYPE_UID:   /* user id record */
1148         rec->r.uid.lid      = buftoulong(p); p += 4;
1149         rec->r.uid.next     = buftoulong(p); p += 4;
1150         rec->r.uid.prefrec  = buftoulong(p); p += 4;
1151         rec->r.uid.siglist  = buftoulong(p); p += 4;
1152         rec->r.uid.uidflags = *p++;
1153         p ++;
1154         memcpy( rec->r.uid.namehash, p, 20);
1155         break;
1156       case RECTYPE_PREF:  /* preference record */
1157         rec->r.pref.lid     = buftoulong(p); p += 4;
1158         rec->r.pref.next    = buftoulong(p); p += 4;
1159         memcpy( rec->r.pref.data, p, 30 );
1160         break;
1161       case RECTYPE_SIG:
1162         rec->r.sig.lid     = buftoulong(p); p += 4;
1163         rec->r.sig.next    = buftoulong(p); p += 4;
1164         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1165             rec->r.sig.sig[i].lid  = buftoulong(p); p += 4;
1166             rec->r.sig.sig[i].flag = *p++;
1167         }
1168         break;
1169       case RECTYPE_SDIR:   /* shadow directory record */
1170         rec->r.sdir.lid     = buftoulong(p); p += 4;
1171         rec->r.sdir.keyid[0]= buftou32(p); p += 4;
1172         rec->r.sdir.keyid[1]= buftou32(p); p += 4;
1173         rec->r.sdir.pubkey_algo = *p++;
1174         p += 3;
1175         rec->r.sdir.hintlist = buftoulong(p);
1176         if( rec->r.sdir.lid != recnum ) {
1177             log_error( "%s: sdir LID != recnum (%lu,%lu)\n",
1178                                db_name, rec->r.sdir.lid, (ulong)recnum );
1179             rc = G10ERR_TRUSTDB;
1180         }
1181         break;
1182       case RECTYPE_CACH:   /* cache record */
1183         rec->r.cache.lid    = buftoulong(p); p += 4;
1184         memcpy(rec->r.cache.blockhash, p, 20); p += 20;
1185         rec->r.cache.trustlevel = *p++;
1186         break;
1187       case RECTYPE_HTBL:
1188         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
1189             rec->r.htbl.item[i] = buftoulong(p); p += 4;
1190         }
1191         break;
1192       case RECTYPE_HLST:
1193         rec->r.hlst.next = buftoulong(p); p += 4;
1194         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
1195             rec->r.hlst.rnum[i] = buftoulong(p); p += 4;
1196         }
1197         break;
1198       default:
1199         log_error( "%s: invalid record type %d at recnum %lu\n",
1200                                    db_name, rec->rectype, (ulong)recnum );
1201         rc = G10ERR_TRUSTDB;
1202         break;
1203     }
1204
1205     return rc;
1206 }
1207
1208 /****************
1209  * Write the record at RECNUM
1210  */
1211 int
1212 tdbio_write_record( TRUSTREC *rec )
1213 {
1214     byte buf[TRUST_RECORD_LEN], *p;
1215     int rc = 0;
1216     int i;
1217     ulong recnum = rec->recnum;
1218
1219     if( db_fd == -1 )
1220         open_db();
1221
1222     memset(buf, 0, TRUST_RECORD_LEN);
1223     p = buf;
1224     *p++ = rec->rectype; p++;
1225     switch( rec->rectype ) {
1226       case 0:  /* unused record */
1227         break;
1228       case RECTYPE_VER: /* version record */
1229         if( recnum )
1230             BUG();
1231         memcpy(p-1, "gpg", 3 ); p += 2;
1232         *p++ = rec->r.ver.version;
1233         *p++ = rec->r.ver.marginals;
1234         *p++ = rec->r.ver.completes;
1235         *p++ = rec->r.ver.cert_depth;
1236         p += 4; /* skip lock flags */
1237         ulongtobuf(p, rec->r.ver.created); p += 4;
1238         ulongtobuf(p, rec->r.ver.modified); p += 4;
1239         ulongtobuf(p, rec->r.ver.validated); p += 4;
1240         ulongtobuf(p, rec->r.ver.keyhashtbl); p += 4;
1241         ulongtobuf(p, rec->r.ver.firstfree ); p += 4;
1242         ulongtobuf(p, rec->r.ver.sdirhashtbl ); p += 4;
1243         break;
1244
1245       case RECTYPE_FREE:
1246         ulongtobuf(p, rec->r.free.next); p += 4;
1247         break;
1248
1249       case RECTYPE_DIR:   /*directory record */
1250         ulongtobuf(p, rec->r.dir.lid); p += 4;
1251         ulongtobuf(p, rec->r.dir.keylist); p += 4;
1252         ulongtobuf(p, rec->r.dir.uidlist); p += 4;
1253         ulongtobuf(p, rec->r.dir.cacherec); p += 4;
1254         *p++ = rec->r.dir.ownertrust;
1255         *p++ = rec->r.dir.dirflags;
1256         *p++ = rec->r.dir.validity;
1257         assert( rec->r.dir.lid == recnum );
1258         break;
1259
1260       case RECTYPE_KEY:
1261         ulongtobuf(p, rec->r.key.lid); p += 4;
1262         ulongtobuf(p, rec->r.key.next); p += 4;
1263         p += 7;
1264         *p++ = rec->r.key.keyflags;
1265         *p++ = rec->r.key.pubkey_algo;
1266         *p++ = rec->r.key.fingerprint_len;
1267         memcpy( p, rec->r.key.fingerprint, 20); p += 20;
1268         break;
1269
1270       case RECTYPE_UID:   /* user id record */
1271         ulongtobuf(p, rec->r.uid.lid); p += 4;
1272         ulongtobuf(p, rec->r.uid.next); p += 4;
1273         ulongtobuf(p, rec->r.uid.prefrec); p += 4;
1274         ulongtobuf(p, rec->r.uid.siglist); p += 4;
1275         *p++ = rec->r.uid.uidflags;
1276         p++;
1277         memcpy( p, rec->r.uid.namehash, 20 ); p += 20;
1278         break;
1279
1280       case RECTYPE_PREF:
1281         ulongtobuf(p, rec->r.pref.lid); p += 4;
1282         ulongtobuf(p, rec->r.pref.next); p += 4;
1283         memcpy( p, rec->r.pref.data, 30 );
1284         break;
1285
1286       case RECTYPE_SIG:
1287         ulongtobuf(p, rec->r.sig.lid); p += 4;
1288         ulongtobuf(p, rec->r.sig.next); p += 4;
1289         for(i=0; i < SIGS_PER_RECORD; i++ ) {
1290             ulongtobuf(p, rec->r.sig.sig[i].lid); p += 4;
1291             *p++ = rec->r.sig.sig[i].flag;
1292         }
1293         break;
1294
1295       case RECTYPE_SDIR:
1296         ulongtobuf( p, rec->r.sdir.lid); p += 4;
1297         u32tobuf( p, rec->r.sdir.keyid[0] ); p += 4;
1298         u32tobuf( p, rec->r.sdir.keyid[1] ); p += 4;
1299         *p++ = rec->r.sdir.pubkey_algo;
1300         p += 3;
1301         ulongtobuf( p, rec->r.sdir.hintlist );
1302         break;
1303
1304       case RECTYPE_CACH:
1305         ulongtobuf(p, rec->r.cache.lid); p += 4;
1306         memcpy(p, rec->r.cache.blockhash, 20); p += 20;
1307         *p++ = rec->r.cache.trustlevel;
1308         break;
1309
1310       case RECTYPE_HTBL:
1311         for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
1312             ulongtobuf( p, rec->r.htbl.item[i]); p += 4;
1313         }
1314         break;
1315
1316       case RECTYPE_HLST:
1317         ulongtobuf( p, rec->r.hlst.next); p += 4;
1318         for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
1319             ulongtobuf( p, rec->r.hlst.rnum[i]); p += 4;
1320         }
1321         break;
1322
1323       default:
1324         BUG();
1325     }
1326
1327     rc = put_record_into_cache( recnum, buf );
1328     if( rc )
1329         ;
1330     if( rec->rectype == RECTYPE_KEY )
1331         rc = update_keyhashtbl( rec );
1332     else if( rec->rectype == RECTYPE_SDIR )
1333         rc = update_sdirhashtbl( rec );
1334
1335     return rc;
1336 }
1337
1338 int
1339 tdbio_delete_record( ulong recnum )
1340 {
1341     TRUSTREC vr, rec;
1342     int rc;
1343
1344     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
1345     if( rc )
1346         log_fatal( _("%s: error reading version record: %s\n"),
1347                                        db_name, g10_errstr(rc) );
1348
1349     rec.recnum = recnum;
1350     rec.rectype = RECTYPE_FREE;
1351     rec.r.free.next = vr.r.ver.firstfree;
1352     vr.r.ver.firstfree = recnum;
1353     rc = tdbio_write_record( &rec );
1354     if( !rc )
1355         rc = tdbio_write_record( &vr );
1356     return rc;
1357 }
1358
1359 /****************
1360  * create a new record and return its record number
1361  */
1362 ulong
1363 tdbio_new_recnum()
1364 {
1365     off_t offset;
1366     ulong recnum;
1367     TRUSTREC vr, rec;
1368     int rc;
1369
1370     /* look for unused records */
1371     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
1372     if( rc )
1373         log_fatal( _("%s: error reading version record: %s\n"),
1374                                              db_name, g10_errstr(rc) );
1375     if( vr.r.ver.firstfree ) {
1376         recnum = vr.r.ver.firstfree;
1377         rc = tdbio_read_record( recnum, &rec, RECTYPE_FREE );
1378         if( rc ) {
1379             log_error( _("%s: error reading free record: %s\n"),
1380                                                   db_name,  g10_errstr(rc) );
1381             return rc;
1382         }
1383         /* update dir record */
1384         vr.r.ver.firstfree = rec.r.free.next;
1385         rc = tdbio_write_record( &vr );
1386         if( rc ) {
1387             log_error( _("%s: error writing dir record: %s\n"),
1388                                                      db_name, g10_errstr(rc) );
1389             return rc;
1390         }
1391         /*zero out the new record */
1392         memset( &rec, 0, sizeof rec );
1393         rec.rectype = 0; /* unused record */
1394         rec.recnum = recnum;
1395         rc = tdbio_write_record( &rec );
1396         if( rc )
1397             log_fatal(_("%s: failed to zero a record: %s\n"),
1398                                        db_name, g10_errstr(rc));
1399     }
1400     else { /* not found, append a new record */
1401         offset = lseek( db_fd, 0, SEEK_END );
1402         if( offset == -1 )
1403             log_fatal("trustdb: lseek to end failed: %s\n", strerror(errno) );
1404         recnum = offset / TRUST_RECORD_LEN;
1405         assert(recnum); /* this is will never be the first record */
1406         /* we must write a record, so that the next call to this function
1407          * returns another recnum */
1408         memset( &rec, 0, sizeof rec );
1409         rec.rectype = 0; /* unused record */
1410         rec.recnum = recnum;
1411         rc = 0;
1412         if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
1413             log_error(_("trustdb rec %lu: lseek failed: %s\n"),
1414                                                 recnum, strerror(errno) );
1415             rc = G10ERR_WRITE_FILE;
1416         }
1417         else {
1418             int n = write( db_fd, &rec, TRUST_RECORD_LEN);
1419             if( n != TRUST_RECORD_LEN ) {
1420                 log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
1421                                                  recnum, n, strerror(errno) );
1422                 rc = G10ERR_WRITE_FILE;
1423             }
1424         }
1425
1426         if( rc )
1427             log_fatal(_("%s: failed to append a record: %s\n"),
1428                                     db_name,    g10_errstr(rc));
1429     }
1430     return recnum ;
1431 }
1432
1433
1434
1435 /****************
1436  * Search the trustdb for a key which matches PK and return the dir record
1437  * The local_id of PK is set to the correct value
1438  */
1439 int
1440 tdbio_search_dir_bypk( PKT_public_key *pk, TRUSTREC *rec )
1441 {
1442     byte fingerprint[MAX_FINGERPRINT_LEN];
1443     size_t fingerlen;
1444     u32 keyid[2];
1445     int rc;
1446
1447     keyid_from_pk( pk, keyid );
1448     fingerprint_from_pk( pk, fingerprint, &fingerlen );
1449     rc = tdbio_search_dir_byfpr( fingerprint, fingerlen,
1450                                  pk->pubkey_algo, rec );
1451
1452     if( !rc ) {
1453         if( pk->local_id && pk->local_id != rec->recnum )
1454             log_error("%s: found record, but LID from memory does "
1455                        "not match recnum (%lu,%lu)\n",
1456                             db_name,  pk->local_id, rec->recnum );
1457         pk->local_id = rec->recnum;
1458     }
1459     return rc;
1460 }
1461
1462
1463 static int
1464 cmp_krec_fpr( void *dataptr, const TRUSTREC *rec )
1465 {
1466     const struct cmp_krec_fpr_struct *d = dataptr;
1467
1468     return rec->rectype == RECTYPE_KEY
1469            && ( !d->pubkey_algo || rec->r.key.pubkey_algo == d->pubkey_algo )
1470            && rec->r.key.fingerprint_len == d->fprlen
1471            && !memcmp( rec->r.key.fingerprint, d->fpr, d->fprlen );
1472 }
1473
1474 int
1475 tdbio_search_dir_byfpr( const byte *fingerprint, size_t fingerlen,
1476                         int pubkey_algo, TRUSTREC *rec )
1477 {
1478     struct cmp_krec_fpr_struct cmpdata;
1479     ulong recnum;
1480     int rc;
1481
1482     assert( fingerlen == 20 || fingerlen == 16 );
1483
1484     /* locate the key using the hash table */
1485     cmpdata.pubkey_algo = pubkey_algo;
1486     cmpdata.fpr = fingerprint;
1487     cmpdata.fprlen = fingerlen;
1488     rc = lookup_hashtable( get_keyhashrec(), fingerprint, fingerlen,
1489                            cmp_krec_fpr, &cmpdata, rec );
1490     if( !rc ) {
1491         recnum = rec->r.key.lid;
1492         /* Now read the dir record */
1493         rc = tdbio_read_record( recnum, rec, RECTYPE_DIR);
1494         if( rc )
1495             log_error("%s: can't read dirrec %lu: %s\n",
1496                                      db_name, recnum, g10_errstr(rc) );
1497     }
1498     return rc;
1499 }
1500
1501
1502
1503 static int
1504 cmp_sdir( void *dataptr, const TRUSTREC *rec )
1505 {
1506     const struct cmp_sdir_struct *d = dataptr;
1507
1508     return rec->rectype == RECTYPE_SDIR
1509            && ( !d->pubkey_algo || rec->r.sdir.pubkey_algo == d->pubkey_algo )
1510            && rec->r.sdir.keyid[0] == d->keyid[0]
1511            && rec->r.sdir.keyid[1] == d->keyid[1];
1512 }
1513
1514
1515 int
1516 tdbio_search_sdir( u32 *keyid, int pubkey_algo, TRUSTREC *rec )
1517 {
1518     struct cmp_sdir_struct cmpdata;
1519     int rc;
1520     byte key[8];
1521
1522     /* locate the shadow dir record using the hash table */
1523     u32tobuf( key   , keyid[0] );
1524     u32tobuf( key+4 , keyid[1] );
1525     cmpdata.pubkey_algo = pubkey_algo;
1526     cmpdata.keyid[0] = keyid[0];
1527     cmpdata.keyid[1] = keyid[1];
1528     rc = lookup_hashtable( get_sdirhashrec(), key, 8,
1529                            cmp_sdir, &cmpdata, rec );
1530     return rc;
1531 }
1532
1533