models.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. from __future__ import annotations
  2. import binascii
  3. import ipaddress
  4. import json
  5. import logging
  6. import re
  7. import secrets
  8. import string
  9. import time
  10. import uuid
  11. from datetime import timedelta
  12. from functools import cached_property
  13. from hashlib import sha256
  14. import dns
  15. import psl_dns
  16. import rest_framework.authtoken.models
  17. from django.conf import settings
  18. from django.contrib.auth.hashers import make_password
  19. from django.contrib.auth.models import AbstractBaseUser, AnonymousUser, BaseUserManager
  20. from django.contrib.postgres.constraints import ExclusionConstraint
  21. from django.contrib.postgres.fields import ArrayField, CIEmailField, RangeOperators
  22. from django.core.exceptions import ValidationError
  23. from django.core.mail import EmailMessage, get_connection
  24. from django.core.validators import MinValueValidator, RegexValidator
  25. from django.db import models
  26. from django.db.models import CharField, F, Manager, Q, Value
  27. from django.db.models.expressions import RawSQL
  28. from django.db.models.functions import Concat, Length
  29. from django.template.loader import get_template
  30. from django.utils import timezone
  31. from django_prometheus.models import ExportModelOperationsMixin
  32. from dns import rdataclass, rdatatype
  33. from dns.exception import Timeout
  34. from dns.rdtypes import ANY, IN
  35. from dns.resolver import NoNameservers
  36. from netfields import CidrAddressField, NetManager
  37. from rest_framework.exceptions import APIException
  38. from desecapi import metrics
  39. from desecapi import pdns
  40. from desecapi.dns import CDS, DLV, DS, LongQuotedTXT
  41. logger = logging.getLogger(__name__)
  42. psl = psl_dns.PSL(resolver=settings.PSL_RESOLVER, timeout=.5)
  43. def validate_lower(value):
  44. if value != value.lower():
  45. raise ValidationError('Invalid value (not lowercase): %(value)s',
  46. code='invalid',
  47. params={'value': value})
  48. def validate_upper(value):
  49. if value != value.upper():
  50. raise ValidationError('Invalid value (not uppercase): %(value)s',
  51. code='invalid',
  52. params={'value': value})
  53. class MyUserManager(BaseUserManager):
  54. def create_user(self, email, password, **extra_fields):
  55. """
  56. Creates and saves a User with the given email, date of
  57. birth and password.
  58. """
  59. if not email:
  60. raise ValueError('Users must have an email address')
  61. email = self.normalize_email(email)
  62. user = self.model(email=email, **extra_fields)
  63. user.set_password(password)
  64. user.save(using=self._db)
  65. return user
  66. def create_superuser(self, email, password):
  67. """
  68. Creates and saves a superuser with the given email, date of
  69. birth and password.
  70. """
  71. user = self.create_user(email, password=password)
  72. user.is_admin = True
  73. user.save(using=self._db)
  74. return user
  75. class User(ExportModelOperationsMixin('User'), AbstractBaseUser):
  76. @staticmethod
  77. def _limit_domains_default():
  78. return settings.LIMIT_USER_DOMAIN_COUNT_DEFAULT
  79. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  80. email = CIEmailField(
  81. verbose_name='email address',
  82. unique=True,
  83. )
  84. is_active = models.BooleanField(default=True)
  85. is_admin = models.BooleanField(default=False)
  86. created = models.DateTimeField(auto_now_add=True)
  87. limit_domains = models.IntegerField(default=_limit_domains_default.__func__, null=True, blank=True)
  88. needs_captcha = models.BooleanField(default=True)
  89. objects = MyUserManager()
  90. USERNAME_FIELD = 'email'
  91. REQUIRED_FIELDS = []
  92. def get_full_name(self):
  93. return self.email
  94. def get_short_name(self):
  95. return self.email
  96. def __str__(self):
  97. return self.email
  98. # noinspection PyMethodMayBeStatic
  99. def has_perm(self, *_):
  100. """Does the user have a specific permission?"""
  101. # Simplest possible answer: Yes, always
  102. return True
  103. # noinspection PyMethodMayBeStatic
  104. def has_module_perms(self, *_):
  105. """Does the user have permissions to view the app `app_label`?"""
  106. # Simplest possible answer: Yes, always
  107. return True
  108. @property
  109. def is_staff(self):
  110. """Is the user a member of staff?"""
  111. # Simplest possible answer: All admins are staff
  112. return self.is_admin
  113. def activate(self):
  114. self.is_active = True
  115. self.needs_captcha = False
  116. self.save()
  117. def change_email(self, email):
  118. old_email = self.email
  119. self.email = email
  120. self.validate_unique()
  121. self.save()
  122. self.send_email('change-email-confirmation-old-email', recipient=old_email)
  123. def change_password(self, raw_password):
  124. self.set_password(raw_password)
  125. self.save()
  126. self.send_email('password-change-confirmation')
  127. def delete(self):
  128. pk = self.pk
  129. ret = super().delete()
  130. logger.warning(f'User {pk} deleted')
  131. return ret
  132. def send_email(self, reason, context=None, recipient=None):
  133. fast_lane = 'email_fast_lane'
  134. slow_lane = 'email_slow_lane'
  135. immediate_lane = 'email_immediate_lane'
  136. lanes = {
  137. 'activate': slow_lane,
  138. 'activate-with-domain': slow_lane,
  139. 'change-email': slow_lane,
  140. 'change-email-confirmation-old-email': fast_lane,
  141. 'password-change-confirmation': fast_lane,
  142. 'reset-password': fast_lane,
  143. 'delete-user': fast_lane,
  144. 'domain-dyndns': fast_lane,
  145. 'renew-domain': immediate_lane,
  146. }
  147. if reason not in lanes:
  148. raise ValueError(f'Cannot send email to user {self.pk} without a good reason: {reason}')
  149. context = context or {}
  150. content = get_template(f'emails/{reason}/content.txt').render(context)
  151. content += f'\nSupport Reference: user_id = {self.pk}\n'
  152. footer = get_template('emails/footer.txt').render()
  153. logger.warning(f'Queuing email for user account {self.pk} (reason: {reason}, lane: {lanes[reason]})')
  154. num_queued = EmailMessage(
  155. subject=get_template(f'emails/{reason}/subject.txt').render(context).strip(),
  156. body=content + footer,
  157. from_email=get_template('emails/from.txt').render(),
  158. to=[recipient or self.email],
  159. connection=get_connection(lane=lanes[reason], debug={'user': self.pk, 'reason': reason})
  160. ).send()
  161. metrics.get('desecapi_messages_queued').labels(reason, self.pk, lanes[reason]).observe(num_queued)
  162. return num_queued
  163. validate_domain_name = [
  164. validate_lower,
  165. RegexValidator(
  166. regex=r'^(([a-z0-9_-]{1,63})\.)*[a-z0-9-]{1,63}$',
  167. message='Domain names must be labels separated by dots. Labels may consist of up to 63 letters, digits, '
  168. 'hyphens, and underscores. The last label may not contain an underscore.',
  169. code='invalid_domain_name',
  170. flags=re.IGNORECASE
  171. )
  172. ]
  173. class DomainManager(Manager):
  174. def filter_qname(self, qname: str, **kwargs) -> models.query.QuerySet:
  175. try:
  176. Domain._meta.get_field('name').run_validators(qname)
  177. except ValidationError:
  178. raise ValueError
  179. return self.annotate(
  180. dotted_name=Concat(Value('.'), 'name', output_field=CharField()),
  181. dotted_qname=Value(f'.{qname}', output_field=CharField()),
  182. name_length=Length('name'),
  183. ).filter(dotted_qname__endswith=F('dotted_name'), **kwargs)
  184. class Domain(ExportModelOperationsMixin('Domain'), models.Model):
  185. @staticmethod
  186. def _minimum_ttl_default():
  187. return settings.MINIMUM_TTL_DEFAULT
  188. class RenewalState(models.IntegerChoices):
  189. IMMORTAL = 0
  190. FRESH = 1
  191. NOTIFIED = 2
  192. WARNED = 3
  193. created = models.DateTimeField(auto_now_add=True)
  194. name = models.CharField(max_length=191,
  195. unique=True,
  196. validators=validate_domain_name)
  197. owner = models.ForeignKey(User, on_delete=models.PROTECT, related_name='domains')
  198. published = models.DateTimeField(null=True, blank=True)
  199. replicated = models.DateTimeField(null=True, blank=True)
  200. replication_duration = models.DurationField(null=True, blank=True)
  201. minimum_ttl = models.PositiveIntegerField(default=_minimum_ttl_default.__func__)
  202. renewal_state = models.IntegerField(choices=RenewalState.choices, default=RenewalState.IMMORTAL)
  203. renewal_changed = models.DateTimeField(auto_now_add=True)
  204. _keys = None
  205. objects = DomainManager()
  206. def __init__(self, *args, **kwargs):
  207. if isinstance(kwargs.get('owner'), AnonymousUser):
  208. kwargs = {**kwargs, 'owner': None} # make a copy and override
  209. # Avoid super().__init__(owner=None, ...) to not mess up *values instantiation in django.db.models.Model.from_db
  210. super().__init__(*args, **kwargs)
  211. if self.pk is None and kwargs.get('renewal_state') is None and self.is_locally_registrable:
  212. self.renewal_state = Domain.RenewalState.FRESH
  213. @cached_property
  214. def public_suffix(self):
  215. try:
  216. public_suffix = psl.get_public_suffix(self.name)
  217. is_public_suffix = psl.is_public_suffix(self.name)
  218. except (Timeout, NoNameservers):
  219. public_suffix = self.name.rpartition('.')[2]
  220. is_public_suffix = ('.' not in self.name) # TLDs are public suffixes
  221. except psl_dns.exceptions.UnsupportedRule as e:
  222. # It would probably be fine to treat this as a non-public suffix (with the TLD acting as the
  223. # public suffix and setting both public_suffix and is_public_suffix accordingly).
  224. # However, in order to allow to investigate the situation, it's better not catch
  225. # this exception. For web requests, our error handler turns it into a 503 error
  226. # and makes sure admins are notified.
  227. raise e
  228. if is_public_suffix:
  229. return public_suffix
  230. # Take into account that any of the parent domains could be a local public suffix. To that
  231. # end, identify the longest local public suffix that is actually a suffix of domain_name.
  232. for local_public_suffix in settings.LOCAL_PUBLIC_SUFFIXES:
  233. has_local_public_suffix_parent = ('.' + self.name).endswith('.' + local_public_suffix)
  234. if has_local_public_suffix_parent and len(local_public_suffix) > len(public_suffix):
  235. public_suffix = local_public_suffix
  236. return public_suffix
  237. def is_covered_by_foreign_zone(self):
  238. # Generate a list of all domains connecting this one and its public suffix.
  239. # If another user owns a zone with one of these names, then the requested
  240. # domain is unavailable because it is part of the other user's zone.
  241. private_components = self.name.rsplit(self.public_suffix, 1)[0].rstrip('.')
  242. private_components = private_components.split('.') if private_components else []
  243. private_domains = ['.'.join(private_components[i:]) for i in range(0, len(private_components))]
  244. private_domains = [f'{private_domain}.{self.public_suffix}' for private_domain in private_domains]
  245. assert self.name == next(iter(private_domains), self.public_suffix)
  246. # Determine whether domain is covered by other users' zones
  247. return Domain.objects.filter(Q(name__in=private_domains) & ~Q(owner=self._owner_or_none)).exists()
  248. def covers_foreign_zone(self):
  249. # Note: This is not completely accurate: Ideally, we should only consider zones with identical public suffix.
  250. # (If a public suffix lies in between, it's ok.) However, as there could be many descendant zones, the accurate
  251. # check is expensive, so currently not implemented (PSL lookups for each of them).
  252. return Domain.objects.filter(Q(name__endswith=f'.{self.name}') & ~Q(owner=self._owner_or_none)).exists()
  253. def is_registrable(self):
  254. """
  255. Returns False if the domain name is reserved, a public suffix, or covered by / covers another user's domain.
  256. Otherwise, True is returned.
  257. """
  258. self.clean() # ensure .name is a domain name
  259. private_generation = self.name.count('.') - self.public_suffix.count('.')
  260. assert private_generation >= 0
  261. # .internal is reserved
  262. if f'.{self.name}'.endswith('.internal'):
  263. return False
  264. # Public suffixes can only be registered if they are local
  265. if private_generation == 0 and self.name not in settings.LOCAL_PUBLIC_SUFFIXES:
  266. return False
  267. # Disallow _acme-challenge.dedyn.io and the like. Rejects reserved direct children of public suffixes.
  268. reserved_prefixes = ('_', 'autoconfig.', 'autodiscover.',)
  269. if private_generation == 1 and any(self.name.startswith(prefix) for prefix in reserved_prefixes):
  270. return False
  271. # Domains covered by another user's zone can't be registered
  272. if self.is_covered_by_foreign_zone():
  273. return False
  274. # Domains that would cover another user's zone can't be registered
  275. if self.covers_foreign_zone():
  276. return False
  277. return True
  278. @property
  279. def keys(self):
  280. if not self._keys:
  281. self._keys = pdns.get_keys(self)
  282. return self._keys
  283. @property
  284. def touched(self):
  285. try:
  286. rrset_touched = max(updated for updated in self.rrset_set.values_list('touched', flat=True))
  287. # If the domain has not been published yet, self.published is None and max() would fail
  288. return rrset_touched if not self.published else max(rrset_touched, self.published)
  289. except ValueError:
  290. # This can be none if the domain was never published and has no records (but there should be at least NS)
  291. return self.published
  292. @property
  293. def is_locally_registrable(self):
  294. return self.parent_domain_name in settings.LOCAL_PUBLIC_SUFFIXES
  295. @property
  296. def _owner_or_none(self):
  297. try:
  298. return self.owner
  299. except Domain.owner.RelatedObjectDoesNotExist:
  300. return None
  301. @property
  302. def parent_domain_name(self):
  303. return self._partitioned_name[1]
  304. @property
  305. def _partitioned_name(self):
  306. subname, _, parent_name = self.name.partition('.')
  307. return subname, parent_name or None
  308. def save(self, *args, **kwargs):
  309. self.full_clean(validate_unique=False)
  310. super().save(*args, **kwargs)
  311. def update_delegation(self, child_domain: Domain):
  312. child_subname, child_domain_name = child_domain._partitioned_name
  313. if self.name != child_domain_name:
  314. raise ValueError('Cannot update delegation of %s as it is not an immediate child domain of %s.' %
  315. (child_domain.name, self.name))
  316. if child_domain.pk:
  317. # Domain real: set delegation
  318. child_keys = child_domain.keys
  319. if not child_keys:
  320. raise APIException('Cannot delegate %s, as it currently has no keys.' % child_domain.name)
  321. RRset.objects.create(domain=self, subname=child_subname, type='NS', ttl=3600, contents=settings.DEFAULT_NS)
  322. RRset.objects.create(domain=self, subname=child_subname, type='DS', ttl=300,
  323. contents=[ds for k in child_keys for ds in k['ds']])
  324. metrics.get('desecapi_autodelegation_created').inc()
  325. else:
  326. # Domain not real: remove delegation
  327. for rrset in self.rrset_set.filter(subname=child_subname, type__in=['NS', 'DS']):
  328. rrset.delete()
  329. metrics.get('desecapi_autodelegation_deleted').inc()
  330. def delete(self):
  331. ret = super().delete()
  332. logger.warning(f'Domain {self.name} deleted (owner: {self.owner.pk})')
  333. return ret
  334. def __str__(self):
  335. return self.name
  336. class Meta:
  337. ordering = ('created',)
  338. class Token(ExportModelOperationsMixin('Token'), rest_framework.authtoken.models.Token):
  339. @staticmethod
  340. def _allowed_subnets_default():
  341. return [ipaddress.IPv4Network('0.0.0.0/0'), ipaddress.IPv6Network('::/0')]
  342. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  343. key = models.CharField("Key", max_length=128, db_index=True, unique=True)
  344. user = models.ForeignKey(
  345. User, related_name='auth_tokens',
  346. on_delete=models.CASCADE, verbose_name="User"
  347. )
  348. name = models.CharField('Name', blank=True, max_length=64)
  349. last_used = models.DateTimeField(null=True, blank=True)
  350. perm_manage_tokens = models.BooleanField(default=False)
  351. allowed_subnets = ArrayField(CidrAddressField(), default=_allowed_subnets_default.__func__)
  352. max_age = models.DurationField(null=True, default=None, validators=[MinValueValidator(timedelta(0))])
  353. max_unused_period = models.DurationField(null=True, default=None, validators=[MinValueValidator(timedelta(0))])
  354. plain = None
  355. objects = NetManager()
  356. @property
  357. def is_valid(self):
  358. now = timezone.now()
  359. # Check max age
  360. try:
  361. if self.created + self.max_age < now:
  362. return False
  363. except TypeError:
  364. pass
  365. # Check regular usage requirement
  366. try:
  367. if (self.last_used or self.created) + self.max_unused_period < now:
  368. return False
  369. except TypeError:
  370. pass
  371. return True
  372. def generate_key(self):
  373. self.plain = secrets.token_urlsafe(21)
  374. self.key = Token.make_hash(self.plain)
  375. return self.key
  376. @staticmethod
  377. def make_hash(plain):
  378. return make_password(plain, salt='static', hasher='pbkdf2_sha256_iter1')
  379. class Donation(ExportModelOperationsMixin('Donation'), models.Model):
  380. @staticmethod
  381. def _created_default():
  382. return timezone.now()
  383. @staticmethod
  384. def _due_default():
  385. return timezone.now() + timedelta(days=7)
  386. @staticmethod
  387. def _mref_default():
  388. return "ONDON" + str(time.time())
  389. created = models.DateTimeField(default=_created_default.__func__)
  390. name = models.CharField(max_length=255)
  391. iban = models.CharField(max_length=34)
  392. bic = models.CharField(max_length=11, blank=True)
  393. amount = models.DecimalField(max_digits=8, decimal_places=2)
  394. message = models.CharField(max_length=255, blank=True)
  395. due = models.DateTimeField(default=_due_default.__func__)
  396. mref = models.CharField(max_length=32, default=_mref_default.__func__)
  397. email = models.EmailField(max_length=255, blank=True)
  398. class Meta:
  399. managed = False
  400. # RR set types: the good, the bad, and the ugly
  401. # known, but unsupported types
  402. RR_SET_TYPES_UNSUPPORTED = {
  403. 'ALIAS', # Requires signing at the frontend, hence unsupported in desec-stack
  404. 'DNAME', # "do not combine with DNSSEC", https://doc.powerdns.com/authoritative/settings.html#dname-processing
  405. 'IPSECKEY', # broken in pdns, https://github.com/PowerDNS/pdns/issues/9055 TODO enable with pdns auth 4.5.0
  406. 'KEY', # Application use restricted by RFC 3445, DNSSEC use replaced by DNSKEY and handled automatically
  407. 'WKS', # General usage not recommended, "SHOULD NOT" be used in SMTP (RFC 1123)
  408. }
  409. # restricted types are managed in use by the API, and cannot directly be modified by the API client
  410. RR_SET_TYPES_AUTOMATIC = {
  411. # corresponding functionality is automatically managed:
  412. 'KEY', 'NSEC', 'NSEC3', 'OPT', 'RRSIG',
  413. # automatically managed by the API:
  414. 'NSEC3PARAM', 'SOA'
  415. }
  416. # backend types are types that are the types supported by the backend(s)
  417. RR_SET_TYPES_BACKEND = pdns.SUPPORTED_RRSET_TYPES
  418. # validation types are types supported by the validation backend, currently: dnspython
  419. RR_SET_TYPES_VALIDATION = set(ANY.__all__) | set(IN.__all__) \
  420. | {'HTTPS', 'SVCB'} # https://github.com/rthalley/dnspython/pull/624
  421. # manageable types are directly managed by the API client
  422. RR_SET_TYPES_MANAGEABLE = \
  423. (RR_SET_TYPES_BACKEND & RR_SET_TYPES_VALIDATION) - RR_SET_TYPES_UNSUPPORTED - RR_SET_TYPES_AUTOMATIC
  424. class RRsetManager(Manager):
  425. def create(self, contents=None, **kwargs):
  426. rrset = super().create(**kwargs)
  427. for content in contents or []:
  428. RR.objects.create(rrset=rrset, content=content)
  429. return rrset
  430. class RRset(ExportModelOperationsMixin('RRset'), models.Model):
  431. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  432. created = models.DateTimeField(auto_now_add=True)
  433. touched = models.DateTimeField(auto_now=True, db_index=True)
  434. domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
  435. subname = models.CharField(
  436. max_length=178,
  437. blank=True,
  438. validators=[
  439. validate_lower,
  440. RegexValidator(
  441. regex=r'^([*]|(([*][.])?([a-z0-9_-]{1,63}[.])*[a-z0-9_-]{1,63}))$',
  442. message='Subname can only use (lowercase) a-z, 0-9, ., -, and _, '
  443. 'may start with a \'*.\', or just be \'*\'. Components may not exceed 63 characters.',
  444. code='invalid_subname'
  445. )
  446. ]
  447. )
  448. type = models.CharField(
  449. max_length=10,
  450. validators=[
  451. validate_upper,
  452. RegexValidator(
  453. regex=r'^[A-Z][A-Z0-9]*$',
  454. message='Type must be uppercase alphanumeric and start with a letter.',
  455. code='invalid_type'
  456. )
  457. ]
  458. )
  459. ttl = models.PositiveIntegerField()
  460. objects = RRsetManager()
  461. class Meta:
  462. constraints = [
  463. ExclusionConstraint(
  464. name='cname_exclusivity',
  465. expressions=[
  466. ('domain', RangeOperators.EQUAL),
  467. ('subname', RangeOperators.EQUAL),
  468. (RawSQL("int4(type = 'CNAME')", ()), RangeOperators.NOT_EQUAL),
  469. ],
  470. ),
  471. ]
  472. unique_together = (("domain", "subname", "type"),)
  473. @staticmethod
  474. def construct_name(subname, domain_name):
  475. return '.'.join(filter(None, [subname, domain_name])) + '.'
  476. @property
  477. def name(self):
  478. return self.construct_name(self.subname, self.domain.name)
  479. def save(self, *args, **kwargs):
  480. self.full_clean(validate_unique=False)
  481. super().save(*args, **kwargs)
  482. def clean_records(self, records_presentation_format):
  483. """
  484. Validates the records belonging to this set. Validation rules follow the DNS specification; some types may
  485. incur additional validation rules.
  486. Raises ValidationError if violation of DNS specification is found.
  487. Returns a set of records in canonical presentation format.
  488. :param records_presentation_format: iterable of records in presentation format
  489. """
  490. errors = []
  491. if self.type == 'CNAME':
  492. if self.subname == '':
  493. errors.append('CNAME RRset cannot have empty subname.')
  494. if len(records_presentation_format) > 1:
  495. errors.append('CNAME RRset cannot have multiple records.')
  496. def _error_msg(record, detail):
  497. return f'Record content of {self.type} {self.name} invalid: \'{record}\': {detail}'
  498. records_canonical_format = set()
  499. for r in records_presentation_format:
  500. try:
  501. r_canonical_format = RR.canonical_presentation_format(r, self.type)
  502. except ValueError as ex:
  503. errors.append(_error_msg(r, str(ex)))
  504. else:
  505. if r_canonical_format in records_canonical_format:
  506. errors.append(_error_msg(r, f'Duplicate record content: this is identical to '
  507. f'\'{r_canonical_format}\''))
  508. else:
  509. records_canonical_format.add(r_canonical_format)
  510. if any(errors):
  511. raise ValidationError(errors)
  512. return records_canonical_format
  513. def save_records(self, records):
  514. """
  515. Updates this RR set's resource records, discarding any old values.
  516. Records are expected in presentation format and are converted to canonical
  517. presentation format (e.g., 127.00.0.1 will be converted to 127.0.0.1).
  518. Raises if a invalid set of records is provided.
  519. This method triggers the following database queries:
  520. - one DELETE query
  521. - one SELECT query for comparison of old with new records
  522. - one INSERT query, if one or more records were added
  523. Changes are saved to the database immediately.
  524. :param records: list of records in presentation format
  525. """
  526. new_records = self.clean_records(records)
  527. # Delete RRs that are not in the new record list from the DB
  528. self.records.exclude(content__in=new_records).delete() # one DELETE
  529. # Retrieve all remaining RRs from the DB
  530. unchanged_records = set(r.content for r in self.records.all()) # one SELECT
  531. # Save missing RRs from the new record list to the DB
  532. added_records = new_records - unchanged_records
  533. rrs = [RR(rrset=self, content=content) for content in added_records]
  534. RR.objects.bulk_create(rrs) # One INSERT
  535. def __str__(self):
  536. return '<RRSet %s domain=%s type=%s subname=%s>' % (self.pk, self.domain.name, self.type, self.subname)
  537. class RRManager(Manager):
  538. def bulk_create(self, rrs, **kwargs):
  539. ret = super().bulk_create(rrs, **kwargs)
  540. # For each rrset, save once to set RRset.updated timestamp and trigger signal for post-save processing
  541. rrsets = {rr.rrset for rr in rrs}
  542. for rrset in rrsets:
  543. rrset.save()
  544. return ret
  545. class RR(ExportModelOperationsMixin('RR'), models.Model):
  546. created = models.DateTimeField(auto_now_add=True)
  547. rrset = models.ForeignKey(RRset, on_delete=models.CASCADE, related_name='records')
  548. content = models.TextField()
  549. objects = RRManager()
  550. _type_map = {
  551. dns.rdatatype.CDS: CDS, # TODO remove when https://github.com/rthalley/dnspython/pull/625 is in main codebase
  552. dns.rdatatype.DLV: DLV, # TODO remove when https://github.com/rthalley/dnspython/pull/625 is in main codebase
  553. dns.rdatatype.DS: DS, # TODO remove when https://github.com/rthalley/dnspython/pull/625 is in main codebase
  554. dns.rdatatype.TXT: LongQuotedTXT, # we slightly deviate from RFC 1035 and allow tokens longer than 255 bytes
  555. dns.rdatatype.SPF: LongQuotedTXT, # we slightly deviate from RFC 1035 and allow tokens longer than 255 bytes
  556. }
  557. @staticmethod
  558. def canonical_presentation_format(any_presentation_format, type_):
  559. """
  560. Converts any valid presentation format for a RR into it's canonical presentation format.
  561. Raises if provided presentation format is invalid.
  562. """
  563. rdtype = rdatatype.from_text(type_)
  564. try:
  565. # Convert to wire format, ensuring input validation.
  566. cls = RR._type_map.get(rdtype, dns.rdata)
  567. wire = cls.from_text(
  568. rdclass=rdataclass.IN,
  569. rdtype=rdtype,
  570. tok=dns.tokenizer.Tokenizer(any_presentation_format),
  571. relativize=False
  572. ).to_digestable()
  573. if len(wire) > 64000:
  574. raise ValidationError(f'Ensure this value has no more than 64000 byte in wire format (it has {len(wire)}).')
  575. parser = dns.wire.Parser(wire, current=0)
  576. with parser.restrict_to(len(wire)):
  577. rdata = cls.from_wire_parser(rdclass=rdataclass.IN, rdtype=rdtype, parser=parser)
  578. # Convert to canonical presentation format, disable chunking of records.
  579. # Exempt types which have chunksize hardcoded (prevents "got multiple values for keyword argument 'chunksize'").
  580. chunksize_exception_types = (dns.rdatatype.OPENPGPKEY, dns.rdatatype.EUI48, dns.rdatatype.EUI64)
  581. if rdtype in chunksize_exception_types:
  582. return rdata.to_text()
  583. else:
  584. return rdata.to_text(chunksize=0)
  585. except binascii.Error:
  586. # e.g., odd-length string
  587. raise ValueError('Cannot parse hexadecimal or base64 record contents')
  588. except dns.exception.SyntaxError as e:
  589. # e.g., A/127.0.0.999
  590. if 'quote' in e.args[0]:
  591. raise ValueError(f'Data for {type_} records must be given using quotation marks.')
  592. else:
  593. raise ValueError(f'Record content for type {type_} malformed: {",".join(e.args)}')
  594. except dns.name.NeedAbsoluteNameOrOrigin:
  595. raise ValueError('Hostname must be fully qualified (i.e., end in a dot: "example.com.")')
  596. except ValueError as ex:
  597. # e.g., string ("asdf") cannot be parsed into int on base 10
  598. raise ValueError(f'Cannot parse record contents: {ex}')
  599. except Exception as e:
  600. # TODO see what exceptions raise here for faulty input
  601. raise e
  602. def __str__(self):
  603. return '<RR %s %s rr_set=%s>' % (self.pk, self.content, self.rrset.pk)
  604. class AuthenticatedAction(models.Model):
  605. """
  606. Represents a procedure call on a defined set of arguments.
  607. Subclasses can define additional arguments by adding Django model fields and must define the action to be taken by
  608. implementing the `_act` method.
  609. AuthenticatedAction provides the `state` property which by default is a hash of the action type (defined by the
  610. action's class path). Other information such as user state can be included in the state hash by (carefully)
  611. overriding the `_state_fields` property. Instantiation of the model, if given a `state` kwarg, will raise an error
  612. if the given state argument does not match the state computed from `_state_fields` at the moment of instantiation.
  613. The same applies to the `act` method: If called on an object that was instantiated without a `state` kwargs, an
  614. error will be raised.
  615. This effectively allows hash-authenticated procedure calls by third parties as long as the server-side state is
  616. unaltered, according to the following protocol:
  617. (1) Instantiate the AuthenticatedAction subclass representing the action to be taken (no `state` kwarg here),
  618. (2) provide information on how to instantiate the instance, and the state hash, to a third party,
  619. (3) when provided with data that allows instantiation and a valid state hash, take the defined action, possibly with
  620. additional parameters chosen by the third party that do not belong to the verified state.
  621. """
  622. _validated = False
  623. class Meta:
  624. managed = False
  625. def __init__(self, *args, **kwargs):
  626. state = kwargs.pop('state', None)
  627. super().__init__(*args, **kwargs)
  628. if state is not None:
  629. self._validated = self.validate_state(state)
  630. if not self._validated:
  631. raise ValueError
  632. @property
  633. def _state_fields(self):
  634. """
  635. Returns a list that defines the state of this action (used for authentication of this action).
  636. Return value must be JSON-serializable.
  637. Values not included in the return value will not be used for authentication, i.e. those values can be varied
  638. freely and function as unauthenticated action input parameters.
  639. Use caution when overriding this method. You will usually want to append a value to the list returned by the
  640. parent. Overriding the behavior altogether could result in reducing the state to fewer variables, resulting
  641. in valid signatures when they were intended to be invalid. The suggested method for overriding is
  642. @property
  643. def _state_fields:
  644. return super()._state_fields + [self.important_value, self.another_added_value]
  645. :return: List of values to be signed.
  646. """
  647. name = '.'.join([self.__module__, self.__class__.__qualname__])
  648. return [name]
  649. @property
  650. def state(self):
  651. state = json.dumps(self._state_fields).encode()
  652. hash = sha256()
  653. hash.update(state)
  654. return hash.hexdigest()
  655. def validate_state(self, value):
  656. return value == self.state
  657. def _act(self):
  658. """
  659. Conduct the action represented by this class.
  660. :return: None
  661. """
  662. raise NotImplementedError
  663. def act(self):
  664. if not self._validated:
  665. raise RuntimeError('Action state could not be verified.')
  666. return self._act()
  667. class AuthenticatedBasicUserAction(AuthenticatedAction):
  668. """
  669. Abstract AuthenticatedAction involving a user instance.
  670. """
  671. user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
  672. class Meta:
  673. managed = False
  674. @property
  675. def _state_fields(self):
  676. return super()._state_fields + [str(self.user.id)]
  677. class AuthenticatedUserAction(AuthenticatedBasicUserAction):
  678. """
  679. Abstract AuthenticatedBasicUserAction, incorporating the user's id, email, password, and is_active flag into the
  680. Message Authentication Code state.
  681. """
  682. class Meta:
  683. managed = False
  684. @property
  685. def _state_fields(self):
  686. # TODO consider adding a "last change" attribute of the user to the state to avoid code
  687. # re-use after the the state has been changed and changed back.
  688. return super()._state_fields + [self.user.email, self.user.password, self.user.is_active]
  689. class AuthenticatedActivateUserAction(AuthenticatedUserAction):
  690. domain = models.CharField(max_length=191)
  691. class Meta:
  692. managed = False
  693. @property
  694. def _state_fields(self):
  695. return super()._state_fields + [self.domain]
  696. def _act(self):
  697. self.user.activate()
  698. class AuthenticatedChangeEmailUserAction(AuthenticatedUserAction):
  699. new_email = models.EmailField()
  700. class Meta:
  701. managed = False
  702. @property
  703. def _state_fields(self):
  704. return super()._state_fields + [self.new_email]
  705. def _act(self):
  706. self.user.change_email(self.new_email)
  707. class AuthenticatedResetPasswordUserAction(AuthenticatedUserAction):
  708. new_password = models.CharField(max_length=128)
  709. class Meta:
  710. managed = False
  711. def _act(self):
  712. self.user.change_password(self.new_password)
  713. class AuthenticatedDeleteUserAction(AuthenticatedUserAction):
  714. class Meta:
  715. managed = False
  716. def _act(self):
  717. self.user.delete()
  718. class AuthenticatedDomainBasicUserAction(AuthenticatedBasicUserAction):
  719. """
  720. Abstract AuthenticatedUserAction involving an domain instance, incorporating the domain's id, name as well as the
  721. owner ID into the Message Authentication Code state.
  722. """
  723. domain = models.ForeignKey(Domain, on_delete=models.DO_NOTHING)
  724. class Meta:
  725. managed = False
  726. @property
  727. def _state_fields(self):
  728. return super()._state_fields + [
  729. str(self.domain.id), # ensures the domain object is identical
  730. self.domain.name, # exclude renamed domains
  731. str(self.domain.owner.id), # exclude transferred domains
  732. ]
  733. class AuthenticatedRenewDomainBasicUserAction(AuthenticatedDomainBasicUserAction):
  734. class Meta:
  735. managed = False
  736. @property
  737. def _state_fields(self):
  738. return super()._state_fields + [str(self.domain.renewal_changed)]
  739. def _act(self):
  740. self.domain.renewal_state = Domain.RenewalState.FRESH
  741. self.domain.renewal_changed = timezone.now()
  742. self.domain.save(update_fields=['renewal_state', 'renewal_changed'])
  743. def captcha_default_content(kind: str) -> str:
  744. if kind == Captcha.Kind.IMAGE:
  745. alphabet = (string.ascii_uppercase + string.digits).translate({ord(c): None for c in 'IO0'})
  746. length = 5
  747. elif kind == Captcha.Kind.AUDIO:
  748. alphabet = string.digits
  749. length = 8
  750. else:
  751. raise ValueError(f'Unknown Captcha kind: {kind}')
  752. content = ''.join([secrets.choice(alphabet) for _ in range(length)])
  753. metrics.get('desecapi_captcha_content_created').labels(kind).inc()
  754. return content
  755. class Captcha(ExportModelOperationsMixin('Captcha'), models.Model):
  756. class Kind(models.TextChoices):
  757. IMAGE = 'image'
  758. AUDIO = 'audio'
  759. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  760. created = models.DateTimeField(auto_now_add=True)
  761. content = models.CharField(max_length=24, default="")
  762. kind = models.CharField(choices=Kind.choices, default=Kind.IMAGE, max_length=24)
  763. def __init__(self, *args, **kwargs):
  764. super().__init__(*args, **kwargs)
  765. if not self.content:
  766. self.content = captcha_default_content(self.kind)
  767. def verify(self, solution: str):
  768. age = timezone.now() - self.created
  769. self.delete()
  770. return (
  771. str(solution).upper().strip() == self.content # solution correct
  772. and
  773. age <= settings.CAPTCHA_VALIDITY_PERIOD # not expired
  774. )