This commit is contained in:
Guillaume Ayoub 2015-12-31 12:49:41 +01:00
parent 95fe2b6824
commit e7ce00d54f
6 changed files with 53 additions and 36 deletions

View File

@ -102,11 +102,12 @@ def run():
# Check and create PID file in a race-free manner # Check and create PID file in a race-free manner
if config.get("server", "pid"): if config.get("server", "pid"):
try: try:
pid_fd = os.open(config.get("server", "pid"), pid_fd = os.open(
os.O_CREAT | os.O_EXCL | os.O_WRONLY) config.get("server", "pid"),
os.O_CREAT | os.O_EXCL | os.O_WRONLY)
except: except:
raise OSError("PID file exists: %s" % raise OSError(
config.get("server", "pid")) "PID file exists: %s" % config.get("server", "pid"))
pid = os.fork() pid = os.fork()
if pid: if pid:
sys.exit() sys.exit()

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# This file is part of Radicale Server - Calendar Server # This file is part of Radicale Server - Calendar Server
# Copyright © 2008-2013 Guillaume Ayoub # Copyright © 2008-2015 Guillaume Ayoub
# Copyright © 2008 Nicolas Kandel # Copyright © 2008 Nicolas Kandel
# Copyright © 2008 Pascal Halter # Copyright © 2008 Pascal Halter
# #

View File

@ -16,7 +16,7 @@
# along with Radicale. If not, see <http://www.gnu.org/licenses/>. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
""" """
Helper functions for working with paths Helper functions for working with paths.
""" """
@ -27,8 +27,11 @@ from . import log
def sanitize_path(path): def sanitize_path(path):
"""Make absolute (with leading slash) to prevent access to other data. """Make path absolute with leading slash to prevent access to other data.
Preserves an potential trailing slash."""
Preserve a potential trailing slash.
"""
trailing_slash = "/" if path.endswith("/") else "" trailing_slash = "/" if path.endswith("/") else ""
path = posixpath.normpath(path) path = posixpath.normpath(path)
new_path = "/" new_path = "/"
@ -41,7 +44,11 @@ def sanitize_path(path):
def is_safe_path_component(path): def is_safe_path_component(path):
"""Checks if path is a single component of a path and is safe to join""" """Check if path is a single component of a POSIX path.
Check that the path is safe to join too.
"""
if not path: if not path:
return False return False
head, _ = posixpath.split(path) head, _ = posixpath.split(path)
@ -53,8 +60,11 @@ def is_safe_path_component(path):
def is_safe_filesystem_path_component(path): def is_safe_filesystem_path_component(path):
"""Checks if path is a single component of a local filesystem path """Check if path is a single component of a filesystem path.
and is safe to join"""
Check that the path is safe to join too.
"""
if not path: if not path:
return False return False
drive, _ = os.path.splitdrive(path) drive, _ = os.path.splitdrive(path)
@ -69,16 +79,19 @@ def is_safe_filesystem_path_component(path):
def path_to_filesystem(path, base_folder): def path_to_filesystem(path, base_folder):
"""Converts path to a local filesystem path relative to base_folder """Convert path to a local filesystem path relative to base_folder.
in a secure manner or raises ValueError."""
Conversion is done in a secure manner, or raises ValueError.
"""
sane_path = sanitize_path(path).strip("/") sane_path = sanitize_path(path).strip("/")
safe_path = base_folder safe_path = base_folder
if not sane_path: if not sane_path:
return safe_path return safe_path
for part in sane_path.split("/"): for part in sane_path.split("/"):
if not is_safe_filesystem_path_component(part): if not is_safe_filesystem_path_component(part):
log.LOGGER.debug("Can't translate path safely to filesystem: %s", log.LOGGER.debug(
path) "Can't translate path safely to filesystem: %s", path)
raise ValueError("Unsafe path") raise ValueError("Unsafe path")
safe_path = os.path.join(safe_path, part) safe_path = os.path.join(safe_path, part)
return safe_path return safe_path

View File

@ -23,6 +23,7 @@ This module loads the rights backend, according to the rights
configuration. configuration.
""" """
import sys import sys
from .. import config from .. import config
@ -43,7 +44,9 @@ def load():
def authorized(user, collection, right): def authorized(user, collection, right):
""" Check when user has rights on collection """Check that an user has rights on a collection.
This method is overriden when appropriate rights backend loaded.
This method is overriden when the appropriate rights backend is loaded.
""" """
raise NotImplementedError() raise NotImplementedError()

View File

@ -103,8 +103,7 @@ class Collection(ical.Collection):
# make sure that the local filename can be translated # make sure that the local filename can be translated
# into an internal path # into an internal path
if not pathutils.is_safe_path_component(filename): if not pathutils.is_safe_path_component(filename):
log.LOGGER.debug("Skipping unsupported filename: %s", log.LOGGER.debug("Skipping unsupported filename: %s", filename)
filename)
continue continue
rel_filename = posixpath.join(path, filename) rel_filename = posixpath.join(path, filename)
if cls.is_node(rel_filename) or cls.is_leaf(rel_filename): if cls.is_node(rel_filename) or cls.is_leaf(rel_filename):
@ -118,13 +117,14 @@ class Collection(ical.Collection):
@classmethod @classmethod
def is_leaf(cls, path): def is_leaf(cls, path):
filesystem_path = pathutils.path_to_filesystem(path, FOLDER) filesystem_path = pathutils.path_to_filesystem(path, FOLDER)
return (os.path.isfile(filesystem_path) and not return (
filesystem_path.endswith(".props")) os.path.isfile(filesystem_path) and not
filesystem_path.endswith(".props"))
@property @property
def last_modified(self): def last_modified(self):
modification_time = \ modification_time = time.gmtime(
time.gmtime(os.path.getmtime(self._filesystem_path)) os.path.getmtime(self._filesystem_path))
return time.strftime("%a, %d %b %Y %H:%M:%S +0000", modification_time) return time.strftime("%a, %d %b %Y %H:%M:%S +0000", modification_time)
@property @property

View File

@ -84,8 +84,9 @@ class Collection(filesystem.Collection):
try: try:
filenames = os.listdir(self._filesystem_path) filenames = os.listdir(self._filesystem_path)
except (OSError, IOError) as e: except (OSError, IOError) as e:
log.LOGGER.info('Error while reading collection %r: %r' log.LOGGER.info(
% (self._filesystem_path, e)) 'Error while reading collection %r: %r' % (
self._filesystem_path, e))
return "" return ""
for filename in filenames: for filename in filenames:
@ -94,25 +95,24 @@ class Collection(filesystem.Collection):
with filesystem.open(path) as fd: with filesystem.open(path) as fd:
items.update(self._parse(fd.read(), components)) items.update(self._parse(fd.read(), components))
except (OSError, IOError) as e: except (OSError, IOError) as e:
log.LOGGER.warning('Error while reading item %r: %r' log.LOGGER.warning(
% (path, e)) 'Error while reading item %r: %r' % (path, e))
return ical.serialize( return ical.serialize(
self.tag, self.headers, sorted(items, key=lambda x: x.name)) self.tag, self.headers, sorted(items, key=lambda x: x.name))
@classmethod @classmethod
def is_node(cls, path): def is_node(cls, path):
filesystem_path = pathutils.path_to_filesystem(path, filesystem_path = pathutils.path_to_filesystem(path, filesystem.FOLDER)
filesystem.FOLDER) return (
return (os.path.isdir(filesystem_path) and os.path.isdir(filesystem_path) and
not os.path.exists(filesystem_path + ".props")) not os.path.exists(filesystem_path + ".props"))
@classmethod @classmethod
def is_leaf(cls, path): def is_leaf(cls, path):
filesystem_path = pathutils.path_to_filesystem(path, filesystem_path = pathutils.path_to_filesystem(path, filesystem.FOLDER)
filesystem.FOLDER) return (
return (os.path.isdir(filesystem_path) and os.path.isdir(filesystem_path) and os.path.exists(path + ".props"))
os.path.exists(path + ".props"))
@property @property
def last_modified(self): def last_modified(self):