* trustdb.h, trustdb.c (is_disabled), gpgv.c (is_disabled): Rename
[gnupg.git] / g10 / tdbio.c
index 62ff267..bc609ad 100644 (file)
 #include "trustdb.h"
 #include "tdbio.h"
 
-#if defined(HAVE_DOSISH_SYSTEM) && !defined(__CYGWIN32__)
+#if defined(HAVE_DOSISH_SYSTEM)
 #define ftruncate chsize
 #endif
 
-#ifdef HAVE_DOSISH_SYSTEM
+#if defined(HAVE_DOSISH_SYSTEM) || defined(__CYGWIN__)
 #define MY_O_BINARY  O_BINARY
 #else
 #define MY_O_BINARY  0
@@ -418,6 +418,29 @@ cleanup(void)
     }
 }
 
+/* Caller must sync */
+int
+tdbio_update_version_record (void)
+{
+  TRUSTREC rec;
+  int rc;
+
+  memset( &rec, 0, sizeof rec );
+
+  rc=tdbio_read_record( 0, &rec, RECTYPE_VER);
+  if(rc==0)
+    {
+      rec.r.ver.created     = make_timestamp();
+      rec.r.ver.marginals   = opt.marginals_needed;
+      rec.r.ver.completes   = opt.completes_needed;
+      rec.r.ver.cert_depth  = opt.max_cert_depth;
+      rec.r.ver.trust_model = opt.trust_model;
+      rc=tdbio_write_record(&rec);
+    }
+
+  return rc;
+}
+
 static int
 create_version_record (void)
 {
@@ -425,11 +448,15 @@ create_version_record (void)
   int rc;
   
   memset( &rec, 0, sizeof rec );
-  rec.r.ver.version = 3;
-  rec.r.ver.created = make_timestamp();
-  rec.r.ver.marginals =  opt.marginals_needed;
-  rec.r.ver.completes =  opt.completes_needed;
-  rec.r.ver.cert_depth = opt.max_cert_depth;
+  rec.r.ver.version     = 3;
+  rec.r.ver.created     = make_timestamp();
+  rec.r.ver.marginals   = opt.marginals_needed;
+  rec.r.ver.completes   = opt.completes_needed;
+  rec.r.ver.cert_depth  = opt.max_cert_depth;
+  if(opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC)
+    rec.r.ver.trust_model = opt.trust_model;
+  else
+    rec.r.ver.trust_model = TM_PGP;
   rec.rectype = RECTYPE_VER;
   rec.recnum = 0;
   rc = tdbio_write_record( &rec );
@@ -450,9 +477,18 @@ tdbio_set_dbname( const char *new_dbname, int create )
        atexit( cleanup );
        initialized = 1;
     }
-    fname = new_dbname? m_strdup( new_dbname )
-                     : make_filename(opt.homedir,
-                                     "trustdb" EXTSEP_S "gpg", NULL );
+
+    if(new_dbname==NULL)
+      fname=make_filename(opt.homedir,"trustdb" EXTSEP_S "gpg", NULL);
+    else if (*new_dbname != DIRSEP_C )
+      {
+       if (strchr(new_dbname, DIRSEP_C) )
+         fname = make_filename (new_dbname, NULL);
+       else
+         fname = make_filename (opt.homedir, new_dbname, NULL);
+      }
+    else
+      fname = m_strdup (new_dbname);
 
     if( access( fname, R_OK ) ) {
        if( errno != ENOENT ) {
@@ -615,38 +651,39 @@ create_hashtable( TRUSTREC *vr, int type )
 int
 tdbio_db_matches_options()
 {
-    static int yes_no = -1;
+  static int yes_no = -1;
 
-    if( yes_no == -1 ) {
-       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) );
+  if( yes_no == -1 )
+    {
+      TRUSTREC vr;
+      int rc;
 
-       if( !vr.r.ver.marginals && !vr.r.ver.completes
-                               && !vr.r.ver.cert_depth )
-       {   /* special hack for trustdbs created by old versions of GnuPG */
-           vr.r.ver.marginals =  opt.marginals_needed;
-           vr.r.ver.completes =  opt.completes_needed;
-           vr.r.ver.cert_depth = opt.max_cert_depth;
-           rc = tdbio_write_record( &vr );
-           if( !rc && !in_transaction )
-               rc = tdbio_sync();
-           if( rc )
-               log_error( _("%s: error writing version record: %s\n"),
-                                               db_name, g10_errstr(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) );
 
-       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;
+      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;
     }
-    return yes_no;
+
+  return yes_no;
 }
 
+byte
+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) );
+  return vr.r.ver.trust_model;
+}
 
 /****************
  * Return the nextstamp value.
@@ -1060,12 +1097,13 @@ tdbio_dump_record( TRUSTREC *rec, FILE *fp  )
       case 0: fprintf(fp, "blank\n");
        break;
       case RECTYPE_VER: fprintf(fp,
-           "version, td=%lu, f=%lu, m/c/d=%d/%d/%d nc=%lu (%s)\n",
+           "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)
                                  );
@@ -1158,7 +1196,8 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
        rec->r.ver.marginals = *p++;
        rec->r.ver.completes = *p++;
        rec->r.ver.cert_depth = *p++;
-       p += 4; /* lock flags */
+       rec->r.ver.trust_model = *p++;
+       p += 3;
        rec->r.ver.created  = buftoulong(p); p += 4;
        rec->r.ver.nextcheck = buftoulong(p); p += 4;
        p += 4;
@@ -1195,13 +1234,16 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
        memcpy( rec->r.trust.fingerprint, p, 20); p+=20;
         rec->r.trust.ownertrust = *p++;
         rec->r.trust.depth = *p++;
-        p += 2;
-       rec->r.trust.validlist  = buftoulong(p); p += 4;
+        rec->r.trust.min_ownertrust = *p++;
+        p++;
+       rec->r.trust.validlist = buftoulong(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.full_count = *p++;
+       rec->r.valid.marginal_count = *p++;
        break;
       default:
        log_error( "%s: invalid record type %d at recnum %lu\n",
@@ -1241,7 +1283,8 @@ tdbio_write_record( TRUSTREC *rec )
        *p++ = rec->r.ver.marginals;
        *p++ = rec->r.ver.completes;
        *p++ = rec->r.ver.cert_depth;
-       p += 4; /* skip lock flags */
+       *p++ = rec->r.ver.trust_model;
+       p += 3;
        ulongtobuf(p, rec->r.ver.created); p += 4;
        ulongtobuf(p, rec->r.ver.nextcheck); p += 4;
        p += 4;
@@ -1273,7 +1316,8 @@ tdbio_write_record( TRUSTREC *rec )
        memcpy( p, rec->r.trust.fingerprint, 20); p += 20;
        *p++ = rec->r.trust.ownertrust;
        *p++ = rec->r.trust.depth;
-        p += 2;
+       *p++ = rec->r.trust.min_ownertrust;
+        p++;
        ulongtobuf( p, rec->r.trust.validlist); p += 4;
        break;
 
@@ -1281,6 +1325,8 @@ tdbio_write_record( TRUSTREC *rec )
        memcpy( p, rec->r.valid.namehash, 20); p += 20;
        *p++ = rec->r.valid.validity;
        ulongtobuf( p, rec->r.valid.next); p += 4;
+       *p++ = rec->r.valid.full_count;
+       *p++ = rec->r.valid.marginal_count;
        break;
 
       default: