1999-03-11 15:42:06 +00:00
|
|
|
/* tdbdump.c
|
2006-04-19 11:26:11 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
1999-03-11 15:42:06 +00: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 19:49:40 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1999-03-11 15:42:06 +00: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/>.
|
1999-03-11 15:42:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
#include "gpg.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/status.h"
|
|
|
|
#include "../common/iobuf.h"
|
1999-03-11 15:42:06 +00:00
|
|
|
#include "keydb.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/util.h"
|
1999-03-11 15:42:06 +00:00
|
|
|
#include "trustdb.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "main.h"
|
2017-03-07 20:21:23 +09:00
|
|
|
#include "../common/i18n.h"
|
1999-03-11 15:42:06 +00:00
|
|
|
#include "tdbio.h"
|
|
|
|
|
|
|
|
|
1999-11-19 16:11:37 +00:00
|
|
|
#define HEXTOBIN(x) ( (x) >= '0' && (x) <= '9' ? ((x)-'0') : \
|
|
|
|
(x) >= 'A' && (x) <= 'F' ? ((x)-'A'+10) : ((x)-'a'+10))
|
1999-03-11 15:42:06 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
|
2015-06-04 17:25:09 +02:00
|
|
|
/*
|
|
|
|
* Write a record; die on error.
|
1999-03-11 15:42:06 +00:00
|
|
|
*/
|
|
|
|
static void
|
2017-03-31 20:03:52 +02:00
|
|
|
write_record (ctrl_t ctrl, TRUSTREC *rec)
|
1999-03-11 15:42:06 +00:00
|
|
|
{
|
2017-03-31 20:03:52 +02:00
|
|
|
int rc = tdbio_write_record (ctrl, rec);
|
1999-03-11 15:42:06 +00:00
|
|
|
if( !rc )
|
|
|
|
return;
|
|
|
|
log_error(_("trust record %lu, type %d: write failed: %s\n"),
|
2015-01-22 12:06:11 +01:00
|
|
|
rec->recnum, rec->rectype, gpg_strerror (rc) );
|
1999-03-11 15:42:06 +00:00
|
|
|
tdbio_invalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-04 17:34:33 +02:00
|
|
|
/*
|
|
|
|
* Dump the entire trustdb to FP or only the entries of one key.
|
1999-03-11 15:42:06 +00:00
|
|
|
*/
|
|
|
|
void
|
2017-03-31 20:03:52 +02:00
|
|
|
list_trustdb (ctrl_t ctrl, estream_t fp, const char *username)
|
1999-03-11 15:42:06 +00:00
|
|
|
{
|
2008-10-20 13:53:23 +00:00
|
|
|
TRUSTREC rec;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2008-10-20 13:53:23 +00:00
|
|
|
(void)username;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2017-03-31 20:03:52 +02:00
|
|
|
init_trustdb (ctrl, 0);
|
2008-10-20 13:53:23 +00:00
|
|
|
/* For now we ignore the user ID. */
|
|
|
|
if (1)
|
|
|
|
{
|
|
|
|
ulong recnum;
|
|
|
|
int i;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2015-06-04 17:34:33 +02:00
|
|
|
es_fprintf (fp, "TrustDB: %s\n", tdbio_get_dbname ());
|
|
|
|
for (i = 9 + strlen (tdbio_get_dbname()); i > 0; i-- )
|
|
|
|
es_fputc ('-', fp);
|
|
|
|
es_putc ('\n', fp);
|
|
|
|
for (recnum=0; !tdbio_read_record (recnum, &rec, 0); recnum++)
|
|
|
|
tdbio_dump_record (&rec, fp);
|
1999-03-11 15:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Print a list of all defined owner trust value.
|
|
|
|
*/
|
|
|
|
void
|
2017-03-31 20:03:52 +02:00
|
|
|
export_ownertrust (ctrl_t ctrl)
|
1999-03-11 15:42:06 +00:00
|
|
|
{
|
2015-02-19 17:22:27 +01:00
|
|
|
TRUSTREC rec;
|
|
|
|
ulong recnum;
|
|
|
|
int i;
|
|
|
|
byte *p;
|
1999-03-11 15:42:06 +00:00
|
|
|
|
2017-03-31 20:03:52 +02:00
|
|
|
init_trustdb (ctrl, 0);
|
2015-02-19 17:22:27 +01:00
|
|
|
es_printf (_("# List of assigned trustvalues, created %s\n"
|
|
|
|
"# (Use \"gpg --import-ownertrust\" to restore them)\n"),
|
|
|
|
asctimestamp( make_timestamp() ) );
|
|
|
|
for (recnum=0; !tdbio_read_record (recnum, &rec, 0); recnum++ )
|
|
|
|
{
|
|
|
|
if (rec.rectype == RECTYPE_TRUST)
|
|
|
|
{
|
|
|
|
if (!rec.r.trust.ownertrust)
|
|
|
|
continue;
|
|
|
|
p = rec.r.trust.fingerprint;
|
|
|
|
for (i=0; i < 20; i++, p++ )
|
|
|
|
es_printf("%02X", *p );
|
|
|
|
es_printf (":%u:\n", (unsigned int)rec.r.trust.ownertrust );
|
1999-03-11 15:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-03-31 20:03:52 +02:00
|
|
|
import_ownertrust (ctrl_t ctrl, const char *fname )
|
1999-03-11 15:42:06 +00:00
|
|
|
{
|
2015-02-19 17:22:27 +01:00
|
|
|
estream_t fp;
|
1999-03-11 15:42:06 +00:00
|
|
|
int is_stdin=0;
|
|
|
|
char line[256];
|
|
|
|
char *p;
|
|
|
|
size_t n, fprlen;
|
2002-06-29 13:46:34 +00:00
|
|
|
unsigned int otrust;
|
|
|
|
byte fpr[20];
|
|
|
|
int any = 0;
|
|
|
|
int rc;
|
1999-03-11 15:42:06 +00:00
|
|
|
|
2017-03-31 20:03:52 +02:00
|
|
|
init_trustdb (ctrl, 0);
|
2006-04-19 11:26:11 +00:00
|
|
|
if( iobuf_is_pipe_filename (fname) ) {
|
2015-02-19 17:22:27 +01:00
|
|
|
fp = es_stdin;
|
1999-03-11 15:42:06 +00:00
|
|
|
fname = "[stdin]";
|
|
|
|
is_stdin = 1;
|
|
|
|
}
|
2015-02-19 17:22:27 +01:00
|
|
|
else if( !(fp = es_fopen( fname, "r" )) ) {
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error ( _("can't open '%s': %s\n"), fname, strerror(errno) );
|
1999-03-11 15:42:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-19 17:22:27 +01:00
|
|
|
if (is_secured_file (es_fileno (fp)))
|
2006-04-19 11:26:11 +00:00
|
|
|
{
|
2015-02-19 17:22:27 +01:00
|
|
|
es_fclose (fp);
|
2010-04-01 13:24:55 +00:00
|
|
|
gpg_err_set_errno (EPERM);
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("can't open '%s': %s\n"), fname, strerror(errno) );
|
2006-04-19 11:26:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-19 17:22:27 +01:00
|
|
|
while (es_fgets (line, DIM(line)-1, fp)) {
|
1999-03-11 15:42:06 +00:00
|
|
|
TRUSTREC rec;
|
|
|
|
|
|
|
|
if( !*line || *line == '#' )
|
|
|
|
continue;
|
|
|
|
n = strlen(line);
|
|
|
|
if( line[n-1] != '\n' ) {
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("error in '%s': %s\n"), fname, _("line too long") );
|
1999-03-11 15:42:06 +00:00
|
|
|
/* ... or last line does not have a LF */
|
|
|
|
break; /* can't continue */
|
|
|
|
}
|
|
|
|
for(p = line; *p && *p != ':' ; p++ )
|
2006-04-19 11:26:11 +00:00
|
|
|
if( !hexdigitp(p) )
|
1999-03-11 15:42:06 +00:00
|
|
|
break;
|
|
|
|
if( *p != ':' ) {
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("error in '%s': %s\n"), fname, _("colon missing") );
|
1999-03-11 15:42:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fprlen = p - line;
|
|
|
|
if( fprlen != 32 && fprlen != 40 ) {
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("error in '%s': %s\n"),
|
2006-04-19 11:26:11 +00:00
|
|
|
fname, _("invalid fingerprint") );
|
1999-03-11 15:42:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if( sscanf(p, ":%u:", &otrust ) != 1 ) {
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("error in '%s': %s\n"),
|
2006-04-19 11:26:11 +00:00
|
|
|
fname, _("ownertrust value missing"));
|
1999-03-11 15:42:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if( !otrust )
|
|
|
|
continue; /* no otrust defined - no need to update or insert */
|
|
|
|
/* convert the ascii fingerprint to binary */
|
2002-06-29 13:46:34 +00:00
|
|
|
for(p=line, fprlen=0; fprlen < 20 && *p != ':'; p += 2 )
|
|
|
|
fpr[fprlen++] = HEXTOBIN(p[0]) * 16 + HEXTOBIN(p[1]);
|
|
|
|
while (fprlen < 20)
|
|
|
|
fpr[fprlen++] = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = tdbio_search_trust_byfpr (fpr, &rec);
|
1999-03-11 15:42:06 +00:00
|
|
|
if( !rc ) { /* found: update */
|
2002-06-29 13:46:34 +00:00
|
|
|
if (rec.r.trust.ownertrust != otrust)
|
|
|
|
{
|
2017-02-25 18:02:27 -05:00
|
|
|
if (!opt.quiet)
|
|
|
|
{
|
|
|
|
if( rec.r.trust.ownertrust )
|
|
|
|
log_info("changing ownertrust from %u to %u\n",
|
|
|
|
rec.r.trust.ownertrust, otrust );
|
|
|
|
else
|
|
|
|
log_info("setting ownertrust to %u\n", otrust );
|
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
rec.r.trust.ownertrust = otrust;
|
2017-03-31 20:03:52 +02:00
|
|
|
write_record (ctrl, &rec);
|
2002-06-29 13:46:34 +00:00
|
|
|
any = 1;
|
|
|
|
}
|
1999-03-11 15:42:06 +00:00
|
|
|
}
|
2015-06-04 18:08:26 +02:00
|
|
|
else if (gpg_err_code (rc) == GPG_ERR_NOT_FOUND) { /* insert */
|
2017-02-25 18:02:27 -05:00
|
|
|
if (!opt.quiet)
|
|
|
|
log_info("inserting ownertrust of %u\n", otrust );
|
2002-06-29 13:46:34 +00:00
|
|
|
memset (&rec, 0, sizeof rec);
|
2017-03-31 20:03:52 +02:00
|
|
|
rec.recnum = tdbio_new_recnum (ctrl);
|
2002-06-29 13:46:34 +00:00
|
|
|
rec.rectype = RECTYPE_TRUST;
|
|
|
|
memcpy (rec.r.trust.fingerprint, fpr, 20);
|
|
|
|
rec.r.trust.ownertrust = otrust;
|
2017-03-31 20:03:52 +02:00
|
|
|
write_record (ctrl, &rec);
|
2002-06-29 13:46:34 +00:00
|
|
|
any = 1;
|
1999-03-11 15:42:06 +00:00
|
|
|
}
|
|
|
|
else /* error */
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error (_("error finding trust record in '%s': %s\n"),
|
2015-01-22 12:06:11 +01:00
|
|
|
fname, gpg_strerror (rc));
|
1999-03-11 15:42:06 +00:00
|
|
|
}
|
2015-02-19 17:22:27 +01:00
|
|
|
if (es_ferror (fp))
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error ( _("read error in '%s': %s\n"), fname, strerror(errno) );
|
2015-02-19 17:22:27 +01:00
|
|
|
if (!is_stdin)
|
|
|
|
es_fclose (fp);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
if (any)
|
|
|
|
{
|
2017-03-31 20:03:52 +02:00
|
|
|
revalidation_mark (ctrl);
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = tdbio_sync ();
|
|
|
|
if (rc)
|
2015-01-22 12:06:11 +01:00
|
|
|
log_error (_("trustdb: sync failed: %s\n"), gpg_strerror (rc) );
|
2002-06-29 13:46:34 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
}
|