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