2003-12-21 01:17:34 +01:00
|
|
|
#! /usr/bin/env python
|
|
|
|
|
|
|
|
import os, string
|
2004-12-26 00:09:19 +01:00
|
|
|
import gnuviech
|
2004-12-26 20:29:32 +01:00
|
|
|
#from GNVAdm import GNVDomain
|
|
|
|
from gnuviech import GNVPrefs, tools
|
|
|
|
from gnuviech.gnvdomain import GNVDomain
|
2003-12-21 01:17:34 +01:00
|
|
|
|
|
|
|
# if [ -n $USERPREFIX ]; then
|
|
|
|
# USERPREFIX="usr"
|
|
|
|
# fi
|
|
|
|
|
|
|
|
# if [ $1 == "" ]; then
|
|
|
|
# echo "give <UserNumber>p<pop3Account> as parameter"
|
|
|
|
# exit
|
|
|
|
# fi
|
|
|
|
|
|
|
|
# NEWUSER="$USERPREFIX$1"
|
|
|
|
# NEWHOME="/home/mail/$NEWUSER"
|
|
|
|
# NEWPASS=$(apg -n 1 -a 1 -CL -m 8 -x 12)
|
|
|
|
# echo $NEWHOME
|
|
|
|
|
|
|
|
# adduser --home "$NEWHOME" --shell /bin/true --no-create-home --firstuid 20000 --ingroup poponly --disabled-password --disabled-login --gecos "Popuser $NEWUSER" $NEWUSER
|
|
|
|
|
|
|
|
# mkdir -p "$NEWHOME"
|
|
|
|
# chown -Rc $NEWUSER.poponly "$NEWHOME"
|
|
|
|
# echo "${NEWUSER}:${NEWPASS}" | chpasswd
|
|
|
|
|
|
|
|
# echo "Herzlich willkommen auf dem GNU-Viech" | mail -s "Willkommen auf dem GNU-Viech" ${NEWUSER}
|
|
|
|
# echo added new pop3 user $NEWUSER with password $NEWPASS
|
|
|
|
|
2004-12-26 20:29:32 +01:00
|
|
|
class MailAliasExists(Exception): pass
|
|
|
|
|
|
|
|
class POP3AccountExists(Exception): pass
|
|
|
|
|
2004-12-26 00:09:19 +01:00
|
|
|
class MailAccount:
|
2004-12-26 20:29:32 +01:00
|
|
|
def __init__(self, domain, localpart):
|
2004-12-21 20:01:59 +01:00
|
|
|
"Initialize a MailAccount instance for a given domain"
|
2004-12-26 00:09:19 +01:00
|
|
|
if (not os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.R_OK & os.X_OK)):
|
|
|
|
self.setupDirs()
|
|
|
|
self.domain = domain
|
2004-12-26 20:29:32 +01:00
|
|
|
self.localpart = localpart
|
|
|
|
self.prefs = domain.prefs
|
|
|
|
self.logger = domain.prefs.getLogger(self)
|
2004-12-26 00:09:19 +01:00
|
|
|
|
2004-12-26 20:29:32 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return "%s@%s" % (self.localpart, self.domain.name)
|
2004-12-21 20:01:59 +01:00
|
|
|
|
|
|
|
class MailAlias(MailAccount):
|
|
|
|
"""This represents a mail alias"""
|
|
|
|
|
2004-12-26 20:29:32 +01:00
|
|
|
def __init__(self, domain, localpart, target):
|
2004-12-21 20:01:59 +01:00
|
|
|
"Initialize the POPAccount class for a given domain"
|
2004-12-26 20:29:32 +01:00
|
|
|
if localpart in domain.mailaliases.keys():
|
|
|
|
raise MailAliasExists
|
|
|
|
MailAccount.__init__(self, domain, localpart)
|
|
|
|
self.setTarget(target)
|
|
|
|
|
|
|
|
def setTarget(self, target):
|
|
|
|
self.target = target
|
|
|
|
self.logger.debug("setting target for alias %s to %s." %
|
|
|
|
(str(self), self.target))
|
|
|
|
# self.aliases = {}
|
|
|
|
# self.readAll()
|
2004-12-21 20:01:59 +01:00
|
|
|
|
2004-12-26 20:29:32 +01:00
|
|
|
# def readAll():
|
|
|
|
# """reads the aliasfile for the given domain"""
|
|
|
|
# self.aliases = {}
|
|
|
|
# if (os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.R_OK)):
|
|
|
|
# try:
|
|
|
|
# aliasfile = open(gnuviech.GNVPrefs.VIRTUALDOMDIR+self.domain.name , 'r')
|
|
|
|
# for line in aliasfile.readlines():
|
|
|
|
# keyvals = string.split(line,":",1)
|
|
|
|
# self.aliases[keyvals[0]] = keyvals[1].strip()
|
|
|
|
# aliasfile.close()
|
|
|
|
# except IOError:
|
|
|
|
# self.logger.error("couldn't read the aliasfile for "+self.domain.name+".")
|
|
|
|
# else:
|
|
|
|
# self.logger.error("couldn't read from "+gnuviech.GNVPrefs.VIRTUALDOMDIR+".")
|
|
|
|
|
|
|
|
# def writeAll(self):
|
|
|
|
# """writes the aliasfile for the given domain with the aliases defined
|
|
|
|
# in the dictionary object aliases"""
|
|
|
|
# if (os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.W_OK)):
|
|
|
|
# try:
|
|
|
|
# aliasfile = open(gnuviech.GNVPrefs.VIRTUALDOMDIR+self.domain.name, 'w')
|
|
|
|
# keys = self.aliases.keys();
|
|
|
|
# keys.sort();
|
|
|
|
# for key in keys:
|
|
|
|
# aliasfile.write("%s:%s" % (key, self.aliases[key]) + "\n")
|
|
|
|
# aliasfile.close()
|
|
|
|
# except IOError:
|
|
|
|
# self.logger.error("writing to aliasfile failed.")
|
|
|
|
# else:
|
|
|
|
# self.logger.error("no write access to directory "+gnuviech.GNVPrefs.VIRTUALDOMDIR+".")
|
|
|
|
|
|
|
|
# def setAlias(self, alias, target):
|
|
|
|
# """sets a mail alias for given domain which directs the MTA to the
|
|
|
|
# given target
|
|
|
|
# """
|
|
|
|
# self.readAll()
|
|
|
|
# self.aliases[alias]=target
|
|
|
|
# self.writeAll()
|
2004-12-21 20:01:59 +01:00
|
|
|
|
|
|
|
class POP3Account(MailAccount):
|
|
|
|
"""This represents a pop 3 account"""
|
|
|
|
|
2004-12-26 20:29:32 +01:00
|
|
|
def __init__(self, domain, localpart):
|
|
|
|
"""Creates a new pop3 mail account"""
|
|
|
|
if localpart in domain.pop3accounts.keys():
|
|
|
|
raise POP3AccountExists
|
|
|
|
MailAccount.__init__(self, domain, localpart)
|
|
|
|
self.logger.debug("adding address %s@%s." % (self.localpart,
|
|
|
|
self.domain.name))
|
|
|
|
self.setPassword(tools.generatePassword())
|
|
|
|
self.setSysUser(domain.getNextUser("pop3"))
|
|
|
|
self.domain.addMailAlias(MailAlias(self.domain,
|
|
|
|
self.localpart, self.sysuser))
|
|
|
|
|
|
|
|
def setPassword(self, newpassword):
|
|
|
|
self.password = newpassword
|
|
|
|
self.logger.debug("set password for %s to %s." %
|
|
|
|
(str(self), self.password))
|
|
|
|
|
|
|
|
def setSysUser(self, username):
|
|
|
|
self.sysuser = username
|
|
|
|
self.logger.debug("set system user for %s to %s" %
|
|
|
|
(str(self), self.sysuser))
|
|
|
|
|
2004-12-21 20:01:59 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2004-12-26 20:29:32 +01:00
|
|
|
prefs = GNVPrefs()
|
|
|
|
domain = GNVDomain("dittberner.info", prefs)
|
|
|
|
domain.addPOP3Account(POP3Account(domain, "test"))
|
|
|
|
domain.addMailAlias(MailAlias(domain, "klaus", "klaus@test.de"))
|