mkcol.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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-2021 Unrud <unrud@outlook.com>
  6. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  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. import posixpath
  21. import socket
  22. from http import client
  23. import radicale.item as radicale_item
  24. from radicale import httputils, pathutils, rights, storage, types, xmlutils
  25. from radicale.app.base import ApplicationBase
  26. from radicale.log import logger
  27. class ApplicationPartMkcol(ApplicationBase):
  28. def do_MKCOL(self, environ: types.WSGIEnviron, base_prefix: str,
  29. path: str, user: str) -> types.WSGIResponse:
  30. """Manage MKCOL request."""
  31. permissions = self._rights.authorization(user, path)
  32. if not rights.intersect(permissions, "Ww"):
  33. return httputils.NOT_ALLOWED
  34. try:
  35. xml_content = self._read_xml_request_body(environ)
  36. except RuntimeError as e:
  37. logger.warning(
  38. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  39. return httputils.BAD_REQUEST
  40. except socket.timeout:
  41. logger.debug("Client timed out", exc_info=True)
  42. return httputils.REQUEST_TIMEOUT
  43. # Prepare before locking
  44. props_with_remove = xmlutils.props_from_request(xml_content)
  45. try:
  46. props = radicale_item.check_and_sanitize_props(props_with_remove)
  47. except ValueError as e:
  48. logger.warning(
  49. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  50. return httputils.BAD_REQUEST
  51. collection_type = props.get("tag") or "UNKNOWN"
  52. if props.get("tag") and "w" not in permissions:
  53. logger.warning("MKCOL request %r (type:%s): %s", path, collection_type, "rejected because of missing rights 'w'")
  54. return httputils.NOT_ALLOWED
  55. if not props.get("tag") and "W" not in permissions:
  56. logger.warning("MKCOL request %r (type:%s): %s", path, collection_type, "rejected because of missing rights 'W'")
  57. return httputils.NOT_ALLOWED
  58. with self._storage.acquire_lock("w", user):
  59. item = next(iter(self._storage.discover(path)), None)
  60. if item:
  61. return httputils.METHOD_NOT_ALLOWED
  62. parent_path = pathutils.unstrip_path(
  63. posixpath.dirname(pathutils.strip_path(path)), True)
  64. parent_item = next(iter(self._storage.discover(parent_path)), None)
  65. if not parent_item:
  66. return httputils.CONFLICT
  67. if (not isinstance(parent_item, storage.BaseCollection) or
  68. parent_item.tag):
  69. return httputils.FORBIDDEN
  70. try:
  71. self._storage.create_collection(path, props=props)
  72. except ValueError as e:
  73. logger.warning(
  74. "Bad MKCOL request on %r (type:%s): %s", path, collection_type, e, exc_info=True)
  75. return httputils.BAD_REQUEST
  76. logger.info("MKCOL request %r (type:%s): %s", path, collection_type, "successful")
  77. return client.CREATED, {}, None