]> git.notmuchmail.org Git - notmuch/blob - util/repair.c
legacy-display: accept text/plain legacy display parts
[notmuch] / util / repair.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2019 Daniel Kahn Gillmor
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Authors: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
19  */
20
21 #include <stdbool.h>
22 #include "repair.h"
23
24
25 static bool
26 _notmuch_crypto_payload_has_legacy_display (GMimeObject *payload)
27 {
28     GMimeMultipart *mpayload;
29     const char *protected_header_parameter;
30     GMimeTextPart *legacy_display;
31     char *legacy_display_header_text = NULL;
32     GMimeStream *stream = NULL;
33     GMimeParser *parser = NULL;
34     GMimeObject *legacy_header_object = NULL, *first;
35     GMimeHeaderList *legacy_display_headers = NULL, *protected_headers = NULL;
36     bool ret = false;
37
38     if (! g_mime_content_type_is_type (g_mime_object_get_content_type (payload),
39                                        "multipart", "mixed"))
40         return false;
41     protected_header_parameter = g_mime_object_get_content_type_parameter (payload, "protected-headers");
42     if ((! protected_header_parameter) || strcmp (protected_header_parameter, "v1"))
43         return false;
44     if (! GMIME_IS_MULTIPART (payload))
45         return false;
46     mpayload = GMIME_MULTIPART (payload);
47     if (mpayload == NULL)
48         return false;
49     if (g_mime_multipart_get_count (mpayload) != 2)
50         return false;
51     first = g_mime_multipart_get_part (mpayload, 0);
52     /* Early implementations that generated "Legacy Display" parts used
53        Content-Type: text/rfc822-headers, but text/plain is more widely
54        rendered, so it is now the standard choice.  We accept either as a
55        Legacy Display part. */
56     if (! (g_mime_content_type_is_type (g_mime_object_get_content_type (first),
57                                         "text", "plain") ||
58            g_mime_content_type_is_type (g_mime_object_get_content_type (first),
59                                         "text", "rfc822-headers")))
60         return false;
61     protected_header_parameter = g_mime_object_get_content_type_parameter (first, "protected-headers");
62     if ((! protected_header_parameter) || strcmp (protected_header_parameter, "v1"))
63         return false;
64     if (! GMIME_IS_TEXT_PART (first))
65         return false;
66
67     /* ensure that the headers in the first part all match the values
68      * found in the payload's own protected headers!  if they don't,
69      * we should not treat this as a valid "legacy-display" part.
70      *
71      * Crafting a GMimeHeaderList object from the content of the
72      * text/rfc822-headers part is pretty clumsy; we should probably
73      * push something into GMime that makes this a one-shot
74      * operation. */
75     if ((protected_headers = g_mime_object_get_header_list (payload), protected_headers) &&
76         (legacy_display = GMIME_TEXT_PART (first), legacy_display) &&
77         (legacy_display_header_text = g_mime_text_part_get_text (legacy_display), legacy_display_header_text) &&
78         (stream = g_mime_stream_mem_new_with_buffer (legacy_display_header_text, strlen (legacy_display_header_text)), stream) &&
79         (g_mime_stream_write (stream, "\r\n\r\n", 4) == 4) &&
80         (g_mime_stream_seek (stream, 0, GMIME_STREAM_SEEK_SET) == 0) &&
81         (parser = g_mime_parser_new_with_stream (stream), parser) &&
82         (legacy_header_object = g_mime_parser_construct_part (parser, NULL), legacy_header_object) &&
83         (legacy_display_headers = g_mime_object_get_header_list (legacy_header_object), legacy_display_headers)) {
84         /* walk through legacy_display_headers, comparing them against
85          * their values in the protected_headers: */
86         ret = true;
87         for (int i = 0; i < g_mime_header_list_get_count (legacy_display_headers); i++) {
88             GMimeHeader *dh = g_mime_header_list_get_header_at (legacy_display_headers, i);
89             if (dh == NULL) {
90                 ret = false;
91                 goto DONE;
92             }
93             GMimeHeader *ph = g_mime_header_list_get_header (protected_headers, g_mime_header_get_name (dh));
94             if (ph == NULL) {
95                 ret = false;
96                 goto DONE;
97             }
98             const char *dhv = g_mime_header_get_value (dh);
99             const char *phv = g_mime_header_get_value (ph);
100             if (dhv == NULL || phv == NULL || strcmp (dhv, phv)) {
101                 ret = false;
102                 goto DONE;
103             }
104         }
105     }
106
107  DONE:
108     if (legacy_display_header_text)
109         g_free (legacy_display_header_text);
110     if (stream)
111         g_object_unref (stream);
112     if (parser)
113         g_object_unref (parser);
114     if (legacy_header_object)
115         g_object_unref (legacy_header_object);
116
117     return ret;
118 }
119
120 GMimeObject *
121 _notmuch_repair_crypto_payload_skip_legacy_display (GMimeObject *payload)
122 {
123     if (_notmuch_crypto_payload_has_legacy_display (payload)) {
124         return g_mime_multipart_get_part (GMIME_MULTIPART (payload), 1);
125     } else {
126         return payload;
127     }
128 }
129
130 /* see
131  * https://tools.ietf.org/html/draft-dkg-openpgp-pgpmime-message-mangling-00#section-4.1.1 */
132 static bool
133 _notmuch_is_mixed_up_mangled (GMimeObject *part)
134 {
135     GMimeMultipart *mpart = NULL;
136     GMimeObject *parts[3] = {NULL, NULL, NULL};
137     GMimeContentType *type = NULL;
138     char *prelude_string = NULL;
139     bool prelude_is_empty;
140
141     if (part == NULL)
142         return false;
143     type = g_mime_object_get_content_type (part);
144     if (type == NULL)
145         return false;
146     if (! g_mime_content_type_is_type (type, "multipart", "mixed"))
147         return false;
148     if (! GMIME_IS_MULTIPART (part)) /* probably impossible */
149         return false;
150     mpart = GMIME_MULTIPART (part);
151     if (mpart == NULL)
152         return false;
153     if (g_mime_multipart_get_count (mpart) != 3)
154         return false;
155     parts[0] = g_mime_multipart_get_part (mpart, 0);
156     if (! g_mime_content_type_is_type (g_mime_object_get_content_type (parts[0]),
157                                        "text", "plain"))
158         return false;
159     if (! GMIME_IS_TEXT_PART (parts[0]))
160         return false;
161     parts[1] = g_mime_multipart_get_part (mpart, 1);
162     if (! g_mime_content_type_is_type (g_mime_object_get_content_type (parts[1]),
163                                        "application", "pgp-encrypted"))
164         return false;
165     parts[2] = g_mime_multipart_get_part (mpart, 2);
166     if (! g_mime_content_type_is_type (g_mime_object_get_content_type (parts[2]),
167                                        "application", "octet-stream"))
168         return false;
169
170     /* Is parts[0] length 0? */
171     prelude_string = g_mime_text_part_get_text (GMIME_TEXT_PART (parts[0]));
172     prelude_is_empty = (prelude_string[0] == '\0');
173     g_free (prelude_string);
174     if (! prelude_is_empty)
175         return false;
176
177     /* FIXME: after decoding and stripping whitespace, is parts[1]
178      * subpart just "Version: 1" ? */
179
180     /* FIXME: can we determine that parts[2] subpart is *only* PGP
181      * encrypted data?  I tried g_mime_part_get_openpgp_data () but
182      * found https://github.com/jstedfast/gmime/issues/60 */
183
184     return true;
185 }
186
187
188 /* see
189  * https://tools.ietf.org/html/draft-dkg-openpgp-pgpmime-message-mangling-00#section-4.1.2 */
190 GMimeObject *
191 _notmuch_repair_mixed_up_mangled (GMimeObject *part)
192 {
193     GMimeMultipart *mpart = NULL, *mpart_ret = NULL;
194     GMimeObject *ret = NULL;
195
196     if (! _notmuch_is_mixed_up_mangled (part))
197         return NULL;
198     mpart = GMIME_MULTIPART (part);
199     ret = GMIME_OBJECT (g_mime_multipart_encrypted_new ());
200     if (ret == NULL)
201         return NULL;
202     mpart_ret = GMIME_MULTIPART (ret);
203     if (mpart_ret == NULL) {
204         g_object_unref (ret);
205         return NULL;
206     }
207     g_mime_object_set_content_type_parameter (ret, "protocol", "application/pgp-encrypted");
208
209     g_mime_multipart_insert (mpart_ret, 0, g_mime_multipart_get_part (mpart, 1));
210     g_mime_multipart_insert (mpart_ret, 1, g_mime_multipart_get_part (mpart, 2));
211     return ret;
212 }