* extract_token() now expects to be supplied with the size of the
[citadel.git] / citadel / 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
10 #define SPAMASSASSIN_PORT       "783"
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <sys/wait.h>
34 #include <string.h>
35 #include <limits.h>
36 #include <sys/socket.h>
37 #include "citadel.h"
38 #include "server.h"
39 #include "sysdep_decls.h"
40 #include "citserver.h"
41 #include "support.h"
42 #include "config.h"
43 #include "control.h"
44 #include "serv_extensions.h"
45 #include "room_ops.h"
46 #include "user_ops.h"
47 #include "policy.h"
48 #include "database.h"
49 #include "msgbase.h"
50 #include "tools.h"
51 #include "internet_addressing.h"
52 #include "domain.h"
53 #include "clientsocket.h"
54
55
56
57 /* 
58  * This is a scanner I had started writing before deciding to just farm the
59  * job out to SpamAssassin.  It *does* work but it's not in use.  We've
60  * commented it out so it doesn't even compile.
61  */
62 #ifdef ___NOT_CURRENTLY_IN_USE___
63 /* Scan a message for spam */
64 int spam_filter(struct CtdlMessage *msg) {
65         int spam_strings_found = 0;
66         struct spamstrings_t *sptr;
67         char *ptr;
68
69         /* Bail out if there's no message text */
70         if (msg->cm_fields['M'] == NULL) return(0);
71
72
73         /* Scan! */
74         ptr = msg->cm_fields['M'];
75         while (ptr++[0] != 0) {
76                 for (sptr = spamstrings; sptr != NULL; sptr = sptr->next) {
77                         if (!strncasecmp(ptr, sptr->string,
78                            strlen(sptr->string))) {
79                                 ++spam_strings_found;
80                         }
81                 }
82         }
83
84         if (spam_strings_found) {
85                 if (msg->cm_fields['0'] != NULL) {
86                         free(msg->cm_fields['0']);
87                 }
88                 msg->cm_fields['0'] = strdup("Unsolicited spam rejected");
89                 return(spam_strings_found);
90         }
91
92         return(0);
93 }
94 #endif
95
96
97
98 /*
99  * Connect to the SpamAssassin server and scan a message.
100  */
101 int spam_assassin(struct CtdlMessage *msg) {
102         int sock = (-1);
103         char sahosts[SIZ];
104         int num_sahosts;
105         char buf[SIZ];
106         int is_spam = 0;
107         int sa;
108         char *msgtext;
109         size_t msglen;
110
111         /* For users who have authenticated to this server we never want to
112          * apply spam filtering, because presumably they're trustworthy.
113          */
114         if (CC->logged_in) return(0);
115
116         /* See if we have any SpamAssassin hosts configured */
117         num_sahosts = get_hosts(sahosts, "spamassassin");
118         if (num_sahosts < 1) return(0);
119
120         /* Try them one by one until we get a working one */
121         for (sa=0; sa<num_sahosts; ++sa) {
122                 extract_token(buf, sahosts, sa, '|', sizeof buf);
123                 lprintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
124                 sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
125                 if (sock >= 0) lprintf(CTDL_DEBUG, "Connected!\n");
126         }
127
128         if (sock < 0) {
129                 /* If the service isn't running, just pass the mail
130                  * through.  Potentially throwing away mails isn't good.
131                  */
132                 return(0);
133         }
134
135         /* Command */
136         lprintf(CTDL_DEBUG, "Transmitting command\n");
137         sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
138         sock_write(sock, buf, strlen(buf));
139
140         /* Message */
141         CC->redirect_buffer = malloc(SIZ);
142         CC->redirect_len = 0;
143         CC->redirect_alloc = SIZ;
144         CtdlOutputPreLoadedMsg(msg, 0L, MT_RFC822, HEADERS_ALL, 0, 1);
145         msgtext = CC->redirect_buffer;
146         msglen = CC->redirect_len;
147         CC->redirect_buffer = NULL;
148         CC->redirect_len = 0;
149         CC->redirect_alloc = 0;
150
151         sock_write(sock, msgtext, msglen);
152         free(msgtext);
153
154         /* Close one end of the socket connection; this tells SpamAssassin
155          * that we're done.
156          */
157         sock_shutdown(sock, SHUT_WR);
158         
159         /* Response */
160         lprintf(CTDL_DEBUG, "Awaiting response\n");
161         if (sock_gets(sock, buf) < 0) {
162                 goto bail;
163         }
164         lprintf(CTDL_DEBUG, "<%s\n", buf);
165         if (strncasecmp(buf, "SPAMD", 5)) {
166                 goto bail;
167         }
168         if (sock_gets(sock, buf) < 0) {
169                 goto bail;
170         }
171         lprintf(CTDL_DEBUG, "<%s\n", buf);
172         if (!strncasecmp(buf, "Spam: True", 10)) {
173                 is_spam = 1;
174         }
175
176         if (is_spam) {
177                 if (msg->cm_fields['0'] != NULL) {
178                         free(msg->cm_fields['0']);
179                 }
180                 msg->cm_fields['0'] = strdup(
181                         "5.7.1 Message rejected by SpamAssassin");
182         }
183
184 bail:   close(sock);
185         return(is_spam);
186 }
187
188
189
190 char *serv_spam_init(void)
191 {
192
193 /* (disabled built-in scanner, see above)
194         CtdlRegisterMessageHook(spam_filter, EVT_SMTPSCAN);
195  */
196
197         CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
198
199         return "$Id$";
200 }