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