1 /* vim: set textwidth=80 tabstop=4: */
4 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
5 * Copyright (C) 1999-2017 Michael Rasmussen and the Claws Mail Team
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.
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.
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/>.
29 #include <glib/gi18n.h>
31 #include <gtk/gtkutils.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <netinet/in.h>
43 #include <arpa/inet.h>
47 #include "common/claws.h"
48 #include "common/version.h"
53 #include "prefs_gtk.h"
54 #include "foldersel.h"
55 #include "statusbar.h"
56 #include "alertpanel.h"
57 #include "clamd-plugin.h"
60 #define UNIX_PATH_MAX 108
63 /* needs to be generic */
64 static const gchar* config_dirs[] = {
68 "/usr/local/etc/clamav",
71 static const gchar* clamd_tokens[] = {
77 static Clamd_Socket* Socket = NULL;
78 static Config* config = NULL;
82 * prefixing with either z or n is recommended
83 * z <=> null terminated command
84 * n <=> newline terminated command
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";
92 void clamd_create_config_automatic(const gchar* path) {
98 /*debug_set_mode(TRUE);*/
99 /*debug_print("%s : %s\n", folder, path);*/
101 g_warning("Missing path");
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);
112 clamd_config_free(config);
113 config = clamd_config_new();
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");
120 /*g_error("%s: Unable to open", path);*/
121 alertpanel_error(_("%s: Unable to open\nclamd will be disabled"), path);
124 while (fgets(buf, sizeof(buf), conf)) {
128 const gchar** tokens = clamd_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, '#');
136 value = g_strndup(tmp, end - tmp);
138 value = g_strdup(g_strchomp(tmp));
139 if (strcmp(clamd_tokens[0], token) == 0) {
141 Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket));
143 Socket->socket.host = NULL;
144 Socket->socket.port = -1;
145 Socket->type = UNIX_SOCKET;
146 Socket->socket.path = g_strdup(value);
150 debug_print("clamctl: %s\n", Socket->socket.path);
154 else if (strcmp(clamd_tokens[1], token) == 0) {
157 Socket = (Clamd_Socket *) malloc(sizeof(Clamd_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");
166 debug_print("clamctl: %s:%d\n",
167 Socket->socket.host, Socket->socket.port);
171 Socket->type = INET_SOCKET;
172 Socket->socket.port = atoi(value);
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);
180 /* We must continue since TCPAddr could also be configured */
182 else if (strcmp(clamd_tokens[2], token) == 0) {
184 Socket = (Clamd_Socket *) malloc(sizeof(Clamd_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);
192 debug_print("clamctl: %s:%d\n",
193 Socket->socket.host, Socket->socket.port);
197 Socket->type = INET_SOCKET;
198 if (Socket->socket.host)
199 g_free(Socket->socket.host);
200 Socket->socket.host = g_strdup(value);
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);
208 /* We must continue since TCPSocket could also be configured */
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);
218 /*debug_set_mode(FALSE);*/
221 void clamd_create_config_manual(const gchar* host, int port) {
222 if (! host || port < 1) {
223 g_warning("Missing host or port < 1");
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);
235 clamd_config_free(config);
236 config = clamd_config_new();
238 config->ConfigType = MANUAL;
239 config->manual.host = g_strdup(host);
240 config->manual.port = port;
242 Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket));
244 Socket->type = INET_SOCKET;
245 Socket->socket.port = port;
246 Socket->socket.host = g_strdup(host);
249 /*g_error("%s: Not able to find required information", path);*/
250 alertpanel_error(_("Could not create socket"));
254 gboolean clamd_find_socket() {
255 const gchar** config_dir = config_dirs;
256 gchar *clamd_conf = NULL;
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))
269 debug_print("Using %s to find configuration\n", clamd_conf);
270 clamd_create_config_automatic(clamd_conf);
276 Config* clamd_get_config() {
280 Clamd_Socket* clamd_get_socket() {
284 static int create_socket() {
285 struct sockaddr_un addr_u;
286 struct sockaddr_in addr_i;
291 /*debug_set_mode(TRUE);*/
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) {
300 debug_print("socket path: %s\n", Socket->socket.path);
301 new_sock = socket(PF_UNIX, SOCK_STREAM, 0);
303 perror("create socket");
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);
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");
321 debug_print("socket file (connect): %d\n", new_sock);
324 addr_i.sin_family = AF_INET;
325 addr_i.sin_port = htons(Socket->socket.port);
326 hp = gethostbyname(Socket->socket.host);
328 g_error("fail to get host by: %s", Socket->socket.host);
331 debug_print("IP socket host: %s:%d\n",
332 Socket->socket.host, Socket->socket.port);
333 bcopy((void *)hp->h_addr, (void *)&addr_i.sin_addr, hp->h_length);
334 new_sock = socket(PF_INET, SOCK_STREAM, 0);
336 perror("create socket");
339 debug_print("IP socket (create): %d\n", new_sock);
340 if (connect(new_sock, (struct sockaddr *)&addr_i, sizeof(addr_i)) < 0) {
341 perror("connect socket");
345 debug_print("IP socket (connect): %d\n", new_sock);
352 static void copy_socket(Clamd_Socket* sock) {
353 Socket = (Clamd_Socket *) malloc(sizeof(Clamd_Socket));
354 Socket->type = sock->type;
355 if (Socket->type == UNIX_SOCKET) {
356 Socket->socket.path = g_strdup(sock->socket.path);
357 Socket->socket.host = NULL;
360 Socket->socket.path = NULL;
361 Socket->socket.host = g_strdup(sock->socket.host);
362 Socket->socket.port = sock->socket.port;
366 Clamd_Stat clamd_init(Clamd_Socket* config) {
369 gboolean connect = FALSE;
372 /*debug_set_mode(TRUE);*/
373 if (config != NULL && Socket != NULL)
376 debug_print("socket: %s\n", config->socket.path);
379 sock = create_socket();
381 debug_print("no connection\n");
382 return NO_CONNECTION;
384 if (write(sock, ping, strlen(ping)) == -1) {
385 debug_print("write error %d\n", errno);
387 return NO_CONNECTION;
389 memset(buf, '\0', sizeof(buf));
390 while ((n_read = read(sock, buf, BUFSIZ - 1)) > 0) {
392 if (buf[n_read - 1] == '\n')
393 buf[n_read - 1] = '\0';
394 debug_print("Ping result: %s\n", buf);
395 if (strcmp("PONG", buf) == 0)
399 sock = create_socket();
401 debug_print("no connection\n");
402 return NO_CONNECTION;
404 if (write(sock, version, strlen(version)) == -1) {
405 debug_print("write error %d\n", errno);
407 return NO_CONNECTION;
409 memset(buf, '\0', sizeof(buf));
410 while ((n_read = read(sock, buf, BUFSIZ - 1)) > 0) {
412 if (buf[n_read - 1] == '\n')
413 buf[n_read - 1] = '\0';
414 debug_print("Version: %s\n", buf);
417 /*debug_set_mode(FALSE);*/
418 return (connect) ? OK : NO_CONNECTION;
421 static Clamd_Stat clamd_stream_scan(int sock,
422 const gchar* path, gchar** res, ssize_t size) {
429 debug_print("Scanning: %s\n", path);
431 memset(buf, '\0', sizeof(buf));
433 if (! res || size < 1) {
437 *res = g_new(gchar, size);
438 memset(*res, '\0', size);
440 if (! g_file_test(path, G_FILE_TEST_EXISTS)) {
441 *res = g_strconcat("ERROR -> ", path, _(": File does not exist"), NULL);
442 debug_print("res: %s\n", *res);
447 fd = open(path, O_RDONLY, O_LARGEFILE);
449 fd = open(path, O_RDONLY);
453 /*g_error("%s: Unable to open", path);*/
454 *res = g_strconcat("ERROR -> ", path, _(": Unable to open"), NULL);
458 debug_print("command: %s\n", instream);
459 if (write(sock, instream, strlen(instream) + 1) == -1) {
461 return NO_CONNECTION;
464 while ((count = read(fd, (void *) buf, BUFSIZ - 1)) > 0) {
466 if (buf[count - 1] == '\n')
467 buf[count - 1] = '\0';
469 debug_print("chunk size: %ld\n", count);
470 chunk = htonl(count);
471 if (write(sock, &chunk, 4) == -1) {
473 *res = g_strconcat("ERROR -> ", _("Socket write error"), NULL);
476 if (write(sock, buf, count) == -1) {
478 *res = g_strconcat("ERROR -> ", _("Socket write error"), NULL);
481 memset(buf, '\0', BUFSIZ - 1);
485 *res = g_strconcat("ERROR -> ", path, _("%s: Error reading"), NULL);
491 if (write(sock, &chunk, 4) == -1) {
492 *res = g_strconcat("ERROR -> ", _("Socket write error"), NULL);
496 debug_print("reading from socket\n");
497 n_read = read(sock, *res, size);
499 *res = g_strconcat("ERROR -> ", _("Socket read error"), NULL);
502 (*res)[n_read] = '\0';
503 debug_print("received: %s\n", *res);
507 Clamd_Stat clamd_verify_email(const gchar* path, response* result) {
514 /*debug_set_mode(TRUE);*/
518 sock = create_socket();
520 debug_print("no connection (socket create)\n");
521 return NO_CONNECTION;
523 memset(buf, '\0', sizeof(buf));
524 if (Socket->type == INET_SOCKET) {
525 gchar* tmp = g_new0(gchar, BUFSIZ);
526 stat = clamd_stream_scan(sock, path, &tmp, BUFSIZ);
529 result->msg = g_strdup(tmp);
531 debug_print("result: %s\n", result->msg);
534 debug_print("copy to buf: %s\n", tmp);
535 memcpy(&buf, tmp, BUFSIZ);
537 debug_print("response: %s\n", buf);
540 command = g_strconcat(scan, " ", path, "\n", NULL);
541 debug_print("command: %s\n", command);
542 if (write(sock, command, strlen(command)) == -1) {
543 debug_print("no connection (socket write)\n");
545 return NO_CONNECTION;
548 memset(buf, '\0', sizeof(buf));
549 /* shouldn't we read only once here? we're checking the last response line anyway */
550 while ((n_read = read(sock, buf, BUFSIZ - 1)) > 0) {
552 if (buf[n_read - 1] == '\n')
553 buf[n_read - 1] = '\0';
554 debug_print("response: %s\n", buf);
558 debug_print("response: %s\n", buf);
561 if (strstr(buf, "ERROR")) {
563 result->msg = g_strdup(buf);
565 else if (strstr(buf, "FOUND")) {
567 result->msg = g_strdup(buf);
574 /*debug_set_mode(FALSE);*/
579 GSList* clamd_verify_dir(const gchar* path) {
586 if (Socket->type == INET_SOCKET)
589 sock = create_socket();
591 debug_print("No socket\n");
594 command = g_strconcat(contscan, path, "\n", NULL);
595 debug_print("command: %s\n", command);
596 if (write(sock, command, strlen(command)) == -1) {
597 debug_print("write error %d\n", errno);
602 memset(buf, '\0', sizeof(buf));
603 while ((n_read = read(sock, buf, BUFSIZ - 1)) > 0) {
604 gchar** tmp = g_strsplit(buf, "\n", 0);
607 gchar* file = *tmp++;
608 debug_print("%s\n", file);
609 if (strstr(file, "ERROR")) {
610 g_warning("%s", file);
611 /* dont report files with errors */
613 else if (strstr(file, "FOUND")) {
614 list = g_slist_append(list, g_strdup(file));
623 void clamd_free_gslist(GSList* list) {
627 tmp = g_slist_next(tmp);
632 gchar* clamd_get_virus_name(gchar* msg) {
633 gchar *head, *tail, *name;
635 tail = g_strrstr_len(msg, strlen(msg), "FOUND");
638 head = g_strstr_len(msg, strlen(msg), ":");
640 name = g_strndup(head, tail - head);
647 switch (Socket->type) {
649 if (Socket->socket.path) {
650 g_free(Socket->socket.path);
651 Socket->socket.path = NULL;
655 if (Socket->socket.host) {
656 g_free(Socket->socket.host);
657 Socket->socket.host = NULL;
665 clamd_config_free(config);
670 Config* clamd_config_new() {
671 return g_new0(Config, 1);
674 void clamd_config_free(Config* c) {
675 if (c->ConfigType == AUTOMATIC) {
676 g_free(c->automatic.folder);
677 c->automatic.folder = NULL;
680 g_free(c->manual.host);
681 c->manual.host = NULL;
686 gchar* int2char(int i) {
687 gchar* s = g_new0(gchar, 5);
694 gchar* long2char(long l) {
695 gchar* s = g_new0(gchar, 5);
697 debug_print("l: %ld\n", l);
698 sprintf(s, "%ld", l);
699 debug_print("s: %s\n", s);