]> git.notmuchmail.org Git - notmuch/blob - mime-node.c
851f963b7ee0e11e1a6a50d848b8f7e8a75adb4d
[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
134 /* Signature list destructor (GMime 2.6) */
135 static int
136 _signature_list_free (GMimeSignatureList **proxy)
137 {
138     g_object_unref (*proxy);
139     return 0;
140 }
141
142 /* Set up signature list destructor (GMime 2.6) */
143 static void
144 set_signature_list_destructor (mime_node_t *node)
145 {
146     GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
147     if (proxy) {
148         *proxy = node->sig_list;
149         talloc_set_destructor (proxy, _signature_list_free);
150     }
151 }
152
153 /* Verify a signed mime node (GMime 2.6) */
154 static void
155 node_verify (mime_node_t *node, GMimeObject *part,
156              notmuch_crypto_context_t *cryptoctx)
157 {
158     GError *err = NULL;
159
160     node->verify_attempted = TRUE;
161     node->sig_list = g_mime_multipart_signed_verify
162         (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);
163
164     if (node->sig_list)
165         set_signature_list_destructor (node);
166     else
167         fprintf (stderr, "Failed to verify signed part: %s\n",
168                  err ? err->message : "no error explanation given");
169
170     if (err)
171         g_error_free (err);
172 }
173
174 /* Decrypt and optionally verify an encrypted mime node (GMime 2.6) */
175 static void
176 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
177                          notmuch_crypto_context_t *cryptoctx)
178 {
179     GError *err = NULL;
180     GMimeDecryptResult *decrypt_result = NULL;
181     GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
182
183     node->decrypt_attempted = TRUE;
184     node->decrypted_child = g_mime_multipart_encrypted_decrypt
185         (encrypteddata, cryptoctx, &decrypt_result, &err);
186     if (! node->decrypted_child) {
187         fprintf (stderr, "Failed to decrypt part: %s\n",
188                  err ? err->message : "no error explanation given");
189         goto DONE;
190     }
191
192     node->decrypt_success = TRUE;
193     node->verify_attempted = TRUE;
194
195     /* This may be NULL if the part is not signed. */
196     node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
197     if (node->sig_list) {
198         g_object_ref (node->sig_list);
199         set_signature_list_destructor (node);
200     }
201     g_object_unref (decrypt_result);
202
203  DONE:
204     if (err)
205         g_error_free (err);
206 }
207
208 #else /* GMIME_ATLEAST_26 */
209
210 /* Signature validity destructor (GMime 2.4) */
211 static int
212 _signature_validity_free (GMimeSignatureValidity **proxy)
213 {
214     g_mime_signature_validity_free (*proxy);
215     return 0;
216 }
217
218 /* Set up signature validity destructor (GMime 2.4) */
219 static void
220 set_signature_validity_destructor (mime_node_t *node)
221 {
222     GMimeSignatureValidity **proxy = talloc (node, GMimeSignatureValidity *);
223     if (proxy) {
224         *proxy = node->sig_validity;
225         talloc_set_destructor (proxy, _signature_validity_free);
226     }
227 }
228
229 /* Verify a signed mime node (GMime 2.4) */
230 static void
231 node_verify (mime_node_t *node, GMimeObject *part,
232              notmuch_crypto_context_t *cryptoctx)
233 {
234     GError *err = NULL;
235
236     node->verify_attempted = TRUE;
237     node->sig_validity = g_mime_multipart_signed_verify
238         (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);
239     if (node->sig_validity) {
240         set_signature_validity_destructor (node);
241     } else {
242         fprintf (stderr, "Failed to verify signed part: %s\n",
243                  err ? err->message : "no error explanation given");
244     }
245
246     if (err)
247         g_error_free (err);
248 }
249
250 /* Decrypt and optionally verify an encrypted mime node (GMime 2.4) */
251 static void
252 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
253                          notmuch_crypto_context_t *cryptoctx)
254 {
255     GError *err = NULL;
256     GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
257
258     node->decrypt_attempted = TRUE;
259     node->decrypted_child = g_mime_multipart_encrypted_decrypt
260         (encrypteddata, cryptoctx, &err);
261     if (! node->decrypted_child) {
262         fprintf (stderr, "Failed to decrypt part: %s\n",
263                  err ? err->message : "no error explanation given");
264         goto DONE;
265     }
266
267     node->decrypt_success = TRUE;
268     node->verify_attempted = TRUE;
269
270     /* The GMimeSignatureValidity returned here is a const, unlike the
271      * one returned by g_mime_multipart_signed_verify() in
272      * node_verify() above, so the destructor is not needed.
273      */
274     node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
275     if (! node->sig_validity)
276         fprintf (stderr, "Failed to verify encrypted signed part: %s\n",
277                  err ? err->message : "no error explanation given");
278
279  DONE:
280     if (err)
281         g_error_free (err);
282 }
283
284 #endif  /* GMIME_ATLEAST_26 */
285
286 static mime_node_t *
287 _mime_node_create (mime_node_t *parent, GMimeObject *part)
288 {
289     mime_node_t *node = talloc_zero (parent, mime_node_t);
290     notmuch_crypto_context_t *cryptoctx = NULL;
291
292     /* Set basic node properties */
293     node->part = part;
294     node->ctx = parent->ctx;
295     if (!talloc_reference (node, node->ctx)) {
296         fprintf (stderr, "Out of memory.\n");
297         talloc_free (node);
298         return NULL;
299     }
300     node->parent = parent;
301     node->part_num = node->next_part_num = -1;
302     node->next_child = 0;
303
304     /* Deal with the different types of parts */
305     if (GMIME_IS_PART (part)) {
306         node->nchildren = 0;
307     } else if (GMIME_IS_MULTIPART (part)) {
308         node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
309     } else if (GMIME_IS_MESSAGE_PART (part)) {
310         /* Promote part to an envelope and open it */
311         GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
312         GMimeMessage *message = g_mime_message_part_get_message (message_part);
313         node->envelope_part = message_part;
314         node->part = GMIME_OBJECT (message);
315         node->nchildren = 1;
316     } else {
317         fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
318                  g_type_name (G_OBJECT_TYPE (part)));
319         talloc_free (node);
320         return NULL;
321     }
322
323     if ((GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt)
324         || (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) {
325         GMimeContentType *content_type = g_mime_object_get_content_type (part);
326         const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol");
327         cryptoctx = notmuch_crypto_get_context (node->ctx->crypto, protocol);
328     }
329
330     /* Handle PGP/MIME parts */
331     if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt && cryptoctx) {
332         if (node->nchildren != 2) {
333             /* this violates RFC 3156 section 4, so we won't bother with it. */
334             fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
335                      "message (must be exactly 2)\n",
336                      node->nchildren);
337         } else {
338             node_decrypt_and_verify (node, part, cryptoctx);
339         }
340     } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify && cryptoctx) {
341         if (node->nchildren != 2) {
342             /* this violates RFC 3156 section 5, so we won't bother with it. */
343             fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
344                      "(must be exactly 2)\n",
345                      node->nchildren);
346         } else {
347             node_verify (node, part, cryptoctx);
348         }
349     }
350
351     return node;
352 }
353
354 mime_node_t *
355 mime_node_child (mime_node_t *parent, int child)
356 {
357     GMimeObject *sub;
358     mime_node_t *node;
359
360     if (!parent || !parent->part || child < 0 || child >= parent->nchildren)
361         return NULL;
362
363     if (GMIME_IS_MULTIPART (parent->part)) {
364         if (child == 1 && parent->decrypted_child)
365             sub = parent->decrypted_child;
366         else
367             sub = g_mime_multipart_get_part
368                 (GMIME_MULTIPART (parent->part), child);
369     } else if (GMIME_IS_MESSAGE (parent->part)) {
370         sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
371     } else {
372         /* This should have been caught by message_part_create */
373         INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
374                         g_type_name (G_OBJECT_TYPE (parent->part)));
375     }
376     node = _mime_node_create (parent, sub);
377
378     if (child == parent->next_child && parent->next_part_num != -1) {
379         /* We're traversing in depth-first order.  Record the child's
380          * depth-first numbering. */
381         node->part_num = parent->next_part_num;
382         node->next_part_num = node->part_num + 1;
383
384         /* Prepare the parent for its next depth-first child. */
385         parent->next_child++;
386         parent->next_part_num = -1;
387
388         if (node->nchildren == 0) {
389             /* We've reached a leaf, so find the parent that has more
390              * children and set it up to number its next child. */
391             mime_node_t *iter = node->parent;
392             while (iter && iter->next_child == iter->nchildren)
393                 iter = iter->parent;
394             if (iter)
395                 iter->next_part_num = node->part_num + 1;
396         }
397     }
398
399     return node;
400 }
401
402 static mime_node_t *
403 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
404 {
405     mime_node_t *ret = NULL;
406     int i;
407
408     if (*n == 0)
409         return node;
410
411     *n -= 1;
412     for (i = 0; i < node->nchildren && !ret; i++) {
413         mime_node_t *child = mime_node_child (node, i);
414         ret = _mime_node_seek_dfs_walk (child, n);
415         if (!ret)
416             talloc_free (child);
417     }
418     return ret;
419 }
420
421 mime_node_t *
422 mime_node_seek_dfs (mime_node_t *node, int n)
423 {
424     if (n < 0)
425         return NULL;
426     return _mime_node_seek_dfs_walk (node, &n);
427 }