#!/usr/bin/python3 # import os import sys HOOK_NAME = 'ucs-apt-hook' FILE_OVERRIDE_HOOK = '/etc/apt/ucs-apt-hook.override' FILE_ENABLE_DEBUG = '/etc/apt/ucs-apt-hook.enable-debug' PROTECTED_PACKAGES = [ 'univention-server-master', 'univention-server-backup', 'univention-server-slave', 'univention-server-member', ] DEBUG = os.path.exists(FILE_ENABLE_DEBUG) def debug(msg: str) -> None: if DEBUG: print(f'D: {HOOK_NAME}: {msg}') def log(msg: str) -> None: print(f'W: {HOOK_NAME}: {msg}') def die(msg: str) -> None: print(f'E: {HOOK_NAME}: {msg}') sys.exit(1) INFO_FD = os.environ.get('APT_HOOK_INFO_FD') if not INFO_FD or INFO_FD == '0' or not INFO_FD.isdigit(): die('APT_HOOK_INFO_FD not set up correctly') fd = os.fdopen(int(INFO_FD)) if not fd: die(f'could not open APT_HOOK_INFO_FD ({INFO_FD!r})') line = fd.readline().strip() debug(f'VERSION: {line!r}') if line != 'VERSION 2': die('expecting hook protocol in version 2') for line in fd: line = line.strip() if not line: break for line in fd: line = line.strip() debug(f'INPUT: {line!r}') if not line: continue try: pkg, oldver, direction, newver, action = line.split(' ', 4) except ValueError: log(f'received unexpected line from apt: {line!r}') continue if pkg in PROTECTED_PACKAGES: if action == '**REMOVE**': if not os.path.exists(FILE_OVERRIDE_HOOK): for line in f''' !!! ERROR !!! An attempt was made to uninstall the {pkg!r} meta package. This usually leads to a defective UCS system. The trigger for the uninstallation attempt can be a problem when resolving automatic package dependencies or a manual uninstallation attempt. If this message occurred during an app installation/update/uninstall or a UCS system update, please visit https://help.univention.de/REPLACEME for the next steps. '''.splitlines(): log(line.strip()) sys.exit(1)