from_file.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # This file is part of Radicale Server - Calendar 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 "right", key "file").
  20. Authentication login is matched against the "user" key, and collection's path
  21. is matched against the "collection" key. You can use Python's ConfigParser
  22. interpolation values %(login)s and %(path)s. You can also get groups from the
  23. user regex in the collection with {0}, {1}, etc.
  24. For example, for the "user" key, ".+" means "authenticated user" and ".*"
  25. means "anybody" (including anonymous users).
  26. Section names are only used for naming the rule.
  27. Leading or ending slashes are trimmed from collection's path.
  28. """
  29. import configparser
  30. import re
  31. from radicale import pathutils, rights
  32. from radicale.log import logger
  33. class Rights(rights.BaseRights):
  34. def __init__(self, configuration):
  35. super().__init__(configuration)
  36. self.filename = configuration.get("rights", "file")
  37. def authorized(self, user, path, permissions):
  38. user = user or ""
  39. sane_path = pathutils.strip_path(path)
  40. # Prevent "regex injection"
  41. user_escaped = re.escape(user)
  42. sane_path_escaped = re.escape(sane_path)
  43. rights_config = configparser.ConfigParser(
  44. {"login": user_escaped, "path": sane_path_escaped})
  45. try:
  46. if not rights_config.read(self.filename):
  47. raise RuntimeError("No such file: %r" %
  48. self.filename)
  49. except Exception as e:
  50. raise RuntimeError("Failed to load rights file %r: %s" %
  51. (self.filename, e)) from e
  52. for section in rights_config.sections():
  53. try:
  54. user_pattern = rights_config.get(section, "user")
  55. collection_pattern = rights_config.get(section, "collection")
  56. user_match = re.fullmatch(user_pattern, user)
  57. collection_match = user_match and re.fullmatch(
  58. collection_pattern.format(
  59. *map(re.escape, user_match.groups())), sane_path)
  60. except Exception as e:
  61. raise RuntimeError("Error in section %r of rights file %r: "
  62. "%s" % (section, self.filename, e)) from e
  63. if user_match and collection_match:
  64. logger.debug("Rule %r:%r matches %r:%r from section %r",
  65. user, sane_path, user_pattern,
  66. collection_pattern, section)
  67. return rights.intersect_permissions(
  68. permissions, rights_config.get(section, "permissions"))
  69. else:
  70. logger.debug("Rule %r:%r doesn't match %r:%r from section %r",
  71. user, sane_path, user_pattern,
  72. collection_pattern, section)
  73. logger.info("Rights: %r:%r doesn't match any section", user, sane_path)
  74. return ""