regex.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2016 Guillaume Ayoub
  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 management.
  20. Rights are based on a regex-based file whose name is specified in the config
  21. (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 re
  32. import sys
  33. import os.path
  34. from .. import config, log
  35. # Manage Python2/3 different modules
  36. if sys.version_info[0] == 2:
  37. from ConfigParser import ConfigParser
  38. from StringIO import StringIO
  39. else:
  40. from configparser import ConfigParser
  41. from io import StringIO
  42. DEFINED_RIGHTS = {
  43. "authenticated": """
  44. [rw]
  45. user:.+
  46. collection:.*
  47. permission:rw
  48. """,
  49. "owner_write": """
  50. [w]
  51. user:.+
  52. collection:^%(login)s(/.*)?$
  53. permission:rw
  54. [r]
  55. user:.+
  56. collection:.*
  57. permission:r
  58. """,
  59. "owner_only": """
  60. [rw]
  61. user:.+
  62. collection:^%(login)s(/.*)?$
  63. permission:rw
  64. """}
  65. def _read_from_sections(user, collection_url, permission):
  66. """Get regex sections."""
  67. filename = os.path.expanduser(config.get("rights", "file"))
  68. rights_type = config.get("rights", "type").lower()
  69. # Prevent "regex injection"
  70. user_escaped = re.escape(user)
  71. collection_url_escaped = re.escape(collection_url)
  72. regex = ConfigParser({"login": user_escaped, "path": collection_url_escaped})
  73. if rights_type in DEFINED_RIGHTS:
  74. log.LOGGER.debug("Rights type '%s'" % rights_type)
  75. regex.readfp(StringIO(DEFINED_RIGHTS[rights_type]))
  76. elif rights_type == "from_file":
  77. log.LOGGER.debug("Reading rights from file %s" % filename)
  78. if not regex.read(filename):
  79. log.LOGGER.error("File '%s' not found for rights" % filename)
  80. return False
  81. else:
  82. log.LOGGER.error("Unknown rights type '%s'" % rights_type)
  83. return False
  84. for section in regex.sections():
  85. re_user = regex.get(section, "user")
  86. re_collection = regex.get(section, "collection")
  87. log.LOGGER.debug(
  88. "Test if '%s:%s' matches against '%s:%s' from section '%s'" % (
  89. user, collection_url, re_user, re_collection, section))
  90. user_match = re.match(re_user, user)
  91. if user_match:
  92. re_collection = re_collection.format(*user_match.groups())
  93. if re.match(re_collection, collection_url):
  94. log.LOGGER.debug("Section '%s' matches" % section)
  95. return permission in regex.get(section, "permission")
  96. else:
  97. log.LOGGER.debug("Section '%s' does not match" % section)
  98. return False
  99. def authorized(user, collection, permission):
  100. """Check if the user is allowed to read or write the collection.
  101. If the user is empty, check for anonymous rights.
  102. """
  103. collection_url = collection.url.rstrip("/") or "/"
  104. if collection_url in (".well-known/carddav", ".well-known/caldav"):
  105. return permission == "r"
  106. rights_type = config.get("rights", "type").lower()
  107. return (
  108. rights_type == "none" or
  109. _read_from_sections(user or "", collection_url, permission))