2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
This module defines the database models of operating system users.
|
|
|
|
|
|
|
|
"""
|
2014-12-22 20:07:11 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-01-31 21:48:07 +01:00
|
|
|
import base64
|
2014-05-25 00:55:02 +02:00
|
|
|
from datetime import date
|
2014-12-26 15:10:36 +01:00
|
|
|
import logging
|
2014-05-25 00:55:02 +02:00
|
|
|
import os
|
2015-01-31 21:48:07 +01:00
|
|
|
import six
|
2014-05-25 00:55:02 +02:00
|
|
|
|
2014-12-17 22:19:44 +01:00
|
|
|
from django.db import models, transaction
|
2014-05-25 00:55:02 +02:00
|
|
|
from django.conf import settings
|
2014-05-24 21:53:49 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2014-05-25 00:55:02 +02:00
|
|
|
from django.utils import timezone
|
2014-05-24 23:40:54 +02:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2014-05-24 21:53:49 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2014-05-24 21:28:33 +02:00
|
|
|
|
|
|
|
from model_utils.models import TimeStampedModel
|
|
|
|
|
2014-05-25 00:55:02 +02:00
|
|
|
from passlib.hash import sha512_crypt
|
|
|
|
from passlib.utils import generate_password
|
|
|
|
|
2014-12-29 15:57:03 +01:00
|
|
|
from taskresults.models import TaskResult
|
|
|
|
|
2015-01-19 21:44:57 +01:00
|
|
|
from ldaptasks.tasks import (
|
2014-05-30 17:10:22 +02:00
|
|
|
add_ldap_user_to_group,
|
2014-05-25 23:35:14 +02:00
|
|
|
create_ldap_group,
|
|
|
|
create_ldap_user,
|
2014-12-29 15:57:03 +01:00
|
|
|
delete_ldap_group,
|
2014-05-30 17:10:22 +02:00
|
|
|
delete_ldap_user,
|
|
|
|
remove_ldap_user_from_group,
|
2015-01-24 16:26:32 +01:00
|
|
|
set_ldap_user_password,
|
2015-01-19 21:44:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from fileservertasks.tasks import (
|
|
|
|
delete_file_mail_userdir,
|
|
|
|
delete_file_sftp_userdir,
|
2015-01-31 23:00:33 +01:00
|
|
|
set_file_ssh_authorized_keys,
|
2014-12-26 15:10:36 +01:00
|
|
|
setup_file_mail_userdir,
|
|
|
|
setup_file_sftp_userdir,
|
2014-05-25 23:35:14 +02:00
|
|
|
)
|
|
|
|
|
2014-05-25 00:55:02 +02:00
|
|
|
|
2014-12-27 22:44:27 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2014-05-30 21:46:10 +02:00
|
|
|
|
|
|
|
|
2014-12-26 15:10:36 +01:00
|
|
|
CANNOT_USE_PRIMARY_GROUP_AS_ADDITIONAL = _(
|
|
|
|
"You can not use a user's primary group.")
|
2014-05-30 21:46:10 +02:00
|
|
|
|
|
|
|
|
2014-05-25 00:55:02 +02:00
|
|
|
class GroupManager(models.Manager):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Manager class for :py:class:`osusers.models.Group`.
|
|
|
|
|
|
|
|
"""
|
2014-05-25 00:55:02 +02:00
|
|
|
|
|
|
|
def get_next_gid(self):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Get the next available group id.
|
|
|
|
|
|
|
|
:returns: group id
|
|
|
|
:rtype: int
|
|
|
|
|
|
|
|
"""
|
2014-05-25 00:55:02 +02:00
|
|
|
q = self.aggregate(models.Max('gid'))
|
2014-05-25 14:53:58 +02:00
|
|
|
if q['gid__max'] is None:
|
|
|
|
return settings.OSUSER_MINGID
|
2014-05-25 00:55:02 +02:00
|
|
|
return max(settings.OSUSER_MINGID, q['gid__max'] + 1)
|
|
|
|
|
2014-05-24 21:28:33 +02:00
|
|
|
|
2014-05-24 23:40:54 +02:00
|
|
|
@python_2_unicode_compatible
|
2014-05-24 21:28:33 +02:00
|
|
|
class Group(TimeStampedModel, models.Model):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
This entity class corresponds to an operating system group.
|
|
|
|
|
|
|
|
"""
|
2014-05-24 23:40:54 +02:00
|
|
|
groupname = models.CharField(
|
|
|
|
_('Group name'), max_length=16, unique=True)
|
|
|
|
gid = models.PositiveSmallIntegerField(
|
|
|
|
_('Group ID'), unique=True, primary_key=True)
|
|
|
|
descr = models.TextField(_('Description'), blank=True)
|
|
|
|
passwd = models.CharField(
|
|
|
|
_('Group password'), max_length=128, blank=True)
|
|
|
|
|
2014-05-25 00:55:02 +02:00
|
|
|
objects = GroupManager()
|
|
|
|
|
2014-05-24 23:40:54 +02:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Group')
|
|
|
|
verbose_name_plural = _('Groups')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{0} ({1})'.format(self.groupname, self.gid)
|
2014-05-24 21:28:33 +02:00
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
@transaction.atomic
|
2014-05-30 17:10:22 +02:00
|
|
|
def save(self, *args, **kwargs):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Save the group to the database and synchronizes group information to
|
|
|
|
LDAP.
|
|
|
|
|
|
|
|
:param args: positional arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.save`
|
|
|
|
:param kwargs: keyword arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.save`
|
|
|
|
:return: self
|
|
|
|
:rtype: :py:class:`osusers.models.Group`
|
|
|
|
|
|
|
|
"""
|
2014-05-30 17:10:22 +02:00
|
|
|
super(Group, self).save(*args, **kwargs)
|
2014-12-26 15:10:36 +01:00
|
|
|
dn = create_ldap_group.delay(
|
|
|
|
self.groupname, self.gid, self.descr).get()
|
2014-12-27 22:44:27 +01:00
|
|
|
_LOGGER.info("created LDAP group with dn %s", dn)
|
2014-05-30 17:10:22 +02:00
|
|
|
return self
|
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
@transaction.atomic
|
2014-05-30 17:10:22 +02:00
|
|
|
def delete(self, *args, **kwargs):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Delete the group from LDAP and the database.
|
|
|
|
|
|
|
|
:param args: positional arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.delete`
|
|
|
|
:param kwargs: keyword arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.delete`
|
|
|
|
|
|
|
|
"""
|
2014-12-29 15:57:03 +01:00
|
|
|
TaskResult.objects.create_task_result(
|
|
|
|
delete_ldap_group.delay(self.groupname),
|
|
|
|
'delete_ldap_group'
|
|
|
|
)
|
2014-05-30 17:10:22 +02:00
|
|
|
super(Group, self).delete(*args, **kwargs)
|
|
|
|
|
2014-05-24 21:28:33 +02:00
|
|
|
|
2014-05-25 00:55:02 +02:00
|
|
|
class UserManager(models.Manager):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Manager class for :py:class:`osusers.models.User`.
|
|
|
|
|
|
|
|
"""
|
2014-05-25 00:55:02 +02:00
|
|
|
|
|
|
|
def get_next_uid(self):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Get the next available user id.
|
|
|
|
|
|
|
|
:return: user id
|
|
|
|
:rtype: int
|
|
|
|
|
|
|
|
"""
|
2014-05-25 00:55:02 +02:00
|
|
|
q = self.aggregate(models.Max('uid'))
|
2014-05-25 14:53:58 +02:00
|
|
|
if q['uid__max'] is None:
|
|
|
|
return settings.OSUSER_MINUID
|
2014-05-25 00:55:02 +02:00
|
|
|
return max(settings.OSUSER_MINUID, q['uid__max'] + 1)
|
|
|
|
|
|
|
|
def get_next_username(self):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Get the next available user name.
|
|
|
|
|
|
|
|
:return: user name
|
|
|
|
:rtype: str
|
|
|
|
|
|
|
|
"""
|
2014-05-25 00:55:02 +02:00
|
|
|
count = 1
|
|
|
|
usernameformat = "{0}{1:02d}"
|
|
|
|
nextuser = usernameformat.format(settings.OSUSER_USERNAME_PREFIX,
|
|
|
|
count)
|
|
|
|
for user in self.values('username').filter(
|
2014-12-27 22:44:27 +01:00
|
|
|
username__startswith=settings.OSUSER_USERNAME_PREFIX
|
|
|
|
).order_by('username'):
|
2014-05-25 23:35:14 +02:00
|
|
|
if user['username'] == nextuser:
|
2014-05-25 00:55:02 +02:00
|
|
|
count += 1
|
|
|
|
nextuser = usernameformat.format(
|
|
|
|
settings.OSUSER_USERNAME_PREFIX, count)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
return nextuser
|
|
|
|
|
2014-12-17 22:19:44 +01:00
|
|
|
@transaction.atomic
|
2014-12-27 19:26:16 +01:00
|
|
|
def create_user(
|
|
|
|
self, customer, username=None, password=None, commit=False
|
|
|
|
):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Create a new user with a primary group named the same as the user and
|
|
|
|
an initial password.
|
|
|
|
|
|
|
|
If username is None the result of :py:meth:`get_next_username` is used.
|
|
|
|
If password is None a new password will be generated using passlib's
|
|
|
|
:py:func:`generate_password`.
|
|
|
|
|
|
|
|
:param customer: Django User instance this user is associated to
|
|
|
|
:param str username: the username or None
|
|
|
|
:param str password: the password or None
|
|
|
|
:param boolean commit: whether to commit the user data to the database
|
|
|
|
or not
|
|
|
|
:return: new user
|
|
|
|
:rtype: :py:class:`osusers.models.User`
|
|
|
|
|
|
|
|
"""
|
2014-05-25 00:55:02 +02:00
|
|
|
uid = self.get_next_uid()
|
|
|
|
gid = Group.objects.get_next_gid()
|
|
|
|
if username is None:
|
|
|
|
username = self.get_next_username()
|
|
|
|
if password is None:
|
|
|
|
password = generate_password()
|
|
|
|
homedir = os.path.join(settings.OSUSER_HOME_BASEPATH, username)
|
|
|
|
group = Group.objects.create(groupname=username, gid=gid)
|
|
|
|
user = self.create(username=username, group=group, uid=uid,
|
2014-12-27 19:26:16 +01:00
|
|
|
homedir=homedir, customer=customer,
|
2014-05-25 00:55:02 +02:00
|
|
|
shell=settings.OSUSER_DEFAULT_SHELL)
|
2014-05-30 17:10:22 +02:00
|
|
|
user.set_password(password)
|
2014-05-25 23:35:14 +02:00
|
|
|
if commit:
|
|
|
|
user.save()
|
2014-05-25 00:55:02 +02:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
2014-05-24 23:40:54 +02:00
|
|
|
@python_2_unicode_compatible
|
2014-05-24 21:28:33 +02:00
|
|
|
class User(TimeStampedModel, models.Model):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
This entity class corresponds to an operating system user.
|
|
|
|
|
|
|
|
"""
|
2014-05-24 23:40:54 +02:00
|
|
|
username = models.CharField(
|
|
|
|
_('User name'), max_length=64, unique=True)
|
|
|
|
uid = models.PositiveSmallIntegerField(
|
|
|
|
_('User ID'), unique=True, primary_key=True)
|
|
|
|
group = models.ForeignKey(Group, verbose_name=_('Group'))
|
|
|
|
gecos = models.CharField(_('Gecos field'), max_length=128, blank=True)
|
|
|
|
homedir = models.CharField(_('Home directory'), max_length=256)
|
|
|
|
shell = models.CharField(_('Login shell'), max_length=64)
|
2014-12-27 19:26:16 +01:00
|
|
|
customer = models.ForeignKey(settings.AUTH_USER_MODEL)
|
2014-05-24 23:40:54 +02:00
|
|
|
|
2014-05-25 00:55:02 +02:00
|
|
|
objects = UserManager()
|
|
|
|
|
2014-05-24 23:40:54 +02:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('User')
|
|
|
|
verbose_name_plural = _('Users')
|
2014-05-24 21:28:33 +02:00
|
|
|
|
2014-05-24 23:40:54 +02:00
|
|
|
def __str__(self):
|
|
|
|
return '{0} ({1})'.format(self.username, self.uid)
|
2014-05-24 21:28:33 +02:00
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
@transaction.atomic
|
2014-05-30 17:10:22 +02:00
|
|
|
def set_password(self, password):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Set the password of the user.
|
|
|
|
|
|
|
|
The password is set to the user's
|
|
|
|
:py:class:`Shadow <osusers.models.Shadow>` instance and to LDAP.
|
|
|
|
|
|
|
|
:param str password: the new password
|
|
|
|
|
|
|
|
"""
|
2014-06-01 15:03:15 +02:00
|
|
|
if hasattr(self, 'shadow'):
|
|
|
|
self.shadow.set_password(password)
|
2015-01-24 16:26:32 +01:00
|
|
|
success = set_ldap_user_password.delay(
|
|
|
|
self.username, password).get()
|
|
|
|
if success:
|
|
|
|
_LOGGER.info(
|
|
|
|
"successfully set LDAP password for %s", self.username)
|
|
|
|
else:
|
|
|
|
_LOGGER.error(
|
|
|
|
"setting the LDAP password for %s failed", self.username)
|
|
|
|
return success
|
2014-06-01 15:03:15 +02:00
|
|
|
else:
|
|
|
|
self.shadow = Shadow.objects.create_shadow(
|
|
|
|
user=self, password=password
|
|
|
|
)
|
2015-01-24 16:26:32 +01:00
|
|
|
dn = create_ldap_user.delay(
|
|
|
|
self.username, self.uid, self.group.gid, self.gecos,
|
|
|
|
self.homedir, self.shell, password
|
|
|
|
).get()
|
|
|
|
_LOGGER.info("set LDAP password for %s", dn)
|
|
|
|
return True
|
|
|
|
|
2015-01-26 21:49:22 +01:00
|
|
|
def is_sftp_user(self):
|
|
|
|
return self.additionalgroup_set.filter(
|
|
|
|
group__groupname=settings.OSUSER_SFTP_GROUP
|
|
|
|
).exists()
|
2014-05-30 17:10:22 +02:00
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
@transaction.atomic
|
2014-05-30 17:10:22 +02:00
|
|
|
def save(self, *args, **kwargs):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Save the user to the database, create user directories and synchronize
|
|
|
|
user information to LDAP.
|
|
|
|
|
|
|
|
:param args: positional arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.save`
|
|
|
|
:param kwargs: keyword arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.save`
|
|
|
|
:return: self
|
|
|
|
:rtype: :py:class:`osusers.models.User`
|
|
|
|
|
|
|
|
"""
|
2014-12-26 15:10:36 +01:00
|
|
|
dn = create_ldap_user.delay(
|
|
|
|
self.username, self.uid, self.group.gid, self.gecos,
|
|
|
|
self.homedir, self.shell, password=None).get()
|
2014-12-29 15:57:03 +01:00
|
|
|
TaskResult.objects.create_task_result(
|
|
|
|
setup_file_sftp_userdir.delay(self.username),
|
|
|
|
'setup_file_sftp_userdir'
|
|
|
|
)
|
|
|
|
TaskResult.objects.create_task_result(
|
|
|
|
setup_file_mail_userdir.delay(self.username),
|
|
|
|
'setup_file_mail_userdir'
|
|
|
|
)
|
2014-12-27 22:44:27 +01:00
|
|
|
_LOGGER.info(
|
2014-12-29 15:57:03 +01:00
|
|
|
"created user %(user)s with LDAP dn %(dn)s, scheduled home "
|
|
|
|
"directory and mail base directory creation.", {
|
2014-12-26 15:10:36 +01:00
|
|
|
'user': self, 'dn': dn,
|
|
|
|
})
|
2014-05-30 17:10:22 +02:00
|
|
|
return super(User, self).save(*args, **kwargs)
|
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
@transaction.atomic
|
2014-05-30 17:10:22 +02:00
|
|
|
def delete(self, *args, **kwargs):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Delete the user and its groups from LDAP and the database and remove
|
|
|
|
the user's directories.
|
|
|
|
|
|
|
|
:param args: positional arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.delete`
|
|
|
|
:param kwargs: keyword arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.delete`
|
|
|
|
|
|
|
|
"""
|
2014-12-29 15:57:03 +01:00
|
|
|
TaskResult.objects.create_task_result(
|
|
|
|
delete_file_mail_userdir.delay(self.username),
|
|
|
|
'delete_file_mail_userdir'
|
|
|
|
)
|
|
|
|
TaskResult.objects.create_task_result(
|
|
|
|
delete_file_sftp_userdir.delay(self.username),
|
|
|
|
'delete_file_sftp_userdir'
|
|
|
|
)
|
2014-12-26 15:10:36 +01:00
|
|
|
for group in [ag.group for ag in self.additionalgroup_set.all()]:
|
|
|
|
remove_ldap_user_from_group.delay(
|
|
|
|
self.username, group.groupname).get()
|
|
|
|
delete_ldap_user.delay(self.username).get()
|
2014-05-30 17:10:22 +02:00
|
|
|
self.group.delete()
|
|
|
|
super(User, self).delete(*args, **kwargs)
|
|
|
|
|
2014-05-24 23:40:54 +02:00
|
|
|
|
2014-05-25 00:55:02 +02:00
|
|
|
class ShadowManager(models.Manager):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Manager class for :py:class:`osusers.models.Shadow`.
|
|
|
|
|
|
|
|
"""
|
2014-05-25 00:55:02 +02:00
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
@transaction.atomic
|
2014-05-25 00:55:02 +02:00
|
|
|
def create_shadow(self, user, password):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Create a new shadow instance with typical Linux settings for the given
|
|
|
|
user with the given password.
|
|
|
|
|
|
|
|
:param user: :py:class:`User <osusers.models.User>` instance
|
|
|
|
:param str password: the password
|
|
|
|
:return: new Shadow instance
|
|
|
|
:rtype: :py:class:`osusers.models.Shadow` instance
|
|
|
|
|
|
|
|
"""
|
2014-05-25 00:55:02 +02:00
|
|
|
changedays = (timezone.now().date() - date(1970, 1, 1)).days
|
|
|
|
shadow = self.create(
|
|
|
|
user=user, changedays=changedays,
|
|
|
|
minage=0, maxage=None, gracedays=7,
|
2014-06-01 14:51:33 +02:00
|
|
|
inactdays=30, expiredays=None
|
2014-05-25 00:55:02 +02:00
|
|
|
)
|
2014-06-01 14:51:33 +02:00
|
|
|
shadow.set_password(password)
|
2014-05-25 00:55:02 +02:00
|
|
|
shadow.save()
|
|
|
|
return shadow
|
|
|
|
|
|
|
|
|
2014-05-24 23:40:54 +02:00
|
|
|
@python_2_unicode_compatible
|
2014-05-24 21:28:33 +02:00
|
|
|
class Shadow(TimeStampedModel, models.Model):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
This entity class corresponds to an operating system user's shadow file
|
|
|
|
entry.
|
|
|
|
|
|
|
|
"""
|
2014-05-24 23:40:54 +02:00
|
|
|
user = models.OneToOneField(User, primary_key=True, verbose_name=_('User'))
|
|
|
|
passwd = models.CharField(_('Encrypted password'), max_length=128)
|
|
|
|
changedays = models.PositiveSmallIntegerField(
|
|
|
|
_('Date of last change'),
|
|
|
|
help_text=_('This is expressed in days since Jan 1, 1970'),
|
|
|
|
blank=True, null=True)
|
|
|
|
minage = models.PositiveSmallIntegerField(
|
|
|
|
_('Minimum age'),
|
|
|
|
help_text=_('Minimum number of days before the password can be'
|
|
|
|
' changed'),
|
|
|
|
blank=True, null=True)
|
|
|
|
maxage = models.PositiveSmallIntegerField(
|
|
|
|
_('Maximum age'),
|
|
|
|
help_text=_('Maximum number of days after which the password has to'
|
|
|
|
' be changed'),
|
|
|
|
blank=True, null=True)
|
|
|
|
gracedays = models.PositiveSmallIntegerField(
|
|
|
|
_('Grace period'),
|
|
|
|
help_text=_('The number of days before the password is going to'
|
|
|
|
' expire'),
|
|
|
|
blank=True, null=True)
|
|
|
|
inactdays = models.PositiveSmallIntegerField(
|
|
|
|
_('Inactivity period'),
|
|
|
|
help_text=_('The number of days after the password has expired during'
|
|
|
|
' which the password should still be accepted'),
|
|
|
|
blank=True, null=True)
|
|
|
|
expiredays = models.PositiveSmallIntegerField(
|
|
|
|
_('Account expiration date'),
|
|
|
|
help_text=_('The date of expiration of the account, expressed as'
|
|
|
|
' number of days since Jan 1, 1970'),
|
|
|
|
blank=True, null=True, default=None)
|
|
|
|
|
2014-05-25 00:55:02 +02:00
|
|
|
objects = ShadowManager()
|
|
|
|
|
2014-05-24 23:40:54 +02:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Shadow password')
|
|
|
|
verbose_name_plural = _('Shadow passwords')
|
2014-05-24 21:53:49 +02:00
|
|
|
|
2014-05-24 23:40:54 +02:00
|
|
|
def __str__(self):
|
|
|
|
return 'for user {0}'.format(self.user)
|
2014-05-24 21:53:49 +02:00
|
|
|
|
2014-06-01 14:51:33 +02:00
|
|
|
def set_password(self, password):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Set and encrypt the password.
|
|
|
|
|
|
|
|
:param str password: the password
|
|
|
|
"""
|
2014-06-01 14:51:33 +02:00
|
|
|
self.passwd = sha512_crypt.encrypt(password)
|
|
|
|
|
2014-05-24 23:40:54 +02:00
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
2014-05-24 21:53:49 +02:00
|
|
|
class AdditionalGroup(TimeStampedModel, models.Model):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
This entity class corresponds to additional group assignments for an
|
|
|
|
:py:class:`operating system user <osusers.models.User>`.
|
|
|
|
|
|
|
|
"""
|
2014-05-24 21:53:49 +02:00
|
|
|
user = models.ForeignKey(User)
|
|
|
|
group = models.ForeignKey(Group)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('user', 'group')
|
2014-05-24 23:40:54 +02:00
|
|
|
verbose_name = _('Additional group')
|
|
|
|
verbose_name_plural = _('Additional groups')
|
2014-05-24 21:53:49 +02:00
|
|
|
|
2014-12-27 22:44:27 +01:00
|
|
|
def __str__(self):
|
|
|
|
return '{0} in {1}'.format(self.user, self.group)
|
|
|
|
|
2014-05-24 21:53:49 +02:00
|
|
|
def clean(self):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Ensure that the assigned group is different from the user's primary
|
|
|
|
group.
|
|
|
|
|
|
|
|
"""
|
2014-05-24 21:53:49 +02:00
|
|
|
if self.user.group == self.group:
|
2014-06-01 01:36:50 +02:00
|
|
|
raise ValidationError(CANNOT_USE_PRIMARY_GROUP_AS_ADDITIONAL)
|
2014-05-24 23:40:54 +02:00
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
@transaction.atomic
|
2014-05-30 17:10:22 +02:00
|
|
|
def save(self, *args, **kwargs):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Persists the group assignment to LDAP and the database.
|
|
|
|
|
|
|
|
:param args: positional arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.save`
|
|
|
|
:param kwargs: keyword arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.save`
|
|
|
|
:return: this instance
|
|
|
|
:rtype: :py:class:`AdditionalGroup <osusers.models.AdditionalGroup>`
|
|
|
|
|
|
|
|
"""
|
2014-12-26 15:10:36 +01:00
|
|
|
add_ldap_user_to_group.delay(
|
|
|
|
self.user.username, self.group.groupname).get()
|
2014-12-27 22:44:27 +01:00
|
|
|
return super(AdditionalGroup, self).save(*args, **kwargs)
|
2014-05-30 17:10:22 +02:00
|
|
|
|
2014-12-22 20:07:11 +01:00
|
|
|
@transaction.atomic
|
2014-05-30 17:10:22 +02:00
|
|
|
def delete(self, *args, **kwargs):
|
2014-12-27 22:44:27 +01:00
|
|
|
"""
|
|
|
|
Delete the group assignment from LDAP and the database.
|
|
|
|
|
|
|
|
:param args: positional arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.delete`
|
|
|
|
:param kwargs: keyword arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.delete`
|
|
|
|
"""
|
2014-12-29 15:57:03 +01:00
|
|
|
TaskResult.objects.create_task_result(
|
|
|
|
remove_ldap_user_from_group.delay(
|
|
|
|
self.user.username, self.group.groupname),
|
|
|
|
'remove_ldap_user_from_group'
|
|
|
|
)
|
2014-05-30 17:10:22 +02:00
|
|
|
super(AdditionalGroup, self).delete(*args, **kwargs)
|
2015-01-31 21:48:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SshPublicKeyManager(models.Manager):
|
|
|
|
"""
|
|
|
|
Default manager for :py:class:`SSH public key
|
|
|
|
<osusers.models.SshPublicKey>` instances.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def parse_keytext(self, keytext):
|
|
|
|
"""
|
|
|
|
Parse a SSH public key in OpenSSH or :rfc:`4716` format into its
|
|
|
|
components algorithm, key data and comment.
|
|
|
|
|
|
|
|
:param str keytext: key text
|
|
|
|
:return: triple of algorithm name, key data and comment
|
|
|
|
:rtype: triple of str
|
|
|
|
|
|
|
|
"""
|
|
|
|
if keytext.startswith('---- BEGIN SSH2 PUBLIC KEY ----'):
|
|
|
|
comment = ''
|
|
|
|
data = ''
|
|
|
|
continued = ''
|
|
|
|
headers = {}
|
|
|
|
for line in keytext.splitlines():
|
|
|
|
if line == '---- BEGIN SSH2 PUBLIC KEY ----':
|
|
|
|
continue
|
|
|
|
elif ':' in line: # a header line
|
|
|
|
header_tag, header_value = [
|
|
|
|
item.strip() for item in line.split(':', 1)]
|
|
|
|
if header_value.endswith('\\'):
|
|
|
|
continued = header_value[:-1]
|
|
|
|
else:
|
|
|
|
headers[header_tag.lower()] = header_value
|
|
|
|
elif continued:
|
|
|
|
if line.endswith('\\'):
|
|
|
|
continued += line[:-1]
|
|
|
|
continue
|
|
|
|
header_value = continued + line
|
|
|
|
headers[header_tag.lower()] = header_value
|
|
|
|
continued = ''
|
|
|
|
elif line == '---- END SSH2 PUBLIC KEY ----':
|
|
|
|
break
|
|
|
|
elif line: # ignore empty lines
|
|
|
|
data += line
|
|
|
|
if 'comment' in headers:
|
|
|
|
comment = headers['comment']
|
|
|
|
else:
|
2015-02-21 20:54:51 +01:00
|
|
|
parts = keytext.split(None, 2)
|
|
|
|
if len(parts) < 2:
|
|
|
|
raise ValueError('invalid SSH public key')
|
2015-01-31 21:48:07 +01:00
|
|
|
data = parts[1]
|
|
|
|
comment = len(parts) == 3 and parts[2] or ""
|
2015-10-11 15:30:23 +02:00
|
|
|
try:
|
|
|
|
keybytes = base64.b64decode(data)
|
|
|
|
except TypeError:
|
|
|
|
raise ValueError('invalid SSH public key')
|
2015-01-31 21:48:07 +01:00
|
|
|
parts = keybytes.split(b'\x00' * 3)
|
|
|
|
alglength = six.byte2int(parts[1])
|
|
|
|
algname = parts[1][1:1+alglength]
|
|
|
|
return algname, data, comment
|
|
|
|
|
|
|
|
def create_ssh_public_key(self, user, keytext):
|
|
|
|
"""
|
|
|
|
Create a new :py:class:`SSH public key <osusers.models.SshPublicKey>`
|
|
|
|
for a user from the given key text representation. The text can be
|
|
|
|
either in openssh format or :rfc:`4716` format.
|
|
|
|
|
|
|
|
:param user: :py:class:`operating system user <osusers.models.User>`
|
|
|
|
:param str keytext: key text
|
|
|
|
:return: public key
|
|
|
|
:retype: :py:class:`osusers.models.SshPublicKey`
|
|
|
|
|
|
|
|
"""
|
|
|
|
algorithm, data, comment = self.parse_keytext(keytext)
|
|
|
|
return self.create(
|
|
|
|
user=user, algorithm=algorithm, data=data, comment=comment)
|
|
|
|
|
|
|
|
|
|
|
|
@python_2_unicode_compatible
|
|
|
|
class SshPublicKey(TimeStampedModel):
|
|
|
|
"""
|
|
|
|
This entity class represents single SSH keys for an :py:class:`operating
|
|
|
|
system user <osusers.models.User>`.
|
|
|
|
|
|
|
|
"""
|
|
|
|
user = models.ForeignKey(User, verbose_name=_('User'))
|
|
|
|
algorithm = models.CharField(_('Algorithm'), max_length=20)
|
|
|
|
data = models.TextField(_('Key bytes'),
|
|
|
|
help_text=_('Base64 encoded key bytes'))
|
|
|
|
comment = models.TextField(_('Comment'), blank=True)
|
|
|
|
|
|
|
|
objects = SshPublicKeyManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('SSH public key')
|
|
|
|
verbose_name_plural = _('SSH public keys')
|
|
|
|
unique_together = [('user', 'algorithm', 'data')]
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{algorithm} {data} {comment}".format(
|
|
|
|
algorithm=self.algorithm, data=self.data, comment=self.comment
|
|
|
|
).strip()
|
2015-01-31 23:00:33 +01:00
|
|
|
|
|
|
|
def save(self, **kwargs):
|
|
|
|
key = super(SshPublicKey, self).save(**kwargs)
|
|
|
|
TaskResult.objects.create_task_result(
|
|
|
|
set_file_ssh_authorized_keys.delay(
|
|
|
|
self.user.username, [
|
|
|
|
str(key) for key in
|
|
|
|
SshPublicKey.objects.filter(user=self.user)]),
|
|
|
|
'set_file_ssh_authorized_keys'
|
|
|
|
)
|
|
|
|
return key
|
|
|
|
|
|
|
|
def delete(self, **kwargs):
|
|
|
|
super(SshPublicKey, self).delete(**kwargs)
|
|
|
|
TaskResult.objects.create_task_result(
|
|
|
|
set_file_ssh_authorized_keys.delay(
|
|
|
|
self.user.username, [
|
|
|
|
str(key) for key in
|
|
|
|
SshPublicKey.objects.filter(user=self.user)]),
|
|
|
|
'set_file_ssh_authorized_keys'
|
|
|
|
)
|