ze-filter  (ze-filter-0.8.0-develop-180218)
ze-dbbl.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 : janvier 2002
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 #include <ze-sys.h>
26 
27 #include "ze-filter.h"
28 
29 #define JDEBUG 0
30 
31 /* ****************************************************************************
32  * *
33  * *
34  **************************************************************************** */
35 #define DREF 32
36 
37 typedef struct {
39  int no;
40  int npm;
41 } dbblq_T;
42 
43 typedef struct {
44  char ip[48];
45  dbblq_T data[DREF];
46 } dbbl_T;
47 
48 
49 static ZEDB_T hdb = ZEDB_INITIALIZER;
50 
51 /* ****************************************************************************
52  * *
53  * *
54  **************************************************************************** */
55 bool
57 {
58  bool res = TRUE;
59  char path[1024];
60 
61  snprintf(path, sizeof (path), "%s/%s", cf_get_str(CF_WDBDIR), "ze-dbbl.db");
62 
63  if (zeDb_OK(&hdb))
64  return TRUE;
65 
66  zeDb_Lock(&hdb);
67  if (!zeDb_OK(&hdb))
68  res = zeDb_Open(&hdb, work_db_env, path, 0644, FALSE, TRUE, 0);
69  zeDb_Unlock(&hdb);
70 
71  /*
72  * atexit(db_close_blacklist);
73  */
74 
75  return res;
76 }
77 
78 /* ****************************************************************************
79  * *
80  * *
81  **************************************************************************** */
82 bool
84 {
85  bool res = TRUE;
86 
87  if (!zeDb_OK(&hdb))
88  return TRUE;
89 
90  zeDb_Lock(&hdb);
91  if (zeDb_OK(&hdb))
92  res = zeDb_Close(&hdb);
93  zeDb_Unlock(&hdb);
94 
95  return res;
96 }
97 
98 /* ****************************************************************************
99  * *
100  * *
101  **************************************************************************** */
102 bool
104  char *ip;
105 {
106  char key[256];
107  dbbl_T value;
108  bool res = FALSE;
109  long tref = (time(NULL) / 3600);
110 
111  if ((ip == NULL) || (strlen(ip) == 0))
112  return FALSE;
113 
114  if (!zeDb_OK(&hdb))
116 
117  zeDb_Lock(&hdb);
118 
119  snprintf(key, sizeof (key), "DNS:Connect:%s", ip);
120  if (zeDb_GetRec(&hdb, key, &value, sizeof (value))) {
121  int i;
122 
123  for (i = 0; i < DREF; i++) {
124  if (value.data[i].tref + DREF >= tref) {
125  /*
126  ** Count data for this... local evaluation
127  */
128  }
129  }
130  /*
131  * global evaluation
132  */
133  }
134 
135  zeDb_Unlock(&hdb);
136 
137  return res;
138 }
139 
140 /* ****************************************************************************
141  * *
142  * *
143  **************************************************************************** */
144 bool
146  char *ip;
147  int what;
148 {
149  char key[256];
150  dbbl_T value;
151  bool res = FALSE;
152  long tref = (time(NULL) / 3600);
153  int iref = tref % DREF;
154 
155  if ((ip == NULL) || (strlen(ip) == 0))
156  return FALSE;
157 
158  if (!zeDb_OK(&hdb))
160 
161  zeDb_Lock(&hdb);
162 
163  if (zeDb_GetRec(&hdb, key, &value, sizeof (value))) {
164  if (value.data[iref].tref != tref)
165  memset(&value.data[iref], 0, sizeof (value.data[iref]));
166  } else
167  memset(&value, 0, sizeof (value));
168 
169  value.data[iref].tref = tref;
170 
171  /*
172  * JOE - update rec
173  */
174 
175  res = zeDb_AddRec(&hdb, key, &value, sizeof (value));
176 
177  zeDb_Unlock(&hdb);
178 
179  return FALSE;
180 }
181 
182 
183 /* ****************************************************************************
184  * *
185  * *
186  **************************************************************************** */
187 #define DIM_BL 16
188 
189 bool
191  char *why;
192  char *key;
193  db_map_T *r;
194 {
195  char k[256];
196  char v[1024];
197  bool found = FALSE;
198 
199  if (!zeDb_OK(&hdb))
201 
202  zeDb_Lock(&hdb);
203 
204  snprintf(k, sizeof (k), "%s:%s", why, key);
205  if (zeDb_GetRec(&hdb, k, &v, sizeof (v))) {
206  char *s, *ptr;
207  char *fields[DIM_BL];
208 
209  found = TRUE;
210  memset(fields, 0, sizeof (fields));
211  if ((r != NULL) && (strlen(v) > 0)) {
212  int n = 0;
213 
214  for (s = strtok_r(v, ":", &ptr); s != NULL; s = strtok_r(NULL, ":", &ptr)) {
215  int i;
216 
217  switch (n) {
218  case 0:
219  i = atoi(s);
220  r->weight = i;
221  break;
222  case 1:
223  i = atol(s);
224  r->date = i;
225  break;
226  case 2:
227  strlcpy(r->ipres, s, sizeof (r->ipres));
228  break;
229  case 3:
230  strlcpy(r->msg, s, sizeof (r->msg));
231  break;
232  default:
233  break;
234  }
235  if (n < DIM_BL)
236  fields[n] = s;
237  n++;
238  }
239  }
240  }
241  zeDb_Unlock(&hdb);
242 
243  return found;
244 }
245 
246 
247 
248 /*
249 typedef struct db_map_T
250 {
251  char why[32];
252  char key[32];
253 
254  time_t date;
255  int weight;
256  char msg[64];
257  char ipres[24];
258 } db_map_T;
259 */
260 
261 /* ****************************************************************************
262  * *
263  * *
264  **************************************************************************** */
265 #define SZPOOL 32
266 
267 typedef struct blpool_T {
268  bool ok;
269  pthread_mutex_t mutex;
270  char *bl[SZPOOL];
272 } blpool_T;
273 
274 static blpool_T blpool = { FALSE, PTHREAD_MUTEX_INITIALIZER };
275 
276 #define DATA_LOCK() \
277  if (pthread_mutex_lock(&blpool.mutex) != 0) { \
278  ZE_LogSysError("pthread_mutex_lock"); \
279  }
280 
281 #define DATA_UNLOCK() \
282  if (pthread_mutex_unlock(&blpool.mutex) != 0) { \
283  ZE_LogSysError("pthread_mutex_unlock"); \
284  }
285 
286 /* ****************************************************************************
287  * *
288  * *
289  **************************************************************************** */
290 static int
291 db_map_get_index(bl)
292  char *bl;
293 {
294  int i;
295 
296  if ((bl == NULL) || (strlen(bl) == 0)) {
297  ZE_MessageWarning(8, "Blacklist name empty or NULL pointer");
298  return -1;
299  }
300 
301  for (i = 0; (i < SZPOOL) && (blpool.bl[i] != NULL); i++) {
302  if (strcasecmp(bl, blpool.bl[i]) == 0)
303  break;
304  }
305 
306  if (i >= SZPOOL)
307  ZE_MessageWarning(8, "Blacklist %s not found");
308 
309  return i;
310 }
311 
312 /* ****************************************************************************
313  * *
314  * *
315  **************************************************************************** */
316 bool
318  char *bl;
319 {
320  bool res = TRUE;
321  char path[1024];
322  int i = 0;
323 
324 
325  if ((bl == NULL) || (strlen(bl) == 0)) {
326  ZE_MessageWarning(8, "Blacklist name empty or NULL pointer");
327  return FALSE;
328  }
329 
330  DATA_LOCK();
331 
332  i = db_map_get_index(bl);
333 
334  if (i < 0) {
336  "Blacklist pool is xxxx - consider increasing it's size");
337  DATA_UNLOCK();
338  return FALSE;
339  }
340 
341  if (i >= SZPOOL) {
343  "Blacklist pool is full - consider increasing it's size");
344  DATA_UNLOCK();
345  return FALSE;
346  }
347 
348  if ((blpool.bl[i] != NULL) || zeDb_OK(&blpool.hdb[i])) {
349  ZE_MessageWarning(8, "Blacklist %s already open", bl);
350  DATA_UNLOCK();
351  return TRUE;
352  }
353 
354  if ((blpool.bl[i] = strdup(bl)) == NULL) {
355  DATA_UNLOCK();
356  ZE_LogSysError("strdup(%s) error", bl);
357  return FALSE;
358  }
359 
360  snprintf(path, sizeof (path), "%s/%s.db", cf_get_str(CF_WDBDIR), bl);
361 
362  zeDb_Lock(&blpool.hdb[i]);
363  if (!zeDb_OK(&blpool.hdb[i]))
364  res = zeDb_Open(&blpool.hdb[i], work_db_env, path, 0644, FALSE, TRUE, 0);
365  zeDb_Unlock(&blpool.hdb[i]);
366 
367  ZE_MessageInfo(9, "Database %s created/openned using handler no %d !", bl, i);
368 
369  DATA_UNLOCK();
370 
371  return res;
372 }
373 
374 /* ****************************************************************************
375  * *
376  * *
377  **************************************************************************** */
378 bool
380  char *bl;
381 {
382  bool res = TRUE;
383  int i = 0;
384 
385  if ((bl == NULL) || (strlen(bl) == 0)) {
386  ZE_MessageWarning(8, "Blacklist name empty or NULL pointer");
387  return FALSE;
388  }
389 
390  DATA_LOCK();
391 
392  i = db_map_get_index(bl);
393 
394  if ((i < 0) || (i >= SZPOOL)) {
395  ZE_MessageWarning(8, "Blacklist not found");
396  DATA_UNLOCK();
397  return FALSE;
398  }
399 
400  if ((blpool.bl[i] == NULL) || !zeDb_OK(&blpool.hdb[i])) {
401  ZE_MessageWarning(8, "Blacklist %s already closed", STRNULL(bl, "(null)"));
402  DATA_UNLOCK();
403  return TRUE;
404  }
405 
406  zeDb_Lock(&blpool.hdb[i]);
407  if (zeDb_OK(&blpool.hdb[i]))
408  res = zeDb_Close(&blpool.hdb[i]);
409  zeDb_Unlock(&blpool.hdb[i]);
410  FREE(blpool.bl[i]);
411 
412  ZE_MessageInfo(9, "Database %s closed !", bl);
413 
414  DATA_UNLOCK();
415 
416  return res;
417 }
418 
419 /* ****************************************************************************
420  * *
421  * *
422  **************************************************************************** */
423 bool
425 {
426  bool res = TRUE;
427  int i = 0;
428 
429  DATA_LOCK();
430 
431  for (i = 0; i < SZPOOL; i++) {
432  FREE(blpool.bl[i]);
433 
434  if (!zeDb_OK(&blpool.hdb[i]))
435  continue;
436 
437  zeDb_Lock(&blpool.hdb[i]);
438  if (zeDb_OK(&blpool.hdb[i]))
439  res = zeDb_Close(&blpool.hdb[i]);
440  zeDb_Unlock(&blpool.hdb[i]);
441  }
442 
443  DATA_UNLOCK();
444 
445  return res;
446 }
447 
448 /* ****************************************************************************
449  * *
450  * *
451  **************************************************************************** */
452 
453 bool
454 db_map_check(bl, why, key, buf, sz)
455  char *bl;
456  char *why;
457  char *key;
458  char *buf;
459  size_t sz;
460 {
461  char k[256];
462  char v[1024];
463  bool found = FALSE;
464  int imin = 0, imax = SZPOOL - 1;
465  int i;
466 
467  /*
468  * new ...
469  */
470  if ((bl == NULL) || (strlen(bl) == 0)) {
471  ZE_MessageWarning(8, "Blacklist name empty or NULL pointer");
472  return FALSE;
473  }
474 
475  DATA_LOCK();
476 
477  if ((bl != NULL) && (strlen(bl) > 0)) {
478  i = db_map_get_index(bl);
479 
480  if (i < 0) {
482  "Blacklist pool is xxxx - consider increasing it's size");
483  DATA_UNLOCK();
484  return FALSE;
485  }
486 
487  if (i >= SZPOOL) {
489  "Blacklist pool is full - consider increasing it's size");
490  DATA_UNLOCK();
491  return FALSE;
492  }
493  imin = imax = i;
494  }
495 
496  for (i = imin; i <= imax; i++) {
497  zeDb_Lock(&blpool.hdb[i]);
498 
499  snprintf(k, sizeof (k), "%s:%s", why, key);
500  if (zeDb_GetRec(&blpool.hdb[i], k, &v, sizeof (v))) {
501  strlcpy(buf, v, sz);
502  found = TRUE;
503  }
504  zeDb_Unlock(&blpool.hdb[i]);
505 
506  if (found)
507  break;
508  }
509 
510  DATA_LOCK();
511 
512  return found;
513 }
514 
515 /* ****************************************************************************
516  * *
517  * *
518  **************************************************************************** */
519 
520 bool
521 db_map_add(bl, why, key, buf)
522  char *bl;
523  char *why;
524  char *key;
525  char *buf;
526 {
527  char k[256];
528  char v[1024];
529  bool ok = FALSE;
530  int i;
531 
532  /*
533  * new ...
534  */
535  if ((bl == NULL) || (strlen(bl) == 0)) {
536  ZE_MessageWarning(8, "Blacklist name empty or NULL pointer");
537  return FALSE;
538  }
539 
540  DATA_LOCK();
541 
542  i = db_map_get_index(bl);
543 
544  if ((i < 0) || (i >= SZPOOL)) {
545  ZE_MessageWarning(8, "Map %s not found !");
546  DATA_UNLOCK();
547  return FALSE;
548  }
549 
550  zeDb_Lock(&blpool.hdb[i]);
551 
552  snprintf(k, sizeof (k), "%s:%s", why, key);
553 
554  ok = zeDb_AddRec(&blpool.hdb[i], k, &v, strlen(v) + 1);
555 
556  zeDb_Unlock(&blpool.hdb[i]);
557 
558  DATA_LOCK();
559 
560  return ok;
561 }
struct blpool_T blpool_T
bool db_map_close(char *bl)
Definition: ze-dbbl.c:379
bool zeDb_Unlock(ZEDB_T *h)
Database unlock.
Definition: zeDb.c:907
int npm
Definition: ze-dbbl.c:40
bool zeDb_AddRec(ZEDB_T *h, char *, void *, size_t)
Definition: zeDb.c:750
bool zeDb_OK(ZEDB_T *h)
Definition: zeDb.c:649
#define FREE(x)
Definition: macros.h:37
time_t date
Definition: ze-dbbl.h:44
#define STRNULL(x, r)
Definition: macros.h:81
char ipres[24]
Definition: ze-dbbl.h:45
int weight
Definition: ze-dbbl.h:43
Definition: zeDb.h:47
#define FALSE
Definition: macros.h:160
#define strlcpy
Definition: zeString.h:32
#define SZPOOL
Definition: ze-dbbl.c:265
#define DIM_BL
Definition: ze-dbbl.c:187
ZEDB_ENV_T * work_db_env
Definition: ze-databases.c:30
dbblq_T data[DREF]
Definition: ze-dbbl.c:45
bool db_check_blacklist(char *ip)
Definition: ze-dbbl.c:103
bool db_map_open(char *bl)
Definition: ze-dbbl.c:317
pthread_mutex_t mutex
Definition: ze-dbbl.c:269
bool db_map_close_all()
Definition: ze-dbbl.c:424
bool zeDb_Open(ZEDB_T *h, ZEDB_ENV_T *, char *, int, bool, bool, size_t)
Definition: zeDb.c:462
Definition: ze-dbbl.c:43
bool ok
Definition: ze-dbbl.c:268
char * bl[SZPOOL]
Definition: ze-dbbl.c:270
bool zeDb_Close(ZEDB_T *h)
Definition: zeDb.c:667
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
#define DATA_LOCK()
Definition: ze-dbbl.c:276
bool zeDb_GetRec(ZEDB_T *h, char *, void *, size_t)
Definition: zeDb.c:791
#define TRUE
Definition: macros.h:157
#define ZE_MessageWarning(level,...)
Definition: zeSyslog.h:92
#define ZE_LogSysError(...)
Definition: zeSyslog.h:129
#define DATA_UNLOCK()
Definition: ze-dbbl.c:281
char * cf_get_str(int id)
Definition: ze-cf.c:854
bool db_blackliste_check(char *why, char *key, db_map_T *r)
Definition: ze-dbbl.c:190
#define CF_WDBDIR
Definition: cfh-defs.h:77
ZEDB_T hdb[SZPOOL]
Definition: ze-dbbl.c:271
#define DREF
Definition: ze-dbbl.c:35
bool db_close_blacklist()
Definition: ze-dbbl.c:83
int no
Definition: ze-dbbl.c:39
char msg[64]
Definition: ze-dbbl.h:46
uint32_t tref
Definition: ze-dbbl.c:38
bool db_map_check(char *bl, char *why, char *key, char *buf, size_t sz)
Definition: ze-dbbl.c:454
bool db_update_blacklist(char *ip, int what)
Definition: ze-dbbl.c:145
bool db_open_blacklist()
Definition: ze-dbbl.c:56
#define ZEDB_INITIALIZER
Definition: zeDb.h:111
long uint32_t
Definition: ze-sys.h:489
bool db_map_add(char *bl, char *why, char *key, char *buf)
Definition: ze-dbbl.c:521
bool zeDb_Lock(ZEDB_T *h)
Database lock.
Definition: zeDb.c:896