2005-07-16 [colin] 1.9.12cvs67
[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
33 #include "sgpgme.h"
34 #include "privacy.h"
35 #include "prefs_common.h"
36 #include "utils.h"
37 #include "alertpanel.h"
38 #include "passphrase.h"
39 #include "prefs_gpg.h"
40 #include "select-keys.h"
41
42 static void sgpgme_disable_all(void)
43 {
44     /* FIXME: set a flag, so that we don't bother the user with failed
45      * gpgme messages */
46 }
47
48 gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig, 
49                                         gpgme_data_t plain, gpgme_data_t dummy)
50 {
51         gpgme_verify_result_t status = NULL;
52         gpgme_error_t err;
53
54         if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
55                 debug_print("op_verify err %s\n", gpgme_strerror(err));
56                 return NULL;
57         }
58         status = gpgme_op_verify_result(ctx);
59
60         return status;
61 }
62
63 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_result_t status)
64 {
65         unsigned long validity = 0;
66         gpgme_signature_t sig = NULL;
67         
68         if (status == NULL)
69                 return SIGNATURE_UNCHECKED;
70
71         sig = status->signatures;
72
73         if (sig == NULL)
74                 return SIGNATURE_UNCHECKED;
75
76         validity = sig->validity;
77
78         switch (gpg_err_code(sig->status)) {
79         case GPG_ERR_NO_ERROR:
80                 if ((validity != GPGME_VALIDITY_MARGINAL) &&
81                     (validity != GPGME_VALIDITY_FULL) &&
82                     (validity != GPGME_VALIDITY_ULTIMATE))
83                         return SIGNATURE_WARN;
84                 return SIGNATURE_OK;
85         case GPG_ERR_SIG_EXPIRED:
86         case GPG_ERR_KEY_EXPIRED:
87                 return SIGNATURE_WARN;
88         case GPG_ERR_BAD_SIGNATURE:
89                 return SIGNATURE_INVALID;
90         case GPG_ERR_NO_PUBKEY:
91                 return SIGNATURE_CHECK_FAILED;
92         default:
93                 return SIGNATURE_CHECK_FAILED;
94         }
95         return SIGNATURE_CHECK_FAILED;
96 }
97
98 static const gchar *get_validity_str(unsigned long validity)
99 {
100         switch (gpg_err_code(validity)) {
101         case GPGME_VALIDITY_UNKNOWN:
102                 return _("Unknown");
103         case GPGME_VALIDITY_UNDEFINED:
104                 return _("Undefined");
105         case GPGME_VALIDITY_NEVER:
106                 return _("Never");
107         case GPGME_VALIDITY_MARGINAL:
108                 return _("Marginal");
109         case GPGME_VALIDITY_FULL:
110                 return _("Full");
111         case GPGME_VALIDITY_ULTIMATE:
112                 return _("Ultimate");
113         default:
114                 return _("Error");
115         }
116 }
117
118 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
119 {
120         gpgme_signature_t sig = NULL;
121         gpgme_user_id_t user = 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 = 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 = 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         unlink(tmp_file);
247         g_free(tmp_file);
248
249         debug_print("data %p (%d %d)\n", 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         gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names);
298         gchar *ret = NULL;
299         int i = 0;
300
301         if (!keys)
302                 return NULL;
303
304         while (keys[i]) {
305                 gpgme_subkey_t skey = keys[i]->subkeys;
306                 gchar *fpr = skey->fpr;
307                 gchar *tmp = NULL;
308                 debug_print("adding %s\n", fpr);
309                 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
310                 g_free(ret);
311                 ret = tmp;
312                 i++;
313         }
314         return ret;
315 }
316
317 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
318 {
319         GPGAccountConfig *config;
320
321         gpgme_signers_clear(ctx);
322
323         config = prefs_gpg_account_get_config(account);
324
325         if (config->sign_key != SIGN_KEY_DEFAULT) {
326                 gchar *keyid;
327                 gpgme_key_t key;
328
329                 if (config->sign_key == SIGN_KEY_BY_FROM)
330                         keyid = account->address;
331                 else if (config->sign_key == SIGN_KEY_CUSTOM)
332                         keyid = config->sign_key_id;
333                 else
334                         return FALSE;
335
336                 gpgme_op_keylist_start(ctx, keyid, 1);
337                 while (!gpgme_op_keylist_next(ctx, &key)) {
338                         gpgme_signers_add(ctx, key);
339                         gpgme_key_release(key);
340                 }
341                 gpgme_op_keylist_end(ctx);
342         }
343
344         prefs_gpg_account_free_config(config);
345
346         return TRUE;
347 }
348
349 void sgpgme_init()
350 {
351         gpgme_engine_info_t engineInfo;
352         if (gpgme_check_version("0.4.5")) {
353                 gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
354                 gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
355                 if (!gpgme_get_engine_info(&engineInfo)) {
356                         while (engineInfo) {
357                                 debug_print("GpgME Protocol: %s\n      Version: %s\n",
358                                         gpgme_get_protocol_name(engineInfo->protocol),
359                                         engineInfo->version);
360                                 engineInfo = engineInfo->next;
361                         }
362                 }
363         } else {
364                 sgpgme_disable_all();
365
366                 if (prefs_gpg_get_config()->gpg_warning) {
367                         AlertValue val;
368
369                         val = alertpanel_message_with_disable
370                                 (_("Warning"),
371                                  _("GnuPG is not installed properly, or needs "
372                                    "to be upgraded.\n"
373                                    "OpenPGP support disabled."), 
374                                 NULL, NULL, NULL, ALERT_WARNING);
375                         if (val & G_ALERTDISABLE)
376                                 prefs_gpg_get_config()->gpg_warning = FALSE;
377                 }
378         }
379 }
380
381 void sgpgme_done()
382 {
383         gpgmegtk_free_passphrase();
384 }
385
386 #endif /* USE_GPGME */