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 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/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2018-02-16 10:23:25 -06:00
|
|
|
#include <algorithm>
|
2015-12-27 21:47:39 -06:00
|
|
|
#include <atomic>
|
2019-07-19 05:39:59 -05:00
|
|
|
#include <chrono>
|
2021-04-15 10:43:44 -05:00
|
|
|
#include <cstdio>
|
2016-12-21 14:37:22 -06:00
|
|
|
#include <map>
|
2017-11-07 07:12:18 -06:00
|
|
|
#include <set>
|
2021-10-21 14:29:15 -05:00
|
|
|
#include <unordered_map>
|
2020-07-27 04:27:00 -05:00
|
|
|
#include <unordered_set>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <string>
|
2020-06-01 21:46:49 -05:00
|
|
|
#include <utility>
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2020-04-02 10:11:36 -05:00
|
|
|
#include <signal.h>
|
|
|
|
|
2015-07-13 09:13:06 -05:00
|
|
|
#include <Poco/Path.h>
|
2017-11-07 07:12:18 -06:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2016-03-08 01:31:29 -06:00
|
|
|
#include <Poco/Util/OptionSet.h>
|
|
|
|
#include <Poco/Util/ServerApplication.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-12-27 21:47:39 -06:00
|
|
|
#include "Util.hpp"
|
2020-01-17 16:31:41 -06:00
|
|
|
#include "FileUtil.hpp"
|
2020-05-12 10:19:41 -05:00
|
|
|
#include "RequestDetails.hpp"
|
2020-04-02 10:11:36 -05:00
|
|
|
#include "WebSocketHandler.hpp"
|
2021-10-21 14:29:15 -05:00
|
|
|
#include "QuarantineUtil.hpp"
|
2022-06-13 06:26:05 -05:00
|
|
|
|
2017-03-04 17:07:17 -06:00
|
|
|
class ChildProcess;
|
2016-08-17 18:57:38 -05:00
|
|
|
class TraceFileWriter;
|
2017-03-30 04:15:28 -05:00
|
|
|
class DocumentBroker;
|
2019-07-04 04:50:33 -05:00
|
|
|
class ClipboardCache;
|
2016-08-17 18:57:38 -05:00
|
|
|
|
2023-02-25 10:18:26 -06:00
|
|
|
std::shared_ptr<ChildProcess> getNewChild_Blocks(unsigned mobileAppDocId);
|
2020-05-02 13:14:05 -05:00
|
|
|
|
2020-04-18 08:44:50 -05:00
|
|
|
// A WSProcess object in the WSD process represents a descendant process, either the direct child
|
2021-05-25 02:16:54 -05:00
|
|
|
// process ForKit or a grandchild Kit process, with which the WSD process communicates through a
|
2020-04-18 08:44:50 -05:00
|
|
|
// WebSocket.
|
2020-04-02 10:11:36 -05:00
|
|
|
class WSProcess
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// @param pid is the process ID.
|
2020-10-31 14:08:08 -05:00
|
|
|
/// @param socket is the underlying Socket to the process.
|
2020-04-10 08:37:29 -05:00
|
|
|
WSProcess(const std::string& name,
|
2019-11-26 07:15:38 -06:00
|
|
|
const pid_t pid,
|
2020-04-02 10:11:36 -05:00
|
|
|
const std::shared_ptr<StreamSocket>& socket,
|
|
|
|
std::shared_ptr<WebSocketHandler> handler) :
|
|
|
|
|
|
|
|
_name(name),
|
|
|
|
_pid(pid),
|
2020-06-01 21:46:49 -05:00
|
|
|
_ws(std::move(handler)),
|
2020-04-02 10:11:36 -05:00
|
|
|
_socket(socket)
|
|
|
|
{
|
|
|
|
LOG_INF(_name << " ctor [" << _pid << "].");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WSProcess(WSProcess&& other) = delete;
|
|
|
|
|
|
|
|
const WSProcess& operator=(WSProcess&& other) = delete;
|
|
|
|
|
|
|
|
virtual ~WSProcess()
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
LOG_DBG('~' << _name << " dtor [" << _pid << "].");
|
2020-04-02 10:11:36 -05:00
|
|
|
|
|
|
|
if (_pid <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
terminate();
|
|
|
|
|
|
|
|
// No need for the socket anymore.
|
|
|
|
_ws.reset();
|
|
|
|
_socket.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Let the child close a nice way.
|
|
|
|
void close()
|
|
|
|
{
|
|
|
|
if (_pid < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
LOG_DBG("Closing ChildProcess [" << _pid << "].");
|
|
|
|
|
2022-12-19 06:09:45 -06:00
|
|
|
requestTermination();
|
2020-04-02 10:11:36 -05:00
|
|
|
|
|
|
|
// Shutdown the socket.
|
|
|
|
if (_ws)
|
|
|
|
_ws->shutdown();
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_ERR("Error while closing child process: " << ex.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
_pid = -1; // Detach from child.
|
|
|
|
}
|
|
|
|
|
2022-12-19 06:09:45 -06:00
|
|
|
/// Request graceful termination.
|
|
|
|
void requestTermination()
|
|
|
|
{
|
|
|
|
// Request the child to exit
|
|
|
|
if (isAlive())
|
|
|
|
{
|
|
|
|
LOG_DBG("Stopping ChildProcess [" << _pid << "] by sending 'exit' command");
|
|
|
|
sendTextFrame("exit", /*flush=*/true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 10:11:36 -05:00
|
|
|
/// Kill or abandon the child.
|
|
|
|
void terminate()
|
|
|
|
{
|
|
|
|
if (_pid < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if !MOBILEAPP
|
|
|
|
if (::kill(_pid, 0) == 0)
|
|
|
|
{
|
|
|
|
LOG_INF("Killing child [" << _pid << "].");
|
2023-03-17 12:34:27 -05:00
|
|
|
#if CODE_COVERAGE || VALGRIND_COOLFORKIT
|
2022-03-13 17:28:31 -05:00
|
|
|
constexpr auto signal = SIGTERM;
|
|
|
|
#else
|
|
|
|
constexpr auto signal = SIGKILL;
|
|
|
|
#endif
|
|
|
|
if (!SigUtil::killChild(_pid, signal))
|
2020-04-02 10:11:36 -05:00
|
|
|
{
|
|
|
|
LOG_ERR("Cannot terminate lokit [" << _pid << "]. Abandoning.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// What to do? Throw some unique exception that the outermost call in the thread catches and
|
|
|
|
// exits from the thread?
|
|
|
|
#endif
|
|
|
|
_pid = -1;
|
|
|
|
}
|
|
|
|
|
2019-11-26 07:15:38 -06:00
|
|
|
pid_t getPid() const { return _pid; }
|
2020-04-02 10:11:36 -05:00
|
|
|
|
|
|
|
/// Send a text payload to the child-process WS.
|
2022-12-19 06:07:17 -06:00
|
|
|
bool sendTextFrame(const std::string& data, bool flush = false)
|
Forward setclipboard data to the Kit as a binary message:
assert: invalid utf-8 - check Message::detectType()
#3 0x00007fce7ae57fd6 in __GI___assert_fail at ./net/WebSocketHandler.hpp",
#4 0x0000558bd2790231 in WebSocketHandler::sendFrame at ./net/WebSocketHandler.hpp:748
#5 0x0000558bd2851202 in WebSocketHandler::sendMessage (flush=false, code=WSOpCode::Text, len=22118,
data=0x7fce5c227b50 "child-045 setclipboard\napplication/x-openoffice-embed-source-xml;windows_formatname=\"Star Embed Source (XML)\"\n1370\nPK\003\004\024", this=0x7fce6c003340)
at ./net/WebSocketHandler.hpp:641
#6 WebSocketHandler::sendTextMessage (flush=false, len=22118, ...) at ./net/WebSocketHandler.hpp:619
#7 WebSocketHandler::sendMessage (msg="child-045 setclipboard\nappl...) at ./net/WebSocketHandler.hpp:613
#8 WSProcess::sendTextFrame (this=0x7fce6c001300, data="child-045 setclipboard\nap...) at wsd/COOLWSD.hpp:142
#9 0x0000558bd27d5678 in DocumentBroker::forwardToChild (this=this@entry=0x7fce64027940, viewId="045", message="setclipboard\n...) at /usr/include/c++/9/bits/shared_ptr_base.h:1020
#10 0x0000558bd2899d50 in ClientSession::handleClipboardRequest (this=0x7fce64027dc0, type=type@entry=DocumentBroker::CLIP_REQUEST_SET, ...) at ./common/Session.hpp:76
#11 0x0000558bd27e0999 in DocumentBroker::handleClipboardRequest (this=0x7fce64027940, type=DocumentBroker::CLIP_REQUEST_SET, ...) at /usr/include/c++/9/bits/shared_ptr_base.h:1020
#12 0x0000558bd2847b8b in ClientRequestDispatcher::handleClipboardRequest(...) at /usr/include/c++/9/bits/shared_ptr_base.h:1020
Change-Id: I406eee0ac3a47986fdd9511e674c9228d1994d38
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2022-08-26 16:09:34 -05:00
|
|
|
{
|
2022-12-19 06:07:17 -06:00
|
|
|
return sendFrame(data, false, flush);
|
Forward setclipboard data to the Kit as a binary message:
assert: invalid utf-8 - check Message::detectType()
#3 0x00007fce7ae57fd6 in __GI___assert_fail at ./net/WebSocketHandler.hpp",
#4 0x0000558bd2790231 in WebSocketHandler::sendFrame at ./net/WebSocketHandler.hpp:748
#5 0x0000558bd2851202 in WebSocketHandler::sendMessage (flush=false, code=WSOpCode::Text, len=22118,
data=0x7fce5c227b50 "child-045 setclipboard\napplication/x-openoffice-embed-source-xml;windows_formatname=\"Star Embed Source (XML)\"\n1370\nPK\003\004\024", this=0x7fce6c003340)
at ./net/WebSocketHandler.hpp:641
#6 WebSocketHandler::sendTextMessage (flush=false, len=22118, ...) at ./net/WebSocketHandler.hpp:619
#7 WebSocketHandler::sendMessage (msg="child-045 setclipboard\nappl...) at ./net/WebSocketHandler.hpp:613
#8 WSProcess::sendTextFrame (this=0x7fce6c001300, data="child-045 setclipboard\nap...) at wsd/COOLWSD.hpp:142
#9 0x0000558bd27d5678 in DocumentBroker::forwardToChild (this=this@entry=0x7fce64027940, viewId="045", message="setclipboard\n...) at /usr/include/c++/9/bits/shared_ptr_base.h:1020
#10 0x0000558bd2899d50 in ClientSession::handleClipboardRequest (this=0x7fce64027dc0, type=type@entry=DocumentBroker::CLIP_REQUEST_SET, ...) at ./common/Session.hpp:76
#11 0x0000558bd27e0999 in DocumentBroker::handleClipboardRequest (this=0x7fce64027940, type=DocumentBroker::CLIP_REQUEST_SET, ...) at /usr/include/c++/9/bits/shared_ptr_base.h:1020
#12 0x0000558bd2847b8b in ClientRequestDispatcher::handleClipboardRequest(...) at /usr/include/c++/9/bits/shared_ptr_base.h:1020
Change-Id: I406eee0ac3a47986fdd9511e674c9228d1994d38
Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2022-08-26 16:09:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a payload to the child-process WS.
|
2022-12-19 06:07:17 -06:00
|
|
|
bool sendFrame(const std::string& data, bool binary = false, bool flush = false)
|
2020-04-02 10:11:36 -05:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_ws)
|
|
|
|
{
|
2022-12-19 06:07:17 -06:00
|
|
|
LOG_TRC("Send to " << _name << " message: ["
|
|
|
|
<< COOLProtocol::getAbbreviatedMessage(data) << ']');
|
|
|
|
_ws->sendMessage(data.c_str(), data.size(),
|
|
|
|
(binary ? WSOpCode::Binary : WSOpCode::Text), flush);
|
2020-04-02 10:11:36 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
LOG_ERR("Failed to send " << _name << " [" << _pid << "] data [" <<
|
2021-11-18 06:08:14 -06:00
|
|
|
COOLProtocol::getAbbreviatedMessage(data) << "] due to: " << exc.what());
|
2020-04-02 10:11:36 -05:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_WRN("No socket to " << _name << " to send [" << COOLProtocol::getAbbreviatedMessage(data) << ']');
|
2020-04-02 10:11:36 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether this child is alive and socket not in error.
|
|
|
|
/// Note: zombies will show as alive, and sockets have waiting
|
|
|
|
/// time after the other end-point closes. So this isn't accurate.
|
|
|
|
virtual bool isAlive() const
|
|
|
|
{
|
|
|
|
#if !MOBILEAPP
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return _pid > 1 && _ws && ::kill(_pid, 0) == 0;
|
|
|
|
}
|
|
|
|
catch (const std::exception&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
return _pid > 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-31 12:45:52 -05:00
|
|
|
protected:
|
|
|
|
std::shared_ptr<WebSocketHandler> getWSHandler() const { return _ws; }
|
|
|
|
std::shared_ptr<Socket> getSocket() const { return _socket; };
|
|
|
|
|
|
|
|
private:
|
2020-04-02 10:11:36 -05:00
|
|
|
std::string _name;
|
2023-06-10 15:20:55 -05:00
|
|
|
std::atomic<pid_t> _pid; //< The process-id, which can be access from different threads.
|
2020-04-02 10:11:36 -05:00
|
|
|
std::shared_ptr<WebSocketHandler> _ws;
|
|
|
|
std::shared_ptr<Socket> _socket;
|
|
|
|
};
|
|
|
|
|
2020-04-20 03:47:50 -05:00
|
|
|
#if !MOBILEAPP
|
|
|
|
|
2023-05-06 13:21:30 -05:00
|
|
|
class ForKitProcWSHandler final : public WebSocketHandler
|
2020-04-02 10:11:36 -05:00
|
|
|
{
|
|
|
|
public:
|
2023-05-06 13:13:24 -05:00
|
|
|
template <typename T>
|
|
|
|
ForKitProcWSHandler(const std::weak_ptr<StreamSocket>& socket, const T& request)
|
2021-03-21 14:58:10 -05:00
|
|
|
: WebSocketHandler(socket.lock(), request)
|
2020-04-02 10:11:36 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-21 14:58:10 -05:00
|
|
|
virtual void handleMessage(const std::vector<char>& data) override;
|
2020-04-02 10:11:36 -05:00
|
|
|
};
|
|
|
|
|
2023-05-06 13:21:30 -05:00
|
|
|
class ForKitProcess final : public WSProcess
|
2020-04-02 10:11:36 -05:00
|
|
|
{
|
|
|
|
public:
|
2023-05-06 13:13:24 -05:00
|
|
|
template <typename T>
|
|
|
|
ForKitProcess(int pid, std::shared_ptr<StreamSocket>& socket, const T& request)
|
2020-04-02 10:11:36 -05:00
|
|
|
: WSProcess("ForKit", pid, socket, std::make_shared<ForKitProcWSHandler>(socket, request))
|
|
|
|
{
|
2020-10-31 12:45:52 -05:00
|
|
|
socket->setHandler(getWSHandler());
|
2020-04-02 10:11:36 -05:00
|
|
|
}
|
|
|
|
};
|
2017-03-04 17:07:17 -06:00
|
|
|
|
2020-04-20 03:47:50 -05:00
|
|
|
#endif
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// The Server class which is responsible for all
|
|
|
|
/// external interactions.
|
2023-05-06 13:21:30 -05:00
|
|
|
class COOLWSD final : public Poco::Util::ServerApplication
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
|
|
|
public:
|
2021-11-18 06:08:14 -06:00
|
|
|
COOLWSD();
|
|
|
|
~COOLWSD();
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2016-03-10 21:33:03 -06:00
|
|
|
// An Application is a singleton anyway,
|
|
|
|
// so just keep these as statics.
|
2019-09-21 13:15:37 -05:00
|
|
|
static std::atomic<uint64_t> NextConnectionId;
|
2016-04-06 22:51:58 -05:00
|
|
|
static unsigned int NumPreSpawnedChildren;
|
2020-02-28 07:55:42 -06:00
|
|
|
#if !MOBILEAPP
|
2016-08-01 03:05:37 -05:00
|
|
|
static bool NoCapsForKit;
|
2018-03-19 10:20:10 -05:00
|
|
|
static bool NoSeccomp;
|
2018-04-17 14:47:17 -05:00
|
|
|
static bool AdminEnabled;
|
2022-02-15 20:04:37 -06:00
|
|
|
static bool UnattendedRun; //< True when run from an unattended test, not interactive.
|
2022-04-13 13:53:49 -05:00
|
|
|
static bool SignalParent;
|
2022-12-05 21:45:59 -06:00
|
|
|
static std::string RouteToken;
|
2020-03-04 13:38:17 -06:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
static bool SingleKit;
|
2023-04-15 06:43:49 -05:00
|
|
|
static bool ForceCaching;
|
2020-02-28 07:55:42 -06:00
|
|
|
#endif
|
2020-04-02 10:11:36 -05:00
|
|
|
static std::shared_ptr<ForKitProcess> ForKitProc;
|
2017-01-15 16:54:55 -06:00
|
|
|
static std::atomic<int> ForKitProcId;
|
2020-04-20 05:26:14 -05:00
|
|
|
#endif
|
2020-06-26 05:58:09 -05:00
|
|
|
static std::string UserInterface;
|
2017-01-26 02:19:50 -06:00
|
|
|
static std::string ConfigFile;
|
2017-11-07 07:12:18 -06:00
|
|
|
static std::string ConfigDir;
|
2016-01-15 02:40:54 -06:00
|
|
|
static std::string SysTemplate;
|
|
|
|
static std::string LoTemplate;
|
2023-04-24 09:56:02 -05:00
|
|
|
static std::string CleanupChildRoot;
|
2016-01-15 02:40:54 -06:00
|
|
|
static std::string ChildRoot;
|
2016-04-14 04:13:30 -05:00
|
|
|
static std::string ServerName;
|
2016-03-20 09:07:24 -05:00
|
|
|
static std::string FileServerRoot;
|
2018-09-06 17:25:50 -05:00
|
|
|
static std::string ServiceRoot; ///< There are installations that need prefixing every page with some path.
|
2022-02-23 04:47:53 -06:00
|
|
|
static std::string TmpFontDir;
|
2016-06-20 13:58:00 -05:00
|
|
|
static std::string LOKitVersion;
|
2021-05-06 07:44:42 -05:00
|
|
|
static bool EnableTraceEventLogging;
|
2023-06-09 04:14:27 -05:00
|
|
|
static bool EnableAccessibility;
|
2021-05-04 03:57:58 -05:00
|
|
|
static FILE *TraceEventFile;
|
2021-06-18 06:53:51 -05:00
|
|
|
static void writeTraceEventRecording(const char *data, std::size_t nbytes);
|
|
|
|
static void writeTraceEventRecording(const std::string &recording);
|
2018-01-07 21:34:28 -06:00
|
|
|
static std::string LogLevel;
|
2022-11-21 10:39:17 -06:00
|
|
|
static std::string LogLevelStartup;
|
2021-05-31 06:00:21 -05:00
|
|
|
static std::string MostVerboseLogLevelSettableFromClient;
|
|
|
|
static std::string LeastVerboseLogLevelSettableFromClient;
|
2019-04-14 11:24:45 -05:00
|
|
|
static bool AnonymizeUserData;
|
2021-11-18 06:08:14 -06:00
|
|
|
static bool CheckCoolUser;
|
2020-06-28 10:30:22 -05:00
|
|
|
static bool CleanupOnly;
|
2020-05-07 15:11:38 -05:00
|
|
|
static bool IsProxyPrefixEnabled;
|
2016-07-13 21:01:23 -05:00
|
|
|
static std::atomic<unsigned> NumConnections;
|
2016-07-31 12:56:57 -05:00
|
|
|
static std::unique_ptr<TraceFileWriter> TraceDumper;
|
2019-08-12 04:04:10 -05:00
|
|
|
#if !MOBILEAPP
|
2019-07-04 04:50:33 -05:00
|
|
|
static std::unique_ptr<ClipboardCache> SavedClipboards;
|
2019-08-12 04:04:10 -05:00
|
|
|
#endif
|
2021-03-13 13:20:46 -06:00
|
|
|
|
2020-07-27 04:27:00 -05:00
|
|
|
static std::unordered_set<std::string> EditFileExtensions;
|
|
|
|
static std::unordered_set<std::string> ViewWithCommentsFileExtensions;
|
2017-10-03 14:48:28 -05:00
|
|
|
static unsigned MaxConnections;
|
|
|
|
static unsigned MaxDocuments;
|
2017-10-03 22:54:05 -05:00
|
|
|
static std::string OverrideWatermark;
|
2017-11-07 07:12:18 -06:00
|
|
|
static std::set<const Poco::Util::AbstractConfiguration*> PluginConfigurations;
|
2020-12-06 13:51:54 -06:00
|
|
|
static std::chrono::steady_clock::time_point StartTime;
|
2022-07-13 16:45:35 -05:00
|
|
|
static std::string BuyProductUrl;
|
2022-05-05 15:06:41 -05:00
|
|
|
static std::string LatestVersion;
|
|
|
|
static std::mutex FetchUpdateMutex;
|
2022-07-18 19:31:12 -05:00
|
|
|
static bool IsBindMountingEnabled;
|
2022-07-13 16:45:35 -05:00
|
|
|
static std::mutex RemoteConfigMutex;
|
2019-12-05 15:27:16 -06:00
|
|
|
#if MOBILEAPP
|
2020-04-24 02:46:54 -05:00
|
|
|
#ifndef IOS
|
2019-12-05 15:27:16 -06:00
|
|
|
/// This is used to be able to wait until the lokit main thread has finished (and it is safe to load a new document).
|
|
|
|
static std::mutex lokit_main_mutex;
|
2020-04-24 02:46:54 -05:00
|
|
|
#endif
|
2019-12-05 15:27:16 -06:00
|
|
|
#endif
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2020-01-17 15:18:42 -06:00
|
|
|
/// For testing only [!]
|
|
|
|
static int getClientPortNumber();
|
2019-06-21 05:47:07 -05:00
|
|
|
/// For testing only [!] DocumentBrokers are mostly single-threaded with their own thread
|
|
|
|
static std::vector<std::shared_ptr<DocumentBroker>> getBrokersTestOnly();
|
2017-09-19 13:06:46 -05:00
|
|
|
|
2021-03-26 11:57:08 -05:00
|
|
|
// Return a map for fast searches. Used in testing and in admin for cleanup
|
|
|
|
static std::set<pid_t> getKitPids();
|
|
|
|
|
2019-09-21 13:15:37 -05:00
|
|
|
static std::string GetConnectionId()
|
2015-12-27 21:47:39 -06:00
|
|
|
{
|
2019-09-21 13:15:37 -05:00
|
|
|
return Util::encodeId(NextConnectionId++, 3);
|
2015-12-27 21:47:39 -06:00
|
|
|
}
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
static bool isSSLEnabled()
|
2016-07-18 06:45:36 -05:00
|
|
|
{
|
2019-08-20 03:20:22 -05:00
|
|
|
#if ENABLE_SSL
|
2021-11-18 06:08:14 -06:00
|
|
|
return !Util::isFuzzing() && COOLWSD::SSLEnabled.get();
|
2019-08-20 03:20:22 -05:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2016-07-18 06:45:36 -05:00
|
|
|
}
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
static bool isSSLTermination()
|
2016-08-28 14:41:28 -05:00
|
|
|
{
|
2019-08-20 03:20:22 -05:00
|
|
|
#if ENABLE_SSL
|
2021-11-18 06:08:14 -06:00
|
|
|
return !Util::isFuzzing() && COOLWSD::SSLTermination.get();
|
2019-08-20 03:20:22 -05:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2016-08-28 14:41:28 -05:00
|
|
|
}
|
2017-05-07 12:28:57 -05:00
|
|
|
|
2022-06-02 16:24:38 -05:00
|
|
|
static std::shared_ptr<TerminatingPoll> getWebServerPoll();
|
|
|
|
|
2020-07-27 04:27:00 -05:00
|
|
|
/// Return true if extension is marked as view action in discovery.xml.
|
2017-06-06 22:43:48 -05:00
|
|
|
static bool IsViewFileExtension(const std::string& extension)
|
|
|
|
{
|
2018-02-16 10:23:25 -06:00
|
|
|
std::string lowerCaseExtension = extension;
|
|
|
|
std::transform(lowerCaseExtension.begin(), lowerCaseExtension.end(), lowerCaseExtension.begin(), ::tolower);
|
2021-10-20 07:06:33 -05:00
|
|
|
#if MOBILEAPP
|
|
|
|
if (lowerCaseExtension == "pdf")
|
|
|
|
return true; // true for only pdf - it is not editable
|
|
|
|
return false; // mark everything else editable on mobile
|
|
|
|
#else
|
|
|
|
return EditFileExtensions.find(lowerCaseExtension) == EditFileExtensions.end();
|
2019-02-13 10:34:59 -06:00
|
|
|
#endif
|
2017-06-06 22:43:48 -05:00
|
|
|
}
|
|
|
|
|
2020-07-27 04:27:00 -05:00
|
|
|
/// Return true if extension is marked as view_comment action in discovery.xml.
|
|
|
|
static bool IsViewWithCommentsFileExtension(const std::string& extension)
|
|
|
|
{
|
2021-10-20 07:06:33 -05:00
|
|
|
|
2020-07-27 04:27:00 -05:00
|
|
|
std::string lowerCaseExtension = extension;
|
|
|
|
std::transform(lowerCaseExtension.begin(), lowerCaseExtension.end(), lowerCaseExtension.begin(), ::tolower);
|
2021-10-20 07:06:33 -05:00
|
|
|
#if MOBILEAPP
|
|
|
|
if (lowerCaseExtension == "pdf")
|
|
|
|
return true; // true for only pdf - it is not editable
|
|
|
|
return false; // mark everything else editable on mobile
|
|
|
|
#else
|
2020-07-27 04:27:00 -05:00
|
|
|
return ViewWithCommentsFileExtensions.find(lowerCaseExtension) != ViewWithCommentsFileExtensions.end();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-07 12:28:57 -05:00
|
|
|
/// Returns the value of the specified application configuration,
|
2019-08-26 15:22:01 -05:00
|
|
|
/// or the default, if one doesn't exist.
|
2017-05-07 12:28:57 -05:00
|
|
|
template<typename T>
|
|
|
|
static
|
|
|
|
T getConfigValue(const std::string& name, const T def)
|
|
|
|
{
|
Add an initial libfuzzer based fuzzer
- target ClientSession::_handleInput(), since crashing there would bring
down the whole loolwsd (not just a kit process), and it deals with
input from untrusted users (browsers)
- add a --enable-fuzzers configure switch to build with
-fsanitize=fuzzer (compared to normal sanitizers build, this is the only
special flag needed)
- configuring other sanitizers is not done automatically, either use
--with-sanitizer=... or the environment variables from LODE's sanitizer
config
- run the actual fuzzer like this:
./clientsession_fuzzer -max_len=16384 fuzzer/data/
- note that at least openSUSE Leap 15.1 sadly ships with a clang with
libfuzzer static libs removed from the package, so you need a
self-built clang to run the fuzzer (either manual build or one from
LODE)
- <https://chromium.googlesource.com/chromium/src/testing/libfuzzer/+/refs/heads/master/efficient_fuzzing.md#execution-speed>
suggests that "You should aim for at least 1,000 exec/s from your fuzz
target locally" (i.e. one run should not take more than 1 ms), so try
this minimal approach first. The alternative would be to start from the
existing loolwsd_fuzzer binary, then step by step cut it down to not
fork(), not do any network traffic, etc -- till it's fast enough that
the fuzzer can find interesting input
- the various configurations start to be really complex (the matrix is
just very large), so try to use Util::isFuzzing() for fuzzer-specific
changes (this is what core.git does as well), and only resort to ifdefs
for the Util::isFuzzing() itself
Change-Id: I72dc1193b34c93eacb5d8e39cef42387d42bd72f
Reviewed-on: https://gerrit.libreoffice.org/c/online/+/89226
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
2020-02-21 08:52:20 -06:00
|
|
|
if (Util::isFuzzing())
|
|
|
|
{
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2017-05-07 12:28:57 -05:00
|
|
|
return getConfigValue(Application::instance().config(), name, def);
|
|
|
|
}
|
2016-08-28 14:41:28 -05:00
|
|
|
|
2022-04-23 18:01:16 -05:00
|
|
|
/// Returns the value of the specified application configuration,
|
|
|
|
/// or the default, if one doesn't exist.
|
|
|
|
template <typename T> static T getConfigValueNonZero(const std::string& name, const T def)
|
|
|
|
{
|
|
|
|
static_assert(std::is_integral<T>::value, "Meaningless on non-integral types");
|
2022-08-22 01:26:20 -05:00
|
|
|
|
|
|
|
if (Util::isFuzzing())
|
|
|
|
{
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:01:16 -05:00
|
|
|
const T res = getConfigValue(Application::instance().config(), name, def);
|
|
|
|
return res <= T(0) ? T(0) : res;
|
|
|
|
}
|
|
|
|
|
2019-10-07 06:51:30 -05:00
|
|
|
/// Reads and processes path entries with the given property
|
|
|
|
/// from the configuration.
|
|
|
|
/// Converts relative paths to absolute.
|
|
|
|
static
|
|
|
|
std::string getPathFromConfig(const std::string& name)
|
|
|
|
{
|
|
|
|
return getPathFromConfig(Application::instance().config(), name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads and processes path entries with the given property
|
|
|
|
/// from the configuration. If value is empty then it reads from fallback
|
|
|
|
/// Converts relative paths to absolute.
|
|
|
|
static
|
|
|
|
std::string getPathFromConfigWithFallback(const std::string& name, const std::string& fallbackName)
|
|
|
|
{
|
2020-09-08 07:06:47 -05:00
|
|
|
std::string value;
|
|
|
|
// the expected path might not exist, in which case Poco throws an exception
|
|
|
|
try
|
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
value = COOLWSD::getPathFromConfig(name);
|
2020-09-08 07:06:47 -05:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
}
|
2019-10-07 06:51:30 -05:00
|
|
|
if (value.empty())
|
2021-11-18 06:08:14 -06:00
|
|
|
return COOLWSD::getPathFromConfig(fallbackName);
|
2019-10-07 06:51:30 -05:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-03-05 14:19:36 -06:00
|
|
|
/// Returns true if and only if the property with the given key exists.
|
|
|
|
static
|
|
|
|
bool hasProperty(const std::string& key)
|
|
|
|
{
|
|
|
|
return Application::instance().config().hasProperty(key);
|
|
|
|
}
|
|
|
|
|
2017-02-05 18:35:54 -06:00
|
|
|
/// Trace a new session and take a snapshot of the file.
|
|
|
|
static void dumpNewSessionTrace(const std::string& id, const std::string& sessionId, const std::string& uri, const std::string& path);
|
|
|
|
|
|
|
|
/// Trace the end of a session.
|
|
|
|
static void dumpEndSessionTrace(const std::string& id, const std::string& sessionId, const std::string& uri);
|
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
static void dumpEventTrace(const std::string& id, const std::string& sessionId, const std::string& data);
|
2016-08-02 20:09:01 -05:00
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
static void dumpIncomingTrace(const std::string& id, const std::string& sessionId, const std::string& data);
|
2016-07-30 21:22:28 -05:00
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
static void dumpOutgoingTrace(const std::string& id, const std::string& sessionId, const std::string& data);
|
2016-07-31 06:54:47 -05:00
|
|
|
|
2017-01-15 19:12:10 -06:00
|
|
|
/// Waits on Forkit and reaps if it dies, then restores.
|
|
|
|
/// Return true if wait succeeds.
|
|
|
|
static bool checkAndRestoreForKit();
|
|
|
|
|
2017-01-15 16:54:55 -06:00
|
|
|
/// Creates a new instance of Forkit.
|
2020-04-26 15:55:16 -05:00
|
|
|
/// Return true when successful.
|
2017-01-15 16:54:55 -06:00
|
|
|
static bool createForKit();
|
|
|
|
|
2020-04-02 10:11:36 -05:00
|
|
|
/// Sends a message to ForKit through PrisonerPoll.
|
|
|
|
static void sendMessageToForKit(const std::string& message);
|
|
|
|
|
2017-04-02 18:56:42 -05:00
|
|
|
/// Checks forkit (and respawns), rebalances
|
|
|
|
/// child kit processes and cleans up DocBrokers.
|
|
|
|
static void doHousekeeping();
|
2017-03-27 20:46:16 -05:00
|
|
|
|
2020-03-16 11:19:37 -05:00
|
|
|
static void checkDiskSpaceAndWarnClients(const bool cacheLastCheck);
|
|
|
|
|
|
|
|
static void checkSessionLimitsAndWarnClients();
|
|
|
|
|
2017-07-07 06:42:19 -05:00
|
|
|
/// Close document with @docKey and a @message
|
|
|
|
static void closeDocument(const std::string& docKey, const std::string& message);
|
|
|
|
|
2020-04-10 08:37:29 -05:00
|
|
|
/// Autosave a given document (currently only called from Admin).
|
2017-07-10 12:15:04 -05:00
|
|
|
static void autoSave(const std::string& docKey);
|
|
|
|
|
2020-11-23 05:07:44 -06:00
|
|
|
/// Sets the log level of current kits.
|
2020-11-15 06:23:54 -06:00
|
|
|
static void setLogLevelsOfKits(const std::string& level);
|
|
|
|
|
2018-06-10 10:35:59 -05:00
|
|
|
/// Anonymize the basename of filenames, preserving the path and extension.
|
|
|
|
static std::string anonymizeUrl(const std::string& url)
|
|
|
|
{
|
2020-01-17 16:31:41 -06:00
|
|
|
return FileUtil::anonymizeUrl(url);
|
2018-06-10 10:35:59 -05:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:50:09 -05:00
|
|
|
/// Anonymize user names and IDs.
|
2020-04-26 15:55:16 -05:00
|
|
|
/// Will use the Obfuscated User ID if one is provided via WOPI.
|
2018-06-10 10:35:59 -05:00
|
|
|
static std::string anonymizeUsername(const std::string& username)
|
|
|
|
{
|
2020-01-17 16:31:41 -06:00
|
|
|
return FileUtil::anonymizeUsername(username);
|
2018-06-10 10:35:59 -05:00
|
|
|
}
|
2022-12-03 00:46:13 -06:00
|
|
|
static void alertAllUsersInternal(const std::string& msg);
|
2018-06-10 10:35:59 -05:00
|
|
|
|
2022-10-19 18:27:24 -05:00
|
|
|
#if ENABLE_DEBUG
|
2019-02-13 11:01:08 -06:00
|
|
|
/// get correct server URL with protocol + port number for this running server
|
|
|
|
static std::string getServerURL();
|
2022-10-19 18:27:24 -05:00
|
|
|
#endif
|
2019-02-13 11:01:08 -06:00
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
protected:
|
2021-03-11 21:46:14 -06:00
|
|
|
void initialize(Poco::Util::Application& self) override
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
innerInitialize(self);
|
|
|
|
}
|
2021-04-15 16:37:46 -05:00
|
|
|
catch (const Poco::Exception& ex)
|
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_FTL("Failed to initialize COOLWSD: "
|
2021-04-15 16:37:46 -05:00
|
|
|
<< ex.displayText()
|
|
|
|
<< (ex.nested() ? " (" + ex.nested()->displayText() + ')' : ""));
|
|
|
|
throw; // Nothing further to do.
|
|
|
|
}
|
2021-03-11 21:46:14 -06:00
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_FTL("Failed to initialize COOLWSD: " << ex.what());
|
2021-03-11 21:46:14 -06:00
|
|
|
throw; // Nothing further to do.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
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;
|
|
|
|
|
2017-03-27 14:14:16 -05:00
|
|
|
/// Handle various global static destructors.
|
2020-09-18 04:03:17 -05:00
|
|
|
static void cleanup();
|
2017-03-27 14:14:16 -05:00
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
private:
|
2019-08-20 03:20:22 -05:00
|
|
|
#if ENABLE_SSL
|
2016-07-19 04:07:07 -05:00
|
|
|
static Util::RuntimeConstant<bool> SSLEnabled;
|
2016-08-28 14:41:28 -05:00
|
|
|
static Util::RuntimeConstant<bool> SSLTermination;
|
2019-08-20 03:20:22 -05:00
|
|
|
#endif
|
2016-07-18 06:45:36 -05:00
|
|
|
|
2022-05-05 15:06:41 -05:00
|
|
|
#if !MOBILEAPP
|
|
|
|
void processFetchUpdate();
|
|
|
|
#endif
|
2016-03-23 06:08:01 -05:00
|
|
|
void initializeSSL();
|
2015-03-17 18:56:15 -05:00
|
|
|
void displayHelp();
|
2016-04-06 17:17:59 -05:00
|
|
|
|
2021-03-11 21:46:14 -06:00
|
|
|
/// The actual initialize implementation.
|
|
|
|
void innerInitialize(Application& self);
|
|
|
|
|
|
|
|
/// The actual main implementation.
|
|
|
|
int innerMain();
|
|
|
|
|
2016-07-18 06:45:36 -05:00
|
|
|
class ConfigValueGetter
|
|
|
|
{
|
2016-07-28 01:39:03 -05:00
|
|
|
Poco::Util::LayeredConfiguration& _config;
|
|
|
|
const std::string& _name;
|
2016-07-18 06:45:36 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
ConfigValueGetter(Poco::Util::LayeredConfiguration& config,
|
|
|
|
const std::string& name)
|
2016-07-28 01:39:03 -05:00
|
|
|
: _config(config)
|
|
|
|
, _name(name)
|
2016-10-29 20:15:00 -05:00
|
|
|
{
|
|
|
|
}
|
2016-07-18 06:45:36 -05:00
|
|
|
|
2017-01-28 16:25:14 -06:00
|
|
|
void operator()(int& value) { value = _config.getInt(_name); }
|
2016-07-28 01:39:03 -05:00
|
|
|
void operator()(unsigned int& value) { value = _config.getUInt(_name); }
|
2019-11-22 09:50:49 -06:00
|
|
|
void operator()(uint64_t& value) { value = _config.getUInt64(_name); }
|
2016-07-28 01:39:03 -05:00
|
|
|
void operator()(bool& value) { value = _config.getBool(_name); }
|
2016-07-30 21:19:31 -05:00
|
|
|
void operator()(std::string& value) { value = _config.getString(_name); }
|
2017-07-07 06:42:19 -05:00
|
|
|
void operator()(double& value) { value = _config.getDouble(_name); }
|
2016-07-18 06:45:36 -05:00
|
|
|
};
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
template <typename T>
|
|
|
|
static bool getSafeConfig(Poco::Util::LayeredConfiguration& config,
|
|
|
|
const std::string& name, T& value)
|
2016-06-25 11:31:22 -05:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-07-18 06:45:36 -05:00
|
|
|
ConfigValueGetter(config, name)(value);
|
2016-06-25 11:31:22 -05:00
|
|
|
return true;
|
|
|
|
}
|
2020-02-28 07:58:32 -06:00
|
|
|
catch (...)
|
2016-06-25 11:31:22 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-18 06:45:36 -05:00
|
|
|
template<typename T>
|
2016-06-25 11:31:22 -05:00
|
|
|
static
|
2016-07-18 06:45:36 -05:00
|
|
|
T getConfigValue(Poco::Util::LayeredConfiguration& config,
|
|
|
|
const std::string& name, const T def)
|
2016-06-25 11:31:22 -05:00
|
|
|
{
|
2016-07-18 06:45:36 -05:00
|
|
|
T value = def;
|
|
|
|
if (getSafeConfig(config, name, value) ||
|
|
|
|
getSafeConfig(config, name + "[@default]", value))
|
2016-06-25 11:31:22 -05:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2016-04-06 17:17:59 -05:00
|
|
|
/// Reads and processes path entries with the given property
|
|
|
|
/// from the configuration.
|
|
|
|
/// Converts relative paths to absolute.
|
2019-10-07 06:51:30 -05:00
|
|
|
static
|
|
|
|
std::string getPathFromConfig(Poco::Util::LayeredConfiguration& config, const std::string& property)
|
2016-04-06 17:17:59 -05:00
|
|
|
{
|
2019-10-07 06:51:30 -05:00
|
|
|
std::string path = config.getString(property);
|
|
|
|
if (path.empty() && config.hasProperty(property + "[@default]"))
|
2016-04-06 22:51:58 -05:00
|
|
|
{
|
|
|
|
// Use the default value if empty and a default provided.
|
2019-10-07 06:51:30 -05:00
|
|
|
path = config.getString(property + "[@default]");
|
2016-04-06 22:51:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reconstruct absolute path if relative.
|
2016-06-25 17:03:41 -05:00
|
|
|
if (!Poco::Path(path).isAbsolute() &&
|
2019-10-07 06:51:30 -05:00
|
|
|
config.hasProperty(property + "[@relative]") &&
|
|
|
|
config.getBool(property + "[@relative]"))
|
2016-04-06 17:17:59 -05:00
|
|
|
{
|
|
|
|
path = Poco::Path(Application::instance().commandPath()).parent().append(path).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
2016-06-25 11:31:22 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Settings passed from the command-line to override those in the config file.
|
|
|
|
std::map<std::string, std::string> _overrideSettings;
|
2018-09-13 11:16:00 -05:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if MOBILEAPP
|
2018-09-13 11:16:00 -05:00
|
|
|
public:
|
|
|
|
static int prisonerServerSocketFD;
|
|
|
|
#endif
|
2015-03-17 18:56:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|