models.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 AAAA, CDS, DLV, DS, LongQuotedTXT, MX, NS, SRV
  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. # Always remove delegation so that we con properly recreate it
  317. for rrset in self.rrset_set.filter(subname=child_subname, type__in=['NS', 'DS']):
  318. rrset.delete()
  319. if child_domain.pk:
  320. # Domain real: (re-)set delegation
  321. child_keys = child_domain.keys
  322. if not child_keys:
  323. raise APIException('Cannot delegate %s, as it currently has no keys.' % child_domain.name)
  324. RRset.objects.create(domain=self, subname=child_subname, type='NS', ttl=3600, contents=settings.DEFAULT_NS)
  325. RRset.objects.create(domain=self, subname=child_subname, type='DS', ttl=300,
  326. contents=[ds for k in child_keys for ds in k['ds']])
  327. metrics.get('desecapi_autodelegation_created').inc()
  328. else:
  329. # Domain not real: that's it
  330. metrics.get('desecapi_autodelegation_deleted').inc()
  331. def delete(self):
  332. ret = super().delete()
  333. logger.warning(f'Domain {self.name} deleted (owner: {self.owner.pk})')
  334. return ret
  335. def __str__(self):
  336. return self.name
  337. class Meta:
  338. ordering = ('created',)
  339. class Token(ExportModelOperationsMixin('Token'), rest_framework.authtoken.models.Token):
  340. @staticmethod
  341. def _allowed_subnets_default():
  342. return [ipaddress.IPv4Network('0.0.0.0/0'), ipaddress.IPv6Network('::/0')]
  343. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  344. key = models.CharField("Key", max_length=128, db_index=True, unique=True)
  345. user = models.ForeignKey(
  346. User, related_name='auth_tokens',
  347. on_delete=models.CASCADE, verbose_name="User"
  348. )
  349. name = models.CharField('Name', blank=True, max_length=64)
  350. last_used = models.DateTimeField(null=True, blank=True)
  351. perm_manage_tokens = models.BooleanField(default=False)
  352. allowed_subnets = ArrayField(CidrAddressField(), default=_allowed_subnets_default.__func__)
  353. max_age = models.DurationField(null=True, default=None, validators=[MinValueValidator(timedelta(0))])
  354. max_unused_period = models.DurationField(null=True, default=None, validators=[MinValueValidator(timedelta(0))])
  355. plain = None
  356. objects = NetManager()
  357. @property
  358. def is_valid(self):
  359. now = timezone.now()
  360. # Check max age
  361. try:
  362. if self.created + self.max_age < now:
  363. return False
  364. except TypeError:
  365. pass
  366. # Check regular usage requirement
  367. try:
  368. if (self.last_used or self.created) + self.max_unused_period < now:
  369. return False
  370. except TypeError:
  371. pass
  372. return True
  373. def generate_key(self):
  374. self.plain = secrets.token_urlsafe(21)
  375. self.key = Token.make_hash(self.plain)
  376. return self.key
  377. @staticmethod
  378. def make_hash(plain):
  379. return make_password(plain, salt='static', hasher='pbkdf2_sha256_iter1')
  380. class Donation(ExportModelOperationsMixin('Donation'), models.Model):
  381. @staticmethod
  382. def _created_default():
  383. return timezone.now()
  384. @staticmethod
  385. def _due_default():
  386. return timezone.now() + timedelta(days=7)
  387. @staticmethod
  388. def _mref_default():
  389. return "ONDON" + str(time.time())
  390. class Interval(models.IntegerChoices):
  391. ONCE = 0
  392. MONTHLY = 1
  393. QUARTERLY = 3
  394. created = models.DateTimeField(default=_created_default.__func__)
  395. name = models.CharField(max_length=255)
  396. iban = models.CharField(max_length=34)
  397. bic = models.CharField(max_length=11, blank=True)
  398. amount = models.DecimalField(max_digits=8, decimal_places=2)
  399. message = models.CharField(max_length=255, blank=True)
  400. due = models.DateTimeField(default=_due_default.__func__)
  401. mref = models.CharField(max_length=32, default=_mref_default.__func__)
  402. interval = models.IntegerField(choices=Interval.choices, default=Interval.ONCE)
  403. email = models.EmailField(max_length=255, blank=True)
  404. class Meta:
  405. managed = False
  406. @property
  407. def interval_label(self):
  408. return dict(self.Interval.choices)[self.interval]
  409. # RR set types: the good, the bad, and the ugly
  410. # known, but unsupported types
  411. RR_SET_TYPES_UNSUPPORTED = {
  412. 'ALIAS', # Requires signing at the frontend, hence unsupported in desec-stack
  413. 'DNAME', # "do not combine with DNSSEC", https://doc.powerdns.com/authoritative/settings.html#dname-processing
  414. 'IPSECKEY', # broken in pdns, https://github.com/PowerDNS/pdns/issues/9055 TODO enable with pdns auth 4.5.0
  415. 'KEY', # Application use restricted by RFC 3445, DNSSEC use replaced by DNSKEY and handled automatically
  416. 'WKS', # General usage not recommended, "SHOULD NOT" be used in SMTP (RFC 1123)
  417. }
  418. # restricted types are managed in use by the API, and cannot directly be modified by the API client
  419. RR_SET_TYPES_AUTOMATIC = {
  420. # corresponding functionality is automatically managed:
  421. 'KEY', 'NSEC', 'NSEC3', 'OPT', 'RRSIG',
  422. # automatically managed by the API:
  423. 'NSEC3PARAM', 'SOA'
  424. }
  425. # backend types are types that are the types supported by the backend(s)
  426. RR_SET_TYPES_BACKEND = pdns.SUPPORTED_RRSET_TYPES
  427. # validation types are types supported by the validation backend, currently: dnspython
  428. RR_SET_TYPES_VALIDATION = set(ANY.__all__) | set(IN.__all__) \
  429. | {'HTTPS', 'SVCB'} # https://github.com/rthalley/dnspython/pull/624
  430. # manageable types are directly managed by the API client
  431. RR_SET_TYPES_MANAGEABLE = \
  432. (RR_SET_TYPES_BACKEND & RR_SET_TYPES_VALIDATION) - RR_SET_TYPES_UNSUPPORTED - RR_SET_TYPES_AUTOMATIC
  433. class RRsetManager(Manager):
  434. def create(self, contents=None, **kwargs):
  435. rrset = super().create(**kwargs)
  436. for content in contents or []:
  437. RR.objects.create(rrset=rrset, content=content)
  438. return rrset
  439. class RRset(ExportModelOperationsMixin('RRset'), models.Model):
  440. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  441. created = models.DateTimeField(auto_now_add=True)
  442. touched = models.DateTimeField(auto_now=True, db_index=True)
  443. domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
  444. subname = models.CharField(
  445. max_length=178,
  446. blank=True,
  447. validators=[
  448. validate_lower,
  449. RegexValidator(
  450. regex=r'^([*]|(([*][.])?([a-z0-9_-]{1,63}[.])*[a-z0-9_-]{1,63}))$',
  451. message='Subname can only use (lowercase) a-z, 0-9, ., -, and _, '
  452. 'may start with a \'*.\', or just be \'*\'. Components may not exceed 63 characters.',
  453. code='invalid_subname'
  454. )
  455. ]
  456. )
  457. type = models.CharField(
  458. max_length=10,
  459. validators=[
  460. validate_upper,
  461. RegexValidator(
  462. regex=r'^[A-Z][A-Z0-9]*$',
  463. message='Type must be uppercase alphanumeric and start with a letter.',
  464. code='invalid_type'
  465. )
  466. ]
  467. )
  468. ttl = models.PositiveIntegerField()
  469. objects = RRsetManager()
  470. class Meta:
  471. constraints = [
  472. ExclusionConstraint(
  473. name='cname_exclusivity',
  474. expressions=[
  475. ('domain', RangeOperators.EQUAL),
  476. ('subname', RangeOperators.EQUAL),
  477. (RawSQL("int4(type = 'CNAME')", ()), RangeOperators.NOT_EQUAL),
  478. ],
  479. ),
  480. ]
  481. unique_together = (("domain", "subname", "type"),)
  482. @staticmethod
  483. def construct_name(subname, domain_name):
  484. return '.'.join(filter(None, [subname, domain_name])) + '.'
  485. @property
  486. def name(self):
  487. return self.construct_name(self.subname, self.domain.name)
  488. def save(self, *args, **kwargs):
  489. # TODO Enforce that subname and type aren't changed. https://github.com/desec-io/desec-stack/issues/553
  490. self.full_clean(validate_unique=False)
  491. super().save(*args, **kwargs)
  492. def clean_records(self, records_presentation_format):
  493. """
  494. Validates the records belonging to this set. Validation rules follow the DNS specification; some types may
  495. incur additional validation rules.
  496. Raises ValidationError if violation of DNS specification is found.
  497. Returns a set of records in canonical presentation format.
  498. :param records_presentation_format: iterable of records in presentation format
  499. """
  500. errors = []
  501. if self.type == 'CNAME':
  502. if self.subname == '':
  503. errors.append('CNAME RRset cannot have empty subname.')
  504. if len(records_presentation_format) > 1:
  505. errors.append('CNAME RRset cannot have multiple records.')
  506. def _error_msg(record, detail):
  507. return f'Record content of {self.type} {self.name} invalid: \'{record}\': {detail}'
  508. records_canonical_format = set()
  509. for r in records_presentation_format:
  510. try:
  511. r_canonical_format = RR.canonical_presentation_format(r, self.type)
  512. except ValueError as ex:
  513. errors.append(_error_msg(r, str(ex)))
  514. else:
  515. if r_canonical_format in records_canonical_format:
  516. errors.append(_error_msg(r, f'Duplicate record content: this is identical to '
  517. f'\'{r_canonical_format}\''))
  518. else:
  519. records_canonical_format.add(r_canonical_format)
  520. if any(errors):
  521. raise ValidationError(errors)
  522. return records_canonical_format
  523. def save_records(self, records):
  524. """
  525. Updates this RR set's resource records, discarding any old values.
  526. Records are expected in presentation format and are converted to canonical
  527. presentation format (e.g., 127.00.0.1 will be converted to 127.0.0.1).
  528. Raises if a invalid set of records is provided.
  529. This method triggers the following database queries:
  530. - one DELETE query
  531. - one SELECT query for comparison of old with new records
  532. - one INSERT query, if one or more records were added
  533. Changes are saved to the database immediately.
  534. :param records: list of records in presentation format
  535. """
  536. new_records = self.clean_records(records)
  537. # Delete RRs that are not in the new record list from the DB
  538. self.records.exclude(content__in=new_records).delete() # one DELETE
  539. # Retrieve all remaining RRs from the DB
  540. unchanged_records = set(r.content for r in self.records.all()) # one SELECT
  541. # Save missing RRs from the new record list to the DB
  542. added_records = new_records - unchanged_records
  543. rrs = [RR(rrset=self, content=content) for content in added_records]
  544. RR.objects.bulk_create(rrs) # One INSERT
  545. def __str__(self):
  546. return '<RRSet %s domain=%s type=%s subname=%s>' % (self.pk, self.domain.name, self.type, self.subname)
  547. class RRManager(Manager):
  548. def bulk_create(self, rrs, **kwargs):
  549. ret = super().bulk_create(rrs, **kwargs)
  550. # For each rrset, save once to set RRset.updated timestamp and trigger signal for post-save processing
  551. rrsets = {rr.rrset for rr in rrs}
  552. for rrset in rrsets:
  553. rrset.save()
  554. return ret
  555. class RR(ExportModelOperationsMixin('RR'), models.Model):
  556. created = models.DateTimeField(auto_now_add=True)
  557. rrset = models.ForeignKey(RRset, on_delete=models.CASCADE, related_name='records')
  558. content = models.TextField()
  559. objects = RRManager()
  560. _type_map = {
  561. dns.rdatatype.AAAA: AAAA, # TODO remove when https://github.com/PowerDNS/pdns/issues/8182 is fixed
  562. dns.rdatatype.CDS: CDS, # TODO remove when https://github.com/rthalley/dnspython/pull/625 is in main codebase
  563. dns.rdatatype.DLV: DLV, # TODO remove when https://github.com/rthalley/dnspython/pull/625 is in main codebase
  564. dns.rdatatype.DS: DS, # TODO remove when https://github.com/rthalley/dnspython/pull/625 is in main codebase
  565. dns.rdatatype.MX: MX, # do DNS name validation the same way as pdns
  566. dns.rdatatype.NS: NS, # do DNS name validation the same way as pdns
  567. dns.rdatatype.SRV: SRV, # do DNS name validation the same way as pdns
  568. dns.rdatatype.TXT: LongQuotedTXT, # we slightly deviate from RFC 1035 and allow tokens longer than 255 bytes
  569. dns.rdatatype.SPF: LongQuotedTXT, # we slightly deviate from RFC 1035 and allow tokens longer than 255 bytes
  570. }
  571. @staticmethod
  572. def canonical_presentation_format(any_presentation_format, type_):
  573. """
  574. Converts any valid presentation format for a RR into it's canonical presentation format.
  575. Raises if provided presentation format is invalid.
  576. """
  577. rdtype = rdatatype.from_text(type_)
  578. try:
  579. # Convert to wire format, ensuring input validation.
  580. cls = RR._type_map.get(rdtype, dns.rdata)
  581. wire = cls.from_text(
  582. rdclass=rdataclass.IN,
  583. rdtype=rdtype,
  584. tok=dns.tokenizer.Tokenizer(any_presentation_format),
  585. relativize=False
  586. ).to_digestable()
  587. if len(wire) > 64000:
  588. raise ValidationError(f'Ensure this value has no more than 64000 byte in wire format (it has {len(wire)}).')
  589. parser = dns.wire.Parser(wire, current=0)
  590. with parser.restrict_to(len(wire)):
  591. rdata = cls.from_wire_parser(rdclass=rdataclass.IN, rdtype=rdtype, parser=parser)
  592. # Convert to canonical presentation format, disable chunking of records.
  593. # Exempt types which have chunksize hardcoded (prevents "got multiple values for keyword argument 'chunksize'").
  594. chunksize_exception_types = (dns.rdatatype.OPENPGPKEY, dns.rdatatype.EUI48, dns.rdatatype.EUI64)
  595. if rdtype in chunksize_exception_types:
  596. return rdata.to_text()
  597. else:
  598. return rdata.to_text(chunksize=0)
  599. except binascii.Error:
  600. # e.g., odd-length string
  601. raise ValueError('Cannot parse hexadecimal or base64 record contents')
  602. except dns.exception.SyntaxError as e:
  603. # e.g., A/127.0.0.999
  604. if 'quote' in e.args[0]:
  605. raise ValueError(f'Data for {type_} records must be given using quotation marks.')
  606. else:
  607. raise ValueError(f'Record content for type {type_} malformed: {",".join(e.args)}')
  608. except dns.name.NeedAbsoluteNameOrOrigin:
  609. raise ValueError('Hostname must be fully qualified (i.e., end in a dot: "example.com.")')
  610. except ValueError as ex:
  611. # e.g., string ("asdf") cannot be parsed into int on base 10
  612. raise ValueError(f'Cannot parse record contents: {ex}')
  613. except Exception as e:
  614. # TODO see what exceptions raise here for faulty input
  615. raise e
  616. def __str__(self):
  617. return '<RR %s %s rr_set=%s>' % (self.pk, self.content, self.rrset.pk)
  618. class AuthenticatedAction(models.Model):
  619. """
  620. Represents a procedure call on a defined set of arguments.
  621. Subclasses can define additional arguments by adding Django model fields and must define the action to be taken by
  622. implementing the `_act` method.
  623. AuthenticatedAction provides the `state` property which by default is a hash of the action type (defined by the
  624. action's class path). Other information such as user state can be included in the state hash by (carefully)
  625. overriding the `_state_fields` property. Instantiation of the model, if given a `state` kwarg, will raise an error
  626. if the given state argument does not match the state computed from `_state_fields` at the moment of instantiation.
  627. The same applies to the `act` method: If called on an object that was instantiated without a `state` kwargs, an
  628. error will be raised.
  629. This effectively allows hash-authenticated procedure calls by third parties as long as the server-side state is
  630. unaltered, according to the following protocol:
  631. (1) Instantiate the AuthenticatedAction subclass representing the action to be taken (no `state` kwarg here),
  632. (2) provide information on how to instantiate the instance, and the state hash, to a third party,
  633. (3) when provided with data that allows instantiation and a valid state hash, take the defined action, possibly with
  634. additional parameters chosen by the third party that do not belong to the verified state.
  635. """
  636. _validated = False
  637. class Meta:
  638. managed = False
  639. def __init__(self, *args, **kwargs):
  640. state = kwargs.pop('state', None)
  641. super().__init__(*args, **kwargs)
  642. if state is not None:
  643. self._validated = self.validate_state(state)
  644. if not self._validated:
  645. raise ValueError
  646. @property
  647. def _state_fields(self):
  648. """
  649. Returns a list that defines the state of this action (used for authentication of this action).
  650. Return value must be JSON-serializable.
  651. Values not included in the return value will not be used for authentication, i.e. those values can be varied
  652. freely and function as unauthenticated action input parameters.
  653. Use caution when overriding this method. You will usually want to append a value to the list returned by the
  654. parent. Overriding the behavior altogether could result in reducing the state to fewer variables, resulting
  655. in valid signatures when they were intended to be invalid. The suggested method for overriding is
  656. @property
  657. def _state_fields:
  658. return super()._state_fields + [self.important_value, self.another_added_value]
  659. :return: List of values to be signed.
  660. """
  661. name = '.'.join([self.__module__, self.__class__.__qualname__])
  662. return [name]
  663. @property
  664. def state(self):
  665. state = json.dumps(self._state_fields).encode()
  666. hash = sha256()
  667. hash.update(state)
  668. return hash.hexdigest()
  669. def validate_state(self, value):
  670. return value == self.state
  671. def _act(self):
  672. """
  673. Conduct the action represented by this class.
  674. :return: None
  675. """
  676. raise NotImplementedError
  677. def act(self):
  678. if not self._validated:
  679. raise RuntimeError('Action state could not be verified.')
  680. return self._act()
  681. class AuthenticatedBasicUserAction(AuthenticatedAction):
  682. """
  683. Abstract AuthenticatedAction involving a user instance.
  684. """
  685. user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
  686. class Meta:
  687. managed = False
  688. @property
  689. def _state_fields(self):
  690. return super()._state_fields + [str(self.user.id)]
  691. class AuthenticatedUserAction(AuthenticatedBasicUserAction):
  692. """
  693. Abstract AuthenticatedBasicUserAction, incorporating the user's id, email, password, and is_active flag into the
  694. Message Authentication Code state.
  695. """
  696. class Meta:
  697. managed = False
  698. @property
  699. def _state_fields(self):
  700. # TODO consider adding a "last change" attribute of the user to the state to avoid code
  701. # re-use after the the state has been changed and changed back.
  702. return super()._state_fields + [self.user.email, self.user.password, self.user.is_active]
  703. class AuthenticatedActivateUserAction(AuthenticatedUserAction):
  704. domain = models.CharField(max_length=191)
  705. class Meta:
  706. managed = False
  707. @property
  708. def _state_fields(self):
  709. return super()._state_fields + [self.domain]
  710. def _act(self):
  711. self.user.activate()
  712. class AuthenticatedChangeEmailUserAction(AuthenticatedUserAction):
  713. new_email = models.EmailField()
  714. class Meta:
  715. managed = False
  716. @property
  717. def _state_fields(self):
  718. return super()._state_fields + [self.new_email]
  719. def _act(self):
  720. self.user.change_email(self.new_email)
  721. class AuthenticatedResetPasswordUserAction(AuthenticatedUserAction):
  722. new_password = models.CharField(max_length=128)
  723. class Meta:
  724. managed = False
  725. def _act(self):
  726. self.user.change_password(self.new_password)
  727. class AuthenticatedDeleteUserAction(AuthenticatedUserAction):
  728. class Meta:
  729. managed = False
  730. def _act(self):
  731. self.user.delete()
  732. class AuthenticatedDomainBasicUserAction(AuthenticatedBasicUserAction):
  733. """
  734. Abstract AuthenticatedUserAction involving an domain instance, incorporating the domain's id, name as well as the
  735. owner ID into the Message Authentication Code state.
  736. """
  737. domain = models.ForeignKey(Domain, on_delete=models.DO_NOTHING)
  738. class Meta:
  739. managed = False
  740. @property
  741. def _state_fields(self):
  742. return super()._state_fields + [
  743. str(self.domain.id), # ensures the domain object is identical
  744. self.domain.name, # exclude renamed domains
  745. str(self.domain.owner.id), # exclude transferred domains
  746. ]
  747. class AuthenticatedRenewDomainBasicUserAction(AuthenticatedDomainBasicUserAction):
  748. class Meta:
  749. managed = False
  750. @property
  751. def _state_fields(self):
  752. return super()._state_fields + [str(self.domain.renewal_changed)]
  753. def _act(self):
  754. self.domain.renewal_state = Domain.RenewalState.FRESH
  755. self.domain.renewal_changed = timezone.now()
  756. self.domain.save(update_fields=['renewal_state', 'renewal_changed'])
  757. def captcha_default_content(kind: str) -> str:
  758. if kind == Captcha.Kind.IMAGE:
  759. alphabet = (string.ascii_uppercase + string.digits).translate({ord(c): None for c in 'IO0'})
  760. length = 5
  761. elif kind == Captcha.Kind.AUDIO:
  762. alphabet = string.digits
  763. length = 8
  764. else:
  765. raise ValueError(f'Unknown Captcha kind: {kind}')
  766. content = ''.join([secrets.choice(alphabet) for _ in range(length)])
  767. metrics.get('desecapi_captcha_content_created').labels(kind).inc()
  768. return content
  769. class Captcha(ExportModelOperationsMixin('Captcha'), models.Model):
  770. class Kind(models.TextChoices):
  771. IMAGE = 'image'
  772. AUDIO = 'audio'
  773. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  774. created = models.DateTimeField(auto_now_add=True)
  775. content = models.CharField(max_length=24, default="")
  776. kind = models.CharField(choices=Kind.choices, default=Kind.IMAGE, max_length=24)
  777. def __init__(self, *args, **kwargs):
  778. super().__init__(*args, **kwargs)
  779. if not self.content:
  780. self.content = captcha_default_content(self.kind)
  781. def verify(self, solution: str):
  782. age = timezone.now() - self.created
  783. self.delete()
  784. return (
  785. str(solution).upper().strip() == self.content # solution correct
  786. and
  787. age <= settings.CAPTCHA_VALIDITY_PERIOD # not expired
  788. )