2016-03-23 07:41:18 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <cassert>
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
#include <Poco/Path.h>
|
2016-03-26 06:50:13 -05:00
|
|
|
#include <Poco/SHA1Engine.h>
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
#include "LOOLWSD.hpp"
|
2016-03-23 07:41:18 -05:00
|
|
|
#include "DocumentBroker.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
#include "Storage.hpp"
|
|
|
|
#include "TileCache.hpp"
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Returns the cache path for a given document URI.
|
|
|
|
std::string getCachePath(const std::string& uri)
|
|
|
|
{
|
|
|
|
Poco::SHA1Engine digestEngine;
|
|
|
|
|
|
|
|
digestEngine.update(uri.c_str(), uri.size());
|
|
|
|
|
|
|
|
return (LOOLWSD::Cache + "/" +
|
|
|
|
Poco::DigestEngine::digestToHex(digestEngine.digest()).insert(3, "/").insert(2, "/").insert(1, "/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
Poco::URI DocumentBroker::sanitizeURI(std::string uri)
|
|
|
|
{
|
|
|
|
// The URI of the document should be url-encoded.
|
|
|
|
std::string decodedUri;
|
|
|
|
Poco::URI::decode(uri, decodedUri);
|
|
|
|
auto uriPublic = Poco::URI(decodedUri);
|
|
|
|
|
|
|
|
if (uriPublic.isRelative() || uriPublic.getScheme() == "file")
|
|
|
|
{
|
|
|
|
// TODO: Validate and limit access to local paths!
|
|
|
|
uriPublic.normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uriPublic.getPath().empty())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid URI.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return uriPublic;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DocumentBroker::getDocKey(const Poco::URI& uri)
|
|
|
|
{
|
|
|
|
// Keep the host as part of the key to close a potential security hole.
|
|
|
|
std::string docKey;
|
|
|
|
Poco::URI::encode(uri.getHost() + uri.getPath(), "", docKey);
|
|
|
|
return docKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentBroker::DocumentBroker(const Poco::URI& uriPublic,
|
|
|
|
const std::string& docKey,
|
|
|
|
const std::string& childRoot) :
|
|
|
|
_uriPublic(uriPublic),
|
|
|
|
_docKey(docKey),
|
|
|
|
_childRoot(childRoot),
|
2016-03-26 06:50:13 -05:00
|
|
|
_cacheRoot(getCachePath(uriPublic.toString())),
|
2016-03-23 07:41:18 -05:00
|
|
|
_sessionsCount(0)
|
|
|
|
{
|
|
|
|
assert(!_docKey.empty());
|
|
|
|
assert(!_childRoot.empty());
|
|
|
|
Log::info("DocumentBroker [" + _uriPublic.toString() + "] created. DocKey: [" + _docKey + "]");
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentBroker::validate(const Poco::URI& uri)
|
|
|
|
{
|
|
|
|
Log::info("Validating: " + uri.toString());
|
|
|
|
auto storage = createStorage("", "", uri);
|
|
|
|
storage->getFileInfo(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentBroker::load(const std::string& jailId)
|
|
|
|
{
|
|
|
|
Log::debug("Loading from URI: " + _uriPublic.toString());
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
if (_storage)
|
|
|
|
{
|
|
|
|
// Already loaded. Nothing to do.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_jailId = jailId;
|
|
|
|
|
|
|
|
// The URL is the publicly visible one, not visible in the chroot jail.
|
|
|
|
// We need to map it to a jailed path and copy the file there.
|
|
|
|
|
|
|
|
// user/doc/jailId
|
2016-03-28 06:01:19 -05:00
|
|
|
const auto jailPath = Poco::Path(JAILED_DOCUMENT_ROOT, jailId);
|
2016-03-23 07:41:18 -05:00
|
|
|
const std::string jailRoot = getJailRoot();
|
|
|
|
|
|
|
|
Log::info("jailPath: " + jailPath.toString() + ", jailRoot: " + jailRoot);
|
|
|
|
|
2016-03-26 09:42:15 -05:00
|
|
|
auto storage = createStorage("", "", _uriPublic);
|
|
|
|
const auto fileInfo = storage->getFileInfo(_uriPublic);
|
|
|
|
_tileCache.reset(new TileCache(_uriPublic.toString(), fileInfo.ModifiedTime, _cacheRoot));
|
2016-03-25 21:56:18 -05:00
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
_storage = createStorage(jailRoot, jailPath.toString(), _uriPublic);
|
|
|
|
|
|
|
|
const auto localPath = _storage->loadStorageFileToLocal();
|
|
|
|
_uriJailed = Poco::URI(Poco::URI("file://"), localPath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentBroker::save()
|
|
|
|
{
|
|
|
|
Log::debug("Saving to URI: " + _uriPublic.toString());
|
|
|
|
|
2016-03-26 08:10:53 -05:00
|
|
|
assert(_storage && _tileCache);
|
|
|
|
if (_storage->saveLocalFileToStorage())
|
|
|
|
{
|
|
|
|
_tileCache->documentSaved();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DocumentBroker::getJailRoot() const
|
|
|
|
{
|
|
|
|
assert(!_jailId.empty());
|
|
|
|
return Poco::Path(_childRoot, _jailId).toString();
|
|
|
|
}
|
|
|
|
|
2016-03-23 11:55:28 -05:00
|
|
|
void DocumentBroker::takeEditLock(const std::string id)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> sessionsLock(_wsSessionsMutex);
|
|
|
|
for (auto& it: _wsSessions)
|
|
|
|
{
|
|
|
|
if (it.first != id)
|
|
|
|
{
|
|
|
|
it.second->setEditLock(false);
|
|
|
|
it.second->sendTextFrame("editlock 0");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it.second->setEditLock(true);
|
|
|
|
it.second->sendTextFrame("editlock 1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentBroker::addWSSession(const std::string id, std::shared_ptr<MasterProcessSession>& ws)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> sessionsLock(_wsSessionsMutex);
|
|
|
|
auto ret = _wsSessions.emplace(id, ws);
|
|
|
|
if (!ret.second)
|
|
|
|
{
|
|
|
|
Log::warn("DocumentBroker: Trying to add already existed session.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DocumentBroker::removeWSSession(const std::string id)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> sessionsLock(_wsSessionsMutex);
|
|
|
|
bool bEditLock = false;
|
|
|
|
auto it = _wsSessions.find(id);
|
|
|
|
if (it != _wsSessions.end())
|
|
|
|
{
|
|
|
|
if (it->second->isEditLocked())
|
|
|
|
bEditLock = true;
|
|
|
|
|
|
|
|
_wsSessions.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bEditLock)
|
|
|
|
{
|
|
|
|
// pass the edit lock to first session in map
|
|
|
|
it = _wsSessions.begin();
|
|
|
|
if (it != _wsSessions.end())
|
|
|
|
{
|
|
|
|
it->second->setEditLock(true);
|
|
|
|
it->second->sendTextFrame("editlock 1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|