regex.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2013 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. Regex-based rights.
  20. Regexes are read from a file whose name is specified in the config (section
  21. "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. Section names are only used for naming the rule.
  27. Leading or ending slashes are trimmed from collection's path.
  28. Examples:
  29. # This means all users starting with "admin" may read any collection
  30. [admin]
  31. user: ^admin.*\|.+?$
  32. collection: .*
  33. permission: r
  34. # This means all users may read and write any collection starting with public.
  35. # We do so by just not testing against the user string.
  36. [public]
  37. user: .*
  38. collection: ^public(/.+)?$
  39. permission: rw
  40. # A little more complex: give read access to users from a domain for all
  41. # collections of all the users (ie. user@domain.tld can read domain/*).
  42. [domain-wide-access]
  43. user: ^.+@(.+)\..+$
  44. collection: ^{0}/.+$
  45. permission: r
  46. # Allow authenticated user to read all collections
  47. [allow-everyone-read]
  48. user: .*
  49. collection: .*
  50. permission: r
  51. # Give write access to owners
  52. [owner-write]
  53. user: .*
  54. collection: ^%(login)s/.+$
  55. permission: w
  56. """
  57. import os.path
  58. import re
  59. from radicale import config, log
  60. # Manage Python2/3 different modules
  61. # pylint: disable=F0401
  62. try:
  63. from configparser import ConfigParser
  64. except ImportError:
  65. from ConfigParser import ConfigParser
  66. # pylint: enable=F0401
  67. FILENAME = (
  68. os.path.expanduser(config.get("rights", "file")) or
  69. log.LOGGER.error("No file name configured for rights type 'regex'"))
  70. def _read_from_sections(user, collection, permission):
  71. """Get regex sections."""
  72. log.LOGGER.debug("Reading regex from file %s" % FILENAME)
  73. regex = ConfigParser({"login": user, "path": collection})
  74. if not regex.read(FILENAME):
  75. log.LOGGER.error(
  76. "File '%s' not found for rights management type 'regex'" %
  77. FILENAME)
  78. return False
  79. for section in regex.sections():
  80. re_user = regex.get(section, "user")
  81. re_collection = regex.get(section, "collection")
  82. log.LOGGER.debug(
  83. "Test if '%s:%s' matches against '%s:%s' from section '%s'" % (
  84. user, collection, re_user, re_collection, section))
  85. user_match = re.match(re_user, user)
  86. if user_match:
  87. re_collection = re_collection.format(*user_match.groups())
  88. if re.match(re_collection, collection):
  89. log.LOGGER.debug("Section '%s' matches" % section)
  90. if permission in regex.get(section, "permission"):
  91. return True
  92. log.LOGGER.debug("Section '%s' does not match" % section)
  93. return False
  94. def read_authorized(user, collection):
  95. """Check if the user is allowed to read the collection."""
  96. return user and _read_from_sections(
  97. user, collection.url.rstrip("/") or "/", "r")
  98. def write_authorized(user, collection):
  99. """Check if the user is allowed to write the collection."""
  100. return user and _read_from_sections(
  101. user, collection.url.rstrip("/") or "/", "w")