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 = ctx->op_data;
41 while (data && data->type != type)
51 data = calloc (1, sizeof (struct ctx_op_data) + size);
53 return gpg_error_from_errno (errno);
54 data->magic = CTX_OP_DATA_MAGIC;
55 data->next = ctx->op_data;
57 data->cleanup = cleanup;
58 data->hook = (void *) (((char *) data) + sizeof (struct ctx_op_data));
67 /* type is: 0: asynchronous operation (use global or user event loop).
68 1: synchronous operation (always use private event loop).
69 2: asynchronous private operation (use private or user
71 256: Modification flag to suppress the engine reset.
74 _gpgme_op_reset (gpgme_ctx_t ctx, int type)
76 gpgme_error_t err = 0;
77 struct gpgme_io_cbs io_cbs;
78 int no_reset = (type & 256);
83 _gpgme_release_result (ctx);
88 if (ctx->engine && no_reset)
92 /* Attempt to reset an existing engine. */
94 err = _gpgme_engine_reset (ctx->engine);
95 if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
97 _gpgme_engine_release (ctx->engine);
104 gpgme_engine_info_t info;
105 info = ctx->engine_info;
106 while (info && info->protocol != ctx->protocol)
110 return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
112 /* Create an engine object. */
113 err = _gpgme_engine_new (info, &ctx->engine);
122 err = _gpgme_engine_set_locale (ctx->engine, LC_CTYPE, ctx->lc_ctype);
126 err = _gpgme_engine_set_locale (ctx->engine,
127 LC_MESSAGES, ctx->lc_messages);
129 if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
133 _gpgme_engine_release (ctx->engine);
139 if (ctx->sub_protocol != GPGME_PROTOCOL_DEFAULT)
141 err = _gpgme_engine_set_protocol (ctx->engine, ctx->sub_protocol);
146 if (type == 1 || (type == 2 && !ctx->io_cbs.add))
148 /* Use private event loop. */
149 io_cbs.add = _gpgme_add_io_cb;
150 io_cbs.add_priv = ctx;
151 io_cbs.remove = _gpgme_remove_io_cb;
152 io_cbs.event = _gpgme_wait_private_event_cb;
153 io_cbs.event_priv = ctx;
155 else if (! ctx->io_cbs.add)
157 /* Use global event loop. */
158 io_cbs.add = _gpgme_add_io_cb;
159 io_cbs.add_priv = ctx;
160 io_cbs.remove = _gpgme_remove_io_cb;
161 io_cbs.event = _gpgme_wait_global_event_cb;
162 io_cbs.event_priv = ctx;
166 /* Use user event loop. */
167 io_cbs.add = _gpgme_wait_user_add_io_cb;
168 io_cbs.add_priv = ctx;
169 io_cbs.remove = _gpgme_wait_user_remove_io_cb;
170 io_cbs.event = _gpgme_wait_user_event_cb;
171 io_cbs.event_priv = ctx;
173 _gpgme_engine_set_io_cbs (ctx->engine, &io_cbs);
178 /* Parse the INV_RECP or INV-SNDR status line in ARGS and return the
181 _gpgme_parse_inv_recp (char *args, gpgme_invalid_key_t *key)
183 gpgme_invalid_key_t inv_key;
187 inv_key = malloc (sizeof (*inv_key));
189 return gpg_error_from_errno (errno);
190 inv_key->next = NULL;
191 gpg_err_set_errno (0);
192 reason = strtol (args, &tail, 0);
193 if (errno || args == tail || (*tail && *tail != ' '))
195 /* The crypto backend does not behave. */
197 return gpg_error (GPG_ERR_INV_ENGINE);
204 inv_key->reason = gpg_error (GPG_ERR_GENERAL);
208 inv_key->reason = gpg_error (GPG_ERR_NO_PUBKEY);
212 inv_key->reason = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
216 inv_key->reason = gpg_error (GPG_ERR_WRONG_KEY_USAGE);
220 inv_key->reason = gpg_error (GPG_ERR_CERT_REVOKED);
224 inv_key->reason = gpg_error (GPG_ERR_CERT_EXPIRED);
228 inv_key->reason = gpg_error (GPG_ERR_NO_CRL_KNOWN);
232 inv_key->reason = gpg_error (GPG_ERR_CRL_TOO_OLD);
236 inv_key->reason = gpg_error (GPG_ERR_NO_POLICY_MATCH);
240 inv_key->reason = gpg_error (GPG_ERR_NO_SECKEY);
244 inv_key->reason = gpg_error (GPG_ERR_PUBKEY_NOT_TRUSTED);
248 inv_key->reason = gpg_error (GPG_ERR_MISSING_CERT);
252 inv_key->reason = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
256 while (*tail && *tail == ' ')
260 inv_key->fpr = strdup (tail);
263 int saved_errno = errno;
265 return gpg_error_from_errno (saved_errno);
276 /* Parse the PLAINTEXT status line in ARGS and return the result in
279 _gpgme_parse_plaintext (char *args, char **filenamep)
288 /* First argument is file type. */
289 while (*args != ' ' && *args != '\0')
296 /* Second argument is the timestamp. */
297 while (*args != ' ' && *args != '\0')
305 while (*tail != ' ' && *tail != '\0')
308 if (filenamep && *args != '\0')
310 char *filename = strdup (args);
312 return gpg_error_from_syserror ();
314 *filenamep = filename;