chiark / gitweb /
Import gnupg2_2.1.17.orig.tar.bz2
[gnupg2.git] / g10 / tdbio.c
1 /* tdbio.c - trust database I/O operations
2  * Copyright (C) 1998-2002, 2012 Free Software Foundation, Inc.
3  * Copyright (C) 1998-2015 Werner Koch
4  *
5  * This file is part of GnuPG.
6  *
7  * GnuPG is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * GnuPG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <https://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include "gpg.h"
32 #include "status.h"
33 #include "iobuf.h"
34 #include "util.h"
35 #include "options.h"
36 #include "main.h"
37 #include "i18n.h"
38 #include "trustdb.h"
39 #include "tdbio.h"
40
41 #if defined(HAVE_DOSISH_SYSTEM) && !defined(ftruncate)
42 #define ftruncate chsize
43 #endif
44
45 #if defined(HAVE_DOSISH_SYSTEM) || defined(__CYGWIN__)
46 #define MY_O_BINARY  O_BINARY
47 #else
48 #define MY_O_BINARY  0
49 #endif
50
51 /* We use ERRNO despite that the cegcc provided open/read/write
52    functions don't set ERRNO - at least show that ERRNO does not make
53    sense.  */
54 #ifdef HAVE_W32CE_SYSTEM
55 #undef strerror
56 #define strerror(a) ("[errno not available]")
57 #endif
58
59 /*
60  * Yes, this is a very simple implementation. We should really
61  * use a page aligned buffer and read complete pages.
62  * To implement a simple trannsaction system, this is sufficient.
63  */
64 typedef struct cache_ctrl_struct *CACHE_CTRL;
65 struct cache_ctrl_struct
66 {
67   CACHE_CTRL next;
68   struct {
69     unsigned used:1;
70     unsigned dirty:1;
71   } flags;
72   ulong recno;
73   char data[TRUST_RECORD_LEN];
74 };
75
76 /* Size of the cache.  The SOFT value is the general one.  While in a
77    transaction this may not be sufficient and thus we may increase it
78    then up to the HARD limit.  */
79 #define MAX_CACHE_ENTRIES_SOFT  200
80 #define MAX_CACHE_ENTRIES_HARD  10000
81
82
83 /* The cache is controlled by these variables.  */
84 static CACHE_CTRL cache_list;
85 static int cache_entries;
86 static int cache_is_dirty;
87
88
89 /* An object to pass information to cmp_krec_fpr. */
90 struct cmp_krec_fpr_struct
91 {
92   int pubkey_algo;
93   const char *fpr;
94   int fprlen;
95 };
96
97 /* An object used to pass information to cmp_[s]dir. */
98 struct cmp_xdir_struct
99 {
100   int pubkey_algo;
101   u32 keyid[2];
102 };
103
104
105 /* The name of the trustdb file.  */
106 static char *db_name;
107
108 /* The handle for locking the trustdb file and a flag to record
109    whether a lock has been taken.  */
110 static dotlock_t lockhandle;
111 static int is_locked;
112
113 /* The file descriptor of the trustdb.  */
114 static int  db_fd = -1;
115
116 /* A flag indicating that a transaction is active.  */
117 static int in_transaction;
118
119
120 \f
121 static void open_db (void);
122 static void create_hashtable (TRUSTREC *vr, int type);
123
124
125 \f
126 /*
127  * Take a lock on the trustdb file name.  I a lock file can't be
128  * created the function terminates the process.  Excvept for a
129  * different return code the function does nothing if the lock has
130  * already been taken.
131  *
132  * Returns: True if lock already exists, False if the lock has
133  *          actually been taken.
134  */
135 static int
136 take_write_lock (void)
137 {
138   if (!lockhandle)
139     lockhandle = dotlock_create (db_name, 0);
140   if (!lockhandle)
141     log_fatal ( _("can't create lock for '%s'\n"), db_name );
142
143   if (!is_locked)
144     {
145       if (dotlock_take (lockhandle, -1) )
146         log_fatal ( _("can't lock '%s'\n"), db_name );
147       else
148         is_locked = 1;
149       return 0;
150     }
151   else
152     return 1;
153 }
154
155
156 /*
157  * Release a lock from the trustdb file unless the global option
158  * --lock-once has been used.
159  */
160 static void
161 release_write_lock (void)
162 {
163   if (!opt.lock_once)
164     if (!dotlock_release (lockhandle))
165       is_locked = 0;
166 }
167 \f
168 /*************************************
169  ************* record cache **********
170  *************************************/
171
172 /*
173  * Get the data from the record cache and return a pointer into that
174  * cache.  Caller should copy the returned data.  NULL is returned on
175  * a cache miss.
176  */
177 static const char *
178 get_record_from_cache (ulong recno)
179 {
180   CACHE_CTRL r;
181
182   for (r = cache_list; r; r = r->next)
183     {
184       if (r->flags.used && r->recno == recno)
185         return r->data;
186     }
187   return NULL;
188 }
189
190
191 /*
192  * Write a cached item back to the trustdb file.
193  *
194  * Returns: 0 on success or an error code.
195  */
196 static int
197 write_cache_item (CACHE_CTRL r)
198 {
199   gpg_error_t err;
200   int n;
201
202   if (lseek (db_fd, r->recno * TRUST_RECORD_LEN, SEEK_SET) == -1)
203     {
204       err = gpg_error_from_syserror ();
205       log_error (_("trustdb rec %lu: lseek failed: %s\n"),
206                  r->recno, strerror (errno));
207       return err;
208     }
209   n = write (db_fd, r->data, TRUST_RECORD_LEN);
210   if (n != TRUST_RECORD_LEN)
211     {
212       err = gpg_error_from_syserror ();
213       log_error (_("trustdb rec %lu: write failed (n=%d): %s\n"),
214                  r->recno, n, strerror (errno) );
215       return err;
216     }
217   r->flags.dirty = 0;
218   return 0;
219 }
220
221
222 /*
223  * Put data into the cache.  This function may flush
224  * some cache entries if the cache is filled up.
225  *
226  * Returns: 0 on success or an error code.
227  */
228 static int
229 put_record_into_cache (ulong recno, const char *data)
230 {
231   CACHE_CTRL r, unused;
232   int dirty_count = 0;
233   int clean_count = 0;
234
235   /* See whether we already cached this one.  */
236   for (unused = NULL, r = cache_list; r; r = r->next)
237     {
238       if (!r->flags.used)
239         {
240           if (!unused)
241             unused = r;
242         }
243       else if (r->recno == recno)
244         {
245           if (!r->flags.dirty)
246             {
247               /* Hmmm: should we use a copy and compare? */
248               if (memcmp (r->data, data, TRUST_RECORD_LEN))
249                 {
250                   r->flags.dirty = 1;
251                   cache_is_dirty = 1;
252                 }
253             }
254           memcpy (r->data, data, TRUST_RECORD_LEN);
255           return 0;
256         }
257       if (r->flags.used)
258         {
259           if (r->flags.dirty)
260             dirty_count++;
261           else
262             clean_count++;
263         }
264     }
265
266   /* Not in the cache: add a new entry. */
267   if (unused)
268     {
269       /* Reuse this entry. */
270       r = unused;
271       r->flags.used = 1;
272       r->recno = recno;
273       memcpy (r->data, data, TRUST_RECORD_LEN);
274       r->flags.dirty = 1;
275       cache_is_dirty = 1;
276       cache_entries++;
277       return 0;
278     }
279
280   /* See whether we reached the limit. */
281   if (cache_entries < MAX_CACHE_ENTRIES_SOFT)
282     {
283       /* No: Put into cache.  */
284       r = xmalloc (sizeof *r);
285       r->flags.used = 1;
286       r->recno = recno;
287       memcpy (r->data, data, TRUST_RECORD_LEN);
288       r->flags.dirty = 1;
289       r->next = cache_list;
290       cache_list = r;
291       cache_is_dirty = 1;
292       cache_entries++;
293       return 0;
294     }
295
296   /* Cache is full: discard some clean entries.  */
297   if (clean_count)
298     {
299       int n;
300
301       /* We discard a third of the clean entries.  */
302       n = clean_count / 3;
303       if (!n)
304         n = 1;
305
306       for (unused = NULL, r = cache_list; r; r = r->next)
307         {
308           if (r->flags.used && !r->flags.dirty)
309             {
310               if (!unused)
311                 unused = r;
312               r->flags.used = 0;
313               cache_entries--;
314               if (!--n)
315                 break;
316             }
317         }
318
319       /* Now put into the cache.  */
320       log_assert (unused);
321       r = unused;
322       r->flags.used = 1;
323       r->recno = recno;
324       memcpy (r->data, data, TRUST_RECORD_LEN);
325       r->flags.dirty = 1;
326       cache_is_dirty = 1;
327       cache_entries++;
328       return 0;
329     }
330
331   /* No clean entries: We have to flush some dirty entries.  */
332   if (in_transaction)
333     {
334       /* But we can't do this while in a transaction.  Thus we
335        * increase the cache size instead.  */
336       if (cache_entries < MAX_CACHE_ENTRIES_HARD)
337         {
338           if (opt.debug && !(cache_entries % 100))
339             log_debug ("increasing tdbio cache size\n");
340           r = xmalloc (sizeof *r);
341           r->flags.used = 1;
342           r->recno = recno;
343           memcpy (r->data, data, TRUST_RECORD_LEN);
344           r->flags.dirty = 1;
345           r->next = cache_list;
346           cache_list = r;
347           cache_is_dirty = 1;
348           cache_entries++;
349           return 0;
350         }
351       /* Hard limit for the cache size reached.  */
352       log_info (_("trustdb transaction too large\n"));
353       return GPG_ERR_RESOURCE_LIMIT;
354     }
355
356   if (dirty_count)
357     {
358       int n;
359
360       /* Discard some dirty entries. */
361       n = dirty_count / 5;
362       if (!n)
363         n = 1;
364
365       take_write_lock ();
366       for (unused = NULL, r = cache_list; r; r = r->next)
367         {
368           if (r->flags.used && r->flags.dirty)
369             {
370               int rc;
371
372               rc = write_cache_item (r);
373               if (rc)
374                 return rc;
375               if (!unused)
376                 unused = r;
377               r->flags.used = 0;
378               cache_entries--;
379               if (!--n)
380                 break;
381             }
382         }
383       release_write_lock ();
384
385       /* Now put into the cache.  */
386       log_assert (unused);
387       r = unused;
388       r->flags.used = 1;
389       r->recno = recno;
390       memcpy (r->data, data, TRUST_RECORD_LEN);
391       r->flags.dirty = 1;
392       cache_is_dirty = 1;
393       cache_entries++;
394       return 0;
395     }
396
397   /* We should never reach this.  */
398   BUG();
399 }
400
401
402 /* Return true if the cache is dirty.  */
403 int
404 tdbio_is_dirty()
405 {
406   return cache_is_dirty;
407 }
408
409
410 /*
411  * Flush the cache.  This cannot be used while in a transaction.
412  */
413 int
414 tdbio_sync()
415 {
416     CACHE_CTRL r;
417     int did_lock = 0;
418
419     if( db_fd == -1 )
420         open_db();
421     if( in_transaction )
422         log_bug("tdbio: syncing while in transaction\n");
423
424     if( !cache_is_dirty )
425         return 0;
426
427     if (!take_write_lock ())
428         did_lock = 1;
429
430     for( r = cache_list; r; r = r->next ) {
431         if( r->flags.used && r->flags.dirty ) {
432             int rc = write_cache_item( r );
433             if( rc )
434                 return rc;
435         }
436     }
437     cache_is_dirty = 0;
438     if (did_lock)
439         release_write_lock ();
440
441     return 0;
442 }
443
444
445 #if 0  /* Not yet used.  */
446 /*
447  * Simple transactions system:
448  * Everything between begin_transaction and end/cancel_transaction
449  * is not immediately written but at the time of end_transaction.
450  *
451  * NOTE: The transaction code is disabled in the 1.2 branch, as it is
452  * not yet used.
453  */
454 int
455 tdbio_begin_transaction ()  /* Not yet used.  */
456 {
457   int rc;
458
459   if (in_transaction)
460     log_bug ("tdbio: nested transactions\n");
461   /* Flush everything out. */
462   rc = tdbio_sync();
463   if (rc)
464     return rc;
465   in_transaction = 1;
466   return 0;
467 }
468
469 int
470 tdbio_end_transaction ()  /* Not yet used.  */
471 {
472   int rc;
473
474   if (!in_transaction)
475     log_bug ("tdbio: no active transaction\n");
476   take_write_lock ();
477   gnupg_block_all_signals ();
478   in_transaction = 0;
479   rc = tdbio_sync();
480   gnupg_unblock_all_signals();
481   release_write_lock ();
482   return rc;
483 }
484
485 int
486 tdbio_cancel_transaction () /* Not yet used.  */
487 {
488   CACHE_CTRL r;
489
490   if (!in_transaction)
491     log_bug ("tdbio: no active transaction\n");
492
493   /* Remove all dirty marked entries, so that the original ones are
494    * read back the next time.  */
495   if (cache_is_dirty)
496     {
497       for (r = cache_list; r; r = r->next)
498         {
499           if (r->flags.used && r->flags.dirty)
500             {
501               r->flags.used = 0;
502               cache_entries--;
503             }
504         }
505       cache_is_dirty = 0;
506     }
507
508   in_transaction = 0;
509   return 0;
510 }
511 #endif  /* Not yet used.  */
512
513
514 \f
515 /********************************************************
516  **************** cached I/O functions ******************
517  ********************************************************/
518
519 /* The cleanup handler for this module.  */
520 static void
521 cleanup (void)
522 {
523   if (is_locked)
524     {
525       if (!dotlock_release (lockhandle))
526         is_locked = 0;
527     }
528 }
529
530
531 /*
532  * Update an existing trustdb record.  The caller must call
533  * tdbio_sync.
534  *
535  * Returns: 0 on success or an error code.
536  */
537 int
538 tdbio_update_version_record (void)
539 {
540   TRUSTREC rec;
541   int rc;
542
543   memset (&rec, 0, sizeof rec);
544
545   rc = tdbio_read_record (0, &rec, RECTYPE_VER);
546   if (!rc)
547     {
548       rec.r.ver.created     = make_timestamp();
549       rec.r.ver.marginals   = opt.marginals_needed;
550       rec.r.ver.completes   = opt.completes_needed;
551       rec.r.ver.cert_depth  = opt.max_cert_depth;
552       rec.r.ver.trust_model = opt.trust_model;
553       rec.r.ver.min_cert_level = opt.min_cert_level;
554       rc=tdbio_write_record(&rec);
555     }
556
557   return rc;
558 }
559
560
561 /*
562  * Create and write the trustdb version record.
563  *
564  * Returns: 0 on success or an error code.
565  */
566 static int
567 create_version_record (void)
568 {
569   TRUSTREC rec;
570   int rc;
571
572   memset (&rec, 0, sizeof rec);
573   rec.r.ver.version     = 3;
574   rec.r.ver.created     = make_timestamp ();
575   rec.r.ver.marginals   = opt.marginals_needed;
576   rec.r.ver.completes   = opt.completes_needed;
577   rec.r.ver.cert_depth  = opt.max_cert_depth;
578   if (opt.trust_model == TM_PGP || opt.trust_model == TM_CLASSIC)
579     rec.r.ver.trust_model = opt.trust_model;
580   else
581     rec.r.ver.trust_model = TM_PGP;
582   rec.r.ver.min_cert_level = opt.min_cert_level;
583   rec.rectype = RECTYPE_VER;
584   rec.recnum = 0;
585   rc = tdbio_write_record (&rec);
586
587   if (!rc)
588     tdbio_sync ();
589
590   if (!rc)
591     create_hashtable (&rec, 0);
592
593   return rc;
594 }
595
596
597 /*
598  * Set the file name for the trustdb to NEW_DBNAME and if CREATE is
599  * true create that file.  If NEW_DBNAME is NULL a default name is
600  * used, if the it does not contain a path component separator ('/')
601  * the global GnuPG home directory is used.
602  *
603  * Returns: 0 on success or an error code.
604  *
605  * On the first call this function registers an atexit handler.
606  *
607  */
608 int
609 tdbio_set_dbname (const char *new_dbname, int create, int *r_nofile)
610 {
611   char *fname, *p;
612   struct stat statbuf;
613   static int initialized = 0;
614   int save_slash;
615
616   if (!initialized)
617     {
618       atexit (cleanup);
619       initialized = 1;
620     }
621
622   *r_nofile = 0;
623
624   if (!new_dbname)
625     {
626       fname = make_filename (gnupg_homedir (),
627                              "trustdb" EXTSEP_S GPGEXT_GPG, NULL);
628     }
629   else if (*new_dbname != DIRSEP_C )
630     {
631       if (strchr (new_dbname, DIRSEP_C))
632         fname = make_filename (new_dbname, NULL);
633       else
634         fname = make_filename (gnupg_homedir (), new_dbname, NULL);
635     }
636   else
637     {
638       fname = xstrdup (new_dbname);
639     }
640
641   xfree (db_name);
642   db_name = fname;
643
644   /* Quick check for (likely) case where there already is a
645    * trustdb.gpg.  This check is not required in theory, but it helps
646    * in practice avoiding costly operations of preparing and taking
647    * the lock.  */
648   if (!stat (fname, &statbuf) && statbuf.st_size > 0)
649     {
650       /* OK, we have the valid trustdb.gpg already.  */
651       return 0;
652     }
653   else if (!create)
654     {
655       *r_nofile = 1;
656       return 0;
657     }
658
659   /* Here comes: No valid trustdb.gpg AND CREATE==1 */
660
661   /*
662    * Make sure the directory exists.  This should be done before
663    * acquiring the lock, which assumes the existence of the directory.
664    */
665   p = strrchr (fname, DIRSEP_C);
666 #if HAVE_W32_SYSTEM
667   {
668     /* Windows may either have a slash or a backslash.  Take
669        care of it.  */
670     char *pp = strrchr (fname, '/');
671     if (!p || pp > p)
672       p = pp;
673   }
674 #endif /*HAVE_W32_SYSTEM*/
675   log_assert (p);
676   save_slash = *p;
677   *p = 0;
678   if (access (fname, F_OK))
679     {
680       try_make_homedir (fname);
681       if (access (fname, F_OK))
682         log_fatal (_("%s: directory does not exist!\n"), fname);
683     }
684   *p = save_slash;
685
686   take_write_lock ();
687
688   if (access (fname, R_OK) || stat (fname, &statbuf) || statbuf.st_size == 0)
689     {
690       FILE *fp;
691       TRUSTREC rec;
692       int rc;
693       mode_t oldmask;
694
695 #ifdef HAVE_W32CE_SYSTEM
696       /* We know how the cegcc implementation of access works ;-). */
697       if (GetLastError () == ERROR_FILE_NOT_FOUND)
698         gpg_err_set_errno (ENOENT);
699       else
700         gpg_err_set_errno (EIO);
701 #endif /*HAVE_W32CE_SYSTEM*/
702       if (errno && errno != ENOENT)
703         log_fatal ( _("can't access '%s': %s\n"), fname, strerror (errno));
704
705       oldmask = umask (077);
706       if (is_secured_filename (fname))
707         {
708           fp = NULL;
709           gpg_err_set_errno (EPERM);
710         }
711       else
712         fp = fopen (fname, "wb");
713       umask(oldmask);
714       if (!fp)
715         log_fatal (_("can't create '%s': %s\n"), fname, strerror (errno));
716       fclose (fp);
717
718       db_fd = open (db_name, O_RDWR | MY_O_BINARY);
719       if (db_fd == -1)
720         log_fatal (_("can't open '%s': %s\n"), db_name, strerror (errno));
721
722       rc = create_version_record ();
723       if (rc)
724         log_fatal (_("%s: failed to create version record: %s"),
725                    fname, gpg_strerror (rc));
726
727       /* Read again to check that we are okay. */
728       if (tdbio_read_record (0, &rec, RECTYPE_VER))
729         log_fatal (_("%s: invalid trustdb created\n"), db_name);
730
731       if (!opt.quiet)
732         log_info (_("%s: trustdb created\n"), db_name);
733     }
734
735   release_write_lock ();
736   return 0;
737 }
738
739
740 /*
741  * Return the full name of the trustdb.
742  */
743 const char *
744 tdbio_get_dbname ()
745 {
746   return db_name;
747 }
748
749
750 /*
751  * Open the trustdb.  This may only be called if it has not yet been
752  * opened and after a successful call to tdbio_set_dbname.  On return
753  * the trustdb handle (DB_FD) is guaranteed to be open.
754  */
755 static void
756 open_db ()
757 {
758   TRUSTREC rec;
759
760   log_assert( db_fd == -1 );
761
762 #ifdef HAVE_W32CE_SYSTEM
763   {
764     DWORD prevrc = 0;
765     wchar_t *wname = utf8_to_wchar (db_name);
766     if (wname)
767       {
768         db_fd = (int)CreateFile (wname, GENERIC_READ|GENERIC_WRITE,
769                                  FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
770                                  OPEN_EXISTING, 0, NULL);
771         xfree (wname);
772       }
773     if (db_fd == -1)
774       log_fatal ("can't open '%s': %d, %d\n", db_name,
775                  (int)prevrc, (int)GetLastError ());
776   }
777 #else /*!HAVE_W32CE_SYSTEM*/
778   db_fd = open (db_name, O_RDWR | MY_O_BINARY );
779   if (db_fd == -1 && (errno == EACCES
780 #ifdef EROFS
781                       || errno == EROFS
782 #endif
783                       )
784       ) {
785       /* Take care of read-only trustdbs.  */
786       db_fd = open (db_name, O_RDONLY | MY_O_BINARY );
787       if (db_fd != -1 && !opt.quiet)
788           log_info (_("Note: trustdb not writable\n"));
789   }
790   if ( db_fd == -1 )
791     log_fatal( _("can't open '%s': %s\n"), db_name, strerror(errno) );
792 #endif /*!HAVE_W32CE_SYSTEM*/
793   register_secured_file (db_name);
794
795   /* Read the version record. */
796   if (tdbio_read_record (0, &rec, RECTYPE_VER ) )
797     log_fatal( _("%s: invalid trustdb\n"), db_name );
798 }
799
800
801 /*
802  * Append a new empty hashtable to the trustdb.  TYPE gives the type
803  * of the hash table.  The only defined type is 0 for a trust hash.
804  * On return the hashtable has been created, written, the version
805  * record update, and the data flushed to the disk.  On a fatal error
806  * the function terminates the process.
807  */
808 static void
809 create_hashtable( TRUSTREC *vr, int type )
810 {
811   TRUSTREC rec;
812   off_t offset;
813   ulong recnum;
814   int i, n, rc;
815
816   offset = lseek (db_fd, 0, SEEK_END);
817   if (offset == -1)
818     log_fatal ("trustdb: lseek to end failed: %s\n", strerror(errno));
819   recnum = offset / TRUST_RECORD_LEN;
820   log_assert (recnum); /* This is will never be the first record. */
821
822   if (!type)
823     vr->r.ver.trusthashtbl = recnum;
824
825   /* Now write the records making up the hash table. */
826   n = (256+ITEMS_PER_HTBL_RECORD-1) / ITEMS_PER_HTBL_RECORD;
827   for (i=0; i < n; i++, recnum++)
828     {
829       memset (&rec, 0, sizeof rec);
830       rec.rectype = RECTYPE_HTBL;
831       rec.recnum = recnum;
832       rc = tdbio_write_record (&rec);
833       if (rc)
834         log_fatal (_("%s: failed to create hashtable: %s\n"),
835                    db_name, gpg_strerror (rc));
836     }
837   /* Update the version record and flush. */
838   rc = tdbio_write_record (vr);
839   if (!rc)
840     rc = tdbio_sync ();
841   if (rc)
842     log_fatal (_("%s: error updating version record: %s\n"),
843                db_name, gpg_strerror (rc));
844 }
845
846
847 /*
848  * Check whether open trustdb matches the global trust options given
849  * for this process.  On a read problem the process is terminated.
850  *
851  * Return: 1 for yes, 0 for no.
852  */
853 int
854 tdbio_db_matches_options()
855 {
856   static int yes_no = -1;
857
858   if (yes_no == -1)
859     {
860       TRUSTREC vr;
861       int rc;
862
863       rc = tdbio_read_record (0, &vr, RECTYPE_VER);
864       if( rc )
865         log_fatal( _("%s: error reading version record: %s\n"),
866                    db_name, gpg_strerror (rc) );
867
868       yes_no = vr.r.ver.marginals == opt.marginals_needed
869         && vr.r.ver.completes == opt.completes_needed
870         && vr.r.ver.cert_depth == opt.max_cert_depth
871         && vr.r.ver.trust_model == opt.trust_model
872         && vr.r.ver.min_cert_level == opt.min_cert_level;
873     }
874
875   return yes_no;
876 }
877
878
879 /*
880  * Read and return the trust model identifier from the trustdb.  On a
881  * read problem the process is terminated.
882  */
883 byte
884 tdbio_read_model (void)
885 {
886   TRUSTREC vr;
887   int rc;
888
889   rc = tdbio_read_record (0, &vr, RECTYPE_VER );
890   if (rc)
891     log_fatal (_("%s: error reading version record: %s\n"),
892                db_name, gpg_strerror (rc) );
893   return vr.r.ver.trust_model;
894 }
895
896
897 /*
898  * Read and return the nextstamp value from the trustdb.  On a read
899  * problem the process is terminated.
900  */
901 ulong
902 tdbio_read_nextcheck ()
903 {
904   TRUSTREC vr;
905   int rc;
906
907   rc = tdbio_read_record (0, &vr, RECTYPE_VER);
908   if (rc)
909     log_fatal (_("%s: error reading version record: %s\n"),
910                db_name, gpg_strerror (rc));
911   return vr.r.ver.nextcheck;
912 }
913
914
915 /*
916  * Write the STAMP nextstamp timestamp to the trustdb.  On a read or
917  * write problem the process is terminated.
918  *
919  * Return: True if the stamp actually changed.
920  */
921 int
922 tdbio_write_nextcheck (ulong stamp)
923 {
924   TRUSTREC vr;
925   int rc;
926
927   rc = tdbio_read_record (0, &vr, RECTYPE_VER);
928   if (rc)
929     log_fatal (_("%s: error reading version record: %s\n"),
930                db_name, gpg_strerror (rc));
931
932   if (vr.r.ver.nextcheck == stamp)
933     return 0;
934
935   vr.r.ver.nextcheck = stamp;
936   rc = tdbio_write_record( &vr );
937   if (rc)
938     log_fatal (_("%s: error writing version record: %s\n"),
939                db_name, gpg_strerror (rc));
940   return 1;
941 }
942
943
944
945 /*
946  * Return the record number of the trusthash table or create one if it
947  * does not yet exist.  On a read or write problem the process is
948  * terminated.
949  *
950  * Return: record number
951  */
952 static ulong
953 get_trusthashrec(void)
954 {
955   static ulong trusthashtbl; /* Record number of the trust hashtable.  */
956
957   if (!trusthashtbl)
958     {
959       TRUSTREC vr;
960       int rc;
961
962       rc = tdbio_read_record (0, &vr, RECTYPE_VER );
963       if (rc)
964         log_fatal (_("%s: error reading version record: %s\n"),
965                    db_name, gpg_strerror (rc) );
966
967       trusthashtbl = vr.r.ver.trusthashtbl;
968     }
969
970   return trusthashtbl;
971 }
972
973
974
975 /*
976  * Update a hashtable in the trustdb.  TABLE gives the start of the
977  * table, KEY and KEYLEN are the key, NEWRECNUM is the record number
978  * to insert into the table.
979  *
980  * Return: 0 on success or an error code.
981  */
982 static int
983 upd_hashtable (ulong table, byte *key, int keylen, ulong newrecnum)
984 {
985   TRUSTREC lastrec, rec;
986   ulong hashrec, item;
987   int msb;
988   int level = 0;
989   int rc, i;
990
991   hashrec = table;
992  next_level:
993   msb = key[level];
994   hashrec += msb / ITEMS_PER_HTBL_RECORD;
995   rc = tdbio_read_record (hashrec, &rec, RECTYPE_HTBL);
996   if (rc)
997     {
998       log_error ("upd_hashtable: read failed: %s\n", gpg_strerror (rc));
999       return rc;
1000     }
1001
1002   item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
1003   if (!item)  /* Insert a new item into the hash table.  */
1004     {
1005       rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = newrecnum;
1006       rc = tdbio_write_record (&rec);
1007       if (rc)
1008         {
1009           log_error ("upd_hashtable: write htbl failed: %s\n",
1010                      gpg_strerror (rc));
1011           return rc;
1012         }
1013     }
1014   else if (item != newrecnum) /* Must do an update.  */
1015     {
1016       lastrec = rec;
1017       rc = tdbio_read_record (item, &rec, 0);
1018       if (rc)
1019         {
1020           log_error ("upd_hashtable: read item failed: %s\n",
1021                      gpg_strerror (rc));
1022           return rc;
1023         }
1024
1025       if (rec.rectype == RECTYPE_HTBL)
1026         {
1027           hashrec = item;
1028           level++;
1029           if (level >= keylen)
1030             {
1031               log_error ("hashtable has invalid indirections.\n");
1032               return GPG_ERR_TRUSTDB;
1033             }
1034           goto next_level;
1035         }
1036       else if (rec.rectype == RECTYPE_HLST) /* Extend the list.  */
1037         {
1038           /* Check whether the key is already in this list. */
1039           for (;;)
1040             {
1041               for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
1042                 {
1043                   if (rec.r.hlst.rnum[i] == newrecnum)
1044                     {
1045                       return 0; /* Okay, already in the list.  */
1046                     }
1047                 }
1048               if (rec.r.hlst.next)
1049                 {
1050                   rc = tdbio_read_record (rec.r.hlst.next, &rec, RECTYPE_HLST);
1051                   if (rc)
1052                     {
1053                       log_error ("upd_hashtable: read hlst failed: %s\n",
1054                                  gpg_strerror (rc) );
1055                       return rc;
1056                     }
1057                 }
1058               else
1059                 break; /* key is not in the list */
1060             }
1061
1062           /* Find the next free entry and put it in.  */
1063           for (;;)
1064             {
1065               for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
1066                 {
1067                   if (!rec.r.hlst.rnum[i])
1068                     {
1069                       /* Empty slot found.  */
1070                       rec.r.hlst.rnum[i] = newrecnum;
1071                       rc = tdbio_write_record (&rec);
1072                       if (rc)
1073                         log_error ("upd_hashtable: write hlst failed: %s\n",
1074                                    gpg_strerror (rc));
1075                       return rc; /* Done.  */
1076                     }
1077                 }
1078
1079               if (rec.r.hlst.next)
1080                 {
1081                   /* read the next reord of the list.  */
1082                   rc = tdbio_read_record (rec.r.hlst.next, &rec, RECTYPE_HLST);
1083                   if (rc)
1084                     {
1085                       log_error ("upd_hashtable: read hlst failed: %s\n",
1086                                  gpg_strerror (rc));
1087                       return rc;
1088                     }
1089                 }
1090               else
1091                 {
1092                   /* Append a new record to the list.  */
1093                   rec.r.hlst.next = item = tdbio_new_recnum ();
1094                   rc = tdbio_write_record (&rec);
1095                   if (rc)
1096                     {
1097                       log_error ("upd_hashtable: write hlst failed: %s\n",
1098                                  gpg_strerror (rc));
1099                       return rc;
1100                     }
1101                   memset (&rec, 0, sizeof rec);
1102                   rec.rectype = RECTYPE_HLST;
1103                   rec.recnum = item;
1104                   rec.r.hlst.rnum[0] = newrecnum;
1105                   rc = tdbio_write_record (&rec);
1106                   if (rc)
1107                     log_error ("upd_hashtable: write ext hlst failed: %s\n",
1108                                gpg_strerror (rc));
1109                   return rc; /* Done.  */
1110                 }
1111             } /* end loop over list slots */
1112
1113         }
1114       else if (rec.rectype == RECTYPE_TRUST) /* Insert a list record.  */
1115         {
1116           if (rec.recnum == newrecnum)
1117             {
1118               return 0;
1119             }
1120           item = rec.recnum; /* Save number of key record.  */
1121           memset (&rec, 0, sizeof rec);
1122           rec.rectype = RECTYPE_HLST;
1123           rec.recnum = tdbio_new_recnum ();
1124           rec.r.hlst.rnum[0] = item;        /* Old key record */
1125           rec.r.hlst.rnum[1] = newrecnum; /* and new key record */
1126           rc = tdbio_write_record (&rec);
1127           if (rc)
1128             {
1129               log_error( "upd_hashtable: write new hlst failed: %s\n",
1130                            gpg_strerror (rc) );
1131               return rc;
1132             }
1133           /* Update the hashtable record.  */
1134           lastrec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = rec.recnum;
1135           rc = tdbio_write_record (&lastrec);
1136           if (rc)
1137             log_error ("upd_hashtable: update htbl failed: %s\n",
1138                        gpg_strerror (rc));
1139           return rc; /* Ready.  */
1140         }
1141       else
1142         {
1143           log_error ("hashtbl %lu: %lu/%d points to an invalid record %lu\n",
1144                      table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
1145           if (opt.verbose > 1)
1146             list_trustdb (es_stderr, NULL);
1147           return GPG_ERR_TRUSTDB;
1148         }
1149     }
1150
1151   return 0;
1152 }
1153
1154
1155 /*
1156  * Drop an entry from a hashtable.  TABLE gives the start of the
1157  * table, KEY and KEYLEN are the key.
1158  *
1159  * Return: 0 on success or an error code.
1160  */
1161 static int
1162 drop_from_hashtable (ulong table, byte *key, int keylen, ulong recnum)
1163 {
1164   TRUSTREC rec;
1165   ulong hashrec, item;
1166   int msb;
1167   int level = 0;
1168   int rc, i;
1169
1170   hashrec = table;
1171  next_level:
1172   msb = key[level];
1173   hashrec += msb / ITEMS_PER_HTBL_RECORD;
1174   rc = tdbio_read_record (hashrec, &rec, RECTYPE_HTBL );
1175   if (rc)
1176     {
1177       log_error ("drop_from_hashtable: read failed: %s\n", gpg_strerror (rc));
1178       return rc;
1179     }
1180
1181   item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
1182   if (!item)
1183     return 0;   /* Not found - forget about it.  */
1184
1185   if (item == recnum) /* Table points direct to the record.  */
1186     {
1187       rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = 0;
1188       rc = tdbio_write_record( &rec );
1189       if (rc)
1190         log_error ("drop_from_hashtable: write htbl failed: %s\n",
1191                    gpg_strerror (rc));
1192       return rc;
1193     }
1194
1195   rc = tdbio_read_record (item, &rec, 0);
1196   if (rc)
1197     {
1198       log_error ("drop_from_hashtable: read item failed: %s\n",
1199                  gpg_strerror (rc));
1200       return rc;
1201     }
1202
1203   if (rec.rectype == RECTYPE_HTBL)
1204     {
1205       hashrec = item;
1206       level++;
1207       if (level >= keylen)
1208         {
1209           log_error ("hashtable has invalid indirections.\n");
1210           return GPG_ERR_TRUSTDB;
1211         }
1212       goto next_level;
1213     }
1214
1215   if (rec.rectype == RECTYPE_HLST)
1216     {
1217       for (;;)
1218         {
1219           for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
1220             {
1221               if (rec.r.hlst.rnum[i] == recnum)
1222                 {
1223                   rec.r.hlst.rnum[i] = 0; /* Mark as free.  */
1224                   rc = tdbio_write_record (&rec);
1225                   if (rc)
1226                     log_error("drop_from_hashtable: write htbl failed: %s\n",
1227                               gpg_strerror (rc));
1228                   return rc;
1229                 }
1230             }
1231           if (rec.r.hlst.next)
1232             {
1233               rc = tdbio_read_record (rec.r.hlst.next, &rec, RECTYPE_HLST);
1234               if (rc)
1235                 {
1236                   log_error ("drop_from_hashtable: read hlst failed: %s\n",
1237                              gpg_strerror (rc));
1238                   return rc;
1239                 }
1240             }
1241           else
1242             return 0; /* Key not in table.  */
1243         }
1244     }
1245
1246   log_error ("hashtbl %lu: %lu/%d points to wrong record %lu\n",
1247              table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
1248   return GPG_ERR_TRUSTDB;
1249 }
1250
1251
1252
1253 /*
1254  * Lookup a record via the hashtable TABLE by (KEY,KEYLEN) and return
1255  * the result in REC.  The return value of CMP() should be True if the
1256  * record is the desired one.
1257  *
1258  * Return: 0 if found, GPG_ERR_NOT_FOUND, or another error code.
1259  */
1260 static gpg_error_t
1261 lookup_hashtable (ulong table, const byte *key, size_t keylen,
1262                   int (*cmpfnc)(const void*, const TRUSTREC *),
1263                   const void *cmpdata, TRUSTREC *rec )
1264 {
1265   int rc;
1266   ulong hashrec, item;
1267   int msb;
1268   int level = 0;
1269
1270   hashrec = table;
1271  next_level:
1272   msb = key[level];
1273   hashrec += msb / ITEMS_PER_HTBL_RECORD;
1274   rc = tdbio_read_record (hashrec, rec, RECTYPE_HTBL);
1275   if (rc)
1276     {
1277       log_error("lookup_hashtable failed: %s\n", gpg_strerror (rc) );
1278       return rc;
1279     }
1280
1281   item = rec->r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
1282   if (!item)
1283     return gpg_error (GPG_ERR_NOT_FOUND);
1284
1285   rc = tdbio_read_record (item, rec, 0);
1286   if (rc)
1287     {
1288       log_error( "hashtable read failed: %s\n", gpg_strerror (rc) );
1289       return rc;
1290     }
1291   if (rec->rectype == RECTYPE_HTBL)
1292     {
1293       hashrec = item;
1294       level++;
1295       if (level >= keylen)
1296         {
1297           log_error ("hashtable has invalid indirections\n");
1298           return GPG_ERR_TRUSTDB;
1299         }
1300       goto next_level;
1301     }
1302   else if (rec->rectype == RECTYPE_HLST)
1303     {
1304       for (;;)
1305         {
1306           int i;
1307
1308           for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
1309             {
1310               if (rec->r.hlst.rnum[i])
1311                 {
1312                   TRUSTREC tmp;
1313
1314                   rc = tdbio_read_record (rec->r.hlst.rnum[i], &tmp, 0);
1315                   if (rc)
1316                     {
1317                       log_error ("lookup_hashtable: read item failed: %s\n",
1318                                  gpg_strerror (rc));
1319                       return rc;
1320                     }
1321                   if ((*cmpfnc)(cmpdata, &tmp))
1322                     {
1323                       *rec = tmp;
1324                       return 0;
1325                     }
1326                 }
1327             }
1328           if (rec->r.hlst.next)
1329             {
1330               rc = tdbio_read_record (rec->r.hlst.next, rec, RECTYPE_HLST);
1331               if (rc)
1332                 {
1333                   log_error ("lookup_hashtable: read hlst failed: %s\n",
1334                              gpg_strerror (rc) );
1335                   return rc;
1336                 }
1337             }
1338           else
1339             return gpg_error (GPG_ERR_NOT_FOUND);
1340         }
1341     }
1342
1343   if ((*cmpfnc)(cmpdata, rec))
1344     return 0; /* really found */
1345
1346   return gpg_error (GPG_ERR_NOT_FOUND); /* no: not found */
1347 }
1348
1349
1350 /*
1351  * Update the trust hash table TR or create the table if it does not
1352  * exist.
1353  *
1354  * Return: 0 on success or an error code.
1355  */
1356 static int
1357 update_trusthashtbl( TRUSTREC *tr )
1358 {
1359   return upd_hashtable (get_trusthashrec(),
1360                         tr->r.trust.fingerprint, 20, tr->recnum);
1361 }
1362
1363
1364 /*
1365  * Dump the trustdb record REC to stream FP.
1366  */
1367 void
1368 tdbio_dump_record (TRUSTREC *rec, estream_t fp)
1369 {
1370   int i;
1371   ulong rnum = rec->recnum;
1372
1373   es_fprintf (fp, "rec %5lu, ", rnum);
1374
1375   switch (rec->rectype)
1376     {
1377     case 0:
1378       es_fprintf (fp, "blank\n");
1379       break;
1380
1381     case RECTYPE_VER:
1382       es_fprintf (fp,
1383          "version, td=%lu, f=%lu, m/c/d=%d/%d/%d tm=%d mcl=%d nc=%lu (%s)\n",
1384                   rec->r.ver.trusthashtbl,
1385                   rec->r.ver.firstfree,
1386                   rec->r.ver.marginals,
1387                   rec->r.ver.completes,
1388                   rec->r.ver.cert_depth,
1389                   rec->r.ver.trust_model,
1390                   rec->r.ver.min_cert_level,
1391                   rec->r.ver.nextcheck,
1392                   strtimestamp(rec->r.ver.nextcheck)
1393                   );
1394       break;
1395
1396     case RECTYPE_FREE:
1397       es_fprintf (fp, "free, next=%lu\n", rec->r.free.next);
1398       break;
1399
1400     case RECTYPE_HTBL:
1401       es_fprintf (fp, "htbl,");
1402       for (i=0; i < ITEMS_PER_HTBL_RECORD; i++)
1403         es_fprintf (fp, " %lu", rec->r.htbl.item[i]);
1404       es_putc ('\n', fp);
1405       break;
1406
1407     case RECTYPE_HLST:
1408       es_fprintf (fp, "hlst, next=%lu,", rec->r.hlst.next);
1409       for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
1410         es_fprintf (fp, " %lu", rec->r.hlst.rnum[i]);
1411       es_putc ('\n', fp);
1412       break;
1413
1414     case RECTYPE_TRUST:
1415       es_fprintf (fp, "trust ");
1416       for (i=0; i < 20; i++)
1417         es_fprintf (fp, "%02X", rec->r.trust.fingerprint[i]);
1418       es_fprintf (fp, ", ot=%d, d=%d, vl=%lu\n", rec->r.trust.ownertrust,
1419                   rec->r.trust.depth, rec->r.trust.validlist);
1420       break;
1421
1422     case RECTYPE_VALID:
1423       es_fprintf (fp, "valid ");
1424       for (i=0; i < 20; i++)
1425         es_fprintf(fp, "%02X", rec->r.valid.namehash[i]);
1426       es_fprintf (fp, ", v=%d, next=%lu\n", rec->r.valid.validity,
1427                   rec->r.valid.next);
1428       break;
1429
1430     default:
1431       es_fprintf (fp, "unknown type %d\n", rec->rectype );
1432       break;
1433     }
1434 }
1435
1436
1437 /*
1438  * Read the record with number RECNUM into the structure REC.  If
1439  * EXPECTED is not 0 reading any other record type will return an
1440  * error.
1441  *
1442  * Return: 0 on success, -1 on EOF, or an error code.
1443  */
1444 int
1445 tdbio_read_record (ulong recnum, TRUSTREC *rec, int expected)
1446 {
1447   byte readbuf[TRUST_RECORD_LEN];
1448   const byte *buf, *p;
1449   gpg_error_t err = 0;
1450   int n, i;
1451
1452   if (db_fd == -1)
1453     open_db ();
1454
1455   buf = get_record_from_cache( recnum );
1456   if (!buf)
1457     {
1458       if (lseek (db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET) == -1)
1459         {
1460           err = gpg_error_from_syserror ();
1461           log_error (_("trustdb: lseek failed: %s\n"), strerror (errno));
1462           return err;
1463         }
1464       n = read (db_fd, readbuf, TRUST_RECORD_LEN);
1465       if (!n)
1466         {
1467           return -1; /* eof */
1468         }
1469       else if (n != TRUST_RECORD_LEN)
1470         {
1471           err = gpg_error_from_syserror ();
1472           log_error (_("trustdb: read failed (n=%d): %s\n"),
1473                      n, strerror(errno));
1474           return err;
1475         }
1476       buf = readbuf;
1477     }
1478   rec->recnum = recnum;
1479   rec->dirty = 0;
1480   p = buf;
1481   rec->rectype = *p++;
1482   if (expected && rec->rectype != expected)
1483     {
1484       log_error ("%lu: read expected rec type %d, got %d\n",
1485                  recnum, expected, rec->rectype);
1486       return gpg_error (GPG_ERR_TRUSTDB);
1487     }
1488   p++;    /* Skip reserved byte.  */
1489   switch (rec->rectype)
1490     {
1491     case 0:  /* unused (free) record */
1492       break;
1493
1494     case RECTYPE_VER: /* version record */
1495       if (memcmp(buf+1, GPGEXT_GPG, 3))
1496         {
1497           log_error (_("%s: not a trustdb file\n"), db_name );
1498           err = gpg_error (GPG_ERR_TRUSTDB);
1499         }
1500       else
1501         {
1502           p += 2; /* skip "gpg" */
1503           rec->r.ver.version  = *p++;
1504           rec->r.ver.marginals = *p++;
1505           rec->r.ver.completes = *p++;
1506           rec->r.ver.cert_depth = *p++;
1507           rec->r.ver.trust_model = *p++;
1508           rec->r.ver.min_cert_level = *p++;
1509           p += 2;
1510           rec->r.ver.created  = buf32_to_ulong(p);
1511           p += 4;
1512           rec->r.ver.nextcheck = buf32_to_ulong(p);
1513           p += 4;
1514           p += 4;
1515           p += 4;
1516           rec->r.ver.firstfree = buf32_to_ulong(p);
1517           p += 4;
1518           p += 4;
1519           rec->r.ver.trusthashtbl = buf32_to_ulong(p);
1520           if (recnum)
1521             {
1522               log_error( _("%s: version record with recnum %lu\n"), db_name,
1523                          (ulong)recnum );
1524               err = gpg_error (GPG_ERR_TRUSTDB);
1525             }
1526           else if (rec->r.ver.version != 3)
1527             {
1528               log_error( _("%s: invalid file version %d\n"), db_name,
1529                          rec->r.ver.version );
1530               err = gpg_error (GPG_ERR_TRUSTDB);
1531             }
1532         }
1533       break;
1534
1535     case RECTYPE_FREE:
1536       rec->r.free.next  = buf32_to_ulong(p);
1537       break;
1538
1539     case RECTYPE_HTBL:
1540       for (i=0; i < ITEMS_PER_HTBL_RECORD; i++)
1541         {
1542           rec->r.htbl.item[i] = buf32_to_ulong(p);
1543           p += 4;
1544         }
1545       break;
1546
1547     case RECTYPE_HLST:
1548       rec->r.hlst.next = buf32_to_ulong(p);
1549       p += 4;
1550       for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
1551         {
1552           rec->r.hlst.rnum[i] = buf32_to_ulong(p);
1553           p += 4;
1554         }
1555       break;
1556
1557     case RECTYPE_TRUST:
1558       memcpy (rec->r.trust.fingerprint, p, 20);
1559       p+=20;
1560       rec->r.trust.ownertrust = *p++;
1561       rec->r.trust.depth = *p++;
1562       rec->r.trust.min_ownertrust = *p++;
1563       p++;
1564       rec->r.trust.validlist = buf32_to_ulong(p);
1565       break;
1566
1567     case RECTYPE_VALID:
1568       memcpy (rec->r.valid.namehash, p, 20);
1569       p+=20;
1570       rec->r.valid.validity = *p++;
1571       rec->r.valid.next = buf32_to_ulong(p);
1572       p += 4;
1573       rec->r.valid.full_count = *p++;
1574       rec->r.valid.marginal_count = *p++;
1575       break;
1576
1577     default:
1578       log_error ("%s: invalid record type %d at recnum %lu\n",
1579                  db_name, rec->rectype, (ulong)recnum);
1580       err = gpg_error (GPG_ERR_TRUSTDB);
1581       break;
1582     }
1583
1584   return err;
1585 }
1586
1587
1588 /*
1589  * Write the record from the struct REC.
1590  *
1591  * Return: 0 on success or an error code.
1592  */
1593 int
1594 tdbio_write_record( TRUSTREC *rec )
1595 {
1596   byte buf[TRUST_RECORD_LEN];
1597   byte *p;
1598   int rc = 0;
1599   int i;
1600   ulong recnum = rec->recnum;
1601
1602   if (db_fd == -1)
1603     open_db ();
1604
1605   memset (buf, 0, TRUST_RECORD_LEN);
1606   p = buf;
1607   *p++ = rec->rectype; p++;
1608
1609   switch (rec->rectype)
1610     {
1611     case 0:  /* unused record */
1612       break;
1613
1614     case RECTYPE_VER: /* version record */
1615       if (recnum)
1616         BUG ();
1617       memcpy(p-1, GPGEXT_GPG, 3 ); p += 2;
1618       *p++ = rec->r.ver.version;
1619       *p++ = rec->r.ver.marginals;
1620       *p++ = rec->r.ver.completes;
1621       *p++ = rec->r.ver.cert_depth;
1622       *p++ = rec->r.ver.trust_model;
1623       *p++ = rec->r.ver.min_cert_level;
1624       p += 2;
1625       ulongtobuf(p, rec->r.ver.created); p += 4;
1626       ulongtobuf(p, rec->r.ver.nextcheck); p += 4;
1627       p += 4;
1628       p += 4;
1629       ulongtobuf(p, rec->r.ver.firstfree ); p += 4;
1630       p += 4;
1631       ulongtobuf(p, rec->r.ver.trusthashtbl ); p += 4;
1632       break;
1633
1634     case RECTYPE_FREE:
1635       ulongtobuf(p, rec->r.free.next); p += 4;
1636       break;
1637
1638     case RECTYPE_HTBL:
1639       for (i=0; i < ITEMS_PER_HTBL_RECORD; i++)
1640         {
1641           ulongtobuf( p, rec->r.htbl.item[i]); p += 4;
1642         }
1643       break;
1644
1645     case RECTYPE_HLST:
1646       ulongtobuf( p, rec->r.hlst.next); p += 4;
1647       for (i=0; i < ITEMS_PER_HLST_RECORD; i++ )
1648         {
1649           ulongtobuf( p, rec->r.hlst.rnum[i]); p += 4;
1650         }
1651       break;
1652
1653     case RECTYPE_TRUST:
1654       memcpy (p, rec->r.trust.fingerprint, 20); p += 20;
1655       *p++ = rec->r.trust.ownertrust;
1656       *p++ = rec->r.trust.depth;
1657       *p++ = rec->r.trust.min_ownertrust;
1658       p++;
1659       ulongtobuf( p, rec->r.trust.validlist); p += 4;
1660       break;
1661
1662     case RECTYPE_VALID:
1663       memcpy (p, rec->r.valid.namehash, 20); p += 20;
1664       *p++ = rec->r.valid.validity;
1665       ulongtobuf( p, rec->r.valid.next); p += 4;
1666       *p++ = rec->r.valid.full_count;
1667       *p++ = rec->r.valid.marginal_count;
1668       break;
1669
1670     default:
1671       BUG();
1672     }
1673
1674   rc = put_record_into_cache (recnum, buf);
1675   if (rc)
1676     ;
1677   else if (rec->rectype == RECTYPE_TRUST)
1678     rc = update_trusthashtbl (rec);
1679
1680   return rc;
1681 }
1682
1683
1684 /*
1685  * Delete the record at record number RECNUm from the trustdb.
1686  *
1687  * Return: 0 on success or an error code.
1688  */
1689 int
1690 tdbio_delete_record (ulong recnum)
1691 {
1692   TRUSTREC vr, rec;
1693   int rc;
1694
1695   /* Must read the record fist, so we can drop it from the hash tables */
1696   rc = tdbio_read_record (recnum, &rec, 0);
1697   if (rc)
1698     ;
1699   else if (rec.rectype == RECTYPE_TRUST)
1700     {
1701       rc = drop_from_hashtable (get_trusthashrec(),
1702                                 rec.r.trust.fingerprint, 20, rec.recnum);
1703     }
1704
1705   if (rc)
1706     return rc;
1707
1708   /* Now we can chnage it to a free record.  */
1709   rc = tdbio_read_record (0, &vr, RECTYPE_VER);
1710   if (rc)
1711     log_fatal (_("%s: error reading version record: %s\n"),
1712                db_name, gpg_strerror (rc));
1713
1714   rec.recnum = recnum;
1715   rec.rectype = RECTYPE_FREE;
1716   rec.r.free.next = vr.r.ver.firstfree;
1717   vr.r.ver.firstfree = recnum;
1718   rc = tdbio_write_record (&rec);
1719   if (!rc)
1720     rc = tdbio_write_record (&vr);
1721
1722   return rc;
1723 }
1724
1725
1726 /*
1727  * Create a new record and return its record number.
1728  */
1729 ulong
1730 tdbio_new_recnum ()
1731 {
1732   off_t offset;
1733   ulong recnum;
1734   TRUSTREC vr, rec;
1735   int rc;
1736
1737   /* Look for unused records.  */
1738   rc = tdbio_read_record (0, &vr, RECTYPE_VER);
1739   if (rc)
1740     log_fatal( _("%s: error reading version record: %s\n"),
1741                db_name, gpg_strerror (rc));
1742   if (vr.r.ver.firstfree)
1743     {
1744       recnum = vr.r.ver.firstfree;
1745       rc = tdbio_read_record (recnum, &rec, RECTYPE_FREE);
1746       if (rc)
1747         {
1748           log_error (_("%s: error reading free record: %s\n"),
1749                      db_name,  gpg_strerror (rc));
1750           return rc;
1751         }
1752       /* Update dir record.  */
1753       vr.r.ver.firstfree = rec.r.free.next;
1754       rc = tdbio_write_record (&vr);
1755       if (rc)
1756         {
1757           log_error (_("%s: error writing dir record: %s\n"),
1758                      db_name, gpg_strerror (rc));
1759           return rc;
1760         }
1761       /* Zero out the new record.  */
1762       memset (&rec, 0, sizeof rec);
1763       rec.rectype = 0; /* Mark as unused record (actually already done
1764                           my the memset).  */
1765       rec.recnum = recnum;
1766       rc = tdbio_write_record (&rec);
1767       if (rc)
1768         log_fatal (_("%s: failed to zero a record: %s\n"),
1769                    db_name, gpg_strerror (rc));
1770     }
1771   else /* Not found - append a new record.  */
1772     {
1773       offset = lseek (db_fd, 0, SEEK_END);
1774       if (offset == (off_t)(-1))
1775         log_fatal ("trustdb: lseek to end failed: %s\n", strerror (errno));
1776       recnum = offset / TRUST_RECORD_LEN;
1777       log_assert (recnum); /* this is will never be the first record */
1778       /* We must write a record, so that the next call to this
1779        * function returns another recnum.  */
1780       memset (&rec, 0, sizeof rec);
1781       rec.rectype = 0; /* unused record */
1782       rec.recnum = recnum;
1783       rc = 0;
1784       if (lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET) == -1)
1785         {
1786           rc = gpg_error_from_syserror ();
1787           log_error (_("trustdb rec %lu: lseek failed: %s\n"),
1788                      recnum, strerror (errno));
1789         }
1790       else
1791         {
1792           int n;
1793
1794           n = write (db_fd, &rec, TRUST_RECORD_LEN);
1795           if (n != TRUST_RECORD_LEN)
1796             {
1797               rc = gpg_error_from_syserror ();
1798               log_error (_("trustdb rec %lu: write failed (n=%d): %s\n"),
1799                          recnum, n, strerror (errno));
1800             }
1801         }
1802
1803       if (rc)
1804         log_fatal (_("%s: failed to append a record: %s\n"),
1805                    db_name,     gpg_strerror (rc));
1806     }
1807
1808   return recnum ;
1809 }
1810
1811
1812
1813 /* Helper function for tdbio_search_trust_byfpr.  */
1814 static int
1815 cmp_trec_fpr ( const void *fpr, const TRUSTREC *rec )
1816 {
1817   return (rec->rectype == RECTYPE_TRUST
1818           && !memcmp (rec->r.trust.fingerprint, fpr, 20));
1819 }
1820
1821
1822 /*
1823  * Given a 20 byte FINGERPRINT search its trust record and return
1824  * that at REC.
1825  *
1826  * Return: 0 if found, GPG_ERR_NOT_FOUND, or another error code.
1827  */
1828 gpg_error_t
1829 tdbio_search_trust_byfpr (const byte *fingerprint, TRUSTREC *rec)
1830 {
1831   int rc;
1832
1833   /* Locate the trust record using the hash table */
1834   rc = lookup_hashtable (get_trusthashrec(), fingerprint, 20,
1835                          cmp_trec_fpr, fingerprint, rec );
1836   return rc;
1837 }
1838
1839
1840 /*
1841  * Given a primary public key object PK search its trust record and
1842  * return that at REC.
1843  *
1844  * Return: 0 if found, GPG_ERR_NOT_FOUND, or another error code.
1845  */
1846 gpg_error_t
1847 tdbio_search_trust_bypk (PKT_public_key *pk, TRUSTREC *rec)
1848 {
1849   byte fingerprint[MAX_FINGERPRINT_LEN];
1850   size_t fingerlen;
1851
1852   fingerprint_from_pk( pk, fingerprint, &fingerlen );
1853   for (; fingerlen < 20; fingerlen++)
1854     fingerprint[fingerlen] = 0;
1855   return tdbio_search_trust_byfpr (fingerprint, rec);
1856 }
1857
1858
1859 /*
1860  * Terminate the process with a message about a corrupted trustdb.
1861  */
1862 void
1863 tdbio_invalid (void)
1864 {
1865   log_error (_("Error: The trustdb is corrupted.\n"));
1866   how_to_fix_the_trustdb ();
1867   g10_exit (2);
1868 }