rights.py 3.0 KB

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