2008-01-12 21:46:28 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2007-07-02 11:14:47 +02:00
|
|
|
#
|
2008-01-12 21:46:28 +01:00
|
|
|
# Copyright (C) 2007, 2008 by Jan Dittberner.
|
2007-07-02 11:14:47 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
# USA.
|
|
|
|
#
|
|
|
|
# Version: $Id$
|
|
|
|
|
2009-08-01 21:35:43 +02:00
|
|
|
from gnuviechadmin.exceptions import CannotDeleteError
|
2007-07-02 11:14:47 +02:00
|
|
|
from gnuviechadmin.util import passwordutils, getenttools
|
2009-08-01 21:35:43 +02:00
|
|
|
from gnuviechadmin.backend.BackendTo import Sysuser
|
|
|
|
from gnuviechadmin.backend.BackendEntity import BackendEntity
|
|
|
|
from gnuviechadmin.backend.BackendEntityHandler import BackendEntityHandler
|
2008-01-12 21:46:28 +01:00
|
|
|
import os
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2008-06-06 21:20:18 +02:00
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
class SysuserEntity(BackendEntity):
|
2007-07-02 11:14:47 +02:00
|
|
|
"""Entity class for system users."""
|
|
|
|
|
2009-08-01 21:35:43 +02:00
|
|
|
def __init__(self, config, delegate, verbose = False, **kwargs):
|
|
|
|
BackendEntity.__init__(self, config, delegate, verbose)
|
2007-07-05 11:00:34 +02:00
|
|
|
for (key, value) in kwargs.items():
|
|
|
|
self.__setattr__(key, value)
|
2007-07-09 08:46:36 +02:00
|
|
|
if not self.delegateto.username:
|
|
|
|
self.delegateto.username = self._get_next_sysusername()
|
|
|
|
if not self.delegateto.usertype:
|
|
|
|
self.delegateto.usertype = self._get_default_sysusertype()
|
|
|
|
if not self.delegateto.home:
|
|
|
|
self.delegateto.home = self._get_home(self.delegateto.username)
|
|
|
|
if not self.delegateto.shell:
|
|
|
|
self.delegateto.shell = self._get_default_shell()
|
|
|
|
(self.delegateto.clearpass, self.delegateto.md5pass) = \
|
|
|
|
passwordutils.get_pw_tuple(self.delegateto.clearpass)
|
|
|
|
if not self.delegateto.sysuid:
|
|
|
|
self.delegateto.sysuid = self._get_next_sysuid()
|
2007-07-02 11:14:47 +02:00
|
|
|
self.validate()
|
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def _get_next_sysusername(self):
|
|
|
|
prefix = config.get('sysuser', 'nameprefix')
|
2008-04-05 18:54:07 +02:00
|
|
|
self.logger.debug("looking for next user with prefix %s" % (prefix))
|
2007-07-02 11:14:47 +02:00
|
|
|
usernames = [user.username for user in \
|
2007-07-09 08:46:36 +02:00
|
|
|
getenttools.find_user_by_prefix(prefix)]
|
2008-04-05 18:54:07 +02:00
|
|
|
if usernames:
|
2008-06-06 21:20:18 +02:00
|
|
|
maxid = max([int(username[len(prefix):]) \
|
|
|
|
for username in usernames])
|
2008-04-05 18:54:07 +02:00
|
|
|
maxid += 2
|
|
|
|
for number in range(1, maxid):
|
|
|
|
username = "%s%02d" % (prefix, number)
|
|
|
|
if not username in usernames:
|
|
|
|
return username
|
|
|
|
else:
|
|
|
|
return "%s%02d" % (prefix, 1)
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def _get_default_sysusertype(self):
|
2007-07-02 11:14:47 +02:00
|
|
|
return 1
|
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def _get_home(self, sysusername):
|
2007-07-02 11:14:47 +02:00
|
|
|
"""Gets a valid home directory for the given user name."""
|
2007-07-09 08:46:36 +02:00
|
|
|
return os.path.join(config.get('sysuser', 'homedirbase'),
|
2007-07-02 11:14:47 +02:00
|
|
|
sysusername)
|
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def _get_default_shell(self):
|
2007-07-02 11:14:47 +02:00
|
|
|
return False
|
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def _get_shell_binary(self):
|
|
|
|
if self.delegateto.shell:
|
|
|
|
return config.get('sysuser', 'shellyes')
|
|
|
|
return config.get('sysuser', 'shellno')
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def _get_next_sysuid(self):
|
2007-07-26 15:21:36 +02:00
|
|
|
return getenttools.get_next_uid(int(config.get('sysuser', 'minuid')),
|
2008-06-06 21:20:18 +02:00
|
|
|
int(config.get('sysuser', 'maxuid')))
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def _populate_home(self):
|
|
|
|
templatedir = get_template_dir(config.get('sysuser', 'hometemplate'))
|
2008-01-12 21:46:28 +01:00
|
|
|
olddir = os.getcwd()
|
|
|
|
os.chdir(templatedir)
|
|
|
|
cmd1 = 'find . -depth \! -regex ".*\.svn.*" \! -name "*~" -print0'
|
2008-06-06 21:20:18 +02:00
|
|
|
cmd2 = 'cpio --pass-through --owner=%(username)s.%(group)s --null ' + \
|
|
|
|
'--make-directories %(home)s' % {
|
|
|
|
'username': self.delegateto.username,
|
|
|
|
'group': config.get('sysuser', 'defaultgroup'),
|
|
|
|
'home': self.delegateto.home}
|
2008-01-12 21:46:28 +01:00
|
|
|
self.supipe((cmd1, cmd2))
|
|
|
|
os.chdir(olddir)
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def _mail_sysuser(self):
|
|
|
|
template = get_template(config.get('common', 'mailtemplates'),
|
|
|
|
config.get('sysuser', 'create.mail'))
|
|
|
|
text = template.substitute({
|
2008-06-06 21:20:18 +02:00
|
|
|
'uid': self.delegateto.sysuid,
|
|
|
|
'firstname': self.delegateto.client.firstname,
|
|
|
|
'lastname': self.delegateto.client.lastname,
|
|
|
|
'email': self.delegateto.client.email,
|
|
|
|
'username': self.delegateto.username,
|
|
|
|
'password': self.delegateto.clearpass,
|
|
|
|
'home': self.delegateto.home,
|
|
|
|
'shell': self._get_shell_binary()})
|
2007-07-09 08:46:36 +02:00
|
|
|
template = get_template_string(config.get('sysuser', 'create_subject'))
|
|
|
|
subject = template.substitute({
|
2008-06-06 21:20:18 +02:00
|
|
|
'username': self.delegateto.username})
|
2007-07-09 08:46:36 +02:00
|
|
|
self.send_mail(subject, text)
|
|
|
|
|
|
|
|
def create_hook(self, session):
|
|
|
|
gecos = config.get('sysuser', 'gecos') % (self.delegateto.username)
|
2008-06-06 21:20:18 +02:00
|
|
|
cmdline = 'adduser --home "%(home)s" --shell "%(shell)s" ' + \
|
|
|
|
'--no-create-home --uid %(sysuid)d --ingroup "%(group)s" ' + \
|
|
|
|
'--disabled-password --gecos "%(gecos)s" %(username)s' % {
|
|
|
|
'home': self.delegateto.home,
|
|
|
|
'shell': self._get_shell_binary(),
|
|
|
|
'sysuid': self.delegateto.sysuid,
|
|
|
|
'group': config.get('sysuser', 'defaultgroup'),
|
|
|
|
'gecos': gecos,
|
|
|
|
'username': self.delegateto.username}
|
2007-07-02 11:14:47 +02:00
|
|
|
self.sucommand(cmdline)
|
|
|
|
cmdline = 'chpasswd --encrypted'
|
|
|
|
inline = '%(username)s:%(md5pass)s' % {
|
2008-06-06 21:20:18 +02:00
|
|
|
'username': self.delegateto.username,
|
|
|
|
'md5pass': self.delegateto.md5pass}
|
2007-07-02 11:14:47 +02:00
|
|
|
self.sucommand(cmdline, inline)
|
2007-07-09 08:46:36 +02:00
|
|
|
self._populate_home()
|
|
|
|
self._mail_sysuser()
|
|
|
|
|
|
|
|
def delete_hook(self, session):
|
|
|
|
if self.delegateto.domains:
|
|
|
|
raise CannotDeleteError(
|
|
|
|
self.delegateto,
|
|
|
|
"it still has the following domains assigned: %s" % (
|
|
|
|
", ".join([domain.name for domain in \
|
|
|
|
self.delegateto.domains])))
|
|
|
|
backupdir = os.path.join(config.get('common', 'backupdir'),
|
|
|
|
config.get('sysuser', 'homebackupdir'))
|
2007-07-02 11:14:47 +02:00
|
|
|
if not os.path.isdir(backupdir):
|
|
|
|
cmdline = 'mkdir -p "%(backupdir)s"' % {
|
2008-06-06 21:20:18 +02:00
|
|
|
'backupdir': backupdir}
|
2007-07-02 11:14:47 +02:00
|
|
|
status = self.sucommand(cmdline)
|
|
|
|
if status != 0:
|
|
|
|
raise Exception("could not create backup directory")
|
2008-06-06 21:20:18 +02:00
|
|
|
cmdline = 'deluser --remove-home --backup --backup-to ' + \
|
|
|
|
' "%(backupdir)s" %(username)s' % {
|
|
|
|
'backupdir': backupdir,
|
|
|
|
'username': self.delegateto.username}
|
2007-07-02 11:14:47 +02:00
|
|
|
self.sucommand(cmdline)
|
|
|
|
|
2008-06-06 21:20:18 +02:00
|
|
|
|
2007-07-02 11:14:47 +02:00
|
|
|
class SysuserHandler(BackendEntityHandler):
|
|
|
|
"""BackendEntityHandler for Sysuser entities."""
|
|
|
|
|
2009-08-01 21:35:43 +02:00
|
|
|
def __init__(self, config, verbose = False):
|
|
|
|
BackendEntityHandler.__init__(self, SysuserEntity, Sysuser,
|
|
|
|
config, verbose)
|