2002-04-24 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / kbx / kbxutil.c
blobdcc073097b6e8467aee057f72cb35b762471dadf
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
21 #include <config.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <unistd.h>
29 #include <gcrypt.h>
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 {
38 aNull = 0,
39 oArmor = 'a',
40 oDryRun = 'n',
41 oOutput = 'o',
42 oQuiet = 'q',
43 oVerbose = 'v',
45 aNoSuchCmd = 500, /* force other values not to be a letter */
46 aFindByFpr,
47 aFindByKid,
48 aFindByUid,
50 oDebug,
51 oDebugAll,
53 oNoArmor,
56 aTest
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")},
79 {0} /* end of list */
83 void myexit (int rc);
85 int keybox_errors_seen = 0;
88 static const char *
89 my_strusage( int level )
91 const char *p;
92 switch( level ) {
93 case 11: p = "kbxutil (GnuPG)";
94 break;
95 case 13: p = VERSION; break;
96 case 17: p = PRINTABLE_OS_NAME; break;
97 case 19: p =
98 _("Please report bugs to " PACKAGE_BUGREPORT ".\n");
99 break;
100 case 1:
101 case 40: p =
102 _("Usage: kbxutil [options] [files] (-h for help)");
103 break;
104 case 41: p =
105 _("Syntax: kbxutil [options] [files]\n"
106 "list, export, import Keybox data\n");
107 break;
110 default: p = NULL;
112 return p;
116 static void
117 i18n_init(void)
119 #ifdef USE_SIMPLE_GETTEXT
120 set_gettext_file( PACKAGE );
121 #else
122 #ifdef ENABLE_NLS
123 #ifdef HAVE_LC_MESSAGES
124 setlocale( LC_TIME, "" );
125 setlocale( LC_MESSAGES, "" );
126 #else
127 setlocale( LC_ALL, "" );
128 #endif
129 bindtextdomain( PACKAGE, GNUPG_LOCALEDIR );
130 textdomain( PACKAGE );
131 #endif
132 #endif
136 /* static void */
137 /* wrong_args( const char *text ) */
138 /* { */
139 /* log_error("usage: kbxutil %s\n", text); */
140 /* myexit ( 1 ); */
141 /* } */
144 #if 0
145 static int
146 hextobyte( const byte *s )
148 int c;
150 if( *s >= '0' && *s <= '9' )
151 c = 16 * (*s - '0');
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');
156 else
157 return -1;
158 s++;
159 if( *s >= '0' && *s <= '9' )
160 c += *s - '0';
161 else if( *s >= 'A' && *s <= 'F' )
162 c += 10 + *s - 'A';
163 else if( *s >= 'a' && *s <= 'f' )
164 c += 10 + *s - 'a';
165 else
166 return -1;
167 return c;
169 #endif
171 #if 0
172 static char *
173 format_fingerprint ( const char *s )
175 int i, c;
176 byte fpr[20];
178 for (i=0; i < 20 && *s; ) {
179 if ( *s == ' ' || *s == '\t' ) {
180 s++;
181 continue;
183 c = hextobyte(s);
184 if (c == -1) {
185 return NULL;
187 fpr[i++] = c;
188 s += 2;
190 return gcry_xstrdup ( fpr );
192 #endif
194 #if 0
195 static int
196 format_keyid ( const char *s, u32 *kid )
198 char helpbuf[9];
199 switch ( strlen ( s ) ) {
200 case 8:
201 kid[0] = 0;
202 kid[1] = strtoul( s, NULL, 16 );
203 return 10;
205 case 16:
206 mem2str( helpbuf, s, 9 );
207 kid[0] = strtoul( helpbuf, NULL, 16 );
208 kid[1] = strtoul( s+8, NULL, 16 );
209 return 11;
211 return 0; /* error */
213 #endif
217 main( int argc, char **argv )
219 ARGPARSE_ARGS pargs;
220 enum cmd_and_opt_values cmd = 0;
222 set_strusage( my_strusage );
223 /*log_set_name("kbxutil"); fixme */
224 #if 0
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) );
232 #endif
234 /*create_dotlock(NULL); register locking cleanup */
235 i18n_init();
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 );
242 pargs.argc = &argc;
243 pargs.argv = &argv;
244 pargs.flags= 1; /* do not remove the args */
245 while (arg_parse( &pargs, opts) )
247 switch (pargs.r_opt)
249 case oVerbose:
250 /*opt.verbose++;*/
251 /*gcry_control( GCRYCTL_SET_VERBOSITY, (int)opt.verbose );*/
252 break;
253 case oDebug:
254 /*opt.debug |= pargs.r.ret_ulong; */
255 break;
256 case oDebugAll:
257 /*opt.debug = ~0;*/
258 break;
260 case aFindByFpr:
261 case aFindByKid:
262 case aFindByUid:
263 cmd = pargs.r_opt;
264 break;
266 default:
267 pargs.err = 2;
268 break;
271 if (log_get_errorcount(0) )
272 myexit(2);
274 if (!cmd)
275 { /* default is to list a KBX file */
276 if (!argc)
277 _keybox_dump_file (NULL, stdout);
278 else
280 for (; argc; argc--, argv++)
281 _keybox_dump_file (*argv, stdout);
284 #if 0
285 else if ( cmd == aFindByFpr ) {
286 char *fpr;
287 if ( argc != 2 )
288 wrong_args ("kbxfile foingerprint");
289 fpr = format_fingerprint ( argv[1] );
290 if ( !fpr )
291 log_error ("invalid formatted fingerprint\n");
292 else {
293 kbxfile_search_by_fpr ( argv[0], fpr );
294 gcry_free ( fpr );
297 else if ( cmd == aFindByKid ) {
298 u32 kid[2];
299 int mode;
301 if ( argc != 2 )
302 wrong_args ("kbxfile short-or-long-keyid");
303 mode = format_keyid ( argv[1], kid );
304 if ( !mode )
305 log_error ("invalid formatted keyID\n");
306 else {
307 kbxfile_search_by_kid ( argv[0], kid, mode );
310 else if ( cmd == aFindByUid ) {
311 if ( argc != 2 )
312 wrong_args ("kbxfile userID");
313 kbxfile_search_by_uid ( argv[0], argv[1] );
315 #endif
316 else
317 log_error ("unsupported action\n");
319 myexit(0);
320 return 8; /*NEVER REACHED*/
324 void
325 myexit( int rc )
327 /* if( opt.debug & DBG_MEMSTAT_VALUE ) {*/
328 /* gcry_control( GCRYCTL_DUMP_MEMORY_STATS ); */
329 /* gcry_control( GCRYCTL_DUMP_RANDOM_STATS ); */
330 /* }*/
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;
335 exit(rc );