* More license declarations
[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 "room_ops.h"
61 #include "user_ops.h"
62 #include "policy.h"
63 #include "database.h"
64 #include "msgbase.h"
65 #include "internet_addressing.h"
66 #include "domain.h"
67 #include "clientsocket.h"
68
69
70 #include "ctdl_module.h"
71
72
73
74 /*
75  * Connect to the clamd server and scan a message.
76  */
77 int clamd(struct CtdlMessage *msg) {
78         int sock = (-1);
79         int streamsock = (-1);
80         char clamhosts[SIZ];
81         int num_clamhosts;
82         char buf[SIZ];
83         char hostbuf[SIZ];
84         char portbuf[SIZ];
85         int is_virus = 0;
86         int clamhost;
87         char *msgtext;
88         size_t msglen;
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
122         /* Command */
123         CtdlLogPrintf(CTDL_DEBUG, "Transmitting STREAM command\n");
124         sprintf(buf, "STREAM\r\n");
125         sock_write(sock, buf, strlen(buf));
126
127         CtdlLogPrintf(CTDL_DEBUG, "Waiting for PORT number\n");
128         if (sock_getln(sock, buf, sizeof buf) < 0) {
129                 goto bail;
130         }
131
132         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
133         if (strncasecmp(buf, "PORT", 4)!=0) {
134                 goto bail;
135         }
136
137         /* Should have received a port number to connect to */
138         extract_token(portbuf, buf, 1, ' ', sizeof portbuf);
139
140         /* Attempt to establish connection to STREAM socket */
141         streamsock = sock_connect(hostbuf, portbuf, "tcp");
142
143         if (streamsock < 0) {
144                 /* If the service isn't running, just pass the mail
145                  * through.  Potentially throwing away mails isn't good.
146                  */
147                 return(0);
148         }
149         else {
150                 CtdlLogPrintf(CTDL_DEBUG, "STREAM socket connected!\n");
151         }
152
153
154
155         /* Message */
156         CC->redirect_buffer = malloc(SIZ);
157         CC->redirect_len = 0;
158         CC->redirect_alloc = SIZ;
159         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
160         msgtext = CC->redirect_buffer;
161         msglen = CC->redirect_len;
162         CC->redirect_buffer = NULL;
163         CC->redirect_len = 0;
164         CC->redirect_alloc = 0;
165
166         sock_write(streamsock, msgtext, msglen);
167         free(msgtext);
168
169         /* Close the streamsocket connection; this tells clamd
170          * that we're done.
171          */
172         close(streamsock);
173         
174         /* Response */
175         CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
176         if (sock_getln(sock, buf, sizeof buf) < 0) {
177                 goto bail;
178         }
179         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
180         if (strncasecmp(buf, "stream: OK", 10)!=0) {
181                 is_virus = 1;
182         }
183
184         if (is_virus) {
185                 if (msg->cm_fields['0'] != NULL) {
186                         free(msg->cm_fields['0']);
187                 }
188                 msg->cm_fields['0'] = strdup("message rejected by virus filter");
189         }
190
191 bail:   close(sock);
192         return(is_virus);
193 }
194
195
196
197 CTDL_MODULE_INIT(virus)
198 {
199         if (!threading)
200         {
201                 CtdlRegisterMessageHook(clamd, EVT_SMTPSCAN);
202         }
203         
204         /* return our Subversion id for the Log */
205         return "$Id$";
206 }