rights.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2016 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 os.path
  32. import re
  33. from configparser import ConfigParser
  34. from importlib import import_module
  35. from io import StringIO
  36. from . import storage
  37. def load(configuration, logger):
  38. """Load the rights manager chosen in configuration."""
  39. auth_type = configuration.get("auth", "type")
  40. rights_type = configuration.get("rights", "type")
  41. if auth_type == "None" or rights_type == "None":
  42. return lambda user, collection, permission: True
  43. elif rights_type in DEFINED_RIGHTS or rights_type == "from_file":
  44. return Rights(configuration, logger).authorized
  45. else:
  46. module = import_module(rights_type)
  47. return module.Rights(configuration, logger).authorized
  48. DEFINED_RIGHTS = {
  49. "authenticated": """
  50. [rw]
  51. user:.+
  52. collection:.*
  53. permission:rw
  54. """,
  55. "owner_write": """
  56. [w]
  57. user:.+
  58. collection:%(login)s(/.*)?
  59. permission:rw
  60. [r]
  61. user:.+
  62. collection:.*
  63. permission:r
  64. """,
  65. "owner_only": """
  66. [rw]
  67. user:.+
  68. collection:%(login)s(/.*)?
  69. permission:rw
  70. [r]
  71. user:.+
  72. collection:
  73. permission:r
  74. """}
  75. class BaseRights:
  76. def __init__(self, configuration, logger):
  77. self.configuration = configuration
  78. self.logger = logger
  79. def authorized(self, user, collection, permission):
  80. """Check if the user is allowed to read or write the collection.
  81. If the user is empty, check for anonymous rights.
  82. """
  83. raise NotImplementedError
  84. class Rights(BaseRights):
  85. def __init__(self, configuration, logger):
  86. super().__init__(configuration, logger)
  87. self.filename = os.path.expanduser(configuration.get("rights", "file"))
  88. self.rights_type = configuration.get("rights", "type").lower()
  89. def authorized(self, user, path, permission):
  90. user = user or ""
  91. if user and not storage.is_safe_path_component(user):
  92. # Prevent usernames like "user/calendar.ics"
  93. raise ValueError("Refused unsafe username: %s", user)
  94. sane_path = storage.sanitize_path(path).strip("/")
  95. # Prevent "regex injection"
  96. user_escaped = re.escape(user)
  97. sane_path_escaped = re.escape(sane_path)
  98. regex = ConfigParser(
  99. {"login": user_escaped, "path": sane_path_escaped})
  100. if self.rights_type in DEFINED_RIGHTS:
  101. self.logger.debug("Rights type '%s'", self.rights_type)
  102. regex.readfp(StringIO(DEFINED_RIGHTS[self.rights_type]))
  103. else:
  104. self.logger.debug("Reading rights from file '%s'", self.filename)
  105. if not regex.read(self.filename):
  106. self.logger.error(
  107. "File '%s' not found for rights", self.filename)
  108. return False
  109. for section in regex.sections():
  110. re_user = regex.get(section, "user")
  111. re_collection = regex.get(section, "collection")
  112. self.logger.debug(
  113. "Test if '%s:%s' matches against '%s:%s' from section '%s'",
  114. user, sane_path, re_user, re_collection, section)
  115. # Emulate fullmatch
  116. user_match = re.match(r"(?:%s)\Z" % re_user, user)
  117. if user_match:
  118. re_collection = re_collection.format(*user_match.groups())
  119. # Emulate fullmatch
  120. if re.match(r"(?:%s)\Z" % re_collection, sane_path):
  121. self.logger.debug("Section '%s' matches", section)
  122. return permission in regex.get(section, "permission")
  123. else:
  124. self.logger.debug("Section '%s' does not match", section)
  125. return False