1 /* op-support.c - Supporting functions.
2 Copyright (C) 2002, 2003, 2004, 2007 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/>.
38 _gpgme_op_data_lookup (gpgme_ctx_t ctx, ctx_op_data_id_t type, void **hook,
39 int size, void (*cleanup) (void *))
41 struct ctx_op_data *data;
44 return gpg_error (GPG_ERR_INV_VALUE);
47 while (data && data->type != type)
57 data = calloc (1, sizeof (struct ctx_op_data) + size);
59 return gpg_error_from_syserror ();
60 data->magic = CTX_OP_DATA_MAGIC;
61 data->next = ctx->op_data;
63 data->cleanup = cleanup;
64 data->hook = (void *) (((char *) data) + sizeof (struct ctx_op_data));
73 /* type is: 0: asynchronous operation (use global or user event loop).
74 1: synchronous operation (always use private event loop).
75 2: asynchronous private operation (use private or user
77 256: Modification flag to suppress the engine reset.
80 _gpgme_op_reset (gpgme_ctx_t ctx, int type)
82 gpgme_error_t err = 0;
83 struct gpgme_io_cbs io_cbs;
84 int no_reset = (type & 256);
89 _gpgme_release_result (ctx);
94 if (ctx->engine && no_reset)
98 /* Attempt to reset an existing engine. */
100 err = _gpgme_engine_reset (ctx->engine);
101 if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
103 _gpgme_engine_release (ctx->engine);
110 gpgme_engine_info_t info;
111 info = ctx->engine_info;
112 while (info && info->protocol != ctx->protocol)
116 return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
118 /* Create an engine object. */
119 err = _gpgme_engine_new (info, &ctx->engine);
128 err = _gpgme_engine_set_locale (ctx->engine, LC_CTYPE, ctx->lc_ctype);
132 err = _gpgme_engine_set_locale (ctx->engine,
133 LC_MESSAGES, ctx->lc_messages);
135 if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
140 err = _gpgme_engine_set_pinentry_mode (ctx->engine,
142 if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
148 _gpgme_engine_release (ctx->engine);
154 if (ctx->sub_protocol != GPGME_PROTOCOL_DEFAULT)
156 err = _gpgme_engine_set_protocol (ctx->engine, ctx->sub_protocol);
161 if (type == 1 || (type == 2 && !ctx->io_cbs.add))
163 /* Use private event loop. */
164 io_cbs.add = _gpgme_add_io_cb;
165 io_cbs.add_priv = ctx;
166 io_cbs.remove = _gpgme_remove_io_cb;
167 io_cbs.event = _gpgme_wait_private_event_cb;
168 io_cbs.event_priv = ctx;
170 else if (! ctx->io_cbs.add)
172 /* Use global event loop. */
173 io_cbs.add = _gpgme_add_io_cb;
174 io_cbs.add_priv = ctx;
175 io_cbs.remove = _gpgme_remove_io_cb;
176 io_cbs.event = _gpgme_wait_global_event_cb;
177 io_cbs.event_priv = ctx;
181 /* Use user event loop. */
182 io_cbs.add = _gpgme_wait_user_add_io_cb;
183 io_cbs.add_priv = ctx;
184 io_cbs.remove = _gpgme_wait_user_remove_io_cb;
185 io_cbs.event = _gpgme_wait_user_event_cb;
186 io_cbs.event_priv = ctx;
188 _gpgme_engine_set_io_cbs (ctx->engine, &io_cbs);
193 /* Parse the INV_RECP or INV-SNDR status line in ARGS and return the
196 _gpgme_parse_inv_recp (char *args, gpgme_invalid_key_t *key)
198 gpgme_invalid_key_t inv_key;
202 inv_key = malloc (sizeof (*inv_key));
204 return gpg_error_from_syserror ();
205 inv_key->next = NULL;
206 gpg_err_set_errno (0);
207 reason = strtol (args, &tail, 0);
208 if (errno || args == tail || (*tail && *tail != ' '))
210 /* The crypto backend does not behave. */
212 return trace_gpg_error (GPG_ERR_INV_ENGINE);
219 inv_key->reason = gpg_error (GPG_ERR_GENERAL);
223 inv_key->reason = gpg_error (GPG_ERR_NO_PUBKEY);
227 inv_key->reason = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
231 inv_key->reason = gpg_error (GPG_ERR_WRONG_KEY_USAGE);
235 inv_key->reason = gpg_error (GPG_ERR_CERT_REVOKED);
239 inv_key->reason = gpg_error (GPG_ERR_CERT_EXPIRED);
243 inv_key->reason = gpg_error (GPG_ERR_NO_CRL_KNOWN);
247 inv_key->reason = gpg_error (GPG_ERR_CRL_TOO_OLD);
251 inv_key->reason = gpg_error (GPG_ERR_NO_POLICY_MATCH);
255 inv_key->reason = gpg_error (GPG_ERR_NO_SECKEY);
259 inv_key->reason = gpg_error (GPG_ERR_PUBKEY_NOT_TRUSTED);
263 inv_key->reason = gpg_error (GPG_ERR_MISSING_CERT);
267 inv_key->reason = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
271 inv_key->reason = gpg_error (252); /*GPG_ERR_KEY_DISABLED*/
275 inv_key->reason = gpg_error (GPG_ERR_INV_USER_ID);
279 while (*tail && *tail == ' ')
283 inv_key->fpr = strdup (tail);
287 return gpg_error_from_syserror ();
298 /* Parse the PLAINTEXT status line in ARGS and return the result in
301 _gpgme_parse_plaintext (char *args, char **filenamep)
310 /* First argument is file type. */
311 while (*args != ' ' && *args != '\0')
318 /* Second argument is the timestamp. */
319 while (*args != ' ' && *args != '\0')
327 while (*tail != ' ' && *tail != '\0')
330 if (filenamep && *args != '\0')
332 char *filename = strdup (args);
334 return gpg_error_from_syserror ();
336 *filenamep = filename;
342 /* Parse a FAILURE status line and return the error code. ARGS is
343 modified to contain the location part. */
345 _gpgme_parse_failure (char *args)
349 where = strchr (args, ' ');
351 return trace_gpg_error (GPG_ERR_INV_ENGINE);
356 where = strchr (which, ' ');