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"
63 static void sgpgme_disable_all(void)
65 /* FIXME: set a flag, so that we don't bother the user with failed
69 gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig,
70 gpgme_data_t plain, gpgme_data_t dummy)
72 gpgme_verify_result_t status = NULL;
75 if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
76 debug_print("op_verify err %s\n", gpgme_strerror(err));
77 privacy_set_error("%s", gpgme_strerror(err));
78 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
80 status = gpgme_op_verify_result(ctx);
81 if (status && status->signatures == NULL) {
82 debug_print("no signature found\n");
83 privacy_set_error(_("No signature found"));
84 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
89 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_result_t status)
91 gpgme_signature_t sig = NULL;
93 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
94 debug_print("system error\n");
95 return SIGNATURE_CHECK_FAILED;
99 debug_print("status == NULL\n");
100 return SIGNATURE_UNCHECKED;
102 sig = status->signatures;
105 debug_print("sig == NULL\n");
106 return SIGNATURE_UNCHECKED;
109 debug_print("err code %d\n", gpg_err_code(sig->status));
110 switch (gpg_err_code(sig->status)) {
111 case GPG_ERR_NO_ERROR:
112 switch (sig->validity) {
113 case GPGME_VALIDITY_NEVER:
114 return SIGNATURE_INVALID;
115 case GPGME_VALIDITY_UNKNOWN:
116 case GPGME_VALIDITY_UNDEFINED:
117 case GPGME_VALIDITY_MARGINAL:
118 case GPGME_VALIDITY_FULL:
119 case GPGME_VALIDITY_ULTIMATE:
122 return SIGNATURE_CHECK_FAILED;
124 case GPG_ERR_SIG_EXPIRED:
125 case GPG_ERR_CERT_REVOKED:
126 return SIGNATURE_WARN;
127 case GPG_ERR_KEY_EXPIRED:
128 return SIGNATURE_KEY_EXPIRED;
129 case GPG_ERR_BAD_SIGNATURE:
130 return SIGNATURE_INVALID;
131 case GPG_ERR_NO_PUBKEY:
132 return SIGNATURE_CHECK_FAILED;
134 return SIGNATURE_CHECK_FAILED;
136 return SIGNATURE_CHECK_FAILED;
139 static const gchar *get_validity_str(unsigned long validity)
141 switch (gpg_err_code(validity)) {
142 case GPGME_VALIDITY_UNKNOWN:
144 case GPGME_VALIDITY_UNDEFINED:
145 return _("Undefined");
146 case GPGME_VALIDITY_NEVER:
148 case GPGME_VALIDITY_MARGINAL:
149 return _("Marginal");
150 case GPGME_VALIDITY_FULL:
152 case GPGME_VALIDITY_ULTIMATE:
153 return _("Ultimate");
159 static const gchar *get_owner_trust_str(unsigned long owner_trust)
161 switch (gpgme_err_code(owner_trust)) {
162 case GPGME_VALIDITY_NEVER:
163 return _("Untrusted");
164 case GPGME_VALIDITY_MARGINAL:
165 return _("Marginal");
166 case GPGME_VALIDITY_FULL:
168 case GPGME_VALIDITY_ULTIMATE:
169 return _("Ultimate");
175 gchar *get_gpg_executable_name()
177 gpgme_engine_info_t e;
179 if (!gpgme_get_engine_info(&e)) {
181 if (e->protocol == GPGME_PROTOCOL_OpenPGP
182 && e->file_name != NULL) {
183 debug_print("Found gpg executable: '%s'\n", e->file_name);
192 static gchar *get_gpg_version_string()
194 gpgme_engine_info_t e;
196 if (!gpgme_get_engine_info(&e)) {
198 if (e->protocol == GPGME_PROTOCOL_OpenPGP
199 && e->version != NULL) {
200 debug_print("Got OpenPGP version: '%s'\n", e->version);
209 static gchar *extract_name(const char *uid)
213 if (!strncmp(uid, "CN=", 3)) {
214 gchar *result = g_strdup(uid+3);
215 if (strstr(result, ","))
216 *(strstr(result, ",")) = '\0';
218 } else if (strstr(uid, ",CN=")) {
219 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
220 if (strstr(result, ","))
221 *(strstr(result, ",")) = '\0';
224 return g_strdup(uid);
227 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
229 gpgme_signature_t sig = NULL;
232 gchar *result = NULL;
233 gpgme_error_t err = 0;
234 static gboolean warned = FALSE;
236 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
237 return g_strdup_printf(_("The signature can't be checked - %s"), privacy_get_error());
240 if (status == NULL) {
241 return g_strdup(_("The signature has not been checked."));
243 sig = status->signatures;
245 return g_strdup(_("The signature has not been checked."));
248 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
249 if (gpg_err_code(err) == GPG_ERR_NO_AGENT) {
251 alertpanel_error(_("PGP Core: Can't get key - no gpg-agent running."));
253 g_warning("PGP Core: Can't get key - no gpg-agent running.");
255 } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) != GPG_ERR_EOF) {
256 return g_strdup_printf(_("The signature can't be checked - %s"),
257 gpgme_strerror(err));
261 uname = extract_name(key->uids->uid);
263 uname = g_strdup("<?>");
265 switch (gpg_err_code(sig->status)) {
266 case GPG_ERR_NO_ERROR:
267 switch ((key && key->uids) ? key->uids->validity : GPGME_VALIDITY_UNKNOWN) {
268 case GPGME_VALIDITY_ULTIMATE:
269 result = g_strdup_printf(_("Good signature from \"%s\" [ultimate]"), uname);
271 case GPGME_VALIDITY_FULL:
272 result = g_strdup_printf(_("Good signature from \"%s\" [full]"), uname);
274 case GPGME_VALIDITY_MARGINAL:
275 result = g_strdup_printf(_("Good signature from \"%s\" [marginal]"), uname);
277 case GPGME_VALIDITY_UNKNOWN:
278 case GPGME_VALIDITY_UNDEFINED:
279 case GPGME_VALIDITY_NEVER:
282 result = g_strdup_printf(_("Good signature from \"%s\""), uname);
284 result = g_strdup_printf(_("Key 0x%s not available to verify this signature"), sig->fpr);
289 case GPG_ERR_SIG_EXPIRED:
290 result = g_strdup_printf(_("Expired signature from \"%s\""), uname);
292 case GPG_ERR_KEY_EXPIRED:
293 result = g_strdup_printf(_("Good signature from \"%s\", but the key has expired"), uname);
295 case GPG_ERR_CERT_REVOKED:
296 result = g_strdup_printf(_("Good signature from \"%s\", but the key has been revoked"), uname);
298 case GPG_ERR_BAD_SIGNATURE:
299 result = g_strdup_printf(_("Bad signature from \"%s\""), uname);
301 case GPG_ERR_NO_PUBKEY: {
302 result = g_strdup_printf(_("Key 0x%s not available to verify this signature"), sig->fpr);
306 result = g_strdup(_("The signature has not been checked"));
310 result = g_strdup(_("Error"));
315 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
320 gpgme_signature_t sig = NULL;
322 siginfo = g_string_sized_new(64);
323 if (status == NULL) {
324 g_string_append_printf(siginfo,
325 _("Error checking signature: no status\n"));
329 sig = status->signatures;
336 const gchar *keytype, *keyid, *uid;
338 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
340 if (err != GPG_ERR_NO_ERROR) {
342 g_string_append_printf(siginfo,
343 _("Error checking signature: %s\n"),
344 gpgme_strerror(err));
348 keytype = gpgme_pubkey_algo_name(
349 key->subkeys->pubkey_algo);
350 keyid = key->subkeys->keyid;
351 uid = key->uids->uid;
358 memset(buf, 0, sizeof(buf));
359 fast_strftime(buf, sizeof(buf)-1, prefs_common_get_prefs()->date_format, localtime_r(&sig->timestamp, <));
360 g_string_append_printf(siginfo,
361 _("Signature made on %s using %s key ID %s\n"),
362 buf, keytype, keyid);
364 switch (gpg_err_code(sig->status)) {
365 case GPG_ERR_NO_ERROR:
366 g_string_append_printf(siginfo,
367 _("Good signature from uid \"%s\" (Validity: %s)\n"),
368 uid, get_validity_str((key && key->uids) ? key->uids->validity:GPGME_VALIDITY_UNKNOWN));
370 case GPG_ERR_KEY_EXPIRED:
371 g_string_append_printf(siginfo,
372 _("Expired key uid \"%s\"\n"),
375 case GPG_ERR_SIG_EXPIRED:
376 g_string_append_printf(siginfo,
377 _("Expired signature from uid \"%s\" (Validity: %s)\n"),
378 uid, get_validity_str((key && key->uids) ? key->uids->validity:GPGME_VALIDITY_UNKNOWN));
380 case GPG_ERR_CERT_REVOKED:
381 g_string_append_printf(siginfo,
382 _("Revoked key uid \"%s\"\n"),
385 case GPG_ERR_BAD_SIGNATURE:
386 g_string_append_printf(siginfo,
387 _("BAD signature from \"%s\"\n"),
393 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
396 key->uids = key->uids ? key->uids->next : NULL;
397 while (key->uids != NULL) {
398 g_string_append_printf(siginfo,
400 _("uid \"%s\" (Validity: %s)\n"), NULL),
402 key->uids->revoked==TRUE?_("Revoked"):get_validity_str(key->uids->validity));
404 key->uids = key->uids->next;
407 g_string_append_printf(siginfo,_("Owner Trust: %s\n"),
408 key ? get_owner_trust_str(key->owner_trust) : _("No key!"));
409 g_string_append(siginfo,
410 _("Primary key fingerprint:"));
411 const char* primary_fpr = NULL;
412 if (key && key->subkeys && key->subkeys->fpr)
413 primary_fpr = key->subkeys->fpr;
415 g_string_append(siginfo, " ?");
416 int idx; /* now pretty-print the fingerprint */
417 for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
419 g_string_append_c(siginfo, ' ');
421 g_string_append_c(siginfo, ' ');
422 g_string_append_c(siginfo, (gchar)*primary_fpr);
424 g_string_append_c(siginfo, '\n');
425 #ifdef HAVE_GPGME_PKA_TRUST
426 if (sig->pka_trust == 1 && sig->pka_address) {
427 g_string_append_printf(siginfo,
428 _("WARNING: Signer's address \"%s\" "
429 "does not match DNS entry\n"),
432 else if (sig->pka_trust == 2 && sig->pka_address) {
433 g_string_append_printf(siginfo,
434 _("Verified signer's address is \"%s\"\n"),
436 /* FIXME: Compare the address to the
439 #endif /*HAVE_GPGME_PKA_TRUST*/
442 g_string_append(siginfo, "\n");
448 g_string_free(siginfo, FALSE);
452 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
454 gpgme_data_t data = NULL;
456 FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
461 err = gpgme_data_new_from_filepart(&data, NULL, fp, mimeinfo->offset, mimeinfo->length);
464 debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
466 debug_print ("gpgme_data_new_from_file failed: %s\n",
467 gpgme_strerror (err));
468 privacy_set_error(_("Couldn't get data from message, %s"), gpgme_strerror(err));
474 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
476 struct passphrase_cb_info_s info;
480 memset (&info, 0, sizeof info);
482 if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
484 privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
488 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
489 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
490 if (!g_getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
492 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
495 prefs_gpg_enable_agent(TRUE);
497 gpgme_set_passphrase_cb (ctx, NULL, &info);
501 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
502 err = gpgme_op_decrypt_verify(ctx, cipher, plain);
503 if (err != GPG_ERR_NO_ERROR) {
504 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
505 privacy_set_error("%s", gpgme_strerror(err));
506 gpgmegtk_free_passphrase();
507 gpgme_data_release(plain);
511 err = cm_gpgme_data_rewind(plain);
513 debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
516 debug_print("decrypted.\n");
517 *status = gpgme_op_verify_result (ctx);
519 err = gpgme_op_decrypt(ctx, cipher, plain);
520 if (err != GPG_ERR_NO_ERROR) {
521 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
522 privacy_set_error("%s", gpgme_strerror(err));
523 gpgmegtk_free_passphrase();
524 gpgme_data_release(plain);
528 err = cm_gpgme_data_rewind(plain);
530 debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
533 debug_print("decrypted.\n");
534 *status = gpgme_op_verify_result (ctx);
539 gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
541 SelectionResult result = KEY_SELECTION_CANCEL;
542 gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result,
548 if (result == KEY_SELECTION_DONT)
549 return g_strdup("_DONT_ENCRYPT_");
554 gpgme_subkey_t skey = keys[i]->subkeys;
555 gchar *fpr = skey->fpr;
557 debug_print("adding %s\n", fpr);
558 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
566 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
567 const gchar *from_addr)
569 GPGAccountConfig *config;
570 const gchar *signer_addr = account->address;
573 gboolean smime = FALSE;
575 gpgme_signers_clear(ctx);
577 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_CMS)
581 signer_addr = from_addr;
582 config = prefs_gpg_account_get_config(account);
585 debug_print("sgpgme_setup_signers: S/MIME protocol\n");
586 sk = config->smime_sign_key;
587 skid = config->smime_sign_key_id;
589 debug_print("sgpgme_setup_signers: OpenPGP protocol\n");
590 sk = config->sign_key;
591 skid = config->sign_key_id;
595 case SIGN_KEY_DEFAULT:
596 debug_print("using default gnupg key\n");
598 case SIGN_KEY_BY_FROM:
599 debug_print("using key for %s\n", signer_addr);
601 case SIGN_KEY_CUSTOM:
602 debug_print("using key for %s\n", skid);
606 if (sk != SIGN_KEY_DEFAULT) {
608 gpgme_key_t key, found_key;
611 if (sk == SIGN_KEY_BY_FROM)
613 else if (sk == SIGN_KEY_CUSTOM)
619 /* Look for any key, not just private ones, or GPGMe doesn't
620 * correctly set the revoked flag. */
621 err = gpgme_op_keylist_start(ctx, keyid, 0);
623 if ((err = gpgme_op_keylist_next(ctx, &key)) != 0)
629 if (!key->can_sign) {
630 debug_print("skipping a key, can not be used for signing\n");
631 gpgme_key_unref(key);
635 if (key->protocol != gpgme_get_protocol(ctx)) {
636 debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
637 gpgme_key_unref(key);
642 debug_print("skipping a key, expired\n");
643 gpgme_key_unref(key);
647 debug_print("skipping a key, revoked\n");
648 gpgme_key_unref(key);
652 debug_print("skipping a key, disabled\n");
653 gpgme_key_unref(key);
657 if (found_key != NULL) {
658 gpgme_key_unref(key);
659 gpgme_op_keylist_end(ctx);
660 g_warning("ambiguous specification of secret key '%s'", keyid);
661 privacy_set_error(_("Secret key specification is ambiguous"));
667 gpgme_op_keylist_end(ctx);
669 if (found_key == NULL) {
670 g_warning("setup_signers start: %s", gpgme_strerror(err));
671 privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
675 err = gpgme_signers_add(ctx, found_key);
676 debug_print("got key (proto %d (pgp %d, smime %d).\n",
677 found_key->protocol, GPGME_PROTOCOL_OpenPGP,
679 gpgme_key_unref(found_key);
682 g_warning("error adding secret key: %s",
683 gpgme_strerror(err));
684 privacy_set_error(_("Error setting secret key: %s"),
685 gpgme_strerror(err));
690 prefs_gpg_account_free_config(config);
694 prefs_gpg_account_free_config(config);
700 gchar *ctype_locale = NULL, *messages_locale = NULL;
701 gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL;
702 gpgme_error_t err = 0;
704 gpgme_engine_info_t engineInfo;
706 if (strcmp(prefs_gpg_get_config()->gpg_path, "") != 0
707 && access(prefs_gpg_get_config()->gpg_path, X_OK) != -1) {
708 err = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, prefs_gpg_get_config()->gpg_path, NULL);
709 if (err != GPG_ERR_NO_ERROR)
710 g_warning("failed to set crypto engine configuration: %s", gpgme_strerror(err));
713 if (gpgme_check_version("1.0.0")) {
715 debug_print("setting gpgme CTYPE locale\n");
717 ctype_locale = g_win32_getlocale();
719 ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
722 debug_print("setting gpgme CTYPE locale to: %s\n", ctype_locale);
723 if (strchr(ctype_locale, '.'))
724 *(strchr(ctype_locale, '.')) = '\0';
725 else if (strchr(ctype_locale, '@'))
726 *(strchr(ctype_locale, '@')) = '\0';
727 ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL);
729 debug_print("setting gpgme locale to UTF8: %s\n", ctype_utf8_locale ? ctype_utf8_locale : "NULL");
730 gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale);
732 debug_print("done\n");
733 g_free(ctype_utf8_locale);
734 g_free(ctype_locale);
736 debug_print("couldn't set gpgme CTYPE locale\n");
740 debug_print("setting gpgme MESSAGES locale\n");
742 messages_locale = g_win32_getlocale();
744 messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
746 if (messages_locale) {
747 debug_print("setting gpgme MESSAGES locale to: %s\n", messages_locale);
748 if (strchr(messages_locale, '.'))
749 *(strchr(messages_locale, '.')) = '\0';
750 else if (strchr(messages_locale, '@'))
751 *(strchr(messages_locale, '@')) = '\0';
752 messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL);
753 debug_print("setting gpgme locale to UTF8: %s\n", messages_utf8_locale ? messages_utf8_locale : "NULL");
755 gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale);
757 debug_print("done\n");
758 g_free(messages_utf8_locale);
759 g_free(messages_locale);
761 debug_print("couldn't set gpgme MESSAGES locale\n");
764 if (!gpgme_get_engine_info(&engineInfo)) {
766 debug_print("GpgME Protocol: %s\n"
767 "Version: %s (req %s)\n"
769 gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
770 engineInfo->version ? engineInfo->version:"???",
771 engineInfo->req_version ? engineInfo->req_version:"???",
772 engineInfo->file_name ? engineInfo->file_name:"???");
773 if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
774 && gpgme_engine_check_version(engineInfo->protocol) !=
776 if (engineInfo->file_name && !engineInfo->version) {
777 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
778 "Engine '%s' isn't installed properly."),
779 gpgme_get_protocol_name(engineInfo->protocol),
780 engineInfo->file_name);
781 } else if (engineInfo->file_name && engineInfo->version
782 && engineInfo->req_version) {
783 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
784 "Engine '%s' version %s is installed, "
785 "but version %s is required.\n"),
786 gpgme_get_protocol_name(engineInfo->protocol),
787 engineInfo->file_name,
789 engineInfo->req_version);
791 alertpanel_error(_("Gpgme protocol '%s' is unusable "
792 "(unknown problem)"),
793 gpgme_get_protocol_name(engineInfo->protocol));
796 engineInfo = engineInfo->next;
800 sgpgme_disable_all();
802 if (prefs_gpg_get_config()->gpg_warning) {
805 val = alertpanel_full
807 _("GnuPG is not installed properly, or needs "
809 "OpenPGP support disabled."),
810 GTK_STOCK_CLOSE, NULL, NULL, ALERTFOCUS_FIRST, TRUE, NULL,
812 if (val & G_ALERTDISABLE)
813 prefs_gpg_get_config()->gpg_warning = FALSE;
820 gpgmegtk_free_passphrase();
830 static void *_export_threaded(void *arg)
832 struct _ExportCtx *ctx = (struct _ExportCtx *)arg;
835 PROCESS_INFORMATION pi = {0};
836 STARTUPINFO si = {0};
838 result = CreateProcess(NULL, ctx->cmd, NULL, NULL, FALSE,
839 NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
840 NULL, NULL, &si, &pi);
843 debug_print("Couldn't execute '%s'\n", ctx->cmd);
845 WaitForSingleObject(pi.hProcess, 10000);
846 result = GetExitCodeProcess(pi.hProcess, &ctx->exitcode);
847 if (ctx->exitcode == STILL_ACTIVE) {
848 debug_print("Process still running, terminating it.\n");
849 TerminateProcess(pi.hProcess, 255);
852 CloseHandle(pi.hProcess);
853 CloseHandle(pi.hThread);
856 debug_print("Process executed, but we couldn't get its exit code (huh?)\n");
865 void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
867 AlertValue val = G_ALERTDEFAULT;
868 gchar *key_parms = NULL;
871 gchar *passphrase = NULL, *passphrase_second = NULL;
873 gchar *tmp = NULL, *gpgver;
874 gpgme_error_t err = 0;
876 GtkWidget *window = NULL;
877 gpgme_genkey_result_t key;
878 gboolean exported = FALSE;
881 account = account_get_default();
883 if (account->address == NULL) {
884 alertpanel_error(_("You have to save the account's information with \"OK\" "
885 "before being able to generate a key pair.\n"));
889 val = alertpanel(_("No PGP key found"),
890 _("Claws Mail did not find a secret PGP key, "
891 "which means that you won't be able to sign "
892 "emails or receive encrypted emails.\n"
893 "Do you want to create a new key pair now?"),
894 GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
895 if (val == G_ALERTDEFAULT) {
901 name = g_strdup(account->name);
903 name = g_strdup(account->address);
905 email = g_strdup(account->address);
906 tmp = g_strdup_printf("%s <%s>", account->name?account->name:account->address, account->address);
907 gpgver = get_gpg_version_string();
908 if (gpgver == NULL || !strncmp(gpgver, "1.", 2)) {
909 debug_print("Using gpg 1.x, using builtin passphrase dialog.\n");
911 passphrase = passphrase_mbox(tmp, NULL, prev_bad, 1);
912 if (passphrase == NULL) {
918 passphrase_second = passphrase_mbox(tmp, NULL, 0, 2);
919 if (passphrase_second == NULL) {
922 if (passphrase != NULL) {
923 memset(passphrase, 0, strlen(passphrase));
929 if (strcmp(passphrase, passphrase_second)) {
930 if (passphrase != NULL) {
931 memset(passphrase, 0, strlen(passphrase));
934 if (passphrase_second != NULL) {
935 memset(passphrase_second, 0, strlen(passphrase_second));
936 g_free(passphrase_second);
943 key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
947 "Subkey-Length: 2048\n"
952 "</GnupgKeyParms>\n",
954 passphrase?"Passphrase: ":"",
955 passphrase?passphrase:"",
957 #ifndef G_PLATFORM_WIN32
959 mlock(passphrase, strlen(passphrase)) == -1)
960 debug_print("couldn't lock passphrase\n");
961 if (passphrase_second &&
962 mlock(passphrase_second, strlen(passphrase_second)) == -1)
963 debug_print("couldn't lock passphrase2\n");
968 if (passphrase_second != NULL) {
969 memset(passphrase_second, 0, strlen(passphrase_second));
970 g_free(passphrase_second);
972 if (passphrase != NULL) {
973 memset(passphrase, 0, strlen(passphrase));
977 err = gpgme_new (&ctx);
979 alertpanel_error(_("Couldn't generate a new key pair: %s"),
980 gpgme_strerror(err));
981 if (key_parms != NULL) {
982 memset(key_parms, 0, strlen(key_parms));
989 window = label_window_create(_("Generating your new key pair... Please move the mouse "
990 "around to help generate entropy..."));
992 err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
993 if (key_parms != NULL) {
994 memset(key_parms, 0, strlen(key_parms));
998 label_window_destroy(window);
1001 alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
1005 key = gpgme_op_genkey_result(ctx);
1007 alertpanel_error(_("Couldn't generate a new key pair: unknown error"));
1011 gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
1012 "Its fingerprint is:\n%s\n\nDo you want to export it "
1014 key->fpr ? key->fpr:"null");
1015 AlertValue val = alertpanel(_("Key generated"), buf,
1016 GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
1018 if (val == G_ALERTALTERNATE) {
1019 gchar *gpgbin = get_gpg_executable_name();
1020 gchar *cmd = g_strdup_printf("\"%s\" --batch --no-tty --send-keys %s",
1021 (gpgbin ? gpgbin : "gpg"), key->fpr);
1022 debug_print("Executing command: %s\n", cmd);
1030 } else if (pid == 0) {
1033 res = WEXITSTATUS(res);
1037 time_t start_wait = time(NULL);
1040 if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
1043 res = WEXITSTATUS(status);
1046 if (time(NULL) - start_wait > 5) {
1047 debug_print("SIGTERM'ing gpg\n");
1050 if (time(NULL) - start_wait > 6) {
1051 debug_print("SIGKILL'ing gpg\n");
1061 /* We need to call gpg in a separate thread, so that waiting for
1062 * it to finish does not block the UI. */
1064 struct _ExportCtx *ectx = malloc(sizeof(struct _ExportCtx));
1067 ectx->exitcode = STILL_ACTIVE;
1070 if (pthread_create(&pt, NULL,
1071 _export_threaded, (void *)ectx) != 0) {
1072 debug_print("Couldn't create thread, continuing unthreaded.\n");
1073 _export_threaded(ctx);
1075 debug_print("Thread created, waiting for it to finish...\n");
1080 debug_print("Thread finished.\n");
1081 pthread_join(pt, NULL);
1083 if (ectx->exitcode == 0)
1091 alertpanel_notice(_("Key exported."));
1093 alertpanel_error(_("Couldn't export key."));
1100 gboolean sgpgme_has_secret_key(void)
1102 gpgme_error_t err = 0;
1106 err = gpgme_new (&ctx);
1108 debug_print("err : %s\n", gpgme_strerror(err));
1112 err = gpgme_op_keylist_start(ctx, NULL, TRUE);
1114 err = gpgme_op_keylist_next(ctx, &key);
1115 gpgme_key_unref(key); /* We're not interested in the key itself. */
1117 gpgme_op_keylist_end(ctx);
1118 if (gpg_err_code(err) == GPG_ERR_EOF) {
1119 if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
1120 gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
1131 void sgpgme_check_create_key(void)
1133 if (prefs_gpg_get_config()->gpg_ask_create_key &&
1134 !sgpgme_has_secret_key()) {
1135 sgpgme_create_secret_key(NULL, TRUE);
1138 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
1139 prefs_gpg_save_config();
1142 void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
1145 void *result = NULL;
1149 cm_return_val_if_fail(data != NULL, NULL);
1150 cm_return_val_if_fail(len != NULL, NULL);
1152 /* I know it's deprecated, but we don't compile with _LARGEFILE */
1153 cm_gpgme_data_rewind(data);
1154 while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
1155 void *rresult = realloc(result, r + w);
1156 if (rresult == NULL) {
1157 g_warning("can't allocate memory");
1163 memcpy(result+w, buf, r);
1169 gpgme_data_release(data);
1171 g_warning("gpgme_data_read() returned an error: %d", (int)r);
1179 gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
1181 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
1182 if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
1183 return gpg_error_from_errno(errno);
1187 return gpgme_data_rewind(dh);
1191 #endif /* USE_GPGME */