class RRset(ExportModelOperationsMixin('RRset'), models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField( null=True) # undocumented, used for debugging only domain = models.ForeignKey(Domain, on_delete=models.CASCADE) subname = models.CharField( max_length=178, blank=True, validators=[ validate_lower, RegexValidator( regex=r'^([*]|(([*][.])?[a-z0-9_.-]*))$', message= 'Subname can only use (lowercase) a-z, 0-9, ., -, and _, ' 'may start with a \'*.\', or just be \'*\'.', code='invalid_subname') ]) type = models.CharField( max_length=10, validators=[ validate_upper, RegexValidator( regex=r'^[A-Z][A-Z0-9]*$', message= 'Type must be uppercase alphanumeric and start with a letter.', code='invalid_type') ]) ttl = models.PositiveIntegerField() objects = RRsetManager() DEAD_TYPES = ('ALIAS', 'DNAME') RESTRICTED_TYPES = ('SOA', 'RRSIG', 'DNSKEY', 'NSEC3PARAM', 'OPT') class Meta: unique_together = (("domain", "subname", "type"), ) @staticmethod def construct_name(subname, domain_name): return '.'.join(filter(None, [subname, domain_name])) + '.' @property def name(self): return self.construct_name(self.subname, self.domain.name) def save(self, *args, **kwargs): self.updated = timezone.now() self.full_clean(validate_unique=False) super().save(*args, **kwargs) def __str__(self): return '<RRSet %s domain=%s type=%s subname=%s>' % ( self.pk, self.domain.name, self.type, self.subname)
class Domain(models.Model): created = models.DateTimeField(auto_now_add=True) name = models.CharField(max_length=191, unique=True, validators=[validate_lower, RegexValidator(regex=r'^[a-z0-9_.-]*[a-z]$', message='Domain name malformed.', code='invalid_domain_name') ]) owner = models.ForeignKey(User, on_delete=models.PROTECT, related_name='domains') published = models.DateTimeField(null=True, blank=True) minimum_ttl = models.PositiveIntegerField(default=settings.MINIMUM_TTL_DEFAULT) @property def keys(self): return pdns.get_keys(self) def partition_name(domain): name = domain.name if isinstance(domain, Domain) else domain subname, _, parent_name = name.partition('.') return subname, parent_name or None def save(self, *args, **kwargs): self.full_clean(validate_unique=False) super().save(*args, **kwargs) def update_delegation(self, child_domain: Domain): child_subname, child_domain_name = child_domain.partition_name() if self.name != child_domain_name: raise ValueError('Cannot update delegation of %s as it is not an immediate child domain of %s.' % (child_domain.name, self.name)) if child_domain.pk: # Domain real: set delegation child_keys = child_domain.keys if not child_keys: raise APIException('Cannot delegate %s, as it currently has no keys.' % child_domain.name) RRset.objects.create(domain=self, subname=child_subname, type='NS', ttl=3600, contents=settings.DEFAULT_NS) RRset.objects.create(domain=self, subname=child_subname, type='DS', ttl=300, contents=[ds for k in child_keys for ds in k['ds']]) else: # Domain not real: remove delegation for rrset in self.rrset_set.filter(subname=child_subname, type__in=['NS', 'DS']): rrset.delete() def __str__(self): return self.name class Meta: ordering = ('created',)
class Domain(ExportModelOperationsMixin('Domain'), models.Model): created = models.DateTimeField(auto_now_add=True) name = models.CharField(max_length=191, unique=True, validators=validate_domain_name) owner = models.ForeignKey(User, on_delete=models.PROTECT, related_name='domains') published = models.DateTimeField(null=True, blank=True) minimum_ttl = models.PositiveIntegerField(default=get_minimum_ttl_default) _keys = None @classmethod def is_registrable(cls, domain_name: str, user: User): """ Returns False in any of the following cases: (a) the domain name is under .internal, (b) the domain_name appears on the public suffix list, (c) the domain is descendant to a zone that belongs to any user different from the given one, unless it's parent is a public suffix, either through the Internet PSL or local settings. Otherwise, True is returned. """ if domain_name != domain_name.lower(): raise ValueError if f'.{domain_name}'.endswith('.internal'): return False try: public_suffix = psl.get_public_suffix(domain_name) is_public_suffix = psl.is_public_suffix(domain_name) except (Timeout, NoNameservers): public_suffix = domain_name.rpartition('.')[2] is_public_suffix = ('.' not in domain_name) # TLDs are public suffixes except psl_dns.exceptions.UnsupportedRule as e: # It would probably be fine to treat this as a non-public suffix (with the TLD acting as the # public suffix and setting both public_suffix and is_public_suffix accordingly). # However, in order to allow to investigate the situation, it's better not catch # this exception. For web requests, our error handler turns it into a 503 error # and makes sure admins are notified. raise e if not is_public_suffix: # Take into account that any of the parent domains could be a local public suffix. To that # end, identify the longest local public suffix that is actually a suffix of domain_name. # Then, override the global PSL result. for local_public_suffix in settings.LOCAL_PUBLIC_SUFFIXES: has_local_public_suffix_parent = ( '.' + domain_name).endswith('.' + local_public_suffix) if has_local_public_suffix_parent and len( local_public_suffix) > len(public_suffix): public_suffix = local_public_suffix is_public_suffix = (public_suffix == domain_name) if is_public_suffix and domain_name not in settings.LOCAL_PUBLIC_SUFFIXES: return False # Generate a list of all domains connecting this one and its public suffix. # If another user owns a zone with one of these names, then the requested # domain is unavailable because it is part of the other user's zone. private_components = domain_name.rsplit(public_suffix, 1)[0].rstrip('.') private_components = private_components.split( '.') if private_components else [] private_components += [public_suffix] private_domains = [ '.'.join(private_components[i:]) for i in range(0, len(private_components) - 1) ] assert is_public_suffix or domain_name == private_domains[0] # Deny registration for non-local public suffixes and for domains covered by other users' zones user = user if not isinstance(user, AnonymousUser) else None return not cls.objects.filter( Q(name__in=private_domains) & ~Q(owner=user)).exists() @property def keys(self): if not self._keys: self._keys = pdns.get_keys(self) return self._keys @property def is_locally_registrable(self): return self.parent_domain_name in settings.LOCAL_PUBLIC_SUFFIXES @property def parent_domain_name(self): return self._partitioned_name[1] @property def _partitioned_name(self): subname, _, parent_name = self.name.partition('.') return subname, parent_name or None def save(self, *args, **kwargs): self.full_clean(validate_unique=False) super().save(*args, **kwargs) def update_delegation(self, child_domain: Domain): child_subname, child_domain_name = child_domain._partitioned_name if self.name != child_domain_name: raise ValueError( 'Cannot update delegation of %s as it is not an immediate child domain of %s.' % (child_domain.name, self.name)) if child_domain.pk: # Domain real: set delegation child_keys = child_domain.keys if not child_keys: raise APIException( 'Cannot delegate %s, as it currently has no keys.' % child_domain.name) RRset.objects.create(domain=self, subname=child_subname, type='NS', ttl=3600, contents=settings.DEFAULT_NS) RRset.objects.create( domain=self, subname=child_subname, type='DS', ttl=300, contents=[ds for k in child_keys for ds in k['ds']]) else: # Domain not real: remove delegation for rrset in self.rrset_set.filter(subname=child_subname, type__in=['NS', 'DS']): rrset.delete() def delete(self): ret = super().delete() logger.warning(f'Domain {self.name} deleted (owner: {self.owner.pk})') return ret def __str__(self): return self.name class Meta: ordering = ('created', )
class RRset(ExportModelOperationsMixin('RRset'), models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) created = models.DateTimeField(auto_now_add=True) touched = models.DateTimeField(auto_now=True) domain = models.ForeignKey(Domain, on_delete=models.CASCADE) subname = models.CharField( max_length=178, blank=True, validators=[ validate_lower, RegexValidator( regex=r'^([*]|(([*][.])?([a-z0-9_-]+[.])*[a-z0-9_-]+))$', message= 'Subname can only use (lowercase) a-z, 0-9, ., -, and _, ' 'may start with a \'*.\', or just be \'*\'.', code='invalid_subname') ]) type = models.CharField( max_length=10, validators=[ validate_upper, RegexValidator( regex=r'^[A-Z][A-Z0-9]*$', message= 'Type must be uppercase alphanumeric and start with a letter.', code='invalid_type') ]) ttl = models.PositiveIntegerField() objects = RRsetManager() class Meta: unique_together = (("domain", "subname", "type"), ) @staticmethod def construct_name(subname, domain_name): return '.'.join(filter(None, [subname, domain_name])) + '.' @property def name(self): return self.construct_name(self.subname, self.domain.name) def save(self, *args, **kwargs): self.full_clean(validate_unique=False) super().save(*args, **kwargs) def clean_records(self, records_presentation_format): """ Validates the records belonging to this set. Validation rules follow the DNS specification; some types may incur additional validation rules. Raises ValidationError if violation of DNS specification is found. Returns a set of records in canonical presentation format. :param records_presentation_format: iterable of records in presentation format """ rdtype = rdatatype.from_text(self.type) errors = [] def _error_msg(record, detail): return f'Record content of {self.type} {self.name} invalid: \'{record}\': {detail}' records_canonical_format = set() for r in records_presentation_format: try: r_canonical_format = RR.canonical_presentation_format( r, rdtype) except binascii.Error: # e.g., odd-length string errors.append( _error_msg( r, 'Cannot parse hexadecimal or base64 record contents')) except dns.exception.SyntaxError as e: # e.g., A/127.0.0.999 if 'quote' in e.args[0]: errors.append( _error_msg( r, f'Data for {self.type} records must be given using quotation marks.' )) else: errors.append( _error_msg( r, f'Record content malformed: {",".join(e.args)}')) except dns.name.NeedAbsoluteNameOrOrigin: errors.append( _error_msg( r, 'Hostname must be fully qualified (i.e., end in a dot: "example.com.")' )) except ValueError: # e.g., string ("asdf") cannot be parsed into int on base 10 errors.append(_error_msg(r, 'Cannot parse record contents')) except Exception as e: # TODO see what exceptions raise here for faulty input raise e else: if r_canonical_format in records_canonical_format: errors.append( _error_msg( r, f'Duplicate record content: this is identical to ' f'\'{r_canonical_format}\'')) else: records_canonical_format.add(r_canonical_format) if any(errors): raise ValidationError(errors) return records_canonical_format def save_records(self, records): """ Updates this RR set's resource records, discarding any old values. Records are expected in presentation format and are converted to canonical presentation format (e.g., 127.00.0.1 will be converted to 127.0.0.1). Raises if a invalid set of records is provided. This method triggers the following database queries: - one DELETE query - one SELECT query for comparison of old with new records - one INSERT query, if one or more records were added Changes are saved to the database immediately. :param records: list of records in presentation format """ new_records = self.clean_records(records) # Delete RRs that are not in the new record list from the DB self.records.exclude(content__in=new_records).delete() # one DELETE # Retrieve all remaining RRs from the DB unchanged_records = set(r.content for r in self.records.all()) # one SELECT # Save missing RRs from the new record list to the DB added_records = new_records - unchanged_records rrs = [RR(rrset=self, content=content) for content in added_records] RR.objects.bulk_create(rrs) # One INSERT def __str__(self): return '<RRSet %s domain=%s type=%s subname=%s>' % ( self.pk, self.domain.name, self.type, self.subname)
class Domain(ExportModelOperationsMixin('Domain'), models.Model): class RenewalState(models.IntegerChoices): IMMORTAL = 0 FRESH = 1 NOTIFIED = 2 WARNED = 3 created = models.DateTimeField(auto_now_add=True) name = models.CharField(max_length=191, unique=True, validators=validate_domain_name) owner = models.ForeignKey(User, on_delete=models.PROTECT, related_name='domains') published = models.DateTimeField(null=True, blank=True) minimum_ttl = models.PositiveIntegerField(default=get_minimum_ttl_default) renewal_state = models.IntegerField(choices=RenewalState.choices, default=RenewalState.IMMORTAL) renewal_changed = models.DateTimeField(auto_now_add=True) _keys = None def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if self.pk is None and kwargs.get( 'renewal_state') is None and self.is_locally_registrable: self.renewal_state = Domain.RenewalState.FRESH @cached_property def public_suffix(self): try: public_suffix = psl.get_public_suffix(self.name) is_public_suffix = psl.is_public_suffix(self.name) except (Timeout, NoNameservers): public_suffix = self.name.rpartition('.')[2] is_public_suffix = ('.' not in self.name) # TLDs are public suffixes except psl_dns.exceptions.UnsupportedRule as e: # It would probably be fine to treat this as a non-public suffix (with the TLD acting as the # public suffix and setting both public_suffix and is_public_suffix accordingly). # However, in order to allow to investigate the situation, it's better not catch # this exception. For web requests, our error handler turns it into a 503 error # and makes sure admins are notified. raise e if is_public_suffix: return public_suffix # Take into account that any of the parent domains could be a local public suffix. To that # end, identify the longest local public suffix that is actually a suffix of domain_name. for local_public_suffix in settings.LOCAL_PUBLIC_SUFFIXES: has_local_public_suffix_parent = ( '.' + self.name).endswith('.' + local_public_suffix) if has_local_public_suffix_parent and len( local_public_suffix) > len(public_suffix): public_suffix = local_public_suffix return public_suffix def is_covered_by_foreign_zone(self): # Generate a list of all domains connecting this one and its public suffix. # If another user owns a zone with one of these names, then the requested # domain is unavailable because it is part of the other user's zone. private_components = self.name.rsplit(self.public_suffix, 1)[0].rstrip('.') private_components = private_components.split( '.') if private_components else [] private_domains = [ '.'.join(private_components[i:]) for i in range(0, len(private_components)) ] private_domains = [ f'{private_domain}.{self.public_suffix}' for private_domain in private_domains ] assert self.name == next(iter(private_domains), self.public_suffix) # Determine whether domain is covered by other users' zones return Domain.objects.filter( Q(name__in=private_domains) & ~Q(owner=self._owner_or_none)).exists() def covers_foreign_zone(self): # Note: This is not completely accurate: Ideally, we should only consider zones with identical public suffix. # (If a public suffix lies in between, it's ok.) However, as there could be many descendant zones, the accurate # check is expensive, so currently not implemented (PSL lookups for each of them). return Domain.objects.filter( Q(name__endswith=f'.{self.name}') & ~Q(owner=self._owner_or_none)).exists() def is_registrable(self): """ Returns False if the domain name is reserved, a public suffix, or covered by / covers another user's domain. Otherwise, True is returned. """ self.clean() # ensure .name is a domain name private_generation = self.name.count('.') - self.public_suffix.count( '.') assert private_generation >= 0 # .internal is reserved if f'.{self.name}'.endswith('.internal'): return False # Public suffixes can only be registered if they are local if private_generation == 0 and self.name not in settings.LOCAL_PUBLIC_SUFFIXES: return False # Disallow _acme-challenge.dedyn.io and the like. Rejects reserved direct children of public suffixes. reserved_prefixes = ( '_', 'autoconfig.', 'autodiscover.', ) if private_generation == 1 and any( self.name.startswith(prefix) for prefix in reserved_prefixes): return False # Domains covered by another user's zone can't be registered if self.is_covered_by_foreign_zone(): return False # Domains that would cover another user's zone can't be registered if self.covers_foreign_zone(): return False return True @property def keys(self): if not self._keys: self._keys = pdns.get_keys(self) return self._keys @property def touched(self): try: rrset_touched = max(updated for updated in self.rrset_set.values_list( 'touched', flat=True)) # If the domain has not been published yet, self.published is None and max() would fail return rrset_touched if not self.published else max( rrset_touched, self.published) except ValueError: # This can be none if the domain was never published and has no records (but there should be at least NS) return self.published @property def is_locally_registrable(self): return self.parent_domain_name in settings.LOCAL_PUBLIC_SUFFIXES @property def _owner_or_none(self): try: return self.owner except Domain.owner.RelatedObjectDoesNotExist: return None @property def parent_domain_name(self): return self._partitioned_name[1] @property def _partitioned_name(self): subname, _, parent_name = self.name.partition('.') return subname, parent_name or None def save(self, *args, **kwargs): self.full_clean(validate_unique=False) super().save(*args, **kwargs) def update_delegation(self, child_domain: Domain): child_subname, child_domain_name = child_domain._partitioned_name if self.name != child_domain_name: raise ValueError( 'Cannot update delegation of %s as it is not an immediate child domain of %s.' % (child_domain.name, self.name)) if child_domain.pk: # Domain real: set delegation child_keys = child_domain.keys if not child_keys: raise APIException( 'Cannot delegate %s, as it currently has no keys.' % child_domain.name) RRset.objects.create(domain=self, subname=child_subname, type='NS', ttl=3600, contents=settings.DEFAULT_NS) RRset.objects.create( domain=self, subname=child_subname, type='DS', ttl=300, contents=[ds for k in child_keys for ds in k['ds']]) metrics.get('desecapi_autodelegation_created').inc() else: # Domain not real: remove delegation for rrset in self.rrset_set.filter(subname=child_subname, type__in=['NS', 'DS']): rrset.delete() metrics.get('desecapi_autodelegation_deleted').inc() def delete(self): ret = super().delete() logger.warning(f'Domain {self.name} deleted (owner: {self.owner.pk})') return ret def __str__(self): return self.name class Meta: ordering = ('created', )
class RRset(ExportModelOperationsMixin('RRset'), models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) created = models.DateTimeField(auto_now_add=True) touched = models.DateTimeField(auto_now=True, db_index=True) domain = models.ForeignKey(Domain, on_delete=models.CASCADE) subname = models.CharField( max_length=178, blank=True, validators=[ validate_lower, RegexValidator( regex= r'^([*]|(([*][.])?([a-z0-9_-]{1,63}[.])*[a-z0-9_-]{1,63}))$', message= 'Subname can only use (lowercase) a-z, 0-9, ., -, and _, ' 'may start with a \'*.\', or just be \'*\'. Components may not exceed 63 characters.', code='invalid_subname') ]) type = models.CharField( max_length=10, validators=[ validate_upper, RegexValidator( regex=r'^[A-Z][A-Z0-9]*$', message= 'Type must be uppercase alphanumeric and start with a letter.', code='invalid_type') ]) ttl = models.PositiveIntegerField() objects = RRsetManager() class Meta: constraints = [ ExclusionConstraint( name='cname_exclusivity', expressions=[ ('domain', RangeOperators.EQUAL), ('subname', RangeOperators.EQUAL), (RawSQL("int4(type = 'CNAME')", ()), RangeOperators.NOT_EQUAL), ], ), ] unique_together = (("domain", "subname", "type"), ) @staticmethod def construct_name(subname, domain_name): return '.'.join(filter(None, [subname, domain_name])) + '.' @property def name(self): return self.construct_name(self.subname, self.domain.name) def save(self, *args, **kwargs): self.full_clean(validate_unique=False) super().save(*args, **kwargs) def clean_records(self, records_presentation_format): """ Validates the records belonging to this set. Validation rules follow the DNS specification; some types may incur additional validation rules. Raises ValidationError if violation of DNS specification is found. Returns a set of records in canonical presentation format. :param records_presentation_format: iterable of records in presentation format """ errors = [] if self.type == 'CNAME': if self.subname == '': errors.append('CNAME RRset cannot have empty subname.') if len(records_presentation_format) > 1: errors.append('CNAME RRset cannot have multiple records.') def _error_msg(record, detail): return f'Record content of {self.type} {self.name} invalid: \'{record}\': {detail}' records_canonical_format = set() for r in records_presentation_format: try: r_canonical_format = RR.canonical_presentation_format( r, self.type) except ValueError as ex: errors.append(_error_msg(r, str(ex))) else: if r_canonical_format in records_canonical_format: errors.append( _error_msg( r, f'Duplicate record content: this is identical to ' f'\'{r_canonical_format}\'')) else: records_canonical_format.add(r_canonical_format) if any(errors): raise ValidationError(errors) return records_canonical_format def save_records(self, records): """ Updates this RR set's resource records, discarding any old values. Records are expected in presentation format and are converted to canonical presentation format (e.g., 127.00.0.1 will be converted to 127.0.0.1). Raises if a invalid set of records is provided. This method triggers the following database queries: - one DELETE query - one SELECT query for comparison of old with new records - one INSERT query, if one or more records were added Changes are saved to the database immediately. :param records: list of records in presentation format """ new_records = self.clean_records(records) # Delete RRs that are not in the new record list from the DB self.records.exclude(content__in=new_records).delete() # one DELETE # Retrieve all remaining RRs from the DB unchanged_records = set(r.content for r in self.records.all()) # one SELECT # Save missing RRs from the new record list to the DB added_records = new_records - unchanged_records rrs = [RR(rrset=self, content=content) for content in added_records] RR.objects.bulk_create(rrs) # One INSERT def __str__(self): return '<RRSet %s domain=%s type=%s subname=%s>' % ( self.pk, self.domain.name, self.type, self.subname)