citadel
About: Citadel is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
  Fossies Dox: citadel.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
serv_spam.c
Go to the documentation of this file.
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-2022 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#include <time.h>
30#include <sys/wait.h>
31#include <string.h>
32#include <limits.h>
33#include <sys/socket.h>
34#include <libcitadel.h>
35#include "../../citadel.h"
36#include "../../server.h"
37#include "../../citserver.h"
38#include "../../support.h"
39#include "../../config.h"
40#include "../../control.h"
41#include "../../user_ops.h"
42#include "../../database.h"
43#include "../../msgbase.h"
44#include "../../internet_addressing.h"
45#include "../../domain.h"
46#include "../../clientsocket.h"
47
48
49#include "../../ctdl_module.h"
50
51
52
53/*
54 * Connect to the SpamAssassin server and scan a message.
55 */
56int spam_assassin(struct CtdlMessage *msg, struct recptypes *recp) {
57 int sock = (-1);
58 char sahosts[SIZ];
59 int num_sahosts;
60 char buf[SIZ];
61 int is_spam = 0;
62 int sa;
63 StrBuf *msgtext;
64 CitContext *CCC=CC;
65
66 /* For users who have authenticated to this server we never want to
67 * apply spam filtering, because presumably they're trustworthy.
68 */
69 if (CC->logged_in) return(0);
70
71 /* See if we have any SpamAssassin hosts configured */
72 num_sahosts = get_hosts(sahosts, "spamassassin");
73 if (num_sahosts < 1) return(0);
74
75 /* Try them one by one until we get a working one */
76 for (sa=0; sa<num_sahosts; ++sa) {
77 extract_token(buf, sahosts, sa, '|', sizeof buf);
78 syslog(LOG_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
80 if (sock >= 0) syslog(LOG_DEBUG, "Connected!\n");
81 }
82
83 if (sock < 0) {
84 /* If the service isn't running, just pass the mail
85 * through. Potentially throwing away mails isn't good.
86 */
87 return(0);
88 }
89
90 CCC->SBuf.Buf = NewStrBuf();
91 CCC->sMigrateBuf = NewStrBuf();
92 CCC->SBuf.ReadWritePointer = NULL;
93
94 /* Command */
95 syslog(LOG_DEBUG, "Transmitting command\n");
96 sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
97 sock_write(&sock, buf, strlen(buf));
98
99 /* Message */
100 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
102 msgtext = CC->redirect_buffer;
103 CC->redirect_buffer = NULL;
104
105 sock_write(&sock, SKEY(msgtext));
106 FreeStrBuf(&msgtext);
107
108 /* Close one end of the socket connection; this tells SpamAssassin
109 * that we're done.
110 */
111 if (sock != -1)
112 sock_shutdown(sock, SHUT_WR);
113
114 /* Response */
115 syslog(LOG_DEBUG, "Awaiting response\n");
116 if (sock_getln(&sock, buf, sizeof buf) < 0) {
117 goto bail;
118 }
119 syslog(LOG_DEBUG, "<%s\n", buf);
120 if (strncasecmp(buf, "SPAMD", 5)) {
121 goto bail;
122 }
123 if (sock_getln(&sock, buf, sizeof buf) < 0) {
124 goto bail;
125 }
126 syslog(LOG_DEBUG, "<%s\n", buf);
127 syslog(LOG_DEBUG, "c_spam_flag_only setting %d\n", CtdlGetConfigInt("c_spam_flag_only"));
128 if (CtdlGetConfigInt("c_spam_flag_only")) {
129 int headerlen;
130 char *cur;
131 char sastatus[10];
132 char sascore[10];
133 char saoutof[10];
134 int numscore;
135
136 syslog(LOG_DEBUG, "flag spam code used");
137
138 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
139 extract_token(sascore, buf, 3, ' ', sizeof sascore);
140 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
141
142 memcpy(buf, HKEY("X-Spam-Level: "));
143 cur = buf + 14;
144 for (numscore = atoi(sascore); numscore>0; numscore--)
145 *(cur++) = '*';
146 *cur = '\0';
147
148 headerlen = cur - buf;
149 headerlen += snprintf(cur, (sizeof(buf) - headerlen),
150 "\r\nX-Spam-Status: %s, score=%s required=%s\r\n",
151 sastatus, sascore, saoutof);
152
153 CM_PrependToField(msg, eMesageText, buf, headerlen);
154
155 } else {
156 syslog(LOG_DEBUG, "reject spam code used");
157 if (!strncasecmp(buf, "Spam: True", 10)) {
158 is_spam = 1;
159 }
160
161 if (is_spam) {
162 CM_SetField(msg, eErrorMsg, HKEY("message rejected by spam filter"));
163 }
164 }
165
166bail: close(sock);
167 FreeStrBuf(&CCC->SBuf.Buf);
168 FreeStrBuf(&CCC->sMigrateBuf);
169 return(is_spam);
170}
171
172
173// Initialization function, called from modules_init.c
175 if (!threading) {
177 }
178
179 /* return our module name for the log */
180 return "spam";
181}
int sock_getln(int *sock, char *buf, int bufsize)
Definition: clientsocket.c:124
int sock_write(int *sock, const char *buf, int nbytes)
Definition: clientsocket.c:147
int sock_connect(char *host, char *service)
Definition: clientsocket.c:22
#define sock_shutdown(sock, how)
Definition: clientsocket.h:27
int CtdlGetConfigInt(char *key)
Definition: config.c:390
#define CC
Definition: context.h:140
void CtdlRegisterMessageHook(int(*handler)(struct CtdlMessage *, struct recptypes *), int EventType)
int threading
Definition: modules_init.c:24
int get_hosts(char *mxbuf, char *rectype)
Definition: domain.c:33
void CM_PrependToField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
Definition: msgbase.c:212
int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage, int mode, int headers_only, int do_proto, int crlf, int flags)
Definition: msgbase.c:1958
void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
Definition: msgbase.c:137
#define HEADERS_ALL
Definition: msgbase.h:39
char * ctdl_module_init_spam(void)
Definition: serv_spam.c:174
int spam_assassin(struct CtdlMessage *msg, struct recptypes *recp)
Definition: serv_spam.c:56
#define SPAMASSASSIN_PORT
Definition: serv_spam.c:18
@ eErrorMsg
Definition: server.h:324
@ eMesageText
Definition: server.h:315
@ MT_RFC822
Definition: server.h:167
#define EVT_SMTPSCAN
Definition: server.h:233
IOBuffer SBuf
Definition: context.h:54
StrBuf * sMigrateBuf
Definition: context.h:57
StrBuf * redirect_buffer
Definition: context.h:62
#define SIZ
Definition: sysconfig.h:33