e8549ee4a8575698724b23bf76134ea9f998b652
[citadel.git] / citadel / modules / spam / 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 <libcitadel.h>
38 #include "citadel.h"
39 #include "server.h"
40 #include "citserver.h"
41 #include "support.h"
42 #include "config.h"
43 #include "control.h"
44 #include "room_ops.h"
45 #include "user_ops.h"
46 #include "policy.h"
47 #include "database.h"
48 #include "msgbase.h"
49 #include "internet_addressing.h"
50 #include "domain.h"
51 #include "clientsocket.h"
52
53
54 #include "ctdl_module.h"
55
56
57
58 /*
59  * Connect to the SpamAssassin server and scan a message.
60  */
61 int spam_assassin(struct CtdlMessage *msg) {
62         int sock = (-1);
63         char sahosts[SIZ];
64         int num_sahosts;
65         char buf[SIZ];
66         int is_spam = 0;
67         int sa;
68         char *msgtext;
69         size_t msglen;
70
71         /* For users who have authenticated to this server we never want to
72          * apply spam filtering, because presumably they're trustworthy.
73          */
74         if (CC->logged_in) return(0);
75
76         /* See if we have any SpamAssassin hosts configured */
77         num_sahosts = get_hosts(sahosts, "spamassassin");
78         if (num_sahosts < 1) return(0);
79
80         /* Try them one by one until we get a working one */
81         for (sa=0; sa<num_sahosts; ++sa) {
82                 extract_token(buf, sahosts, sa, '|', sizeof buf);
83                 CtdlLogPrintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
84                 sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
85                 if (sock >= 0) CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
86         }
87
88         if (sock < 0) {
89                 /* If the service isn't running, just pass the mail
90                  * through.  Potentially throwing away mails isn't good.
91                  */
92                 return(0);
93         }
94
95         /* Command */
96         CtdlLogPrintf(CTDL_DEBUG, "Transmitting command\n");
97         sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
98         sock_write(sock, buf, strlen(buf));
99
100         /* Message */
101         CC->redirect_buffer = malloc(SIZ);
102         CC->redirect_len = 0;
103         CC->redirect_alloc = SIZ;
104         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1);
105         msgtext = CC->redirect_buffer;
106         msglen = CC->redirect_len;
107         CC->redirect_buffer = NULL;
108         CC->redirect_len = 0;
109         CC->redirect_alloc = 0;
110
111         sock_write(sock, msgtext, msglen);
112         free(msgtext);
113
114         /* Close one end of the socket connection; this tells SpamAssassin
115          * that we're done.
116          */
117         sock_shutdown(sock, SHUT_WR);
118         
119         /* Response */
120         CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
121         if (sock_getln(sock, buf, sizeof buf) < 0) {
122                 goto bail;
123         }
124         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
125         if (strncasecmp(buf, "SPAMD", 5)) {
126                 goto bail;
127         }
128         if (sock_getln(sock, buf, sizeof buf) < 0) {
129                 goto bail;
130         }
131         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
132         if (!strncasecmp(buf, "Spam: True", 10)) {
133                 is_spam = 1;
134         }
135
136         if (is_spam) {
137                 if (msg->cm_fields['0'] != NULL) {
138                         free(msg->cm_fields['0']);
139                 }
140                 msg->cm_fields['0'] = strdup("message rejected by spam filter");
141         }
142
143 bail:   close(sock);
144         return(is_spam);
145 }
146
147
148
149 CTDL_MODULE_INIT(spam)
150 {
151         if (!threading)
152         {
153                 CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
154         }
155         
156         /* return our Subversion id for the Log */
157         return "$Id$";
158 }