]> git.notmuchmail.org Git - notmuch/blob - mime-node.c
cli: new crypto verify flag to handle verification
[notmuch] / mime-node.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see http://www.gnu.org/licenses/ .
18  *
19  * Authors: Carl Worth <cworth@cworth.org>
20  *          Keith Packard <keithp@keithp.com>
21  *          Austin Clements <aclements@csail.mit.edu>
22  */
23
24 #include "notmuch-client.h"
25
26 /* Context that gets inherited from the root node. */
27 typedef struct mime_node_context {
28     /* Per-message resources.  These are allocated internally and must
29      * be destroyed. */
30     FILE *file;
31     GMimeStream *stream;
32     GMimeParser *parser;
33     GMimeMessage *mime_message;
34
35     /* Context provided by the caller. */
36     notmuch_crypto_t *crypto;
37 } mime_node_context_t;
38
39 static int
40 _mime_node_context_free (mime_node_context_t *res)
41 {
42     if (res->mime_message)
43         g_object_unref (res->mime_message);
44
45     if (res->parser)
46         g_object_unref (res->parser);
47
48     if (res->stream)
49         g_object_unref (res->stream);
50
51     if (res->file)
52         fclose (res->file);
53
54     return 0;
55 }
56
57 notmuch_status_t
58 mime_node_open (const void *ctx, notmuch_message_t *message,
59                 notmuch_crypto_t *crypto, mime_node_t **root_out)
60 {
61     const char *filename = notmuch_message_get_filename (message);
62     mime_node_context_t *mctx;
63     mime_node_t *root;
64     notmuch_status_t status;
65
66     root = talloc_zero (ctx, mime_node_t);
67     if (root == NULL) {
68         fprintf (stderr, "Out of memory.\n");
69         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
70         goto DONE;
71     }
72
73     /* Create the tree-wide context */
74     mctx = talloc_zero (root, mime_node_context_t);
75     if (mctx == NULL) {
76         fprintf (stderr, "Out of memory.\n");
77         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
78         goto DONE;
79     }
80     talloc_set_destructor (mctx, _mime_node_context_free);
81
82     mctx->file = fopen (filename, "r");
83     if (! mctx->file) {
84         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
85         status = NOTMUCH_STATUS_FILE_ERROR;
86         goto DONE;
87     }
88
89     mctx->stream = g_mime_stream_file_new (mctx->file);
90     if (!mctx->stream) {
91         fprintf (stderr, "Out of memory.\n");
92         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
93         goto DONE;
94     }
95     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (mctx->stream), FALSE);
96
97     mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
98     if (!mctx->parser) {
99         fprintf (stderr, "Out of memory.\n");
100         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
101         goto DONE;
102     }
103
104     mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
105     if (!mctx->mime_message) {
106         fprintf (stderr, "Failed to parse %s\n", filename);
107         status = NOTMUCH_STATUS_FILE_ERROR;
108         goto DONE;
109     }
110
111     mctx->crypto = crypto;
112
113     /* Create the root node */
114     root->part = GMIME_OBJECT (mctx->mime_message);
115     root->envelope_file = message;
116     root->nchildren = 1;
117     root->ctx = mctx;
118
119     root->parent = NULL;
120     root->part_num = 0;
121     root->next_child = 0;
122     root->next_part_num = 1;
123
124     *root_out = root;
125     return NOTMUCH_STATUS_SUCCESS;
126
127 DONE:
128     talloc_free (root);
129     return status;
130 }
131
132 #ifdef GMIME_ATLEAST_26
133 static int
134 _signature_list_free (GMimeSignatureList **proxy)
135 {
136     g_object_unref (*proxy);
137     return 0;
138 }
139 #else
140 static int
141 _signature_validity_free (GMimeSignatureValidity **proxy)
142 {
143     g_mime_signature_validity_free (*proxy);
144     return 0;
145 }
146 #endif
147
148 static mime_node_t *
149 _mime_node_create (mime_node_t *parent, GMimeObject *part)
150 {
151     mime_node_t *node = talloc_zero (parent, mime_node_t);
152     GError *err = NULL;
153
154     /* Set basic node properties */
155     node->part = part;
156     node->ctx = parent->ctx;
157     if (!talloc_reference (node, node->ctx)) {
158         fprintf (stderr, "Out of memory.\n");
159         talloc_free (node);
160         return NULL;
161     }
162     node->parent = parent;
163     node->part_num = node->next_part_num = -1;
164     node->next_child = 0;
165
166     /* Deal with the different types of parts */
167     if (GMIME_IS_PART (part)) {
168         node->nchildren = 0;
169     } else if (GMIME_IS_MULTIPART (part)) {
170         node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
171     } else if (GMIME_IS_MESSAGE_PART (part)) {
172         /* Promote part to an envelope and open it */
173         GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
174         GMimeMessage *message = g_mime_message_part_get_message (message_part);
175         node->envelope_part = message_part;
176         node->part = GMIME_OBJECT (message);
177         node->nchildren = 1;
178     } else {
179         fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
180                  g_type_name (G_OBJECT_TYPE (part)));
181         talloc_free (node);
182         return NULL;
183     }
184
185     /* Handle PGP/MIME parts */
186     if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt) {
187         if (node->nchildren != 2) {
188             /* this violates RFC 3156 section 4, so we won't bother with it. */
189             fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
190                      "message (must be exactly 2)\n",
191                      node->nchildren);
192         } else {
193             GMimeMultipartEncrypted *encrypteddata =
194                 GMIME_MULTIPART_ENCRYPTED (part);
195             node->decrypt_attempted = TRUE;
196 #ifdef GMIME_ATLEAST_26
197             GMimeDecryptResult *decrypt_result = NULL;
198             node->decrypted_child = g_mime_multipart_encrypted_decrypt
199                 (encrypteddata, node->ctx->crypto->gpgctx, &decrypt_result, &err);
200 #else
201             node->decrypted_child = g_mime_multipart_encrypted_decrypt
202                 (encrypteddata, node->ctx->crypto->gpgctx, &err);
203 #endif
204             if (node->decrypted_child) {
205                 node->decrypt_success = node->verify_attempted = TRUE;
206 #ifdef GMIME_ATLEAST_26
207                 /* This may be NULL if the part is not signed. */
208                 node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
209                 if (node->sig_list)
210                     g_object_ref (node->sig_list);
211                 g_object_unref (decrypt_result);
212 #else
213                 node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
214 #endif
215             } else {
216                 fprintf (stderr, "Failed to decrypt part: %s\n",
217                          (err ? err->message : "no error explanation given"));
218             }
219         }
220     } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify) {
221         if (node->nchildren != 2) {
222             /* this violates RFC 3156 section 5, so we won't bother with it. */
223             fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
224                      "(must be exactly 2)\n",
225                      node->nchildren);
226         } else {
227 #ifdef GMIME_ATLEAST_26
228             node->sig_list = g_mime_multipart_signed_verify
229                 (GMIME_MULTIPART_SIGNED (part), node->ctx->crypto->gpgctx, &err);
230             node->verify_attempted = TRUE;
231
232             if (!node->sig_list)
233                 fprintf (stderr, "Failed to verify signed part: %s\n",
234                          (err ? err->message : "no error explanation given"));
235 #else
236             /* For some reason the GMimeSignatureValidity returned
237              * here is not a const (inconsistent with that
238              * returned by
239              * g_mime_multipart_encrypted_get_signature_validity,
240              * and therefore needs to be properly disposed of.
241              *
242              * In GMime 2.6, they're both non-const, so we'll be able
243              * to clean up this asymmetry. */
244             GMimeSignatureValidity *sig_validity = g_mime_multipart_signed_verify
245                 (GMIME_MULTIPART_SIGNED (part), node->ctx->crypto->gpgctx, &err);
246             node->verify_attempted = TRUE;
247             node->sig_validity = sig_validity;
248             if (sig_validity) {
249                 GMimeSignatureValidity **proxy =
250                     talloc (node, GMimeSignatureValidity *);
251                 *proxy = sig_validity;
252                 talloc_set_destructor (proxy, _signature_validity_free);
253             }
254 #endif
255         }
256     }
257
258 #ifdef GMIME_ATLEAST_26
259     /* sig_list may be created in both above cases, so we need to
260      * cleanly handle it here. */
261     if (node->sig_list) {
262         GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
263         *proxy = node->sig_list;
264         talloc_set_destructor (proxy, _signature_list_free);
265     }
266 #endif
267
268 #ifndef GMIME_ATLEAST_26
269     if (node->verify_attempted && !node->sig_validity)
270         fprintf (stderr, "Failed to verify signed part: %s\n",
271                  (err ? err->message : "no error explanation given"));
272 #endif
273
274     if (err)
275         g_error_free (err);
276
277     return node;
278 }
279
280 mime_node_t *
281 mime_node_child (mime_node_t *parent, int child)
282 {
283     GMimeObject *sub;
284     mime_node_t *node;
285
286     if (!parent || child < 0 || child >= parent->nchildren)
287         return NULL;
288
289     if (GMIME_IS_MULTIPART (parent->part)) {
290         if (child == 1 && parent->decrypted_child)
291             sub = parent->decrypted_child;
292         else
293             sub = g_mime_multipart_get_part
294                 (GMIME_MULTIPART (parent->part), child);
295     } else if (GMIME_IS_MESSAGE (parent->part)) {
296         sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
297     } else {
298         /* This should have been caught by message_part_create */
299         INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
300                         g_type_name (G_OBJECT_TYPE (parent->part)));
301     }
302     node = _mime_node_create (parent, sub);
303
304     if (child == parent->next_child && parent->next_part_num != -1) {
305         /* We're traversing in depth-first order.  Record the child's
306          * depth-first numbering. */
307         node->part_num = parent->next_part_num;
308         node->next_part_num = node->part_num + 1;
309
310         /* Prepare the parent for its next depth-first child. */
311         parent->next_child++;
312         parent->next_part_num = -1;
313
314         if (node->nchildren == 0) {
315             /* We've reached a leaf, so find the parent that has more
316              * children and set it up to number its next child. */
317             mime_node_t *iter = node->parent;
318             while (iter && iter->next_child == iter->nchildren)
319                 iter = iter->parent;
320             if (iter)
321                 iter->next_part_num = node->part_num + 1;
322         }
323     }
324
325     return node;
326 }
327
328 static mime_node_t *
329 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
330 {
331     mime_node_t *ret = NULL;
332     int i;
333
334     if (*n == 0)
335         return node;
336
337     *n -= 1;
338     for (i = 0; i < node->nchildren && !ret; i++) {
339         mime_node_t *child = mime_node_child (node, i);
340         ret = _mime_node_seek_dfs_walk (child, n);
341         if (!ret)
342             talloc_free (child);
343     }
344     return ret;
345 }
346
347 mime_node_t *
348 mime_node_seek_dfs (mime_node_t *node, int n)
349 {
350     if (n < 0)
351         return NULL;
352     return _mime_node_seek_dfs_walk (node, &n);
353 }