from_file.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2012-2017 Guillaume Ayoub
  3. # Copyright © 2017-2021 Unrud <unrud@outlook.com>
  4. # Copyright © 2024-2024 Peter Bieringer <pb@bieringer.de>
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Rights backend based on a regex-based file whose name is specified in the
  20. config (section "rights", key "file").
  21. The login is matched against the "user" key, and the collection path
  22. is matched against the "collection" key. In the "collection" regex you can use
  23. `{user}` and get groups from the "user" regex with `{0}`, `{1}`, etc.
  24. In consequence of the parameter substitution you have to write `{{` and `}}`
  25. if you want to use regular curly braces in the "user" and "collection" regexes.
  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 re
  33. from radicale import config, pathutils, rights
  34. from radicale.log import logger
  35. class Rights(rights.BaseRights):
  36. _filename: str
  37. def __init__(self, configuration: config.Configuration) -> None:
  38. super().__init__(configuration)
  39. self._filename = configuration.get("rights", "file")
  40. self._log_rights_rule_doesnt_match_on_debug = configuration.get("logging", "rights_rule_doesnt_match_on_debug")
  41. self._rights_config = configparser.ConfigParser()
  42. try:
  43. with open(self._filename, "r") as f:
  44. self._rights_config.read_file(f)
  45. logger.debug("Read rights file")
  46. except Exception as e:
  47. raise RuntimeError("Failed to load rights file %r: %s" %
  48. (self._filename, e)) from e
  49. def authorization(self, user: str, path: str) -> str:
  50. user = user or ""
  51. sane_path = pathutils.strip_path(path)
  52. # Prevent "regex injection"
  53. escaped_user = re.escape(user)
  54. if not self._log_rights_rule_doesnt_match_on_debug:
  55. logger.debug("logging of rules which doesn't match suppressed by config/option [logging] rights_rule_doesnt_match_on_debug")
  56. for section in self._rights_config.sections():
  57. group_match = None
  58. user_match = None
  59. try:
  60. user_pattern = self._rights_config.get(section, "user", fallback="")
  61. collection_pattern = self._rights_config.get(section, "collection")
  62. allowed_groups = self._rights_config.get(section, "groups", fallback="").split(",")
  63. try:
  64. group_match = len(self._user_groups.intersection(allowed_groups)) > 0
  65. except Exception:
  66. pass
  67. # Use empty format() for harmonized handling of curly braces
  68. if user_pattern != "":
  69. user_match = re.fullmatch(user_pattern.format(), user)
  70. user_collection_match = user_match and re.fullmatch(
  71. collection_pattern.format(
  72. *(re.escape(s) for s in user_match.groups()),
  73. user=escaped_user), sane_path)
  74. group_collection_match = group_match and re.fullmatch(
  75. collection_pattern.format(user=escaped_user), sane_path)
  76. except Exception as e:
  77. raise RuntimeError("Error in section %r of rights file %r: "
  78. "%s" % (section, self._filename, e)) from e
  79. if user_match and user_collection_match:
  80. permission = self._rights_config.get(section, "permissions")
  81. logger.debug("Rule %r:%r matches %r:%r from section %r permission %r",
  82. user, sane_path, user_pattern,
  83. collection_pattern, section, permission)
  84. return permission
  85. if group_match and group_collection_match:
  86. permission = self._rights_config.get(section, "permissions")
  87. logger.debug("Rule %r:%r matches %r:%r from section %r permission %r by group membership",
  88. user, sane_path, user_pattern,
  89. collection_pattern, section, permission)
  90. return permission
  91. if self._log_rights_rule_doesnt_match_on_debug:
  92. logger.debug("Rule %r:%r doesn't match %r:%r from section %r",
  93. user, sane_path, user_pattern, collection_pattern,
  94. section)
  95. logger.debug("Rights: %r:%r doesn't match any section", user, sane_path)
  96. return ""