rights.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. 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 io
  33. import os.path
  34. from . import config, log
  35. # Manage Python2/3 different modules
  36. # pylint: disable=F0401
  37. try:
  38. from configparser import ConfigParser
  39. except ImportError:
  40. from ConfigParser import ConfigParser
  41. # pylint: enable=F0401
  42. DEFINED_RIGHTS = {
  43. "owner_write": "[r]\nuser:.*\ncollection:.*\npermission:r\n"
  44. "[w]\nuser:.*\ncollection:^%(login)s/.+$\npermission:w",
  45. "owner_only": "[rw]\nuser:.*\ncollection:^%(login)s/.+$\npermission:rw"}
  46. def _read_from_sections(user, collection, permission):
  47. """Get regex sections."""
  48. filename = os.path.expanduser(config.get("rights", "file"))
  49. rights_type = config.get("rights", "type").lower()
  50. regex = ConfigParser({"login": user, "path": collection})
  51. if rights_type in DEFINED_RIGHTS:
  52. log.LOGGER.debug("Rights type '%s'" % rights_type)
  53. regex.readfp(io.BytesIO(DEFINED_RIGHTS[rights_type]))
  54. elif rights_type == "from_file":
  55. log.LOGGER.debug("Reading rights from file %s" % filename)
  56. if not regex.read(filename):
  57. log.LOGGER.error("File '%s' not found for rights" % filename)
  58. return False
  59. else:
  60. log.LOGGER.error("Unknown rights type '%s'" % rights_type)
  61. return False
  62. for section in regex.sections():
  63. re_user = regex.get(section, "user")
  64. re_collection = regex.get(section, "collection")
  65. log.LOGGER.debug(
  66. "Test if '%s:%s' matches against '%s:%s' from section '%s'" % (
  67. user, collection, re_user, re_collection, section))
  68. user_match = re.match(re_user, user)
  69. if user_match:
  70. re_collection = re_collection.format(*user_match.groups())
  71. if re.match(re_collection, collection):
  72. log.LOGGER.debug("Section '%s' matches" % section)
  73. if permission in regex.get(section, "permission"):
  74. return True
  75. log.LOGGER.debug("Section '%s' does not match" % section)
  76. return False
  77. def authorized(user, collection, right):
  78. """Check if the user is allowed to read or write the collection.
  79. If the user is empty it checks for anonymous rights
  80. """
  81. rights_type = config.get("rights", "type").lower()
  82. return rights_type == "none" or (_read_from_sections(
  83. user or "", collection.url.rstrip("/") or "/", right))