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