mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
- Avoid flake8 warnings. - Makes the call more explicitly readable in case an exception is expected but check=True is not passed by mistake. Tests: - Many tests are skipped since the changes are considered trivial. check=False is already the default for subprocess.run() method. - actions/package: Install an app when it is not installed. - actions/upgrade: Run manual upgrades. - actions/users: Change a user password. Login. Create/remove a user. - actions/zoph: Restore a database. - container: On a fresh repository, run ./container up,ssh,stop,destroy for a testing container. - plinth/action_utils.py: Enable/disable an app that has a running service. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
72 lines
1.9 KiB
Python
72 lines
1.9 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import logging
|
|
import os
|
|
import pwd
|
|
import subprocess
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from plinth.actions import superuser_run
|
|
from plinth.errors import ActionError
|
|
from plinth.modules.email_server import interproc
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def exists_nam(username):
|
|
"""Returns True if the user's home directory exists"""
|
|
try:
|
|
passwd = pwd.getpwnam(username)
|
|
except KeyError as e:
|
|
raise ValidationError(_('User does not exist')) from e
|
|
return _exists(passwd)
|
|
|
|
|
|
def exists_uid(uid_number):
|
|
"""Returns True if the user's home directory exists"""
|
|
try:
|
|
passwd = pwd.getpwuid(uid_number)
|
|
except KeyError as e:
|
|
raise ValidationError(_('User does not exist')) from e
|
|
return _exists(passwd)
|
|
|
|
|
|
def _exists(passwd):
|
|
return os.path.exists(passwd.pw_dir)
|
|
|
|
|
|
def put_nam(username):
|
|
"""Create a home directory for the user (identified by username)"""
|
|
_put('nam', username)
|
|
|
|
|
|
def put_uid(uid_number):
|
|
"""Create a home directory for the user (identified by UID)"""
|
|
_put('uid', str(uid_number))
|
|
|
|
|
|
def _put(arg_type, user_info):
|
|
try:
|
|
args = ['-i', 'home', 'mk', arg_type, user_info]
|
|
superuser_run('email_server', args)
|
|
except ActionError as e:
|
|
raise RuntimeError('Action script failure') from e
|
|
|
|
|
|
def action_mk(arg_type, user_info):
|
|
if arg_type == 'nam':
|
|
passwd = pwd.getpwnam(user_info)
|
|
elif arg_type == 'uid':
|
|
passwd = pwd.getpwuid(int(user_info))
|
|
else:
|
|
raise ValueError('Unknown arg_type')
|
|
|
|
args = ['sudo', '-n', '--user=#' + str(passwd.pw_uid)]
|
|
args.extend(['/bin/sh', '-c', 'mkdir -p ~'])
|
|
completed = subprocess.run(args, capture_output=True, check=False)
|
|
if completed.returncode != 0:
|
|
interproc.log_subprocess(completed)
|
|
raise OSError('Could not create home directory')
|