2 * This module allows Citadel to use clamd to filter incoming messages
3 * arriving via SMTP. For more information on clamd, visit
4 * http://clamav.net (the ClamAV project is not in any way
5 * affiliated with the Citadel project).
7 * Copyright (c) 1987-2012 by the citadel.org team
9 * This program is open source software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 3.
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.
18 #define CLAMD_PORT "3310"
28 #include <sys/types.h>
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
44 #include <sys/socket.h>
45 #include <libcitadel.h>
48 #include "citserver.h"
55 #include "internet_addressing.h"
57 #include "clientsocket.h"
58 #include "ctdl_module.h"
62 * Connect to the clamd server and scan a message.
64 int clamd(struct CtdlMessage *msg, recptypes *recp) {
66 int streamsock = (-1);
77 /* See if we have any clamd hosts configured */
78 num_clamhosts = get_hosts(clamhosts, "clamav");
79 if (num_clamhosts < 1) {
83 /* Try them one by one until we get a working one */
84 for (clamhost=0; clamhost<num_clamhosts; ++clamhost) {
85 extract_token(buf, clamhosts, clamhost, '|', sizeof buf);
86 syslog(LOG_INFO, "Connecting to clamd at <%s>\n", buf);
88 /* Assuming a host:port entry */
89 extract_token(hostbuf, buf, 0, ':', sizeof hostbuf);
90 if (extract_token(portbuf, buf, 1, ':', sizeof portbuf)==-1)
91 /* Didn't specify a port so we'll try the psuedo-standard 3310 */
92 sock = sock_connect(hostbuf, CLAMD_PORT);
94 /* Port specified lets try connecting to it! */
95 sock = sock_connect(hostbuf, portbuf);
97 if (sock >= 0) syslog(LOG_DEBUG, "Connected!\n");
101 /* If the service isn't running, just pass the mail
102 * through. Potentially throwing away mails isn't good.
107 CCC->SBuf.Buf = NewStrBuf();
108 CCC->sMigrateBuf = NewStrBuf();
109 CCC->SBuf.ReadWritePointer = NULL;
112 syslog(LOG_DEBUG, "Transmitting STREAM command\n");
113 sprintf(buf, "STREAM\r\n");
114 sock_write(&sock, buf, strlen(buf));
116 syslog(LOG_DEBUG, "Waiting for PORT number\n");
117 if (sock_getln(&sock, buf, sizeof buf) < 0) {
121 syslog(LOG_DEBUG, "<%s\n", buf);
122 if (strncasecmp(buf, "PORT", 4)!=0) {
126 /* Should have received a port number to connect to */
127 extract_token(portbuf, buf, 1, ' ', sizeof portbuf);
129 /* Attempt to establish connection to STREAM socket */
130 streamsock = sock_connect(hostbuf, portbuf);
132 if (streamsock < 0) {
133 /* If the service isn't running, just pass the mail
134 * through. Potentially throwing away mails isn't good.
136 FreeStrBuf(&CCC->SBuf.Buf);
137 FreeStrBuf(&CCC->sMigrateBuf);
141 syslog(LOG_DEBUG, "STREAM socket connected!\n");
146 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
147 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
148 msgtext = CC->redirect_buffer;
149 CC->redirect_buffer = NULL;
151 sock_write(&streamsock, SKEY(msgtext));
152 FreeStrBuf(&msgtext);
154 /* Close the streamsocket connection; this tells clamd
157 if (streamsock != -1) {
162 syslog(LOG_DEBUG, "Awaiting response\n");
163 if (sock_getln(&sock, buf, sizeof buf) < 0) {
166 syslog(LOG_DEBUG, "<%s\n", buf);
167 if (strncasecmp(buf, "stream: OK", 10)!=0) {
172 CM_SetField(msg, eErrorMsg, HKEY("message rejected by virus filter"));
176 FreeStrBuf(&CCC->SBuf.Buf);
177 FreeStrBuf(&CCC->sMigrateBuf);
182 CTDL_MODULE_INIT(virus)
186 CtdlRegisterMessageHook(clamd, EVT_SMTPSCAN);
189 /* return our module name for the log */