gpg: Change --print-pka-records into an option.
[gnupg.git] / g10 / tdbio.c
index 11e3bb3..69438b4 100644 (file)
@@ -1,11 +1,11 @@
-/* tdbio.c - trust databse I/O operations
- * Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+/* tdbio.c - trust database I/O operations
+ * Copyright (C) 1998-2002, 2012 Free Software Foundation, Inc.
  *
  * This file is part of GnuPG.
  *
  * GnuPG is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * GnuPG is distributed in the hope that it will be useful,
@@ -14,9 +14,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
- * USA.
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <config.h>
@@ -31,7 +29,7 @@
 #include <unistd.h>
 
 #include "gpg.h"
-#include "errors.h"
+#include "status.h"
 #include "iobuf.h"
 #include "util.h"
 #include "options.h"
@@ -40,7 +38,7 @@
 #include "trustdb.h"
 #include "tdbio.h"
 
-#if defined(HAVE_DOSISH_SYSTEM)
+#if defined(HAVE_DOSISH_SYSTEM) && !defined(ftruncate)
 #define ftruncate chsize
 #endif
 
 #define MY_O_BINARY  0
 #endif
 
+/* We use ERRNO despite that the cegcc provided open/read/write
+   functions don't set ERRNO - at least show that ERRNO does not make
+   sense.  */
+#ifdef HAVE_W32CE_SYSTEM
+#undef strerror
+#define strerror(a) ("[errno not available]")
+#endif
 
 /****************
  * Yes, this is a very simple implementation. We should really
@@ -88,7 +93,7 @@ struct cmp_xdir_struct {
 
 
 static char *db_name;
-static DOTLOCK lockhandle;
+static dotlock_t lockhandle;
 static int is_locked;
 static int  db_fd = -1;
 static int in_transaction;
@@ -126,14 +131,14 @@ write_cache_item( CACHE_CTRL r )
     int n;
 
     if( lseek( db_fd, r->recno * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
-        err = gpg_error_from_errno (errno);
+        err = gpg_error_from_syserror ();
        log_error(_("trustdb rec %lu: lseek failed: %s\n"),
                                            r->recno, strerror(errno) );
        return err;
     }
     n = write( db_fd, r->data, TRUST_RECORD_LEN);
     if( n != TRUST_RECORD_LEN ) {
-        err = gpg_error_from_errno (errno);
+        err = gpg_error_from_syserror ();
        log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
                                            r->recno, n, strerror(errno) );
        return err;
@@ -245,14 +250,14 @@ put_record_into_cache( ulong recno, const char *data )
            return 0;
        }
        log_info(_("trustdb transaction too large\n"));
-       return G10ERR_RESOURCE_LIMIT;
+       return GPG_ERR_RESOURCE_LIMIT;
     }
     if( dirty_count ) {
        int n = dirty_count / 5; /* discard some dirty entries */
        if( !n )
            n = 1;
        if( !is_locked ) {
-           if( make_dotlock( lockhandle, -1 ) )
+           if( dotlock_take( lockhandle, -1 ) )
                log_fatal("can't acquire lock - giving up\n");
            else
                is_locked = 1;
@@ -271,7 +276,7 @@ put_record_into_cache( ulong recno, const char *data )
            }
        }
        if( !opt.lock_once ) {
-           if( !release_dotlock( lockhandle ) )
+           if( !dotlock_release( lockhandle ) )
                is_locked = 0;
        }
        assert( unused );
@@ -313,7 +318,7 @@ tdbio_sync()
        return 0;
 
     if( !is_locked ) {
-       if( make_dotlock( lockhandle, -1 ) )
+       if( dotlock_take( lockhandle, -1 ) )
            log_fatal("can't acquire lock - giving up\n");
        else
            is_locked = 1;
@@ -328,7 +333,7 @@ tdbio_sync()
     }
     cache_is_dirty = 0;
     if( did_lock && !opt.lock_once ) {
-       if( !release_dotlock( lockhandle ) )
+       if( !dotlock_release (lockhandle) )
            is_locked = 0;
     }
 
@@ -368,7 +373,7 @@ tdbio_end_transaction()
     if( !in_transaction )
        log_bug("tdbio: no active transaction\n");
     if( !is_locked ) {
-       if( make_dotlock( lockhandle, -1 ) )
+       if( dotlock_take( lockhandle, -1 ) )
            log_fatal("can't acquire lock - giving up\n");
        else
            is_locked = 1;
@@ -378,7 +383,7 @@ tdbio_end_transaction()
     rc = tdbio_sync();
     unblock_all_signals();
     if( !opt.lock_once ) {
-       if( !release_dotlock( lockhandle ) )
+       if( !dotlock_release (lockhandle) )
            is_locked = 0;
     }
     return rc;
@@ -418,7 +423,7 @@ static void
 cleanup(void)
 {
     if( is_locked ) {
-       if( !release_dotlock(lockhandle) )
+       if( !dotlock_release (lockhandle) )
            is_locked = 0;
     }
 }
@@ -440,6 +445,7 @@ tdbio_update_version_record (void)
       rec.r.ver.completes   = opt.completes_needed;
       rec.r.ver.cert_depth  = opt.max_cert_depth;
       rec.r.ver.trust_model = opt.trust_model;
+      rec.r.ver.min_cert_level = opt.min_cert_level;
       rc=tdbio_write_record(&rec);
     }
 
@@ -451,7 +457,7 @@ create_version_record (void)
 {
   TRUSTREC rec;
   int rc;
-  
+
   memset( &rec, 0, sizeof rec );
   rec.r.ver.version     = 3;
   rec.r.ver.created     = make_timestamp();
@@ -462,6 +468,7 @@ create_version_record (void)
     rec.r.ver.trust_model = opt.trust_model;
   else
     rec.r.ver.trust_model = TM_PGP;
+  rec.r.ver.min_cert_level = opt.min_cert_level;
   rec.rectype = RECTYPE_VER;
   rec.recnum = 0;
   rc = tdbio_write_record( &rec );
@@ -473,7 +480,7 @@ create_version_record (void)
 
 
 int
-tdbio_set_dbname( const char *new_dbname, int create )
+tdbio_set_dbname( const char *new_dbname, int create, int *r_nofile)
 {
     char *fname;
     static int initialized = 0;
@@ -483,8 +490,10 @@ tdbio_set_dbname( const char *new_dbname, int create )
        initialized = 1;
     }
 
+    *r_nofile = 0;
+
     if(new_dbname==NULL)
-      fname=make_filename(opt.homedir,"trustdb" EXTSEP_S "gpg", NULL);
+      fname=make_filename(opt.homedir,"trustdb" EXTSEP_S GPGEXT_GPG, NULL);
     else if (*new_dbname != DIRSEP_C )
       {
        if (strchr(new_dbname, DIRSEP_C) )
@@ -496,62 +505,85 @@ tdbio_set_dbname( const char *new_dbname, int create )
       fname = xstrdup (new_dbname);
 
     if( access( fname, R_OK ) ) {
+#ifdef HAVE_W32CE_SYSTEM
+      /* We know how the cegcc implementation of access works ;-). */
+      if (GetLastError () == ERROR_FILE_NOT_FOUND)
+        gpg_err_set_errno (ENOENT);
+      else
+        gpg_err_set_errno (EIO);
+#endif /*HAVE_W32CE_SYSTEM*/
        if( errno != ENOENT ) {
-           log_error( _("can't access `%s': %s\n"), fname, strerror(errno) );
+           log_error( _("can't access '%s': %s\n"), fname, strerror(errno) );
            xfree(fname);
-           return G10ERR_TRUSTDB;
+           return GPG_ERR_TRUSTDB;
        }
-       if( create ) {
+       if (!create)
+          *r_nofile = 1;
+        else {
            FILE *fp;
            TRUSTREC rec;
            int rc;
            char *p = strrchr( fname, DIRSEP_C );
            mode_t oldmask;
-
-           assert(p);
+            int save_slash;
+
+#if HAVE_W32_SYSTEM
+            {
+              /* Windows may either have a slash or a backslash.  Take
+                 care of it.  */
+              char *pp = strrchr (fname, '/');
+              if (!p || pp > p)
+                p = pp;
+            }
+#endif /*HAVE_W32_SYSTEM*/
+           assert (p);
+            save_slash = *p;
            *p = 0;
            if( access( fname, F_OK ) ) {
                try_make_homedir( fname );
-               log_fatal( _("%s: directory does not exist!\n"), fname );
+                if (access (fname, F_OK ))
+                  log_fatal (_("%s: directory does not exist!\n"), fname);
            }
-           *p = DIRSEP_C;
+           *p = save_slash;
 
            xfree(db_name);
            db_name = fname;
 #ifdef __riscos__
            if( !lockhandle )
-               lockhandle = create_dotlock( db_name );
+              lockhandle = dotlock_create (db_name, 0);
            if( !lockhandle )
-               log_fatal( _("can't create lock for `%s'\n"), db_name );
-            if( make_dotlock( lockhandle, -1 ) )
-                log_fatal( _("can't lock `%s'\n"), db_name );
+               log_fatal( _("can't create lock for '%s'\n"), db_name );
+            if( dotlock_make (lockhandle, -1) )
+                log_fatal( _("can't lock '%s'\n"), db_name );
 #endif /* __riscos__ */
            oldmask=umask(077);
             if (is_secured_filename (fname)) {
                 fp = NULL;
-                errno = EPERM;
+                gpg_err_set_errno (EPERM);
             }
             else
                 fp =fopen( fname, "wb" );
            umask(oldmask);
            if( !fp )
-               log_fatal( _("can't create `%s': %s\n"), fname, strerror(errno) );
+               log_fatal (_("can't create '%s': %s\n"),
+                           fname, strerror (errno));
            fclose(fp);
            db_fd = open( db_name, O_RDWR | MY_O_BINARY );
            if( db_fd == -1 )
-               log_fatal( _("can't open `%s': %s\n"), db_name, strerror(errno) );
+               log_fatal (_("can't open '%s': %s\n"),
+                           db_name, strerror (errno));
 
 #ifndef __riscos__
            if( !lockhandle )
-               lockhandle = create_dotlock( db_name );
+              lockhandle = dotlock_create (db_name, 0);
            if( !lockhandle )
-               log_fatal( _("can't create lock for `%s'\n"), db_name );
+               log_fatal( _("can't create lock for '%s'\n"), db_name );
 #endif /* !__riscos__ */
 
             rc = create_version_record ();
            if( rc )
                log_fatal( _("%s: failed to create version record: %s"),
-                                                  fname, g10_errstr(rc));
+                                                  fname, gpg_strerror (rc));
            /* and read again to check that we are okay */
            if( tdbio_read_record( 0, &rec, RECTYPE_VER ) )
                log_fatal( _("%s: invalid trustdb created\n"), db_name );
@@ -584,25 +616,44 @@ open_db()
   assert( db_fd == -1 );
 
   if (!lockhandle )
-    lockhandle = create_dotlock( db_name );
+    lockhandle = dotlock_create (db_name, 0);
   if (!lockhandle )
-    log_fatal( _("can't create lock for `%s'\n"), db_name );
+    log_fatal( _("can't create lock for '%s'\n"), db_name );
 #ifdef __riscos__
-  if (make_dotlock( lockhandle, -1 ) )
-    log_fatal( _("can't lock `%s'\n"), db_name );
+  if (dotlock_take (lockhandle, -1) )
+    log_fatal( _("can't lock '%s'\n"), db_name );
 #endif /* __riscos__ */
+#ifdef HAVE_W32CE_SYSTEM
+  {
+    DWORD prevrc = 0;
+    wchar_t *wname = utf8_to_wchar (db_name);
+    if (wname)
+      {
+        db_fd = (int)CreateFile (wname, GENERIC_READ|GENERIC_WRITE,
+                                 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
+                                 OPEN_EXISTING, 0, NULL);
+        xfree (wname);
+      }
+    if (db_fd == -1)
+      log_fatal ("can't open '%s': %d, %d\n", db_name,
+                 (int)prevrc, (int)GetLastError ());
+  }
+#else /*!HAVE_W32CE_SYSTEM*/
   db_fd = open (db_name, O_RDWR | MY_O_BINARY );
   if (db_fd == -1 && (errno == EACCES
 #ifdef EROFS
-                      || errno == EROFS)
+                      || errno == EROFS
 #endif
+                      )
       ) {
+      /* Take care of read-only trustdbs.  */
       db_fd = open (db_name, O_RDONLY | MY_O_BINARY );
-      if (db_fd != -1)
-          log_info (_("NOTE: trustdb not writable\n"));
+      if (db_fd != -1 && !opt.quiet)
+          log_info (_("Note: trustdb not writable\n"));
   }
   if ( db_fd == -1 )
-    log_fatal( _("can't open `%s': %s\n"), db_name, strerror(errno) );
+    log_fatal( _("can't open '%s': %s\n"), db_name, strerror(errno) );
+#endif /*!HAVE_W32CE_SYSTEM*/
   register_secured_file (db_name);
 
   /* Read the version record. */
@@ -640,7 +691,7 @@ create_hashtable( TRUSTREC *vr, int type )
         rc = tdbio_write_record( &rec );
         if( rc )
             log_fatal( _("%s: failed to create hashtable: %s\n"),
-                                       db_name, g10_errstr(rc));
+                                       db_name, gpg_strerror (rc));
     }
     /* update the version record */
     rc = tdbio_write_record( vr );
@@ -648,7 +699,7 @@ create_hashtable( TRUSTREC *vr, int type )
        rc = tdbio_sync();
     if( rc )
        log_fatal( _("%s: error updating version record: %s\n"),
-                                                 db_name, g10_errstr(rc));
+                                                 db_name, gpg_strerror (rc));
 }
 
 
@@ -665,12 +716,13 @@ tdbio_db_matches_options()
       rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
       if( rc )
        log_fatal( _("%s: error reading version record: %s\n"),
-                  db_name, g10_errstr(rc) );
+                  db_name, gpg_strerror (rc) );
 
       yes_no = vr.r.ver.marginals == opt.marginals_needed
        && vr.r.ver.completes == opt.completes_needed
        && vr.r.ver.cert_depth == opt.max_cert_depth
-       && vr.r.ver.trust_model == opt.trust_model;
+       && vr.r.ver.trust_model == opt.trust_model
+       && vr.r.ver.min_cert_level == opt.min_cert_level;
     }
 
   return yes_no;
@@ -681,11 +733,11 @@ tdbio_read_model(void)
 {
   TRUSTREC vr;
   int rc;
+
   rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
   if( rc )
     log_fatal( _("%s: error reading version record: %s\n"),
-              db_name, g10_errstr(rc) );
+              db_name, gpg_strerror (rc) );
   return vr.r.ver.trust_model;
 }
 
@@ -701,7 +753,7 @@ tdbio_read_nextcheck ()
     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
     if( rc )
        log_fatal( _("%s: error reading version record: %s\n"),
-                                                   db_name, g10_errstr(rc) );
+                                                   db_name, gpg_strerror (rc));
     return vr.r.ver.nextcheck;
 }
 
@@ -715,7 +767,7 @@ tdbio_write_nextcheck (ulong stamp)
     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
     if( rc )
        log_fatal( _("%s: error reading version record: %s\n"),
-                                      db_name, g10_errstr(rc) );
+                                      db_name, gpg_strerror (rc) );
 
     if (vr.r.ver.nextcheck == stamp)
       return 0;
@@ -724,7 +776,7 @@ tdbio_write_nextcheck (ulong stamp)
     rc = tdbio_write_record( &vr );
     if( rc )
        log_fatal( _("%s: error writing version record: %s\n"),
-                                      db_name, g10_errstr(rc) );
+                                      db_name, gpg_strerror (rc) );
     return 1;
 }
 
@@ -745,7 +797,7 @@ get_trusthashrec(void)
        rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
        if( rc )
            log_fatal( _("%s: error reading version record: %s\n"),
-                                           db_name, g10_errstr(rc) );
+                                           db_name, gpg_strerror (rc) );
        if( !vr.r.ver.trusthashtbl )
            create_hashtable( &vr, 0 );
 
@@ -776,7 +828,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
     hashrec += msb / ITEMS_PER_HTBL_RECORD;
     rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
     if( rc ) {
-       log_error("upd_hashtable: read failed: %s\n",   g10_errstr(rc) );
+       log_error("upd_hashtable: read failed: %s\n",   gpg_strerror (rc) );
        return rc;
     }
 
@@ -786,7 +838,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
        rc = tdbio_write_record( &rec );
        if( rc ) {
            log_error("upd_hashtable: write htbl failed: %s\n",
-                                                           g10_errstr(rc) );
+                                                           gpg_strerror (rc) );
            return rc;
        }
     }
@@ -795,7 +847,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
        rc = tdbio_read_record( item, &rec, 0 );
        if( rc ) {
            log_error( "upd_hashtable: read item failed: %s\n",
-                                                           g10_errstr(rc) );
+                                                           gpg_strerror (rc) );
            return rc;
        }
 
@@ -804,7 +856,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
            level++;
            if( level >= keylen ) {
                log_error( "hashtable has invalid indirections.\n");
-               return G10ERR_TRUSTDB;
+               return GPG_ERR_TRUSTDB;
            }
            goto next_level;
        }
@@ -820,8 +872,8 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
                    rc = tdbio_read_record( rec.r.hlst.next,
                                                       &rec, RECTYPE_HLST);
                    if( rc ) {
-                       log_error"upd_hashtable: read hlst failed: %s\n",
-                                                            g10_errstr(rc) );
+                       log_error ("upd_hashtable: read hlst failed: %s\n",
+                                   gpg_strerror (rc) );
                        return rc;
                    }
                }
@@ -835,8 +887,8 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
                        rec.r.hlst.rnum[i] = newrecnum;
                        rc = tdbio_write_record( &rec );
                        if( rc )
-                           log_error"upd_hashtable: write hlst failed: %s\n",
-                                                             g10_errstr(rc) );
+                           log_error ("upd_hashtable: write hlst failed: %s\n",
+                                       gpg_strerror (rc));
                        return rc; /* done */
                    }
                }
@@ -844,8 +896,8 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
                    rc = tdbio_read_record( rec.r.hlst.next,
                                                      &rec, RECTYPE_HLST );
                    if( rc ) {
-                       log_error"upd_hashtable: read hlst failed: %s\n",
-                                                            g10_errstr(rc) );
+                       log_error ("upd_hashtable: read hlst failed: %s\n",
+                                   gpg_strerror (rc));
                        return rc;
                    }
                }
@@ -854,7 +906,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
                    rc = tdbio_write_record( &rec );
                    if( rc ) {
                        log_error( "upd_hashtable: write hlst failed: %s\n",
-                                                         g10_errstr(rc) );
+                                                         gpg_strerror (rc) );
                        return rc;
                    }
                    memset( &rec, 0, sizeof rec );
@@ -864,7 +916,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
                    rc = tdbio_write_record( &rec );
                    if( rc )
                        log_error( "upd_hashtable: write ext hlst failed: %s\n",
-                                                         g10_errstr(rc) );
+                                                         gpg_strerror (rc) );
                    return rc; /* done */
                }
            } /* end loop over hlst slots */
@@ -882,22 +934,22 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
            rc = tdbio_write_record( &rec );
            if( rc ) {
                log_error( "upd_hashtable: write new hlst failed: %s\n",
-                                                 g10_errstr(rc) );
+                                                 gpg_strerror (rc) );
                return rc;
            }
            /* update the hashtable record */
            lastrec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = rec.recnum;
            rc = tdbio_write_record( &lastrec );
            if( rc )
-               log_error"upd_hashtable: update htbl failed: %s\n",
-                                                            g10_errstr(rc) );
+               log_error ("upd_hashtable: update htbl failed: %s\n",
+                           gpg_strerror (rc));
            return rc; /* ready */
        }
        else {
            log_error( "hashtbl %lu: %lu/%d points to an invalid record %lu\n",
                       table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
            list_trustdb(NULL);
-           return G10ERR_TRUSTDB;
+           return GPG_ERR_TRUSTDB;
        }
     }
 
@@ -925,7 +977,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
     rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
     if( rc ) {
        log_error("drop_from_hashtable: read failed: %s\n",
-                                                       g10_errstr(rc) );
+                                                       gpg_strerror (rc) );
        return rc;
     }
 
@@ -938,14 +990,14 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
        rc = tdbio_write_record( &rec );
        if( rc )
            log_error("drop_from_hashtable: write htbl failed: %s\n",
-                                                           g10_errstr(rc) );
+                                                           gpg_strerror (rc) );
        return rc;
     }
 
     rc = tdbio_read_record( item, &rec, 0 );
     if( rc ) {
        log_error( "drop_from_hashtable: read item failed: %s\n",
-                                                       g10_errstr(rc) );
+                                                       gpg_strerror (rc) );
        return rc;
     }
 
@@ -954,7 +1006,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
        level++;
        if( level >= keylen ) {
            log_error( "hashtable has invalid indirections.\n");
-           return G10ERR_TRUSTDB;
+           return GPG_ERR_TRUSTDB;
        }
        goto next_level;
     }
@@ -966,8 +1018,8 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
                    rec.r.hlst.rnum[i] = 0; /* drop */
                    rc = tdbio_write_record( &rec );
                    if( rc )
-                       log_error("drop_from_hashtable: write htbl failed: %s\n",
-                                                                       g10_errstr(rc) );
+                      log_error("drop_from_hashtable: write htbl failed: %s\n",
+                                gpg_strerror (rc));
                    return rc;
                }
            }
@@ -976,7 +1028,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
                                                   &rec, RECTYPE_HLST);
                if( rc ) {
                    log_error( "drop_from_hashtable: read hlst failed: %s\n",
-                                                        g10_errstr(rc) );
+                                                        gpg_strerror (rc) );
                    return rc;
                }
            }
@@ -987,7 +1039,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
 
     log_error( "hashtbl %lu: %lu/%d points to wrong record %lu\n",
                    table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
-    return G10ERR_TRUSTDB;
+    return GPG_ERR_TRUSTDB;
 }
 
 
@@ -999,8 +1051,8 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
  */
 static int
 lookup_hashtable( ulong table, const byte *key, size_t keylen,
-                 int (*cmpfnc)(void*, const TRUSTREC *), void *cmpdata,
-                                               TRUSTREC *rec )
+                 int (*cmpfnc)(const void*, const TRUSTREC *),
+                  const void *cmpdata, TRUSTREC *rec )
 {
     int rc;
     ulong hashrec, item;
@@ -1013,7 +1065,7 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
     hashrec += msb / ITEMS_PER_HTBL_RECORD;
     rc = tdbio_read_record( hashrec, rec, RECTYPE_HTBL );
     if( rc ) {
-       log_error("lookup_hashtable failed: %s\n", g10_errstr(rc) );
+       log_error("lookup_hashtable failed: %s\n", gpg_strerror (rc) );
        return rc;
     }
 
@@ -1023,7 +1075,7 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
 
     rc = tdbio_read_record( item, rec, 0 );
     if( rc ) {
-       log_error( "hashtable read failed: %s\n", g10_errstr(rc) );
+       log_error( "hashtable read failed: %s\n", gpg_strerror (rc) );
        return rc;
     }
     if( rec->rectype == RECTYPE_HTBL ) {
@@ -1031,7 +1083,7 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
        level++;
        if( level >= keylen ) {
            log_error("hashtable has invalid indirections\n");
-           return G10ERR_TRUSTDB;
+           return GPG_ERR_TRUSTDB;
        }
        goto next_level;
     }
@@ -1045,8 +1097,8 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
 
                    rc = tdbio_read_record( rec->r.hlst.rnum[i], &tmp, 0 );
                    if( rc ) {
-                       log_error"lookup_hashtable: read item failed: %s\n",
-                                                             g10_errstr(rc) );
+                       log_error ("lookup_hashtable: read item failed: %s\n",
+                                   gpg_strerror (rc));
                        return rc;
                    }
                    if( (*cmpfnc)( cmpdata, &tmp ) ) {
@@ -1058,8 +1110,8 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
            if( rec->r.hlst.next ) {
                rc = tdbio_read_record( rec->r.hlst.next, rec, RECTYPE_HLST );
                if( rc ) {
-                   log_error"lookup_hashtable: read hlst failed: %s\n",
-                                                        g10_errstr(rc) );
+                   log_error ("lookup_hashtable: read hlst failed: %s\n",
+                               gpg_strerror (rc) );
                    return rc;
                }
            }
@@ -1089,58 +1141,62 @@ update_trusthashtbl( TRUSTREC *tr )
 
 
 void
-tdbio_dump_record( TRUSTREC *rec, FILE *fp  )
+tdbio_dump_record (TRUSTREC *rec, estream_t fp)
 {
     int i;
     ulong rnum = rec->recnum;
 
-    fprintf(fp, "rec %5lu, ", rnum );
+    es_fprintf ( fp, "rec %5lu, ", rnum );
 
     switch( rec->rectype ) {
-      case 0: fprintf(fp, "blank\n");
+      case 0:
+        es_fprintf (fp, "blank\n");
        break;
-      case RECTYPE_VER: fprintf(fp,
-           "version, td=%lu, f=%lu, m/c/d=%d/%d/%d tm=%d nc=%lu (%s)\n",
-                                   rec->r.ver.trusthashtbl,
-                                  rec->r.ver.firstfree,
-                                  rec->r.ver.marginals,
-                                  rec->r.ver.completes,
-                                  rec->r.ver.cert_depth,
-                                  rec->r.ver.trust_model,
-                                   rec->r.ver.nextcheck,
-                                  strtimestamp(rec->r.ver.nextcheck)
-                                 );
+      case RECTYPE_VER:
+        es_fprintf (fp,
+         "version, td=%lu, f=%lu, m/c/d=%d/%d/%d tm=%d mcl=%d nc=%lu (%s)\n",
+                rec->r.ver.trusthashtbl,
+                rec->r.ver.firstfree,
+                rec->r.ver.marginals,
+                rec->r.ver.completes,
+                rec->r.ver.cert_depth,
+                rec->r.ver.trust_model,
+                rec->r.ver.min_cert_level,
+                rec->r.ver.nextcheck,
+                strtimestamp(rec->r.ver.nextcheck)
+                );
        break;
-      case RECTYPE_FREE: fprintf(fp, "free, next=%lu\n", rec->r.free.next );
+      case RECTYPE_FREE:
+        es_fprintf (fp, "free, next=%lu\n", rec->r.free.next );
        break;
       case RECTYPE_HTBL:
-       fprintf(fp, "htbl,");
+       es_fprintf (fp, "htbl,");
        for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ )
-           fprintf(fp, " %lu", rec->r.htbl.item[i] );
-       putc('\n', fp);
+          es_fprintf (fp, " %lu", rec->r.htbl.item[i] );
+       es_putc ('\n', fp);
        break;
       case RECTYPE_HLST:
-       fprintf(fp, "hlst, next=%lu,", rec->r.hlst.next );
+       es_fprintf (fp, "hlst, next=%lu,", rec->r.hlst.next );
        for(i=0; i < ITEMS_PER_HLST_RECORD; i++ )
-           fprintf(fp, " %lu", rec->r.hlst.rnum[i] );
-       putc('\n', fp);
+          es_fprintf (fp, " %lu", rec->r.hlst.rnum[i] );
+       es_putc ('\n', fp);
        break;
       case RECTYPE_TRUST:
-       fprintf(fp, "trust ");
+       es_fprintf (fp, "trust ");
        for(i=0; i < 20; i++ )
-           fprintf(fp, "%02X", rec->r.trust.fingerprint[i] );
-        fprintf (fp, ", ot=%d, d=%d, vl=%lu\n", rec->r.trust.ownertrust,
-                 rec->r.trust.depth, rec->r.trust.validlist);
+           es_fprintf (fp, "%02X", rec->r.trust.fingerprint[i] );
+        es_fprintf (fp, ", ot=%d, d=%d, vl=%lu\n", rec->r.trust.ownertrust,
+                    rec->r.trust.depth, rec->r.trust.validlist);
        break;
       case RECTYPE_VALID:
-       fprintf(fp, "valid ");
+       es_fprintf (fp, "valid ");
        for(i=0; i < 20; i++ )
-           fprintf(fp, "%02X", rec->r.valid.namehash[i] );
-        fprintf (fp, ", v=%d, next=%lu\n", rec->r.valid.validity,
-                 rec->r.valid.next);
+          es_fprintf(fp, "%02X", rec->r.valid.namehash[i] );
+        es_fprintf (fp, ", v=%d, next=%lu\n", rec->r.valid.validity,
+                    rec->r.valid.next);
        break;
       default:
-       fprintf(fp, "unknown type %d\n", rec->rectype );
+       es_fprintf (fp, "unknown type %d\n", rec->rectype );
        break;
     }
 }
@@ -1162,7 +1218,7 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
     buf = get_record_from_cache( recnum );
     if( !buf ) {
        if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
-            err = gpg_error_from_errno (errno);
+            err = gpg_error_from_syserror ();
            log_error(_("trustdb: lseek failed: %s\n"), strerror(errno) );
            return err;
        }
@@ -1171,7 +1227,7 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
            return -1; /* eof */
        }
        else if( n != TRUST_RECORD_LEN ) {
-            err = gpg_error_from_errno (errno);
+            err = gpg_error_from_syserror ();
            log_error(_("trustdb: read failed (n=%d): %s\n"), n,
                                                        strerror(errno) );
            return err;
@@ -1192,7 +1248,7 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
       case 0:  /* unused (free) record */
        break;
       case RECTYPE_VER: /* version record */
-       if( memcmp(buf+1, "gpg", 3 ) ) {
+       if( memcmp(buf+1, GPGEXT_GPG, 3 ) ) {
            log_error( _("%s: not a trustdb file\n"), db_name );
            err = gpg_error (GPG_ERR_TRUSTDB);
        }
@@ -1202,14 +1258,15 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
        rec->r.ver.completes = *p++;
        rec->r.ver.cert_depth = *p++;
        rec->r.ver.trust_model = *p++;
-       p += 3;
-       rec->r.ver.created  = buftoulong(p); p += 4;
-       rec->r.ver.nextcheck = buftoulong(p); p += 4;
+       rec->r.ver.min_cert_level = *p++;
+       p += 2;
+       rec->r.ver.created  = buf32_to_ulong(p); p += 4;
+       rec->r.ver.nextcheck = buf32_to_ulong(p); p += 4;
        p += 4;
        p += 4;
-       rec->r.ver.firstfree =buftoulong(p); p += 4;
+       rec->r.ver.firstfree =buf32_to_ulong(p); p += 4;
        p += 4;
-       rec->r.ver.trusthashtbl =buftoulong(p); p += 4;
+       rec->r.ver.trusthashtbl =buf32_to_ulong(p); p += 4;
        if( recnum ) {
            log_error( _("%s: version record with recnum %lu\n"), db_name,
                                                             (ulong)recnum );
@@ -1222,17 +1279,17 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
        }
        break;
       case RECTYPE_FREE:
-       rec->r.free.next  = buftoulong(p); p += 4;
+       rec->r.free.next  = buf32_to_ulong(p); p += 4;
        break;
       case RECTYPE_HTBL:
        for(i=0; i < ITEMS_PER_HTBL_RECORD; i++ ) {
-           rec->r.htbl.item[i] = buftoulong(p); p += 4;
+           rec->r.htbl.item[i] = buf32_to_ulong(p); p += 4;
        }
        break;
       case RECTYPE_HLST:
-       rec->r.hlst.next = buftoulong(p); p += 4;
+       rec->r.hlst.next = buf32_to_ulong(p); p += 4;
        for(i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
-           rec->r.hlst.rnum[i] = buftoulong(p); p += 4;
+           rec->r.hlst.rnum[i] = buf32_to_ulong(p); p += 4;
        }
        break;
       case RECTYPE_TRUST:
@@ -1241,12 +1298,12 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
         rec->r.trust.depth = *p++;
         rec->r.trust.min_ownertrust = *p++;
         p++;
-       rec->r.trust.validlist = buftoulong(p); p += 4;
+       rec->r.trust.validlist = buf32_to_ulong(p); p += 4;
        break;
       case RECTYPE_VALID:
        memcpy( rec->r.valid.namehash, p, 20); p+=20;
         rec->r.valid.validity = *p++;
-       rec->r.valid.next = buftoulong(p); p += 4;
+       rec->r.valid.next = buf32_to_ulong(p); p += 4;
        rec->r.valid.full_count = *p++;
        rec->r.valid.marginal_count = *p++;
        break;
@@ -1283,13 +1340,14 @@ tdbio_write_record( TRUSTREC *rec )
       case RECTYPE_VER: /* version record */
        if( recnum )
            BUG();
-       memcpy(p-1, "gpg", 3 ); p += 2;
+       memcpy(p-1, GPGEXT_GPG, 3 ); p += 2;
        *p++ = rec->r.ver.version;
        *p++ = rec->r.ver.marginals;
        *p++ = rec->r.ver.completes;
        *p++ = rec->r.ver.cert_depth;
        *p++ = rec->r.ver.trust_model;
-       p += 3;
+       *p++ = rec->r.ver.min_cert_level;
+       p += 2;
        ulongtobuf(p, rec->r.ver.created); p += 4;
        ulongtobuf(p, rec->r.ver.nextcheck); p += 4;
        p += 4;
@@ -1369,7 +1427,7 @@ tdbio_delete_record( ulong recnum )
     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
     if( rc )
        log_fatal( _("%s: error reading version record: %s\n"),
-                                      db_name, g10_errstr(rc) );
+                                      db_name, gpg_strerror (rc) );
 
     rec.recnum = recnum;
     rec.rectype = RECTYPE_FREE;
@@ -1396,21 +1454,21 @@ tdbio_new_recnum()
     rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
     if( rc )
        log_fatal( _("%s: error reading version record: %s\n"),
-                                            db_name, g10_errstr(rc) );
+                                            db_name, gpg_strerror (rc) );
     if( vr.r.ver.firstfree ) {
        recnum = vr.r.ver.firstfree;
        rc = tdbio_read_record( recnum, &rec, RECTYPE_FREE );
        if( rc ) {
            log_error( _("%s: error reading free record: %s\n"),
-                                                 db_name,  g10_errstr(rc) );
+                                                 db_name,  gpg_strerror (rc) );
            return rc;
        }
        /* update dir record */
        vr.r.ver.firstfree = rec.r.free.next;
        rc = tdbio_write_record( &vr );
        if( rc ) {
-           log_error_("%s: error writing dir record: %s\n"),
-                                                    db_name, g10_errstr(rc) );
+           log_error (_("%s: error writing dir record: %s\n"),
+                       db_name, gpg_strerror (rc));
            return rc;
        }
        /*zero out the new record */
@@ -1420,7 +1478,7 @@ tdbio_new_recnum()
        rc = tdbio_write_record( &rec );
        if( rc )
            log_fatal(_("%s: failed to zero a record: %s\n"),
-                                      db_name, g10_errstr(rc));
+                                      db_name, gpg_strerror (rc));
     }
     else { /* not found, append a new record */
        offset = lseek( db_fd, 0, SEEK_END );
@@ -1435,14 +1493,14 @@ tdbio_new_recnum()
        rec.recnum = recnum;
        rc = 0;
        if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
-            rc = gpg_error_from_errno (errno);
+            rc = gpg_error_from_syserror ();
            log_error(_("trustdb rec %lu: lseek failed: %s\n"),
                                                recnum, strerror(errno) );
        }
        else {
            int n = write( db_fd, &rec, TRUST_RECORD_LEN);
            if( n != TRUST_RECORD_LEN ) {
-                rc = gpg_error_from_errno (errno);
+                rc = gpg_error_from_syserror ();
                log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
                                                 recnum, n, strerror(errno) );
            }
@@ -1450,7 +1508,7 @@ tdbio_new_recnum()
 
        if( rc )
            log_fatal(_("%s: failed to append a record: %s\n"),
-                                   db_name,    g10_errstr(rc));
+                                   db_name,    gpg_strerror (rc));
     }
     return recnum ;
 }
@@ -1458,10 +1516,10 @@ tdbio_new_recnum()
 
 
 static int
-cmp_trec_fpr ( void *fpr, const TRUSTREC *rec )
+cmp_trec_fpr ( const void *fpr, const TRUSTREC *rec )
 {
-    return rec->rectype == RECTYPE_TRUST
-          && !memcmp( rec->r.trust.fingerprint, fpr, 20);
+  return (rec->rectype == RECTYPE_TRUST
+          && !memcmp (rec->r.trust.fingerprint, fpr, 20));
 }
 
 
@@ -1472,7 +1530,7 @@ tdbio_search_trust_byfpr( const byte *fingerprint, TRUSTREC *rec )
 
     /* locate the trust record using the hash table */
     rc = lookup_hashtable( get_trusthashrec(), fingerprint, 20,
-                          cmp_trec_fpr, (void*)fingerprint, rec );
+                          cmp_trec_fpr, fingerprint, rec );
     return rc;
 }
 
@@ -1489,12 +1547,10 @@ tdbio_search_trust_bypk (PKT_public_key *pk, TRUSTREC *rec)
 }
 
 
-
 void
 tdbio_invalid(void)
 {
-    log_error(_(
-       "the trustdb is corrupted; please run \"gpg --fix-trustdb\".\n") );
-    g10_exit(2);
+  log_error (_("Error: The trustdb is corrupted.\n"));
+  how_to_fix_the_trustdb ();
+  g10_exit (2);
 }
-