4 * This module allows Citadel to use clamd to filter incoming messages
5 * arriving via SMTP. For more information on clamd, visit
6 * http://clamav.net (the ClamAV project is not in any way
7 * affiliated with the Citadel project).
9 * Copyright (c) 1987-2009 by the citadel.org team
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define CLAMD_PORT "3310"
36 #include <sys/types.h>
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
43 # include <sys/time.h>
52 #include <sys/socket.h>
53 #include <libcitadel.h>
56 #include "citserver.h"
63 #include "internet_addressing.h"
65 #include "clientsocket.h"
68 #include "ctdl_module.h"
73 * Connect to the clamd server and scan a message.
75 int clamd(struct CtdlMessage *msg) {
77 int streamsock = (-1);
88 /* Don't care if you're logged in. You can still spread viruses.
90 /* if (CC->logged_in) return(0); */
92 /* See if we have any clamd hosts configured */
93 num_clamhosts = get_hosts(clamhosts, "clamav");
94 if (num_clamhosts < 1) return(0);
96 /* Try them one by one until we get a working one */
97 for (clamhost=0; clamhost<num_clamhosts; ++clamhost) {
98 extract_token(buf, clamhosts, clamhost, '|', sizeof buf);
99 CtdlLogPrintf(CTDL_INFO, "Connecting to clamd at <%s>\n", buf);
101 /* Assuming a host:port entry */
102 extract_token(hostbuf, buf, 0, ':', sizeof hostbuf);
103 if (extract_token(portbuf, buf, 1, ':', sizeof portbuf)==-1)
104 /* Didn't specify a port so we'll try the psuedo-standard 3310 */
105 sock = sock_connect(hostbuf, CLAMD_PORT);
107 /* Port specified lets try connecting to it! */
108 sock = sock_connect(hostbuf, portbuf);
110 if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
114 /* If the service isn't running, just pass the mail
115 * through. Potentially throwing away mails isn't good.
120 CCC->sReadBuf = NewStrBuf();
121 CCC->sMigrateBuf = NewStrBuf();
125 CtdlLogPrintf(CTDL_DEBUG, "Transmitting STREAM command\n");
126 sprintf(buf, "STREAM\r\n");
127 sock_write(&sock, buf, strlen(buf));
129 CtdlLogPrintf(CTDL_DEBUG, "Waiting for PORT number\n");
130 if (sock_getln(&sock, buf, sizeof buf) < 0) {
134 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
135 if (strncasecmp(buf, "PORT", 4)!=0) {
139 /* Should have received a port number to connect to */
140 extract_token(portbuf, buf, 1, ' ', sizeof portbuf);
142 /* Attempt to establish connection to STREAM socket */
143 streamsock = sock_connect(hostbuf, portbuf);
145 if (streamsock < 0) {
146 /* If the service isn't running, just pass the mail
147 * through. Potentially throwing away mails isn't good.
149 FreeStrBuf(&CCC->sReadBuf);
150 FreeStrBuf(&CCC->sMigrateBuf);
154 CtdlLogPrintf(CTDL_DEBUG, "STREAM socket connected!\n");
160 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
161 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
162 msgtext = CC->redirect_buffer;
163 CC->redirect_buffer = NULL;
165 sock_write(&streamsock, SKEY(msgtext));
166 FreeStrBuf(&msgtext);
168 /* Close the streamsocket connection; this tells clamd
171 if (streamsock != -1)
175 CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
176 if (sock_getln(&sock, buf, sizeof buf) < 0) {
179 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
180 if (strncasecmp(buf, "stream: OK", 10)!=0) {
185 if (msg->cm_fields['0'] != NULL) {
186 free(msg->cm_fields['0']);
188 msg->cm_fields['0'] = strdup("message rejected by virus filter");
192 FreeStrBuf(&CCC->sReadBuf);
193 FreeStrBuf(&CCC->sMigrateBuf);
199 CTDL_MODULE_INIT(virus)
203 CtdlRegisterMessageHook(clamd, EVT_SMTPSCAN);
206 /* return our Subversion id for the Log */