2005-09-14 [paul] 1.9.14cvs26
[claws.git] / src / plugins / pgpcore / sgpgme.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto & the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19  
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23  
24 #ifdef USE_GPGME
25
26 #include <time.h>
27 #include <gtk/gtk.h>
28 #include <gpgme.h>
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <stdio.h>
32 #include <errno.h>
33
34 #include "sgpgme.h"
35 #include "privacy.h"
36 #include "prefs_common.h"
37 #include "utils.h"
38 #include "alertpanel.h"
39 #include "passphrase.h"
40 #include "prefs_gpg.h"
41 #include "select-keys.h"
42
43 static void sgpgme_disable_all(void)
44 {
45     /* FIXME: set a flag, so that we don't bother the user with failed
46      * gpgme messages */
47 }
48
49 gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig, 
50                                         gpgme_data_t plain, gpgme_data_t dummy)
51 {
52         gpgme_verify_result_t status = NULL;
53         gpgme_error_t err;
54
55         if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
56                 debug_print("op_verify err %s\n", gpgme_strerror(err));
57                 return NULL;
58         }
59         status = gpgme_op_verify_result(ctx);
60
61         return status;
62 }
63
64 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_result_t status)
65 {
66         unsigned long validity = 0;
67         gpgme_signature_t sig = NULL;
68         
69         if (status == NULL)
70                 return SIGNATURE_UNCHECKED;
71
72         sig = status->signatures;
73
74         if (sig == NULL)
75                 return SIGNATURE_UNCHECKED;
76
77         validity = sig->validity;
78
79         switch (gpg_err_code(sig->status)) {
80         case GPG_ERR_NO_ERROR:
81                 if ((validity != GPGME_VALIDITY_MARGINAL) &&
82                     (validity != GPGME_VALIDITY_FULL) &&
83                     (validity != GPGME_VALIDITY_ULTIMATE))
84                         return SIGNATURE_WARN;
85                 return SIGNATURE_OK;
86         case GPG_ERR_SIG_EXPIRED:
87         case GPG_ERR_KEY_EXPIRED:
88                 return SIGNATURE_WARN;
89         case GPG_ERR_BAD_SIGNATURE:
90                 return SIGNATURE_INVALID;
91         case GPG_ERR_NO_PUBKEY:
92                 return SIGNATURE_CHECK_FAILED;
93         default:
94                 return SIGNATURE_CHECK_FAILED;
95         }
96         return SIGNATURE_CHECK_FAILED;
97 }
98
99 static const gchar *get_validity_str(unsigned long validity)
100 {
101         switch (gpg_err_code(validity)) {
102         case GPGME_VALIDITY_UNKNOWN:
103                 return _("Unknown");
104         case GPGME_VALIDITY_UNDEFINED:
105                 return _("Undefined");
106         case GPGME_VALIDITY_NEVER:
107                 return _("Never");
108         case GPGME_VALIDITY_MARGINAL:
109                 return _("Marginal");
110         case GPGME_VALIDITY_FULL:
111                 return _("Full");
112         case GPGME_VALIDITY_ULTIMATE:
113                 return _("Ultimate");
114         default:
115                 return _("Error");
116         }
117 }
118
119 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
120 {
121         gpgme_signature_t sig = NULL;
122         gchar *uname = NULL;
123         gpgme_key_t key;
124
125         if (status == NULL) {
126                 return g_strdup(_("The signature has not been checked."));
127         }
128         sig = status->signatures;
129         if (sig == NULL) {
130                 return g_strdup(_("The signature has not been checked."));
131         }
132
133         gpgme_get_key(ctx, sig->fpr, &key, 0);
134         if (key)
135                 uname = key->uids->uid;
136         else
137                 uname = "<?>";
138         switch (gpg_err_code(sig->status)) {
139         case GPG_ERR_NO_ERROR:
140         {
141                 return g_strdup_printf(_("Good signature from %s (Trust: %s)."),
142                         uname, get_validity_str(sig->validity));
143         }
144         case GPG_ERR_SIG_EXPIRED:
145                 return g_strdup_printf(_("Expired signature from %s."), uname);
146         case GPG_ERR_KEY_EXPIRED:
147                 return g_strdup_printf(_("Expired key from %s."), uname);
148         case GPG_ERR_BAD_SIGNATURE:
149                 return g_strdup_printf(_("Bad signature from %s."), uname);
150         case GPG_ERR_NO_PUBKEY:
151                 return g_strdup(_("No key available to verify this signature."));
152         default:
153                 return g_strdup(_("The signature has not been checked."));
154         }
155         return g_strdup(_("Error"));
156 }
157
158 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
159 {
160         gint i = 0;
161         gchar *ret;
162         GString *siginfo;
163         gpgme_signature_t sig = status->signatures;
164         
165         siginfo = g_string_sized_new(64);
166         while (sig) {
167                 gpgme_user_id_t user = NULL;
168                 gpgme_key_t key;
169
170                 const gchar *keytype, *keyid, *uid;
171                 
172                 gpgme_get_key(ctx, sig->fpr, &key, 0);
173                 user = key->uids;
174
175                 keytype = gpgme_pubkey_algo_name(key->subkeys->pubkey_algo);
176                 keyid = key->subkeys->keyid;
177                 g_string_append_printf(siginfo,
178                         _("Signature made using %s key ID %s\n"),
179                         keytype, keyid);
180                 
181                 uid = user->uid;
182                 switch (gpg_err_code(sig->status)) {
183                 case GPG_ERR_NO_ERROR:
184                 case GPG_ERR_KEY_EXPIRED:
185                         g_string_append_printf(siginfo,
186                                 _("Good signature from \"%s\"\n"),
187                                 uid);
188                         break;
189                 case GPG_ERR_SIG_EXPIRED:
190                         g_string_append_printf(siginfo,
191                                 _("Expired signature from \"%s\"\n"),
192                                 uid);
193                         break;
194                 case GPG_ERR_BAD_SIGNATURE:
195                         g_string_append_printf(siginfo,
196                                 _("BAD signature from \"%s\"\n"),
197                                 uid);
198                         break;
199                 default:
200                         break;
201                 }
202                 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
203                         gint j = 1;
204                         user = user->next;
205                         while (user != NULL) {
206                                 g_string_append_printf(siginfo,
207                                         _("                aka \"%s\"\n"),
208                                         user->uid);
209                                 j++;
210                                 user = user->next;
211                         }
212                         g_string_append_printf(siginfo,
213                                 _("Primary key fingerprint: %s\n"), 
214                                 sig->fpr);
215                 }
216                 
217                 g_string_append(siginfo, "\n");
218                 i++;
219                 sig = sig->next;
220         }
221
222         ret = siginfo->str;
223         g_string_free(siginfo, FALSE);
224         return ret;
225 }
226
227 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
228 {
229         gpgme_data_t data = NULL;
230         gpgme_error_t err;
231         FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
232         gchar *tmp_file = NULL;
233
234         if (!fp) 
235                 return NULL;
236
237         tmp_file = get_tmp_file();
238         copy_file_part(fp, mimeinfo->offset, mimeinfo->length, tmp_file);
239         fclose(fp);
240         fp = g_fopen(tmp_file, "rb");
241         debug_print("tmp file %s\n", tmp_file);
242         if (!fp) 
243                 return NULL;
244         
245         err = gpgme_data_new_from_file(&data, tmp_file, 1);
246         g_unlink(tmp_file);
247         g_free(tmp_file);
248
249         debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
250         if (err) {
251                 debug_print ("gpgme_data_new_from_file failed: %s\n",
252                              gpgme_strerror (err));
253                 return NULL;
254         }
255         return data;
256 }
257
258 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
259 {
260         struct passphrase_cb_info_s info;
261         gpgme_data_t plain;
262         gpgme_error_t err;
263
264         memset (&info, 0, sizeof info);
265         
266         if (gpgme_data_new(&plain) != GPG_ERR_NO_ERROR) {
267                 gpgme_release(ctx);
268                 return NULL;
269         }
270         
271         if (!getenv("GPG_AGENT_INFO")) {
272                 info.c = ctx;
273                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
274         }
275
276         err = gpgme_op_decrypt_verify(ctx, cipher, plain);
277         if (err != GPG_ERR_NO_ERROR) {
278                 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
279                 gpgmegtk_free_passphrase();
280                 gpgme_data_release(plain);
281                 return NULL;
282         }
283
284         err = gpgme_data_rewind(plain);
285         if (err) {
286                 debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
287         }
288
289         debug_print("decrypted.\n");
290         *status = gpgme_op_verify_result (ctx);
291
292         return plain;
293 }
294
295 gchar *sgpgme_get_encrypt_data(GSList *recp_names)
296 {
297         SelectionResult result = KEY_SELECTION_CANCEL;
298         gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result);
299         gchar *ret = NULL;
300         int i = 0;
301
302         if (!keys) {
303                 if (result == KEY_SELECTION_DONT)
304                         return g_strdup("_DONT_ENCRYPT_");
305                 else
306                         return NULL;
307         }
308         while (keys[i]) {
309                 gpgme_subkey_t skey = keys[i]->subkeys;
310                 gchar *fpr = skey->fpr;
311                 gchar *tmp = NULL;
312                 debug_print("adding %s\n", fpr);
313                 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
314                 g_free(ret);
315                 ret = tmp;
316                 i++;
317         }
318         return ret;
319 }
320
321 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
322 {
323         GPGAccountConfig *config;
324
325         gpgme_signers_clear(ctx);
326
327         config = prefs_gpg_account_get_config(account);
328
329         if (config->sign_key != SIGN_KEY_DEFAULT) {
330                 gchar *keyid;
331                 gpgme_key_t key;
332
333                 if (config->sign_key == SIGN_KEY_BY_FROM)
334                         keyid = account->address;
335                 else if (config->sign_key == SIGN_KEY_CUSTOM)
336                         keyid = config->sign_key_id;
337                 else
338                         return FALSE;
339
340                 gpgme_op_keylist_start(ctx, keyid, 1);
341                 while (!gpgme_op_keylist_next(ctx, &key)) {
342                         gpgme_signers_add(ctx, key);
343                         gpgme_key_release(key);
344                 }
345                 gpgme_op_keylist_end(ctx);
346         }
347
348         prefs_gpg_account_free_config(config);
349
350         return TRUE;
351 }
352
353 void sgpgme_init()
354 {
355         gpgme_engine_info_t engineInfo;
356         if (gpgme_check_version("0.4.5")) {
357                 gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
358                 gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
359                 if (!gpgme_get_engine_info(&engineInfo)) {
360                         while (engineInfo) {
361                                 debug_print("GpgME Protocol: %s\n      Version: %s\n",
362                                         gpgme_get_protocol_name(engineInfo->protocol),
363                                         engineInfo->version);
364                                 engineInfo = engineInfo->next;
365                         }
366                 }
367         } else {
368                 sgpgme_disable_all();
369
370                 if (prefs_gpg_get_config()->gpg_warning) {
371                         AlertValue val;
372
373                         val = alertpanel_full
374                                 (_("Warning"),
375                                  _("GnuPG is not installed properly, or needs "
376                                  "to be upgraded.\n"
377                                  "OpenPGP support disabled."),
378                                  GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
379                                  ALERT_WARNING, G_ALERTDEFAULT);
380                         if (val & G_ALERTDISABLE)
381                                 prefs_gpg_get_config()->gpg_warning = FALSE;
382                 }
383         }
384 }
385
386 void sgpgme_done()
387 {
388         gpgmegtk_free_passphrase();
389 }
390
391 #endif /* USE_GPGME */