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