2013-09-28 10:44:29 +02:00
|
|
|
from django.db import models
|
2013-09-29 14:36:29 +02:00
|
|
|
from django.contrib.auth.models import User
|
2013-09-29 14:08:22 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2013-09-29 13:36:28 +02:00
|
|
|
from django.core.validators import RegexValidator
|
2013-09-29 13:53:35 +02:00
|
|
|
from django.conf import settings
|
2013-09-29 20:30:10 +02:00
|
|
|
from django.db.models.signals import post_delete
|
2013-09-29 19:58:08 +02:00
|
|
|
from django.contrib.auth.hashers import make_password
|
2013-09-29 17:43:17 +02:00
|
|
|
from main import dnstools
|
2013-09-29 20:43:48 +02:00
|
|
|
import dns.resolver
|
2013-09-29 21:00:08 +02:00
|
|
|
from datetime import datetime
|
2013-09-28 18:02:13 +02:00
|
|
|
|
2013-09-29 14:08:22 +02:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
class BlacklistedDomain(models.Model):
|
2013-09-29 16:12:19 +02:00
|
|
|
domain = models.CharField(
|
|
|
|
max_length=256,
|
|
|
|
unique=True,
|
|
|
|
help_text='Blacklisted domain. Evaluated as regex (search).')
|
2013-09-29 14:08:22 +02:00
|
|
|
|
|
|
|
last_update = models.DateTimeField(auto_now=True)
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
2013-09-29 14:21:23 +02:00
|
|
|
created_by = models.ForeignKey(User, blank=True, null=True)
|
2013-09-29 14:08:22 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2013-09-29 15:26:28 +02:00
|
|
|
return u"%s" % (self.domain, )
|
2013-09-29 14:08:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
def domain_blacklist_validator(value):
|
|
|
|
for bd in BlacklistedDomain.objects.all():
|
|
|
|
if re.search(bd.domain, value):
|
|
|
|
raise ValidationError(u'This domain is not allowed')
|
|
|
|
|
2013-09-28 18:02:13 +02:00
|
|
|
|
2013-09-29 13:36:28 +02:00
|
|
|
class Domain(models.Model):
|
|
|
|
domain = models.CharField(max_length=256, unique=True)
|
|
|
|
|
|
|
|
last_update = models.DateTimeField(auto_now=True)
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
2013-09-29 16:38:30 +02:00
|
|
|
created_by = models.ForeignKey(User, blank=True, null=True)
|
2013-09-29 13:36:28 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2013-09-29 15:26:28 +02:00
|
|
|
return u"%s" % (self.domain, )
|
2013-09-29 13:36:28 +02:00
|
|
|
|
|
|
|
|
2013-09-28 18:02:13 +02:00
|
|
|
class Host(models.Model):
|
2013-09-29 13:36:28 +02:00
|
|
|
"""TODO: hash update_secret on save (if not already hashed)"""
|
2013-09-29 13:47:02 +02:00
|
|
|
subdomain = models.CharField(max_length=256, validators=[
|
2013-09-29 14:21:23 +02:00
|
|
|
RegexValidator(
|
|
|
|
regex=r'^(([a-z0-9][a-z0-9\-]*[a-z0-9])|[a-z0-9])$',
|
2013-09-29 16:12:19 +02:00
|
|
|
message='Invalid subdomain: only "a-z", "0-9" and "-" is allowed'
|
2013-09-29 14:21:23 +02:00
|
|
|
),
|
|
|
|
domain_blacklist_validator])
|
2013-09-29 13:36:28 +02:00
|
|
|
domain = models.ForeignKey(Domain)
|
2013-09-29 15:12:24 +02:00
|
|
|
update_secret = models.CharField(max_length=256) # gets hashed on save
|
2013-09-29 16:03:56 +02:00
|
|
|
comment = models.CharField(
|
|
|
|
max_length=256, default='', blank=True, null=True)
|
2013-09-28 18:02:13 +02:00
|
|
|
|
|
|
|
last_update = models.DateTimeField(auto_now=True)
|
2013-09-29 20:50:33 +02:00
|
|
|
last_api_update = models.DateTimeField(blank=True, null=True)
|
2013-09-28 18:02:13 +02:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
2013-09-29 16:07:33 +02:00
|
|
|
created_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL, related_name='hosts')
|
2013-09-28 18:02:13 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2013-09-29 16:03:56 +02:00
|
|
|
return u"%s.%s - %s" % (
|
|
|
|
self.subdomain, self.domain.domain, self.comment)
|
2013-09-28 10:44:29 +02:00
|
|
|
|
2013-09-29 13:36:28 +02:00
|
|
|
class Meta:
|
|
|
|
unique_together = (('subdomain', 'domain'),)
|
2013-09-29 17:43:17 +02:00
|
|
|
|
|
|
|
def get_fqdn(self):
|
2013-09-29 18:15:15 +02:00
|
|
|
return '%s.%s' % (self.subdomain, self.domain.domain)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def filter_by_fqdn(cls, fqdn, **kwargs):
|
|
|
|
# Assuming subdomain has no dots (.) the fqdn is split at the first dot
|
|
|
|
splitted = fqdn.split('.', 1)
|
|
|
|
if not len(splitted) == 2:
|
|
|
|
raise NotImplemented("FQDN has to contain a dot")
|
|
|
|
return Host.objects.filter(
|
|
|
|
subdomain=splitted[0], domain__domain=splitted[1], **kwargs)
|
2013-09-29 17:43:17 +02:00
|
|
|
|
2013-09-29 20:39:07 +02:00
|
|
|
def getIPv4(self):
|
2013-09-29 20:43:48 +02:00
|
|
|
try:
|
|
|
|
return dnstools.query_ns(self.get_fqdn(), 'A')
|
2013-09-29 21:00:08 +02:00
|
|
|
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
|
|
|
|
return '-'
|
2013-09-29 20:39:07 +02:00
|
|
|
|
|
|
|
def getIPv6(self):
|
2013-09-29 20:43:48 +02:00
|
|
|
try:
|
2013-09-29 20:45:55 +02:00
|
|
|
return dnstools.query_ns(self.get_fqdn(), 'AAAA')
|
2013-09-29 21:00:08 +02:00
|
|
|
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
|
|
|
|
return '-'
|
|
|
|
|
|
|
|
def poke(self):
|
|
|
|
self.last_api_update = datetime.now()
|
|
|
|
self.save()
|
2013-09-29 20:39:07 +02:00
|
|
|
|
2013-09-29 19:58:08 +02:00
|
|
|
def generate_secret(self):
|
|
|
|
# note: we use a quick hasher for the update_secret as expensive
|
|
|
|
# more modern hashes might put too much load on the servers. also
|
|
|
|
# many update clients might use http without ssl, so it is not too
|
|
|
|
# secure anyway.
|
|
|
|
secret = User.objects.make_random_password()
|
|
|
|
self.update_secret = make_password(
|
|
|
|
secret,
|
|
|
|
hasher='sha1'
|
|
|
|
)
|
|
|
|
self.save()
|
|
|
|
return secret
|
|
|
|
|
2013-09-29 17:43:17 +02:00
|
|
|
|
|
|
|
def post_delete_host(sender, **kwargs):
|
|
|
|
obj = kwargs['instance']
|
|
|
|
dnstools.delete(obj.get_fqdn())
|
|
|
|
|
2013-09-29 20:30:10 +02:00
|
|
|
post_delete.connect(post_delete_host, sender=Host)
|