__init__.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2016 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. Radicale Server module.
  20. This module offers a WSGI application class.
  21. To use this module, you should take a look at the file ``radicale.py`` that
  22. should have been included in this package.
  23. """
  24. import os
  25. import pprint
  26. import base64
  27. import socket
  28. import socketserver
  29. import ssl
  30. import wsgiref.simple_server
  31. import re
  32. from http import client
  33. from urllib.parse import unquote, urlparse
  34. import vobject
  35. from . import auth, rights, storage, xmlutils
  36. VERSION = "2.0.0-pre"
  37. # Standard "not allowed" response that is returned when an authenticated user
  38. # tries to access information they don't have rights to
  39. NOT_ALLOWED = (client.FORBIDDEN, {}, None)
  40. WELL_KNOWN_RE = re.compile(r"/\.well-known/(carddav|caldav)/?$")
  41. class HTTPServer(wsgiref.simple_server.WSGIServer):
  42. """HTTP server."""
  43. def __init__(self, address, handler, bind_and_activate=True):
  44. """Create server."""
  45. ipv6 = ":" in address[0]
  46. if ipv6:
  47. self.address_family = socket.AF_INET6
  48. # Do not bind and activate, as we might change socket options
  49. super().__init__(address, handler, False)
  50. if ipv6:
  51. # Only allow IPv6 connections to the IPv6 socket
  52. self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
  53. if bind_and_activate:
  54. self.server_bind()
  55. self.server_activate()
  56. class HTTPSServer(HTTPServer):
  57. """HTTPS server."""
  58. # These class attributes must be set before creating instance
  59. certificate = None
  60. key = None
  61. protocol = None
  62. cyphers = None
  63. def __init__(self, address, handler):
  64. """Create server by wrapping HTTP socket in an SSL socket."""
  65. super().__init__(address, handler, bind_and_activate=False)
  66. self.socket = ssl.wrap_socket(
  67. self.socket, self.key, self.certificate, server_side=True,
  68. ssl_version=self.protocol, cyphers=self.cyphers)
  69. self.server_bind()
  70. self.server_activate()
  71. class ThreadedHTTPServer(socketserver.ThreadingMixIn, HTTPServer):
  72. pass
  73. class ThreadedHTTPSServer(socketserver.ThreadingMixIn, HTTPSServer):
  74. pass
  75. class RequestHandler(wsgiref.simple_server.WSGIRequestHandler):
  76. """HTTP requests handler."""
  77. def log_message(self, *args, **kwargs):
  78. """Disable inner logging management."""
  79. class Application:
  80. """WSGI application managing collections."""
  81. def __init__(self, configuration, logger):
  82. """Initialize application."""
  83. super().__init__()
  84. self.configuration = configuration
  85. self.logger = logger
  86. self.is_authenticated = auth.load(configuration, logger)
  87. self.Collection = storage.load(configuration, logger)
  88. self.authorized = rights.load(configuration, logger)
  89. self.encoding = configuration.get("encoding", "request")
  90. if configuration.getboolean("logging", "full_environment"):
  91. self.headers_log = lambda environ: environ
  92. # This method is overriden in __init__ if full_environment is set
  93. # pylint: disable=E0202
  94. @staticmethod
  95. def headers_log(environ):
  96. """Remove environment variables from the headers for logging."""
  97. request_environ = dict(environ)
  98. for shell_variable in os.environ:
  99. if shell_variable in request_environ:
  100. del request_environ[shell_variable]
  101. return request_environ
  102. # pylint: enable=E0202
  103. def decode(self, text, environ):
  104. """Try to magically decode ``text`` according to given ``environ``."""
  105. # List of charsets to try
  106. charsets = []
  107. # First append content charset given in the request
  108. content_type = environ.get("CONTENT_TYPE")
  109. if content_type and "charset=" in content_type:
  110. charsets.append(
  111. content_type.split("charset=")[1].split(";")[0].strip())
  112. # Then append default Radicale charset
  113. charsets.append(self.encoding)
  114. # Then append various fallbacks
  115. charsets.append("utf-8")
  116. charsets.append("iso8859-1")
  117. # Try to decode
  118. for charset in charsets:
  119. try:
  120. return text.decode(charset)
  121. except UnicodeDecodeError:
  122. pass
  123. raise UnicodeDecodeError
  124. def collect_allowed_items(self, items, user):
  125. """Get items from request that user is allowed to access."""
  126. read_last_collection_allowed = None
  127. write_last_collection_allowed = None
  128. read_allowed_items = []
  129. write_allowed_items = []
  130. for item in items:
  131. if isinstance(item, self.Collection):
  132. if self.authorized(user, item, "r"):
  133. self.logger.debug(
  134. "%s has read access to collection %s" %
  135. (user or "Anonymous", item.path or "/"))
  136. read_last_collection_allowed = True
  137. read_allowed_items.append(item)
  138. else:
  139. self.logger.debug(
  140. "%s has NO read access to collection %s" %
  141. (user or "Anonymous", item.path or "/"))
  142. read_last_collection_allowed = False
  143. if self.authorized(user, item, "w"):
  144. self.logger.debug(
  145. "%s has write access to collection %s" %
  146. (user or "Anonymous", item.path or "/"))
  147. write_last_collection_allowed = True
  148. write_allowed_items.append(item)
  149. else:
  150. self.logger.debug(
  151. "%s has NO write access to collection %s" %
  152. (user or "Anonymous", item.path or "/"))
  153. write_last_collection_allowed = False
  154. else:
  155. # item is not a collection, it's the child of the last
  156. # collection we've met in the loop. Only add this item
  157. # if this last collection was allowed.
  158. if read_last_collection_allowed:
  159. self.logger.debug(
  160. "%s has read access to item %s" %
  161. (user or "Anonymous", item.href))
  162. read_allowed_items.append(item)
  163. else:
  164. self.logger.debug(
  165. "%s has NO read access to item %s" %
  166. (user or "Anonymous", item.href))
  167. if write_last_collection_allowed:
  168. self.logger.debug(
  169. "%s has write access to item %s" %
  170. (user or "Anonymous", item.href))
  171. write_allowed_items.append(item)
  172. else:
  173. self.logger.debug(
  174. "%s has NO write access to item %s" %
  175. (user or "Anonymous", item.href))
  176. return read_allowed_items, write_allowed_items
  177. def __call__(self, environ, start_response):
  178. """Manage a request."""
  179. self.logger.info("%s request at %s received" % (
  180. environ["REQUEST_METHOD"], environ["PATH_INFO"]))
  181. headers = pprint.pformat(self.headers_log(environ))
  182. self.logger.debug("Request headers:\n%s" % headers)
  183. # Strip base_prefix from request URI
  184. base_prefix = self.configuration.get("server", "base_prefix")
  185. if environ["PATH_INFO"].startswith(base_prefix):
  186. environ["PATH_INFO"] = environ["PATH_INFO"][len(base_prefix):]
  187. elif self.configuration.get("server", "can_skip_base_prefix"):
  188. self.logger.debug(
  189. "Prefix already stripped from path: %s", environ["PATH_INFO"])
  190. else:
  191. # Request path not starting with base_prefix, not allowed
  192. self.logger.debug(
  193. "Path not starting with prefix: %s", environ["PATH_INFO"])
  194. status, headers, _ = NOT_ALLOWED
  195. start_response(status, list(headers.items()))
  196. return []
  197. # Sanitize request URI
  198. environ["PATH_INFO"] = storage.sanitize_path(
  199. unquote(environ["PATH_INFO"]))
  200. self.logger.debug("Sanitized path: %s", environ["PATH_INFO"])
  201. path = environ["PATH_INFO"]
  202. # Get function corresponding to method
  203. function = getattr(self, "do_%s" % environ["REQUEST_METHOD"].upper())
  204. # Ask authentication backend to check rights
  205. authorization = environ.get("HTTP_AUTHORIZATION", None)
  206. if authorization:
  207. authorization = authorization.lstrip("Basic").strip()
  208. user, password = self.decode(base64.b64decode(
  209. authorization.encode("ascii")), environ).split(":", 1)
  210. else:
  211. user = environ.get("REMOTE_USER")
  212. password = None
  213. well_known = WELL_KNOWN_RE.match(path)
  214. if well_known:
  215. redirect = self.configuration.get(
  216. "well-known", well_known.group(1))
  217. try:
  218. redirect = redirect % ({"user": user} if user else {})
  219. except KeyError:
  220. status = client.UNAUTHORIZED
  221. realm = self.configuration.get("server", "realm")
  222. headers = {"WWW-Authenticate": "Basic realm=\"%s\"" % realm}
  223. self.logger.info(
  224. "Refused /.well-known/ redirection to anonymous user")
  225. else:
  226. status = client.SEE_OTHER
  227. self.logger.info("/.well-known/ redirection to: %s" % redirect)
  228. headers = {"Location": redirect}
  229. status = "%i %s" % (
  230. status, client.responses.get(status, "Unknown"))
  231. start_response(status, list(headers.items()))
  232. return []
  233. is_authenticated = self.is_authenticated(user, password)
  234. is_valid_user = is_authenticated or not user
  235. # Get content
  236. content_length = int(environ.get("CONTENT_LENGTH") or 0)
  237. if content_length:
  238. content = self.decode(
  239. environ["wsgi.input"].read(content_length), environ)
  240. self.logger.debug("Request content:\n%s" % content)
  241. else:
  242. content = None
  243. if is_valid_user:
  244. if function in (self.do_GET, self.do_HEAD,
  245. self.do_OPTIONS, self.do_PROPFIND,
  246. self.do_REPORT):
  247. lock_mode = "r"
  248. else:
  249. lock_mode = "w"
  250. with self.Collection.acquire_lock(lock_mode):
  251. items = self.Collection.discover(
  252. path, environ.get("HTTP_DEPTH", "0"))
  253. read_allowed_items, write_allowed_items = (
  254. self.collect_allowed_items(items, user))
  255. if (read_allowed_items or write_allowed_items or
  256. is_authenticated and function == self.do_PROPFIND or
  257. function == self.do_OPTIONS):
  258. status, headers, answer = function(
  259. environ, read_allowed_items, write_allowed_items,
  260. content, user)
  261. else:
  262. status, headers, answer = NOT_ALLOWED
  263. else:
  264. status, headers, answer = NOT_ALLOWED
  265. if (status, headers, answer) == NOT_ALLOWED and not is_authenticated:
  266. # Unknown or unauthorized user
  267. self.logger.info("%s refused" % (user or "Anonymous user"))
  268. status = client.UNAUTHORIZED
  269. realm = self.configuration.get("server", "realm")
  270. headers = {
  271. "WWW-Authenticate":
  272. "Basic realm=\"%s\"" % realm}
  273. answer = None
  274. # Set content length
  275. if answer:
  276. self.logger.debug("Response content:\n%s" % answer, environ)
  277. answer = answer.encode(self.encoding)
  278. headers["Content-Length"] = str(len(answer))
  279. if self.configuration.has_section("headers"):
  280. for key in self.configuration.options("headers"):
  281. headers[key] = self.configuration.get("headers", key)
  282. # Start response
  283. status = "%i %s" % (status, client.responses.get(status, "Unknown"))
  284. self.logger.debug("Answer status: %s" % status)
  285. start_response(status, list(headers.items()))
  286. # Return response content
  287. return [answer] if answer else []
  288. # All these functions must have the same parameters, some are useless
  289. # pylint: disable=W0612,W0613,R0201
  290. def do_DELETE(self, environ, read_collections, write_collections, content,
  291. user):
  292. """Manage DELETE request."""
  293. if not write_collections:
  294. return NOT_ALLOWED
  295. collection = write_collections[0]
  296. if collection.path == environ["PATH_INFO"].strip("/"):
  297. # Path matching the collection, the collection must be deleted
  298. item = collection
  299. else:
  300. # Try to get an item matching the path
  301. name = xmlutils.name_from_path(environ["PATH_INFO"], collection)
  302. item = collection.get(name)
  303. if item:
  304. if_match = environ.get("HTTP_IF_MATCH", "*")
  305. if if_match in ("*", item.etag):
  306. # No ETag precondition or precondition verified, delete item
  307. answer = xmlutils.delete(environ["PATH_INFO"], collection)
  308. return client.OK, {}, answer
  309. # No item or ETag precondition not verified, do not delete item
  310. return client.PRECONDITION_FAILED, {}, None
  311. def do_GET(self, environ, read_collections, write_collections, content,
  312. user):
  313. """Manage GET request."""
  314. # Display a "Radicale works!" message if the root URL is requested
  315. if environ["PATH_INFO"] == "/":
  316. headers = {"Content-type": "text/html"}
  317. answer = "<!DOCTYPE html>\n<title>Radicale</title>Radicale works!"
  318. return client.OK, headers, answer
  319. if not read_collections:
  320. return NOT_ALLOWED
  321. collection = read_collections[0]
  322. item_name = xmlutils.name_from_path(environ["PATH_INFO"], collection)
  323. if item_name:
  324. # Get collection item
  325. item = collection.get(item_name)
  326. if item:
  327. answer = item.serialize()
  328. etag = item.etag
  329. else:
  330. return client.NOT_FOUND, {}, None
  331. else:
  332. # Get whole collection
  333. answer = collection.serialize()
  334. etag = collection.etag
  335. if answer:
  336. headers = {
  337. "Content-Type": storage.MIMETYPES[collection.get_meta("tag")],
  338. "Last-Modified": collection.last_modified,
  339. "ETag": etag}
  340. else:
  341. headers = {}
  342. return client.OK, headers, answer
  343. def do_HEAD(self, environ, read_collections, write_collections, content,
  344. user):
  345. """Manage HEAD request."""
  346. status, headers, answer = self.do_GET(
  347. environ, read_collections, write_collections, content, user)
  348. return status, headers, None
  349. def do_MKCALENDAR(self, environ, read_collections, write_collections,
  350. content, user):
  351. """Manage MKCALENDAR request."""
  352. if not write_collections:
  353. return NOT_ALLOWED
  354. collection = write_collections[0]
  355. props = xmlutils.props_from_request(content)
  356. # TODO: use this?
  357. # timezone = props.get("C:calendar-timezone")
  358. collection = self.Collection.create_collection(
  359. environ["PATH_INFO"], tag="VCALENDAR")
  360. for key, value in props.items():
  361. collection.set_meta(key, value)
  362. return client.CREATED, {}, None
  363. def do_MKCOL(self, environ, read_collections, write_collections, content,
  364. user):
  365. """Manage MKCOL request."""
  366. if not write_collections:
  367. return NOT_ALLOWED
  368. collection = write_collections[0]
  369. props = xmlutils.props_from_request(content)
  370. collection = self.Collection.create_collection(environ["PATH_INFO"])
  371. for key, value in props.items():
  372. collection.set_meta(key, value)
  373. return client.CREATED, {}, None
  374. def do_MOVE(self, environ, read_collections, write_collections, content,
  375. user):
  376. """Manage MOVE request."""
  377. if not write_collections:
  378. return NOT_ALLOWED
  379. from_collection = write_collections[0]
  380. from_name = xmlutils.name_from_path(
  381. environ["PATH_INFO"], from_collection)
  382. item = from_collection.get(from_name)
  383. if item:
  384. # Move the item
  385. to_url_parts = urlparse(environ["HTTP_DESTINATION"])
  386. if to_url_parts.netloc == environ["HTTP_HOST"]:
  387. to_url = to_url_parts.path
  388. to_path, to_name = to_url.rstrip("/").rsplit("/", 1)
  389. for to_collection in self.Collection.discover(
  390. to_path, depth="0"):
  391. if to_collection in write_collections:
  392. to_collection.upload(to_name, item)
  393. from_collection.delete(from_name)
  394. return client.CREATED, {}, None
  395. else:
  396. return NOT_ALLOWED
  397. else:
  398. # Remote destination server, not supported
  399. return client.BAD_GATEWAY, {}, None
  400. else:
  401. # No item found
  402. return client.GONE, {}, None
  403. def do_OPTIONS(self, environ, read_collections, write_collections,
  404. content, user):
  405. """Manage OPTIONS request."""
  406. headers = {
  407. "Allow": ("DELETE, HEAD, GET, MKCALENDAR, MKCOL, MOVE, "
  408. "OPTIONS, PROPFIND, PROPPATCH, PUT, REPORT"),
  409. "DAV": "1, 2, 3, calendar-access, addressbook, extended-mkcol"}
  410. return client.OK, headers, None
  411. def do_PROPFIND(self, environ, read_collections, write_collections,
  412. content, user):
  413. """Manage PROPFIND request."""
  414. if not read_collections:
  415. return client.NOT_FOUND, {}, None
  416. headers = {
  417. "DAV": "1, 2, 3, calendar-access, addressbook, extended-mkcol",
  418. "Content-Type": "text/xml"}
  419. answer = xmlutils.propfind(
  420. environ["PATH_INFO"], content, read_collections, write_collections,
  421. user)
  422. return client.MULTI_STATUS, headers, answer
  423. def do_PROPPATCH(self, environ, read_collections, write_collections,
  424. content, user):
  425. """Manage PROPPATCH request."""
  426. if not write_collections:
  427. return NOT_ALLOWED
  428. collection = write_collections[0]
  429. answer = xmlutils.proppatch(environ["PATH_INFO"], content, collection)
  430. headers = {
  431. "DAV": "1, 2, 3, calendar-access, addressbook, extended-mkcol",
  432. "Content-Type": "text/xml"}
  433. return client.MULTI_STATUS, headers, answer
  434. def do_PUT(self, environ, read_collections, write_collections, content,
  435. user):
  436. """Manage PUT request."""
  437. if not write_collections:
  438. return NOT_ALLOWED
  439. collection = write_collections[0]
  440. content_type = environ.get("CONTENT_TYPE")
  441. if content_type:
  442. tags = {value: key for key, value in storage.MIMETYPES.items()}
  443. collection.set_meta("tag", tags[content_type.split(";")[0]])
  444. headers = {}
  445. item_name = xmlutils.name_from_path(environ["PATH_INFO"], collection)
  446. item = collection.get(item_name)
  447. etag = environ.get("HTTP_IF_MATCH", "")
  448. match = environ.get("HTTP_IF_NONE_MATCH", "") == "*"
  449. if (not item and not etag) or (
  450. item and ((etag or item.etag) == item.etag) and not match):
  451. # PUT allowed in 3 cases
  452. # Case 1: No item and no ETag precondition: Add new item
  453. # Case 2: Item and ETag precondition verified: Modify item
  454. # Case 3: Item and no Etag precondition: Force modifying item
  455. items = list(vobject.readComponents(content))
  456. if items:
  457. if item:
  458. # PUT is modifying an existing item
  459. new_item = collection.update(item_name, items[0])
  460. elif item_name:
  461. # PUT is adding a new item
  462. new_item = collection.upload(item_name, items[0])
  463. else:
  464. # PUT is replacing the whole collection
  465. collection.delete()
  466. new_item = self.Collection.create_collection(
  467. environ["PATH_INFO"], items)
  468. if new_item:
  469. headers["ETag"] = new_item.etag
  470. status = client.CREATED
  471. else:
  472. # PUT rejected in all other cases
  473. status = client.PRECONDITION_FAILED
  474. return status, headers, None
  475. def do_REPORT(self, environ, read_collections, write_collections, content,
  476. user):
  477. """Manage REPORT request."""
  478. if not read_collections:
  479. return NOT_ALLOWED
  480. collection = read_collections[0]
  481. headers = {"Content-Type": "text/xml"}
  482. answer = xmlutils.report(environ["PATH_INFO"], content, collection)
  483. return client.MULTI_STATUS, headers, answer
  484. # pylint: enable=W0612,W0613,R0201