def getDns(self, domain = None): data = [] db = MailDatabase.getInstance() cursor = db.getCursor() if domain is None: query = ( 'SELECT dns_id, domain_id FROM dns' ) cursor.execute(query) else: query = ( 'SELECT dns_id, domain_id FROM dns WHERE domain_id = %s' ) cursor.execute(query, (domain,)) dnsIds = [] for (dns_id, domain_id) in cursor: dnsIds.append(dns_id) for i in dnsIds: dns = Dns(i) dns.load() data.append(dns.toDict()) del(dns) cursor.close() return data
def generateBindFile(self): dl = DomainList() content = [] content.append('$ORIGIN .') content.append('$TTL %is' % (self.ttl,)) # get soa entry. from modules.dns import Dns soa = Dns.getSoaForDomain(self.id) if soa is None: raise ValueError('Missing SOA-Entry. Cannot generatee Bind-File before!') return False for f in soa.generateDnsEntry(dl): content.append(f) # first add all entries, which have no key! for dns in Dns.getDnsForDomain(self.id): if len(dns.key.strip()) <= 0: content.extend(dns.generateDnsEntry(dl)) content.append('$ORIGIN %s.' % (self.getFullDomain(dl),)) # now the rest for dns in Dns.getDnsForDomain(self.id): if len(dns.key.strip()) > 0: content.extend(dns.generateDnsEntry(dl)) return '\n'.join(content)
def saveDns(self, domain, dns): from modules.domain import Domain dnsList = DNSList() for f in dns['_items']: dnsList.add(Dns.fromDict(f)) log.debug('Want to save %i dns items!' % (len(dnsList),)) for d in dnsList: d.save() if domain is not None and (type(domain) == int or len(domain.strip()) > 0) and conf.getboolean('dns', 'active'): content = self.getDomainZoneFile(domain) # now we need the Domain dom = Domain(domain) if not dom.load(): log.warning('Could not load the domain %s for generating zone file!' % (domain,)) return False # we need the fully qualified domain name! fqdn = dom.getFullDomain() # now try to save it! fileName = '%s.db' % (fqdn,) path = os.path.join(conf.get('dns', 'cache'), fileName) addToZoneFile = False if not os.path.exists(path): addToZoneFile = True try: with open(path, 'wb') as f: f.write(content.encode('utf-8')) except Exception as e: log.warning('Could not update the database file for the DNS-Service because of %s' % (str(e),)) else: log.info('Update the DNS-Service-Database %s' % (os.path.join(conf.get('dns', 'cache'), fileName),)) if addToZoneFile: self.__addZoneFile(path) # reload try: reloadDns() except Exception as e: log.critical('Could not reload the DNS-Service because of %s!' % (str(e),)) else: log.info('DNS-Service reloaded with success!') return True