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_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);
82 mctx->file = fopen (filename, "r");
84 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
85 status = NOTMUCH_STATUS_FILE_ERROR;
89 mctx->stream = g_mime_stream_file_new (mctx->file);
91 fprintf (stderr, "Out of memory.\n");
92 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
95 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (mctx->stream), FALSE);
97 mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
99 fprintf (stderr, "Out of memory.\n");
100 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
104 mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
105 if (!mctx->mime_message) {
106 fprintf (stderr, "Failed to parse %s\n", filename);
107 status = NOTMUCH_STATUS_FILE_ERROR;
111 mctx->crypto = crypto;
113 /* Create the root node */
114 root->part = GMIME_OBJECT (mctx->mime_message);
115 root->envelope_file = message;
121 root->next_child = 0;
122 root->next_part_num = 1;
125 return NOTMUCH_STATUS_SUCCESS;
132 #ifdef GMIME_ATLEAST_26
134 /* Signature list destructor (GMime 2.6) */
136 _signature_list_free (GMimeSignatureList **proxy)
138 g_object_unref (*proxy);
142 /* Set up signature list destructor (GMime 2.6) */
144 set_signature_list_destructor (mime_node_t *node)
146 GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
148 *proxy = node->sig_list;
149 talloc_set_destructor (proxy, _signature_list_free);
153 /* Verify a signed mime node (GMime 2.6) */
155 node_verify (mime_node_t *node, GMimeObject *part,
156 notmuch_crypto_context_t *cryptoctx)
160 node->verify_attempted = TRUE;
161 node->sig_list = g_mime_multipart_signed_verify
162 (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);
165 set_signature_list_destructor (node);
167 fprintf (stderr, "Failed to verify signed part: %s\n",
168 err ? err->message : "no error explanation given");
174 /* Decrypt and optionally verify an encrypted mime node (GMime 2.6) */
176 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
177 notmuch_crypto_context_t *cryptoctx)
180 GMimeDecryptResult *decrypt_result = NULL;
181 GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
183 node->decrypt_attempted = TRUE;
184 node->decrypted_child = g_mime_multipart_encrypted_decrypt
185 (encrypteddata, cryptoctx, &decrypt_result, &err);
186 if (! node->decrypted_child) {
187 fprintf (stderr, "Failed to decrypt part: %s\n",
188 err ? err->message : "no error explanation given");
192 node->decrypt_success = TRUE;
193 node->verify_attempted = TRUE;
195 /* This may be NULL if the part is not signed. */
196 node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
197 if (node->sig_list) {
198 g_object_ref (node->sig_list);
199 set_signature_list_destructor (node);
201 g_object_unref (decrypt_result);
208 #else /* GMIME_ATLEAST_26 */
210 /* Signature validity destructor (GMime 2.4) */
212 _signature_validity_free (GMimeSignatureValidity **proxy)
214 g_mime_signature_validity_free (*proxy);
218 /* Set up signature validity destructor (GMime 2.4) */
220 set_signature_validity_destructor (mime_node_t *node,
221 GMimeSignatureValidity *sig_validity)
223 GMimeSignatureValidity **proxy = talloc (node, GMimeSignatureValidity *);
225 *proxy = sig_validity;
226 talloc_set_destructor (proxy, _signature_validity_free);
230 /* Verify a signed mime node (GMime 2.4) */
232 node_verify (mime_node_t *node, GMimeObject *part,
233 notmuch_crypto_context_t *cryptoctx)
236 GMimeSignatureValidity *sig_validity;
238 node->verify_attempted = TRUE;
239 sig_validity = g_mime_multipart_signed_verify
240 (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);
241 node->sig_validity = sig_validity;
243 set_signature_validity_destructor (node, sig_validity);
245 fprintf (stderr, "Failed to verify signed part: %s\n",
246 err ? err->message : "no error explanation given");
253 /* Decrypt and optionally verify an encrypted mime node (GMime 2.4) */
255 node_decrypt_and_verify (mime_node_t *node, GMimeObject *part,
256 notmuch_crypto_context_t *cryptoctx)
259 GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part);
261 node->decrypt_attempted = TRUE;
262 node->decrypted_child = g_mime_multipart_encrypted_decrypt
263 (encrypteddata, cryptoctx, &err);
264 if (! node->decrypted_child) {
265 fprintf (stderr, "Failed to decrypt part: %s\n",
266 err ? err->message : "no error explanation given");
270 node->decrypt_success = TRUE;
271 node->verify_attempted = TRUE;
273 /* The GMimeSignatureValidity returned here is a const, unlike the
274 * one returned by g_mime_multipart_signed_verify() in
275 * node_verify() above, so the destructor is not needed.
277 node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
278 if (! node->sig_validity)
279 fprintf (stderr, "Failed to verify encrypted signed part: %s\n",
280 err ? err->message : "no error explanation given");
287 #endif /* GMIME_ATLEAST_26 */
290 _mime_node_create (mime_node_t *parent, GMimeObject *part)
292 mime_node_t *node = talloc_zero (parent, mime_node_t);
293 notmuch_crypto_context_t *cryptoctx = NULL;
295 /* Set basic node properties */
297 node->ctx = parent->ctx;
298 if (!talloc_reference (node, node->ctx)) {
299 fprintf (stderr, "Out of memory.\n");
303 node->parent = parent;
304 node->part_num = node->next_part_num = -1;
305 node->next_child = 0;
307 /* Deal with the different types of parts */
308 if (GMIME_IS_PART (part)) {
310 } else if (GMIME_IS_MULTIPART (part)) {
311 node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
312 } else if (GMIME_IS_MESSAGE_PART (part)) {
313 /* Promote part to an envelope and open it */
314 GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
315 GMimeMessage *message = g_mime_message_part_get_message (message_part);
316 node->envelope_part = message_part;
317 node->part = GMIME_OBJECT (message);
320 fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
321 g_type_name (G_OBJECT_TYPE (part)));
326 if ((GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt)
327 || (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) {
328 GMimeContentType *content_type = g_mime_object_get_content_type (part);
329 const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol");
330 cryptoctx = notmuch_crypto_get_context (node->ctx->crypto, protocol);
333 /* Handle PGP/MIME parts */
334 if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt && cryptoctx) {
335 if (node->nchildren != 2) {
336 /* this violates RFC 3156 section 4, so we won't bother with it. */
337 fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
338 "message (must be exactly 2)\n",
341 node_decrypt_and_verify (node, part, cryptoctx);
343 } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify && cryptoctx) {
344 if (node->nchildren != 2) {
345 /* this violates RFC 3156 section 5, so we won't bother with it. */
346 fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
347 "(must be exactly 2)\n",
350 node_verify (node, part, cryptoctx);
358 mime_node_child (mime_node_t *parent, int child)
363 if (!parent || !parent->part || child < 0 || child >= parent->nchildren)
366 if (GMIME_IS_MULTIPART (parent->part)) {
367 if (child == 1 && parent->decrypted_child)
368 sub = parent->decrypted_child;
370 sub = g_mime_multipart_get_part
371 (GMIME_MULTIPART (parent->part), child);
372 } else if (GMIME_IS_MESSAGE (parent->part)) {
373 sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
375 /* This should have been caught by message_part_create */
376 INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
377 g_type_name (G_OBJECT_TYPE (parent->part)));
379 node = _mime_node_create (parent, sub);
381 if (child == parent->next_child && parent->next_part_num != -1) {
382 /* We're traversing in depth-first order. Record the child's
383 * depth-first numbering. */
384 node->part_num = parent->next_part_num;
385 node->next_part_num = node->part_num + 1;
387 /* Prepare the parent for its next depth-first child. */
388 parent->next_child++;
389 parent->next_part_num = -1;
391 if (node->nchildren == 0) {
392 /* We've reached a leaf, so find the parent that has more
393 * children and set it up to number its next child. */
394 mime_node_t *iter = node->parent;
395 while (iter && iter->next_child == iter->nchildren)
398 iter->next_part_num = node->part_num + 1;
406 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
408 mime_node_t *ret = NULL;
415 for (i = 0; i < node->nchildren && !ret; i++) {
416 mime_node_t *child = mime_node_child (node, i);
417 ret = _mime_node_seek_dfs_walk (child, n);
425 mime_node_seek_dfs (mime_node_t *node, int n)
429 return _mime_node_seek_dfs_walk (node, &n);