regex.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008 Nicolas Kandel
  5. # Copyright © 2008 Pascal Halter
  6. # Copyright © 2008-2013 Guillaume Ayoub
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. """
  21. Rights management.
  22. Rights are based on a regex-based file whose name is specified in the config
  23. (section "right", key "file").
  24. Authentication login is matched against the "user" key, and collection's path
  25. is matched against the "collection" key. You can use Python's ConfigParser
  26. interpolation values %(login)s and %(path)s. You can also get groups from the
  27. user regex in the collection with {0}, {1}, etc.
  28. For example, for the "user" key, ".+" means "authenticated user" and ".*"
  29. means "anybody" (including anonymous users).
  30. Section names are only used for naming the rule.
  31. Leading or ending slashes are trimmed from collection's path.
  32. """
  33. import re
  34. import os.path
  35. from .. import config, log
  36. # Manage Python2/3 different modules
  37. # pylint: disable=F0401
  38. try:
  39. from configparser import ConfigParser
  40. from io import StringIO
  41. except ImportError:
  42. from ConfigParser import ConfigParser
  43. from StringIO import StringIO
  44. # pylint: enable=F0401
  45. DEFINED_RIGHTS = {
  46. "authenticated": "[rw]\nuser:.+\ncollection:.*\npermission:rw",
  47. "owner_write": "[r]\nuser:.+\ncollection:.*\npermission:r\n"
  48. "[w]\nuser:.+\ncollection:^%(login)s(/.*)?$\npermission:w",
  49. "owner_only": "[rw]\nuser:.+\ncollection:^%(login)s(/.*)?$\npermission:rw",
  50. }
  51. def _read_from_sections(user, collection_url, permission):
  52. """Get regex sections."""
  53. filename = os.path.expanduser(config.get("rights", "file"))
  54. rights_type = config.get("rights", "type").lower()
  55. regex = ConfigParser({"login": user, "path": collection_url})
  56. if rights_type in DEFINED_RIGHTS:
  57. log.LOGGER.debug("Rights type '%s'" % rights_type)
  58. regex.readfp(StringIO(DEFINED_RIGHTS[rights_type]))
  59. elif rights_type == "from_file":
  60. log.LOGGER.debug("Reading rights from file %s" % filename)
  61. if not regex.read(filename):
  62. log.LOGGER.error("File '%s' not found for rights" % filename)
  63. return False
  64. else:
  65. log.LOGGER.error("Unknown rights type '%s'" % rights_type)
  66. return False
  67. for section in regex.sections():
  68. re_user = regex.get(section, "user")
  69. re_collection = regex.get(section, "collection")
  70. log.LOGGER.debug(
  71. "Test if '%s:%s' matches against '%s:%s' from section '%s'" % (
  72. user, collection_url, re_user, re_collection, section))
  73. user_match = re.match(re_user, user)
  74. if user_match:
  75. re_collection = re_collection.format(*user_match.groups())
  76. if re.match(re_collection, collection_url):
  77. log.LOGGER.debug("Section '%s' matches" % section)
  78. if permission in regex.get(section, "permission"):
  79. return True
  80. else:
  81. log.LOGGER.debug("Section '%s' does not match" % section)
  82. return False
  83. def authorized(user, collection, permission):
  84. """Check if the user is allowed to read or write the collection.
  85. If the user is empty it checks for anonymous rights
  86. """
  87. collection_url = collection.url.rstrip("/") or "/"
  88. if collection_url in (".well-known/carddav", ".well-known/caldav"):
  89. return permission == "r"
  90. rights_type = config.get("rights", "type").lower()
  91. return (
  92. rights_type == "none" or
  93. _read_from_sections(user or "", collection_url, permission))