[PATCH 2/4] notmuch-reply: respect users.other_name in From
[notmuch-archives.git] / 8e / 8b50579372435c9166c2d83ac3d64d729a4e78
1 Return-Path: <schnouki@schnouki.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 olra.theworths.org (Postfix) with ESMTP id 81553431FC3\r
6         for <notmuch@notmuchmail.org>; Thu, 19 Jan 2012 16:06:40 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.1\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.1 tagged_above=-999 required=5\r
12         tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1]\r
13         autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id 1SlMe6BkZWKA for <notmuch@notmuchmail.org>;\r
17         Thu, 19 Jan 2012 16:06:38 -0800 (PST)\r
18 Received: from ks3536.kimsufi.com (schnouki.net [87.98.217.222])\r
19         by olra.theworths.org (Postfix) with ESMTP id 09321431FB6\r
20         for <notmuch@notmuchmail.org>; Thu, 19 Jan 2012 16:06:38 -0800 (PST)\r
21 Received: from odin.local (nancy.schnouki.net [78.238.0.45])\r
22         by ks3536.kimsufi.com (Postfix) with ESMTPSA id A998C6C000C;\r
23         Fri, 20 Jan 2012 01:06:30 +0100 (CET)\r
24 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=schnouki.net;\r
25         s=key-schnouki; t=1327017990;\r
26         bh=Y01cuVIUk4nks8LZejgpYLa3eFuW0twYq9xCJUigJ7g=;\r
27         h=From:To:Subject:Date:Message-Id:In-Reply-To:References;\r
28         b=D940iM7l73IcOJKmPb5umk2pV0EDzTElcAauBI3XGb7nczXBkpf3kP9oqR6E3IcUK\r
29         xht9L6DTbhGI7jUHlp4kzKjKA8fD/031SLAWzkjT6OQWnhzR4YGC/DyPJVv8VDcEH6\r
30         EFsJNF1OG5ohG8W4ZFY4X87GIVQyUYZHT1gnglfM=\r
31 From: Thomas Jost <schnouki@schnouki.net>\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH v3 2/2] Add compatibility with gmime 2.6\r
34 Date: Fri, 20 Jan 2012 01:06:27 +0100\r
35 Message-Id: <1327017987-3361-3-git-send-email-schnouki@schnouki.net>\r
36 X-Mailer: git-send-email 1.7.8.4\r
37 In-Reply-To: <1327017987-3361-1-git-send-email-schnouki@schnouki.net>\r
38 References: <87ty3r2rqt.fsf@schnouki.net>\r
39         <1327017987-3361-1-git-send-email-schnouki@schnouki.net>\r
40 X-BeenThere: notmuch@notmuchmail.org\r
41 X-Mailman-Version: 2.1.13\r
42 Precedence: list\r
43 List-Id: "Use and development of the notmuch mail system."\r
44         <notmuch.notmuchmail.org>\r
45 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
47 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
48 List-Post: <mailto:notmuch@notmuchmail.org>\r
49 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
50 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
51         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
52 X-List-Received-Date: Fri, 20 Jan 2012 00:06:41 -0000\r
53 \r
54 There are lots of API changes in gmime 2.6 crypto handling. By adding\r
55 preprocessor directives, it is however possible to add gmime 2.6 compatibility\r
56 while preserving compatibility with gmime 2.4 too.\r
57 \r
58 This is mostly based on id:"8762i8hrb9.fsf@bookbinder.fernseed.info".\r
59 \r
60 This was tested against both gmime 2.6.4 and 2.4.31. With gmime 2.4.31, the\r
61 crypto tests all work fine (as expected). With gmime 2.6.4, one crypto test is\r
62 currently broken (signature verification with signer key unavailable), most\r
63 likely because of a bug in gmime which will hopefully be fixed in a future\r
64 version.\r
65 ---\r
66  mime-node.c      |   60 ++++++++++++++++++++++++++++++++--\r
67  notmuch-client.h |   30 ++++++++++++++++-\r
68  notmuch-reply.c  |    7 ++++\r
69  notmuch-show.c   |   95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
70  show-message.c   |    4 ++\r
71  test/crypto      |    2 +\r
72  6 files changed, 192 insertions(+), 6 deletions(-)\r
73 \r
74 diff --git a/mime-node.c b/mime-node.c\r
75 index d26bb44..ad19f5e 100644\r
76 --- a/mime-node.c\r
77 +++ b/mime-node.c\r
78 @@ -33,7 +33,11 @@ typedef struct mime_node_context {\r
79      GMimeMessage *mime_message;\r
80  \r
81      /* Context provided by the caller. */\r
82 +#ifdef GMIME_ATLEAST_26\r
83 +    GMimeCryptoContext *cryptoctx;\r
84 +#else\r
85      GMimeCipherContext *cryptoctx;\r
86 +#endif\r
87      notmuch_bool_t decrypt;\r
88  } mime_node_context_t;\r
89  \r
90 @@ -57,8 +61,12 @@ _mime_node_context_free (mime_node_context_t *res)\r
91  \r
92  notmuch_status_t\r
93  mime_node_open (const void *ctx, notmuch_message_t *message,\r
94 -               GMimeCipherContext *cryptoctx, notmuch_bool_t decrypt,\r
95 -               mime_node_t **root_out)\r
96 +#ifdef GMIME_ATLEAST_26\r
97 +               GMimeCryptoContext *cryptoctx,\r
98 +#else\r
99 +               GMimeCipherContext *cryptoctx,\r
100 +#endif\r
101 +               notmuch_bool_t decrypt, mime_node_t **root_out)\r
102  {\r
103      const char *filename = notmuch_message_get_filename (message);\r
104      mime_node_context_t *mctx;\r
105 @@ -112,12 +120,21 @@ DONE:\r
106      return status;\r
107  }\r
108  \r
109 +#ifdef GMIME_ATLEAST_26\r
110 +static int\r
111 +_signature_list_free (GMimeSignatureList **proxy)\r
112 +{\r
113 +    g_object_unref (*proxy);\r
114 +    return 0;\r
115 +}\r
116 +#else\r
117  static int\r
118  _signature_validity_free (GMimeSignatureValidity **proxy)\r
119  {\r
120      g_mime_signature_validity_free (*proxy);\r
121      return 0;\r
122  }\r
123 +#endif\r
124  \r
125  static mime_node_t *\r
126  _mime_node_create (const mime_node_t *parent, GMimeObject *part)\r
127 @@ -165,11 +182,24 @@ _mime_node_create (const mime_node_t *parent, GMimeObject *part)\r
128             GMimeMultipartEncrypted *encrypteddata =\r
129                 GMIME_MULTIPART_ENCRYPTED (part);\r
130             node->decrypt_attempted = TRUE;\r
131 +#ifdef GMIME_ATLEAST_26\r
132 +           GMimeDecryptResult *decrypt_result = NULL;\r
133 +           node->decrypted_child = g_mime_multipart_encrypted_decrypt\r
134 +               (encrypteddata, node->ctx->cryptoctx, &decrypt_result, &err);\r
135 +#else\r
136             node->decrypted_child = g_mime_multipart_encrypted_decrypt\r
137                 (encrypteddata, node->ctx->cryptoctx, &err);\r
138 +#endif\r
139             if (node->decrypted_child) {\r
140                 node->decrypt_success = node->verify_attempted = TRUE;\r
141 +#ifdef GMIME_ATLEAST_26\r
142 +               /* This may be NULL if the part is not signed. */\r
143 +               node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);\r
144 +               g_object_ref (node->sig_list);\r
145 +               g_object_unref (decrypt_result);\r
146 +#else\r
147                 node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);\r
148 +#endif\r
149             } else {\r
150                 fprintf (stderr, "Failed to decrypt part: %s\n",\r
151                          (err ? err->message : "no error explanation given"));\r
152 @@ -182,6 +212,15 @@ _mime_node_create (const mime_node_t *parent, GMimeObject *part)\r
153                      "(must be exactly 2)\n",\r
154                      node->nchildren);\r
155         } else {\r
156 +#ifdef GMIME_ATLEAST_26\r
157 +           node->sig_list = g_mime_multipart_signed_verify\r
158 +               (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);\r
159 +           node->verify_attempted = TRUE;\r
160 +\r
161 +           if (!node->sig_list)\r
162 +               fprintf (stderr, "Failed to verify signed part: %s\n",\r
163 +                        (err ? err->message : "no error explanation given"));\r
164 +#else\r
165             /* For some reason the GMimeSignatureValidity returned\r
166              * here is not a const (inconsistent with that\r
167              * returned by\r
168 @@ -195,17 +234,30 @@ _mime_node_create (const mime_node_t *parent, GMimeObject *part)\r
169             node->verify_attempted = TRUE;\r
170             node->sig_validity = sig_validity;\r
171             if (sig_validity) {\r
172 -               GMimeSignatureValidity **proxy =\r
173 -                   talloc (node, GMimeSignatureValidity *);\r
174 +               GMimeSignatureValidity **proxy = talloc (node, GMimeSignatureValidity *);\r
175                 *proxy = sig_validity;\r
176                 talloc_set_destructor (proxy, _signature_validity_free);\r
177             }\r
178 +#endif\r
179         }\r
180      }\r
181  \r
182 +#ifdef GMIME_ATLEAST_26\r
183 +    /* sig_list may be created in both above cases, so we need to\r
184 +     * cleanly handle it here. */\r
185 +    if (node->sig_list) {\r
186 +       GMimeSignatureList **proxy =\r
187 +           talloc (node, GMimeSignatureList *);\r
188 +       *proxy = node->sig_list;\r
189 +       talloc_set_destructor (proxy, _signature_list_free);\r
190 +    }\r
191 +#endif\r
192 +\r
193 +#ifndef GMIME_ATLEAST_26\r
194      if (node->verify_attempted && !node->sig_validity)\r
195         fprintf (stderr, "Failed to verify signed part: %s\n",\r
196                  (err ? err->message : "no error explanation given"));\r
197 +#endif\r
198  \r
199      if (err)\r
200         g_error_free (err);\r
201 diff --git a/notmuch-client.h b/notmuch-client.h\r
202 index 62ede28..9c1d383 100644\r
203 --- a/notmuch-client.h\r
204 +++ b/notmuch-client.h\r
205 @@ -30,6 +30,14 @@\r
206  \r
207  #include <gmime/gmime.h>\r
208  \r
209 +/* GMIME_CHECK_VERSION in gmime 2.4 is not usable from the\r
210 + * preprocessor (it calls a runtime function). But since\r
211 + * GMIME_MAJOR_VERSION and friends were added in gmime 2.6, we can use\r
212 + * these to check the version number. */\r
213 +#ifdef GMIME_MAJOR_VERSION\r
214 +#define GMIME_ATLEAST_26\r
215 +#endif\r
216 +\r
217  #include "notmuch.h"\r
218  \r
219  /* This is separate from notmuch-private.h because we're trying to\r
220 @@ -69,7 +77,11 @@ typedef struct notmuch_show_format {\r
221      void (*part_start) (GMimeObject *part,\r
222                         int *part_count);\r
223      void (*part_encstatus) (int status);\r
224 +#ifdef GMIME_ATLEAST_26\r
225 +    void (*part_sigstatus) (GMimeSignatureList* siglist);\r
226 +#else\r
227      void (*part_sigstatus) (const GMimeSignatureValidity* validity);\r
228 +#endif\r
229      void (*part_content) (GMimeObject *part);\r
230      void (*part_end) (GMimeObject *part);\r
231      const char *part_sep;\r
232 @@ -83,7 +95,11 @@ typedef struct notmuch_show_params {\r
233      int entire_thread;\r
234      int raw;\r
235      int part;\r
236 +#ifdef GMIME_ATLEAST_26\r
237 +    GMimeCryptoContext* cryptoctx;\r
238 +#else\r
239      GMimeCipherContext* cryptoctx;\r
240 +#endif\r
241      int decrypt;\r
242  } notmuch_show_params_t;\r
243  \r
244 @@ -290,11 +306,17 @@ typedef struct mime_node {\r
245  \r
246      /* True if signature verification on this part was attempted. */\r
247      notmuch_bool_t verify_attempted;\r
248 +#ifdef GMIME_ATLEAST_26\r
249 +    /* The list of signatures for signed or encrypted containers. If\r
250 +     * there are no signatures, this will be NULL. */\r
251 +    GMimeSignatureList* sig_list;\r
252 +#else\r
253      /* For signed or encrypted containers, the validity of the\r
254       * signature.  May be NULL if signature verification failed.  If\r
255       * there are simply no signatures, this will be non-NULL with an\r
256       * empty signers list. */\r
257      const GMimeSignatureValidity *sig_validity;\r
258 +#endif\r
259  \r
260      /* Internal: Context inherited from the root iterator. */\r
261      struct mime_node_context *ctx;\r
262 @@ -319,8 +341,12 @@ typedef struct mime_node {\r
263   */\r
264  notmuch_status_t\r
265  mime_node_open (const void *ctx, notmuch_message_t *message,\r
266 -               GMimeCipherContext *cryptoctx, notmuch_bool_t decrypt,\r
267 -               mime_node_t **node_out);\r
268 +#ifdef GMIME_ATLEAST_26\r
269 +               GMimeCryptoContext *cryptoctx,\r
270 +#else\r
271 +               GMimeCipherContext *cryptoctx,\r
272 +#endif\r
273 +               notmuch_bool_t decrypt, mime_node_t **node_out);\r
274  \r
275  /* Return a new MIME node for the requested child part of parent.\r
276   * parent will be used as the talloc context for the returned child\r
277 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
278 index 0f682db..bf67960 100644\r
279 --- a/notmuch-reply.c\r
280 +++ b/notmuch-reply.c\r
281 @@ -688,15 +688,22 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
282         reply_format_func = notmuch_reply_format_default;\r
283  \r
284      if (decrypt) {\r
285 +#ifdef GMIME_ATLEAST_26\r
286 +       /* TODO: GMimePasswordRequestFunc */\r
287 +       params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");\r
288 +#else\r
289         GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
290         params.cryptoctx = g_mime_gpg_context_new (session, "gpg");\r
291 +#endif\r
292         if (params.cryptoctx) {\r
293             g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);\r
294             params.decrypt = TRUE;\r
295         } else {\r
296             fprintf (stderr, "Failed to construct gpg context.\n");\r
297         }\r
298 +#ifndef GMIME_ATLEAST_26\r
299         g_object_unref (session);\r
300 +#endif\r
301      }\r
302  \r
303      config = notmuch_config_open (ctx, NULL, NULL);\r
304 diff --git a/notmuch-show.c b/notmuch-show.c\r
305 index 91f566c..190210c 100644\r
306 --- a/notmuch-show.c\r
307 +++ b/notmuch-show.c\r
308 @@ -76,7 +76,11 @@ static void\r
309  format_part_encstatus_json (int status);\r
310  \r
311  static void\r
312 +#ifdef GMIME_ATLEAST_26\r
313 +format_part_sigstatus_json (GMimeSignatureList* siglist);\r
314 +#else\r
315  format_part_sigstatus_json (const GMimeSignatureValidity* validity);\r
316 +#endif\r
317  \r
318  static void\r
319  format_part_content_json (GMimeObject *part);\r
320 @@ -486,6 +490,21 @@ show_text_part_content (GMimeObject *part, GMimeStream *stream_out)\r
321         g_object_unref(stream_filter);\r
322  }\r
323  \r
324 +#ifdef GMIME_ATLEAST_26\r
325 +static const char*\r
326 +signature_status_to_string (GMimeSignatureStatus x)\r
327 +{\r
328 +    switch (x) {\r
329 +    case GMIME_SIGNATURE_STATUS_GOOD:\r
330 +       return "good";\r
331 +    case GMIME_SIGNATURE_STATUS_BAD:\r
332 +       return "bad";\r
333 +    case GMIME_SIGNATURE_STATUS_ERROR:\r
334 +       return "error";\r
335 +    }\r
336 +    return "unknown";\r
337 +}\r
338 +#else\r
339  static const char*\r
340  signer_status_to_string (GMimeSignerStatus x)\r
341  {\r
342 @@ -501,6 +520,7 @@ signer_status_to_string (GMimeSignerStatus x)\r
343      }\r
344      return "unknown";\r
345  }\r
346 +#endif\r
347  \r
348  static void\r
349  format_part_start_text (GMimeObject *part, int *part_count)\r
350 @@ -592,6 +612,73 @@ format_part_encstatus_json (int status)\r
351      printf ("}]");\r
352  }\r
353  \r
354 +#ifdef GMIME_ATLEAST_26\r
355 +static void\r
356 +format_part_sigstatus_json (GMimeSignatureList *siglist)\r
357 +{\r
358 +    printf (", \"sigstatus\": [");\r
359 +\r
360 +    if (!siglist) {\r
361 +       printf ("]");\r
362 +       return;\r
363 +    }\r
364 +\r
365 +    void *ctx_quote = talloc_new (NULL);\r
366 +    int i;\r
367 +    for (i = 0; i < g_mime_signature_list_length (siglist); i++) {\r
368 +       GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);\r
369 +\r
370 +       if (i > 0)\r
371 +           printf (", ");\r
372 +\r
373 +       printf ("{");\r
374 +\r
375 +       /* status */\r
376 +       GMimeSignatureStatus status = g_mime_signature_get_status (signature);\r
377 +       printf ("\"status\": %s",\r
378 +               json_quote_str (ctx_quote,\r
379 +                               signature_status_to_string (status)));\r
380 +\r
381 +       GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);\r
382 +       if (status == GMIME_SIGNATURE_STATUS_GOOD) {\r
383 +           if (certificate)\r
384 +               printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, g_mime_certificate_get_fingerprint (certificate)));\r
385 +           /* these dates are seconds since the epoch; should we\r
386 +            * provide a more human-readable format string? */\r
387 +           time_t created = g_mime_signature_get_created (signature);\r
388 +           if (created != -1)\r
389 +               printf (", \"created\": %d", (int) created);\r
390 +           time_t expires = g_mime_signature_get_expires (signature);\r
391 +           if (expires > 0)\r
392 +               printf (", \"expires\": %d", (int) expires);\r
393 +           /* output user id only if validity is FULL or ULTIMATE. */\r
394 +           /* note that gmime is using the term "trust" here, which\r
395 +            * is WRONG.  It's actually user id "validity". */\r
396 +           if (certificate) {\r
397 +               const char *name = g_mime_certificate_get_name (certificate);\r
398 +               GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);\r
399 +               if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE))\r
400 +                   printf (", \"userid\": %s", json_quote_str (ctx_quote, name));\r
401 +           }\r
402 +       } else if (certificate) {\r
403 +           const char *key_id = g_mime_certificate_get_key_id (certificate);\r
404 +           if (key_id)\r
405 +               printf (", \"keyid\": %s", json_quote_str (ctx_quote, key_id));\r
406 +       }\r
407 +\r
408 +       GMimeSignatureError errors = g_mime_signature_get_errors (signature);\r
409 +       if (errors != GMIME_SIGNATURE_ERROR_NONE) {\r
410 +           printf (", \"errors\": %d", errors);\r
411 +       }\r
412 +\r
413 +       printf ("}");\r
414 +     }\r
415 +\r
416 +    printf ("]");\r
417 +\r
418 +    talloc_free (ctx_quote);\r
419 +}\r
420 +#else\r
421  static void\r
422  format_part_sigstatus_json (const GMimeSignatureValidity* validity)\r
423  {\r
424 @@ -652,6 +739,7 @@ format_part_sigstatus_json (const GMimeSignatureValidity* validity)\r
425  \r
426      talloc_free (ctx_quote);\r
427  }\r
428 +#endif\r
429  \r
430  static void\r
431  format_part_content_json (GMimeObject *part)\r
432 @@ -990,13 +1078,20 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
433         } else if ((STRNCMP_LITERAL (argv[i], "--verify") == 0) ||\r
434                    (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {\r
435             if (params.cryptoctx == NULL) {\r
436 +#ifdef GMIME_ATLEAST_26\r
437 +               /* TODO: GMimePasswordRequestFunc */\r
438 +               if (NULL == (params.cryptoctx = g_mime_gpg_context_new(NULL, "gpg")))\r
439 +#else\r
440                 GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);\r
441                 if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))\r
442 +#endif\r
443                     fprintf (stderr, "Failed to construct gpg context.\n");\r
444                 else\r
445                     g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);\r
446 +#ifndef GMIME_ATLEAST_26\r
447                 g_object_unref (session);\r
448                 session = NULL;\r
449 +#endif\r
450             }\r
451             if (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)\r
452                 params.decrypt = 1;\r
453 diff --git a/show-message.c b/show-message.c\r
454 index 8768889..83ecf81 100644\r
455 --- a/show-message.c\r
456 +++ b/show-message.c\r
457 @@ -48,7 +48,11 @@ show_message_part (mime_node_t *node,\r
458         format->part_encstatus (node->decrypt_success);\r
459  \r
460      if (node->verify_attempted && format->part_sigstatus)\r
461 +#ifdef GMIME_ATLEAST_26\r
462 +       format->part_sigstatus (node->sig_list);\r
463 +#else\r
464         format->part_sigstatus (node->sig_validity);\r
465 +#endif\r
466  \r
467      format->part_content (part);\r
468  \r
469 diff --git a/test/crypto b/test/crypto\r
470 index 0af4aa8..3779abc 100755\r
471 --- a/test/crypto\r
472 +++ b/test/crypto\r
473 @@ -104,6 +104,8 @@ test_expect_equal \\r
474      "$expected"\r
475  \r
476  test_begin_subtest "signature verification with signer key unavailable"\r
477 +# this is broken with current versions of gmime-2.6\r
478 +(ldd $(which notmuch) | grep -q gmime-2.6) && test_subtest_known_broken\r
479  # move the gnupghome temporarily out of the way\r
480  mv "${GNUPGHOME}"{,.bak}\r
481  output=$(notmuch show --format=json --verify subject:"test signed message 001" \\r
482 -- \r
483 1.7.8.4\r
484 \r