ssh: Restrict logins to groups root, admin and freedombox-ssh

Tests:

- Remove restricted console logins. Try to login via SSH with non-admin and note
that it fails. sudo into the user succeeds.

- Add a user to freedombox-ssh group from Users & Groups app. Login with SSH
succeeds.

- Login with admin user succeeds with and without adding to freedombox-ssh
group.

- On a fresh install, non-admin users are not restricted.

- On an upgrade from a version with the patch, non-admin users are restricted.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2022-12-05 20:47:08 -05:00
1 parent 7d4283d7b8
commit 21c8a8945f
3 files changed
+36 -2

No files matched your search

+20
View File
@@ -3,6 +3,7 @@
import grp
import os
import pathlib
import pwd
import shutil
import stat
@@ -13,6 +14,8 @@ import augeas
from plinth import action_utils, utils
from plinth.actions import privileged
config_file = pathlib.Path('/etc/ssh/sshd_config.d/freedombox.conf')
def _validate_user(username, password, must_be_admin=True):
"""Validate a user."""
@@ -53,6 +56,23 @@ def setup():
action_utils.dpkg_reconfigure('openssh-server', {})
@privileged
def restrict_users(should_restrict: bool):
"""Restrict SSH logins to groups root, admin and freedombox-ssh."""
if not should_restrict:
config_file.unlink(missing_ok=True)
else:
config_file.write_text('AllowGroups root admin freedombox-ssh\n',
encoding='utf-8')
action_utils.service_reload('sshd')
def are_users_restricted() -> bool:
"""Return whether only restricted groups of users are allowed."""
return config_file.exists()
def get_user_homedir(username):
"""Return the home dir of a user by looking up in password database."""
try: