From b5c3c9523f2706e35910685fcaafc9f423abc1cb Mon Sep 17 00:00:00 2001 From: jakob <131469091+seriousbusinessprofessional@users.noreply.github.com> Date: Wed, 22 Nov 2023 11:29:41 -0500 Subject: [PATCH] Update verifyinstall.py added brew support to verify installs using alternate backend --- libpkpass/commands/verifyinstall.py | 94 ++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/libpkpass/commands/verifyinstall.py b/libpkpass/commands/verifyinstall.py index a5ee9eb..87897c4 100644 --- a/libpkpass/commands/verifyinstall.py +++ b/libpkpass/commands/verifyinstall.py @@ -1,8 +1,12 @@ """This is used to check the os requirements""" from os import path +from platform import python_version +from re import search +from subprocess import check_output, CalledProcessError from shutil import which from libpkpass.commands.command import Command - +from libpkpass.errors import BadBackendError +from libpkpass._version import get_versions class VerifyInstall(Command): #################################################################### @@ -23,7 +27,7 @@ def _run_command_execution(self): #################################################################### """Run function for class.""" #################################################################### - yield from print_messages(check_required_software, "installed software check") + yield from print_messages(check_required_software, "installed software check", SCBackend=self.args["SCBackend"]) yield from print_messages( check_passdb, "passdb check", @@ -41,36 +45,106 @@ def _run_command_execution(self): def _validate_args(self): pass +def get_backend(SCBackend): + if SCBackend == "opensc": + return(SCBackend) + elif SCBackend == "yubi": + return(SCBackend) + raise BadBackendError(SCBackend) + + def check_exists(name): #################################################################### """Check whether a program exists in path""" #################################################################### - return which(name) is not None + path=which(name) + if path is not None: + return path +def check_exists_brew(name): + whichcmd="brew --prefix --installed "+ name + " 2> /dev/null" + try: + path=check_output(whichcmd, shell=True).decode('utf-8').strip() + #print(path) + except CalledProcessError: + path=None + return path def print_messages(func, msg, **kwargs): yield f"Starting {msg}" yield func(**kwargs) +def check_brew(): + if which("brew"): + return True + else: + return False + #may need an exception here to catch which not existing -def check_required_software(): - required_tools = { - "pkcs15-tool (available via opensc)": ["pkcs15-tool"], - "ssl (openssl or libressl)": ["openssl", "libressl"], - } +def check_required_software(**kwargs): + print("Using Python Version "+python_version()) + print("Using Pkpass Version: "+get_versions()["version"]) + SCBackend=get_backend(kwargs['SCBackend']) + print("Using SCBackend: "+SCBackend) + if SCBackend=="yubi": + required_tools = { + "ssl (openssl or libressl)": ["openssl", "libressl"], + "yubico-piv-tool": ["yubico-piv-tool", "libp11"], + } + elif SCBackend=="opensc": + required_tools = { + "pkcs15-tool (available via opensc)": ["pkcs15-tool", "opensc"], + "ssl (openssl or libressl)": ["openssl", "libressl"], + } not_found = [] + found = [] + paths = [] + brew=check_brew() for key, value in required_tools.items(): found_tool = False for tool in value: - if check_exists(tool): - found_tool = True + if brew: + brewexists=check_exists_brew(tool) + if brewexists: + found.append(tool) + found_tool = True + print(tool+" is installed with brew") + paths.append(brewexists) + else: + exists=check_exists(tool) + if exists: + found.append(tool) + found_tool = True + print(tool+" is installed") + paths.append(exists) if not found_tool: not_found.append(key) + matches = dict(zip(found,paths)) if not_found: return "The following packages were not found: \n\t%s" % "\n\t".join(not_found) + if brew and SCBackend=="yubi": + check_links(matches) return "Successful installed software check" +def check_links(software): + software['local']="/usr/local" + for package,path in software.items(): + pathlib=path+"/lib" + needspkcs=["openssl","libp11","local"] + needslibykcs=["yubico-piv-tool","local"] + checkdir=check_output(["ls","-l",pathlib]).decode('utf-8').strip() + if search("engines-3", checkdir): + if search("pkcs11.dylib", check_output(["ls","-l",pathlib+"/engines-3"]).decode('utf-8').strip()): + print("pkcs11.dylib exists in "+pathlib+"/engines-3") + elif package in needspkcs: + print("Required packages are installed, however no file was detected at "+pathlib+"/engines-3/pkcs11.dylib . A link may be needed.") + if search("libykcs11.dylib", checkdir): + print("libykcs11.dylib exists in "+pathlib) + elif package in needslibykcs: + print("Required packages are installed, however no file was detected at "+pathlib+"libykcs11.dylib . A link may be needed.") + + def check_passdb(cabundle, pwstore, certpath=None, keypath=None, connect=None): ret_msg = []