storage.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2014 Jean-Marc Martins
  3. # Copyright © 2012-2016 Guillaume Ayoub
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Storage backends.
  19. This module loads the storage backend, according to the storage configuration.
  20. Default storage uses one folder per collection and one file per collection
  21. entry.
  22. """
  23. import json
  24. import os
  25. import posixpath
  26. import shutil
  27. import sys
  28. import time
  29. from contextlib import contextmanager
  30. from hashlib import md5
  31. from random import randint
  32. from uuid import uuid4
  33. import vobject
  34. from . import config, log
  35. def _load():
  36. """Load the storage manager chosen in configuration."""
  37. storage_type = config.get("storage", "type")
  38. if storage_type == "multifilesystem":
  39. module = sys.modules[__name__]
  40. else:
  41. __import__(storage_type)
  42. module = sys.modules[storage_type]
  43. sys.modules[__name__].Collection = module.Collection
  44. FOLDER = os.path.expanduser(config.get("storage", "filesystem_folder"))
  45. FILESYSTEM_ENCODING = sys.getfilesystemencoding()
  46. STORAGE_ENCODING = config.get("encoding", "stock")
  47. def serialize(tag, headers=(), items=()):
  48. """Return a text corresponding to given collection ``tag``.
  49. The text may have the given ``headers`` and ``items`` added around the
  50. items if needed (ie. for calendars).
  51. """
  52. items = sorted(items, key=lambda x: x.name)
  53. if tag == "VADDRESSBOOK":
  54. lines = [item.text.strip() for item in items]
  55. else:
  56. lines = ["BEGIN:%s" % tag]
  57. for part in (headers, items):
  58. if part:
  59. lines.append("\r\n".join(item.text.strip() for item in part))
  60. lines.append("END:%s" % tag)
  61. lines.append("")
  62. return "\r\n".join(lines)
  63. def sanitize_path(path):
  64. """Make path absolute with leading slash to prevent access to other data.
  65. Preserve a potential trailing slash.
  66. """
  67. trailing_slash = "/" if path.endswith("/") else ""
  68. path = posixpath.normpath(path)
  69. new_path = "/"
  70. for part in path.split("/"):
  71. if not part or part in (".", ".."):
  72. continue
  73. new_path = posixpath.join(new_path, part)
  74. trailing_slash = "" if new_path.endswith("/") else trailing_slash
  75. return new_path + trailing_slash
  76. def is_safe_filesystem_path_component(path):
  77. """Check if path is a single component of a filesystem path.
  78. Check that the path is safe to join too.
  79. """
  80. return (
  81. path and not os.path.splitdrive(path)[0] and
  82. not os.path.split(path)[0] and path not in (os.curdir, os.pardir))
  83. def path_to_filesystem(path):
  84. """Convert path to a local filesystem path relative to base_folder.
  85. Conversion is done in a secure manner, or raises ``ValueError``.
  86. """
  87. sane_path = sanitize_path(path).strip("/")
  88. safe_path = FOLDER
  89. if not sane_path:
  90. return safe_path
  91. for part in sane_path.split("/"):
  92. if not is_safe_filesystem_path_component(part):
  93. log.LOGGER.debug(
  94. "Can't translate path safely to filesystem: %s", path)
  95. raise ValueError("Unsafe path")
  96. safe_path = os.path.join(safe_path, part)
  97. return safe_path
  98. class Item(object):
  99. """Internal iCal item."""
  100. def __init__(self, text, name=None):
  101. """Initialize object from ``text`` and different ``kwargs``."""
  102. self.component = vobject.readOne(text)
  103. self._name = name
  104. if not self.component.name:
  105. # Header
  106. self._name = next(self.component.lines()).name.lower()
  107. return
  108. # We must synchronize the name in the text and in the object.
  109. # An item must have a name, determined in order by:
  110. #
  111. # - the ``name`` parameter
  112. # - the ``X-RADICALE-NAME`` iCal property (for Events, Todos, Journals)
  113. # - the ``UID`` iCal property (for Events, Todos, Journals)
  114. # - the ``TZID`` iCal property (for Timezones)
  115. if not self._name:
  116. for line in self.component.lines():
  117. if line.name in ("X-RADICALE-NAME", "UID", "TZID"):
  118. self._name = line.value
  119. if line.name == "X-RADICALE-NAME":
  120. break
  121. if self._name:
  122. # Leading and ending brackets that may have been put by Outlook.
  123. # Slashes are mostly unwanted when saving collections on disk.
  124. self._name = self._name.strip("{}").replace("/", "_")
  125. else:
  126. self._name = uuid4().hex
  127. if not hasattr(self.component, "x_radicale_name"):
  128. self.component.add("X-RADICALE-NAME")
  129. self.component.x_radicale_name.value = self._name
  130. def __hash__(self):
  131. return hash(self.text)
  132. def __eq__(self, item):
  133. return isinstance(item, Item) and self.text == item.text
  134. @property
  135. def etag(self):
  136. """Item etag.
  137. Etag is mainly used to know if an item has changed.
  138. """
  139. etag = md5()
  140. etag.update(self.text.encode("utf-8"))
  141. return '"%s"' % etag.hexdigest()
  142. @property
  143. def name(self):
  144. """Item name.
  145. Name is mainly used to give an URL to the item.
  146. """
  147. return self._name
  148. @property
  149. def text(self):
  150. """Item serialized text."""
  151. return self.component.serialize()
  152. class Header(Item):
  153. """Internal header class."""
  154. class Timezone(Item):
  155. """Internal timezone class."""
  156. tag = "VTIMEZONE"
  157. class Component(Item):
  158. """Internal main component of a collection."""
  159. class Event(Component):
  160. """Internal event class."""
  161. tag = "VEVENT"
  162. mimetype = "text/calendar"
  163. class Todo(Component):
  164. """Internal todo class."""
  165. tag = "VTODO" # pylint: disable=W0511
  166. mimetype = "text/calendar"
  167. class Journal(Component):
  168. """Internal journal class."""
  169. tag = "VJOURNAL"
  170. mimetype = "text/calendar"
  171. class Card(Component):
  172. """Internal card class."""
  173. tag = "VCARD"
  174. mimetype = "text/vcard"
  175. class Collection:
  176. """Collection stored in several files per calendar."""
  177. def __init__(self, path, principal=False):
  178. """Initialize the collection.
  179. ``path`` must be the normalized relative path of the collection, using
  180. the slash as the folder delimiter, with no leading nor trailing slash.
  181. """
  182. self.encoding = "utf-8"
  183. # path should already be sanitized
  184. self.path = sanitize_path(path).strip("/")
  185. split_path = self.path.split("/")
  186. if principal and split_path and self.is_node(self.path):
  187. # Already existing principal collection
  188. self.owner = split_path[0]
  189. elif len(split_path) > 1:
  190. # URL with at least one folder
  191. self.owner = split_path[0]
  192. else:
  193. self.owner = None
  194. self.is_principal = principal
  195. self._items = None
  196. @classmethod
  197. def from_path(cls, path, depth="1", include_container=True):
  198. """Return a list of collections and items under the given ``path``.
  199. If ``depth`` is "0", only the actual object under ``path`` is
  200. returned.
  201. If ``depth`` is anything but "0", it is considered as "1" and direct
  202. children are included in the result. If ``include_container`` is
  203. ``True`` (the default), the containing object is included in the
  204. result.
  205. The ``path`` is relative.
  206. """
  207. # path == None means wrong URL
  208. if path is None:
  209. return []
  210. # path should already be sanitized
  211. sane_path = sanitize_path(path).strip("/")
  212. attributes = sane_path.split("/")
  213. if not attributes:
  214. return []
  215. # Try to guess if the path leads to a collection or an item
  216. if cls.is_leaf("/".join(attributes[:-1])):
  217. attributes.pop()
  218. result = []
  219. path = "/".join(attributes)
  220. principal = len(attributes) <= 1
  221. if cls.is_node(path):
  222. if depth == "0":
  223. result.append(cls(path, principal))
  224. else:
  225. if include_container:
  226. result.append(cls(path, principal))
  227. for child in cls.children(path):
  228. result.append(child)
  229. else:
  230. if depth == "0":
  231. result.append(cls(path))
  232. else:
  233. collection = cls(path, principal)
  234. if include_container:
  235. result.append(collection)
  236. result.extend(collection.components)
  237. return result
  238. @property
  239. def _filesystem_path(self):
  240. """Absolute path of the file at local ``path``."""
  241. return path_to_filesystem(self.path)
  242. @property
  243. def _props_path(self):
  244. """Absolute path of the file storing the collection properties."""
  245. return self._filesystem_path + ".props"
  246. def _create_dirs(self):
  247. """Create folder storing the collection if absent."""
  248. if not os.path.exists(self._filesystem_path):
  249. os.makedirs(self._filesystem_path)
  250. def set_mimetype(self, mimetype):
  251. self._create_dirs()
  252. with self.props as props:
  253. if "tag" not in props:
  254. if mimetype == "text/vcard":
  255. props["tag"] = "VADDRESSBOOK"
  256. else:
  257. props["tag"] = "VCALENDAR"
  258. @property
  259. def exists(self):
  260. """``True`` if the collection exists on the storage, else ``False``."""
  261. return self.is_node(self.path) or self.is_leaf(self.path)
  262. @staticmethod
  263. def _parse(text, item_types, name=None):
  264. """Find items with type in ``item_types`` in ``text``.
  265. If ``name`` is given, give this name to new items in ``text``.
  266. Return a dict of items.
  267. """
  268. item_tags = {item_type.tag: item_type for item_type in item_types}
  269. items = {}
  270. root = next(vobject.readComponents(text))
  271. components = (
  272. root.components() if root.name in ("VADDRESSBOOK", "VCALENDAR")
  273. else (root,))
  274. for component in components:
  275. item_name = None if component.name == "VTIMEZONE" else name
  276. item_type = item_tags[component.name]
  277. item = item_type(component.serialize(), item_name)
  278. if item.name in items:
  279. text = "\r\n".join((item.text, items[item.name].text))
  280. items[item.name] = item_type(text, item.name)
  281. else:
  282. items[item.name] = item
  283. return items
  284. def save(self, text):
  285. self._create_dirs()
  286. item_types = (Timezone, Event, Todo, Journal, Card)
  287. for name, component in self._parse(text, item_types).items():
  288. if not is_safe_filesystem_path_component(name):
  289. log.LOGGER.debug(
  290. "Can't tranlate name safely to filesystem, "
  291. "skipping component: %s", name)
  292. continue
  293. filename = os.path.join(self._filesystem_path, name)
  294. with open(filename, "w", encoding=STORAGE_ENCODING) as fd:
  295. fd.write(component.text)
  296. @property
  297. def headers(self):
  298. return (
  299. Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
  300. Header("VERSION:%s" % self.version))
  301. def delete(self):
  302. shutil.rmtree(self._filesystem_path)
  303. os.remove(self._props_path)
  304. def remove(self, name):
  305. if not is_safe_filesystem_path_component(name):
  306. log.LOGGER.debug(
  307. "Can't tranlate name safely to filesystem, "
  308. "skipping component: %s", name)
  309. return
  310. if name in self.items:
  311. del self.items[name]
  312. filesystem_path = os.path.join(self._filesystem_path, name)
  313. if os.path.exists(filesystem_path):
  314. os.remove(filesystem_path)
  315. @property
  316. def text(self):
  317. components = (Timezone, Event, Todo, Journal, Card)
  318. items = {}
  319. try:
  320. filenames = os.listdir(self._filesystem_path)
  321. except (OSError, IOError) as e:
  322. log.LOGGER.info(
  323. "Error while reading collection %r: %r" % (
  324. self._filesystem_path, e))
  325. return ""
  326. for filename in filenames:
  327. path = os.path.join(self._filesystem_path, filename)
  328. try:
  329. with open(path, encoding=STORAGE_ENCODING) as fd:
  330. items.update(self._parse(fd.read(), components))
  331. except (OSError, IOError) as e:
  332. log.LOGGER.warning(
  333. "Error while reading item %r: %r" % (path, e))
  334. return serialize(
  335. self.tag, self.headers, sorted(items.values(), key=lambda x: x.name))
  336. @classmethod
  337. def children(cls, path):
  338. filesystem_path = path_to_filesystem(path)
  339. _, directories, files = next(os.walk(filesystem_path))
  340. for path in directories + files:
  341. # Check that the local path can be translated into an internal path
  342. if not path or posixpath.split(path)[0] or path in (".", ".."):
  343. log.LOGGER.debug("Skipping unsupported filename: %s", path)
  344. continue
  345. relative_path = posixpath.join(path, path)
  346. if cls.is_node(relative_path) or cls.is_leaf(relative_path):
  347. yield cls(relative_path)
  348. @classmethod
  349. def is_node(cls, path):
  350. filesystem_path = path_to_filesystem(path)
  351. return (
  352. os.path.isdir(filesystem_path) and
  353. not os.path.exists(filesystem_path + ".props"))
  354. @classmethod
  355. def is_leaf(cls, path):
  356. filesystem_path = path_to_filesystem(path)
  357. return (
  358. os.path.isdir(filesystem_path) and
  359. os.path.exists(filesystem_path + ".props"))
  360. @property
  361. def last_modified(self):
  362. last = max([
  363. os.path.getmtime(os.path.join(self._filesystem_path, filename))
  364. for filename in os.listdir(self._filesystem_path)] or [0])
  365. return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(last))
  366. @property
  367. @contextmanager
  368. def props(self):
  369. # On enter
  370. properties = {}
  371. if os.path.exists(self._props_path):
  372. with open(self._props_path) as prop_file:
  373. properties.update(json.load(prop_file))
  374. old_properties = properties.copy()
  375. yield properties
  376. # On exit
  377. if old_properties != properties:
  378. with open(self._props_path, "w") as prop_file:
  379. json.dump(properties, prop_file)
  380. def append(self, name, text):
  381. """Append items from ``text`` to collection.
  382. If ``name`` is given, give this name to new items in ``text``.
  383. """
  384. new_items = self._parse(
  385. text, (Timezone, Event, Todo, Journal, Card), name)
  386. for new_item in new_items.values():
  387. if new_item.name not in self.items:
  388. self.items[new_item.name] = new_item
  389. self.write()
  390. def replace(self, name, text):
  391. """Replace content by ``text`` in collection objet called ``name``."""
  392. self.remove(name)
  393. self.append(name, text)
  394. def write(self):
  395. """Write collection with given parameters."""
  396. text = serialize(self.tag, self.headers, self.items.values())
  397. self.save(text)
  398. @property
  399. def tag(self):
  400. """Type of the collection."""
  401. with self.props as props:
  402. if "tag" not in props:
  403. try:
  404. tag = open(self.path).readlines()[0][6:].rstrip()
  405. except IOError:
  406. if self.path.endswith((".vcf", "/carddav")):
  407. props["tag"] = "VADDRESSBOOK"
  408. else:
  409. props["tag"] = "VCALENDAR"
  410. else:
  411. if tag in ("VADDRESSBOOK", "VCARD"):
  412. props["tag"] = "VADDRESSBOOK"
  413. else:
  414. props["tag"] = "VCALENDAR"
  415. return props["tag"]
  416. @property
  417. def mimetype(self):
  418. """Mimetype of the collection."""
  419. if self.tag == "VADDRESSBOOK":
  420. return "text/vcard"
  421. elif self.tag == "VCALENDAR":
  422. return "text/calendar"
  423. @property
  424. def resource_type(self):
  425. """Resource type of the collection."""
  426. if self.tag == "VADDRESSBOOK":
  427. return "addressbook"
  428. elif self.tag == "VCALENDAR":
  429. return "calendar"
  430. @property
  431. def etag(self):
  432. """Etag from collection."""
  433. etag = md5()
  434. etag.update(self.text.encode("utf-8"))
  435. return '"%s"' % etag.hexdigest()
  436. @property
  437. def name(self):
  438. """Collection name."""
  439. with self.props as props:
  440. return props.get("D:displayname", self.path.split(os.path.sep)[-1])
  441. @property
  442. def color(self):
  443. """Collection color."""
  444. with self.props as props:
  445. if "ICAL:calendar-color" not in props:
  446. props["ICAL:calendar-color"] = "#%x" % randint(0, 255 ** 3 - 1)
  447. return props["ICAL:calendar-color"]
  448. @property
  449. def items(self):
  450. """Get list of all items in collection."""
  451. if self._items is None:
  452. self._items = self._parse(
  453. self.text, (Event, Todo, Journal, Card, Timezone))
  454. return self._items
  455. @property
  456. def timezones(self):
  457. """Get list of all timezones in collection."""
  458. return [
  459. item for item in self.items.values() if item.tag == Timezone.tag]
  460. @property
  461. def components(self):
  462. """Get list of all components in collection."""
  463. tags = [item_type.tag for item_type in (Event, Todo, Journal, Card)]
  464. return [item for item in self.items.values() if item.tag in tags]
  465. @property
  466. def owner_url(self):
  467. """Get the collection URL according to its owner."""
  468. return "/%s/" % self.owner if self.owner else None
  469. @property
  470. def url(self):
  471. """Get the standard collection URL."""
  472. return "%s/" % self.path
  473. @property
  474. def version(self):
  475. """Get the version of the collection type."""
  476. return "3.0" if self.tag == "VADDRESSBOOK" else "2.0"