1553498921dc7331e5c598f6fd52086b88fa7b7a
[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         StrBuf *msgtext;
87         CitContext *CCC;
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         CCC=CC;
121         CCC->sReadBuf = NewStrBuf();
122         CCC->sMigrateBuf = NewStrBuf();
123         CCC->sPos = NULL;
124
125         /* Command */
126         CtdlLogPrintf(CTDL_DEBUG, "Transmitting STREAM command\n");
127         sprintf(buf, "STREAM\r\n");
128         sock_write(&sock, buf, strlen(buf));
129
130         CtdlLogPrintf(CTDL_DEBUG, "Waiting for PORT number\n");
131         if (sock_getln(&sock, buf, sizeof buf) < 0) {
132                 goto bail;
133         }
134
135         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
136         if (strncasecmp(buf, "PORT", 4)!=0) {
137                 goto bail;
138         }
139
140         /* Should have received a port number to connect to */
141         extract_token(portbuf, buf, 1, ' ', sizeof portbuf);
142
143         /* Attempt to establish connection to STREAM socket */
144         streamsock = sock_connect(hostbuf, portbuf, "tcp");
145
146         if (streamsock < 0) {
147                 /* If the service isn't running, just pass the mail
148                  * through.  Potentially throwing away mails isn't good.
149                  */
150                 FreeStrBuf(&CCC->sReadBuf);
151                 FreeStrBuf(&CCC->sMigrateBuf);
152                 return(0);
153         }
154         else {
155                 CtdlLogPrintf(CTDL_DEBUG, "STREAM socket connected!\n");
156         }
157
158
159
160         /* Message */
161         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
162         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
163         msgtext = CC->redirect_buffer;
164         CC->redirect_buffer = NULL;
165
166         sock_write(&streamsock, SKEY(msgtext));
167         FreeStrBuf(&msgtext);
168
169         /* Close the streamsocket connection; this tells clamd
170          * that we're done.
171          */
172         if (streamsock != -1)
173                 close(streamsock);
174         
175         /* Response */
176         CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
177         if (sock_getln(&sock, buf, sizeof buf) < 0) {
178                 goto bail;
179         }
180         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
181         if (strncasecmp(buf, "stream: OK", 10)!=0) {
182                 is_virus = 1;
183         }
184
185         if (is_virus) {
186                 if (msg->cm_fields['0'] != NULL) {
187                         free(msg->cm_fields['0']);
188                 }
189                 msg->cm_fields['0'] = strdup("message rejected by virus filter");
190         }
191
192 bail:   close(sock);
193         FreeStrBuf(&CCC->sReadBuf);
194         FreeStrBuf(&CCC->sMigrateBuf);
195         return(is_virus);
196 }
197
198
199
200 CTDL_MODULE_INIT(virus)
201 {
202         if (!threading)
203         {
204                 CtdlRegisterMessageHook(clamd, EVT_SMTPSCAN);
205         }
206         
207         /* return our Subversion id for the Log */
208         return "$Id$";
209 }