check_slapdd_crc32/check_slapdd_crc32

148 lines
4.3 KiB
Plaintext
Raw Permalink Normal View History

2020-07-03 17:19:54 +02:00
#!/usr/bin/python3
"""
OpenLDAP tool to check CRC32 of LDIF files of slapd.d directory
"""
import argparse
import binascii
import logging
import os
import re
import sys
2024-03-13 23:37:29 +01:00
version = "0.0"
default_slapdd_path = "/etc/ldap/slapd.d"
2020-07-03 17:19:54 +02:00
2022-05-01 12:59:51 +02:00
# Main
2024-03-13 23:37:29 +01:00
parser = argparse.ArgumentParser(description=f"{__doc__} (version: {version})")
2020-07-03 17:19:54 +02:00
2024-03-13 23:37:29 +01:00
parser.add_argument("-d", "--debug", action="store_true", help="Show debug messages")
2020-07-03 17:19:54 +02:00
2024-03-13 23:37:29 +01:00
parser.add_argument("-v", "--verbose", action="store_true", help="Show verbose messages")
2020-07-03 17:19:54 +02:00
parser.add_argument(
2024-03-13 23:37:29 +01:00
"-l", "--log-file", action="store", type=str, dest="logfile", help="Log file path"
2020-07-03 17:19:54 +02:00
)
parser.add_argument(
2024-03-13 23:37:29 +01:00
"-C",
"--console",
action="store_true",
help="Also log on console (even if log file is provided)",
2020-07-03 17:19:54 +02:00
)
2024-03-13 23:37:29 +01:00
parser.add_argument("-f", "--fix", action="store_true", help="Fix CRC32 value in LDIF files")
2020-07-03 17:19:54 +02:00
parser.add_argument(
2024-03-13 23:37:29 +01:00
"-p",
"--path",
action="store",
2020-07-03 17:19:54 +02:00
type=str,
2024-03-13 23:37:29 +01:00
dest="slapdd_path",
help=f"Default slapd.d directory path (default: {default_slapdd_path}",
default=default_slapdd_path,
2020-07-03 17:19:54 +02:00
)
options = parser.parse_args()
# Initialize log
log = logging.getLogger()
2022-05-01 12:59:51 +02:00
logformat = logging.Formatter(
2024-03-13 23:37:29 +01:00
f"%(asctime)s - {os.path.basename(sys.argv[0])} - %(levelname)s - " "%(message)s"
)
2020-07-03 17:19:54 +02:00
if options.debug:
log.setLevel(logging.DEBUG)
elif options.verbose:
log.setLevel(logging.INFO)
else:
log.setLevel(logging.WARNING)
if options.logfile:
logfile = logging.FileHandler(options.logfile)
logfile.setFormatter(logformat)
log.addHandler(logfile)
if not options.logfile or options.console:
logconsole = logging.StreamHandler()
logconsole.setFormatter(logformat)
log.addHandler(logconsole)
2022-05-01 12:59:51 +02:00
2020-07-03 17:19:54 +02:00
def check_file(dir_path, file_name):
"""
Check CRC32 of an LDIF file
:param dir_path: The directory path
:param file_name: The file name
"""
path = os.path.join(dir_path, file_name)
lines = []
current_crc32 = None
try:
2024-03-13 23:37:29 +01:00
with open(path, "rb") as fd:
2020-07-03 17:19:54 +02:00
for line in fd.readlines():
2024-03-13 23:37:29 +01:00
if line.startswith(b"# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify."):
logging.debug("%s: AUTO-GENERATED line detected, pass (%s)", path, line)
2020-07-03 17:19:54 +02:00
continue
2024-03-13 23:37:29 +01:00
if line.startswith(b"# CRC32 "):
2022-05-01 12:59:51 +02:00
logging.debug(
2024-03-13 23:37:29 +01:00
"%s: CRC32 line detected, retrieve current CRC32 value (%s)", path, line
)
current_crc32 = re.match("^# CRC32 (.*)$", line.decode()).group(1)
logging.debug('%s: current CRC32 found is "%s"', path, current_crc32)
2020-07-03 17:19:54 +02:00
continue
lines.append(line)
2024-03-13 23:37:29 +01:00
except OSError as err:
logging.error("%s: fail to read file content (%s)", path, err)
2020-07-03 17:19:54 +02:00
return False
2022-05-01 12:59:51 +02:00
# pylint: disable=consider-using-f-string
2024-03-13 23:37:29 +01:00
crc32 = ("%08X" % ((binascii.crc32(b"".join(lines)) & 0xFFFFFFFF) % (1 << 32))).lower()
2020-07-03 17:19:54 +02:00
if current_crc32:
if current_crc32 == crc32:
2024-03-13 23:37:29 +01:00
log.info("%s: current CRC32 value is correct (%s)", path, crc32)
2020-07-03 17:19:54 +02:00
else:
2024-03-13 23:37:29 +01:00
log.warning("%s: invalid CRC32 value found (%s != %s)", path, current_crc32, crc32)
2020-07-03 17:19:54 +02:00
fix_crc32(path, crc32, lines)
else:
2024-03-13 23:37:29 +01:00
log.warning('%s: no CRC32 value found. Correct CRC32 value is "%s".', path, crc32)
2020-07-03 17:19:54 +02:00
fix_crc32(path, crc32, lines)
return True
2022-05-01 12:59:51 +02:00
2020-07-03 17:19:54 +02:00
def fix_crc32(path, crc32, lines):
"""
Fix CRC32 value of an LDIF file
:param path: The file path
:param crc32: The CRC32 value of the file
:param lines: Array of file lines without headers
"""
if not options.fix:
return True
try:
headers_lines = [
2024-03-13 23:37:29 +01:00
b"# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.\n",
b"# CRC32 " + crc32.encode() + b"\n",
]
2024-03-13 23:37:29 +01:00
with open(path, "wb") as fd:
for line in headers_lines + lines:
2020-07-03 17:19:54 +02:00
fd.write(line)
2024-03-13 23:37:29 +01:00
except OSError as err:
logging.error("%s: fail to write new file content (%s)", path, err)
2020-07-03 17:19:54 +02:00
return False
return True
2022-05-01 12:59:51 +02:00
2020-07-03 17:19:54 +02:00
log.info('Checking CRC32 in slapd directory "%s"', options.slapdd_path)
for dirpath, dnames, fnames in os.walk(options.slapdd_path):
2022-05-01 12:59:51 +02:00
log.debug(
2024-03-13 23:37:29 +01:00
'%s: sub-dirs = "%s", files = "%s"', dirpath, '", "'.join(dnames), '", "'.join(fnames)
)
2020-07-03 17:19:54 +02:00
for fname in fnames:
2024-03-13 23:37:29 +01:00
if fname.endswith(".ldif"):
2020-07-03 17:19:54 +02:00
check_file(dirpath, fname)