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