2008-12-15 [colin] 3.6.1cvs78
[claws.git] / src / plugins / pgpcore / sgpgme.c
index b167a562cf62839055ac9b8b68dd18ed6eb541e4..2ba23ba45c1c3fe11f4948ee3769298e29a99f9e 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2007 the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
  
 #ifdef HAVE_CONFIG_H
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
 #include <errno.h>
+#include <sys/types.h>
+#ifndef G_OS_WIN32
+#  include <sys/wait.h>
+#endif
+#if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
+#  include <sys/signal.h>
+#endif
 #ifndef G_OS_WIN32
 #include <sys/mman.h>
 #endif
@@ -61,12 +70,15 @@ gpgme_verify_result_t sgpgme_verify_signature(gpgme_ctx_t ctx, gpgme_data_t sig,
 
        if ((err = gpgme_op_verify(ctx, sig, plain, dummy)) != GPG_ERR_NO_ERROR) {
                debug_print("op_verify err %s\n", gpgme_strerror(err));
-               privacy_set_error(gpgme_strerror(err));
+               privacy_set_error("%s", gpgme_strerror(err));
                return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
-               
        }
        status = gpgme_op_verify_result(ctx);
-
+       if (status && status->signatures == NULL) {
+               debug_print("no signature found\n");
+               privacy_set_error(_("No signature found"));
+               return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
+       }
        return status;
 }
 
@@ -108,8 +120,10 @@ SignatureStatus sgpgme_sigstat_gpgme_to_privacy(gpgme_ctx_t ctx, gpgme_verify_re
                        return SIGNATURE_CHECK_FAILED;
                }
        case GPG_ERR_SIG_EXPIRED:
-       case GPG_ERR_KEY_EXPIRED:
+       case GPG_ERR_CERT_REVOKED:
                return SIGNATURE_WARN;
+       case GPG_ERR_KEY_EXPIRED:
+               return SIGNATURE_KEY_EXPIRED;
        case GPG_ERR_BAD_SIGNATURE:
                return SIGNATURE_INVALID;
        case GPG_ERR_NO_PUBKEY:
@@ -186,6 +200,9 @@ gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                else
                        g_warning(_("PGP Core: Can't get key - no gpg-agent running."));
                warned = TRUE;
+       } else if (gpg_err_code(err) != GPG_ERR_NO_ERROR && gpg_err_code(err) != GPG_ERR_EOF) {
+               return g_strdup_printf(_("The signature can't be checked - %s"), 
+                       gpgme_strerror(err));
        }
        if (key)
                uname = extract_name(key->uids->uid);
@@ -243,11 +260,18 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
        while (sig) {
                gpgme_user_id_t user = NULL;
                gpgme_key_t key;
-
+               gpgme_error_t err;
                const gchar *keytype, *keyid, *uid;
                
-               gpgme_get_key(ctx, sig->fpr, &key, 0);
+               err = gpgme_get_key(ctx, sig->fpr, &key, 0);
 
+               if (err != GPG_ERR_NO_ERROR) {
+                       key = NULL;
+                       g_string_append_printf(siginfo, 
+                               _("Error checking signature: %s\n"),
+                               gpgme_strerror(err));
+                       goto bail;
+               }
                if (key) {
                        user = key->uids;
                        keytype = gpgme_pubkey_algo_name(
@@ -267,13 +291,13 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                case GPG_ERR_NO_ERROR:
                case GPG_ERR_KEY_EXPIRED:
                        g_string_append_printf(siginfo,
-                               _("Good signature from \"%s\" (Trust: %s)\n"),
-                               uid, get_validity_str(sig->validity));
+                               _("Good signature from uid \"%s\" (Validity: %s)\n"),
+                               uid, get_validity_str(user->validity));
                        break;
                case GPG_ERR_SIG_EXPIRED:
                        g_string_append_printf(siginfo,
-                               _("Expired signature from \"%s\"\n"),
-                               uid);
+                               _("Expired signature from uid \"%s\" (Validity: %s)\n"),
+                               uid, get_validity_str(user->validity));
                        break;
                case GPG_ERR_BAD_SIGNATURE:
                        g_string_append_printf(siginfo,
@@ -288,8 +312,9 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                        user = user ? user->next : NULL;
                        while (user != NULL) {
                                g_string_append_printf(siginfo,
-                                       _("                aka \"%s\"\n"),
-                                       user->uid);
+                                       _("                    uid \"%s\" (Validity: %s)\n"),
+                                       user->uid,
+                                       get_validity_str(user->validity));
                                j++;
                                user = user->next;
                        }
@@ -317,7 +342,7 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                i++;
                sig = sig->next;
        }
-
+bail:
        ret = siginfo->str;
        g_string_free(siginfo, FALSE);
        return ret;
@@ -340,7 +365,7 @@ gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
        debug_print("tmp file %s\n", tmp_file);
        
        err = gpgme_data_new_from_file(&data, tmp_file, 1);
-       g_unlink(tmp_file);
+       claws_unlink(tmp_file);
        g_free(tmp_file);
 
        debug_print("data %p (%d %d)\n", (void *)&data, mimeinfo->offset, mimeinfo->length);
@@ -368,11 +393,13 @@ gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *s
        }
        
        if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
-               if (!getenv("GPG_AGENT_INFO")) {
+               prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
+               if (!getenv("GPG_AGENT_INFO") || !prefs_gpg_get_config()->use_gpg_agent) {
                        info.c = ctx;
                        gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
                }
        } else {
+               prefs_gpg_enable_agent(TRUE);
                info.c = ctx;
                gpgme_set_passphrase_cb (ctx, NULL, &info);
        }
@@ -399,6 +426,7 @@ gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *s
                err = gpgme_op_decrypt(ctx, cipher, plain);
                if (err != GPG_ERR_NO_ERROR) {
                        debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
+                       privacy_set_error("%s", gpgme_strerror(err));
                        gpgmegtk_free_passphrase();
                        gpgme_data_release(plain);
                        return NULL;
@@ -442,12 +470,16 @@ gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
        return ret;
 }
 
-gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
+gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
+                             const gchar *from_addr)
 {
        GPGAccountConfig *config;
+       const gchar *signer_addr = account->address;
 
        gpgme_signers_clear(ctx);
 
+       if (from_addr)
+               signer_addr = from_addr;
        config = prefs_gpg_account_get_config(account);
 
        switch(config->sign_key) {
@@ -455,7 +487,7 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
                debug_print("using default gnupg key\n");
                break;
        case SIGN_KEY_BY_FROM:
-               debug_print("using key for %s\n", account->address);
+               debug_print("using key for %s\n", signer_addr);
                break;
        case SIGN_KEY_CUSTOM:
                debug_print("using key for %s\n", config->sign_key_id);
@@ -468,23 +500,51 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
                gpgme_error_t err;
 
                if (config->sign_key == SIGN_KEY_BY_FROM)
-                       keyid = account->address;
+                       keyid = signer_addr;
                else if (config->sign_key == SIGN_KEY_CUSTOM)
                        keyid = config->sign_key_id;
                else
                        goto bail;
 
                err = gpgme_op_keylist_start(ctx, keyid, 1);
-               if (!err)
-                       err = gpgme_op_keylist_next(ctx, &key);
+               if (!err) {
+                       do {
+                               err = gpgme_op_keylist_next(ctx, &key);
+                               if (!err && key && key->protocol == gpgme_get_protocol(ctx) &&
+                                   !key->expired && !key->revoked)
+                                       break;
+                               if (!err && key && key->protocol != gpgme_get_protocol(ctx)) {
+                                       debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
+                                       gpgme_key_release(key);
+                               }
+                               if (!err && key && (key->expired || key->revoked)) {
+                                       debug_print("skipping a key (%s)\n", key->expired?"expired":"revoked");
+                                       gpgme_key_release(key);
+                               }
+                       } while (!err);
+               }
                if (err) {
                        g_warning("setup_signers start: %s", gpgme_strerror(err));
                        privacy_set_error(_("Secret key not found (%s)"), gpgme_strerror(err));
                        goto bail;
                }
                
-               err = gpgme_op_keylist_next(ctx, &key2);
+               do {
+                       err = gpgme_op_keylist_next(ctx, &key2);
+                       if (!err && key2 && key2->protocol == gpgme_get_protocol(ctx) &&
+                           !key2->expired && !key2->revoked)
+                               break;
+                       if (!err && key && key2->protocol != gpgme_get_protocol(ctx)) {
+                               debug_print("skipping a key (wrong protocol %d)\n", key2->protocol);
+                               gpgme_key_release(key2);
+                       }
+                       if (!err && key && (key2->expired || key2->revoked)) {
+                               debug_print("skipping a key (%s)\n", key2->expired?"expired":"revoked");
+                               gpgme_key_release(key2);
+                       }
+               } while (!err);
                if (!err) {
+                       gpgme_key_release(key2);
                        g_warning("ambiguous specification of secret key '%s'\n",
                                keyid);
                        privacy_set_error(_("Secret key specification is ambiguous"));
@@ -493,6 +553,8 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
                
                gpgme_op_keylist_end(ctx);
                err = gpgme_signers_add(ctx, key);
+               debug_print("got key (proto %d (pgp %d, smime %d).\n", key->protocol,
+                               GPGME_PROTOCOL_OpenPGP, GPGME_PROTOCOL_CMS);
                gpgme_key_release(key);
                
                if (err) {
@@ -522,13 +584,15 @@ void sgpgme_init()
 #endif
                if (!gpgme_get_engine_info(&engineInfo)) {
                        while (engineInfo) {
+#ifndef G_OS_WIN32
                                debug_print("GpgME Protocol: %s\n"
                                            "Version: %s (req %s)\n"
                                            "Executable: %s\n",
-                                       gpgme_get_protocol_name(engineInfo->protocol),
+                                       gpgme_get_protocol_name(engineInfo->protocol) ? gpgme_get_protocol_name(engineInfo->protocol):"???",
                                        engineInfo->version ? engineInfo->version:"???",
                                        engineInfo->req_version ? engineInfo->req_version:"???",
                                        engineInfo->file_name ? engineInfo->file_name:"???");
+#endif
                                if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
                                &&  gpgme_engine_check_version(engineInfo->protocol) != 
                                        GPG_ERR_NO_ERROR) {
@@ -596,9 +660,14 @@ void sgpgme_create_secret_key(PrefsAccount *account, gboolean ask_create)
        if (account == NULL)
                account = account_get_default();
 
+       if (account->address == NULL) {
+               alertpanel_error(_("You have to save the account's information with \"OK\" "
+                                  "before being able to generate a key pair.\n"));
+               return;
+       }
        if (ask_create) {
                val = alertpanel(_("No PGP key found"),
-                               _("Sylpheed-Claws did not find a secret PGP key, "
+                               _("Claws Mail did not find a secret PGP key, "
                                  "which means that you won't be able to sign "
                                  "emails or receive encrypted emails.\n"
                                  "Do you want to create a new key pair now?"),
@@ -681,7 +750,7 @@ again:
        err = gpgme_op_genkey(ctx, key_parms, NULL, NULL);
        g_free(key_parms);
 
-       gtk_widget_destroy(window);
+       label_window_destroy(window);
 
        if (err) {
                alertpanel_error(_("Couldn't generate a new key pair: %s"), gpgme_strerror(err));
@@ -694,9 +763,58 @@ again:
                gpgme_release(ctx);
                return;
        } else {
-               alertpanel_notice(_("Your new key pair has been generated. "
-                                   "Its fingerprint is:\n%s"),
+               gchar *buf = g_strdup_printf(_("Your new key pair has been generated. "
+                                   "Its fingerprint is:\n%s\n\nDo you want to export it "
+                                   "to a keyserver?"),
                                    key->fpr ? key->fpr:"null");
+               AlertValue val = alertpanel(_("Key generated"), buf,
+                                 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
+               g_free(buf);
+               if (val == G_ALERTALTERNATE) {
+#ifndef G_OS_WIN32
+                       gchar *cmd = g_strdup_printf("gpg --no-tty --send-keys %s", key->fpr);
+                       int res = 0;
+                       pid_t pid = 0;
+                       pid = fork();
+                       if (pid == -1) {
+                               res = -1;
+                       } else if (pid == 0) {
+                               /* son */
+                               res = system(cmd);
+                               res = WEXITSTATUS(res);
+                               _exit(res);
+                       } else {
+                               int status = 0;
+                               time_t start_wait = time(NULL);
+                               res = -1;
+                               do {
+                                       if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
+                                               usleep(200000);
+                                       } else {
+                                               res = WEXITSTATUS(status);
+                                               break;
+                                       }
+                                       if (time(NULL) - start_wait > 5) {
+                                               debug_print("SIGTERM'ing gpg\n");
+                                               kill(pid, SIGTERM);
+                                       }
+                                       if (time(NULL) - start_wait > 6) {
+                                               debug_print("SIGKILL'ing gpg\n");
+                                               kill(pid, SIGKILL);
+                                               break;
+                                       }
+                               } while(1);
+                       }
+                       if (res == 0) {
+                               alertpanel_notice(_("Key exported."));
+                       } else {
+                               alertpanel_error(_("Couldn't export key."));
+                       }
+                       g_free(cmd);
+#else
+                       alertpanel_error(_("Key export isn't implemented in Windows."));
+#endif
+               }
        }
        prefs_gpg_get_config()->gpg_ask_create_key = FALSE;
        prefs_gpg_save_config();
@@ -735,4 +853,35 @@ void sgpgme_check_create_key(void)
                prefs_gpg_save_config();
        }       
 }
+
+void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
+{
+       char buf[BUFSIZ];
+       void *result = NULL;
+       ssize_t r = 0;
+       size_t w = 0;
+       
+       if (data == NULL)
+               return NULL;
+       if (len == NULL)
+               return NULL;
+
+       /* I know it's deprecated, but we don't compile with _LARGEFILE */
+       gpgme_data_rewind(data);
+       while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
+               result = realloc(result, r + w);
+               memcpy(result+w, buf, r);
+               w += r;
+       }
+       
+       *len = w;
+
+       gpgme_data_release(data);
+       if (r < 0) {
+               free(result);
+               *len = 0;
+               return NULL;
+       }
+       return result;
+}
 #endif /* USE_GPGME */