1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
4 * Copyright © 2009 Keith Packard
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.
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.
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/ .
19 * Authors: Carl Worth <cworth@cworth.org>
20 * Keith Packard <keithp@keithp.com>
21 * Austin Clements <aclements@csail.mit.edu>
24 #include <sys/types.h>
28 #include "notmuch-client.h"
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
36 GMimeMessage *mime_message;
37 _notmuch_message_crypto_t *msg_crypto;
39 /* Context provided by the caller. */
40 _notmuch_crypto_t *crypto;
41 } mime_node_context_t;
44 _mime_node_context_free (mime_node_context_t *res)
46 if (res->mime_message)
47 g_object_unref (res->mime_message);
50 g_object_unref (res->parser);
53 g_object_unref (res->stream);
58 const _notmuch_message_crypto_t*
59 mime_node_get_message_crypto_status (mime_node_t *node)
61 return node->ctx->msg_crypto;
65 mime_node_open (const void *ctx, notmuch_message_t *message,
66 _notmuch_crypto_t *crypto, mime_node_t **root_out)
68 const char *filename = notmuch_message_get_filename (message);
69 mime_node_context_t *mctx;
71 notmuch_status_t status;
74 root = talloc_zero (ctx, mime_node_t);
76 fprintf (stderr, "Out of memory.\n");
77 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
81 /* Create the tree-wide context */
82 mctx = talloc_zero (root, mime_node_context_t);
84 fprintf (stderr, "Out of memory.\n");
85 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
88 talloc_set_destructor (mctx, _mime_node_context_free);
91 fd = open (filename, O_RDONLY);
93 /* Slow path - for some reason the first file in the list is
94 * not available anymore. This is clearly a problem in the
95 * database, but we are not going to let this problem be a
97 notmuch_filenames_t *filenames;
98 for (filenames = notmuch_message_get_filenames (message);
99 notmuch_filenames_valid (filenames);
100 notmuch_filenames_move_to_next (filenames))
102 filename = notmuch_filenames_get (filenames);
103 fd = open (filename, O_RDONLY);
108 talloc_free (filenames);
111 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
112 status = NOTMUCH_STATUS_FILE_ERROR;
117 mctx->stream = g_mime_stream_gzfile_new (fd);
119 fprintf (stderr, "Out of memory.\n");
120 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
124 mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
126 fprintf (stderr, "Out of memory.\n");
127 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
131 mctx->mime_message = g_mime_parser_construct_message (mctx->parser, NULL);
132 if (!mctx->mime_message) {
133 fprintf (stderr, "Failed to parse %s\n", filename);
134 status = NOTMUCH_STATUS_FILE_ERROR;
138 mctx->msg_crypto = _notmuch_message_crypto_new (mctx);
140 mctx->crypto = crypto;
142 /* Create the root node */
143 root->part = GMIME_OBJECT (mctx->mime_message);
144 root->envelope_file = message;
150 root->next_child = 0;
151 root->next_part_num = 1;
154 return NOTMUCH_STATUS_SUCCESS;
161 /* Signature list destructor */
163 _signature_list_free (GMimeSignatureList **proxy)
165 g_object_unref (*proxy);
169 /* Set up signature list destructor */
171 set_signature_list_destructor (mime_node_t *node)
173 GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
175 *proxy = node->sig_list;
176 talloc_set_destructor (proxy, _signature_list_free);
180 /* Verify a signed mime node */
182 node_verify (mime_node_t *node, GMimeObject *part)
185 notmuch_status_t status;
187 node->verify_attempted = true;
188 node->sig_list = g_mime_multipart_signed_verify
189 (GMIME_MULTIPART_SIGNED (part), GMIME_ENCRYPT_NONE, &err);
192 set_signature_list_destructor (node);
194 fprintf (stderr, "Failed to verify signed part: %s\n",
195 err ? err->message : "no error explanation given");
200 status = _notmuch_message_crypto_potential_sig_list (node->ctx->msg_crypto, node->sig_list);
201 if (status) /* this is a warning, not an error */
202 fprintf (stderr, "Warning: failed to note signature status: %s.\n", notmuch_status_to_string (status));
205 /* Decrypt and optionally verify an encrypted mime node */
207 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part)
210 GMimeDecryptResult *decrypt_result = NULL;
211 notmuch_status_t status;
212 GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
213 notmuch_message_t *message = NULL;
215 if (! node->decrypted_child) {
216 for (mime_node_t *parent = node; parent; parent = parent->parent)
217 if (parent->envelope_file) {
218 message = parent->envelope_file;
222 node->decrypted_child = _notmuch_crypto_decrypt (&node->decrypt_attempted,
223 node->ctx->crypto->decrypt,
225 encrypteddata, &decrypt_result, &err);
227 if (! node->decrypted_child) {
228 fprintf (stderr, "Failed to decrypt part: %s\n",
229 err ? err->message : "no error explanation given");
233 node->decrypt_success = true;
234 status = _notmuch_message_crypto_successful_decryption (node->ctx->msg_crypto);
235 if (status) /* this is a warning, not an error */
236 fprintf (stderr, "Warning: failed to note decryption status: %s.\n", notmuch_status_to_string (status));
238 if (decrypt_result) {
239 /* This may be NULL if the part is not signed. */
240 node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
241 if (node->sig_list) {
242 node->verify_attempted = true;
243 g_object_ref (node->sig_list);
244 set_signature_list_destructor (node);
245 status = _notmuch_message_crypto_potential_sig_list (node->ctx->msg_crypto, node->sig_list);
246 if (status) /* this is a warning, not an error */
247 fprintf (stderr, "Warning: failed to note signature status: %s.\n", notmuch_status_to_string (status));
250 if (node->ctx->crypto->decrypt == NOTMUCH_DECRYPT_TRUE && message) {
251 notmuch_database_t *db = notmuch_message_get_database (message);
252 const char *session_key = g_mime_decrypt_result_get_session_key (decrypt_result);
253 if (db && session_key)
254 print_status_message ("Failed to stash session key in the database",
256 notmuch_message_add_property (message, "session-key",
259 g_object_unref (decrypt_result);
268 _mime_node_create (mime_node_t *parent, GMimeObject *part, int numchild)
270 mime_node_t *node = talloc_zero (parent, mime_node_t);
271 notmuch_status_t status;
273 /* Set basic node properties */
275 node->ctx = parent->ctx;
276 if (!talloc_reference (node, node->ctx)) {
277 fprintf (stderr, "Out of memory.\n");
281 node->parent = parent;
282 node->part_num = node->next_part_num = -1;
283 node->next_child = 0;
285 /* Deal with the different types of parts */
286 if (GMIME_IS_PART (part)) {
288 } else if (GMIME_IS_MULTIPART (part)) {
289 node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
290 } else if (GMIME_IS_MESSAGE_PART (part)) {
291 /* Promote part to an envelope and open it */
292 GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
293 GMimeMessage *message = g_mime_message_part_get_message (message_part);
294 node->envelope_part = message_part;
295 node->part = GMIME_OBJECT (message);
298 fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
299 g_type_name (G_OBJECT_TYPE (part)));
304 /* Handle PGP/MIME parts */
305 if (GMIME_IS_MULTIPART_ENCRYPTED (part) && (node->ctx->crypto->decrypt != NOTMUCH_DECRYPT_FALSE)) {
306 if (node->nchildren != 2) {
307 /* this violates RFC 3156 section 4, so we won't bother with it. */
308 fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
309 "message (must be exactly 2)\n",
312 node_decrypt_and_verify (node, part);
314 } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify) {
315 if (node->nchildren != 2) {
316 /* this violates RFC 3156 section 5, so we won't bother with it. */
317 fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
318 "(must be exactly 2)\n",
321 node_verify (node, part);
324 status = _notmuch_message_crypto_potential_payload (node->ctx->msg_crypto, part, parent ? parent->part : NULL, numchild);
326 fprintf (stderr, "Warning: failed to record potential crypto payload (%s).\n", notmuch_status_to_string (status));
333 mime_node_child (mime_node_t *parent, int child)
338 if (!parent || !parent->part || child < 0 || child >= parent->nchildren)
341 if (GMIME_IS_MULTIPART (parent->part)) {
342 if (child == GMIME_MULTIPART_ENCRYPTED_CONTENT && parent->decrypted_child)
343 sub = parent->decrypted_child;
345 sub = g_mime_multipart_get_part
346 (GMIME_MULTIPART (parent->part), child);
347 } else if (GMIME_IS_MESSAGE (parent->part)) {
348 sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
350 /* This should have been caught by message_part_create */
351 INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
352 g_type_name (G_OBJECT_TYPE (parent->part)));
354 node = _mime_node_create (parent, sub, child);
356 if (child == parent->next_child && parent->next_part_num != -1) {
357 /* We're traversing in depth-first order. Record the child's
358 * depth-first numbering. */
359 node->part_num = parent->next_part_num;
360 node->next_part_num = node->part_num + 1;
362 /* Prepare the parent for its next depth-first child. */
363 parent->next_child++;
364 parent->next_part_num = -1;
366 if (node->nchildren == 0) {
367 /* We've reached a leaf, so find the parent that has more
368 * children and set it up to number its next child. */
369 mime_node_t *iter = node->parent;
370 while (iter && iter->next_child == iter->nchildren)
373 iter->next_part_num = node->part_num + 1;
381 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
389 for (i = 0; i < node->nchildren; i++) {
390 mime_node_t *child = mime_node_child (node, i);
391 mime_node_t *ret = _mime_node_seek_dfs_walk (child, n);
401 mime_node_seek_dfs (mime_node_t *node, int n)
405 return _mime_node_seek_dfs_walk (node, &n);