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).
10 #define CLAMD_PORT "3310"
20 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
36 #include <sys/socket.h>
37 #include <libcitadel.h>
40 #include "citserver.h"
49 #include "internet_addressing.h"
51 #include "clientsocket.h"
54 #include "ctdl_module.h"
59 * Connect to the clamd server and scan a message.
61 int clamd(struct CtdlMessage
*msg
) {
63 int streamsock
= (-1);
74 /* Don't care if you're logged in. You can still spread viruses.
76 /* if (CC->logged_in) return(0); */
78 /* See if we have any clamd hosts configured */
79 num_clamhosts
= get_hosts(clamhosts
, "clamav");
80 if (num_clamhosts
< 1) return(0);
82 /* Try them one by one until we get a working one */
83 for (clamhost
=0; clamhost
<num_clamhosts
; ++clamhost
) {
84 extract_token(buf
, clamhosts
, clamhost
, '|', sizeof buf
);
85 CtdlLogPrintf(CTDL_INFO
, "Connecting to clamd at <%s>\n", buf
);
87 /* Assuming a host:port entry */
88 extract_token(hostbuf
, buf
, 0, ':', sizeof hostbuf
);
89 if (extract_token(portbuf
, buf
, 1, ':', sizeof portbuf
)==-1)
90 /* Didn't specify a port so we'll try the psuedo-standard 3310 */
91 sock
= sock_connect(hostbuf
, CLAMD_PORT
, "tcp");
93 /* Port specified lets try connecting to it! */
94 sock
= sock_connect(hostbuf
, portbuf
, "tcp");
96 if (sock
>= 0) CtdlLogPrintf(CTDL_DEBUG
, "Connected!\n");
100 /* If the service isn't running, just pass the mail
101 * through. Potentially throwing away mails isn't good.
107 CtdlLogPrintf(CTDL_DEBUG
, "Transmitting STREAM command\n");
108 sprintf(buf
, "STREAM\r\n");
109 sock_write(sock
, buf
, strlen(buf
));
111 CtdlLogPrintf(CTDL_DEBUG
, "Waiting for PORT number\n");
112 if (sock_getln(sock
, buf
, sizeof buf
) < 0) {
116 CtdlLogPrintf(CTDL_DEBUG
, "<%s\n", buf
);
117 if (strncasecmp(buf
, "PORT", 4)!=0) {
121 /* Should have received a port number to connect to */
122 extract_token(portbuf
, buf
, 1, ' ', sizeof portbuf
);
124 /* Attempt to establish connection to STREAM socket */
125 streamsock
= sock_connect(hostbuf
, portbuf
, "tcp");
127 if (streamsock
< 0) {
128 /* If the service isn't running, just pass the mail
129 * through. Potentially throwing away mails isn't good.
134 CtdlLogPrintf(CTDL_DEBUG
, "STREAM socket connected!\n");
140 CC
->redirect_buffer
= malloc(SIZ
);
141 CC
->redirect_len
= 0;
142 CC
->redirect_alloc
= SIZ
;
143 CtdlOutputPreLoadedMsg(msg
, MT_RFC822
, HEADERS_ALL
, 0, 1, 0);
144 msgtext
= CC
->redirect_buffer
;
145 msglen
= CC
->redirect_len
;
146 CC
->redirect_buffer
= NULL
;
147 CC
->redirect_len
= 0;
148 CC
->redirect_alloc
= 0;
150 sock_write(streamsock
, msgtext
, msglen
);
153 /* Close the streamsocket connection; this tells clamd
159 CtdlLogPrintf(CTDL_DEBUG
, "Awaiting response\n");
160 if (sock_getln(sock
, buf
, sizeof buf
) < 0) {
163 CtdlLogPrintf(CTDL_DEBUG
, "<%s\n", buf
);
164 if (strncasecmp(buf
, "stream: OK", 10)!=0) {
169 if (msg
->cm_fields
['0'] != NULL
) {
170 free(msg
->cm_fields
['0']);
172 msg
->cm_fields
['0'] = strdup("message rejected by virus filter");
181 CTDL_MODULE_INIT(virus
)
185 CtdlRegisterMessageHook(clamd
, EVT_SMTPSCAN
);
188 /* return our Subversion id for the Log */