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 static char *mime_version_name[] = {
66 /* stolen from rfc2015.c */
68 gpg_name_cmp(const char *a, const char *b)
70 for( ; *a && *b; a++, b++) {
72 && toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
80 pgptext_decrypt (MimeInfo *partinfo, FILE *fp)
84 GpgmeData cipher = NULL, plain = NULL;
85 struct passphrase_cb_info_s info;
87 memset (&info, 0, sizeof info);
89 err = gpgme_new (&ctx);
91 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
95 err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
96 partinfo->fpos, partinfo->size);
98 debug_print ("gpgme_data_new_from_filepart failed: %s\n",
99 gpgme_strerror (err));
103 err = gpgme_data_new (&plain);
105 debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
109 if (!getenv("GPG_AGENT_INFO")) {
111 gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, &info);
114 err = gpgme_op_decrypt (ctx, cipher, plain);
117 gpgme_data_release (cipher);
119 gpgmegtk_free_passphrase();
120 debug_print ("decryption failed: %s\n", gpgme_strerror (err));
121 gpgme_data_release (plain);
125 debug_print ("decryption succeeded\n");
132 headerp(char *p, char **names)
141 if(p2[-1] == ' ' || p2[-1] == '\t') {
150 for(i = 0 ; names[i] != NULL; i++) {
151 if(!gpg_name_cmp (names[i], p))
156 return names[i] != NULL;
159 MimeInfo * pgptext_find_signature (MimeInfo *mimeinfo)
163 gboolean pgptext_has_signature (MimeInfo *mimeinfo)
166 * check for the following strings:
167 * -----BEGIN PGP SIGNED MESSAGE-----
169 * -----BEGIN PGP SIGNATURE-----
170 * -----END PGP SIGNATURE-----
176 void pgptext_check_signature (MimeInfo *mimeinfo, FILE *fp)
180 int pgptext_is_encrypted (MimeInfo *mimeinfo, MsgInfo *msginfo)
183 gchar *file, *tmpchk;
185 gboolean has_begin_pgp_msg = FALSE;
186 gboolean has_end_pgp_msg = FALSE;
187 gchar *check_begin_pgp_msg = "-----BEGIN PGP MESSAGE-----\n";
188 gchar *check_end_pgp_msg = "-----END PGP MESSAGE-----\n";
190 g_return_if_fail(msginfo != NULL);
195 if ((fp = procmsg_open_message(msginfo)) == NULL) return;
196 mimeinfo = procmime_scan_mime_header(fp);
198 if (!mimeinfo) return;
200 file = procmsg_get_message_file_path(msginfo);
201 g_return_if_fail(file != NULL);
203 if (mimeinfo->mime_type != MIME_TEXT) {
204 if ((fp = fopen(file, "rb")) == NULL) {
205 FILE_OP_ERROR(file, "fopen");
209 if (mimeinfo->mime_type == MIME_MULTIPART) {
210 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
212 while (fgets(buf, sizeof(buf), fp) != NULL)
213 if (buf[0] == '\r' || buf[0] == '\n') break;
215 /* now check for a pgptext encrypted message */
216 while (fgets(buf, sizeof(buf), fp) != NULL) {
217 tmpchk = g_strnfill(sizeof(buf), '\n');
218 memmove(tmpchk, &buf, sizeof(buf));
220 if (strstr(tmpchk, check_begin_pgp_msg) != NULL)
221 has_begin_pgp_msg = TRUE;
222 if (strstr(tmpchk, check_end_pgp_msg) != NULL)
223 has_end_pgp_msg = TRUE;
229 if ((fp = fopen(file, "rb")) == NULL) {
230 FILE_OP_ERROR(file, "fopen");
234 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
236 while (fgets(buf, sizeof(buf), fp) != NULL)
237 if (buf[0] == '\r' || buf[0] == '\n') break;
239 /* now check for a pgptext encrypted message */
240 while (fgets(buf, sizeof(buf), fp) != NULL) {
241 tmpchk = g_strnfill(sizeof(buf), '\n');
242 memmove(tmpchk, &buf, sizeof(buf));
244 if (strstr(tmpchk, check_begin_pgp_msg) != NULL)
245 has_begin_pgp_msg = TRUE;
246 if (strstr(tmpchk, check_end_pgp_msg) != NULL)
247 has_end_pgp_msg = TRUE;
256 /* do we have a proper message? */
257 if (has_begin_pgp_msg && has_end_pgp_msg)
263 void pgptext_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
276 g_return_if_fail (mimeinfo->mime_type == MIME_TEXT);
278 debug_print ("text/plain with pgptext encountered\n");
280 partinfo = procmime_scan_message(msginfo);
283 if (fseek(fp, partinfo->fpos, SEEK_SET) < 0)
285 while (fgets(buf, sizeof(buf), fp) != NULL) {
286 partinfo->fpos = partinfo->fpos + strlen(buf);
287 if (buf[0] == '\r' || buf[0] == '\n') break;
290 while (fgets(buf, sizeof(buf), fp) != NULL)
291 partinfo->size = partinfo->size + strlen(buf);
293 plain = pgptext_decrypt (partinfo, fp);
295 msginfo->decryption_failed = 1;
299 fname = g_strdup_printf("%s%cplaintext.%08x",
300 get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
302 if ((dstfp = fopen(fname, "wb")) == NULL) {
303 FILE_OP_ERROR(fname, "fopen");
305 msginfo->decryption_failed = 1;
309 /* write the orginal header to the new file */
310 if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
313 while (fgets(buf, sizeof(buf), fp)) {
314 if (headerp (buf, content_names))
316 if (buf[0] == '\r' || buf[0] == '\n')
321 err = gpgme_data_rewind (plain);
323 debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
325 /* insert blank line to avoid some trouble... */
328 while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
329 fwrite (buf, nread, 1, dstfp);
332 if (err != GPGME_EOF) {
333 debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
338 msginfo->plaintext_file = fname;
339 msginfo->decryption_failed = 0;
343 int pgptext_encrypt (const char *file, GSList *recp_list)
347 int pgptext_sign (const char *file, PrefsAccount *ac)
351 #endif /* USE_GPGME */