ze-filter  (ze-filter-0.8.0-develop-180218)
ze-log-regex.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 16:24:39 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-log-regex.h>
29 
30 /* ***************************************************************************
31  * *
32  * *
33  *****************************************************************************/
35 
36 static int nberr = 0;
37 
38 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
39 static LOG_T logt = LOG_INITIALIZER;
40 
41 #ifdef MAX_ERR
42 #undef MAX_ERR
43 #endif
44 
45 #define MAX_ERR 16
46 
47 /* ***************************************************************************
48  * *
49  * *
50  *****************************************************************************/
51 bool
53 {
54  bool result = TRUE;
55  char path[1024];
56  char *wkdir = cf_get_str(CF_WORKDIR);
57  char *logname = cf_get_str(CF_REGEX_LOG_FILE);
58 
59  ADJUST_LOG_NAME(path, logname, wkdir, "none:");
60 
61  ZE_MessageInfo(12, "Reopening regex log file...");
62  MUTEX_LOCK(&mutex);
63 
64  if (!log_ready(&logt))
65  result = log_open(&logt, path);
66  else
67  result = log_reopen(&logt);
68 
70 
71  return result;
72 }
73 
74 /* ***************************************************************************
75  * *
76  * *
77  *****************************************************************************/
78 bool
79 log_found_regex(id, ip, tag, nb, score, expr)
80  char *id;
81  char *ip;
82  char *tag;
83  int nb;
84  int score;
85  char *expr;
86 {
87  char *logname = cf_get_str(CF_REGEX_LOG_FILE);
88  char sout[256];
89  time_t now = time(NULL);
90  bool res = TRUE;
91 
92  if (!mailregexlog2file)
93  return TRUE;
94 
96  return TRUE;
97 
98  if (nberr > MAX_ERR)
99  return FALSE;
100 
101  if (!log_ready(&logt) && !log_regex_reopen())
102  return FALSE;
103 
104  MUTEX_LOCK(&mutex);
105 
106  if (ip == NULL)
107  ip = "0.0.0.0";
108 
109  if (tag == NULL)
110  tag = "NOTAG";
111 
112  if (mailregexlog2file && log_ready(&logt)) {
113  static time_t lasterror = (time_t) 0;
114 
115 #if 0
116  if (!log_ready(&logt)) {
117  if (nberr < MAX_ERR || lasterror + 60 < now) {
118  if ((logname == NULL) || (strlen(logname) == 0)) {
119  ZE_LogMsgError(0, "CF_REGEX_LOG : bad filename");
120  nberr++;
121 
122  res = FALSE;
123  goto fin;
124  }
125  if (!log_open(&logt, logname))
126  goto fin;
127  } else
128  goto fin;
129  }
130 #endif
131 
132  if (!log_printf(&logt, "%10ld %12s IP=(%s) TAG=(%s) %3d %3d %s\n",
133  (long) now, id, ip, tag, nb, score, expr)) {
134  log_close(&logt);
135  nberr++;
136  } else
137  nberr = 0;
138  } else {
139  ZE_MessageInfo(9, "%12s %-12s - %3d %3d %s",
140  id, STRNULL(tag, "NOTAG"), nb, score, expr);
141  nberr = 0;
142  }
143 
144 fin:
146 
147  return res;
148 }
#define LOG_INITIALIZER
Definition: ze-txtlog.h:96
bool log_ready(LOG_T *)
Definition: ze-txtlog.c:130
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
#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
#define FALSE
Definition: macros.h:160
#define ZE_LogMsgError(level,...)
Definition: zeSyslog.h:113
#define CF_REGEX_LOG_FILE
Definition: cfh-defs.h:122
int cf_get_int(int id)
Definition: ze-cf.c:803
bool mailregexlog2file
Definition: ze-log-regex.c:34
bool log_printf(LOG_T *, char *,...)
Definition: ze-txtlog.c:355
#define CF_DUMP_FOUND_REGEX
Definition: cfh-defs.h:121
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
int nb
Definition: ze-connopen.c:61
#define OPT_NO
Definition: ze-cf.h:44
#define TRUE
Definition: macros.h:157
bool log_found_regex(char *id, char *ip, char *tag, int nb, int score, char *expr)
Definition: ze-log-regex.c:79
char * cf_get_str(int id)
Definition: ze-cf.c:854
bool log_regex_reopen()
Definition: ze-log-regex.c:52
bool log_close(LOG_T *)
Definition: ze-txtlog.c:296
#define MAX_ERR
Definition: ze-log-regex.c:45
#define CF_WORKDIR
Definition: cfh-defs.h:67
#define ADJUST_LOG_NAME(path, fname, dir, defval)
Definition: ze-txtlog.h:133