rights.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2017 Guillaume Ayoub
  3. #
  4. # This library is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Rights backends.
  18. This module loads the rights backend, according to the rights
  19. configuration.
  20. Default rights are based on a regex-based file whose name is specified in the
  21. config (section "right", key "file").
  22. Authentication login is matched against the "user" key, and collection's path
  23. is matched against the "collection" key. You can use Python's ConfigParser
  24. interpolation values %(login)s and %(path)s. You can also get groups from the
  25. user regex in the collection with {0}, {1}, etc.
  26. For example, for the "user" key, ".+" means "authenticated user" and ".*"
  27. means "anybody" (including anonymous users).
  28. Section names are only used for naming the rule.
  29. Leading or ending slashes are trimmed from collection's path.
  30. """
  31. import configparser
  32. import os.path
  33. import re
  34. from importlib import import_module
  35. from radicale import storage
  36. from radicale.log import logger
  37. INTERNAL_TYPES = ("none", "authenticated", "owner_write", "owner_only",
  38. "from_file")
  39. def load(configuration):
  40. """Load the rights manager chosen in configuration."""
  41. rights_type = configuration.get("rights", "type")
  42. if rights_type == "authenticated":
  43. rights_class = AuthenticatedRights
  44. elif rights_type == "owner_write":
  45. rights_class = OwnerWriteRights
  46. elif rights_type == "owner_only":
  47. rights_class = OwnerOnlyRights
  48. elif rights_type == "from_file":
  49. rights_class = Rights
  50. else:
  51. try:
  52. rights_class = import_module(rights_type).Rights
  53. except Exception as e:
  54. raise RuntimeError("Failed to load rights module %r: %s" %
  55. (rights_type, e)) from e
  56. logger.info("Rights type is %r", rights_type)
  57. return rights_class(configuration)
  58. def intersect_permissions(a, b="RrWw"):
  59. return "".join(set(a).intersection(set(b)))
  60. class BaseRights:
  61. def __init__(self, configuration):
  62. self.configuration = configuration
  63. def authorized(self, user, path, permissions):
  64. """Check if the user is allowed to read or write the collection.
  65. If ``user`` is empty, check for anonymous rights.
  66. ``path`` is sanitized.
  67. ``permissions`` can include "R", "r", "W", "w"
  68. Returns granted rights.
  69. """
  70. raise NotImplementedError
  71. class AuthenticatedRights(BaseRights):
  72. def __init__(self, *args, **kwargs):
  73. super().__init__(*args, **kwargs)
  74. self._verify_user = self.configuration.get("auth", "type") != "none"
  75. def authorized(self, user, path, permissions):
  76. if self._verify_user and not user:
  77. return ""
  78. sane_path = storage.sanitize_path(path).strip("/")
  79. if "/" not in sane_path:
  80. return intersect_permissions(permissions, "RW")
  81. if sane_path.count("/") == 1:
  82. return intersect_permissions(permissions, "rw")
  83. return ""
  84. class OwnerWriteRights(AuthenticatedRights):
  85. def authorized(self, user, path, permissions):
  86. if self._verify_user and not user:
  87. return ""
  88. sane_path = storage.sanitize_path(path).strip("/")
  89. if not sane_path:
  90. return intersect_permissions(permissions, "R")
  91. if self._verify_user:
  92. owned = user == sane_path.split("/", maxsplit=1)[0]
  93. else:
  94. owned = True
  95. if "/" not in sane_path:
  96. return intersect_permissions(permissions, "RW" if owned else "R")
  97. if sane_path.count("/") == 1:
  98. return intersect_permissions(permissions, "rw" if owned else "r")
  99. return ""
  100. class OwnerOnlyRights(AuthenticatedRights):
  101. def authorized(self, user, path, permissions):
  102. if self._verify_user and not user:
  103. return ""
  104. sane_path = storage.sanitize_path(path).strip("/")
  105. if not sane_path:
  106. return intersect_permissions(permissions, "R")
  107. if self._verify_user and user != sane_path.split("/", maxsplit=1)[0]:
  108. return ""
  109. if "/" not in sane_path:
  110. return intersect_permissions(permissions, "RW")
  111. if sane_path.count("/") == 1:
  112. return intersect_permissions(permissions, "rw")
  113. return ""
  114. class Rights(BaseRights):
  115. def __init__(self, configuration):
  116. super().__init__(configuration)
  117. self.filename = os.path.expanduser(configuration.get("rights", "file"))
  118. def authorized(self, user, path, permissions):
  119. user = user or ""
  120. sane_path = storage.sanitize_path(path).strip("/")
  121. # Prevent "regex injection"
  122. user_escaped = re.escape(user)
  123. sane_path_escaped = re.escape(sane_path)
  124. rights_config = configparser.ConfigParser(
  125. {"login": user_escaped, "path": sane_path_escaped})
  126. try:
  127. if not rights_config.read(self.filename):
  128. raise RuntimeError("No such file: %r" %
  129. self.filename)
  130. except Exception as e:
  131. raise RuntimeError("Failed to load rights file %r: %s" %
  132. (self.filename, e)) from e
  133. for section in rights_config.sections():
  134. try:
  135. user_pattern = rights_config.get(section, "user")
  136. collection_pattern = rights_config.get(section, "collection")
  137. user_match = re.fullmatch(user_pattern, user)
  138. collection_match = user_match and re.fullmatch(
  139. collection_pattern.format(
  140. *map(re.escape, user_match.groups())), sane_path)
  141. except Exception as e:
  142. raise RuntimeError("Error in section %r of rights file %r: "
  143. "%s" % (section, self.filename, e)) from e
  144. if user_match and collection_match:
  145. logger.debug("Rule %r:%r matches %r:%r from section %r",
  146. user, sane_path, user_pattern,
  147. collection_pattern, section)
  148. return intersect_permissions(
  149. permissions, rights_config.get(section, "permissions"))
  150. else:
  151. logger.debug("Rule %r:%r doesn't match %r:%r from section %r",
  152. user, sane_path, user_pattern,
  153. collection_pattern, section)
  154. logger.info("Rights: %r:%r doesn't match any section", user, sane_path)
  155. return ""