468a09737a2421886277b58352750aedb152458c
[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  * Connect to the SpamAssassin server and scan a message.
59  */
60 int spam_assassin(struct CtdlMessage *msg) {
61         int sock = (-1);
62         char sahosts[SIZ];
63         int num_sahosts;
64         char buf[SIZ];
65         int is_spam = 0;
66         int sa;
67         char *msgtext;
68         size_t msglen;
69
70         /* For users who have authenticated to this server we never want to
71          * apply spam filtering, because presumably they're trustworthy.
72          */
73         if (CC->logged_in) return(0);
74
75         /* See if we have any SpamAssassin hosts configured */
76         num_sahosts = get_hosts(sahosts, "spamassassin");
77         if (num_sahosts < 1) return(0);
78
79         /* Try them one by one until we get a working one */
80         for (sa=0; sa<num_sahosts; ++sa) {
81                 extract_token(buf, sahosts, sa, '|', sizeof buf);
82                 lprintf(CTDL_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
83                 sock = sock_connect(buf, SPAMASSASSIN_PORT, "tcp");
84                 if (sock >= 0) lprintf(CTDL_DEBUG, "Connected!\n");
85         }
86
87         if (sock < 0) {
88                 /* If the service isn't running, just pass the mail
89                  * through.  Potentially throwing away mails isn't good.
90                  */
91                 return(0);
92         }
93
94         /* Command */
95         lprintf(CTDL_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         CC->redirect_buffer = malloc(SIZ);
101         CC->redirect_len = 0;
102         CC->redirect_alloc = SIZ;
103         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1);
104         msgtext = CC->redirect_buffer;
105         msglen = CC->redirect_len;
106         CC->redirect_buffer = NULL;
107         CC->redirect_len = 0;
108         CC->redirect_alloc = 0;
109
110         sock_write(sock, msgtext, msglen);
111         free(msgtext);
112
113         /* Close one end of the socket connection; this tells SpamAssassin
114          * that we're done.
115          */
116         sock_shutdown(sock, SHUT_WR);
117         
118         /* Response */
119         lprintf(CTDL_DEBUG, "Awaiting response\n");
120         if (sock_gets(sock, buf) < 0) {
121                 goto bail;
122         }
123         lprintf(CTDL_DEBUG, "<%s\n", buf);
124         if (strncasecmp(buf, "SPAMD", 5)) {
125                 goto bail;
126         }
127         if (sock_gets(sock, buf) < 0) {
128                 goto bail;
129         }
130         lprintf(CTDL_DEBUG, "<%s\n", buf);
131         if (!strncasecmp(buf, "Spam: True", 10)) {
132                 is_spam = 1;
133         }
134
135         if (is_spam) {
136                 if (msg->cm_fields['0'] != NULL) {
137                         free(msg->cm_fields['0']);
138                 }
139                 msg->cm_fields['0'] = strdup("5.7.1 message rejected by spam filter");
140         }
141
142 bail:   close(sock);
143         return(is_spam);
144 }
145
146
147
148 char *serv_spam_init(void)
149 {
150         CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
151
152         /* return our Subversion id for the Log */
153         return "$Id$";
154 }