storage.py 19 KB

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