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
if config.get("server", "pid"):
try:
pid_fd = os.open(config.get("server", "pid"),
os.O_CREAT | os.O_EXCL | os.O_WRONLY)
pid_fd = os.open(
config.get("server", "pid"),
os.O_CREAT | os.O_EXCL | os.O_WRONLY)
except:
raise OSError("PID file exists: %s" %
config.get("server", "pid"))
raise OSError(
"PID file exists: %s" % config.get("server", "pid"))
pid = os.fork()
if pid:
sys.exit()

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# 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 Pascal Halter
#

View File

@ -16,7 +16,7 @@
# 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):
"""Make absolute (with leading slash) to prevent access to other data.
Preserves an potential trailing slash."""
"""Make path absolute with leading slash to prevent access to other data.
Preserve a potential trailing slash.
"""
trailing_slash = "/" if path.endswith("/") else ""
path = posixpath.normpath(path)
new_path = "/"
@ -41,7 +44,11 @@ def sanitize_path(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:
return False
head, _ = posixpath.split(path)
@ -53,8 +60,11 @@ def is_safe_path_component(path):
def is_safe_filesystem_path_component(path):
"""Checks if path is a single component of a local filesystem path
and is safe to join"""
"""Check if path is a single component of a filesystem path.
Check that the path is safe to join too.
"""
if not path:
return False
drive, _ = os.path.splitdrive(path)
@ -69,16 +79,19 @@ def is_safe_filesystem_path_component(path):
def path_to_filesystem(path, base_folder):
"""Converts path to a local filesystem path relative to base_folder
in a secure manner or raises ValueError."""
"""Convert path to a local filesystem path relative to base_folder.
Conversion is done in a secure manner, or raises ValueError.
"""
sane_path = sanitize_path(path).strip("/")
safe_path = base_folder
if not sane_path:
return safe_path
for part in sane_path.split("/"):
if not is_safe_filesystem_path_component(part):
log.LOGGER.debug("Can't translate path safely to filesystem: %s",
path)
log.LOGGER.debug(
"Can't translate path safely to filesystem: %s", path)
raise ValueError("Unsafe path")
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.
"""
import sys
from .. import config
@ -43,7 +44,9 @@ def load():
def authorized(user, collection, right):
""" Check when user has rights on collection
This method is overriden when appropriate rights backend loaded.
"""Check that an user has rights on a collection.
This method is overriden when the appropriate rights backend is loaded.
"""
raise NotImplementedError()

View File

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

View File

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