[PATCH v4 04/16] Provide _notmuch_crypto_{set,get}_gpg_path
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>
Fri, 8 Jul 2016 09:27:15 +0000 (11:27 +0200)
committerW. Trevor King <wking@tremily.us>
Sat, 20 Aug 2016 23:22:11 +0000 (16:22 -0700)
db/848d19aee8f947494f4b3f431ec42f75315dbd [new file with mode: 0644]

diff --git a/db/848d19aee8f947494f4b3f431ec42f75315dbd b/db/848d19aee8f947494f4b3f431ec42f75315dbd
new file mode 100644 (file)
index 0000000..d79c290
--- /dev/null
@@ -0,0 +1,255 @@
+Return-Path: <dkg@fifthhorseman.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 4D4AE6DE09E2\r
+ for <notmuch@notmuchmail.org>; Fri,  8 Jul 2016 03:14:03 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.045\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.045 tagged_above=-999 required=5 tests=[AWL=0.045]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id dGcy_W3zhuYE for <notmuch@notmuchmail.org>;\r
+ Fri,  8 Jul 2016 03:13:55 -0700 (PDT)\r
+Received: from che.mayfirst.org (che.mayfirst.org [162.247.75.118])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 8A35F6DE0362\r
+ for <notmuch@notmuchmail.org>; Fri,  8 Jul 2016 03:13:12 -0700 (PDT)\r
+Received: from fifthhorseman.net (unknown [88.128.80.54])\r
+ by che.mayfirst.org (Postfix) with ESMTPSA id C572CF99C\r
+ for <notmuch@notmuchmail.org>; Fri,  8 Jul 2016 06:13:11 -0400 (EDT)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id 9174A20FAE; Fri,  8 Jul 2016 11:27:34 +0200 (CEST)\r
+From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH v4 04/16] Provide _notmuch_crypto_{set,get}_gpg_path\r
+Date: Fri,  8 Jul 2016 11:27:15 +0200\r
+Message-Id: <1467970047-8013-5-git-send-email-dkg@fifthhorseman.net>\r
+X-Mailer: git-send-email 2.8.1\r
+In-Reply-To: <1467970047-8013-1-git-send-email-dkg@fifthhorseman.net>\r
+References: <1467970047-8013-1-git-send-email-dkg@fifthhorseman.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 08 Jul 2016 10:14:03 -0000\r
+\r
+Use functions to access the gpg_path for a _notmuch_crypto_t object.\r
+This lets us return sensible defaults based on the state of the user's\r
+machine.\r
+\r
+If the passed-in _notmuch_crypto_t is NULL, then just return the\r
+system's default choice of gpg.\r
+---\r
+ notmuch-reply.c | 13 ++++++++++---\r
+ notmuch-show.c  | 12 ++++++++++--\r
+ util/crypto.c   | 50 +++++++++++++++++++++++++++++++++++++++++++++++---\r
+ util/crypto.h   |  8 +++++++-\r
+ 4 files changed, 74 insertions(+), 9 deletions(-)\r
+\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index 42aef47..d0b4a0d 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -790,13 +790,15 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+       .crypto = {\r
+           .verify = FALSE,\r
+           .decrypt = FALSE,\r
+-          .gpgpath = NULL\r
++          .gpg_path = NULL\r
+       }\r
+     };\r
+     int format = FORMAT_DEFAULT;\r
+     int reply_all = TRUE;\r
+     struct sprinter *sp = NULL;\r
+-\r
++    notmuch_status_t status;\r
++    const char *gpg_path = NULL;\r
++    \r
+     notmuch_opt_desc_t options[] = {\r
+       { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',\r
+         (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },\r
+@@ -845,7 +847,12 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+       return EXIT_FAILURE;\r
+     }\r
\r
+-    params.crypto.gpgpath = notmuch_config_get_crypto_gpg_path (config);\r
++    gpg_path = notmuch_config_get_crypto_gpg_path (config);\r
++    status = _notmuch_crypto_set_gpg_path (&(params.crypto), gpg_path);\r
++    if (status != NOTMUCH_STATUS_SUCCESS) {\r
++      fprintf (stderr, "Error: could not set gpg_path to '%s'.\n", gpg_path);\r
++      return EXIT_FAILURE;\r
++    }\r
\r
+     if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+                              NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index 8ebf4ff..60411d0 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -1006,13 +1006,15 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])\r
+       .crypto = {\r
+           .verify = FALSE,\r
+           .decrypt = FALSE,\r
+-          .gpgpath = NULL\r
++          .gpg_path = NULL\r
+       },\r
+       .include_html = FALSE\r
+     };\r
+     int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;\r
+     int exclude = EXCLUDE_TRUE;\r
+     int entire_thread = ENTIRE_THREAD_DEFAULT;\r
++    notmuch_status_t status;\r
++    const char *gpg_path = NULL;\r
\r
+     notmuch_opt_desc_t options[] = {\r
+       { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',\r
+@@ -1130,7 +1132,13 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])\r
+       return EXIT_FAILURE;\r
+     }\r
\r
+-    params.crypto.gpgpath = notmuch_config_get_crypto_gpg_path (config);\r
++\r
++    gpg_path = notmuch_config_get_crypto_gpg_path (config);\r
++    status = _notmuch_crypto_set_gpg_path (&(params.crypto), gpg_path);\r
++    if (status != NOTMUCH_STATUS_SUCCESS) {\r
++      fprintf (stderr, "Error: could not set gpg_path to '%s'.\n", gpg_path);\r
++      return EXIT_FAILURE;\r
++    }\r
\r
+     if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+                              NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
+diff --git a/util/crypto.c b/util/crypto.c\r
+index cce5cbc..9766c2c 100644\r
+--- a/util/crypto.c\r
++++ b/util/crypto.c\r
+@@ -21,7 +21,9 @@\r
\r
+ #include "notmuch.h"\r
+ #include "crypto.h"\r
++#include "search-path.h"\r
+ #include <string.h>\r
++#include <talloc.h>\r
\r
+ #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))\r
\r
+@@ -38,7 +40,7 @@ get_gpg_context (_notmuch_crypto_t *crypto, GMimeCryptoContext **ctx)\r
+     }\r
\r
+     /* TODO: GMimePasswordRequestFunc */\r
+-    crypto->gpgctx = g_mime_gpg_context_new (NULL, crypto->gpgpath ? crypto->gpgpath : "gpg");\r
++    crypto->gpgctx = g_mime_gpg_context_new (NULL, _notmuch_crypto_get_gpg_path (crypto));\r
+     if (! crypto->gpgctx) {\r
+       return NOTMUCH_STATUS_FAILED_CRYPTO_CONTEXT_CREATION;\r
+     }\r
+@@ -51,7 +53,7 @@ get_gpg_context (_notmuch_crypto_t *crypto, GMimeCryptoContext **ctx)\r
+ }\r
\r
+ /* Create or pass on a PKCS7 context (GMime 2.6) */\r
+-static notmuch_status_t \r
++static notmuch_status_t\r
+ get_pkcs7_context (_notmuch_crypto_t *crypto, GMimeCryptoContext **ctx)\r
+ {\r
+     if (ctx == NULL || crypto == NULL)\r
+@@ -76,7 +78,7 @@ get_pkcs7_context (_notmuch_crypto_t *crypto, GMimeCryptoContext **ctx)\r
+ }\r
+ static const struct {\r
+     const char *protocol;\r
+-    notmuch_status_t (*get_context) (_notmuch_crypto_t *crypto, GMimeCryptoContext **ctx);\r
++    notmuch_status_t (*get_context)(_notmuch_crypto_t *crypto, GMimeCryptoContext **ctx);\r
+ } protocols[] = {\r
+     {\r
+       .protocol = "application/pgp-signature",\r
+@@ -120,6 +122,45 @@ _notmuch_crypto_get_gmime_ctx_for_protocol (_notmuch_crypto_t *crypto,\r
+     return NOTMUCH_STATUS_UNKNOWN_CRYPTO_PROTOCOL;\r
+ }\r
\r
++const char *\r
++_notmuch_crypto_get_gpg_path (const _notmuch_crypto_t *crypto)\r
++{\r
++    if (crypto && crypto->gpg_path)\r
++      return crypto->gpg_path;\r
++\r
++    if (test_for_executable ("gpg2")) return "gpg2";\r
++    if (test_for_executable ("gpg")) return "gpg";\r
++    return NULL;\r
++}\r
++\r
++notmuch_status_t\r
++_notmuch_crypto_set_gpg_path (_notmuch_crypto_t *crypto, const char *gpg_path)\r
++{\r
++    /* return success if this matches what is already configured */\r
++    if ((! gpg_path && ! crypto->gpg_path) ||\r
++      (gpg_path && crypto->gpg_path && 0 == strcmp (gpg_path, crypto->gpg_path)))\r
++      return NOTMUCH_STATUS_SUCCESS;\r
++\r
++    if (! gpg_path && ! test_for_executable (gpg_path))\r
++      return NOTMUCH_STATUS_FILE_ERROR;\r
++\r
++    /* clear any existing gpgctx, since things are changing */\r
++    if (crypto->gpgctx) {\r
++      g_object_unref (crypto->gpgctx);\r
++      crypto->gpgctx = NULL;\r
++    }\r
++\r
++    if (crypto->gpg_path) {\r
++      talloc_free (crypto->gpg_path);\r
++      crypto->gpg_path = NULL;\r
++    }\r
++\r
++    if (gpg_path)\r
++      crypto->gpg_path = talloc_strdup (NULL, gpg_path);\r
++\r
++    return NOTMUCH_STATUS_SUCCESS;\r
++}\r
++\r
+ void\r
+ _notmuch_crypto_cleanup (_notmuch_crypto_t *crypto)\r
+ {\r
+@@ -132,4 +173,7 @@ _notmuch_crypto_cleanup (_notmuch_crypto_t *crypto)\r
+       g_object_unref (crypto->pkcs7ctx);\r
+       crypto->pkcs7ctx = NULL;\r
+     }\r
++\r
++    talloc_free (crypto->gpg_path);\r
++    crypto->gpg_path = NULL;\r
+ }\r
+diff --git a/util/crypto.h b/util/crypto.h\r
+index 7cb0a39..70fc8ef 100644\r
+--- a/util/crypto.h\r
++++ b/util/crypto.h\r
+@@ -11,7 +11,7 @@ typedef struct _notmuch_crypto {\r
+     GMimeCryptoContext* pkcs7ctx;\r
+     notmuch_bool_t verify;\r
+     notmuch_bool_t decrypt;\r
+-    const char *gpgpath;\r
++    char *gpg_path;\r
+ } _notmuch_crypto_t;\r
\r
\r
+@@ -20,6 +20,12 @@ _notmuch_crypto_get_gmime_ctx_for_protocol (_notmuch_crypto_t *crypto,\r
+                                           const char *protocol,\r
+                                           GMimeCryptoContext **ctx);\r
\r
++notmuch_status_t\r
++_notmuch_crypto_set_gpg_path (_notmuch_crypto_t *crypto, const char *gpg_path);\r
++\r
++const char *\r
++_notmuch_crypto_get_gpg_path (const _notmuch_crypto_t *crypto);\r
++\r
+ void\r
+ _notmuch_crypto_cleanup (_notmuch_crypto_t *crypto);\r
\r
+-- \r
+2.8.1\r
+\r