2013-12-15 17:09:22 +01:00
|
|
|
"""
|
|
|
|
models for hosts, domains, service updaters, ...
|
|
|
|
"""
|
|
|
|
|
2013-10-03 19:26:39 +02:00
|
|
|
import re
|
2014-09-03 16:26:04 +02:00
|
|
|
import time
|
2013-11-02 11:29:06 +01:00
|
|
|
import base64
|
2013-10-03 19:26:39 +02:00
|
|
|
|
|
|
|
import dns.resolver
|
|
|
|
|
2013-09-28 10:44:29 +02:00
|
|
|
from django.db import models
|
2013-11-24 09:37:47 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
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
|
2014-10-27 23:06:08 +01:00
|
|
|
from django.db.models.signals import pre_delete, post_save
|
2013-09-29 19:58:08 +02:00
|
|
|
from django.contrib.auth.hashers import make_password
|
2013-11-02 11:29:06 +01:00
|
|
|
from django.utils.timezone import now
|
2014-09-03 14:29:54 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2013-09-28 18:02:13 +02:00
|
|
|
|
2013-10-17 20:50:44 +00:00
|
|
|
from . import dnstools
|
2013-09-29 14:08:22 +02:00
|
|
|
|
2014-09-03 16:26:04 +02:00
|
|
|
RESULT_MSG_LEN = 255
|
|
|
|
|
|
|
|
|
|
|
|
def result_fmt(msg):
|
|
|
|
"""
|
|
|
|
format the message for storage into client/server_result_msg fields
|
|
|
|
"""
|
|
|
|
msg = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(time.time())) + ' ' + msg
|
|
|
|
return msg[:RESULT_MSG_LEN]
|
|
|
|
|
2013-09-29 14:08:22 +02:00
|
|
|
|
2014-09-21 22:47:05 +02:00
|
|
|
class BlacklistedHost(models.Model):
|
2014-09-21 22:31:26 +02:00
|
|
|
name_re = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_('name RegEx'),
|
2013-11-23 02:51:18 +01:00
|
|
|
max_length=255,
|
2013-09-29 16:12:19 +02:00
|
|
|
unique=True,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_('Blacklisted domain. Evaluated as regex (search).'))
|
2013-09-29 14:08:22 +02:00
|
|
|
|
2014-11-09 12:27:54 +01:00
|
|
|
last_update = models.DateTimeField(_('last update'), auto_now=True)
|
|
|
|
created = models.DateTimeField(_('created at'), auto_now_add=True)
|
|
|
|
created_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
related_name='blacklisted_domains',
|
|
|
|
verbose_name=_('created by'))
|
2013-09-29 14:08:22 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2014-09-21 22:31:26 +02:00
|
|
|
return u"%s" % (self.name_re, )
|
2013-09-29 14:08:22 +02:00
|
|
|
|
2014-11-09 12:27:54 +01:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('blacklisted host')
|
|
|
|
verbose_name_plural = _('blacklisted hosts')
|
|
|
|
|
2013-09-29 14:08:22 +02:00
|
|
|
|
2014-09-21 22:48:51 +02:00
|
|
|
def host_blacklist_validator(value):
|
2014-09-21 22:47:05 +02:00
|
|
|
for bd in BlacklistedHost.objects.all():
|
2014-09-21 22:31:26 +02:00
|
|
|
if re.search(bd.name_re, value):
|
2013-11-24 09:50:57 +01:00
|
|
|
raise ValidationError(u'This name is blacklisted')
|
2013-09-29 14:08:22 +02:00
|
|
|
|
2013-09-28 18:02:13 +02:00
|
|
|
|
2013-11-06 02:21:12 +01:00
|
|
|
from collections import namedtuple
|
|
|
|
UpdateAlgorithm = namedtuple("update_algorithm", "bitlength bind_name")
|
|
|
|
|
|
|
|
UPDATE_ALGORITHM_DEFAULT = 'HMAC_SHA512'
|
|
|
|
UPDATE_ALGORITHMS = {
|
|
|
|
# dnspython_name -> UpdateAlgorithm namedtuple
|
|
|
|
'HMAC_SHA512': UpdateAlgorithm(512, 'hmac-sha512', ),
|
|
|
|
'HMAC_SHA384': UpdateAlgorithm(384, 'hmac-sha384', ),
|
|
|
|
'HMAC_SHA256': UpdateAlgorithm(256, 'hmac-sha256', ),
|
|
|
|
'HMAC_SHA224': UpdateAlgorithm(224, 'hmac-sha224', ),
|
|
|
|
'HMAC_SHA1': UpdateAlgorithm(160, 'hmac-sha1', ),
|
|
|
|
'HMAC_MD5': UpdateAlgorithm(128, 'hmac-md5', ),
|
|
|
|
}
|
|
|
|
|
|
|
|
UPDATE_ALGORITHM_CHOICES = [(k, k) for k in UPDATE_ALGORITHMS]
|
2013-10-18 15:30:17 -07:00
|
|
|
|
|
|
|
|
2013-09-29 13:36:28 +02:00
|
|
|
class Domain(models.Model):
|
2014-09-21 22:31:26 +02:00
|
|
|
name = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("name"),
|
2013-11-23 02:51:18 +01:00
|
|
|
max_length=255, # RFC 2181 (and also: max length of unique fields)
|
|
|
|
unique=True,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Name of the zone where dynamic hosts may get added"))
|
2013-10-17 21:02:54 +00:00
|
|
|
nameserver_ip = models.GenericIPAddressField(
|
2014-11-15 22:42:13 +01:00
|
|
|
_("nameserver IP (primary)"),
|
2013-11-23 02:51:18 +01:00
|
|
|
max_length=40, # ipv6 = 8 * 4 digits + 7 colons
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("IP where the dynamic DNS updates for this zone will be sent to"))
|
2014-11-15 22:42:13 +01:00
|
|
|
nameserver2_ip = models.GenericIPAddressField(
|
|
|
|
_("nameserver IP (secondary)"),
|
|
|
|
max_length=40, # ipv6 = 8 * 4 digits + 7 colons
|
|
|
|
blank=True, null=True,
|
2014-11-17 21:37:03 +01:00
|
|
|
help_text=_("IP where DNS queries for this zone will be sent to"))
|
2013-11-24 05:04:07 +01:00
|
|
|
nameserver_update_secret = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("nameserver update secret"),
|
2013-11-23 02:51:18 +01:00
|
|
|
max_length=88, # 512 bits base64 -> 88 bytes
|
2013-11-24 05:04:07 +01:00
|
|
|
default='',
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Shared secret that allows updating this zone (base64 encoded)"))
|
2013-10-18 15:30:17 -07:00
|
|
|
nameserver_update_algorithm = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("nameserver update algorithm"),
|
2013-11-23 02:51:18 +01:00
|
|
|
max_length=16, # see elements of UPDATE_ALGORITHM_CHOICES
|
2013-11-24 04:14:31 +01:00
|
|
|
default=UPDATE_ALGORITHM_DEFAULT, choices=UPDATE_ALGORITHM_CHOICES,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("HMAC_SHA512 is fine for bind9 (you can change this later, if needed)"))
|
2013-10-27 11:59:16 +01:00
|
|
|
public = models.BooleanField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("public"),
|
2013-10-27 11:59:16 +01:00
|
|
|
default=False,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Check to allow any user to add dynamic hosts to this zone - "
|
|
|
|
"if not checked, we'll only allow the owner to add hosts"))
|
2013-10-27 05:14:47 +01:00
|
|
|
# available means "nameserver for domain operating and reachable" -
|
|
|
|
# gets set to False if we have trouble reaching the nameserver
|
2013-10-27 11:59:16 +01:00
|
|
|
available = models.BooleanField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("available"),
|
2013-10-27 11:59:16 +01:00
|
|
|
default=True,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Check if nameserver is available/reachable - "
|
|
|
|
"if not checked, we'll pause querying/updating this nameserver for a while"))
|
2013-11-02 12:37:27 +01:00
|
|
|
comment = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("comment"),
|
2013-11-23 02:51:18 +01:00
|
|
|
max_length=255, # should be enough
|
|
|
|
default='', blank=True, null=True,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Some arbitrary comment about your domain. "
|
|
|
|
"If your domain is public, the comment will be also publicly shown."))
|
2013-09-29 13:36:28 +02:00
|
|
|
|
2014-11-09 12:27:54 +01:00
|
|
|
last_update = models.DateTimeField(_("last update"), auto_now=True)
|
|
|
|
created = models.DateTimeField(_("created at"), auto_now_add=True)
|
|
|
|
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='domains', verbose_name=_("created by"))
|
2013-09-29 13:36:28 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2014-09-21 22:31:26 +02:00
|
|
|
return u"%s" % (self.name, )
|
2013-09-29 13:36:28 +02:00
|
|
|
|
2013-11-02 11:29:06 +01:00
|
|
|
def generate_ns_secret(self):
|
2013-11-06 02:21:12 +01:00
|
|
|
algorithm = self.nameserver_update_algorithm
|
|
|
|
bitlength = UPDATE_ALGORITHMS[algorithm].bitlength
|
2013-11-24 09:37:47 +01:00
|
|
|
user_model = get_user_model()
|
2013-12-14 00:35:29 +01:00
|
|
|
secret = user_model.objects.make_random_password(length=bitlength // 8)
|
|
|
|
secret = secret.encode('utf-8')
|
2013-11-24 05:04:07 +01:00
|
|
|
self.nameserver_update_secret = secret_base64 = base64.b64encode(secret)
|
2013-11-02 11:29:06 +01:00
|
|
|
self.save()
|
2013-11-24 05:04:07 +01:00
|
|
|
return secret_base64
|
2013-11-02 11:29:06 +01:00
|
|
|
|
|
|
|
def get_bind9_algorithm(self):
|
2013-11-06 02:21:12 +01:00
|
|
|
return UPDATE_ALGORITHMS.get(self.nameserver_update_algorithm).bind_name
|
2013-09-29 13:36:28 +02:00
|
|
|
|
2014-11-09 12:27:54 +01:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('domain')
|
|
|
|
verbose_name_plural = _('domains')
|
|
|
|
|
2013-11-02 12:37:27 +01:00
|
|
|
|
2013-09-28 18:02:13 +02:00
|
|
|
class Host(models.Model):
|
2014-09-21 22:31:26 +02:00
|
|
|
name = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("name"),
|
2013-11-23 02:51:18 +01:00
|
|
|
max_length=255, # RFC 2181 (and considering having multiple joined labels here later)
|
|
|
|
validators=[
|
|
|
|
RegexValidator(
|
|
|
|
regex=r'^(([a-z0-9][a-z0-9\-]*[a-z0-9])|[a-z0-9])$',
|
2014-09-21 22:31:26 +02:00
|
|
|
message='Invalid host name: only "a-z", "0-9" and "-" is allowed'
|
2013-11-23 02:51:18 +01:00
|
|
|
),
|
2014-09-21 22:48:51 +02:00
|
|
|
host_blacklist_validator,
|
2013-11-23 02:51:18 +01:00
|
|
|
],
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("The name of your host."))
|
2014-11-09 12:27:54 +01:00
|
|
|
domain = models.ForeignKey(Domain, on_delete=models.CASCADE, verbose_name=_("domain"))
|
2013-11-23 02:51:18 +01:00
|
|
|
update_secret = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("update secret"),
|
2013-11-23 02:51:18 +01:00
|
|
|
max_length=64, # secret gets hashed (on save) to salted sha1, 58 bytes str len
|
|
|
|
)
|
2013-09-29 16:03:56 +02:00
|
|
|
comment = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("comment"),
|
2013-11-23 02:51:18 +01:00
|
|
|
max_length=255, # should be enough
|
|
|
|
default='', blank=True, null=True,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Some arbitrary comment about your host, e.g who / what / where this host is"))
|
2013-09-28 18:02:13 +02:00
|
|
|
|
2013-11-30 12:32:03 +01:00
|
|
|
# available means that this host may be updated (or not, if False) -
|
|
|
|
# gets set to False if abuse happens (client malfunctioning) or
|
2014-11-21 11:00:59 +01:00
|
|
|
# if updating this host triggers other errors or if host is considered stale:
|
2013-11-30 12:32:03 +01:00
|
|
|
available = models.BooleanField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("available"),
|
2013-11-30 12:32:03 +01:00
|
|
|
default=True,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Check if host is available/in use - "
|
|
|
|
"if not checked, we won't accept updates for this host"))
|
2014-09-25 23:47:48 +02:00
|
|
|
netmask_ipv4 = models.PositiveSmallIntegerField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("netmask IPv4"),
|
2014-09-25 23:40:52 +02:00
|
|
|
default=32,
|
|
|
|
help_text=_("Netmask/Prefix length for IPv4."))
|
2014-09-25 23:47:48 +02:00
|
|
|
netmask_ipv6 = models.PositiveSmallIntegerField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("netmask IPv6"),
|
2014-09-25 23:40:52 +02:00
|
|
|
default=64,
|
|
|
|
help_text=_("Netmask/Prefix length for IPv6."))
|
2013-11-30 12:32:03 +01:00
|
|
|
# abuse means that we (either the operator or some automatic mechanism)
|
|
|
|
# think the host is used in some abusive or unfair way, e.g.:
|
|
|
|
# sending nochg updates way too often or otherwise using a defect,
|
|
|
|
# misconfigured or otherwise malfunctioning update client
|
|
|
|
# acting against fair use / ToS.
|
|
|
|
# the abuse flag can be switched off by the user, if the user thinks
|
|
|
|
# he fixed the problem on his side (or that there was no problem).
|
|
|
|
abuse = models.BooleanField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("abuse"),
|
2013-11-30 12:32:03 +01:00
|
|
|
default=False,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Checked if we think you abuse the service - "
|
|
|
|
"you may uncheck this AFTER fixing all issues on your side"))
|
2013-11-30 12:32:03 +01:00
|
|
|
|
|
|
|
# similar to above, but can not be toggled by the user:
|
|
|
|
abuse_blocked = models.BooleanField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("abuse blocked"),
|
2013-11-30 12:32:03 +01:00
|
|
|
default=False,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Checked to block a host for abuse."))
|
2013-11-30 12:32:03 +01:00
|
|
|
|
2013-11-24 11:42:59 +01:00
|
|
|
# count client misbehaviours, like sending nochg updates or other
|
|
|
|
# errors that should make the client stop trying to update:
|
2014-11-09 12:27:54 +01:00
|
|
|
client_faults = models.PositiveIntegerField(_("client faults"), default=0)
|
2014-09-03 16:26:04 +02:00
|
|
|
client_result_msg = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("client result msg"),
|
2014-09-03 16:26:04 +02:00
|
|
|
max_length=RESULT_MSG_LEN,
|
|
|
|
default='', blank=True, null=True,
|
|
|
|
help_text=_("Latest result message relating to the client"))
|
2013-11-24 11:42:59 +01:00
|
|
|
|
|
|
|
# count server faults that happened when updating this host
|
2014-11-09 12:27:54 +01:00
|
|
|
server_faults = models.PositiveIntegerField(_("server faults"), default=0)
|
2014-09-03 16:26:04 +02:00
|
|
|
server_result_msg = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("server result msg"),
|
2014-09-03 16:26:04 +02:00
|
|
|
max_length=RESULT_MSG_LEN,
|
|
|
|
default='', blank=True, null=True,
|
|
|
|
help_text=_("Latest result message relating to the server"))
|
2013-11-24 11:42:59 +01:00
|
|
|
|
2014-11-16 00:04:27 +01:00
|
|
|
# count api auth errors - maybe caused by host owner (misconfigured update client)
|
|
|
|
api_auth_faults = models.PositiveIntegerField(_("api auth faults"), default=0)
|
|
|
|
api_auth_result_msg = models.CharField(
|
|
|
|
_("api auth result msg"),
|
|
|
|
max_length=RESULT_MSG_LEN,
|
|
|
|
default='', blank=True, null=True,
|
|
|
|
help_text=_("Latest result message relating to api authentication"))
|
|
|
|
|
2013-11-03 08:32:43 +01:00
|
|
|
# when we received the last update for v4/v6 addr
|
2014-11-09 12:27:54 +01:00
|
|
|
last_update_ipv4 = models.DateTimeField(_("last update IPv4"), blank=True, null=True)
|
|
|
|
last_update_ipv6 = models.DateTimeField(_("last update IPv6"), blank=True, null=True)
|
2013-11-03 08:32:43 +01:00
|
|
|
# how we received the last update for v4/v6 addr
|
2014-11-09 12:27:54 +01:00
|
|
|
tls_update_ipv4 = models.BooleanField(_("TLS update IPv4"), default=False)
|
2014-11-17 21:37:03 +01:00
|
|
|
tls_update_ipv6 = models.BooleanField(_("TLS update IPv6"), default=False)
|
2013-11-24 08:34:01 +01:00
|
|
|
|
2014-11-21 11:00:59 +01:00
|
|
|
# for "hosts --stale-check --notify-user" management command
|
|
|
|
staleness = models.PositiveIntegerField(_("staleness"), default=0)
|
|
|
|
staleness_notification_timestamp = models.DateTimeField(_("staleness notification time"), blank=True, null=True)
|
|
|
|
|
2014-11-09 12:27:54 +01:00
|
|
|
last_update = models.DateTimeField(_("last update"), auto_now=True)
|
|
|
|
created = models.DateTimeField(_("created at"), auto_now_add=True)
|
|
|
|
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='hosts', verbose_name=_("created by"),)
|
2013-09-28 18:02:13 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2013-11-24 05:23:20 +01:00
|
|
|
return u"%s.%s" % (
|
2014-09-21 22:31:26 +02:00
|
|
|
self.name, self.domain.name)
|
2013-09-28 10:44:29 +02:00
|
|
|
|
2013-10-03 17:21:18 +02:00
|
|
|
class Meta(object):
|
2014-09-21 22:31:26 +02:00
|
|
|
unique_together = (('name', 'domain'), )
|
|
|
|
index_together = (('name', 'domain'), )
|
2014-11-09 12:27:54 +01:00
|
|
|
verbose_name = _('host')
|
|
|
|
verbose_name_plural = _('hosts')
|
2013-09-29 17:43:17 +02:00
|
|
|
|
|
|
|
def get_fqdn(self):
|
2014-09-21 22:31:26 +02:00
|
|
|
return dnstools.FQDN(self.name, self.domain.name)
|
2013-09-29 18:15:15 +02:00
|
|
|
|
|
|
|
@classmethod
|
2014-05-29 15:25:38 +02:00
|
|
|
def get_by_fqdn(cls, fqdn, **kwargs):
|
2013-09-29 18:15:15 +02:00
|
|
|
# Assuming subdomain has no dots (.) the fqdn is split at the first dot
|
|
|
|
splitted = fqdn.split('.', 1)
|
2014-05-29 15:25:38 +02:00
|
|
|
if len(splitted) != 2:
|
|
|
|
raise ValueError("get_by_fqdn(%s): FQDN has to contain (at least) one dot" % fqdn)
|
|
|
|
try:
|
2014-09-21 22:31:26 +02:00
|
|
|
host = Host.objects.get(name=splitted[0], domain__name=splitted[1], **kwargs)
|
2014-05-29 15:25:38 +02:00
|
|
|
except Host.DoesNotExist:
|
2013-11-24 10:43:15 +01:00
|
|
|
return None
|
2014-05-29 15:25:38 +02:00
|
|
|
except Host.MultipleObjectsReturned:
|
|
|
|
# should not happen, see Meta.unique_together
|
|
|
|
raise ValueError("get_by_fqdn(%s) found more than 1 host" % fqdn)
|
|
|
|
else:
|
|
|
|
return host
|
2013-09-29 17:43:17 +02:00
|
|
|
|
2013-11-30 10:07:46 +01:00
|
|
|
def get_ip(self, kind):
|
|
|
|
record = 'A' if kind == 'ipv4' else 'AAAA'
|
2013-09-29 20:43:48 +02:00
|
|
|
try:
|
2014-08-30 18:27:21 +02:00
|
|
|
return dnstools.query_ns(self.get_fqdn(), record)
|
2014-01-22 14:03:36 +01:00
|
|
|
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
|
|
|
|
return 'none'
|
|
|
|
except (dns.resolver.NoNameservers, dns.resolver.Timeout, dnstools.NameServerNotAvailable):
|
2013-10-27 05:14:47 +01:00
|
|
|
return 'error'
|
2013-09-29 20:39:07 +02:00
|
|
|
|
2013-11-30 10:07:46 +01:00
|
|
|
def get_ipv4(self):
|
|
|
|
return self.get_ip('ipv4')
|
|
|
|
|
2013-11-05 00:32:07 +01:00
|
|
|
def get_ipv6(self):
|
2013-11-30 10:07:46 +01:00
|
|
|
return self.get_ip('ipv6')
|
2013-09-29 21:00:08 +02:00
|
|
|
|
2014-05-30 01:28:34 +02:00
|
|
|
def poke(self, kind, secure):
|
2013-10-27 13:09:46 +01:00
|
|
|
if kind == 'ipv4':
|
|
|
|
self.last_update_ipv4 = now()
|
2014-05-30 02:03:48 +02:00
|
|
|
self.tls_update_ipv4 = secure
|
2013-10-27 13:09:46 +01:00
|
|
|
else:
|
|
|
|
self.last_update_ipv6 = now()
|
2014-05-30 02:03:48 +02:00
|
|
|
self.tls_update_ipv6 = secure
|
2013-09-29 21:00:08 +02:00
|
|
|
self.save()
|
2013-09-29 20:39:07 +02:00
|
|
|
|
2014-09-03 16:26:04 +02:00
|
|
|
def register_client_result(self, msg, fault=False):
|
2014-09-03 15:34:31 +02:00
|
|
|
if fault:
|
|
|
|
self.client_faults += 1
|
2014-09-03 16:26:04 +02:00
|
|
|
self.client_result_msg = result_fmt(msg)
|
2013-11-24 11:42:59 +01:00
|
|
|
self.save()
|
|
|
|
|
2014-09-03 16:26:04 +02:00
|
|
|
def register_server_result(self, msg, fault=False):
|
2014-09-03 15:34:31 +02:00
|
|
|
if fault:
|
|
|
|
self.server_faults += 1
|
2014-09-03 16:26:04 +02:00
|
|
|
self.server_result_msg = result_fmt(msg)
|
2013-11-24 11:42:59 +01:00
|
|
|
self.save()
|
|
|
|
|
2014-11-16 00:04:27 +01:00
|
|
|
def register_api_auth_result(self, msg, fault=False):
|
|
|
|
if fault:
|
|
|
|
self.api_auth_faults += 1
|
|
|
|
self.api_auth_result_msg = result_fmt(msg)
|
|
|
|
self.save()
|
|
|
|
|
2013-11-16 01:25:05 +01:00
|
|
|
def generate_secret(self, secret=None):
|
2013-09-29 19:58:08 +02:00
|
|
|
# note: we use a quick hasher for the update_secret as expensive
|
|
|
|
# more modern hashes might put too much load on the servers. also
|
2014-05-30 01:10:33 +02:00
|
|
|
# many update clients might use http without tls, so it is not too
|
2013-09-29 19:58:08 +02:00
|
|
|
# secure anyway.
|
2013-11-16 01:25:05 +01:00
|
|
|
if secret is None:
|
2013-11-24 09:37:47 +01:00
|
|
|
user_model = get_user_model()
|
|
|
|
secret = user_model.objects.make_random_password()
|
2013-09-29 19:58:08 +02:00
|
|
|
self.update_secret = make_password(
|
|
|
|
secret,
|
|
|
|
hasher='sha1'
|
|
|
|
)
|
|
|
|
self.save()
|
|
|
|
return secret
|
|
|
|
|
2013-09-29 17:43:17 +02:00
|
|
|
|
2013-11-02 00:12:36 +01:00
|
|
|
def pre_delete_host(sender, **kwargs):
|
2013-09-29 17:43:17 +02:00
|
|
|
obj = kwargs['instance']
|
2013-11-02 00:12:36 +01:00
|
|
|
try:
|
2014-08-30 18:27:21 +02:00
|
|
|
dnstools.delete(obj.get_fqdn())
|
2013-11-02 00:12:36 +01:00
|
|
|
except (dnstools.Timeout, dnstools.NameServerNotAvailable):
|
|
|
|
# well, we tried to clean up, but we didn't reach the nameserver
|
|
|
|
pass
|
2013-11-23 02:57:15 +01:00
|
|
|
except (dnstools.DnsUpdateError, ):
|
|
|
|
# e.g. PeerBadSignature if host is protected by a key we do not have
|
|
|
|
pass
|
2013-09-29 17:43:17 +02:00
|
|
|
|
2013-11-02 00:12:36 +01:00
|
|
|
pre_delete.connect(pre_delete_host, sender=Host)
|
2013-11-26 08:10:05 +01:00
|
|
|
|
|
|
|
|
2014-10-27 23:06:08 +01:00
|
|
|
def post_save_host(sender, **kwargs):
|
|
|
|
obj = kwargs['instance']
|
|
|
|
if obj.abuse or obj.abuse_blocked:
|
|
|
|
try:
|
|
|
|
dnstools.delete(obj.get_fqdn())
|
|
|
|
except (dnstools.Timeout, dnstools.NameServerNotAvailable):
|
|
|
|
# well, we tried to clean up, but we didn't reach the nameserver
|
|
|
|
pass
|
|
|
|
except (dnstools.DnsUpdateError, ):
|
|
|
|
# e.g. PeerBadSignature if host is protected by a key we do not have
|
|
|
|
pass
|
|
|
|
|
|
|
|
post_save.connect(post_save_host, sender=Host)
|
|
|
|
|
|
|
|
|
2014-09-23 00:48:54 +02:00
|
|
|
class RelatedHost(models.Model):
|
|
|
|
# host addr = network_of_main_host + interface_id
|
|
|
|
name = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("name"),
|
2014-09-23 00:48:54 +02:00
|
|
|
max_length=255, # RFC 2181 (and considering having multiple joined labels here later)
|
|
|
|
validators=[
|
|
|
|
RegexValidator(
|
|
|
|
regex=r'^(([a-z0-9][a-z0-9\-]*[a-z0-9])|[a-z0-9])$',
|
|
|
|
message='Invalid host name: only "a-z", "0-9" and "-" is allowed'
|
|
|
|
),
|
|
|
|
],
|
|
|
|
help_text=_("The name of a host in same network as your main host."))
|
2014-09-25 22:05:14 +02:00
|
|
|
comment = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("comment"),
|
2014-09-25 22:05:14 +02:00
|
|
|
max_length=255, # should be enough
|
|
|
|
default='', blank=True, null=True,
|
|
|
|
help_text=_("Some arbitrary comment about your host, e.g who / what / where this host is"))
|
2014-09-23 00:48:54 +02:00
|
|
|
interface_id_ipv4 = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("interface ID IPv4"),
|
2014-09-23 00:48:54 +02:00
|
|
|
default='',
|
|
|
|
max_length=16, # 123.123.123.123
|
2014-09-25 20:15:18 +02:00
|
|
|
help_text=_("The IPv4 interface ID of this host. Use IPv4 notation."))
|
2014-09-23 00:48:54 +02:00
|
|
|
interface_id_ipv6 = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("interface ID IPv6"),
|
2014-09-23 00:48:54 +02:00
|
|
|
default='',
|
|
|
|
max_length=22, # ::1234:5678:9abc:def0
|
2014-09-25 20:15:18 +02:00
|
|
|
help_text=_("The IPv6 interface ID of this host. Use IPv6 notation."))
|
2014-09-23 00:48:54 +02:00
|
|
|
available = models.BooleanField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("available"),
|
2014-09-23 00:48:54 +02:00
|
|
|
default=True,
|
|
|
|
help_text=_("Check if host is available/in use - "
|
|
|
|
"if not checked, we won't accept updates for this host"))
|
|
|
|
|
2014-11-09 12:27:54 +01:00
|
|
|
main_host = models.ForeignKey(
|
|
|
|
Host,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='relatedhosts',
|
|
|
|
verbose_name=_("main host"))
|
2014-09-23 00:48:54 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s.%s" % (
|
2014-09-26 02:07:58 +02:00
|
|
|
self.name, self.main_host.__unicode__())
|
2014-09-23 00:48:54 +02:00
|
|
|
|
|
|
|
class Meta(object):
|
|
|
|
unique_together = (('name', 'main_host'), )
|
2014-11-09 12:27:54 +01:00
|
|
|
verbose_name = _('related host')
|
|
|
|
verbose_name_plural = _('related hosts')
|
2014-09-23 00:48:54 +02:00
|
|
|
|
2014-09-25 20:15:18 +02:00
|
|
|
def get_fqdn(self):
|
2014-09-25 20:38:12 +02:00
|
|
|
main = self.main_host.get_fqdn()
|
|
|
|
# note: we put the related hosts (subhosts) into same zone as the main host,
|
|
|
|
# so the resulting hostname has a dot inside:
|
|
|
|
return dnstools.FQDN('%s.%s' % (self.name, main.host), main.domain)
|
|
|
|
|
2014-09-26 00:16:52 +02:00
|
|
|
def get_ip(self, kind):
|
|
|
|
record = 'A' if kind == 'ipv4' else 'AAAA'
|
|
|
|
try:
|
|
|
|
return dnstools.query_ns(self.get_fqdn(), record)
|
|
|
|
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
|
|
|
|
return 'none'
|
|
|
|
except (dns.resolver.NoNameservers, dns.resolver.Timeout, dnstools.NameServerNotAvailable):
|
|
|
|
return 'error'
|
|
|
|
|
|
|
|
def get_ipv4(self):
|
|
|
|
return self.get_ip('ipv4')
|
|
|
|
|
|
|
|
def get_ipv6(self):
|
|
|
|
return self.get_ip('ipv6')
|
|
|
|
|
2014-09-25 20:38:12 +02:00
|
|
|
|
|
|
|
pre_delete.connect(pre_delete_host, sender=RelatedHost)
|
2014-09-25 20:15:18 +02:00
|
|
|
|
2014-09-23 00:48:54 +02:00
|
|
|
|
2013-11-26 08:10:05 +01:00
|
|
|
class ServiceUpdater(models.Model):
|
|
|
|
name = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("name"),
|
2013-11-26 08:10:05 +01:00
|
|
|
max_length=32,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Service name"))
|
2013-11-26 08:10:05 +01:00
|
|
|
comment = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("comment"),
|
2013-11-26 08:10:05 +01:00
|
|
|
max_length=255, # should be enough
|
|
|
|
default='', blank=True, null=True,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Some arbitrary comment about the service"))
|
2013-11-26 08:10:05 +01:00
|
|
|
server = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("server"),
|
2013-11-26 08:10:05 +01:00
|
|
|
max_length=255, # should be enough
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Update Server [name or IP] of this service"))
|
2013-11-26 08:10:05 +01:00
|
|
|
path = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("path"),
|
2013-11-26 08:10:05 +01:00
|
|
|
max_length=255, # should be enough
|
|
|
|
default='/nic/update',
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Update Server URL path of this service"))
|
2013-11-26 08:10:05 +01:00
|
|
|
secure = models.BooleanField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("secure"),
|
2013-11-26 08:10:05 +01:00
|
|
|
default=True,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Use https / TLS to contact the Update Server?"))
|
2013-11-26 08:10:05 +01:00
|
|
|
|
2013-11-29 02:11:55 +01:00
|
|
|
# what kind(s) of IPs is (are) acceptable to this service:
|
2014-11-09 12:27:54 +01:00
|
|
|
accept_ipv4 = models.BooleanField(_("accept IPv4"), default=False)
|
|
|
|
accept_ipv6 = models.BooleanField(_("accept IPv6"), default=False)
|
2013-11-29 02:11:55 +01:00
|
|
|
|
2014-11-09 12:27:54 +01:00
|
|
|
last_update = models.DateTimeField(_("last update"), auto_now=True)
|
|
|
|
created = models.DateTimeField(_("created at"), auto_now_add=True)
|
|
|
|
created_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
related_name='serviceupdater',
|
|
|
|
verbose_name=_("created by"))
|
2013-11-26 08:10:05 +01:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2013-11-29 11:13:59 +01:00
|
|
|
return u"%s" % (self.name, )
|
2013-11-26 08:10:05 +01:00
|
|
|
|
2014-11-09 12:27:54 +01:00
|
|
|
class Meta(object):
|
2014-11-15 22:27:04 +01:00
|
|
|
verbose_name = _('service updater')
|
|
|
|
verbose_name_plural = _('service updaters')
|
2014-11-09 12:27:54 +01:00
|
|
|
|
2013-11-26 08:10:05 +01:00
|
|
|
|
|
|
|
class ServiceUpdaterHostConfig(models.Model):
|
2014-11-09 12:27:54 +01:00
|
|
|
service = models.ForeignKey(ServiceUpdater, on_delete=models.CASCADE, verbose_name=_("service"))
|
2013-11-26 08:10:05 +01:00
|
|
|
|
|
|
|
hostname = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("hostname"),
|
2013-11-26 08:10:05 +01:00
|
|
|
max_length=255, # should be enough
|
|
|
|
default='', blank=True, null=True,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("The hostname for that service (used in query string)"))
|
2013-11-26 08:10:05 +01:00
|
|
|
comment = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("comment"),
|
2013-11-26 08:10:05 +01:00
|
|
|
max_length=255, # should be enough
|
|
|
|
default='', blank=True, null=True,
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("Some arbitrary comment about your host on that service"))
|
2013-11-26 08:10:05 +01:00
|
|
|
# credentials for http basic auth for THAT service (not for us),
|
|
|
|
# we need to store the password in plain text, we can't hash it
|
|
|
|
name = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("name"),
|
2013-11-26 08:10:05 +01:00
|
|
|
max_length=255, # should be enough
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("The name/id for that service (used for http basic auth)"))
|
2013-11-26 08:10:05 +01:00
|
|
|
password = models.CharField(
|
2014-11-09 12:27:54 +01:00
|
|
|
_("password"),
|
2013-11-26 08:10:05 +01:00
|
|
|
max_length=255, # should be enough
|
2014-09-03 14:29:54 +02:00
|
|
|
help_text=_("The password/secret for that service (used for http basic auth)"))
|
2013-11-26 08:10:05 +01:00
|
|
|
|
2013-11-29 02:11:55 +01:00
|
|
|
# what kind(s) of IPs should be given to this service:
|
2014-11-09 12:27:54 +01:00
|
|
|
give_ipv4 = models.BooleanField(_("give IPv4"), default=False)
|
|
|
|
give_ipv6 = models.BooleanField(_("give IPv6"), default=False)
|
|
|
|
|
|
|
|
host = models.ForeignKey(
|
|
|
|
Host,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='serviceupdaterhostconfigs',
|
|
|
|
verbose_name=_("host"))
|
|
|
|
|
|
|
|
last_update = models.DateTimeField(_("last update"), auto_now=True)
|
|
|
|
created = models.DateTimeField(_("created at"), auto_now_add=True)
|
|
|
|
created_by = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
related_name='serviceupdaterhostconfigs',
|
|
|
|
verbose_name=_("created by"))
|
2013-11-26 08:10:05 +01:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2013-11-29 11:13:59 +01:00
|
|
|
return u"%s (%s)" % (self.hostname, self.service.name, )
|
2014-11-09 12:27:54 +01:00
|
|
|
|
|
|
|
class Meta(object):
|
|
|
|
verbose_name = _('service updater host config')
|
|
|
|
verbose_name_plural = _('service updater host configs')
|