2007-02-12 22:39:14 +01:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2007 by Jan Dittberner.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2007-07-02 11:14:47 +02:00
|
|
|
# Version: $Id: client.py 1101 2007-02-28 21:15:20Z jan $
|
2007-02-12 22:39:14 +01:00
|
|
|
|
|
|
|
from sqlalchemy import *
|
2007-07-02 11:14:47 +02:00
|
|
|
from tables import domain_table
|
2007-02-12 22:39:14 +01:00
|
|
|
from gnuviechadmin.exceptions import *
|
|
|
|
|
2007-07-05 11:00:34 +02:00
|
|
|
from record import Record
|
|
|
|
import datetime
|
2007-07-02 11:14:47 +02:00
|
|
|
from BackendEntity import *
|
|
|
|
from BackendEntityHandler import *
|
2007-07-05 11:00:34 +02:00
|
|
|
from settings import config
|
2007-02-12 22:39:14 +01:00
|
|
|
|
2007-07-02 11:14:47 +02:00
|
|
|
class Domain(BackendEntity):
|
|
|
|
"""Entity class for DNS domains."""
|
|
|
|
|
|
|
|
_shortkeys = ("domainid", "sysuserid", "name", "type")
|
2007-07-05 11:00:34 +02:00
|
|
|
_valid_domain_types = ("MASTER", "SLAVE")
|
2007-07-02 11:14:47 +02:00
|
|
|
|
|
|
|
def __init__(self, verbose = False, **kwargs):
|
|
|
|
BackendEntity.__init__(self, verbose)
|
|
|
|
self.domainid = None
|
|
|
|
self.sysuserid = None
|
|
|
|
self.name = None
|
|
|
|
self.type = None
|
2007-07-05 11:00:34 +02:00
|
|
|
self.master = None
|
|
|
|
self.ns1 = None
|
|
|
|
self.ns2 = None
|
|
|
|
self.mx = None
|
|
|
|
self.ipaddr = None
|
|
|
|
for (key, value) in kwargs.items():
|
|
|
|
self.__setattr__(key, value)
|
|
|
|
if not self.type:
|
|
|
|
self.type = self.getdefaultdomaintype()
|
|
|
|
if not self.ns1:
|
|
|
|
self.ns1 = config.get('domain', 'defaultns1')
|
|
|
|
if not self.ns2:
|
|
|
|
self.ns2 = config.get('domain', 'defaultns2')
|
|
|
|
if not self.mx:
|
|
|
|
self.mx = config.get('domain', 'defaultmx')
|
|
|
|
if not self.ipaddr:
|
|
|
|
self.ipaddr = config.get('domain', 'defaultip')
|
|
|
|
self.type = self.type.upper()
|
2007-07-02 11:14:47 +02:00
|
|
|
self.validate()
|
|
|
|
|
2007-07-05 11:00:34 +02:00
|
|
|
def getdefaultdomaintype(self):
|
|
|
|
return self._valid_domain_types[0]
|
|
|
|
|
|
|
|
def validate(self):
|
|
|
|
BackendEntity.validate(self)
|
|
|
|
if not self.type in self._valid_domain_types:
|
|
|
|
raise ValidationFailedError(
|
|
|
|
self, "invalid domain type %s" % (self.type))
|
|
|
|
if self.type == 'SLAVE' and not self.master:
|
|
|
|
raise ValidationFailedError(
|
|
|
|
self, "you have to specify a master for slave domains.")
|
|
|
|
if self.type == 'MASTER':
|
|
|
|
if not self.ns1 or not self.ns2:
|
|
|
|
raise ValidationFailedError(
|
|
|
|
self, "two nameservers must be specified.")
|
|
|
|
if not self.mx:
|
|
|
|
raise ValidationFailedError(
|
|
|
|
self, "a primary mx host must be specified.")
|
|
|
|
|
|
|
|
def _getnewserial(self):
|
|
|
|
current = datetime.datetime.now()
|
|
|
|
return int("%04d%02d%02d01" % \
|
|
|
|
(current.year, current.month, current.day))
|
|
|
|
|
|
|
|
def _getnewsoa(self):
|
|
|
|
return '%s %s %d %d %d %d %d' % \
|
|
|
|
(self.ns1,
|
|
|
|
config.get('domain', 'defaulthostmaster'),
|
|
|
|
self._getnewserial(),
|
|
|
|
config.getint('domain', 'defaultrefresh'),
|
|
|
|
config.getint('domain', 'defaultretry'),
|
|
|
|
config.getint('domain', 'defaultexpire'),
|
|
|
|
config.getint('domain', 'defaultminimumttl'))
|
|
|
|
|
|
|
|
def create_hook(self):
|
|
|
|
self.records.append(Record(
|
|
|
|
name = self.name, type = 'SOA',
|
|
|
|
content = self._getnewsoa(),
|
|
|
|
ttl = config.getint('domain', 'defaultttl')))
|
|
|
|
self.records.append(Record(
|
|
|
|
name = self.name, type = 'NS', content = self.ns1,
|
|
|
|
ttl = config.getint('domain', 'defaultttl')))
|
|
|
|
self.records.append(Record(
|
|
|
|
name = self.name, type = 'NS', content = self.ns2,
|
|
|
|
ttl = config.getint('domain', 'defaultttl')))
|
|
|
|
self.records.append(Record(
|
|
|
|
name = self.name, type = 'MX', content = self.mx,
|
|
|
|
ttl = config.getint('domain', 'defaultttl'),
|
|
|
|
prio = config.getint('domain', 'defaultmxprio')))
|
|
|
|
self.records.append(Record(
|
|
|
|
name = self.name, type = 'A', content = self.ipaddr,
|
|
|
|
ttl = config.getint('domain', 'defaultttl')))
|
|
|
|
self.records.append(Record(
|
|
|
|
name = "www.%s" % (self.name), type = 'A', content = self.ipaddr,
|
|
|
|
ttl = config.getint('domain', 'defaultttl')))
|
|
|
|
|
|
|
|
def delete_hook(self):
|
|
|
|
pass
|
|
|
|
|
2007-07-02 11:14:47 +02:00
|
|
|
domain_mapper = mapper(Domain, domain_table)
|
2007-07-05 11:00:34 +02:00
|
|
|
domain_mapper.add_property("records", relation(Record))
|
2007-07-02 11:14:47 +02:00
|
|
|
|
|
|
|
class DomainHandler(BackendEntityHandler):
|
|
|
|
"""BackendEntityHandler for Domain entities."""
|
|
|
|
|
|
|
|
def __init__(self, verbose = False):
|
|
|
|
BackendEntityHandler.__init__(self, Domain, verbose)
|