1
0
Fork 0
mirror of https://github.com/ethauvin/fail2ban-digest.git synced 2025-04-26 02:57:12 -07:00

create a new empty database when deleting the old one

when --delete is used we should leave an empty database in place. If
fail2ban_digest is used via cron the missing DB will cause an error to
be triggered and the admin will be mailed because one cron job failed,
which is not the case actually
This commit is contained in:
Enrico Tagliavini 2017-06-23 11:29:38 +02:00
parent 0cfe4f940e
commit ad8670c376

View file

@ -116,14 +116,19 @@ def add(db, ip):
def digest(db, delete):
db_file = db_location + '/' + db + '.dbm'
new_db_file = db_location + '/.' + db + '.dbm'
try:
db = db_busy_open(db_file, 'r', 30) # this is just a trick to lock the DB, to have a consistent copy
except OSError as e:
# we raise a RuntimeError instead of an OSError to have prettier output
raise RuntimeError('Error while opening database file %s: %s' % (repr(db_file), str(e))) from None
if delete:
#os.rename(db_file, db_file + '.digest') # just in case we want to delay the removal in the future
os.unlink(db_file)
# wipe the DB, leave an empty one so maildigest will find it (if called from cron a failure would mail
# the admin)
with dbm.open(new_db_file, 'n') as new_db:
new_db[db_creation_date_key] = datetime.utcnow().strftime(db_date_format).encode('UTF-8')
os.rename(new_db_file, db_file)
try:
db_creation_date = db[db_creation_date_key].decode('UTF-8')
except KeyError as e: