2012-01-20 04:33:51 +01:00
|
|
|
|
/* tdbio.c - trust database I/O operations
|
2015-01-22 12:06:11 +01:00
|
|
|
|
* Copyright (C) 1998-2002, 2012 Free Software Foundation, Inc.
|
2015-06-04 17:25:09 +02:00
|
|
|
|
* Copyright (C) 1998-2015 Werner Koch
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*
|
|
|
|
|
* 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
|
2007-07-04 21:49:40 +02:00
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 09:14:21 +02:00
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2003-06-18 21:56:13 +02:00
|
|
|
|
#include "gpg.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
|
#include "../common/status.h"
|
|
|
|
|
#include "../common/iobuf.h"
|
|
|
|
|
#include "../common/util.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
|
#include "options.h"
|
|
|
|
|
#include "main.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
|
#include "../common/i18n.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
|
#include "trustdb.h"
|
|
|
|
|
#include "tdbio.h"
|
|
|
|
|
|
2014-03-07 09:46:44 +01:00
|
|
|
|
#if defined(HAVE_DOSISH_SYSTEM) && !defined(ftruncate)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
#define ftruncate chsize
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(HAVE_DOSISH_SYSTEM) || defined(__CYGWIN__)
|
|
|
|
|
#define MY_O_BINARY O_BINARY
|
|
|
|
|
#else
|
|
|
|
|
#define MY_O_BINARY 0
|
|
|
|
|
#endif
|
|
|
|
|
|
2010-12-09 12:50:22 +01:00
|
|
|
|
/* 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
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
2003-06-05 09:14:21 +02:00
|
|
|
|
* Yes, this is a very simple implementation. We should really
|
|
|
|
|
* use a page aligned buffer and read complete pages.
|
|
|
|
|
* To implement a simple trannsaction system, this is sufficient.
|
|
|
|
|
*/
|
|
|
|
|
typedef struct cache_ctrl_struct *CACHE_CTRL;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
struct cache_ctrl_struct
|
|
|
|
|
{
|
|
|
|
|
CACHE_CTRL next;
|
|
|
|
|
struct {
|
|
|
|
|
unsigned used:1;
|
|
|
|
|
unsigned dirty:1;
|
|
|
|
|
} flags;
|
|
|
|
|
ulong recno;
|
|
|
|
|
char data[TRUST_RECORD_LEN];
|
2003-06-05 09:14:21 +02:00
|
|
|
|
};
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Size of the cache. The SOFT value is the general one. While in a
|
|
|
|
|
transaction this may not be sufficient and thus we may increase it
|
|
|
|
|
then up to the HARD limit. */
|
|
|
|
|
#define MAX_CACHE_ENTRIES_SOFT 200
|
|
|
|
|
#define MAX_CACHE_ENTRIES_HARD 10000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* The cache is controlled by these variables. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
static CACHE_CTRL cache_list;
|
|
|
|
|
static int cache_entries;
|
|
|
|
|
static int cache_is_dirty;
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
2015-11-16 12:41:46 +01:00
|
|
|
|
/* An object to pass information to cmp_krec_fpr. */
|
2015-06-04 17:25:09 +02:00
|
|
|
|
struct cmp_krec_fpr_struct
|
|
|
|
|
{
|
|
|
|
|
int pubkey_algo;
|
|
|
|
|
const char *fpr;
|
|
|
|
|
int fprlen;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
};
|
|
|
|
|
|
2015-11-16 12:41:46 +01:00
|
|
|
|
/* An object used to pass information to cmp_[s]dir. */
|
2015-06-04 17:25:09 +02:00
|
|
|
|
struct cmp_xdir_struct
|
|
|
|
|
{
|
|
|
|
|
int pubkey_algo;
|
|
|
|
|
u32 keyid[2];
|
2003-06-05 09:14:21 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* The name of the trustdb file. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
static char *db_name;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
2018-03-26 16:57:04 +02:00
|
|
|
|
/* The handle for locking the trustdb file and a counter to record how
|
|
|
|
|
* often this lock has been taken. That counter is required becuase
|
|
|
|
|
* dotlock does not implemen recursive locks. */
|
2009-09-23 12:28:41 +02:00
|
|
|
|
static dotlock_t lockhandle;
|
2018-03-26 16:57:04 +02:00
|
|
|
|
static unsigned int is_locked;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/* The file descriptor of the trustdb. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
static int db_fd = -1;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/* A flag indicating that a transaction is active. */
|
2018-03-26 16:26:46 +02:00
|
|
|
|
/* static int in_transaction; Not yet used. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
static void open_db (void);
|
2017-03-31 20:03:52 +02:00
|
|
|
|
static void create_hashtable (ctrl_t ctrl, TRUSTREC *vr, int type);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Take a lock on the trustdb file name. I a lock file can't be
|
2018-03-26 16:26:46 +02:00
|
|
|
|
* created the function terminates the process. Except for a
|
2015-06-04 17:25:09 +02:00
|
|
|
|
* different return code the function does nothing if the lock has
|
|
|
|
|
* already been taken.
|
|
|
|
|
*
|
|
|
|
|
* Returns: True if lock already exists, False if the lock has
|
|
|
|
|
* actually been taken.
|
|
|
|
|
*/
|
2015-05-28 10:08:37 +02:00
|
|
|
|
static int
|
|
|
|
|
take_write_lock (void)
|
|
|
|
|
{
|
2018-03-26 16:57:04 +02:00
|
|
|
|
int rc;
|
|
|
|
|
|
2015-05-28 10:08:37 +02:00
|
|
|
|
if (!lockhandle)
|
|
|
|
|
lockhandle = dotlock_create (db_name, 0);
|
|
|
|
|
if (!lockhandle)
|
|
|
|
|
log_fatal ( _("can't create lock for '%s'\n"), db_name );
|
|
|
|
|
|
|
|
|
|
if (!is_locked)
|
|
|
|
|
{
|
|
|
|
|
if (dotlock_take (lockhandle, -1) )
|
|
|
|
|
log_fatal ( _("can't lock '%s'\n"), db_name );
|
2018-03-26 16:57:04 +02:00
|
|
|
|
rc = 0;
|
2015-05-28 10:08:37 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
2018-03-26 16:57:04 +02:00
|
|
|
|
rc = 1;
|
|
|
|
|
|
|
|
|
|
if (opt.lock_once)
|
|
|
|
|
is_locked = 1;
|
|
|
|
|
else
|
|
|
|
|
is_locked++;
|
|
|
|
|
return rc;
|
2015-05-28 10:08:37 +02:00
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Release a lock from the trustdb file unless the global option
|
|
|
|
|
* --lock-once has been used.
|
|
|
|
|
*/
|
2015-05-28 10:08:37 +02:00
|
|
|
|
static void
|
|
|
|
|
release_write_lock (void)
|
|
|
|
|
{
|
2018-03-26 16:57:04 +02:00
|
|
|
|
if (opt.lock_once)
|
|
|
|
|
return; /* Don't care; here IS_LOCKED is fixed to 1. */
|
|
|
|
|
|
|
|
|
|
if (!is_locked)
|
|
|
|
|
{
|
|
|
|
|
log_error ("Ooops, tdbio:release_write_lock with no lock held\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (--is_locked)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (dotlock_release (lockhandle))
|
|
|
|
|
log_error ("Oops, tdbio:release_write_locked failed\n");
|
2015-05-28 10:08:37 +02:00
|
|
|
|
}
|
2018-03-26 16:57:04 +02:00
|
|
|
|
|
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
|
|
/*************************************
|
|
|
|
|
************* record cache **********
|
|
|
|
|
*************************************/
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Get the data from the record cache and return a pointer into that
|
|
|
|
|
* cache. Caller should copy the returned data. NULL is returned on
|
|
|
|
|
* a cache miss.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
static const char *
|
2015-06-04 17:25:09 +02:00
|
|
|
|
get_record_from_cache (ulong recno)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
CACHE_CTRL r;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
for (r = cache_list; r; r = r->next)
|
|
|
|
|
{
|
|
|
|
|
if (r->flags.used && r->recno == recno)
|
|
|
|
|
return r->data;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
return NULL;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Write a cached item back to the trustdb file.
|
|
|
|
|
*
|
|
|
|
|
* Returns: 0 on success or an error code.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
static int
|
2015-06-04 17:25:09 +02:00
|
|
|
|
write_cache_item (CACHE_CTRL r)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
gpg_error_t err;
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
if (lseek (db_fd, r->recno * TRUST_RECORD_LEN, SEEK_SET) == -1)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error (_("trustdb rec %lu: lseek failed: %s\n"),
|
|
|
|
|
r->recno, strerror (errno));
|
|
|
|
|
return err;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
n = write (db_fd, r->data, TRUST_RECORD_LEN);
|
|
|
|
|
if (n != TRUST_RECORD_LEN)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error (_("trustdb rec %lu: write failed (n=%d): %s\n"),
|
|
|
|
|
r->recno, n, strerror (errno) );
|
|
|
|
|
return err;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
r->flags.dirty = 0;
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Put data into the cache. This function may flush
|
|
|
|
|
* some cache entries if the cache is filled up.
|
|
|
|
|
*
|
|
|
|
|
* Returns: 0 on success or an error code.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
2015-10-19 10:51:05 +02:00
|
|
|
|
static int
|
2015-06-04 17:25:09 +02:00
|
|
|
|
put_record_into_cache (ulong recno, const char *data)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
CACHE_CTRL r, unused;
|
|
|
|
|
int dirty_count = 0;
|
|
|
|
|
int clean_count = 0;
|
|
|
|
|
|
|
|
|
|
/* See whether we already cached this one. */
|
|
|
|
|
for (unused = NULL, r = cache_list; r; r = r->next)
|
|
|
|
|
{
|
|
|
|
|
if (!r->flags.used)
|
|
|
|
|
{
|
|
|
|
|
if (!unused)
|
|
|
|
|
unused = r;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else if (r->recno == recno)
|
|
|
|
|
{
|
|
|
|
|
if (!r->flags.dirty)
|
|
|
|
|
{
|
|
|
|
|
/* Hmmm: should we use a copy and compare? */
|
|
|
|
|
if (memcmp (r->data, data, TRUST_RECORD_LEN))
|
|
|
|
|
{
|
|
|
|
|
r->flags.dirty = 1;
|
|
|
|
|
cache_is_dirty = 1;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
memcpy (r->data, data, TRUST_RECORD_LEN);
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (r->flags.used)
|
|
|
|
|
{
|
|
|
|
|
if (r->flags.dirty)
|
|
|
|
|
dirty_count++;
|
|
|
|
|
else
|
|
|
|
|
clean_count++;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/* Not in the cache: add a new entry. */
|
|
|
|
|
if (unused)
|
|
|
|
|
{
|
|
|
|
|
/* Reuse this entry. */
|
|
|
|
|
r = unused;
|
|
|
|
|
r->flags.used = 1;
|
|
|
|
|
r->recno = recno;
|
|
|
|
|
memcpy (r->data, data, TRUST_RECORD_LEN);
|
|
|
|
|
r->flags.dirty = 1;
|
|
|
|
|
cache_is_dirty = 1;
|
|
|
|
|
cache_entries++;
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/* See whether we reached the limit. */
|
|
|
|
|
if (cache_entries < MAX_CACHE_ENTRIES_SOFT)
|
|
|
|
|
{
|
|
|
|
|
/* No: Put into cache. */
|
|
|
|
|
r = xmalloc (sizeof *r);
|
|
|
|
|
r->flags.used = 1;
|
|
|
|
|
r->recno = recno;
|
|
|
|
|
memcpy (r->data, data, TRUST_RECORD_LEN);
|
|
|
|
|
r->flags.dirty = 1;
|
|
|
|
|
r->next = cache_list;
|
|
|
|
|
cache_list = r;
|
|
|
|
|
cache_is_dirty = 1;
|
|
|
|
|
cache_entries++;
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/* Cache is full: discard some clean entries. */
|
|
|
|
|
if (clean_count)
|
|
|
|
|
{
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
/* We discard a third of the clean entries. */
|
|
|
|
|
n = clean_count / 3;
|
|
|
|
|
if (!n)
|
|
|
|
|
n = 1;
|
|
|
|
|
|
|
|
|
|
for (unused = NULL, r = cache_list; r; r = r->next)
|
|
|
|
|
{
|
|
|
|
|
if (r->flags.used && !r->flags.dirty)
|
|
|
|
|
{
|
|
|
|
|
if (!unused)
|
|
|
|
|
unused = r;
|
|
|
|
|
r->flags.used = 0;
|
|
|
|
|
cache_entries--;
|
|
|
|
|
if (!--n)
|
|
|
|
|
break;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/* Now put into the cache. */
|
2016-04-29 11:05:24 +02:00
|
|
|
|
log_assert (unused);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
r = unused;
|
|
|
|
|
r->flags.used = 1;
|
|
|
|
|
r->recno = recno;
|
|
|
|
|
memcpy (r->data, data, TRUST_RECORD_LEN);
|
|
|
|
|
r->flags.dirty = 1;
|
|
|
|
|
cache_is_dirty = 1;
|
|
|
|
|
cache_entries++;
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/* No clean entries: We have to flush some dirty entries. */
|
2018-03-26 16:26:46 +02:00
|
|
|
|
#if 0 /* Transactions are not yet used. */
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (in_transaction)
|
|
|
|
|
{
|
|
|
|
|
/* But we can't do this while in a transaction. Thus we
|
|
|
|
|
* increase the cache size instead. */
|
|
|
|
|
if (cache_entries < MAX_CACHE_ENTRIES_HARD)
|
|
|
|
|
{
|
|
|
|
|
if (opt.debug && !(cache_entries % 100))
|
|
|
|
|
log_debug ("increasing tdbio cache size\n");
|
|
|
|
|
r = xmalloc (sizeof *r);
|
|
|
|
|
r->flags.used = 1;
|
|
|
|
|
r->recno = recno;
|
|
|
|
|
memcpy (r->data, data, TRUST_RECORD_LEN);
|
|
|
|
|
r->flags.dirty = 1;
|
|
|
|
|
r->next = cache_list;
|
|
|
|
|
cache_list = r;
|
|
|
|
|
cache_is_dirty = 1;
|
|
|
|
|
cache_entries++;
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Hard limit for the cache size reached. */
|
|
|
|
|
log_info (_("trustdb transaction too large\n"));
|
|
|
|
|
return GPG_ERR_RESOURCE_LIMIT;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2018-03-26 16:26:46 +02:00
|
|
|
|
#endif
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
if (dirty_count)
|
|
|
|
|
{
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
/* Discard some dirty entries. */
|
|
|
|
|
n = dirty_count / 5;
|
|
|
|
|
if (!n)
|
|
|
|
|
n = 1;
|
|
|
|
|
|
|
|
|
|
take_write_lock ();
|
|
|
|
|
for (unused = NULL, r = cache_list; r; r = r->next)
|
|
|
|
|
{
|
|
|
|
|
if (r->flags.used && r->flags.dirty)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
rc = write_cache_item (r);
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
if (!unused)
|
|
|
|
|
unused = r;
|
|
|
|
|
r->flags.used = 0;
|
|
|
|
|
cache_entries--;
|
|
|
|
|
if (!--n)
|
|
|
|
|
break;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
release_write_lock ();
|
|
|
|
|
|
|
|
|
|
/* Now put into the cache. */
|
2016-04-29 11:05:24 +02:00
|
|
|
|
log_assert (unused);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
r = unused;
|
|
|
|
|
r->flags.used = 1;
|
|
|
|
|
r->recno = recno;
|
|
|
|
|
memcpy (r->data, data, TRUST_RECORD_LEN);
|
|
|
|
|
r->flags.dirty = 1;
|
|
|
|
|
cache_is_dirty = 1;
|
|
|
|
|
cache_entries++;
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/* We should never reach this. */
|
|
|
|
|
BUG();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Return true if the cache is dirty. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
int
|
|
|
|
|
tdbio_is_dirty()
|
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
return cache_is_dirty;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
2003-06-05 09:14:21 +02:00
|
|
|
|
* Flush the cache. This cannot be used while in a transaction.
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
tdbio_sync()
|
|
|
|
|
{
|
|
|
|
|
CACHE_CTRL r;
|
|
|
|
|
int did_lock = 0;
|
|
|
|
|
|
|
|
|
|
if( db_fd == -1 )
|
|
|
|
|
open_db();
|
2018-03-26 16:26:46 +02:00
|
|
|
|
#if 0 /* Transactions are not yet used. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
if( in_transaction )
|
|
|
|
|
log_bug("tdbio: syncing while in transaction\n");
|
2018-03-26 16:26:46 +02:00
|
|
|
|
#endif
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
|
|
if( !cache_is_dirty )
|
|
|
|
|
return 0;
|
|
|
|
|
|
2015-05-28 10:08:37 +02:00
|
|
|
|
if (!take_write_lock ())
|
|
|
|
|
did_lock = 1;
|
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
for( r = cache_list; r; r = r->next ) {
|
|
|
|
|
if( r->flags.used && r->flags.dirty ) {
|
|
|
|
|
int rc = write_cache_item( r );
|
|
|
|
|
if( rc )
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cache_is_dirty = 0;
|
2015-05-28 10:08:37 +02:00
|
|
|
|
if (did_lock)
|
|
|
|
|
release_write_lock ();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
#if 0 /* Not yet used. */
|
|
|
|
|
/*
|
2003-06-05 09:14:21 +02:00
|
|
|
|
* Simple transactions system:
|
|
|
|
|
* Everything between begin_transaction and end/cancel_transaction
|
2015-11-16 12:41:46 +01:00
|
|
|
|
* is not immediately written but at the time of end_transaction.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*
|
2015-06-04 17:25:09 +02:00
|
|
|
|
* NOTE: The transaction code is disabled in the 1.2 branch, as it is
|
|
|
|
|
* not yet used.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
int
|
2015-06-04 17:25:09 +02:00
|
|
|
|
tdbio_begin_transaction () /* Not yet used. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
int rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (in_transaction)
|
|
|
|
|
log_bug ("tdbio: nested transactions\n");
|
|
|
|
|
/* Flush everything out. */
|
|
|
|
|
rc = tdbio_sync();
|
|
|
|
|
if (rc)
|
|
|
|
|
return rc;
|
|
|
|
|
in_transaction = 1;
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2015-06-04 17:25:09 +02:00
|
|
|
|
tdbio_end_transaction () /* Not yet used. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
if (!in_transaction)
|
|
|
|
|
log_bug ("tdbio: no active transaction\n");
|
|
|
|
|
take_write_lock ();
|
|
|
|
|
gnupg_block_all_signals ();
|
|
|
|
|
in_transaction = 0;
|
|
|
|
|
rc = tdbio_sync();
|
|
|
|
|
gnupg_unblock_all_signals();
|
|
|
|
|
release_write_lock ();
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2015-06-04 17:25:09 +02:00
|
|
|
|
tdbio_cancel_transaction () /* Not yet used. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
CACHE_CTRL r;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (!in_transaction)
|
|
|
|
|
log_bug ("tdbio: no active transaction\n");
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Remove all dirty marked entries, so that the original ones are
|
|
|
|
|
* read back the next time. */
|
|
|
|
|
if (cache_is_dirty)
|
|
|
|
|
{
|
|
|
|
|
for (r = cache_list; r; r = r->next)
|
|
|
|
|
{
|
|
|
|
|
if (r->flags.used && r->flags.dirty)
|
|
|
|
|
{
|
|
|
|
|
r->flags.used = 0;
|
|
|
|
|
cache_entries--;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
cache_is_dirty = 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
in_transaction = 0;
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
#endif /* Not yet used. */
|
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************
|
|
|
|
|
**************** cached I/O functions ******************
|
|
|
|
|
********************************************************/
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* The cleanup handler for this module. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
static void
|
2015-06-04 17:25:09 +02:00
|
|
|
|
cleanup (void)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (is_locked)
|
|
|
|
|
{
|
|
|
|
|
if (!dotlock_release (lockhandle))
|
|
|
|
|
is_locked = 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Update an existing trustdb record. The caller must call
|
|
|
|
|
* tdbio_sync.
|
|
|
|
|
*
|
|
|
|
|
* Returns: 0 on success or an error code.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
int
|
2017-03-31 20:03:52 +02:00
|
|
|
|
tdbio_update_version_record (ctrl_t ctrl)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
|
|
|
|
TRUSTREC rec;
|
|
|
|
|
int rc;
|
2018-10-10 11:46:16 +02:00
|
|
|
|
int opt_tm;
|
|
|
|
|
|
|
|
|
|
/* Never store a TOFU trust model in the trustdb. Use PGP instead. */
|
|
|
|
|
opt_tm = opt.trust_model;
|
|
|
|
|
if (opt_tm == TM_TOFU || opt_tm == TM_TOFU_PGP)
|
|
|
|
|
opt_tm = TM_PGP;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
memset (&rec, 0, sizeof rec);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rc = tdbio_read_record (0, &rec, RECTYPE_VER);
|
|
|
|
|
if (!rc)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
|
|
|
|
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;
|
2018-10-10 11:46:16 +02:00
|
|
|
|
rec.r.ver.trust_model = opt_tm;
|
2012-01-20 04:33:51 +01:00
|
|
|
|
rec.r.ver.min_cert_level = opt.min_cert_level;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Create and write the trustdb version record.
|
2018-03-26 16:26:46 +02:00
|
|
|
|
* This is called with the writelock activ.
|
2015-06-04 17:25:09 +02:00
|
|
|
|
* Returns: 0 on success or an error code.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
static int
|
2017-03-31 20:03:52 +02:00
|
|
|
|
create_version_record (ctrl_t ctrl)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
|
|
|
|
TRUSTREC rec;
|
|
|
|
|
int rc;
|
2018-10-10 11:46:16 +02:00
|
|
|
|
int opt_tm;
|
|
|
|
|
|
|
|
|
|
/* Never store a TOFU trust model in the trustdb. Use PGP instead. */
|
|
|
|
|
opt_tm = opt.trust_model;
|
|
|
|
|
if (opt_tm == TM_TOFU || opt_tm == TM_TOFU_PGP)
|
|
|
|
|
opt_tm = TM_PGP;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
memset (&rec, 0, sizeof rec);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
rec.r.ver.version = 3;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rec.r.ver.created = make_timestamp ();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
rec.r.ver.marginals = opt.marginals_needed;
|
|
|
|
|
rec.r.ver.completes = opt.completes_needed;
|
|
|
|
|
rec.r.ver.cert_depth = opt.max_cert_depth;
|
2018-10-10 11:46:16 +02:00
|
|
|
|
if (opt_tm == TM_PGP || opt_tm == TM_CLASSIC)
|
|
|
|
|
rec.r.ver.trust_model = opt_tm;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
else
|
|
|
|
|
rec.r.ver.trust_model = TM_PGP;
|
2012-01-20 04:33:51 +01:00
|
|
|
|
rec.r.ver.min_cert_level = opt.min_cert_level;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
rec.rectype = RECTYPE_VER;
|
|
|
|
|
rec.recnum = 0;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2016-06-15 01:41:56 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (!rc)
|
|
|
|
|
tdbio_sync ();
|
2016-06-15 01:41:56 +02:00
|
|
|
|
|
|
|
|
|
if (!rc)
|
2017-03-31 20:03:52 +02:00
|
|
|
|
create_hashtable (ctrl, &rec, 0);
|
2016-06-15 01:41:56 +02:00
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Set the file name for the trustdb to NEW_DBNAME and if CREATE is
|
|
|
|
|
* true create that file. If NEW_DBNAME is NULL a default name is
|
|
|
|
|
* used, if the it does not contain a path component separator ('/')
|
|
|
|
|
* the global GnuPG home directory is used.
|
|
|
|
|
*
|
|
|
|
|
* Returns: 0 on success or an error code.
|
|
|
|
|
*
|
|
|
|
|
* On the first call this function registers an atexit handler.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
int
|
2017-03-31 20:03:52 +02:00
|
|
|
|
tdbio_set_dbname (ctrl_t ctrl, const char *new_dbname,
|
|
|
|
|
int create, int *r_nofile)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2016-02-12 02:10:33 +01:00
|
|
|
|
char *fname, *p;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
struct stat statbuf;
|
|
|
|
|
static int initialized = 0;
|
2016-02-12 02:10:33 +01:00
|
|
|
|
int save_slash;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (!initialized)
|
|
|
|
|
{
|
|
|
|
|
atexit (cleanup);
|
|
|
|
|
initialized = 1;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
*r_nofile = 0;
|
2013-10-11 09:25:58 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (!new_dbname)
|
|
|
|
|
{
|
2016-06-07 10:59:46 +02:00
|
|
|
|
fname = make_filename (gnupg_homedir (),
|
|
|
|
|
"trustdb" EXTSEP_S GPGEXT_GPG, NULL);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
}
|
|
|
|
|
else if (*new_dbname != DIRSEP_C )
|
|
|
|
|
{
|
|
|
|
|
if (strchr (new_dbname, DIRSEP_C))
|
|
|
|
|
fname = make_filename (new_dbname, NULL);
|
|
|
|
|
else
|
2016-06-07 10:59:46 +02:00
|
|
|
|
fname = make_filename (gnupg_homedir (), new_dbname, NULL);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-06-18 21:56:13 +02:00
|
|
|
|
fname = xstrdup (new_dbname);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
xfree (db_name);
|
|
|
|
|
db_name = fname;
|
2015-05-28 10:08:37 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Quick check for (likely) case where there already is a
|
|
|
|
|
* trustdb.gpg. This check is not required in theory, but it helps
|
|
|
|
|
* in practice avoiding costly operations of preparing and taking
|
|
|
|
|
* the lock. */
|
2020-10-20 16:38:06 +02:00
|
|
|
|
if (!gnupg_stat (fname, &statbuf) && statbuf.st_size > 0)
|
2015-06-04 17:25:09 +02:00
|
|
|
|
{
|
2015-05-28 10:08:37 +02:00
|
|
|
|
/* OK, we have the valid trustdb.gpg already. */
|
|
|
|
|
return 0;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
}
|
2016-02-12 02:10:33 +01:00
|
|
|
|
else if (!create)
|
|
|
|
|
{
|
|
|
|
|
*r_nofile = 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Here comes: No valid trustdb.gpg AND CREATE==1 */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Make sure the directory exists. This should be done before
|
|
|
|
|
* acquiring the lock, which assumes the existence of the directory.
|
|
|
|
|
*/
|
|
|
|
|
p = strrchr (fname, DIRSEP_C);
|
|
|
|
|
#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*/
|
2016-04-29 11:05:24 +02:00
|
|
|
|
log_assert (p);
|
2016-02-12 02:10:33 +01:00
|
|
|
|
save_slash = *p;
|
|
|
|
|
*p = 0;
|
2020-10-20 10:43:55 +02:00
|
|
|
|
if (gnupg_access (fname, F_OK))
|
2016-02-12 02:10:33 +01:00
|
|
|
|
{
|
|
|
|
|
try_make_homedir (fname);
|
2020-10-20 10:43:55 +02:00
|
|
|
|
if (gnupg_access (fname, F_OK))
|
2016-02-12 02:10:33 +01:00
|
|
|
|
log_fatal (_("%s: directory does not exist!\n"), fname);
|
|
|
|
|
}
|
|
|
|
|
*p = save_slash;
|
2015-05-28 10:08:37 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
take_write_lock ();
|
2015-05-28 10:08:37 +02:00
|
|
|
|
|
2020-10-20 10:43:55 +02:00
|
|
|
|
if (gnupg_access (fname, R_OK)
|
2020-10-20 16:38:06 +02:00
|
|
|
|
|| gnupg_stat (fname, &statbuf)
|
2020-10-20 10:43:55 +02:00
|
|
|
|
|| statbuf.st_size == 0)
|
2015-06-04 17:25:09 +02:00
|
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
|
estream_t fp;
|
2016-02-12 02:10:33 +01:00
|
|
|
|
TRUSTREC rec;
|
|
|
|
|
int rc;
|
|
|
|
|
mode_t oldmask;
|
|
|
|
|
|
2010-12-09 12:50:22 +01:00
|
|
|
|
#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*/
|
2016-08-10 16:41:22 +02:00
|
|
|
|
if (errno && errno != ENOENT)
|
2015-06-04 17:25:09 +02:00
|
|
|
|
log_fatal ( _("can't access '%s': %s\n"), fname, strerror (errno));
|
|
|
|
|
|
2016-02-12 02:10:33 +01:00
|
|
|
|
oldmask = umask (077);
|
|
|
|
|
if (is_secured_filename (fname))
|
2015-06-04 17:25:09 +02:00
|
|
|
|
{
|
2016-02-12 02:10:33 +01:00
|
|
|
|
fp = NULL;
|
|
|
|
|
gpg_err_set_errno (EPERM);
|
|
|
|
|
}
|
|
|
|
|
else
|
2020-10-20 11:52:16 +02:00
|
|
|
|
fp = es_fopen (fname, "wb");
|
2016-02-12 02:10:33 +01:00
|
|
|
|
umask(oldmask);
|
|
|
|
|
if (!fp)
|
|
|
|
|
log_fatal (_("can't create '%s': %s\n"), fname, strerror (errno));
|
2020-10-20 11:52:16 +02:00
|
|
|
|
es_fclose (fp);
|
2016-02-12 02:10:33 +01:00
|
|
|
|
|
2020-10-20 14:08:35 +02:00
|
|
|
|
db_fd = gnupg_open (db_name, O_RDWR | MY_O_BINARY, 0);
|
2016-02-12 02:10:33 +01:00
|
|
|
|
if (db_fd == -1)
|
|
|
|
|
log_fatal (_("can't open '%s': %s\n"), db_name, strerror (errno));
|
|
|
|
|
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = create_version_record (ctrl);
|
2016-02-12 02:10:33 +01:00
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: failed to create version record: %s"),
|
|
|
|
|
fname, gpg_strerror (rc));
|
|
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
|
|
if (!opt.quiet)
|
|
|
|
|
log_info (_("%s: trustdb created\n"), db_name);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-05-28 10:08:37 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
release_write_lock ();
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Return the full name of the trustdb.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
const char *
|
2015-06-04 17:25:09 +02:00
|
|
|
|
tdbio_get_dbname ()
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
return db_name;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Open the trustdb. This may only be called if it has not yet been
|
|
|
|
|
* opened and after a successful call to tdbio_set_dbname. On return
|
|
|
|
|
* the trustdb handle (DB_FD) is guaranteed to be open.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
static void
|
2015-06-04 17:25:09 +02:00
|
|
|
|
open_db ()
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
|
|
|
|
TRUSTREC rec;
|
|
|
|
|
|
2016-04-29 11:05:24 +02:00
|
|
|
|
log_assert( db_fd == -1 );
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2010-12-09 13:57:50 +01:00
|
|
|
|
#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)
|
2012-06-05 19:29:22 +02:00
|
|
|
|
log_fatal ("can't open '%s': %d, %d\n", db_name,
|
2010-12-09 13:57:50 +01:00
|
|
|
|
(int)prevrc, (int)GetLastError ());
|
|
|
|
|
}
|
|
|
|
|
#else /*!HAVE_W32CE_SYSTEM*/
|
2020-10-20 14:08:35 +02:00
|
|
|
|
db_fd = gnupg_open (db_name, O_RDWR | MY_O_BINARY, 0);
|
2006-04-19 13:26:11 +02:00
|
|
|
|
if (db_fd == -1 && (errno == EACCES
|
|
|
|
|
#ifdef EROFS
|
2008-08-01 12:51:11 +02:00
|
|
|
|
|| errno == EROFS
|
2006-04-19 13:26:11 +02:00
|
|
|
|
#endif
|
2008-08-01 12:51:11 +02:00
|
|
|
|
)
|
2006-04-19 13:26:11 +02:00
|
|
|
|
) {
|
2010-12-09 13:57:50 +01:00
|
|
|
|
/* Take care of read-only trustdbs. */
|
2020-10-20 14:08:35 +02:00
|
|
|
|
db_fd = gnupg_open (db_name, O_RDONLY | MY_O_BINARY, 0);
|
2011-07-29 09:58:34 +02:00
|
|
|
|
if (db_fd != -1 && !opt.quiet)
|
2014-10-10 15:29:42 +02:00
|
|
|
|
log_info (_("Note: trustdb not writable\n"));
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
if ( db_fd == -1 )
|
2012-06-05 19:29:22 +02:00
|
|
|
|
log_fatal( _("can't open '%s': %s\n"), db_name, strerror(errno) );
|
2010-12-09 13:57:50 +01:00
|
|
|
|
#endif /*!HAVE_W32CE_SYSTEM*/
|
2006-04-19 13:26:11 +02:00
|
|
|
|
register_secured_file (db_name);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
|
/* Read the version record. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
if (tdbio_read_record (0, &rec, RECTYPE_VER ) )
|
|
|
|
|
log_fatal( _("%s: invalid trustdb\n"), db_name );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Append a new empty hashtable to the trustdb. TYPE gives the type
|
|
|
|
|
* of the hash table. The only defined type is 0 for a trust hash.
|
|
|
|
|
* On return the hashtable has been created, written, the version
|
|
|
|
|
* record update, and the data flushed to the disk. On a fatal error
|
|
|
|
|
* the function terminates the process.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
static void
|
2017-03-31 20:03:52 +02:00
|
|
|
|
create_hashtable (ctrl_t ctrl, TRUSTREC *vr, int type)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
TRUSTREC rec;
|
|
|
|
|
off_t offset;
|
|
|
|
|
ulong recnum;
|
|
|
|
|
int i, n, rc;
|
|
|
|
|
|
|
|
|
|
offset = lseek (db_fd, 0, SEEK_END);
|
|
|
|
|
if (offset == -1)
|
|
|
|
|
log_fatal ("trustdb: lseek to end failed: %s\n", strerror(errno));
|
|
|
|
|
recnum = offset / TRUST_RECORD_LEN;
|
2016-04-29 11:05:24 +02:00
|
|
|
|
log_assert (recnum); /* This is will never be the first record. */
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
if (!type)
|
|
|
|
|
vr->r.ver.trusthashtbl = recnum;
|
|
|
|
|
|
|
|
|
|
/* Now write the records making up the hash table. */
|
|
|
|
|
n = (256+ITEMS_PER_HTBL_RECORD-1) / ITEMS_PER_HTBL_RECORD;
|
|
|
|
|
for (i=0; i < n; i++, recnum++)
|
|
|
|
|
{
|
|
|
|
|
memset (&rec, 0, sizeof rec);
|
|
|
|
|
rec.rectype = RECTYPE_HTBL;
|
|
|
|
|
rec.recnum = recnum;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: failed to create hashtable: %s\n"),
|
|
|
|
|
db_name, gpg_strerror (rc));
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Update the version record and flush. */
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, vr);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (!rc)
|
|
|
|
|
rc = tdbio_sync ();
|
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: error updating version record: %s\n"),
|
|
|
|
|
db_name, gpg_strerror (rc));
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Check whether open trustdb matches the global trust options given
|
|
|
|
|
* for this process. On a read problem the process is terminated.
|
|
|
|
|
*
|
|
|
|
|
* Return: 1 for yes, 0 for no.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
int
|
|
|
|
|
tdbio_db_matches_options()
|
|
|
|
|
{
|
|
|
|
|
static int yes_no = -1;
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (yes_no == -1)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
|
|
|
|
TRUSTREC vr;
|
|
|
|
|
int rc;
|
2018-10-10 11:46:16 +02:00
|
|
|
|
int opt_tm, tm;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rc = tdbio_read_record (0, &vr, RECTYPE_VER);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
if( rc )
|
|
|
|
|
log_fatal( _("%s: error reading version record: %s\n"),
|
2015-01-22 12:06:11 +01:00
|
|
|
|
db_name, gpg_strerror (rc) );
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2018-10-10 11:46:16 +02:00
|
|
|
|
/* Consider tofu and pgp the same. */
|
|
|
|
|
tm = vr.r.ver.trust_model;
|
|
|
|
|
if (tm == TM_TOFU || tm == TM_TOFU_PGP)
|
|
|
|
|
tm = TM_PGP;
|
|
|
|
|
opt_tm = opt.trust_model;
|
|
|
|
|
if (opt_tm == TM_TOFU || opt_tm == TM_TOFU_PGP)
|
|
|
|
|
opt_tm = TM_PGP;
|
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
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
|
2018-10-10 11:46:16 +02:00
|
|
|
|
&& tm == opt_tm
|
2012-01-20 04:33:51 +01:00
|
|
|
|
&& vr.r.ver.min_cert_level == opt.min_cert_level;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return yes_no;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Read and return the trust model identifier from the trustdb. On a
|
|
|
|
|
* read problem the process is terminated.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
byte
|
2015-06-04 17:25:09 +02:00
|
|
|
|
tdbio_read_model (void)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
|
|
|
|
TRUSTREC vr;
|
|
|
|
|
int rc;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rc = tdbio_read_record (0, &vr, RECTYPE_VER );
|
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: error reading version record: %s\n"),
|
2015-01-22 12:06:11 +01:00
|
|
|
|
db_name, gpg_strerror (rc) );
|
2003-06-05 09:14:21 +02:00
|
|
|
|
return vr.r.ver.trust_model;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Read and return the nextstamp value from the trustdb. On a read
|
|
|
|
|
* problem the process is terminated.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
ulong
|
|
|
|
|
tdbio_read_nextcheck ()
|
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
TRUSTREC vr;
|
|
|
|
|
int rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rc = tdbio_read_record (0, &vr, RECTYPE_VER);
|
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: error reading version record: %s\n"),
|
|
|
|
|
db_name, gpg_strerror (rc));
|
|
|
|
|
return vr.r.ver.nextcheck;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Write the STAMP nextstamp timestamp to the trustdb. On a read or
|
|
|
|
|
* write problem the process is terminated.
|
|
|
|
|
*
|
|
|
|
|
* Return: True if the stamp actually changed.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
int
|
2017-03-31 20:03:52 +02:00
|
|
|
|
tdbio_write_nextcheck (ctrl_t ctrl, ulong stamp)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
TRUSTREC vr;
|
|
|
|
|
int rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rc = tdbio_read_record (0, &vr, RECTYPE_VER);
|
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: error reading version record: %s\n"),
|
|
|
|
|
db_name, gpg_strerror (rc));
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (vr.r.ver.nextcheck == stamp)
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
vr.r.ver.nextcheck = stamp;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &vr);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: error writing version record: %s\n"),
|
|
|
|
|
db_name, gpg_strerror (rc));
|
|
|
|
|
return 1;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Return the record number of the trusthash table or create one if it
|
|
|
|
|
* does not yet exist. On a read or write problem the process is
|
|
|
|
|
* terminated.
|
|
|
|
|
*
|
|
|
|
|
* Return: record number
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
static ulong
|
2018-03-26 18:06:43 +02:00
|
|
|
|
get_trusthashrec (ctrl_t ctrl)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
static ulong trusthashtbl; /* Record number of the trust hashtable. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2018-03-26 18:06:43 +02:00
|
|
|
|
(void)ctrl;
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (!trusthashtbl)
|
|
|
|
|
{
|
|
|
|
|
TRUSTREC vr;
|
|
|
|
|
int rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rc = tdbio_read_record (0, &vr, RECTYPE_VER );
|
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: error reading version record: %s\n"),
|
|
|
|
|
db_name, gpg_strerror (rc) );
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2018-03-26 18:20:16 +02:00
|
|
|
|
if (!vr.r.ver.trusthashtbl)
|
|
|
|
|
{
|
|
|
|
|
/* Oops: the trustdb is corrupt because the hashtable is
|
|
|
|
|
* always created along with the version record. However,
|
|
|
|
|
* if something went initially wrong it may happen that
|
|
|
|
|
* there is just the version record. We try to fix it here.
|
|
|
|
|
* If we can't do that we return 0 - this is the version
|
|
|
|
|
* record and thus the actual read will detect the mismatch
|
|
|
|
|
* and bail out. Note that create_hashtable updates VR. */
|
|
|
|
|
take_write_lock ();
|
|
|
|
|
if (lseek (db_fd, 0, SEEK_END) == TRUST_RECORD_LEN)
|
|
|
|
|
create_hashtable (ctrl, &vr, 0);
|
|
|
|
|
release_write_lock ();
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
trusthashtbl = vr.r.ver.trusthashtbl;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
return trusthashtbl;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Update a hashtable in the trustdb. TABLE gives the start of the
|
|
|
|
|
* table, KEY and KEYLEN are the key, NEWRECNUM is the record number
|
|
|
|
|
* to insert into the table.
|
|
|
|
|
*
|
|
|
|
|
* Return: 0 on success or an error code.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
static int
|
2017-03-31 20:03:52 +02:00
|
|
|
|
upd_hashtable (ctrl_t ctrl, ulong table, byte *key, int keylen, ulong newrecnum)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
TRUSTREC lastrec, rec;
|
|
|
|
|
ulong hashrec, item;
|
|
|
|
|
int msb;
|
|
|
|
|
int level = 0;
|
|
|
|
|
int rc, i;
|
|
|
|
|
|
|
|
|
|
hashrec = table;
|
|
|
|
|
next_level:
|
|
|
|
|
msb = key[level];
|
|
|
|
|
hashrec += msb / ITEMS_PER_HTBL_RECORD;
|
|
|
|
|
rc = tdbio_read_record (hashrec, &rec, RECTYPE_HTBL);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("upd_hashtable: read failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
|
|
|
|
|
if (!item) /* Insert a new item into the hash table. */
|
|
|
|
|
{
|
|
|
|
|
rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = newrecnum;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("upd_hashtable: write htbl failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else if (item != newrecnum) /* Must do an update. */
|
|
|
|
|
{
|
|
|
|
|
lastrec = rec;
|
|
|
|
|
rc = tdbio_read_record (item, &rec, 0);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("upd_hashtable: read item failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rec.rectype == RECTYPE_HTBL)
|
|
|
|
|
{
|
|
|
|
|
hashrec = item;
|
|
|
|
|
level++;
|
|
|
|
|
if (level >= keylen)
|
|
|
|
|
{
|
|
|
|
|
log_error ("hashtable has invalid indirections.\n");
|
|
|
|
|
return GPG_ERR_TRUSTDB;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
goto next_level;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else if (rec.rectype == RECTYPE_HLST) /* Extend the list. */
|
|
|
|
|
{
|
|
|
|
|
/* Check whether the key is already in this list. */
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
|
|
|
|
|
{
|
|
|
|
|
if (rec.r.hlst.rnum[i] == newrecnum)
|
|
|
|
|
{
|
|
|
|
|
return 0; /* Okay, already in the list. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rec.r.hlst.next)
|
|
|
|
|
{
|
|
|
|
|
rc = tdbio_read_record (rec.r.hlst.next, &rec, RECTYPE_HLST);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("upd_hashtable: read hlst failed: %s\n",
|
|
|
|
|
gpg_strerror (rc) );
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else
|
|
|
|
|
break; /* key is not in the list */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/* Find the next free entry and put it in. */
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
|
|
|
|
|
{
|
|
|
|
|
if (!rec.r.hlst.rnum[i])
|
|
|
|
|
{
|
|
|
|
|
/* Empty slot found. */
|
|
|
|
|
rec.r.hlst.rnum[i] = newrecnum;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("upd_hashtable: write hlst failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc; /* Done. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
if (rec.r.hlst.next)
|
|
|
|
|
{
|
|
|
|
|
/* read the next reord of the list. */
|
|
|
|
|
rc = tdbio_read_record (rec.r.hlst.next, &rec, RECTYPE_HLST);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("upd_hashtable: read hlst failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Append a new record to the list. */
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rec.r.hlst.next = item = tdbio_new_recnum (ctrl);
|
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("upd_hashtable: write hlst failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
memset (&rec, 0, sizeof rec);
|
|
|
|
|
rec.rectype = RECTYPE_HLST;
|
|
|
|
|
rec.recnum = item;
|
|
|
|
|
rec.r.hlst.rnum[0] = newrecnum;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("upd_hashtable: write ext hlst failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc; /* Done. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
} /* end loop over list slots */
|
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else if (rec.rectype == RECTYPE_TRUST) /* Insert a list record. */
|
|
|
|
|
{
|
|
|
|
|
if (rec.recnum == newrecnum)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
item = rec.recnum; /* Save number of key record. */
|
|
|
|
|
memset (&rec, 0, sizeof rec);
|
|
|
|
|
rec.rectype = RECTYPE_HLST;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rec.recnum = tdbio_new_recnum (ctrl);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rec.r.hlst.rnum[0] = item; /* Old key record */
|
|
|
|
|
rec.r.hlst.rnum[1] = newrecnum; /* and new key record */
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error( "upd_hashtable: write new hlst failed: %s\n",
|
|
|
|
|
gpg_strerror (rc) );
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
/* Update the hashtable record. */
|
|
|
|
|
lastrec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = rec.recnum;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &lastrec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (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);
|
2015-06-04 17:34:33 +02:00
|
|
|
|
if (opt.verbose > 1)
|
2017-03-31 20:03:52 +02:00
|
|
|
|
list_trustdb (ctrl, es_stderr, NULL);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
return GPG_ERR_TRUSTDB;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Drop an entry from a hashtable. TABLE gives the start of the
|
|
|
|
|
* table, KEY and KEYLEN are the key.
|
|
|
|
|
*
|
|
|
|
|
* Return: 0 on success or an error code.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
static int
|
2017-03-31 20:03:52 +02:00
|
|
|
|
drop_from_hashtable (ctrl_t ctrl, ulong table,
|
|
|
|
|
byte *key, int keylen, ulong recnum)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
TRUSTREC rec;
|
|
|
|
|
ulong hashrec, item;
|
|
|
|
|
int msb;
|
|
|
|
|
int level = 0;
|
|
|
|
|
int rc, i;
|
|
|
|
|
|
|
|
|
|
hashrec = table;
|
|
|
|
|
next_level:
|
|
|
|
|
msb = key[level];
|
|
|
|
|
hashrec += msb / ITEMS_PER_HTBL_RECORD;
|
|
|
|
|
rc = tdbio_read_record (hashrec, &rec, RECTYPE_HTBL );
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("drop_from_hashtable: read failed: %s\n", gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
|
|
|
|
|
if (!item)
|
|
|
|
|
return 0; /* Not found - forget about it. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (item == recnum) /* Table points direct to the record. */
|
|
|
|
|
{
|
|
|
|
|
rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = 0;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
log_error ("drop_from_hashtable: write htbl failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rc = tdbio_read_record (item, &rec, 0);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("drop_from_hashtable: read item failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rec.rectype == RECTYPE_HTBL)
|
|
|
|
|
{
|
|
|
|
|
hashrec = item;
|
|
|
|
|
level++;
|
|
|
|
|
if (level >= keylen)
|
|
|
|
|
{
|
|
|
|
|
log_error ("hashtable has invalid indirections.\n");
|
|
|
|
|
return GPG_ERR_TRUSTDB;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
goto next_level;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rec.rectype == RECTYPE_HLST)
|
|
|
|
|
{
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
|
|
|
|
|
{
|
|
|
|
|
if (rec.r.hlst.rnum[i] == recnum)
|
|
|
|
|
{
|
|
|
|
|
rec.r.hlst.rnum[i] = 0; /* Mark as free. */
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
log_error("drop_from_hashtable: write htbl failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rec.r.hlst.next)
|
|
|
|
|
{
|
|
|
|
|
rc = tdbio_read_record (rec.r.hlst.next, &rec, RECTYPE_HLST);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("drop_from_hashtable: read hlst failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else
|
|
|
|
|
return 0; /* Key not in table. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
log_error ("hashtbl %lu: %lu/%d points to wrong record %lu\n",
|
|
|
|
|
table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
|
|
|
|
|
return GPG_ERR_TRUSTDB;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Lookup a record via the hashtable TABLE by (KEY,KEYLEN) and return
|
|
|
|
|
* the result in REC. The return value of CMP() should be True if the
|
|
|
|
|
* record is the desired one.
|
|
|
|
|
*
|
2015-06-04 18:08:26 +02:00
|
|
|
|
* Return: 0 if found, GPG_ERR_NOT_FOUND, or another error code.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
2015-06-04 18:08:26 +02:00
|
|
|
|
static gpg_error_t
|
2015-06-04 17:25:09 +02:00
|
|
|
|
lookup_hashtable (ulong table, const byte *key, size_t keylen,
|
2011-02-04 12:57:53 +01:00
|
|
|
|
int (*cmpfnc)(const void*, const TRUSTREC *),
|
2008-03-26 10:38:42 +01:00
|
|
|
|
const void *cmpdata, TRUSTREC *rec )
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
int rc;
|
|
|
|
|
ulong hashrec, item;
|
|
|
|
|
int msb;
|
|
|
|
|
int level = 0;
|
|
|
|
|
|
2018-03-26 17:43:40 +02:00
|
|
|
|
if (!table)
|
|
|
|
|
{
|
|
|
|
|
rc = gpg_error (GPG_ERR_INV_RECORD);
|
|
|
|
|
log_error("lookup_hashtable failed: %s\n", "request for record 0");
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
hashrec = table;
|
|
|
|
|
next_level:
|
|
|
|
|
msb = key[level];
|
|
|
|
|
hashrec += msb / ITEMS_PER_HTBL_RECORD;
|
|
|
|
|
rc = tdbio_read_record (hashrec, rec, RECTYPE_HTBL);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error("lookup_hashtable failed: %s\n", gpg_strerror (rc) );
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
item = rec->r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
|
|
|
|
|
if (!item)
|
2015-06-04 18:08:26 +02:00
|
|
|
|
return gpg_error (GPG_ERR_NOT_FOUND);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rc = tdbio_read_record (item, rec, 0);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error( "hashtable read failed: %s\n", gpg_strerror (rc) );
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rec->rectype == RECTYPE_HTBL)
|
|
|
|
|
{
|
|
|
|
|
hashrec = item;
|
|
|
|
|
level++;
|
|
|
|
|
if (level >= keylen)
|
|
|
|
|
{
|
|
|
|
|
log_error ("hashtable has invalid indirections\n");
|
|
|
|
|
return GPG_ERR_TRUSTDB;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
goto next_level;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else if (rec->rectype == RECTYPE_HLST)
|
|
|
|
|
{
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
|
|
|
|
|
{
|
|
|
|
|
if (rec->r.hlst.rnum[i])
|
|
|
|
|
{
|
|
|
|
|
TRUSTREC tmp;
|
|
|
|
|
|
|
|
|
|
rc = tdbio_read_record (rec->r.hlst.rnum[i], &tmp, 0);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("lookup_hashtable: read item failed: %s\n",
|
|
|
|
|
gpg_strerror (rc));
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if ((*cmpfnc)(cmpdata, &tmp))
|
|
|
|
|
{
|
|
|
|
|
*rec = tmp;
|
|
|
|
|
return 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
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",
|
|
|
|
|
gpg_strerror (rc) );
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else
|
2015-06-04 18:08:26 +02:00
|
|
|
|
return gpg_error (GPG_ERR_NOT_FOUND);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if ((*cmpfnc)(cmpdata, rec))
|
|
|
|
|
return 0; /* really found */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 18:08:26 +02:00
|
|
|
|
return gpg_error (GPG_ERR_NOT_FOUND); /* no: not found */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Update the trust hash table TR or create the table if it does not
|
|
|
|
|
* exist.
|
|
|
|
|
*
|
|
|
|
|
* Return: 0 on success or an error code.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
static int
|
2017-03-31 20:03:52 +02:00
|
|
|
|
update_trusthashtbl (ctrl_t ctrl, TRUSTREC *tr)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2018-03-26 18:06:43 +02:00
|
|
|
|
return upd_hashtable (ctrl, get_trusthashrec (ctrl),
|
2015-06-04 17:25:09 +02:00
|
|
|
|
tr->r.trust.fingerprint, 20, tr->recnum);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Dump the trustdb record REC to stream FP.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
void
|
2015-02-19 17:22:27 +01:00
|
|
|
|
tdbio_dump_record (TRUSTREC *rec, estream_t fp)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
int i;
|
|
|
|
|
ulong rnum = rec->recnum;
|
|
|
|
|
|
|
|
|
|
es_fprintf (fp, "rec %5lu, ", rnum);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
switch (rec->rectype)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
es_fprintf (fp, "blank\n");
|
|
|
|
|
break;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
case RECTYPE_VER:
|
|
|
|
|
es_fprintf (fp,
|
2015-02-19 17:22:27 +01:00
|
|
|
|
"version, td=%lu, f=%lu, m/c/d=%d/%d/%d tm=%d mcl=%d nc=%lu (%s)\n",
|
2015-06-04 17:25:09 +02:00
|
|
|
|
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:
|
|
|
|
|
es_fprintf (fp, "free, next=%lu\n", rec->r.free.next);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_HTBL:
|
|
|
|
|
es_fprintf (fp, "htbl,");
|
|
|
|
|
for (i=0; i < ITEMS_PER_HTBL_RECORD; i++)
|
|
|
|
|
es_fprintf (fp, " %lu", rec->r.htbl.item[i]);
|
|
|
|
|
es_putc ('\n', fp);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_HLST:
|
|
|
|
|
es_fprintf (fp, "hlst, next=%lu,", rec->r.hlst.next);
|
|
|
|
|
for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
|
|
|
|
|
es_fprintf (fp, " %lu", rec->r.hlst.rnum[i]);
|
|
|
|
|
es_putc ('\n', fp);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_TRUST:
|
|
|
|
|
es_fprintf (fp, "trust ");
|
|
|
|
|
for (i=0; i < 20; i++)
|
|
|
|
|
es_fprintf (fp, "%02X", rec->r.trust.fingerprint[i]);
|
2021-11-13 20:59:17 +01:00
|
|
|
|
es_fprintf (fp, ", ot=%d, d=%d, vl=%lu, mo=%d, f=%02x\n",
|
|
|
|
|
rec->r.trust.ownertrust,
|
|
|
|
|
rec->r.trust.depth, rec->r.trust.validlist,
|
|
|
|
|
rec->r.trust.min_ownertrust, rec->r.trust.flags);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_VALID:
|
|
|
|
|
es_fprintf (fp, "valid ");
|
|
|
|
|
for (i=0; i < 20; i++)
|
|
|
|
|
es_fprintf(fp, "%02X", rec->r.valid.namehash[i]);
|
2021-11-13 20:59:17 +01:00
|
|
|
|
es_fprintf (fp, ", v=%d, next=%lu, f=%d, m=%d\n",
|
|
|
|
|
rec->r.valid.validity, rec->r.valid.next,
|
|
|
|
|
rec->r.valid.full_count, rec->r.valid.marginal_count);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
es_fprintf (fp, "unknown type %d\n", rec->rectype );
|
|
|
|
|
break;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Read the record with number RECNUM into the structure REC. If
|
|
|
|
|
* EXPECTED is not 0 reading any other record type will return an
|
|
|
|
|
* error.
|
|
|
|
|
*
|
2018-03-26 17:43:40 +02:00
|
|
|
|
* Return: 0 on success or an error code.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
int
|
2015-06-04 17:25:09 +02:00
|
|
|
|
tdbio_read_record (ulong recnum, TRUSTREC *rec, int expected)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
byte readbuf[TRUST_RECORD_LEN];
|
|
|
|
|
const byte *buf, *p;
|
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
int n, i;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (db_fd == -1)
|
|
|
|
|
open_db ();
|
|
|
|
|
|
|
|
|
|
buf = get_record_from_cache( recnum );
|
|
|
|
|
if (!buf)
|
|
|
|
|
{
|
|
|
|
|
if (lseek (db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET) == -1)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error (_("trustdb: lseek failed: %s\n"), strerror (errno));
|
|
|
|
|
return err;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
n = read (db_fd, readbuf, TRUST_RECORD_LEN);
|
|
|
|
|
if (!n)
|
|
|
|
|
{
|
2018-03-26 17:43:40 +02:00
|
|
|
|
return gpg_error (GPG_ERR_EOF);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else if (n != TRUST_RECORD_LEN)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error (_("trustdb: read failed (n=%d): %s\n"),
|
|
|
|
|
n, strerror(errno));
|
|
|
|
|
return err;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
buf = readbuf;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rec->recnum = recnum;
|
|
|
|
|
rec->dirty = 0;
|
|
|
|
|
p = buf;
|
|
|
|
|
rec->rectype = *p++;
|
|
|
|
|
if (expected && rec->rectype != expected)
|
|
|
|
|
{
|
|
|
|
|
log_error ("%lu: read expected rec type %d, got %d\n",
|
|
|
|
|
recnum, expected, rec->rectype);
|
|
|
|
|
return gpg_error (GPG_ERR_TRUSTDB);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
p++; /* Skip reserved byte. */
|
|
|
|
|
switch (rec->rectype)
|
|
|
|
|
{
|
|
|
|
|
case 0: /* unused (free) record */
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_VER: /* version record */
|
|
|
|
|
if (memcmp(buf+1, GPGEXT_GPG, 3))
|
|
|
|
|
{
|
|
|
|
|
log_error (_("%s: not a trustdb file\n"), db_name );
|
|
|
|
|
err = gpg_error (GPG_ERR_TRUSTDB);
|
|
|
|
|
}
|
2015-06-04 17:39:55 +02:00
|
|
|
|
else
|
2015-06-04 17:25:09 +02:00
|
|
|
|
{
|
2015-06-04 17:39:55 +02:00
|
|
|
|
p += 2; /* skip "gpg" */
|
|
|
|
|
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++;
|
|
|
|
|
rec->r.ver.min_cert_level = *p++;
|
|
|
|
|
p += 2;
|
2016-01-07 19:01:18 +01:00
|
|
|
|
rec->r.ver.created = buf32_to_ulong(p);
|
2015-06-04 17:39:55 +02:00
|
|
|
|
p += 4;
|
2016-01-07 19:01:18 +01:00
|
|
|
|
rec->r.ver.nextcheck = buf32_to_ulong(p);
|
2015-06-04 17:39:55 +02:00
|
|
|
|
p += 4;
|
|
|
|
|
p += 4;
|
2016-01-07 19:01:18 +01:00
|
|
|
|
p += 4;
|
|
|
|
|
rec->r.ver.firstfree = buf32_to_ulong(p);
|
|
|
|
|
p += 4;
|
|
|
|
|
p += 4;
|
|
|
|
|
rec->r.ver.trusthashtbl = buf32_to_ulong(p);
|
2015-06-04 17:39:55 +02:00
|
|
|
|
if (recnum)
|
|
|
|
|
{
|
|
|
|
|
log_error( _("%s: version record with recnum %lu\n"), db_name,
|
|
|
|
|
(ulong)recnum );
|
|
|
|
|
err = gpg_error (GPG_ERR_TRUSTDB);
|
|
|
|
|
}
|
|
|
|
|
else if (rec->r.ver.version != 3)
|
|
|
|
|
{
|
|
|
|
|
log_error( _("%s: invalid file version %d\n"), db_name,
|
|
|
|
|
rec->r.ver.version );
|
|
|
|
|
err = gpg_error (GPG_ERR_TRUSTDB);
|
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_FREE:
|
2016-01-07 19:01:18 +01:00
|
|
|
|
rec->r.free.next = buf32_to_ulong(p);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_HTBL:
|
|
|
|
|
for (i=0; i < ITEMS_PER_HTBL_RECORD; i++)
|
|
|
|
|
{
|
2016-01-07 19:01:18 +01:00
|
|
|
|
rec->r.htbl.item[i] = buf32_to_ulong(p);
|
|
|
|
|
p += 4;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_HLST:
|
2016-01-07 19:01:18 +01:00
|
|
|
|
rec->r.hlst.next = buf32_to_ulong(p);
|
|
|
|
|
p += 4;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
|
|
|
|
|
{
|
2016-01-07 19:01:18 +01:00
|
|
|
|
rec->r.hlst.rnum[i] = buf32_to_ulong(p);
|
|
|
|
|
p += 4;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_TRUST:
|
2016-01-07 19:01:18 +01:00
|
|
|
|
memcpy (rec->r.trust.fingerprint, p, 20);
|
|
|
|
|
p+=20;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rec->r.trust.ownertrust = *p++;
|
|
|
|
|
rec->r.trust.depth = *p++;
|
|
|
|
|
rec->r.trust.min_ownertrust = *p++;
|
2021-11-13 20:59:17 +01:00
|
|
|
|
rec->r.trust.flags = *p++;
|
2016-01-07 19:01:18 +01:00
|
|
|
|
rec->r.trust.validlist = buf32_to_ulong(p);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_VALID:
|
2016-01-07 19:01:18 +01:00
|
|
|
|
memcpy (rec->r.valid.namehash, p, 20);
|
|
|
|
|
p+=20;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rec->r.valid.validity = *p++;
|
2016-01-07 19:01:18 +01:00
|
|
|
|
rec->r.valid.next = buf32_to_ulong(p);
|
|
|
|
|
p += 4;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
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",
|
|
|
|
|
db_name, rec->rectype, (ulong)recnum);
|
|
|
|
|
err = gpg_error (GPG_ERR_TRUSTDB);
|
|
|
|
|
break;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
return err;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Write the record from the struct REC.
|
|
|
|
|
*
|
|
|
|
|
* Return: 0 on success or an error code.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
int
|
2017-03-31 20:03:52 +02:00
|
|
|
|
tdbio_write_record (ctrl_t ctrl, TRUSTREC *rec)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
byte buf[TRUST_RECORD_LEN];
|
|
|
|
|
byte *p;
|
|
|
|
|
int rc = 0;
|
|
|
|
|
int i;
|
|
|
|
|
ulong recnum = rec->recnum;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (db_fd == -1)
|
|
|
|
|
open_db ();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
memset (buf, 0, TRUST_RECORD_LEN);
|
|
|
|
|
p = buf;
|
|
|
|
|
*p++ = rec->rectype; p++;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
switch (rec->rectype)
|
|
|
|
|
{
|
|
|
|
|
case 0: /* unused record */
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_VER: /* version record */
|
|
|
|
|
if (recnum)
|
|
|
|
|
BUG ();
|
|
|
|
|
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++ = 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;
|
|
|
|
|
p += 4;
|
|
|
|
|
ulongtobuf(p, rec->r.ver.firstfree ); p += 4;
|
|
|
|
|
p += 4;
|
|
|
|
|
ulongtobuf(p, rec->r.ver.trusthashtbl ); p += 4;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_FREE:
|
|
|
|
|
ulongtobuf(p, rec->r.free.next); p += 4;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_HTBL:
|
|
|
|
|
for (i=0; i < ITEMS_PER_HTBL_RECORD; i++)
|
|
|
|
|
{
|
|
|
|
|
ulongtobuf( p, rec->r.htbl.item[i]); p += 4;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_HLST:
|
|
|
|
|
ulongtobuf( p, rec->r.hlst.next); p += 4;
|
|
|
|
|
for (i=0; i < ITEMS_PER_HLST_RECORD; i++ )
|
|
|
|
|
{
|
|
|
|
|
ulongtobuf( p, rec->r.hlst.rnum[i]); p += 4;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RECTYPE_TRUST:
|
|
|
|
|
memcpy (p, rec->r.trust.fingerprint, 20); p += 20;
|
|
|
|
|
*p++ = rec->r.trust.ownertrust;
|
|
|
|
|
*p++ = rec->r.trust.depth;
|
|
|
|
|
*p++ = rec->r.trust.min_ownertrust;
|
2021-11-13 20:59:17 +01:00
|
|
|
|
*p++ = rec->r.trust.flags;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
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:
|
|
|
|
|
BUG();
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rc = put_record_into_cache (recnum, buf);
|
|
|
|
|
if (rc)
|
|
|
|
|
;
|
|
|
|
|
else if (rec->rectype == RECTYPE_TRUST)
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = update_trusthashtbl (ctrl, rec);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Delete the record at record number RECNUm from the trustdb.
|
|
|
|
|
*
|
|
|
|
|
* Return: 0 on success or an error code.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
int
|
2017-03-31 20:03:52 +02:00
|
|
|
|
tdbio_delete_record (ctrl_t ctrl, ulong recnum)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
TRUSTREC vr, rec;
|
|
|
|
|
int rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Must read the record fist, so we can drop it from the hash tables */
|
|
|
|
|
rc = tdbio_read_record (recnum, &rec, 0);
|
|
|
|
|
if (rc)
|
|
|
|
|
;
|
|
|
|
|
else if (rec.rectype == RECTYPE_TRUST)
|
|
|
|
|
{
|
2018-03-26 18:06:43 +02:00
|
|
|
|
rc = drop_from_hashtable (ctrl, get_trusthashrec (ctrl),
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rec.r.trust.fingerprint, 20, rec.recnum);
|
|
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
return rc;
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
2017-04-28 03:06:33 +02:00
|
|
|
|
/* Now we can change it to a free record. */
|
2015-06-04 17:25:09 +02:00
|
|
|
|
rc = tdbio_read_record (0, &vr, RECTYPE_VER);
|
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: error reading version record: %s\n"),
|
|
|
|
|
db_name, gpg_strerror (rc));
|
|
|
|
|
|
|
|
|
|
rec.recnum = recnum;
|
|
|
|
|
rec.rectype = RECTYPE_FREE;
|
|
|
|
|
rec.r.free.next = vr.r.ver.firstfree;
|
|
|
|
|
vr.r.ver.firstfree = recnum;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (!rc)
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &vr);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Create a new record and return its record number.
|
2003-06-05 09:14:21 +02:00
|
|
|
|
*/
|
|
|
|
|
ulong
|
2017-03-31 20:03:52 +02:00
|
|
|
|
tdbio_new_recnum (ctrl_t ctrl)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
off_t offset;
|
|
|
|
|
ulong recnum;
|
|
|
|
|
TRUSTREC vr, rec;
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
/* Look for unused records. */
|
|
|
|
|
rc = tdbio_read_record (0, &vr, RECTYPE_VER);
|
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal( _("%s: error reading version record: %s\n"),
|
|
|
|
|
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)
|
2018-03-26 17:43:40 +02:00
|
|
|
|
log_fatal (_("%s: error reading free record: %s\n"),
|
|
|
|
|
db_name, gpg_strerror (rc));
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Update dir record. */
|
|
|
|
|
vr.r.ver.firstfree = rec.r.free.next;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &vr);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
2018-03-26 17:43:40 +02:00
|
|
|
|
log_fatal (_("%s: error writing dir record: %s\n"),
|
|
|
|
|
db_name, gpg_strerror (rc));
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Zero out the new record. */
|
|
|
|
|
memset (&rec, 0, sizeof rec);
|
|
|
|
|
rec.rectype = 0; /* Mark as unused record (actually already done
|
|
|
|
|
my the memset). */
|
|
|
|
|
rec.recnum = recnum;
|
2017-03-31 20:03:52 +02:00
|
|
|
|
rc = tdbio_write_record (ctrl, &rec);
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: failed to zero a record: %s\n"),
|
|
|
|
|
db_name, gpg_strerror (rc));
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else /* Not found - append a new record. */
|
|
|
|
|
{
|
|
|
|
|
offset = lseek (db_fd, 0, SEEK_END);
|
|
|
|
|
if (offset == (off_t)(-1))
|
|
|
|
|
log_fatal ("trustdb: lseek to end failed: %s\n", strerror (errno));
|
|
|
|
|
recnum = offset / TRUST_RECORD_LEN;
|
2018-03-26 17:43:40 +02:00
|
|
|
|
log_assert (recnum); /* This will never be the first record */
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* We must write a record, so that the next call to this
|
|
|
|
|
* function returns another recnum. */
|
|
|
|
|
memset (&rec, 0, sizeof rec);
|
|
|
|
|
rec.rectype = 0; /* unused record */
|
|
|
|
|
rec.recnum = recnum;
|
|
|
|
|
rc = 0;
|
|
|
|
|
if (lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET) == -1)
|
|
|
|
|
{
|
|
|
|
|
rc = gpg_error_from_syserror ();
|
|
|
|
|
log_error (_("trustdb rec %lu: lseek failed: %s\n"),
|
|
|
|
|
recnum, strerror (errno));
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
n = write (db_fd, &rec, TRUST_RECORD_LEN);
|
|
|
|
|
if (n != TRUST_RECORD_LEN)
|
|
|
|
|
{
|
|
|
|
|
rc = gpg_error_from_syserror ();
|
|
|
|
|
log_error (_("trustdb rec %lu: write failed (n=%d): %s\n"),
|
2018-03-26 17:43:40 +02:00
|
|
|
|
recnum, n, gpg_strerror (rc));
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
if (rc)
|
|
|
|
|
log_fatal (_("%s: failed to append a record: %s\n"),
|
2018-03-26 17:43:40 +02:00
|
|
|
|
db_name, gpg_strerror (rc));
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
return recnum ;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Helper function for tdbio_search_trust_byfpr. */
|
2003-06-05 09:14:21 +02:00
|
|
|
|
static int
|
2008-03-26 10:38:42 +01:00
|
|
|
|
cmp_trec_fpr ( const void *fpr, const TRUSTREC *rec )
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2008-03-26 10:38:42 +01:00
|
|
|
|
return (rec->rectype == RECTYPE_TRUST
|
|
|
|
|
&& !memcmp (rec->r.trust.fingerprint, fpr, 20));
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Given a 20 byte FINGERPRINT search its trust record and return
|
|
|
|
|
* that at REC.
|
|
|
|
|
*
|
2015-06-04 18:08:26 +02:00
|
|
|
|
* Return: 0 if found, GPG_ERR_NOT_FOUND, or another error code.
|
2015-06-04 17:25:09 +02:00
|
|
|
|
*/
|
2015-06-04 18:08:26 +02:00
|
|
|
|
gpg_error_t
|
2018-03-26 18:06:43 +02:00
|
|
|
|
tdbio_search_trust_byfpr (ctrl_t ctrl, const byte *fingerprint, TRUSTREC *rec)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
int rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/* Locate the trust record using the hash table */
|
2018-03-26 18:06:43 +02:00
|
|
|
|
rc = lookup_hashtable (get_trusthashrec (ctrl), fingerprint, 20,
|
2015-06-04 17:25:09 +02:00
|
|
|
|
cmp_trec_fpr, fingerprint, rec );
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Given a primary public key object PK search its trust record and
|
|
|
|
|
* return that at REC.
|
|
|
|
|
*
|
2015-06-04 18:08:26 +02:00
|
|
|
|
* Return: 0 if found, GPG_ERR_NOT_FOUND, or another error code.
|
2015-06-04 17:25:09 +02:00
|
|
|
|
*/
|
2015-06-04 18:08:26 +02:00
|
|
|
|
gpg_error_t
|
2018-03-26 18:06:43 +02:00
|
|
|
|
tdbio_search_trust_bypk (ctrl_t ctrl, PKT_public_key *pk, TRUSTREC *rec)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2015-06-04 17:25:09 +02:00
|
|
|
|
byte fingerprint[MAX_FINGERPRINT_LEN];
|
|
|
|
|
size_t fingerlen;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
fingerprint_from_pk( pk, fingerprint, &fingerlen );
|
|
|
|
|
for (; fingerlen < 20; fingerlen++)
|
|
|
|
|
fingerprint[fingerlen] = 0;
|
2018-03-26 18:06:43 +02:00
|
|
|
|
return tdbio_search_trust_byfpr (ctrl, fingerprint, rec);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Terminate the process with a message about a corrupted trustdb.
|
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
void
|
2015-06-04 17:25:09 +02:00
|
|
|
|
tdbio_invalid (void)
|
2003-06-05 09:14:21 +02:00
|
|
|
|
{
|
2009-07-22 11:43:10 +02:00
|
|
|
|
log_error (_("Error: The trustdb is corrupted.\n"));
|
|
|
|
|
how_to_fix_the_trustdb ();
|
|
|
|
|
g10_exit (2);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
}
|