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/>.
37 _gpgme_op_data_lookup (gpgme_ctx_t ctx, ctx_op_data_id_t type, void **hook,
38 int size, void (*cleanup) (void *))
40 struct ctx_op_data *data;
43 return gpg_error (GPG_ERR_INV_VALUE);
46 while (data && data->type != type)
56 data = calloc (1, sizeof (struct ctx_op_data) + size);
58 return gpg_error_from_errno (errno);
59 data->magic = CTX_OP_DATA_MAGIC;
60 data->next = ctx->op_data;
62 data->cleanup = cleanup;
63 data->hook = (void *) (((char *) data) + sizeof (struct ctx_op_data));
72 /* type is: 0: asynchronous operation (use global or user event loop).
73 1: synchronous operation (always use private event loop).
74 2: asynchronous private operation (use private or user
76 256: Modification flag to suppress the engine reset.
79 _gpgme_op_reset (gpgme_ctx_t ctx, int type)
81 gpgme_error_t err = 0;
82 struct gpgme_io_cbs io_cbs;
83 int no_reset = (type & 256);
88 _gpgme_release_result (ctx);
93 if (ctx->engine && no_reset)
97 /* Attempt to reset an existing engine. */
99 err = _gpgme_engine_reset (ctx->engine);
100 if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
102 _gpgme_engine_release (ctx->engine);
109 gpgme_engine_info_t info;
110 info = ctx->engine_info;
111 while (info && info->protocol != ctx->protocol)
115 return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
117 /* Create an engine object. */
118 err = _gpgme_engine_new (info, &ctx->engine);
127 err = _gpgme_engine_set_locale (ctx->engine, LC_CTYPE, ctx->lc_ctype);
131 err = _gpgme_engine_set_locale (ctx->engine,
132 LC_MESSAGES, ctx->lc_messages);
134 if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
138 _gpgme_engine_release (ctx->engine);
144 if (ctx->sub_protocol != GPGME_PROTOCOL_DEFAULT)
146 err = _gpgme_engine_set_protocol (ctx->engine, ctx->sub_protocol);
151 if (type == 1 || (type == 2 && !ctx->io_cbs.add))
153 /* Use private event loop. */
154 io_cbs.add = _gpgme_add_io_cb;
155 io_cbs.add_priv = ctx;
156 io_cbs.remove = _gpgme_remove_io_cb;
157 io_cbs.event = _gpgme_wait_private_event_cb;
158 io_cbs.event_priv = ctx;
160 else if (! ctx->io_cbs.add)
162 /* Use global event loop. */
163 io_cbs.add = _gpgme_add_io_cb;
164 io_cbs.add_priv = ctx;
165 io_cbs.remove = _gpgme_remove_io_cb;
166 io_cbs.event = _gpgme_wait_global_event_cb;
167 io_cbs.event_priv = ctx;
171 /* Use user event loop. */
172 io_cbs.add = _gpgme_wait_user_add_io_cb;
173 io_cbs.add_priv = ctx;
174 io_cbs.remove = _gpgme_wait_user_remove_io_cb;
175 io_cbs.event = _gpgme_wait_user_event_cb;
176 io_cbs.event_priv = ctx;
178 _gpgme_engine_set_io_cbs (ctx->engine, &io_cbs);
183 /* Parse the INV_RECP or INV-SNDR status line in ARGS and return the
186 _gpgme_parse_inv_recp (char *args, gpgme_invalid_key_t *key)
188 gpgme_invalid_key_t inv_key;
192 inv_key = malloc (sizeof (*inv_key));
194 return gpg_error_from_errno (errno);
195 inv_key->next = NULL;
196 gpg_err_set_errno (0);
197 reason = strtol (args, &tail, 0);
198 if (errno || args == tail || (*tail && *tail != ' '))
200 /* The crypto backend does not behave. */
202 return gpg_error (GPG_ERR_INV_ENGINE);
209 inv_key->reason = gpg_error (GPG_ERR_GENERAL);
213 inv_key->reason = gpg_error (GPG_ERR_NO_PUBKEY);
217 inv_key->reason = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
221 inv_key->reason = gpg_error (GPG_ERR_WRONG_KEY_USAGE);
225 inv_key->reason = gpg_error (GPG_ERR_CERT_REVOKED);
229 inv_key->reason = gpg_error (GPG_ERR_CERT_EXPIRED);
233 inv_key->reason = gpg_error (GPG_ERR_NO_CRL_KNOWN);
237 inv_key->reason = gpg_error (GPG_ERR_CRL_TOO_OLD);
241 inv_key->reason = gpg_error (GPG_ERR_NO_POLICY_MATCH);
245 inv_key->reason = gpg_error (GPG_ERR_NO_SECKEY);
249 inv_key->reason = gpg_error (GPG_ERR_PUBKEY_NOT_TRUSTED);
253 inv_key->reason = gpg_error (GPG_ERR_MISSING_CERT);
257 inv_key->reason = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
261 while (*tail && *tail == ' ')
265 inv_key->fpr = strdup (tail);
268 int saved_errno = errno;
270 return gpg_error_from_errno (saved_errno);
281 /* Parse the PLAINTEXT status line in ARGS and return the result in
284 _gpgme_parse_plaintext (char *args, char **filenamep)
293 /* First argument is file type. */
294 while (*args != ' ' && *args != '\0')
301 /* Second argument is the timestamp. */
302 while (*args != ' ' && *args != '\0')
310 while (*tail != ' ' && *tail != '\0')
313 if (filenamep && *args != '\0')
315 char *filename = strdup (args);
317 return gpg_error_from_syserror ();
319 *filenamep = filename;