1
1
mirror of https://github.com/jrbrtsn/ban2fail synced 2024-06-16 03:48:03 +00:00
ban2fail/offEntry.c

270 lines
7.2 KiB
C
Raw Normal View History

2019-11-23 03:40:23 +00:00
/***************************************************************************
* Copyright (C) 2019 by John D. Robertson *
* john@rrci.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
2019-11-29 22:23:16 +00:00
#define _GNU_SOURCE
2019-11-23 03:40:23 +00:00
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
2019-12-03 02:56:55 +00:00
#include <string.h>
2019-11-23 03:40:23 +00:00
#include "ban2fail.h"
#include "cntry.h"
2019-12-03 02:56:55 +00:00
#include "ez_libdb.h"
2019-11-29 14:00:39 +00:00
#include "ez_libc.h"
2019-11-23 03:40:23 +00:00
#include "map.h"
2019-12-01 22:23:30 +00:00
#include "offEntry.h"
2019-11-23 03:40:23 +00:00
#include "util.h"
/********************************************************/
2019-12-02 03:29:32 +00:00
/**************** OFFENTRY ******************************/
2019-11-23 03:40:23 +00:00
/********************************************************/
static void
2019-12-02 03:29:32 +00:00
common_constructor(OFFENTRY *self)
2019-11-23 03:40:23 +00:00
/******************************************************************
* common portion for all constructors.
*/
{
memset(self, 0, sizeof(*self));
}
2019-12-02 03:29:32 +00:00
OFFENTRY*
OFFENTRY_addr_constructor(OFFENTRY *self, const char *addr)
2019-11-23 03:40:23 +00:00
/********************************************************
* Prepare for use.
*/
{
2019-12-02 03:29:32 +00:00
OFFENTRY *rtn= NULL;
2019-11-23 03:40:23 +00:00
common_constructor(self);
strncpy(self->addr, addr, sizeof(self->addr)-1);
const char *cntry= COUNTRY_get_code(self->addr);
if(cntry)
strncpy(self->cntry, cntry, 2);
rtn= self;
abort:
return rtn;
}
2019-12-02 03:29:32 +00:00
OFFENTRY*
OFFENTRY_cache_constructor(OFFENTRY *self, const char *cacheFileEntry)
2019-11-23 03:40:23 +00:00
/********************************************************
* Prepare for use.
*/
{
2019-12-02 03:29:32 +00:00
OFFENTRY *rtn= NULL;
2019-11-23 03:40:23 +00:00
common_constructor(self);
2019-12-04 02:38:51 +00:00
long long ll;
2019-11-23 03:40:23 +00:00
2019-12-04 02:38:51 +00:00
int rc= sscanf(cacheFileEntry, "%u %u %lld %45s %2s"
, &self->count
, &self->severity
, &ll
, self->addr
, self->cntry
);
if(4 > rc) {
2019-11-23 03:40:23 +00:00
eprintf("ERROR: failed to interpret \"%s\"", cacheFileEntry);
goto abort;
}
2019-12-04 02:38:51 +00:00
self->latest= ll;
#ifdef qqDEBUG
if(self->severity) {
eprintf("%s : %u", self->addr, self->severity);
}
#endif
2019-11-23 03:40:23 +00:00
rtn= self;
abort:
return rtn;
}
void*
2019-12-02 03:29:32 +00:00
OFFENTRY_destructor(OFFENTRY *self)
2019-11-23 03:40:23 +00:00
/********************************************************
* Free resources.
*/
{
2019-12-01 18:04:41 +00:00
if(self->dns.name)
2019-11-30 19:14:42 +00:00
free(self->dns.name);
2019-11-23 03:40:23 +00:00
return self;
}
void
2019-12-04 02:38:51 +00:00
OFFENTRY_register(OFFENTRY *self, unsigned severity, time_t when)
2019-11-23 03:40:23 +00:00
/********************************************************
* Register the current failure try.
*/
{
/* Keep track of count */
++self->count;
2019-12-04 02:38:51 +00:00
#ifdef qqDEBUG
if(severity) {
eprintf("Severity= %u", severity);
}
#endif
/* Keep track of most severe match */
if(self->severity < severity)
self->severity= severity;
/* Keep track of the most recent offense time */
if(self->latest < when)
self->latest= when;
2019-11-23 03:40:23 +00:00
}
int
2019-12-02 03:29:32 +00:00
OFFENTRY_cacheWrite(OFFENTRY *self, FILE *fh)
2019-11-23 03:40:23 +00:00
/********************************************************
* Write to the cache file in a form we can read later.
*/
{
2019-12-04 02:38:51 +00:00
ez_fprintf(fh, "%u %u %lld %s %s\n"
2019-11-23 03:40:23 +00:00
, self->count
2019-12-04 02:38:51 +00:00
, self->severity
, (long long)self->latest
2019-11-23 03:40:23 +00:00
, self->addr
, self->cntry
);
return 0;
}
int
2019-12-02 03:29:32 +00:00
OFFENTRY_print(OFFENTRY *self, FILE *fh)
2019-11-23 03:40:23 +00:00
/********************************************************
* Print a human readable representation of *self.
*/
{
ez_fprintf(fh,
2019-12-04 02:38:51 +00:00
"\tLOGENTRY %p { addr= \"%s\", cntry= \"%2s\" count= %u, severity= %u }\n"
2019-11-23 03:40:23 +00:00
, self
, self->addr
, self->cntry
, self->count
2019-12-04 02:38:51 +00:00
, self->severity
2019-11-23 03:40:23 +00:00
);
return 0;
}
int
2019-12-02 03:29:32 +00:00
OFFENTRY_map_addr(OFFENTRY *self, MAP *h_rtnMap)
2019-11-23 03:40:23 +00:00
/********************************************************
2019-12-02 03:29:32 +00:00
* Create a map of OFFENTRY objects with composite
2019-11-23 03:40:23 +00:00
* counts by address.
*/
{
2019-12-02 03:29:32 +00:00
OFFENTRY *e= MAP_findStrItem(h_rtnMap, self->addr);
2019-11-23 03:40:23 +00:00
if(!e) {
2019-12-02 03:29:32 +00:00
OFFENTRY_addr_create(e, self->addr);
2019-11-23 03:40:23 +00:00
assert(e);
MAP_addStrKey(h_rtnMap, e->addr, e);
}
e->count += self->count;
2019-12-04 02:38:51 +00:00
if(e->severity < self->severity)
e->severity= self->severity;
if(e->latest < self->latest)
e->latest= self->latest;
2019-11-23 03:40:23 +00:00
return 0;
}
int
2019-12-02 03:29:32 +00:00
OFFENTRY_offenseCount(OFFENTRY *self, unsigned *h_sum)
2019-11-23 03:40:23 +00:00
/********************************************************
* Get a count of all offenses for this entry.
*/
{
*h_sum += self->count;
2019-12-03 02:56:55 +00:00
//eprintf("%s numItems= %u", self->addr, PTRVEC_numItems(&self->rptObj_vec));
2019-11-23 03:40:23 +00:00
return 0;
}
2019-12-04 02:38:51 +00:00
int
OFFENTRY_list(OFFENTRY *self, FILE *fh, int flags, unsigned nAllowed)
/********************************************************
* Print in listing form
*/
{
static const struct bitTuple BlockBitTuples[]= {
{.name= "BLK", .bit= BLOCKED_FLG},
{.name= "+blk+", .bit= WOULD_BLOCK_FLG},
{.name= "-blk-", .bit= UNJUST_BLOCK_FLG},
{.name= "WL", .bit= WHITELIST_FLG},
{/* Terminating member */}
};
const static struct bitTuple dns_flagsArr[]= {
{.name= "~", .bit= PDNS_FWD_FAIL_FLG},
2019-12-04 14:11:39 +00:00
{.name= "!", .bit= PDNS_FWD_MISMATCH_FLG},
2019-12-04 02:38:51 +00:00
{.name= "!!", .bit= PDNS_FWD_NONE_FLG},
{.name= "NXDOMAIN", .bit= PDNS_NXDOMAIN_FLG},
{.name= "SERVFAIL", .bit= PDNS_SERVFAIL_FLG},
{}
};
2019-12-06 13:44:51 +00:00
#ifdef OLD
2019-12-04 02:38:51 +00:00
const static char *dns_fmt= "%u %-13s %-15s\t%5u/%-4d offenses %s [%s] %s %s\n",
*fmt= "%u %-13s %-15s\t%5u/%-4d offenses %s [%s]\n";
ez_fprintf(fh, self->dns.flags ? dns_fmt : fmt
, self->severity
, self->latest ? local_strftime(&self->latest, "%b %d %H:%M") : ""
, self->addr
, self->count
, nAllowed
, self->cntry[0] ? self->cntry : "--"
, bits2str(flags, BlockBitTuples)
, self->dns.name ? self->dns.name : ""
, bits2str(self->dns.flags, dns_flagsArr)
);
2019-12-06 13:44:51 +00:00
#else
const static char *dns_fmt= "%u %-13s %5u/%-4d offenses %s [%-3s] %s \t%s %s\n",
*fmt= "%u %-13s %5u/%-4d offenses %s [%-3s] %s\n";
ez_fprintf(fh, self->dns.flags ? dns_fmt : fmt
, self->severity
, self->latest ? local_strftime(&self->latest, "%b %d %H:%M") : ""
, self->count
, nAllowed
, self->cntry[0] ? self->cntry : "--"
, bits2str(flags, BlockBitTuples)
, self->addr
, self->dns.name ? self->dns.name : ""
, bits2str(self->dns.flags, dns_flagsArr)
);
#endif
2019-12-04 02:38:51 +00:00
return 0;
}