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"
64 #include "internet_addressing.h"
66 #include "clientsocket.h"
69 #include "ctdl_module.h"
74 * Connect to the clamd server and scan a message.
76 int clamd(struct CtdlMessage *msg) {
78 int streamsock = (-1);
90 /* Don't care if you're logged in. You can still spread viruses.
92 /* if (CC->logged_in) return(0); */
94 /* See if we have any clamd hosts configured */
95 num_clamhosts = get_hosts(clamhosts, "clamav");
96 if (num_clamhosts < 1) return(0);
98 /* Try them one by one until we get a working one */
99 for (clamhost=0; clamhost<num_clamhosts; ++clamhost) {
100 extract_token(buf, clamhosts, clamhost, '|', sizeof buf);
101 CtdlLogPrintf(CTDL_INFO, "Connecting to clamd at <%s>\n", buf);
103 /* Assuming a host:port entry */
104 extract_token(hostbuf, buf, 0, ':', sizeof hostbuf);
105 if (extract_token(portbuf, buf, 1, ':', sizeof portbuf)==-1)
106 /* Didn't specify a port so we'll try the psuedo-standard 3310 */
107 sock = sock_connect(hostbuf, CLAMD_PORT, "tcp");
109 /* Port specified lets try connecting to it! */
110 sock = sock_connect(hostbuf, portbuf, "tcp");
112 if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
116 /* If the service isn't running, just pass the mail
117 * through. Potentially throwing away mails isn't good.
122 CCC->sReadBuf = NewStrBuf();
123 CCC->sMigrateBuf = NewStrBuf();
127 CtdlLogPrintf(CTDL_DEBUG, "Transmitting STREAM command\n");
128 sprintf(buf, "STREAM\r\n");
129 sock_write(&sock, buf, strlen(buf));
131 CtdlLogPrintf(CTDL_DEBUG, "Waiting for PORT number\n");
132 if (sock_getln(&sock, buf, sizeof buf) < 0) {
136 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
137 if (strncasecmp(buf, "PORT", 4)!=0) {
141 /* Should have received a port number to connect to */
142 extract_token(portbuf, buf, 1, ' ', sizeof portbuf);
144 /* Attempt to establish connection to STREAM socket */
145 streamsock = sock_connect(hostbuf, portbuf, "tcp");
147 if (streamsock < 0) {
148 /* If the service isn't running, just pass the mail
149 * through. Potentially throwing away mails isn't good.
151 FreeStrBuf(&CCC->sReadBuf);
152 FreeStrBuf(&CCC->sMigrateBuf);
156 CtdlLogPrintf(CTDL_DEBUG, "STREAM socket connected!\n");
162 CC->redirect_buffer = malloc(SIZ);
163 CC->redirect_len = 0;
164 CC->redirect_alloc = SIZ;
165 CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
166 msgtext = CC->redirect_buffer;
167 msglen = CC->redirect_len;
168 CC->redirect_buffer = NULL;
169 CC->redirect_len = 0;
170 CC->redirect_alloc = 0;
172 sock_write(&streamsock, msgtext, msglen);
175 /* Close the streamsocket connection; this tells clamd
178 if (streamsock != -1)
182 CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
183 if (sock_getln(&sock, buf, sizeof buf) < 0) {
186 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
187 if (strncasecmp(buf, "stream: OK", 10)!=0) {
192 if (msg->cm_fields['0'] != NULL) {
193 free(msg->cm_fields['0']);
195 msg->cm_fields['0'] = strdup("message rejected by virus filter");
199 FreeStrBuf(&CCC->sReadBuf);
200 FreeStrBuf(&CCC->sMigrateBuf);
206 CTDL_MODULE_INIT(virus)
210 CtdlRegisterMessageHook(clamd, EVT_SMTPSCAN);
213 /* return our Subversion id for the Log */