2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2007 the Claws Mail Team
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 3 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, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
36 #include "common/claws.h"
37 #include "common/version.h"
45 #include "prefs_gtk.h"
47 #include "clamav_plugin.h"
49 #define PLUGIN_NAME (_("Clam AntiVirus"))
52 static MessageCallback message_callback;
54 static ClamAvConfig config;
56 static PrefParam param[] = {
57 {"clamav_enable", "FALSE", &config.clamav_enable, P_BOOL,
59 {"clamav_enable_arc", "FALSE", &config.clamav_enable_arc, P_BOOL,
61 {"clamav_max_size", "1", &config.clamav_max_size, P_USHORT,
63 {"clamav_recv_infected", "TRUE", &config.clamav_recv_infected, P_BOOL,
65 {"clamav_save_folder", NULL, &config.clamav_save_folder, P_STRING,
68 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
72 static struct cl_engine *engine;
74 static struct cl_node *cl_database;
77 struct scan_parameters {
80 struct cl_limits limits;
82 struct cl_engine *engine;
86 gboolean scan_archive;
89 static gboolean scan_func(GNode *node, gpointer data)
91 struct scan_parameters *params = (struct scan_parameters *) data;
92 MimeInfo *mimeinfo = (MimeInfo *) node->data;
95 unsigned long int size;
98 outfile = procmime_get_tmp_file_name(mimeinfo);
99 if (procmime_get_part(outfile, mimeinfo) < 0)
100 g_warning("Can't get the part of multipart message.");
102 debug_print("Scanning %s\n", outfile);
104 if ((ret = cl_scanfile(outfile, &virname, &size, params->engine,
105 ¶ms->limits, params->scan_archive)) == CL_VIRUS) {
107 if ((ret = cl_scanfile(outfile, &virname, &size, params->root,
108 ¶ms->limits, params->scan_archive)) == CL_VIRUS) {
110 params->is_infected = TRUE;
111 debug_print("Detected %s virus.\n", virname);
113 debug_print("No virus detected.\n");
115 debug_print("Error: %s\n", cl_strerror(ret));
121 return params->is_infected;
124 static gboolean mail_filtering_hook(gpointer source, gpointer data)
126 MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
127 MsgInfo *msginfo = mail_filtering_data->msginfo;
130 struct scan_parameters params;
132 if (!config.clamav_enable)
135 mimeinfo = procmime_scan_message(msginfo);
136 if (!mimeinfo) return FALSE;
138 debug_print("Scanning message %d for viruses\n", msginfo->msgnum);
139 if (message_callback != NULL)
140 message_callback(_("ClamAV: scanning message..."));
142 params.is_infected = FALSE;
144 params.engine = engine;
146 params.root = cl_database;
149 params.limits.maxfiles = 1000; /* max files */
150 params.limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
151 params.limits.maxreclevel = 8; /* maximum recursion level */
152 params.limits.maxratio = 200; /* maximal compression ratio */
154 if (config.clamav_enable_arc)
155 params.scan_archive = TRUE;
157 g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, scan_func, ¶ms);
159 if (params.is_infected) {
160 if (config.clamav_recv_infected) {
161 FolderItem *clamav_save_folder;
163 if ((!config.clamav_save_folder) ||
164 (config.clamav_save_folder[0] == '\0') ||
165 ((clamav_save_folder = folder_find_item_from_identifier(config.clamav_save_folder)) == NULL))
166 clamav_save_folder = folder_get_default_trash();
168 procmsg_msginfo_unset_flags(msginfo, ~0, 0);
169 msginfo->filter_op = IS_MOVE;
170 msginfo->to_filter_folder = clamav_save_folder;
172 folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
176 procmime_mimeinfo_free_all(mimeinfo);
178 return params.is_infected;
181 ClamAvConfig *clamav_get_config(void)
186 void clamav_save_config(void)
191 debug_print("Saving ClamAV Page\n");
193 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
194 pfile = prefs_write_open(rcpath);
196 if (!pfile || (prefs_set_block_label(pfile, "ClamAV") < 0))
199 if (prefs_write_param(param, pfile->fp) < 0) {
200 g_warning("failed to write ClamAV configuration to file\n");
201 prefs_file_close_revert(pfile);
204 if (fprintf(pfile->fp, "\n") < 0) {
205 FILE_OP_ERROR(rcpath, "fprintf");
206 prefs_file_close_revert(pfile);
208 prefs_file_close(pfile);
211 void clamav_set_message_callback(MessageCallback callback)
213 message_callback = callback;
217 int cl_build(struct cl_engine *engine);
218 void cl_free(struct cl_engine *engine);
220 int cl_build(struct cl_node *root);
221 void cl_free(struct cl_node *root);
224 gint plugin_init(gchar **error)
229 unsigned int sigs = 0;
234 if (!check_plugin_version(MAKE_NUMERIC_VERSION(2,9,2,72),
235 VERSION_NUMERIC, PLUGIN_NAME, error))
238 hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
240 *error = g_strdup(_("Failed to register mail filtering hook"));
244 prefs_set_default(param);
245 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
246 prefs_read_config(param, "ClamAV", rcpath, NULL);
252 if ((ret = cl_load(cl_retdbdir(), &engine, &sigs, CL_DB_STDOPT))) {
253 debug_print("cl_load: %s\n", cl_strerror(ret));
254 *error = g_strdup_printf("cl_load: %s\n", cl_strerror(ret));
257 debug_print("Database loaded (containing in total %d signatures)\n", sigs);
259 if ((ret = cl_loaddbdir(cl_retdbdir(), &cl_database, &no))) {
260 debug_print("cl_loaddbdir: %s\n", cl_strerror(ret));
261 *error = g_strdup_printf("cl_loaddbdir: %s\n", cl_strerror(ret));
264 debug_print("Database loaded (containing in total %d signatures)\n", no);
269 if((ret = cl_build(engine))) {
271 if((ret = cl_build(cl_database))) {
273 debug_print("Database initialization error: %s\n", cl_strerror(ret));
274 *error = g_strdup_printf("Database initialization error: %s\n", cl_strerror(ret));
278 debug_print("ClamAV plugin loaded\n");
284 gboolean plugin_done(void)
286 hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
287 g_free(config.clamav_save_folder);
291 cl_free(cl_database);
295 debug_print("ClamAV plugin unloaded\n");
299 const gchar *plugin_name(void)
304 const gchar *plugin_desc(void)
306 return _("This plugin uses Clam AntiVirus to scan all messages that are "
307 "received from an IMAP, LOCAL or POP account.\n"
309 "When a message attachment is found to contain a virus it can be "
310 "deleted or saved in a specially designated folder.\n"
312 "Options can be found in /Configuration/Preferences/Plugins/Clam AntiVirus");
315 const gchar *plugin_type(void)
320 const gchar *plugin_licence(void)
325 const gchar *plugin_version(void)
330 struct PluginFeature *plugin_provides(void)
332 static struct PluginFeature features[] =
333 { {PLUGIN_FILTERING, N_("Virus detection")},
334 {PLUGIN_NOTHING, NULL}};