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