dd6148121753243b55f6b822e27ebc4380af5b19
[claws.git] / src / plugins / clamd / libclamd / clamd-plugin.c
1 /* vim: set textwidth=80 tabstop=4: */
2
3 /*
4  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
5  * Copyright (C) 1999-2008 Michael Rasmussen and the Claws Mail Team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  * 
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #       include "config.h"
24 #endif
25
26 #include "defs.h"
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <gtk/gtkutils.h>
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <netinet/in.h>
42 #include <sys/un.h>
43 #include <arpa/inet.h>
44 #include <netdb.h>
45 #include <errno.h>
46
47 #include "common/claws.h"
48 #include "common/version.h"
49 #include "plugin.h"
50 #include "utils.h"
51 #include "prefs.h"
52 #include "folder.h"
53 #include "prefs_gtk.h"
54 #include "foldersel.h"
55 #include "statusbar.h"
56 #include "alertpanel.h"
57 #include "clamd-plugin.h"
58
59 /* needs to be generic */
60 static const gchar* config_dirs[] = { 
61         "/etc", 
62         "/usr/local/etc",
63         "/etc/clamav",
64         "/usr/local/etc/clamav",
65         NULL };
66
67 static const gchar* clamd_tokens[] = {
68         "LocalSocket",
69         "TCPSocket",
70         "TCPAddr",
71         NULL };
72
73 static Clamd_Socket* Socket = NULL;
74 static Config* config = NULL;
75
76 /**
77  *  clamd commands used
78  *  prefixing with either z or n is recommended
79  *  z <=> null terminated command
80  *  n <=> newline terminated command
81  */
82 static const gchar ping[] = "nPING\n";
83 static const gchar version[] = "nVERSION\n";
84 static const gchar scan[] = "nSCAN";
85 static const gchar contscan[] = "nCONTSCAN";
86 static const gchar instream[10] = "zINSTREAM\0";
87
88 void clamd_create_config_automatic(const gchar* path) {
89         FILE* conf;
90         char buf[1024];
91         gchar* key = NULL;
92         gchar* value = NULL;
93
94         /*debug_set_mode(TRUE);*/
95         /*debug_print("%s : %s\n", folder, path);*/
96         if (! path) {
97                 g_warning("Missing path");
98                 return;
99         }
100         if (config && config->ConfigType == AUTOMATIC &&
101                         config->automatic.folder &&
102                         strcmp(config->automatic.folder, path) == 0) {
103                 debug_print("%s : %s - Identical. No need to read again\n",
104                         config->automatic.folder, path);
105                 return;
106         }
107         if (config)
108                 clamd_config_free(config);
109         config = clamd_config_new();
110         
111         config->ConfigType = AUTOMATIC;
112         config->automatic.folder = g_strdup(path);
113         debug_print("Opening %s to parse config file\n", path);
114         conf = fopen(path, "r");
115         if (!conf) {
116                 /*g_error("%s: Unable to open", path);*/
117                 alertpanel_error(_("%s: Unable to open\nclamd will be disabled"), path);
118                 return;
119         }
120         while (fgets(buf, sizeof(buf), conf)) {
121                 g_strstrip(buf);
122                 if (buf[0] == '#')
123                         continue;
124                 const gchar** tokens = clamd_tokens;
125                 while (*tokens) {
126                         const gchar* token = *tokens++;
127                         if ((key = g_strstr_len(buf, strlen(buf), token)) != NULL) {
128                                 gchar* tmp = &(*(key + strlen(token)));
129                                 tmp = g_strchug(tmp);
130                                 gchar* end = index(tmp, '#');
131                                 if (end)
132                                         value = g_strndup(tmp, end - tmp);
133                                 else
134                                         value = g_strdup(g_strchomp(tmp));
135                                 if (strcmp(clamd_tokens[0], token) == 0) {
136                                         /* UNIX socket */
137                                         Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket *));
138                                         if (Socket) {
139                                                 Socket->socket.path = NULL;
140                                                 Socket->socket.host = NULL;
141                                                 Socket->socket.port = -1;
142                                                 Socket->type = UNIX_SOCKET;
143                                                 Socket->socket.path = g_strdup(value);
144                                                 g_free(value);
145                                                 value = NULL;
146                                                 fclose(conf);
147                                                 debug_print("clamctl: %s\n", Socket->socket.path);
148                                                 return;
149                                         }
150                                 }
151                                 else if (strcmp(clamd_tokens[1], token) == 0) {
152                                         /* INET socket */
153                                         if (! Socket) {
154                                                 Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket *));
155                                                 if (Socket) {
156                                                         Socket->socket.path = NULL;
157                                                         Socket->socket.host = NULL;
158                                                         Socket->socket.port = -1;
159                                                         Socket->type = INET_SOCKET;
160                                                         Socket->socket.port = atoi(value);
161                                                         Socket->socket.host = g_strdup("localhost");
162                                                         g_free(value);
163                                                         value = NULL;
164                                                         debug_print("clamctl: %s:%d\n", 
165                                                                 Socket->socket.host, Socket->socket.port);
166                                                 }
167                                         }
168                                         else {
169                                                 Socket->type = INET_SOCKET;
170                                                 Socket->socket.port = atoi(value);
171                                                 g_free(value);
172                                                 value = NULL;
173                                                 if (! Socket->socket.host)
174                                                         Socket->socket.host = g_strdup("localhost");
175                                                 debug_print("clamctl: %s:%d\n", 
176                                                         Socket->socket.host, Socket->socket.port);
177                                         }
178                                         /* We must continue since TCPAddr could also be configured */
179                                 }
180                                 else if (strcmp(clamd_tokens[2], token) == 0) {
181                                         if (! Socket) {
182                                                 Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket));
183                                                 if (Socket) {
184                                                         Socket->socket.path = NULL;
185                                                         Socket->socket.host = NULL;
186                                                         Socket->socket.port = 3310; /* default port */
187                                                         Socket->type = INET_SOCKET;
188                                                         Socket->socket.host = g_strdup(value);
189                                                         g_free(value);
190                                                         value = NULL;
191                                                         debug_print("clamctl: %s:%d\n", 
192                                                                 Socket->socket.host, Socket->socket.port);
193                                                 }
194                                         }
195                                         else {
196                                                 Socket->type = INET_SOCKET;
197                                                 if (Socket->socket.host)
198                                                         g_free(Socket->socket.host);
199                                                 Socket->socket.host = g_strdup(value);
200                                                 g_free(value);
201                                                 value = NULL;
202                                                 if (Socket->socket.port == -1)
203                                                         Socket->socket.port = 3310;
204                                                 debug_print("clamctl: %s:%d\n", 
205                                                         Socket->socket.host, Socket->socket.port);
206                                         }
207                                         /* We must continue since TCPSocket could also be configured */
208                                 }
209                         }
210                 }
211         }
212         fclose(conf);
213         if (! (Socket && (Socket->socket.port || Socket->socket.path))) {
214                 /*g_error("%s: Not able to find required information", path);*/
215                 alertpanel_error(_("%s: Not able to find required information\nclamd will be disabled"), path);
216         }
217         /*debug_set_mode(FALSE);*/
218 }
219
220 void clamd_create_config_manual(const gchar* host, int port) {
221         if (! host || port < 1) {
222                 g_warning("Missing host or port < 1");
223                 return;
224         }
225         if (config && config->ConfigType == MANUAL &&
226                         config->manual.host && config->manual.port == port &&
227                         strcmp(config->manual.host, host) == 0) {
228                 debug_print("%s : %s and %d : %d - Identical. No need to read again\n",
229                         config->manual.host, host, config->manual.port, port);
230                 return;
231         }
232
233         if (config)
234                 clamd_config_free(config);
235         config = clamd_config_new();
236         
237         config->ConfigType = MANUAL;
238         config->manual.host = g_strdup(host);
239         config->manual.port = port;
240         /* INET socket */
241         Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket));
242         if (Socket) {
243                 Socket->type = INET_SOCKET;
244                 Socket->socket.port = port;
245                 Socket->socket.host = g_strdup(host);
246         }
247         else {
248                 /*g_error("%s: Not able to find required information", path);*/
249                 alertpanel_error(_("Could not create socket"));
250         }
251 }
252
253 gboolean clamd_find_socket() {
254         const gchar** config_dir = config_dirs;
255         gchar *clamd_conf = NULL;
256         
257         while (*config_dir) {
258                 clamd_conf = g_strdup_printf("%s/clamd.conf", *config_dir++);
259                 debug_print("Looking for %s\n", clamd_conf);
260                 if (g_file_test(clamd_conf, G_FILE_TEST_EXISTS))
261                         break;
262                 g_free(clamd_conf);
263                 clamd_conf = NULL;
264         }
265         if (! clamd_conf)
266                 return FALSE;
267
268         debug_print("Using %s to find configuration\n", clamd_conf);
269         clamd_create_config_automatic(clamd_conf);
270         g_free(clamd_conf);
271
272         return TRUE;
273 }
274
275 Config* clamd_get_config() {
276         return config;
277 }
278
279 Clamd_Socket* clamd_get_socket() {
280         return Socket;
281 }
282
283 static int create_socket() {
284         struct sockaddr_un addr_u;
285         struct sockaddr_in addr_i;
286         struct hostent *hp;
287
288         int new_sock = -1;
289
290         /*debug_set_mode(TRUE);*/
291         if (! Socket) {
292                 return -1;
293         }
294         memset(&addr_u, 0, sizeof(addr_u));
295         memset(&addr_i, 0, sizeof(addr_i));
296         debug_print("socket->type: %d\n", Socket->type);
297         switch (Socket->type) {
298                 case UNIX_SOCKET:
299                         debug_print("socket path: %s\n", Socket->socket.path);
300                         new_sock = socket(PF_UNIX, SOCK_STREAM, 0);
301                         debug_print("socket file (create): %d\n", new_sock);
302                         if (new_sock < 0) 
303                                 return new_sock;
304                         addr_u.sun_family = AF_UNIX;
305                         memcpy(addr_u.sun_path, Socket->socket.path, 
306                                         strlen(Socket->socket.path));
307                         if (connect(new_sock, (struct sockaddr *) &addr_u, sizeof(addr_u)) < 0) {
308                                 perror("connect socket");
309                                 close(new_sock);
310                                 new_sock = -2;
311                         }
312                         debug_print("socket file (connect): %d\n", new_sock);
313                         break;
314                 case INET_SOCKET:
315                         addr_i.sin_family = AF_INET;
316                         addr_i.sin_port = htons(Socket->socket.port);
317                         hp = gethostbyname(Socket->socket.host);
318                         bcopy((void *)hp->h_addr, (void *)&addr_i.sin_addr, hp->h_length);
319                         new_sock = socket(PF_INET, SOCK_STREAM, 0);
320                         if (new_sock < 0)
321                                 return new_sock;
322                         if (connect(new_sock, (struct sockaddr *)&addr_i, sizeof(addr_i)) < 0) {
323                                 perror("connect socket");
324                                 close(new_sock);
325                                 new_sock = -2;
326                         }
327                         break;
328         }
329
330         return new_sock;
331 }
332
333 static void copy_socket(Clamd_Socket* sock) {
334         Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket));
335         Socket->socket.path = NULL;
336         Socket->socket.host = NULL;
337         Socket->type = sock->type;
338         if (Socket->type == UNIX_SOCKET) {
339                 Socket->socket.path = g_strdup(sock->socket.path);
340         }
341         else {
342                 Socket->socket.host = g_strdup(sock->socket.host);
343                 Socket->socket.port = sock->socket.port;
344         }
345 }
346
347 Clamd_Stat clamd_init(Clamd_Socket* config) {
348         gchar buf[BUFSIZ];
349         int n_read;
350         gboolean connect = FALSE;
351         int sock;
352
353         /*debug_set_mode(TRUE);*/
354         if (config != NULL && Socket != NULL)
355                 return NO_SOCKET;
356         if (config) {
357                 debug_print("socket: %s\n", config->socket.path);
358                 copy_socket(config);
359         }
360         sock = create_socket();
361         if (sock < 0) {
362                 debug_print("no connection\n");
363                 return NO_CONNECTION;
364         }
365         if (write(sock, ping, strlen(ping)) == -1) {
366                 debug_print("write error %d\n", errno);
367                 close(sock);
368                 return NO_CONNECTION;
369         }
370         memset(buf, '\0', sizeof(buf));
371         while ((n_read = read(sock, buf, BUFSIZ)) > 0) {
372                 n_read = n_read < BUFSIZ ? n_read : BUFSIZ - 1;
373                 buf[n_read] = '\0';
374                 if (buf[strlen(buf) - 1] == '\n')
375                         buf[strlen(buf) - 1] = '\0';
376                 debug_print("Ping result: %s\n", buf);
377                 if (strcmp("PONG", buf) == 0)
378                         connect = TRUE;
379         }
380         close(sock);
381         sock = create_socket();
382         if (sock < 0) {
383             debug_print("no connection\n");
384             return NO_CONNECTION;
385         }
386         if (write(sock, version, strlen(version)) == -1) {
387             debug_print("write error %d\n", errno);
388             close(sock);
389             return NO_CONNECTION;
390         }
391         memset(buf, '\0', sizeof(buf));
392         while ((n_read = read(sock, buf, sizeof(buf))) > 0) {
393             buf[n_read] = '\0';
394             if (buf[strlen(buf) - 1] == '\n')
395                 buf[strlen(buf) - 1] = '\0';
396             debug_print("Version: %s\n", buf);
397         }
398         close(sock);
399         /*debug_set_mode(FALSE);*/
400         return (connect) ? OK : NO_CONNECTION;
401 }
402
403 static Clamd_Stat clamd_stream_scan(int sock,
404                 const gchar* path, gchar** res, ssize_t size) {
405         int fd;
406         ssize_t count;
407         gchar buf[BUFSIZ];
408         int n_read;
409         int32_t chunk;
410         
411         debug_print("Scanning: %s\n", path);
412
413         memset(buf, '\0', sizeof(buf));
414
415         if (! res || size < 1) {
416                 return SCAN_ERROR;
417         }
418         if (! *res)
419                 *res = g_new(gchar, size);
420         memset(*res, '\0', size);
421         
422         if (! g_file_test(path, G_FILE_TEST_EXISTS)) {
423                 *res = g_strconcat("ERROR -> ", path, _(": File does not exist"), NULL);
424                 debug_print("res: %s\n", *res);
425                 return SCAN_ERROR;
426         }
427
428 #ifdef _LARGE_FILES
429         fd = open(path, O_RDONLY, O_LARGEFILE);
430 #else
431         fd = open(path, O_RDONLY);
432 #endif
433
434         if (fd < 0) {
435                 /*g_error("%s: Unable to open", path);*/
436                 *res = g_strconcat("ERROR -> ", path, _(": Unable to open"), NULL);
437                 return SCAN_ERROR;
438         }
439         
440         debug_print("command: %s\n", instream);
441         if (write(sock, instream, strlen(instream) + 1) == -1) {
442                 close(fd);
443                 return NO_CONNECTION;
444         }
445
446         while ((count = read(fd, (void *) buf, sizeof(buf))) > 0) {
447                 buf[sizeof(buf) - 1] = '\0';
448                 if (buf[strlen(buf) - 1] == '\n')
449                         buf[strlen(buf) - 1] = '\0';
450                 debug_print("read: %ld bytes\n", count);
451                 
452                 debug_print("chunk size: %ld\n", count);
453                 chunk = htonl(count);
454                 if (write(sock, &chunk, 4) == -1) {
455                         close(fd);
456                         *res = g_strconcat("ERROR -> ", _("Socket write error"), NULL);
457                         return SCAN_ERROR;
458                 }
459                 if (write(sock, buf, count) == -1) {
460                         close(fd);
461                         *res = g_strconcat("ERROR -> ", _("Socket write error"), NULL);
462                         return SCAN_ERROR;
463                 }
464                 memset(buf, '\0', sizeof(buf));
465         }
466         if (count == -1) {
467                 close(fd);
468                 *res = g_strconcat("ERROR -> ", path, _("%s: Error reading"), NULL);
469                 return SCAN_ERROR;
470         }
471         close(fd);
472         
473         chunk = htonl(0);
474         if (write(sock, &chunk, 4) == -1) {
475                 *res = g_strconcat("ERROR -> ", _("Socket write error"), NULL);
476                 return SCAN_ERROR;
477         }
478         
479         debug_print("reading from socket\n");
480         n_read = read(sock, *res, size);
481         if (n_read < 0) {
482                 *res = g_strconcat("ERROR -> ", _("Socket read error"), NULL);
483                 return SCAN_ERROR;
484         }
485         debug_print("received: %s\n", *res);
486         return OK;
487 }
488
489 Clamd_Stat clamd_verify_email(const gchar* path, response* result) {
490         gchar buf[BUFSIZ];
491         int n_read;
492         gchar* command;
493         Clamd_Stat stat;
494         int sock;
495
496         /*debug_set_mode(TRUE);*/
497         if (!result) {
498                 return SCAN_ERROR;
499         }
500         sock = create_socket();
501         if (sock < 0) {
502                 debug_print("no connection\n");
503                 return NO_CONNECTION;
504         }
505         memset(buf, '\0', sizeof(buf));
506         if (Socket->type == INET_SOCKET) {
507                 gchar* tmp = g_new0(gchar, BUFSIZ);
508                 stat = clamd_stream_scan(sock, path, &tmp, BUFSIZ);
509                 if (stat != OK) {
510                         close(sock);
511                         result->msg = g_strdup(tmp);
512                         g_free(tmp);
513                         debug_print("result: %s\n", result->msg);
514                         return stat;
515                 }
516                 debug_print("copy to buf: %s\n", tmp);
517                 memcpy(&buf, tmp, BUFSIZ);
518                 g_free(tmp);
519         }
520         else {
521                 command = g_strconcat(scan, " ", path, "\n", NULL);
522                 debug_print("command: %s\n", command);
523                 if (write(sock, command, strlen(command)) == -1) {
524                         debug_print("no connection\n");
525                         stat = NO_CONNECTION;
526                 }
527                 g_free(command);
528                 memset(buf, '\0', sizeof(buf));
529                 while ((n_read = read(sock, buf, BUFSIZ)) > 0) {
530                         n_read = n_read < BUFSIZ ? n_read : BUFSIZ - 1;
531                         buf[n_read] = '\0';
532                         if (buf[strlen(buf) - 1] == '\n')
533                                 buf[strlen(buf) - 1] = '\0';
534                 }
535         }
536         debug_print("response: %s\n", buf);
537         if (strstr(buf, "ERROR")) {
538                 stat = SCAN_ERROR;
539                 result->msg = g_strdup(buf);
540         }               
541         else if (strstr(buf, "FOUND")) {
542                 stat = VIRUS;
543                 result->msg = g_strdup(buf);
544         }               
545         else {
546                 stat = OK;
547                 result->msg = NULL;
548         }
549         close(sock);
550         /*debug_set_mode(FALSE);*/
551
552         return stat;
553 }
554
555 GSList* clamd_verify_dir(const gchar* path) {
556         gchar buf[BUFSIZ];
557         int n_read;
558         gchar* command;
559         GSList *list = NULL;
560         int sock;
561
562         if (Socket->type == INET_SOCKET)
563                 return list;
564
565         sock = create_socket();
566         if (sock < 0) {
567                 debug_print("No socket\n");
568                 return list;
569         }
570         command = g_strconcat(contscan, path, "\n", NULL);
571         debug_print("command: %s\n", command);
572         if (write(sock, command, strlen(command)) == -1) {
573                 debug_print("write error %d\n", errno);
574                 close(sock);
575                 return list;
576         }
577         g_free(command);
578         memset(buf, '\0', sizeof(buf));
579         while ((n_read = read(sock, buf, BUFSIZ)) > 0) {
580                 gchar** tmp = g_strsplit(buf, "\n", 0);
581                 gchar** head = tmp;
582                 while (*tmp) {
583                         gchar* file = *tmp++;
584                         debug_print("%s\n", file);
585                         if (strstr(file, "ERROR")) {
586                                 g_warning("%s", file);
587                                 /* dont report files with errors */
588                         }
589                         else if (strstr(file, "FOUND")) {
590                                 list = g_slist_append(list, g_strdup(file));
591                         }
592                 }
593                 g_strfreev(head);
594         }
595         close(sock);
596         return list;
597 }
598
599 void clamd_free_gslist(GSList* list) {
600         GSList* tmp = list;
601         while(tmp) {
602                 g_free(tmp->data);
603                 tmp = g_slist_next(tmp);
604         }
605         g_slist_free(list);
606 }
607
608 gchar* clamd_get_virus_name(gchar* msg) {
609         gchar *head, *tail, *name;
610
611         tail = g_strrstr_len(msg, strlen(msg), "FOUND");
612         if (! tail)
613                 return NULL;
614         head = g_strstr_len(msg, strlen(msg), ":");
615         ++head;
616         name = g_strndup(head, tail - head);
617         g_strstrip(name);
618         return name;
619 }
620
621 void clamd_free() {
622         if (Socket) {
623                 switch (Socket->type) {
624                     case UNIX_SOCKET:
625                         if (Socket->socket.path) {
626                             g_free(Socket->socket.path);
627                             Socket->socket.path = NULL;
628                         }
629                         break;
630                     case INET_SOCKET:
631                         if (Socket->socket.host) {
632                             g_free(Socket->socket.host);
633                             Socket->socket.host = NULL;
634                         }
635                         break;
636                 }
637                 g_free(Socket);
638                 Socket = NULL;
639         }
640         if (config) {
641             clamd_config_free(config);
642             config = NULL;
643         }
644 }
645
646 Config* clamd_config_new() {
647         return g_new0(Config, 1);
648 }
649
650 void clamd_config_free(Config* c) {
651         if (c->ConfigType == AUTOMATIC) {
652                 g_free(c->automatic.folder);
653                 c->automatic.folder = NULL;
654         }
655         else {
656                 g_free(c->manual.host);
657                 c->manual.host = NULL;
658         }
659         g_free(c);
660 }
661
662 gchar* int2char(int i) {
663         gchar* s = g_new0(gchar, 5);
664
665         sprintf(s, "%d", i);
666         
667         return s;
668 }
669
670 gchar* long2char(long l) {
671         gchar* s = g_new0(gchar, 5);
672
673         debug_print("l: %ld\n", l);
674         sprintf(s, "%ld", l);
675         debug_print("s: %s\n", s);
676         
677         return s;
678 }