2006-10-24 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / kbx / kbxutil.c
blob19d356007726bf166287d621e5df1e3b523689b0
1 /* kbxutil.c - The Keybox utility
2 * Copyright (C) 2000, 2001, 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <assert.h>
32 #define JNLIB_NEED_LOG_LOGV
33 #include "../jnlib/logging.h"
34 #include "../jnlib/argparse.h"
35 #include "../jnlib/stringhelp.h"
36 #include "../jnlib/utf8conv.h"
37 #include "../common/i18n.h"
38 #include "keybox-defs.h"
40 #include <gcrypt.h>
43 enum cmd_and_opt_values {
44 aNull = 0,
45 oArmor = 'a',
46 oDryRun = 'n',
47 oOutput = 'o',
48 oQuiet = 'q',
49 oVerbose = 'v',
51 aNoSuchCmd = 500, /* force other values not to be a letter */
52 aFindByFpr,
53 aFindByKid,
54 aFindByUid,
55 aStats,
56 aImportOpenPGP,
58 oDebug,
59 oDebugAll,
61 oNoArmor,
64 aTest
68 static ARGPARSE_OPTS opts[] = {
69 { 300, NULL, 0, N_("@Commands:\n ") },
71 /* { aFindByFpr, "find-by-fpr", 0, "|FPR| find key using it's fingerprnt" }, */
72 /* { aFindByKid, "find-by-kid", 0, "|KID| find key using it's keyid" }, */
73 /* { aFindByUid, "find-by-uid", 0, "|NAME| find key by user name" }, */
74 { aStats, "stats", 0, "show key statistics" },
75 { aImportOpenPGP, "import-openpgp", 0, "import OpenPGP keyblocks"},
77 { 301, NULL, 0, N_("@\nOptions:\n ") },
79 /* { oArmor, "armor", 0, N_("create ascii armored output")}, */
80 /* { oArmor, "armour", 0, "@" }, */
81 /* { oOutput, "output", 2, N_("use as output file")}, */
82 { oVerbose, "verbose", 0, N_("verbose") },
83 { oQuiet, "quiet", 0, N_("be somewhat more quiet") },
84 { oDryRun, "dry-run", 0, N_("do not make any changes") },
86 { oDebug, "debug" ,4|16, N_("set debugging flags")},
87 { oDebugAll, "debug-all" ,0, N_("enable full debugging")},
89 {0} /* end of list */
93 void myexit (int rc);
95 int keybox_errors_seen = 0;
98 static const char *
99 my_strusage( int level )
101 const char *p;
102 switch( level ) {
103 case 11: p = "kbxutil (GnuPG)";
104 break;
105 case 13: p = VERSION; break;
106 case 17: p = PRINTABLE_OS_NAME; break;
107 case 19: p =
108 _("Please report bugs to " PACKAGE_BUGREPORT ".\n");
109 break;
110 case 1:
111 case 40: p =
112 _("Usage: kbxutil [options] [files] (-h for help)");
113 break;
114 case 41: p =
115 _("Syntax: kbxutil [options] [files]\n"
116 "list, export, import Keybox data\n");
117 break;
120 default: p = NULL;
122 return p;
126 static void
127 i18n_init(void)
129 #ifdef USE_SIMPLE_GETTEXT
130 set_gettext_file( PACKAGE_GT );
131 #else
132 #ifdef ENABLE_NLS
133 setlocale( LC_ALL, "" );
134 bindtextdomain( PACKAGE_GT, LOCALEDIR );
135 textdomain( PACKAGE_GT );
136 #endif
137 #endif
140 /* Used by gcry for logging */
141 static void
142 my_gcry_logger (void *dummy, int level, const char *fmt, va_list arg_ptr)
144 /* Map the log levels. */
145 switch (level)
147 case GCRY_LOG_CONT: level = JNLIB_LOG_CONT; break;
148 case GCRY_LOG_INFO: level = JNLIB_LOG_INFO; break;
149 case GCRY_LOG_WARN: level = JNLIB_LOG_WARN; break;
150 case GCRY_LOG_ERROR:level = JNLIB_LOG_ERROR; break;
151 case GCRY_LOG_FATAL:level = JNLIB_LOG_FATAL; break;
152 case GCRY_LOG_BUG: level = JNLIB_LOG_BUG; break;
153 case GCRY_LOG_DEBUG:level = JNLIB_LOG_DEBUG; break;
154 default: level = JNLIB_LOG_ERROR; break;
156 log_logv (level, fmt, arg_ptr);
161 /* static void */
162 /* wrong_args( const char *text ) */
163 /* { */
164 /* log_error("usage: kbxutil %s\n", text); */
165 /* myexit ( 1 ); */
166 /* } */
169 #if 0
170 static int
171 hextobyte( const byte *s )
173 int c;
175 if( *s >= '0' && *s <= '9' )
176 c = 16 * (*s - '0');
177 else if( *s >= 'A' && *s <= 'F' )
178 c = 16 * (10 + *s - 'A');
179 else if( *s >= 'a' && *s <= 'f' )
180 c = 16 * (10 + *s - 'a');
181 else
182 return -1;
183 s++;
184 if( *s >= '0' && *s <= '9' )
185 c += *s - '0';
186 else if( *s >= 'A' && *s <= 'F' )
187 c += 10 + *s - 'A';
188 else if( *s >= 'a' && *s <= 'f' )
189 c += 10 + *s - 'a';
190 else
191 return -1;
192 return c;
194 #endif
196 #if 0
197 static char *
198 format_fingerprint ( const char *s )
200 int i, c;
201 byte fpr[20];
203 for (i=0; i < 20 && *s; ) {
204 if ( *s == ' ' || *s == '\t' ) {
205 s++;
206 continue;
208 c = hextobyte(s);
209 if (c == -1) {
210 return NULL;
212 fpr[i++] = c;
213 s += 2;
215 return gcry_xstrdup ( fpr );
217 #endif
219 #if 0
220 static int
221 format_keyid ( const char *s, u32 *kid )
223 char helpbuf[9];
224 switch ( strlen ( s ) ) {
225 case 8:
226 kid[0] = 0;
227 kid[1] = strtoul( s, NULL, 16 );
228 return 10;
230 case 16:
231 mem2str( helpbuf, s, 9 );
232 kid[0] = strtoul( helpbuf, NULL, 16 );
233 kid[1] = strtoul( s+8, NULL, 16 );
234 return 11;
236 return 0; /* error */
238 #endif
240 static char *
241 read_file (const char *fname, size_t *r_length)
243 FILE *fp;
244 char *buf;
245 size_t buflen;
247 if (!strcmp (fname, "-"))
249 size_t nread, bufsize = 0;
251 fp = stdin;
252 buf = NULL;
253 buflen = 0;
254 #define NCHUNK 8192
257 bufsize += NCHUNK;
258 if (!buf)
259 buf = xtrymalloc (bufsize);
260 else
261 buf = xtryrealloc (buf, bufsize);
262 if (!buf)
263 log_fatal ("can't allocate buffer: %s\n", strerror (errno));
265 nread = fread (buf+buflen, 1, NCHUNK, fp);
266 if (nread < NCHUNK && ferror (fp))
268 log_error ("error reading `[stdin]': %s\n", strerror (errno));
269 xfree (buf);
270 return NULL;
272 buflen += nread;
274 while (nread == NCHUNK);
275 #undef NCHUNK
278 else
280 struct stat st;
282 fp = fopen (fname, "rb");
283 if (!fp)
285 log_error ("can't open `%s': %s\n", fname, strerror (errno));
286 return NULL;
289 if (fstat (fileno(fp), &st))
291 log_error ("can't stat `%s': %s\n", fname, strerror (errno));
292 fclose (fp);
293 return NULL;
296 buflen = st.st_size;
297 buf = xtrymalloc (buflen+1);
298 if (!buf)
299 log_fatal ("can't allocate buffer: %s\n", strerror (errno));
300 if (fread (buf, buflen, 1, fp) != 1)
302 log_error ("error reading `%s': %s\n", fname, strerror (errno));
303 fclose (fp);
304 xfree (buf);
305 return NULL;
307 fclose (fp);
310 *r_length = buflen;
311 return buf;
315 static void
316 dump_fpr (const unsigned char *buffer, size_t len)
318 int i;
320 for (i=0; i < len; i++, buffer++)
322 if (len == 20)
324 if (i == 10)
325 putchar (' ');
326 printf (" %02X%02X", buffer[0], buffer[1]);
327 i++; buffer++;
329 else
331 if (i && !(i % 8))
332 putchar (' ');
333 printf (" %02X", buffer[0]);
339 static void
340 dump_openpgp_key (keybox_openpgp_info_t info, const unsigned char *image)
342 printf ("pub %02X%02X%02X%02X",
343 info->primary.keyid[4], info->primary.keyid[5],
344 info->primary.keyid[6], info->primary.keyid[7] );
345 dump_fpr (info->primary.fpr, info->primary.fprlen);
346 putchar ('\n');
347 if (info->nsubkeys)
349 struct _keybox_openpgp_key_info *k;
351 k = &info->subkeys;
354 printf ("sub %02X%02X%02X%02X",
355 k->keyid[4], k->keyid[5],
356 k->keyid[6], k->keyid[7] );
357 dump_fpr (k->fpr, k->fprlen);
358 putchar ('\n');
359 k = k->next;
361 while (k);
363 if (info->nuids)
365 struct _keybox_openpgp_uid_info *u;
367 u = &info->uids;
370 printf ("uid\t\t%.*s\n", u->len, image + u->off);
371 u = u->next;
373 while (u);
378 static void
379 import_openpgp (const char *filename)
381 gpg_error_t err;
382 char *buffer;
383 size_t buflen, nparsed;
384 unsigned char *p;
385 struct _keybox_openpgp_info info;
387 buffer = read_file (filename, &buflen);
388 if (!buffer)
389 return;
390 p = (unsigned char *)buffer;
391 for (;;)
393 err = _keybox_parse_openpgp (p, buflen, &nparsed, &info);
394 assert (nparsed <= buflen);
395 if (err)
397 if (gpg_err_code (err) == GPG_ERR_NO_DATA)
398 break;
399 log_info ("%s: failed to parse OpenPGP keyblock: %s\n",
400 filename, gpg_strerror (err));
402 else
404 dump_openpgp_key (&info, p);
405 _keybox_destroy_openpgp_info (&info);
407 p += nparsed;
408 buflen -= nparsed;
410 xfree (buffer);
417 main( int argc, char **argv )
419 ARGPARSE_ARGS pargs;
420 enum cmd_and_opt_values cmd = 0;
422 set_strusage( my_strusage );
423 gcry_control (GCRYCTL_DISABLE_SECMEM);
424 log_set_prefix ("kbxutil", 1);
425 set_native_charset (NULL);
426 i18n_init ();
428 /* Check that the libraries are suitable. Do it here because
429 the option parsing may need services of the library. */
430 if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )
432 log_fatal( _("libgcrypt is too old (need %s, have %s)\n"),
433 NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL) );
436 gcry_set_log_handler (my_gcry_logger, NULL);
438 /*create_dotlock(NULL); register locking cleanup */
440 /* We need to use the gcry malloc function because jnlib does use them */
441 keybox_set_malloc_hooks (gcry_malloc, gcry_realloc, gcry_free);
442 ksba_set_malloc_hooks (gcry_malloc, gcry_realloc, gcry_free );
445 pargs.argc = &argc;
446 pargs.argv = &argv;
447 pargs.flags= 1; /* do not remove the args */
448 while (arg_parse( &pargs, opts) )
450 switch (pargs.r_opt)
452 case oVerbose:
453 /*opt.verbose++;*/
454 /*gcry_control( GCRYCTL_SET_VERBOSITY, (int)opt.verbose );*/
455 break;
456 case oDebug:
457 /*opt.debug |= pargs.r.ret_ulong; */
458 break;
459 case oDebugAll:
460 /*opt.debug = ~0;*/
461 break;
463 case aFindByFpr:
464 case aFindByKid:
465 case aFindByUid:
466 case aStats:
467 case aImportOpenPGP:
468 cmd = pargs.r_opt;
469 break;
471 default:
472 pargs.err = 2;
473 break;
476 if (log_get_errorcount(0) )
477 myexit(2);
479 if (!cmd)
480 { /* Default is to list a KBX file */
481 if (!argc)
482 _keybox_dump_file (NULL, 0, stdout);
483 else
485 for (; argc; argc--, argv++)
486 _keybox_dump_file (*argv, 0, stdout);
489 else if (cmd == aStats )
491 if (!argc)
492 _keybox_dump_file (NULL, 1, stdout);
493 else
495 for (; argc; argc--, argv++)
496 _keybox_dump_file (*argv, 1, stdout);
499 else if (cmd == aImportOpenPGP)
501 if (!argc)
502 import_openpgp ("-");
503 else
505 for (; argc; argc--, argv++)
506 import_openpgp (*argv);
509 #if 0
510 else if ( cmd == aFindByFpr )
512 char *fpr;
513 if ( argc != 2 )
514 wrong_args ("kbxfile foingerprint");
515 fpr = format_fingerprint ( argv[1] );
516 if ( !fpr )
517 log_error ("invalid formatted fingerprint\n");
518 else
520 kbxfile_search_by_fpr ( argv[0], fpr );
521 gcry_free ( fpr );
524 else if ( cmd == aFindByKid )
526 u32 kid[2];
527 int mode;
529 if ( argc != 2 )
530 wrong_args ("kbxfile short-or-long-keyid");
531 mode = format_keyid ( argv[1], kid );
532 if ( !mode )
533 log_error ("invalid formatted keyID\n");
534 else
536 kbxfile_search_by_kid ( argv[0], kid, mode );
539 else if ( cmd == aFindByUid )
541 if ( argc != 2 )
542 wrong_args ("kbxfile userID");
543 kbxfile_search_by_uid ( argv[0], argv[1] );
545 #endif
546 else
547 log_error ("unsupported action\n");
549 myexit(0);
550 return 8; /*NEVER REACHED*/
554 void
555 myexit( int rc )
557 /* if( opt.debug & DBG_MEMSTAT_VALUE ) {*/
558 /* gcry_control( GCRYCTL_DUMP_MEMORY_STATS ); */
559 /* gcry_control( GCRYCTL_DUMP_RANDOM_STATS ); */
560 /* }*/
561 /* if( opt.debug ) */
562 /* gcry_control( GCRYCTL_DUMP_SECMEM_STATS ); */
563 rc = rc? rc : log_get_errorcount(0)? 2 :
564 keybox_errors_seen? 1 : 0;
565 exit(rc );