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/>.
34 export_status_handler (void *priv, gpgme_status_code_t code, char *args)
41 export_start (gpgme_ctx_t ctx, int synchronous, const char *pattern,
42 gpgme_export_mode_t mode, gpgme_data_t keydata)
46 if ((mode & ~(GPGME_EXPORT_MODE_EXTERN
47 |GPGME_EXPORT_MODE_MINIMAL)))
48 return gpg_error (GPG_ERR_INV_VALUE); /* Invalid flags in MODE. */
51 if ((mode & GPGME_EXPORT_MODE_EXTERN))
54 return gpg_error (GPG_ERR_INV_VALUE);
59 return gpg_error (GPG_ERR_INV_VALUE);
62 err = _gpgme_op_reset (ctx, synchronous);
66 _gpgme_engine_set_status_handler (ctx->engine, export_status_handler, ctx);
68 return _gpgme_engine_op_export (ctx->engine, pattern, mode, keydata,
73 /* Export the keys listed in PATTERN into KEYDATA. */
75 gpgme_op_export_start (gpgme_ctx_t ctx, const char *pattern,
76 gpgme_export_mode_t mode, gpgme_data_t keydata)
80 TRACE_BEG3 (DEBUG_CTX, "gpgme_op_export_start", ctx,
81 "pattern=%s, mode=0x%x, keydata=%p", pattern, mode, keydata);
82 err = export_start (ctx, 0, pattern, mode, keydata);
83 return TRACE_ERR (err);
87 /* Export the keys listed in PATTERN into KEYDATA. */
89 gpgme_op_export (gpgme_ctx_t ctx, const char *pattern,
90 gpgme_export_mode_t mode, gpgme_data_t keydata)
94 TRACE_BEG3 (DEBUG_CTX, "gpgme_op_export", ctx,
95 "pattern=%s, mode=0x%x, keydata=%p", pattern, mode, keydata);
97 err = export_start (ctx, 1, pattern, mode, keydata);
99 err = _gpgme_wait_one (ctx);
105 export_ext_start (gpgme_ctx_t ctx, int synchronous, const char *pattern[],
106 gpgme_export_mode_t mode, gpgme_data_t keydata)
110 if ((mode & ~(GPGME_EXPORT_MODE_EXTERN
111 |GPGME_EXPORT_MODE_MINIMAL)))
112 return gpg_error (GPG_ERR_INV_VALUE); /* Invalid flags in MODE. */
114 if ((mode & GPGME_EXPORT_MODE_EXTERN))
117 return gpg_error (GPG_ERR_INV_VALUE);
122 return gpg_error (GPG_ERR_INV_VALUE);
125 err = _gpgme_op_reset (ctx, synchronous);
129 _gpgme_engine_set_status_handler (ctx->engine, export_status_handler, ctx);
131 return _gpgme_engine_op_export_ext (ctx->engine, pattern, mode, keydata,
136 /* Export the keys listed in PATTERN into KEYDATA. */
138 gpgme_op_export_ext_start (gpgme_ctx_t ctx, const char *pattern[],
139 gpgme_export_mode_t mode, gpgme_data_t keydata)
143 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_ext_start", ctx,
144 "mode=0x%x, keydata=%p", mode, keydata);
146 if (_gpgme_debug_trace () && pattern)
152 TRACE_LOG2 ("pattern[%i] = %s", i, pattern[i]);
157 err = export_ext_start (ctx, 0, pattern, mode, keydata);
158 return TRACE_ERR (err);
162 /* Export the keys listed in PATTERN into KEYDATA. */
164 gpgme_op_export_ext (gpgme_ctx_t ctx, const char *pattern[],
165 gpgme_export_mode_t mode, gpgme_data_t keydata)
169 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_ext_start", ctx,
170 "mode=0x%x, keydata=%p", mode, keydata);
172 if (_gpgme_debug_trace () && pattern)
178 TRACE_LOG2 ("pattern[%i] = %s", i, pattern[i]);
183 err = export_ext_start (ctx, 1, pattern, mode, keydata);
185 err = _gpgme_wait_one (ctx);
186 return TRACE_ERR (err);
194 export_keys_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t keys[],
195 gpgme_export_mode_t mode, gpgme_data_t keydata)
202 return gpg_error (GPG_ERR_INV_VALUE);
204 /* Create a list of pattern from the keys. */
205 for (idx=nkeys=0; keys[idx]; idx++)
206 if (keys[idx]->protocol == ctx->protocol)
209 return gpg_error (GPG_ERR_NO_DATA);
211 pattern = calloc (nkeys+1, sizeof *pattern);
213 return gpg_error_from_syserror ();
215 for (idx=nkeys=0; keys[idx]; idx++)
216 if (keys[idx]->protocol == ctx->protocol
217 && keys[idx]->subkeys
218 && keys[idx]->subkeys->fpr
219 && *keys[idx]->subkeys->fpr)
221 pattern[nkeys] = strdup (keys[idx]->subkeys->fpr);
224 err = gpg_error_from_syserror ();
231 /* Pass on to the regular function. */
232 err = export_ext_start (ctx, synchronous, (const char**)pattern,
236 for (idx=0; pattern[idx]; idx++)
244 /* Export the keys from the array KEYS into KEYDATA. Only keys of the
245 current protocol are exported and only those which have a
246 fingerprint set; that is keys received with some external search
247 methods are silently skipped. */
249 gpgme_op_export_keys_start (gpgme_ctx_t ctx,
251 gpgme_export_mode_t mode,
252 gpgme_data_t keydata)
256 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_keys_start", ctx,
257 "mode=0x%x, keydata=%p", mode, keydata);
259 if (_gpgme_debug_trace () && keys)
265 TRACE_LOG3 ("keys[%i] = %p (%s)", i, keys[i],
266 (keys[i]->subkeys && keys[i]->subkeys->fpr) ?
267 keys[i]->subkeys->fpr : "invalid");
272 err = export_keys_start (ctx, 0, keys, mode, keydata);
273 return TRACE_ERR (err);
277 gpgme_op_export_keys (gpgme_ctx_t ctx,
279 gpgme_export_mode_t mode,
280 gpgme_data_t keydata)
284 TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_keys", ctx,
285 "mode=0x%x, keydata=%p", mode, keydata);
287 if (_gpgme_debug_trace () && keys)
293 TRACE_LOG3 ("keys[%i] = %p (%s)", i, keys[i],
294 (keys[i]->subkeys && keys[i]->subkeys->fpr) ?
295 keys[i]->subkeys->fpr : "invalid");
300 err = export_keys_start (ctx, 1, keys, mode, keydata);
302 err = _gpgme_wait_one (ctx);
303 return TRACE_ERR (err);