- mail related tables in database schema
- gpg encryption for mails - domain creation and deletion completed - logging - use pwd and grp git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@230 a67ec6bc-e5d5-0310-a910-815c51eb3124
This commit is contained in:
parent
3f4457bdca
commit
fdea3217c8
28 changed files with 877 additions and 323 deletions
|
|
@ -19,41 +19,35 @@
|
|||
#
|
||||
# Version: $Id$
|
||||
|
||||
import ConfigParser, os
|
||||
from subprocess import *
|
||||
from sqlalchemy import *
|
||||
import smtplib, os, logging, tempfile
|
||||
from email.MIMEText import MIMEText
|
||||
from pyme import core
|
||||
from pyme.constants.sig import mode
|
||||
|
||||
from settings import config
|
||||
from gnuviechadmin.exceptions import *
|
||||
from subprocess import *
|
||||
import sqlalchemy
|
||||
|
||||
class BackendEntity(object):
|
||||
"""This is the abstract base class for all backend entity classes."""
|
||||
|
||||
def __init__(self, verbose = False):
|
||||
def __init__(self, delegateto, verbose = False):
|
||||
self.logger = logging.getLogger("%s.%s" % (
|
||||
self.__class__.__module__, self.__class__.__name__))
|
||||
self.delegateto = delegateto
|
||||
self.verbose = verbose
|
||||
self.config = ConfigParser.ConfigParser()
|
||||
self.config.readfp(open('gnuviechadmin/defaults.cfg'))
|
||||
self.config.read(['gnuviechadmin/gva.cfg',
|
||||
os.path.expanduser('~/.gva.cfg')])
|
||||
|
||||
def __repr__(self):
|
||||
if self.verbose:
|
||||
cols = [col for col in \
|
||||
object_mapper(self).local_table.columns.keys()]
|
||||
format = "%(class)s:"
|
||||
format = format + ", ".join([col + "=%(" + col + ")s" for col in \
|
||||
cols])
|
||||
data = {'class' : self.__class__.__name__}
|
||||
else:
|
||||
cols = self._shortkeys
|
||||
format = ",".join("%(" + col + ")s" for col in cols)
|
||||
data = {}
|
||||
data.update(dict([(col, self.__getattribute__(col)) for col in cols]))
|
||||
return format % data
|
||||
return self.delegateto.__repr__(verbose = self.verbose)
|
||||
|
||||
def sucommand(self, cmdline, pipedata = None):
|
||||
"""Executes a command as root using the configured suwrapper
|
||||
command. If a pipe is specified it is used as stdin of the
|
||||
subprocess."""
|
||||
suwrapper = self.config.get('common', 'suwrapper')
|
||||
self.logger.debug("sucommand called: %s (pipedata=%s)", cmdline,
|
||||
str(pipedata))
|
||||
suwrapper = config.get('common', 'suwrapper')
|
||||
toexec = "%s %s" % (suwrapper, cmdline)
|
||||
if pipedata:
|
||||
p = Popen(toexec, shell = True, stdin=PIPE)
|
||||
|
|
@ -63,21 +57,80 @@ class BackendEntity(object):
|
|||
sts = os.waitpid(p.pid, 0)
|
||||
if self.verbose:
|
||||
print "%s|%s: %d" % (pipedata, toexec, sts[1])
|
||||
self.logger.info("%s|%s: %d", pipedata, toexec, sts[1])
|
||||
else:
|
||||
p = Popen(toexec, shell = True)
|
||||
sts = os.waitpid(p.pid, 0)
|
||||
if self.verbose:
|
||||
print "%s: %s" % (toexec, sts[1])
|
||||
self.logger.info("%s: %s", toexec, sts[1])
|
||||
return sts[1]
|
||||
|
||||
def send_mail(self, subject, text):
|
||||
"""This method sends a mail with the given text and subject
|
||||
and signs it usign GnuPG. If a public key of the recipient is
|
||||
available the mail is encrypted."""
|
||||
plain = core.Data(text)
|
||||
sig = core.Data()
|
||||
c = core.Context()
|
||||
signer = config.get('common', 'mailfrom')
|
||||
rcpt = config.get('common', 'mailto')
|
||||
c.signers_clear()
|
||||
for sigkey in [x for x in c.op_keylist_all(signer, 1)]:
|
||||
if sigkey.can_sign:
|
||||
c.signers_add(sigkey)
|
||||
if not c.signers_enum(0):
|
||||
raise Exception("No secret keys for signing available for %s." % (
|
||||
signer))
|
||||
keylist = []
|
||||
for key in [x for x in c.op_keylist_all(rcpt, 1)]:
|
||||
valid = 0
|
||||
subkey = key.subkeys
|
||||
while subkey:
|
||||
keyid = subkey.keyid
|
||||
if keyid == None:
|
||||
break
|
||||
can_encrypt = subkey.can_encrypt
|
||||
valid += can_encrypt
|
||||
subkey = subkey.next
|
||||
if valid:
|
||||
keylist.append(key)
|
||||
if keylist:
|
||||
c.set_armor(1)
|
||||
c.op_encrypt_sign(keylist, 1, plain, sig)
|
||||
else:
|
||||
c.op_sign(plain, sig, mode.CLEAR)
|
||||
sig.seek(0,0)
|
||||
|
||||
msg = MIMEText(sig.read())
|
||||
msg['Subject'] = subject
|
||||
msg['From'] = signer
|
||||
msg['To'] = rcpt
|
||||
|
||||
s = smtplib.SMTP()
|
||||
s.connect()
|
||||
s.sendmail(signer, [rcpt], msg.as_string())
|
||||
s.close()
|
||||
|
||||
def validate(self):
|
||||
"""Validates whether all mandatory fields of the entity have
|
||||
values."""
|
||||
missingfields = []
|
||||
for key in [col.name for col in \
|
||||
object_mapper(self).local_table.columns \
|
||||
sqlalchemy.object_mapper(
|
||||
self.delegateto).local_table.columns \
|
||||
if not col.primary_key and not col.nullable]:
|
||||
if self.__getattribute__(key) is None:
|
||||
if self.delegateto.__getattribute__(key) is None:
|
||||
missingfields.append(key)
|
||||
if missingfields:
|
||||
raise MissingFieldsError(missingfields)
|
||||
|
||||
def write_to_file(self, filename, template):
|
||||
"""Write the data from template to the specified file."""
|
||||
tmp = tempfile.NamedTemporaryFile()
|
||||
tmp.write(template)
|
||||
tmp.flush()
|
||||
cmd = 'cp "%s" "%s"' % (tmp.name, filename)
|
||||
self.sucommand(cmd)
|
||||
tmp.close()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue