2006-12-15 [wwp] 2.6.1cvs40
[claws.git] / src / plugins / clamav / clamav_plugin.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <glib/gi18n.h>
28 #include <clamav.h>
29
30 #include "common/claws.h"
31 #include "common/version.h"
32 #include "plugin.h"
33 #include "utils.h"
34 #include "hooks.h"
35 #include "inc.h"
36 #include "mimeview.h"
37 #include "folder.h"
38 #include "prefs.h"
39 #include "prefs_gtk.h"
40
41 #include "clamav_plugin.h"
42
43 #define PLUGIN_NAME (_("Clam AntiVirus"))
44
45 static guint hook_id;
46 static MessageCallback message_callback;
47
48 static ClamAvConfig config;
49
50 static PrefParam param[] = {
51         {"clamav_enable", "FALSE", &config.clamav_enable, P_BOOL,
52          NULL, NULL, NULL},
53         {"clamav_enable_arc", "FALSE", &config.clamav_enable_arc, P_BOOL,
54          NULL, NULL, NULL},
55         {"clamav_max_size", "1", &config.clamav_max_size, P_USHORT,
56          NULL, NULL, NULL},
57         {"clamav_recv_infected", "TRUE", &config.clamav_recv_infected, P_BOOL,
58          NULL, NULL, NULL},
59         {"clamav_save_folder", NULL, &config.clamav_save_folder, P_STRING,
60          NULL, NULL, NULL},
61
62         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
63 };
64
65 static struct cl_node *cl_database;
66 struct scan_parameters {
67         gboolean is_infected;
68
69         struct cl_limits limits;
70         struct cl_node *root;
71         gboolean scan_archive;
72 };
73
74 static gboolean scan_func(GNode *node, gpointer data)
75 {
76         struct scan_parameters *params = (struct scan_parameters *) data;
77         MimeInfo *mimeinfo = (MimeInfo *) node->data;
78         gchar *outfile;
79         int ret;
80         unsigned long int size;
81         const char *virname;
82
83         outfile = procmime_get_tmp_file_name(mimeinfo);
84         if (procmime_get_part(outfile, mimeinfo) < 0)
85                 g_warning("Can't get the part of multipart message.");
86         else {
87                 debug_print("Scanning %s\n", outfile);
88                 if ((ret = cl_scanfile(outfile, &virname, &size, params->root, 
89                                       &params->limits, params->scan_archive)) == CL_VIRUS) {
90                         params->is_infected = TRUE;
91                         debug_print("Detected %s virus.\n", virname); 
92                 } else {
93                         debug_print("No virus detected.\n");
94                         if (ret != CL_CLEAN)
95                                 debug_print("Error: %s\n", cl_strerror(ret));
96                 }
97
98                 g_unlink(outfile);
99         }
100
101         return params->is_infected;
102 }
103
104 static gboolean mail_filtering_hook(gpointer source, gpointer data)
105 {
106         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
107         MsgInfo *msginfo = mail_filtering_data->msginfo;
108         MimeInfo *mimeinfo;
109
110         struct scan_parameters params;
111
112         if (!config.clamav_enable)
113                 return FALSE;
114
115         mimeinfo = procmime_scan_message(msginfo);
116         if (!mimeinfo) return FALSE;
117
118         debug_print("Scanning message %d for viruses\n", msginfo->msgnum);
119         if (message_callback != NULL)
120                 message_callback(_("ClamAV: scanning message..."));
121
122         params.is_infected = FALSE;
123         params.root = cl_database;
124
125         params.limits.maxfiles = 1000; /* max files */
126         params.limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
127         params.limits.maxreclevel = 8; /* maximum recursion level */
128         params.limits.maxratio = 200; /* maximal compression ratio */
129         params.limits.archivememlim = 0; /* disable memory limit for bzip2 scanner */
130
131         if (config.clamav_enable_arc)
132                 params.scan_archive = TRUE;
133
134         g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, scan_func, &params);
135
136         if (params.is_infected) {
137                 if (config.clamav_recv_infected) {
138                         FolderItem *clamav_save_folder;
139
140                         if ((!config.clamav_save_folder) ||
141                             (config.clamav_save_folder[0] == '\0') ||
142                             ((clamav_save_folder = folder_find_item_from_identifier(config.clamav_save_folder)) == NULL))
143                                     clamav_save_folder = folder_get_default_trash();
144
145                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
146                         msginfo->is_move = TRUE;
147                         msginfo->to_filter_folder = clamav_save_folder;
148                 } else {
149                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
150                 }
151         }
152         
153         procmime_mimeinfo_free_all(mimeinfo);
154         
155         return params.is_infected;
156 }
157
158 ClamAvConfig *clamav_get_config(void)
159 {
160         return &config;
161 }
162
163 void clamav_save_config(void)
164 {
165         PrefFile *pfile;
166         gchar *rcpath;
167
168         debug_print("Saving ClamAV Page\n");
169
170         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
171         pfile = prefs_write_open(rcpath);
172         g_free(rcpath);
173         if (!pfile || (prefs_set_block_label(pfile, "ClamAV") < 0))
174                 return;
175
176         if (prefs_write_param(param, pfile->fp) < 0) {
177                 g_warning("failed to write ClamAV configuration to file\n");
178                 prefs_file_close_revert(pfile);
179                 return;
180         }
181         fprintf(pfile->fp, "\n");
182
183         prefs_file_close(pfile);
184 }
185
186 void clamav_set_message_callback(MessageCallback callback)
187 {
188         message_callback = callback;
189 }
190
191 int cl_build(struct cl_node *root);
192 void cl_free(struct cl_node *root);
193 gint plugin_init(gchar **error)
194 {
195         gchar *rcpath;
196         int ret;
197         unsigned int no;
198
199         if (!check_plugin_version(MAKE_NUMERIC_VERSION(0, 9, 3, 86),
200                                 VERSION_NUMERIC, PLUGIN_NAME, error))
201                 return -1;
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         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
211         prefs_read_config(param, "ClamAV", rcpath, NULL);
212         g_free(rcpath);
213
214         clamav_gtk_init();
215
216         if ((ret = cl_loaddbdir(cl_retdbdir(), &cl_database, &no))) {
217                 debug_print("cl_loaddbdir: %s\n", cl_strerror(ret));
218                 *error = g_strdup_printf("cl_loaddbdir: %s\n", cl_strerror(ret));
219                 return -1;
220         }
221
222         debug_print("Database loaded (containing in total %d signatures)\n", no);
223
224         if((ret = cl_build(cl_database))) {
225                 debug_print("Database initialization error: %s\n", cl_strerror(ret));
226                 *error = g_strdup_printf("Database initialization error: %s\n", cl_strerror(ret));
227                 return -1;
228         }
229
230         debug_print("ClamAV plugin loaded\n");
231
232         return 0;
233         
234 }
235
236 void plugin_done(void)
237 {
238         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
239         g_free(config.clamav_save_folder);
240         cl_free(cl_database);
241         clamav_gtk_done();
242
243         debug_print("ClamAV plugin unloaded\n");
244 }
245
246 const gchar *plugin_name(void)
247 {
248         return PLUGIN_NAME;
249 }
250
251 const gchar *plugin_desc(void)
252 {
253         return _("This plugin uses Clam AntiVirus to scan all messages that are "
254                "received from an IMAP, LOCAL or POP account.\n"
255                "\n"
256                "When a message attachment is found to contain a virus it can be "
257                "deleted or saved in a specially designated folder.\n"
258                "\n"
259                "Options can be found in /Configuration/Preferences/Plugins/Clam AntiVirus");
260 }
261
262 const gchar *plugin_type(void)
263 {
264         return "GTK2";
265 }
266
267 const gchar *plugin_licence(void)
268 {
269         return "GPL";
270 }
271
272 const gchar *plugin_version(void)
273 {
274         return VERSION;
275 }
276
277 struct PluginFeature *plugin_provides(void)
278 {
279         static struct PluginFeature features[] = 
280                 { {PLUGIN_FILTERING, N_("Virus detection")},
281                   {PLUGIN_NOTHING, NULL}};
282         return features;
283 }