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 http://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_context_t *cryptoctx;
37 notmuch_bool_t decrypt;
38 } mime_node_context_t;
41 _mime_node_context_free (mime_node_context_t *res)
43 if (res->mime_message)
44 g_object_unref (res->mime_message);
47 g_object_unref (res->parser);
50 g_object_unref (res->stream);
59 mime_node_open (const void *ctx, notmuch_message_t *message,
60 notmuch_crypto_t *crypto, mime_node_t **root_out)
62 const char *filename = notmuch_message_get_filename (message);
63 mime_node_context_t *mctx;
65 notmuch_status_t status;
67 root = talloc_zero (ctx, mime_node_t);
69 fprintf (stderr, "Out of memory.\n");
70 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
74 /* Create the tree-wide context */
75 mctx = talloc_zero (root, mime_node_context_t);
77 fprintf (stderr, "Out of memory.\n");
78 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
81 talloc_set_destructor (mctx, _mime_node_context_free);
83 mctx->file = fopen (filename, "r");
85 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
86 status = NOTMUCH_STATUS_FILE_ERROR;
90 mctx->stream = g_mime_stream_file_new (mctx->file);
92 fprintf (stderr, "Out of memory.\n");
93 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
96 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (mctx->stream), FALSE);
98 mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
100 fprintf (stderr, "Out of memory.\n");
101 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
105 mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
106 if (!mctx->mime_message) {
107 fprintf (stderr, "Failed to parse %s\n", filename);
108 status = NOTMUCH_STATUS_FILE_ERROR;
112 mctx->cryptoctx = crypto->gpgctx;
113 mctx->decrypt = crypto->decrypt;
115 /* Create the root node */
116 root->part = GMIME_OBJECT (mctx->mime_message);
117 root->envelope_file = message;
123 root->next_child = 0;
124 root->next_part_num = 1;
127 return NOTMUCH_STATUS_SUCCESS;
134 #ifdef GMIME_ATLEAST_26
136 _signature_list_free (GMimeSignatureList **proxy)
138 g_object_unref (*proxy);
143 _signature_validity_free (GMimeSignatureValidity **proxy)
145 g_mime_signature_validity_free (*proxy);
151 _mime_node_create (mime_node_t *parent, GMimeObject *part)
153 mime_node_t *node = talloc_zero (parent, mime_node_t);
156 /* Set basic node properties */
158 node->ctx = parent->ctx;
159 if (!talloc_reference (node, node->ctx)) {
160 fprintf (stderr, "Out of memory.\n");
164 node->parent = parent;
165 node->part_num = node->next_part_num = -1;
166 node->next_child = 0;
168 /* Deal with the different types of parts */
169 if (GMIME_IS_PART (part)) {
171 } else if (GMIME_IS_MULTIPART (part)) {
172 node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
173 } else if (GMIME_IS_MESSAGE_PART (part)) {
174 /* Promote part to an envelope and open it */
175 GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
176 GMimeMessage *message = g_mime_message_part_get_message (message_part);
177 node->envelope_part = message_part;
178 node->part = GMIME_OBJECT (message);
181 fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
182 g_type_name (G_OBJECT_TYPE (part)));
187 /* Handle PGP/MIME parts */
188 if (GMIME_IS_MULTIPART_ENCRYPTED (part)
189 && node->ctx->cryptoctx && node->ctx->decrypt) {
190 if (node->nchildren != 2) {
191 /* this violates RFC 3156 section 4, so we won't bother with it. */
192 fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
193 "message (must be exactly 2)\n",
196 GMimeMultipartEncrypted *encrypteddata =
197 GMIME_MULTIPART_ENCRYPTED (part);
198 node->decrypt_attempted = TRUE;
199 #ifdef GMIME_ATLEAST_26
200 GMimeDecryptResult *decrypt_result = NULL;
201 node->decrypted_child = g_mime_multipart_encrypted_decrypt
202 (encrypteddata, node->ctx->cryptoctx, &decrypt_result, &err);
204 node->decrypted_child = g_mime_multipart_encrypted_decrypt
205 (encrypteddata, node->ctx->cryptoctx, &err);
207 if (node->decrypted_child) {
208 node->decrypt_success = node->verify_attempted = TRUE;
209 #ifdef GMIME_ATLEAST_26
210 /* This may be NULL if the part is not signed. */
211 node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
213 g_object_ref (node->sig_list);
214 g_object_unref (decrypt_result);
216 node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
219 fprintf (stderr, "Failed to decrypt part: %s\n",
220 (err ? err->message : "no error explanation given"));
223 } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->cryptoctx) {
224 if (node->nchildren != 2) {
225 /* this violates RFC 3156 section 5, so we won't bother with it. */
226 fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
227 "(must be exactly 2)\n",
230 #ifdef GMIME_ATLEAST_26
231 node->sig_list = g_mime_multipart_signed_verify
232 (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);
233 node->verify_attempted = TRUE;
236 fprintf (stderr, "Failed to verify signed part: %s\n",
237 (err ? err->message : "no error explanation given"));
239 /* For some reason the GMimeSignatureValidity returned
240 * here is not a const (inconsistent with that
242 * g_mime_multipart_encrypted_get_signature_validity,
243 * and therefore needs to be properly disposed of.
245 * In GMime 2.6, they're both non-const, so we'll be able
246 * to clean up this asymmetry. */
247 GMimeSignatureValidity *sig_validity = g_mime_multipart_signed_verify
248 (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);
249 node->verify_attempted = TRUE;
250 node->sig_validity = sig_validity;
252 GMimeSignatureValidity **proxy =
253 talloc (node, GMimeSignatureValidity *);
254 *proxy = sig_validity;
255 talloc_set_destructor (proxy, _signature_validity_free);
261 #ifdef GMIME_ATLEAST_26
262 /* sig_list may be created in both above cases, so we need to
263 * cleanly handle it here. */
264 if (node->sig_list) {
265 GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
266 *proxy = node->sig_list;
267 talloc_set_destructor (proxy, _signature_list_free);
271 #ifndef GMIME_ATLEAST_26
272 if (node->verify_attempted && !node->sig_validity)
273 fprintf (stderr, "Failed to verify signed part: %s\n",
274 (err ? err->message : "no error explanation given"));
284 mime_node_child (mime_node_t *parent, int child)
289 if (!parent || child < 0 || child >= parent->nchildren)
292 if (GMIME_IS_MULTIPART (parent->part)) {
293 if (child == 1 && parent->decrypted_child)
294 sub = parent->decrypted_child;
296 sub = g_mime_multipart_get_part
297 (GMIME_MULTIPART (parent->part), child);
298 } else if (GMIME_IS_MESSAGE (parent->part)) {
299 sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
301 /* This should have been caught by message_part_create */
302 INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
303 g_type_name (G_OBJECT_TYPE (parent->part)));
305 node = _mime_node_create (parent, sub);
307 if (child == parent->next_child && parent->next_part_num != -1) {
308 /* We're traversing in depth-first order. Record the child's
309 * depth-first numbering. */
310 node->part_num = parent->next_part_num;
311 node->next_part_num = node->part_num + 1;
313 /* Prepare the parent for its next depth-first child. */
314 parent->next_child++;
315 parent->next_part_num = -1;
317 if (node->nchildren == 0) {
318 /* We've reached a leaf, so find the parent that has more
319 * children and set it up to number its next child. */
320 mime_node_t *iter = node->parent;
321 while (iter && iter->next_child == iter->nchildren)
324 iter->next_part_num = node->part_num + 1;
332 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
334 mime_node_t *ret = NULL;
341 for (i = 0; i < node->nchildren && !ret; i++) {
342 mime_node_t *child = mime_node_child (node, i);
343 ret = _mime_node_seek_dfs_walk (child, n);
351 mime_node_seek_dfs (mime_node_t *node, int n)
355 return _mime_node_seek_dfs_walk (node, &n);