fix bug where multipart/alternative messages would slip through without being scanned
[claws.git] / src / plugins / clamav / clamav_plugin.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto and the Sylpheed-Claws Team
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <clamav.h>
28
29 #include "intl.h"
30 #include "plugin.h"
31 #include "utils.h"
32 #include "hooks.h"
33 #include "inc.h"
34 #include "mimeview.h"
35 #include "folder.h"
36 #include "prefs.h"
37 #include "prefs_gtk.h"
38
39 #include "clamav_plugin.h"
40
41 static gint hook_id;
42
43 static ClamAvConfig config;
44
45 static PrefParam param[] = {
46         {"clamav_enable", "FALSE", &config.clamav_enable, P_BOOL,
47          NULL, NULL, NULL},
48         {"clamav_enable_arc", "FALSE", &config.clamav_enable_arc, P_BOOL,
49          NULL, NULL, NULL},
50         {"clamav_max_size", "1", &config.clamav_max_size, P_USHORT,
51          NULL, NULL, NULL},
52         {"clamav_recv_infected", "TRUE", &config.clamav_recv_infected, P_BOOL,
53          NULL, NULL, NULL},
54         {"clamav_save_folder", NULL, &config.clamav_save_folder, P_STRING,
55          NULL, NULL, NULL},
56
57         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
58 };
59
60 static gboolean mail_filtering_hook(gpointer source, gpointer data)
61 {
62         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
63         MsgInfo *msginfo = mail_filtering_data->msginfo;
64         gboolean is_infected = FALSE;
65         FILE  *fp;
66         MimeInfo *mimeinfo;
67         MimeInfo *child;
68         gchar *infile;
69         gchar *outfile;
70         gint scan_archive = 0;
71
72         int ret, no;
73         unsigned long int size;
74         char *virname;
75         struct cl_node *root = NULL;
76         struct cl_limits limits;
77
78         if (!config.clamav_enable)
79                 return FALSE;
80
81         fp = procmsg_open_message(msginfo);
82         if (fp == NULL) {
83                 debug_print("failed to open message file");
84         } 
85         
86         mimeinfo = procmime_scan_message(msginfo);
87         if (!mimeinfo) return FALSE;
88
89         child = mimeinfo->children;
90         if (!child) {
91                 procmime_mimeinfo_free_all(mimeinfo);
92                 return FALSE;
93         }
94
95         debug_print("Scanning message %d for viruses\n", msginfo->msgnum);
96
97         infile = procmsg_get_message_file_path(msginfo);
98
99
100         if((ret = cl_loaddbdir(cl_retdbdir(), &root, &no))) {
101                 debug_print("cl_loaddbdir: %s\n", cl_perror(ret));
102                 exit(2);
103         }
104
105         debug_print("Loaded %d viruses.\n", no);
106
107         cl_buildtrie(root);
108
109         limits.maxfiles = 1000; /* max files */
110         limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
111         limits.maxreclevel = 8; /* maximum recursion level */
112
113         if (config.clamav_enable_arc)
114                 scan_archive = TRUE;
115
116         while (child != NULL) {
117                 if (child->children || child->mime_type == MIME_MULTIPART) {
118                         child = procmime_mimeinfo_next(child);
119                         continue;
120                 }
121                 if(child->parent && child->parent->parent
122                 && !strcasecmp(child->parent->parent->content_type, "multipart/signed")
123                 && child->mime_type == MIME_TEXT) {
124                         /* this is the main text part of a signed message */
125                         child = procmime_mimeinfo_next(child);
126                         continue;
127                 }
128                 outfile = procmime_get_tmp_file_name(child);
129                 if (procmime_get_part(outfile, infile, child) < 0)
130                         g_warning("Can't get the part of multipart message.");
131                 else {
132                         debug_print("Scanning %s\n", outfile);
133                         if ((ret = cl_scanfile(outfile, &virname, &size, root, 
134                                               &limits, scan_archive)) == CL_VIRUS) {
135                                 is_infected = TRUE;
136                                 debug_print("Detected %s virus.\n", virname); 
137                         } else {
138                                 debug_print("No virus detected.\n");
139                                 if (ret != CL_CLEAN)
140                                         debug_print("Error: %s\n", cl_perror(ret));
141                         }
142
143                         unlink(outfile);
144
145                         if (is_infected) break;
146                         child = child->next;
147                 }
148         }
149
150         if (is_infected) {
151                 if (config.clamav_recv_infected) {
152                         FolderItem *clamav_save_folder;
153
154                         if ((!config.clamav_save_folder) ||
155                             (config.clamav_save_folder[0] == '\0') ||
156                             ((clamav_save_folder = folder_find_item_from_identifier(config.clamav_save_folder)) == NULL))
157                                     clamav_save_folder = folder_get_default_trash();
158
159                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
160                         folder_item_move_msg(clamav_save_folder, msginfo);
161                 } else {
162                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
163                 }
164         }
165         
166         cl_freetrie(root);
167         g_free(infile);
168         procmime_mimeinfo_free_all(mimeinfo);
169         
170         return is_infected;
171 }
172
173 ClamAvConfig *clamav_get_config(void)
174 {
175         return &config;
176 }
177
178 void clamav_save_config(void)
179 {
180         PrefFile *pfile;
181         gchar *rcpath;
182
183         debug_print("Saving ClamAV Page\n");
184
185         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
186         pfile = prefs_write_open(rcpath);
187         g_free(rcpath);
188         if (!pfile || (prefs_set_block_label(pfile, "ClamAV") < 0))
189                 return;
190
191         if (prefs_write_param(param, pfile->fp) < 0) {
192                 g_warning("failed to write ClamAV configuration to file\n");
193                 prefs_file_close_revert(pfile);
194                 return;
195         }
196         fprintf(pfile->fp, "\n");
197
198         prefs_file_close(pfile);
199 }
200
201 gint plugin_init(gchar **error)
202 {
203         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
204         if (hook_id == -1) {
205                 *error = g_strdup("Failed to register mail filtering hook");
206                 return -1;
207         }
208
209         prefs_set_default(param);
210         prefs_read_config(param, "ClamAV", COMMON_RC);
211
212         debug_print("ClamAV plugin loaded\n");
213
214         return 0;
215         
216 }
217
218 void plugin_done(void)
219 {
220         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
221         g_free(config.clamav_save_folder);
222         
223         debug_print("ClamAV plugin unloaded\n");
224 }
225
226 const gchar *plugin_name(void)
227 {
228         return _("Clam AntiVirus");
229 }
230
231 const gchar *plugin_desc(void)
232 {
233         return _("This plugin uses Clam AntiVirus to scan all message attachments "
234                "that are received from a POP account.\n"
235                "\n"
236                "When a message attachment is found to contain a virus it can be "
237                "deleted or saved in a specially designated folder.\n"
238                "\n"
239                "This plugin only contains the actual function for scanning "
240                "and deleting or moving the message. You probably want to load "
241                "the Gtk+ User Interface plugin too, otherwise you will have to "
242                "manually write the plugin configuration.\n");
243 }
244
245 const gchar *plugin_type(void)
246 {
247         return "Common";
248 }