mkcol.py 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 errno
  21. import posixpath
  22. import re
  23. import socket
  24. from http import client
  25. import radicale.item as radicale_item
  26. from radicale import httputils, pathutils, rights, storage, types, xmlutils
  27. from radicale.app.base import ApplicationBase
  28. from radicale.log import logger
  29. class ApplicationPartMkcol(ApplicationBase):
  30. def do_MKCOL(self, environ: types.WSGIEnviron, base_prefix: str,
  31. path: str, user: str) -> types.WSGIResponse:
  32. """Manage MKCOL request."""
  33. permissions = self._rights.authorization(user, path)
  34. if not rights.intersect(permissions, "Ww"):
  35. return httputils.NOT_ALLOWED
  36. try:
  37. xml_content = self._read_xml_request_body(environ)
  38. except RuntimeError as e:
  39. logger.warning(
  40. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  41. return httputils.BAD_REQUEST
  42. except socket.timeout:
  43. logger.debug("Client timed out", exc_info=True)
  44. return httputils.REQUEST_TIMEOUT
  45. # Prepare before locking
  46. props_with_remove = xmlutils.props_from_request(xml_content)
  47. try:
  48. props = radicale_item.check_and_sanitize_props(props_with_remove)
  49. except ValueError as e:
  50. logger.warning(
  51. "Bad MKCOL request on %r: %s", path, e, exc_info=True)
  52. return httputils.BAD_REQUEST
  53. collection_type = props.get("tag") or "UNKNOWN"
  54. if 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. if not props.get("tag") and "W" not in permissions:
  58. logger.warning("MKCOL request %r (type:%s): %s", path, collection_type, "rejected because of missing rights 'W'")
  59. return httputils.NOT_ALLOWED
  60. with self._storage.acquire_lock("w", user, path=path, request="MKCOL"):
  61. item = next(iter(self._storage.discover(path)), None)
  62. if item:
  63. return httputils.METHOD_NOT_ALLOWED
  64. parent_path = pathutils.unstrip_path(
  65. posixpath.dirname(pathutils.strip_path(path)), True)
  66. parent_item = next(iter(self._storage.discover(parent_path)), None)
  67. if not parent_item:
  68. return httputils.CONFLICT
  69. if (not isinstance(parent_item, storage.BaseCollection) or
  70. parent_item.tag):
  71. return httputils.FORBIDDEN
  72. try:
  73. self._storage.create_collection(path, props=props)
  74. except ValueError as e:
  75. # return better matching HTTP result in case errno is provided and catched
  76. errno_match = re.search("\\[Errno ([0-9]+)\\]", str(e))
  77. if errno_match:
  78. logger.error(
  79. "Failed MKCOL request on %r (type:%s): %s", path, collection_type, e, exc_info=True)
  80. errno_e = int(errno_match.group(1))
  81. if errno_e == errno.ENOSPC:
  82. return httputils.INSUFFICIENT_STORAGE
  83. elif errno_e in [errno.EPERM, errno.EACCES]:
  84. return httputils.FORBIDDEN
  85. else:
  86. return httputils.INTERNAL_SERVER_ERROR
  87. else:
  88. logger.warning(
  89. "Bad MKCOL request on %r (type:%s): %s", path, collection_type, e, exc_info=True)
  90. return httputils.BAD_REQUEST
  91. logger.info("MKCOL request %r (type:%s): %s", path, collection_type, "successful")
  92. return client.CREATED, {}, None