setools/sechecker
Chris PeBenito 244cd81ff7 Replace str.format() with f-strings.
Signed-off-by: Chris PeBenito <chpebeni@linux.microsoft.com>
2024-04-02 15:36:38 -04:00

73 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright 2020 Microsoft Corporation
#
# SPDX-License-Identifier: GPL-2.0-only
#
import setools
import argparse
import sys
import logging
import signal
import warnings
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
parser = argparse.ArgumentParser(description="SELinux policy checker tool.")
parser.add_argument("--version", action="version", version=setools.__version__)
parser.add_argument("config", help="Path to the checker configuration file.")
parser.add_argument("policy", help="Path to the SELinux policy to check.", nargs="?")
parser.add_argument("-o", "--output_file", help="Path to log output.", required=False)
parser.add_argument("-v", "--verbose", action="store_true",
help="Print extra informational messages")
parser.add_argument("--debug", action="store_true", dest="debug", help="Enable debugging.")
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s|%(levelname)s|%(name)s|%(message)s')
if not sys.warnoptions:
warnings.simplefilter("default")
elif args.verbose:
logging.basicConfig(level=logging.INFO, format='%(message)s')
if not sys.warnoptions:
warnings.simplefilter("default")
else:
logging.basicConfig(level=logging.WARNING, format='%(message)s')
if not sys.warnoptions:
warnings.simplefilter("ignore")
try:
p = setools.SELinuxPolicy(args.policy)
c = setools.PolicyChecker(p, args.config)
if args.output_file:
with open(args.output_file, "w", encoding="utf-8") as fd:
failures = c.run(output=fd)
else:
failures = c.run()
sys.exit(1 if failures else 0)
except setools.exception.InvalidCheckerConfig as err:
if args.debug:
raise
else:
print(err)
sys.exit(2)
except AssertionError:
# Always provide a traceback for assertion errors
raise
except Exception as err:
if args.debug:
raise
else:
print(err)
sys.exit(3)