2003-08-05 19:11:04 +02:00
|
|
|
/* kbxutil.c - The Keybox utility
|
2011-04-28 20:21:14 +02:00
|
|
|
* Copyright (C) 2000, 2001, 2004, 2007, 2011 Free Software Foundation, Inc.
|
2003-08-05 19:11:04 +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-08-05 19:11:04 +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/>.
|
2020-02-21 20:28:47 +01:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2003-08-05 19:11:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2020-02-21 20:28:47 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2004-08-24 21:55:47 +02:00
|
|
|
#include <sys/stat.h>
|
2003-08-05 19:11:04 +02:00
|
|
|
#include <unistd.h>
|
2007-08-23 19:41:22 +02:00
|
|
|
#include <limits.h>
|
2004-08-24 21:55:47 +02:00
|
|
|
#include <assert.h>
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2022-09-14 09:49:31 +02:00
|
|
|
#include "keybox-defs.h"
|
2010-03-10 13:24:58 +01:00
|
|
|
#include "../common/logging.h"
|
|
|
|
#include "../common/stringhelp.h"
|
|
|
|
#include "../common/utf8conv.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/i18n.h"
|
2012-02-06 20:50:47 +01:00
|
|
|
#include "../common/init.h"
|
2003-08-05 19:11:04 +02:00
|
|
|
#include <gcrypt.h>
|
|
|
|
|
2012-02-06 20:50:47 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
enum cmd_and_opt_values {
|
|
|
|
aNull = 0,
|
|
|
|
oArmor = 'a',
|
|
|
|
oDryRun = 'n',
|
|
|
|
oOutput = 'o',
|
|
|
|
oQuiet = 'q',
|
|
|
|
oVerbose = 'v',
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
aNoSuchCmd = 500, /* force other values not to be a letter */
|
|
|
|
aFindByFpr,
|
|
|
|
aFindByKid,
|
|
|
|
aFindByUid,
|
2004-06-18 11:53:56 +02:00
|
|
|
aStats,
|
2004-08-24 21:55:47 +02:00
|
|
|
aImportOpenPGP,
|
2007-08-23 19:41:22 +02:00
|
|
|
aFindDups,
|
|
|
|
aCut,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
oDebug,
|
|
|
|
oDebugAll,
|
|
|
|
|
|
|
|
oNoArmor,
|
2007-08-23 19:41:22 +02:00
|
|
|
oFrom,
|
|
|
|
oTo,
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
aTest
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-02-21 20:28:47 +01:00
|
|
|
static gpgrt_opt_t opts[] = {
|
2003-08-05 19:11:04 +02:00
|
|
|
{ 300, NULL, 0, N_("@Commands:\n ") },
|
|
|
|
|
2004-06-18 11:53:56 +02:00
|
|
|
/* { aFindByFpr, "find-by-fpr", 0, "|FPR| find key using it's fingerprnt" }, */
|
|
|
|
/* { aFindByKid, "find-by-kid", 0, "|KID| find key using it's keyid" }, */
|
|
|
|
/* { aFindByUid, "find-by-uid", 0, "|NAME| find key by user name" }, */
|
2011-02-04 12:57:53 +01:00
|
|
|
{ aStats, "stats", 0, "show key statistics" },
|
2004-08-24 21:55:47 +02:00
|
|
|
{ aImportOpenPGP, "import-openpgp", 0, "import OpenPGP keyblocks"},
|
2007-08-23 19:41:22 +02:00
|
|
|
{ aFindDups, "find-dups", 0, "find duplicates" },
|
|
|
|
{ aCut, "cut", 0, "export records" },
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
{ 301, NULL, 0, N_("@\nOptions:\n ") },
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-08-23 19:41:22 +02:00
|
|
|
{ oFrom, "from", 4, "|N|first record to export" },
|
|
|
|
{ oTo, "to", 4, "|N|last record to export" },
|
2004-06-18 11:53:56 +02:00
|
|
|
/* { oArmor, "armor", 0, N_("create ascii armored output")}, */
|
|
|
|
/* { oArmor, "armour", 0, "@" }, */
|
|
|
|
/* { oOutput, "output", 2, N_("use as output file")}, */
|
2003-08-05 19:11:04 +02:00
|
|
|
{ oVerbose, "verbose", 0, N_("verbose") },
|
|
|
|
{ oQuiet, "quiet", 0, N_("be somewhat more quiet") },
|
|
|
|
{ oDryRun, "dry-run", 0, N_("do not make any changes") },
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
{ oDebug, "debug" ,4|16, N_("set debugging flags")},
|
|
|
|
{ oDebugAll, "debug-all" ,0, N_("enable full debugging")},
|
|
|
|
|
2017-07-19 06:41:18 +02:00
|
|
|
ARGPARSE_end () /* end of list */
|
2003-08-05 19:11:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void myexit (int rc);
|
|
|
|
|
|
|
|
int keybox_errors_seen = 0;
|
|
|
|
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
my_strusage( int level )
|
|
|
|
{
|
2020-02-21 20:28:47 +01:00
|
|
|
const char *p;
|
|
|
|
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case 9: p = "GPL-3.0-or-later"; break;
|
|
|
|
case 11: p = "kbxutil (@GNUPG@)";
|
|
|
|
break;
|
|
|
|
case 13: p = VERSION; break;
|
|
|
|
case 14: p = GNUPG_DEF_COPYRIGHT_LINE; break;
|
|
|
|
case 17: p = PRINTABLE_OS_NAME; break;
|
|
|
|
case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
case 40: p =
|
|
|
|
_("Usage: kbxutil [options] [files] (-h for help)");
|
|
|
|
break;
|
|
|
|
case 41: p =
|
|
|
|
_("Syntax: kbxutil [options] [files]\n"
|
|
|
|
"List, export, import Keybox data\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default: p = NULL;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2020-02-21 20:28:47 +01:00
|
|
|
return p;
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* static void */
|
|
|
|
/* wrong_args( const char *text ) */
|
|
|
|
/* { */
|
|
|
|
/* log_error("usage: kbxutil %s\n", text); */
|
|
|
|
/* myexit ( 1 ); */
|
|
|
|
/* } */
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int
|
|
|
|
hextobyte( const byte *s )
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if( *s >= '0' && *s <= '9' )
|
|
|
|
c = 16 * (*s - '0');
|
|
|
|
else if( *s >= 'A' && *s <= 'F' )
|
|
|
|
c = 16 * (10 + *s - 'A');
|
|
|
|
else if( *s >= 'a' && *s <= 'f' )
|
|
|
|
c = 16 * (10 + *s - 'a');
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
s++;
|
|
|
|
if( *s >= '0' && *s <= '9' )
|
|
|
|
c += *s - '0';
|
|
|
|
else if( *s >= 'A' && *s <= 'F' )
|
|
|
|
c += 10 + *s - 'A';
|
|
|
|
else if( *s >= 'a' && *s <= 'f' )
|
|
|
|
c += 10 + *s - 'a';
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static char *
|
|
|
|
format_fingerprint ( const char *s )
|
|
|
|
{
|
|
|
|
int i, c;
|
|
|
|
byte fpr[20];
|
|
|
|
|
|
|
|
for (i=0; i < 20 && *s; ) {
|
|
|
|
if ( *s == ' ' || *s == '\t' ) {
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c = hextobyte(s);
|
|
|
|
if (c == -1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fpr[i++] = c;
|
|
|
|
s += 2;
|
|
|
|
}
|
|
|
|
return gcry_xstrdup ( fpr );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int
|
|
|
|
format_keyid ( const char *s, u32 *kid )
|
|
|
|
{
|
|
|
|
char helpbuf[9];
|
|
|
|
switch ( strlen ( s ) ) {
|
|
|
|
case 8:
|
|
|
|
kid[0] = 0;
|
|
|
|
kid[1] = strtoul( s, NULL, 16 );
|
|
|
|
return 10;
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
mem2str( helpbuf, s, 9 );
|
|
|
|
kid[0] = strtoul( helpbuf, NULL, 16 );
|
|
|
|
kid[1] = strtoul( s+8, NULL, 16 );
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
return 0; /* error */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-08-24 21:55:47 +02:00
|
|
|
static char *
|
|
|
|
read_file (const char *fname, size_t *r_length)
|
|
|
|
{
|
2020-11-11 15:22:51 +01:00
|
|
|
estream_t fp;
|
2004-08-24 21:55:47 +02:00
|
|
|
char *buf;
|
|
|
|
size_t buflen;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-08-24 21:55:47 +02:00
|
|
|
if (!strcmp (fname, "-"))
|
|
|
|
{
|
|
|
|
size_t nread, bufsize = 0;
|
|
|
|
|
2020-11-11 15:22:51 +01:00
|
|
|
fp = es_stdin;
|
2004-08-24 21:55:47 +02:00
|
|
|
buf = NULL;
|
|
|
|
buflen = 0;
|
|
|
|
#define NCHUNK 8192
|
2011-02-04 12:57:53 +01:00
|
|
|
do
|
2004-08-24 21:55:47 +02:00
|
|
|
{
|
|
|
|
bufsize += NCHUNK;
|
|
|
|
if (!buf)
|
|
|
|
buf = xtrymalloc (bufsize);
|
|
|
|
else
|
|
|
|
buf = xtryrealloc (buf, bufsize);
|
|
|
|
if (!buf)
|
|
|
|
log_fatal ("can't allocate buffer: %s\n", strerror (errno));
|
|
|
|
|
2020-11-11 15:22:51 +01:00
|
|
|
nread = es_fread (buf+buflen, 1, NCHUNK, fp);
|
|
|
|
if (nread < NCHUNK && es_ferror (fp))
|
2004-08-24 21:55:47 +02:00
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error ("error reading '[stdin]': %s\n", strerror (errno));
|
2004-08-24 21:55:47 +02:00
|
|
|
xfree (buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buflen += nread;
|
|
|
|
}
|
|
|
|
while (nread == NCHUNK);
|
|
|
|
#undef NCHUNK
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2020-11-11 15:22:51 +01:00
|
|
|
fp = es_fopen (fname, "rb");
|
2004-08-24 21:55:47 +02:00
|
|
|
if (!fp)
|
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error ("can't open '%s': %s\n", fname, strerror (errno));
|
2004-08-24 21:55:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2020-11-11 15:22:51 +01:00
|
|
|
if (fstat (es_fileno(fp), &st))
|
2004-08-24 21:55:47 +02:00
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error ("can't stat '%s': %s\n", fname, strerror (errno));
|
2020-11-11 15:22:51 +01:00
|
|
|
es_fclose (fp);
|
2004-08-24 21:55:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-08-24 21:55:47 +02:00
|
|
|
buflen = st.st_size;
|
|
|
|
buf = xtrymalloc (buflen+1);
|
|
|
|
if (!buf)
|
|
|
|
log_fatal ("can't allocate buffer: %s\n", strerror (errno));
|
2020-11-11 15:22:51 +01:00
|
|
|
if (es_fread (buf, buflen, 1, fp) != 1)
|
2004-08-24 21:55:47 +02:00
|
|
|
{
|
2012-06-05 19:29:22 +02:00
|
|
|
log_error ("error reading '%s': %s\n", fname, strerror (errno));
|
2020-11-11 15:22:51 +01:00
|
|
|
es_fclose (fp);
|
2004-08-24 21:55:47 +02:00
|
|
|
xfree (buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-11-11 15:22:51 +01:00
|
|
|
es_fclose (fp);
|
2004-08-24 21:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*r_length = buflen;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_fpr (const unsigned char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i < len; i++, buffer++)
|
|
|
|
{
|
|
|
|
if (len == 20)
|
|
|
|
{
|
|
|
|
if (i == 10)
|
|
|
|
putchar (' ');
|
|
|
|
printf (" %02X%02X", buffer[0], buffer[1]);
|
|
|
|
i++; buffer++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (i && !(i % 8))
|
|
|
|
putchar (' ');
|
|
|
|
printf (" %02X", buffer[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-29 19:52:08 +01:00
|
|
|
static void
|
|
|
|
dump_grip (const unsigned char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i < len; i++, buffer++)
|
|
|
|
{
|
|
|
|
printf ("%02X", buffer[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-24 21:55:47 +02:00
|
|
|
static void
|
|
|
|
dump_openpgp_key (keybox_openpgp_info_t info, const unsigned char *image)
|
|
|
|
{
|
2012-12-28 13:45:41 +01:00
|
|
|
printf ("pub %2d %02X%02X%02X%02X",
|
|
|
|
info->primary.algo,
|
2004-08-24 21:55:47 +02:00
|
|
|
info->primary.keyid[4], info->primary.keyid[5],
|
|
|
|
info->primary.keyid[6], info->primary.keyid[7] );
|
|
|
|
dump_fpr (info->primary.fpr, info->primary.fprlen);
|
|
|
|
putchar ('\n');
|
2019-01-29 19:52:08 +01:00
|
|
|
fputs ("grp ", stdout);
|
|
|
|
dump_grip (info->primary.grip, 20);
|
|
|
|
putchar ('\n');
|
2004-08-24 21:55:47 +02:00
|
|
|
if (info->nsubkeys)
|
|
|
|
{
|
|
|
|
struct _keybox_openpgp_key_info *k;
|
|
|
|
|
|
|
|
k = &info->subkeys;
|
2011-02-04 12:57:53 +01:00
|
|
|
do
|
2004-08-24 21:55:47 +02:00
|
|
|
{
|
2012-12-28 13:45:41 +01:00
|
|
|
printf ("sub %2d %02X%02X%02X%02X",
|
|
|
|
k->algo,
|
2004-08-24 21:55:47 +02:00
|
|
|
k->keyid[4], k->keyid[5],
|
|
|
|
k->keyid[6], k->keyid[7] );
|
|
|
|
dump_fpr (k->fpr, k->fprlen);
|
|
|
|
putchar ('\n');
|
2019-01-29 19:52:08 +01:00
|
|
|
fputs ("grp ", stdout);
|
|
|
|
dump_grip (k->grip, 20);
|
|
|
|
putchar ('\n');
|
2004-08-24 21:55:47 +02:00
|
|
|
k = k->next;
|
|
|
|
}
|
|
|
|
while (k);
|
|
|
|
}
|
|
|
|
if (info->nuids)
|
|
|
|
{
|
|
|
|
struct _keybox_openpgp_uid_info *u;
|
|
|
|
|
|
|
|
u = &info->uids;
|
2011-02-04 12:57:53 +01:00
|
|
|
do
|
2004-08-24 21:55:47 +02:00
|
|
|
{
|
2006-11-20 17:49:41 +01:00
|
|
|
printf ("uid\t\t%.*s\n", (int)u->len, image + u->off);
|
2004-08-24 21:55:47 +02:00
|
|
|
u = u->next;
|
|
|
|
}
|
|
|
|
while (u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2012-12-28 14:03:16 +01:00
|
|
|
import_openpgp (const char *filename, int dryrun)
|
2004-08-24 21:55:47 +02:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
char *buffer;
|
|
|
|
size_t buflen, nparsed;
|
|
|
|
unsigned char *p;
|
|
|
|
struct _keybox_openpgp_info info;
|
2012-12-28 14:03:16 +01:00
|
|
|
KEYBOXBLOB blob;
|
2004-08-24 21:55:47 +02:00
|
|
|
|
|
|
|
buffer = read_file (filename, &buflen);
|
|
|
|
if (!buffer)
|
|
|
|
return;
|
2005-06-16 10:12:03 +02:00
|
|
|
p = (unsigned char *)buffer;
|
2004-08-24 21:55:47 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
err = _keybox_parse_openpgp (p, buflen, &nparsed, &info);
|
|
|
|
assert (nparsed <= buflen);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
if (gpg_err_code (err) == GPG_ERR_NO_DATA)
|
|
|
|
break;
|
2011-04-28 20:21:14 +02:00
|
|
|
if (gpg_err_code (err) == GPG_ERR_UNSUPPORTED_ALGORITHM)
|
|
|
|
{
|
|
|
|
/* This is likely a v3 key packet with a non-RSA
|
|
|
|
algorithm. These are keys from very early versions
|
|
|
|
of GnuPG (pre-OpenPGP). */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fflush (es_stdout);
|
2011-04-28 20:21:14 +02:00
|
|
|
log_info ("%s: failed to parse OpenPGP keyblock: %s\n",
|
|
|
|
filename, gpg_strerror (err));
|
|
|
|
}
|
2004-08-24 21:55:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-28 14:03:16 +01:00
|
|
|
if (dryrun)
|
|
|
|
dump_openpgp_key (&info, p);
|
|
|
|
else
|
|
|
|
{
|
2017-03-30 15:18:45 +02:00
|
|
|
err = _keybox_create_openpgp_blob (&blob, &info, p, nparsed, 0);
|
2012-12-28 14:03:16 +01:00
|
|
|
if (err)
|
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fflush (es_stdout);
|
2012-12-28 14:03:16 +01:00
|
|
|
log_error ("%s: failed to create OpenPGP keyblock: %s\n",
|
|
|
|
filename, gpg_strerror (err));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
err = _keybox_write_blob (blob, es_stdout, NULL);
|
2012-12-28 14:03:16 +01:00
|
|
|
_keybox_release_blob (blob);
|
|
|
|
if (err)
|
|
|
|
{
|
2020-10-20 11:52:16 +02:00
|
|
|
es_fflush (es_stdout);
|
2012-12-28 14:03:16 +01:00
|
|
|
log_error ("%s: failed to write OpenPGP keyblock: %s\n",
|
|
|
|
filename, gpg_strerror (err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-24 21:55:47 +02:00
|
|
|
_keybox_destroy_openpgp_info (&info);
|
|
|
|
}
|
|
|
|
p += nparsed;
|
|
|
|
buflen -= nparsed;
|
|
|
|
}
|
|
|
|
xfree (buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
int
|
2020-02-21 20:28:47 +01:00
|
|
|
main (int argc, char **argv)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2020-02-21 20:28:47 +01:00
|
|
|
gpgrt_argparse_t pargs;
|
2003-08-05 19:11:04 +02:00
|
|
|
enum cmd_and_opt_values cmd = 0;
|
2007-08-23 19:41:22 +02:00
|
|
|
unsigned long from = 0, to = ULONG_MAX;
|
2012-12-28 14:03:16 +01:00
|
|
|
int dry_run = 0;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2015-01-28 19:57:22 +01:00
|
|
|
early_system_init ();
|
2020-02-21 20:28:47 +01:00
|
|
|
gpgrt_set_strusage( my_strusage );
|
2004-08-24 21:55:47 +02:00
|
|
|
gcry_control (GCRYCTL_DISABLE_SECMEM);
|
Call log_set_prefix() with human-readable labels.
* agent/preset-passphrase.c, agent/protect-tool.c, dirmngr/dirmngr.c
* dirmngr/t-http.c, g10/gpg.c, g10/gpgv.c, g13/g13-syshelp.c
* g13/g13.c, kbx/kbxutil.c, scd/scdaemon.c, sm/gpgsm.c
* tests/gpgscm/main.c, tools/gpg-check-pattern.c
* tools/gpg-connect-agent.c, tools/gpgconf.c, tools/gpgtar.c
* tools/symcryptrun.c: Invoke log_set_prefix() with
human-readable labels.
--
Some invocations of log_set_prefix() were done with raw numeric values
instead of values that humans can understand. Use symbolic
representations instead of numeric for better readability.
Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
2016-08-12 07:37:57 +02:00
|
|
|
log_set_prefix ("kbxutil", GPGRT_LOG_WITH_PREFIX);
|
2007-06-14 19:05:07 +02:00
|
|
|
|
|
|
|
/* Make sure that our subsystems are ready. */
|
2004-08-24 21:55:47 +02:00
|
|
|
i18n_init ();
|
2010-03-22 13:46:05 +01:00
|
|
|
init_common_subsystems (&argc, &argv);
|
2024-05-16 09:34:00 +02:00
|
|
|
setup_libgcrypt_logging ();
|
2004-08-24 21:55:47 +02:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
/*create_dotlock(NULL); register locking cleanup */
|
|
|
|
|
2010-03-10 13:24:58 +01:00
|
|
|
/* We need to use the gcry malloc function because jnlib uses them. */
|
2003-08-05 19:11:04 +02:00
|
|
|
ksba_set_malloc_hooks (gcry_malloc, gcry_realloc, gcry_free );
|
|
|
|
|
|
|
|
|
|
|
|
pargs.argc = &argc;
|
|
|
|
pargs.argv = &argv;
|
2020-02-21 20:28:47 +01:00
|
|
|
pargs.flags= ARGPARSE_FLAG_KEEP;
|
|
|
|
while (gpgrt_argparse (NULL,&pargs, opts))
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
switch (pargs.r_opt)
|
|
|
|
{
|
|
|
|
case oVerbose:
|
|
|
|
/*opt.verbose++;*/
|
|
|
|
/*gcry_control( GCRYCTL_SET_VERBOSITY, (int)opt.verbose );*/
|
|
|
|
break;
|
|
|
|
case oDebug:
|
|
|
|
/*opt.debug |= pargs.r.ret_ulong; */
|
|
|
|
break;
|
|
|
|
case oDebugAll:
|
|
|
|
/*opt.debug = ~0;*/
|
|
|
|
break;
|
|
|
|
|
|
|
|
case aFindByFpr:
|
|
|
|
case aFindByKid:
|
|
|
|
case aFindByUid:
|
2004-06-18 11:53:56 +02:00
|
|
|
case aStats:
|
2004-08-24 21:55:47 +02:00
|
|
|
case aImportOpenPGP:
|
2007-08-23 19:41:22 +02:00
|
|
|
case aFindDups:
|
|
|
|
case aCut:
|
2003-08-05 19:11:04 +02:00
|
|
|
cmd = pargs.r_opt;
|
|
|
|
break;
|
2004-08-24 21:55:47 +02:00
|
|
|
|
2007-08-23 19:41:22 +02:00
|
|
|
case oFrom: from = pargs.r.ret_ulong; break;
|
|
|
|
case oTo: to = pargs.r.ret_ulong; break;
|
|
|
|
|
2012-12-28 14:03:16 +01:00
|
|
|
case oDryRun: dry_run = 1; break;
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
default:
|
|
|
|
pargs.err = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-21 20:28:47 +01:00
|
|
|
gpgrt_argparse (NULL, &pargs, NULL);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2007-08-23 19:41:22 +02:00
|
|
|
if (to < from)
|
|
|
|
log_error ("record number of \"--to\" is lower than \"--from\" one\n");
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
if (log_get_errorcount(0) )
|
|
|
|
myexit(2);
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
if (!cmd)
|
2004-08-24 21:55:47 +02:00
|
|
|
{ /* Default is to list a KBX file */
|
2011-02-04 12:57:53 +01:00
|
|
|
if (!argc)
|
2004-08-24 21:55:47 +02:00
|
|
|
_keybox_dump_file (NULL, 0, stdout);
|
|
|
|
else
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
for (; argc; argc--, argv++)
|
2004-08-24 21:55:47 +02:00
|
|
|
_keybox_dump_file (*argv, 0, stdout);
|
|
|
|
}
|
|
|
|
}
|
2004-06-18 11:53:56 +02:00
|
|
|
else if (cmd == aStats )
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
if (!argc)
|
2004-08-24 21:55:47 +02:00
|
|
|
_keybox_dump_file (NULL, 1, stdout);
|
|
|
|
else
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
for (; argc; argc--, argv++)
|
2004-08-24 21:55:47 +02:00
|
|
|
_keybox_dump_file (*argv, 1, stdout);
|
|
|
|
}
|
|
|
|
}
|
2007-08-23 19:41:22 +02:00
|
|
|
else if (cmd == aFindDups )
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
if (!argc)
|
2007-08-23 19:41:22 +02:00
|
|
|
_keybox_dump_find_dups (NULL, 0, stdout);
|
|
|
|
else
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
for (; argc; argc--, argv++)
|
2007-08-23 19:41:22 +02:00
|
|
|
_keybox_dump_find_dups (*argv, 0, stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd == aCut )
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
if (!argc)
|
2007-08-23 19:41:22 +02:00
|
|
|
_keybox_dump_cut_records (NULL, from, to, stdout);
|
|
|
|
else
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
for (; argc; argc--, argv++)
|
2007-08-23 19:41:22 +02:00
|
|
|
_keybox_dump_cut_records (*argv, from, to, stdout);
|
|
|
|
}
|
|
|
|
}
|
2004-08-24 21:55:47 +02:00
|
|
|
else if (cmd == aImportOpenPGP)
|
|
|
|
{
|
|
|
|
if (!argc)
|
2012-12-28 14:03:16 +01:00
|
|
|
import_openpgp ("-", dry_run);
|
2004-08-24 21:55:47 +02:00
|
|
|
else
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
for (; argc; argc--, argv++)
|
2012-12-28 14:03:16 +01:00
|
|
|
import_openpgp (*argv, dry_run);
|
2004-08-24 21:55:47 +02:00
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
#if 0
|
2011-02-04 12:57:53 +01:00
|
|
|
else if ( cmd == aFindByFpr )
|
2004-06-18 11:53:56 +02:00
|
|
|
{
|
|
|
|
char *fpr;
|
|
|
|
if ( argc != 2 )
|
|
|
|
wrong_args ("kbxfile foingerprint");
|
|
|
|
fpr = format_fingerprint ( argv[1] );
|
|
|
|
if ( !fpr )
|
|
|
|
log_error ("invalid formatted fingerprint\n");
|
2011-02-04 12:57:53 +01:00
|
|
|
else
|
2004-06-18 11:53:56 +02:00
|
|
|
{
|
|
|
|
kbxfile_search_by_fpr ( argv[0], fpr );
|
|
|
|
gcry_free ( fpr );
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
else if ( cmd == aFindByKid )
|
2004-06-18 11:53:56 +02:00
|
|
|
{
|
|
|
|
u32 kid[2];
|
|
|
|
int mode;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-06-18 11:53:56 +02:00
|
|
|
if ( argc != 2 )
|
|
|
|
wrong_args ("kbxfile short-or-long-keyid");
|
|
|
|
mode = format_keyid ( argv[1], kid );
|
|
|
|
if ( !mode )
|
|
|
|
log_error ("invalid formatted keyID\n");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
kbxfile_search_by_kid ( argv[0], kid, mode );
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
else if ( cmd == aFindByUid )
|
2004-06-18 11:53:56 +02:00
|
|
|
{
|
|
|
|
if ( argc != 2 )
|
|
|
|
wrong_args ("kbxfile userID");
|
|
|
|
kbxfile_search_by_uid ( argv[0], argv[1] );
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
#endif
|
2004-06-18 11:53:56 +02:00
|
|
|
else
|
|
|
|
log_error ("unsupported action\n");
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2004-06-18 11:53:56 +02:00
|
|
|
myexit(0);
|
|
|
|
return 8; /*NEVER REACHED*/
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
myexit( int rc )
|
|
|
|
{
|
|
|
|
/* if( opt.debug & DBG_MEMSTAT_VALUE ) {*/
|
|
|
|
/* gcry_control( GCRYCTL_DUMP_MEMORY_STATS ); */
|
|
|
|
/* gcry_control( GCRYCTL_DUMP_RANDOM_STATS ); */
|
|
|
|
/* }*/
|
|
|
|
/* if( opt.debug ) */
|
|
|
|
/* gcry_control( GCRYCTL_DUMP_SECMEM_STATS ); */
|
|
|
|
rc = rc? rc : log_get_errorcount(0)? 2 :
|
|
|
|
keybox_errors_seen? 1 : 0;
|
|
|
|
exit(rc );
|
|
|
|
}
|