7d2aba4c779951bb8d0bb9c34487592ae44b7506
[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         CitContext *CCC;
89
90         /* Don't care if you're logged in.  You can still spread viruses.
91          */
92         /* if (CC->logged_in) return(0); */
93
94         /* See if we have any clamd hosts configured */
95         num_clamhosts = get_hosts(clamhosts, "clamav");
96         if (num_clamhosts < 1) return(0);
97
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);
102
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");
108                 else
109                   /* Port specified lets try connecting to it! */
110                   sock = sock_connect(hostbuf, portbuf, "tcp");
111
112                 if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
113         }
114
115         if (sock < 0) {
116                 /* If the service isn't running, just pass the mail
117                  * through.  Potentially throwing away mails isn't good.
118                  */
119                 return(0);
120         }
121         CCC=CC;
122         CCC->sReadBuf = NewStrBuf();
123         CCC->sMigrateBuf = NewStrBuf();
124         CCC->sPos = NULL;
125
126         /* Command */
127         CtdlLogPrintf(CTDL_DEBUG, "Transmitting STREAM command\n");
128         sprintf(buf, "STREAM\r\n");
129         sock_write(&sock, buf, strlen(buf));
130
131         CtdlLogPrintf(CTDL_DEBUG, "Waiting for PORT number\n");
132         if (sock_getln(&sock, buf, sizeof buf) < 0) {
133                 goto bail;
134         }
135
136         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
137         if (strncasecmp(buf, "PORT", 4)!=0) {
138                 goto bail;
139         }
140
141         /* Should have received a port number to connect to */
142         extract_token(portbuf, buf, 1, ' ', sizeof portbuf);
143
144         /* Attempt to establish connection to STREAM socket */
145         streamsock = sock_connect(hostbuf, portbuf, "tcp");
146
147         if (streamsock < 0) {
148                 /* If the service isn't running, just pass the mail
149                  * through.  Potentially throwing away mails isn't good.
150                  */
151                 FreeStrBuf(&CCC->sReadBuf);
152                 FreeStrBuf(&CCC->sMigrateBuf);
153                 return(0);
154         }
155         else {
156                 CtdlLogPrintf(CTDL_DEBUG, "STREAM socket connected!\n");
157         }
158
159
160
161         /* Message */
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;
171
172         sock_write(&streamsock, msgtext, msglen);
173         free(msgtext);
174
175         /* Close the streamsocket connection; this tells clamd
176          * that we're done.
177          */
178         if (streamsock != -1)
179                 close(streamsock);
180         
181         /* Response */
182         CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
183         if (sock_getln(&sock, buf, sizeof buf) < 0) {
184                 goto bail;
185         }
186         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
187         if (strncasecmp(buf, "stream: OK", 10)!=0) {
188                 is_virus = 1;
189         }
190
191         if (is_virus) {
192                 if (msg->cm_fields['0'] != NULL) {
193                         free(msg->cm_fields['0']);
194                 }
195                 msg->cm_fields['0'] = strdup("message rejected by virus filter");
196         }
197
198 bail:   close(sock);
199         FreeStrBuf(&CCC->sReadBuf);
200         FreeStrBuf(&CCC->sMigrateBuf);
201         return(is_virus);
202 }
203
204
205
206 CTDL_MODULE_INIT(virus)
207 {
208         if (!threading)
209         {
210                 CtdlRegisterMessageHook(clamd, EVT_SMTPSCAN);
211         }
212         
213         /* return our Subversion id for the Log */
214         return "$Id$";
215 }