propfind.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. # This file is part of Radicale Server - Calendar 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 collections
  20. import itertools
  21. import posixpath
  22. import socket
  23. from http import client
  24. from xml.etree import ElementTree as ET
  25. from radicale import app, httputils, pathutils, rights, storage, xmlutils
  26. from radicale.log import logger
  27. def xml_propfind(base_prefix, path, xml_request, allowed_items, user,
  28. encoding):
  29. """Read and answer PROPFIND requests.
  30. Read rfc4918-9.1 for info.
  31. The collections parameter is a list of collections that are to be included
  32. in the output.
  33. """
  34. # A client may choose not to submit a request body. An empty PROPFIND
  35. # request body MUST be treated as if it were an 'allprop' request.
  36. top_tag = (xml_request[0] if xml_request is not None else
  37. ET.Element(xmlutils.make_clark("D:allprop")))
  38. props = ()
  39. allprop = False
  40. propname = False
  41. if top_tag.tag == xmlutils.make_clark("D:allprop"):
  42. allprop = True
  43. elif top_tag.tag == xmlutils.make_clark("D:propname"):
  44. propname = True
  45. elif top_tag.tag == xmlutils.make_clark("D:prop"):
  46. props = [prop.tag for prop in top_tag]
  47. if xmlutils.make_clark("D:current-user-principal") in props and not user:
  48. # Ask for authentication
  49. # Returning the DAV:unauthenticated pseudo-principal as specified in
  50. # RFC 5397 doesn't seem to work with DAVx5.
  51. return client.FORBIDDEN, None
  52. # Writing answer
  53. multistatus = ET.Element(xmlutils.make_clark("D:multistatus"))
  54. for item, permission in allowed_items:
  55. write = permission == "w"
  56. multistatus.append(xml_propfind_response(
  57. base_prefix, path, item, props, user, encoding, write=write,
  58. allprop=allprop, propname=propname))
  59. return client.MULTI_STATUS, multistatus
  60. def xml_propfind_response(base_prefix, path, item, props, user, encoding,
  61. write=False, propname=False, allprop=False):
  62. """Build and return a PROPFIND response."""
  63. if propname and allprop or (props and (propname or allprop)):
  64. raise ValueError("Only use one of props, propname and allprops")
  65. is_collection = isinstance(item, storage.BaseCollection)
  66. if is_collection:
  67. is_leaf = item.get_meta("tag") in ("VADDRESSBOOK", "VCALENDAR")
  68. collection = item
  69. else:
  70. collection = item.collection
  71. response = ET.Element(xmlutils.make_clark("D:response"))
  72. href = ET.Element(xmlutils.make_clark("D:href"))
  73. if is_collection:
  74. # Some clients expect collections to end with /
  75. uri = pathutils.unstrip_path(item.path, True)
  76. else:
  77. uri = pathutils.unstrip_path(
  78. posixpath.join(collection.path, item.href))
  79. href.text = xmlutils.make_href(base_prefix, uri)
  80. response.append(href)
  81. if propname or allprop:
  82. props = []
  83. # Should list all properties that can be retrieved by the code below
  84. props.append(xmlutils.make_clark("D:principal-collection-set"))
  85. props.append(xmlutils.make_clark("D:current-user-principal"))
  86. props.append(xmlutils.make_clark("D:current-user-privilege-set"))
  87. props.append(xmlutils.make_clark("D:supported-report-set"))
  88. props.append(xmlutils.make_clark("D:resourcetype"))
  89. props.append(xmlutils.make_clark("D:owner"))
  90. if is_collection and collection.is_principal:
  91. props.append(xmlutils.make_clark("C:calendar-user-address-set"))
  92. props.append(xmlutils.make_clark("D:principal-URL"))
  93. props.append(xmlutils.make_clark("CR:addressbook-home-set"))
  94. props.append(xmlutils.make_clark("C:calendar-home-set"))
  95. if not is_collection or is_leaf:
  96. props.append(xmlutils.make_clark("D:getetag"))
  97. props.append(xmlutils.make_clark("D:getlastmodified"))
  98. props.append(xmlutils.make_clark("D:getcontenttype"))
  99. props.append(xmlutils.make_clark("D:getcontentlength"))
  100. if is_collection:
  101. if is_leaf:
  102. props.append(xmlutils.make_clark("D:displayname"))
  103. props.append(xmlutils.make_clark("D:sync-token"))
  104. if collection.get_meta("tag") == "VCALENDAR":
  105. props.append(xmlutils.make_clark("CS:getctag"))
  106. props.append(
  107. xmlutils.make_clark("C:supported-calendar-component-set"))
  108. meta = item.get_meta()
  109. for tag in meta:
  110. if tag == "tag":
  111. continue
  112. clark_tag = xmlutils.make_clark(tag)
  113. if clark_tag not in props:
  114. props.append(clark_tag)
  115. responses = collections.defaultdict(list)
  116. if propname:
  117. for tag in props:
  118. responses[200].append(ET.Element(tag))
  119. props = ()
  120. for tag in props:
  121. element = ET.Element(tag)
  122. is404 = False
  123. if tag == xmlutils.make_clark("D:getetag"):
  124. if not is_collection or is_leaf:
  125. element.text = item.etag
  126. else:
  127. is404 = True
  128. elif tag == xmlutils.make_clark("D:getlastmodified"):
  129. if not is_collection or is_leaf:
  130. element.text = item.last_modified
  131. else:
  132. is404 = True
  133. elif tag == xmlutils.make_clark("D:principal-collection-set"):
  134. tag = ET.Element(xmlutils.make_clark("D:href"))
  135. tag.text = xmlutils.make_href(base_prefix, "/")
  136. element.append(tag)
  137. elif (tag in (xmlutils.make_clark("C:calendar-user-address-set"),
  138. xmlutils.make_clark("D:principal-URL"),
  139. xmlutils.make_clark("CR:addressbook-home-set"),
  140. xmlutils.make_clark("C:calendar-home-set")) and
  141. collection.is_principal and is_collection):
  142. tag = ET.Element(xmlutils.make_clark("D:href"))
  143. tag.text = xmlutils.make_href(base_prefix, path)
  144. element.append(tag)
  145. elif tag == xmlutils.make_clark("C:supported-calendar-component-set"):
  146. human_tag = xmlutils.make_human_tag(tag)
  147. if is_collection and is_leaf:
  148. meta = item.get_meta(human_tag)
  149. if meta:
  150. components = meta.split(",")
  151. else:
  152. components = ("VTODO", "VEVENT", "VJOURNAL")
  153. for component in components:
  154. comp = ET.Element(xmlutils.make_clark("C:comp"))
  155. comp.set("name", component)
  156. element.append(comp)
  157. else:
  158. is404 = True
  159. elif tag == xmlutils.make_clark("D:current-user-principal"):
  160. if user:
  161. tag = ET.Element(xmlutils.make_clark("D:href"))
  162. tag.text = xmlutils.make_href(base_prefix, "/%s/" % user)
  163. element.append(tag)
  164. else:
  165. element.append(ET.Element(
  166. xmlutils.make_clark("D:unauthenticated")))
  167. elif tag == xmlutils.make_clark("D:current-user-privilege-set"):
  168. privileges = ["D:read"]
  169. if write:
  170. privileges.append("D:all")
  171. privileges.append("D:write")
  172. privileges.append("D:write-properties")
  173. privileges.append("D:write-content")
  174. for human_tag in privileges:
  175. privilege = ET.Element(xmlutils.make_clark("D:privilege"))
  176. privilege.append(ET.Element(
  177. xmlutils.make_clark(human_tag)))
  178. element.append(privilege)
  179. elif tag == xmlutils.make_clark("D:supported-report-set"):
  180. # These 3 reports are not implemented
  181. reports = ["D:expand-property",
  182. "D:principal-search-property-set",
  183. "D:principal-property-search"]
  184. if is_collection and is_leaf:
  185. reports.append("D:sync-collection")
  186. if item.get_meta("tag") == "VADDRESSBOOK":
  187. reports.append("CR:addressbook-multiget")
  188. reports.append("CR:addressbook-query")
  189. elif item.get_meta("tag") == "VCALENDAR":
  190. reports.append("C:calendar-multiget")
  191. reports.append("C:calendar-query")
  192. for human_tag in reports:
  193. supported_report = ET.Element(
  194. xmlutils.make_clark("D:supported-report"))
  195. report_tag = ET.Element(xmlutils.make_clark("D:report"))
  196. report_tag.append(ET.Element(xmlutils.make_clark(human_tag)))
  197. supported_report.append(report_tag)
  198. element.append(supported_report)
  199. elif tag == xmlutils.make_clark("D:getcontentlength"):
  200. if not is_collection or is_leaf:
  201. element.text = str(len(item.serialize().encode(encoding)))
  202. else:
  203. is404 = True
  204. elif tag == xmlutils.make_clark("D:owner"):
  205. # return empty elment, if no owner available (rfc3744-5.1)
  206. if collection.owner:
  207. tag = ET.Element(xmlutils.make_clark("D:href"))
  208. tag.text = xmlutils.make_href(
  209. base_prefix, "/%s/" % collection.owner)
  210. element.append(tag)
  211. elif is_collection:
  212. if tag == xmlutils.make_clark("D:getcontenttype"):
  213. if is_leaf:
  214. element.text = xmlutils.MIMETYPES[item.get_meta("tag")]
  215. else:
  216. is404 = True
  217. elif tag == xmlutils.make_clark("D:resourcetype"):
  218. if item.is_principal:
  219. tag = ET.Element(xmlutils.make_clark("D:principal"))
  220. element.append(tag)
  221. if is_leaf:
  222. if item.get_meta("tag") == "VADDRESSBOOK":
  223. tag = ET.Element(
  224. xmlutils.make_clark("CR:addressbook"))
  225. element.append(tag)
  226. elif item.get_meta("tag") == "VCALENDAR":
  227. tag = ET.Element(xmlutils.make_clark("C:calendar"))
  228. element.append(tag)
  229. tag = ET.Element(xmlutils.make_clark("D:collection"))
  230. element.append(tag)
  231. elif tag == xmlutils.make_clark("RADICALE:displayname"):
  232. # Only for internal use by the web interface
  233. displayname = item.get_meta("D:displayname")
  234. if displayname is not None:
  235. element.text = displayname
  236. else:
  237. is404 = True
  238. elif tag == xmlutils.make_clark("D:displayname"):
  239. displayname = item.get_meta("D:displayname")
  240. if not displayname and is_leaf:
  241. displayname = item.path
  242. if displayname is not None:
  243. element.text = displayname
  244. else:
  245. is404 = True
  246. elif tag == xmlutils.make_clark("CS:getctag"):
  247. if is_leaf:
  248. element.text = item.etag
  249. else:
  250. is404 = True
  251. elif tag == xmlutils.make_clark("D:sync-token"):
  252. if is_leaf:
  253. element.text, _ = item.sync()
  254. else:
  255. is404 = True
  256. else:
  257. human_tag = xmlutils.make_human_tag(tag)
  258. meta = item.get_meta(human_tag)
  259. if meta is not None:
  260. element.text = meta
  261. else:
  262. is404 = True
  263. # Not for collections
  264. elif tag == xmlutils.make_clark("D:getcontenttype"):
  265. element.text = xmlutils.get_content_type(item, encoding)
  266. elif tag == xmlutils.make_clark("D:resourcetype"):
  267. # resourcetype must be returned empty for non-collection elements
  268. pass
  269. else:
  270. is404 = True
  271. responses[404 if is404 else 200].append(element)
  272. for status_code, childs in responses.items():
  273. if not childs:
  274. continue
  275. propstat = ET.Element(xmlutils.make_clark("D:propstat"))
  276. response.append(propstat)
  277. prop = ET.Element(xmlutils.make_clark("D:prop"))
  278. prop.extend(childs)
  279. propstat.append(prop)
  280. status = ET.Element(xmlutils.make_clark("D:status"))
  281. status.text = xmlutils.make_response(status_code)
  282. propstat.append(status)
  283. return response
  284. class ApplicationPropfindMixin:
  285. def _collect_allowed_items(self, items, user):
  286. """Get items from request that user is allowed to access."""
  287. for item in items:
  288. if isinstance(item, storage.BaseCollection):
  289. path = pathutils.unstrip_path(item.path, True)
  290. if item.get_meta("tag"):
  291. permissions = rights.intersect(
  292. self._rights.authorization(user, path), "rw")
  293. target = "collection with tag %r" % item.path
  294. else:
  295. permissions = rights.intersect(
  296. self._rights.authorization(user, path), "RW")
  297. target = "collection %r" % item.path
  298. else:
  299. path = pathutils.unstrip_path(item.collection.path, True)
  300. permissions = rights.intersect(
  301. self._rights.authorization(user, path), "rw")
  302. target = "item %r from %r" % (item.href, item.collection.path)
  303. if rights.intersect(permissions, "Ww"):
  304. permission = "w"
  305. status = "write"
  306. elif rights.intersect(permissions, "Rr"):
  307. permission = "r"
  308. status = "read"
  309. else:
  310. permission = ""
  311. status = "NO"
  312. logger.debug(
  313. "%s has %s access to %s",
  314. repr(user) if user else "anonymous user", status, target)
  315. if permission:
  316. yield item, permission
  317. def do_PROPFIND(self, environ, base_prefix, path, user):
  318. """Manage PROPFIND request."""
  319. access = app.Access(self._rights, user, path)
  320. if not access.check("r"):
  321. return httputils.NOT_ALLOWED
  322. try:
  323. xml_content = self._read_xml_content(environ)
  324. except RuntimeError as e:
  325. logger.warning(
  326. "Bad PROPFIND request on %r: %s", path, e, exc_info=True)
  327. return httputils.BAD_REQUEST
  328. except socket.timeout:
  329. logger.debug("client timed out", exc_info=True)
  330. return httputils.REQUEST_TIMEOUT
  331. with self._storage.acquire_lock("r", user):
  332. items = self._storage.discover(
  333. path, environ.get("HTTP_DEPTH", "0"))
  334. # take root item for rights checking
  335. item = next(items, None)
  336. if not item:
  337. return httputils.NOT_FOUND
  338. if not access.check("r", item):
  339. return httputils.NOT_ALLOWED
  340. # put item back
  341. items = itertools.chain([item], items)
  342. allowed_items = self._collect_allowed_items(items, user)
  343. headers = {"DAV": httputils.DAV_HEADERS,
  344. "Content-Type": "text/xml; charset=%s" % self._encoding}
  345. status, xml_answer = xml_propfind(
  346. base_prefix, path, xml_content, allowed_items, user,
  347. self._encoding)
  348. if status == client.FORBIDDEN and xml_answer is None:
  349. return httputils.NOT_ALLOWED
  350. return status, headers, self._write_xml_content(xml_answer)