b8f3a6c960d5d669e5cb78005406994402ee7721
[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  * Copyright (c) 1998-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 SPAMASSASSIN_PORT       "783"
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 "policy.h"
62 #include "database.h"
63 #include "msgbase.h"
64 #include "internet_addressing.h"
65 #include "domain.h"
66 #include "clientsocket.h"
67
68
69 #include "ctdl_module.h"
70
71
72
73 /*
74  * Connect to the SpamAssassin server and scan a message.
75  */
76 int spam_assassin(struct CtdlMessage *msg) {
77         int sock = (-1);
78         char sahosts[SIZ];
79         int num_sahosts;
80         char buf[SIZ];
81         int is_spam = 0;
82         int sa;
83         char *msgtext;
84         size_t msglen;
85         CitContext *CCC=CC;
86
87         /* For users who have authenticated to this server we never want to
88          * apply spam filtering, because presumably they're trustworthy.
89          */
90         if (CC->logged_in) return(0);
91
92         /* See if we have any SpamAssassin hosts configured */
93         num_sahosts = get_hosts(sahosts, "spamassassin");
94         if (num_sahosts < 1) return(0);
95
96         /* Try them one by one until we get a working one */
97         for (sa=0; sa<num_sahosts; ++sa) {
98                 extract_token(buf, sahosts, sa, '|', sizeof buf);
99                 CtdlLogPrintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
100                 sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
101                 if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
102         }
103
104         if (sock < 0) {
105                 /* If the service isn't running, just pass the mail
106                  * through.  Potentially throwing away mails isn't good.
107                  */
108                 return(0);
109         }
110
111         CCC->sReadBuf = NewStrBuf();
112         CCC->sMigrateBuf = NewStrBuf();
113         CCC->sPos = NULL;
114
115         /* Command */
116         CtdlLogPrintf(CTDL_DEBUG, "Transmitting command\n");
117         sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
118         sock_write(&sock, buf, strlen(buf));
119
120         /* Message */
121         CCC->redirect_buffer = malloc(SIZ);
122         CCC->redirect_len = 0;
123         CCC->redirect_alloc = SIZ;
124         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
125         msgtext = CC->redirect_buffer;
126         msglen = CC->redirect_len;
127         CCC->redirect_buffer = NULL;
128         CCC->redirect_len = 0;
129         CCC->redirect_alloc = 0;
130
131         sock_write(&sock, msgtext, msglen);
132         free(msgtext);
133
134         /* Close one end of the socket connection; this tells SpamAssassin
135          * that we're done.
136          */
137         if (sock != -1)
138                 sock_shutdown(sock, SHUT_WR);
139         
140         /* Response */
141         CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
142         if (sock_getln(&sock, buf, sizeof buf) < 0) {
143                 goto bail;
144         }
145         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
146         if (strncasecmp(buf, "SPAMD", 5)) {
147                 goto bail;
148         }
149         if (sock_getln(&sock, buf, sizeof buf) < 0) {
150                 goto bail;
151         }
152         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
153         CtdlLogPrintf(CTDL_DEBUG, "c_spam_flag_only setting %d\n", config.c_spam_flag_only);
154         if (config.c_spam_flag_only) {
155                 CtdlLogPrintf(CTDL_DEBUG, "flag spam code used");
156                 int headerlen;
157                 int newmsgsize;
158                 int oldmsgsize;
159
160                 char sastatus[10];
161                 char sascore[10];
162                 char saoutof[10];
163                 int numscore;
164
165                 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
166                 extract_token(sascore, buf, 3, ' ', sizeof sascore);
167                 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
168
169                 sprintf(buf,"X-Spam-Level: ");
170                 char *cur = buf + 14;
171                 for (numscore = atoi(sascore); numscore>0; numscore--)
172                         *(cur++) = '*';
173                 *cur = '\0';
174
175                 sprintf(cur,"\r\nX-Spam-Status: %s, score=%s required=%s\r\n", sastatus, sascore, saoutof);
176                 headerlen = strlen(buf);
177                 oldmsgsize = strlen(msg->cm_fields['M']) + 1;
178                 newmsgsize = headerlen + oldmsgsize;
179
180                 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], newmsgsize);
181
182                 memmove(msg->cm_fields['M']+headerlen,msg->cm_fields['M'],oldmsgsize);
183                 memcpy(msg->cm_fields['M'],buf,headerlen);
184
185         } else {
186                 CtdlLogPrintf(CTDL_DEBUG, "reject spam code used");
187                 if (!strncasecmp(buf, "Spam: True", 10)) {
188                         is_spam = 1;
189                 }
190
191                 if (is_spam) {
192                         if (msg->cm_fields['0'] != NULL) {
193                                 free(msg->cm_fields['0']);
194                         }
195                         msg->cm_fields['0'] = strdup("message rejected by spam filter");
196                 }
197         }
198
199 bail:   close(sock);
200         FreeStrBuf(&CCC->sReadBuf);
201         FreeStrBuf(&CCC->sMigrateBuf);
202         return(is_spam);
203 }
204
205
206
207 CTDL_MODULE_INIT(spam)
208 {
209         if (!threading)
210         {
211                 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
212         }
213         
214         /* return our Subversion id for the Log */
215         return "$Id$";
216 }