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