Back to index

citadel  8.12
serv_spam.c
Go to the documentation of this file.
00001 /*
00002  * This module allows Citadel to use SpamAssassin to filter incoming messages
00003  * arriving via SMTP.  For more information on SpamAssassin, visit
00004  * http://www.spamassassin.org (the SpamAssassin project is not in any way
00005  * affiliated with the Citadel project).
00006  *
00007  * Copyright (c) 1998-2012 by the citadel.org team
00008  *
00009  * This program is open source software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License version 3.
00011  * 
00012  * 
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * 
00020  * 
00021  * 
00022  */
00023 
00024 #define SPAMASSASSIN_PORT       "783"
00025 
00026 #include "sysdep.h"
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include <stdio.h>
00030 #include <fcntl.h>
00031 #include <signal.h>
00032 #include <pwd.h>
00033 #include <errno.h>
00034 #include <sys/types.h>
00035 
00036 #if TIME_WITH_SYS_TIME
00037 # include <sys/time.h>
00038 # include <time.h>
00039 #else
00040 # if HAVE_SYS_TIME_H
00041 #  include <sys/time.h>
00042 # else
00043 #  include <time.h>
00044 # endif
00045 #endif
00046 
00047 #include <sys/wait.h>
00048 #include <string.h>
00049 #include <limits.h>
00050 #include <sys/socket.h>
00051 #include <libcitadel.h>
00052 #include "citadel.h"
00053 #include "server.h"
00054 #include "citserver.h"
00055 #include "support.h"
00056 #include "config.h"
00057 #include "control.h"
00058 #include "user_ops.h"
00059 #include "database.h"
00060 #include "msgbase.h"
00061 #include "internet_addressing.h"
00062 #include "domain.h"
00063 #include "clientsocket.h"
00064 
00065 
00066 #include "ctdl_module.h"
00067 
00068 
00069 
00070 /*
00071  * Connect to the SpamAssassin server and scan a message.
00072  */
00073 int spam_assassin(struct CtdlMessage *msg) {
00074        int sock = (-1);
00075        char sahosts[SIZ];
00076        int num_sahosts;
00077        char buf[SIZ];
00078        int is_spam = 0;
00079        int sa;
00080        StrBuf *msgtext;
00081        CitContext *CCC=CC;
00082 
00083        /* For users who have authenticated to this server we never want to
00084         * apply spam filtering, because presumably they're trustworthy.
00085         */
00086        if (CC->logged_in) return(0);
00087 
00088        /* See if we have any SpamAssassin hosts configured */
00089        num_sahosts = get_hosts(sahosts, "spamassassin");
00090        if (num_sahosts < 1) return(0);
00091 
00092        /* Try them one by one until we get a working one */
00093         for (sa=0; sa<num_sahosts; ++sa) {
00094                 extract_token(buf, sahosts, sa, '|', sizeof buf);
00095                 syslog(LOG_INFO, "Connecting to SpamAssassin at <%s>\n", buf);
00096                 sock = sock_connect(buf, SPAMASSASSIN_PORT);
00097                 if (sock >= 0) syslog(LOG_DEBUG, "Connected!\n");
00098         }
00099 
00100        if (sock < 0) {
00101               /* If the service isn't running, just pass the mail
00102                * through.  Potentially throwing away mails isn't good.
00103                */
00104               return(0);
00105        }
00106 
00107        CCC->SBuf.Buf = NewStrBuf();
00108        CCC->sMigrateBuf = NewStrBuf();
00109        CCC->SBuf.ReadWritePointer = NULL;
00110 
00111        /* Command */
00112        syslog(LOG_DEBUG, "Transmitting command\n");
00113        sprintf(buf, "CHECK SPAMC/1.2\r\n\r\n");
00114        sock_write(&sock, buf, strlen(buf));
00115 
00116        /* Message */
00117        CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
00118        CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
00119        msgtext = CC->redirect_buffer;
00120        CC->redirect_buffer = NULL;
00121 
00122        sock_write(&sock, SKEY(msgtext));
00123        FreeStrBuf(&msgtext);
00124 
00125        /* Close one end of the socket connection; this tells SpamAssassin
00126         * that we're done.
00127         */
00128        if (sock != -1)
00129               sock_shutdown(sock, SHUT_WR);
00130        
00131        /* Response */
00132        syslog(LOG_DEBUG, "Awaiting response\n");
00133         if (sock_getln(&sock, buf, sizeof buf) < 0) {
00134                 goto bail;
00135         }
00136         syslog(LOG_DEBUG, "<%s\n", buf);
00137        if (strncasecmp(buf, "SPAMD", 5)) {
00138               goto bail;
00139        }
00140         if (sock_getln(&sock, buf, sizeof buf) < 0) {
00141                 goto bail;
00142         }
00143         syslog(LOG_DEBUG, "<%s\n", buf);
00144         syslog(LOG_DEBUG, "c_spam_flag_only setting %d\n", config.c_spam_flag_only);
00145         if (config.c_spam_flag_only) {
00146                 syslog(LOG_DEBUG, "flag spam code used");
00147               int headerlen;
00148               int newmsgsize;
00149               int oldmsgsize;
00150 
00151               char sastatus[10];
00152               char sascore[10];
00153               char saoutof[10];
00154               int numscore;
00155 
00156                 extract_token(sastatus, buf, 1, ' ', sizeof sastatus);
00157                 extract_token(sascore, buf, 3, ' ', sizeof sascore);
00158                 extract_token(saoutof, buf, 5, ' ', sizeof saoutof);
00159 
00160               sprintf(buf,"X-Spam-Level: ");
00161               char *cur = buf + 14;
00162               for (numscore = atoi(sascore); numscore>0; numscore--)
00163                      *(cur++) = '*';
00164               *cur = '\0';
00165 
00166               sprintf(cur,"\r\nX-Spam-Status: %s, score=%s required=%s\r\n", sastatus, sascore, saoutof);
00167               headerlen = strlen(buf);
00168               oldmsgsize = strlen(msg->cm_fields['M']) + 1;
00169               newmsgsize = headerlen + oldmsgsize;
00170 
00171               msg->cm_fields['M'] = realloc(msg->cm_fields['M'], newmsgsize);
00172 
00173               memmove(msg->cm_fields['M']+headerlen,msg->cm_fields['M'],oldmsgsize);
00174               memcpy(msg->cm_fields['M'],buf,headerlen);
00175 
00176        } else {
00177                 syslog(LOG_DEBUG, "reject spam code used");
00178               if (!strncasecmp(buf, "Spam: True", 10)) {
00179                      is_spam = 1;
00180               }
00181 
00182               if (is_spam) {
00183                      if (msg->cm_fields['0'] != NULL) {
00184                             free(msg->cm_fields['0']);
00185                      }
00186                      msg->cm_fields['0'] = strdup("message rejected by spam filter");
00187               }
00188        }
00189 
00190 bail:  close(sock);
00191        FreeStrBuf(&CCC->SBuf.Buf);
00192        FreeStrBuf(&CCC->sMigrateBuf);
00193        return(is_spam);
00194 }
00195 
00196 
00197 
00198 CTDL_MODULE_INIT(spam)
00199 {
00200        if (!threading)
00201        {
00202               CtdlRegisterMessageHook(spam_assassin, EVT_SMTPSCAN);
00203        }
00204        
00205        /* return our module name for the log */
00206         return "spam";
00207 }