2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 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 *extract_name(const char *uid)
196 if (!strncmp(uid, "CN=", 3)) {
197 gchar *result = g_strdup(uid+3);
198 if (strstr(result, ","))
199 *(strstr(result, ",")) = '\0';
201 } else if (strstr(uid, ",CN=")) {
202 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
203 if (strstr(result, ","))
204 *(strstr(result, ",")) = '\0';
207 return g_strdup(uid);
210 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
212 gpgme_signature_t sig = NULL;
215 gchar *result = NULL;
216 gpgme_error_t err = 0;
217 static gboolean warned = FALSE;
219 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
220 return g_strdup_printf(_("The signature can't be checked - %s"), privacy_get_error());
223 if (status == NULL) {
224 return g_strdup(_("The signature has not been checked."));
226 sig = status->signatures;
228 return g_strdup(_("The signature has not been checked."));
231 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
232 if (gpg_err_code(err) == GPG_ERR_NO_AGENT) {
234 alertpanel_error(_("PGP Core: Can't get key - no gpg-agent running."));
236 g_warning("PGP Core: Can't get key - no gpg-agent running.");
238 } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) != GPG_ERR_EOF) {
239 return g_strdup_printf(_("The signature can't be checked - %s"),
240 gpgme_strerror(err));
244 uname = extract_name(key->uids->uid);
246 uname = g_strdup("<?>");
248 switch (gpg_err_code(sig->status)) {
249 case GPG_ERR_NO_ERROR:
250 switch ((key && key->uids) ? key->uids->validity : GPGME_VALIDITY_UNKNOWN) {
251 case GPGME_VALIDITY_ULTIMATE:
252 result = g_strdup_printf(_("Good signature from \"%s\" [ultimate]"), uname);
254 case GPGME_VALIDITY_FULL:
255 result = g_strdup_printf(_("Good signature from \"%s\" [full]"), uname);
257 case GPGME_VALIDITY_MARGINAL:
258 result = g_strdup_printf(_("Good signature from \"%s\" [marginal]"), uname);
260 case GPGME_VALIDITY_UNKNOWN:
261 case GPGME_VALIDITY_UNDEFINED:
262 case GPGME_VALIDITY_NEVER:
265 result = g_strdup_printf(_("Good signature from \"%s\""), uname);
267 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
268 result = g_strdup_printf(_("Key 0x%s not available to verify this signature"), id);
274 case GPG_ERR_SIG_EXPIRED:
275 result = g_strdup_printf(_("Expired signature from \"%s\""), uname);
277 case GPG_ERR_KEY_EXPIRED:
278 result = g_strdup_printf(_("Good signature from \"%s\", but the key has expired"), uname);
280 case GPG_ERR_CERT_REVOKED:
281 result = g_strdup_printf(_("Good signature from \"%s\", but the key has been revoked"), uname);
283 case GPG_ERR_BAD_SIGNATURE:
284 result = g_strdup_printf(_("Bad signature from \"%s\""), uname);
286 case GPG_ERR_NO_PUBKEY: {
287 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
288 result = g_strdup_printf(_("Key 0x%s not available to verify this signature"), id);
293 result = g_strdup(_("The signature has not been checked"));
297 result = g_strdup(_("Error"));
302 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
307 gpgme_signature_t sig = NULL;
309 siginfo = g_string_sized_new(64);
310 if (status == NULL) {
311 g_string_append_printf(siginfo,
312 _("Error checking signature: no status\n"));
316 sig = status->signatures;
323 const gchar *keytype, *keyid, *uid;
325 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
327 if (err != GPG_ERR_NO_ERROR) {
329 g_string_append_printf(siginfo,
330 _("Error checking signature: %s\n"),
331 gpgme_strerror(err));
335 keytype = gpgme_pubkey_algo_name(
336 key->subkeys->pubkey_algo);
337 keyid = key->subkeys->keyid;
338 uid = key->uids->uid;
345 memset(buf, 0, sizeof(buf));
346 fast_strftime(buf, sizeof(buf)-1, prefs_common_get_prefs()->date_format, localtime_r(&sig->timestamp, <));
347 g_string_append_printf(siginfo,
348 _("Signature made on %s using %s key ID %s\n"),
349 buf, keytype, keyid);
351 switch (gpg_err_code(sig->status)) {
352 case GPG_ERR_NO_ERROR:
353 g_string_append_printf(siginfo,
354 _("Good signature from uid \"%s\" (Validity: %s)\n"),
355 uid, get_validity_str((key && key->uids) ? key->uids->validity:GPGME_VALIDITY_UNKNOWN));
357 case GPG_ERR_KEY_EXPIRED:
358 g_string_append_printf(siginfo,
359 _("Expired key uid \"%s\"\n"),
362 case GPG_ERR_SIG_EXPIRED:
363 g_string_append_printf(siginfo,
364 _("Expired signature from uid \"%s\" (Validity: %s)\n"),
365 uid, get_validity_str((key && key->uids) ? key->uids->validity:GPGME_VALIDITY_UNKNOWN));
367 case GPG_ERR_CERT_REVOKED:
368 g_string_append_printf(siginfo,
369 _("Revoked key uid \"%s\"\n"),
372 case GPG_ERR_BAD_SIGNATURE:
373 g_string_append_printf(siginfo,
374 _("BAD signature from \"%s\"\n"),
380 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
383 key->uids = key->uids ? key->uids->next : NULL;
384 while (key->uids != NULL) {
385 g_string_append_printf(siginfo,
387 _("uid \"%s\" (Validity: %s)\n"), NULL),
389 key->uids->revoked==TRUE?_("Revoked"):get_validity_str(key->uids->validity));
391 key->uids = key->uids->next;
394 g_string_append_printf(siginfo,_("Owner Trust: %s\n"),
395 key ? get_owner_trust_str(key->owner_trust) : _("No key!"));
396 g_string_append(siginfo,
397 _("Primary key fingerprint:"));
398 const char* primary_fpr = NULL;
399 if (key && key->subkeys && key->subkeys->fpr)
400 primary_fpr = key->subkeys->fpr;
402 g_string_append(siginfo, " ?");
403 int idx; /* now pretty-print the fingerprint */
404 for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
406 g_string_append_c(siginfo, ' ');
408 g_string_append_c(siginfo, ' ');
409 g_string_append_c(siginfo, (gchar)*primary_fpr);
411 g_string_append_c(siginfo, '\n');
412 #ifdef HAVE_GPGME_PKA_TRUST
413 if (sig->pka_trust == 1 && sig->pka_address) {
414 g_string_append_printf(siginfo,
415 _("WARNING: Signer's address \"%s\" "
416 "does not match DNS entry\n"),
419 else if (sig->pka_trust == 2 && sig->pka_address) {
420 g_string_append_printf(siginfo,
421 _("Verified signer's address is \"%s\"\n"),
423 /* FIXME: Compare the address to the
426 #endif /*HAVE_GPGME_PKA_TRUST*/
429 g_string_append(siginfo, "\n");
435 g_string_free(siginfo, FALSE);
439 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
441 gpgme_data_t data = NULL;
443 FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
448 err = gpgme_data_new_from_filepart(&data, NULL, fp, mimeinfo->offset, mimeinfo->length);
451 debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
453 debug_print ("gpgme_data_new_from_file failed: %s\n",
454 gpgme_strerror (err));
455 privacy_set_error(_("Couldn't get data from message, %s"), gpgme_strerror(err));
461 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
463 struct passphrase_cb_info_s info;
467 memset (&info, 0, sizeof info);
469 if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
471 privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
475 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
476 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
477 if (!g_getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
479 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
482 prefs_gpg_enable_agent(TRUE);
484 gpgme_set_passphrase_cb (ctx, NULL, &info);
488 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
489 err = gpgme_op_decrypt_verify(ctx, cipher, plain);
490 if (err != GPG_ERR_NO_ERROR) {
491 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
492 privacy_set_error("%s", gpgme_strerror(err));
493 gpgmegtk_free_passphrase();
494 gpgme_data_release(plain);
498 err = cm_gpgme_data_rewind(plain);
500 debug_print("can't seek (%d %d %s)\n", err, errno, g_strerror(errno));
503 debug_print("decrypted.\n");
504 *status = gpgme_op_verify_result (ctx);
506 err = gpgme_op_decrypt(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);
526 gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
528 SelectionResult result = KEY_SELECTION_CANCEL;
529 gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result,
535 if (result == KEY_SELECTION_DONT)
536 return g_strdup("_DONT_ENCRYPT_");
541 gpgme_subkey_t skey = keys[i]->subkeys;
542 gchar *fpr = skey->fpr;
544 debug_print("adding %s\n", fpr);
545 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
553 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
554 const gchar *from_addr)
556 GPGAccountConfig *config;
557 const gchar *signer_addr = account->address;
559 gpgme_signers_clear(ctx);
562 signer_addr = from_addr;
563 config = prefs_gpg_account_get_config(account);
565 switch(config->sign_key) {
566 case SIGN_KEY_DEFAULT:
567 debug_print("using default gnupg key\n");
569 case SIGN_KEY_BY_FROM:
570 debug_print("using key for %s\n", signer_addr);
572 case SIGN_KEY_CUSTOM:
573 debug_print("using key for %s\n", config->sign_key_id);
577 if (config->sign_key != SIGN_KEY_DEFAULT) {
579 gpgme_key_t key, found_key;
582 if (config->sign_key == SIGN_KEY_BY_FROM)
584 else if (config->sign_key == SIGN_KEY_CUSTOM)
585 keyid = config->sign_key_id;
590 /* Look for any key, not just private ones, or GPGMe doesn't
591 * correctly set the revoked flag. */
592 err = gpgme_op_keylist_start(ctx, keyid, 0);
593 while ((err = gpgme_op_keylist_next(ctx, &key)) == 0) {
600 if (key->protocol != gpgme_get_protocol(ctx)) {
601 debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
602 gpgme_key_release(key);
607 debug_print("skipping a key, expired");
608 gpgme_key_release(key);
612 debug_print("skipping a key, revoked");
613 gpgme_key_release(key);
617 debug_print("skipping a key, disabled");
618 gpgme_key_release(key);
622 if (found_key != NULL) {
623 gpgme_key_release(key);
624 gpgme_op_keylist_end(ctx);
625 g_warning("ambiguous specification of secret key '%s'", keyid);
626 privacy_set_error(_("Secret key specification is ambiguous"));
632 gpgme_op_keylist_end(ctx);
634 if (found_key == NULL) {
635 g_warning("setup_signers start: %s", gpgme_strerror(err));
636 privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
640 err = gpgme_signers_add(ctx, found_key);
641 debug_print("got key (proto %d (pgp %d, smime %d).\n",
642 found_key->protocol, GPGME_PROTOCOL_OpenPGP,
644 gpgme_key_release(found_key);
647 g_warning("error adding secret key: %s",
648 gpgme_strerror(err));
649 privacy_set_error(_("Error setting secret key: %s"),
650 gpgme_strerror(err));
655 prefs_gpg_account_free_config(config);
659 prefs_gpg_account_free_config(config);
665 gchar *ctype_locale = NULL, *messages_locale = NULL;
666 gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL;
667 gpgme_error_t err = 0;
669 gpgme_engine_info_t engineInfo;
671 if (strcmp(prefs_gpg_get_config()->gpg_path, "") != 0
672 && access(prefs_gpg_get_config()->gpg_path, X_OK) != -1) {
673 err = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, prefs_gpg_get_config()->gpg_path, NULL);
674 if (err != GPG_ERR_NO_ERROR)
675 g_warning("failed to set crypto engine configuration: %s", gpgme_strerror(err));
678 if (gpgme_check_version("1.0.0")) {
680 debug_print("setting gpgme CTYPE locale\n");
682 ctype_locale = g_win32_getlocale();
684 ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
687 debug_print("setting gpgme CTYPE locale to: %s\n", ctype_locale);
688 if (strchr(ctype_locale, '.'))
689 *(strchr(ctype_locale, '.')) = '\0';
690 else if (strchr(ctype_locale, '@'))
691 *(strchr(ctype_locale, '@')) = '\0';
692 ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL);
694 debug_print("setting gpgme locale to UTF8: %s\n", ctype_utf8_locale ? ctype_utf8_locale : "NULL");
695 gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale);
697 debug_print("done\n");
698 g_free(ctype_utf8_locale);
699 g_free(ctype_locale);
701 debug_print("couldn't set gpgme CTYPE locale\n");
705 debug_print("setting gpgme MESSAGES locale\n");
707 messages_locale = g_win32_getlocale();
709 messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
711 if (messages_locale) {
712 debug_print("setting gpgme MESSAGES locale to: %s\n", messages_locale);
713 if (strchr(messages_locale, '.'))
714 *(strchr(messages_locale, '.')) = '\0';
715 else if (strchr(messages_locale, '@'))
716 *(strchr(messages_locale, '@')) = '\0';
717 messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL);
718 debug_print("setting gpgme locale to UTF8: %s\n", messages_utf8_locale ? messages_utf8_locale : "NULL");
720 gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale);
722 debug_print("done\n");
723 g_free(messages_utf8_locale);
724 g_free(messages_locale);
726 debug_print("couldn't set gpgme MESSAGES locale\n");
729 if (!gpgme_get_engine_info(&engineInfo)) {
731 debug_print("GpgME Protocol: %s\n"
732 "Version: %s (req %s)\n"
734 gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
735 engineInfo->version ? engineInfo->version:"???",
736 engineInfo->req_version ? engineInfo->req_version:"???",
737 engineInfo->file_name ? engineInfo->file_name:"???");
738 if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
739 && gpgme_engine_check_version(engineInfo->protocol) !=
741 if (engineInfo->file_name && !engineInfo->version) {
742 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
743 "Engine '%s' isn't installed properly."),
744 gpgme_get_protocol_name(engineInfo->protocol),
745 engineInfo->file_name);
746 } else if (engineInfo->file_name && engineInfo->version
747 && engineInfo->req_version) {
748 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
749 "Engine '%s' version %s is installed, "
750 "but version %s is required.\n"),
751 gpgme_get_protocol_name(engineInfo->protocol),
752 engineInfo->file_name,
754 engineInfo->req_version);
756 alertpanel_error(_("Gpgme protocol '%s' is unusable "
757 "(unknown problem)"),
758 gpgme_get_protocol_name(engineInfo->protocol));
761 engineInfo = engineInfo->next;
765 sgpgme_disable_all();
767 if (prefs_gpg_get_config()->gpg_warning) {
770 val = alertpanel_full
772 _("GnuPG is not installed properly, or needs "
774 "OpenPGP support disabled."),
775 GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
776 ALERT_WARNING, G_ALERTDEFAULT);
777 if (val & G_ALERTDISABLE)
778 prefs_gpg_get_config()->gpg_warning = FALSE;
785 gpgmegtk_free_passphrase();
795 static void *_export_threaded(void *arg)
797 struct _ExportCtx *ctx = (struct _ExportCtx *)arg;
800 PROCESS_INFORMATION pi = {0};
801 STARTUPINFO si = {0};
803 result = CreateProcess(NULL, ctx->cmd, NULL, NULL, FALSE,
804 NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
805 NULL, NULL, &si, &pi);
808 debug_print("Couldn't execute '%s'\n", ctx->cmd);
810 WaitForSingleObject(pi.hProcess, 10000);
811 result = GetExitCodeProcess(pi.hProcess, &ctx->exitcode);
812 if (ctx->exitcode == STILL_ACTIVE) {
813 debug_print("Process still running, terminating it.\n");
814 TerminateProcess(pi.hProcess, 255);
817 CloseHandle(pi.hProcess);
818 CloseHandle(pi.hThread);
821 debug_print("Process executed, but we couldn't get its exit code (huh?)\n");
830 void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
832 AlertValue val = G_ALERTDEFAULT;
833 gchar *key_parms = NULL;
836 gchar *passphrase = NULL, *passphrase_second = NULL;
839 gpgme_error_t err = 0;
841 GtkWidget *window = NULL;
842 gpgme_genkey_result_t key;
843 gboolean exported = FALSE;
846 account = account_get_default();
848 if (account->address == NULL) {
849 alertpanel_error(_("You have to save the account's information with \"OK\" "
850 "before being able to generate a key pair.\n"));
854 val = alertpanel(_("No PGP key found"),
855 _("Claws Mail did not find a secret PGP key, "
856 "which means that you won't be able to sign "
857 "emails or receive encrypted emails.\n"
858 "Do you want to create a new key pair now?"),
859 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
860 if (val == G_ALERTDEFAULT) {
866 name = g_strdup(account->name);
868 name = g_strdup(account->address);
870 email = g_strdup(account->address);
871 tmp = g_strdup_printf("%s <%s>", account->name?account->name:account->address, account->address);
873 passphrase = passphrase_mbox(tmp, NULL, prev_bad, 1);
874 if (passphrase == NULL) {
880 passphrase_second = passphrase_mbox(tmp, NULL, 0, 2);
881 if (passphrase_second == NULL) {
888 if (strcmp(passphrase, passphrase_second)) {
890 g_free(passphrase_second);
895 key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
899 "Subkey-Length: 2048\n"
904 "</GnupgKeyParms>\n",
906 strlen(passphrase)?"Passphrase: ":"",
908 strlen(passphrase)?"\n":"");
909 #ifndef G_PLATFORM_WIN32
910 if (mlock(passphrase, strlen(passphrase)) == -1)
911 debug_print("couldn't lock passphrase\n");
912 if (mlock(passphrase_second, strlen(passphrase_second)) == -1)
913 debug_print("couldn't lock passphrase2\n");
918 g_free(passphrase_second);
921 err = gpgme_new (&ctx);
923 alertpanel_error(_("Couldn't generate a new key pair: %s"),
924 gpgme_strerror(err));
930 window = label_window_create(_("Generating your new key pair... Please move the mouse "
931 "around to help generate entropy..."));
933 err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
936 label_window_destroy(window);
939 alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
943 key = gpgme_op_genkey_result(ctx);
945 alertpanel_error(_("Couldn't generate a new key pair: unknown error"));
949 gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
950 "Its fingerprint is:\n%s\n\nDo you want to export it "
952 key->fpr ? key->fpr:"null");
953 AlertValue val = alertpanel(_("Key generated"), buf,
954 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
956 if (val == G_ALERTALTERNATE) {
957 gchar *gpgbin = get_gpg_executable_name();
958 gchar *cmd = g_strdup_printf("\"%s\" --batch --no-tty --send-keys %s",
959 (gpgbin ? gpgbin : "gpg"), key->fpr);
960 debug_print("Executing command: %s\n", cmd);
968 } else if (pid == 0) {
971 res = WEXITSTATUS(res);
975 time_t start_wait = time(NULL);
978 if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
981 res = WEXITSTATUS(status);
984 if (time(NULL) - start_wait > 5) {
985 debug_print("SIGTERM'ing gpg\n");
988 if (time(NULL) - start_wait > 6) {
989 debug_print("SIGKILL'ing gpg\n");
999 /* We need to call gpg in a separate thread, so that waiting for
1000 * it to finish does not block the UI. */
1002 struct _ExportCtx *ectx = malloc(sizeof(struct _ExportCtx));
1005 ectx->exitcode = STILL_ACTIVE;
1008 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
1009 _export_threaded, (void *)ectx) != 0) {
1010 debug_print("Couldn't create thread, continuing unthreaded.\n");
1011 _export_threaded(ctx);
1013 debug_print("Thread created, waiting for it to finish...\n");
1018 debug_print("Thread finished.\n");
1019 pthread_join(pt, NULL);
1021 if (ectx->exitcode == 0)
1029 alertpanel_notice(_("Key exported."));
1031 alertpanel_error(_("Couldn't export key."));
1038 gboolean sgpgme_has_secret_key(void)
1040 gpgme_error_t err = 0;
1044 err = gpgme_new (&ctx);
1046 debug_print("err : %s\n", gpgme_strerror(err));
1050 err = gpgme_op_keylist_start(ctx, NULL, TRUE);
1052 err = gpgme_op_keylist_next(ctx, &key);
1053 gpgme_op_keylist_end(ctx);
1054 if (gpg_err_code(err) == GPG_ERR_EOF) {
1055 if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
1056 gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
1067 void sgpgme_check_create_key(void)
1069 if (prefs_gpg_get_config()->gpg_ask_create_key &&
1070 !sgpgme_has_secret_key()) {
1071 sgpgme_create_secret_key(NULL, TRUE);
1074 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
1075 prefs_gpg_save_config();
1078 void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
1081 void *result = NULL;
1090 /* I know it's deprecated, but we don't compile with _LARGEFILE */
1091 cm_gpgme_data_rewind(data);
1092 while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
1093 void *rresult = realloc(result, r + w);
1094 if (rresult == NULL) {
1095 g_warning("can't allocate memory");
1101 memcpy(result+w, buf, r);
1107 gpgme_data_release(data);
1116 gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
1118 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
1119 if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
1120 return gpg_error_from_errno(errno);
1124 return gpgme_data_rewind(dh);
1128 #endif /* USE_GPGME */