2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2016 the Claws Mail team
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.
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.
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/>.
21 #include "claws-features.h"
30 #include <glib/gi18n.h>
35 #include <sys/types.h>
37 # include <sys/wait.h>
42 #if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
43 # include <sys/signal.h>
54 #include "prefs_common.h"
56 #include "alertpanel.h"
57 #include "passphrase.h"
58 #include "prefs_gpg.h"
60 #include "select-keys.h"
62 #include "file-utils.h"
64 static void sgpgme_disable_all(void)
66 /* FIXME: set a flag, so that we don't bother the user with failed
70 gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig,
71 gpgme_data_t plain, gpgme_data_t dummy)
73 gpgme_verify_result_t status = NULL;
76 if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
77 debug_print("op_verify err %s\n", gpgme_strerror(err));
78 privacy_set_error("%s", gpgme_strerror(err));
79 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
81 status = gpgme_op_verify_result(ctx);
82 if (status && status->signatures == NULL) {
83 debug_print("no signature found\n");
84 privacy_set_error(_("No signature found"));
85 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
90 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_result_t status)
92 gpgme_signature_t sig = NULL;
94 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
95 debug_print("system error\n");
96 return SIGNATURE_CHECK_FAILED;
100 debug_print("status == NULL\n");
101 return SIGNATURE_UNCHECKED;
103 sig = status->signatures;
106 debug_print("sig == NULL\n");
107 return SIGNATURE_UNCHECKED;
110 debug_print("err code %d\n", gpg_err_code(sig->status));
111 switch (gpg_err_code(sig->status)) {
112 case GPG_ERR_NO_ERROR:
113 switch (sig->validity) {
114 case GPGME_VALIDITY_NEVER:
115 return SIGNATURE_INVALID;
116 case GPGME_VALIDITY_UNKNOWN:
117 case GPGME_VALIDITY_UNDEFINED:
118 case GPGME_VALIDITY_MARGINAL:
119 case GPGME_VALIDITY_FULL:
120 case GPGME_VALIDITY_ULTIMATE:
123 return SIGNATURE_CHECK_FAILED;
125 case GPG_ERR_SIG_EXPIRED:
126 case GPG_ERR_CERT_REVOKED:
127 return SIGNATURE_WARN;
128 case GPG_ERR_KEY_EXPIRED:
129 return SIGNATURE_KEY_EXPIRED;
130 case GPG_ERR_BAD_SIGNATURE:
131 return SIGNATURE_INVALID;
132 case GPG_ERR_NO_PUBKEY:
133 return SIGNATURE_CHECK_FAILED;
135 return SIGNATURE_CHECK_FAILED;
137 return SIGNATURE_CHECK_FAILED;
140 static const gchar *get_validity_str(unsigned long validity)
142 switch (gpg_err_code(validity)) {
143 case GPGME_VALIDITY_UNKNOWN:
145 case GPGME_VALIDITY_UNDEFINED:
146 return _("Undefined");
147 case GPGME_VALIDITY_NEVER:
149 case GPGME_VALIDITY_MARGINAL:
150 return _("Marginal");
151 case GPGME_VALIDITY_FULL:
153 case GPGME_VALIDITY_ULTIMATE:
154 return _("Ultimate");
160 static const gchar *get_owner_trust_str(unsigned long owner_trust)
162 switch (gpgme_err_code(owner_trust)) {
163 case GPGME_VALIDITY_NEVER:
164 return _("Untrusted");
165 case GPGME_VALIDITY_MARGINAL:
166 return _("Marginal");
167 case GPGME_VALIDITY_FULL:
169 case GPGME_VALIDITY_ULTIMATE:
170 return _("Ultimate");
176 gchar *get_gpg_executable_name()
178 gpgme_engine_info_t e;
180 if (!gpgme_get_engine_info(&e)) {
182 if (e->protocol == GPGME_PROTOCOL_OpenPGP
183 && e->file_name != NULL) {
184 debug_print("Found gpg executable: '%s'\n", e->file_name);
193 static gchar *get_gpg_version_string()
195 gpgme_engine_info_t e;
197 if (!gpgme_get_engine_info(&e)) {
199 if (e->protocol == GPGME_PROTOCOL_OpenPGP
200 && e->version != NULL) {
201 debug_print("Got OpenPGP version: '%s'\n", e->version);
210 static gchar *extract_name(const char *uid)
214 if (!strncmp(uid, "CN=", 3)) {
215 gchar *result = g_strdup(uid+3);
216 if (strstr(result, ","))
217 *(strstr(result, ",")) = '\0';
219 } else if (strstr(uid, ",CN=")) {
220 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
221 if (strstr(result, ","))
222 *(strstr(result, ",")) = '\0';
225 return g_strdup(uid);
228 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
230 gpgme_signature_t sig = NULL;
233 gchar *result = NULL;
234 gpgme_error_t err = 0;
235 static gboolean warned = FALSE;
237 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
238 return g_strdup_printf(_("The signature can't be checked - %s"), privacy_get_error());
241 if (status == NULL) {
242 return g_strdup(_("The signature has not been checked."));
244 sig = status->signatures;
246 return g_strdup(_("The signature has not been checked."));
249 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
250 if (gpg_err_code(err) == GPG_ERR_NO_AGENT) {
252 alertpanel_error(_("PGP Core: Can't get key - no gpg-agent running."));
254 g_warning("PGP Core: Can't get key - no gpg-agent running.");
256 } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) != GPG_ERR_EOF) {
257 return g_strdup_printf(_("The signature can't be checked - %s"),
258 gpgme_strerror(err));
262 uname = extract_name(key->uids->uid);
264 uname = g_strdup("<?>");
266 switch (gpg_err_code(sig->status)) {
267 case GPG_ERR_NO_ERROR:
268 switch ((key && key->uids) ? key->uids->validity : GPGME_VALIDITY_UNKNOWN) {
269 case GPGME_VALIDITY_ULTIMATE:
270 result = g_strdup_printf(_("Good signature from \"%s\" [ultimate]"), uname);
272 case GPGME_VALIDITY_FULL:
273 result = g_strdup_printf(_("Good signature from \"%s\" [full]"), uname);
275 case GPGME_VALIDITY_MARGINAL:
276 result = g_strdup_printf(_("Good signature from \"%s\" [marginal]"), uname);
278 case GPGME_VALIDITY_UNKNOWN:
279 case GPGME_VALIDITY_UNDEFINED:
280 case GPGME_VALIDITY_NEVER:
283 result = g_strdup_printf(_("Good signature from \"%s\""), uname);
285 result = g_strdup_printf(_("Key 0x%s not available to verify this signature"), sig->fpr);
290 case GPG_ERR_SIG_EXPIRED:
291 result = g_strdup_printf(_("Expired signature from \"%s\""), uname);
293 case GPG_ERR_KEY_EXPIRED:
294 result = g_strdup_printf(_("Good signature from \"%s\", but the key has expired"), uname);
296 case GPG_ERR_CERT_REVOKED:
297 result = g_strdup_printf(_("Good signature from \"%s\", but the key has been revoked"), uname);
299 case GPG_ERR_BAD_SIGNATURE:
300 result = g_strdup_printf(_("Bad signature from \"%s\""), uname);
302 case GPG_ERR_NO_PUBKEY: {
303 result = g_strdup_printf(_("Key 0x%s not available to verify this signature"), sig->fpr);
307 result = g_strdup(_("The signature has not been checked"));
311 result = g_strdup(_("Error"));
315 gpgme_key_release(key);
320 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
325 gpgme_signature_t sig = NULL;
327 siginfo = g_string_sized_new(64);
328 if (status == NULL) {
329 g_string_append_printf(siginfo,
330 _("Error checking signature: no status\n"));
334 sig = status->signatures;
341 const gchar *keytype, *keyid, *uid;
343 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
345 if (err != GPG_ERR_NO_ERROR) {
347 g_string_append_printf(siginfo,
348 _("Error checking signature: %s\n"),
349 gpgme_strerror(err));
353 keytype = gpgme_pubkey_algo_name(
354 key->subkeys->pubkey_algo);
355 keyid = key->subkeys->keyid;
356 uid = key->uids->uid;
363 memset(buf, 0, sizeof(buf));
364 fast_strftime(buf, sizeof(buf)-1, prefs_common_get_prefs()->date_format, localtime_r((time_t *)&sig->timestamp, <));
365 g_string_append_printf(siginfo,
366 _("Signature made on %s using %s key ID %s\n"),
367 buf, keytype, keyid);
369 switch (gpg_err_code(sig->status)) {
370 case GPG_ERR_NO_ERROR:
371 g_string_append_printf(siginfo,
372 _("Good signature from uid \"%s\" (Validity: %s)\n"),
373 uid, get_validity_str((key && key->uids) ? key->uids->validity:GPGME_VALIDITY_UNKNOWN));
375 case GPG_ERR_KEY_EXPIRED:
376 g_string_append_printf(siginfo,
377 _("Expired key uid \"%s\"\n"),
380 case GPG_ERR_SIG_EXPIRED:
381 g_string_append_printf(siginfo,
382 _("Expired signature from uid \"%s\" (Validity: %s)\n"),
383 uid, get_validity_str((key && key->uids) ? key->uids->validity:GPGME_VALIDITY_UNKNOWN));
385 case GPG_ERR_CERT_REVOKED:
386 g_string_append_printf(siginfo,
387 _("Revoked key uid \"%s\"\n"),
390 case GPG_ERR_BAD_SIGNATURE:
391 g_string_append_printf(siginfo,
392 _("BAD signature from \"%s\"\n"),
398 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
401 key->uids = key->uids ? key->uids->next : NULL;
402 while (key->uids != NULL) {
403 g_string_append_printf(siginfo,
405 _("uid \"%s\" (Validity: %s)\n"), NULL),
407 key->uids->revoked==TRUE?_("Revoked"):get_validity_str(key->uids->validity));
409 key->uids = key->uids->next;
412 g_string_append_printf(siginfo,_("Owner Trust: %s\n"),
413 key ? get_owner_trust_str(key->owner_trust) : _("No key!"));
414 g_string_append(siginfo,
415 _("Primary key fingerprint:"));
416 const char* primary_fpr = NULL;
417 if (key && key->subkeys && key->subkeys->fpr)
418 primary_fpr = key->subkeys->fpr;
420 g_string_append(siginfo, " ?");
421 int idx; /* now pretty-print the fingerprint */
422 for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
424 g_string_append_c(siginfo, ' ');
426 g_string_append_c(siginfo, ' ');
427 g_string_append_c(siginfo, (gchar)*primary_fpr);
429 g_string_append_c(siginfo, '\n');
430 #ifdef HAVE_GPGME_PKA_TRUST
431 if (sig->pka_trust == 1 && sig->pka_address) {
432 g_string_append_printf(siginfo,
433 _("WARNING: Signer's address \"%s\" "
434 "does not match DNS entry\n"),
437 else if (sig->pka_trust == 2 && sig->pka_address) {
438 g_string_append_printf(siginfo,
439 _("Verified signer's address is \"%s\"\n"),
441 /* FIXME: Compare the address to the
444 #endif /*HAVE_GPGME_PKA_TRUST*/
447 g_string_append(siginfo, "\n");
453 g_string_free(siginfo, FALSE);
457 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
459 gpgme_data_t data = NULL;
461 FILE *fp = claws_fopen(mimeinfo->data.filename, "rb");
466 err = gpgme_data_new_from_filepart(&data, NULL, fp, mimeinfo->offset, mimeinfo->length);
469 debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
471 debug_print ("gpgme_data_new_from_file failed: %s\n",
472 gpgme_strerror (err));
473 privacy_set_error(_("Couldn't get data from message, %s"), gpgme_strerror(err));
479 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
481 struct passphrase_cb_info_s info;
485 memset (&info, 0, sizeof info);
487 if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
489 privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
493 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
494 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
495 if (!g_getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
497 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
500 prefs_gpg_enable_agent(TRUE);
502 gpgme_set_passphrase_cb (ctx, NULL, &info);
506 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
507 err = gpgme_op_decrypt_verify(ctx, cipher, plain);
508 if (err != GPG_ERR_NO_ERROR) {
509 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
510 privacy_set_error("%s", gpgme_strerror(err));
511 gpgmegtk_free_passphrase();
512 gpgme_data_release(plain);
516 err = cm_gpgme_data_rewind(plain);
518 debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
521 debug_print("decrypted.\n");
522 *status = gpgme_op_verify_result (ctx);
524 err = gpgme_op_decrypt(ctx, cipher, plain);
525 if (err != GPG_ERR_NO_ERROR) {
526 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
527 privacy_set_error("%s", gpgme_strerror(err));
528 gpgmegtk_free_passphrase();
529 gpgme_data_release(plain);
533 err = cm_gpgme_data_rewind(plain);
535 debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
538 debug_print("decrypted.\n");
539 *status = gpgme_op_verify_result (ctx);
544 gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
546 SelectionResult result = KEY_SELECTION_CANCEL;
547 gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result,
553 if (result == KEY_SELECTION_DONT)
554 return g_strdup("_DONT_ENCRYPT_");
559 gpgme_subkey_t skey = keys[i]->subkeys;
560 gchar *fpr = skey->fpr;
562 debug_print("adding %s\n", fpr);
563 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
571 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
572 const gchar *from_addr)
574 GPGAccountConfig *config;
575 const gchar *signer_addr = account->address;
578 gboolean smime = FALSE;
580 gpgme_signers_clear(ctx);
582 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_CMS)
586 signer_addr = from_addr;
587 config = prefs_gpg_account_get_config(account);
590 debug_print("sgpgme_setup_signers: S/MIME protocol\n");
591 sk = config->smime_sign_key;
592 skid = config->smime_sign_key_id;
594 debug_print("sgpgme_setup_signers: OpenPGP protocol\n");
595 sk = config->sign_key;
596 skid = config->sign_key_id;
600 case SIGN_KEY_DEFAULT:
601 debug_print("using default gnupg key\n");
603 case SIGN_KEY_BY_FROM:
604 debug_print("using key for %s\n", signer_addr);
606 case SIGN_KEY_CUSTOM:
607 debug_print("using key for %s\n", skid);
611 if (sk != SIGN_KEY_DEFAULT) {
613 gpgme_key_t key, found_key;
616 if (sk == SIGN_KEY_BY_FROM)
618 else if (sk == SIGN_KEY_CUSTOM)
624 /* Look for any key, not just private ones, or GPGMe doesn't
625 * correctly set the revoked flag. */
626 err = gpgme_op_keylist_start(ctx, keyid, 0);
628 if ((err = gpgme_op_keylist_next(ctx, &key)) != 0)
634 if (!key->can_sign) {
635 debug_print("skipping a key, can not be used for signing\n");
636 gpgme_key_unref(key);
640 if (key->protocol != gpgme_get_protocol(ctx)) {
641 debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
642 gpgme_key_unref(key);
647 debug_print("skipping a key, expired\n");
648 gpgme_key_unref(key);
652 debug_print("skipping a key, revoked\n");
653 gpgme_key_unref(key);
657 debug_print("skipping a key, disabled\n");
658 gpgme_key_unref(key);
662 if (found_key != NULL) {
663 gpgme_key_unref(key);
664 gpgme_op_keylist_end(ctx);
665 g_warning("ambiguous specification of secret key '%s'", keyid);
666 privacy_set_error(_("Secret key specification is ambiguous"));
672 gpgme_op_keylist_end(ctx);
674 if (found_key == NULL) {
675 g_warning("setup_signers start: %s", gpgme_strerror(err));
676 privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
680 err = gpgme_signers_add(ctx, found_key);
681 debug_print("got key (proto %d (pgp %d, smime %d).\n",
682 found_key->protocol, GPGME_PROTOCOL_OpenPGP,
684 gpgme_key_unref(found_key);
687 g_warning("error adding secret key: %s",
688 gpgme_strerror(err));
689 privacy_set_error(_("Error setting secret key: %s"),
690 gpgme_strerror(err));
695 prefs_gpg_account_free_config(config);
699 prefs_gpg_account_free_config(config);
705 gchar *ctype_locale = NULL, *messages_locale = NULL;
706 gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL;
707 gpgme_error_t err = 0;
709 gpgme_engine_info_t engineInfo;
711 if (strcmp(prefs_gpg_get_config()->gpg_path, "") != 0
712 && access(prefs_gpg_get_config()->gpg_path, X_OK) != -1) {
713 err = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, prefs_gpg_get_config()->gpg_path, NULL);
714 if (err != GPG_ERR_NO_ERROR)
715 g_warning("failed to set crypto engine configuration: %s", gpgme_strerror(err));
718 if (gpgme_check_version("1.0.0")) {
720 debug_print("setting gpgme CTYPE locale\n");
722 ctype_locale = g_win32_getlocale();
724 ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
727 debug_print("setting gpgme CTYPE locale to: %s\n", ctype_locale);
728 if (strchr(ctype_locale, '.'))
729 *(strchr(ctype_locale, '.')) = '\0';
730 else if (strchr(ctype_locale, '@'))
731 *(strchr(ctype_locale, '@')) = '\0';
732 ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL);
734 debug_print("setting gpgme locale to UTF8: %s\n", ctype_utf8_locale ? ctype_utf8_locale : "NULL");
735 gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale);
737 debug_print("done\n");
738 g_free(ctype_utf8_locale);
739 g_free(ctype_locale);
741 debug_print("couldn't set gpgme CTYPE locale\n");
745 debug_print("setting gpgme MESSAGES locale\n");
747 messages_locale = g_win32_getlocale();
749 messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
751 if (messages_locale) {
752 debug_print("setting gpgme MESSAGES locale to: %s\n", messages_locale);
753 if (strchr(messages_locale, '.'))
754 *(strchr(messages_locale, '.')) = '\0';
755 else if (strchr(messages_locale, '@'))
756 *(strchr(messages_locale, '@')) = '\0';
757 messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL);
758 debug_print("setting gpgme locale to UTF8: %s\n", messages_utf8_locale ? messages_utf8_locale : "NULL");
760 gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale);
762 debug_print("done\n");
763 g_free(messages_utf8_locale);
764 g_free(messages_locale);
766 debug_print("couldn't set gpgme MESSAGES locale\n");
769 if (!gpgme_get_engine_info(&engineInfo)) {
771 debug_print("GpgME Protocol: %s\n"
772 "Version: %s (req %s)\n"
774 gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
775 engineInfo->version ? engineInfo->version:"???",
776 engineInfo->req_version ? engineInfo->req_version:"???",
777 engineInfo->file_name ? engineInfo->file_name:"???");
778 if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
779 && gpgme_engine_check_version(engineInfo->protocol) !=
781 if (engineInfo->file_name && !engineInfo->version) {
782 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
783 "Engine '%s' isn't installed properly."),
784 gpgme_get_protocol_name(engineInfo->protocol),
785 engineInfo->file_name);
786 } else if (engineInfo->file_name && engineInfo->version
787 && engineInfo->req_version) {
788 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
789 "Engine '%s' version %s is installed, "
790 "but version %s is required.\n"),
791 gpgme_get_protocol_name(engineInfo->protocol),
792 engineInfo->file_name,
794 engineInfo->req_version);
796 alertpanel_error(_("Gpgme protocol '%s' is unusable "
797 "(unknown problem)"),
798 gpgme_get_protocol_name(engineInfo->protocol));
801 engineInfo = engineInfo->next;
805 sgpgme_disable_all();
807 if (prefs_gpg_get_config()->gpg_warning) {
810 val = alertpanel_full
812 _("GnuPG is not installed properly, or needs "
814 "OpenPGP support disabled."),
815 GTK_STOCK_CLOSE, NULL, NULL, ALERTFOCUS_FIRST, TRUE, NULL,
817 if (val & G_ALERTDISABLE)
818 prefs_gpg_get_config()->gpg_warning = FALSE;
825 gpgmegtk_free_passphrase();
835 static void *_export_threaded(void *arg)
837 struct _ExportCtx *ctx = (struct _ExportCtx *)arg;
840 PROCESS_INFORMATION pi = {0};
841 STARTUPINFO si = {0};
843 result = CreateProcess(NULL, ctx->cmd, NULL, NULL, FALSE,
844 NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
845 NULL, NULL, &si, &pi);
848 debug_print("Couldn't execute '%s'\n", ctx->cmd);
850 WaitForSingleObject(pi.hProcess, 10000);
851 result = GetExitCodeProcess(pi.hProcess, &ctx->exitcode);
852 if (ctx->exitcode == STILL_ACTIVE) {
853 debug_print("Process still running, terminating it.\n");
854 TerminateProcess(pi.hProcess, 255);
857 CloseHandle(pi.hProcess);
858 CloseHandle(pi.hThread);
861 debug_print("Process executed, but we couldn't get its exit code (huh?)\n");
870 void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
872 AlertValue val = G_ALERTDEFAULT;
873 gchar *key_parms = NULL;
876 gchar *passphrase = NULL, *passphrase_second = NULL;
878 gchar *tmp = NULL, *gpgver;
879 gpgme_error_t err = 0;
881 GtkWidget *window = NULL;
882 gpgme_genkey_result_t key;
883 gboolean exported = FALSE;
886 account = account_get_default();
888 if (account->address == NULL) {
889 alertpanel_error(_("You have to save the account's information with \"OK\" "
890 "before being able to generate a key pair.\n"));
894 val = alertpanel(_("No PGP key found"),
895 _("Claws Mail did not find a secret PGP key, "
896 "which means that you won't be able to sign "
897 "emails or receive encrypted emails.\n"
898 "Do you want to create a new key pair now?"),
899 GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
900 if (val == G_ALERTDEFAULT) {
906 name = g_strdup(account->name);
908 name = g_strdup(account->address);
910 email = g_strdup(account->address);
911 tmp = g_strdup_printf("%s <%s>", account->name?account->name:account->address, account->address);
912 gpgver = get_gpg_version_string();
913 if (gpgver == NULL || !strncmp(gpgver, "1.", 2)) {
914 debug_print("Using gpg 1.x, using builtin passphrase dialog.\n");
916 passphrase = passphrase_mbox(tmp, NULL, prev_bad, 1);
917 if (passphrase == NULL) {
923 passphrase_second = passphrase_mbox(tmp, NULL, 0, 2);
924 if (passphrase_second == NULL) {
927 if (passphrase != NULL) {
928 memset(passphrase, 0, strlen(passphrase));
934 if (strcmp(passphrase, passphrase_second)) {
935 if (passphrase != NULL) {
936 memset(passphrase, 0, strlen(passphrase));
939 if (passphrase_second != NULL) {
940 memset(passphrase_second, 0, strlen(passphrase_second));
941 g_free(passphrase_second);
948 key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
952 "Subkey-Length: 2048\n"
957 "</GnupgKeyParms>\n",
959 passphrase?"Passphrase: ":"",
960 passphrase?passphrase:"",
962 #ifndef G_PLATFORM_WIN32
964 mlock(passphrase, strlen(passphrase)) == -1)
965 debug_print("couldn't lock passphrase\n");
966 if (passphrase_second &&
967 mlock(passphrase_second, strlen(passphrase_second)) == -1)
968 debug_print("couldn't lock passphrase2\n");
973 if (passphrase_second != NULL) {
974 memset(passphrase_second, 0, strlen(passphrase_second));
975 g_free(passphrase_second);
977 if (passphrase != NULL) {
978 memset(passphrase, 0, strlen(passphrase));
982 err = gpgme_new (&ctx);
984 alertpanel_error(_("Couldn't generate a new key pair: %s"),
985 gpgme_strerror(err));
986 if (key_parms != NULL) {
987 memset(key_parms, 0, strlen(key_parms));
994 window = label_window_create(_("Generating your new key pair... Please move the mouse "
995 "around to help generate entropy..."));
997 err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
998 if (key_parms != NULL) {
999 memset(key_parms, 0, strlen(key_parms));
1003 label_window_destroy(window);
1006 alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
1010 key = gpgme_op_genkey_result(ctx);
1012 alertpanel_error(_("Couldn't generate a new key pair: unknown error"));
1016 gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
1017 "Its fingerprint is:\n%s\n\nDo you want to export it "
1019 key->fpr ? key->fpr:"null");
1020 AlertValue val = alertpanel(_("Key generated"), buf,
1021 GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
1023 if (val == G_ALERTALTERNATE) {
1024 gchar *gpgbin = get_gpg_executable_name();
1025 gchar *cmd = g_strdup_printf("\"%s\" --batch --no-tty --send-keys %s",
1026 (gpgbin ? gpgbin : "gpg"), key->fpr);
1027 debug_print("Executing command: %s\n", cmd);
1035 } else if (pid == 0) {
1038 res = WEXITSTATUS(res);
1042 time_t start_wait = time(NULL);
1045 if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
1048 res = WEXITSTATUS(status);
1051 if (time(NULL) - start_wait > 5) {
1052 debug_print("SIGTERM'ing gpg\n");
1055 if (time(NULL) - start_wait > 6) {
1056 debug_print("SIGKILL'ing gpg\n");
1066 /* We need to call gpg in a separate thread, so that waiting for
1067 * it to finish does not block the UI. */
1069 struct _ExportCtx *ectx = malloc(sizeof(struct _ExportCtx));
1072 ectx->exitcode = STILL_ACTIVE;
1075 if (pthread_create(&pt, NULL,
1076 _export_threaded, (void *)ectx) != 0) {
1077 debug_print("Couldn't create thread, continuing unthreaded.\n");
1078 _export_threaded(ctx);
1080 debug_print("Thread created, waiting for it to finish...\n");
1085 debug_print("Thread finished.\n");
1086 pthread_join(pt, NULL);
1088 if (ectx->exitcode == 0)
1096 alertpanel_notice(_("Key exported."));
1098 alertpanel_error(_("Couldn't export key."));
1105 gboolean sgpgme_has_secret_key(void)
1107 gpgme_error_t err = 0;
1111 err = gpgme_new (&ctx);
1113 debug_print("err : %s\n", gpgme_strerror(err));
1117 err = gpgme_op_keylist_start(ctx, NULL, TRUE);
1119 err = gpgme_op_keylist_next(ctx, &key);
1120 gpgme_key_unref(key); /* We're not interested in the key itself. */
1122 gpgme_op_keylist_end(ctx);
1123 if (gpg_err_code(err) == GPG_ERR_EOF) {
1124 if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
1125 gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
1136 void sgpgme_check_create_key(void)
1138 if (prefs_gpg_get_config()->gpg_ask_create_key &&
1139 !sgpgme_has_secret_key()) {
1140 sgpgme_create_secret_key(NULL, TRUE);
1143 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
1144 prefs_gpg_save_config();
1147 void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
1150 void *result = NULL;
1154 cm_return_val_if_fail(data != NULL, NULL);
1155 cm_return_val_if_fail(len != NULL, NULL);
1157 /* I know it's deprecated, but we don't compile with _LARGEFILE */
1158 cm_gpgme_data_rewind(data);
1159 while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
1160 void *rresult = realloc(result, r + w);
1161 if (rresult == NULL) {
1162 g_warning("can't allocate memory");
1168 memcpy(result+w, buf, r);
1174 gpgme_data_release(data);
1176 g_warning("gpgme_data_read() returned an error: %d", (int)r);
1184 gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
1186 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
1187 if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
1188 return gpg_error_from_errno(errno);
1192 return gpgme_data_rewind(dh);
1196 #endif /* USE_GPGME */