]> git.notmuchmail.org Git - notmuch/blob - mime-node.c
lib: support user prefix names in term generation
[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 https://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 <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 #include "notmuch-client.h"
29
30 /* Context that gets inherited from the root node. */
31 typedef struct mime_node_context {
32     /* Per-message resources.  These are allocated internally and must
33      * be destroyed. */
34     GMimeStream *stream;
35     GMimeParser *parser;
36     GMimeMessage *mime_message;
37
38     /* Context provided by the caller. */
39     _notmuch_crypto_t *crypto;
40 } mime_node_context_t;
41
42 static int
43 _mime_node_context_free (mime_node_context_t *res)
44 {
45     if (res->mime_message)
46         g_object_unref (res->mime_message);
47
48     if (res->parser)
49         g_object_unref (res->parser);
50
51     if (res->stream)
52         g_object_unref (res->stream);
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     int fd;
66
67     root = talloc_zero (ctx, mime_node_t);
68     if (root == NULL) {
69         fprintf (stderr, "Out of memory.\n");
70         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
71         goto DONE;
72     }
73
74     /* Create the tree-wide context */
75     mctx = talloc_zero (root, mime_node_context_t);
76     if (mctx == NULL) {
77         fprintf (stderr, "Out of memory.\n");
78         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
79         goto DONE;
80     }
81     talloc_set_destructor (mctx, _mime_node_context_free);
82
83     /* Fast path */
84     fd = open (filename, O_RDONLY);
85     if (fd == -1) {
86         /* Slow path - for some reason the first file in the list is
87          * not available anymore. This is clearly a problem in the
88          * database, but we are not going to let this problem be a
89          * show stopper */
90         notmuch_filenames_t *filenames;
91         for (filenames = notmuch_message_get_filenames (message);
92              notmuch_filenames_valid (filenames);
93              notmuch_filenames_move_to_next (filenames))
94         {
95             filename = notmuch_filenames_get (filenames);
96             fd = open (filename, O_RDONLY);
97             if (fd != -1)
98                 break;
99         }
100
101         talloc_free (filenames);
102         if (fd == -1) {
103             /* Give up */
104             fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
105                 status = NOTMUCH_STATUS_FILE_ERROR;
106                 goto DONE;
107             }
108         }
109
110     mctx->stream = g_mime_stream_gzfile_new (fd);
111     if (!mctx->stream) {
112         fprintf (stderr, "Out of memory.\n");
113         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
114         goto DONE;
115     }
116
117     mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
118     if (!mctx->parser) {
119         fprintf (stderr, "Out of memory.\n");
120         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
121         goto DONE;
122     }
123
124     mctx->mime_message = g_mime_parser_construct_message (mctx->parser, NULL);
125     if (!mctx->mime_message) {
126         fprintf (stderr, "Failed to parse %s\n", filename);
127         status = NOTMUCH_STATUS_FILE_ERROR;
128         goto DONE;
129     }
130
131     mctx->crypto = crypto;
132
133     /* Create the root node */
134     root->part = GMIME_OBJECT (mctx->mime_message);
135     root->envelope_file = message;
136     root->nchildren = 1;
137     root->ctx = mctx;
138
139     root->parent = NULL;
140     root->part_num = 0;
141     root->next_child = 0;
142     root->next_part_num = 1;
143
144     *root_out = root;
145     return NOTMUCH_STATUS_SUCCESS;
146
147 DONE:
148     talloc_free (root);
149     return status;
150 }
151
152 /* Signature list destructor */
153 static int
154 _signature_list_free (GMimeSignatureList **proxy)
155 {
156     g_object_unref (*proxy);
157     return 0;
158 }
159
160 /* Set up signature list destructor */
161 static void
162 set_signature_list_destructor (mime_node_t *node)
163 {
164     GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
165     if (proxy) {
166         *proxy = node->sig_list;
167         talloc_set_destructor (proxy, _signature_list_free);
168     }
169 }
170
171 /* Verify a signed mime node */
172 static void
173 node_verify (mime_node_t *node, GMimeObject *part)
174 {
175     GError *err = NULL;
176
177     node->verify_attempted = true;
178     node->sig_list = g_mime_multipart_signed_verify
179         (GMIME_MULTIPART_SIGNED (part), GMIME_ENCRYPT_NONE, &err);
180
181     if (node->sig_list)
182         set_signature_list_destructor (node);
183     else
184         fprintf (stderr, "Failed to verify signed part: %s\n",
185                  err ? err->message : "no error explanation given");
186
187     if (err)
188         g_error_free (err);
189 }
190
191 /* Decrypt and optionally verify an encrypted mime node */
192 static void
193 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part)
194 {
195     GError *err = NULL;
196     GMimeDecryptResult *decrypt_result = NULL;
197     GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
198     notmuch_message_t *message = NULL;
199
200     if (! node->decrypted_child) {
201         for (mime_node_t *parent = node; parent; parent = parent->parent)
202             if (parent->envelope_file) {
203                 message = parent->envelope_file;
204                 break;
205             }
206
207         node->decrypted_child = _notmuch_crypto_decrypt (&node->decrypt_attempted,
208                                                          node->ctx->crypto->decrypt,
209                                                          message,
210                                                          encrypteddata, &decrypt_result, &err);
211     }
212     if (! node->decrypted_child) {
213         fprintf (stderr, "Failed to decrypt part: %s\n",
214                  err ? err->message : "no error explanation given");
215         goto DONE;
216     }
217
218     node->decrypt_success = true;
219
220     if (decrypt_result) {
221         /* This may be NULL if the part is not signed. */
222         node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
223         if (node->sig_list) {
224             node->verify_attempted = true;
225             g_object_ref (node->sig_list);
226             set_signature_list_destructor (node);
227         }
228
229         if (node->ctx->crypto->decrypt == NOTMUCH_DECRYPT_TRUE && message) {
230             notmuch_database_t *db = notmuch_message_get_database (message);
231             const char *session_key = g_mime_decrypt_result_get_session_key (decrypt_result);
232             if (db && session_key)
233                 print_status_message ("Failed to stash session key in the database",
234                                       message,
235                                       notmuch_message_add_property (message, "session-key",
236                                                                     session_key));
237         }
238         g_object_unref (decrypt_result);
239     }
240
241  DONE:
242     if (err)
243         g_error_free (err);
244 }
245
246 static mime_node_t *
247 _mime_node_create (mime_node_t *parent, GMimeObject *part)
248 {
249     mime_node_t *node = talloc_zero (parent, mime_node_t);
250
251     /* Set basic node properties */
252     node->part = part;
253     node->ctx = parent->ctx;
254     if (!talloc_reference (node, node->ctx)) {
255         fprintf (stderr, "Out of memory.\n");
256         talloc_free (node);
257         return NULL;
258     }
259     node->parent = parent;
260     node->part_num = node->next_part_num = -1;
261     node->next_child = 0;
262
263     /* Deal with the different types of parts */
264     if (GMIME_IS_PART (part)) {
265         node->nchildren = 0;
266     } else if (GMIME_IS_MULTIPART (part)) {
267         node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
268     } else if (GMIME_IS_MESSAGE_PART (part)) {
269         /* Promote part to an envelope and open it */
270         GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
271         GMimeMessage *message = g_mime_message_part_get_message (message_part);
272         node->envelope_part = message_part;
273         node->part = GMIME_OBJECT (message);
274         node->nchildren = 1;
275     } else {
276         fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
277                  g_type_name (G_OBJECT_TYPE (part)));
278         talloc_free (node);
279         return NULL;
280     }
281
282     /* Handle PGP/MIME parts */
283     if (GMIME_IS_MULTIPART_ENCRYPTED (part) && (node->ctx->crypto->decrypt != NOTMUCH_DECRYPT_FALSE)) {
284         if (node->nchildren != 2) {
285             /* this violates RFC 3156 section 4, so we won't bother with it. */
286             fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
287                      "message (must be exactly 2)\n",
288                      node->nchildren);
289         } else {
290             node_decrypt_and_verify (node, part);
291         }
292     } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify) {
293         if (node->nchildren != 2) {
294             /* this violates RFC 3156 section 5, so we won't bother with it. */
295             fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
296                      "(must be exactly 2)\n",
297                      node->nchildren);
298         } else {
299             node_verify (node, part);
300         }
301     }
302
303     return node;
304 }
305
306 mime_node_t *
307 mime_node_child (mime_node_t *parent, int child)
308 {
309     GMimeObject *sub;
310     mime_node_t *node;
311
312     if (!parent || !parent->part || child < 0 || child >= parent->nchildren)
313         return NULL;
314
315     if (GMIME_IS_MULTIPART (parent->part)) {
316         if (child == 1 && parent->decrypted_child)
317             sub = parent->decrypted_child;
318         else
319             sub = g_mime_multipart_get_part
320                 (GMIME_MULTIPART (parent->part), child);
321     } else if (GMIME_IS_MESSAGE (parent->part)) {
322         sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
323     } else {
324         /* This should have been caught by message_part_create */
325         INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
326                         g_type_name (G_OBJECT_TYPE (parent->part)));
327     }
328     node = _mime_node_create (parent, sub);
329
330     if (child == parent->next_child && parent->next_part_num != -1) {
331         /* We're traversing in depth-first order.  Record the child's
332          * depth-first numbering. */
333         node->part_num = parent->next_part_num;
334         node->next_part_num = node->part_num + 1;
335
336         /* Prepare the parent for its next depth-first child. */
337         parent->next_child++;
338         parent->next_part_num = -1;
339
340         if (node->nchildren == 0) {
341             /* We've reached a leaf, so find the parent that has more
342              * children and set it up to number its next child. */
343             mime_node_t *iter = node->parent;
344             while (iter && iter->next_child == iter->nchildren)
345                 iter = iter->parent;
346             if (iter)
347                 iter->next_part_num = node->part_num + 1;
348         }
349     }
350
351     return node;
352 }
353
354 static mime_node_t *
355 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
356 {
357     int i;
358
359     if (*n == 0)
360         return node;
361
362     *n -= 1;
363     for (i = 0; i < node->nchildren; i++) {
364         mime_node_t *child = mime_node_child (node, i);
365         mime_node_t *ret = _mime_node_seek_dfs_walk (child, n);
366         if (ret)
367             return ret;
368
369         talloc_free (child);
370     }
371     return NULL;
372 }
373
374 mime_node_t *
375 mime_node_seek_dfs (mime_node_t *node, int n)
376 {
377     if (n < 0)
378         return NULL;
379     return _mime_node_seek_dfs_walk (node, &n);
380 }