citadel
About: Citadel is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
  Fossies Dox: citadel.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
serv_virus.c
Go to the documentation of this file.
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-2022 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 */
53int 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);
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
164bail: close(sock);
165 FreeStrBuf(&CCC->SBuf.Buf);
166 FreeStrBuf(&CCC->sMigrateBuf);
167 return(is_virus);
168}
169
170
171// Initialization function, called from modules_init.c
173 if (!threading) {
175 }
176
177 /* return our module name for the log */
178 return "virus";
179}
int sock_getln(int *sock, char *buf, int bufsize)
Definition: clientsocket.c:124
int sock_write(int *sock, const char *buf, int nbytes)
Definition: clientsocket.c:147
int sock_connect(char *host, char *service)
Definition: clientsocket.c:22
#define CC
Definition: context.h:140
void CtdlRegisterMessageHook(int(*handler)(struct CtdlMessage *, struct recptypes *), int EventType)
int threading
Definition: modules_init.c:24
int get_hosts(char *mxbuf, char *rectype)
Definition: domain.c:33
int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage, int mode, int headers_only, int do_proto, int crlf, int flags)
Definition: msgbase.c:1958
void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
Definition: msgbase.c:137
#define HEADERS_ALL
Definition: msgbase.h:39
char * ctdl_module_init_virus(void)
Definition: serv_virus.c:172
#define CLAMD_PORT
Definition: serv_virus.c:18
int clamd(struct CtdlMessage *msg, struct recptypes *recp)
Definition: serv_virus.c:53
@ eErrorMsg
Definition: server.h:324
@ MT_RFC822
Definition: server.h:167
#define EVT_SMTPSCAN
Definition: server.h:233
IOBuffer SBuf
Definition: context.h:54
StrBuf * sMigrateBuf
Definition: context.h:57
#define SIZ
Definition: sysconfig.h:33