Improve logic for locking down SSH config

what a tricky mess
This commit is contained in:
Lance Edgar 2019-02-09 17:55:12 -06:00
parent 93c2db902e
commit 05d6e093a7

View file

@ -50,6 +50,7 @@ def configure(c, allow_root=False):
path = '/etc/ssh/sshd_config'
# PermitRootLogin no (or without-password)
# TODO: this probably needs the same treatment as PasswordAuthentication got
if c.run("grep '^PermitRootLogin ' {}".format(path), warn=True).failed:
c.sudo('sed -i.bak -e "s/^#PermitRootLogin .*/PermitRootLogin {}/" {}'.format(
'without-password' if allow_root else 'no', path))
@ -59,8 +60,11 @@ def configure(c, allow_root=False):
# PasswordAuthentication no
if c.run("grep '^PasswordAuthentication ' {}".format(path), warn=True).failed:
c.sudo('sed -i.bak -e "s/^#?PasswordAuthentication .*/PasswordAuthentication no/" {}'.format(path))
if c.run("grep '^#PasswordAuthentication ' {}".format(path), warn=True).failed:
c.sudo("""bash -c 'echo "PasswordAuthentication no" >> /etc/ssh/sshd_config'""")
else:
c.sudo('sed -i.bak -e "s/^PasswordAuthentication .*/PasswordAuthentication no/" {}'.format(path))
c.sudo("sed -i.bak -e 's/^#PasswordAuthentication .*/PasswordAuthentication no/' {}".format(path))
else:
c.sudo("sed -i.bak -e 's/^PasswordAuthentication .*/PasswordAuthentication no/' {}".format(path))
restart(c)