2009-06-16 11:42:21 +00:00
|
|
|
/* pgp-export.c - Helper to run an export command
|
|
|
|
Copyright (C) 2008, 2009 g10 Code GmbH
|
|
|
|
|
|
|
|
This file is part of GPGME.
|
2012-09-25 13:29:49 +00:00
|
|
|
|
2009-06-16 11:42:21 +00:00
|
|
|
GPGME is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2.1 of
|
|
|
|
the License, or (at your option) any later version.
|
2012-09-25 13:29:49 +00:00
|
|
|
|
2009-06-16 11:42:21 +00:00
|
|
|
GPGME 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
|
|
|
|
Lesser General Public License for more details.
|
2012-09-25 13:29:49 +00:00
|
|
|
|
2009-06-16 11:42:21 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* We need to include config.h so that we know whether we are building
|
|
|
|
with large file system (LFS) support. */
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gpgme.h>
|
|
|
|
|
2009-07-07 14:17:39 +00:00
|
|
|
#define PGM "run-export"
|
2009-06-16 11:42:21 +00:00
|
|
|
|
2009-07-07 14:17:39 +00:00
|
|
|
#include "run-support.h"
|
2009-06-16 11:42:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int verbose;
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
show_usage (int ex)
|
|
|
|
{
|
|
|
|
fputs ("usage: " PGM " [options] USERIDS\n\n"
|
|
|
|
"Options:\n"
|
|
|
|
" --verbose run in verbose mode\n"
|
|
|
|
" --extern send keys to the keyserver (TAKE CARE!)\n"
|
|
|
|
, stderr);
|
|
|
|
exit (ex);
|
|
|
|
}
|
|
|
|
|
2012-09-25 13:29:49 +00:00
|
|
|
int
|
2009-06-16 11:42:21 +00:00
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
int last_argc = -1;
|
|
|
|
gpgme_error_t err;
|
|
|
|
gpgme_ctx_t ctx;
|
|
|
|
gpgme_key_t key;
|
|
|
|
gpgme_keylist_result_t result;
|
|
|
|
gpgme_key_t keyarray[100];
|
|
|
|
int keyidx = 0;
|
|
|
|
gpgme_data_t out;
|
|
|
|
gpgme_export_mode_t mode = 0;
|
|
|
|
|
|
|
|
if (argc)
|
|
|
|
{ argc--; argv++; }
|
|
|
|
|
|
|
|
while (argc && last_argc != argc )
|
|
|
|
{
|
|
|
|
last_argc = argc;
|
|
|
|
if (!strcmp (*argv, "--"))
|
|
|
|
{
|
|
|
|
argc--; argv++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!strcmp (*argv, "--help"))
|
|
|
|
show_usage (0);
|
|
|
|
else if (!strcmp (*argv, "--verbose"))
|
|
|
|
{
|
|
|
|
verbose = 1;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
else if (!strcmp (*argv, "--extern"))
|
|
|
|
{
|
|
|
|
mode |= GPGME_KEYLIST_MODE_EXTERN;
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
else if (!strncmp (*argv, "--", 2))
|
|
|
|
show_usage (1);
|
2012-09-25 13:29:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-06-16 11:42:21 +00:00
|
|
|
if (!argc)
|
|
|
|
show_usage (1);
|
|
|
|
|
|
|
|
init_gpgme (GPGME_PROTOCOL_OpenPGP);
|
|
|
|
|
|
|
|
err = gpgme_new (&ctx);
|
|
|
|
fail_if_err (err);
|
|
|
|
gpgme_set_protocol (ctx, GPGME_PROTOCOL_OpenPGP);
|
|
|
|
|
|
|
|
/* Lookup the keys. */
|
|
|
|
err = gpgme_op_keylist_ext_start (ctx, (const char**)argv, 0, 0);
|
|
|
|
fail_if_err (err);
|
2012-09-25 13:29:49 +00:00
|
|
|
|
2009-06-16 11:42:21 +00:00
|
|
|
while (!(err = gpgme_op_keylist_next (ctx, &key)))
|
|
|
|
{
|
|
|
|
printf ("keyid: %s (fpr: %s)\n",
|
|
|
|
key->subkeys?nonnull (key->subkeys->keyid):"?",
|
|
|
|
key->subkeys?nonnull (key->subkeys->fpr):"?");
|
|
|
|
|
|
|
|
if (keyidx < DIM (keyarray)-1)
|
|
|
|
keyarray[keyidx++] = key;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (stderr, PGM": too many keys"
|
|
|
|
"- skipping this key\n");
|
|
|
|
gpgme_key_unref (key);
|
|
|
|
}
|
|
|
|
}
|
Use gpgme interface for error handling to avoid linking with gpg-error.
* tests/t-data.c, tests/run-export.c, tests/run-keylist.c,
tests/run-support.h, tests/run-verify.c, tests/gpg/t-decrypt-verify.c,
tests/gpg/t-edit.c, tests/gpg/t-eventloop.c, tests/gpg/t-keylist-sig.c,
tests/gpg/t-keylist.c, tests/gpg/t-support.h, tests/gpg/t-trustlist.c,
tests/gpg/t-verify.c, tests/gpg/t-wait.c, tests/gpgsm/cms-decrypt.c,
tests/gpgsm/cms-keylist.c, tests/gpgsm/t-keylist.c,
tests/gpgsm/t-support.h, tests/gpgsm/t-verify.c,
tests/opassuan/t-command.c: Use gpgme interface for gpg-error functions.
2012-02-08 14:26:17 +00:00
|
|
|
if (gpgme_err_code (err) != GPG_ERR_EOF)
|
2009-06-16 11:42:21 +00:00
|
|
|
fail_if_err (err);
|
|
|
|
err = gpgme_op_keylist_end (ctx);
|
|
|
|
fail_if_err (err);
|
|
|
|
keyarray[keyidx] = NULL;
|
|
|
|
|
|
|
|
result = gpgme_op_keylist_result (ctx);
|
|
|
|
if (result->truncated)
|
|
|
|
{
|
|
|
|
fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now for the actual export. */
|
|
|
|
if ((mode & GPGME_KEYLIST_MODE_EXTERN))
|
|
|
|
printf ("sending keys to keyserver\n");
|
|
|
|
|
|
|
|
err = gpgme_data_new (&out);
|
|
|
|
fail_if_err (err);
|
|
|
|
|
|
|
|
gpgme_set_armor (ctx, 1);
|
2012-09-25 13:29:49 +00:00
|
|
|
err = gpgme_op_export_keys (ctx, keyarray, mode,
|
2009-06-16 11:42:21 +00:00
|
|
|
(mode & GPGME_KEYLIST_MODE_EXTERN)? NULL:out);
|
|
|
|
fail_if_err (err);
|
|
|
|
|
|
|
|
fflush (NULL);
|
|
|
|
if (!(mode & GPGME_KEYLIST_MODE_EXTERN))
|
|
|
|
{
|
|
|
|
fputs ("Begin Result:\n", stdout);
|
|
|
|
print_data (out);
|
|
|
|
fputs ("End Result.\n", stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup. */
|
|
|
|
gpgme_data_release (out);
|
|
|
|
|
|
|
|
for (keyidx=0; keyarray[keyidx]; keyidx++)
|
|
|
|
gpgme_key_unref (keyarray[keyidx]);
|
|
|
|
|
|
|
|
gpgme_release (ctx);
|
|
|
|
return 0;
|
|
|
|
}
|