* move policy.c into modules/expire/expire_policy.c, since it just controls this.
[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 "database.h"
62 #include "msgbase.h"
63 #include "internet_addressing.h"
64 #include "domain.h"
65 #include "clientsocket.h"
66
67
68 #include "ctdl_module.h"
69
70
71
72 /*
73  * Connect to the clamd server and scan a message.
74  */
75 int clamd(struct CtdlMessage *msg) {
76         int sock = (-1);
77         int streamsock = (-1);
78         char clamhosts[SIZ];
79         int num_clamhosts;
80         char buf[SIZ];
81         char hostbuf[SIZ];
82         char portbuf[SIZ];
83         int is_virus = 0;
84         int clamhost;
85         StrBuf *msgtext;
86         CitContext *CCC;
87
88         /* Don't care if you're logged in.  You can still spread viruses.
89          */
90         /* if (CC->logged_in) return(0); */
91
92         /* See if we have any clamd hosts configured */
93         num_clamhosts = get_hosts(clamhosts, "clamav");
94         if (num_clamhosts < 1) return(0);
95
96         /* Try them one by one until we get a working one */
97         for (clamhost=0; clamhost<num_clamhosts; ++clamhost) {
98                 extract_token(buf, clamhosts, clamhost, '|', sizeof buf);
99                 CtdlLogPrintf(CTDL_INFO, "Connecting to clamd at <%s>\n", buf);
100
101                 /* Assuming a host:port entry */ 
102                 extract_token(hostbuf, buf, 0, ':', sizeof hostbuf);
103                 if (extract_token(portbuf, buf, 1, ':', sizeof portbuf)==-1)
104                   /* Didn't specify a port so we'll try the psuedo-standard 3310 */
105                   sock = sock_connect(hostbuf, CLAMD_PORT, "tcp");
106                 else
107                   /* Port specified lets try connecting to it! */
108                   sock = sock_connect(hostbuf, portbuf, "tcp");
109
110                 if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
111         }
112
113         if (sock < 0) {
114                 /* If the service isn't running, just pass the mail
115                  * through.  Potentially throwing away mails isn't good.
116                  */
117                 return(0);
118         }
119         CCC=CC;
120         CCC->sReadBuf = NewStrBuf();
121         CCC->sMigrateBuf = NewStrBuf();
122         CCC->sPos = NULL;
123
124         /* Command */
125         CtdlLogPrintf(CTDL_DEBUG, "Transmitting STREAM command\n");
126         sprintf(buf, "STREAM\r\n");
127         sock_write(&sock, buf, strlen(buf));
128
129         CtdlLogPrintf(CTDL_DEBUG, "Waiting for PORT number\n");
130         if (sock_getln(&sock, buf, sizeof buf) < 0) {
131                 goto bail;
132         }
133
134         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
135         if (strncasecmp(buf, "PORT", 4)!=0) {
136                 goto bail;
137         }
138
139         /* Should have received a port number to connect to */
140         extract_token(portbuf, buf, 1, ' ', sizeof portbuf);
141
142         /* Attempt to establish connection to STREAM socket */
143         streamsock = sock_connect(hostbuf, portbuf, "tcp");
144
145         if (streamsock < 0) {
146                 /* If the service isn't running, just pass the mail
147                  * through.  Potentially throwing away mails isn't good.
148                  */
149                 FreeStrBuf(&CCC->sReadBuf);
150                 FreeStrBuf(&CCC->sMigrateBuf);
151                 return(0);
152         }
153         else {
154                 CtdlLogPrintf(CTDL_DEBUG, "STREAM socket connected!\n");
155         }
156
157
158
159         /* Message */
160         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
161         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
162         msgtext = CC->redirect_buffer;
163         CC->redirect_buffer = NULL;
164
165         sock_write(&streamsock, SKEY(msgtext));
166         FreeStrBuf(&msgtext);
167
168         /* Close the streamsocket connection; this tells clamd
169          * that we're done.
170          */
171         if (streamsock != -1)
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         FreeStrBuf(&CCC->sReadBuf);
193         FreeStrBuf(&CCC->sMigrateBuf);
194         return(is_virus);
195 }
196
197
198
199 CTDL_MODULE_INIT(virus)
200 {
201         if (!threading)
202         {
203                 CtdlRegisterMessageHook(clamd, EVT_SMTPSCAN);
204         }
205         
206         /* return our Subversion id for the Log */
207         return "$Id$";
208 }