76a422d8524da1dbf82c2a16c02e4187203acf62
[citadel.git] / citadel / server / modules / spam / serv_spam.c
1 // This module allows Citadel to use an external SpamAssassin service to filter incoming messages arriving via SMTP.
2 //
3 // Copyright (c) 1998-2022 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
7
8 #define SPAMASSASSIN_PORT       "783"
9
10 #include "../../sysdep.h"
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <pwd.h>
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <time.h>
20 #include <sys/wait.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <sys/socket.h>
24 #include <libcitadel.h>
25 #include "../../citadel.h"
26 #include "../../server.h"
27 #include "../../citserver.h"
28 #include "../../support.h"
29 #include "../../config.h"
30 #include "../../control.h"
31 #include "../../user_ops.h"
32 #include "../../database.h"
33 #include "../../msgbase.h"
34 #include "../../internet_addressing.h"
35 #include "../../domain.h"
36 #include "../../clientsocket.h"
37 #include "../../ctdl_module.h"
38
39
40 // Connect to the SpamAssassin server and scan a message.
41 int spam_assassin(struct CtdlMessage *msg, struct recptypes *recp) {
42         int sock = (-1);
43         char sahosts[SIZ];
44         int num_sahosts;
45         char buf[SIZ];
46         int is_spam = 0;
47         int sa;
48         StrBuf *msgtext;
49
50         // For users who have authenticated to this server we never want to
51         // apply spam filtering, because presumably they're trustworthy.
52         if (CC->logged_in) return(0);
53
54         // See if we have any SpamAssassin hosts configured
55         num_sahosts = get_hosts(sahosts, "spamassassin");
56         if (num_sahosts < 1) return(0);
57
58         // Try them one by one until we get a working one
59         for (sa=0; sa<num_sahosts; ++sa) {
60                 extract_token(buf, sahosts, sa, '|', sizeof buf);
61                 syslog(LOG_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
62                 sock = sock_connect(buf, SPAMASSASSIN_PORT);
63                 if (sock >= 0) syslog(LOG_DEBUG, "Connected!\n");
64         }
65
66         // If the service isn't running, just pass the mail through.  Potentially throwing away mails isn't good.
67         if (sock < 0) {
68                 return(0);
69         }
70
71         CC->SBuf.Buf = NewStrBuf();
72         CC->sMigrateBuf = NewStrBuf();
73         CC->SBuf.ReadWritePointer = NULL;
74
75         // Command
76         syslog(LOG_DEBUG, "Transmitting command\n");
77         sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
78         sock_write(&sock, buf, strlen(buf));
79
80         // Message
81         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
82         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
83         msgtext = CC->redirect_buffer;
84         CC->redirect_buffer = NULL;
85
86         sock_write(&sock, SKEY(msgtext));
87         FreeStrBuf(&msgtext);
88
89         // Close one end of the socket connection; this tells SpamAssassin that we're done.
90         if (sock != -1)
91                 sock_shutdown(sock, SHUT_WR);
92         
93         // Response
94         syslog(LOG_DEBUG, "Awaiting response\n");
95         if (sock_getln(&sock, buf, sizeof buf) < 0) {
96                 goto bail;
97         }
98         syslog(LOG_DEBUG, "<%s\n", buf);
99         if (strncasecmp(buf, "SPAMD", 5)) {
100                 goto bail;
101         }
102         if (sock_getln(&sock, buf, sizeof buf) < 0) {
103                 goto bail;
104         }
105         syslog(LOG_DEBUG, "<%s\n", buf);
106         syslog(LOG_DEBUG, "c_spam_flag_only setting %d\n", CtdlGetConfigInt("c_spam_flag_only"));
107         if (CtdlGetConfigInt("c_spam_flag_only")) {
108                 int headerlen;
109                 char *cur;
110                 char sastatus[10];
111                 char sascore[10];
112                 char saoutof[10];
113                 int numscore;
114
115                 syslog(LOG_DEBUG, "flag spam code used");
116
117                 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
118                 extract_token(sascore, buf, 3, ' ', sizeof sascore);
119                 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
120
121                 memcpy(buf, HKEY("X-Spam-Level: "));
122                 cur = buf + 14;
123                 for (numscore = atoi(sascore); numscore>0; numscore--)
124                         *(cur++) = '*';
125                 *cur = '\0';
126
127                 headerlen  = cur - buf;
128                 headerlen += snprintf(cur, (sizeof(buf) - headerlen), 
129                                      "\r\nX-Spam-Status: %s, score=%s required=%s\r\n",
130                                      sastatus, sascore, saoutof);
131
132                 CM_PrependToField(msg, eMesageText, buf, headerlen);
133
134         }
135         else {
136                 syslog(LOG_DEBUG, "reject spam code used");
137                 if (!strncasecmp(buf, "Spam: True", 10)) {
138                         is_spam = 1;
139                 }
140
141                 if (is_spam) {
142                         CM_SetField(msg, eErrorMsg, HKEY("message rejected by spam filter"));
143                 }
144         }
145
146 bail:   close(sock);
147         FreeStrBuf(&CC->SBuf.Buf);
148         FreeStrBuf(&CC->sMigrateBuf);
149         return(is_spam);
150 }
151
152
153 // Initialization function, called from modules_init.c
154 char *ctdl_module_init_spam(void) {
155         if (!threading) {
156                 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
157         }
158         
159         // return our module name for the log
160         return "spam";
161 }