* custom sockets need to work buffered too...
[citadel.git] / citadel / modules / clamav / serv_virus.c
1 /*
2  * $Id$
3  *
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).
8  *
9  * Copyright (c) 1987-2009 by the citadel.org team
10  *
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.
15  *
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.
20  *
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
24  */
25
26 #define CLAMD_PORT       "3310"
27
28 #include "sysdep.h"
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <pwd.h>
35 #include <errno.h>
36 #include <sys/types.h>
37
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
40 # include <time.h>
41 #else
42 # if HAVE_SYS_TIME_H
43 #  include <sys/time.h>
44 # else
45 #  include <time.h>
46 # endif
47 #endif
48
49 #include <sys/wait.h>
50 #include <string.h>
51 #include <limits.h>
52 #include <sys/socket.h>
53 #include <libcitadel.h>
54 #include "citadel.h"
55 #include "server.h"
56 #include "citserver.h"
57 #include "support.h"
58 #include "config.h"
59 #include "control.h"
60 #include "user_ops.h"
61 #include "policy.h"
62 #include "database.h"
63 #include "msgbase.h"
64 #include "internet_addressing.h"
65 #include "domain.h"
66 #include "clientsocket.h"
67
68
69 #include "ctdl_module.h"
70
71
72
73 /*
74  * Connect to the clamd server and scan a message.
75  */
76 int clamd(struct CtdlMessage *msg) {
77         int sock = (-1);
78         int streamsock = (-1);
79         char clamhosts[SIZ];
80         int num_clamhosts;
81         char buf[SIZ];
82         char hostbuf[SIZ];
83         char portbuf[SIZ];
84         int is_virus = 0;
85         int clamhost;
86         char *msgtext;
87         size_t msglen;
88
89         /* Don't care if you're logged in.  You can still spread viruses.
90          */
91         /* if (CC->logged_in) return(0); */
92
93         /* See if we have any clamd hosts configured */
94         num_clamhosts = get_hosts(clamhosts, "clamav");
95         if (num_clamhosts < 1) return(0);
96
97         /* Try them one by one until we get a working one */
98         for (clamhost=0; clamhost<num_clamhosts; ++clamhost) {
99                 extract_token(buf, clamhosts, clamhost, '|', sizeof buf);
100                 CtdlLogPrintf(CTDL_INFO, "Connecting to clamd at <%s>\n", buf);
101
102                 /* Assuming a host:port entry */ 
103                 extract_token(hostbuf, buf, 0, ':', sizeof hostbuf);
104                 if (extract_token(portbuf, buf, 1, ':', sizeof portbuf)==-1)
105                   /* Didn't specify a port so we'll try the psuedo-standard 3310 */
106                   sock = sock_connect(hostbuf, CLAMD_PORT, "tcp");
107                 else
108                   /* Port specified lets try connecting to it! */
109                   sock = sock_connect(hostbuf, portbuf, "tcp");
110
111                 if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
112         }
113
114         if (sock < 0) {
115                 /* If the service isn't running, just pass the mail
116                  * through.  Potentially throwing away mails isn't good.
117                  */
118                 return(0);
119         }
120
121         /* Command */
122         CtdlLogPrintf(CTDL_DEBUG, "Transmitting STREAM command\n");
123         sprintf(buf, "STREAM\r\n");
124         sock_write(sock, buf, strlen(buf));
125
126         CtdlLogPrintf(CTDL_DEBUG, "Waiting for PORT number\n");
127         if (sock_getln(&sock, buf, sizeof buf) < 0) {
128                 goto bail;
129         }
130
131         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
132         if (strncasecmp(buf, "PORT", 4)!=0) {
133                 goto bail;
134         }
135
136         /* Should have received a port number to connect to */
137         extract_token(portbuf, buf, 1, ' ', sizeof portbuf);
138
139         /* Attempt to establish connection to STREAM socket */
140         streamsock = sock_connect(hostbuf, portbuf, "tcp");
141
142         if (streamsock < 0) {
143                 /* If the service isn't running, just pass the mail
144                  * through.  Potentially throwing away mails isn't good.
145                  */
146                 return(0);
147         }
148         else {
149                 CtdlLogPrintf(CTDL_DEBUG, "STREAM socket connected!\n");
150         }
151
152
153
154         /* Message */
155         CC->redirect_buffer = malloc(SIZ);
156         CC->redirect_len = 0;
157         CC->redirect_alloc = SIZ;
158         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
159         msgtext = CC->redirect_buffer;
160         msglen = CC->redirect_len;
161         CC->redirect_buffer = NULL;
162         CC->redirect_len = 0;
163         CC->redirect_alloc = 0;
164
165         sock_write(streamsock, msgtext, msglen);
166         free(msgtext);
167
168         /* Close the streamsocket connection; this tells clamd
169          * that we're done.
170          */
171         close(streamsock);
172         
173         /* Response */
174         CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
175         if (sock_getln(&sock, buf, sizeof buf) < 0) {
176                 goto bail;
177         }
178         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
179         if (strncasecmp(buf, "stream: OK", 10)!=0) {
180                 is_virus = 1;
181         }
182
183         if (is_virus) {
184                 if (msg->cm_fields['0'] != NULL) {
185                         free(msg->cm_fields['0']);
186                 }
187                 msg->cm_fields['0'] = strdup("message rejected by virus filter");
188         }
189
190 bail:   close(sock);
191         return(is_virus);
192 }
193
194
195
196 CTDL_MODULE_INIT(virus)
197 {
198         if (!threading)
199         {
200                 CtdlRegisterMessageHook(clamd, EVT_SMTPSCAN);
201         }
202         
203         /* return our Subversion id for the Log */
204         return "$Id$";
205 }