models.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. from __future__ import annotations
  2. import json
  3. import logging
  4. import secrets
  5. import string
  6. import time
  7. import uuid
  8. from base64 import urlsafe_b64encode
  9. from datetime import timedelta
  10. from hashlib import sha256
  11. from os import urandom
  12. import psl_dns
  13. import rest_framework.authtoken.models
  14. from django.conf import settings
  15. from django.contrib.auth.hashers import make_password
  16. from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, AnonymousUser
  17. from django.core.exceptions import ValidationError
  18. from django.core.mail import EmailMessage, get_connection
  19. from django.core.validators import RegexValidator
  20. from django.db import models
  21. from django.db.models import Manager, Q
  22. from django.template.loader import get_template
  23. from django.utils import timezone
  24. from dns.exception import Timeout
  25. from dns.resolver import NoNameservers
  26. from rest_framework.exceptions import APIException
  27. from desecapi import pdns
  28. logger = logging.getLogger(__name__)
  29. psl = psl_dns.PSL(resolver=settings.PSL_RESOLVER, timeout=.5)
  30. def validate_lower(value):
  31. if value != value.lower():
  32. raise ValidationError('Invalid value (not lowercase): %(value)s',
  33. code='invalid',
  34. params={'value': value})
  35. def validate_upper(value):
  36. if value != value.upper():
  37. raise ValidationError('Invalid value (not uppercase): %(value)s',
  38. code='invalid',
  39. params={'value': value})
  40. class MyUserManager(BaseUserManager):
  41. def create_user(self, email, password, **extra_fields):
  42. """
  43. Creates and saves a User with the given email, date of
  44. birth and password.
  45. """
  46. if not email:
  47. raise ValueError('Users must have an email address')
  48. email = self.normalize_email(email)
  49. user = self.model(email=email, **extra_fields)
  50. user.set_password(password)
  51. user.save(using=self._db)
  52. return user
  53. def create_superuser(self, email, password):
  54. """
  55. Creates and saves a superuser with the given email, date of
  56. birth and password.
  57. """
  58. user = self.create_user(email, password=password)
  59. user.is_admin = True
  60. user.save(using=self._db)
  61. return user
  62. class User(AbstractBaseUser):
  63. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  64. email = models.EmailField(
  65. verbose_name='email address',
  66. max_length=191,
  67. unique=True,
  68. )
  69. is_active = models.BooleanField(default=True)
  70. is_admin = models.BooleanField(default=False)
  71. created = models.DateTimeField(auto_now_add=True)
  72. limit_domains = models.IntegerField(default=settings.LIMIT_USER_DOMAIN_COUNT_DEFAULT, null=True, blank=True)
  73. objects = MyUserManager()
  74. USERNAME_FIELD = 'email'
  75. REQUIRED_FIELDS = []
  76. def get_full_name(self):
  77. return self.email
  78. def get_short_name(self):
  79. return self.email
  80. def __str__(self):
  81. return self.email
  82. # noinspection PyMethodMayBeStatic
  83. def has_perm(self, *_):
  84. """Does the user have a specific permission?"""
  85. # Simplest possible answer: Yes, always
  86. return True
  87. # noinspection PyMethodMayBeStatic
  88. def has_module_perms(self, *_):
  89. """Does the user have permissions to view the app `app_label`?"""
  90. # Simplest possible answer: Yes, always
  91. return True
  92. @property
  93. def is_staff(self):
  94. """Is the user a member of staff?"""
  95. # Simplest possible answer: All admins are staff
  96. return self.is_admin
  97. def activate(self):
  98. self.is_active = True
  99. self.save()
  100. def change_email(self, email):
  101. old_email = self.email
  102. self.email = email
  103. self.validate_unique()
  104. self.save()
  105. self.send_email('change-email-confirmation-old-email', recipient=old_email)
  106. def change_password(self, raw_password):
  107. self.set_password(raw_password)
  108. self.save()
  109. self.send_email('password-change-confirmation')
  110. def send_email(self, reason, context=None, recipient=None):
  111. fast_lane = 'email_fast_lane'
  112. slow_lane = 'email_slow_lane'
  113. lanes = {
  114. 'activate': slow_lane,
  115. 'activate-with-domain': slow_lane,
  116. 'change-email': slow_lane,
  117. 'change-email-confirmation-old-email': fast_lane,
  118. 'password-change-confirmation': fast_lane,
  119. 'reset-password': fast_lane,
  120. 'delete-user': fast_lane,
  121. 'domain-dyndns': fast_lane,
  122. }
  123. if reason not in lanes:
  124. raise ValueError(f'Cannot send email to user {self.pk} without a good reason: {reason}')
  125. context = context or {}
  126. content = get_template(f'emails/{reason}/content.txt').render(context)
  127. footer = get_template('emails/footer.txt').render()
  128. logger.warning(f'Queuing email for user account {self.pk} (reason: {reason})')
  129. return EmailMessage(
  130. subject=get_template(f'emails/{reason}/subject.txt').render(context).strip(),
  131. body=content + footer,
  132. from_email=get_template('emails/from.txt').render(),
  133. to=[recipient or self.email],
  134. connection=get_connection(lane=lanes[reason], debug={'user': self.pk, 'reason': reason})
  135. ).send()
  136. class Token(rest_framework.authtoken.models.Token):
  137. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  138. key = models.CharField("Key", max_length=128, db_index=True, unique=True)
  139. user = models.ForeignKey(
  140. User, related_name='auth_tokens',
  141. on_delete=models.CASCADE, verbose_name="User"
  142. )
  143. name = models.CharField("Name", max_length=64, default="")
  144. plain = None
  145. def generate_key(self):
  146. self.plain = urlsafe_b64encode(urandom(21)).decode()
  147. self.key = Token.make_hash(self.plain)
  148. return self.key
  149. @staticmethod
  150. def make_hash(plain):
  151. return make_password(plain, salt='static', hasher='pbkdf2_sha256_iter1')
  152. validate_domain_name = [
  153. validate_lower,
  154. RegexValidator(
  155. regex=r'^([a-z0-9_-]{1,63}\.)*[a-z]{1,63}$',
  156. message='Invalid value (not a DNS name).',
  157. code='invalid_domain_name'
  158. )
  159. ]
  160. def get_minimum_ttl_default():
  161. return settings.MINIMUM_TTL_DEFAULT
  162. class Domain(models.Model):
  163. created = models.DateTimeField(auto_now_add=True)
  164. name = models.CharField(max_length=191,
  165. unique=True,
  166. validators=validate_domain_name)
  167. owner = models.ForeignKey(User, on_delete=models.PROTECT, related_name='domains')
  168. published = models.DateTimeField(null=True, blank=True)
  169. minimum_ttl = models.PositiveIntegerField(default=get_minimum_ttl_default)
  170. @classmethod
  171. def is_registrable(cls, domain_name: str, user: User):
  172. """
  173. Returns False in any of the following cases:
  174. (a) the domain_name appears on the public suffix list,
  175. (b) the domain is descendant to a zone that belongs to any user different from the given one,
  176. unless it's parent is a public suffix, either through the Internet PSL or local settings.
  177. Otherwise, True is returned.
  178. """
  179. if domain_name != domain_name.lower():
  180. raise ValueError
  181. try:
  182. public_suffix = psl.get_public_suffix(domain_name)
  183. is_public_suffix = psl.is_public_suffix(domain_name)
  184. except (Timeout, NoNameservers):
  185. public_suffix = domain_name.rpartition('.')[2]
  186. is_public_suffix = False
  187. except psl_dns.exceptions.UnsupportedRule as e:
  188. # It would probably be fine to treat this as a non-public suffix (with the TLD acting as the
  189. # public suffix and setting both public_suffix and is_public_suffix accordingly).
  190. # However, in order to allow to investigate the situation, it's better not catch
  191. # this exception. For web requests, our error handler turns it into a 503 error
  192. # and makes sure admins are notified.
  193. raise e
  194. if not is_public_suffix:
  195. # Take into account that any of the parent domains could be a local public suffix. To that
  196. # end, identify the longest local public suffix that is actually a suffix of domain_name.
  197. # Then, override the global PSL result.
  198. for local_public_suffix in settings.LOCAL_PUBLIC_SUFFIXES:
  199. has_local_public_suffix_parent = ('.' + domain_name).endswith('.' + local_public_suffix)
  200. if has_local_public_suffix_parent and len(local_public_suffix) > len(public_suffix):
  201. public_suffix = local_public_suffix
  202. is_public_suffix = (public_suffix == domain_name)
  203. if is_public_suffix and domain_name not in settings.LOCAL_PUBLIC_SUFFIXES:
  204. return False
  205. # Generate a list of all domains connecting this one and its public suffix.
  206. # If another user owns a zone with one of these names, then the requested
  207. # domain is unavailable because it is part of the other user's zone.
  208. private_components = domain_name.rsplit(public_suffix, 1)[0].rstrip('.')
  209. private_components = private_components.split('.') if private_components else []
  210. private_components += [public_suffix]
  211. private_domains = ['.'.join(private_components[i:]) for i in range(0, len(private_components) - 1)]
  212. assert is_public_suffix or domain_name == private_domains[0]
  213. # Deny registration for non-local public suffixes and for domains covered by other users' zones
  214. user = user if not isinstance(user, AnonymousUser) else None
  215. return not cls.objects.filter(Q(name__in=private_domains) & ~Q(owner=user)).exists()
  216. @property
  217. def keys(self):
  218. return pdns.get_keys(self)
  219. @property
  220. def is_locally_registrable(self):
  221. return self.parent_domain_name in settings.LOCAL_PUBLIC_SUFFIXES
  222. @property
  223. def parent_domain_name(self):
  224. return self._partitioned_name[1]
  225. @property
  226. def _partitioned_name(self):
  227. subname, _, parent_name = self.name.partition('.')
  228. return subname, parent_name or None
  229. def save(self, *args, **kwargs):
  230. self.full_clean(validate_unique=False)
  231. super().save(*args, **kwargs)
  232. def update_delegation(self, child_domain: Domain):
  233. child_subname, child_domain_name = child_domain._partitioned_name
  234. if self.name != child_domain_name:
  235. raise ValueError('Cannot update delegation of %s as it is not an immediate child domain of %s.' %
  236. (child_domain.name, self.name))
  237. if child_domain.pk:
  238. # Domain real: set delegation
  239. child_keys = child_domain.keys
  240. if not child_keys:
  241. raise APIException('Cannot delegate %s, as it currently has no keys.' % child_domain.name)
  242. RRset.objects.create(domain=self, subname=child_subname, type='NS', ttl=3600, contents=settings.DEFAULT_NS)
  243. RRset.objects.create(domain=self, subname=child_subname, type='DS', ttl=300,
  244. contents=[ds for k in child_keys for ds in k['ds']])
  245. else:
  246. # Domain not real: remove delegation
  247. for rrset in self.rrset_set.filter(subname=child_subname, type__in=['NS', 'DS']):
  248. rrset.delete()
  249. def __str__(self):
  250. return self.name
  251. class Meta:
  252. ordering = ('created',)
  253. def get_default_value_created():
  254. return timezone.now()
  255. def get_default_value_due():
  256. return timezone.now() + timedelta(days=7)
  257. def get_default_value_mref():
  258. return "ONDON" + str(time.time())
  259. class Donation(models.Model):
  260. created = models.DateTimeField(default=get_default_value_created)
  261. name = models.CharField(max_length=255)
  262. iban = models.CharField(max_length=34)
  263. bic = models.CharField(max_length=11)
  264. amount = models.DecimalField(max_digits=8, decimal_places=2)
  265. message = models.CharField(max_length=255, blank=True)
  266. due = models.DateTimeField(default=get_default_value_due)
  267. mref = models.CharField(max_length=32, default=get_default_value_mref)
  268. email = models.EmailField(max_length=255, blank=True)
  269. class Meta:
  270. managed = False
  271. class RRsetManager(Manager):
  272. def create(self, contents=None, **kwargs):
  273. rrset = super().create(**kwargs)
  274. for content in contents or []:
  275. RR.objects.create(rrset=rrset, content=content)
  276. return rrset
  277. class RRset(models.Model):
  278. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  279. created = models.DateTimeField(auto_now_add=True)
  280. updated = models.DateTimeField(null=True) # undocumented, used for debugging only
  281. domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
  282. subname = models.CharField(
  283. max_length=178,
  284. blank=True,
  285. validators=[
  286. validate_lower,
  287. RegexValidator(
  288. regex=r'^([*]|(([*][.])?[a-z0-9_.-]*))$',
  289. message='Subname can only use (lowercase) a-z, 0-9, ., -, and _, '
  290. 'may start with a \'*.\', or just be \'*\'.',
  291. code='invalid_subname'
  292. )
  293. ]
  294. )
  295. type = models.CharField(
  296. max_length=10,
  297. validators=[
  298. validate_upper,
  299. RegexValidator(
  300. regex=r'^[A-Z][A-Z0-9]*$',
  301. message='Type must be uppercase alphanumeric and start with a letter.',
  302. code='invalid_type'
  303. )
  304. ]
  305. )
  306. ttl = models.PositiveIntegerField()
  307. objects = RRsetManager()
  308. DEAD_TYPES = ('ALIAS', 'DNAME')
  309. RESTRICTED_TYPES = ('SOA', 'RRSIG', 'DNSKEY', 'NSEC3PARAM', 'OPT')
  310. class Meta:
  311. unique_together = (("domain", "subname", "type"),)
  312. @staticmethod
  313. def construct_name(subname, domain_name):
  314. return '.'.join(filter(None, [subname, domain_name])) + '.'
  315. @property
  316. def name(self):
  317. return self.construct_name(self.subname, self.domain.name)
  318. def save(self, *args, **kwargs):
  319. self.updated = timezone.now()
  320. self.full_clean(validate_unique=False)
  321. super().save(*args, **kwargs)
  322. def __str__(self):
  323. return '<RRSet domain=%s type=%s subname=%s>' % (self.domain.name, self.type, self.subname)
  324. class RRManager(Manager):
  325. def bulk_create(self, rrs, **kwargs):
  326. ret = super().bulk_create(rrs, **kwargs)
  327. # For each rrset, save once to set RRset.updated timestamp and trigger signal for post-save processing
  328. rrsets = {rr.rrset for rr in rrs}
  329. for rrset in rrsets:
  330. rrset.save()
  331. return ret
  332. class RR(models.Model):
  333. created = models.DateTimeField(auto_now_add=True)
  334. rrset = models.ForeignKey(RRset, on_delete=models.CASCADE, related_name='records')
  335. # max_length is determined based on the calculation in
  336. # https://lists.isc.org/pipermail/bind-users/2008-April/070148.html
  337. content = models.CharField(max_length=4092)
  338. objects = RRManager()
  339. def __str__(self):
  340. return '<RR %s>' % self.content
  341. class AuthenticatedAction(models.Model):
  342. """
  343. Represents a procedure call on a defined set of arguments.
  344. Subclasses can define additional arguments by adding Django model fields and must define the action to be taken by
  345. implementing the `_act` method.
  346. AuthenticatedAction provides the `state` property which by default is a hash of the action type (defined by the
  347. action's class path). Other information such as user state can be included in the state hash by (carefully)
  348. overriding the `_state_fields` property. Instantiation of the model, if given a `state` kwarg, will raise an error
  349. if the given state argument does not match the state computed from `_state_fields` at the moment of instantiation.
  350. The same applies to the `act` method: If called on an object that was instantiated without a `state` kwargs, an
  351. error will be raised.
  352. This effectively allows hash-authenticated procedure calls by third parties as long as the server-side state is
  353. unaltered, according to the following protocol:
  354. (1) Instantiate the AuthenticatedAction subclass representing the action to be taken (no `state` kwarg here),
  355. (2) provide information on how to instantiate the instance, and the state hash, to a third party,
  356. (3) when provided with data that allows instantiation and a valid state hash, take the defined action, possibly with
  357. additional parameters chosen by the third party that do not belong to the verified state.
  358. """
  359. _validated = False
  360. class Meta:
  361. managed = False
  362. def __init__(self, *args, **kwargs):
  363. state = kwargs.pop('state', None)
  364. super().__init__(*args, **kwargs)
  365. if state is not None:
  366. self._validated = self.validate_state(state)
  367. if not self._validated:
  368. raise ValueError
  369. @property
  370. def _state_fields(self):
  371. """
  372. Returns a list that defines the state of this action (used for authentication of this action).
  373. Return value must be JSON-serializable.
  374. Values not included in the return value will not be used for authentication, i.e. those values can be varied
  375. freely and function as unauthenticated action input parameters.
  376. Use caution when overriding this method. You will usually want to append a value to the list returned by the
  377. parent. Overriding the behavior altogether could result in reducing the state to fewer variables, resulting
  378. in valid signatures when they were intended to be invalid. The suggested method for overriding is
  379. @property
  380. def _state_fields:
  381. return super()._state_fields + [self.important_value, self.another_added_value]
  382. :return: List of values to be signed.
  383. """
  384. # TODO consider adding a "last change" attribute of the user to the state to avoid code
  385. # re-use after the the state has been changed and changed back.
  386. name = '.'.join([self.__module__, self.__class__.__qualname__])
  387. return [name]
  388. @property
  389. def state(self):
  390. state = json.dumps(self._state_fields).encode()
  391. hash = sha256()
  392. hash.update(state)
  393. return hash.hexdigest()
  394. def validate_state(self, value):
  395. return value == self.state
  396. def _act(self):
  397. """
  398. Conduct the action represented by this class.
  399. :return: None
  400. """
  401. raise NotImplementedError
  402. def act(self):
  403. if not self._validated:
  404. raise RuntimeError('Action state could not be verified.')
  405. return self._act()
  406. class AuthenticatedUserAction(AuthenticatedAction):
  407. """
  408. Abstract AuthenticatedAction involving an user instance, incorporating the user's id, email, password, and
  409. is_active flag into the Message Authentication Code state.
  410. """
  411. user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
  412. class Meta:
  413. managed = False
  414. @property
  415. def _state_fields(self):
  416. return super()._state_fields + [str(self.user.id), self.user.email, self.user.password, self.user.is_active]
  417. def _act(self):
  418. raise NotImplementedError
  419. class AuthenticatedActivateUserAction(AuthenticatedUserAction):
  420. domain = models.CharField(max_length=191)
  421. class Meta:
  422. managed = False
  423. @property
  424. def _state_fields(self):
  425. return super()._state_fields + [self.domain]
  426. def _act(self):
  427. self.user.activate()
  428. class AuthenticatedChangeEmailUserAction(AuthenticatedUserAction):
  429. new_email = models.EmailField()
  430. class Meta:
  431. managed = False
  432. @property
  433. def _state_fields(self):
  434. return super()._state_fields + [self.new_email]
  435. def _act(self):
  436. self.user.change_email(self.new_email)
  437. class AuthenticatedResetPasswordUserAction(AuthenticatedUserAction):
  438. new_password = models.CharField(max_length=128)
  439. class Meta:
  440. managed = False
  441. def _act(self):
  442. self.user.change_password(self.new_password)
  443. class AuthenticatedDeleteUserAction(AuthenticatedUserAction):
  444. class Meta:
  445. managed = False
  446. def _act(self):
  447. self.user.delete()
  448. def captcha_default_content():
  449. alphabet = (string.ascii_uppercase + string.digits).translate({ord(c): None for c in 'IO0'})
  450. return ''.join([secrets.choice(alphabet) for _ in range(5)])
  451. class Captcha(models.Model):
  452. id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
  453. created = models.DateTimeField(auto_now_add=True)
  454. content = models.CharField(
  455. max_length=24,
  456. default=captcha_default_content,
  457. )
  458. def verify(self, solution: str):
  459. age = timezone.now() - self.created
  460. self.delete()
  461. return (
  462. str(solution).upper().strip() == self.content # solution correct
  463. and
  464. age <= settings.CAPTCHA_VALIDITY_PERIOD # not expired
  465. )