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 "notmuch-client.h"
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
33 GMimeMessage *mime_message;
35 /* Context provided by the caller. */
36 _notmuch_crypto_t *crypto;
37 } mime_node_context_t;
40 _mime_node_context_free (mime_node_context_t *res)
42 if (res->mime_message)
43 g_object_unref (res->mime_message);
46 g_object_unref (res->parser);
49 g_object_unref (res->stream);
58 mime_node_open (const void *ctx, notmuch_message_t *message,
59 _notmuch_crypto_t *crypto, mime_node_t **root_out)
61 const char *filename = notmuch_message_get_filename (message);
62 mime_node_context_t *mctx;
64 notmuch_status_t status;
66 root = talloc_zero (ctx, mime_node_t);
68 fprintf (stderr, "Out of memory.\n");
69 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
73 /* Create the tree-wide context */
74 mctx = talloc_zero (root, mime_node_context_t);
76 fprintf (stderr, "Out of memory.\n");
77 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
80 talloc_set_destructor (mctx, _mime_node_context_free);
83 mctx->file = fopen (filename, "r");
85 /* Slow path - for some reason the first file in the list is
86 * not available anymore. This is clearly a problem in the
87 * database, but we are not going to let this problem be a
89 notmuch_filenames_t *filenames;
90 for (filenames = notmuch_message_get_filenames (message);
91 notmuch_filenames_valid (filenames);
92 notmuch_filenames_move_to_next (filenames))
94 filename = notmuch_filenames_get (filenames);
95 mctx->file = fopen (filename, "r");
100 talloc_free (filenames);
103 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
104 status = NOTMUCH_STATUS_FILE_ERROR;
109 mctx->stream = g_mime_stream_file_new (mctx->file);
111 fprintf (stderr, "Out of memory.\n");
112 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
115 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (mctx->stream), false);
117 mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
119 fprintf (stderr, "Out of memory.\n");
120 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
124 mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
125 if (!mctx->mime_message) {
126 fprintf (stderr, "Failed to parse %s\n", filename);
127 status = NOTMUCH_STATUS_FILE_ERROR;
131 mctx->crypto = crypto;
133 /* Create the root node */
134 root->part = GMIME_OBJECT (mctx->mime_message);
135 root->envelope_file = message;
141 root->next_child = 0;
142 root->next_part_num = 1;
145 return NOTMUCH_STATUS_SUCCESS;
152 /* Signature list destructor (GMime 2.6) */
154 _signature_list_free (GMimeSignatureList **proxy)
156 g_object_unref (*proxy);
160 /* Set up signature list destructor (GMime 2.6) */
162 set_signature_list_destructor (mime_node_t *node)
164 GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
166 *proxy = node->sig_list;
167 talloc_set_destructor (proxy, _signature_list_free);
171 /* Verify a signed mime node (GMime 2.6) */
173 node_verify (mime_node_t *node, GMimeObject *part,
174 g_mime_3_unused(GMimeCryptoContext *cryptoctx))
178 node->verify_attempted = true;
179 node->sig_list = g_mime_multipart_signed_verify
180 (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);
183 set_signature_list_destructor (node);
185 fprintf (stderr, "Failed to verify signed part: %s\n",
186 err ? err->message : "no error explanation given");
192 /* Decrypt and optionally verify an encrypted mime node (GMime 2.6) */
194 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
195 g_mime_3_unused(GMimeCryptoContext *cryptoctx))
198 GMimeDecryptResult *decrypt_result = NULL;
199 GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
200 notmuch_message_t *message = NULL;
202 if (! node->decrypted_child) {
203 for (mime_node_t *parent = node; parent; parent = parent->parent)
204 if (parent->envelope_file) {
205 message = parent->envelope_file;
209 node->decrypted_child = _notmuch_crypto_decrypt (&node->decrypt_attempted,
210 node->ctx->crypto->decrypt,
212 cryptoctx, encrypteddata, &decrypt_result, &err);
214 if (! node->decrypted_child) {
215 fprintf (stderr, "Failed to decrypt part: %s\n",
216 err ? err->message : "no error explanation given");
220 node->decrypt_success = true;
221 node->verify_attempted = true;
223 if (decrypt_result) {
224 /* This may be NULL if the part is not signed. */
225 node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
226 if (node->sig_list) {
227 g_object_ref (node->sig_list);
228 set_signature_list_destructor (node);
231 #if HAVE_GMIME_SESSION_KEYS
232 if (node->ctx->crypto->decrypt == NOTMUCH_DECRYPT_TRUE && message) {
233 notmuch_database_t *db = notmuch_message_get_database (message);
234 const char *session_key = g_mime_decrypt_result_get_session_key (decrypt_result);
235 if (db && session_key)
236 print_status_message ("Failed to stash session key in the database",
238 notmuch_message_add_property (message, "session-key",
242 g_object_unref (decrypt_result);
251 _mime_node_create (mime_node_t *parent, GMimeObject *part)
253 mime_node_t *node = talloc_zero (parent, mime_node_t);
254 GMimeCryptoContext *cryptoctx = NULL;
256 /* Set basic node properties */
258 node->ctx = parent->ctx;
259 if (!talloc_reference (node, node->ctx)) {
260 fprintf (stderr, "Out of memory.\n");
264 node->parent = parent;
265 node->part_num = node->next_part_num = -1;
266 node->next_child = 0;
268 /* Deal with the different types of parts */
269 if (GMIME_IS_PART (part)) {
271 } else if (GMIME_IS_MULTIPART (part)) {
272 node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
273 } else if (GMIME_IS_MESSAGE_PART (part)) {
274 /* Promote part to an envelope and open it */
275 GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
276 GMimeMessage *message = g_mime_message_part_get_message (message_part);
277 node->envelope_part = message_part;
278 node->part = GMIME_OBJECT (message);
281 fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
282 g_type_name (G_OBJECT_TYPE (part)));
287 #if (GMIME_MAJOR_VERSION < 3)
288 if ((GMIME_IS_MULTIPART_ENCRYPTED (part) && (node->ctx->crypto->decrypt != NOTMUCH_DECRYPT_FALSE))
289 || (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) {
290 GMimeContentType *content_type = g_mime_object_get_content_type (part);
291 const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol");
292 notmuch_status_t status;
293 status = _notmuch_crypto_get_gmime_ctx_for_protocol (node->ctx->crypto,
294 protocol, &cryptoctx);
295 if (status) /* this is a warning, not an error */
296 fprintf (stderr, "Warning: %s (%s).\n", notmuch_status_to_string (status),
297 protocol ? protocol : "NULL");
303 /* Handle PGP/MIME parts */
304 if (GMIME_IS_MULTIPART_ENCRYPTED (part) && (node->ctx->crypto->decrypt != NOTMUCH_DECRYPT_FALSE)) {
305 if (node->nchildren != 2) {
306 /* this violates RFC 3156 section 4, so we won't bother with it. */
307 fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
308 "message (must be exactly 2)\n",
311 node_decrypt_and_verify (node, part, cryptoctx);
313 } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify) {
314 if (node->nchildren != 2) {
315 /* this violates RFC 3156 section 5, so we won't bother with it. */
316 fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
317 "(must be exactly 2)\n",
320 node_verify (node, part, cryptoctx);
328 mime_node_child (mime_node_t *parent, int child)
333 if (!parent || !parent->part || child < 0 || child >= parent->nchildren)
336 if (GMIME_IS_MULTIPART (parent->part)) {
337 if (child == 1 && parent->decrypted_child)
338 sub = parent->decrypted_child;
340 sub = g_mime_multipart_get_part
341 (GMIME_MULTIPART (parent->part), child);
342 } else if (GMIME_IS_MESSAGE (parent->part)) {
343 sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
345 /* This should have been caught by message_part_create */
346 INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
347 g_type_name (G_OBJECT_TYPE (parent->part)));
349 node = _mime_node_create (parent, sub);
351 if (child == parent->next_child && parent->next_part_num != -1) {
352 /* We're traversing in depth-first order. Record the child's
353 * depth-first numbering. */
354 node->part_num = parent->next_part_num;
355 node->next_part_num = node->part_num + 1;
357 /* Prepare the parent for its next depth-first child. */
358 parent->next_child++;
359 parent->next_part_num = -1;
361 if (node->nchildren == 0) {
362 /* We've reached a leaf, so find the parent that has more
363 * children and set it up to number its next child. */
364 mime_node_t *iter = node->parent;
365 while (iter && iter->next_child == iter->nchildren)
368 iter->next_part_num = node->part_num + 1;
376 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
384 for (i = 0; i < node->nchildren; i++) {
385 mime_node_t *child = mime_node_child (node, i);
386 mime_node_t *ret = _mime_node_seek_dfs_walk (child, n);
396 mime_node_seek_dfs (mime_node_t *node, int n)
400 return _mime_node_seek_dfs_walk (node, &n);