# -*- 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. # # Version: $Id: client.py 1101 2007-02-28 21:15:20Z jan $ from sqlalchemy import * from tables import domain_table from gnuviechadmin.exceptions import * from record import Record import datetime from BackendEntity import * from BackendEntityHandler import * from settings import config class Domain(BackendEntity): """Entity class for DNS domains.""" _shortkeys = ("domainid", "sysuserid", "name", "type") _valid_domain_types = ("MASTER", "SLAVE") def __init__(self, verbose = False, **kwargs): BackendEntity.__init__(self, verbose) self.domainid = None self.sysuserid = None self.name = None self.type = None 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() self.validate() 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 domain_mapper = mapper(Domain, domain_table) domain_mapper.add_property("records", relation(Record)) class DomainHandler(BackendEntityHandler): """BackendEntityHandler for Domain entities.""" def __init__(self, verbose = False): BackendEntityHandler.__init__(self, Domain, verbose)