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 GMimeCipherContext *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 GMimeCipherContext *cryptoctx, notmuch_bool_t decrypt,
61 mime_node_t **root_out)
63 const char *filename = notmuch_message_get_filename (message);
64 mime_node_context_t *mctx;
66 notmuch_status_t status;
68 root = talloc_zero (ctx, mime_node_t);
70 fprintf (stderr, "Out of memory.\n");
71 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
75 /* Create the tree-wide context */
76 mctx = talloc_zero (root, mime_node_context_t);
78 fprintf (stderr, "Out of memory.\n");
79 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
82 talloc_set_destructor (mctx, _mime_node_context_free);
84 mctx->file = fopen (filename, "r");
86 fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
87 status = NOTMUCH_STATUS_FILE_ERROR;
91 mctx->stream = g_mime_stream_file_new (mctx->file);
92 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (mctx->stream), FALSE);
94 mctx->parser = g_mime_parser_new_with_stream (mctx->stream);
96 mctx->mime_message = g_mime_parser_construct_message (mctx->parser);
98 mctx->cryptoctx = cryptoctx;
99 mctx->decrypt = decrypt;
101 /* Create the root node */
102 root->part = GMIME_OBJECT (mctx->mime_message);
103 root->envelope_file = message;
108 return NOTMUCH_STATUS_SUCCESS;
116 _signature_validity_free (GMimeSignatureValidity **proxy)
118 g_mime_signature_validity_free (*proxy);
123 _mime_node_create (const mime_node_t *parent, GMimeObject *part)
125 mime_node_t *node = talloc_zero (parent, mime_node_t);
128 /* Set basic node properties */
130 node->ctx = parent->ctx;
131 if (!talloc_reference (node, node->ctx)) {
132 fprintf (stderr, "Out of memory.\n");
137 /* Deal with the different types of parts */
138 if (GMIME_IS_PART (part)) {
140 } else if (GMIME_IS_MULTIPART (part)) {
141 node->nchildren = g_mime_multipart_get_count (GMIME_MULTIPART (part));
142 } else if (GMIME_IS_MESSAGE_PART (part)) {
143 /* Promote part to an envelope and open it */
144 GMimeMessagePart *message_part = GMIME_MESSAGE_PART (part);
145 GMimeMessage *message = g_mime_message_part_get_message (message_part);
146 node->envelope_part = message_part;
147 node->part = GMIME_OBJECT (message);
150 fprintf (stderr, "Warning: Unknown mime part type: %s.\n",
151 g_type_name (G_OBJECT_TYPE (part)));
156 /* Handle PGP/MIME parts */
157 if (GMIME_IS_MULTIPART_ENCRYPTED (part)
158 && node->ctx->cryptoctx && node->ctx->decrypt) {
159 if (node->nchildren != 2) {
160 /* this violates RFC 3156 section 4, so we won't bother with it. */
161 fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "
162 "message (must be exactly 2)\n",
165 GMimeMultipartEncrypted *encrypteddata =
166 GMIME_MULTIPART_ENCRYPTED (part);
167 node->decrypt_attempted = TRUE;
168 node->decrypted_child = g_mime_multipart_encrypted_decrypt
169 (encrypteddata, node->ctx->cryptoctx, &err);
170 if (node->decrypted_child) {
171 node->decrypt_success = node->verify_attempted = TRUE;
172 node->sig_validity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata);
174 fprintf (stderr, "Failed to decrypt part: %s\n",
175 (err ? err->message : "no error explanation given"));
178 } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->cryptoctx) {
179 if (node->nchildren != 2) {
180 /* this violates RFC 3156 section 5, so we won't bother with it. */
181 fprintf (stderr, "Error: %d part(s) for a multipart/signed message "
182 "(must be exactly 2)\n",
185 /* For some reason the GMimeSignatureValidity returned
186 * here is not a const (inconsistent with that
188 * g_mime_multipart_encrypted_get_signature_validity,
189 * and therefore needs to be properly disposed of.
191 * In GMime 2.6, they're both non-const, so we'll be able
192 * to clean up this asymmetry. */
193 GMimeSignatureValidity *sig_validity = g_mime_multipart_signed_verify
194 (GMIME_MULTIPART_SIGNED (part), node->ctx->cryptoctx, &err);
195 node->verify_attempted = TRUE;
196 node->sig_validity = sig_validity;
198 GMimeSignatureValidity **proxy =
199 talloc (node, GMimeSignatureValidity *);
200 *proxy = sig_validity;
201 talloc_set_destructor (proxy, _signature_validity_free);
206 if (node->verify_attempted && !node->sig_validity)
207 fprintf (stderr, "Failed to verify signed part: %s\n",
208 (err ? err->message : "no error explanation given"));
217 mime_node_child (const mime_node_t *parent, int child)
221 if (!parent || child < 0 || child >= parent->nchildren)
224 if (GMIME_IS_MULTIPART (parent->part)) {
225 if (child == 1 && parent->decrypted_child)
226 sub = parent->decrypted_child;
228 sub = g_mime_multipart_get_part
229 (GMIME_MULTIPART (parent->part), child);
230 } else if (GMIME_IS_MESSAGE (parent->part)) {
231 sub = g_mime_message_get_mime_part (GMIME_MESSAGE (parent->part));
233 /* This should have been caught by message_part_create */
234 INTERNAL_ERROR ("Unexpected GMimeObject type: %s",
235 g_type_name (G_OBJECT_TYPE (parent->part)));
237 return _mime_node_create (parent, sub);
241 _mime_node_seek_dfs_walk (mime_node_t *node, int *n)
243 mime_node_t *ret = NULL;
250 for (i = 0; i < node->nchildren && !ret; i++) {
251 mime_node_t *child = mime_node_child (node, i);
252 ret = _mime_node_seek_dfs_walk (child, n);
260 mime_node_seek_dfs (mime_node_t *node, int n)
264 return _mime_node_seek_dfs_walk (node, &n);