b8ccfb8d69517b2acd76564acddb602c0e1145fa
[citadel.git] / citadel / modules / spam / serv_spam.c
1 /*
2  * $Id$
3  *
4  * This module allows Citadel to use SpamAssassin to filter incoming messages
5  * arriving via SMTP.  For more information on SpamAssassin, visit
6  * http://www.spamassassin.org (the SpamAssassin project is not in any way
7  * affiliated with the Citadel project).
8  */
9
10 #define SPAMASSASSIN_PORT       "783"
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <sys/wait.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <sys/socket.h>
37 #include <libcitadel.h>
38 #include "citadel.h"
39 #include "server.h"
40 #include "citserver.h"
41 #include "support.h"
42 #include "config.h"
43 #include "control.h"
44 #include "room_ops.h"
45 #include "user_ops.h"
46 #include "policy.h"
47 #include "database.h"
48 #include "msgbase.h"
49 #include "internet_addressing.h"
50 #include "domain.h"
51 #include "clientsocket.h"
52
53
54 #include "ctdl_module.h"
55
56
57
58 /*
59  * Connect to the SpamAssassin server and scan a message.
60  */
61 int spam_assassin(struct CtdlMessage *msg) {
62         int sock = (-1);
63         char sahosts[SIZ];
64         int num_sahosts;
65         char buf[SIZ];
66         int is_spam = 0;
67         int sa;
68         char *msgtext;
69         size_t msglen;
70
71         /* For users who have authenticated to this server we never want to
72          * apply spam filtering, because presumably they're trustworthy.
73          */
74         if (CC->logged_in) return(0);
75
76         /* See if we have any SpamAssassin hosts configured */
77         num_sahosts = get_hosts(sahosts, "spamassassin");
78         if (num_sahosts < 1) return(0);
79
80         /* Try them one by one until we get a working one */
81         for (sa=0; sa<num_sahosts; ++sa) {
82                 extract_token(buf, sahosts, sa, '|', sizeof buf);
83                 CtdlLogPrintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
84                 sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
85                 if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
86         }
87
88         if (sock < 0) {
89                 /* If the service isn't running, just pass the mail
90                  * through.  Potentially throwing away mails isn't good.
91                  */
92                 return(0);
93         }
94
95         /* Command */
96         CtdlLogPrintf(CTDL_DEBUG, "Transmitting command\n");
97         sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
98         sock_write(sock, buf, strlen(buf));
99
100         /* Message */
101         CC->redirect_buffer = malloc(SIZ);
102         CC->redirect_len = 0;
103         CC->redirect_alloc = SIZ;
104         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
105         msgtext = CC->redirect_buffer;
106         msglen = CC->redirect_len;
107         CC->redirect_buffer = NULL;
108         CC->redirect_len = 0;
109         CC->redirect_alloc = 0;
110
111         sock_write(sock, msgtext, msglen);
112         free(msgtext);
113
114         /* Close one end of the socket connection; this tells SpamAssassin
115          * that we're done.
116          */
117         sock_shutdown(sock, SHUT_WR);
118         
119         /* Response */
120         CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
121         if (sock_getln(sock, buf, sizeof buf) < 0) {
122                 goto bail;
123         }
124         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
125         if (strncasecmp(buf, "SPAMD", 5)) {
126                 goto bail;
127         }
128         if (sock_getln(sock, buf, sizeof buf) < 0) {
129                 goto bail;
130         }
131         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
132         CtdlLogPrintf(CTDL_DEBUG, "c_spam_flag_only setting %d\n", config.c_spam_flag_only);
133         if (config.c_spam_flag_only) {
134                 CtdlLogPrintf(CTDL_DEBUG, "flag spam code used");
135                 int headerlen;
136                 int newmsgsize;
137                 int oldmsgsize;
138
139                 char sastatus[10];
140                 char sascore[10];
141                 char saoutof[10];
142                 int numscore;
143
144                 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
145                 extract_token(sascore, buf, 3, ' ', sizeof sascore);
146                 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
147
148                 sprintf(buf,"X-Spam-Level: ");
149                 char *cur = buf + 14;
150                 for (numscore = atoi(sascore); numscore>0; numscore--)
151                         *(cur++) = '*';
152                 *cur = '\0';
153
154                 sprintf(cur,"\r\nX-Spam-Status: %s, score=%s required=%s\r\n", sastatus, sascore, saoutof);
155                 headerlen = strlen(buf);
156                 oldmsgsize = strlen(msg->cm_fields['M']) + 1;
157                 newmsgsize = headerlen + oldmsgsize;
158
159                 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], newmsgsize);
160
161                 memmove(msg->cm_fields['M']+headerlen,msg->cm_fields['M'],oldmsgsize);
162                 memcpy(msg->cm_fields['M'],buf,headerlen);
163
164         } else {
165                 CtdlLogPrintf(CTDL_DEBUG, "reject spam code used");
166                 if (!strncasecmp(buf, "Spam: True", 10)) {
167                         is_spam = 1;
168                 }
169
170                 if (is_spam) {
171                         if (msg->cm_fields['0'] != NULL) {
172                                 free(msg->cm_fields['0']);
173                         }
174                         msg->cm_fields['0'] = strdup("message rejected by spam filter");
175                 }
176         }
177
178 bail:   close(sock);
179         return(is_spam);
180 }
181
182
183
184 CTDL_MODULE_INIT(spam)
185 {
186         if (!threading)
187         {
188                 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
189         }
190         
191         /* return our Subversion id for the Log */
192         return "$Id$";
193 }