rights.py 3.0 KB

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