]> git.notmuchmail.org Git - notmuch/blob - mime-node.c
emacs: break up notmuch-show-archive-thread-internal into two more generally useful...
[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 #ifdef GMIME_ATLEAST_26
37     GMimeCryptoContext *cryptoctx;
38 #else
39     GMimeCipherContext *cryptoctx;
40 #endif
41     notmuch_bool_t decrypt;
42 } mime_node_context_t;
43
44 static int
45 _mime_node_context_free (mime_node_context_t *res)
46 {
47     if (res->mime_message)
48         g_object_unref (res->mime_message);
49
50     if (res->parser)
51         g_object_unref (res->parser);
52
53     if (res->stream)
54         g_object_unref (res->stream);
55
56     if (res->file)
57         fclose (res->file);
58
59     return 0;
60 }
61
62 notmuch_status_t
63 mime_node_open (const void *ctx, notmuch_message_t *message,
64 #ifdef GMIME_ATLEAST_26
65                 GMimeCryptoContext *cryptoctx,
66 #else
67                 GMimeCipherContext *cryptoctx,
68 #endif
69                 notmuch_bool_t decrypt, mime_node_t **root_out)
70 {
71     const char *filename = notmuch_message_get_filename (message);
72     mime_node_context_t *mctx;
73     mime_node_t *root;
74     notmuch_status_t status;
75
76     root = talloc_zero (ctx, mime_node_t);
77     if (root == NULL) {
78         fprintf (stderr, "Out of memory.\n");
79         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
80         goto DONE;
81     }
82
83     /* Create the tree-wide context */
84     mctx = talloc_zero (root, mime_node_context_t);
85     if (mctx == NULL) {
86         fprintf (stderr, "Out of memory.\n");
87         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
88         goto DONE;
89     }
90     talloc_set_destructor (mctx, _mime_node_context_free);
91
92     mctx->file = fopen (filename, "r");
93     if (! mctx->file) {
94         fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
95         status = NOTMUCH_STATUS_FILE_ERROR;
96         goto DONE;
97     }
98
99     mctx->stream = g_mime_stream_file_new (mctx->file);
100     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (mctx->stream), FALSE);
101
102     mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
103
104     mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
105
106     mctx->cryptoctx = cryptoctx;
107     mctx->decrypt = decrypt;
108
109     /* Create the root node */
110     root->part = GMIME_OBJECT (mctx->mime_message);
111     root->envelope_file = message;
112     root->nchildren = 1;
113     root->ctx = mctx;
114
115     root->parent = NULL;
116     root->part_num = 0;
117     root->next_child = 0;
118     root->next_part_num = 1;
119
120     *root_out = root;
121     return NOTMUCH_STATUS_SUCCESS;
122
123 DONE:
124     talloc_free (root);
125     return status;
126 }
127
128 #ifdef GMIME_ATLEAST_26
129 static int
130 _signature_list_free (GMimeSignatureList **proxy)
131 {
132     g_object_unref (*proxy);
133     return 0;
134 }
135 #else
136 static int
137 _signature_validity_free (GMimeSignatureValidity **proxy)
138 {
139     g_mime_signature_validity_free (*proxy);
140     return 0;
141 }
142 #endif
143
144 static mime_node_t *
145 _mime_node_create (mime_node_t *parent, GMimeObject *part)
146 {
147     mime_node_t *node = talloc_zero (parent, mime_node_t);
148     GError *err = NULL;
149
150     /* Set basic node properties */
151     node->part = part;
152     node->ctx = parent->ctx;
153     if (!talloc_reference (node, node->ctx)) {
154         fprintf (stderr, "Out of memory.\n");
155         talloc_free (node);
156         return NULL;
157     }
158     node->parent = parent;
159     node->part_num = node->next_part_num = -1;
160     node->next_child = 0;
161
162     /* Deal with the different types of parts */
163     if (GMIME_IS_PART (part)) {
164         node->nchildren = 0;
165     } else if (GMIME_IS_MULTIPART (part)) {
166         node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
167     } else if (GMIME_IS_MESSAGE_PART (part)) {
168         /* Promote part to an envelope and open it */
169         GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
170         GMimeMessage *message = g_mime_message_part_get_message (message_part);
171         node->envelope_part = message_part;
172         node->part = GMIME_OBJECT (message);
173         node->nchildren = 1;
174     } else {
175         fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
176                  g_type_name (G_OBJECT_TYPE (part)));
177         talloc_free (node);
178         return NULL;
179     }
180
181     /* Handle PGP/MIME parts */
182     if (GMIME_IS_MULTIPART_ENCRYPTED (part)
183         && node->ctx->cryptoctx && node->ctx->decrypt) {
184         if (node->nchildren != 2) {
185             /* this violates RFC 3156 section 4, so we won't bother with it. */
186             fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
187                      "message (must be exactly 2)\n",
188                      node->nchildren);
189         } else {
190             GMimeMultipartEncrypted *encrypteddata =
191                 GMIME_MULTIPART_ENCRYPTED (part);
192             node->decrypt_attempted = TRUE;
193 #ifdef GMIME_ATLEAST_26
194             GMimeDecryptResult *decrypt_result = NULL;
195             node->decrypted_child = g_mime_multipart_encrypted_decrypt
196                 (encrypteddata, node->ctx->cryptoctx, &decrypt_result, &err);
197 #else
198             node->decrypted_child = g_mime_multipart_encrypted_decrypt
199                 (encrypteddata, node->ctx->cryptoctx, &err);
200 #endif
201             if (node->decrypted_child) {
202                 node->decrypt_success = node->verify_attempted = TRUE;
203 #ifdef GMIME_ATLEAST_26
204                 /* This may be NULL if the part is not signed. */
205                 node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
206                 if (node->sig_list)
207                     g_object_ref (node->sig_list);
208                 g_object_unref (decrypt_result);
209 #else
210                 node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
211 #endif
212             } else {
213                 fprintf (stderr, "Failed to decrypt part: %s\n",
214                          (err ? err->message : "no error explanation given"));
215             }
216         }
217     } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->cryptoctx) {
218         if (node->nchildren != 2) {
219             /* this violates RFC 3156 section 5, so we won't bother with it. */
220             fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
221                      "(must be exactly 2)\n",
222                      node->nchildren);
223         } else {
224 #ifdef GMIME_ATLEAST_26
225             node->sig_list = g_mime_multipart_signed_verify
226                 (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);
227             node->verify_attempted = TRUE;
228
229             if (!node->sig_list)
230                 fprintf (stderr, "Failed to verify signed part: %s\n",
231                          (err ? err->message : "no error explanation given"));
232 #else
233             /* For some reason the GMimeSignatureValidity returned
234              * here is not a const (inconsistent with that
235              * returned by
236              * g_mime_multipart_encrypted_get_signature_validity,
237              * and therefore needs to be properly disposed of.
238              *
239              * In GMime 2.6, they're both non-const, so we'll be able
240              * to clean up this asymmetry. */
241             GMimeSignatureValidity *sig_validity = g_mime_multipart_signed_verify
242                 (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);
243             node->verify_attempted = TRUE;
244             node->sig_validity = sig_validity;
245             if (sig_validity) {
246                 GMimeSignatureValidity **proxy =
247                     talloc (node, GMimeSignatureValidity *);
248                 *proxy = sig_validity;
249                 talloc_set_destructor (proxy, _signature_validity_free);
250             }
251 #endif
252         }
253     }
254
255 #ifdef GMIME_ATLEAST_26
256     /* sig_list may be created in both above cases, so we need to
257      * cleanly handle it here. */
258     if (node->sig_list) {
259         GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
260         *proxy = node->sig_list;
261         talloc_set_destructor (proxy, _signature_list_free);
262     }
263 #endif
264
265 #ifndef GMIME_ATLEAST_26
266     if (node->verify_attempted && !node->sig_validity)
267         fprintf (stderr, "Failed to verify signed part: %s\n",
268                  (err ? err->message : "no error explanation given"));
269 #endif
270
271     if (err)
272         g_error_free (err);
273
274     return node;
275 }
276
277 mime_node_t *
278 mime_node_child (mime_node_t *parent, int child)
279 {
280     GMimeObject *sub;
281     mime_node_t *node;
282
283     if (!parent || child < 0 || child >= parent->nchildren)
284         return NULL;
285
286     if (GMIME_IS_MULTIPART (parent->part)) {
287         if (child == 1 && parent->decrypted_child)
288             sub = parent->decrypted_child;
289         else
290             sub = g_mime_multipart_get_part
291                 (GMIME_MULTIPART (parent->part), child);
292     } else if (GMIME_IS_MESSAGE (parent->part)) {
293         sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
294     } else {
295         /* This should have been caught by message_part_create */
296         INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
297                         g_type_name (G_OBJECT_TYPE (parent->part)));
298     }
299     node = _mime_node_create (parent, sub);
300
301     if (child == parent->next_child && parent->next_part_num != -1) {
302         /* We're traversing in depth-first order.  Record the child's
303          * depth-first numbering. */
304         node->part_num = parent->next_part_num;
305         node->next_part_num = node->part_num + 1;
306
307         /* Prepare the parent for its next depth-first child. */
308         parent->next_child++;
309         parent->next_part_num = -1;
310
311         if (node->nchildren == 0) {
312             /* We've reached a leaf, so find the parent that has more
313              * children and set it up to number its next child. */
314             mime_node_t *iter = node->parent;
315             while (iter && iter->next_child == iter->nchildren)
316                 iter = iter->parent;
317             if (iter)
318                 iter->next_part_num = node->part_num + 1;
319         }
320     }
321
322     return node;
323 }
324
325 static mime_node_t *
326 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
327 {
328     mime_node_t *ret = NULL;
329     int i;
330
331     if (*n == 0)
332         return node;
333
334     *n -= 1;
335     for (i = 0; i < node->nchildren && !ret; i++) {
336         mime_node_t *child = mime_node_child (node, i);
337         ret = _mime_node_seek_dfs_walk (child, n);
338         if (!ret)
339             talloc_free (child);
340     }
341     return ret;
342 }
343
344 mime_node_t *
345 mime_node_seek_dfs (mime_node_t *node, int n)
346 {
347     if (n < 0)
348         return NULL;
349     return _mime_node_seek_dfs_walk (node, &n);
350 }