numa_hit 215129169 numa_miss 0 numa_foreign 0 interleave_hit 938 local_node 215129169 other_node 0 )F# Copyright (C) 2009-2010 Canonical Ltd. # Copyright (C) 2012, 2013 Hewlett-Packard Development Company, L.P. # # Author: Scott Moser # Author: Juerg Haefliger # # This file is part of cloud-init. See LICENSE file for license information. """Set Passwords: Set user passwords and enable/disable SSH password auth""" import logging import random import re import string from typing import List from cloudinit import features, lifecycle, subp, util from cloudinit.cloud import Cloud from cloudinit.config import Config from cloudinit.config.schema import MetaSchema from cloudinit.distros import ALL_DISTROS, Distro, ug_util from cloudinit.log import log_util from cloudinit.settings import PER_INSTANCE from cloudinit.ssh_util import update_ssh_config meta: MetaSchema = { "id": "cc_set_passwords", "distros": [ALL_DISTROS], "frequency": PER_INSTANCE, "activate_by_schema_keys": [], } LOG = logging.getLogger(__name__) def get_users_by_type(users_list: list, pw_type: str) -> list: """either password or type: RANDOM is required, user is always required""" return ( [] if not users_list else [ (item["name"], item.get("password", "RANDOM")) for item in users_list if item.get("type", "hash") == pw_type ] ) def _restart_ssh_daemon(distro: Distro, service: str, *extra_args: str): try: distro.manage_service("restart", service, *extra_args) LOG.debug("Restarted the SSH daemon.") except subp.ProcessExecutionError as e: LOG.warning( "'ssh_pwauth' configuration may not be applied. Cloud-init was " "unable to restart SSH daemon due to error: '%s'", e, ) def handle_ssh_pwauth(pw_auth, distro: Distro): """Apply sshd PasswordAuthentication changes. @param pw_auth: config setting from 'pw_auth'. Best given as True, False, or "unchanged". @param distro: an instance of the distro class for the target distribution @return: None""" service = distro.get_option("ssh_svcname", "ssh") cfg_name = "PasswordAuthentication" if isinstance(pw_auth, str): lifecycle.deprecate( deprecated="Using a string value for the 'ssh_pwauth' key", deprecated_version="22.2", extra_message="Use a boolean value with 'ssh_pwauth'.", ) if util.is_true(pw_auth): cfg_val = "yes" elif util.is_false(pw_auth): cfg_val = "no" else: bmsg = f"Leaving SSH config '{cfg_name}' unchanged." if pw_auth is None or pw_auth.lower() == "unchanged": LOG.debug("%s ssh_pwauth=%s", bmsg, pw_auth) else: LOG.warning("%s Unrecognized value: ssh_pwauth=%s", bmsg, pw_auth) return updated = update_ssh_config({cfg_name: cfg_val}) if not updated: LOG.debug("No need to restart SSH service, %s not updated.", cfg_name) return if distro.uses_systemd(): state = subp.subp( [ "systemctl", "show", "--property", "ActiveState", "--value", service, ] ).stdout.strip() if state.lower() in ["active", "activating", "reloading"]: # This module runs Before=sshd.service. What that means is that # the code can only get to this point if a user manually starts the # network stage. While this isn't a well-supported use-case, this # does cause a deadlock if started via systemd directly: # "systemctl start cloud-init.service". Prevent users from causing # this deadlock by forcing systemd to ignore dependencies when # restarting. Note that this deadlock is not possible in newer # versions of cloud-init, since starting the second service doesn't