From 0cfe4f940e4eb77f7323ac822334e5556ee10de9 Mon Sep 17 00:00:00 2001 From: Enrico Tagliavini Date: Fri, 23 Jun 2017 11:09:40 +0200 Subject: [PATCH] better handle missing dbm file, print better error Using the dbm module with autodetection is quite broken. Opening a dbm file with 'r' flag will raise a useless dbm.error, not inherinting OSError. The reason for the error is lost. When forcing GNU dbm module a proper exception is raised. --- bin/fail2ban_digest | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/fail2ban_digest b/bin/fail2ban_digest index 3ac64c9..ca012af 100755 --- a/bin/fail2ban_digest +++ b/bin/fail2ban_digest @@ -18,6 +18,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. from datetime import datetime, timedelta +from dbm import gnu as dbm from email.message import EmailMessage from smtplib import SMTP from string import Template @@ -26,7 +27,6 @@ from traceback import print_exc import argparse import atexit -import dbm import errno import os import socket @@ -116,7 +116,11 @@ def add(db, ip): def digest(db, delete): db_file = db_location + '/' + db + '.dbm' - db = db_busy_open(db_file, 'r', 30) # this is just a trick to lock the DB, to have a consistent copy + 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) @@ -237,7 +241,6 @@ if __name__ == '__main__': help = 'send email to specified user / address. Default is root' ) - args = parser.parse_args(sys.argv[1:]) #print(args) try: