2006-06-29 [colin] 2.3.1cvs40
[claws.git] / src / plugins / pgpcore / sgpgme.c
index dba63e7c93226cf1450c83dc3ffd53355f9a7609..0d1ed252b8468d5007b4621978d8a8d22992edc4 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto & the Sylpheed-Claws team
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws 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
@@ -57,6 +57,7 @@ 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));
                return GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR);
                
        }
@@ -159,9 +160,11 @@ gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
        gchar *uname = NULL;
        gpgme_key_t key;
        gchar *result = NULL;
+       gpgme_error_t err = 0;
+       static gboolean warned = FALSE;
 
        if (GPOINTER_TO_INT(status) == -GPG_ERR_SYSTEM_ERROR) {
-               return g_strdup(_("The signature can't be checked - GPG error."));
+               return g_strdup_printf(_("The signature can't be checked - %s"), privacy_get_error());
        }
 
        if (status == NULL) {
@@ -172,7 +175,14 @@ gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                return g_strdup(_("The signature has not been checked."));
        }
 
-       gpgme_get_key(ctx, sig->fpr, &key, 0);
+       err = gpgme_get_key(ctx, sig->fpr, &key, 0);
+       if (gpg_err_code(err) == GPG_ERR_NO_AGENT) {
+               if (!warned)
+                       alertpanel_error(_("PGP Core: Can't get key - no gpg-agent running."));
+               else
+                       g_warning(_("PGP Core: Can't get key - no gpg-agent running."));
+               warned = TRUE;
+       }
        if (key)
                uname = extract_name(key->uids->uid);
        else
@@ -282,8 +292,23 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                        g_string_append_printf(siginfo,
                                _("Primary key fingerprint: %s\n"), 
                                sig ? sig->fpr: "?");
+#ifdef HAVE_GPGME_PKA_TRUST
+                        if (sig->pka_trust == 1 && sig->pka_address) {
+                                g_string_append_printf(siginfo,
+                                   _("WARNING: Signer's address \"%s\" "
+                                      "does not match DNS entry\n"), 
+                                   sig->pka_address);
+                        }
+                        else if (sig->pka_trust == 2 && sig->pka_address) {
+                                g_string_append_printf(siginfo,
+                                   _("Verified signer's address is \"%s\"\n"),
+                                   sig->pka_address);
+                                /* FIXME: Compare the address to the
+                                 * From: address.  */
+                        }
+#endif /*HAVE_GPGME_PKA_TRUST*/
                }
-               
+
                g_string_append(siginfo, "\n");
                i++;
                sig = sig->next;
@@ -320,6 +345,7 @@ gpgme_data_t sgpgme_data_from_mimeinfo(MimeInfo *mimeinfo)
        if (err) {
                debug_print ("gpgme_data_new_from_file failed: %s\n",
                             gpgme_strerror (err));
+               privacy_set_error(_("Couldn't get data from message, %s"), gpgme_strerror(err));
                return NULL;
        }
        return data;
@@ -333,39 +359,65 @@ gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *s
 
        memset (&info, 0, sizeof info);
        
-       if (gpgme_data_new(&plain) != GPG_ERR_NO_ERROR) {
+       if ((err = gpgme_data_new(&plain)) != GPG_ERR_NO_ERROR) {
                gpgme_release(ctx);
+               privacy_set_error(_("Couldn't initialize data, %s"), gpgme_strerror(err));
                return NULL;
        }
        
-       if (!getenv("GPG_AGENT_INFO")) {
+       if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
+               if (!getenv("GPG_AGENT_INFO")) {
+                       info.c = ctx;
+                       gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
+               }
+       } else {
                info.c = ctx;
-               gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
-       }
-
-       err = gpgme_op_decrypt_verify(ctx, cipher, plain);
-       if (err != GPG_ERR_NO_ERROR) {
-               debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
-               gpgmegtk_free_passphrase();
-               gpgme_data_release(plain);
-               return NULL;
+               gpgme_set_passphrase_cb (ctx, NULL, &info);
        }
+       
+       
+       if (gpgme_get_protocol(ctx) == GPGME_PROTOCOL_OpenPGP) {
+               err = gpgme_op_decrypt_verify(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;
+               }
 
-       err = gpgme_data_rewind(plain);
-       if (err) {
-               debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
-       }
+               err = gpgme_data_rewind(plain);
+               if (err) {
+                       debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
+               }
+
+               debug_print("decrypted.\n");
+               *status = gpgme_op_verify_result (ctx);
+       } else {
+               err = gpgme_op_decrypt(ctx, cipher, plain);
+               if (err != GPG_ERR_NO_ERROR) {
+                       debug_print("can't decrypt (%s)\n", gpgme_strerror(err));
+                       gpgmegtk_free_passphrase();
+                       gpgme_data_release(plain);
+                       return NULL;
+               }
 
-       debug_print("decrypted.\n");
-       *status = gpgme_op_verify_result (ctx);
+               err = gpgme_data_rewind(plain);
+               if (err) {
+                       debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
+               }
 
+               debug_print("decrypted.\n");
+               *status = gpgme_op_verify_result (ctx);
+       }
        return plain;
 }
 
-gchar *sgpgme_get_encrypt_data(GSList *recp_names)
+gchar *sgpgme_get_encrypt_data(GSList *recp_names, gpgme_protocol_t proto)
 {
        SelectionResult result = KEY_SELECTION_CANCEL;
-       gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result);
+       gpgme_key_t *keys = gpgmegtk_recipient_selection(recp_names, &result,
+                               proto);
        gchar *ret = NULL;
        int i = 0;
 
@@ -396,41 +448,108 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account)
 
        config = prefs_gpg_account_get_config(account);
 
+       switch(config->sign_key) {
+       case SIGN_KEY_DEFAULT:
+               debug_print("using default gnupg key\n");
+               break;
+       case SIGN_KEY_BY_FROM:
+               debug_print("using key for %s\n", account->address);
+               break;
+       case SIGN_KEY_CUSTOM:
+               debug_print("using key for %s\n", config->sign_key_id);
+               break;
+       }
+
        if (config->sign_key != SIGN_KEY_DEFAULT) {
                gchar *keyid;
-               gpgme_key_t key;
+               gpgme_key_t key, key2;
+               gpgme_error_t err;
 
                if (config->sign_key == SIGN_KEY_BY_FROM)
                        keyid = account->address;
                else if (config->sign_key == SIGN_KEY_CUSTOM)
                        keyid = config->sign_key_id;
                else
-                       return FALSE;
-
-               gpgme_op_keylist_start(ctx, keyid, 1);
-               while (!gpgme_op_keylist_next(ctx, &key)) {
-                       gpgme_signers_add(ctx, key);
-                       gpgme_key_release(key);
+                       goto bail;
+
+               err = gpgme_op_keylist_start(ctx, keyid, 1);
+               if (!err)
+                       err = gpgme_op_keylist_next(ctx, &key);
+               if (err) {
+                       g_warning("setup_signers start: %s", gpgme_strerror(err));
+                       privacy_set_error(_("Private key not found (%s)"), gpgme_strerror(err));
+                       goto bail;
                }
+               
+               err = gpgme_op_keylist_next(ctx, &key2);
+               if (!err) {
+                       g_warning("ambiguous specification of private key '%s'\n",
+                               keyid);
+                       privacy_set_error(_("Private key specification is ambiguous"));
+                       goto bail;
+               }
+               
                gpgme_op_keylist_end(ctx);
+               err = gpgme_signers_add(ctx, key);
+               gpgme_key_release(key);
+               
+               if (err) {
+                       g_warning("error adding secret key: %s\n", gpgme_strerror(err));
+                       privacy_set_error(_("Error setting private key: %s"), gpgme_strerror(err));
+                       goto bail;
+               }
        }
 
        prefs_gpg_account_free_config(config);
 
        return TRUE;
+bail:
+       prefs_gpg_account_free_config(config);
+       return FALSE;
 }
 
 void sgpgme_init()
 {
        gpgme_engine_info_t engineInfo;
-       if (gpgme_check_version("0.4.5")) {
+       if (gpgme_check_version("1.0.0")) {
+#ifdef LC_CTYPE
                gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
+#endif
+#ifdef LC_MESSAGES
                gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
+#endif
                if (!gpgme_get_engine_info(&engineInfo)) {
                        while (engineInfo) {
-                               debug_print("GpgME Protocol: %s\n      Version: %s\n",
+                               debug_print("GpgME Protocol: %s\n"
+                                           "Version: %s (req %s)\n"
+                                           "Executable: %s\n",
                                        gpgme_get_protocol_name(engineInfo->protocol),
-                                       engineInfo->version);
+                                       engineInfo->version ? engineInfo->version:"???",
+                                       engineInfo->req_version ? engineInfo->req_version:"???",
+                                       engineInfo->file_name ? engineInfo->file_name:"???");
+                               if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP
+                               &&  gpgme_engine_check_version(engineInfo->protocol) != 
+                                       GPG_ERR_NO_ERROR) {
+                                       if (engineInfo->file_name && !engineInfo->version) {
+                                               alertpanel_error(_("Gpgme protocol '%s' is unusable: "
+                                                                  "Engine '%s' isn't installed properly."),
+                                                                  gpgme_get_protocol_name(engineInfo->protocol),
+                                                                  engineInfo->file_name);
+                                       } else if (engineInfo->file_name && engineInfo->version
+                                         && engineInfo->req_version) {
+                                               alertpanel_error(_("Gpgme protocol '%s' is unusable: "
+                                                                  "Engine '%s' version %s is installed, "
+                                                                  "but version %s is required.\n"),
+                                                                  gpgme_get_protocol_name(engineInfo->protocol),
+                                                                  engineInfo->file_name,
+                                                                  engineInfo->version,
+                                                                  engineInfo->req_version);
+                                       } else {
+                                               alertpanel_error(_("Gpgme protocol '%s' is unusable "
+                                                                  "(unknown problem)"),
+                                                                  gpgme_get_protocol_name(engineInfo->protocol));
+                                       }
+                               }
                                engineInfo = engineInfo->next;
                        }
                }