2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2014 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/>.
22 #include "claws-features.h"
31 #include <glib/gi18n.h>
36 #include <sys/types.h>
38 # include <sys/wait.h>
40 #if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
41 # include <sys/signal.h>
52 #include "prefs_common.h"
54 #include "alertpanel.h"
55 #include "passphrase.h"
56 #include "prefs_gpg.h"
58 #include "select-keys.h"
60 static void sgpgme_disable_all(void)
62 /* FIXME: set a flag, so that we don't bother the user with failed
66 gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig,
67 gpgme_data_t plain, gpgme_data_t dummy)
69 gpgme_verify_result_t status = NULL;
72 if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
73 debug_print("op_verify err %s\n", gpgme_strerror(err));
74 privacy_set_error("%s", gpgme_strerror(err));
75 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
77 status = gpgme_op_verify_result(ctx);
78 if (status && status->signatures == NULL) {
79 debug_print("no signature found\n");
80 privacy_set_error(_("No signature found"));
81 return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
86 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_result_t status)
88 gpgme_signature_t sig = NULL;
90 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
91 debug_print("system error\n");
92 return SIGNATURE_CHECK_FAILED;
96 debug_print("status == NULL\n");
97 return SIGNATURE_UNCHECKED;
99 sig = status->signatures;
102 debug_print("sig == NULL\n");
103 return SIGNATURE_UNCHECKED;
106 debug_print("err code %d\n", gpg_err_code(sig->status));
107 switch (gpg_err_code(sig->status)) {
108 case GPG_ERR_NO_ERROR:
109 switch (sig->validity) {
110 case GPGME_VALIDITY_NEVER:
111 return SIGNATURE_INVALID;
112 case GPGME_VALIDITY_UNKNOWN:
113 case GPGME_VALIDITY_UNDEFINED:
114 case GPGME_VALIDITY_MARGINAL:
115 case GPGME_VALIDITY_FULL:
116 case GPGME_VALIDITY_ULTIMATE:
119 return SIGNATURE_CHECK_FAILED;
121 case GPG_ERR_SIG_EXPIRED:
122 case GPG_ERR_CERT_REVOKED:
123 return SIGNATURE_WARN;
124 case GPG_ERR_KEY_EXPIRED:
125 return SIGNATURE_KEY_EXPIRED;
126 case GPG_ERR_BAD_SIGNATURE:
127 return SIGNATURE_INVALID;
128 case GPG_ERR_NO_PUBKEY:
129 return SIGNATURE_CHECK_FAILED;
131 return SIGNATURE_CHECK_FAILED;
133 return SIGNATURE_CHECK_FAILED;
136 static const gchar *get_validity_str(unsigned long validity)
138 switch (gpg_err_code(validity)) {
139 case GPGME_VALIDITY_UNKNOWN:
141 case GPGME_VALIDITY_UNDEFINED:
142 return _("Undefined");
143 case GPGME_VALIDITY_NEVER:
145 case GPGME_VALIDITY_MARGINAL:
146 return _("Marginal");
147 case GPGME_VALIDITY_FULL:
149 case GPGME_VALIDITY_ULTIMATE:
150 return _("Ultimate");
156 static const gchar *get_owner_trust_str(unsigned long owner_trust)
158 switch (gpgme_err_code(owner_trust)) {
159 case GPGME_VALIDITY_NEVER:
160 return _("Untrusted");
161 case GPGME_VALIDITY_MARGINAL:
162 return _("Marginal");
163 case GPGME_VALIDITY_FULL:
165 case GPGME_VALIDITY_ULTIMATE:
166 return _("Ultimate");
172 static gchar *extract_name(const char *uid)
176 if (!strncmp(uid, "CN=", 3)) {
177 gchar *result = g_strdup(uid+3);
178 if (strstr(result, ","))
179 *(strstr(result, ",")) = '\0';
181 } else if (strstr(uid, ",CN=")) {
182 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
183 if (strstr(result, ","))
184 *(strstr(result, ",")) = '\0';
187 return g_strdup(uid);
190 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
192 gpgme_signature_t sig = NULL;
195 gchar *result = NULL;
196 gpgme_error_t err = 0;
197 static gboolean warned = FALSE;
199 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
200 return g_strdup_printf(_("The signature can't be checked - %s"), privacy_get_error());
203 if (status == NULL) {
204 return g_strdup(_("The signature has not been checked."));
206 sig = status->signatures;
208 return g_strdup(_("The signature has not been checked."));
211 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
212 if (gpg_err_code(err) == GPG_ERR_NO_AGENT) {
214 alertpanel_error(_("PGP Core: Can't get key - no gpg-agent running."));
216 g_warning(_("PGP Core: Can't get key - no gpg-agent running."));
218 } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) != GPG_ERR_EOF) {
219 return g_strdup_printf(_("The signature can't be checked - %s"),
220 gpgme_strerror(err));
223 uname = extract_name(key->uids->uid);
225 uname = g_strdup("<?>");
226 switch (gpg_err_code(sig->status)) {
227 case GPG_ERR_NO_ERROR:
228 result = g_strdup_printf(_("Good signature from %s."), uname);
230 case GPG_ERR_SIG_EXPIRED:
231 result = g_strdup_printf(_("Expired signature from %s."), uname);
233 case GPG_ERR_KEY_EXPIRED:
234 result = g_strdup_printf(_("Good signature from %s, but the key has expired."), uname);
236 case GPG_ERR_CERT_REVOKED:
237 result = g_strdup_printf(_("Good signature from %s, but the key has been revoked."), uname);
239 case GPG_ERR_BAD_SIGNATURE:
240 result = g_strdup_printf(_("Bad signature from %s."), uname);
242 case GPG_ERR_NO_PUBKEY: {
243 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
244 result = g_strdup_printf(_("Key 0x%s not available to verify this signature."), id);
249 result = g_strdup(_("The signature has not been checked."));
253 result = g_strdup(_("Error"));
258 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
263 gpgme_signature_t sig = NULL;
265 siginfo = g_string_sized_new(64);
266 if (status == NULL) {
267 g_string_append_printf(siginfo,
268 _("Error checking signature: no status\n"));
272 sig = status->signatures;
277 gpgme_user_id_t user = NULL;
280 const gchar *keytype, *keyid, *uid;
282 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
284 if (err != GPG_ERR_NO_ERROR) {
286 g_string_append_printf(siginfo,
287 _("Error checking signature: %s\n"),
288 gpgme_strerror(err));
293 keytype = gpgme_pubkey_algo_name(
294 key->subkeys->pubkey_algo);
295 keyid = key->subkeys->keyid;
303 memset(buf, 0, sizeof(buf));
304 fast_strftime(buf, sizeof(buf)-1, prefs_common_get_prefs()->date_format, localtime_r(&sig->timestamp, <));
305 g_string_append_printf(siginfo,
306 _("Signature made on %s using %s key ID %s\n"),
307 buf, keytype, keyid);
309 switch (gpg_err_code(sig->status)) {
310 case GPG_ERR_NO_ERROR:
311 g_string_append_printf(siginfo,
312 _("Good signature from uid \"%s\" (Validity: %s)\n"),
313 uid, get_validity_str(user?user->validity:GPGME_VALIDITY_UNKNOWN));
315 case GPG_ERR_KEY_EXPIRED:
316 g_string_append_printf(siginfo,
317 _("Expired key uid \"%s\"\n"),
320 case GPG_ERR_SIG_EXPIRED:
321 g_string_append_printf(siginfo,
322 _("Expired signature from uid \"%s\" (Validity: %s)\n"),
323 uid, get_validity_str(user?user->validity:GPGME_VALIDITY_UNKNOWN));
325 case GPG_ERR_CERT_REVOKED:
326 g_string_append_printf(siginfo,
327 _("Revoked key uid \"%s\"\n"),
330 case GPG_ERR_BAD_SIGNATURE:
331 g_string_append_printf(siginfo,
332 _("BAD signature from \"%s\"\n"),
338 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
340 user = user ? user->next : NULL;
341 while (user != NULL) {
342 g_string_append_printf(siginfo,
343 _(" uid \"%s\" (Validity: %s)\n"),
345 user->revoked==TRUE?_("Revoked"):get_validity_str(user->validity));
349 g_string_append_printf(siginfo,_("Owner Trust: %s\n"),
350 get_owner_trust_str(key->owner_trust));
351 g_string_append(siginfo,
352 _("Primary key fingerprint:"));
353 const char* primary_fpr = NULL;
354 if (key && key->subkeys && key->subkeys->fpr)
355 primary_fpr = key->subkeys->fpr;
357 g_string_append(siginfo, " ?");
358 int idx; /* now pretty-print the fingerprint */
359 for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
361 g_string_append_c(siginfo, ' ');
363 g_string_append_c(siginfo, ' ');
364 g_string_append_c(siginfo, (gchar)*primary_fpr);
366 g_string_append_c(siginfo, '\n');
367 #ifdef HAVE_GPGME_PKA_TRUST
368 if (sig->pka_trust == 1 && sig->pka_address) {
369 g_string_append_printf(siginfo,
370 _("WARNING: Signer's address \"%s\" "
371 "does not match DNS entry\n"),
374 else if (sig->pka_trust == 2 && sig->pka_address) {
375 g_string_append_printf(siginfo,
376 _("Verified signer's address is \"%s\"\n"),
378 /* FIXME: Compare the address to the
381 #endif /*HAVE_GPGME_PKA_TRUST*/
384 g_string_append(siginfo, "\n");
390 g_string_free(siginfo, FALSE);
394 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
396 gpgme_data_t data = NULL;
398 FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
403 err = gpgme_data_new_from_filepart(&data, NULL, fp, mimeinfo->offset, mimeinfo->length);
405 debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
407 debug_print ("gpgme_data_new_from_file failed: %s\n",
408 gpgme_strerror (err));
409 privacy_set_error(_("Couldn't get data from message, %s"), gpgme_strerror(err));
415 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
417 struct passphrase_cb_info_s info;
421 memset (&info, 0, sizeof info);
423 if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
425 privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
429 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
430 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
431 if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
433 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
436 prefs_gpg_enable_agent(TRUE);
438 gpgme_set_passphrase_cb (ctx, NULL, &info);
442 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
443 err = gpgme_op_decrypt_verify(ctx, cipher, plain);
444 if (err != GPG_ERR_NO_ERROR) {
445 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
446 privacy_set_error("%s", gpgme_strerror(err));
447 gpgmegtk_free_passphrase();
448 gpgme_data_release(plain);
452 err = cm_gpgme_data_rewind(plain);
454 debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
457 debug_print("decrypted.\n");
458 *status = gpgme_op_verify_result (ctx);
460 err = gpgme_op_decrypt(ctx, cipher, plain);
461 if (err != GPG_ERR_NO_ERROR) {
462 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
463 privacy_set_error("%s", gpgme_strerror(err));
464 gpgmegtk_free_passphrase();
465 gpgme_data_release(plain);
469 err = cm_gpgme_data_rewind(plain);
471 debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
474 debug_print("decrypted.\n");
475 *status = gpgme_op_verify_result (ctx);
480 gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
482 SelectionResult result = KEY_SELECTION_CANCEL;
483 gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result,
489 if (result == KEY_SELECTION_DONT)
490 return g_strdup("_DONT_ENCRYPT_");
495 gpgme_subkey_t skey = keys[i]->subkeys;
496 gchar *fpr = skey->fpr;
498 debug_print("adding %s\n", fpr);
499 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
507 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
508 const gchar *from_addr)
510 GPGAccountConfig *config;
511 const gchar *signer_addr = account->address;
513 gpgme_signers_clear(ctx);
516 signer_addr = from_addr;
517 config = prefs_gpg_account_get_config(account);
519 switch(config->sign_key) {
520 case SIGN_KEY_DEFAULT:
521 debug_print("using default gnupg key\n");
523 case SIGN_KEY_BY_FROM:
524 debug_print("using key for %s\n", signer_addr);
526 case SIGN_KEY_CUSTOM:
527 debug_print("using key for %s\n", config->sign_key_id);
531 if (config->sign_key != SIGN_KEY_DEFAULT) {
533 gpgme_key_t key, found_key;
536 if (config->sign_key == SIGN_KEY_BY_FROM)
538 else if (config->sign_key == SIGN_KEY_CUSTOM)
539 keyid = config->sign_key_id;
544 /* Look for any key, not just private ones, or GPGMe doesn't
545 * correctly set the revoked flag. */
546 err = gpgme_op_keylist_start(ctx, keyid, 0);
547 while ((err = gpgme_op_keylist_next(ctx, &key)) == 0) {
554 if (key->protocol != gpgme_get_protocol(ctx)) {
555 debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
556 gpgme_key_release(key);
561 debug_print("skipping a key, expired");
562 gpgme_key_release(key);
566 debug_print("skipping a key, revoked");
567 gpgme_key_release(key);
571 debug_print("skipping a key, disabled");
572 gpgme_key_release(key);
576 if (found_key != NULL) {
577 gpgme_key_release(key);
578 gpgme_op_keylist_end(ctx);
579 g_warning("ambiguous specification of secret key '%s'\n", keyid);
580 privacy_set_error(_("Secret key specification is ambiguous"));
586 gpgme_op_keylist_end(ctx);
588 if (found_key == NULL) {
589 g_warning("setup_signers start: %s", gpgme_strerror(err));
590 privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
594 err = gpgme_signers_add(ctx, found_key);
595 debug_print("got key (proto %d (pgp %d, smime %d).\n",
596 found_key->protocol, GPGME_PROTOCOL_OpenPGP,
598 gpgme_key_release(found_key);
601 g_warning("error adding secret key: %s\n",
602 gpgme_strerror(err));
603 privacy_set_error(_("Error setting secret key: %s"),
604 gpgme_strerror(err));
609 prefs_gpg_account_free_config(config);
613 prefs_gpg_account_free_config(config);
619 gchar *ctype_locale = NULL, *messages_locale = NULL;
620 gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL;
622 gpgme_engine_info_t engineInfo;
623 if (gpgme_check_version("1.0.0")) {
625 debug_print("setting gpgme CTYPE locale\n");
627 ctype_locale = g_win32_getlocale();
629 ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
632 debug_print("setting gpgme CTYPE locale to: %s\n", ctype_locale);
633 if (strchr(ctype_locale, '.'))
634 *(strchr(ctype_locale, '.')) = '\0';
635 else if (strchr(ctype_locale, '@'))
636 *(strchr(ctype_locale, '@')) = '\0';
637 ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL);
639 debug_print("setting gpgme locale to UTF8: %s\n", ctype_utf8_locale ? ctype_utf8_locale : "NULL");
640 gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale);
642 debug_print("done\n");
643 g_free(ctype_utf8_locale);
644 g_free(ctype_locale);
646 debug_print("couldn't set gpgme CTYPE locale\n");
650 debug_print("setting gpgme MESSAGES locale\n");
652 messages_locale = g_win32_getlocale();
654 messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
656 if (messages_locale) {
657 debug_print("setting gpgme MESSAGES locale to: %s\n", messages_locale);
658 if (strchr(messages_locale, '.'))
659 *(strchr(messages_locale, '.')) = '\0';
660 else if (strchr(messages_locale, '@'))
661 *(strchr(messages_locale, '@')) = '\0';
662 messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL);
663 debug_print("setting gpgme locale to UTF8: %s\n", messages_utf8_locale ? messages_utf8_locale : "NULL");
665 gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale);
667 debug_print("done\n");
668 g_free(messages_utf8_locale);
669 g_free(messages_locale);
671 debug_print("couldn't set gpgme MESSAGES locale\n");
674 if (!gpgme_get_engine_info(&engineInfo)) {
676 debug_print("GpgME Protocol: %s\n"
677 "Version: %s (req %s)\n"
679 gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
680 engineInfo->version ? engineInfo->version:"???",
681 engineInfo->req_version ? engineInfo->req_version:"???",
682 engineInfo->file_name ? engineInfo->file_name:"???");
683 if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
684 && gpgme_engine_check_version(engineInfo->protocol) !=
686 if (engineInfo->file_name && !engineInfo->version) {
687 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
688 "Engine '%s' isn't installed properly."),
689 gpgme_get_protocol_name(engineInfo->protocol),
690 engineInfo->file_name);
691 } else if (engineInfo->file_name && engineInfo->version
692 && engineInfo->req_version) {
693 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
694 "Engine '%s' version %s is installed, "
695 "but version %s is required.\n"),
696 gpgme_get_protocol_name(engineInfo->protocol),
697 engineInfo->file_name,
699 engineInfo->req_version);
701 alertpanel_error(_("Gpgme protocol '%s' is unusable "
702 "(unknown problem)"),
703 gpgme_get_protocol_name(engineInfo->protocol));
706 engineInfo = engineInfo->next;
710 sgpgme_disable_all();
712 if (prefs_gpg_get_config()->gpg_warning) {
715 val = alertpanel_full
717 _("GnuPG is not installed properly, or needs "
719 "OpenPGP support disabled."),
720 GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
721 ALERT_WARNING, G_ALERTDEFAULT);
722 if (val & G_ALERTDISABLE)
723 prefs_gpg_get_config()->gpg_warning = FALSE;
730 gpgmegtk_free_passphrase();
733 void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
735 AlertValue val = G_ALERTDEFAULT;
736 gchar *key_parms = NULL;
739 gchar *passphrase = NULL, *passphrase_second = NULL;
742 gpgme_error_t err = 0;
744 GtkWidget *window = NULL;
745 gpgme_genkey_result_t key;
748 account = account_get_default();
750 if (account->address == NULL) {
751 alertpanel_error(_("You have to save the account's information with \"OK\" "
752 "before being able to generate a key pair.\n"));
756 val = alertpanel(_("No PGP key found"),
757 _("Claws Mail did not find a secret PGP key, "
758 "which means that you won't be able to sign "
759 "emails or receive encrypted emails.\n"
760 "Do you want to create a new key pair now?"),
761 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
762 if (val == G_ALERTDEFAULT) {
763 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
764 prefs_gpg_save_config();
770 name = g_strdup(account->name);
772 name = g_strdup(account->address);
774 email = g_strdup(account->address);
775 tmp = g_strdup_printf("%s <%s>", account->name?account->name:account->address, account->address);
777 passphrase = passphrase_mbox(tmp, NULL, prev_bad, 1);
778 if (passphrase == NULL) {
784 passphrase_second = passphrase_mbox(tmp, NULL, 0, 2);
785 if (passphrase_second == NULL) {
792 if (strcmp(passphrase, passphrase_second)) {
794 g_free(passphrase_second);
799 key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
803 "Subkey-Length: 2048\n"
808 "</GnupgKeyParms>\n",
810 strlen(passphrase)?"Passphrase: ":"",
812 strlen(passphrase)?"\n":"");
813 #ifndef G_PLATFORM_WIN32
814 if (mlock(passphrase, strlen(passphrase)) == -1)
815 debug_print("couldn't lock passphrase\n");
816 if (mlock(passphrase_second, strlen(passphrase_second)) == -1)
817 debug_print("couldn't lock passphrase2\n");
822 g_free(passphrase_second);
825 err = gpgme_new (&ctx);
827 alertpanel_error(_("Couldn't generate a new key pair: %s"),
828 gpgme_strerror(err));
834 window = label_window_create(_("Generating your new key pair... Please move the mouse "
835 "around to help generate entropy..."));
837 err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
840 label_window_destroy(window);
843 alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
847 key = gpgme_op_genkey_result(ctx);
849 alertpanel_error(_("Couldn't generate a new key pair: unknown error"));
853 gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
854 "Its fingerprint is:\n%s\n\nDo you want to export it "
856 key->fpr ? key->fpr:"null");
857 AlertValue val = alertpanel(_("Key generated"), buf,
858 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
860 if (val == G_ALERTALTERNATE) {
862 gchar *cmd = g_strdup_printf("gpg --no-tty --send-keys %s", key->fpr);
868 } else if (pid == 0) {
871 res = WEXITSTATUS(res);
875 time_t start_wait = time(NULL);
878 if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
881 res = WEXITSTATUS(status);
884 if (time(NULL) - start_wait > 5) {
885 debug_print("SIGTERM'ing gpg\n");
888 if (time(NULL) - start_wait > 6) {
889 debug_print("SIGKILL'ing gpg\n");
896 alertpanel_notice(_("Key exported."));
898 alertpanel_error(_("Couldn't export key."));
902 alertpanel_error(_("Key export isn't implemented in Windows."));
906 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
907 prefs_gpg_save_config();
911 gboolean sgpgme_has_secret_key(void)
913 gpgme_error_t err = 0;
917 err = gpgme_new (&ctx);
919 debug_print("err : %s\n", gpgme_strerror(err));
923 err = gpgme_op_keylist_start(ctx, NULL, TRUE);
925 err = gpgme_op_keylist_next(ctx, &key);
926 gpgme_op_keylist_end(ctx);
927 if (gpg_err_code(err) == GPG_ERR_EOF) {
928 if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
929 gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
940 void sgpgme_check_create_key(void)
942 if (prefs_gpg_get_config()->gpg_ask_create_key &&
943 !sgpgme_has_secret_key()) {
944 sgpgme_create_secret_key(NULL, TRUE);
946 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
947 prefs_gpg_save_config();
951 void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
963 /* I know it's deprecated, but we don't compile with _LARGEFILE */
964 cm_gpgme_data_rewind(data);
965 while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
966 result = realloc(result, r + w);
967 if (result == NULL) {
968 g_warning("can't allocate memory\n");
971 memcpy(result+w, buf, r);
977 gpgme_data_release(data);
986 gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
988 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
989 if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
990 return gpg_error_from_errno(errno);
994 return gpgme_data_rewind(dh);
998 #endif /* USE_GPGME */