# SPDX-FileCopyrightText: 2016-2023 Helmut Pozimski # # SPDX-License-Identifier: GPL-2.0-only # -*- coding: utf8 -*- """ collection of helper functions used in other modules of acme-updater. """ import logging import datetime import os import shutil import hashlib import subprocess import OpenSSL import dns.tsigkeyring import dns.update import dns.query from typing import List from amulib.vhost_entry import ApacheVhostEntry LOGGER = logging.getLogger("acme-updater") def parse_apache_vhost(file_obj) -> List[ApacheVhostEntry]: """ Parses a given vhost file and extracts the main domain, the certificate file, the TLS key file and all domains contained within the vhost. :param file_obj: file obj pointing to a vhost to parse :return: list of tuples with domains and found certificates :rtype: list """ vhost_started = False parsed_info = [] cert_path = "" key_path = "" main_domain = "" domains = set() for line in file_obj: if " list: """ Extracts the subjectAltName entries from a X509 certficiate :param certificate: the certificate to extract the subjectAltName \ entries from :type certificate: OpenSSL.crypto.X509 :return: list of hostnames :rtype: list """ alt_names = [] for i in range(0, certificate.get_extension_count(), 1): if certificate.get_extension(i).get_short_name() == b"subjectAltName": extension_string = str(certificate.get_extension(i)) for entry in extension_string.split(","): alt_names.append(entry.split(":")[1]) break return alt_names def restart_service(service_name: str): if os.path.exists("/run/systemd/system"): subprocess.call(["/usr/bin/systemctl", "restart", service_name]) else: subprocess.call(["/etc/init.d/%s" % service_name, "restart"])