102 lines
3.0 KiB
Python
102 lines
3.0 KiB
Python
"""Select an installer identity and pin a trusted SSH destination."""
|
|||
|
|
|
||
|
|
import pwd
|
||
|
|
import re
|
||
|
|
import stat
|
||
|
|
from hashlib import sha256
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from . import engine
|
||
|
|
from .config import InstallError
|
||
|
|
|
||
|
|
|
||
|
|
def root_ssh_directory():
|
||
|
|
return Path(pwd.getpwuid(0).pw_dir) / ".ssh"
|
||
|
|
|
||
|
|
|
||
|
|
def root_controlled(path, kind):
|
||
|
|
try:
|
||
|
|
info = path.lstat()
|
||
|
|
except OSError:
|
||
|
|
return False
|
||
|
|
return kind(info.st_mode) and info.st_uid == 0 and not info.st_mode & 0o022
|
||
|
|
|
||
|
|
|
||
|
|
def available_identities():
|
||
|
|
directory = root_ssh_directory()
|
||
|
|
if not root_controlled(directory, stat.S_ISDIR):
|
||
|
|
return ()
|
||
|
|
identities = []
|
||
|
|
for path in sorted(directory.iterdir()):
|
||
|
|
try:
|
||
|
|
engine.root_file(path, 0o600)
|
||
|
|
with path.open("rb") as stream:
|
||
|
|
header = stream.readline(96)
|
||
|
|
except (InstallError, OSError):
|
||
|
|
continue
|
||
|
|
if re.fullmatch(rb"-----BEGIN [A-Z ]*PRIVATE KEY-----\r?\n", header):
|
||
|
|
identities.append(str(path))
|
||
|
|
return tuple(identities)
|
||
|
|
|
||
|
|
|
||
|
|
def fingerprint(key):
|
||
|
|
if not re.fullmatch(
|
||
|
|
r"(?:ssh-|ecdsa-sha2-)[a-z0-9@.\-]+ [A-Za-z0-9+/=]+", key
|
||
|
|
):
|
||
|
|
raise InstallError("Paste the verified SSH host public key.")
|
||
|
|
result = engine.run(
|
||
|
|
"ssh-keygen",
|
||
|
|
"-lf",
|
||
|
|
"-",
|
||
|
|
source=f"{key}\n".encode("ascii"),
|
||
|
|
label="Validate the SSH host public key",
|
||
|
|
)
|
||
|
|
match = re.search(r"\bSHA256:[A-Za-z0-9+/]+", result)
|
||
|
|
if match is None:
|
||
|
|
raise InstallError("Read the fingerprint of the SSH host public key.")
|
||
|
|
return match[0]
|
||
|
|
|
||
|
|
|
||
|
|
def trusted_keys(host, port):
|
||
|
|
directory = root_ssh_directory()
|
||
|
|
path = directory / "known_hosts"
|
||
|
|
if not root_controlled(directory, stat.S_ISDIR) or not root_controlled(
|
||
|
|
path, stat.S_ISREG
|
||
|
|
):
|
||
|
|
return {}
|
||
|
|
endpoint = host if port == 22 else f"[{host}]:{port}"
|
||
|
|
try:
|
||
|
|
entries = engine.run(
|
||
|
|
"ssh-keygen",
|
||
|
|
"-F",
|
||
|
|
endpoint,
|
||
|
|
"-f",
|
||
|
|
path,
|
||
|
|
label="Look up the destination in root's known_hosts",
|
||
|
|
)
|
||
|
|
except InstallError:
|
||
|
|
return {}
|
||
|
|
trusted, revoked = {}, set()
|
||
|
|
for line in entries.splitlines():
|
||
|
|
fields = line.split()
|
||
|
|
if len(fields) >= 4 and fields[0] == "@revoked":
|
||
|
|
revoked.add(" ".join(fields[2:4]))
|
||
|
|
if len(fields) < 3 or fields[0].startswith(("#", "@")):
|
||
|
|
continue
|
||
|
|
key = " ".join(fields[1:3])
|
||
|
|
try:
|
||
|
|
label = f"{fields[1]} {fingerprint(key)}"
|
||
|
|
except InstallError:
|
||
|
|
continue
|
||
|
|
trusted[label] = key
|
||
|
|
return {label: key for label, key in trusted.items() if key not in revoked}
|
||
|
|
|
||
|
|
|
||
|
|
def store_host_key(host, port, key):
|
||
|
|
directory = engine.STATE / "ssh-host-keys"
|
||
|
|
engine.root_directory(engine.STATE, mode=0o700)
|
||
|
|
engine.root_directory(directory, mode=0o700)
|
||
|
|
digest = sha256(f"{host}:{port}\n{key}".encode("ascii")).hexdigest()
|
||
|
|
path = directory / f"{digest}.pub"
|
||
|
|
engine.put_file(path, key + "\n", mode=0o600)
|
||
|
|
return str(path)
|