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 393B76DE1B66
\r
6 for <notmuch@notmuchmail.org>; Tue, 19 Jan 2016 18:53:21 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org
\r
11 X-Spam-Status: No, score=-0.023 tagged_above=-999 required=5
\r
12 tests=[AWL=-0.023] 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 2X5Y4l47KP1u for <notmuch@notmuchmail.org>;
\r
16 Tue, 19 Jan 2016 18:53:18 -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 622956DE1416
\r
19 for <notmuch@notmuchmail.org>; Tue, 19 Jan 2016 18:53:15 -0800 (PST)
\r
20 Received: from fifthhorseman.net (unknown [38.109.115.130])
\r
21 by che.mayfirst.org (Postfix) with ESMTPSA id 307E8F984
\r
22 for <notmuch@notmuchmail.org>; Tue, 19 Jan 2016 21:53:11 -0500 (EST)
\r
23 Received: by fifthhorseman.net (Postfix, from userid 1000)
\r
24 id CD9E02024F; Tue, 19 Jan 2016 18:53:10 -0800 (PST)
\r
25 From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
\r
26 To: Notmuch Mail <notmuch@notmuchmail.org>
\r
27 Subject: [PATCH v2 02/16] Move crypto.c into libutil
\r
28 Date: Tue, 19 Jan 2016 21:52:35 -0500
\r
29 Message-Id: <1453258369-7366-3-git-send-email-dkg@fifthhorseman.net>
\r
30 X-Mailer: git-send-email 2.7.0.rc3
\r
31 In-Reply-To: <1453258369-7366-1-git-send-email-dkg@fifthhorseman.net>
\r
32 References: <1453258369-7366-1-git-send-email-dkg@fifthhorseman.net>
\r
34 Content-Type: text/plain; charset=UTF-8
\r
35 Content-Transfer-Encoding: 8bit
\r
36 X-BeenThere: notmuch@notmuchmail.org
\r
37 X-Mailman-Version: 2.1.20
\r
39 List-Id: "Use and development of the notmuch mail system."
\r
40 <notmuch.notmuchmail.org>
\r
41 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,
\r
42 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
43 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>
\r
44 List-Post: <mailto:notmuch@notmuchmail.org>
\r
45 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
46 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,
\r
47 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
48 X-List-Received-Date: Wed, 20 Jan 2016 02:53:21 -0000
\r
50 This prepares us for using the crypto object in both the library and
\r
53 i've prefixed notmuch_crypto with _ to indicate that while this can be
\r
54 built into the library when needed, it's not something to be exported
\r
57 Makefile.local | 1 -
\r
58 crypto.c | 99 --------------------------------------------------
\r
59 mime-node.c | 12 +++---
\r
60 notmuch-client.h | 20 ++--------
\r
61 notmuch-reply.c | 2 +-
\r
62 notmuch-show.c | 2 +-
\r
63 util/Makefile.local | 2 +-
\r
64 util/crypto.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
65 util/crypto.h | 23 ++++++++++++
\r
66 9 files changed, 138 insertions(+), 126 deletions(-)
\r
67 delete mode 100644 crypto.c
\r
68 create mode 100644 util/crypto.c
\r
69 create mode 100644 util/crypto.h
\r
71 diff --git a/Makefile.local b/Makefile.local
\r
72 index 066ecf2..6206771 100644
\r
73 --- a/Makefile.local
\r
74 +++ b/Makefile.local
\r
75 @@ -293,7 +293,6 @@ notmuch_client_srcs = \
\r
82 notmuch_client_modules = $(notmuch_client_srcs:.c=.o)
\r
83 diff --git a/crypto.c b/crypto.c
\r
84 deleted file mode 100644
\r
85 index feae949..0000000
\r
89 -/* notmuch - Not much of an email program, (just index and search)
\r
91 - * Copyright © 2012 Jameson Rollins
\r
93 - * This program is free software: you can redistribute it and/or modify
\r
94 - * it under the terms of the GNU General Public License as published by
\r
95 - * the Free Software Foundation, either version 3 of the License, or
\r
96 - * (at your option) any later version.
\r
98 - * This program is distributed in the hope that it will be useful,
\r
99 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
100 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
101 - * GNU General Public License for more details.
\r
103 - * You should have received a copy of the GNU General Public License
\r
104 - * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
106 - * Authors: Jameson Rollins <jrollins@finestructure.net>
\r
109 -#include "notmuch-client.h"
\r
111 -/* Create a GPG context (GMime 2.6) */
\r
112 -static notmuch_crypto_context_t *
\r
113 -create_gpg_context (notmuch_crypto_t *crypto)
\r
115 - notmuch_crypto_context_t *gpgctx;
\r
117 - if (crypto->gpgctx)
\r
118 - return crypto->gpgctx;
\r
120 - /* TODO: GMimePasswordRequestFunc */
\r
121 - gpgctx = g_mime_gpg_context_new (NULL, crypto->gpgpath ? crypto->gpgpath : "gpg");
\r
123 - fprintf (stderr, "Failed to construct gpg context.\n");
\r
126 - crypto->gpgctx = gpgctx;
\r
128 - g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) gpgctx, TRUE);
\r
129 - g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE);
\r
134 -static const struct {
\r
135 - const char *protocol;
\r
136 - notmuch_crypto_context_t *(*get_context) (notmuch_crypto_t *crypto);
\r
139 - .protocol = "application/pgp-signature",
\r
140 - .get_context = create_gpg_context,
\r
143 - .protocol = "application/pgp-encrypted",
\r
144 - .get_context = create_gpg_context,
\r
148 -/* for the specified protocol return the context pointer (initializing
\r
150 -notmuch_crypto_context_t *
\r
151 -notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol)
\r
153 - notmuch_crypto_context_t *cryptoctx = NULL;
\r
156 - if (! protocol) {
\r
157 - fprintf (stderr, "Cryptographic protocol is empty.\n");
\r
158 - return cryptoctx;
\r
161 - /* As per RFC 1847 section 2.1: "the [protocol] value token is
\r
162 - * comprised of the type and sub-type tokens of the Content-Type".
\r
163 - * As per RFC 1521 section 2: "Content-Type values, subtypes, and
\r
164 - * parameter names as defined in this document are
\r
165 - * case-insensitive." Thus, we use strcasecmp for the protocol.
\r
167 - for (i = 0; i < ARRAY_SIZE (protocols); i++) {
\r
168 - if (strcasecmp (protocol, protocols[i].protocol) == 0)
\r
169 - return protocols[i].get_context (crypto);
\r
172 - fprintf (stderr, "Unknown or unsupported cryptographic protocol %s.\n",
\r
179 -notmuch_crypto_cleanup (notmuch_crypto_t *crypto)
\r
181 - if (crypto->gpgctx) {
\r
182 - g_object_unref (crypto->gpgctx);
\r
183 - crypto->gpgctx = NULL;
\r
188 diff --git a/mime-node.c b/mime-node.c
\r
189 index e96e663..a8f5670 100644
\r
192 @@ -33,7 +33,7 @@ typedef struct mime_node_context {
\r
193 GMimeMessage *mime_message;
\r
195 /* Context provided by the caller. */
\r
196 - notmuch_crypto_t *crypto;
\r
197 + _notmuch_crypto_t *crypto;
\r
198 } mime_node_context_t;
\r
201 @@ -56,7 +56,7 @@ _mime_node_context_free (mime_node_context_t *res)
\r
204 mime_node_open (const void *ctx, notmuch_message_t *message,
\r
205 - notmuch_crypto_t *crypto, mime_node_t **root_out)
\r
206 + _notmuch_crypto_t *crypto, mime_node_t **root_out)
\r
208 const char *filename = notmuch_message_get_filename (message);
\r
209 mime_node_context_t *mctx;
\r
210 @@ -151,7 +151,7 @@ set_signature_list_destructor (mime_node_t *node)
\r
211 /* Verify a signed mime node (GMime 2.6) */
\r
213 node_verify (mime_node_t *node, GMimeObject *part,
\r
214 - notmuch_crypto_context_t *cryptoctx)
\r
215 + GMimeCryptoContext *cryptoctx)
\r
217 GError *err = NULL;
\r
219 @@ -172,7 +172,7 @@ node_verify (mime_node_t *node, GMimeObject *part,
\r
220 /* Decrypt and optionally verify an encrypted mime node (GMime 2.6) */
\r
222 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
\r
223 - notmuch_crypto_context_t *cryptoctx)
\r
224 + GMimeCryptoContext *cryptoctx)
\r
226 GError *err = NULL;
\r
227 GMimeDecryptResult *decrypt_result = NULL;
\r
228 @@ -207,7 +207,7 @@ static mime_node_t *
\r
229 _mime_node_create (mime_node_t *parent, GMimeObject *part)
\r
231 mime_node_t *node = talloc_zero (parent, mime_node_t);
\r
232 - notmuch_crypto_context_t *cryptoctx = NULL;
\r
233 + GMimeCryptoContext *cryptoctx = NULL;
\r
235 /* Set basic node properties */
\r
237 @@ -244,7 +244,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
\r
238 || (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) {
\r
239 GMimeContentType *content_type = g_mime_object_get_content_type (part);
\r
240 const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol");
\r
241 - cryptoctx = notmuch_crypto_get_context (node->ctx->crypto, protocol);
\r
242 + cryptoctx = _notmuch_crypto_get_gmime_context (node->ctx->crypto, protocol);
\r
245 /* Handle PGP/MIME parts */
\r
246 diff --git a/notmuch-client.h b/notmuch-client.h
\r
247 index 3bd2903..6157cd9 100644
\r
248 --- a/notmuch-client.h
\r
249 +++ b/notmuch-client.h
\r
252 #include <gmime/gmime.h>
\r
254 -typedef GMimeCryptoContext notmuch_crypto_context_t;
\r
256 #include "notmuch.h"
\r
258 /* This is separate from notmuch-private.h because we're trying to
\r
259 @@ -51,6 +49,7 @@ typedef GMimeCryptoContext notmuch_crypto_context_t;
\r
262 #include "talloc-extra.h"
\r
263 +#include "crypto.h"
\r
265 #define unused(x) x __attribute__ ((unused))
\r
267 @@ -68,20 +67,13 @@ typedef struct notmuch_show_format {
\r
268 const struct notmuch_show_params *params);
\r
269 } notmuch_show_format_t;
\r
271 -typedef struct notmuch_crypto {
\r
272 - notmuch_crypto_context_t* gpgctx;
\r
273 - notmuch_bool_t verify;
\r
274 - notmuch_bool_t decrypt;
\r
275 - const char *gpgpath;
\r
276 -} notmuch_crypto_t;
\r
278 typedef struct notmuch_show_params {
\r
279 notmuch_bool_t entire_thread;
\r
280 notmuch_bool_t omit_excluded;
\r
281 notmuch_bool_t output_body;
\r
282 notmuch_bool_t raw;
\r
284 - notmuch_crypto_t crypto;
\r
285 + _notmuch_crypto_t crypto;
\r
286 notmuch_bool_t include_html;
\r
287 } notmuch_show_params_t;
\r
289 @@ -164,12 +156,6 @@ typedef struct _notmuch_config notmuch_config_t;
\r
291 notmuch_exit_if_unsupported_format (void);
\r
293 -notmuch_crypto_context_t *
\r
294 -notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol);
\r
297 -notmuch_crypto_cleanup (notmuch_crypto_t *crypto);
\r
300 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]);
\r
302 @@ -420,7 +406,7 @@ struct mime_node {
\r
305 mime_node_open (const void *ctx, notmuch_message_t *message,
\r
306 - notmuch_crypto_t *crypto, mime_node_t **node_out);
\r
307 + _notmuch_crypto_t *crypto, mime_node_t **node_out);
\r
309 /* Return a new MIME node for the requested child part of parent.
\r
310 * parent will be used as the talloc context for the returned child
\r
311 diff --git a/notmuch-reply.c b/notmuch-reply.c
\r
312 index 6df54fc..eccfb32 100644
\r
313 --- a/notmuch-reply.c
\r
314 +++ b/notmuch-reply.c
\r
315 @@ -862,7 +862,7 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
\r
316 if (reply_format_func (config, config, query, ¶ms, reply_all, sp) != 0)
\r
317 return EXIT_FAILURE;
\r
319 - notmuch_crypto_cleanup (¶ms.crypto);
\r
320 + _notmuch_crypto_cleanup (¶ms.crypto);
\r
321 notmuch_query_destroy (query);
\r
322 notmuch_database_destroy (notmuch);
\r
324 diff --git a/notmuch-show.c b/notmuch-show.c
\r
325 index 87e52bb..3c91ece 100644
\r
326 --- a/notmuch-show.c
\r
327 +++ b/notmuch-show.c
\r
328 @@ -1171,7 +1171,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
\r
329 ret = do_show (config, query, format, sprinter, ¶ms);
\r
332 - notmuch_crypto_cleanup (¶ms.crypto);
\r
333 + _notmuch_crypto_cleanup (¶ms.crypto);
\r
334 notmuch_query_destroy (query);
\r
335 notmuch_database_destroy (notmuch);
\r
337 diff --git a/util/Makefile.local b/util/Makefile.local
\r
338 index 8b2b91b..7590618 100644
\r
339 --- a/util/Makefile.local
\r
340 +++ b/util/Makefile.local
\r
341 @@ -5,7 +5,7 @@ extra_cflags += -I$(srcdir)/$(dir)
\r
343 libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \
\r
344 $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \
\r
345 - $(dir)/util.c $(dir)/search-path.c
\r
346 + $(dir)/util.c $(dir)/search-path.c $(dir)/crypto.c
\r
348 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
350 diff --git a/util/crypto.c b/util/crypto.c
\r
351 new file mode 100644
\r
352 index 0000000..1712347
\r
354 +++ b/util/crypto.c
\r
356 +/* notmuch - Not much of an email program, (just index and search)
\r
358 + * Copyright © 2012 Jameson Rollins
\r
360 + * This program is free software: you can redistribute it and/or modify
\r
361 + * it under the terms of the GNU General Public License as published by
\r
362 + * the Free Software Foundation, either version 3 of the License, or
\r
363 + * (at your option) any later version.
\r
365 + * This program is distributed in the hope that it will be useful,
\r
366 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
367 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
368 + * GNU General Public License for more details.
\r
370 + * You should have received a copy of the GNU General Public License
\r
371 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
373 + * Authors: Jameson Rollins <jrollins@finestructure.net>
\r
374 + * Daniel Kahn Gillmor <dkg@fifthhorseman.net>
\r
377 +#include "notmuch.h"
\r
378 +#include "crypto.h"
\r
379 +#include <string.h>
\r
381 +#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
\r
383 +/* Create a GPG context (GMime 2.6) */
\r
384 +static GMimeCryptoContext*
\r
385 +create_gpg_context (_notmuch_crypto_t *crypto)
\r
387 + GMimeCryptoContext *gpgctx;
\r
389 + if (crypto->gpgctx) {
\r
390 + return crypto->gpgctx;
\r
393 + /* TODO: GMimePasswordRequestFunc */
\r
394 + gpgctx = g_mime_gpg_context_new (NULL, crypto->gpgpath ? crypto->gpgpath : "gpg");
\r
396 + fprintf (stderr, "Failed to construct gpg context.\n");
\r
399 + crypto->gpgctx = gpgctx;
\r
401 + g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) gpgctx, TRUE);
\r
402 + g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE);
\r
404 + return crypto->gpgctx;
\r
407 +static const struct {
\r
408 + const char *protocol;
\r
409 + GMimeCryptoContext *(*get_context) (_notmuch_crypto_t *crypto);
\r
412 + .protocol = "application/pgp-signature",
\r
413 + .get_context = create_gpg_context,
\r
416 + .protocol = "application/pgp-encrypted",
\r
417 + .get_context = create_gpg_context,
\r
421 +/* for the specified protocol return the context pointer (initializing
\r
423 +GMimeCryptoContext *
\r
424 +_notmuch_crypto_get_gmime_context (_notmuch_crypto_t *crypto, const char *protocol)
\r
426 + GMimeCryptoContext *cryptoctx = NULL;
\r
429 + if (! protocol) {
\r
430 + fprintf (stderr, "Cryptographic protocol is empty.\n");
\r
431 + return cryptoctx;
\r
434 + /* As per RFC 1847 section 2.1: "the [protocol] value token is
\r
435 + * comprised of the type and sub-type tokens of the Content-Type".
\r
436 + * As per RFC 1521 section 2: "Content-Type values, subtypes, and
\r
437 + * parameter names as defined in this document are
\r
438 + * case-insensitive." Thus, we use strcasecmp for the protocol.
\r
440 + for (i = 0; i < ARRAY_SIZE (protocols); i++) {
\r
441 + if (strcasecmp (protocol, protocols[i].protocol) == 0)
\r
442 + return protocols[i].get_context (crypto);
\r
445 + fprintf (stderr, "Unknown or unsupported cryptographic protocol %s.\n",
\r
452 +_notmuch_crypto_cleanup (_notmuch_crypto_t *crypto)
\r
454 + if (crypto->gpgctx) {
\r
455 + g_object_unref (crypto->gpgctx);
\r
456 + crypto->gpgctx = NULL;
\r
459 diff --git a/util/crypto.h b/util/crypto.h
\r
460 new file mode 100644
\r
461 index 0000000..0e65472
\r
463 +++ b/util/crypto.h
\r
468 +#include "notmuch.h"
\r
469 +#include <gmime/gmime.h>
\r
471 +typedef struct _notmuch_crypto {
\r
472 + GMimeCryptoContext* gpgctx;
\r
473 + notmuch_bool_t verify;
\r
474 + notmuch_bool_t decrypt;
\r
475 + const char *gpgpath;
\r
476 +} _notmuch_crypto_t;
\r
479 +GMimeCryptoContext *
\r
480 +_notmuch_crypto_get_gmime_context (_notmuch_crypto_t *crypto, const char *protocol);
\r
483 +_notmuch_crypto_cleanup (_notmuch_crypto_t *crypto);
\r