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 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
285 result = g_strdup_printf(_("Key 0x%s not available to verify this signature"), id);
291 case GPG_ERR_SIG_EXPIRED:
292 result = g_strdup_printf(_("Expired signature from \"%s\""), uname);
294 case GPG_ERR_KEY_EXPIRED:
295 result = g_strdup_printf(_("Good signature from \"%s\", but the key has expired"), uname);
297 case GPG_ERR_CERT_REVOKED:
298 result = g_strdup_printf(_("Good signature from \"%s\", but the key has been revoked"), uname);
300 case GPG_ERR_BAD_SIGNATURE:
301 result = g_strdup_printf(_("Bad signature from \"%s\""), uname);
303 case GPG_ERR_NO_PUBKEY: {
304 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
305 result = g_strdup_printf(_("Key 0x%s not available to verify this signature"), id);
310 result = g_strdup(_("The signature has not been checked"));
314 result = g_strdup(_("Error"));
319 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
324 gpgme_signature_t sig = NULL;
326 siginfo = g_string_sized_new(64);
327 if (status == NULL) {
328 g_string_append_printf(siginfo,
329 _("Error checking signature: no status\n"));
333 sig = status->signatures;
340 const gchar *keytype, *keyid, *uid;
342 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
344 if (err != GPG_ERR_NO_ERROR) {
346 g_string_append_printf(siginfo,
347 _("Error checking signature: %s\n"),
348 gpgme_strerror(err));
352 keytype = gpgme_pubkey_algo_name(
353 key->subkeys->pubkey_algo);
354 keyid = key->subkeys->keyid;
355 uid = key->uids->uid;
362 memset(buf, 0, sizeof(buf));
363 fast_strftime(buf, sizeof(buf)-1, prefs_common_get_prefs()->date_format, localtime_r(&sig->timestamp, <));
364 g_string_append_printf(siginfo,
365 _("Signature made on %s using %s key ID %s\n"),
366 buf, keytype, keyid);
368 switch (gpg_err_code(sig->status)) {
369 case GPG_ERR_NO_ERROR:
370 g_string_append_printf(siginfo,
371 _("Good signature from uid \"%s\" (Validity: %s)\n"),
372 uid, get_validity_str((key && key->uids) ? key->uids->validity:GPGME_VALIDITY_UNKNOWN));
374 case GPG_ERR_KEY_EXPIRED:
375 g_string_append_printf(siginfo,
376 _("Expired key uid \"%s\"\n"),
379 case GPG_ERR_SIG_EXPIRED:
380 g_string_append_printf(siginfo,
381 _("Expired signature from uid \"%s\" (Validity: %s)\n"),
382 uid, get_validity_str((key && key->uids) ? key->uids->validity:GPGME_VALIDITY_UNKNOWN));
384 case GPG_ERR_CERT_REVOKED:
385 g_string_append_printf(siginfo,
386 _("Revoked key uid \"%s\"\n"),
389 case GPG_ERR_BAD_SIGNATURE:
390 g_string_append_printf(siginfo,
391 _("BAD signature from \"%s\"\n"),
397 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
400 key->uids = key->uids ? key->uids->next : NULL;
401 while (key->uids != NULL) {
402 g_string_append_printf(siginfo,
404 _("uid \"%s\" (Validity: %s)\n"), NULL),
406 key->uids->revoked==TRUE?_("Revoked"):get_validity_str(key->uids->validity));
408 key->uids = key->uids->next;
411 g_string_append_printf(siginfo,_("Owner Trust: %s\n"),
412 key ? get_owner_trust_str(key->owner_trust) : _("No key!"));
413 g_string_append(siginfo,
414 _("Primary key fingerprint:"));
415 const char* primary_fpr = NULL;
416 if (key && key->subkeys && key->subkeys->fpr)
417 primary_fpr = key->subkeys->fpr;
419 g_string_append(siginfo, " ?");
420 int idx; /* now pretty-print the fingerprint */
421 for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
423 g_string_append_c(siginfo, ' ');
425 g_string_append_c(siginfo, ' ');
426 g_string_append_c(siginfo, (gchar)*primary_fpr);
428 g_string_append_c(siginfo, '\n');
429 #ifdef HAVE_GPGME_PKA_TRUST
430 if (sig->pka_trust == 1 && sig->pka_address) {
431 g_string_append_printf(siginfo,
432 _("WARNING: Signer's address \"%s\" "
433 "does not match DNS entry\n"),
436 else if (sig->pka_trust == 2 && sig->pka_address) {
437 g_string_append_printf(siginfo,
438 _("Verified signer's address is \"%s\"\n"),
440 /* FIXME: Compare the address to the
443 #endif /*HAVE_GPGME_PKA_TRUST*/
446 g_string_append(siginfo, "\n");
452 g_string_free(siginfo, FALSE);
456 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
458 gpgme_data_t data = NULL;
460 FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
465 err = gpgme_data_new_from_filepart(&data, NULL, fp, mimeinfo->offset, mimeinfo->length);
468 debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
470 debug_print ("gpgme_data_new_from_file failed: %s\n",
471 gpgme_strerror (err));
472 privacy_set_error(_("Couldn't get data from message, %s"), gpgme_strerror(err));
478 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
480 struct passphrase_cb_info_s info;
484 memset (&info, 0, sizeof info);
486 if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
488 privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
492 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
493 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
494 if (!g_getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
496 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
499 prefs_gpg_enable_agent(TRUE);
501 gpgme_set_passphrase_cb (ctx, NULL, &info);
505 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
506 err = gpgme_op_decrypt_verify(ctx, cipher, plain);
507 if (err != GPG_ERR_NO_ERROR) {
508 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
509 privacy_set_error("%s", gpgme_strerror(err));
510 gpgmegtk_free_passphrase();
511 gpgme_data_release(plain);
515 err = cm_gpgme_data_rewind(plain);
517 debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
520 debug_print("decrypted.\n");
521 *status = gpgme_op_verify_result (ctx);
523 err = gpgme_op_decrypt(ctx, cipher, plain);
524 if (err != GPG_ERR_NO_ERROR) {
525 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
526 privacy_set_error("%s", gpgme_strerror(err));
527 gpgmegtk_free_passphrase();
528 gpgme_data_release(plain);
532 err = cm_gpgme_data_rewind(plain);
534 debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
537 debug_print("decrypted.\n");
538 *status = gpgme_op_verify_result (ctx);
543 gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
545 SelectionResult result = KEY_SELECTION_CANCEL;
546 gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result,
552 if (result == KEY_SELECTION_DONT)
553 return g_strdup("_DONT_ENCRYPT_");
558 gpgme_subkey_t skey = keys[i]->subkeys;
559 gchar *fpr = skey->fpr;
561 debug_print("adding %s\n", fpr);
562 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
570 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
571 const gchar *from_addr)
573 GPGAccountConfig *config;
574 const gchar *signer_addr = account->address;
576 gpgme_signers_clear(ctx);
579 signer_addr = from_addr;
580 config = prefs_gpg_account_get_config(account);
582 switch(config->sign_key) {
583 case SIGN_KEY_DEFAULT:
584 debug_print("using default gnupg key\n");
586 case SIGN_KEY_BY_FROM:
587 debug_print("using key for %s\n", signer_addr);
589 case SIGN_KEY_CUSTOM:
590 debug_print("using key for %s\n", config->sign_key_id);
594 if (config->sign_key != SIGN_KEY_DEFAULT) {
596 gpgme_key_t key, found_key;
599 if (config->sign_key == SIGN_KEY_BY_FROM)
601 else if (config->sign_key == SIGN_KEY_CUSTOM)
602 keyid = config->sign_key_id;
607 /* Look for any key, not just private ones, or GPGMe doesn't
608 * correctly set the revoked flag. */
609 err = gpgme_op_keylist_start(ctx, keyid, 0);
611 if ((err = gpgme_op_keylist_next(ctx, &key)) != 0)
617 if (!key->can_sign) {
618 debug_print("skipping a key, can not be used for signing\n");
619 gpgme_key_unref(key);
623 if (key->protocol != gpgme_get_protocol(ctx)) {
624 debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
625 gpgme_key_unref(key);
630 debug_print("skipping a key, expired\n");
631 gpgme_key_unref(key);
635 debug_print("skipping a key, revoked\n");
636 gpgme_key_unref(key);
640 debug_print("skipping a key, disabled\n");
641 gpgme_key_unref(key);
645 if (found_key != NULL) {
646 gpgme_key_unref(key);
647 gpgme_op_keylist_end(ctx);
648 g_warning("ambiguous specification of secret key '%s'", keyid);
649 privacy_set_error(_("Secret key specification is ambiguous"));
655 gpgme_op_keylist_end(ctx);
657 if (found_key == NULL) {
658 g_warning("setup_signers start: %s", gpgme_strerror(err));
659 privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
663 err = gpgme_signers_add(ctx, found_key);
664 debug_print("got key (proto %d (pgp %d, smime %d).\n",
665 found_key->protocol, GPGME_PROTOCOL_OpenPGP,
667 gpgme_key_unref(found_key);
670 g_warning("error adding secret key: %s",
671 gpgme_strerror(err));
672 privacy_set_error(_("Error setting secret key: %s"),
673 gpgme_strerror(err));
678 prefs_gpg_account_free_config(config);
682 prefs_gpg_account_free_config(config);
688 gchar *ctype_locale = NULL, *messages_locale = NULL;
689 gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL;
690 gpgme_error_t err = 0;
692 gpgme_engine_info_t engineInfo;
694 if (strcmp(prefs_gpg_get_config()->gpg_path, "") != 0
695 && access(prefs_gpg_get_config()->gpg_path, X_OK) != -1) {
696 err = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, prefs_gpg_get_config()->gpg_path, NULL);
697 if (err != GPG_ERR_NO_ERROR)
698 g_warning("failed to set crypto engine configuration: %s", gpgme_strerror(err));
701 if (gpgme_check_version("1.0.0")) {
703 debug_print("setting gpgme CTYPE locale\n");
705 ctype_locale = g_win32_getlocale();
707 ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
710 debug_print("setting gpgme CTYPE locale to: %s\n", ctype_locale);
711 if (strchr(ctype_locale, '.'))
712 *(strchr(ctype_locale, '.')) = '\0';
713 else if (strchr(ctype_locale, '@'))
714 *(strchr(ctype_locale, '@')) = '\0';
715 ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL);
717 debug_print("setting gpgme locale to UTF8: %s\n", ctype_utf8_locale ? ctype_utf8_locale : "NULL");
718 gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale);
720 debug_print("done\n");
721 g_free(ctype_utf8_locale);
722 g_free(ctype_locale);
724 debug_print("couldn't set gpgme CTYPE locale\n");
728 debug_print("setting gpgme MESSAGES locale\n");
730 messages_locale = g_win32_getlocale();
732 messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
734 if (messages_locale) {
735 debug_print("setting gpgme MESSAGES locale to: %s\n", messages_locale);
736 if (strchr(messages_locale, '.'))
737 *(strchr(messages_locale, '.')) = '\0';
738 else if (strchr(messages_locale, '@'))
739 *(strchr(messages_locale, '@')) = '\0';
740 messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL);
741 debug_print("setting gpgme locale to UTF8: %s\n", messages_utf8_locale ? messages_utf8_locale : "NULL");
743 gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale);
745 debug_print("done\n");
746 g_free(messages_utf8_locale);
747 g_free(messages_locale);
749 debug_print("couldn't set gpgme MESSAGES locale\n");
752 if (!gpgme_get_engine_info(&engineInfo)) {
754 debug_print("GpgME Protocol: %s\n"
755 "Version: %s (req %s)\n"
757 gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
758 engineInfo->version ? engineInfo->version:"???",
759 engineInfo->req_version ? engineInfo->req_version:"???",
760 engineInfo->file_name ? engineInfo->file_name:"???");
761 if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
762 && gpgme_engine_check_version(engineInfo->protocol) !=
764 if (engineInfo->file_name && !engineInfo->version) {
765 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
766 "Engine '%s' isn't installed properly."),
767 gpgme_get_protocol_name(engineInfo->protocol),
768 engineInfo->file_name);
769 } else if (engineInfo->file_name && engineInfo->version
770 && engineInfo->req_version) {
771 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
772 "Engine '%s' version %s is installed, "
773 "but version %s is required.\n"),
774 gpgme_get_protocol_name(engineInfo->protocol),
775 engineInfo->file_name,
777 engineInfo->req_version);
779 alertpanel_error(_("Gpgme protocol '%s' is unusable "
780 "(unknown problem)"),
781 gpgme_get_protocol_name(engineInfo->protocol));
784 engineInfo = engineInfo->next;
788 sgpgme_disable_all();
790 if (prefs_gpg_get_config()->gpg_warning) {
793 val = alertpanel_full
795 _("GnuPG is not installed properly, or needs "
797 "OpenPGP support disabled."),
798 GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
799 ALERT_WARNING, G_ALERTDEFAULT);
800 if (val & G_ALERTDISABLE)
801 prefs_gpg_get_config()->gpg_warning = FALSE;
808 gpgmegtk_free_passphrase();
818 static void *_export_threaded(void *arg)
820 struct _ExportCtx *ctx = (struct _ExportCtx *)arg;
823 PROCESS_INFORMATION pi = {0};
824 STARTUPINFO si = {0};
826 result = CreateProcess(NULL, ctx->cmd, NULL, NULL, FALSE,
827 NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
828 NULL, NULL, &si, &pi);
831 debug_print("Couldn't execute '%s'\n", ctx->cmd);
833 WaitForSingleObject(pi.hProcess, 10000);
834 result = GetExitCodeProcess(pi.hProcess, &ctx->exitcode);
835 if (ctx->exitcode == STILL_ACTIVE) {
836 debug_print("Process still running, terminating it.\n");
837 TerminateProcess(pi.hProcess, 255);
840 CloseHandle(pi.hProcess);
841 CloseHandle(pi.hThread);
844 debug_print("Process executed, but we couldn't get its exit code (huh?)\n");
853 void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
855 AlertValue val = G_ALERTDEFAULT;
856 gchar *key_parms = NULL;
859 gchar *passphrase = NULL, *passphrase_second = NULL;
861 gchar *tmp = NULL, *gpgver;
862 gpgme_error_t err = 0;
864 GtkWidget *window = NULL;
865 gpgme_genkey_result_t key;
866 gboolean exported = FALSE;
869 account = account_get_default();
871 if (account->address == NULL) {
872 alertpanel_error(_("You have to save the account's information with \"OK\" "
873 "before being able to generate a key pair.\n"));
877 val = alertpanel(_("No PGP key found"),
878 _("Claws Mail did not find a secret PGP key, "
879 "which means that you won't be able to sign "
880 "emails or receive encrypted emails.\n"
881 "Do you want to create a new key pair now?"),
882 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
883 if (val == G_ALERTDEFAULT) {
889 name = g_strdup(account->name);
891 name = g_strdup(account->address);
893 email = g_strdup(account->address);
894 tmp = g_strdup_printf("%s <%s>", account->name?account->name:account->address, account->address);
895 gpgver = get_gpg_version_string();
896 if (gpgver == NULL || !strncmp(gpgver, "1.", 2)) {
897 debug_print("Using gpg 1.x, using builtin passphrase dialog.\n");
899 passphrase = passphrase_mbox(tmp, NULL, prev_bad, 1);
900 if (passphrase == NULL) {
906 passphrase_second = passphrase_mbox(tmp, NULL, 0, 2);
907 if (passphrase_second == NULL) {
910 if (passphrase != NULL) {
911 memset(passphrase, 0, strlen(passphrase));
917 if (strcmp(passphrase, passphrase_second)) {
918 if (passphrase != NULL) {
919 memset(passphrase, 0, strlen(passphrase));
922 if (passphrase_second != NULL) {
923 memset(passphrase_second, 0, strlen(passphrase_second));
924 g_free(passphrase_second);
931 key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
935 "Subkey-Length: 2048\n"
940 "</GnupgKeyParms>\n",
942 passphrase?"Passphrase: ":"",
943 passphrase?passphrase:"",
945 #ifndef G_PLATFORM_WIN32
947 mlock(passphrase, strlen(passphrase)) == -1)
948 debug_print("couldn't lock passphrase\n");
949 if (passphrase_second &&
950 mlock(passphrase_second, strlen(passphrase_second)) == -1)
951 debug_print("couldn't lock passphrase2\n");
956 if (passphrase_second != NULL) {
957 memset(passphrase_second, 0, strlen(passphrase_second));
958 g_free(passphrase_second);
960 if (passphrase != NULL) {
961 memset(passphrase, 0, strlen(passphrase));
965 err = gpgme_new (&ctx);
967 alertpanel_error(_("Couldn't generate a new key pair: %s"),
968 gpgme_strerror(err));
969 if (key_parms != NULL) {
970 memset(key_parms, 0, strlen(key_parms));
977 window = label_window_create(_("Generating your new key pair... Please move the mouse "
978 "around to help generate entropy..."));
980 err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
981 if (key_parms != NULL) {
982 memset(key_parms, 0, strlen(key_parms));
986 label_window_destroy(window);
989 alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
993 key = gpgme_op_genkey_result(ctx);
995 alertpanel_error(_("Couldn't generate a new key pair: unknown error"));
999 gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
1000 "Its fingerprint is:\n%s\n\nDo you want to export it "
1002 key->fpr ? key->fpr:"null");
1003 AlertValue val = alertpanel(_("Key generated"), buf,
1004 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
1006 if (val == G_ALERTALTERNATE) {
1007 gchar *gpgbin = get_gpg_executable_name();
1008 gchar *cmd = g_strdup_printf("\"%s\" --batch --no-tty --send-keys %s",
1009 (gpgbin ? gpgbin : "gpg"), key->fpr);
1010 debug_print("Executing command: %s\n", cmd);
1018 } else if (pid == 0) {
1021 res = WEXITSTATUS(res);
1025 time_t start_wait = time(NULL);
1028 if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
1031 res = WEXITSTATUS(status);
1034 if (time(NULL) - start_wait > 5) {
1035 debug_print("SIGTERM'ing gpg\n");
1038 if (time(NULL) - start_wait > 6) {
1039 debug_print("SIGKILL'ing gpg\n");
1049 /* We need to call gpg in a separate thread, so that waiting for
1050 * it to finish does not block the UI. */
1052 struct _ExportCtx *ectx = malloc(sizeof(struct _ExportCtx));
1055 ectx->exitcode = STILL_ACTIVE;
1058 if (pthread_create(&pt, NULL,
1059 _export_threaded, (void *)ectx) != 0) {
1060 debug_print("Couldn't create thread, continuing unthreaded.\n");
1061 _export_threaded(ctx);
1063 debug_print("Thread created, waiting for it to finish...\n");
1068 debug_print("Thread finished.\n");
1069 pthread_join(pt, NULL);
1071 if (ectx->exitcode == 0)
1079 alertpanel_notice(_("Key exported."));
1081 alertpanel_error(_("Couldn't export key."));
1088 gboolean sgpgme_has_secret_key(void)
1090 gpgme_error_t err = 0;
1094 err = gpgme_new (&ctx);
1096 debug_print("err : %s\n", gpgme_strerror(err));
1100 err = gpgme_op_keylist_start(ctx, NULL, TRUE);
1102 err = gpgme_op_keylist_next(ctx, &key);
1103 gpgme_key_unref(key); /* We're not interested in the key itself. */
1104 gpgme_op_keylist_end(ctx);
1105 if (gpg_err_code(err) == GPG_ERR_EOF) {
1106 if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
1107 gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
1118 void sgpgme_check_create_key(void)
1120 if (prefs_gpg_get_config()->gpg_ask_create_key &&
1121 !sgpgme_has_secret_key()) {
1122 sgpgme_create_secret_key(NULL, TRUE);
1125 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
1126 prefs_gpg_save_config();
1129 void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
1132 void *result = NULL;
1136 cm_return_val_if_fail(data != NULL, NULL);
1137 cm_return_val_if_fail(len != NULL, NULL);
1139 /* I know it's deprecated, but we don't compile with _LARGEFILE */
1140 cm_gpgme_data_rewind(data);
1141 while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
1142 void *rresult = realloc(result, r + w);
1143 if (rresult == NULL) {
1144 g_warning("can't allocate memory");
1150 memcpy(result+w, buf, r);
1156 gpgme_data_release(data);
1158 g_warning("gpgme_data_read() returned an error: %d", (int)r);
1166 gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
1168 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
1169 if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
1170 return gpg_error_from_errno(errno);
1174 return gpgme_data_rewind(dh);
1178 #endif /* USE_GPGME */