mkcol.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2017 Guillaume Ayoub
  5. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. import posixpath
  20. import socket
  21. from http import client
  22. import radicale.item as radicale_item
  23. from radicale import httputils, pathutils, rights, storage, types, xmlutils
  24. from radicale.app.base import ApplicationBase
  25. from radicale.log import logger
  26. class ApplicationPartMkcol(ApplicationBase):
  27. def do_MKCOL(self, environ: types.WSGIEnviron, base_prefix: str,
  28. path: str, user: str) -> types.WSGIResponse:
  29. """Manage MKCOL request."""
  30. permissions = self._rights.authorization(user, path)
  31. if not rights.intersect(permissions, "Ww"):
  32. return httputils.NOT_ALLOWED
  33. try:
  34. xml_content = self._read_xml_request_body(environ)
  35. except RuntimeError as e:
  36. logger.warning(
  37. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  38. return httputils.BAD_REQUEST
  39. except socket.timeout:
  40. logger.debug("Client timed out", exc_info=True)
  41. return httputils.REQUEST_TIMEOUT
  42. # Prepare before locking
  43. props_with_remove = xmlutils.props_from_request(xml_content)
  44. try:
  45. props = radicale_item.check_and_sanitize_props(props_with_remove)
  46. except ValueError as e:
  47. logger.warning(
  48. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  49. return httputils.BAD_REQUEST
  50. collection_type = props.get("tag") or "UNKNOWN"
  51. if props.get("tag") and "w" not in permissions:
  52. logger.warning("MKCOL request %r (type:%s): %s", path, collection_type, "rejected because of missing rights 'w'")
  53. return httputils.NOT_ALLOWED
  54. if not props.get("tag") and "W" not in permissions:
  55. logger.warning("MKCOL request %r (type:%s): %s", path, collection_type, "rejected because of missing rights 'W'")
  56. return httputils.NOT_ALLOWED
  57. with self._storage.acquire_lock("w", user):
  58. item = next(iter(self._storage.discover(path)), None)
  59. if item:
  60. return httputils.METHOD_NOT_ALLOWED
  61. parent_path = pathutils.unstrip_path(
  62. posixpath.dirname(pathutils.strip_path(path)), True)
  63. parent_item = next(iter(self._storage.discover(parent_path)), None)
  64. if not parent_item:
  65. return httputils.CONFLICT
  66. if (not isinstance(parent_item, storage.BaseCollection) or
  67. parent_item.tag):
  68. return httputils.FORBIDDEN
  69. try:
  70. self._storage.create_collection(path, props=props)
  71. except ValueError as e:
  72. logger.warning(
  73. "Bad MKCOL request on %r (type:%s): %s", path, collection_type, e, exc_info=True)
  74. return httputils.BAD_REQUEST
  75. logger.info("MKCOL request %r (type:%s): %s", path, collection_type, "successful")
  76. return client.CREATED, {}, None