Re: encoding of message-ids
[notmuch-archives.git] / 4e / 28e9989209db194a821fb944f145698d7d517e
1 Return-Path: <dkg@fifthhorseman.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5  by arlo.cworth.org (Postfix) with ESMTP id 99EC46DE1601\r
6  for <notmuch@notmuchmail.org>; Wed,  9 Dec 2015 19:40:15 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.033\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.033 tagged_above=-999 required=5\r
12  tests=[AWL=-0.033] autolearn=disabled\r
13 Received: from arlo.cworth.org ([127.0.0.1])\r
14  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
15  with ESMTP id sMav7f67XOXL for <notmuch@notmuchmail.org>;\r
16  Wed,  9 Dec 2015 19:40:13 -0800 (PST)\r
17 Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
18  by arlo.cworth.org (Postfix) with ESMTP id 9D7606DE18EC\r
19  for <notmuch@notmuchmail.org>; Wed,  9 Dec 2015 19:40:06 -0800 (PST)\r
20 Received: from fifthhorseman.net (unknown [38.109.115.130])\r
21  by che.mayfirst.org (Postfix) with ESMTPSA id 5965FF989\r
22  for <notmuch@notmuchmail.org>; Wed,  9 Dec 2015 22:40:04 -0500 (EST)\r
23 Received: by fifthhorseman.net (Postfix, from userid 1000)\r
24  id BC38D21227; Wed,  9 Dec 2015 22:40:03 -0500 (EST)\r
25 From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
26 To: Notmuch Mail <notmuch@notmuchmail.org>\r
27 Subject: [PATCH 7/9] add a gpg_path value for notmuch_database_t\r
28 Date: Wed,  9 Dec 2015 22:39:44 -0500\r
29 Message-Id: <1449718786-28000-8-git-send-email-dkg@fifthhorseman.net>\r
30 X-Mailer: git-send-email 2.6.2\r
31 In-Reply-To: <1449718786-28000-1-git-send-email-dkg@fifthhorseman.net>\r
32 References: <1449718786-28000-1-git-send-email-dkg@fifthhorseman.net>\r
33 X-BeenThere: notmuch@notmuchmail.org\r
34 X-Mailman-Version: 2.1.20\r
35 Precedence: list\r
36 List-Id: "Use and development of the notmuch mail system."\r
37  <notmuch.notmuchmail.org>\r
38 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
39  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
40 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
41 List-Post: <mailto:notmuch@notmuchmail.org>\r
42 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
43 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
44  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
45 X-List-Received-Date: Thu, 10 Dec 2015 03:40:16 -0000\r
46 \r
47 Exposing this to the user of the library lets the user point to\r
48 arbitrary gpg executables when trying to decrypt.\r
49 ---\r
50  lib/database-private.h |  3 ++\r
51  lib/database.cc        | 93 +++++++++++++++++++++++++++++++++++++++++++-------\r
52  lib/notmuch.h          | 31 +++++++++++++++++\r
53  3 files changed, 115 insertions(+), 12 deletions(-)\r
54 \r
55 diff --git a/lib/database-private.h b/lib/database-private.h\r
56 index 1bf76c5..9a35044 100644\r
57 --- a/lib/database-private.h\r
58 +++ b/lib/database-private.h\r
59 @@ -171,6 +171,9 @@ struct _notmuch_database {\r
60       * notmuch_database_new_revision. */\r
61      unsigned long revision;\r
62      const char *uuid;\r
63 +\r
64 +    /* can be NULL, meaning "try to find gpg2 or gpg if possible" */\r
65 +    char *gpg_path;\r
66      GMimeCryptoContext *gpg_crypto_ctx;\r
67  \r
68      Xapian::QueryParser *query_parser;\r
69 diff --git a/lib/database.cc b/lib/database.cc\r
70 index d0e8800..c40ce77 100644\r
71 --- a/lib/database.cc\r
72 +++ b/lib/database.cc\r
73 @@ -996,6 +996,7 @@ notmuch_database_open_verbose (const char *path,\r
74             notmuch, notmuch->xapian_db->get_uuid ().c_str ());\r
75  \r
76         notmuch->gpg_crypto_ctx = NULL;\r
77 +       notmuch->gpg_path = NULL;\r
78         \r
79         notmuch->query_parser = new Xapian::QueryParser;\r
80         notmuch->term_gen = new Xapian::TermGenerator;\r
81 @@ -1096,6 +1097,10 @@ notmuch_database_close (notmuch_database_t *notmuch)\r
82         g_object_unref (notmuch->gpg_crypto_ctx);\r
83         notmuch->gpg_crypto_ctx = NULL;\r
84      }\r
85 +    if (notmuch->gpg_path) {\r
86 +       talloc_free(notmuch->gpg_path);\r
87 +       notmuch->gpg_path = NULL;\r
88 +    }\r
89      \r
90      return status;\r
91  }\r
92 @@ -2393,17 +2398,6 @@ _notmuch_database_link_message (notmuch_database_t *notmuch,\r
93      return status;\r
94  }\r
95  \r
96 -static const char*\r
97 -_notmuch_database_get_gpg_path (notmuch_database_t *notmuch)\r
98 -{\r
99 -#define try_gpg_path(z) if (!access(z, X_OK)) return z\r
100 -    try_gpg_path("/usr/bin/gpg2");\r
101 -    try_gpg_path("/bin/gpg2");\r
102 -    try_gpg_path("/usr/bin/gpg");\r
103 -    try_gpg_path("/bin/gpg");\r
104 -    return NULL;\r
105 -}\r
106 -\r
107  notmuch_private_status_t\r
108  _notmuch_database_get_crypto_for_protocol (notmuch_database_t *notmuch,\r
109                                            const char *protocol,\r
110 @@ -2425,7 +2419,7 @@ _notmuch_database_get_crypto_for_protocol (notmuch_database_t *notmuch,\r
111              * here? how would this config get into the library?  Is\r
112              * this an option we can set on the database object?  Or\r
113              * elsewhere?  */\r
114 -           notmuch->gpg_crypto_ctx = g_mime_gpg_context_new (NULL, _notmuch_database_get_gpg_path(notmuch));\r
115 +           notmuch->gpg_crypto_ctx = g_mime_gpg_context_new (NULL, notmuch_database_get_gpg_path(notmuch));\r
116             if (! notmuch->gpg_crypto_ctx)\r
117                 return NOTMUCH_PRIVATE_STATUS_FAILED_CRYPTO_CONTEXT_CREATION;\r
118  \r
119 @@ -2752,3 +2746,78 @@ notmuch_database_status_string (const notmuch_database_t *notmuch)\r
120  {\r
121      return notmuch->status_string;\r
122  }\r
123 +\r
124 +\r
125 +static notmuch_bool_t\r
126 +_find_in_path(const char* path)\r
127 +{\r
128 +    char *c = NULL, *save = NULL, *tok;\r
129 +    size_t n;\r
130 +    int dfd = -1;\r
131 +    notmuch_bool_t ret = FALSE;\r
132 +    \r
133 +    n = confstr(_CS_PATH, NULL, 0);\r
134 +    c = (char*)talloc_size(NULL, n);\r
135 +    if (!c)\r
136 +       return FALSE;\r
137 +    confstr(_CS_PATH, c, n);\r
138 +\r
139 +    tok = strtok_r(c, ":", &save);\r
140 +    while (tok) {\r
141 +       dfd = open(tok, O_DIRECTORY | O_RDONLY);\r
142 +       if (dfd != -1) {\r
143 +           if (!faccessat(dfd, path, X_OK, 0)) {\r
144 +               ret = TRUE;\r
145 +               goto done;\r
146 +           }\r
147 +           close(dfd);\r
148 +       }\r
149 +       tok = strtok_r(NULL, ":", &save);\r
150 +    }\r
151 +done:\r
152 +    if (dfd != -1)\r
153 +       close(dfd);\r
154 +    if (c)\r
155 +       talloc_free(c);\r
156 +    return ret;\r
157 +}\r
158 +\r
159 +notmuch_status_t\r
160 +notmuch_database_set_gpg_path (notmuch_database_t *notmuch, const char* path)\r
161 +{\r
162 +    /* return success if this matches what is already configured */\r
163 +    if ((!path && !notmuch->gpg_path) ||\r
164 +       (path && notmuch->gpg_path && 0 == strcmp(path, notmuch->gpg_path)))\r
165 +       return NOTMUCH_STATUS_SUCCESS;\r
166 +    \r
167 +    if (!path && !_find_in_path(path))\r
168 +       return NOTMUCH_STATUS_FILE_ERROR;\r
169 +\r
170 +    /* clear any existing gpg_crypto_ctx, since things are changing */\r
171 +    if (notmuch->gpg_crypto_ctx) {\r
172 +       g_object_unref (notmuch->gpg_crypto_ctx);\r
173 +       notmuch->gpg_crypto_ctx = NULL;\r
174 +    }\r
175 +\r
176 +    if (notmuch->gpg_path) {\r
177 +       talloc_free(notmuch->gpg_path);\r
178 +       notmuch->gpg_path = NULL;\r
179 +    }\r
180 +\r
181 +    if (path)\r
182 +       notmuch->gpg_path = talloc_strdup (notmuch, path);\r
183 +    \r
184 +    return NOTMUCH_STATUS_SUCCESS;\r
185 +}\r
186 +\r
187 +const char*\r
188 +notmuch_database_get_gpg_path (const notmuch_database_t *notmuch)\r
189 +{\r
190 +    if (notmuch->gpg_path)\r
191 +       return notmuch->gpg_path;\r
192 +\r
193 +#define try_gpg_path(z) if (_find_in_path(z)) return z\r
194 +    try_gpg_path("gpg2");\r
195 +    try_gpg_path("gpg");\r
196 +    return NULL;\r
197 +}\r
198 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
199 index 809a2ea..e9cfed3 100644\r
200 --- a/lib/notmuch.h\r
201 +++ b/lib/notmuch.h\r
202 @@ -591,6 +591,37 @@ notmuch_database_add_message_try_decrypt (notmuch_database_t *database,\r
203                                           notmuch_message_t **message);\r
204  \r
205  /**\r
206 + * Tell the notmuch database where to find GnuPG.\r
207 + *\r
208 + * This is only useful when notmuch might try to use GnuPG to decrypt\r
209 + * MIME parts (see for example\r
210 + * notmuch_database_add_message_try_decrypt).  The argument needs to\r
211 + * be an executable version of gpg.\r
212 + * \r
213 + * If this function has never been invoked, notmuch will try to find\r
214 + * gpg in reasonable places.\r
215 + *\r
216 + * This value is not currently stored in the database on disk, it is\r
217 + * only used for this notmuch_database_t while it exists.\r
218 + *\r
219 + * Return value:\r
220 + *\r
221 + * NOTMUCH_STATUS_SUCCESS: the path was accepted and will be used.\r
222 + * \r
223 + * NOTMUCH_STATUS_FILE_ERROR: the path given either wasn't found or\r
224 + *      wasn't executable.\r
225 + */\r
226 +notmuch_status_t\r
227 +notmuch_database_set_gpg_path (notmuch_database_t *database, const char* path);\r
228 +                              \r
229 +/**\r
230 + * Find out where the notmuch database will try to find gpg if it\r
231 + * needs to use it.\r
232 + */\r
233 +const char*\r
234 +notmuch_database_get_gpg_path (const notmuch_database_t *database);\r
235 +\r
236 +/**\r
237   * Remove a message filename from the given notmuch database. If the\r
238   * message has no more filenames, remove the message.\r
239   *\r
240 -- \r
241 2.6.2\r
242 \r