2006-01-13 [paul] 1.9.100cvs142
[claws.git] / src / plugins / pgpcore / sgpgme.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #if HAVE_LOCALE_H
34 #  include <locale.h>
35 #endif
36
37 #include "sgpgme.h"
38 #include "privacy.h"
39 #include "prefs_common.h"
40 #include "utils.h"
41 #include "alertpanel.h"
42 #include "passphrase.h"
43 #include "prefs_gpg.h"
44 #include "select-keys.h"
45
46 static void sgpgme_disable_all(void)
47 {
48     /* FIXME: set a flag, so that we don't bother the user with failed
49      * gpgme messages */
50 }
51
52 gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig, 
53                                         gpgme_data_t plain, gpgme_data_t dummy)
54 {
55         gpgme_verify_result_t status = NULL;
56         gpgme_error_t err;
57
58         if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
59                 debug_print("op_verify err %s\n", gpgme_strerror(err));
60                 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
61                 
62         }
63         status = gpgme_op_verify_result(ctx);
64
65         return status;
66 }
67
68 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_result_t status)
69 {
70         unsigned long validity = 0;
71         gpgme_signature_t sig = NULL;
72         
73         if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
74                 debug_print("system error\n");
75                 return SIGNATURE_CHECK_FAILED;
76         }
77
78         if (status == NULL) {
79                 debug_print("status == NULL\n");
80                 return SIGNATURE_UNCHECKED;
81         }
82         sig = status->signatures;
83
84         if (sig == NULL) {
85                 debug_print("sig == NULL\n");
86                 return SIGNATURE_UNCHECKED;
87         }
88         validity = sig->validity;
89
90         debug_print("err code %d\n", gpg_err_code(sig->status));
91         switch (gpg_err_code(sig->status)) {
92         case GPG_ERR_NO_ERROR:
93                 switch (gpg_err_code(sig->validity)) {
94                 case GPGME_VALIDITY_NEVER:
95                         return SIGNATURE_INVALID;
96                 case GPGME_VALIDITY_UNKNOWN:
97                 case GPGME_VALIDITY_UNDEFINED:
98                 case GPGME_VALIDITY_MARGINAL:
99                 case GPGME_VALIDITY_FULL:
100                 case GPGME_VALIDITY_ULTIMATE:
101                         return SIGNATURE_OK;
102                 default:
103                         return SIGNATURE_CHECK_FAILED;
104                 }
105         case GPG_ERR_SIG_EXPIRED:
106         case GPG_ERR_KEY_EXPIRED:
107                 return SIGNATURE_WARN;
108         case GPG_ERR_BAD_SIGNATURE:
109                 return SIGNATURE_INVALID;
110         case GPG_ERR_NO_PUBKEY:
111                 return SIGNATURE_CHECK_FAILED;
112         default:
113                 return SIGNATURE_CHECK_FAILED;
114         }
115         return SIGNATURE_CHECK_FAILED;
116 }
117
118 static const gchar *get_validity_str(unsigned long validity)
119 {
120         switch (gpg_err_code(validity)) {
121         case GPGME_VALIDITY_UNKNOWN:
122                 return _("Unknown");
123         case GPGME_VALIDITY_UNDEFINED:
124                 return _("Undefined");
125         case GPGME_VALIDITY_NEVER:
126                 return _("Never");
127         case GPGME_VALIDITY_MARGINAL:
128                 return _("Marginal");
129         case GPGME_VALIDITY_FULL:
130                 return _("Full");
131         case GPGME_VALIDITY_ULTIMATE:
132                 return _("Ultimate");
133         default:
134                 return _("Error");
135         }
136 }
137
138 static gchar *extract_name(const char *uid)
139 {
140         if (uid == NULL)
141                 return NULL;
142         if (!strncmp(uid, "CN=", 3)) {
143                 gchar *result = g_strdup(uid+3);
144                 if (strstr(result, ","))
145                         *(strstr(result, ",")) = '\0';
146                 return result;
147         } else if (strstr(uid, ",CN=")) {
148                 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
149                 if (strstr(result, ","))
150                         *(strstr(result, ",")) = '\0';
151                 return result;
152         } else {
153                 return g_strdup(uid);
154         }
155 }
156 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
157 {
158         gpgme_signature_t sig = NULL;
159         gchar *uname = NULL;
160         gpgme_key_t key;
161         gchar *result = NULL;
162
163         if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
164                 return g_strdup(_("The signature can't be checked - GPG error."));
165         }
166
167         if (status == NULL) {
168                 return g_strdup(_("The signature has not been checked."));
169         }
170         sig = status->signatures;
171         if (sig == NULL) {
172                 return g_strdup(_("The signature has not been checked."));
173         }
174
175         gpgme_get_key(ctx, sig->fpr, &key, 0);
176         if (key)
177                 uname = extract_name(key->uids->uid);
178         else
179                 uname = g_strdup("<?>");
180         switch (gpg_err_code(sig->status)) {
181         case GPG_ERR_NO_ERROR:
182                 switch (gpg_err_code(sig->validity)) {
183                 case GPGME_VALIDITY_MARGINAL:
184                 case GPGME_VALIDITY_FULL:
185                 case GPGME_VALIDITY_ULTIMATE:
186                         result = g_strdup_printf(_("Good signature from %s."), uname);
187                         break;
188                 case GPGME_VALIDITY_UNKNOWN:
189                 case GPGME_VALIDITY_UNDEFINED:
190                 case GPGME_VALIDITY_NEVER:
191                 default:
192                         result = g_strdup_printf(_("Good signature (untrusted) from %s."), uname);
193                         break;
194                 }
195                 break;
196         case GPG_ERR_SIG_EXPIRED:
197                 result = g_strdup_printf(_("Expired signature from %s."), uname);
198                 break;
199         case GPG_ERR_KEY_EXPIRED:
200                 result = g_strdup_printf(_("Expired key from %s."), uname);
201                 break;
202         case GPG_ERR_BAD_SIGNATURE:
203                 result = g_strdup_printf(_("Bad signature from %s."), uname);
204                 break;
205         case GPG_ERR_NO_PUBKEY: {
206                 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
207                 result = g_strdup_printf(_("Key 0x%s not available to verify this signature."), id);
208                 g_free(id);
209                 break;
210                 }
211         default:
212                 result = g_strdup(_("The signature has not been checked."));
213                 break;
214         }
215         if (result == NULL)
216                 result = g_strdup(_("Error"));
217         g_free(uname);
218         return result;
219 }
220
221 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
222 {
223         gint i = 0;
224         gchar *ret;
225         GString *siginfo;
226         gpgme_signature_t sig = status->signatures;
227         
228         siginfo = g_string_sized_new(64);
229         while (sig) {
230                 gpgme_user_id_t user = NULL;
231                 gpgme_key_t key;
232
233                 const gchar *keytype, *keyid, *uid;
234                 
235                 gpgme_get_key(ctx, sig->fpr, &key, 0);
236
237                 if (key) {
238                         user = key->uids;
239                         keytype = gpgme_pubkey_algo_name(
240                                         key->subkeys->pubkey_algo);
241                         keyid = key->subkeys->keyid;
242                         uid = user->uid;
243                 } else {
244                         keytype = "?";
245                         keyid = "?";
246                         uid = "?";
247                 }
248                 g_string_append_printf(siginfo,
249                         _("Signature made using %s key ID %s\n"),
250                         keytype, keyid);
251                 
252                 switch (gpg_err_code(sig->status)) {
253                 case GPG_ERR_NO_ERROR:
254                 case GPG_ERR_KEY_EXPIRED:
255                         g_string_append_printf(siginfo,
256                                 _("Good signature from \"%s\" (Trust: %s)\n"),
257                                 uid, get_validity_str(sig->validity));
258                         break;
259                 case GPG_ERR_SIG_EXPIRED:
260                         g_string_append_printf(siginfo,
261                                 _("Expired signature from \"%s\"\n"),
262                                 uid);
263                         break;
264                 case GPG_ERR_BAD_SIGNATURE:
265                         g_string_append_printf(siginfo,
266                                 _("BAD signature from \"%s\"\n"),
267                                 uid);
268                         break;
269                 default:
270                         break;
271                 }
272                 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
273                         gint j = 1;
274                         user = user ? user->next : NULL;
275                         while (user != NULL) {
276                                 g_string_append_printf(siginfo,
277                                         _("                aka \"%s\"\n"),
278                                         user->uid);
279                                 j++;
280                                 user = user->next;
281                         }
282                         g_string_append_printf(siginfo,
283                                 _("Primary key fingerprint: %s\n"), 
284                                 sig ? sig->fpr: "?");
285 #ifdef HAVE_GPGME_PKA_TRUST
286                         if (sig->pka_trust == 1 && sig->pka_address) {
287                                 g_string_append_printf(siginfo,
288                                    _("WARNING: Signer's address \"%s\" "
289                                       "does not match DNS entry\n"), 
290                                    sig->pka_address);
291                         }
292                         else if (sig->pka_trust == 2 && sig->pka_address) {
293                                 g_string_append_printf(siginfo,
294                                    _("Verified signer's address is \"%s\"\n"),
295                                    sig->pka_address);
296                                 /* FIXME: Compare the address to the
297                                  * From: address.  */
298                         }
299 #endif /*HAVE_GPGME_PKA_TRUST*/
300                 }
301
302                 g_string_append(siginfo, "\n");
303                 i++;
304                 sig = sig->next;
305         }
306
307         ret = siginfo->str;
308         g_string_free(siginfo, FALSE);
309         return ret;
310 }
311
312 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
313 {
314         gpgme_data_t data = NULL;
315         gpgme_error_t err;
316         FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
317         gchar *tmp_file = NULL;
318
319         if (!fp) 
320                 return NULL;
321
322         tmp_file = get_tmp_file();
323         copy_file_part(fp, mimeinfo->offset, mimeinfo->length, tmp_file);
324         fclose(fp);
325         fp = g_fopen(tmp_file, "rb");
326         debug_print("tmp file %s\n", tmp_file);
327         if (!fp) 
328                 return NULL;
329         
330         err = gpgme_data_new_from_file(&data, tmp_file, 1);
331         g_unlink(tmp_file);
332         g_free(tmp_file);
333
334         debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
335         if (err) {
336                 debug_print ("gpgme_data_new_from_file failed: %s\n",
337                              gpgme_strerror (err));
338                 return NULL;
339         }
340         return data;
341 }
342
343 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
344 {
345         struct passphrase_cb_info_s info;
346         gpgme_data_t plain;
347         gpgme_error_t err;
348
349         memset (&info, 0, sizeof info);
350         
351         if (gpgme_data_new(&plain) != GPG_ERR_NO_ERROR) {
352                 gpgme_release(ctx);
353                 return NULL;
354         }
355         
356         if (!getenv("GPG_AGENT_INFO")) {
357                 info.c = ctx;
358                 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
359         }
360
361         err = gpgme_op_decrypt_verify(ctx, cipher, plain);
362         if (err != GPG_ERR_NO_ERROR) {
363                 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
364                 gpgmegtk_free_passphrase();
365                 gpgme_data_release(plain);
366                 return NULL;
367         }
368
369         err = gpgme_data_rewind(plain);
370         if (err) {
371                 debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
372         }
373
374         debug_print("decrypted.\n");
375         *status = gpgme_op_verify_result (ctx);
376
377         return plain;
378 }
379
380 gchar *sgpgme_get_encrypt_data(GSList *recp_names)
381 {
382         SelectionResult result = KEY_SELECTION_CANCEL;
383         gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result);
384         gchar *ret = NULL;
385         int i = 0;
386
387         if (!keys) {
388                 if (result == KEY_SELECTION_DONT)
389                         return g_strdup("_DONT_ENCRYPT_");
390                 else
391                         return NULL;
392         }
393         while (keys[i]) {
394                 gpgme_subkey_t skey = keys[i]->subkeys;
395                 gchar *fpr = skey->fpr;
396                 gchar *tmp = NULL;
397                 debug_print("adding %s\n", fpr);
398                 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
399                 g_free(ret);
400                 ret = tmp;
401                 i++;
402         }
403         return ret;
404 }
405
406 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
407 {
408         GPGAccountConfig *config;
409
410         gpgme_signers_clear(ctx);
411
412         config = prefs_gpg_account_get_config(account);
413
414         if (config->sign_key != SIGN_KEY_DEFAULT) {
415                 gchar *keyid;
416                 gpgme_key_t key;
417
418                 if (config->sign_key == SIGN_KEY_BY_FROM)
419                         keyid = account->address;
420                 else if (config->sign_key == SIGN_KEY_CUSTOM)
421                         keyid = config->sign_key_id;
422                 else
423                         return FALSE;
424
425                 gpgme_op_keylist_start(ctx, keyid, 1);
426                 while (!gpgme_op_keylist_next(ctx, &key)) {
427                         gpgme_signers_add(ctx, key);
428                         gpgme_key_release(key);
429                 }
430                 gpgme_op_keylist_end(ctx);
431         }
432
433         prefs_gpg_account_free_config(config);
434
435         return TRUE;
436 }
437
438 void sgpgme_init()
439 {
440         gpgme_engine_info_t engineInfo;
441         if (gpgme_check_version("1.0.0")) {
442 #ifdef LC_CTYPE
443                 gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
444 #endif
445 #ifdef LC_MESSAGES
446                 gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
447 #endif
448                 if (!gpgme_get_engine_info(&engineInfo)) {
449                         while (engineInfo) {
450                                 debug_print("GpgME Protocol: %s\n      Version: %s\n",
451                                         gpgme_get_protocol_name(engineInfo->protocol),
452                                         engineInfo->version);
453                                 engineInfo = engineInfo->next;
454                         }
455                 }
456         } else {
457                 sgpgme_disable_all();
458
459                 if (prefs_gpg_get_config()->gpg_warning) {
460                         AlertValue val;
461
462                         val = alertpanel_full
463                                 (_("Warning"),
464                                  _("GnuPG is not installed properly, or needs "
465                                  "to be upgraded.\n"
466                                  "OpenPGP support disabled."),
467                                  GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
468                                  ALERT_WARNING, G_ALERTDEFAULT);
469                         if (val & G_ALERTDISABLE)
470                                 prefs_gpg_get_config()->gpg_warning = FALSE;
471                 }
472         }
473 }
474
475 void sgpgme_done()
476 {
477         gpgmegtk_free_passphrase();
478 }
479
480 #endif /* USE_GPGME */