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 #ifdef GMIME_ATLEAST_26
37 GMimeCryptoContext *cryptoctx;
39 GMimeCipherContext *cryptoctx;
41 notmuch_bool_t decrypt;
42 } mime_node_context_t;
45 _mime_node_context_free (mime_node_context_t *res)
47 if (res->mime_message)
48 g_object_unref (res->mime_message);
51 g_object_unref (res->parser);
54 g_object_unref (res->stream);
63 mime_node_open (const void *ctx, notmuch_message_t *message,
64 #ifdef GMIME_ATLEAST_26
65 GMimeCryptoContext *cryptoctx,
67 GMimeCipherContext *cryptoctx,
69 notmuch_bool_t decrypt, mime_node_t **root_out)
71 const char *filename = notmuch_message_get_filename (message);
72 mime_node_context_t *mctx;
74 notmuch_status_t status;
76 root = talloc_zero (ctx, mime_node_t);
78 fprintf (stderr, "Out of memory.\n");
79 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
83 /* Create the tree-wide context */
84 mctx = talloc_zero (root, mime_node_context_t);
86 fprintf (stderr, "Out of memory.\n");
87 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
90 talloc_set_destructor (mctx, _mime_node_context_free);
92 mctx->file = fopen (filename, "r");
94 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
95 status = NOTMUCH_STATUS_FILE_ERROR;
99 mctx->stream = g_mime_stream_file_new (mctx->file);
100 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (mctx->stream), FALSE);
102 mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
104 mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
106 mctx->cryptoctx = cryptoctx;
107 mctx->decrypt = decrypt;
109 /* Create the root node */
110 root->part = GMIME_OBJECT (mctx->mime_message);
111 root->envelope_file = message;
116 return NOTMUCH_STATUS_SUCCESS;
123 #ifdef GMIME_ATLEAST_26
125 _signature_list_free (GMimeSignatureList **proxy)
127 g_object_unref (*proxy);
132 _signature_validity_free (GMimeSignatureValidity **proxy)
134 g_mime_signature_validity_free (*proxy);
140 _mime_node_create (const mime_node_t *parent, GMimeObject *part)
142 mime_node_t *node = talloc_zero (parent, mime_node_t);
145 /* Set basic node properties */
147 node->ctx = parent->ctx;
148 if (!talloc_reference (node, node->ctx)) {
149 fprintf (stderr, "Out of memory.\n");
154 /* Deal with the different types of parts */
155 if (GMIME_IS_PART (part)) {
157 } else if (GMIME_IS_MULTIPART (part)) {
158 node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
159 } else if (GMIME_IS_MESSAGE_PART (part)) {
160 /* Promote part to an envelope and open it */
161 GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
162 GMimeMessage *message = g_mime_message_part_get_message (message_part);
163 node->envelope_part = message_part;
164 node->part = GMIME_OBJECT (message);
167 fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
168 g_type_name (G_OBJECT_TYPE (part)));
173 /* Handle PGP/MIME parts */
174 if (GMIME_IS_MULTIPART_ENCRYPTED (part)
175 && node->ctx->cryptoctx && node->ctx->decrypt) {
176 if (node->nchildren != 2) {
177 /* this violates RFC 3156 section 4, so we won't bother with it. */
178 fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
179 "message (must be exactly 2)\n",
182 GMimeMultipartEncrypted *encrypteddata =
183 GMIME_MULTIPART_ENCRYPTED (part);
184 node->decrypt_attempted = TRUE;
185 #ifdef GMIME_ATLEAST_26
186 GMimeDecryptResult *decrypt_result = NULL;
187 node->decrypted_child = g_mime_multipart_encrypted_decrypt
188 (encrypteddata, node->ctx->cryptoctx, &decrypt_result, &err);
190 node->decrypted_child = g_mime_multipart_encrypted_decrypt
191 (encrypteddata, node->ctx->cryptoctx, &err);
193 if (node->decrypted_child) {
194 node->decrypt_success = node->verify_attempted = TRUE;
195 #ifdef GMIME_ATLEAST_26
196 /* This may be NULL if the part is not signed. */
197 node->sig_list = g_mime_decrypt_result_get_signatures (decrypt_result);
199 g_object_ref (node->sig_list);
200 g_object_unref (decrypt_result);
202 node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
205 fprintf (stderr, "Failed to decrypt part: %s\n",
206 (err ? err->message : "no error explanation given"));
209 } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->cryptoctx) {
210 if (node->nchildren != 2) {
211 /* this violates RFC 3156 section 5, so we won't bother with it. */
212 fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
213 "(must be exactly 2)\n",
216 #ifdef GMIME_ATLEAST_26
217 node->sig_list = g_mime_multipart_signed_verify
218 (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);
219 node->verify_attempted = TRUE;
222 fprintf (stderr, "Failed to verify signed part: %s\n",
223 (err ? err->message : "no error explanation given"));
225 /* For some reason the GMimeSignatureValidity returned
226 * here is not a const (inconsistent with that
228 * g_mime_multipart_encrypted_get_signature_validity,
229 * and therefore needs to be properly disposed of.
231 * In GMime 2.6, they're both non-const, so we'll be able
232 * to clean up this asymmetry. */
233 GMimeSignatureValidity *sig_validity = g_mime_multipart_signed_verify
234 (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);
235 node->verify_attempted = TRUE;
236 node->sig_validity = sig_validity;
238 GMimeSignatureValidity **proxy =
239 talloc (node, GMimeSignatureValidity *);
240 *proxy = sig_validity;
241 talloc_set_destructor (proxy, _signature_validity_free);
247 #ifdef GMIME_ATLEAST_26
248 /* sig_list may be created in both above cases, so we need to
249 * cleanly handle it here. */
250 if (node->sig_list) {
251 GMimeSignatureList **proxy = talloc (node, GMimeSignatureList *);
252 *proxy = node->sig_list;
253 talloc_set_destructor (proxy, _signature_list_free);
257 #ifndef GMIME_ATLEAST_26
258 if (node->verify_attempted && !node->sig_validity)
259 fprintf (stderr, "Failed to verify signed part: %s\n",
260 (err ? err->message : "no error explanation given"));
270 mime_node_child (const mime_node_t *parent, int child)
274 if (!parent || child < 0 || child >= parent->nchildren)
277 if (GMIME_IS_MULTIPART (parent->part)) {
278 if (child == 1 && parent->decrypted_child)
279 sub = parent->decrypted_child;
281 sub = g_mime_multipart_get_part
282 (GMIME_MULTIPART (parent->part), child);
283 } else if (GMIME_IS_MESSAGE (parent->part)) {
284 sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
286 /* This should have been caught by message_part_create */
287 INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
288 g_type_name (G_OBJECT_TYPE (parent->part)));
290 return _mime_node_create (parent, sub);
294 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
296 mime_node_t *ret = NULL;
303 for (i = 0; i < node->nchildren && !ret; i++) {
304 mime_node_t *child = mime_node_child (node, i);
305 ret = _mime_node_seek_dfs_walk (child, n);
313 mime_node_seek_dfs (mime_node_t *node, int n)
317 return _mime_node_seek_dfs_walk (node, &n);