# -*- coding: utf-8 -*-## This file is part of django-powerdns-manager.## django-powerdns-manager is a web based PowerDNS administration panel.## Development Web Site:# - http://www.codetrax.org/projects/django-powerdns-manager# Public Source Code Repository:# - https://source.codetrax.org/hgroot/django-powerdns-manager## Copyright 2012 George Notaras <gnot [at] g-loaded.eu>## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#importtimeimportrefromdjangoimportformsfromdjango.db.models.loadingimportcachefromdjango.utils.translationimportugettext_lazyas_fromdjango.core.validatorsimportvalidate_ipv4_addressfromdjango.core.validatorsimportvalidate_ipv6_addressfromdjango.core.exceptionsimportValidationErrorfrompowerdns_managerimportsettingsfrompowerdns_manager.utilsimportvalidate_hostnameclassBaseRecordModelForm(forms.ModelForm):"""Base ModelForm for Record instances. """classMeta:model=cache.get_model('powerdns_manager','Record')defclean(self):"""ModelForm clean code for all Record ModelForms. 1) Makes sure the RR's name is within the current zone. For instance, if the zone origin is 'centos.example.org', the user will not be able to add records with a name 'example.org'. Such a name would belong to the parent zone and is bogus information if added to the current zone. """# Check 1: Makes sure the RR's name is within the current zone.# This ensures that we do not catch a SOA record, for which the name# is added in the SoaRecordModelForm.save(). A SOA record will never# have a bogus name, since it is not user-editable. ifnotself.cleaned_data.has_key('name'):returnself.cleaned_dataname=self.cleaned_data.get('name')domain=self.cleaned_data.get('domain')ifnameanddomain:iflen(name.split('.'))<len(domain.name.split('.')):msg='Invalid record name. This name belongs to a parent zone.'self._errors["name"]=self.error_class([msg])returnself.cleaned_dataclassSoaRecordModelForm(BaseRecordModelForm):"""ModelForm for SOA resource records. By default, PowerDNS expects the content of the SOA records to contain the following information: primary hostmaster serial refresh retry expire default_ttl This is too inconvenient to edit. See: http://doc.powerdns.com/types.html#soa-type To deal with this problem we add separate form fields to the ModelForm for each one of the pieces of data that form the SOA RR content above. When the form is initialized, these fields get their initial values from the SOA RR content (see __init__() below). When the SOA RR is saved, the values of these extra fields are concatenated in save() to form the SOA RR content that PowerDNS expects to find stored in the database. Also, if TTl information is missing, then the default TTL (mandatory field) is used. """primary=forms.CharField(max_length=96,initial='',required=True,label=_('primary nameserver'),help_text="""The name of the name server that was the original or primary source of data for this zone.""")hostmaster=forms.CharField(max_length=64,initial='',required=True,label=_('hostmaster mailbox'),help_text="""A name which specifies the mailbox of the person responsible for this zone. This should be specified in the mailbox-as-domain-name format where the `@' character is replaced with a dot. Example: hostmaster.domain.tld represents hostmaster@domain.tld""")serial=forms.IntegerField(min_value=1,initial=1,required=True,label=_('serial'),widget=forms.TextInput(attrs={'readonly':'readonly'}),help_text="""The serial is generated automatically and is not user-editable. The serial is a "version number" for this zone. DNS servers that rely on AXFR for zone transfers use this to determine when updates have occurred. Popular values to use are the Unix timestamp or a date in the form YYYYMMDD.""")refresh=forms.IntegerField(min_value=300,initial=10800,required=True,label=_('refresh'),help_text="""The number of seconds after which slave nameservers should check to see if this zone has been changed. If the zone's serial number has changed, the slave nameserver initiates a zone transfer. Example: 28800""")retry=forms.IntegerField(min_value=300,initial=3600,required=True,label=_('retry'),help_text="""This specifies the number of seconds a slave nameserver should wait before retrying if it attmepts to transfer this zone but fails. Example: 7200""")expire=forms.IntegerField(min_value=300,initial=604800,required=True,label=_('expire'),help_text="""If for expire seconds the primary server cannot be reached, all information about the zone is invalidated on the secondary servers (i.e., they are no longer authoritative for that zone). Example: 604800""")default_ttl=forms.IntegerField(min_value=300,initial=3600,required=True,label=_('minimum TTL'),help_text="""The minimum TTL field that should be exported with any RR from this zone. If any RR in the database has a lower TTL, this TTL is sent instead. Example: 86400""")def__init__(self,*args,**kwargs):"""ModelForm constructor. If the user edits an existing SOA RR through the InlineModelAdmin, the following code reads the existing content of the ``Record.content`` field, splits the information into pieces, and fills the initial data of the extra fields we have added to the form. See: http://doc.powerdns.com/types.html#soa-type """ifkwargs.has_key('instance'):instance=kwargs['instance']ifinstance.pkisnotNone:# This check asserts that this is an EDITifinstance.type=='SOA':bits=instance.content.split()kwargs['initial']={'primary':bits[0],'hostmaster':bits[1],'serial':bits[2],'refresh':bits[3],'retry':bits[4],'expire':bits[5],'default_ttl':bits[6],}super(SoaRecordModelForm,self).__init__(*args,**kwargs)defclean_primary(self):primary=self.cleaned_data.get('primary')validate_hostname(primary)returnprimarydefclean_hostmaster(self):hostmaster=self.cleaned_data.get('hostmaster')ifhostmaster.find('@')!=-1:raiseforms.ValidationError("""This should be specified in the mailbox-as-domain-name format where the `@' character is replaced with a dot. Example: hostmaster.domain.tld represents hostmaster@domain.tld""")# treat the hostmaster as a hostname and use the hostname validator.validate_hostname(hostmaster)returnhostmasterdefsave(self,*args,**kwargs):"""Saves the SOA RR ModelForm. Model fields that were not editable in the admin interface are set here. 1) The SOA type is set. 2) The values of these extra SOA-specific fields are concatenated in order to form the SOA RR content that PowerDNS expects to find stored in the database. 3) The TTL field, if missing, is set equal to the ``default_ttl`` form field. 4) Sets the ``name`` field of the SOA record equal to the name of the associated domain. PowerDNS Manager allows only one SOA RR per zone. The ``name`` field of the SOA record is not editable in the ModelAdmin. """self.instance.type='SOA'self.instance.content='%s%s%d%s%s%s%s'%(self.cleaned_data.get('primary'),self.cleaned_data.get('hostmaster'),int(time.time()),self.cleaned_data.get('refresh'),self.cleaned_data.get('retry'),self.cleaned_data.get('expire'),self.cleaned_data.get('default_ttl'))ifnotself.instance.ttl:self.instance.ttl=self.cleaned_data.get('default_ttl')domain=self.cleaned_data.get('domain')self.instance.name=domain.namereturnsuper(SoaRecordModelForm,self).save(*args,**kwargs)#class SoaRecordInlineModelFormset(BaseInlineFormSet):# """Inline formset for SOA resource records.# # Here we set the prefix ``soa`` for the formset that contain SOA records.# This is because there are two inlines (SoaRecordInline, RecordInline)# based on the same model (Record).# # This model form is used in the SoaRecordInline, which facilitates editing# the SOA resource record of the zone.# # """# model = cache.get_model('powerdns_manager', 'Record')# # @classmethod# def get_default_prefix(cls):# default_prefix = super(SoaRecordInlineModelFormset, cls).get_default_prefix()# return 'soa-%s' % default_prefixclassNsRecordModelForm(BaseRecordModelForm):"""ModelForm for NS resource records."""defclean_name(self):name=self.cleaned_data.get('name')validate_hostname(name)returnnamedefclean_content(self):content=self.cleaned_data.get('content')validate_hostname(content)returncontentdefsave(self,*args,**kwargs):self.instance.type='NS'returnsuper(NsRecordModelForm,self).save(*args,**kwargs)classMxRecordModelForm(BaseRecordModelForm):"""ModelForm for MX resource records."""defclean_name(self):name=self.cleaned_data.get('name')validate_hostname(name,supports_wildcard=True)returnnamedefclean_content(self):content=self.cleaned_data.get('content')validate_hostname(content)returncontentdefsave(self,*args,**kwargs):self.instance.type='MX'returnsuper(MxRecordModelForm,self).save(*args,**kwargs)classSrvRecordModelForm(BaseRecordModelForm):"""ModelForm for SRV resource records. For details see docstrings in SoaRecordModelForm. """weight=forms.IntegerField(min_value=1,required=True,label=_('weight'),widget=forms.TextInput(attrs={'size':'3'}),help_text="""A relative weight for records with the same priority.""")port=forms.IntegerField(min_value=1,max_value=65535,required=True,label=_('port'),widget=forms.TextInput(attrs={'size':'5'}),help_text="""The TCP or UDP port on which the service is to be found.""")target=forms.CharField(max_length=128,required=True,label=_('target'),widget=forms.TextInput(attrs={'size':'25'}),help_text="""The canonical hostname of the machine providing the service (no trailing dot).""")def__init__(self,*args,**kwargs):"""Set initial values for extra ModelForm fields."""ifkwargs.has_key('instance'):instance=kwargs['instance']ifinstance.pkisnotNone:# This check asserts that this is an EDITifinstance.type=='SRV':bits=instance.content.split()kwargs['initial']={'weight':bits[0],'port':bits[1],'target':bits[2],}super(SrvRecordModelForm,self).__init__(*args,**kwargs)defclean_name(self):name=self.cleaned_data.get('name')validate_hostname(name)returnnamedefclean_target(self):target=self.cleaned_data.get('target')validate_hostname(target)returntargetdefsave(self,*args,**kwargs):self.instance.type='SRV'self.instance.content='%d%d%s'%(self.cleaned_data.get('weight'),self.cleaned_data.get('port'),self.cleaned_data.get('target'),)returnsuper(SrvRecordModelForm,self).save(*args,**kwargs)classARecordModelForm(BaseRecordModelForm):"""ModelForm for A resource records."""defclean_name(self):name=self.cleaned_data.get('name')validate_hostname(name,supports_wildcard=True)returnnamedefclean_content(self):"""Ensures that content is an IPv4 address."""content=self.cleaned_data.get('content')try:validate_ipv4_address(content)exceptValidationError:raiseforms.ValidationError("""Content should contain an IPv4 address""")else:returncontentdefsave(self,*args,**kwargs):self.instance.type='A'returnsuper(ARecordModelForm,self).save(*args,**kwargs)classAaaaRecordModelForm(BaseRecordModelForm):"""ModelForm for AAAA resource records."""defclean_content(self):"""Ensures that content is an IPv6 address."""content=self.cleaned_data.get('content')try:validate_ipv6_address(content)exceptValidationError:raiseforms.ValidationError("""Content should contain an IPv6 address""")else:returncontentdefsave(self,*args,**kwargs):self.instance.type='AAAA'returnsuper(AaaaRecordModelForm,self).save(*args,**kwargs)classCnameRecordModelForm(BaseRecordModelForm):"""ModelForm for CNAME resource records."""defsave(self,*args,**kwargs):self.instance.type='CNAME'returnsuper(CnameRecordModelForm,self).save(*args,**kwargs)classPtrRecordModelForm(BaseRecordModelForm):"""ModelForm for PTR resource records."""defsave(self,*args,**kwargs):self.instance.type='PTR'returnsuper(PtrRecordModelForm,self).save(*args,**kwargs)classTxtRecordModelForm(BaseRecordModelForm):"""ModelForm for TXT resource records."""defsave(self,*args,**kwargs):self.instance.type='TXT'returnsuper(TxtRecordModelForm,self).save(*args,**kwargs)classDsRecordModelForm(BaseRecordModelForm):"""ModelForm for DS resource records."""defsave(self,*args,**kwargs):self.instance.type='DS'returnsuper(DsRecordModelForm,self).save(*args,**kwargs)classCertRecordModelForm(BaseRecordModelForm):"""ModelForm for CERT resource records."""defsave(self,*args,**kwargs):self.instance.type='CERT'returnsuper(CertRecordModelForm,self).save(*args,**kwargs)classHinfoRecordModelForm(BaseRecordModelForm):"""ModelForm for HINFO resource records."""defsave(self,*args,**kwargs):self.instance.type='HINFO'returnsuper(HinfoRecordModelForm,self).save(*args,**kwargs)classLocRecordModelForm(BaseRecordModelForm):"""ModelForm for LOC resource records."""defsave(self,*args,**kwargs):self.instance.type='LOC'returnsuper(LocRecordModelForm,self).save(*args,**kwargs)classSpfRecordModelForm(BaseRecordModelForm):"""ModelForm for SPF resource records."""defsave(self,*args,**kwargs):self.instance.type='SPF'returnsuper(SpfRecordModelForm,self).save(*args,**kwargs)classSshfpRecordModelForm(BaseRecordModelForm):"""ModelForm for SSHFP resource records."""defsave(self,*args,**kwargs):self.instance.type='SSHFP'returnsuper(SshfpRecordModelForm,self).save(*args,**kwargs)classRpRecordModelForm(BaseRecordModelForm):"""ModelForm for RP resource records."""defsave(self,*args,**kwargs):self.instance.type='RP'returnsuper(RpRecordModelForm,self).save(*args,**kwargs)classNaptrRecordModelForm(BaseRecordModelForm):"""ModelForm for NAPTR resource records."""defsave(self,*args,**kwargs):self.instance.type='NAPTR'returnsuper(NaptrRecordModelForm,self).save(*args,**kwargs)classAfsdbRecordModelForm(BaseRecordModelForm):"""ModelForm for AFSDB resource records."""defsave(self,*args,**kwargs):self.instance.type='AFSDB'returnsuper(AfsdbRecordModelForm,self).save(*args,**kwargs)classDnskeyRecordModelForm(BaseRecordModelForm):"""ModelForm for DNSKEY resource records."""defsave(self,*args,**kwargs):self.instance.type='DNSKEY'returnsuper(DnskeyRecordModelForm,self).save(*args,**kwargs)classKeyRecordModelForm(BaseRecordModelForm):"""ModelForm for KEY resource records."""defsave(self,*args,**kwargs):self.instance.type='KEY'returnsuper(KeyRecordModelForm,self).save(*args,**kwargs)classNsecRecordModelForm(BaseRecordModelForm):"""ModelForm for NSEC resource records."""defsave(self,*args,**kwargs):self.instance.type='NSEC'returnsuper(NsecRecordModelForm,self).save(*args,**kwargs)classRrsigRecordModelForm(BaseRecordModelForm):"""ModelForm for RRSIG resource records."""defsave(self,*args,**kwargs):self.instance.type='RRSIG'returnsuper(RrsigRecordModelForm,self).save(*args,**kwargs)classZoneImportForm(forms.Form):"""This form is used to import zone files through the ``import_zone_view``. """origin=forms.CharField(max_length=128,initial='',required=False,label=_('Origin'),help_text="""Enter the origin, otherwise make sure this information is available in your zone file either by the $ORIGIN directive or by using an FQDN in the name field of each record. (optional)""")zonetext=forms.CharField(widget=forms.Textarea,initial='',required=True,label=_('Zone file'),help_text="""Paste the zone file text. (required)""")overwrite=forms.BooleanField(required=False,label=_('Overwrite'),help_text="""If checked, existing zone will be replaced by this one. Proceed with caution.""")classAxfrImportForm(forms.Form):"""This form is used to import zone data from AXFR responses through the ``import_axfr_view``. """origin=forms.CharField(max_length=128,initial='',required=True,label=_('Origin'),help_text="""Enter the domain name to import.""")nameserver=forms.GenericIPAddressField(protocol='both',required=True,label=_('Nameserver IP'),help_text="""Enter the IP address (IPv4 or IPv6 of the nameserver that contains zone information.""")overwrite=forms.BooleanField(required=False,label=_('Overwrite'),help_text="""If checked, existing zone will be replaced by this one. Proceed with caution.""")classDynamicIPUpdateForm(forms.Form):"""This form is used to validate the supplied data in the POST request. """api_key=forms.CharField(max_length=24,required=True)hostname=forms.CharField(max_length=128,required=False)ipv4=forms.GenericIPAddressField(protocol='IPv4',required=False)ipv6=forms.GenericIPAddressField(protocol='IPv6',required=False)defclean_api_key(self):"""Checks the provided API key. 1) The key must contain [A-Z0-9] 2) A dynamic zone must be configured with the supplied key """api_key=self.cleaned_data.get('api_key')ifnotre.match('^[A-Z0-9]+$',api_key):raiseforms.ValidationError('Invalid API key')DynamicZone=cache.get_model('powerdns_manager','DynamicZone')try:DynamicZone.objects.get(api_key__exact=api_key)exceptDynamicZone.DoesNotExist:raiseforms.ValidationError('Invalid API key')else:returnapi_keydefclean_hostname(self):"""Checks the provided hostname. Hostname may be empty. Performs sanity checks. """hostname=self.cleaned_data.get('hostname')ifnothostname:returnhostnameifnotre.match('^[A-Za-z0-9._\-]+$',hostname):raiseforms.ValidationError('Invalid hostname')returnhostnameclassZoneTypeSelectionForm(forms.Form):"""This form is used in intermediate page that sets the zone type in bulk."""frompowerdns_manager.modelsimportDomaindomaintype=forms.ChoiceField(choices=Domain.DOMAIN_TYPE_CHOICES,required=True,label=_('Zone type'),help_text="""Select the zone type. Native refers to native SQL replication. Master/Slave refers to DNS server based zone transfers.""")classTtlSelectionForm(forms.Form):"""This form is used in intermediate page that sets the RR TTL in bulk."""new_ttl=forms.IntegerField(min_value=settings.PDNS_DEFAULT_RR_TTL,required=True,label=_('New TTL'),help_text="""Enter the new Time-To-Live (TTL) in seconds.""")reset_zone_minimum=forms.BooleanField(required=False,label=_('Reset minimum TTL of the zones?'),help_text="""If checked, the minimum TTL of the selected zones will be reset to the new TTL value.""")