81f53e13a41f68b8ee506ee1de8df8ab72c4dbdd
[citadel.git] / citadel / modules / clamav / serv_virus.c
1 /*
2  * This module allows Citadel to use clamd to filter incoming messages
3  * arriving via SMTP.  For more information on clamd, visit
4  * http://clamav.net (the ClamAV project is not in any way
5  * affiliated with the Citadel project).
6  *
7  * Copyright (c) 1987-2012 by the citadel.org team
8  *
9  *  This program is open source software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 3.
11  *  
12  *  
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  
20  *  
21  *  
22  */
23
24 #define CLAMD_PORT       "3310"
25
26 #include "sysdep.h"
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <errno.h>
34 #include <sys/types.h>
35
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
38 # include <time.h>
39 #else
40 # if HAVE_SYS_TIME_H
41 #  include <sys/time.h>
42 # else
43 #  include <time.h>
44 # endif
45 #endif
46
47 #include <sys/wait.h>
48 #include <string.h>
49 #include <limits.h>
50 #include <sys/socket.h>
51 #include <libcitadel.h>
52 #include "citadel.h"
53 #include "server.h"
54 #include "citserver.h"
55 #include "support.h"
56 #include "config.h"
57 #include "control.h"
58 #include "user_ops.h"
59 #include "database.h"
60 #include "msgbase.h"
61 #include "internet_addressing.h"
62 #include "domain.h"
63 #include "clientsocket.h"
64
65
66 #include "ctdl_module.h"
67
68
69
70 /*
71  * Connect to the clamd server and scan a message.
72  */
73 int clamd(struct CtdlMessage *msg, recptypes *recp) {
74         int sock = (-1);
75         int streamsock = (-1);
76         char clamhosts[SIZ];
77         int num_clamhosts;
78         char buf[SIZ];
79         char hostbuf[SIZ];
80         char portbuf[SIZ];
81         int is_virus = 0;
82         int clamhost;
83         StrBuf *msgtext;
84         CitContext *CCC;
85
86         /* Don't care if you're logged in.  You can still spread viruses.
87          */
88         /* if (CC->logged_in) return(0); */
89
90         /* See if we have any clamd hosts configured */
91         num_clamhosts = get_hosts(clamhosts, "clamav");
92         if (num_clamhosts < 1) return(0);
93
94         /* Try them one by one until we get a working one */
95         for (clamhost=0; clamhost<num_clamhosts; ++clamhost) {
96                 extract_token(buf, clamhosts, clamhost, '|', sizeof buf);
97                 syslog(LOG_INFO, "Connecting to clamd at <%s>\n", buf);
98
99                 /* Assuming a host:port entry */ 
100                 extract_token(hostbuf, buf, 0, ':', sizeof hostbuf);
101                 if (extract_token(portbuf, buf, 1, ':', sizeof portbuf)==-1)
102                   /* Didn't specify a port so we'll try the psuedo-standard 3310 */
103                   sock = sock_connect(hostbuf, CLAMD_PORT);
104                 else
105                   /* Port specified lets try connecting to it! */
106                   sock = sock_connect(hostbuf, portbuf);
107
108                 if (sock >= 0) syslog(LOG_DEBUG, "Connected!\n");
109         }
110
111         if (sock < 0) {
112                 /* If the service isn't running, just pass the mail
113                  * through.  Potentially throwing away mails isn't good.
114                  */
115                 return(0);
116         }
117         CCC=CC;
118         CCC->SBuf.Buf = NewStrBuf();
119         CCC->sMigrateBuf = NewStrBuf();
120         CCC->SBuf.ReadWritePointer = NULL;
121
122         /* Command */
123         syslog(LOG_DEBUG, "Transmitting STREAM command\n");
124         sprintf(buf, "STREAM\r\n");
125         sock_write(&sock, buf, strlen(buf));
126
127         syslog(LOG_DEBUG, "Waiting for PORT number\n");
128         if (sock_getln(&sock, buf, sizeof buf) < 0) {
129                 goto bail;
130         }
131
132         syslog(LOG_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);
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                 FreeStrBuf(&CCC->SBuf.Buf);
148                 FreeStrBuf(&CCC->sMigrateBuf);
149                 return(0);
150         }
151         else {
152                 syslog(LOG_DEBUG, "STREAM socket connected!\n");
153         }
154
155
156
157         /* Message */
158         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
159         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
160         msgtext = CC->redirect_buffer;
161         CC->redirect_buffer = NULL;
162
163         sock_write(&streamsock, SKEY(msgtext));
164         FreeStrBuf(&msgtext);
165
166         /* Close the streamsocket connection; this tells clamd
167          * that we're done.
168          */
169         if (streamsock != -1)
170                 close(streamsock);
171         
172         /* Response */
173         syslog(LOG_DEBUG, "Awaiting response\n");
174         if (sock_getln(&sock, buf, sizeof buf) < 0) {
175                 goto bail;
176         }
177         syslog(LOG_DEBUG, "<%s\n", buf);
178         if (strncasecmp(buf, "stream: OK", 10)!=0) {
179                 is_virus = 1;
180         }
181
182         if (is_virus) {
183                 CM_SetField(msg, eErrorMsg, HKEY("message rejected by virus filter"));
184         }
185
186 bail:   close(sock);
187         FreeStrBuf(&CCC->SBuf.Buf);
188         FreeStrBuf(&CCC->sMigrateBuf);
189         return(is_virus);
190 }
191
192
193
194 CTDL_MODULE_INIT(virus)
195 {
196         if (!threading)
197         {
198                 CtdlRegisterMessageHook(clamd, EVT_SMTPSCAN);
199         }
200         
201         /* return our module name for the log */
202         return "virus";
203 }