models.py 33 KB

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