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