"Fossies" - the Fresh Open Source Software Archive

Member "citadel/modules/clamav/serv_virus.c" (5 Jun 2021, 4815 Bytes) of package /linux/www/citadel.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "serv_virus.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 8.24_vs_9.01.

    1 /*
    2  * This module allows Citadel to use clamd to filter incoming messages
    3  * arriving via SMTP.  For more information on clamd, visit
    4  * http://clamav.net (the ClamAV project is not in any way
    5  * affiliated with the Citadel project).
    6  *
    7  * Copyright (c) 1987-2012 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 CLAMD_PORT       "3310"
   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 #include "ctdl_module.h"
   48 
   49 
   50 /*
   51  * Connect to the clamd server and scan a message.
   52  */
   53 int clamd(struct CtdlMessage *msg, struct recptypes *recp) {
   54     int sock = (-1);
   55     int streamsock = (-1);
   56     char clamhosts[SIZ];
   57     int num_clamhosts;
   58     char buf[SIZ];
   59         char hostbuf[SIZ];
   60         char portbuf[SIZ];
   61     int is_virus = 0;
   62     int clamhost;
   63     StrBuf *msgtext;
   64     CitContext *CCC;
   65 
   66     /* See if we have any clamd hosts configured */
   67     num_clamhosts = get_hosts(clamhosts, "clamav");
   68     if (num_clamhosts < 1) {
   69         return(0);
   70     }
   71 
   72     /* Try them one by one until we get a working one */
   73         for (clamhost=0; clamhost<num_clamhosts; ++clamhost) {
   74                 extract_token(buf, clamhosts, clamhost, '|', sizeof buf);
   75                 syslog(LOG_INFO, "Connecting to clamd at <%s>\n", buf);
   76 
   77                 /* Assuming a host:port entry */ 
   78                 extract_token(hostbuf, buf, 0, ':', sizeof hostbuf);
   79                 if (extract_token(portbuf, buf, 1, ':', sizeof portbuf)==-1)
   80                   /* Didn't specify a port so we'll try the psuedo-standard 3310 */
   81                   sock = sock_connect(hostbuf, CLAMD_PORT);
   82                 else
   83                   /* Port specified lets try connecting to it! */
   84                   sock = sock_connect(hostbuf, portbuf);
   85 
   86                 if (sock >= 0) syslog(LOG_DEBUG, "Connected!\n");
   87         }
   88 
   89     if (sock < 0) {
   90         /* If the service isn't running, just pass the mail
   91          * through.  Potentially throwing away mails isn't good.
   92          */
   93         return(0);
   94     }
   95     CCC=CC;
   96     CCC->SBuf.Buf = NewStrBuf();
   97     CCC->sMigrateBuf = NewStrBuf();
   98     CCC->SBuf.ReadWritePointer = NULL;
   99 
  100     /* Command */
  101     syslog(LOG_DEBUG, "Transmitting STREAM command\n");
  102     sprintf(buf, "STREAM\r\n");
  103     sock_write(&sock, buf, strlen(buf));
  104 
  105     syslog(LOG_DEBUG, "Waiting for PORT number\n");
  106         if (sock_getln(&sock, buf, sizeof buf) < 0) {
  107                 goto bail;
  108         }
  109 
  110         syslog(LOG_DEBUG, "<%s\n", buf);
  111     if (strncasecmp(buf, "PORT", 4)!=0) {
  112             goto bail;
  113     }
  114 
  115         /* Should have received a port number to connect to */
  116     extract_token(portbuf, buf, 1, ' ', sizeof portbuf);
  117 
  118     /* Attempt to establish connection to STREAM socket */
  119         streamsock = sock_connect(hostbuf, portbuf);
  120 
  121     if (streamsock < 0) {
  122         /* If the service isn't running, just pass the mail
  123          * through.  Potentially throwing away mails isn't good.
  124          */
  125         FreeStrBuf(&CCC->SBuf.Buf);
  126         FreeStrBuf(&CCC->sMigrateBuf);
  127         return(0);
  128         }
  129     else {
  130             syslog(LOG_DEBUG, "STREAM socket connected!\n");
  131     }
  132 
  133 
  134     /* Message */
  135     CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
  136     CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ALL, 0, 1, 0);
  137     msgtext = CC->redirect_buffer;
  138     CC->redirect_buffer = NULL;
  139 
  140     sock_write(&streamsock, SKEY(msgtext));
  141     FreeStrBuf(&msgtext);
  142 
  143     /* Close the streamsocket connection; this tells clamd
  144      * that we're done.
  145      */
  146     if (streamsock != -1) {
  147         close(streamsock);
  148     }
  149     
  150     /* Response */
  151     syslog(LOG_DEBUG, "Awaiting response\n");
  152         if (sock_getln(&sock, buf, sizeof buf) < 0) {
  153                 goto bail;
  154         }
  155         syslog(LOG_DEBUG, "<%s\n", buf);
  156     if (strncasecmp(buf, "stream: OK", 10)!=0) {
  157         is_virus = 1;
  158     }
  159 
  160     if (is_virus) {
  161         CM_SetField(msg, eErrorMsg, HKEY("message rejected by virus filter"));
  162     }
  163 
  164 bail:   close(sock);
  165     FreeStrBuf(&CCC->SBuf.Buf);
  166     FreeStrBuf(&CCC->sMigrateBuf);
  167     return(is_virus);
  168 }
  169 
  170 
  171 CTDL_MODULE_INIT(virus)
  172 {
  173     if (!threading)
  174     {
  175         CtdlRegisterMessageHook(clamd, EVT_SMTPSCAN);
  176     }
  177     
  178     /* return our module name for the log */
  179         return "virus";
  180 }