__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008-2011 Guillaume Ayoub
  5. # Copyright © 2008 Nicolas Kandel
  6. # Copyright © 2008 Pascal Halter
  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. """
  21. Radicale Server module.
  22. This module offers a WSGI application class.
  23. To use this module, you should take a look at the file ``radicale.py`` that
  24. should have been included in this package.
  25. """
  26. import os
  27. import posixpath
  28. import pprint
  29. import base64
  30. import socket
  31. import ssl
  32. import wsgiref.simple_server
  33. # Manage Python2/3 different modules
  34. # pylint: disable=F0401
  35. try:
  36. from http import client, server
  37. except ImportError:
  38. import httplib as client
  39. import BaseHTTPServer as server
  40. # pylint: enable=F0401
  41. from radicale import acl, config, ical, log, xmlutils
  42. VERSION = "git"
  43. class HTTPServer(wsgiref.simple_server.WSGIServer, object):
  44. """HTTP server."""
  45. def __init__(self, address, handler, bind_and_activate=True):
  46. """Create server."""
  47. ipv6 = ":" in address[0]
  48. if ipv6:
  49. self.address_family = socket.AF_INET6
  50. # Do not bind and activate, as we might change socket options
  51. super(HTTPServer, self).__init__(address, handler, False)
  52. if ipv6:
  53. # Only allow IPv6 connections to the IPv6 socket
  54. self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
  55. if bind_and_activate:
  56. self.server_bind()
  57. self.server_activate()
  58. class HTTPSServer(HTTPServer):
  59. """HTTPS server."""
  60. def __init__(self, address, handler):
  61. """Create server by wrapping HTTP socket in an SSL socket."""
  62. super(HTTPSServer, self).__init__(address, handler, False)
  63. self.socket = ssl.wrap_socket(
  64. self.socket,
  65. server_side=True,
  66. certfile=config.get("server", "certificate"),
  67. keyfile=config.get("server", "key"),
  68. ssl_version=ssl.PROTOCOL_SSLv23)
  69. self.server_bind()
  70. self.server_activate()
  71. class RequestHandler(wsgiref.simple_server.WSGIRequestHandler):
  72. """HTTP requests handler."""
  73. def log_message(self, *args, **kwargs):
  74. """Disable inner logging management."""
  75. class Application(object):
  76. """WSGI application managing calendars."""
  77. def __init__(self):
  78. """Initialize application."""
  79. super(Application, self).__init__()
  80. self.acl = acl.load()
  81. self.encoding = config.get("encoding", "request")
  82. if config.getboolean('logging', 'full_environment'):
  83. self.headers_log = lambda environ: environ
  84. def headers_log(self, environ):
  85. request_environ = dict(environ)
  86. for shell_variable in os.environ:
  87. #if shell_variable not in request_environ:
  88. # continue
  89. del request_environ[shell_variable]
  90. return request_environ
  91. def decode(self, text, environ):
  92. """Try to magically decode ``text`` according to given ``environ``."""
  93. # List of charsets to try
  94. charsets = []
  95. # First append content charset given in the request
  96. content_type = environ.get("CONTENT_TYPE")
  97. if content_type and "charset=" in content_type:
  98. charsets.append(content_type.split("charset=")[1].strip())
  99. # Then append default Radicale charset
  100. charsets.append(self.encoding)
  101. # Then append various fallbacks
  102. charsets.append("utf-8")
  103. charsets.append("iso8859-1")
  104. # Try to decode
  105. for charset in charsets:
  106. try:
  107. return text.decode(charset)
  108. except UnicodeDecodeError:
  109. pass
  110. raise UnicodeDecodeError
  111. def __call__(self, environ, start_response):
  112. """Manage a request."""
  113. log.LOGGER.info("%s request at %s received" % (
  114. environ["REQUEST_METHOD"], environ["PATH_INFO"]))
  115. headers = pprint.pformat(self.headers_log(environ))
  116. log.LOGGER.debug("Request headers:\n%s" % headers)
  117. # Get content
  118. content_length = int(environ.get("CONTENT_LENGTH") or 0)
  119. if content_length:
  120. content = self.decode(
  121. environ["wsgi.input"].read(content_length), environ)
  122. log.LOGGER.debug("Request content:\n%s" % content)
  123. else:
  124. content = None
  125. # Find calendar
  126. attributes = posixpath.normpath(
  127. environ["PATH_INFO"].strip("/")).split("/")
  128. if attributes:
  129. if attributes[-1].endswith(".ics"):
  130. attributes.pop()
  131. path = "/".join(attributes[:min(len(attributes), 2)])
  132. calendar = ical.Calendar(path)
  133. else:
  134. calendar = None
  135. # Get function corresponding to method
  136. function = getattr(self, environ["REQUEST_METHOD"].lower())
  137. # Check rights
  138. if not calendar or not self.acl:
  139. # No calendar or no acl, don't check rights
  140. status, headers, answer = function(environ, calendar, content)
  141. else:
  142. # Ask authentication backend to check rights
  143. log.LOGGER.info(
  144. "Checking rights for calendar owned by %s" % calendar.owner)
  145. authorization = environ.get("HTTP_AUTHORIZATION", None)
  146. if authorization:
  147. auth = authorization.lstrip("Basic").strip().encode("ascii")
  148. user, password = self.decode(
  149. base64.b64decode(auth), environ).split(":")
  150. else:
  151. user = password = None
  152. if self.acl.has_right(calendar.owner, user, password):
  153. log.LOGGER.info("%s allowed" % (user or "anonymous user"))
  154. status, headers, answer = function(environ, calendar, content)
  155. else:
  156. log.LOGGER.info("%s refused" % (user or "anonymous user"))
  157. status = client.UNAUTHORIZED
  158. headers = {
  159. "WWW-Authenticate":
  160. "Basic realm=\"Radicale Server - Password Required\""}
  161. answer = None
  162. # Set content length
  163. if answer:
  164. log.LOGGER.debug(
  165. "Response content:\n%s" % self.decode(answer, environ))
  166. headers["Content-Length"] = str(len(answer))
  167. # Start response
  168. status = "%i %s" % (status, client.responses.get(status, ""))
  169. start_response(status, list(headers.items()))
  170. # Return response content
  171. return [answer] if answer else []
  172. # All these functions must have the same parameters, some are useless
  173. # pylint: disable=W0612,W0613,R0201
  174. def get(self, environ, calendar, content):
  175. """Manage GET request."""
  176. item_name = xmlutils.name_from_path(environ["PATH_INFO"], calendar)
  177. if item_name:
  178. # Get calendar item
  179. item = calendar.get_item(item_name)
  180. if item:
  181. items = calendar.timezones
  182. items.append(item)
  183. answer_text = ical.serialize(
  184. headers=calendar.headers, items=items)
  185. etag = item.etag
  186. else:
  187. return client.GONE, {}, None
  188. else:
  189. # Get whole calendar
  190. answer_text = calendar.text
  191. etag = calendar.etag
  192. headers = {
  193. "Content-Type": "text/calendar",
  194. "Last-Modified": calendar.last_modified,
  195. "ETag": etag}
  196. answer = answer_text.encode(self.encoding)
  197. return client.OK, headers, answer
  198. def head(self, environ, calendar, content):
  199. """Manage HEAD request."""
  200. status, headers, answer = self.get(environ, calendar, content)
  201. return status, headers, None
  202. def delete(self, environ, calendar, content):
  203. """Manage DELETE request."""
  204. item = calendar.get_item(
  205. xmlutils.name_from_path(environ["PATH_INFO"], calendar))
  206. if item and environ.get("HTTP_IF_MATCH", item.etag) == item.etag:
  207. # No ETag precondition or precondition verified, delete item
  208. answer = xmlutils.delete(environ["PATH_INFO"], calendar)
  209. status = client.NO_CONTENT
  210. else:
  211. # No item or ETag precondition not verified, do not delete item
  212. answer = None
  213. status = client.PRECONDITION_FAILED
  214. return status, {}, answer
  215. def mkcalendar(self, environ, calendar, content):
  216. """Manage MKCALENDAR request."""
  217. return client.CREATED, {}, None
  218. def options(self, environ, calendar, content):
  219. """Manage OPTIONS request."""
  220. headers = {
  221. "Allow": "DELETE, HEAD, GET, MKCALENDAR, " \
  222. "OPTIONS, PROPFIND, PROPPATCH, PUT, REPORT",
  223. "DAV": "1, calendar-access"}
  224. return client.OK, headers, None
  225. def propfind(self, environ, calendar, content):
  226. """Manage PROPFIND request."""
  227. headers = {
  228. "DAV": "1, calendar-access",
  229. "Content-Type": "text/xml"}
  230. answer = xmlutils.propfind(
  231. environ["PATH_INFO"], content, calendar,
  232. environ.get("HTTP_DEPTH", "infinity"))
  233. return client.MULTI_STATUS, headers, answer
  234. def proppatch(self, environ, calendar, content):
  235. """Manage PROPPATCH request."""
  236. xmlutils.proppatch(environ["PATH_INFO"], content, calendar)
  237. headers = {
  238. "DAV": "1, calendar-access",
  239. "Content-Type": "text/xml"}
  240. return client.MULTI_STATUS, headers, None
  241. def put(self, environ, calendar, content):
  242. """Manage PUT request."""
  243. headers = {}
  244. item_name = xmlutils.name_from_path(environ["PATH_INFO"], calendar)
  245. item = calendar.get_item(item_name)
  246. if (not item and not environ.get("HTTP_IF_MATCH")) or (
  247. item and environ.get("HTTP_IF_MATCH", item.etag) == item.etag):
  248. # PUT allowed in 3 cases
  249. # Case 1: No item and no ETag precondition: Add new item
  250. # Case 2: Item and ETag precondition verified: Modify item
  251. # Case 3: Item and no Etag precondition: Force modifying item
  252. xmlutils.put(environ["PATH_INFO"], content, calendar)
  253. status = client.CREATED
  254. headers["ETag"] = calendar.get_item(item_name).etag
  255. else:
  256. # PUT rejected in all other cases
  257. status = client.PRECONDITION_FAILED
  258. return status, headers, None
  259. def report(self, environ, calendar, content):
  260. """Manage REPORT request."""
  261. headers = {'Content-Type': 'text/xml'}
  262. answer = xmlutils.report(environ["PATH_INFO"], content, calendar)
  263. return client.MULTI_STATUS, headers, answer
  264. # pylint: enable=W0612,W0613,R0201