2012-07-10 [mones] 3.8.1cvs12
[claws.git] / src / plugins / pgpcore / sgpgme.c
index 2ba23ba45c1c3fe11f4948ee3769298e29a99f9e..52f6d36ba3cbb15d397d24961f6423331322b467 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 the Claws Mail team
+ * Copyright (C) 1999-2012 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
@@ -19,6 +19,7 @@
  
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
  
 #ifdef USE_GPGME
@@ -211,11 +212,11 @@ gchar *sgpgme_sigstat_info_short(gpgme_ctx_t ctx, gpgme_verify_result_t status)
        switch (gpg_err_code(sig->status)) {
        case GPG_ERR_NO_ERROR:
                switch (gpg_err_code(sig->validity)) {
-               case GPGME_VALIDITY_MARGINAL:
                case GPGME_VALIDITY_FULL:
                case GPGME_VALIDITY_ULTIMATE:
                        result = g_strdup_printf(_("Good signature from %s."), uname);
                        break;
+               case GPGME_VALIDITY_MARGINAL:
                case GPGME_VALIDITY_UNKNOWN:
                case GPGME_VALIDITY_UNDEFINED:
                case GPGME_VALIDITY_NEVER:
@@ -254,9 +255,17 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
        gint i = 0;
        gchar *ret;
        GString *siginfo;
-       gpgme_signature_t sig = status->signatures;
-       
+       gpgme_signature_t sig = NULL;
+
        siginfo = g_string_sized_new(64);
+       if (status == NULL) {
+               g_string_append_printf(siginfo,
+                       _("Error checking signature: no status\n"));
+               goto bail;
+        }
+
+       sig = status->signatures;
+       
        while (sig) {
                gpgme_user_id_t user = NULL;
                gpgme_key_t key;
@@ -292,12 +301,12 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                case GPG_ERR_KEY_EXPIRED:
                        g_string_append_printf(siginfo,
                                _("Good signature from uid \"%s\" (Validity: %s)\n"),
-                               uid, get_validity_str(user->validity));
+                               uid, get_validity_str(user?user->validity:GPGME_VALIDITY_UNKNOWN));
                        break;
                case GPG_ERR_SIG_EXPIRED:
                        g_string_append_printf(siginfo,
                                _("Expired signature from uid \"%s\" (Validity: %s)\n"),
-                               uid, get_validity_str(user->validity));
+                               uid, get_validity_str(user?user->validity:GPGME_VALIDITY_UNKNOWN));
                        break;
                case GPG_ERR_BAD_SIGNATURE:
                        g_string_append_printf(siginfo,
@@ -318,9 +327,22 @@ gchar *sgpgme_sigstat_info_full(gpgme_ctx_t ctx, gpgme_verify_result_t status)
                                j++;
                                user = user->next;
                        }
-                       g_string_append_printf(siginfo,
-                               _("Primary key fingerprint: %s\n"), 
-                               sig ? sig->fpr: "?");
+                       g_string_append(siginfo,
+                               _("Primary key fingerprint:"));
+                       const char* primary_fpr = NULL;
+                       if (key && key->subkeys && key->subkeys->fpr)
+                               primary_fpr = key->subkeys->fpr;
+                       else
+                               g_string_append(siginfo, " ?");
+                       int idx; /* now pretty-print the fingerprint */
+                       for (idx=0; primary_fpr && *primary_fpr!='\0'; idx++, primary_fpr++) {
+                               if (idx%4==0)
+                                       g_string_append_c(siginfo, ' ');
+                               if (idx%20==0)
+                                       g_string_append_c(siginfo, ' ');
+                               g_string_append_c(siginfo, (gchar)*primary_fpr);
+                       }
+                       g_string_append_c(siginfo, '\n');
 #ifdef HAVE_GPGME_PKA_TRUST
                         if (sig->pka_trust == 1 && sig->pka_address) {
                                 g_string_append_printf(siginfo,
@@ -415,7 +437,7 @@ gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *s
                        return NULL;
                }
 
-               err = gpgme_data_rewind(plain);
+               err = cm_gpgme_data_rewind(plain);
                if (err) {
                        debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
                }
@@ -432,7 +454,7 @@ gpgme_data_t sgpgme_decrypt_verify(gpgme_data_t cipher, gpgme_verify_result_t *s
                        return NULL;
                }
 
-               err = gpgme_data_rewind(plain);
+               err = cm_gpgme_data_rewind(plain);
                if (err) {
                        debug_print("can't seek (%d %d %s)\n", err, errno, strerror(errno));
                }
@@ -495,7 +517,7 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
        }
 
        if (config->sign_key != SIGN_KEY_DEFAULT) {
-               gchar *keyid;
+               const gchar *keyid;
                gpgme_key_t key, key2;
                gpgme_error_t err;
 
@@ -511,14 +533,22 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
                        do {
                                err = gpgme_op_keylist_next(ctx, &key);
                                if (!err && key && key->protocol == gpgme_get_protocol(ctx) &&
-                                   !key->expired && !key->revoked)
+                                   !key->expired && !key->revoked && !key->disabled)
                                        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");
+                               if (!err && key && (key->expired || key->revoked || key->disabled)) {
+                                       
+                                       debug_print("skipping a key");
+                                       if (key->expired) 
+                                               debug_print(" expired");
+                                       if (key->revoked) 
+                                               debug_print(" revoked");
+                                       if (key->disabled) 
+                                               debug_print(" disabled");
+                                       debug_print("\n");
                                        gpgme_key_release(key);
                                }
                        } while (!err);
@@ -532,14 +562,21 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
                do {
                        err = gpgme_op_keylist_next(ctx, &key2);
                        if (!err && key2 && key2->protocol == gpgme_get_protocol(ctx) &&
-                           !key2->expired && !key2->revoked)
+                           !key2->expired && !key2->revoked && !key2->disabled)
                                break;
-                       if (!err && key && key2->protocol != gpgme_get_protocol(ctx)) {
+                       if (!err && key2 && 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");
+                       if (!err && key2 && (key2->expired || key2->revoked || key2->disabled)) {
+                                       debug_print("skipping a key");
+                                       if (key2->expired) 
+                                               debug_print(" expired");
+                                       if (key2->revoked) 
+                                               debug_print(" revoked");
+                                       if (key2->disabled) 
+                                               debug_print(" disabled");
+                                       debug_print("\n");
                                gpgme_key_release(key2);
                        }
                } while (!err);
@@ -832,15 +869,22 @@ gboolean sgpgme_has_secret_key(void)
                debug_print("err : %s\n", gpgme_strerror(err));
                return TRUE;
        }
+check_again:
        err = gpgme_op_keylist_start(ctx, NULL, TRUE);
        if (!err)
                err = gpgme_op_keylist_next(ctx, &key);
        gpgme_op_keylist_end(ctx);
-       gpgme_release(ctx);
-       if (gpg_err_code(err) == GPG_ERR_EOF)
+       if (gpg_err_code(err) == GPG_ERR_EOF) {
+               if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {
+                       gpgme_set_protocol(ctx, GPGME_PROTOCOL_CMS);
+                       goto check_again;
+               }
+               gpgme_release(ctx);
                return FALSE;
-       else
+       } else {
+               gpgme_release(ctx);
                return TRUE;
+       }
 }
 
 void sgpgme_check_create_key(void)
@@ -867,7 +911,7 @@ void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
                return NULL;
 
        /* I know it's deprecated, but we don't compile with _LARGEFILE */
-       gpgme_data_rewind(data);
+       cm_gpgme_data_rewind(data);
        while ((r = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
                result = realloc(result, r + w);
                memcpy(result+w, buf, r);
@@ -884,4 +928,17 @@ void *sgpgme_data_release_and_get_mem(gpgme_data_t data, size_t *len)
        }
        return result;
 }
+
+gpgme_error_t cm_gpgme_data_rewind(gpgme_data_t dh)
+{
+#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
+       if (gpgme_data_seek(dh, (off_t)0, SEEK_SET) == -1)
+               return gpg_error_from_errno(errno);
+       else
+               return 0;
+#else
+       return gpgme_data_rewind(dh);
+#endif
+}
+
 #endif /* USE_GPGME */