2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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 gchar *extract_name(const char *uid)
160 if (!strncmp(uid, "CN=", 3)) {
161 gchar *result = g_strdup(uid+3);
162 if (strstr(result, ","))
163 *(strstr(result, ",")) = '\0';
165 } else if (strstr(uid, ",CN=")) {
166 gchar *result = g_strdup(strstr(uid, ",CN=")+4);
167 if (strstr(result, ","))
168 *(strstr(result, ",")) = '\0';
171 return g_strdup(uid);
174 gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
176 gpgme_signature_t sig = NULL;
179 gchar *result = NULL;
180 gpgme_error_t err = 0;
181 static gboolean warned = FALSE;
183 if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
184 return g_strdup_printf(_("The signature can't be checked - %s"), privacy_get_error());
187 if (status == NULL) {
188 return g_strdup(_("The signature has not been checked."));
190 sig = status->signatures;
192 return g_strdup(_("The signature has not been checked."));
195 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
196 if (gpg_err_code(err) == GPG_ERR_NO_AGENT) {
198 alertpanel_error(_("PGP Core: Can't get key - no gpg-agent running."));
200 g_warning(_("PGP Core: Can't get key - no gpg-agent running."));
202 } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) != GPG_ERR_EOF) {
203 return g_strdup_printf(_("The signature can't be checked - %s"),
204 gpgme_strerror(err));
207 uname = extract_name(key->uids->uid);
209 uname = g_strdup("<?>");
210 switch (gpg_err_code(sig->status)) {
211 case GPG_ERR_NO_ERROR:
212 result = g_strdup_printf(_("Good signature from %s."), uname);
214 case GPG_ERR_SIG_EXPIRED:
215 result = g_strdup_printf(_("Expired signature from %s."), uname);
217 case GPG_ERR_KEY_EXPIRED:
218 result = g_strdup_printf(_("Good signature from %s, but the key has expired."), uname);
220 case GPG_ERR_CERT_REVOKED:
221 result = g_strdup_printf(_("Good signature from %s, but the key has been revoked."), uname);
223 case GPG_ERR_BAD_SIGNATURE:
224 result = g_strdup_printf(_("Bad signature from %s."), uname);
226 case GPG_ERR_NO_PUBKEY: {
227 gchar *id = g_strdup(sig->fpr + strlen(sig->fpr)-8);
228 result = g_strdup_printf(_("Key 0x%s not available to verify this signature."), id);
233 result = g_strdup(_("The signature has not been checked."));
237 result = g_strdup(_("Error"));
242 gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
247 gpgme_signature_t sig = NULL;
249 siginfo = g_string_sized_new(64);
250 if (status == NULL) {
251 g_string_append_printf(siginfo,
252 _("Error checking signature: no status\n"));
256 sig = status->signatures;
261 gpgme_user_id_t user = NULL;
264 const gchar *keytype, *keyid, *uid;
266 err = gpgme_get_key(ctx, sig->fpr, &key, 0);
268 if (err != GPG_ERR_NO_ERROR) {
270 g_string_append_printf(siginfo,
271 _("Error checking signature: %s\n"),
272 gpgme_strerror(err));
277 keytype = gpgme_pubkey_algo_name(
278 key->subkeys->pubkey_algo);
279 keyid = key->subkeys->keyid;
287 memset(buf, 0, sizeof(buf));
288 fast_strftime(buf, sizeof(buf)-1, prefs_common_get_prefs()->date_format, localtime_r(&sig->timestamp, <));
289 g_string_append_printf(siginfo,
290 _("Signature made on %s using %s key ID %s\n"),
291 buf, keytype, keyid);
293 switch (gpg_err_code(sig->status)) {
294 case GPG_ERR_NO_ERROR:
295 g_string_append_printf(siginfo,
296 _("Good signature from uid \"%s\" (Validity: %s)\n"),
297 uid, get_validity_str(user?user->validity:GPGME_VALIDITY_UNKNOWN));
299 case GPG_ERR_KEY_EXPIRED:
300 g_string_append_printf(siginfo,
301 _("Expired key uid \"%s\"\n"),
304 case GPG_ERR_SIG_EXPIRED:
305 g_string_append_printf(siginfo,
306 _("Expired signature from uid \"%s\" (Validity: %s)\n"),
307 uid, get_validity_str(user?user->validity:GPGME_VALIDITY_UNKNOWN));
309 case GPG_ERR_CERT_REVOKED:
310 g_string_append_printf(siginfo,
311 _("Revoked key uid \"%s\"\n"),
314 case GPG_ERR_BAD_SIGNATURE:
315 g_string_append_printf(siginfo,
316 _("BAD signature from \"%s\"\n"),
322 if (sig->status != GPG_ERR_BAD_SIGNATURE) {
324 user = user ? user->next : NULL;
325 while (user != NULL) {
326 g_string_append_printf(siginfo,
327 _(" uid \"%s\" (Validity: %s)\n"),
329 get_validity_str(user->validity));
333 g_string_append(siginfo,
334 _("Primary key fingerprint:"));
335 const char* primary_fpr = NULL;
336 if (key && key->subkeys && key->subkeys->fpr)
337 primary_fpr = key->subkeys->fpr;
339 g_string_append(siginfo, " ?");
340 int idx; /* now pretty-print the fingerprint */
341 for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
343 g_string_append_c(siginfo, ' ');
345 g_string_append_c(siginfo, ' ');
346 g_string_append_c(siginfo, (gchar)*primary_fpr);
348 g_string_append_c(siginfo, '\n');
349 #ifdef HAVE_GPGME_PKA_TRUST
350 if (sig->pka_trust == 1 && sig->pka_address) {
351 g_string_append_printf(siginfo,
352 _("WARNING: Signer's address \"%s\" "
353 "does not match DNS entry\n"),
356 else if (sig->pka_trust == 2 && sig->pka_address) {
357 g_string_append_printf(siginfo,
358 _("Verified signer's address is \"%s\"\n"),
360 /* FIXME: Compare the address to the
363 #endif /*HAVE_GPGME_PKA_TRUST*/
366 g_string_append(siginfo, "\n");
372 g_string_free(siginfo, FALSE);
376 gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
378 gpgme_data_t data = NULL;
380 FILE *fp = g_fopen(mimeinfo->data.filename, "rb");
381 gchar *tmp_file = NULL;
386 tmp_file = get_tmp_file();
387 copy_file_part(fp, mimeinfo->offset, mimeinfo->length, tmp_file);
390 debug_print("tmp file %s\n", tmp_file);
392 err = gpgme_data_new_from_file(&data, tmp_file, 1);
393 claws_unlink(tmp_file);
396 debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
398 debug_print ("gpgme_data_new_from_file failed: %s\n",
399 gpgme_strerror (err));
400 privacy_set_error(_("Couldn't get data from message, %s"), gpgme_strerror(err));
406 gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *status, gpgme_ctx_t ctx)
408 struct passphrase_cb_info_s info;
412 memset (&info, 0, sizeof info);
414 if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
416 privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
420 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
421 prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
422 if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
424 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
427 prefs_gpg_enable_agent(TRUE);
429 gpgme_set_passphrase_cb (ctx, NULL, &info);
433 if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
434 err = gpgme_op_decrypt_verify(ctx, cipher, plain);
435 if (err != GPG_ERR_NO_ERROR) {
436 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
437 privacy_set_error("%s", gpgme_strerror(err));
438 gpgmegtk_free_passphrase();
439 gpgme_data_release(plain);
443 err = cm_gpgme_data_rewind(plain);
445 debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
448 debug_print("decrypted.\n");
449 *status = gpgme_op_verify_result (ctx);
451 err = gpgme_op_decrypt(ctx, cipher, plain);
452 if (err != GPG_ERR_NO_ERROR) {
453 debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
454 privacy_set_error("%s", gpgme_strerror(err));
455 gpgmegtk_free_passphrase();
456 gpgme_data_release(plain);
460 err = cm_gpgme_data_rewind(plain);
462 debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
465 debug_print("decrypted.\n");
466 *status = gpgme_op_verify_result (ctx);
471 gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
473 SelectionResult result = KEY_SELECTION_CANCEL;
474 gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result,
480 if (result == KEY_SELECTION_DONT)
481 return g_strdup("_DONT_ENCRYPT_");
486 gpgme_subkey_t skey = keys[i]->subkeys;
487 gchar *fpr = skey->fpr;
489 debug_print("adding %s\n", fpr);
490 tmp = g_strconcat(ret?ret:"", fpr, " ", NULL);
498 gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
499 const gchar *from_addr)
501 GPGAccountConfig *config;
502 const gchar *signer_addr = account->address;
504 gpgme_signers_clear(ctx);
507 signer_addr = from_addr;
508 config = prefs_gpg_account_get_config(account);
510 switch(config->sign_key) {
511 case SIGN_KEY_DEFAULT:
512 debug_print("using default gnupg key\n");
514 case SIGN_KEY_BY_FROM:
515 debug_print("using key for %s\n", signer_addr);
517 case SIGN_KEY_CUSTOM:
518 debug_print("using key for %s\n", config->sign_key_id);
522 if (config->sign_key != SIGN_KEY_DEFAULT) {
524 gpgme_key_t key, found_key;
527 if (config->sign_key == SIGN_KEY_BY_FROM)
529 else if (config->sign_key == SIGN_KEY_CUSTOM)
530 keyid = config->sign_key_id;
535 /* Look for any key, not just private ones, or GPGMe doesn't
536 * correctly set the revoked flag. */
537 err = gpgme_op_keylist_start(ctx, keyid, 0);
538 while ((err = gpgme_op_keylist_next(ctx, &key)) == 0) {
545 if (key->protocol != gpgme_get_protocol(ctx)) {
546 debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
547 gpgme_key_release(key);
552 debug_print("skipping a key, expired");
553 gpgme_key_release(key);
557 debug_print("skipping a key, revoked");
558 gpgme_key_release(key);
562 debug_print("skipping a key, disabled");
563 gpgme_key_release(key);
567 if (found_key != NULL) {
568 gpgme_key_release(key);
569 gpgme_op_keylist_end(ctx);
570 g_warning("ambiguous specification of secret key '%s'\n", keyid);
571 privacy_set_error(_("Secret key specification is ambiguous"));
577 gpgme_op_keylist_end(ctx);
579 if (found_key == NULL) {
580 g_warning("setup_signers start: %s", gpgme_strerror(err));
581 privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
585 err = gpgme_signers_add(ctx, found_key);
586 debug_print("got key (proto %d (pgp %d, smime %d).\n",
587 found_key->protocol, GPGME_PROTOCOL_OpenPGP,
589 gpgme_key_release(found_key);
592 g_warning("error adding secret key: %s\n",
593 gpgme_strerror(err));
594 privacy_set_error(_("Error setting secret key: %s"),
595 gpgme_strerror(err));
600 prefs_gpg_account_free_config(config);
604 prefs_gpg_account_free_config(config);
610 gchar *ctype_locale = NULL, *messages_locale = NULL;
611 gchar *ctype_utf8_locale = NULL, *messages_utf8_locale = NULL;
613 gpgme_engine_info_t engineInfo;
614 if (gpgme_check_version("1.0.0")) {
616 debug_print("setting gpgme CTYPE locale\n");
618 ctype_locale = g_win32_getlocale();
620 ctype_locale = g_strdup(setlocale(LC_CTYPE, NULL));
623 debug_print("setting gpgme CTYPE locale to: %s\n", ctype_locale);
624 if (strchr(ctype_locale, '.'))
625 *(strchr(ctype_locale, '.')) = '\0';
626 else if (strchr(ctype_locale, '@'))
627 *(strchr(ctype_locale, '@')) = '\0';
628 ctype_utf8_locale = g_strconcat(ctype_locale, ".UTF-8", NULL);
630 debug_print("setting gpgme locale to UTF8: %s\n", ctype_utf8_locale ? ctype_utf8_locale : "NULL");
631 gpgme_set_locale(NULL, LC_CTYPE, ctype_utf8_locale);
633 debug_print("done\n");
634 g_free(ctype_utf8_locale);
635 g_free(ctype_locale);
637 debug_print("couldn't set gpgme CTYPE locale\n");
641 debug_print("setting gpgme MESSAGES locale\n");
643 messages_locale = g_win32_getlocale();
645 messages_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
647 if (messages_locale) {
648 debug_print("setting gpgme MESSAGES locale to: %s\n", messages_locale);
649 if (strchr(messages_locale, '.'))
650 *(strchr(messages_locale, '.')) = '\0';
651 else if (strchr(messages_locale, '@'))
652 *(strchr(messages_locale, '@')) = '\0';
653 messages_utf8_locale = g_strconcat(messages_locale, ".UTF-8", NULL);
654 debug_print("setting gpgme locale to UTF8: %s\n", messages_utf8_locale ? messages_utf8_locale : "NULL");
656 gpgme_set_locale(NULL, LC_MESSAGES, messages_utf8_locale);
658 debug_print("done\n");
659 g_free(messages_utf8_locale);
660 g_free(messages_locale);
662 debug_print("couldn't set gpgme MESSAGES locale\n");
665 if (!gpgme_get_engine_info(&engineInfo)) {
667 debug_print("GpgME Protocol: %s\n"
668 "Version: %s (req %s)\n"
670 gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
671 engineInfo->version ? engineInfo->version:"???",
672 engineInfo->req_version ? engineInfo->req_version:"???",
673 engineInfo->file_name ? engineInfo->file_name:"???");
674 if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
675 && gpgme_engine_check_version(engineInfo->protocol) !=
677 if (engineInfo->file_name && !engineInfo->version) {
678 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
679 "Engine '%s' isn't installed properly."),
680 gpgme_get_protocol_name(engineInfo->protocol),
681 engineInfo->file_name);
682 } else if (engineInfo->file_name && engineInfo->version
683 && engineInfo->req_version) {
684 alertpanel_error(_("Gpgme protocol '%s' is unusable: "
685 "Engine '%s' version %s is installed, "
686 "but version %s is required.\n"),
687 gpgme_get_protocol_name(engineInfo->protocol),
688 engineInfo->file_name,
690 engineInfo->req_version);
692 alertpanel_error(_("Gpgme protocol '%s' is unusable "
693 "(unknown problem)"),
694 gpgme_get_protocol_name(engineInfo->protocol));
697 engineInfo = engineInfo->next;
701 sgpgme_disable_all();
703 if (prefs_gpg_get_config()->gpg_warning) {
706 val = alertpanel_full
708 _("GnuPG is not installed properly, or needs "
710 "OpenPGP support disabled."),
711 GTK_STOCK_CLOSE, NULL, NULL, TRUE, NULL,
712 ALERT_WARNING, G_ALERTDEFAULT);
713 if (val & G_ALERTDISABLE)
714 prefs_gpg_get_config()->gpg_warning = FALSE;
721 gpgmegtk_free_passphrase();
724 void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
726 AlertValue val = G_ALERTDEFAULT;
727 gchar *key_parms = NULL;
730 gchar *passphrase = NULL, *passphrase_second = NULL;
733 gpgme_error_t err = 0;
735 GtkWidget *window = NULL;
736 gpgme_genkey_result_t key;
739 account = account_get_default();
741 if (account->address == NULL) {
742 alertpanel_error(_("You have to save the account's information with \"OK\" "
743 "before being able to generate a key pair.\n"));
747 val = alertpanel(_("No PGP key found"),
748 _("Claws Mail did not find a secret PGP key, "
749 "which means that you won't be able to sign "
750 "emails or receive encrypted emails.\n"
751 "Do you want to create a new key pair now?"),
752 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
753 if (val == G_ALERTDEFAULT) {
754 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
755 prefs_gpg_save_config();
761 name = g_strdup(account->name);
763 name = g_strdup(account->address);
765 email = g_strdup(account->address);
766 tmp = g_strdup_printf("%s <%s>", account->name?account->name:account->address, account->address);
768 passphrase = passphrase_mbox(tmp, NULL, prev_bad, 1);
769 if (passphrase == NULL) {
775 passphrase_second = passphrase_mbox(tmp, NULL, 0, 2);
776 if (passphrase_second == NULL) {
783 if (strcmp(passphrase, passphrase_second)) {
785 g_free(passphrase_second);
790 key_parms = g_strdup_printf("<GnupgKeyParms format=\"internal\">\n"
794 "Subkey-Length: 2048\n"
799 "</GnupgKeyParms>\n",
801 strlen(passphrase)?"Passphrase: ":"",
803 strlen(passphrase)?"\n":"");
804 #ifndef G_PLATFORM_WIN32
805 if (mlock(passphrase, strlen(passphrase)) == -1)
806 debug_print("couldn't lock passphrase\n");
807 if (mlock(passphrase_second, strlen(passphrase_second)) == -1)
808 debug_print("couldn't lock passphrase2\n");
813 g_free(passphrase_second);
816 err = gpgme_new (&ctx);
818 alertpanel_error(_("Couldn't generate a new key pair: %s"),
819 gpgme_strerror(err));
825 window = label_window_create(_("Generating your new key pair... Please move the mouse "
826 "around to help generate entropy..."));
828 err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
831 label_window_destroy(window);
834 alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
838 key = gpgme_op_genkey_result(ctx);
840 alertpanel_error(_("Couldn't generate a new key pair: unknown error"));
844 gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
845 "Its fingerprint is:\n%s\n\nDo you want to export it "
847 key->fpr ? key->fpr:"null");
848 AlertValue val = alertpanel(_("Key generated"), buf,
849 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
851 if (val == G_ALERTALTERNATE) {
853 gchar *cmd = g_strdup_printf("gpg --no-tty --send-keys %s", key->fpr);
859 } else if (pid == 0) {
862 res = WEXITSTATUS(res);
866 time_t start_wait = time(NULL);
869 if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
872 res = WEXITSTATUS(status);
875 if (time(NULL) - start_wait > 5) {
876 debug_print("SIGTERM'ing gpg\n");
879 if (time(NULL) - start_wait > 6) {
880 debug_print("SIGKILL'ing gpg\n");
887 alertpanel_notice(_("Key exported."));
889 alertpanel_error(_("Couldn't export key."));
893 alertpanel_error(_("Key export isn't implemented in Windows."));
897 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
898 prefs_gpg_save_config();
902 gboolean sgpgme_has_secret_key(void)
904 gpgme_error_t err = 0;
908 err = gpgme_new (&ctx);
910 debug_print("err : %s\n", gpgme_strerror(err));
914 err = gpgme_op_keylist_start(ctx, NULL, TRUE);
916 err = gpgme_op_keylist_next(ctx, &key);
917 gpgme_op_keylist_end(ctx);
918 if (gpg_err_code(err) == GPG_ERR_EOF) {
919 if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
920 gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
931 void sgpgme_check_create_key(void)
933 if (prefs_gpg_get_config()->gpg_ask_create_key &&
934 !sgpgme_has_secret_key()) {
935 sgpgme_create_secret_key(NULL, TRUE);
937 prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
938 prefs_gpg_save_config();
942 void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
954 /* I know it's deprecated, but we don't compile with _LARGEFILE */
955 cm_gpgme_data_rewind(data);
956 while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
957 result = realloc(result, r + w);
958 if (result == NULL) {
959 g_warning("can't allocate memory\n");
962 memcpy(result+w, buf, r);
968 gpgme_data_release(data);
977 gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
979 #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
980 if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
981 return gpg_error_from_errno(errno);
985 return gpgme_data_rewind(dh);
989 #endif /* USE_GPGME */