1
0
Fork 0
mirror of https://github.com/ethauvin/fail2ban-digest.git synced 2025-04-27 03:08:12 -07:00

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.
This commit is contained in:
Enrico Tagliavini 2017-06-23 11:09:40 +02:00
parent c10b8a6c94
commit 0cfe4f940e

View file

@ -18,6 +18,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from datetime import datetime, timedelta from datetime import datetime, timedelta
from dbm import gnu as dbm
from email.message import EmailMessage from email.message import EmailMessage
from smtplib import SMTP from smtplib import SMTP
from string import Template from string import Template
@ -26,7 +27,6 @@ from traceback import print_exc
import argparse import argparse
import atexit import atexit
import dbm
import errno import errno
import os import os
import socket import socket
@ -116,7 +116,11 @@ def add(db, ip):
def digest(db, delete): def digest(db, delete):
db_file = db_location + '/' + db + '.dbm' 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: if delete:
#os.rename(db_file, db_file + '.digest') # just in case we want to delay the removal in the future #os.rename(db_file, db_file + '.digest') # just in case we want to delay the removal in the future
os.unlink(db_file) os.unlink(db_file)
@ -237,7 +241,6 @@ if __name__ == '__main__':
help = 'send email to specified user / address. Default is root' help = 'send email to specified user / address. Default is root'
) )
args = parser.parse_args(sys.argv[1:]) args = parser.parse_args(sys.argv[1:])
#print(args) #print(args)
try: try: