2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-17 18:56:15 -05:00
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_LOOLWSD_HPP
|
|
|
|
#define INCLUDED_LOOLWSD_HPP
|
|
|
|
|
2015-05-22 08:34:21 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <string>
|
2015-07-13 09:13:06 -05:00
|
|
|
#include <mutex>
|
2015-12-27 21:47:39 -06:00
|
|
|
#include <atomic>
|
2015-03-17 18:56:15 -05:00
|
|
|
|
|
|
|
#include <Poco/Util/OptionSet.h>
|
2015-07-13 09:13:06 -05:00
|
|
|
#include <Poco/Random.h>
|
|
|
|
#include <Poco/Path.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Util/ServerApplication.h>
|
2015-07-17 13:02:25 -05:00
|
|
|
#include <Poco/SharedMemory.h>
|
2015-07-18 11:35:16 -05:00
|
|
|
#include <Poco/NamedMutex.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2016-01-06 23:16:47 -06:00
|
|
|
#include "Common.hpp"
|
2015-12-27 21:47:39 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
2016-01-06 23:16:47 -06:00
|
|
|
// A Document as mananged by us.
|
|
|
|
// Contains URI, physical path, etc.
|
|
|
|
class Document
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
static
|
|
|
|
std::shared_ptr<Document> create(const std::string& url,
|
|
|
|
const std::string& jailRoot,
|
|
|
|
const std::string& childId)
|
|
|
|
{
|
|
|
|
// TODO: Sanitize the url and limit access!
|
|
|
|
auto uriPublic = Poco::URI(url);
|
|
|
|
uriPublic.normalize();
|
|
|
|
|
|
|
|
const auto publicFilePath = uriPublic.getPath();
|
|
|
|
|
|
|
|
if (publicFilePath.empty())
|
|
|
|
throw std::runtime_error("Invalid URL.");
|
|
|
|
|
|
|
|
// This lock could become a bottleneck.
|
|
|
|
// In that case, we can use a pool and index by publicPath.
|
|
|
|
std::unique_lock<std::mutex> lock(DocumentsMutex);
|
|
|
|
|
|
|
|
// Find the document if already open.
|
2016-01-09 11:02:11 -06:00
|
|
|
auto it = UriToDocumentMap.lower_bound(publicFilePath);
|
|
|
|
if (it != UriToDocumentMap.end() && it->first == publicFilePath)
|
2016-01-06 23:16:47 -06:00
|
|
|
{
|
|
|
|
Log::info("Document [" + it->first + "] found.");
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
auto uriJailed = uriPublic;
|
|
|
|
if (uriPublic.isRelative() || uriPublic.getScheme() == "file")
|
|
|
|
{
|
|
|
|
// chroot/jailId/user/doc
|
|
|
|
const auto jailedDocRoot = Poco::Path(jailRoot, JailedDocumentRoot);
|
|
|
|
|
|
|
|
// chroot/jailId/user/doc/childId
|
|
|
|
const auto docPath = Poco::Path(jailedDocRoot, childId);
|
|
|
|
Poco::File(docPath).createDirectories();
|
|
|
|
|
|
|
|
const auto filename = Poco::Path(uriPublic.getPath()).getFileName();
|
|
|
|
|
|
|
|
// chroot/jailId/user/doc/childId/file.ext
|
|
|
|
const auto jailedFilePath = Poco::Path(docPath, filename).toString();
|
|
|
|
|
2016-01-06 23:33:54 -06:00
|
|
|
const auto localPath = Poco::Path(JailedDocumentRoot, childId);
|
|
|
|
uriJailed = Poco::URI(Poco::URI("file://"), Poco::Path(localPath, filename).toString());
|
2016-01-06 23:16:47 -06:00
|
|
|
|
|
|
|
Log::info("Public URI [" + uriPublic.toString() +
|
|
|
|
"] jailed to [" + uriJailed.toString() + "].");
|
|
|
|
|
|
|
|
#ifdef __linux
|
|
|
|
Log::info("Linking " + publicFilePath + " to " + jailedFilePath);
|
|
|
|
if (!Poco::File(jailedFilePath).exists() && link(publicFilePath.c_str(), jailedFilePath.c_str()) == -1)
|
|
|
|
{
|
|
|
|
// Failed
|
|
|
|
Log::error("link(\"" + publicFilePath + "\", \"" + jailedFilePath + "\") failed.");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Fallback to copying.
|
|
|
|
if (!Poco::File(jailedFilePath).exists())
|
|
|
|
{
|
|
|
|
Log::info("Copying " + publicFilePath + " to " + jailedFilePath);
|
|
|
|
Poco::File(publicFilePath).copyTo(jailedFilePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
Log::error("copyTo(\"" + publicFilePath + "\", \"" + jailedFilePath + "\") failed: " + exc.displayText());
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::info("Public URI [" + uriPublic.toString() +
|
|
|
|
"] is not a file.");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto document = std::shared_ptr<Document>(new Document(uriPublic, uriJailed, childId));
|
|
|
|
|
|
|
|
Log::info("Document [" + publicFilePath + "] created.");
|
|
|
|
it = UriToDocumentMap.emplace_hint(it, publicFilePath, document);
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::URI getPublicUri() const { return _uriPublic; }
|
|
|
|
Poco::URI getJailedUri() const { return _uriJailed; }
|
|
|
|
std::string getChildId() const { return _childId; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Document(const Poco::URI& uriPublic,
|
|
|
|
const Poco::URI& uriJailed,
|
|
|
|
const std::string& childId) :
|
|
|
|
_uriPublic(uriPublic),
|
|
|
|
_uriJailed(uriJailed),
|
|
|
|
_childId(childId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Document management mutex.
|
|
|
|
static std::mutex DocumentsMutex;
|
|
|
|
static std::map<std::string, std::shared_ptr<Document>> UriToDocumentMap;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Poco::URI _uriPublic;
|
|
|
|
const Poco::URI _uriJailed;
|
|
|
|
const std::string _childId;
|
|
|
|
};
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
class LOOLWSD: public Poco::Util::ServerApplication
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LOOLWSD();
|
|
|
|
~LOOLWSD();
|
|
|
|
|
|
|
|
// An Application is a singleton anyway, so just keep these as
|
|
|
|
// statics
|
2015-12-27 21:47:39 -06:00
|
|
|
static std::atomic<unsigned> NextSessionId;
|
2015-12-28 15:34:21 -06:00
|
|
|
static int NumPreSpawnedChildren;
|
|
|
|
static int BrokerWritePipe;
|
2015-08-05 19:01:39 -05:00
|
|
|
static bool doTest;
|
2015-06-04 09:07:49 -05:00
|
|
|
static std::string cache;
|
2015-04-08 09:22:42 -05:00
|
|
|
static std::string sysTemplate;
|
|
|
|
static std::string loTemplate;
|
|
|
|
static std::string childRoot;
|
2016-01-06 22:37:18 -06:00
|
|
|
static std::string jailId;
|
2015-04-08 09:22:42 -05:00
|
|
|
static std::string loSubPath;
|
2015-12-28 15:34:21 -06:00
|
|
|
static Poco::NamedMutex NamedMutexLOOL;
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
static const std::string CHILD_URI;
|
2015-07-19 15:49:11 -05:00
|
|
|
static const std::string PIDLOG;
|
2015-12-13 12:28:01 -06:00
|
|
|
static const std::string FIFO_FILE;
|
2015-08-05 19:20:05 -05:00
|
|
|
static const std::string LOKIT_PIDLOG;
|
2015-03-26 01:36:35 -05:00
|
|
|
|
2015-12-27 21:47:39 -06:00
|
|
|
static
|
|
|
|
std::string GenSessionId()
|
|
|
|
{
|
|
|
|
return Util::encodeId(++NextSessionId, 4);
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
protected:
|
|
|
|
void initialize(Poco::Util::Application& self) override;
|
|
|
|
void uninitialize() override;
|
|
|
|
void defineOptions(Poco::Util::OptionSet& options) override;
|
|
|
|
void handleOption(const std::string& name, const std::string& value) override;
|
|
|
|
int main(const std::vector<std::string>& args) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void displayHelp();
|
2015-07-13 09:13:06 -05:00
|
|
|
void componentMain();
|
|
|
|
void desktopMain();
|
|
|
|
void startupComponent(int nComponents);
|
|
|
|
void startupDesktop(int nDesktop);
|
|
|
|
int createComponent();
|
|
|
|
int createDesktop();
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2016-01-06 21:47:01 -06:00
|
|
|
bool createBroker(const std::string& jailId);
|
2015-03-17 18:56:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|