Source

django-powerdns-manager / src / powerdns_manager / models.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# -*- 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.
#

from django.db import models
from django.db.models import signals
from django.utils.translation import ugettext_lazy as _
from django.db.models.loading import cache
from django.core.urlresolvers import reverse

from powerdns_manager import settings
from powerdns_manager import signal_cb
from powerdns_manager.utils import generate_serial
from powerdns_manager.utils import generate_serial_timestamp
from powerdns_manager.utils import generate_api_key



"""
http://doc.powerdns.com/generic-mypgsql-backends.html#idp9002432
http://wiki.powerdns.com/trac/wiki/fields
"""


class Domain(models.Model):
    """Model for PowerDNS domain."""
    
    DOMAIN_TYPE_CHOICES = (
        ('NATIVE', 'Native'),
        ('MASTER', 'Master'),
        ('SLAVE', 'Slave'),
        #('SUPERSLAVE', 'Superslave'),
    )
    name = models.CharField(max_length=255, unique=True, db_index=True, verbose_name=_('name'), help_text="""This field is the actual domainname. This is the field that powerDNS matches to when it gets a request. The domainname should be in the format of: domainname.TLD (no trailing dot)""")
    master = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('master'), help_text="""Enter a comma delimited list of nameservers that are master for this domain. This setting applies only to slave zones.""")
    last_check = models.PositiveIntegerField(max_length=11, null=True, verbose_name=_('last check'), help_text="""Last time this domain was checked for freshness.""")
    type = models.CharField(max_length=6, choices=DOMAIN_TYPE_CHOICES, default=settings.PDNS_DEFAULT_ZONE_TYPE, verbose_name=_('type'), help_text="""Select the zone type. Native refers to native SQL replication. Master/Slave refers to DNS server based zone transfers.""")
    notified_serial = models.PositiveIntegerField(max_length=11, null=True, verbose_name=_('notified serial'), help_text="""The last notified serial of a master domain. This is updated from the SOA record of the domain.""")
    account = models.CharField(max_length=40, blank=True, null=True, verbose_name=_('account'), help_text="""Determine if a certain host is a supermaster for a certain domain name. (???)""")

    # PowerDNS Manager internal fields
    #date_created = models.DateTimeField(auto_now_add=True, verbose_name=_('Created on'))
    date_modified = models.DateTimeField(auto_now=True, verbose_name=_('Last Modified'))
    created_by = models.ForeignKey('auth.User', related_name='%(app_label)s_%(class)s_created_by', null=True, verbose_name=_('created by'), help_text="""The Django user this zone belongs to.""")
    
    class Meta:
        db_table = 'domains'
        verbose_name = _('zone')
        verbose_name_plural = _('zones')
        get_latest_by = 'date_modified'
        ordering = ['name']

    def __unicode__(self):
        return self.name
    
#    def save(self, *args, **kwargs):
#        """Saves the instance to the database.
#        
#        The serial of the zone is updated.
#        
#        """
#        self.update_serial()
#        return super(Domain, self).save(*args, **kwargs)
    
    def get_minimum_ttl(self):
        """Returns the minimum TTL.
        
        The SOA record of the zone is retrieved and the minimum TTL is extracted
        from the ``content`` field.
        
        If a SOA record does not exist for this zone, PDNS_DEFAULT_RR_TTL
        is returned from the settings..
        
        """
        Record = cache.get_model('powerdns_manager', 'Record')
        try:
            soa_rr = Record.objects.get(domain=self, type='SOA')
        except Record.DoesNotExist:
            return settings.PDNS_DEFAULT_RR_TTL
        else:
            return soa_rr.content.split()[-1]
    
    def set_minimum_ttl(self, new_minimum_ttl):
        """Sets the minimum TTL.
        
        Accepts ``new_minimum_ttl`` (integer)
        
        SOA content:  primary hostmaster serial refresh retry expire default_ttl
        
        Important
        ---------
        Make sure this is not called while the SOA record object is modified.
        
        TODO: Investigate whether it is needed to perform any checks against settings.PDNS_DEFAULT_RR_TTL
        
        """
        Record = cache.get_model('powerdns_manager', 'Record')
        try:
            soa_rr = Record.objects.get(domain=self, type='SOA')
        except Record.DoesNotExist:
            raise Exception('SOA Resource Record does not exist.')
        else:
            bits = soa_rr.content.split()
            bits[6] = new_minimum_ttl
            soa_rr.content = ' '.join(bits)
            soa_rr.save()
    
    def update_serial(self):
        """Updates the serial of the zone (SOA record).
        
        SOA content:  primary hostmaster serial refresh retry expire default_ttl
        
        """
        Record = cache.get_model('powerdns_manager', 'Record')
        try:
            soa_rr = Record.objects.get(domain=self, type='SOA')
        except Record.DoesNotExist:
            raise Exception('SOA Resource Record does not exist.')
        else:
            bits = soa_rr.content.split()
            bits[2] = str(generate_serial(serial_old=bits[2]))
            soa_rr.content = ' '.join(bits)
            soa_rr.save()
    
    def export_zone_html_link(self):
        html_link = '<a href="%s"><strong>export zone</strong></a>' % reverse('export_zone', kwargs={'origin': self.name})
        return html_link
    export_zone_html_link.allow_tags = True
    export_zone_html_link.short_description = 'Export'

signal_cb.zone_saved.connect(signal_cb.rectify_zone_cb, sender=Domain)



class Record(models.Model):
    """Model for PowerDNS resource record.
    
    Supported record types:
    
        http://doc.powerdns.com/types.html
    
    Rules for filling in AUTH and ORDERNAME fields:
        
        http://doc.powerdns.com/dnssec-modes.html#dnssec-direct-database
    
    """
    # Build resource record choices list
    RECORD_TYPE_CHOICES = []
    for enabled_rr_type in settings.PDNS_ENABLED_RR_TYPES:
        RECORD_TYPE_CHOICES.append( (enabled_rr_type, enabled_rr_type) )

    domain = models.ForeignKey('powerdns_manager.Domain', related_name='%(app_label)s_%(class)s_domain', verbose_name=_('domain'), help_text=_("""Select the domain this record belongs to."""))
    name = models.CharField(max_length=255, null=True, db_index=True, verbose_name=_('name'), help_text="""Actual name of a record. Must not end in a '.' and be fully qualified - it is not relative to the name of the domain!  For example: www.test.com (no trailing dot)""")
    # See section 8.5 about why the type field allows NULL. (PowerDNS 3.2 and above)
    type = models.CharField(max_length=10, null=True, db_index=True, choices=RECORD_TYPE_CHOICES, verbose_name=_('type'), help_text="""Select the type of the resource record.""")
    content = models.CharField(max_length=255, null=True, verbose_name=_('content'), help_text="""This is the 'right hand side' of a DNS record. For an A record, this is the IP address for example.""")
    ttl = models.PositiveIntegerField(max_length=11, blank=True, null=True, verbose_name=_('TTL'), help_text="""How long the DNS-client are allowed to remember this record. Also known as Time To Live(TTL) This value is in seconds.""")
    prio = models.PositiveIntegerField(max_length=11, null=True, verbose_name=_('priority'), help_text="""For MX records, this should be the priority of the mail exchanger specified.""")
    # Extra fields for DNSSEC (http://doc.powerdns.com/dnssec-modes.html#dnssec-direct-database)
    auth = models.NullBooleanField(verbose_name=_('authoritative'), help_text="""The 'auth' field should be set to '1' for data for which the zone itself is authoritative, which includes the SOA record and its own NS records. The 'auth' field should be 0 however for NS records which are used for delegation, and also for any glue (A, AAAA) records present for this purpose. Do note that the DS record for a secure delegation should be authoritative!""")
    ordername = models.CharField(max_length=255, null=True, db_index=True, verbose_name=_('ordername'), help_text="""http://doc.powerdns.com/dnssec-modes.html#dnssec-direct-database""")
    
    # This is set to the current timestamp on every save
    change_date = models.PositiveIntegerField(max_length=11, null=True, verbose_name=_('change date'), help_text="""Timestamp for the last update. This is used by PowerDNS internally.""")

    # PowerDNS Manager internal fields
    date_modified = models.DateTimeField(auto_now=True, verbose_name=_('Last Modified'))

    class Meta:
        db_table = 'records'
        verbose_name = _('record')
        verbose_name_plural = _('records')
        get_latest_by = 'date_modified'
        ordering = ['type']
        # The following index is created by a MySQL statement in ``sql/record.mysql.sql``
        #     CREATE INDEX nametype_index ON records(name,type);
        # SEE: http://stackoverflow.com/questions/1578195/django-create-index-non-unique-multiple-column
        
    def __unicode__(self):
        #return '%s %s' % (self.type, self.name)
        return self.name
    
    def save(self, *args, **kwargs):
        """Saves the instance to the database.
        
        The following actions are performed:
        
        1) Sets the current timestamp to the ``change_date`` field. This is
        used by PowerDNS.
        
        2) Sets the TTL of the resource record(s), if missing. Since
        ``get_minimum_ttl()`` retrieves the minimum TTL from the SOA record,
        if a SOA record has not been saved yet, then PDNS_DEFAULT_RR_TTL will
        be used instead.
        
        3) Set the ``auth`` field. Needed by PowerDNS internals.
        
        4) Set the ``ordername`` field. Needed by PowerDNS internals.
        
        """
        self.change_date = generate_serial_timestamp()
        
        if not self.ttl:
            self.ttl = self.domain.get_minimum_ttl()
        
        # auth and ordername fields are set automatically after the zone and
        # all records have been saved. See: admin.DomainAdmin.save_related()
        
        return super(Record, self).save(*args, **kwargs)



class SuperMaster(models.Model):
    """Model for PowerDNS supermasters.
    
    More on supermasters: http://doc.powerdns.com/slave.html#supermaster
    
    PDNS can recognize so called 'supermasters'. A supermaster is a host which
    is master for domains and for which we are to be a slave. When a master
    (re)loads a domain, it sends out a notification to its slaves. Normally,
    such a notification is only accepted if PDNS already knows that it is a
    slave for a domain.
    
    SuperMaster records should be added on the secondary DNS servers (slaves).
    This table contains known and trusted master servers.
    
    A supermaster is a host which is master for domains and for which we are to be a slave.  ref  ref2
    This table is an authorization table for the IP/nameserver. 

    """
    ip = models.GenericIPAddressField(verbose_name=_('IP address'), help_text="""IP address for supermaster (IPv4 or IPv6).""")
    # TODO: added unique --- check if OK
    nameserver = models.CharField(max_length=255, unique=True, verbose_name=_('nameserver'), help_text="""Hostname of the supermaster.""")
    account = models.CharField(max_length=40, blank=True, null=True, verbose_name=_('account'), help_text="""Account name (???)""")

    # PowerDNS Manager internal fields
    date_modified = models.DateTimeField(auto_now=True, verbose_name=_('Last Modified'))

    class Meta:
        db_table = 'supermasters'
        verbose_name = _('supermaster')
        verbose_name_plural = _('supermasters')
        get_latest_by = 'date_modified'
        ordering = ['nameserver']
        #unique_together = (
        #    ('ip', 'nameserver'),   # This is custom addition. check if causes problems
        #    #('nameserver', 'account'),    # TODO: check what account really is. maybe we need to add a created_by field here.
        #)

    def __unicode__(self):
        return self.nameserver


class DomainMetadata(models.Model):
    """Model for PowerDNS domain metadata.
    
    More on domain metadata: http://doc.powerdns.com/domainmetadata.html
    
    """
    PER_ZONE_METADATA_CHOICES = (
        ('ALLOW-AXFR-FROM', 'ALLOW-AXFR-FROM'),
        ('AXFR-MASTER-TSIG', 'AXFR-MASTER-TSIG'),
        ('LUA-AXFR-SCRIPT', 'LUA-AXFR-SCRIPT'),
        ('NSEC3NARROW', 'NSEC3NARROW'),
        ('NSEC3PARAM', 'NSEC3PARAM'),
        ('PRESIGNED', 'PRESIGNED'),
        ('SOA-EDIT', 'SOA-EDIT'),
        ('TSIG-ALLOW-AXFR', 'TSIG-ALLOW-AXFR'),
    )
    domain = models.ForeignKey('powerdns_manager.Domain', related_name='%(app_label)s_%(class)s_domain', verbose_name=_('domain'), help_text=_("""Select the domain this record belongs to."""))
    kind = models.CharField(max_length=16, choices=PER_ZONE_METADATA_CHOICES, verbose_name=_('setting'), help_text="""Select a setting.""")
    content = models.TextField(blank=True, null=True, verbose_name=_('content'), help_text="""Enter the metadata.""")
    
    # PowerDNS Manager internal fields
    date_modified = models.DateTimeField(auto_now=True, verbose_name=_('Last Modified'))

    class Meta:
        db_table = 'domainmetadata'
        verbose_name = _('domain metadata')
        verbose_name_plural = _('domain metadata')
        get_latest_by = 'date_modified'
        ordering = ['kind']
        
    def __unicode__(self):
        return self.kind



class CryptoKey(models.Model):
    """Model for PowerDNS domain crypto keys.
    
    See: http://doc.powerdns.com/dnssec-supported.html
    
    """
    domain = models.ForeignKey('powerdns_manager.Domain', related_name='%(app_label)s_%(class)s_domain', verbose_name=_('domain'), help_text=_("""Select the domain this record belongs to."""))
    flags = models.PositiveIntegerField(verbose_name=_('flags'), help_text="""Key flags.""")
    active = models.BooleanField(verbose_name=_('active'), help_text="""Check to activate key.""")
    # TODO: Check if content may be empty
    content = models.TextField(blank=True, null=True, verbose_name=_('content'), help_text="""Enter the key data.""")
    
    # PowerDNS Manager internal fields
    date_modified = models.DateTimeField(auto_now=True, verbose_name=_('Last Modified'))

    class Meta:
        db_table = 'cryptokeys'
        verbose_name = _('crypto key')
        verbose_name_plural = _('crypto keys')
        get_latest_by = 'date_modified'
        ordering = ['domain']
        
    def __unicode__(self):
        return self.domain.name



class TsigKey(models.Model):
    """Model for PowerDNS domain TSIG keys.
    
    See: http://doc.powerdns.com/tsig.html
    
    """
    ALGORITHM_CHOICES = (
        ('hmac-md5', 'hmac-md5'),
        # TODO: check for more
    )
    name = models.CharField(max_length=255, verbose_name=_('name'), help_text="""Enter a name for the key.""")
    algorithm = models.CharField(max_length=50, choices=ALGORITHM_CHOICES, verbose_name=_('algorithm'), help_text="""Select hashing algorithm.""")
    secret = models.CharField(max_length=255, verbose_name=_('secret'), help_text="""Enter the shared secret.""")

    # PowerDNS Manager internal fields
    date_modified = models.DateTimeField(auto_now=True, verbose_name=_('Last Modified'))
    created_by = models.ForeignKey('auth.User', related_name='%(app_label)s_%(class)s_created_by', null=True, verbose_name=_('created by'), help_text="""The Django user this TSIG key belongs to.""")


    class Meta:
        db_table = 'tsigkeys'
        verbose_name = _('TSIG Key')
        verbose_name_plural = _('TSIG Keys')
        get_latest_by = 'date_modified'
        ordering = ['name']
        unique_together = (
            ('name', 'algorithm'),
        )
        
    def __unicode__(self):
        return self.name



class DynamicZone(models.Model):
    """Model for Dynamic Zones.
    
    This is a PowerDNS Manager feature to support updating, the A and AAAA
    resource records of a zone over HTTP.
    
    This feature can be used to easily update the IP of hosts whose IP
    address is dynamic.
    
    """
    domain = models.ForeignKey('powerdns_manager.Domain', unique=True, related_name='%(app_label)s_%(class)s_domain', verbose_name=_('domain'), help_text=_("""Select the domain, the A and AAAA records of which might be updated dynamically over HTTP."""))
    is_dynamic = models.BooleanField(verbose_name=_('Dynamic zone'), help_text="""Check to mark this zone as dynamic. An API key will be generated for you so as to be able to update the A nd AAAA records IP addresses over HTTP.""")
    api_key = models.CharField(max_length=24, null=True, verbose_name=_('API Key'), help_text="""The API key is generated automatically. To reset it, use the relevant action in the changelist view.""")
    date_modified = models.DateTimeField(auto_now=True, verbose_name=_('Last Modified'))
    
    class Meta:
        db_table = 'dynamiczones'
        verbose_name = _('dynamic zone')
        verbose_name_plural = _('dynamic zones')
        get_latest_by = 'date_modified'
        ordering = ['-domain']
        
    def __unicode__(self):
        return self.domain.name
    
    def save(self, *args, **kwargs):
        """Saves the instance to the database.
        
        If ``is_dynamic`` has been enabled and if ``api_key`` is empty,
        then generate a new API key. If ``api_key`` is not empty, do nothing.
        
        If ``is_dynamic`` is not enabled, always set ``api_key`` to NULL.
        
        """
        if self.is_dynamic:
            if not self.api_key:
                self.api_key = generate_api_key()
        else:
            self.api_key = None
        
        return super(DynamicZone, self).save(*args, **kwargs)