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$
|
|
|
|
|
|
|
|
from gnuviechadmin.exceptions import *
|
|
|
|
from gnuviechadmin.util import passwordutils, getenttools
|
2007-07-09 08:46:36 +02:00
|
|
|
from settings import config
|
|
|
|
from BackendTo import *
|
2007-07-02 11:14:47 +02:00
|
|
|
from BackendEntity import *
|
|
|
|
from BackendEntityHandler import *
|
2008-01-12 21:46:28 +01:00
|
|
|
import os
|
2007-07-02 11:14:47 +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."""
|
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def __init__(self, delegate, verbose = False, **kwargs):
|
|
|
|
BackendEntity.__init__(self, 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')
|
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)]
|
2007-07-02 11:14:47 +02:00
|
|
|
maxid = max([int(username[len(prefix):]) for username in usernames])
|
2008-01-12 21:46:28 +01:00
|
|
|
maxid += 2
|
|
|
|
for number in range(1, maxid):
|
|
|
|
username = "%s%02d" % (prefix, number)
|
2007-07-02 11:14:47 +02:00
|
|
|
if not username in usernames:
|
|
|
|
return username
|
|
|
|
|
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')),
|
|
|
|
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'
|
|
|
|
cmd2 = 'cpio --pass-through --owner=%(username)s.%(group)s --null --make-directories %(home)s' % {
|
2007-07-09 08:46:36 +02:00
|
|
|
'username' : self.delegateto.username,
|
|
|
|
'group' : config.get('sysuser', 'defaultgroup'),
|
2008-01-12 21:46:28 +01:00
|
|
|
'home' : self.delegateto.home}
|
|
|
|
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({
|
|
|
|
'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()})
|
|
|
|
template = get_template_string(config.get('sysuser', 'create_subject'))
|
|
|
|
subject = template.substitute({
|
|
|
|
'username' : self.delegateto.username})
|
|
|
|
self.send_mail(subject, text)
|
|
|
|
|
|
|
|
def create_hook(self, session):
|
|
|
|
gecos = config.get('sysuser', 'gecos') % (self.delegateto.username)
|
2007-07-02 11:14:47 +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' % {
|
2007-07-09 08:46:36 +02:00
|
|
|
'home' : self.delegateto.home,
|
|
|
|
'shell' : self._get_shell_binary(),
|
|
|
|
'sysuid' : self.delegateto.sysuid,
|
|
|
|
'group' : config.get('sysuser', 'defaultgroup'),
|
2007-07-02 11:14:47 +02:00
|
|
|
'gecos' : gecos,
|
2007-07-09 08:46:36 +02:00
|
|
|
'username' : self.delegateto.username}
|
2007-07-02 11:14:47 +02:00
|
|
|
self.sucommand(cmdline)
|
|
|
|
cmdline = 'chpasswd --encrypted'
|
|
|
|
inline = '%(username)s:%(md5pass)s' % {
|
2007-07-09 08:46:36 +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"' % {
|
|
|
|
'backupdir' : backupdir}
|
|
|
|
status = self.sucommand(cmdline)
|
|
|
|
if status != 0:
|
|
|
|
raise Exception("could not create backup directory")
|
|
|
|
cmdline = 'deluser --remove-home --backup --backup-to "%(backupdir)s" %(username)s' % {
|
|
|
|
'backupdir' : backupdir,
|
2007-07-09 08:46:36 +02:00
|
|
|
'username' : self.delegateto.username}
|
2007-07-02 11:14:47 +02:00
|
|
|
self.sucommand(cmdline)
|
|
|
|
|
|
|
|
class SysuserHandler(BackendEntityHandler):
|
|
|
|
"""BackendEntityHandler for Sysuser entities."""
|
|
|
|
|
|
|
|
def __init__(self, verbose = False):
|
2007-07-09 08:46:36 +02:00
|
|
|
BackendEntityHandler.__init__(self, SysuserEntity, Sysuser, verbose)
|