]> git.notmuchmail.org Git - notmuch/blob - mime-node.c
test: session keys are known broken without session key support
[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 https://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     /* Fast path */
83     mctx->file = fopen (filename, "r");
84     if (! mctx->file) {
85         /* Slow path - for some reason the first file in the list is
86          * not available anymore. This is clearly a problem in the
87          * database, but we are not going to let this problem be a
88          * show stopper */
89         notmuch_filenames_t *filenames;
90         for (filenames = notmuch_message_get_filenames (message);
91              notmuch_filenames_valid (filenames);
92              notmuch_filenames_move_to_next (filenames))
93         {
94             filename = notmuch_filenames_get (filenames);
95             mctx->file = fopen (filename, "r");
96             if (mctx->file)
97                 break;
98         }
99
100         talloc_free (filenames);
101         if (! mctx->file) {
102             /* Give up */
103             fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
104                 status = NOTMUCH_STATUS_FILE_ERROR;
105                 goto DONE;
106             }
107         }
108
109     mctx->stream = g_mime_stream_file_new (mctx->file);
110     if (!mctx->stream) {
111         fprintf (stderr, "Out of memory.\n");
112         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
113         goto DONE;
114     }
115     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (mctx->stream), false);
116
117     mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
118     if (!mctx->parser) {
119         fprintf (stderr, "Out of memory.\n");
120         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
121         goto DONE;
122     }
123
124     mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
125     if (!mctx->mime_message) {
126         fprintf (stderr, "Failed to parse %s\n", filename);
127         status = NOTMUCH_STATUS_FILE_ERROR;
128         goto DONE;
129     }
130
131     mctx->crypto = crypto;
132
133     /* Create the root node */
134     root->part = GMIME_OBJECT (mctx->mime_message);
135     root->envelope_file = message;
136     root->nchildren = 1;
137     root->ctx = mctx;
138
139     root->parent = NULL;
140     root->part_num = 0;
141     root->next_child = 0;
142     root->next_part_num = 1;
143
144     *root_out = root;
145     return NOTMUCH_STATUS_SUCCESS;
146
147 DONE:
148     talloc_free (root);
149     return status;
150 }
151
152 /* Signature list destructor (GMime 2.6) */
153 static int
154 _signature_list_free (GMimeSignatureList **proxy)
155 {
156     g_object_unref (*proxy);
157     return 0;
158 }
159
160 /* Set up signature list destructor (GMime 2.6) */
161 static void
162 set_signature_list_destructor (mime_node_t *node)
163 {
164     GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
165     if (proxy) {
166         *proxy = node->sig_list;
167         talloc_set_destructor (proxy, _signature_list_free);
168     }
169 }
170
171 /* Verify a signed mime node (GMime 2.6) */
172 static void
173 node_verify (mime_node_t *node, GMimeObject *part,
174              g_mime_3_unused(GMimeCryptoContext *cryptoctx))
175 {
176     GError *err = NULL;
177
178     node->verify_attempted = true;
179     node->sig_list = g_mime_multipart_signed_verify
180         (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);
181
182     if (node->sig_list)
183         set_signature_list_destructor (node);
184     else
185         fprintf (stderr, "Failed to verify signed part: %s\n",
186                  err ? err->message : "no error explanation given");
187
188     if (err)
189         g_error_free (err);
190 }
191
192 /* Decrypt and optionally verify an encrypted mime node (GMime 2.6) */
193 static void
194 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
195                          g_mime_3_unused(GMimeCryptoContext *cryptoctx))
196 {
197     GError *err = NULL;
198     GMimeDecryptResult *decrypt_result = NULL;
199     GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
200
201     if (! node->decrypted_child) {
202         mime_node_t *parent;
203         for (parent = node; parent; parent = parent->parent)
204             if (parent->envelope_file)
205                 break;
206
207         node->decrypt_attempted = true;
208         node->decrypted_child = _notmuch_crypto_decrypt (parent ? parent->envelope_file : NULL,
209                                                          cryptoctx, encrypteddata, &decrypt_result, &err);
210     }
211     if (! node->decrypted_child) {
212         fprintf (stderr, "Failed to decrypt part: %s\n",
213                  err ? err->message : "no error explanation given");
214         goto DONE;
215     }
216
217     node->decrypt_success = true;
218     node->verify_attempted = true;
219
220     if (decrypt_result) {
221         /* This may be NULL if the part is not signed. */
222         node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
223         if (node->sig_list) {
224             g_object_ref (node->sig_list);
225             set_signature_list_destructor (node);
226         }
227         g_object_unref (decrypt_result);
228     }
229
230  DONE:
231     if (err)
232         g_error_free (err);
233 }
234
235 static mime_node_t *
236 _mime_node_create (mime_node_t *parent, GMimeObject *part)
237 {
238     mime_node_t *node = talloc_zero (parent, mime_node_t);
239     GMimeCryptoContext *cryptoctx = NULL;
240
241     /* Set basic node properties */
242     node->part = part;
243     node->ctx = parent->ctx;
244     if (!talloc_reference (node, node->ctx)) {
245         fprintf (stderr, "Out of memory.\n");
246         talloc_free (node);
247         return NULL;
248     }
249     node->parent = parent;
250     node->part_num = node->next_part_num = -1;
251     node->next_child = 0;
252
253     /* Deal with the different types of parts */
254     if (GMIME_IS_PART (part)) {
255         node->nchildren = 0;
256     } else if (GMIME_IS_MULTIPART (part)) {
257         node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
258     } else if (GMIME_IS_MESSAGE_PART (part)) {
259         /* Promote part to an envelope and open it */
260         GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
261         GMimeMessage *message = g_mime_message_part_get_message (message_part);
262         node->envelope_part = message_part;
263         node->part = GMIME_OBJECT (message);
264         node->nchildren = 1;
265     } else {
266         fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
267                  g_type_name (G_OBJECT_TYPE (part)));
268         talloc_free (node);
269         return NULL;
270     }
271
272 #if (GMIME_MAJOR_VERSION < 3)
273     if ((GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt)
274         || (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) {
275         GMimeContentType *content_type = g_mime_object_get_content_type (part);
276         const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol");
277         notmuch_status_t status;
278         status = _notmuch_crypto_get_gmime_ctx_for_protocol (node->ctx->crypto,
279                                                              protocol, &cryptoctx);
280         if (status) /* this is a warning, not an error */
281             fprintf (stderr, "Warning: %s (%s).\n", notmuch_status_to_string (status),
282                      protocol ? protocol : "NULL");
283         if (!cryptoctx)
284             return node;
285     }
286 #endif
287
288     /* Handle PGP/MIME parts */
289     if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt) {
290         if (node->nchildren != 2) {
291             /* this violates RFC 3156 section 4, so we won't bother with it. */
292             fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
293                      "message (must be exactly 2)\n",
294                      node->nchildren);
295         } else {
296             node_decrypt_and_verify (node, part, cryptoctx);
297         }
298     } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify) {
299         if (node->nchildren != 2) {
300             /* this violates RFC 3156 section 5, so we won't bother with it. */
301             fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
302                      "(must be exactly 2)\n",
303                      node->nchildren);
304         } else {
305             node_verify (node, part, cryptoctx);
306         }
307     }
308
309     return node;
310 }
311
312 mime_node_t *
313 mime_node_child (mime_node_t *parent, int child)
314 {
315     GMimeObject *sub;
316     mime_node_t *node;
317
318     if (!parent || !parent->part || child < 0 || child >= parent->nchildren)
319         return NULL;
320
321     if (GMIME_IS_MULTIPART (parent->part)) {
322         if (child == 1 && parent->decrypted_child)
323             sub = parent->decrypted_child;
324         else
325             sub = g_mime_multipart_get_part
326                 (GMIME_MULTIPART (parent->part), child);
327     } else if (GMIME_IS_MESSAGE (parent->part)) {
328         sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
329     } else {
330         /* This should have been caught by message_part_create */
331         INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
332                         g_type_name (G_OBJECT_TYPE (parent->part)));
333     }
334     node = _mime_node_create (parent, sub);
335
336     if (child == parent->next_child && parent->next_part_num != -1) {
337         /* We're traversing in depth-first order.  Record the child's
338          * depth-first numbering. */
339         node->part_num = parent->next_part_num;
340         node->next_part_num = node->part_num + 1;
341
342         /* Prepare the parent for its next depth-first child. */
343         parent->next_child++;
344         parent->next_part_num = -1;
345
346         if (node->nchildren == 0) {
347             /* We've reached a leaf, so find the parent that has more
348              * children and set it up to number its next child. */
349             mime_node_t *iter = node->parent;
350             while (iter && iter->next_child == iter->nchildren)
351                 iter = iter->parent;
352             if (iter)
353                 iter->next_part_num = node->part_num + 1;
354         }
355     }
356
357     return node;
358 }
359
360 static mime_node_t *
361 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
362 {
363     int i;
364
365     if (*n == 0)
366         return node;
367
368     *n -= 1;
369     for (i = 0; i < node->nchildren; i++) {
370         mime_node_t *child = mime_node_child (node, i);
371         mime_node_t *ret = _mime_node_seek_dfs_walk (child, n);
372         if (ret)
373             return ret;
374
375         talloc_free (child);
376     }
377     return NULL;
378 }
379
380 mime_node_t *
381 mime_node_seek_dfs (mime_node_t *node, int n)
382 {
383     if (n < 0)
384         return NULL;
385     return _mime_node_seek_dfs_walk (node, &n);
386 }