#! /usr/bin/env python3 # -*- coding: utf8 -*- """ This script scans all apache vhosts on the system, checks if they use letsencrypt certificates for which acmetool has renewed the certificate and replaces the old certificate with the new one automatically. This way, acmetool can run as an unprivileged user and acme-updater can take care of swapping out certificates while running as root. Requirements: * python 3.x * module OpenSSL """ import os import logging import datetime import shutil import subprocess import OpenSSL # directory for enabled apache vhosts, might be different on your distribution VHOSTS_DIRECTORY = "/etc/apache2/sites-enabled" # Log level, default is info LOG_LEVEL = logging.INFO # State directory for acmetool, default is /var/lib/acme ACME_STATE_DIR = "/var/lib/acme" LOGGER = logging.getLogger("acme-updater") LOGGER.setLevel(LOG_LEVEL) CONSOLE_HANDLER = logging.StreamHandler() LOGGER.addHandler(CONSOLE_HANDLER) PARSED_VHOSTS = [] CERT_RENEWED = False def parse_vhost(file_obj): """ Parses a given vhost file and extracts the main domain, the certificate file and the TLS key file. :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 = "" for line in file_obj: if "