2001-10-13 09:11:42 +02:00
|
|
|
/*
|
|
|
|
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
|
|
|
|
* Copyright (C) 2001 Jens Jahnke <jan0sch@gmx.net>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_GPGME
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include <gpgme.h>
|
|
|
|
|
|
|
|
#include "intl.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "passphrase.h"
|
|
|
|
#include "rfc2015.h"
|
|
|
|
|
|
|
|
#include "pgptext.h"
|
|
|
|
|
|
|
|
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
|
|
|
|
|
|
|
|
static char *content_names[] = {
|
|
|
|
"Content-Type",
|
|
|
|
"Content-Disposition",
|
|
|
|
"Content-Transfer-Encoding",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2001-10-20 01:37:48 +02:00
|
|
|
/* stolen from rfc2015.c */
|
|
|
|
static int
|
|
|
|
gpg_name_cmp(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
for( ; *a && *b; a++, b++) {
|
|
|
|
if(*a != *b
|
|
|
|
&& toupper(*(unsigned char *)a) != toupper(*(unsigned char *)b))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *a != *b;
|
|
|
|
}
|
|
|
|
|
2001-10-13 09:11:42 +02:00
|
|
|
static GpgmeData
|
|
|
|
pgptext_decrypt (MimeInfo *partinfo, FILE *fp)
|
|
|
|
{
|
|
|
|
GpgmeCtx ctx = NULL;
|
|
|
|
GpgmeError err;
|
|
|
|
GpgmeData cipher = NULL, plain = NULL;
|
|
|
|
struct passphrase_cb_info_s info;
|
|
|
|
|
|
|
|
memset (&info, 0, sizeof info);
|
|
|
|
|
|
|
|
err = gpgme_new (&ctx);
|
|
|
|
if (err) {
|
2001-12-24 11:23:17 +01:00
|
|
|
debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
|
2001-10-13 09:11:42 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gpgme_data_new_from_filepart (&cipher, NULL, fp,
|
|
|
|
partinfo->fpos, partinfo->size);
|
|
|
|
if (err) {
|
2001-12-24 11:23:17 +01:00
|
|
|
debug_print ("gpgme_data_new_from_filepart failed: %s\n",
|
|
|
|
gpgme_strerror (err));
|
2001-10-13 09:11:42 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gpgme_data_new (&plain);
|
|
|
|
if (err) {
|
2001-12-24 11:23:17 +01:00
|
|
|
debug_print ("gpgme_new failed: %s\n", gpgme_strerror (err));
|
2001-10-13 09:11:42 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!getenv("GPG_AGENT_INFO")) {
|
|
|
|
info.c = ctx;
|
2002-07-02 21:49:11 +02:00
|
|
|
gpgme_set_passphrase_cb (ctx, gpgmegtk_passphrase_cb, (void *)&info);
|
2001-10-13 09:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = gpgme_op_decrypt (ctx, cipher, plain);
|
|
|
|
|
|
|
|
leave:
|
|
|
|
gpgme_data_release (cipher);
|
|
|
|
if (err) {
|
2002-04-13 12:21:38 +02:00
|
|
|
gpgmegtk_free_passphrase();
|
2001-12-24 11:23:17 +01:00
|
|
|
debug_print ("decryption failed: %s\n", gpgme_strerror (err));
|
2001-10-13 09:11:42 +02:00
|
|
|
gpgme_data_release (plain);
|
|
|
|
plain = NULL;
|
|
|
|
}
|
|
|
|
else
|
2001-12-24 11:23:17 +01:00
|
|
|
debug_print ("decryption succeeded\n");
|
2001-10-13 09:11:42 +02:00
|
|
|
|
|
|
|
gpgme_release (ctx);
|
|
|
|
return plain;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
headerp(char *p, char **names)
|
|
|
|
{
|
|
|
|
int i, c;
|
|
|
|
char *p2;
|
|
|
|
|
|
|
|
p2 = strchr(p, ':');
|
|
|
|
if(!p2 || p == p2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(p2[-1] == ' ' || p2[-1] == '\t') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!names[0])
|
2002-03-05 19:13:35 +01:00
|
|
|
return 1;
|
2001-10-13 09:11:42 +02:00
|
|
|
|
|
|
|
c = *p2;
|
|
|
|
*p2 = 0;
|
|
|
|
for(i = 0 ; names[i] != NULL; i++) {
|
2001-10-20 01:37:48 +02:00
|
|
|
if(!gpg_name_cmp (names[i], p))
|
2001-10-13 09:11:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*p2 = c;
|
|
|
|
|
|
|
|
return names[i] != NULL;
|
|
|
|
}
|
|
|
|
|
2002-03-05 19:13:35 +01:00
|
|
|
MimeInfo * pgptext_find_signature (MimeInfo *mimeinfo)
|
2001-10-13 09:11:42 +02:00
|
|
|
{
|
2002-07-02 21:49:11 +02:00
|
|
|
return NULL;
|
2002-03-05 19:13:35 +01:00
|
|
|
}
|
2002-02-26 20:06:28 +01:00
|
|
|
|
2002-03-05 19:13:35 +01:00
|
|
|
gboolean pgptext_has_signature (MimeInfo *mimeinfo)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* check for the following strings:
|
|
|
|
* -----BEGIN PGP SIGNED MESSAGE-----
|
|
|
|
* ----- ????
|
|
|
|
* -----BEGIN PGP SIGNATURE-----
|
|
|
|
* -----END PGP SIGNATURE-----
|
|
|
|
*/
|
2001-10-13 09:11:42 +02:00
|
|
|
|
2002-03-05 19:13:35 +01:00
|
|
|
return 0;
|
2001-10-13 09:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pgptext_check_signature (MimeInfo *mimeinfo, FILE *fp)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int pgptext_is_encrypted (MimeInfo *mimeinfo, MsgInfo *msginfo)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
gchar *file, *tmpchk;
|
|
|
|
gchar buf[BUFFSIZE];
|
|
|
|
gboolean has_begin_pgp_msg = FALSE;
|
|
|
|
gboolean has_end_pgp_msg = FALSE;
|
|
|
|
gchar *check_begin_pgp_msg = "-----BEGIN PGP MESSAGE-----\n";
|
|
|
|
gchar *check_end_pgp_msg = "-----END PGP MESSAGE-----\n";
|
|
|
|
|
2002-07-02 21:49:11 +02:00
|
|
|
g_return_val_if_fail(msginfo != NULL, 0);
|
2001-10-13 09:11:42 +02:00
|
|
|
|
|
|
|
if (!mimeinfo)
|
|
|
|
return 0;
|
2002-03-05 19:13:35 +01:00
|
|
|
|
2002-07-02 21:49:11 +02:00
|
|
|
if ((fp = procmsg_open_message(msginfo)) == NULL) return 0;
|
2002-02-21 15:56:09 +01:00
|
|
|
mimeinfo = procmime_scan_mime_header(fp);
|
2001-10-13 09:11:42 +02:00
|
|
|
fclose(fp);
|
2002-07-02 21:49:11 +02:00
|
|
|
if (!mimeinfo) return 0;
|
2001-10-13 09:11:42 +02:00
|
|
|
|
|
|
|
file = procmsg_get_message_file_path(msginfo);
|
2002-07-02 21:49:11 +02:00
|
|
|
g_return_val_if_fail(file != NULL, 0);
|
2001-10-13 09:11:42 +02:00
|
|
|
|
|
|
|
if (mimeinfo->mime_type != MIME_TEXT) {
|
2002-03-14 11:17:32 +01:00
|
|
|
if ((fp = fopen(file, "rb")) == NULL) {
|
2001-10-13 09:11:42 +02:00
|
|
|
FILE_OP_ERROR(file, "fopen");
|
2002-09-22 02:06:59 +02:00
|
|
|
g_free(file);
|
2002-07-02 21:49:11 +02:00
|
|
|
return 0;
|
2001-10-13 09:11:42 +02:00
|
|
|
}
|
|
|
|
/* skip headers */
|
|
|
|
if (mimeinfo->mime_type == MIME_MULTIPART) {
|
|
|
|
if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
|
|
|
|
perror("fseek");
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL)
|
|
|
|
if (buf[0] == '\r' || buf[0] == '\n') break;
|
|
|
|
}
|
|
|
|
/* now check for a pgptext encrypted message */
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
|
|
|
tmpchk = g_strnfill(sizeof(buf), '\n');
|
|
|
|
memmove(tmpchk, &buf, sizeof(buf));
|
|
|
|
|
2002-03-05 19:13:35 +01:00
|
|
|
if (strstr(tmpchk, check_begin_pgp_msg) != NULL)
|
2001-10-13 09:11:42 +02:00
|
|
|
has_begin_pgp_msg = TRUE;
|
2002-03-05 19:13:35 +01:00
|
|
|
if (strstr(tmpchk, check_end_pgp_msg) != NULL)
|
2001-10-13 09:11:42 +02:00
|
|
|
has_end_pgp_msg = TRUE;
|
2002-03-05 19:13:35 +01:00
|
|
|
|
2001-10-13 09:11:42 +02:00
|
|
|
g_free(tmpchk);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
} else {
|
2002-03-14 11:17:32 +01:00
|
|
|
if ((fp = fopen(file, "rb")) == NULL) {
|
2001-10-13 09:11:42 +02:00
|
|
|
FILE_OP_ERROR(file, "fopen");
|
2002-09-22 02:06:59 +02:00
|
|
|
g_free(file);
|
2002-07-02 21:49:11 +02:00
|
|
|
return 0;
|
2001-10-13 09:11:42 +02:00
|
|
|
}
|
|
|
|
/* skip headers */
|
|
|
|
if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
|
|
|
|
perror("fseek");
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL)
|
|
|
|
if (buf[0] == '\r' || buf[0] == '\n') break;
|
|
|
|
|
|
|
|
/* now check for a pgptext encrypted message */
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
|
|
|
tmpchk = g_strnfill(sizeof(buf), '\n');
|
|
|
|
memmove(tmpchk, &buf, sizeof(buf));
|
|
|
|
|
2002-03-05 19:13:35 +01:00
|
|
|
if (strstr(tmpchk, check_begin_pgp_msg) != NULL)
|
2001-10-13 09:11:42 +02:00
|
|
|
has_begin_pgp_msg = TRUE;
|
2002-03-05 19:13:35 +01:00
|
|
|
if (strstr(tmpchk, check_end_pgp_msg) != NULL)
|
2001-10-13 09:11:42 +02:00
|
|
|
has_end_pgp_msg = TRUE;
|
2002-03-05 19:13:35 +01:00
|
|
|
|
2001-10-13 09:11:42 +02:00
|
|
|
g_free(tmpchk);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2002-03-05 19:13:35 +01:00
|
|
|
g_free(file);
|
2001-10-13 09:11:42 +02:00
|
|
|
|
|
|
|
/* do we have a proper message? */
|
|
|
|
if (has_begin_pgp_msg && has_end_pgp_msg)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pgptext_decrypt_message (MsgInfo *msginfo, MimeInfo *mimeinfo, FILE *fp)
|
|
|
|
{
|
|
|
|
static int id;
|
|
|
|
MimeInfo *partinfo;
|
|
|
|
char *fname;
|
|
|
|
GpgmeData plain;
|
|
|
|
FILE *dstfp;
|
|
|
|
size_t nread;
|
|
|
|
char buf[BUFFSIZE];
|
|
|
|
GpgmeError err;
|
|
|
|
|
|
|
|
g_return_if_fail (mimeinfo->mime_type == MIME_TEXT);
|
|
|
|
|
2001-12-24 11:23:17 +01:00
|
|
|
debug_print ("text/plain with pgptext encountered\n");
|
2001-10-13 09:11:42 +02:00
|
|
|
|
|
|
|
partinfo = procmime_scan_message(msginfo);
|
|
|
|
|
|
|
|
/* skip headers */
|
|
|
|
if (fseek(fp, partinfo->fpos, SEEK_SET) < 0)
|
|
|
|
perror("fseek");
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
|
|
|
partinfo->fpos = partinfo->fpos + strlen(buf);
|
|
|
|
if (buf[0] == '\r' || buf[0] == '\n') break;
|
|
|
|
}
|
|
|
|
/* get size */
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL)
|
|
|
|
partinfo->size = partinfo->size + strlen(buf);
|
|
|
|
|
|
|
|
plain = pgptext_decrypt (partinfo, fp);
|
|
|
|
if (!plain) {
|
|
|
|
msginfo->decryption_failed = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fname = g_strdup_printf("%s%cplaintext.%08x",
|
|
|
|
get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id);
|
|
|
|
|
2002-03-14 11:17:32 +01:00
|
|
|
if ((dstfp = fopen(fname, "wb")) == NULL) {
|
2001-10-13 09:11:42 +02:00
|
|
|
FILE_OP_ERROR(fname, "fopen");
|
|
|
|
g_free(fname);
|
|
|
|
msginfo->decryption_failed = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the orginal header to the new file */
|
|
|
|
if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
|
|
|
|
perror("fseek");
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), fp)) {
|
|
|
|
if (headerp (buf, content_names))
|
|
|
|
continue;
|
|
|
|
if (buf[0] == '\r' || buf[0] == '\n')
|
|
|
|
break;
|
|
|
|
fputs (buf, dstfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gpgme_data_rewind (plain);
|
|
|
|
if (err)
|
2001-12-24 11:23:17 +01:00
|
|
|
debug_print ("gpgme_data_rewind failed: %s\n", gpgme_strerror (err));
|
2001-10-13 09:11:42 +02:00
|
|
|
|
|
|
|
/* insert blank line to avoid some trouble... */
|
|
|
|
fputs ("\n", dstfp);
|
|
|
|
|
|
|
|
while (!(err = gpgme_data_read (plain, buf, sizeof(buf), &nread))) {
|
|
|
|
fwrite (buf, nread, 1, dstfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err != GPGME_EOF) {
|
2001-12-24 11:23:17 +01:00
|
|
|
debug_print ("gpgme_data_read failed: %s\n", gpgme_strerror (err));
|
2001-10-13 09:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose (dstfp);
|
|
|
|
|
|
|
|
msginfo->plaintext_file = fname;
|
|
|
|
msginfo->decryption_failed = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-03-05 19:13:35 +01:00
|
|
|
int pgptext_encrypt (const char *file, GSList *recp_list)
|
2001-10-13 09:11:42 +02:00
|
|
|
{
|
2002-07-02 21:49:11 +02:00
|
|
|
return 0;
|
2001-10-13 09:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int pgptext_sign (const char *file, PrefsAccount *ac)
|
|
|
|
{
|
2002-07-02 21:49:11 +02:00
|
|
|
return 0;
|
2002-03-05 19:13:35 +01:00
|
|
|
}
|
2001-10-13 09:11:42 +02:00
|
|
|
|
|
|
|
#endif /* USE_GPGME */
|
|
|
|
|