f9a823d9ed5466994f25500ba924fd253effa8e6
[citadel.git] / citadel / modules / spam / serv_spam.c
1 /*
2  * This module allows Citadel to use SpamAssassin to filter incoming messages
3  * arriving via SMTP.  For more information on SpamAssassin, visit
4  * http://www.spamassassin.org (the SpamAssassin project is not in any way
5  * affiliated with the Citadel project).
6  *
7  * Copyright (c) 1998-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 SPAMASSASSIN_PORT       "783"
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 SpamAssassin server and scan a message.
72  */
73 int spam_assassin(struct CtdlMessage *msg, recptypes *recp) {
74         int sock = (-1);
75         char sahosts[SIZ];
76         int num_sahosts;
77         char buf[SIZ];
78         int is_spam = 0;
79         int sa;
80         StrBuf *msgtext;
81         CitContext *CCC=CC;
82
83         /* For users who have authenticated to this server we never want to
84          * apply spam filtering, because presumably they're trustworthy.
85          */
86         if (CC->logged_in) return(0);
87
88         /* See if we have any SpamAssassin hosts configured */
89         num_sahosts = get_hosts(sahosts, "spamassassin");
90         if (num_sahosts < 1) return(0);
91
92         /* Try them one by one until we get a working one */
93         for (sa=0; sa<num_sahosts; ++sa) {
94                 extract_token(buf, sahosts, sa, '|', sizeof buf);
95                 syslog(LOG_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
96                 sock = sock_connect(buf, SPAMASSASSIN_PORT);
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
107         CCC->SBuf.Buf = NewStrBuf();
108         CCC->sMigrateBuf = NewStrBuf();
109         CCC->SBuf.ReadWritePointer = NULL;
110
111         /* Command */
112         syslog(LOG_DEBUG, "Transmitting command\n");
113         sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
114         sock_write(&sock, buf, strlen(buf));
115
116         /* Message */
117         CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
118         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
119         msgtext = CC->redirect_buffer;
120         CC->redirect_buffer = NULL;
121
122         sock_write(&sock, SKEY(msgtext));
123         FreeStrBuf(&msgtext);
124
125         /* Close one end of the socket connection; this tells SpamAssassin
126          * that we're done.
127          */
128         if (sock != -1)
129                 sock_shutdown(sock, SHUT_WR);
130         
131         /* Response */
132         syslog(LOG_DEBUG, "Awaiting response\n");
133         if (sock_getln(&sock, buf, sizeof buf) < 0) {
134                 goto bail;
135         }
136         syslog(LOG_DEBUG, "<%s\n", buf);
137         if (strncasecmp(buf, "SPAMD", 5)) {
138                 goto bail;
139         }
140         if (sock_getln(&sock, buf, sizeof buf) < 0) {
141                 goto bail;
142         }
143         syslog(LOG_DEBUG, "<%s\n", buf);
144         syslog(LOG_DEBUG, "c_spam_flag_only setting %d\n", config.c_spam_flag_only);
145         if (config.c_spam_flag_only) {
146                 int headerlen;
147                 char *cur;
148                 char sastatus[10];
149                 char sascore[10];
150                 char saoutof[10];
151                 int numscore;
152
153                 syslog(LOG_DEBUG, "flag spam code used");
154
155                 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
156                 extract_token(sascore, buf, 3, ' ', sizeof sascore);
157                 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
158
159                 memcpy(buf, HKEY("X-Spam-Level: "));
160                 cur = buf + 14;
161                 for (numscore = atoi(sascore); numscore>0; numscore--)
162                         *(cur++) = '*';
163                 *cur = '\0';
164
165                 headerlen  = cur - buf;
166                 headerlen += snprintf(cur, (sizeof(buf) - headerlen), 
167                                      "\r\nX-Spam-Status: %s, score=%s required=%s\r\n",
168                                      sastatus, sascore, saoutof);
169
170                 CM_PrependToField(msg, eMesageText, buf, headerlen);
171
172         } else {
173                 syslog(LOG_DEBUG, "reject spam code used");
174                 if (!strncasecmp(buf, "Spam: True", 10)) {
175                         is_spam = 1;
176                 }
177
178                 if (is_spam) {
179                         CM_SetField(msg, eErrorMsg, HKEY("message rejected by spam filter"));
180                 }
181         }
182
183 bail:   close(sock);
184         FreeStrBuf(&CCC->SBuf.Buf);
185         FreeStrBuf(&CCC->sMigrateBuf);
186         return(is_spam);
187 }
188
189
190
191 CTDL_MODULE_INIT(spam)
192 {
193         if (!threading)
194         {
195                 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
196         }
197         
198         /* return our module name for the log */
199         return "spam";
200 }