fix bug #2879, 'Plain text mails treated as encrypted'
[claws.git] / src / plugins / pgpinline / pgpinline.c
index 338914bbaa112bd23386cc814c5321bd8119b5ca..a1ab2768b54bb708595c6c7cfd7ca19acb942173 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2011 Colin Leroy <colin@colino.net> and 
+ * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> and 
  * the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -20,6 +20,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef USE_GPGME
@@ -172,6 +173,22 @@ static gchar *get_part_as_string(MimeInfo *mimeinfo)
        return textdata;        
 }
 
+static gchar *pgpinline_locate_armor_header(gchar *textdata, const gchar *armor_header)
+{
+       gchar *pos;
+
+       pos = strstr(textdata, armor_header);
+       /*
+        * It's only a valid armor header if it's at the
+        * beginning of the buffer or a new line.
+        */
+       if (pos != NULL && (pos == textdata || *(pos-1) == '\n'))
+       {
+             return pos;
+       }
+       return NULL;
+}
+
 static gboolean pgpinline_is_signed(MimeInfo *mimeinfo)
 {
        PrivacyDataPGP *data = NULL;
@@ -363,7 +380,7 @@ static gboolean pgpinline_is_encrypted(MimeInfo *mimeinfo)
        if (!textdata)
                return FALSE;
        
-       if (!strstr(textdata, enc_indicator)) {
+       if (!pgpinline_locate_armor_header(textdata, enc_indicator)) {
                g_free(textdata);
                return FALSE;
        }
@@ -455,7 +472,7 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
        }
 
        /* Store any part before encrypted text */
-       pos = strstr(textdata, begin_indicator);
+       pos = pgpinline_locate_armor_header(textdata, begin_indicator);
        if (pos != NULL && (pos - textdata) > 0) {
            if (fwrite(textdata, 1, pos - textdata, dstfp) < pos - textdata) {
                FILE_OP_ERROR(fname, "fwrite");
@@ -475,10 +492,12 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
        if (len > 0) {
                if (fwrite(chars, 1, len, dstfp) < len) {
                        FILE_OP_ERROR(fname, "fwrite");
+                       g_free(chars);
                        privacy_set_error(_("Couldn't write to decrypted file %s"), fname);
                        goto FILE_ERROR;
                }
        }
+       g_free(chars);
        /* Store any part after encrypted text */
        if (fwrite(_("--- End of PGP/Inline encrypted data ---\n"), 1,
                strlen(_("--- End of PGP/Inline encrypted data ---\n")), 
@@ -488,7 +507,7 @@ static MimeInfo *pgpinline_decrypt(MimeInfo *mimeinfo)
                        goto FILE_ERROR;
        }
        if (pos != NULL) {
-           pos = strstr(pos, end_indicator);
+           pos = pgpinline_locate_armor_header(pos, end_indicator);
            if (pos != NULL && *pos != '\0') {
                pos += strlen(end_indicator);
                if (fwrite(pos, 1, strlen(pos), dstfp) < strlen(pos)) {