d71515674f7e89d6bea9849b26e821501facdc2c
[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., 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/sylpheed.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 static guint hook_id;
44 static MessageCallback message_callback;
45
46 static ClamAvConfig config;
47
48 static PrefParam param[] = {
49         {"clamav_enable", "FALSE", &config.clamav_enable, P_BOOL,
50          NULL, NULL, NULL},
51         {"clamav_enable_arc", "FALSE", &config.clamav_enable_arc, P_BOOL,
52          NULL, NULL, NULL},
53         {"clamav_max_size", "1", &config.clamav_max_size, P_USHORT,
54          NULL, NULL, NULL},
55         {"clamav_recv_infected", "TRUE", &config.clamav_recv_infected, P_BOOL,
56          NULL, NULL, NULL},
57         {"clamav_save_folder", NULL, &config.clamav_save_folder, P_STRING,
58          NULL, NULL, NULL},
59
60         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
61 };
62
63 static struct cl_node *cl_database;
64 struct scan_parameters {
65         gboolean is_infected;
66
67         struct cl_limits limits;
68         struct cl_node *root;
69         gboolean scan_archive;
70 };
71
72 static gboolean scan_func(GNode *node, gpointer data)
73 {
74         struct scan_parameters *params = (struct scan_parameters *) data;
75         MimeInfo *mimeinfo = (MimeInfo *) node->data;
76         gchar *outfile;
77         int ret;
78         unsigned long int size;
79         const char *virname;
80
81         outfile = procmime_get_tmp_file_name(mimeinfo);
82         if (procmime_get_part(outfile, mimeinfo) < 0)
83                 g_warning("Can't get the part of multipart message.");
84         else {
85                 debug_print("Scanning %s\n", outfile);
86                 if ((ret = cl_scanfile(outfile, &virname, &size, params->root, 
87                                       &params->limits, params->scan_archive)) == CL_VIRUS) {
88                         params->is_infected = TRUE;
89                         debug_print("Detected %s virus.\n", virname); 
90                 } else {
91                         debug_print("No virus detected.\n");
92                         if (ret != CL_CLEAN)
93                                 debug_print("Error: %s\n", cl_strerror(ret));
94                 }
95
96                 g_unlink(outfile);
97         }
98
99         return params->is_infected;
100 }
101
102 static gboolean mail_filtering_hook(gpointer source, gpointer data)
103 {
104         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
105         MsgInfo *msginfo = mail_filtering_data->msginfo;
106         MimeInfo *mimeinfo;
107
108         struct scan_parameters params;
109
110         if (!config.clamav_enable)
111                 return FALSE;
112
113         mimeinfo = procmime_scan_message(msginfo);
114         if (!mimeinfo) return FALSE;
115
116         debug_print("Scanning message %d for viruses\n", msginfo->msgnum);
117         if (message_callback != NULL)
118                 message_callback(_("ClamAV: scanning message..."));
119
120         params.is_infected = FALSE;
121         params.root = cl_database;
122
123         params.limits.maxfiles = 1000; /* max files */
124         params.limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
125         params.limits.maxreclevel = 8; /* maximum recursion level */
126         params.limits.maxratio = 200; /* maximal compression ratio */
127         params.limits.archivememlim = 0; /* disable memory limit for bzip2 scanner */
128
129         if (config.clamav_enable_arc)
130                 params.scan_archive = TRUE;
131
132         g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, scan_func, &params);
133
134         if (params.is_infected) {
135                 if (config.clamav_recv_infected) {
136                         FolderItem *clamav_save_folder;
137
138                         if ((!config.clamav_save_folder) ||
139                             (config.clamav_save_folder[0] == '\0') ||
140                             ((clamav_save_folder = folder_find_item_from_identifier(config.clamav_save_folder)) == NULL))
141                                     clamav_save_folder = folder_get_default_trash();
142
143                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
144                         folder_item_move_msg(clamav_save_folder, msginfo);
145                 } else {
146                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
147                 }
148         }
149         
150         procmime_mimeinfo_free_all(mimeinfo);
151         
152         return params.is_infected;
153 }
154
155 ClamAvConfig *clamav_get_config(void)
156 {
157         return &config;
158 }
159
160 void clamav_save_config(void)
161 {
162         PrefFile *pfile;
163         gchar *rcpath;
164
165         debug_print("Saving ClamAV Page\n");
166
167         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
168         pfile = prefs_write_open(rcpath);
169         g_free(rcpath);
170         if (!pfile || (prefs_set_block_label(pfile, "ClamAV") < 0))
171                 return;
172
173         if (prefs_write_param(param, pfile->fp) < 0) {
174                 g_warning("failed to write ClamAV configuration to file\n");
175                 prefs_file_close_revert(pfile);
176                 return;
177         }
178         fprintf(pfile->fp, "\n");
179
180         prefs_file_close(pfile);
181 }
182
183 void clamav_set_message_callback(MessageCallback callback)
184 {
185         message_callback = callback;
186 }
187
188 void cl_buildtrie(struct cl_node *root);
189 void cl_freetrie(struct cl_node *root);
190 gint plugin_init(gchar **error)
191 {
192         gchar *rcpath;
193         int no, ret;
194         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
195                 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the ClamAV plugin was built with");
196                 return -1;
197         }
198
199         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
200                 *error = g_strdup("Your version of Sylpheed-Claws is too old for the ClamAV plugin");
201                 return -1;
202         }
203
204         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
205         if (hook_id == -1) {
206                 *error = g_strdup("Failed to register mail filtering hook");
207                 return -1;
208         }
209
210         prefs_set_default(param);
211         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
212         prefs_read_config(param, "ClamAV", rcpath, NULL);
213         g_free(rcpath);
214
215         clamav_gtk_init();
216
217         if ((ret = cl_loaddbdir(cl_retdbdir(), &cl_database, &no))) {
218                 debug_print("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                 return -1;
227         }
228
229         debug_print("ClamAV plugin loaded\n");
230
231         return 0;
232         
233 }
234
235 void plugin_done(void)
236 {
237         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
238         g_free(config.clamav_save_folder);
239         cl_free(cl_database);
240         debug_print("ClamAV plugin unloaded\n");
241 }
242
243 const gchar *plugin_name(void)
244 {
245         return _("Clam AntiVirus");
246 }
247
248 const gchar *plugin_desc(void)
249 {
250         return _("This plugin uses Clam AntiVirus to scan all messages that are "
251                "received from an IMAP, LOCAL or POP account.\n"
252                "\n"
253                "When a message attachment is found to contain a virus it can be "
254                "deleted or saved in a specially designated folder.\n"
255                "\n");
256 }
257
258 const gchar *plugin_type(void)
259 {
260         return "GTK2";
261 }