* options.h, gpg.c (main), mainproc.c (check_sig_and_print), keyserver.c
[gnupg.git] / g10 / tdbio.c
index 993cb06..fbd569d 100644 (file)
@@ -15,7 +15,8 @@
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
  */
 
 #include <config.h>
@@ -187,7 +188,7 @@ put_record_into_cache( ulong recno, const char *data )
     }
     /* see whether we reached the limit */
     if( cache_entries < MAX_CACHE_ENTRIES_SOFT ) { /* no */
-       r = m_alloc( sizeof *r );
+       r = xmalloc( sizeof *r );
        r->flags.used = 1;
        r->recno = recno;
        memcpy( r->data, data, TRUST_RECORD_LEN );
@@ -230,7 +231,7 @@ put_record_into_cache( ulong recno, const char *data )
        if( cache_entries < MAX_CACHE_ENTRIES_HARD ) { /* no */
            if( opt.debug && !(cache_entries % 100) )
                log_debug("increasing tdbio cache size\n");
-           r = m_alloc( sizeof *r );
+           r = xmalloc( sizeof *r );
            r->flags.used = 1;
            r->recno = recno;
            memcpy( r->data, data, TRUST_RECORD_LEN );
@@ -332,7 +333,9 @@ tdbio_sync()
     return 0;
 }
 
-
+#if 0
+/* The transaction code is disabled in the 1.2.x branch, as it is not
+   yet used.  It will be enabled in 1.3.x. */
 
 /****************
  * Simple transactions system:
@@ -402,7 +405,7 @@ tdbio_cancel_transaction()
     in_transaction = 0;
     return 0;
 }
-
+#endif
 
 \f
 /********************************************************
@@ -453,7 +456,10 @@ create_version_record (void)
   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;
+  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 );
@@ -474,14 +480,23 @@ 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 = xstrdup (new_dbname);
 
     if( access( fname, R_OK ) ) {
        if( errno != ENOENT ) {
-           log_error( _("%s: can't access: %s\n"), fname, strerror(errno) );
-           m_free(fname);
+           log_error( _("can't access `%s': %s\n"), fname, strerror(errno) );
+           xfree(fname);
            return G10ERR_TRUSTDB;
        }
        if( create ) {
@@ -499,31 +514,36 @@ tdbio_set_dbname( const char *new_dbname, int create )
            }
            *p = DIRSEP_C;
 
-           m_free(db_name);
+           xfree(db_name);
            db_name = fname;
 #ifdef __riscos__
            if( !lockhandle )
                lockhandle = create_dotlock( db_name );
            if( !lockhandle )
-               log_fatal( _("%s: can't create lock\n"), db_name );
+               log_fatal( _("can't create lock for `%s'\n"), db_name );
             if( make_dotlock( lockhandle, -1 ) )
-                log_fatal( _("%s: can't make lock\n"), db_name );
+                log_fatal( _("can't lock `%s'\n"), db_name );
 #endif /* __riscos__ */
            oldmask=umask(077);
-           fp =fopen( fname, "wb" );
+            if (is_secured_filename (fname)) {
+                fp = NULL;
+                errno = EPERM;
+            }
+            else
+                fp =fopen( fname, "wb" );
            umask(oldmask);
            if( !fp )
-               log_fatal( _("%s: can't create: %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( _("%s: can't open: %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 );
            if( !lockhandle )
-               log_fatal( _("%s: can't create lock\n"), db_name );
+               log_fatal( _("can't create lock for `%s'\n"), db_name );
 #endif /* !__riscos__ */
 
             rc = create_version_record ();
@@ -540,7 +560,7 @@ tdbio_set_dbname( const char *new_dbname, int create )
            return 0;
        }
     }
-    m_free(db_name);
+    xfree(db_name);
     db_name = fname;
     return 0;
 }
@@ -566,19 +586,24 @@ open_db()
   if (!lockhandle )
     lockhandle = create_dotlock( db_name );
   if (!lockhandle )
-    log_fatal( _("%s: can't create lock\n"), db_name );
+    log_fatal( _("can't create lock for `%s'\n"), db_name );
 #ifdef __riscos__
   if (make_dotlock( lockhandle, -1 ) )
-    log_fatal( _("%s: can't make lock\n"), db_name );
+    log_fatal( _("can't lock `%s'\n"), db_name );
 #endif /* __riscos__ */
   db_fd = open (db_name, O_RDWR | MY_O_BINARY );
-  if (db_fd == -1 && errno == EACCES) {
+  if (db_fd == -1 && (errno == EACCES
+#ifdef EROFS
+                      || errno == EROFS)
+#endif
+      ) {
       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 )
-    log_fatal( _("%s: can't open: %s\n"), db_name, strerror(errno) );
+    log_fatal( _("can't open `%s': %s\n"), db_name, strerror(errno) );
+  register_secured_file (db_name);
 
   /* check whether we need to do a version migration */
   do
@@ -660,6 +685,18 @@ tdbio_db_matches_options()
   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.
@@ -748,8 +785,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( db_name, "upd_hashtable: read failed: %s\n",
-                                                       g10_errstr(rc) );
+       log_error("upd_hashtable: read failed: %s\n",   g10_errstr(rc) );
        return rc;
     }
 
@@ -758,7 +794,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
        rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = newrecnum;
        rc = tdbio_write_record( &rec );
        if( rc ) {
-           log_error( db_name, "upd_hashtable: write htbl failed: %s\n",
+           log_error("upd_hashtable: write htbl failed: %s\n",
                                                            g10_errstr(rc) );
            return rc;
        }
@@ -897,7 +933,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
     hashrec += msb / ITEMS_PER_HTBL_RECORD;
     rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
     if( rc ) {
-       log_error( db_name, "drop_from_hashtable: read failed: %s\n",
+       log_error("drop_from_hashtable: read failed: %s\n",
                                                        g10_errstr(rc) );
        return rc;
     }
@@ -910,7 +946,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
        rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = 0;
        rc = tdbio_write_record( &rec );
        if( rc )
-           log_error( db_name, "drop_from_hashtable: write htbl failed: %s\n",
+           log_error("drop_from_hashtable: write htbl failed: %s\n",
                                                            g10_errstr(rc) );
        return rc;
     }
@@ -939,7 +975,7 @@ 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( db_name, "drop_from_hashtable: write htbl failed: %s\n",
+                       log_error("drop_from_hashtable: write htbl failed: %s\n",
                                                                        g10_errstr(rc) );
                    return rc;
                }
@@ -986,7 +1022,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( db_name, "lookup_hashtable failed: %s\n", g10_errstr(rc) );
+       log_error("lookup_hashtable failed: %s\n", g10_errstr(rc) );
        return rc;
     }
 
@@ -996,14 +1032,14 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
 
     rc = tdbio_read_record( item, rec, 0 );
     if( rc ) {
-       log_error( db_name, "hashtable read failed: %s\n", g10_errstr(rc) );
+       log_error( "hashtable read failed: %s\n", g10_errstr(rc) );
        return rc;
     }
     if( rec->rectype == RECTYPE_HTBL ) {
        hashrec = item;
        level++;
        if( level >= keylen ) {
-           log_error( db_name, "hashtable has invalid indirections\n");
+           log_error("hashtable has invalid indirections\n");
            return G10ERR_TRUSTDB;
        }
        goto next_level;
@@ -1210,14 +1246,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",
@@ -1290,15 +1328,17 @@ 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;
-       ulongtobuf( p, rec->r.trust.validlist); p += 4;
        *p++ = rec->r.trust.min_ownertrust;
+        p++;
+       ulongtobuf( p, rec->r.trust.validlist); p += 4;
        break;
 
       case RECTYPE_VALID:
        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:
@@ -1488,7 +1528,7 @@ migrate_from_v2 ()
   int rc, count;
 
   ottable_size = 5;
-  ottable = m_alloc (ottable_size * sizeof *ottable);
+  ottable = xmalloc (ottable_size * sizeof *ottable);
   ottable_used = 0;
 
   /* We have some restrictions here.  We can't use the version record
@@ -1518,7 +1558,7 @@ migrate_from_v2 ()
       if (ottable_used == ottable_size)
         {
           ottable_size += 1000;
-          ottable = m_realloc (ottable, ottable_size * sizeof *ottable);
+          ottable = xrealloc (ottable, ottable_size * sizeof *ottable);
         }
       ottable[ottable_used].keyrecno = buftoulong (oldbuf+6);
       ottable[ottable_used].ot = oldbuf[18];
@@ -1589,8 +1629,5 @@ migrate_from_v2 ()
   if (rc)
     log_fatal ("failed to sync `%s'\n", db_name);
   log_info ("migrated %d version 2 ownertrusts\n", count);
-  m_free (ottable);
+  xfree (ottable);
 }
-
-
-