gpg: Change --print-pka-records into an option.
[gnupg.git] / g10 / tdbio.c
index 09f31aa..69438b4 100644 (file)
@@ -1,5 +1,5 @@
-/* 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.
  *
@@ -38,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
 
@@ -250,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;
@@ -276,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 );
@@ -318,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;
@@ -333,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;
     }
 
@@ -373,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;
@@ -383,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;
@@ -423,7 +423,7 @@ static void
 cleanup(void)
 {
     if( is_locked ) {
-       if( !release_dotlock(lockhandle) )
+       if( !dotlock_release (lockhandle) )
            is_locked = 0;
     }
 }
@@ -445,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);
     }
 
@@ -467,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 );
@@ -478,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;
@@ -488,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) )
@@ -509,11 +513,13 @@ tdbio_set_dbname( const char *new_dbname, int create )
         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;
@@ -544,11 +550,11 @@ tdbio_set_dbname( const char *new_dbname, int create )
            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)) {
@@ -559,23 +565,25 @@ tdbio_set_dbname( const char *new_dbname, int create )
                 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 );
@@ -608,12 +616,12 @@ 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
   {
@@ -627,7 +635,7 @@ open_db()
         xfree (wname);
       }
     if (db_fd == -1)
-      log_fatal ("can't open `%s': %d, %d\n", db_name,
+      log_fatal ("can't open '%s': %d, %d\n", db_name,
                  (int)prevrc, (int)GetLastError ());
   }
 #else /*!HAVE_W32CE_SYSTEM*/
@@ -641,10 +649,10 @@ open_db()
       /* Take care of read-only trustdbs.  */
       db_fd = open (db_name, O_RDONLY | MY_O_BINARY );
       if (db_fd != -1 && !opt.quiet)
-          log_info (_("NOTE: trustdb not writable\n"));
+          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);
 
@@ -683,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 );
@@ -691,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));
 }
 
 
@@ -708,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;
@@ -728,7 +737,7 @@ tdbio_read_model(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) );
   return vr.r.ver.trust_model;
 }
 
@@ -744,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;
 }
 
@@ -758,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;
@@ -767,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;
 }
 
@@ -788,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 );
 
@@ -819,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;
     }
 
@@ -829,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;
        }
     }
@@ -838,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;
        }
 
@@ -847,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;
        }
@@ -863,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;
                    }
                }
@@ -878,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 */
                    }
                }
@@ -887,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;
                    }
                }
@@ -897,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 );
@@ -907,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 */
@@ -925,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;
        }
     }
 
@@ -968,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;
     }
 
@@ -981,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;
     }
 
@@ -997,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;
     }
@@ -1009,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;
                }
            }
@@ -1019,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;
                }
            }
@@ -1030,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;
 }
 
 
@@ -1056,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;
     }
 
@@ -1066,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 ) {
@@ -1074,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;
     }
@@ -1088,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 ) ) {
@@ -1101,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;
                }
            }
@@ -1132,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;
     }
 }
@@ -1235,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);
        }
@@ -1245,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 );
@@ -1265,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:
@@ -1284,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;
@@ -1326,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;
@@ -1412,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;
@@ -1439,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 */
@@ -1463,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 );
@@ -1493,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 ;
 }