Require autoconf 1.11
[gpgme.git] / src / op-support.c
1 /* op-support.c - Supporting functions.
2    Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH
3
4    This file is part of GPGME.
5  
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.
10    
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.
15    
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/>.
18  */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26 #ifdef HAVE_LOCALE_H
27 #include <locale.h>
28 #endif
29
30 #include "gpgme.h"
31 #include "context.h"
32 #include "ops.h"
33 #include "util.h"
34
35 \f
36 gpgme_error_t
37 _gpgme_op_data_lookup (gpgme_ctx_t ctx, ctx_op_data_id_t type, void **hook,
38                        int size, void (*cleanup) (void *))
39 {
40   struct ctx_op_data *data = ctx->op_data;
41   while (data && data->type != type)
42     data = data->next;
43   if (!data)
44     {
45       if (size < 0)
46         {
47           *hook = NULL;
48           return 0;
49         }
50
51       data = calloc (1, sizeof (struct ctx_op_data) + size);
52       if (!data)
53         return gpg_error_from_errno (errno);
54       data->magic = CTX_OP_DATA_MAGIC;
55       data->next = ctx->op_data;
56       data->type = type;
57       data->cleanup = cleanup;
58       data->hook = (void *) (((char *) data) + sizeof (struct ctx_op_data));
59       data->references = 1;
60       ctx->op_data = data;
61     }
62   *hook = data->hook;
63   return 0;
64 }
65
66
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
70             event loop).  
71             256: Modification flag to suppress the engine reset.
72 */
73 gpgme_error_t
74 _gpgme_op_reset (gpgme_ctx_t ctx, int type)
75 {
76   gpgme_error_t err = 0;
77   struct gpgme_io_cbs io_cbs;
78   int no_reset = (type & 256);
79   int reuse_engine = 0;
80
81   type &= 255;
82
83   _gpgme_release_result (ctx);
84   LOCK (ctx->lock);
85   ctx->canceled = 0;
86   UNLOCK (ctx->lock);
87
88   if (ctx->engine && no_reset)
89     reuse_engine = 1;
90   else if (ctx->engine)
91     {
92       /* Attempt to reset an existing engine.  */
93
94       err = _gpgme_engine_reset (ctx->engine);
95       if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
96         {
97           _gpgme_engine_release (ctx->engine);
98           ctx->engine = NULL;
99         }
100     }
101
102   if (!ctx->engine)
103     {
104       gpgme_engine_info_t info;
105       info = ctx->engine_info;
106       while (info && info->protocol != ctx->protocol)
107         info = info->next;
108
109       if (!info)
110         return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
111
112       /* Create an engine object.  */
113       err = _gpgme_engine_new (info, &ctx->engine);
114       if (err)
115         return err;
116     }
117
118   if (!reuse_engine)
119     {
120       err = 0;
121 #ifdef LC_CTYPE
122       err = _gpgme_engine_set_locale (ctx->engine, LC_CTYPE, ctx->lc_ctype);
123 #endif
124 #ifdef LC_MESSAGES
125       if (!err)
126         err = _gpgme_engine_set_locale (ctx->engine,
127                                         LC_MESSAGES, ctx->lc_messages);
128 #endif
129       if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED)
130         err = 0;
131       if (err)
132         {
133           _gpgme_engine_release (ctx->engine);
134           ctx->engine = NULL;
135           return err;
136         }
137     }
138
139   if (ctx->sub_protocol != GPGME_PROTOCOL_DEFAULT)
140     {
141       err = _gpgme_engine_set_protocol (ctx->engine, ctx->sub_protocol);
142       if (err)
143         return err;
144     }
145
146   if (type == 1 || (type == 2 && !ctx->io_cbs.add))
147     {
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;
154     }
155   else if (! ctx->io_cbs.add)
156     {
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;
163     }
164   else
165     {
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;
172     }
173   _gpgme_engine_set_io_cbs (ctx->engine, &io_cbs);
174   return err;
175 }
176
177 \f
178 /* Parse the INV_RECP or INV-SNDR status line in ARGS and return the
179    result in KEY.  */
180 gpgme_error_t
181 _gpgme_parse_inv_recp (char *args, gpgme_invalid_key_t *key)
182 {
183   gpgme_invalid_key_t inv_key;
184   char *tail;
185   long int reason;
186
187   inv_key = malloc (sizeof (*inv_key));
188   if (!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 != ' '))
194     {
195       /* The crypto backend does not behave.  */
196       free (inv_key);
197       return gpg_error (GPG_ERR_INV_ENGINE);
198     }
199
200   switch (reason)
201     {
202     default:
203     case 0:
204       inv_key->reason = gpg_error (GPG_ERR_GENERAL);
205       break;
206
207     case 1:
208       inv_key->reason = gpg_error (GPG_ERR_NO_PUBKEY);
209       break;
210
211     case 2:
212       inv_key->reason = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
213       break;
214
215     case 3:
216       inv_key->reason = gpg_error (GPG_ERR_WRONG_KEY_USAGE);
217       break;
218
219     case 4:
220       inv_key->reason = gpg_error (GPG_ERR_CERT_REVOKED);
221       break;
222
223     case 5:
224       inv_key->reason = gpg_error (GPG_ERR_CERT_EXPIRED);
225       break;
226
227     case 6:
228       inv_key->reason = gpg_error (GPG_ERR_NO_CRL_KNOWN);
229       break;
230
231     case 7:
232       inv_key->reason = gpg_error (GPG_ERR_CRL_TOO_OLD);
233       break;
234
235     case 8:
236       inv_key->reason = gpg_error (GPG_ERR_NO_POLICY_MATCH);
237       break;
238
239     case 9:
240       inv_key->reason = gpg_error (GPG_ERR_NO_SECKEY);
241       break;
242
243     case 10:
244       inv_key->reason = gpg_error (GPG_ERR_PUBKEY_NOT_TRUSTED);
245       break;
246
247     case 11:
248       inv_key->reason = gpg_error (GPG_ERR_MISSING_CERT);
249       break;
250
251     case 12:
252       inv_key->reason = gpg_error (GPG_ERR_MISSING_ISSUER_CERT);
253       break;
254     }
255
256   while (*tail && *tail == ' ')
257     tail++;
258   if (*tail)
259     {
260       inv_key->fpr = strdup (tail);
261       if (!inv_key->fpr)
262         {
263           int saved_errno = errno;
264           free (inv_key);
265           return gpg_error_from_errno (saved_errno);
266         }
267     }
268   else
269     inv_key->fpr = NULL;
270
271   *key = inv_key;
272   return 0;
273 }
274
275
276 /* Parse the PLAINTEXT status line in ARGS and return the result in
277    FILENAMEP.  */
278 gpgme_error_t
279 _gpgme_parse_plaintext (char *args, char **filenamep)
280 {
281   char *tail;
282
283   while (*args == ' ')
284     args++;
285   if (*args == '\0')
286     return 0;
287
288   /* First argument is file type.  */
289   while (*args != ' ' && *args != '\0')
290     args++;
291   while (*args == ' ')
292     args++;
293   if (*args == '\0')
294     return 0;
295
296   /* Second argument is the timestamp.  */
297   while (*args != ' ' && *args != '\0')
298     args++;
299   while (*args == ' ')
300     args++;
301   if (*args == '\0')
302     return 0;
303
304   tail = args;
305   while (*tail != ' ' && *tail != '\0')
306     tail++;
307   *tail = '\0';
308   if (filenamep && *args != '\0')
309     {
310       char *filename = strdup (args);
311       if (!filename)
312         return gpg_error_from_syserror ();
313
314       *filenamep = filename;
315     }
316   return 0;
317 }