rename some params, resize max_size window, re-arrange the order of some gui elements...
[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/plugin.h"
30 #include "common/utils.h"
31 #include "hooks.h"
32 #include "inc.h"
33 #include "mimeview.h"
34 #include "folder.h"
35 #include "prefs.h"
36 #include "prefs_gtk.h"
37
38 #include "clamav_plugin.h"
39
40 static gint hook_id;
41
42 static ClamAvConfig config;
43
44 static PrefParam param[] = {
45         {"clamav_enable", "FALSE", &config.clamav_enable, P_BOOL,
46          NULL, NULL, NULL},
47         {"clamav_enable_arc", "FALSE", &config.clamav_enable_arc, P_BOOL,
48          NULL, NULL, NULL},
49         {"clamav_max_size", "1", &config.clamav_max_size, P_USHORT,
50          NULL, NULL, NULL},
51         {"clamav_recv_infected", "TRUE", &config.clamav_recv_infected, P_BOOL,
52          NULL, NULL, NULL},
53         {"clamav_save_folder", NULL, &config.clamav_save_folder, P_STRING,
54          NULL, NULL, NULL},
55
56         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
57 };
58
59 #define IS_FIRST_PART_TEXT(info) \
60         ((info->mime_type == MIME_TEXT || info->mime_type == MIME_TEXT_HTML || \
61           info->mime_type == MIME_TEXT_ENRICHED) || \
62          (info->mime_type == MIME_MULTIPART && info->content_type && \
63           !strcasecmp(info->content_type, "multipart/alternative") && \
64           (info->children && \
65            (info->children->mime_type == MIME_TEXT || \
66             info->children->mime_type == MIME_TEXT_HTML || \
67             info->children->mime_type == MIME_TEXT_ENRICHED))))
68
69 static gboolean mail_filtering_hook(gpointer source, gpointer data)
70 {
71         MailFilteringData *mail_filtering_data = (MailFilteringData *) source;
72         MsgInfo *msginfo = mail_filtering_data->msginfo;
73         gboolean is_infected = FALSE;
74         FILE  *fp;
75         MimeInfo *mimeinfo;
76         MimeInfo *child;
77         gchar *infile;
78         gchar *outfile;
79         gint scan_archive = 0;
80
81         int ret, no;
82         unsigned long int size;
83         long double kb;
84         char *virname;
85         struct cl_node *root = NULL;
86         struct cl_limits limits;
87
88         if (!config.clamav_enable)
89                 return FALSE;
90
91         fp = procmsg_open_message(msginfo);
92         if (fp == NULL) {
93                 debug_print("failed to open message file");
94         } 
95         
96         mimeinfo = procmime_scan_message(msginfo);
97         if (!mimeinfo) return FALSE;
98
99         child = mimeinfo->children;
100         if (!child || IS_FIRST_PART_TEXT(mimeinfo)) {
101                 procmime_mimeinfo_free_all(mimeinfo);
102                 return FALSE;
103         }
104
105         debug_print("Scanning message %d for viruses\n", msginfo->msgnum);
106
107         if (IS_FIRST_PART_TEXT(child))
108                 child = child->next;
109
110         infile = procmsg_get_message_file_path(msginfo);
111
112
113         if((ret = cl_loaddbdir(cl_retdbdir(), &root, &no))) {
114                 debug_print("cl_loaddbdir: %s\n", cl_perror(ret));
115                 exit(2);
116         }
117
118         debug_print("Loaded %d viruses.\n", no);
119
120         cl_buildtrie(root);
121
122         limits.maxfiles = 1000; /* max files */
123         limits.maxfilesize = config.clamav_max_size * 1048576; /* maximum archived file size */
124         limits.maxreclevel = 8; /* maximum recursion level */
125
126         if (config.clamav_enable_arc)
127                 scan_archive = TRUE;
128
129         while (child != NULL) {
130                 if (child->children || child->mime_type == MIME_MULTIPART) {
131                         child = procmime_mimeinfo_next(child);
132                         continue;
133                 }
134                 if(child->parent && child->parent->parent
135                 && !strcasecmp(child->parent->parent->content_type, "multipart/signed")
136                 && child->mime_type == MIME_TEXT) {
137                         /* this is the main text part of a signed message */
138                         child = procmime_mimeinfo_next(child);
139                         continue;
140                 }
141                 outfile = procmime_get_tmp_file_name(child);
142                 if (procmime_get_part(outfile, infile, child) < 0)
143                         g_warning("Can't get the part of multipart message.");
144                 else {
145                         debug_print("Scanning %s\n", outfile);
146                         if ((ret = cl_scanfile(outfile, &virname, &size, root, 
147                                               &limits, scan_archive)) == CL_VIRUS) {
148                                 is_infected = TRUE;
149                                 debug_print("Detected %s virus.\n", virname); 
150                         } else {
151                                 debug_print("No virus detected.\n");
152                                 if (ret != CL_CLEAN)
153                                         debug_print("Error: %s\n", cl_perror(ret));
154                         }
155
156                         kb = size * (CL_COUNT_PRECISION / 1024);
157                         debug_print("Data scanned: %2.2Lf Kb\n", kb);
158     
159                         unlink(outfile);
160
161                         if (is_infected) break;
162                         child = child->next;
163                 }
164         }
165
166         if (is_infected) {
167                 debug_print("message part(s) infected with %s\n", virname);
168                 if (config.clamav_recv_infected) {
169                         FolderItem *clamav_save_folder;
170
171                         if ((!config.clamav_save_folder) ||
172                             (config.clamav_save_folder[0] == '\0') ||
173                             ((clamav_save_folder = folder_find_item_from_identifier(config.clamav_save_folder)) == NULL))
174                                     clamav_save_folder = folder_get_default_trash();
175
176                         procmsg_msginfo_unset_flags(msginfo, ~0, 0);
177                         folder_item_move_msg(clamav_save_folder, msginfo);
178                 } else {
179                         folder_item_remove_msg(msginfo->folder, msginfo->msgnum);
180                 }
181         }
182         
183         cl_freetrie(root);
184         g_free(infile);
185         procmime_mimeinfo_free_all(mimeinfo);
186         
187         return is_infected;
188 }
189
190 #undef IS_FIRST_PART_TEXT
191
192 ClamAvConfig *clamav_get_config()
193 {
194         return &config;
195 }
196
197 void clamav_save_config()
198 {
199         PrefFile *pfile;
200         gchar *rcpath;
201
202         debug_print("Saving ClamAV Page\n");
203
204         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
205         pfile = prefs_write_open(rcpath);
206         g_free(rcpath);
207         if (!pfile || (prefs_set_block_label(pfile, "ClamAV") < 0))
208                 return;
209
210         if (prefs_write_param(param, pfile->fp) < 0) {
211                 g_warning("failed to write ClamAV configuration to file\n");
212                 prefs_file_close_revert(pfile);
213                 return;
214         }
215         fprintf(pfile->fp, "\n");
216
217         prefs_file_close(pfile);
218 }
219
220 gint plugin_init(gchar **error)
221 {
222         hook_id = hooks_register_hook(MAIL_FILTERING_HOOKLIST, mail_filtering_hook, NULL);
223         if (hook_id == -1) {
224                 *error = g_strdup("Failed to register mail filtering hook");
225                 return -1;
226         }
227
228         prefs_set_default(param);
229         prefs_read_config(param, "ClamAV", COMMON_RC);
230
231         debug_print("ClamAV plugin loaded\n");
232
233         return 0;
234         
235 }
236
237 void plugin_done()
238 {
239         hooks_unregister_hook(MAIL_FILTERING_HOOKLIST, hook_id);
240         g_free(config.clamav_save_folder);
241         
242         debug_print("ClamAV plugin unloaded\n");
243 }
244
245 const gchar *plugin_name()
246 {
247         return "Clam AntiVirus";
248 }
249
250 const gchar *plugin_desc()
251 {
252         return "This plugin uses Clam AntiVirus to scan all message attachments "
253                "that are received from a POP account.\n"
254                "\n"
255                "When a message attachment is found to contain a virus it can be "
256                "deleted or saved in a specially designated folder.\n"
257                "\n"
258                "This plugin only contains the actual function for scanning "
259                "and deleting or moving the message. You probably want to load "
260                "the Gtk+ User Interface plugin too, otherwise you will have to "
261                "manually write the plugin configuration.\n";
262 }
263
264 const gchar *plugin_type()
265 {
266         return "Common";
267 }