]> git.notmuchmail.org Git - notmuch/blob - mime-node.c
test: Remove #! line from test-lib.sh
[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     GMimeCipherContext *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                 GMimeCipherContext *cryptoctx, notmuch_bool_t decrypt,
61                 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     g_mime_stream_file_set_owner (GMIME_STREAM_FILE (mctx->stream), FALSE);
93
94     mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
95
96     mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
97
98     mctx->cryptoctx = cryptoctx;
99     mctx->decrypt = decrypt;
100
101     /* Create the root node */
102     root->part = GMIME_OBJECT (mctx->mime_message);
103     root->envelope_file = message;
104     root->nchildren = 1;
105     root->ctx = mctx;
106
107     *root_out = root;
108     return NOTMUCH_STATUS_SUCCESS;
109
110 DONE:
111     talloc_free (root);
112     return status;
113 }
114
115 static int
116 _signature_validity_free (GMimeSignatureValidity **proxy)
117 {
118     g_mime_signature_validity_free (*proxy);
119     return 0;
120 }
121
122 static mime_node_t *
123 _mime_node_create (const mime_node_t *parent, GMimeObject *part)
124 {
125     mime_node_t *node = talloc_zero (parent, mime_node_t);
126     GError *err = NULL;
127
128     /* Set basic node properties */
129     node->part = part;
130     node->ctx = parent->ctx;
131     if (!talloc_reference (node, node->ctx)) {
132         fprintf (stderr, "Out of memory.\n");
133         talloc_free (node);
134         return NULL;
135     }
136
137     /* Deal with the different types of parts */
138     if (GMIME_IS_PART (part)) {
139         node->nchildren = 0;
140     } else if (GMIME_IS_MULTIPART (part)) {
141         node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
142     } else if (GMIME_IS_MESSAGE_PART (part)) {
143         /* Promote part to an envelope and open it */
144         GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
145         GMimeMessage *message = g_mime_message_part_get_message (message_part);
146         node->envelope_part = message_part;
147         node->part = GMIME_OBJECT (message);
148         node->nchildren = 1;
149     } else {
150         fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
151                  g_type_name (G_OBJECT_TYPE (part)));
152         talloc_free (node);
153         return NULL;
154     }
155
156     /* Handle PGP/MIME parts */
157     if (GMIME_IS_MULTIPART_ENCRYPTED (part)
158         && node->ctx->cryptoctx && node->ctx->decrypt) {
159         if (node->nchildren != 2) {
160             /* this violates RFC 3156 section 4, so we won't bother with it. */
161             fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
162                      "message (must be exactly 2)\n",
163                      node->nchildren);
164         } else {
165             GMimeMultipartEncrypted *encrypteddata =
166                 GMIME_MULTIPART_ENCRYPTED (part);
167             node->decrypt_attempted = TRUE;
168             node->decrypted_child = g_mime_multipart_encrypted_decrypt
169                 (encrypteddata, node->ctx->cryptoctx, &err);
170             if (node->decrypted_child) {
171                 node->decrypt_success = node->verify_attempted = TRUE;
172                 node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
173             } else {
174                 fprintf (stderr, "Failed to decrypt part: %s\n",
175                          (err ? err->message : "no error explanation given"));
176             }
177         }
178     } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->cryptoctx) {
179         if (node->nchildren != 2) {
180             /* this violates RFC 3156 section 5, so we won't bother with it. */
181             fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
182                      "(must be exactly 2)\n",
183                      node->nchildren);
184         } else {
185             /* For some reason the GMimeSignatureValidity returned
186              * here is not a const (inconsistent with that
187              * returned by
188              * g_mime_multipart_encrypted_get_signature_validity,
189              * and therefore needs to be properly disposed of.
190              *
191              * In GMime 2.6, they're both non-const, so we'll be able
192              * to clean up this asymmetry. */
193             GMimeSignatureValidity *sig_validity = g_mime_multipart_signed_verify
194                 (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);
195             node->verify_attempted = TRUE;
196             node->sig_validity = sig_validity;
197             if (sig_validity) {
198                 GMimeSignatureValidity **proxy =
199                     talloc (node, GMimeSignatureValidity *);
200                 *proxy = sig_validity;
201                 talloc_set_destructor (proxy, _signature_validity_free);
202             }
203         }
204     }
205
206     if (node->verify_attempted && !node->sig_validity)
207         fprintf (stderr, "Failed to verify signed part: %s\n",
208                  (err ? err->message : "no error explanation given"));
209
210     if (err)
211         g_error_free (err);
212
213     return node;
214 }
215
216 mime_node_t *
217 mime_node_child (const mime_node_t *parent, int child)
218 {
219     GMimeObject *sub;
220
221     if (!parent || child < 0 || child >= parent->nchildren)
222         return NULL;
223
224     if (GMIME_IS_MULTIPART (parent->part)) {
225         if (child == 1 && parent->decrypted_child)
226             sub = parent->decrypted_child;
227         else
228             sub = g_mime_multipart_get_part
229                 (GMIME_MULTIPART (parent->part), child);
230     } else if (GMIME_IS_MESSAGE (parent->part)) {
231         sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
232     } else {
233         /* This should have been caught by message_part_create */
234         INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
235                         g_type_name (G_OBJECT_TYPE (parent->part)));
236     }
237     return _mime_node_create (parent, sub);
238 }
239
240 static mime_node_t *
241 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
242 {
243     mime_node_t *ret = NULL;
244     int i;
245
246     if (*n == 0)
247         return node;
248
249     *n -= 1;
250     for (i = 0; i < node->nchildren && !ret; i++) {
251         mime_node_t *child = mime_node_child (node, i);
252         ret = _mime_node_seek_dfs_walk (child, n);
253         if (!ret)
254             talloc_free (child);
255     }
256     return ret;
257 }
258
259 mime_node_t *
260 mime_node_seek_dfs (mime_node_t *node, int n)
261 {
262     if (n < 0)
263         return NULL;
264     return _mime_node_seek_dfs_walk (node, &n);
265 }