1 /* export.c - Export a key.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003, 2004, 2010 g10 Code GmbH
5 This file is part of GPGME.
7 GPGME is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of
10 the License, or (at your option) any later version.
12 GPGME is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this program; if not, see <http://www.gnu.org/licenses/>.
35 export_status_handler (void *priv, gpgme_status_code_t code, char *args)
42 export_start (gpgme_ctx_t ctx, int synchronous, const char *pattern,
43 gpgme_export_mode_t mode, gpgme_data_t keydata)
47 if ((mode & ~(GPGME_EXPORT_MODE_EXTERN
48 |GPGME_EXPORT_MODE_MINIMAL)))
49 return gpg_error (GPG_ERR_INV_VALUE); /* Invalid flags in MODE. */
52 if ((mode & GPGME_EXPORT_MODE_EXTERN))
55 return gpg_error (GPG_ERR_INV_VALUE);
60 return gpg_error (GPG_ERR_INV_VALUE);
63 err = _gpgme_op_reset (ctx, synchronous);
67 _gpgme_engine_set_status_handler (ctx->engine, export_status_handler, ctx);
69 return _gpgme_engine_op_export (ctx->engine, pattern, mode, keydata,
74 /* Export the keys listed in PATTERN into KEYDATA. */
76 gpgme_op_export_start (gpgme_ctx_t ctx, const char *pattern,
77 gpgme_export_mode_t mode, gpgme_data_t keydata)
81 TRACE_BEG3 (DEBUG_CTX, "gpgme_op_export_start", ctx,
82 "pattern=%s, mode=0x%x, keydata=%p", pattern, mode, keydata);
85 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
87 err = export_start (ctx, 0, pattern, mode, keydata);
88 return TRACE_ERR (err);
92 /* Export the keys listed in PATTERN into KEYDATA. */
94 gpgme_op_export (gpgme_ctx_t ctx, const char *pattern,
95 gpgme_export_mode_t mode, gpgme_data_t keydata)
99 TRACE_BEG3 (DEBUG_CTX, "gpgme_op_export", ctx,
100 "pattern=%s, mode=0x%x, keydata=%p", pattern, mode, keydata);
103 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
105 err = export_start (ctx, 1, pattern, mode, keydata);
107 err = _gpgme_wait_one (ctx);
113 export_ext_start (gpgme_ctx_t ctx, int synchronous, const char *pattern[],
114 gpgme_export_mode_t mode, gpgme_data_t keydata)
118 if ((mode & ~(GPGME_EXPORT_MODE_EXTERN
119 |GPGME_EXPORT_MODE_MINIMAL)))
120 return gpg_error (GPG_ERR_INV_VALUE); /* Invalid flags in MODE. */
122 if ((mode & GPGME_EXPORT_MODE_EXTERN))
125 return gpg_error (GPG_ERR_INV_VALUE);
130 return gpg_error (GPG_ERR_INV_VALUE);
133 err = _gpgme_op_reset (ctx, synchronous);
137 _gpgme_engine_set_status_handler (ctx->engine, export_status_handler, ctx);
139 return _gpgme_engine_op_export_ext (ctx->engine, pattern, mode, keydata,
144 /* Export the keys listed in PATTERN into KEYDATA. */
146 gpgme_op_export_ext_start (gpgme_ctx_t ctx, const char *pattern[],
147 gpgme_export_mode_t mode, gpgme_data_t keydata)
151 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_ext_start", ctx,
152 "mode=0x%x, keydata=%p", mode, keydata);
155 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
157 if (_gpgme_debug_trace () && pattern)
163 TRACE_LOG2 ("pattern[%i] = %s", i, pattern[i]);
168 err = export_ext_start (ctx, 0, pattern, mode, keydata);
169 return TRACE_ERR (err);
173 /* Export the keys listed in PATTERN into KEYDATA. */
175 gpgme_op_export_ext (gpgme_ctx_t ctx, const char *pattern[],
176 gpgme_export_mode_t mode, gpgme_data_t keydata)
180 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_ext_start", ctx,
181 "mode=0x%x, keydata=%p", mode, keydata);
184 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
186 if (_gpgme_debug_trace () && pattern)
192 TRACE_LOG2 ("pattern[%i] = %s", i, pattern[i]);
197 err = export_ext_start (ctx, 1, pattern, mode, keydata);
199 err = _gpgme_wait_one (ctx);
200 return TRACE_ERR (err);
208 export_keys_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t keys[],
209 gpgme_export_mode_t mode, gpgme_data_t keydata)
216 return gpg_error (GPG_ERR_INV_VALUE);
218 /* Create a list of pattern from the keys. */
219 for (idx=nkeys=0; keys[idx]; idx++)
220 if (keys[idx]->protocol == ctx->protocol)
223 return gpg_error (GPG_ERR_NO_DATA);
225 pattern = calloc (nkeys+1, sizeof *pattern);
227 return gpg_error_from_syserror ();
229 for (idx=nkeys=0; keys[idx]; idx++)
230 if (keys[idx]->protocol == ctx->protocol
231 && keys[idx]->subkeys
232 && keys[idx]->subkeys->fpr
233 && *keys[idx]->subkeys->fpr)
235 pattern[nkeys] = strdup (keys[idx]->subkeys->fpr);
238 err = gpg_error_from_syserror ();
245 /* Pass on to the regular function. */
246 err = export_ext_start (ctx, synchronous, (const char**)pattern,
250 for (idx=0; pattern[idx]; idx++)
258 /* Export the keys from the array KEYS into KEYDATA. Only keys of the
259 current protocol are exported and only those which have a
260 fingerprint set; that is keys received with some external search
261 methods are silently skipped. */
263 gpgme_op_export_keys_start (gpgme_ctx_t ctx,
265 gpgme_export_mode_t mode,
266 gpgme_data_t keydata)
270 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_keys_start", ctx,
271 "mode=0x%x, keydata=%p", mode, keydata);
274 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
276 if (_gpgme_debug_trace () && keys)
282 TRACE_LOG3 ("keys[%i] = %p (%s)", i, keys[i],
283 (keys[i]->subkeys && keys[i]->subkeys->fpr) ?
284 keys[i]->subkeys->fpr : "invalid");
289 err = export_keys_start (ctx, 0, keys, mode, keydata);
290 return TRACE_ERR (err);
294 gpgme_op_export_keys (gpgme_ctx_t ctx,
296 gpgme_export_mode_t mode,
297 gpgme_data_t keydata)
301 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_keys", ctx,
302 "mode=0x%x, keydata=%p", mode, keydata);
305 return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
307 if (_gpgme_debug_trace () && keys)
313 TRACE_LOG3 ("keys[%i] = %p (%s)", i, keys[i],
314 (keys[i]->subkeys && keys[i]->subkeys->fpr) ?
315 keys[i]->subkeys->fpr : "invalid");
320 err = export_keys_start (ctx, 1, keys, mode, keydata);
322 err = _gpgme_wait_one (ctx);
323 return TRACE_ERR (err);