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
|
|
|
|
from GNVAdm 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 00:09:19 +01:00
|
|
|
class MailAccount:
|
2004-12-21 20:01:59 +01:00
|
|
|
def __init__(self, domain):
|
|
|
|
"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
|
|
|
|
|
|
|
|
def setupDirs(self):
|
|
|
|
os.mkdir(gnuviech.GNVPrefs.VIRTUALDOMDIR)
|
2004-12-21 20:01:59 +01:00
|
|
|
|
|
|
|
class MailAlias(MailAccount):
|
|
|
|
"""This represents a mail alias"""
|
|
|
|
|
|
|
|
def __init__(self, domain):
|
|
|
|
"Initialize the POPAccount class for a given domain"
|
|
|
|
MailAccount.__init__(self, domain)
|
2004-12-26 00:09:19 +01:00
|
|
|
self.aliases = {}
|
2004-12-21 20:01:59 +01:00
|
|
|
self.readAll()
|
|
|
|
|
|
|
|
def readAll(self):
|
|
|
|
"""reads the aliasfile for the given domain"""
|
2004-12-26 00:09:19 +01:00
|
|
|
self.aliases = {}
|
|
|
|
if (os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.R_OK)):
|
2004-12-21 20:01:59 +01:00
|
|
|
try:
|
2004-12-26 00:09:19 +01:00
|
|
|
aliasfile = open(gnuviech.GNVPrefs.VIRTUALDOMDIR+self.domain.name , 'r')
|
2004-12-21 20:01:59 +01:00
|
|
|
for line in aliasfile.readlines():
|
|
|
|
keyvals = string.split(line,":",1)
|
2004-12-26 00:09:19 +01:00
|
|
|
self.aliases[keyvals[0]] = keyvals[1].strip()
|
2004-12-21 20:01:59 +01:00
|
|
|
aliasfile.close()
|
|
|
|
except IOError:
|
2004-12-26 00:09:19 +01:00
|
|
|
print "couldn't read the aliasfile for "+self.domain.name+"."
|
2004-12-21 20:01:59 +01:00
|
|
|
else:
|
2004-12-26 00:09:19 +01:00
|
|
|
print "couldn't read from "+gnuviech.GNVPrefs.VIRTUALDOMDIR+"."
|
2004-12-21 20:01:59 +01:00
|
|
|
|
|
|
|
def writeAll(self):
|
|
|
|
"""writes the aliasfile for the given domain with the aliases defined
|
|
|
|
in the dictionary object aliases"""
|
2004-12-26 00:09:19 +01:00
|
|
|
if (os.access(gnuviech.GNVPrefs.VIRTUALDOMDIR, os.W_OK)):
|
2004-12-21 20:01:59 +01:00
|
|
|
try:
|
2004-12-26 00:09:19 +01:00
|
|
|
aliasfile = open(gnuviech.GNVPrefs.VIRTUALDOMDIR+self.domain.name, 'w')
|
|
|
|
keys = self.aliases.keys();
|
2004-12-21 20:01:59 +01:00
|
|
|
keys.sort();
|
|
|
|
for key in keys:
|
2004-12-26 00:09:19 +01:00
|
|
|
aliasfile.write("%s:%s" % (key, self.aliases[key]) + "\n")
|
2004-12-21 20:01:59 +01:00
|
|
|
aliasfile.close()
|
|
|
|
except IOError:
|
|
|
|
print "writing to aliasfile failed."
|
|
|
|
else:
|
2004-12-26 00:09:19 +01:00
|
|
|
print "no write access to directory "+gnuviech.GNVPrefs.VIRTUALDOMDIR+"."
|
2004-12-21 20:01:59 +01:00
|
|
|
|
|
|
|
def setAlias(self, alias, target):
|
|
|
|
"""sets a mail alias for given domain which directs the MTA to the
|
|
|
|
given target
|
|
|
|
"""
|
|
|
|
self.readAll()
|
2004-12-26 00:09:19 +01:00
|
|
|
self.aliases[alias]=target
|
2004-12-21 20:01:59 +01:00
|
|
|
self.writeAll()
|
|
|
|
|
|
|
|
class POP3Account(MailAccount):
|
|
|
|
"""This represents a pop 3 account"""
|
|
|
|
|
|
|
|
def create(self, address):
|
|
|
|
"""Creates a pop3/imap account for the domain"""
|
|
|
|
print self
|
|
|
|
print "adding address "+address
|
2004-12-26 00:09:19 +01:00
|
|
|
alias = MailAlias(self.domain)
|
|
|
|
alias.setAlias(address, self.domain.getNextUser("pop3"))
|
2004-12-21 20:01:59 +01:00
|
|
|
print alias
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2004-12-26 00:09:19 +01:00
|
|
|
popacc = POP3Account(GNVDomain("dittberner.info"))
|
2004-12-21 20:01:59 +01:00
|
|
|
print popacc
|
2004-12-26 00:09:19 +01:00
|
|
|
popacc.create("test")
|
2004-12-21 20:01:59 +01:00
|
|
|
|
2004-12-26 00:09:19 +01:00
|
|
|
alias = MailAlias(GNVDomain("dittberner.info"))
|
2004-12-21 20:01:59 +01:00
|
|
|
print alias
|
|
|
|
alias.setAlias("klaus", "klaus@dittberner.info")
|
|
|
|
print alias
|