2015-08-05 22:58:35 +00:00
|
|
|
import fnmatch
|
2014-10-01 16:42:40 +00:00
|
|
|
import os
|
2014-07-25 15:17:50 +00:00
|
|
|
import textwrap
|
|
|
|
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2015-04-05 18:02:36 +00:00
|
|
|
from orchestra.contrib.orchestration import ServiceController, replace
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.resources import ServiceMonitor
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-01 16:42:40 +00:00
|
|
|
from . import settings
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-01 16:42:40 +00:00
|
|
|
|
2016-03-08 10:16:49 +00:00
|
|
|
class UNIXUserController(ServiceController):
|
2015-04-23 19:46:23 +00:00
|
|
|
"""
|
2015-04-24 11:39:20 +00:00
|
|
|
Basic UNIX system user/group support based on <tt>useradd</tt>, <tt>usermod</tt>, <tt>userdel</tt> and <tt>groupdel</tt>.
|
2015-05-12 12:38:40 +00:00
|
|
|
Autodetects and uses ACL if available, for better permission management.
|
2015-04-23 19:46:23 +00:00
|
|
|
"""
|
2015-04-05 18:02:36 +00:00
|
|
|
verbose_name = _("UNIX user")
|
2014-09-30 16:06:42 +00:00
|
|
|
model = 'systemusers.SystemUser'
|
2023-07-11 19:37:00 +00:00
|
|
|
# actions = ('save', 'delete', 'set_permission', 'validate_paths_exist', 'create_link')
|
|
|
|
actions = ('save', 'delete', 'set_permission', 'create_link')
|
2015-05-12 12:38:40 +00:00
|
|
|
doc_settings = (settings, (
|
|
|
|
'SYSTEMUSERS_DEFAULT_GROUP_MEMBERS',
|
|
|
|
'SYSTEMUSERS_MOVE_ON_DELETE_PATH',
|
|
|
|
'SYSTEMUSERS_FORBIDDEN_PATHS'
|
|
|
|
))
|
2014-10-01 16:42:40 +00:00
|
|
|
|
|
|
|
def save(self, user):
|
|
|
|
context = self.get_context(user)
|
2015-04-04 17:44:07 +00:00
|
|
|
if not context['user']:
|
|
|
|
return
|
2023-07-11 19:37:00 +00:00
|
|
|
|
|
|
|
if context['home'] != context['base_home']:
|
|
|
|
self.append(textwrap.dedent("""
|
|
|
|
if [[ ! -e '%(home)s' ]]; then
|
|
|
|
echo "%(home)s path does not exists." >&2
|
|
|
|
exit 0
|
|
|
|
fi""") % context
|
|
|
|
)
|
|
|
|
|
2021-01-13 15:41:36 +00:00
|
|
|
if not user.active:
|
|
|
|
self.append(textwrap.dedent("""
|
|
|
|
#Just disable that user, if it exists
|
|
|
|
if id %(user)s ; then
|
|
|
|
usermod %(user)s --password '%(password)s'
|
|
|
|
fi
|
|
|
|
""") % context)
|
|
|
|
return
|
2015-03-26 16:00:30 +00:00
|
|
|
# TODO userd add will fail if %(user)s group already exists
|
2014-10-01 16:42:40 +00:00
|
|
|
self.append(textwrap.dedent("""
|
2015-05-21 17:53:59 +00:00
|
|
|
# Update/create user state for %(user)s
|
2015-09-20 11:35:22 +00:00
|
|
|
if id %(user)s ; then
|
2016-05-05 11:58:35 +00:00
|
|
|
usermod %(user)s --home '%(home)s' \\
|
2015-05-13 17:22:54 +00:00
|
|
|
--password '%(password)s' \\
|
2016-05-05 11:58:35 +00:00
|
|
|
--shell '%(shell)s' \\
|
|
|
|
--groups '%(groups)s'
|
2014-10-01 16:42:40 +00:00
|
|
|
else
|
2015-05-21 17:53:59 +00:00
|
|
|
useradd_code=0
|
2016-05-05 11:58:35 +00:00
|
|
|
useradd %(user)s --home '%(home)s' \\
|
2015-05-13 17:22:54 +00:00
|
|
|
--password '%(password)s' \\
|
2016-05-05 11:58:35 +00:00
|
|
|
--shell '%(shell)s' \\
|
|
|
|
--groups '%(groups)s' || useradd_code=$?
|
2015-05-21 17:53:59 +00:00
|
|
|
if [[ $useradd_code -eq 8 ]]; then
|
|
|
|
# User is logged in, kill and retry
|
|
|
|
pkill -u %(user)s; sleep 2
|
|
|
|
pkill -9 -u %(user)s; sleep 1
|
2016-05-05 11:58:35 +00:00
|
|
|
useradd %(user)s --home '%(home)s' \\
|
2015-05-21 17:53:59 +00:00
|
|
|
--password '%(password)s' \\
|
2016-05-05 11:58:35 +00:00
|
|
|
--shell '%(shell)s' \\
|
|
|
|
--groups '%(groups)s'
|
2015-05-21 17:53:59 +00:00
|
|
|
elif [[ $useradd_code -ne 0 ]]; then
|
|
|
|
exit $useradd_code
|
|
|
|
fi
|
2014-10-01 16:42:40 +00:00
|
|
|
fi
|
2016-05-05 11:58:35 +00:00
|
|
|
mkdir -p '%(base_home)s'
|
|
|
|
chmod 750 '%(base_home)s'
|
2015-10-15 22:31:54 +00:00
|
|
|
""") % context
|
2015-03-10 16:57:23 +00:00
|
|
|
)
|
2015-04-05 22:34:47 +00:00
|
|
|
if context['home'] != context['base_home']:
|
2016-05-05 11:58:35 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
2015-05-21 17:53:59 +00:00
|
|
|
# Set extra permissions: %(user)s home is inside %(mainuser)s home
|
2021-01-13 15:41:36 +00:00
|
|
|
if true; then
|
2023-07-10 18:16:52 +00:00
|
|
|
# if mount | grep "^$(df %(home)s|grep '^/'|cut -d' ' -f1)\s" | grep acl > /dev/null; then
|
2015-10-03 14:35:34 +00:00
|
|
|
# Account group as the owner
|
2016-05-05 11:58:35 +00:00
|
|
|
chown %(mainuser)s:%(mainuser)s '%(home)s'
|
|
|
|
chmod g+s '%(home)s'
|
2015-05-12 12:38:40 +00:00
|
|
|
# Home access
|
|
|
|
setfacl -m u:%(user)s:--x '%(mainuser_home)s'
|
|
|
|
# Grant perms to future files within the directory
|
2016-05-05 11:58:35 +00:00
|
|
|
setfacl -m d:u:%(user)s:rwx '%(home)s'
|
2015-05-12 12:38:40 +00:00
|
|
|
# Grant access to main user
|
2016-05-05 11:58:35 +00:00
|
|
|
setfacl -m d:u:%(mainuser)s:rwx '%(home)s'
|
2015-05-12 12:38:40 +00:00
|
|
|
else
|
|
|
|
chmod g+rxw %(home)s
|
|
|
|
fi""") % context
|
2015-04-05 22:34:47 +00:00
|
|
|
)
|
2015-10-03 14:35:34 +00:00
|
|
|
else:
|
2015-10-15 22:31:54 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
2016-05-05 11:58:35 +00:00
|
|
|
chown %(user)s:%(group)s '%(home)s'
|
2015-10-15 22:31:54 +00:00
|
|
|
ls -A /etc/skel/ | while read line; do
|
2016-05-05 11:58:35 +00:00
|
|
|
if [[ ! -e "%(home)s/${line}" ]]; then
|
|
|
|
cp -a "/etc/skel/${line}" "%(home)s/${line}" && \\
|
|
|
|
chown -R %(user)s:%(group)s "%(home)s/${line}"
|
2015-10-15 22:31:54 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
""") % context
|
|
|
|
)
|
2014-10-28 09:51:27 +00:00
|
|
|
for member in settings.SYSTEMUSERS_DEFAULT_GROUP_MEMBERS:
|
|
|
|
context['member'] = member
|
2015-04-09 14:32:10 +00:00
|
|
|
self.append('usermod -a -G %(user)s %(member)s || exit_code=$?' % context)
|
2014-10-28 09:51:27 +00:00
|
|
|
if not user.is_main:
|
2015-04-09 14:32:10 +00:00
|
|
|
self.append('usermod -a -G %(user)s %(mainuser)s || exit_code=$?' % context)
|
2014-10-01 16:42:40 +00:00
|
|
|
|
|
|
|
def delete(self, user):
|
|
|
|
context = self.get_context(user)
|
2015-04-04 17:44:07 +00:00
|
|
|
if not context['user']:
|
|
|
|
return
|
2015-05-21 14:32:06 +00:00
|
|
|
self.append(textwrap.dedent("""
|
2023-07-11 19:37:00 +00:00
|
|
|
if ! id %(user)s &> /dev/null; then
|
|
|
|
echo "user %(user)s not exitst" >&2;
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2015-05-21 14:32:06 +00:00
|
|
|
# Delete %(user)s user
|
2015-05-12 12:38:40 +00:00
|
|
|
nohup bash -c 'sleep 2 && killall -u %(user)s -s KILL' &> /dev/null &
|
2015-03-10 21:51:10 +00:00
|
|
|
killall -u %(user)s || true
|
2015-04-26 13:53:00 +00:00
|
|
|
userdel %(user)s || exit_code=$?
|
2015-05-21 17:53:59 +00:00
|
|
|
groupdel %(group)s || exit_code=$?\
|
2015-04-05 22:34:47 +00:00
|
|
|
""") % context
|
2015-03-10 16:57:23 +00:00
|
|
|
)
|
2015-04-09 14:32:10 +00:00
|
|
|
if context['deleted_home']:
|
2016-02-16 12:04:52 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Move home into SYSTEMUSERS_MOVE_ON_DELETE_PATH, nesting if exists.
|
|
|
|
deleted_home="%(deleted_home)s"
|
2016-05-05 11:58:35 +00:00
|
|
|
while [[ -e "$deleted_home" ]]; do
|
2016-02-16 12:04:52 +00:00
|
|
|
deleted_home="${deleted_home}/$(basename ${deleted_home})"
|
|
|
|
done
|
2016-05-05 11:58:35 +00:00
|
|
|
mv '%(base_home)s' "$deleted_home" || exit_code=$?
|
2016-02-16 12:04:52 +00:00
|
|
|
""") % context
|
|
|
|
)
|
2015-04-09 14:32:10 +00:00
|
|
|
else:
|
2016-06-17 10:00:04 +00:00
|
|
|
self.append("rm -fr -- '%(base_home)s'" % context)
|
2014-10-17 10:04:47 +00:00
|
|
|
|
2015-08-05 22:58:35 +00:00
|
|
|
def grant_permissions(self, user, context):
|
|
|
|
context['perms'] = user.set_perm_perms
|
|
|
|
# Capital X adds execution permissions for directories, not files
|
|
|
|
context['perms_X'] = context['perms'] + 'X'
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Grant execution permissions to every parent directory
|
|
|
|
for access_path in %(access_paths)s; do
|
|
|
|
# Preserve existing ACLs
|
|
|
|
acl=$(getfacl -a "$access_path" | grep '^user:%(user)s:') && {
|
|
|
|
perms=$(echo "$acl" | cut -d':' -f3)
|
|
|
|
perms=$(echo "$perms" | cut -c 1,2)x
|
|
|
|
setfacl -m u:%(user)s:$perms "$access_path"
|
|
|
|
} || setfacl -m u:%(user)s:--x "$access_path"
|
|
|
|
done
|
|
|
|
# Grant perms to existing files, excluding execution
|
|
|
|
find '%(perm_to)s' -type f %(exclude_acl)s \\
|
|
|
|
-exec setfacl -m u:%(user)s:%(perms)s {} \\;
|
|
|
|
# Grant perms to extisting directories and set defaults for future content
|
|
|
|
find '%(perm_to)s' -type d %(exclude_acl)s \\
|
|
|
|
-exec setfacl -m u:%(user)s:%(perms_X)s -m d:u:%(user)s:%(perms_X)s {} \\;
|
|
|
|
# Account group as the owner of new files
|
|
|
|
chmod g+s '%(perm_to)s'""") % context
|
|
|
|
)
|
|
|
|
if not user.is_main:
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Grant access to main user
|
|
|
|
find '%(perm_to)s' -type d %(exclude_acl)s \\
|
|
|
|
-exec setfacl -m d:u:%(mainuser)s:rwx {} \\;\
|
|
|
|
""") % context
|
|
|
|
)
|
|
|
|
|
2015-11-26 10:42:18 +00:00
|
|
|
def revoke_permissions(self, user, context):
|
2015-08-05 22:58:35 +00:00
|
|
|
revoke_perms = {
|
|
|
|
'rw': '',
|
|
|
|
'r': 'w',
|
|
|
|
'w': 'r',
|
|
|
|
}
|
2015-11-26 10:42:18 +00:00
|
|
|
context.update({
|
|
|
|
'perms': revoke_perms[user.set_perm_perms],
|
|
|
|
'option': '-x' if user.set_perm_perms == 'rw' else '-m'
|
|
|
|
})
|
2015-08-05 22:58:35 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Revoke permissions
|
|
|
|
find '%(perm_to)s' %(exclude_acl)s \\
|
2015-11-26 10:42:18 +00:00
|
|
|
-exec setfacl %(option)s u:%(user)s:%(perms)s {} \\;\
|
2015-08-05 22:58:35 +00:00
|
|
|
""") % context
|
|
|
|
)
|
|
|
|
|
2015-05-11 14:05:39 +00:00
|
|
|
def set_permission(self, user):
|
2014-11-14 16:52:54 +00:00
|
|
|
context = self.get_context(user)
|
2015-05-08 14:05:57 +00:00
|
|
|
context.update({
|
2015-05-11 14:05:39 +00:00
|
|
|
'perm_action': user.set_perm_action,
|
|
|
|
'perm_to': os.path.join(user.set_perm_base_home, user.set_perm_home_extension),
|
2015-05-08 14:05:57 +00:00
|
|
|
})
|
2015-05-11 14:05:39 +00:00
|
|
|
exclude_acl = []
|
2015-05-12 12:38:40 +00:00
|
|
|
for exclude in settings.SYSTEMUSERS_FORBIDDEN_PATHS:
|
2015-08-04 09:47:39 +00:00
|
|
|
context['exclude_acl'] = os.path.join(user.set_perm_base_home, exclude)
|
2015-05-13 12:16:51 +00:00
|
|
|
exclude_acl.append('-not -path "%(exclude_acl)s"' % context)
|
2015-05-12 12:38:40 +00:00
|
|
|
context['exclude_acl'] = ' \\\n -a '.join(exclude_acl) if exclude_acl else ''
|
2015-08-04 09:47:39 +00:00
|
|
|
# Access paths
|
|
|
|
head = user.set_perm_base_home
|
|
|
|
relative = ''
|
|
|
|
access_paths = ["'%s'" % head]
|
|
|
|
for tail in user.set_perm_home_extension.split(os.sep)[:-1]:
|
|
|
|
relative = os.path.join(relative, tail)
|
|
|
|
for exclude in settings.SYSTEMUSERS_FORBIDDEN_PATHS:
|
|
|
|
if fnmatch.fnmatch(relative, exclude):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# No match
|
|
|
|
head = os.path.join(head, tail)
|
|
|
|
access_paths.append("'%s'" % head)
|
|
|
|
context['access_paths'] = ' '.join(access_paths)
|
2015-08-05 22:58:35 +00:00
|
|
|
|
2015-05-11 14:05:39 +00:00
|
|
|
if user.set_perm_action == 'grant':
|
2015-08-05 22:58:35 +00:00
|
|
|
self.grant_permissions(user, context)
|
2015-05-11 14:05:39 +00:00
|
|
|
elif user.set_perm_action == 'revoke':
|
2015-08-05 22:58:35 +00:00
|
|
|
self.revoke_permissions(user, context)
|
2015-05-08 14:05:57 +00:00
|
|
|
else:
|
2015-05-11 14:05:39 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2016-02-09 12:17:42 +00:00
|
|
|
def create_link(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
context.update({
|
|
|
|
'link_target': user.create_link_target,
|
|
|
|
'link_name': user.create_link_name,
|
|
|
|
})
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Create link
|
2016-06-17 10:00:04 +00:00
|
|
|
su - %(user)s --shell /bin/bash << 'EOF' || exit_code=1
|
2016-05-05 11:58:35 +00:00
|
|
|
if [[ ! -e '%(link_name)s' ]]; then
|
|
|
|
ln -s '%(link_target)s' '%(link_name)s'
|
2016-02-09 12:17:42 +00:00
|
|
|
else
|
|
|
|
echo "%(link_name)s already exists, doing nothing." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
EOF""") % context
|
|
|
|
)
|
|
|
|
|
2016-04-15 09:56:10 +00:00
|
|
|
def validate_paths_exist(self, user):
|
|
|
|
for path in user.paths_to_validate:
|
|
|
|
context = {
|
|
|
|
'path': path,
|
|
|
|
}
|
|
|
|
self.append(textwrap.dedent("""
|
|
|
|
if [[ ! -e '%(path)s' ]]; then
|
|
|
|
echo "%(path)s path does not exists." >&2
|
|
|
|
fi""") % context
|
|
|
|
)
|
2014-11-14 16:52:54 +00:00
|
|
|
|
2014-10-01 16:42:40 +00:00
|
|
|
def get_groups(self, user):
|
|
|
|
if user.is_main:
|
2014-10-02 15:58:27 +00:00
|
|
|
return user.account.systemusers.exclude(username=user.username).values_list('username', flat=True)
|
2014-10-28 09:51:27 +00:00
|
|
|
return list(user.groups.values_list('username', flat=True))
|
2014-10-01 16:42:40 +00:00
|
|
|
|
|
|
|
def get_context(self, user):
|
|
|
|
context = {
|
2014-11-18 17:47:26 +00:00
|
|
|
'object_id': user.pk,
|
2015-03-10 21:51:10 +00:00
|
|
|
'user': user.username,
|
|
|
|
'group': user.username,
|
2016-05-05 11:58:35 +00:00
|
|
|
'groups': ','.join(self.get_groups(user)),
|
2014-10-01 16:42:40 +00:00
|
|
|
'password': user.password if user.active else '*%s' % user.password,
|
|
|
|
'shell': user.shell,
|
2015-03-10 21:51:10 +00:00
|
|
|
'mainuser': user.username if user.is_main else user.account.username,
|
2015-04-05 22:34:47 +00:00
|
|
|
'home': user.get_home(),
|
2015-04-07 15:14:49 +00:00
|
|
|
'base_home': user.get_base_home(),
|
2015-05-12 12:38:40 +00:00
|
|
|
'mainuser_home': user.main.get_home(),
|
2014-10-01 16:42:40 +00:00
|
|
|
}
|
2015-04-09 14:32:10 +00:00
|
|
|
context['deleted_home'] = settings.SYSTEMUSERS_MOVE_ON_DELETE_PATH % context
|
2015-04-05 18:02:36 +00:00
|
|
|
return replace(context, "'", '"')
|
2014-10-01 16:42:40 +00:00
|
|
|
|
|
|
|
|
2015-04-05 18:02:36 +00:00
|
|
|
class UNIXUserDisk(ServiceMonitor):
|
2015-04-23 19:46:23 +00:00
|
|
|
"""
|
2015-04-24 11:39:20 +00:00
|
|
|
<tt>du -bs <home></tt>
|
2015-04-23 19:46:23 +00:00
|
|
|
"""
|
2014-10-01 16:42:40 +00:00
|
|
|
model = 'systemusers.SystemUser'
|
|
|
|
resource = ServiceMonitor.DISK
|
2015-04-05 18:02:36 +00:00
|
|
|
verbose_name = _('UNIX user disk')
|
2015-08-04 09:47:39 +00:00
|
|
|
delete_old_equal_values = True
|
2014-11-18 17:47:26 +00:00
|
|
|
|
|
|
|
def prepare(self):
|
2015-04-05 18:02:36 +00:00
|
|
|
super(UNIXUserDisk, self).prepare()
|
2014-11-18 17:47:26 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
function monitor () {
|
2016-05-18 14:08:12 +00:00
|
|
|
{ SIZE=$(du -bs "$1") && echo $SIZE || echo 0; } | awk {'print $1'}
|
2014-11-18 17:47:26 +00:00
|
|
|
}"""
|
|
|
|
))
|
2014-10-01 16:42:40 +00:00
|
|
|
|
|
|
|
def monitor(self, user):
|
|
|
|
context = self.get_context(user)
|
2015-04-05 22:34:47 +00:00
|
|
|
self.append("echo %(object_id)s $(monitor %(base_home)s)" % context)
|
2014-10-01 16:42:40 +00:00
|
|
|
|
|
|
|
def get_context(self, user):
|
2015-03-20 15:13:08 +00:00
|
|
|
context = {
|
2014-10-01 16:42:40 +00:00
|
|
|
'object_id': user.pk,
|
2015-04-05 22:34:47 +00:00
|
|
|
'base_home': user.get_base_home(),
|
2014-10-01 16:42:40 +00:00
|
|
|
}
|
2015-04-05 18:02:36 +00:00
|
|
|
return replace(context, "'", '"')
|
2015-03-20 15:13:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Exim4Traffic(ServiceMonitor):
|
2015-04-23 19:46:23 +00:00
|
|
|
"""
|
2015-04-24 11:39:20 +00:00
|
|
|
Exim4 mainlog parser for mails sent on the webserver by system users (e.g. via PHP <tt>mail()</tt>)
|
2015-04-23 19:46:23 +00:00
|
|
|
"""
|
2015-03-20 15:13:08 +00:00
|
|
|
model = 'systemusers.SystemUser'
|
|
|
|
resource = ServiceMonitor.TRAFFIC
|
2015-04-05 18:02:36 +00:00
|
|
|
verbose_name = _("Exim4 traffic")
|
2015-03-20 15:13:08 +00:00
|
|
|
script_executable = '/usr/bin/python'
|
2015-08-04 09:47:39 +00:00
|
|
|
monthly_sum_old_values = True
|
2015-04-24 11:39:20 +00:00
|
|
|
doc_settings = (settings,
|
|
|
|
('SYSTEMUSERS_MAIL_LOG_PATH',)
|
|
|
|
)
|
2015-03-20 15:13:08 +00:00
|
|
|
|
|
|
|
def prepare(self):
|
2015-04-07 15:14:49 +00:00
|
|
|
mainlog = settings.SYSTEMUSERS_MAIL_LOG_PATH
|
2015-03-20 15:13:08 +00:00
|
|
|
context = {
|
|
|
|
'current_date': self.current_date.strftime("%Y-%m-%d %H:%M:%S %Z"),
|
|
|
|
'mainlogs': str((mainlog, mainlog+'.1')),
|
|
|
|
}
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
|
|
|
from dateutil import tz
|
|
|
|
|
|
|
|
def to_local_timezone(date, tzlocal=tz.tzlocal()):
|
|
|
|
date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S %Z')
|
|
|
|
date = date.replace(tzinfo=tz.tzutc())
|
|
|
|
date = date.astimezone(tzlocal)
|
|
|
|
return date
|
|
|
|
|
|
|
|
mainlogs = {mainlogs}
|
|
|
|
# Use local timezone
|
|
|
|
end_date = to_local_timezone('{current_date}')
|
|
|
|
end_date = int(end_date.strftime('%Y%m%d%H%M%S'))
|
|
|
|
users = {{}}
|
|
|
|
|
|
|
|
def prepare(object_id, username, ini_date):
|
|
|
|
global users
|
|
|
|
ini_date = to_local_timezone(ini_date)
|
|
|
|
ini_date = int(ini_date.strftime('%Y%m%d%H%M%S'))
|
|
|
|
users[username] = [ini_date, object_id, 0]
|
|
|
|
|
|
|
|
def monitor(users, end_date, mainlogs):
|
|
|
|
user_regex = re.compile(r' U=([^ ]+) ')
|
|
|
|
for mainlog in mainlogs:
|
|
|
|
try:
|
|
|
|
with open(mainlog, 'r') as mainlog:
|
|
|
|
for line in mainlog.readlines():
|
|
|
|
if ' <= ' in line and 'P=local' in line:
|
2015-03-29 16:10:07 +00:00
|
|
|
username = user_regex.search(line).groups()[0]
|
2015-03-20 15:13:08 +00:00
|
|
|
try:
|
|
|
|
sender = users[username]
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
date, time, id, __, __, user, protocol, size = line.split()[:8]
|
|
|
|
date = date.replace('-', '')
|
|
|
|
date += time.replace(':', '')
|
|
|
|
if sender[0] < int(date) < end_date:
|
|
|
|
sender[2] += int(size[2:])
|
|
|
|
except IOError as e:
|
2016-11-14 09:24:41 +00:00
|
|
|
sys.stderr.write(str(e))
|
2015-03-20 15:13:08 +00:00
|
|
|
|
2015-04-16 13:15:21 +00:00
|
|
|
for username, opts in users.iteritems():
|
2015-03-20 15:13:08 +00:00
|
|
|
__, object_id, size = opts
|
|
|
|
print object_id, size
|
|
|
|
""").format(**context)
|
|
|
|
)
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
self.append('monitor(users, end_date, mainlogs)')
|
|
|
|
|
|
|
|
def monitor(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
self.append("prepare(%(object_id)s, '%(username)s', '%(last_date)s')" % context)
|
|
|
|
|
|
|
|
def get_context(self, user):
|
2015-04-05 18:02:36 +00:00
|
|
|
context = {
|
2015-03-20 15:13:08 +00:00
|
|
|
'username': user.username,
|
|
|
|
'object_id': user.pk,
|
|
|
|
'last_date': self.get_last_date(user.pk).strftime("%Y-%m-%d %H:%M:%S %Z"),
|
|
|
|
}
|
2015-05-21 13:34:12 +00:00
|
|
|
return context
|
2015-03-20 15:13:08 +00:00
|
|
|
|
|
|
|
|
2015-04-05 18:02:36 +00:00
|
|
|
class VsFTPdTraffic(ServiceMonitor):
|
2015-04-23 19:46:23 +00:00
|
|
|
"""
|
|
|
|
vsFTPd log parser.
|
|
|
|
"""
|
2015-03-20 15:13:08 +00:00
|
|
|
model = 'systemusers.SystemUser'
|
|
|
|
resource = ServiceMonitor.TRAFFIC
|
2015-04-05 18:02:36 +00:00
|
|
|
verbose_name = _('VsFTPd traffic')
|
2015-03-20 15:13:08 +00:00
|
|
|
script_executable = '/usr/bin/python'
|
2015-08-04 09:47:39 +00:00
|
|
|
monthly_sum_old_values = True
|
2015-04-24 11:39:20 +00:00
|
|
|
doc_settings = (settings,
|
|
|
|
('SYSTEMUSERS_FTP_LOG_PATH',)
|
|
|
|
)
|
2015-03-20 15:13:08 +00:00
|
|
|
|
|
|
|
def prepare(self):
|
|
|
|
vsftplog = settings.SYSTEMUSERS_FTP_LOG_PATH
|
|
|
|
context = {
|
|
|
|
'current_date': self.current_date.strftime("%Y-%m-%d %H:%M:%S %Z"),
|
|
|
|
'vsftplogs': str((vsftplog, vsftplog+'.1')),
|
|
|
|
}
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
|
|
|
from dateutil import tz
|
2015-04-05 18:02:36 +00:00
|
|
|
|
2015-03-20 15:13:08 +00:00
|
|
|
def to_local_timezone(date, tzlocal=tz.tzlocal()):
|
|
|
|
date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S %Z')
|
|
|
|
date = date.replace(tzinfo=tz.tzutc())
|
|
|
|
date = date.astimezone(tzlocal)
|
|
|
|
return date
|
2015-04-05 18:02:36 +00:00
|
|
|
|
2015-03-20 15:13:08 +00:00
|
|
|
vsftplogs = {vsftplogs}
|
|
|
|
# Use local timezone
|
|
|
|
end_date = to_local_timezone('{current_date}')
|
|
|
|
end_date = int(end_date.strftime('%Y%m%d%H%M%S'))
|
|
|
|
users = {{}}
|
2015-04-29 10:51:30 +00:00
|
|
|
months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
|
|
|
|
months = dict((m, '%02d' % n) for n, m in enumerate(months, 1))
|
2015-04-05 18:02:36 +00:00
|
|
|
|
2015-03-20 15:13:08 +00:00
|
|
|
def prepare(object_id, username, ini_date):
|
|
|
|
global users
|
|
|
|
ini_date = to_local_timezone(ini_date)
|
|
|
|
ini_date = int(ini_date.strftime('%Y%m%d%H%M%S'))
|
|
|
|
users[username] = [ini_date, object_id, 0]
|
2015-04-05 18:02:36 +00:00
|
|
|
|
2015-03-20 15:13:08 +00:00
|
|
|
def monitor(users, end_date, months, vsftplogs):
|
2015-03-29 16:10:07 +00:00
|
|
|
user_regex = re.compile(r'\] \[([^ ]+)\] (OK|FAIL) ')
|
2015-03-20 15:13:08 +00:00
|
|
|
bytes_regex = re.compile(r', ([0-9]+) bytes, ')
|
|
|
|
for vsftplog in vsftplogs:
|
|
|
|
try:
|
|
|
|
with open(vsftplog, 'r') as vsftplog:
|
|
|
|
for line in vsftplog.readlines():
|
|
|
|
if ' bytes, ' in line:
|
|
|
|
username = user_regex.search(line).groups()[0]
|
|
|
|
try:
|
|
|
|
user = users[username]
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
__, month, day, time, year = line.split()[:5]
|
|
|
|
date = year + months[month] + day + time.replace(':', '')
|
|
|
|
if user[0] < int(date) < end_date:
|
|
|
|
bytes = bytes_regex.search(line).groups()[0]
|
|
|
|
user[2] += int(bytes)
|
|
|
|
except IOError as e:
|
2016-11-14 09:24:41 +00:00
|
|
|
sys.stderr.write(str(e))
|
2015-03-20 15:13:08 +00:00
|
|
|
|
2015-04-02 16:14:55 +00:00
|
|
|
for username, opts in users.items():
|
2015-03-20 15:13:08 +00:00
|
|
|
__, object_id, size = opts
|
|
|
|
print object_id, size
|
|
|
|
""").format(**context)
|
|
|
|
)
|
|
|
|
|
|
|
|
def monitor(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
self.append("prepare(%(object_id)s, '%(username)s', '%(last_date)s')" % context)
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
self.append('monitor(users, end_date, months, vsftplogs)')
|
|
|
|
|
|
|
|
def get_context(self, user):
|
2015-04-05 18:02:36 +00:00
|
|
|
context = {
|
2015-03-20 15:13:08 +00:00
|
|
|
'last_date': self.get_last_date(user.pk).strftime("%Y-%m-%d %H:%M:%S %Z"),
|
|
|
|
'object_id': user.pk,
|
|
|
|
'username': user.username,
|
|
|
|
}
|
2015-04-05 18:02:36 +00:00
|
|
|
return replace(context, "'", '"')
|
2023-07-10 18:16:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
class UNIXUserControllerNewServers(ServiceController):
|
|
|
|
"""
|
|
|
|
Basic UNIX system user/group support based on <tt>useradd</tt>, <tt>usermod</tt>, <tt>userdel</tt> and <tt>groupdel</tt>.
|
|
|
|
Autodetects and uses ACL if available, for better permission management.
|
|
|
|
"""
|
|
|
|
verbose_name = _("UNIX user new servers")
|
|
|
|
model = 'systemusers.SystemUser'
|
2023-07-11 19:37:00 +00:00
|
|
|
# actions = ('save', 'delete', 'set_permission', 'validate_paths_exist', 'create_link')
|
|
|
|
actions = ('save', 'delete', 'set_permission', 'create_link')
|
2023-07-10 18:16:52 +00:00
|
|
|
doc_settings = (settings, (
|
|
|
|
'SYSTEMUSERS_DEFAULT_GROUP_MEMBERS',
|
|
|
|
'SYSTEMUSERS_MOVE_ON_DELETE_PATH',
|
|
|
|
'SYSTEMUSERS_FORBIDDEN_PATHS'
|
|
|
|
))
|
|
|
|
|
|
|
|
def save(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
if not context['user']:
|
|
|
|
return
|
2023-07-11 19:37:00 +00:00
|
|
|
|
2023-07-10 18:16:52 +00:00
|
|
|
if not user.active:
|
|
|
|
self.append(textwrap.dedent("""
|
|
|
|
#Just disable that user, if it exists
|
|
|
|
if id %(user)s ; then
|
|
|
|
usermod %(user)s --password '%(password)s'
|
|
|
|
fi
|
|
|
|
""") % context)
|
|
|
|
return
|
|
|
|
if user.is_main:
|
|
|
|
# TODO userd add will fail if %(user)s group already exists
|
|
|
|
self.append(textwrap.dedent("""
|
|
|
|
# Update/create user state for %(user)s
|
2023-07-11 19:37:00 +00:00
|
|
|
if id %(user)s &> /dev/null; then
|
2023-07-10 18:16:52 +00:00
|
|
|
usermod %(user)s --home '%(home)s/%(user)s' \\
|
|
|
|
--password '%(password)s' \\
|
|
|
|
--shell '%(shell)s' \\
|
|
|
|
--groups '%(groups)s'
|
|
|
|
else
|
|
|
|
useradd_code=0
|
|
|
|
useradd %(user)s --home '%(home)s/%(user)s' \\
|
|
|
|
--password '%(password)s' \\
|
|
|
|
--shell '%(shell)s' \\
|
|
|
|
--groups '%(groups)s' || useradd_code=$?
|
|
|
|
if [[ $useradd_code -eq 8 ]]; then
|
|
|
|
# User is logged in, kill and retry
|
|
|
|
pkill -u %(user)s; sleep 2
|
|
|
|
pkill -9 -u %(user)s; sleep 1
|
|
|
|
useradd %(user)s --home '%(home)s/%(user)s' \\
|
|
|
|
--password '%(password)s' \\
|
|
|
|
--shell '%(shell)s' \\
|
|
|
|
--groups '%(groups)s'
|
|
|
|
elif [[ $useradd_code -ne 0 ]]; then
|
|
|
|
exit $useradd_code
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
mkdir -p '%(base_home)s/%(user)s'
|
|
|
|
chown root:%(user)s %(base_home)s
|
|
|
|
chmod 710 '%(base_home)s'
|
|
|
|
setfacl -m 'u:%(user)s:rx' %(base_home)s
|
|
|
|
|
|
|
|
chown %(user)s:%(user)s '%(base_home)s/%(user)s'
|
|
|
|
chmod 700 '%(base_home)s/%(user)s'
|
|
|
|
""") % context
|
|
|
|
)
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
ls -A /etc/skel/ | while read line; do
|
|
|
|
if [[ ! -e "%(home)s/${line}" ]]; then
|
|
|
|
cp -a "/etc/skel/${line}" "%(base_home)s/%(user)s/${line}" && \\
|
|
|
|
chown -R %(user)s:%(user)s "%(base_home)s/%(user)s/${line}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
""") % context
|
|
|
|
)
|
2023-07-24 15:39:18 +00:00
|
|
|
|
2023-07-21 16:26:42 +00:00
|
|
|
for member in settings.SYSTEMUSERS_DEFAULT_GROUP_MEMBERS:
|
|
|
|
context['member'] = member
|
|
|
|
self.append('usermod -a -G %(user)s %(member)s || exit_code=$?' % context)
|
|
|
|
|
2023-07-10 18:16:52 +00:00
|
|
|
|
|
|
|
def delete(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
if not context['user']:
|
|
|
|
return
|
2023-07-21 16:26:42 +00:00
|
|
|
if user.is_main:
|
|
|
|
self.append(textwrap.dedent("""\
|
2023-07-11 19:37:00 +00:00
|
|
|
# Delete %(user)s user
|
2023-07-21 16:26:42 +00:00
|
|
|
nohup bash -c 'sleep 2 && killall -u %(user)s -s KILL' &> /dev/null &
|
|
|
|
killall -u %(user)s || true
|
2023-07-11 19:37:00 +00:00
|
|
|
userdel %(user)s || exit_code=$?
|
|
|
|
groupdel %(group)s || exit_code=$?
|
2023-07-21 16:26:42 +00:00
|
|
|
""") % context
|
|
|
|
)
|
|
|
|
if context['deleted_home']:
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Move home into SYSTEMUSERS_MOVE_ON_DELETE_PATH, nesting if exists.
|
|
|
|
deleted_home="%(deleted_home)s"
|
|
|
|
while [[ -e "$deleted_home" ]]; do
|
|
|
|
deleted_home="${deleted_home}/$(basename ${deleted_home})"
|
|
|
|
done
|
|
|
|
mv '%(base_home)s' "$deleted_home" || exit_code=$?
|
|
|
|
""") % context
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.append("rm -fr -- '%(base_home)s'" % context)
|
2023-07-10 18:16:52 +00:00
|
|
|
|
2023-07-21 16:26:42 +00:00
|
|
|
# TODO: comprovar funciones que no se suelen utilizar
|
2023-07-10 18:16:52 +00:00
|
|
|
def grant_permissions(self, user, context):
|
|
|
|
context['perms'] = user.set_perm_perms
|
|
|
|
# Capital X adds execution permissions for directories, not files
|
|
|
|
context['perms_X'] = context['perms'] + 'X'
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Grant execution permissions to every parent directory
|
|
|
|
for access_path in %(access_paths)s; do
|
|
|
|
# Preserve existing ACLs
|
|
|
|
acl=$(getfacl -a "$access_path" | grep '^user:%(user)s:') && {
|
|
|
|
perms=$(echo "$acl" | cut -d':' -f3)
|
|
|
|
perms=$(echo "$perms" | cut -c 1,2)x
|
|
|
|
setfacl -m u:%(user)s:$perms "$access_path"
|
|
|
|
} || setfacl -m u:%(user)s:--x "$access_path"
|
|
|
|
done
|
|
|
|
# Grant perms to existing files, excluding execution
|
|
|
|
find '%(perm_to)s' -type f %(exclude_acl)s \\
|
|
|
|
-exec setfacl -m u:%(user)s:%(perms)s {} \\;
|
|
|
|
# Grant perms to extisting directories and set defaults for future content
|
|
|
|
find '%(perm_to)s' -type d %(exclude_acl)s \\
|
|
|
|
-exec setfacl -m u:%(user)s:%(perms_X)s -m d:u:%(user)s:%(perms_X)s {} \\;
|
|
|
|
# Account group as the owner of new files
|
|
|
|
chmod g+s '%(perm_to)s'""") % context
|
|
|
|
)
|
|
|
|
if not user.is_main:
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Grant access to main user
|
|
|
|
find '%(perm_to)s' -type d %(exclude_acl)s \\
|
|
|
|
-exec setfacl -m d:u:%(mainuser)s:rwx {} \\;\
|
|
|
|
""") % context
|
|
|
|
)
|
|
|
|
|
|
|
|
def revoke_permissions(self, user, context):
|
|
|
|
revoke_perms = {
|
|
|
|
'rw': '',
|
|
|
|
'r': 'w',
|
|
|
|
'w': 'r',
|
|
|
|
}
|
|
|
|
context.update({
|
|
|
|
'perms': revoke_perms[user.set_perm_perms],
|
|
|
|
'option': '-x' if user.set_perm_perms == 'rw' else '-m'
|
|
|
|
})
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Revoke permissions
|
|
|
|
find '%(perm_to)s' %(exclude_acl)s \\
|
|
|
|
-exec setfacl %(option)s u:%(user)s:%(perms)s {} \\;\
|
|
|
|
""") % context
|
|
|
|
)
|
|
|
|
|
|
|
|
def set_permission(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
context.update({
|
|
|
|
'perm_action': user.set_perm_action,
|
|
|
|
'perm_to': os.path.join(user.set_perm_base_home, user.set_perm_home_extension),
|
|
|
|
})
|
|
|
|
exclude_acl = []
|
|
|
|
for exclude in settings.SYSTEMUSERS_FORBIDDEN_PATHS:
|
|
|
|
context['exclude_acl'] = os.path.join(user.set_perm_base_home, exclude)
|
|
|
|
exclude_acl.append('-not -path "%(exclude_acl)s"' % context)
|
|
|
|
context['exclude_acl'] = ' \\\n -a '.join(exclude_acl) if exclude_acl else ''
|
|
|
|
# Access paths
|
|
|
|
head = user.set_perm_base_home
|
|
|
|
relative = ''
|
|
|
|
access_paths = ["'%s'" % head]
|
|
|
|
for tail in user.set_perm_home_extension.split(os.sep)[:-1]:
|
|
|
|
relative = os.path.join(relative, tail)
|
|
|
|
for exclude in settings.SYSTEMUSERS_FORBIDDEN_PATHS:
|
|
|
|
if fnmatch.fnmatch(relative, exclude):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# No match
|
|
|
|
head = os.path.join(head, tail)
|
|
|
|
access_paths.append("'%s'" % head)
|
|
|
|
context['access_paths'] = ' '.join(access_paths)
|
|
|
|
|
|
|
|
if user.set_perm_action == 'grant':
|
|
|
|
self.grant_permissions(user, context)
|
|
|
|
elif user.set_perm_action == 'revoke':
|
|
|
|
self.revoke_permissions(user, context)
|
|
|
|
else:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def create_link(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
context.update({
|
|
|
|
'link_target': user.create_link_target,
|
|
|
|
'link_name': user.create_link_name,
|
|
|
|
})
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Create link
|
|
|
|
su - %(user)s --shell /bin/bash << 'EOF' || exit_code=1
|
|
|
|
if [[ ! -e '%(link_name)s' ]]; then
|
|
|
|
ln -s '%(link_target)s' '%(link_name)s'
|
|
|
|
else
|
|
|
|
echo "%(link_name)s already exists, doing nothing." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
EOF""") % context
|
|
|
|
)
|
|
|
|
|
|
|
|
def validate_paths_exist(self, user):
|
|
|
|
for path in user.paths_to_validate:
|
|
|
|
context = {
|
|
|
|
'path': path,
|
|
|
|
}
|
|
|
|
self.append(textwrap.dedent("""
|
|
|
|
if [[ ! -e '%(path)s' ]]; then
|
|
|
|
echo "%(path)s path does not exists." >&2
|
|
|
|
fi""") % context
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_groups(self, user):
|
2023-07-21 16:26:42 +00:00
|
|
|
groups = []
|
2023-07-10 18:16:52 +00:00
|
|
|
if user.is_main:
|
|
|
|
groups.append("main-systemusers")
|
|
|
|
return groups
|
|
|
|
|
|
|
|
def get_context(self, user):
|
|
|
|
context = {
|
|
|
|
'object_id': user.pk,
|
|
|
|
'user': user.username,
|
|
|
|
'group': user.username,
|
|
|
|
'groups': ','.join(self.get_groups(user)),
|
|
|
|
'password': user.password if user.active else '*%s' % user.password,
|
|
|
|
'shell': user.shell,
|
|
|
|
'mainuser': user.username if user.is_main else user.account.username,
|
|
|
|
'home': user.get_home(),
|
|
|
|
'base_home': user.get_base_home(),
|
|
|
|
'mainuser_home': user.main.get_home(),
|
|
|
|
}
|
2023-07-24 15:39:18 +00:00
|
|
|
context['deleted_home'] = settings.SYSTEMUSERS_MOVE_ON_DELETE_PATH % context
|
|
|
|
return replace(context, "'", '"')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebappUserController(ServiceController):
|
|
|
|
"""
|
|
|
|
Basic UNIX system user/group support based on <tt>useradd</tt>, <tt>usermod</tt>, <tt>userdel</tt> and <tt>groupdel</tt>.
|
|
|
|
Autodetects and uses ACL if available, for better permission management.
|
|
|
|
"""
|
|
|
|
verbose_name = _("SFTP Webapp user")
|
|
|
|
model = 'systemusers.WebappUsers'
|
|
|
|
actions = ('save', 'delete',)
|
|
|
|
doc_settings = (settings, (
|
|
|
|
'SYSTEMUSERS_DEFAULT_GROUP_MEMBERS',
|
|
|
|
'SYSTEMUSERS_MOVE_ON_DELETE_PATH',
|
|
|
|
'SYSTEMUSERS_FORBIDDEN_PATHS'
|
|
|
|
))
|
|
|
|
|
|
|
|
def save(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
if not context['user']:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.append(textwrap.dedent("""
|
|
|
|
# Update/create user state for %(user)s
|
|
|
|
if id %(user)s &> /dev/null; then
|
|
|
|
usermod %(user)s --home '/%(home)s' \\
|
|
|
|
--password '%(password)s' \\
|
|
|
|
--shell '%(shell)s' \\
|
|
|
|
--groups '%(groups)s'
|
|
|
|
else
|
|
|
|
useradd_code=0
|
|
|
|
useradd %(user)s --home '/%(home)s' \\
|
|
|
|
--password '%(password)s' \\
|
|
|
|
--shell '%(shell)s' \\
|
|
|
|
--groups '%(groups)s' || useradd_code=$?
|
|
|
|
if [[ $useradd_code -eq 8 ]]; then
|
|
|
|
# User is logged in, kill and retry
|
|
|
|
pkill -u %(user)s; sleep 2
|
|
|
|
pkill -9 -u %(user)s; sleep 1
|
|
|
|
useradd %(user)s --home '/%(home)s' \\
|
|
|
|
--password '%(password)s' \\
|
|
|
|
--shell '%(shell)s' \\
|
|
|
|
--groups '%(groups)s'
|
|
|
|
elif [[ $useradd_code -ne 0 ]]; then
|
|
|
|
exit $useradd_code
|
|
|
|
fi
|
2023-07-25 20:16:38 +00:00
|
|
|
usermod -aG %(user)s www-data
|
2023-07-24 15:39:18 +00:00
|
|
|
fi
|
|
|
|
usermod -aG %(user)s %(parent)s
|
|
|
|
|
|
|
|
# Ensure homedir exists and has correct perms
|
|
|
|
mkdir -p '%(webapp_path)s' || exit_code=1
|
|
|
|
chown %(user)s:%(user)s %(webapp_path)s || exit_code=1
|
|
|
|
chmod 750 '%(webapp_path)s' || exit_code=1
|
|
|
|
|
|
|
|
# Create /chroots/$uid symlink into /home/$user.parent/webapps/
|
|
|
|
uid=$(id -u "%(user)s")
|
|
|
|
ln -n -f -s %(base_home)s/webapps /chroots/$uid || exit_code=1
|
|
|
|
""") % context
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def delete(self, user):
|
|
|
|
context = self.get_context(user)
|
|
|
|
if not context['user']:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Delete %(user)s user
|
|
|
|
uid=$(id -u "%(user)s")
|
|
|
|
|
|
|
|
nohup bash -c 'sleep 2 && killall -u %(user)s -s KILL' &> /dev/null &
|
|
|
|
killall -u %(user)s || true
|
|
|
|
userdel %(user)s || exit_code=$?
|
|
|
|
groupdel %(group)s || exit_code=$?
|
|
|
|
|
|
|
|
# Delete /chroots/$uid symlink into /home/$user.parent/webapps/
|
|
|
|
rm /chroots/$uid
|
|
|
|
""") % context
|
|
|
|
)
|
|
|
|
if context['deleted_home']:
|
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
# Move home into SYSTEMUSERS_MOVE_ON_DELETE_PATH, nesting if exists.
|
|
|
|
mv '%(webapp_path)s' '%(deleted_home)s' || exit_code=$?
|
|
|
|
""") % context
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.append("rm -fr -- '%(webapp_path)s'" % context)
|
|
|
|
|
|
|
|
|
|
|
|
def get_groups(self, user):
|
|
|
|
groups = []
|
2023-09-05 15:40:58 +00:00
|
|
|
# groups = list(user.account.systemusers.exclude(username=user.username).values_list('username', flat=True))
|
|
|
|
groups.append(user.account.main_systemuser.username)
|
2023-07-24 15:39:18 +00:00
|
|
|
groups.append("webapp-systemusers")
|
|
|
|
return groups
|
|
|
|
|
|
|
|
def get_context(self, user):
|
|
|
|
context = {
|
|
|
|
'object_id': user.pk,
|
|
|
|
'user': user.username,
|
|
|
|
'group': user.username,
|
|
|
|
'groups': ','.join(self.get_groups(user)),
|
|
|
|
'password': user.password, #if user.active else '*%s' % user.password,
|
|
|
|
'shell': user.shell,
|
|
|
|
'home': user.home,
|
|
|
|
'base_home': user.get_base_home(),
|
|
|
|
'webapp_path': os.path.normpath(user.get_base_home() + "/webapps/" + user.home),
|
|
|
|
'parent': user.get_parent(),
|
|
|
|
}
|
2023-07-25 20:16:38 +00:00
|
|
|
context['deleted_home'] = context['webapp_path'] + ".deleted"
|
2023-07-10 18:16:52 +00:00
|
|
|
return replace(context, "'", '"')
|