2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Jens Jahnke <jan0sch@gmx.net>
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 2 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 #include "procheader.h"
44 #include "prefs_common.h"
45 #include "passphrase.h"
46 #include "select-keys.h"
47 #include "sigstatus.h"
52 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
54 static char *content_names[] = {
56 "Content-Disposition",
57 "Content-Transfer-Encoding",
61 /* stolen from rfc2015.c */
63 gpg_name_cmp(const char *a, const char *b)
65 for( ; *a && *b; a++, b++) {
67 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
75 pgptext_decrypt (MimeInfo *partinfo, FILE *fp)
79 GpgmeData cipher = NULL, plain = NULL;
80 struct passphrase_cb_info_s info;
82 memset (&info, 0, sizeof info);
84 err = gpgme_new (&ctx);
86 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
90 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
91 partinfo->fpos, partinfo->size);
93 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
94 gpgme_strerror (err));
98 err = gpgme_data_new (&plain);
100 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
104 if (!getenv("GPG_AGENT_INFO")) {
106 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, (void *)&info);
109 err = gpgme_op_decrypt (ctx, cipher, plain);
112 gpgme_data_release (cipher);
114 gpgmegtk_free_passphrase();
115 debug_print ("decryption failed: %s\n", gpgme_strerror (err));
116 gpgme_data_release (plain);
120 debug_print ("decryption succeeded\n");
127 headerp(char *p, char **names)
136 if(p2[-1] == ' ' || p2[-1] == '\t') {
145 for(i = 0 ; names[i] != NULL; i++) {
146 if(!gpg_name_cmp (names[i], p))
151 return names[i] != NULL;
154 MimeInfo * pgptext_find_signature (MimeInfo *mimeinfo)
159 gboolean pgptext_has_signature (MimeInfo *mimeinfo)
162 * check for the following strings:
163 * -----BEGIN PGP SIGNED MESSAGE-----
165 * -----BEGIN PGP SIGNATURE-----
166 * -----END PGP SIGNATURE-----
172 void pgptext_check_signature (MimeInfo *mimeinfo, FILE *fp)
176 int pgptext_is_encrypted (MimeInfo *mimeinfo, MsgInfo *msginfo)
179 gchar *file, *tmpchk;
181 gboolean has_begin_pgp_msg = FALSE;
182 gboolean has_end_pgp_msg = FALSE;
183 gchar *check_begin_pgp_msg = "-----BEGIN PGP MESSAGE-----\n";
184 gchar *check_end_pgp_msg = "-----END PGP MESSAGE-----\n";
186 g_return_val_if_fail(msginfo != NULL, 0);
191 if ((fp = procmsg_open_message(msginfo)) == NULL) return 0;
192 mimeinfo = procmime_scan_mime_header(fp);
194 if (!mimeinfo) return 0;
196 file = procmsg_get_message_file_path(msginfo);
197 g_return_val_if_fail(file != NULL, 0);
199 if (mimeinfo->mime_type != MIME_TEXT) {
200 if ((fp = fopen(file, "rb")) == NULL) {
201 FILE_OP_ERROR(file, "fopen");
205 if (mimeinfo->mime_type == MIME_MULTIPART) {
206 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
208 while (fgets(buf, sizeof(buf), fp) != NULL)
209 if (buf[0] == '\r' || buf[0] == '\n') break;
211 /* now check for a pgptext encrypted message */
212 while (fgets(buf, sizeof(buf), fp) != NULL) {
213 tmpchk = g_strnfill(sizeof(buf), '\n');
214 memmove(tmpchk, &buf, sizeof(buf));
216 if (strstr(tmpchk, check_begin_pgp_msg) != NULL)
217 has_begin_pgp_msg = TRUE;
218 if (strstr(tmpchk, check_end_pgp_msg) != NULL)
219 has_end_pgp_msg = TRUE;
225 if ((fp = fopen(file, "rb")) == NULL) {
226 FILE_OP_ERROR(file, "fopen");
230 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
232 while (fgets(buf, sizeof(buf), fp) != NULL)
233 if (buf[0] == '\r' || buf[0] == '\n') break;
235 /* now check for a pgptext encrypted message */
236 while (fgets(buf, sizeof(buf), fp) != NULL) {
237 tmpchk = g_strnfill(sizeof(buf), '\n');
238 memmove(tmpchk, &buf, sizeof(buf));
240 if (strstr(tmpchk, check_begin_pgp_msg) != NULL)
241 has_begin_pgp_msg = TRUE;
242 if (strstr(tmpchk, check_end_pgp_msg) != NULL)
243 has_end_pgp_msg = TRUE;
252 /* do we have a proper message? */
253 if (has_begin_pgp_msg && has_end_pgp_msg)
259 void pgptext_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
270 g_return_if_fail (mimeinfo->mime_type == MIME_TEXT);
272 debug_print ("text/plain with pgptext encountered\n");
274 partinfo = procmime_scan_message(msginfo);
277 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0)
279 while (fgets(buf, sizeof(buf), fp) != NULL) {
280 partinfo->fpos = partinfo->fpos + strlen(buf);
281 if (buf[0] == '\r' || buf[0] == '\n') break;
284 while (fgets(buf, sizeof(buf), fp) != NULL)
285 partinfo->size = partinfo->size + strlen(buf);
287 plain = pgptext_decrypt (partinfo, fp);
289 msginfo->decryption_failed = 1;
293 fname = g_strdup_printf("%s%cplaintext.%08x",
294 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
296 if ((dstfp = fopen(fname, "wb")) == NULL) {
297 FILE_OP_ERROR(fname, "fopen");
299 msginfo->decryption_failed = 1;
303 /* write the orginal header to the new file */
304 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
307 while (fgets(buf, sizeof(buf), fp)) {
308 if (headerp (buf, content_names))
310 if (buf[0] == '\r' || buf[0] == '\n')
315 err = gpgme_data_rewind (plain);
317 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
319 /* insert blank line to avoid some trouble... */
322 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
323 fwrite (buf, nread, 1, dstfp);
326 if (err != GPGME_EOF) {
327 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
332 msginfo->plaintext_file = fname;
333 msginfo->decryption_failed = 0;
337 int pgptext_encrypt (const char *file, GSList *recp_list)
342 int pgptext_sign (const char *file, PrefsAccount *ac)
347 #endif /* USE_GPGME */