]> git.notmuchmail.org Git - notmuch/blob - mime-node.c
test: avoid showing legacy-display parts
[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 <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 #include "notmuch-client.h"
29
30 /* Context that gets inherited from the root node. */
31 typedef struct mime_node_context {
32     /* Per-message resources.  These are allocated internally and must
33      * be destroyed. */
34     GMimeStream *stream;
35     GMimeParser *parser;
36     GMimeMessage *mime_message;
37     _notmuch_message_crypto_t *msg_crypto;
38
39     /* Context provided by the caller. */
40     _notmuch_crypto_t *crypto;
41 } mime_node_context_t;
42
43 static int
44 _mime_node_context_free (mime_node_context_t *res)
45 {
46     if (res->mime_message)
47         g_object_unref (res->mime_message);
48
49     if (res->parser)
50         g_object_unref (res->parser);
51
52     if (res->stream)
53         g_object_unref (res->stream);
54
55     return 0;
56 }
57
58 const _notmuch_message_crypto_t *
59 mime_node_get_message_crypto_status (mime_node_t *node)
60 {
61     return node->ctx->msg_crypto;
62 }
63
64 notmuch_status_t
65 mime_node_open (const void *ctx, notmuch_message_t *message,
66                 _notmuch_crypto_t *crypto, mime_node_t **root_out)
67 {
68     const char *filename = notmuch_message_get_filename (message);
69     mime_node_context_t *mctx;
70     mime_node_t *root;
71     notmuch_status_t status;
72     int fd;
73
74     root = talloc_zero (ctx, mime_node_t);
75     if (root == NULL) {
76         fprintf (stderr, "Out of memory.\n");
77         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
78         goto DONE;
79     }
80
81     /* Create the tree-wide context */
82     mctx = talloc_zero (root, mime_node_context_t);
83     if (mctx == NULL) {
84         fprintf (stderr, "Out of memory.\n");
85         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
86         goto DONE;
87     }
88     talloc_set_destructor (mctx, _mime_node_context_free);
89
90     /* Fast path */
91     fd = open (filename, O_RDONLY);
92     if (fd == -1) {
93         /* Slow path - for some reason the first file in the list is
94          * not available anymore. This is clearly a problem in the
95          * database, but we are not going to let this problem be a
96          * show stopper */
97         notmuch_filenames_t *filenames;
98         for (filenames = notmuch_message_get_filenames (message);
99              notmuch_filenames_valid (filenames);
100              notmuch_filenames_move_to_next (filenames)) {
101             filename = notmuch_filenames_get (filenames);
102             fd = open (filename, O_RDONLY);
103             if (fd != -1)
104                 break;
105         }
106
107         talloc_free (filenames);
108         if (fd == -1) {
109             /* Give up */
110             fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
111             status = NOTMUCH_STATUS_FILE_ERROR;
112             goto DONE;
113         }
114     }
115
116     mctx->stream = g_mime_stream_gzfile_new (fd);
117     if (! mctx->stream) {
118         fprintf (stderr, "Out of memory.\n");
119         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
120         goto DONE;
121     }
122
123     mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
124     if (! mctx->parser) {
125         fprintf (stderr, "Out of memory.\n");
126         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
127         goto DONE;
128     }
129
130     mctx->mime_message = g_mime_parser_construct_message (mctx->parser, NULL);
131     if (! mctx->mime_message) {
132         fprintf (stderr, "Failed to parse %s\n", filename);
133         status = NOTMUCH_STATUS_FILE_ERROR;
134         goto DONE;
135     }
136
137     mctx->msg_crypto = _notmuch_message_crypto_new (mctx);
138
139     mctx->crypto = crypto;
140
141     /* Create the root node */
142     root->part = GMIME_OBJECT (mctx->mime_message);
143     root->envelope_file = message;
144     root->nchildren = 1;
145     root->ctx = mctx;
146
147     root->parent = NULL;
148     root->part_num = 0;
149     root->next_child = 0;
150     root->next_part_num = 1;
151
152     *root_out = root;
153     return NOTMUCH_STATUS_SUCCESS;
154
155   DONE:
156     talloc_free (root);
157     return status;
158 }
159
160 /* Signature list destructor */
161 static int
162 _signature_list_free (GMimeSignatureList **proxy)
163 {
164     g_object_unref (*proxy);
165     return 0;
166 }
167
168 /* Set up signature list destructor */
169 static void
170 set_signature_list_destructor (mime_node_t *node)
171 {
172     GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
173
174     if (proxy) {
175         *proxy = node->sig_list;
176         talloc_set_destructor (proxy, _signature_list_free);
177     }
178 }
179
180 /* Verify a signed mime node */
181 static void
182 node_verify (mime_node_t *node, GMimeObject *part)
183 {
184     GError *err = NULL;
185     notmuch_status_t status;
186
187     node->verify_attempted = true;
188     node->sig_list = g_mime_multipart_signed_verify (
189         GMIME_MULTIPART_SIGNED (part), GMIME_ENCRYPT_NONE, &err);
190
191     if (node->sig_list)
192         set_signature_list_destructor (node);
193     else
194         fprintf (stderr, "Failed to verify signed part: %s\n",
195                  err ? err->message : "no error explanation given");
196
197     if (err)
198         g_error_free (err);
199
200     status = _notmuch_message_crypto_potential_sig_list (node->ctx->msg_crypto, node->sig_list);
201     if (status) /* this is a warning, not an error */
202         fprintf (stderr, "Warning: failed to note signature status: %s.\n", notmuch_status_to_string (status));
203 }
204
205 /* Decrypt and optionally verify an encrypted mime node */
206 static void
207 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part)
208 {
209     GError *err = NULL;
210     GMimeDecryptResult *decrypt_result = NULL;
211     notmuch_status_t status;
212     GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
213     notmuch_message_t *message = NULL;
214
215     if (! node->decrypted_child) {
216         for (mime_node_t *parent = node; parent; parent = parent->parent)
217             if (parent->envelope_file) {
218                 message = parent->envelope_file;
219                 break;
220             }
221
222         node->decrypted_child = _notmuch_crypto_decrypt (&node->decrypt_attempted,
223                                                          node->ctx->crypto->decrypt,
224                                                          message,
225                                                          encrypteddata, &decrypt_result, &err);
226     }
227     if (! node->decrypted_child) {
228         fprintf (stderr, "Failed to decrypt part: %s\n",
229                  err ? err->message : "no error explanation given");
230         goto DONE;
231     }
232
233     node->decrypt_success = true;
234     status = _notmuch_message_crypto_successful_decryption (node->ctx->msg_crypto);
235     if (status) /* this is a warning, not an error */
236         fprintf (stderr, "Warning: failed to note decryption status: %s.\n", notmuch_status_to_string (status));
237
238     if (decrypt_result) {
239         /* This may be NULL if the part is not signed. */
240         node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
241         if (node->sig_list) {
242             node->verify_attempted = true;
243             g_object_ref (node->sig_list);
244             set_signature_list_destructor (node);
245             status = _notmuch_message_crypto_potential_sig_list (node->ctx->msg_crypto, node->sig_list);
246             if (status) /* this is a warning, not an error */
247                 fprintf (stderr, "Warning: failed to note signature status: %s.\n", notmuch_status_to_string (status));
248         }
249
250         if (node->ctx->crypto->decrypt == NOTMUCH_DECRYPT_TRUE && message) {
251             notmuch_database_t *db = notmuch_message_get_database (message);
252             const char *session_key = g_mime_decrypt_result_get_session_key (decrypt_result);
253             if (db && session_key)
254                 print_status_message ("Failed to stash session key in the database",
255                                       message,
256                                       notmuch_message_add_property (message, "session-key",
257                                                                     session_key));
258         }
259         g_object_unref (decrypt_result);
260     }
261
262   DONE:
263     if (err)
264         g_error_free (err);
265 }
266
267 static bool
268 _mime_node_set_up_part (mime_node_t *node, GMimeObject *part, int numchild);
269
270 static mime_node_t *
271 _mime_node_create (mime_node_t *parent, GMimeObject *part, int numchild)
272 {
273     mime_node_t *node = talloc_zero (parent, mime_node_t);
274
275     /* Set basic node properties */
276     node->ctx = parent->ctx;
277     if (! talloc_reference (node, node->ctx)) {
278         fprintf (stderr, "Out of memory.\n");
279         talloc_free (node);
280         return NULL;
281     }
282     node->parent = parent;
283     node->part_num = node->next_part_num = -1;
284     node->next_child = 0;
285
286     if (_mime_node_set_up_part (node, part, numchild))
287         return node;
288     talloc_free (node);
289     return NULL;
290 }
291
292 /* associate a MIME part with a node. */
293 static bool
294 _mime_node_set_up_part (mime_node_t *node, GMimeObject *part, int numchild)
295 {
296     notmuch_status_t status;
297
298     /* Deal with the different types of parts */
299     if (GMIME_IS_PART (part)) {
300         node->part = part;
301         node->nchildren = 0;
302     } else if (GMIME_IS_MULTIPART (part)) {
303         node->part = part;
304         node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
305     } else if (GMIME_IS_MESSAGE_PART (part)) {
306         /* Promote part to an envelope and open it */
307         GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
308         GMimeMessage *message = g_mime_message_part_get_message (message_part);
309         node->envelope_part = message_part;
310         node->part = GMIME_OBJECT (message);
311         node->nchildren = 1;
312     } else {
313         fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
314                  g_type_name (G_OBJECT_TYPE (part)));
315         return false;
316     }
317
318     /* Handle PGP/MIME parts (by definition not cryptographic payload parts) */
319     if (GMIME_IS_MULTIPART_ENCRYPTED (part) && (node->ctx->crypto->decrypt != NOTMUCH_DECRYPT_FALSE)) {
320         if (node->nchildren != 2) {
321             /* this violates RFC 3156 section 4, so we won't bother with it. */
322             fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
323                      "message (must be exactly 2)\n",
324                      node->nchildren);
325         } else {
326             node_decrypt_and_verify (node, part);
327         }
328     } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify) {
329         if (node->nchildren != 2) {
330             /* this violates RFC 3156 section 5, so we won't bother with it. */
331             fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
332                      "(must be exactly 2)\n",
333                      node->nchildren);
334         } else {
335             node_verify (node, part);
336         }
337     } else {
338         status = _notmuch_message_crypto_potential_payload (node->ctx->msg_crypto, part, node->parent ? node->parent->part : NULL, numchild);
339         if (status)
340             fprintf (stderr, "Warning: failed to record potential crypto payload (%s).\n", notmuch_status_to_string (status));
341     }
342
343     return true;
344 }
345
346 mime_node_t *
347 mime_node_child (mime_node_t *parent, int child)
348 {
349     GMimeObject *sub;
350     mime_node_t *node;
351
352     if (! parent || ! parent->part || child < 0 || child >= parent->nchildren)
353         return NULL;
354
355     if (GMIME_IS_MULTIPART (parent->part)) {
356         if (child == GMIME_MULTIPART_ENCRYPTED_CONTENT && parent->decrypted_child)
357             sub = parent->decrypted_child;
358         else
359             sub = g_mime_multipart_get_part (
360                 GMIME_MULTIPART (parent->part), child);
361     } else if (GMIME_IS_MESSAGE (parent->part)) {
362         sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
363     } else {
364         /* This should have been caught by _mime_node_set_up_part */
365         INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
366                         g_type_name (G_OBJECT_TYPE (parent->part)));
367     }
368     node = _mime_node_create (parent, sub, child);
369
370     if (child == parent->next_child && parent->next_part_num != -1) {
371         /* We're traversing in depth-first order.  Record the child's
372          * depth-first numbering. */
373         node->part_num = parent->next_part_num;
374         node->next_part_num = node->part_num + 1;
375
376         /* Prepare the parent for its next depth-first child. */
377         parent->next_child++;
378         parent->next_part_num = -1;
379
380         if (node->nchildren == 0) {
381             /* We've reached a leaf, so find the parent that has more
382              * children and set it up to number its next child. */
383             mime_node_t *iter = node->parent;
384             while (iter && iter->next_child == iter->nchildren)
385                 iter = iter->parent;
386             if (iter)
387                 iter->next_part_num = node->part_num + 1;
388         }
389     }
390
391     return node;
392 }
393
394 static mime_node_t *
395 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
396 {
397     int i;
398
399     if (*n == 0)
400         return node;
401
402     *n -= 1;
403     for (i = 0; i < node->nchildren; i++) {
404         mime_node_t *child = mime_node_child (node, i);
405         mime_node_t *ret = _mime_node_seek_dfs_walk (child, n);
406         if (ret)
407             return ret;
408
409         talloc_free (child);
410     }
411     return NULL;
412 }
413
414 mime_node_t *
415 mime_node_seek_dfs (mime_node_t *node, int n)
416 {
417     if (n < 0)
418         return NULL;
419     return _mime_node_seek_dfs_walk (node, &n);
420 }