2012-11-08 [colin] 3.8.1cvs115
[claws.git] / src / plugins / pgpcore / sgpgme.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19  
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24  
25 #ifdef USE_GPGME
26
27 #include <time.h>
28 #include <gtk/gtk.h>
29 #include <gpgme.h>
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #ifndef G_OS_WIN32
38 #  include <sys/wait.h>
39 #endif
40 #if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
41 #  include <sys/signal.h>
42 #endif
43 #ifndef G_OS_WIN32
44 #include <sys/mman.h>
45 #endif
46 #if HAVE_LOCALE_H
47 #  include <locale.h>
48 #endif
49
50 #include "sgpgme.h"
51 #include "privacy.h"
52 #include "prefs_common.h"
53 #include "utils.h"
54 #include "alertpanel.h"
55 #include "passphrase.h"
56 #include "prefs_gpg.h"
57 #include "account.h"
58 #include "select-keys.h"
59
60 static void sgpgme_disable_all(void)
61 {
62     /* FIXME: set a flag, so that we don't bother the user with failed
63      * gpgme messages */
64 }
65
66 gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig, 
67                                         gpgme_data_t plain, gpgme_data_t dummy)
68 {
69         gpgme_verify_result_t status = NULL;
70         gpgme_error_t err;
71
72         if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
73                 debug_print("op_verify err %s\n", gpgme_strerror(err));
74                 privacy_set_error("%s", gpgme_strerror(err));
75                 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
76         }
77         status = gpgme_op_verify_result(ctx);
78         if (status && status->signatures == NULL) {
79                 debug_print("no signature found\n");
80                 privacy_set_error(_("No signature found"));
81                 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
82         }
83         return status;
84 }
85
86 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_result_t status)
87 {
88         gpgme_signature_t sig = NULL;
89         
90         if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
91                 debug_print("system error\n");
92                 return SIGNATURE_CHECK_FAILED;
93         }
94
95         if (status == NULL) {
96                 debug_print("status == NULL\n");
97                 return SIGNATURE_UNCHECKED;
98         }
99         sig = status->signatures;
100
101         if (sig == NULL) {
102                 debug_print("sig == NULL\n");
103                 return SIGNATURE_UNCHECKED;
104         }
105
106         debug_print("err code %d\n", gpg_err_code(sig->status));
107         switch (gpg_err_code(sig->status)) {
108         case GPG_ERR_NO_ERROR:
109                 switch (gpg_err_code(sig->validity)) {
110                 case GPGME_VALIDITY_NEVER:
111                         return SIGNATURE_INVALID;
112                 case GPGME_VALIDITY_UNKNOWN:
113                 case GPGME_VALIDITY_UNDEFINED:
114                 case GPGME_VALIDITY_MARGINAL:
115                 case GPGME_VALIDITY_FULL:
116                 case GPGME_VALIDITY_ULTIMATE:
117                         return SIGNATURE_OK;
118                 default:
119                         return SIGNATURE_CHECK_FAILED;
120                 }
121         case GPG_ERR_SIG_EXPIRED:
122         case GPG_ERR_CERT_REVOKED:
123                 return SIGNATURE_WARN;
124         case GPG_ERR_KEY_EXPIRED:
125                 return SIGNATURE_KEY_EXPIRED;
126         case GPG_ERR_BAD_SIGNATURE:
127                 return SIGNATURE_INVALID;
128         case GPG_ERR_NO_PUBKEY:
129                 return SIGNATURE_CHECK_FAILED;
130         default:
131                 return SIGNATURE_CHECK_FAILED;
132         }
133         return SIGNATURE_CHECK_FAILED;
134 }
135
136 static const gchar *get_validity_str(unsigned long validity)
137 {
138         switch (gpg_err_code(validity)) {
139         case GPGME_VALIDITY_UNKNOWN:
140                 return _("Unknown");
141         case GPGME_VALIDITY_UNDEFINED:
142                 return _("Undefined");
143         case GPGME_VALIDITY_NEVER:
144                 return _("Never");
145         case GPGME_VALIDITY_MARGINAL:
146                 return _("Marginal");
147         case GPGME_VALIDITY_FULL:
148                 return _("Full");
149         case GPGME_VALIDITY_ULTIMATE:
150                 return _("Ultimate");
151         default:
152                 return _("Error");
153         }
154 }
155
156 static gchar *extract_name(const char *uid)
157 {
158         if (uid == NULL)
159                 return NULL;
160         if (!strncmp(uid, "CN=", 3)) {
161                 gchar *result = g_strdup(uid+3);
162                 if (strstr(result, ","))
163                         *(strstr(result, ",")) = '\0';
164                 return result;
165         } else if (strstr(uid, ",CN=")) {
166                 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
167                 if (strstr(result, ","))
168                         *(strstr(result, ",")) = '\0';
169                 return result;
170         } else {
171                 return g_strdup(uid);
172         }
173 }
174 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
175 {
176         gpgme_signature_t sig = NULL;
177         gchar *uname = NULL;
178         gpgme_key_t key;
179         gchar *result = NULL;
180         gpgme_error_t err = 0;
181         static gboolean warned = FALSE;
182
183         if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
184                 return g_strdup_printf(_("The signature can't be checked - %s"), privacy_get_error());
185         }
186
187         if (status == NULL) {
188                 return g_strdup(_("The signature has not been checked."));
189         }
190         sig = status->signatures;
191         if (sig == NULL) {
192                 return g_strdup(_("The signature has not been checked."));
193         }
194
195         err = gpgme_get_key(ctx, sig->fpr, &key, 0);
196         if (gpg_err_code(err) == GPG_ERR_NO_AGENT) {
197                 if (!warned)
198                         alertpanel_error(_("PGP Core: Can't get key - no gpg-agent running."));
199                 else
200                         g_warning(_("PGP Core: Can't get key - no gpg-agent running."));
201                 warned = TRUE;
202         } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) != GPG_ERR_EOF) {
203                 return g_strdup_printf(_("The signature can't be checked - %s"), 
204                         gpgme_strerror(err));
205         }
206         if (key)
207                 uname = extract_name(key->uids->uid);
208         else
209                 uname = g_strdup("<?>");
210         switch (gpg_err_code(sig->status)) {
211         case GPG_ERR_NO_ERROR:
212                 switch (gpg_err_code(sig->validity)) {
213                 case GPGME_VALIDITY_FULL:
214                 case GPGME_VALIDITY_ULTIMATE:
215                         result = g_strdup_printf(_("Good signature from %s."), uname);
216                         break;
217                 case GPGME_VALIDITY_MARGINAL:
218                 case GPGME_VALIDITY_UNKNOWN:
219                 case GPGME_VALIDITY_UNDEFINED:
220                 case GPGME_VALIDITY_NEVER:
221                 default:
222                         result = g_strdup_printf(_("Good signature (untrusted) from %s."), uname);
223                         break;
224                 }
225                 break;
226         case GPG_ERR_SIG_EXPIRED:
227                 result = g_strdup_printf(_("Expired signature from %s."), uname);
228                 break;
229         case GPG_ERR_KEY_EXPIRED:
230                 result = g_strdup_printf(_("Expired key from %s."), uname);
231                 break;
232         case GPG_ERR_BAD_SIGNATURE:
233                 result = g_strdup_printf(_("Bad signature from %s."), uname);
234                 break;
235         case GPG_ERR_NO_PUBKEY: {
236                 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
237                 result = g_strdup_printf(_("Key 0x%s not available to verify this signature."), id);
238                 g_free(id);
239                 break;
240                 }
241         default:
242                 result = g_strdup(_("The signature has not been checked."));
243                 break;
244         }
245         if (result == NULL)
246                 result = g_strdup(_("Error"));
247         g_free(uname);
248         return result;
249 }
250
251 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
252 {
253         gint i = 0;
254         gchar *ret;
255         GString *siginfo;
256         gpgme_signature_t sig = NULL;
257
258         siginfo = g_string_sized_new(64);
259         if (status == NULL) {
260                 g_string_append_printf(siginfo,
261                         _("Error checking signature: no status\n"));
262                 goto bail;
263          }
264
265         sig = status->signatures;
266         
267         while (sig) {
268                 gpgme_user_id_t user = NULL;
269                 gpgme_key_t key;
270                 gpgme_error_t err;
271                 const gchar *keytype, *keyid, *uid;
272                 
273                 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
274
275                 if (err != GPG_ERR_NO_ERROR) {
276                         key = NULL;
277                         g_string_append_printf(siginfo, 
278                                 _("Error checking signature: %s\n"),
279                                 gpgme_strerror(err));
280                         goto bail;
281                 }
282                 if (key) {
283                         user = key->uids;
284                         keytype = gpgme_pubkey_algo_name(
285                                         key->subkeys->pubkey_algo);
286                         keyid = key->subkeys->keyid;
287                         uid = user->uid;
288                 } else {
289                         keytype = "?";
290                         keyid = "?";
291                         uid = "?";
292                 }
293                 g_string_append_printf(siginfo,
294                         _("Signature made using %s key ID %s\n"),
295                         keytype, keyid);
296                 
297                 switch (gpg_err_code(sig->status)) {
298                 case GPG_ERR_NO_ERROR:
299                 case GPG_ERR_KEY_EXPIRED:
300                         g_string_append_printf(siginfo,
301                                 _("Good signature from uid \"%s\" (Validity: %s)\n"),
302                                 uid, get_validity_str(user?user->validity:GPGME_VALIDITY_UNKNOWN));
303                         break;
304                 case GPG_ERR_SIG_EXPIRED:
305                         g_string_append_printf(siginfo,
306                                 _("Expired signature from uid \"%s\" (Validity: %s)\n"),
307                                 uid, get_validity_str(user?user->validity:GPGME_VALIDITY_UNKNOWN));
308                         break;
309                 case GPG_ERR_BAD_SIGNATURE:
310                         g_string_append_printf(siginfo,
311                                 _("BAD signature from \"%s\"\n"),
312                                 uid);
313                         break;
314                 default:
315                         break;
316                 }
317                 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
318                         gint j = 1;
319                         user = user ? user->next : NULL;
320                         while (user != NULL) {
321                                 g_string_append_printf(siginfo,
322                                         _("                    uid \"%s\" (Validity: %s)\n"),
323                                         user->uid,
324                                         get_validity_str(user->validity));
325                                 j++;
326                                 user = user->next;
327                         }
328                         g_string_append(siginfo,
329                                 _("Primary key fingerprint:"));
330                         const char* primary_fpr = NULL;
331                         if (key && key->subkeys && key->subkeys->fpr)
332                                 primary_fpr = key->subkeys->fpr;
333                         else
334                                 g_string_append(siginfo, " ?");
335                         int idx; /* now pretty-print the fingerprint */
336                         for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
337                                 if (idx%4==0)
338                                         g_string_append_c(siginfo, ' ');
339                                 if (idx%20==0)
340                                         g_string_append_c(siginfo, ' ');
341                                 g_string_append_c(siginfo, (gchar)*primary_fpr);
342                         }
343                         g_string_append_c(siginfo, '\n');
344 #ifdef HAVE_GPGME_PKA_TRUST
345                         if (sig->pka_trust == 1 && sig->pka_address) {
346                                 g_string_append_printf(siginfo,
347                                    _("WARNING: Signer's address \"%s\" "
348                                       "does not match DNS entry\n"), 
349                                    sig->pka_address);
350                         }
351                         else if (sig->pka_trust == 2 && sig->pka_address) {
352                                 g_string_append_printf(siginfo,
353                                    _("Verified signer's address is \"%s\"\n"),
354                                    sig->pka_address);
355                                 /* FIXME: Compare the address to the
356                                  * From: address.  */
357                         }
358 #endif /*HAVE_GPGME_PKA_TRUST*/
359                 }
360
361                 g_string_append(siginfo, "\n");
362                 i++;
363                 sig = sig->next;
364         }
365 bail:
366         ret = siginfo->str;
367         g_string_free(siginfo, FALSE);
368         return ret;
369 }
370
371 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
372 {
373         gpgme_data_t data = NULL;
374         gpgme_error_t err;
375         FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
376         gchar *tmp_file = NULL;
377
378         if (!fp) 
379                 return NULL;
380
381         tmp_file = get_tmp_file();
382         copy_file_part(fp, mimeinfo->offset, mimeinfo->length, tmp_file);
383         fclose(fp);
384         fp = NULL;
385         debug_print("tmp file %s\n", tmp_file);
386         
387         err = gpgme_data_new_from_file(&data, tmp_file, 1);
388         claws_unlink(tmp_file);
389         g_free(tmp_file);
390
391         debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
392         if (err) {
393                 debug_print ("gpgme_data_new_from_file failed: %s\n",
394                              gpgme_strerror (err));
395                 privacy_set_error(_("Couldn't get data from message, %s"), gpgme_strerror(err));
396                 return NULL;
397         }
398         return data;
399 }
400
401 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
402 {
403         struct passphrase_cb_info_s info;
404         gpgme_data_t plain;
405         gpgme_error_t err;
406
407         memset (&info, 0, sizeof info);
408         
409         if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
410                 gpgme_release(ctx);
411                 privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
412                 return NULL;
413         }
414         
415         if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
416                 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
417                 if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
418                         info.c = ctx;
419                         gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
420                 }
421         } else {
422                 prefs_gpg_enable_agent(TRUE);
423                 info.c = ctx;
424                 gpgme_set_passphrase_cb (ctx, NULL, &info);
425         }
426         
427         
428         if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
429                 err = gpgme_op_decrypt_verify(ctx, cipher, plain);
430                 if (err != GPG_ERR_NO_ERROR) {
431                         debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
432                         privacy_set_error("%s", gpgme_strerror(err));
433                         gpgmegtk_free_passphrase();
434                         gpgme_data_release(plain);
435                         return NULL;
436                 }
437
438                 err = cm_gpgme_data_rewind(plain);
439                 if (err) {
440                         debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
441                 }
442
443                 debug_print("decrypted.\n");
444                 *status = gpgme_op_verify_result (ctx);
445         } else {
446                 err = gpgme_op_decrypt(ctx, cipher, plain);
447                 if (err != GPG_ERR_NO_ERROR) {
448                         debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
449                         privacy_set_error("%s", gpgme_strerror(err));
450                         gpgmegtk_free_passphrase();
451                         gpgme_data_release(plain);
452                         return NULL;
453                 }
454
455                 err = cm_gpgme_data_rewind(plain);
456                 if (err) {
457                         debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
458                 }
459
460                 debug_print("decrypted.\n");
461                 *status = gpgme_op_verify_result (ctx);
462         }
463         return plain;
464 }
465
466 gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
467 {
468         SelectionResult result = KEY_SELECTION_CANCEL;
469         gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result,
470                                 proto);
471         gchar *ret = NULL;
472         int i = 0;
473
474         if (!keys) {
475                 if (result == KEY_SELECTION_DONT)
476                         return g_strdup("_DONT_ENCRYPT_");
477                 else
478                         return NULL;
479         }
480         while (keys[i]) {
481                 gpgme_subkey_t skey = keys[i]->subkeys;
482                 gchar *fpr = skey->fpr;
483                 gchar *tmp = NULL;
484                 debug_print("adding %s\n", fpr);
485                 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
486                 g_free(ret);
487                 ret = tmp;
488                 i++;
489         }
490         return ret;
491 }
492
493 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
494                               const gchar *from_addr)
495 {
496         GPGAccountConfig *config;
497         const gchar *signer_addr = account->address;
498
499         gpgme_signers_clear(ctx);
500
501         if (from_addr)
502                 signer_addr = from_addr;
503         config = prefs_gpg_account_get_config(account);
504
505         switch(config->sign_key) {
506         case SIGN_KEY_DEFAULT:
507                 debug_print("using default gnupg key\n");
508                 break;
509         case SIGN_KEY_BY_FROM:
510                 debug_print("using key for %s\n", signer_addr);
511                 break;
512         case SIGN_KEY_CUSTOM:
513                 debug_print("using key for %s\n", config->sign_key_id);
514                 break;
515         }
516
517         if (config->sign_key != SIGN_KEY_DEFAULT) {
518                 const gchar *keyid;
519                 gpgme_key_t key, key2;
520                 gpgme_error_t err;
521
522                 if (config->sign_key == SIGN_KEY_BY_FROM)
523                         keyid = signer_addr;
524                 else if (config->sign_key == SIGN_KEY_CUSTOM)
525                         keyid = config->sign_key_id;
526                 else
527                         goto bail;
528
529                 err = gpgme_op_keylist_start(ctx, keyid, 1);
530                 if (!err) {
531                         do {
532                                 err = gpgme_op_keylist_next(ctx, &key);
533                                 if (!err && key && key->protocol == gpgme_get_protocol(ctx) &&
534                                     !key->expired && !key->revoked && !key->disabled)
535                                         break;
536                                 if (!err && key && key->protocol != gpgme_get_protocol(ctx)) {
537                                         debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
538                                         gpgme_key_release(key);
539                                 }
540                                 if (!err && key && (key->expired || key->revoked || key->disabled)) {
541                                         
542                                         debug_print("skipping a key");
543                                         if (key->expired) 
544                                                 debug_print(" expired");
545                                         if (key->revoked) 
546                                                 debug_print(" revoked");
547                                         if (key->disabled) 
548                                                 debug_print(" disabled");
549                                         debug_print("\n");
550                                         gpgme_key_release(key);
551                                 }
552                         } while (!err);
553                 }
554                 if (err) {
555                         g_warning("setup_signers start: %s", gpgme_strerror(err));
556                         privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
557                         goto bail;
558                 }
559                 
560                 do {
561                         err = gpgme_op_keylist_next(ctx, &key2);
562                         if (!err && key2 && key2->protocol == gpgme_get_protocol(ctx) &&
563                             !key2->expired && !key2->revoked && !key2->disabled)
564                                 break;
565                         if (!err && key2 && key2->protocol != gpgme_get_protocol(ctx)) {
566                                 debug_print("skipping a key (wrong protocol %d)\n", key2->protocol);
567                                 gpgme_key_release(key2);
568                         }
569                         if (!err && key2 && (key2->expired || key2->revoked || key2->disabled)) {
570                                         debug_print("skipping a key");
571                                         if (key2->expired) 
572                                                 debug_print(" expired");
573                                         if (key2->revoked) 
574                                                 debug_print(" revoked");
575                                         if (key2->disabled) 
576                                                 debug_print(" disabled");
577                                         debug_print("\n");
578                                 gpgme_key_release(key2);
579                         }
580                 } while (!err);
581                 if (!err) {
582                         gpgme_key_release(key2);
583                         g_warning("ambiguous specification of secret key '%s'\n",
584                                 keyid);
585                         privacy_set_error(_("Secret key specification is ambiguous"));
586                         goto bail;
587                 }
588                 
589                 gpgme_op_keylist_end(ctx);
590                 err = gpgme_signers_add(ctx, key);
591                 debug_print("got key (proto %d (pgp %d, smime %d).\n", key->protocol,
592                                 GPGME_PROTOCOL_OpenPGP, GPGME_PROTOCOL_CMS);
593                 gpgme_key_release(key);
594                 
595                 if (err) {
596                         g_warning("error adding secret key: %s\n", gpgme_strerror(err));
597                         privacy_set_error(_("Error setting secret key: %s"), gpgme_strerror(err));
598                         goto bail;
599                 }
600         }
601
602         prefs_gpg_account_free_config(config);
603
604         return TRUE;
605 bail:
606         prefs_gpg_account_free_config(config);
607         return FALSE;
608 }
609
610 void sgpgme_init()
611 {
612         gchar *ctype_locale = NULL, *messages_locale = NULL;
613         gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL;
614
615         gpgme_engine_info_t engineInfo;
616         if (gpgme_check_version("1.0.0")) {
617 #ifdef LC_CTYPE
618                 debug_print("setting gpgme CTYPE locale\n");
619 #ifdef G_OS_WIN32
620                 ctype_locale = g_win32_getlocale();
621 #else
622                 ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
623 #endif
624                 debug_print("setting gpgme locale to: %s\n", ctype_locale ? ctype_locale : "NULL");
625                 if (strchr(ctype_locale, '.'))
626                         *(strchr(ctype_locale, '.')) = '\0';
627                 else if (strchr(ctype_locale, '@'))
628                         *(strchr(ctype_locale, '@')) = '\0';
629                 ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL);
630
631                 debug_print("setting gpgme locale to UTF8: %s\n", ctype_utf8_locale ? ctype_utf8_locale : "NULL");
632                 gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale);
633
634                 debug_print("done\n");
635                 g_free(ctype_utf8_locale);
636                 g_free(ctype_locale);
637 #endif
638 #ifdef LC_MESSAGES
639                 debug_print("setting gpgme MESSAGES locale\n");
640 #ifdef G_OS_WIN32
641                 messages_locale = g_win32_getlocale();
642 #else
643                 messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
644 #endif
645                 debug_print("setting gpgme locale to: %s\n", messages_locale ? messages_locale : "NULL");
646                 if (strchr(messages_locale, '.'))
647                         *(strchr(messages_locale, '.')) = '\0';
648                 else if (strchr(messages_locale, '@'))
649                         *(strchr(messages_locale, '@')) = '\0';
650                 messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL);
651                 debug_print("setting gpgme locale to UTF8: %s\n", messages_utf8_locale ? messages_utf8_locale : "NULL");
652
653                 gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale);
654
655                 debug_print("done\n");
656                 g_free(messages_utf8_locale);
657                 g_free(messages_locale);
658 #endif
659                 if (!gpgme_get_engine_info(&engineInfo)) {
660                         while (engineInfo) {
661 #ifndef G_OS_WIN32
662                                 debug_print("GpgME Protocol: %s\n"
663                                             "Version: %s (req %s)\n"
664                                             "Executable: %s\n",
665                                         gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
666                                         engineInfo->version ? engineInfo->version:"???",
667                                         engineInfo->req_version ? engineInfo->req_version:"???",
668                                         engineInfo->file_name ? engineInfo->file_name:"???");
669 #endif
670                                 if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
671                                 &&  gpgme_engine_check_version(engineInfo->protocol) != 
672                                         GPG_ERR_NO_ERROR) {
673                                         if (engineInfo->file_name && !engineInfo->version) {
674                                                 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
675                                                                    "Engine '%s' isn't installed properly."),
676                                                                    gpgme_get_protocol_name(engineInfo->protocol),
677                                                                    engineInfo->file_name);
678                                         } else if (engineInfo->file_name && engineInfo->version
679                                           && engineInfo->req_version) {
680                                                 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
681                                                                    "Engine '%s' version %s is installed, "
682                                                                    "but version %s is required.\n"),
683                                                                    gpgme_get_protocol_name(engineInfo->protocol),
684                                                                    engineInfo->file_name,
685                                                                    engineInfo->version,
686                                                                    engineInfo->req_version);
687                                         } else {
688                                                 alertpanel_error(_("Gpgme protocol '%s' is unusable "
689                                                                    "(unknown problem)"),
690                                                                    gpgme_get_protocol_name(engineInfo->protocol));
691                                         }
692                                 }
693                                 engineInfo = engineInfo->next;
694                         }
695                 }
696         } else {
697                 sgpgme_disable_all();
698
699                 if (prefs_gpg_get_config()->gpg_warning) {
700                         AlertValue val;
701
702                         val = alertpanel_full
703                                 (_("Warning"),
704                                  _("GnuPG is not installed properly, or needs "
705                                  "to be upgraded.\n"
706                                  "OpenPGP support disabled."),
707                                  GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
708                                  ALERT_WARNING, G_ALERTDEFAULT);
709                         if (val & G_ALERTDISABLE)
710                                 prefs_gpg_get_config()->gpg_warning = FALSE;
711                 }
712         }
713 }
714
715 void sgpgme_done()
716 {
717         gpgmegtk_free_passphrase();
718 }
719
720 void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
721 {
722         AlertValue val = G_ALERTDEFAULT;
723         gchar *key_parms = NULL;
724         gchar *name = NULL;
725         gchar *email = NULL;
726         gchar *passphrase = NULL, *passphrase_second = NULL;
727         gint prev_bad = 0;
728         gchar *tmp = NULL;
729         gpgme_error_t err = 0;
730         gpgme_ctx_t ctx;
731         GtkWidget *window = NULL;
732         gpgme_genkey_result_t key;
733
734         if (account == NULL)
735                 account = account_get_default();
736
737         if (account->address == NULL) {
738                 alertpanel_error(_("You have to save the account's information with \"OK\" "
739                                    "before being able to generate a key pair.\n"));
740                 return;
741         }
742         if (ask_create) {
743                 val = alertpanel(_("No PGP key found"),
744                                 _("Claws Mail did not find a secret PGP key, "
745                                   "which means that you won't be able to sign "
746                                   "emails or receive encrypted emails.\n"
747                                   "Do you want to create a new key pair now?"),
748                                   GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
749                 if (val == G_ALERTDEFAULT) {
750                         prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
751                         prefs_gpg_save_config();
752                         return;
753                 }
754         }
755
756         if (account->name) {
757                 name = g_strdup(account->name);
758         } else {
759                 name = g_strdup(account->address);
760         }
761         email = g_strdup(account->address);
762         tmp = g_strdup_printf("%s <%s>", account->name?account->name:account->address, account->address);
763 again:
764         passphrase = passphrase_mbox(tmp, NULL, prev_bad, 1);
765         if (passphrase == NULL) {
766                 g_free(tmp);
767                 g_free(email);
768                 g_free(name);           
769                 return;
770         }
771         passphrase_second = passphrase_mbox(tmp, NULL, 0, 2);
772         if (passphrase_second == NULL) {
773                 g_free(tmp);
774                 g_free(email);
775                 g_free(passphrase);             
776                 g_free(name);           
777                 return;
778         }
779         if (strcmp(passphrase, passphrase_second)) {
780                 g_free(passphrase);
781                 g_free(passphrase_second);
782                 prev_bad = 1;
783                 goto again;
784         }
785         
786         key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
787                                         "Key-Type: DSA\n"
788                                         "Key-Length: 1024\n"
789                                         "Subkey-Type: ELG-E\n"
790                                         "Subkey-Length: 2048\n"
791                                         "Name-Real: %s\n"
792                                         "Name-Email: %s\n"
793                                         "Expire-Date: 0\n"
794                                         "%s%s%s"
795                                         "</GnupgKeyParms>\n",
796                                         name, email, 
797                                         strlen(passphrase)?"Passphrase: ":"",
798                                         passphrase,
799                                         strlen(passphrase)?"\n":"");
800 #ifndef G_PLATFORM_WIN32
801         if (mlock(passphrase, strlen(passphrase)) == -1)
802                 debug_print("couldn't lock passphrase\n");
803         if (mlock(passphrase_second, strlen(passphrase_second)) == -1)
804                 debug_print("couldn't lock passphrase2\n");
805 #endif
806         g_free(tmp);
807         g_free(email);
808         g_free(name);
809         g_free(passphrase_second);
810         g_free(passphrase);
811         
812         err = gpgme_new (&ctx);
813         if (err) {
814                 alertpanel_error(_("Couldn't generate a new key pair: %s"),
815                                  gpgme_strerror(err));
816                 g_free(key_parms);
817                 return;
818         }
819         
820
821         window = label_window_create(_("Generating your new key pair... Please move the mouse "
822                               "around to help generate entropy..."));
823
824         err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
825         g_free(key_parms);
826
827         label_window_destroy(window);
828
829         if (err) {
830                 alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
831                 gpgme_release(ctx);
832                 return;
833         }
834         key = gpgme_op_genkey_result(ctx);
835         if (key == NULL) {
836                 alertpanel_error(_("Couldn't generate a new key pair: unknown error"));
837                 gpgme_release(ctx);
838                 return;
839         } else {
840                 gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
841                                     "Its fingerprint is:\n%s\n\nDo you want to export it "
842                                     "to a keyserver?"),
843                                     key->fpr ? key->fpr:"null");
844                 AlertValue val = alertpanel(_("Key generated"), buf,
845                                   GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
846                 g_free(buf);
847                 if (val == G_ALERTALTERNATE) {
848 #ifndef G_OS_WIN32
849                         gchar *cmd = g_strdup_printf("gpg --no-tty --send-keys %s", key->fpr);
850                         int res = 0;
851                         pid_t pid = 0;
852                         pid = fork();
853                         if (pid == -1) {
854                                 res = -1;
855                         } else if (pid == 0) {
856                                 /* son */
857                                 res = system(cmd);
858                                 res = WEXITSTATUS(res);
859                                 _exit(res);
860                         } else {
861                                 int status = 0;
862                                 time_t start_wait = time(NULL);
863                                 res = -1;
864                                 do {
865                                         if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
866                                                 usleep(200000);
867                                         } else {
868                                                 res = WEXITSTATUS(status);
869                                                 break;
870                                         }
871                                         if (time(NULL) - start_wait > 5) {
872                                                 debug_print("SIGTERM'ing gpg\n");
873                                                 kill(pid, SIGTERM);
874                                         }
875                                         if (time(NULL) - start_wait > 6) {
876                                                 debug_print("SIGKILL'ing gpg\n");
877                                                 kill(pid, SIGKILL);
878                                                 break;
879                                         }
880                                 } while(1);
881                         }
882                         if (res == 0) {
883                                 alertpanel_notice(_("Key exported."));
884                         } else {
885                                 alertpanel_error(_("Couldn't export key."));
886                         }
887                         g_free(cmd);
888 #else
889                         alertpanel_error(_("Key export isn't implemented in Windows."));
890 #endif
891                 }
892         }
893         prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
894         prefs_gpg_save_config();
895         gpgme_release(ctx);
896 }
897
898 gboolean sgpgme_has_secret_key(void)
899 {
900         gpgme_error_t err = 0;
901         gpgme_ctx_t ctx;
902         gpgme_key_t key;
903
904         err = gpgme_new (&ctx);
905         if (err) {
906                 debug_print("err : %s\n", gpgme_strerror(err));
907                 return TRUE;
908         }
909 check_again:
910         err = gpgme_op_keylist_start(ctx, NULL, TRUE);
911         if (!err)
912                 err = gpgme_op_keylist_next(ctx, &key);
913         gpgme_op_keylist_end(ctx);
914         if (gpg_err_code(err) == GPG_ERR_EOF) {
915                 if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
916                         gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
917                         goto check_again;
918                 }
919                 gpgme_release(ctx);
920                 return FALSE;
921         } else {
922                 gpgme_release(ctx);
923                 return TRUE;
924         }
925 }
926
927 void sgpgme_check_create_key(void)
928 {
929         if (prefs_gpg_get_config()->gpg_ask_create_key &&
930             !sgpgme_has_secret_key()) {
931                 sgpgme_create_secret_key(NULL, TRUE);
932         } else {
933                 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
934                 prefs_gpg_save_config();
935         }       
936 }
937
938 void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
939 {
940         char buf[BUFSIZ];
941         void *result = NULL;
942         ssize_t r = 0;
943         size_t w = 0;
944         
945         if (data == NULL)
946                 return NULL;
947         if (len == NULL)
948                 return NULL;
949
950         /* I know it's deprecated, but we don't compile with _LARGEFILE */
951         cm_gpgme_data_rewind(data);
952         while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
953                 result = realloc(result, r + w);
954                 memcpy(result+w, buf, r);
955                 w += r;
956         }
957         
958         *len = w;
959
960         gpgme_data_release(data);
961         if (r < 0) {
962                 free(result);
963                 *len = 0;
964                 return NULL;
965         }
966         return result;
967 }
968
969 gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
970 {
971 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
972         if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
973                 return gpg_error_from_errno(errno);
974         else
975                 return 0;
976 #else
977         return gpgme_data_rewind(dh);
978 #endif
979 }
980
981 #endif /* USE_GPGME */