]> git.notmuchmail.org Git - notmuch/blob - mime-node.c
27077f7f40f34cdb89080ed43a64f72ac4e2a81d
[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_out = root;
116     return NOTMUCH_STATUS_SUCCESS;
117
118 DONE:
119     talloc_free (root);
120     return status;
121 }
122
123 #ifdef GMIME_ATLEAST_26
124 static int
125 _signature_list_free (GMimeSignatureList **proxy)
126 {
127     g_object_unref (*proxy);
128     return 0;
129 }
130 #else
131 static int
132 _signature_validity_free (GMimeSignatureValidity **proxy)
133 {
134     g_mime_signature_validity_free (*proxy);
135     return 0;
136 }
137 #endif
138
139 static mime_node_t *
140 _mime_node_create (const mime_node_t *parent, GMimeObject *part)
141 {
142     mime_node_t *node = talloc_zero (parent, mime_node_t);
143     GError *err = NULL;
144
145     /* Set basic node properties */
146     node->part = part;
147     node->ctx = parent->ctx;
148     if (!talloc_reference (node, node->ctx)) {
149         fprintf (stderr, "Out of memory.\n");
150         talloc_free (node);
151         return NULL;
152     }
153
154     /* Deal with the different types of parts */
155     if (GMIME_IS_PART (part)) {
156         node->nchildren = 0;
157     } else if (GMIME_IS_MULTIPART (part)) {
158         node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
159     } else if (GMIME_IS_MESSAGE_PART (part)) {
160         /* Promote part to an envelope and open it */
161         GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
162         GMimeMessage *message = g_mime_message_part_get_message (message_part);
163         node->envelope_part = message_part;
164         node->part = GMIME_OBJECT (message);
165         node->nchildren = 1;
166     } else {
167         fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
168                  g_type_name (G_OBJECT_TYPE (part)));
169         talloc_free (node);
170         return NULL;
171     }
172
173     /* Handle PGP/MIME parts */
174     if (GMIME_IS_MULTIPART_ENCRYPTED (part)
175         && node->ctx->cryptoctx && node->ctx->decrypt) {
176         if (node->nchildren != 2) {
177             /* this violates RFC 3156 section 4, so we won't bother with it. */
178             fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
179                      "message (must be exactly 2)\n",
180                      node->nchildren);
181         } else {
182             GMimeMultipartEncrypted *encrypteddata =
183                 GMIME_MULTIPART_ENCRYPTED (part);
184             node->decrypt_attempted = TRUE;
185 #ifdef GMIME_ATLEAST_26
186             GMimeDecryptResult *decrypt_result = NULL;
187             node->decrypted_child = g_mime_multipart_encrypted_decrypt
188                 (encrypteddata, node->ctx->cryptoctx, &decrypt_result, &err);
189 #else
190             node->decrypted_child = g_mime_multipart_encrypted_decrypt
191                 (encrypteddata, node->ctx->cryptoctx, &err);
192 #endif
193             if (node->decrypted_child) {
194                 node->decrypt_success = node->verify_attempted = TRUE;
195 #ifdef GMIME_ATLEAST_26
196                 /* This may be NULL if the part is not signed. */
197                 node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
198                 if (node->sig_list)
199                     g_object_ref (node->sig_list);
200                 g_object_unref (decrypt_result);
201 #else
202                 node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
203 #endif
204             } else {
205                 fprintf (stderr, "Failed to decrypt part: %s\n",
206                          (err ? err->message : "no error explanation given"));
207             }
208         }
209     } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->cryptoctx) {
210         if (node->nchildren != 2) {
211             /* this violates RFC 3156 section 5, so we won't bother with it. */
212             fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
213                      "(must be exactly 2)\n",
214                      node->nchildren);
215         } else {
216 #ifdef GMIME_ATLEAST_26
217             node->sig_list = g_mime_multipart_signed_verify
218                 (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);
219             node->verify_attempted = TRUE;
220
221             if (!node->sig_list)
222                 fprintf (stderr, "Failed to verify signed part: %s\n",
223                          (err ? err->message : "no error explanation given"));
224 #else
225             /* For some reason the GMimeSignatureValidity returned
226              * here is not a const (inconsistent with that
227              * returned by
228              * g_mime_multipart_encrypted_get_signature_validity,
229              * and therefore needs to be properly disposed of.
230              *
231              * In GMime 2.6, they're both non-const, so we'll be able
232              * to clean up this asymmetry. */
233             GMimeSignatureValidity *sig_validity = g_mime_multipart_signed_verify
234                 (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);
235             node->verify_attempted = TRUE;
236             node->sig_validity = sig_validity;
237             if (sig_validity) {
238                 GMimeSignatureValidity **proxy =
239                     talloc (node, GMimeSignatureValidity *);
240                 *proxy = sig_validity;
241                 talloc_set_destructor (proxy, _signature_validity_free);
242             }
243 #endif
244         }
245     }
246
247 #ifdef GMIME_ATLEAST_26
248     /* sig_list may be created in both above cases, so we need to
249      * cleanly handle it here. */
250     if (node->sig_list) {
251         GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
252         *proxy = node->sig_list;
253         talloc_set_destructor (proxy, _signature_list_free);
254     }
255 #endif
256
257 #ifndef GMIME_ATLEAST_26
258     if (node->verify_attempted && !node->sig_validity)
259         fprintf (stderr, "Failed to verify signed part: %s\n",
260                  (err ? err->message : "no error explanation given"));
261 #endif
262
263     if (err)
264         g_error_free (err);
265
266     return node;
267 }
268
269 mime_node_t *
270 mime_node_child (const mime_node_t *parent, int child)
271 {
272     GMimeObject *sub;
273
274     if (!parent || child < 0 || child >= parent->nchildren)
275         return NULL;
276
277     if (GMIME_IS_MULTIPART (parent->part)) {
278         if (child == 1 && parent->decrypted_child)
279             sub = parent->decrypted_child;
280         else
281             sub = g_mime_multipart_get_part
282                 (GMIME_MULTIPART (parent->part), child);
283     } else if (GMIME_IS_MESSAGE (parent->part)) {
284         sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
285     } else {
286         /* This should have been caught by message_part_create */
287         INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
288                         g_type_name (G_OBJECT_TYPE (parent->part)));
289     }
290     return _mime_node_create (parent, sub);
291 }
292
293 static mime_node_t *
294 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
295 {
296     mime_node_t *ret = NULL;
297     int i;
298
299     if (*n == 0)
300         return node;
301
302     *n -= 1;
303     for (i = 0; i < node->nchildren && !ret; i++) {
304         mime_node_t *child = mime_node_child (node, i);
305         ret = _mime_node_seek_dfs_walk (child, n);
306         if (!ret)
307             talloc_free (child);
308     }
309     return ret;
310 }
311
312 mime_node_t *
313 mime_node_seek_dfs (mime_node_t *node, int n)
314 {
315     if (n < 0)
316         return NULL;
317     return _mime_node_seek_dfs_walk (node, &n);
318 }