1 /* kbxutil.c - The Keybox utility
2 * Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
31 #include "../jnlib/logging.h"
32 #include "../jnlib/argparse.h"
33 #include "../jnlib/stringhelp.h"
34 #include "../common/i18n.h"
35 #include "keybox-defs.h"
37 enum cmd_and_opt_values
{
45 aNoSuchCmd
= 500, /* force other values not to be a letter */
60 static ARGPARSE_OPTS opts
[] = {
61 { 300, NULL
, 0, N_("@Commands:\n ") },
63 { aFindByFpr
, "find-by-fpr", 0, "|FPR| find key using it's fingerprnt" },
64 { aFindByKid
, "find-by-kid", 0, "|KID| find key using it's keyid" },
65 { aFindByUid
, "find-by-uid", 0, "|NAME| find key by user name" },
67 { 301, NULL
, 0, N_("@\nOptions:\n ") },
69 { oArmor
, "armor", 0, N_("create ascii armored output")},
70 { oArmor
, "armour", 0, "@" },
71 { oOutput
, "output", 2, N_("use as output file")},
72 { oVerbose
, "verbose", 0, N_("verbose") },
73 { oQuiet
, "quiet", 0, N_("be somewhat more quiet") },
74 { oDryRun
, "dry-run", 0, N_("do not make any changes") },
76 { oDebug
, "debug" ,4|16, N_("set debugging flags")},
77 { oDebugAll
, "debug-all" ,0, N_("enable full debugging")},
85 int keybox_errors_seen
= 0;
89 my_strusage( int level
)
93 case 11: p
= "kbxutil (GnuPG)";
95 case 13: p
= VERSION
; break;
96 case 17: p
= PRINTABLE_OS_NAME
; break;
98 _("Please report bugs to " PACKAGE_BUGREPORT
".\n");
102 _("Usage: kbxutil [options] [files] (-h for help)");
105 _("Syntax: kbxutil [options] [files]\n"
106 "list, export, import Keybox data\n");
119 #ifdef USE_SIMPLE_GETTEXT
120 set_gettext_file( PACKAGE
);
123 #ifdef HAVE_LC_MESSAGES
124 setlocale( LC_TIME
, "" );
125 setlocale( LC_MESSAGES
, "" );
127 setlocale( LC_ALL
, "" );
129 bindtextdomain( PACKAGE
, GNUPG_LOCALEDIR
);
130 textdomain( PACKAGE
);
137 /* wrong_args( const char *text ) */
139 /* log_error("usage: kbxutil %s\n", text); */
146 hextobyte( const byte
*s
)
150 if( *s
>= '0' && *s
<= '9' )
152 else if( *s
>= 'A' && *s
<= 'F' )
153 c
= 16 * (10 + *s
- 'A');
154 else if( *s
>= 'a' && *s
<= 'f' )
155 c
= 16 * (10 + *s
- 'a');
159 if( *s
>= '0' && *s
<= '9' )
161 else if( *s
>= 'A' && *s
<= 'F' )
163 else if( *s
>= 'a' && *s
<= 'f' )
173 format_fingerprint ( const char *s
)
178 for (i
=0; i
< 20 && *s
; ) {
179 if ( *s
== ' ' || *s
== '\t' ) {
190 return gcry_xstrdup ( fpr
);
196 format_keyid ( const char *s
, u32
*kid
)
199 switch ( strlen ( s
) ) {
202 kid
[1] = strtoul( s
, NULL
, 16 );
206 mem2str( helpbuf
, s
, 9 );
207 kid
[0] = strtoul( helpbuf
, NULL
, 16 );
208 kid
[1] = strtoul( s
+8, NULL
, 16 );
211 return 0; /* error */
217 main( int argc
, char **argv
)
220 enum cmd_and_opt_values cmd
= 0;
222 set_strusage( my_strusage
);
223 /*log_set_name("kbxutil"); fixme */
225 /* check that the libraries are suitable. Do it here because
226 * the option parse may need services of the library */
227 if ( !gcry_check_version ( "1.1.4" ) )
229 log_fatal(_("libgcrypt is too old (need %s, have %s)\n"),
230 "1.1.4", gcry_check_version(NULL
) );
234 /*create_dotlock(NULL); register locking cleanup */
237 /* We need to use the gcry malloc function because jnlib does use them */
238 keybox_set_malloc_hooks (gcry_malloc
, gcry_realloc
, gcry_free
);
239 ksba_set_malloc_hooks (gcry_malloc
, gcry_realloc
, gcry_free
);
244 pargs
.flags
= 1; /* do not remove the args */
245 while (arg_parse( &pargs
, opts
) )
251 /*gcry_control( GCRYCTL_SET_VERBOSITY, (int)opt.verbose );*/
254 /*opt.debug |= pargs.r.ret_ulong; */
271 if (log_get_errorcount(0) )
275 { /* default is to list a KBX file */
277 _keybox_dump_file (NULL
, stdout
);
280 for (; argc
; argc
--, argv
++)
281 _keybox_dump_file (*argv
, stdout
);
285 else if ( cmd
== aFindByFpr
) {
288 wrong_args ("kbxfile foingerprint");
289 fpr
= format_fingerprint ( argv
[1] );
291 log_error ("invalid formatted fingerprint\n");
293 kbxfile_search_by_fpr ( argv
[0], fpr
);
297 else if ( cmd
== aFindByKid
) {
302 wrong_args ("kbxfile short-or-long-keyid");
303 mode
= format_keyid ( argv
[1], kid
);
305 log_error ("invalid formatted keyID\n");
307 kbxfile_search_by_kid ( argv
[0], kid
, mode
);
310 else if ( cmd
== aFindByUid
) {
312 wrong_args ("kbxfile userID");
313 kbxfile_search_by_uid ( argv
[0], argv
[1] );
317 log_error ("unsupported action\n");
320 return 8; /*NEVER REACHED*/
327 /* if( opt.debug & DBG_MEMSTAT_VALUE ) {*/
328 /* gcry_control( GCRYCTL_DUMP_MEMORY_STATS ); */
329 /* gcry_control( GCRYCTL_DUMP_RANDOM_STATS ); */
331 /* if( opt.debug ) */
332 /* gcry_control( GCRYCTL_DUMP_SECMEM_STATS ); */
333 rc
= rc
? rc
: log_get_errorcount(0)? 2 :
334 keybox_errors_seen
? 1 : 0;