from_file.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2012-2017 Guillaume Ayoub
  3. # Copyright © 2017-2019 Unrud <unrud@outlook.com>
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Rights backend based on a regex-based file whose name is specified in the
  19. config (section "rights", key "file").
  20. The login is matched against the "user" key, and the collection path
  21. is matched against the "collection" key. In the "collection" regex you can use
  22. `{user}` and get groups from the "user" regex with `{0}`, `{1}`, etc.
  23. In consequence of the parameter subsitution you have to write `{{` and `}}`
  24. if you want to use regular curly braces in the "user" and "collection" regexes.
  25. For example, for the "user" key, ".+" means "authenticated user" and ".*"
  26. means "anybody" (including anonymous users).
  27. Section names are only used for naming the rule.
  28. Leading or ending slashes are trimmed from collection's path.
  29. """
  30. from configparser import ConfigParser
  31. import re
  32. from radicale import config, pathutils, rights
  33. from radicale.log import logger
  34. class Rights(rights.BaseRights):
  35. _filename: str
  36. _rights_config: ConfigParser
  37. _user_groups: set
  38. def __init__(self, configuration: config.Configuration) -> None:
  39. super().__init__(configuration)
  40. self._filename = configuration.get("rights", "file")
  41. self._rights_config = ConfigParser()
  42. try:
  43. with open(self._filename, "r") as f:
  44. self._rights_config.read_file(f)
  45. logger.debug("Rights were read")
  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. logger.debug("authorization called %r %r",user,path)
  55. for section in self._rights_config.sections():
  56. group_match = []
  57. try:
  58. collection_pattern = self._rights_config.get(section, "collection")
  59. user_pattern = self._rights_config.get(section, "user", fallback = "")
  60. groups = self._rights_config.get(section, "groups", fallback = "").split(",")
  61. try:
  62. group_match = self._user_groups & set(groups)
  63. logger.debug("Groups %r, %r",",".join(group_match),";".join(groups))
  64. except:
  65. pass
  66. # Use empty format() for harmonized handling of curly braces
  67. user_match = re.fullmatch(user_pattern.format(), user)
  68. u_collection_match = user_match and re.fullmatch(
  69. collection_pattern.format(
  70. *(re.escape(s) for s in user_match.groups()),
  71. user=escaped_user), sane_path)
  72. g_collection_match = re.fullmatch( collection_pattern.format(user=escaped_user), sane_path)
  73. except Exception as e:
  74. raise RuntimeError("Error in section %r of rights file %r: "
  75. "%s" % (section, self._filename, e)) from e
  76. if user_match and u_collection_match:
  77. logger.debug("User rule %r:%r matches %r:%r from section %r",
  78. user, sane_path, user_pattern,
  79. collection_pattern, section)
  80. return self._rights_config.get(section, "permissions")
  81. if len(group_match) > 0 and g_collection_match:
  82. logger.debug("Group rule %r:%r matches %r from section %r",
  83. group_match, sane_path,
  84. collection_pattern, section)
  85. return self._rights_config.get(section, "permissions")
  86. logger.debug("Rule %r:%r doesn't match %r:%r from section %r",
  87. user, sane_path, user_pattern, collection_pattern,
  88. section)
  89. logger.info("Rights: %r:%r doesn't match any section", user, sane_path)
  90. return ""