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