From 400838b790a5d377f4292e14f12f7af5d8effa9c Mon Sep 17 00:00:00 2001 From: KiekerJan Date: Sun, 5 Nov 2023 21:45:11 +0100 Subject: [PATCH] Use sshd -T instead of directly reading the configuration files --- management/dns_update.py | 16 +++++--------- management/status_checks.py | 44 +++++++++---------------------------- management/utils.py | 24 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 45 deletions(-) diff --git a/management/dns_update.py b/management/dns_update.py index 9a768ea8..b62b3754 100755 --- a/management/dns_update.py +++ b/management/dns_update.py @@ -9,7 +9,7 @@ import ipaddress import rtyaml import dns.resolver -from utils import shell, load_env_vars_from_file, safe_domain_name, sort_domains +from utils import shell, load_env_vars_from_file, safe_domain_name, sort_domains, get_ssh_port from ssl_certificates import get_ssl_certificates, check_certificate # From https://stackoverflow.com/questions/3026957/how-to-validate-a-domain-name-using-regex-php/16491074#16491074 @@ -454,16 +454,10 @@ def build_sshfp_records(): # if SSH has been configured to listen on a nonstandard port, we must # specify that port to sshkeyscan. - port = 22 - with open('/etc/ssh/sshd_config', 'r') as f: - for line in f: - s = line.rstrip().split() - if len(s) == 2 and s[0] == 'Port': - try: - port = int(s[1]) - except ValueError: - pass - break + port = get_ssh_port() + # If nothing returned, assume default + if not port: + port = 22 keys = shell("check_output", ["ssh-keyscan", "-4", "-t", "rsa,dsa,ecdsa,ed25519", "-p", str(port), "localhost"]) keys = sorted(keys.split("\n")) diff --git a/management/status_checks.py b/management/status_checks.py index 07e7dc1d..56a9a4fe 100755 --- a/management/status_checks.py +++ b/management/status_checks.py @@ -18,7 +18,7 @@ from web_update import get_web_domains, get_domains_with_a_records from ssl_certificates import get_ssl_certificates, get_domain_ssl_files, check_certificate from mailconfig import get_mail_domains, get_mail_aliases -from utils import shell, sort_domains, load_env_vars_from_file, load_settings +from utils import shell, sort_domains, load_env_vars_from_file, load_settings, get_ssh_port, get_ssh_config_value def get_services(): return [ @@ -66,24 +66,6 @@ def run_checks(rounded_values, env, output, pool, domains_to_check=None): run_network_checks(env, output) run_domain_checks(rounded_values, env, output, pool, domains_to_check=domains_to_check) -def get_ssh_port(): - # Returns ssh port - try: - output = shell('check_output', ['sshd', '-T']) - except FileNotFoundError: - # sshd is not installed. That's ok. - return None - - returnNext = False - for e in output.split(): - if returnNext: - return int(e) - if e == "port": - returnNext = True - - # Did not find port! - return None - def run_services_checks(env, output, pool): # Check that system services are running. all_running = True @@ -208,21 +190,15 @@ def is_port_allowed(ufw, port): return any(re.match(str(port) +"[/ \t].*", item) for item in ufw) def check_ssh_password(env, output): - # Check that SSH login with password is disabled. The openssh-server - # package may not be installed so check that before trying to access - # the configuration file. - if not os.path.exists("/etc/ssh/sshd_config"): - return - with open("/etc/ssh/sshd_config", "r") as f: - sshd = f.read() - if re.search("\nPasswordAuthentication\s+yes", sshd) \ - or not re.search("\nPasswordAuthentication\s+no", sshd): - output.print_error("""The SSH server on this machine permits password-based login. A more secure - way to log in is using a public key. Add your SSH public key to $HOME/.ssh/authorized_keys, check - that you can log in without a password, set the option 'PasswordAuthentication no' in - /etc/ssh/sshd_config, and then restart the openssh via 'sudo service ssh restart'.""") - else: - output.print_ok("SSH disallows password-based login.") + config_value = get_ssh_config_value("passwordauthentication") + if config_value: + if config_value == "no": + output.print_ok("SSH disallows password-based login.") + else: + output.print_error("""The SSH server on this machine permits password-based login. A more secure + way to log in is using a public key. Add your SSH public key to $HOME/.ssh/authorized_keys, check + that you can log in without a password, set the option 'PasswordAuthentication no' in + /etc/ssh/sshd_config, and then restart the openssh via 'sudo service ssh restart'.""") def is_reboot_needed_due_to_package_installation(): return os.path.exists("/var/run/reboot-required") diff --git a/management/utils.py b/management/utils.py index b5ca7e59..00110d23 100644 --- a/management/utils.py +++ b/management/utils.py @@ -178,6 +178,30 @@ def wait_for_service(port, public, env, timeout): return False time.sleep(min(timeout/4, 1)) +def get_ssh_port(): + return int(get_ssh_config_value("port")) + +def get_ssh_config_value(parameter_name): + # Returns ssh port + try: + output = shell('check_output', ['sshd', '-T']) + except FileNotFoundError: + # sshd is not installed. That's ok. + return None + except subprocess.CalledProcessError: + # error while calling shell command + return None + + returnNext = False + for e in output.split(): + if returnNext: + return e + if e == parameter_name: + returnNext = True + + # Did not find port! + return None + if __name__ == "__main__": from web_update import get_web_domains env = load_environment()