f1d4c5dd3446d0032324b3deda557539252bc03d
[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
127         if (config.clamav_enable_arc)
128                 params.scan_archive = TRUE;
129
130         g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, scan_func, &params);
131
132         if (params.is_infected) {
133                 if (config.clamav_recv_infected) {
134                         FolderItem *clamav_save_folder;
135
136                         if ((!config.clamav_save_folder) ||
137                             (config.clamav_save_folder[0] == '\0') ||
138                             ((clamav_save_folder = folder_find_item_from_identifier(config.clamav_save_folder)) == NULL))
139                                     clamav_save_folder = folder_get_default_trash();
140
141                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
142                         folder_item_move_msg(clamav_save_folder, msginfo);
143                 } else {
144                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
145                 }
146         }
147         
148         procmime_mimeinfo_free_all(mimeinfo);
149         
150         return params.is_infected;
151 }
152
153 ClamAvConfig *clamav_get_config(void)
154 {
155         return &config;
156 }
157
158 void clamav_save_config(void)
159 {
160         PrefFile *pfile;
161         gchar *rcpath;
162
163         debug_print("Saving ClamAV Page\n");
164
165         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
166         pfile = prefs_write_open(rcpath);
167         g_free(rcpath);
168         if (!pfile || (prefs_set_block_label(pfile, "ClamAV") < 0))
169                 return;
170
171         if (prefs_write_param(param, pfile->fp) < 0) {
172                 g_warning("failed to write ClamAV configuration to file\n");
173                 prefs_file_close_revert(pfile);
174                 return;
175         }
176         fprintf(pfile->fp, "\n");
177
178         prefs_file_close(pfile);
179 }
180
181 void clamav_set_message_callback(MessageCallback callback)
182 {
183         message_callback = callback;
184 }
185
186 void cl_buildtrie(struct cl_node *root);
187 void cl_freetrie(struct cl_node *root);
188 gint plugin_init(gchar **error)
189 {
190         gchar *rcpath;
191         int no, ret;
192         if ((sylpheed_get_version() > VERSION_NUMERIC)) {
193                 *error = g_strdup("Your version of Sylpheed-Claws is newer than the version the ClamAV plugin was built with");
194                 return -1;
195         }
196
197         if ((sylpheed_get_version() < MAKE_NUMERIC_VERSION(0, 9, 3, 86))) {
198                 *error = g_strdup("Your version of Sylpheed-Claws is too old for the ClamAV plugin");
199                 return -1;
200         }
201
202         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
203         if (hook_id == -1) {
204                 *error = g_strdup("Failed to register mail filtering hook");
205                 return -1;
206         }
207
208         prefs_set_default(param);
209         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
210         prefs_read_config(param, "ClamAV", rcpath, NULL);
211         g_free(rcpath);
212
213         clamav_gtk_init();
214
215         if ((ret = cl_loaddbdir(cl_retdbdir(), &cl_database, &no)) != 0) {
216                 debug_print("cl_loaddbdir: %s\n", cl_strerror(ret));
217                 return -1;
218         }
219
220         debug_print("Database loaded (containing in total %d signatures)\n", no);
221
222         cl_buildtrie(cl_database);
223
224         debug_print("ClamAV plugin loaded\n");
225
226         return 0;
227         
228 }
229
230 void plugin_done(void)
231 {
232         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
233         g_free(config.clamav_save_folder);
234         cl_freetrie(cl_database);
235         debug_print("ClamAV plugin unloaded\n");
236 }
237
238 const gchar *plugin_name(void)
239 {
240         return _("Clam AntiVirus");
241 }
242
243 const gchar *plugin_desc(void)
244 {
245         return _("This plugin uses Clam AntiVirus to scan all messages that are "
246                "received from an IMAP, LOCAL or POP account.\n"
247                "\n"
248                "When a message attachment is found to contain a virus it can be "
249                "deleted or saved in a specially designated folder.\n"
250                "\n");
251 }
252
253 const gchar *plugin_type(void)
254 {
255         return "GTK2";
256 }