ze-filter  (ze-filter-0.8.0-develop-180218)
ze-log-quarantine.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * ze-filter - Mail Server Filter for sendmail
5  *
6  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
7  *
8  * Auteur : Jose Marcio Martins da Cruz
9  * jose.marcio.mc@gmail.org
10  *
11  * Historique :
12  * Creation : Tue Jan 17 14:47:29 CET 2006
13  *
14  * This program is free software, but with restricted license :
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * More details about ze-filter license can be found at ze-filter
22  * web site : http://foss.jose-marcio.org
23  */
24 
25 
26 #include <ze-sys.h>
27 #include <ze-filter.h>
28 #include <ze-filter-data.h>
29 #include <ze-log-quarantine.h>
30 #include <ze-spool.h>
31 
32 /* ****************************************************************************
33  * *
34  * *
35  ******************************************************************************/
36 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
37 
38 static LOG_T tlog = LOG_INITIALIZER;
39 
40 /* ****************************************************************************
41  * *
42  * *
43  ******************************************************************************/
44 bool
46 {
47  bool res = TRUE;
48  char path[1024];
49  char *wkdir = cf_get_str(CF_WORKDIR);
50  char *logname = cf_get_str(CF_QUARANTINE_LOG_FILE);
51 
52  ADJUST_LOG_NAME(path, logname, wkdir, "none:");
53 
54  MUTEX_LOCK(&mutex);
55 
56  if (!log_ready(&tlog))
57  res = log_open(&tlog, path);
58  else
59  res = log_reopen(&tlog);
60 
62 
63  return res;
64 }
65 
66 /* ****************************************************************************
67  * *
68  * *
69  ******************************************************************************/
70 void
71 log_quarantine(ctx, files)
72  SMFICTX *ctx;
73  attachment_T *files;
74 {
75  CTXPRIV_T *priv = MLFIPRIV(ctx);
76 
77  attachment_T *p;
78  int r;
79  char *logname = cf_get_str(CF_QUARANTINE_LOG_FILE);
80 
81  char ipbuf[SZ_IP];
82 
83  char *id;
84  char *ip;
85  int why;
86 
87  if (priv == NULL)
88  return;
89 
90  id = CONNID_STR(priv->id);
91  if (id == NULL)
92  id = "00000000.000";
93 
94  ip = priv->peer_addr;
95 
96  if ((logname == NULL) || (strlen(logname) == 0))
97  return;
98 
99  if (strcasecmp(logname, "NONE") == 0)
100  return;
101 
102  ZE_MessageInfo(19, "%s Logging quarantine message...", id);
103 
104  snprintf(ipbuf, sizeof (ipbuf), "IP=(%s)", STRNULL(ip, "0.0.0.0"));
105 
106  if (!log_ready(&tlog) && !log_quarantine_reopen())
107  return;
108 
109  MUTEX_LOCK(&mutex);
110 
111  why = priv->save_why;
112 
113 #if 0
114  if (!log_ready(&tlog)) {
115  if (!log_open(&tlog, logname))
116  goto fin;
117  }
118 #endif
119 
120  if (log_ready(&tlog)) {
121  rcpt_addr_T *rcpt;
122  char msgid[256];
123  char *suffix = NULL;
124 
125  snprintf(msgid, sizeof (msgid), "%s.%04X", CONNID_STR(priv->id),
126  priv->nb_msgs - 1);
127 
128  log_printf(&tlog, "%-18s CONN %10d %-16s %s\n",
129  msgid, CONNID_INT(priv->id), priv->peer_addr,
130  STRNULL(priv->peer_name, "NONAME"));
131 
132  suffix = STRNULL(priv->fsuffix, SUFFIX_UNKNOWN);
133 
134  log_printf(&tlog, "%-18s WHY %s\n", msgid, suffix);
135 
136  {
137  char *fname = NULL;
138 
139  fname = basename(priv->fname);
140  if (fname == NULL)
141  fname = priv->fname;
142 
143  log_printf(&tlog, "%-18s QUAR %s%s\n", msgid, fname, suffix);
144  }
145 
146  if (priv->hdr_subject != NULL) {
147  char *p;
148 
149  while ((p = strpbrk(priv->hdr_subject, "\r\n")) != NULL) {
150  *p = ' ';
151  }
152  }
153  log_printf(&tlog, "%-18s SUBJ %s\n", msgid,
154  STRNULL(priv->hdr_subject, "---"));
155 
156  log_printf(&tlog, "%-18s FROM %s\n", msgid,
157  STRNULL(priv->hdr_from, "NULL"));
158 
159  log_printf(&tlog, "%-18s ENV_FROM %s\n", msgid,
160  STRNULL(priv->env_from, "NULL"));
161 
162  for (rcpt = priv->env_rcpt; rcpt != NULL; rcpt = rcpt->next)
163  log_printf(&tlog, "%-18s RCPT %3d %s\n",
164  msgid, rcpt->access, STRNULL(rcpt->rcpt, "???"));
165 
166  log_printf(&tlog, "%-18s SIZE %8lu\n", msgid, priv->msg_size);
167 
168 #if 0
169  if (strcasecmp(suffix, SUFFIX_SPAM) == 0)
170 #endif
171  {
172  if (priv->score_str != NULL && !STRCASEEQUAL(priv->score_str, ""))
173  log_printf(&tlog, "%-18s SCORE %s\n", msgid, priv->score_str);
174  if (priv->status_str != NULL && !STRCASEEQUAL(priv->status_str, ""))
175  log_printf(&tlog, "%-18s STATUS %s\n", msgid, priv->status_str);
176  }
177 
178  if (strcasecmp(suffix, SUFFIX_VIRUS) == 0) {
179  log_printf(&tlog, "%-18s VIRUS %s\n", msgid,
180  STRNULL(priv->msg.virus, "UNKNOWN"));
181  }
182 
183  if (TRUE || strcasecmp(suffix, SUFFIX_QUARANTINE) != 0) {
184  p = files;
185  while (p != NULL) {
186  char *serror = "???";
187 
188  if ((p->name == NULL) || (strlen(p->name) == 0))
189  continue;
190 
191  serror = STRBOOL(p->xfile, "XXX", "---");
192 
193  log_printf(&tlog, "%-18s FILE %s %-30s %s\n",
194  msgid, serror, STRNULL(p->mimetype, "mime-unknown"),
195  p->name);
196  p = p->next;
197  }
198  }
199  log_printf(&tlog, "\n");
200  }
201 
202 fin:
204 }
rcpt_addr_T * env_rcpt
char * virus
#define STRBOOL(x, t, f)
Definition: macros.h:87
#define LOG_INITIALIZER
Definition: ze-txtlog.h:96
char * rcpt
Definition: ze-rcpt-list.h:31
#define SUFFIX_SPAM
Definition: ze-spool.h:40
char * fname
bool log_ready(LOG_T *)
Definition: ze-txtlog.c:130
#define CONNID_INT(connid)
Definition: ze-filter.h:114
#define SUFFIX_QUARANTINE
Definition: ze-spool.h:42
bool log_reopen(LOG_T *)
Definition: ze-txtlog.c:275
#define MUTEX_UNLOCK(mutex)
Definition: macros.h:101
bool log_open(LOG_T *, char *)
Definition: ze-txtlog.c:146
char * peer_addr
#define STRNULL(x, r)
Definition: macros.h:81
#define MUTEX_LOCK(mutex)
Definition: macros.h:93
pthread_mutex_t mutex
Definition: ze-connopen.c:63
bool log_quarantine_reopen()
#define SZ_IP
Definition: ze-libjc.h:77
#define MLFIPRIV(ctx)
#define SUFFIX_VIRUS
Definition: ze-spool.h:38
CONNID_T id
unsigned long msg_size
rcpt_addr_T * next
Definition: ze-rcpt-list.h:39
bool log_printf(LOG_T *, char *,...)
Definition: ze-txtlog.c:355
char * fsuffix
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
#define TRUE
Definition: macros.h:157
#define CF_QUARANTINE_LOG_FILE
Definition: cfh-defs.h:74
char * cf_get_str(int id)
Definition: ze-cf.c:854
char * mimetype
Definition: ze-mimelist.h:72
char * peer_name
char * env_from
void log_quarantine(SMFICTX *ctx, attachment_T *files)
char * score_str
char * status_str
#define CONNID_STR(connid)
Definition: ze-filter.h:113
char * hdr_subject
#define STRCASEEQUAL(a, b)
Definition: macros.h:72
char * hdr_from
char * name
Definition: ze-mimelist.h:70
#define SUFFIX_UNKNOWN
Definition: ze-spool.h:36
struct attachment_T * next
Definition: ze-mimelist.h:74
#define CF_WORKDIR
Definition: cfh-defs.h:67
#define ADJUST_LOG_NAME(path, fname, dir, defval)
Definition: ze-txtlog.h:133