df7f97e77d9a92f96f2c5e35d76218b343dc767d
[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-2015 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 SPAMASSASSIN_PORT       "783"
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
59
60 #include "ctdl_module.h"
61
62
63
64 /*
65  * Connect to the SpamAssassin server and scan a message.
66  */
67 int spam_assassin(struct CtdlMessage *msg, recptypes *recp) {
68         int sock = (-1);
69         char sahosts[SIZ];
70         int num_sahosts;
71         char buf[SIZ];
72         int is_spam = 0;
73         int sa;
74         StrBuf *msgtext;
75         CitContext *CCC=CC;
76
77         /* For users who have authenticated to this server we never want to
78          * apply spam filtering, because presumably they're trustworthy.
79          */
80         if (CC->logged_in) return(0);
81
82         /* See if we have any SpamAssassin hosts configured */
83         num_sahosts = get_hosts(sahosts, "spamassassin");
84         if (num_sahosts < 1) return(0);
85
86         /* Try them one by one until we get a working one */
87         for (sa=0; sa<num_sahosts; ++sa) {
88                 extract_token(buf, sahosts, sa, '|', sizeof buf);
89                 syslog(LOG_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
90                 sock = sock_connect(buf, SPAMASSASSIN_PORT);
91                 if (sock >= 0) syslog(LOG_DEBUG, "Connected!\n");
92         }
93
94         if (sock < 0) {
95                 /* If the service isn't running, just pass the mail
96                  * through.  Potentially throwing away mails isn't good.
97                  */
98                 return(0);
99         }
100
101         CCC->SBuf.Buf = NewStrBuf();
102         CCC->sMigrateBuf = NewStrBuf();
103         CCC->SBuf.ReadWritePointer = NULL;
104
105         /* Command */
106         syslog(LOG_DEBUG, "Transmitting command\n");
107         sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
108         sock_write(&sock, buf, strlen(buf));
109
110         /* Message */
111         CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
112         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
113         msgtext = CC->redirect_buffer;
114         CC->redirect_buffer = NULL;
115
116         sock_write(&sock, SKEY(msgtext));
117         FreeStrBuf(&msgtext);
118
119         /* Close one end of the socket connection; this tells SpamAssassin
120          * that we're done.
121          */
122         if (sock != -1)
123                 sock_shutdown(sock, SHUT_WR);
124         
125         /* Response */
126         syslog(LOG_DEBUG, "Awaiting response\n");
127         if (sock_getln(&sock, buf, sizeof buf) < 0) {
128                 goto bail;
129         }
130         syslog(LOG_DEBUG, "<%s\n", buf);
131         if (strncasecmp(buf, "SPAMD", 5)) {
132                 goto bail;
133         }
134         if (sock_getln(&sock, buf, sizeof buf) < 0) {
135                 goto bail;
136         }
137         syslog(LOG_DEBUG, "<%s\n", buf);
138         syslog(LOG_DEBUG, "c_spam_flag_only setting %d\n", CtdlGetConfigInt("c_spam_flag_only"));
139         if (CtdlGetConfigInt("c_spam_flag_only")) {
140                 int headerlen;
141                 char *cur;
142                 char sastatus[10];
143                 char sascore[10];
144                 char saoutof[10];
145                 int numscore;
146
147                 syslog(LOG_DEBUG, "flag spam code used");
148
149                 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
150                 extract_token(sascore, buf, 3, ' ', sizeof sascore);
151                 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
152
153                 memcpy(buf, HKEY("X-Spam-Level: "));
154                 cur = buf + 14;
155                 for (numscore = atoi(sascore); numscore>0; numscore--)
156                         *(cur++) = '*';
157                 *cur = '\0';
158
159                 headerlen  = cur - buf;
160                 headerlen += snprintf(cur, (sizeof(buf) - headerlen), 
161                                      "\r\nX-Spam-Status: %s, score=%s required=%s\r\n",
162                                      sastatus, sascore, saoutof);
163
164                 CM_PrependToField(msg, eMesageText, buf, headerlen);
165
166         } else {
167                 syslog(LOG_DEBUG, "reject spam code used");
168                 if (!strncasecmp(buf, "Spam: True", 10)) {
169                         is_spam = 1;
170                 }
171
172                 if (is_spam) {
173                         CM_SetField(msg, eErrorMsg, HKEY("message rejected by spam filter"));
174                 }
175         }
176
177 bail:   close(sock);
178         FreeStrBuf(&CCC->SBuf.Buf);
179         FreeStrBuf(&CCC->sMigrateBuf);
180         return(is_spam);
181 }
182
183
184
185 CTDL_MODULE_INIT(spam)
186 {
187         if (!threading)
188         {
189                 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
190         }
191         
192         /* return our module name for the log */
193         return "spam";
194 }