1 /* pgp-export.c - Helper to run an export command
2 Copyright (C) 2008, 2009 g10 Code GmbH
4 This file is part of GPGME.
6 GPGME is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
11 GPGME is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* We need to include config.h so that we know whether we are building
21 with large file system (LFS) support. */
32 #define PGM "run-export"
34 #include "run-support.h"
43 fputs ("usage: " PGM " [options] USERIDS\n\n"
45 " --verbose run in verbose mode\n"
46 " --extern send keys to the keyserver (TAKE CARE!)\n"
52 main (int argc, char **argv)
58 gpgme_keylist_result_t result;
59 gpgme_key_t keyarray[100];
62 gpgme_export_mode_t mode = 0;
67 while (argc && last_argc != argc )
70 if (!strcmp (*argv, "--"))
75 else if (!strcmp (*argv, "--help"))
77 else if (!strcmp (*argv, "--verbose"))
82 else if (!strcmp (*argv, "--extern"))
84 mode |= GPGME_KEYLIST_MODE_EXTERN;
87 else if (!strncmp (*argv, "--", 2))
95 init_gpgme (GPGME_PROTOCOL_OpenPGP);
97 err = gpgme_new (&ctx);
99 gpgme_set_protocol (ctx, GPGME_PROTOCOL_OpenPGP);
101 /* Lookup the keys. */
102 err = gpgme_op_keylist_ext_start (ctx, (const char**)argv, 0, 0);
105 while (!(err = gpgme_op_keylist_next (ctx, &key)))
107 printf ("keyid: %s (fpr: %s)\n",
108 key->subkeys?nonnull (key->subkeys->keyid):"?",
109 key->subkeys?nonnull (key->subkeys->fpr):"?");
111 if (keyidx < DIM (keyarray)-1)
112 keyarray[keyidx++] = key;
115 fprintf (stderr, PGM": too many keys"
116 "- skipping this key\n");
117 gpgme_key_unref (key);
120 if (gpgme_err_code (err) != GPG_ERR_EOF)
122 err = gpgme_op_keylist_end (ctx);
124 keyarray[keyidx] = NULL;
126 result = gpgme_op_keylist_result (ctx);
127 if (result->truncated)
129 fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
133 /* Now for the actual export. */
134 if ((mode & GPGME_KEYLIST_MODE_EXTERN))
135 printf ("sending keys to keyserver\n");
137 err = gpgme_data_new (&out);
140 gpgme_set_armor (ctx, 1);
141 err = gpgme_op_export_keys (ctx, keyarray, mode,
142 (mode & GPGME_KEYLIST_MODE_EXTERN)? NULL:out);
146 if (!(mode & GPGME_KEYLIST_MODE_EXTERN))
148 fputs ("Begin Result:\n", stdout);
150 fputs ("End Result.\n", stdout);
154 gpgme_data_release (out);
156 for (keyidx=0; keyarray[keyidx]; keyidx++)
157 gpgme_key_unref (keyarray[keyidx]);