2016-03-31 01:48:34 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2016-04-08 06:49:33 -05:00
|
|
|
|
2017-03-08 10:38:22 -06:00
|
|
|
#include "Storage.hpp"
|
|
|
|
|
2016-11-11 08:32:29 -06:00
|
|
|
#include <algorithm>
|
2020-06-11 09:54:27 -05:00
|
|
|
#include <memory>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <cassert>
|
2018-06-10 10:42:15 -05:00
|
|
|
#include <errno.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <fstream>
|
2017-10-24 04:31:39 -05:00
|
|
|
#include <iconv.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <string>
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-12-16 07:19:27 -06:00
|
|
|
#include <Poco/Exception.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
2018-09-05 07:11:05 -05:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-09-05 07:11:05 -05:00
|
|
|
|
|
|
|
#include <Poco/Net/AcceptCertificateHandler.h>
|
|
|
|
#include <Poco/Net/Context.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <Poco/Net/DNS.h>
|
2016-04-08 04:24:52 -05:00
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
2018-09-05 07:11:05 -05:00
|
|
|
#include <Poco/Net/KeyConsoleHandler.h>
|
2017-05-17 03:48:51 -05:00
|
|
|
#include <Poco/Net/NameValueCollection.h>
|
2016-04-19 03:17:50 -05:00
|
|
|
#include <Poco/Net/NetworkInterface.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/Net/SSLManager.h>
|
2018-09-05 07:11:05 -05:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/StreamCopier.h>
|
2018-07-17 01:01:05 -05:00
|
|
|
#include <Poco/URI.h>
|
2016-03-31 01:48:34 -05:00
|
|
|
|
|
|
|
#include "Auth.hpp"
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <Common.hpp>
|
2016-04-16 11:55:56 -05:00
|
|
|
#include "Exceptions.hpp"
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <Util.hpp>
|
2019-11-07 09:24:37 -06:00
|
|
|
#include "ProofKey.hpp"
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <common/FileUtil.hpp>
|
2018-02-17 16:32:41 -06:00
|
|
|
#include <common/JsonUtil.hpp>
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-06-07 02:18:49 -05:00
|
|
|
bool StorageBase::FilesystemEnabled;
|
|
|
|
bool StorageBase::WopiEnabled;
|
2020-04-29 14:24:33 -05:00
|
|
|
bool StorageBase::SSLAsScheme = true;
|
|
|
|
bool StorageBase::SSLEnabled = false;
|
2016-06-07 02:18:49 -05:00
|
|
|
Util::RegexListMatcher StorageBase::WopiHosts;
|
2016-04-16 07:13:59 -05:00
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-09-13 11:16:00 -05:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
std::string StorageBase::getLocalRootPath() const
|
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
std::string localPath = _jailPath;
|
2016-03-31 01:48:34 -05:00
|
|
|
if (localPath[0] == '/')
|
|
|
|
{
|
|
|
|
// Remove the leading /
|
|
|
|
localPath.erase(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// /chroot/jailId/user/doc/childId
|
2018-02-07 03:17:59 -06:00
|
|
|
const Poco::Path rootPath = Poco::Path(_localStorePath, localPath);
|
2016-03-31 01:48:34 -05:00
|
|
|
Poco::File(rootPath).createDirectories();
|
|
|
|
|
|
|
|
return rootPath.toString();
|
|
|
|
}
|
|
|
|
|
2018-09-13 11:16:00 -05:00
|
|
|
#endif
|
|
|
|
|
2016-04-16 07:13:59 -05:00
|
|
|
void StorageBase::initialize()
|
|
|
|
{
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-10-30 14:52:26 -05:00
|
|
|
const auto& app = Poco::Util::Application::instance();
|
2016-06-07 02:18:49 -05:00
|
|
|
FilesystemEnabled = app.config().getBool("storage.filesystem[@allow]", false);
|
2016-04-16 07:13:59 -05:00
|
|
|
|
|
|
|
// Parse the WOPI settings.
|
2016-06-07 02:18:49 -05:00
|
|
|
WopiHosts.clear();
|
|
|
|
WopiEnabled = app.config().getBool("storage.wopi[@allow]", false);
|
|
|
|
if (WopiEnabled)
|
2016-04-16 07:13:59 -05:00
|
|
|
{
|
|
|
|
for (size_t i = 0; ; ++i)
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
const std::string path = "storage.wopi.host[" + std::to_string(i) + ']';
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string host = app.config().getString(path, "");
|
2016-04-16 17:12:10 -05:00
|
|
|
if (!host.empty())
|
2016-04-16 07:13:59 -05:00
|
|
|
{
|
2016-04-16 17:12:10 -05:00
|
|
|
if (app.config().getBool(path + "[@allow]", false))
|
2016-04-16 07:13:59 -05:00
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Adding trusted WOPI host: [" << host << "].");
|
2016-06-07 02:18:49 -05:00
|
|
|
WopiHosts.allow(host);
|
2016-04-16 17:12:10 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Adding blocked WOPI host: [" << host << "].");
|
2016-06-07 02:18:49 -05:00
|
|
|
WopiHosts.deny(host);
|
2016-04-16 07:13:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!app.config().has(path))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-27 12:47:35 -05:00
|
|
|
|
|
|
|
#if ENABLE_SSL
|
|
|
|
// FIXME: should use our own SSL socket implementation here.
|
|
|
|
Poco::Crypto::initializeCrypto();
|
|
|
|
Poco::Net::initializeSSL();
|
|
|
|
|
|
|
|
// Init client
|
|
|
|
Poco::Net::Context::Params sslClientParams;
|
|
|
|
|
2020-04-29 14:24:33 -05:00
|
|
|
// false default for upgrade to preserve legacy configuration
|
|
|
|
// in-config-file defaults are true.
|
|
|
|
SSLAsScheme = LOOLWSD::getConfigValue<bool>("storage.ssl.as_scheme", false);
|
|
|
|
|
2019-10-24 11:00:07 -05:00
|
|
|
// Fallback to ssl.enable if not set - for back compatibility & simplicity.
|
|
|
|
SSLEnabled = LOOLWSD::getConfigValue<bool>(
|
|
|
|
"storage.ssl.enable", LOOLWSD::getConfigValue<bool>("ssl.enable", true));
|
|
|
|
|
2019-10-07 06:51:30 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
char *StorageSSLEnabled = getenv("STORAGE_SSL_ENABLE");
|
|
|
|
if (StorageSSLEnabled != NULL)
|
|
|
|
{
|
|
|
|
if (!strcasecmp(StorageSSLEnabled, "true"))
|
|
|
|
SSLEnabled = true;
|
|
|
|
else if (!strcasecmp(StorageSSLEnabled, "false"))
|
|
|
|
SSLEnabled = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (SSLEnabled)
|
|
|
|
{
|
|
|
|
sslClientParams.certificateFile = LOOLWSD::getPathFromConfigWithFallback("storage.ssl.cert_file_path", "ssl.cert_file_path");
|
|
|
|
sslClientParams.privateKeyFile = LOOLWSD::getPathFromConfigWithFallback("storage.ssl.key_file_path", "ssl.key_file_path");
|
|
|
|
sslClientParams.caLocation = LOOLWSD::getPathFromConfigWithFallback("storage.ssl.ca_file_path", "ssl.ca_file_path");
|
|
|
|
sslClientParams.cipherList = LOOLWSD::getPathFromConfigWithFallback("storage.ssl.cipher_list", "ssl.cipher_list");
|
|
|
|
|
|
|
|
sslClientParams.verificationMode = (sslClientParams.caLocation.empty() ? Poco::Net::Context::VERIFY_NONE : Poco::Net::Context::VERIFY_STRICT);
|
2019-10-18 12:21:09 -05:00
|
|
|
sslClientParams.loadDefaultCAs = true;
|
2019-10-07 06:51:30 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
sslClientParams.verificationMode = Poco::Net::Context::VERIFY_NONE;
|
2017-04-27 12:47:35 -05:00
|
|
|
|
|
|
|
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler> consoleClientHandler = new Poco::Net::KeyConsoleHandler(false);
|
|
|
|
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> invalidClientCertHandler = new Poco::Net::AcceptCertificateHandler(false);
|
|
|
|
|
|
|
|
Poco::Net::Context::Ptr sslClientContext = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslClientParams);
|
2019-10-18 12:21:09 -05:00
|
|
|
sslClientContext->disableProtocols(Poco::Net::Context::Protocols::PROTO_SSLV2 |
|
|
|
|
Poco::Net::Context::Protocols::PROTO_SSLV3 |
|
|
|
|
Poco::Net::Context::Protocols::PROTO_TLSV1);
|
2017-04-27 12:47:35 -05:00
|
|
|
Poco::Net::SSLManager::instance().initializeClient(consoleClientHandler, invalidClientCertHandler, sslClientContext);
|
|
|
|
#endif
|
2018-09-13 11:16:00 -05:00
|
|
|
#else
|
|
|
|
FilesystemEnabled = true;
|
|
|
|
#endif
|
2016-04-16 07:13:59 -05:00
|
|
|
}
|
|
|
|
|
2018-10-20 06:26:38 -05:00
|
|
|
bool StorageBase::allowedWopiHost(const std::string& host)
|
2018-10-20 05:57:53 -05:00
|
|
|
{
|
2018-10-20 06:26:38 -05:00
|
|
|
return WopiEnabled && WopiHosts.match(host);
|
2018-10-20 05:57:53 -05:00
|
|
|
}
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-09-05 07:11:05 -05:00
|
|
|
|
2017-05-19 03:32:24 -05:00
|
|
|
bool isLocalhost(const std::string& targetHost)
|
2016-04-19 03:17:50 -05:00
|
|
|
{
|
2016-04-19 08:32:22 -05:00
|
|
|
std::string targetAddress;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
targetAddress = Poco::Net::DNS::resolveOne(targetHost).toString();
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2016-12-22 15:41:05 -06:00
|
|
|
LOG_WRN("Poco::Net::DNS::resolveOne(\"" << targetHost << "\") failed: " << exc.displayText());
|
2016-04-19 08:32:22 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
targetAddress = Poco::Net::IPAddress(targetHost).toString();
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc1)
|
|
|
|
{
|
2016-12-22 15:41:05 -06:00
|
|
|
LOG_WRN("Poco::Net::IPAddress(\"" << targetHost << "\") failed: " << exc1.displayText());
|
2016-04-19 08:32:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 03:17:50 -05:00
|
|
|
Poco::Net::NetworkInterface::NetworkInterfaceList list = Poco::Net::NetworkInterface::list(true,true);
|
2016-04-28 01:43:19 -05:00
|
|
|
for (auto& netif : list)
|
2016-04-19 03:17:50 -05:00
|
|
|
{
|
|
|
|
std::string address = netif.address().toString();
|
2016-04-26 02:34:32 -05:00
|
|
|
address = address.substr(0, address.find('%', 0));
|
2016-04-19 03:17:50 -05:00
|
|
|
if (address == targetAddress)
|
2016-04-19 08:32:22 -05:00
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("WOPI host is on the same host as the WOPI client: \"" <<
|
|
|
|
targetAddress << "\". Connection is allowed.");
|
2016-04-19 03:17:50 -05:00
|
|
|
return true;
|
2016-04-19 08:32:22 -05:00
|
|
|
}
|
2016-04-19 03:17:50 -05:00
|
|
|
}
|
2016-11-03 19:26:40 -05:00
|
|
|
|
|
|
|
LOG_INF("WOPI host is not on the same host as the WOPI client: \"" <<
|
|
|
|
targetAddress << "\". Connection is not allowed.");
|
2016-04-19 03:17:50 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
|
|
|
|
2016-10-14 05:09:43 -05:00
|
|
|
std::unique_ptr<StorageBase> StorageBase::create(const Poco::URI& uri, const std::string& jailRoot, const std::string& jailPath)
|
2016-04-07 15:59:27 -05:00
|
|
|
{
|
Attempt to handle unauthorized WOPI usage better
Use the previously unused UnauthorizedRequestException for this, and
throw a such in StorageBase::create() when the WOPI host doesn't match
any of those configured.
In a developer debug build, without access to any real WOPI
functionality, you can test by setting the FAKE_UNAUTHORIZED
environment variable and attempting to edit a plain local file:
URI. That will cause such an exception to be thrown in that function.
Catch that UnauthorizedRequestException in
ClientRequestHandler::handleGetRequest(), and send an 'error:
cmd=internal kind=unauthorized' message to the client. Handle that in
loleaflet in the same place where the 'error: cmd=internal
kild=diskfull' message is handled, and in the same fashion, giving up
on the document.
Actually, using exceptions for relatively non-exceptional situations
like this is lame and makes understanding the code harder, but that is
just my personal preference...
FIXME: By the time StorageBase::create() gets called we have already
sent three 'statusindicator:' messages ('find', 'connect', and
'ready') to the client. We should ideally do the checks we do in
StorageBase::create() much earlier.
Also consider that ClientRequestHandler::handleClientRequest() has
code that catches UnauthorizedRequestException and
BadRequestException, and tries to set the HTTP response in those
cases. I am not sure if that functionality has ever been exercised,
though. Currently, we upgrade the HTTP connection to WebSocket early,
and only after that we check whether the WOPI host is authorized
etc. By that time it is too late to return an HTTP response to the
user. If that even is what we ideally should do? If not, then we
probably should drop the code that constructs HTTP responses and
attempts to send them.
Also, if I, as a test, force an HTTPResponse::HTTP_BAD_REQUEST to be
sent before the HTTP connection is upgraded to WebSocket, loleaflet
throws up the generic "Well, this is embarrassing" dialog anyway. At
least in Firefox on Linux. (Instead of the browser showing some own
dialog, which I was half-expecting to happen.)
2016-10-17 08:55:20 -05:00
|
|
|
// FIXME: By the time this gets called we have already sent to the client three
|
|
|
|
// 'statusindicator:' messages: 'find', 'connect' and 'ready'. We should ideally do the checks
|
|
|
|
// here much earlier. Also, using exceptions is lame and makes understanding the code harder,
|
|
|
|
// but that is just my personal preference.
|
|
|
|
|
2016-04-07 15:59:27 -05:00
|
|
|
std::unique_ptr<StorageBase> storage;
|
|
|
|
|
2016-10-14 05:09:43 -05:00
|
|
|
if (UnitWSD::get().createStorage(uri, jailRoot, jailPath, storage))
|
2016-04-16 07:13:59 -05:00
|
|
|
{
|
2020-06-22 07:24:11 -05:00
|
|
|
LOG_INF("Storage create hooked.");
|
2016-08-31 10:05:10 -05:00
|
|
|
if (storage)
|
2016-11-03 19:26:40 -05:00
|
|
|
{
|
2016-08-31 10:05:10 -05:00
|
|
|
return storage;
|
2016-11-03 19:26:40 -05:00
|
|
|
}
|
2016-04-16 07:13:59 -05:00
|
|
|
}
|
2016-04-07 15:59:27 -05:00
|
|
|
else if (uri.isRelative() || uri.getScheme() == "file")
|
|
|
|
{
|
2018-06-15 08:54:44 -05:00
|
|
|
LOG_INF("Public URI [" << LOOLWSD::anonymizeUrl(uri.toString()) << "] is a file.");
|
2016-11-03 19:26:40 -05:00
|
|
|
|
Attempt to handle unauthorized WOPI usage better
Use the previously unused UnauthorizedRequestException for this, and
throw a such in StorageBase::create() when the WOPI host doesn't match
any of those configured.
In a developer debug build, without access to any real WOPI
functionality, you can test by setting the FAKE_UNAUTHORIZED
environment variable and attempting to edit a plain local file:
URI. That will cause such an exception to be thrown in that function.
Catch that UnauthorizedRequestException in
ClientRequestHandler::handleGetRequest(), and send an 'error:
cmd=internal kind=unauthorized' message to the client. Handle that in
loleaflet in the same place where the 'error: cmd=internal
kild=diskfull' message is handled, and in the same fashion, giving up
on the document.
Actually, using exceptions for relatively non-exceptional situations
like this is lame and makes understanding the code harder, but that is
just my personal preference...
FIXME: By the time StorageBase::create() gets called we have already
sent three 'statusindicator:' messages ('find', 'connect', and
'ready') to the client. We should ideally do the checks we do in
StorageBase::create() much earlier.
Also consider that ClientRequestHandler::handleClientRequest() has
code that catches UnauthorizedRequestException and
BadRequestException, and tries to set the HTTP response in those
cases. I am not sure if that functionality has ever been exercised,
though. Currently, we upgrade the HTTP connection to WebSocket early,
and only after that we check whether the WOPI host is authorized
etc. By that time it is too late to return an HTTP response to the
user. If that even is what we ideally should do? If not, then we
probably should drop the code that constructs HTTP responses and
attempts to send them.
Also, if I, as a test, force an HTTPResponse::HTTP_BAD_REQUEST to be
sent before the HTTP connection is upgraded to WebSocket, loleaflet
throws up the generic "Well, this is embarrassing" dialog anyway. At
least in Firefox on Linux. (Instead of the browser showing some own
dialog, which I was half-expecting to happen.)
2016-10-17 08:55:20 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
if (std::getenv("FAKE_UNAUTHORIZED"))
|
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_FTL("Faking an UnauthorizedRequestException");
|
Attempt to handle unauthorized WOPI usage better
Use the previously unused UnauthorizedRequestException for this, and
throw a such in StorageBase::create() when the WOPI host doesn't match
any of those configured.
In a developer debug build, without access to any real WOPI
functionality, you can test by setting the FAKE_UNAUTHORIZED
environment variable and attempting to edit a plain local file:
URI. That will cause such an exception to be thrown in that function.
Catch that UnauthorizedRequestException in
ClientRequestHandler::handleGetRequest(), and send an 'error:
cmd=internal kind=unauthorized' message to the client. Handle that in
loleaflet in the same place where the 'error: cmd=internal
kild=diskfull' message is handled, and in the same fashion, giving up
on the document.
Actually, using exceptions for relatively non-exceptional situations
like this is lame and makes understanding the code harder, but that is
just my personal preference...
FIXME: By the time StorageBase::create() gets called we have already
sent three 'statusindicator:' messages ('find', 'connect', and
'ready') to the client. We should ideally do the checks we do in
StorageBase::create() much earlier.
Also consider that ClientRequestHandler::handleClientRequest() has
code that catches UnauthorizedRequestException and
BadRequestException, and tries to set the HTTP response in those
cases. I am not sure if that functionality has ever been exercised,
though. Currently, we upgrade the HTTP connection to WebSocket early,
and only after that we check whether the WOPI host is authorized
etc. By that time it is too late to return an HTTP response to the
user. If that even is what we ideally should do? If not, then we
probably should drop the code that constructs HTTP responses and
attempts to send them.
Also, if I, as a test, force an HTTPResponse::HTTP_BAD_REQUEST to be
sent before the HTTP connection is upgraded to WebSocket, loleaflet
throws up the generic "Well, this is embarrassing" dialog anyway. At
least in Firefox on Linux. (Instead of the browser showing some own
dialog, which I was half-expecting to happen.)
2016-10-17 08:55:20 -05:00
|
|
|
throw UnauthorizedRequestException("No acceptable WOPI hosts found matching the target host in config.");
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-07 02:18:49 -05:00
|
|
|
if (FilesystemEnabled)
|
2016-04-07 15:59:27 -05:00
|
|
|
{
|
2016-10-14 05:09:43 -05:00
|
|
|
return std::unique_ptr<StorageBase>(new LocalStorage(uri, jailRoot, jailPath));
|
2016-04-07 15:59:27 -05:00
|
|
|
}
|
2017-08-02 11:55:43 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// guard against attempts to escape
|
|
|
|
Poco::URI normalizedUri(uri);
|
|
|
|
normalizedUri.normalize();
|
|
|
|
|
|
|
|
std::vector<std::string> pathSegments;
|
|
|
|
normalizedUri.getPathSegments(pathSegments);
|
|
|
|
|
|
|
|
if (pathSegments.size() == 4 && pathSegments[0] == "tmp" && pathSegments[1] == "convert-to")
|
|
|
|
{
|
2018-06-15 08:54:44 -05:00
|
|
|
LOG_INF("Public URI [" << LOOLWSD::anonymizeUrl(normalizedUri.toString()) << "] is actually a convert-to tempfile.");
|
2017-08-02 11:55:43 -05:00
|
|
|
return std::unique_ptr<StorageBase>(new LocalStorage(normalizedUri, jailRoot, jailPath));
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 15:59:27 -05:00
|
|
|
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_ERR("Local Storage is disabled by default. Enable in the config file or on the command-line to enable.");
|
2016-04-07 15:59:27 -05:00
|
|
|
}
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-06-07 02:18:49 -05:00
|
|
|
else if (WopiEnabled)
|
2016-04-07 15:59:27 -05:00
|
|
|
{
|
2018-06-15 08:54:44 -05:00
|
|
|
LOG_INF("Public URI [" << LOOLWSD::anonymizeUrl(uri.toString()) << "] considered WOPI.");
|
2016-04-26 02:34:32 -05:00
|
|
|
const auto& targetHost = uri.getHost();
|
2020-10-14 13:52:49 -05:00
|
|
|
bool allowed(false);
|
2017-05-19 03:32:24 -05:00
|
|
|
if (WopiHosts.match(targetHost) || isLocalhost(targetHost))
|
2016-04-16 11:55:56 -05:00
|
|
|
{
|
2020-10-14 13:52:49 -05:00
|
|
|
allowed = true;
|
|
|
|
}
|
|
|
|
if (!allowed)
|
|
|
|
{
|
|
|
|
// check if the IP address is in the list of allowed hosts
|
|
|
|
const auto hostAddresses(Poco::Net::DNS::resolve(targetHost));
|
|
|
|
for (auto &address : hostAddresses.addresses())
|
|
|
|
{
|
|
|
|
if (WopiHosts.match(address.toString()))
|
|
|
|
{
|
|
|
|
allowed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-16 11:55:56 -05:00
|
|
|
}
|
2020-10-14 13:52:49 -05:00
|
|
|
if (allowed)
|
|
|
|
return std::unique_ptr<StorageBase>(new WopiStorage(uri, jailRoot, jailPath));
|
2018-02-01 06:49:28 -06:00
|
|
|
LOG_ERR("No acceptable WOPI hosts found matching the target host [" << targetHost << "] in config.");
|
Attempt to handle unauthorized WOPI usage better
Use the previously unused UnauthorizedRequestException for this, and
throw a such in StorageBase::create() when the WOPI host doesn't match
any of those configured.
In a developer debug build, without access to any real WOPI
functionality, you can test by setting the FAKE_UNAUTHORIZED
environment variable and attempting to edit a plain local file:
URI. That will cause such an exception to be thrown in that function.
Catch that UnauthorizedRequestException in
ClientRequestHandler::handleGetRequest(), and send an 'error:
cmd=internal kind=unauthorized' message to the client. Handle that in
loleaflet in the same place where the 'error: cmd=internal
kild=diskfull' message is handled, and in the same fashion, giving up
on the document.
Actually, using exceptions for relatively non-exceptional situations
like this is lame and makes understanding the code harder, but that is
just my personal preference...
FIXME: By the time StorageBase::create() gets called we have already
sent three 'statusindicator:' messages ('find', 'connect', and
'ready') to the client. We should ideally do the checks we do in
StorageBase::create() much earlier.
Also consider that ClientRequestHandler::handleClientRequest() has
code that catches UnauthorizedRequestException and
BadRequestException, and tries to set the HTTP response in those
cases. I am not sure if that functionality has ever been exercised,
though. Currently, we upgrade the HTTP connection to WebSocket early,
and only after that we check whether the WOPI host is authorized
etc. By that time it is too late to return an HTTP response to the
user. If that even is what we ideally should do? If not, then we
probably should drop the code that constructs HTTP responses and
attempts to send them.
Also, if I, as a test, force an HTTPResponse::HTTP_BAD_REQUEST to be
sent before the HTTP connection is upgraded to WebSocket, loleaflet
throws up the generic "Well, this is embarrassing" dialog anyway. At
least in Firefox on Linux. (Instead of the browser showing some own
dialog, which I was half-expecting to happen.)
2016-10-17 08:55:20 -05:00
|
|
|
throw UnauthorizedRequestException("No acceptable WOPI hosts found matching the target host [" + targetHost + "] in config.");
|
2016-04-16 07:13:59 -05:00
|
|
|
}
|
2018-09-05 07:11:05 -05:00
|
|
|
#endif
|
2016-04-16 11:55:56 -05:00
|
|
|
throw BadRequestException("No Storage configured or invalid URI.");
|
2016-04-07 15:59:27 -05:00
|
|
|
}
|
|
|
|
|
2016-10-26 07:47:42 -05:00
|
|
|
std::atomic<unsigned> LocalStorage::LastLocalStorageId;
|
2016-09-30 04:02:09 -05:00
|
|
|
|
2017-05-12 10:42:03 -05:00
|
|
|
std::unique_ptr<LocalStorage::LocalFileInfo> LocalStorage::getLocalFileInfo()
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2020-11-15 08:25:24 -06:00
|
|
|
const Poco::Path path = getUri().getPath();
|
|
|
|
LOG_DBG("Getting info for local uri [" << LOOLWSD::anonymizeUrl(getUri().toString())
|
|
|
|
<< "], path [" << LOOLWSD::anonymizeUrl(path.toString())
|
|
|
|
<< "].");
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2020-11-15 08:25:24 -06:00
|
|
|
const FileUtil::Stat stat(path.toString());
|
|
|
|
const std::chrono::system_clock::time_point lastModified = stat.modifiedTimepoint();
|
|
|
|
const std::size_t size = stat.size();
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2020-11-15 08:25:24 -06:00
|
|
|
setFileInfo(FileInfo(path.getFileName(), "LocalOwner", lastModified, size));
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2020-11-15 08:25:24 -06:00
|
|
|
// Set automatic userid and username.
|
|
|
|
const std::string userId = std::to_string(LastLocalStorageId++);
|
|
|
|
return std::unique_ptr<LocalStorage::LocalFileInfo>(
|
|
|
|
new LocalFileInfo("LocalUser" + userId, "LocalUser#" + userId));
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:04:26 -06:00
|
|
|
std::string LocalStorage::loadStorageFileToLocal(const Authorization& /*auth*/,
|
|
|
|
const std::string& /*cookies*/,
|
|
|
|
LockContext& /*lockCtx*/,
|
|
|
|
const std::string& /*templateUri*/)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-03-31 01:48:34 -05:00
|
|
|
// /chroot/jailId/user/doc/childId/file.ext
|
2018-11-28 02:07:29 -06:00
|
|
|
const std::string filename = Poco::Path(getUri().getPath()).getFileName();
|
|
|
|
setRootFilePath(Poco::Path(getLocalRootPath(), filename).toString());
|
|
|
|
setRootFilePathAnonym(LOOLWSD::anonymizeUrl(getRootFilePath()));
|
|
|
|
LOG_INF("Public URI [" << LOOLWSD::anonymizeUrl(getUri().getPath()) <<
|
|
|
|
"] jailed to [" << getRootFilePathAnonym() << "].");
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-09-30 08:09:53 -05:00
|
|
|
// Despite the talk about URIs it seems that _uri is actually just a pathname here
|
2018-11-28 02:07:29 -06:00
|
|
|
const std::string publicFilePath = getUri().getPath();
|
2016-09-30 08:09:53 -05:00
|
|
|
|
2018-11-28 02:07:29 -06:00
|
|
|
if (!FileUtil::checkDiskSpace(getRootFilePath()))
|
2016-11-03 19:26:40 -05:00
|
|
|
{
|
2018-11-28 02:07:29 -06:00
|
|
|
throw StorageSpaceLowException("Low disk space for " + getRootFilePathAnonym());
|
2016-11-03 19:26:40 -05:00
|
|
|
}
|
2016-09-30 08:09:53 -05:00
|
|
|
|
2018-11-28 02:07:29 -06:00
|
|
|
LOG_INF("Linking " << LOOLWSD::anonymizeUrl(publicFilePath) << " to " << getRootFilePathAnonym());
|
|
|
|
if (!Poco::File(getRootFilePath()).exists() && link(publicFilePath.c_str(), getRootFilePath().c_str()) == -1)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
|
|
|
// Failed
|
2020-11-15 08:25:24 -06:00
|
|
|
LOG_WRN("link(\"" << LOOLWSD::anonymizeUrl(publicFilePath) << "\", \""
|
|
|
|
<< getRootFilePathAnonym() << "\") failed. Will copy. Linking error: "
|
|
|
|
<< Util::symbolicErrno(errno) << ' ' << strerror(errno));
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Fallback to copying.
|
2018-11-28 02:07:29 -06:00
|
|
|
if (!Poco::File(getRootFilePath()).exists())
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2020-01-18 09:56:01 -06:00
|
|
|
FileUtil::copyFileTo(publicFilePath, getRootFilePath());
|
2016-03-31 01:48:34 -05:00
|
|
|
_isCopy = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2020-11-15 08:25:24 -06:00
|
|
|
LOG_ERR("copyTo(\"" << LOOLWSD::anonymizeUrl(publicFilePath) << "\", \""
|
|
|
|
<< getRootFilePathAnonym() << "\") failed: " << exc.displayText());
|
2016-03-31 01:48:34 -05:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2018-11-28 02:07:29 -06:00
|
|
|
setLoaded(true);
|
2016-03-31 01:48:34 -05:00
|
|
|
// Now return the jailed path.
|
2017-02-06 16:26:38 -06:00
|
|
|
#ifndef KIT_IN_PROCESS
|
2017-04-05 13:20:47 -05:00
|
|
|
if (LOOLWSD::NoCapsForKit)
|
2018-11-28 02:07:29 -06:00
|
|
|
return getRootFilePath();
|
2017-04-05 13:20:47 -05:00
|
|
|
else
|
2018-11-28 02:07:29 -06:00
|
|
|
return Poco::Path(getJailPath(), filename).toString();
|
2017-02-06 16:26:38 -06:00
|
|
|
#else
|
2018-11-28 02:07:29 -06:00
|
|
|
return getRootFilePath();
|
2017-02-06 16:26:38 -06:00
|
|
|
#endif
|
2018-09-13 11:16:00 -05:00
|
|
|
|
|
|
|
#else // MOBILEAPP
|
|
|
|
|
|
|
|
// In the mobile app we use no jail
|
2018-11-28 04:40:52 -06:00
|
|
|
setRootFilePath(getUri().getPath());
|
|
|
|
setLoaded(true);
|
2018-09-13 11:16:00 -05:00
|
|
|
|
2018-11-28 04:40:52 -06:00
|
|
|
return getRootFilePath();
|
2018-09-13 11:16:00 -05:00
|
|
|
#endif
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:04:26 -06:00
|
|
|
StorageBase::SaveResult
|
|
|
|
LocalStorage::saveLocalFileToStorage(const Authorization& /*auth*/, const std::string& /*cookies*/,
|
|
|
|
LockContext& /*lockCtx*/, const std::string& /*saveAsPath*/,
|
|
|
|
const std::string& /*saveAsFilename*/, bool /*isRename*/)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2020-11-15 08:25:24 -06:00
|
|
|
const std::string path = getUri().getPath();
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
try
|
|
|
|
{
|
2020-11-15 08:25:24 -06:00
|
|
|
LOG_TRC("Saving local file to local file storage (isCopy: " << _isCopy << ") for "
|
|
|
|
<< getRootFilePathAnonym());
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
// Copy the file back.
|
2018-11-28 02:07:29 -06:00
|
|
|
if (_isCopy && Poco::File(getRootFilePath()).exists())
|
2020-11-15 08:25:24 -06:00
|
|
|
FileUtil::copyFileTo(getRootFilePath(), path);
|
2017-06-10 09:45:00 -05:00
|
|
|
|
|
|
|
// update its fileinfo object. This is used later to check if someone else changed the
|
|
|
|
// document while we are/were editing it
|
2020-11-15 08:25:24 -06:00
|
|
|
getFileInfo().setModifiedTime(FileUtil::Stat(path).modifiedTimepoint());
|
2018-11-29 01:42:43 -06:00
|
|
|
LOG_TRC("New FileInfo modified time in storage " << getFileInfo().getModifiedTime());
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2020-11-15 08:33:26 -06:00
|
|
|
LOG_ERR("copyTo(\"" << getRootFilePathAnonym() << "\", \"" << LOOLWSD::anonymizeUrl(path)
|
|
|
|
<< "\") failed: " << exc.displayText());
|
|
|
|
return StorageBase::SaveResult::Result::FAILED;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2020-11-15 08:33:26 -06:00
|
|
|
return StorageBase::SaveResult::Result::OK;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
Start on a gtk+-based workalike to the iOS app
The idea is that it would work sufficiently identically, so that even
people without a Mac and without an iOS device could participate in
development of the non-iOS-specific bits, like the JavaScript, or the
online MOBILEAPP-specific plumbing. Which would be great.
No, this doesn't do anything sane yet. It does compile the same online
C++ files as the iOS app, though. (Some minor tweaks were needed in a
couple of them to silence gcc warnings.)
There is a plain Makefile, but I should change to using autofoo, too.
Eventually, this will need to be built in a separate tree from a
normal online, just like when using the --enable-iosapp configure
switch. (But for now, doesn't matter.)
Change-Id: I13e4d921acb99d802d2f9da4b0df4a237ca60ad6
2018-10-16 16:40:49 -05:00
|
|
|
|
2019-10-07 06:51:30 -05:00
|
|
|
Poco::Net::HTTPClientSession* StorageBase::getHTTPClientSession(const Poco::URI& uri)
|
2020-04-29 14:24:33 -05:00
|
|
|
{
|
|
|
|
bool useSSL = false;
|
|
|
|
if (SSLAsScheme)
|
|
|
|
{
|
|
|
|
// the WOPI URI itself should control whether we use SSL or not
|
|
|
|
// for whether we verify vs. certificates, cf. above
|
|
|
|
useSSL = uri.getScheme() != "http";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We decoupled the Wopi communication from client communication because
|
|
|
|
// the Wopi communication must have an independent policy.
|
|
|
|
// So, we will use here only Storage settings.
|
|
|
|
useSSL = SSLEnabled || LOOLWSD::isSSLTermination();
|
|
|
|
}
|
2019-10-09 07:22:57 -05:00
|
|
|
// We decoupled the Wopi communication from client communication because
|
2019-10-07 06:51:30 -05:00
|
|
|
// the Wopi communication must have an independent policy.
|
|
|
|
// So, we will use here only Storage settings.
|
2020-07-16 09:44:41 -05:00
|
|
|
Poco::Net::HTTPClientSession* session = useSSL
|
2020-04-29 14:24:33 -05:00
|
|
|
? new Poco::Net::HTTPSClientSession(uri.getHost(), uri.getPort(),
|
|
|
|
Poco::Net::SSLManager::instance().defaultClientContext())
|
|
|
|
: new Poco::Net::HTTPClientSession(uri.getHost(), uri.getPort());
|
2020-07-16 09:44:41 -05:00
|
|
|
|
|
|
|
// Set the timeout to the configured value.
|
|
|
|
static int timeoutSec = LOOLWSD::getConfigValue<int>("net.connection_timeout_secs", 30);
|
|
|
|
session->setTimeout(Poco::Timespan(timeoutSec, 0));
|
|
|
|
|
|
|
|
return session;
|
2020-04-29 14:24:33 -05:00
|
|
|
}
|
2019-10-09 05:17:33 -05:00
|
|
|
|
2018-02-17 16:32:41 -06:00
|
|
|
namespace
|
|
|
|
{
|
2016-07-18 06:45:36 -05:00
|
|
|
|
2019-08-26 20:16:54 -05:00
|
|
|
static void addStorageDebugCookie(Poco::Net::HTTPRequest& request)
|
2017-05-17 03:48:51 -05:00
|
|
|
{
|
2017-05-17 06:13:24 -05:00
|
|
|
(void) request;
|
2017-05-17 03:48:51 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
if (std::getenv("LOOL_STORAGE_COOKIE"))
|
|
|
|
{
|
|
|
|
Poco::Net::NameValueCollection nvcCookies;
|
2020-05-21 09:22:49 -05:00
|
|
|
StringVector cookieTokens = Util::tokenize(std::string(std::getenv("LOOL_STORAGE_COOKIE")), ':');
|
2017-05-17 03:48:51 -05:00
|
|
|
if (cookieTokens.size() == 2)
|
|
|
|
{
|
|
|
|
nvcCookies.add(cookieTokens[0], cookieTokens[1]);
|
|
|
|
request.setCookies(nvcCookies);
|
2020-05-24 08:10:18 -05:00
|
|
|
LOG_TRC("Added storage debug cookie [" << cookieTokens[0] << '=' << cookieTokens[1] << "].");
|
2017-05-17 03:48:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-08-26 20:16:54 -05:00
|
|
|
static void addStorageReuseCookie(Poco::Net::HTTPRequest& request, const std::string& reuseStorageCookies)
|
2019-08-21 08:23:40 -05:00
|
|
|
{
|
2019-08-26 20:16:54 -05:00
|
|
|
if (!reuseStorageCookies.empty())
|
2019-08-21 08:23:40 -05:00
|
|
|
{
|
2020-05-21 09:22:49 -05:00
|
|
|
|
2019-08-21 08:23:40 -05:00
|
|
|
Poco::Net::NameValueCollection nvcCookies;
|
2020-01-18 15:04:26 -06:00
|
|
|
request.getCookies(nvcCookies); // Preserve existing cookies.
|
|
|
|
|
2020-05-21 09:22:49 -05:00
|
|
|
StringVector cookies = Util::tokenize(reuseStorageCookies, ':');
|
|
|
|
LOG_TRC("Parsing reuse cookies to set in Wopi request ["
|
|
|
|
<< reuseStorageCookies << "], found " << cookies.size() << " cookies.");
|
2019-08-21 08:23:40 -05:00
|
|
|
for (auto cookie : cookies)
|
|
|
|
{
|
2020-05-21 09:22:49 -05:00
|
|
|
StringVector cookieTokens = Util::tokenize(cookies.getParam(cookie), '=');
|
2019-08-21 08:23:40 -05:00
|
|
|
if (cookieTokens.size() == 2)
|
|
|
|
{
|
|
|
|
nvcCookies.add(cookieTokens[0], cookieTokens[1]);
|
2020-01-18 15:04:26 -06:00
|
|
|
LOG_DBG("Added storage reuse cookie [" << cookieTokens[0] << '=' << cookieTokens[1] << "].");
|
2019-08-21 08:23:40 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-18 15:04:26 -06:00
|
|
|
|
2019-08-21 08:23:40 -05:00
|
|
|
request.setCookies(nvcCookies);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 12:01:06 -06:00
|
|
|
// access_token must be decoded
|
2020-02-19 07:38:47 -06:00
|
|
|
void addWopiProof(Poco::Net::HTTPRequest& request, const Poco::URI& uri,
|
2020-02-14 12:01:06 -06:00
|
|
|
const std::string& access_token)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2020-02-19 07:38:47 -06:00
|
|
|
assert(!uri.isRelative());
|
|
|
|
for (const auto& header : GetProofHeaders(access_token, uri.toString()))
|
2019-11-07 09:24:37 -06:00
|
|
|
request.set(header.first, header.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, std::string> GetQueryParams(const Poco::URI& uri)
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string> result;
|
|
|
|
for (const auto& param : uri.getQueryParameters())
|
|
|
|
result.emplace(param);
|
|
|
|
return result;
|
2019-11-19 15:23:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2019-11-28 10:09:58 -06:00
|
|
|
#endif // !MOBILEAPP
|
|
|
|
|
2019-11-19 16:51:45 -06:00
|
|
|
void LockContext::initSupportsLocks()
|
|
|
|
{
|
2019-11-28 10:09:58 -06:00
|
|
|
#if MOBILEAPP
|
|
|
|
_supportsLocks = false;
|
|
|
|
#else
|
2019-11-19 16:51:45 -06:00
|
|
|
if (_supportsLocks)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// first time token setup
|
|
|
|
_supportsLocks = true;
|
|
|
|
_lockToken = "lool-lock" + Util::rng::getHexString(8);
|
2019-11-28 10:09:58 -06:00
|
|
|
#endif
|
2019-11-19 16:51:45 -06:00
|
|
|
}
|
|
|
|
|
2019-11-27 02:43:05 -06:00
|
|
|
bool LockContext::needsRefresh(const std::chrono::steady_clock::time_point &now) const
|
|
|
|
{
|
|
|
|
static int refreshSeconds = LOOLWSD::getConfigValue<int>("storage.wopi.locking.refresh", 900);
|
|
|
|
return _supportsLocks && _isLocked && refreshSeconds > 0 &&
|
|
|
|
std::chrono::duration_cast<std::chrono::seconds>
|
|
|
|
(now - _lastLockTime).count() >= refreshSeconds;
|
|
|
|
}
|
|
|
|
|
2019-12-16 01:33:16 -06:00
|
|
|
void LockContext::dumpState(std::ostream& os) const
|
2019-11-27 02:43:05 -06:00
|
|
|
{
|
|
|
|
if (!_supportsLocks)
|
|
|
|
return;
|
2020-05-24 08:10:18 -05:00
|
|
|
os << " lock:"
|
|
|
|
"\n locked: " << _isLocked;
|
|
|
|
os << "\n token: '" << _lockToken;
|
|
|
|
os << "\n last locked: " << Util::getSteadyClockAsString(_lastLockTime) << '\n';
|
2019-11-27 02:43:05 -06:00
|
|
|
}
|
|
|
|
|
2019-11-28 10:09:58 -06:00
|
|
|
#if !MOBILEAPP
|
|
|
|
|
2020-06-14 11:44:10 -05:00
|
|
|
void WopiStorage::initHttpRequest(Poco::Net::HTTPRequest& request, const Poco::URI& uri,
|
|
|
|
const Authorization& auth, const std::string& cookies) const
|
|
|
|
{
|
|
|
|
request.set("User-Agent", WOPI_AGENT_STRING);
|
|
|
|
|
|
|
|
auth.authorizeRequest(request);
|
|
|
|
|
|
|
|
addStorageDebugCookie(request);
|
|
|
|
|
|
|
|
// TODO: Avoid repeated parsing.
|
|
|
|
std::map<std::string, std::string> params = GetQueryParams(uri);
|
|
|
|
addWopiProof(request, uri, params["access_token"]);
|
|
|
|
|
|
|
|
if (_reuseCookies)
|
|
|
|
addStorageReuseCookie(request, cookies);
|
2020-10-08 12:20:41 -05:00
|
|
|
|
|
|
|
// Helps wrt. debugging cluster cases from the logs
|
|
|
|
request.set("X-LOOL-WOPI-ServerId", Util::getProcessIdentifier());
|
2020-06-14 11:44:10 -05:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:04:26 -06:00
|
|
|
std::unique_ptr<WopiStorage::WOPIFileInfo> WopiStorage::getWOPIFileInfo(const Authorization& auth,
|
|
|
|
const std::string& cookies,
|
|
|
|
LockContext& lockCtx)
|
2019-11-19 15:23:50 -06:00
|
|
|
{
|
|
|
|
// update the access_token to the one matching to the session
|
|
|
|
Poco::URI uriObject(getUri());
|
|
|
|
auth.authorizeURI(uriObject);
|
|
|
|
const std::string uriAnonym = LOOLWSD::anonymizeUrl(uriObject.toString());
|
2019-08-26 20:16:54 -05:00
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_DBG("Getting info for wopi uri [" << uriAnonym << "].");
|
2016-10-14 05:09:43 -05:00
|
|
|
|
2018-06-10 10:42:15 -05:00
|
|
|
std::string wopiResponse;
|
2016-12-23 01:53:50 -06:00
|
|
|
std::chrono::duration<double> callDuration(0);
|
|
|
|
try
|
|
|
|
{
|
2020-06-14 11:44:10 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET,
|
|
|
|
uriObject.getPathAndQuery(),
|
|
|
|
Poco::Net::HTTPMessage::HTTP_1_1);
|
|
|
|
initHttpRequest(request, uriObject, auth, cookies);
|
|
|
|
|
2018-06-10 10:42:15 -05:00
|
|
|
const auto startTime = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> psession(getHTTPClientSession(uriObject));
|
2020-04-20 02:07:25 -05:00
|
|
|
Log::StreamLogger logger = Log::trace();
|
|
|
|
if (logger.enabled())
|
|
|
|
{
|
|
|
|
logger << "WOPI::CheckFileInfo request header for URI [" << uriAnonym << "]:\n";
|
|
|
|
for (const auto& pair : request)
|
|
|
|
{
|
|
|
|
logger << '\t' << pair.first << ": " << pair.second << " / ";
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_END(logger, true);
|
|
|
|
}
|
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
psession->sendRequest(request);
|
2016-07-18 06:45:36 -05:00
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::istream& rs = psession->receiveResponse(response);
|
|
|
|
callDuration = (std::chrono::steady_clock::now() - startTime);
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2020-06-20 13:07:13 -05:00
|
|
|
Log::StreamLogger logRes = Log::trace();
|
|
|
|
if (logRes.enabled())
|
2016-12-23 01:53:50 -06:00
|
|
|
{
|
2020-06-20 13:07:13 -05:00
|
|
|
logRes << "WOPI::CheckFileInfo response header for URI [" << uriAnonym
|
|
|
|
<< "]: " << response.getStatus() << '\n';
|
2016-12-22 15:41:05 -06:00
|
|
|
for (const auto& pair : response)
|
|
|
|
{
|
2020-06-20 13:07:13 -05:00
|
|
|
logRes << '\t' << pair.first << ": " << pair.second << " / ";
|
2016-12-22 15:41:05 -06:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:07:13 -05:00
|
|
|
LOG_END(logRes, true);
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2017-05-18 13:05:45 -05:00
|
|
|
if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_OK)
|
|
|
|
{
|
2017-05-19 10:34:03 -05:00
|
|
|
LOG_ERR("WOPI::CheckFileInfo failed with " << response.getStatus() << ' ' << response.getReason());
|
2017-05-18 13:05:45 -05:00
|
|
|
throw StorageConnectionException("WOPI::CheckFileInfo failed");
|
|
|
|
}
|
|
|
|
|
2018-06-10 10:42:15 -05:00
|
|
|
Poco::StreamCopier::copyToString(rs, wopiResponse);
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2018-06-10 19:55:52 -05:00
|
|
|
catch (const Poco::Exception& pexc)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_ERR("Cannot get file info from WOPI storage uri [" << uriAnonym << "]. Error: " <<
|
2020-05-24 08:10:18 -05:00
|
|
|
pexc.displayText() << (pexc.nested() ? " (" + pexc.nested()->displayText() + ')' : ""));
|
2016-12-23 01:53:50 -06:00
|
|
|
throw;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
2019-12-23 07:45:38 -06:00
|
|
|
catch (const BadRequestException& exc)
|
|
|
|
{
|
2020-10-06 22:29:56 -05:00
|
|
|
LOG_ERR("Cannot get file info from WOPI storage uri [" << uriAnonym << "]. Error: " << exc.what());
|
2019-12-23 07:45:38 -06:00
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
Poco::JSON::Object::Ptr object;
|
2018-06-10 10:42:15 -05:00
|
|
|
if (JsonUtil::parseJSON(wopiResponse, object))
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2019-04-14 11:24:45 -05:00
|
|
|
if (LOOLWSD::AnonymizeUserData)
|
2018-08-01 21:04:33 -05:00
|
|
|
LOG_DBG("WOPI::CheckFileInfo (" << callDuration.count() * 1000. << " ms): anonymizing...");
|
|
|
|
else
|
|
|
|
LOG_DBG("WOPI::CheckFileInfo (" << callDuration.count() * 1000. << " ms): " << wopiResponse);
|
|
|
|
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t size = 0;
|
2020-06-11 09:54:27 -05:00
|
|
|
std::string filename, ownerId, lastModifiedTime;
|
|
|
|
|
|
|
|
JsonUtil::findJSONValue(object, "Size", size);
|
2018-02-17 16:32:41 -06:00
|
|
|
JsonUtil::findJSONValue(object, "OwnerId", ownerId);
|
2020-06-11 09:54:27 -05:00
|
|
|
JsonUtil::findJSONValue(object, "BaseFileName", filename);
|
|
|
|
JsonUtil::findJSONValue(object, "LastModifiedTime", lastModifiedTime);
|
|
|
|
|
|
|
|
const std::chrono::system_clock::time_point modifiedTime = Util::iso8601ToTimestamp(lastModifiedTime, "LastModifiedTime");
|
|
|
|
FileInfo fileInfo = FileInfo({filename, ownerId, modifiedTime, size});
|
|
|
|
setFileInfo(fileInfo);
|
2018-06-10 10:42:15 -05:00
|
|
|
|
2019-04-14 11:24:45 -05:00
|
|
|
if (LOOLWSD::AnonymizeUserData)
|
2018-11-28 02:07:29 -06:00
|
|
|
Util::mapAnonymized(Util::getFilenameFromURL(filename), Util::getFilenameFromURL(getUri().toString()));
|
2018-07-10 22:09:27 -05:00
|
|
|
|
2020-06-11 09:54:27 -05:00
|
|
|
auto wopiInfo = std::unique_ptr<WopiStorage::WOPIFileInfo>(new WOPIFileInfo(fileInfo, callDuration, object));
|
|
|
|
if (wopiInfo->getSupportsLocks())
|
|
|
|
lockCtx.initSupportsLocks();
|
2018-06-10 10:42:15 -05:00
|
|
|
|
2020-06-11 09:54:27 -05:00
|
|
|
return wopiInfo;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
2016-10-21 03:57:17 -05:00
|
|
|
else
|
2016-12-22 15:41:05 -06:00
|
|
|
{
|
2019-04-14 11:24:45 -05:00
|
|
|
if (LOOLWSD::AnonymizeUserData)
|
2018-08-01 21:04:33 -05:00
|
|
|
wopiResponse = "obfuscated";
|
|
|
|
|
|
|
|
LOG_ERR("WOPI::CheckFileInfo (" << callDuration.count() * 1000. <<
|
|
|
|
" ms) failed or no valid JSON payload returned. Access denied. "
|
|
|
|
"Original response: [" << wopiResponse << "].");
|
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
throw UnauthorizedRequestException("Access denied. WOPI::CheckFileInfo failed on: " + uriAnonym);
|
2016-12-22 15:41:05 -06:00
|
|
|
}
|
2020-06-11 09:54:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WopiStorage::WOPIFileInfo::init()
|
|
|
|
{
|
|
|
|
_userCanWrite = false;
|
|
|
|
_enableOwnerTermination = false;
|
|
|
|
_hidePrintOption = false;
|
|
|
|
_hideSaveOption = false;
|
|
|
|
_hideExportOption = false;
|
|
|
|
_disablePrint = false;
|
|
|
|
_disableExport = false;
|
|
|
|
_disableCopy = false;
|
|
|
|
_disableInactiveMessages = false;
|
|
|
|
_downloadAsPostMessage = false;
|
|
|
|
_userCanNotWriteRelative = true;
|
|
|
|
_enableInsertRemoteImage = false;
|
|
|
|
_enableShare = false;
|
|
|
|
_supportsLocks = false;
|
|
|
|
_supportsRename = false;
|
|
|
|
_userCanRename = false;
|
|
|
|
_hideUserList = "false";
|
|
|
|
_disableChangeTrackingRecord = WOPIFileInfo::TriState::Unset;
|
|
|
|
_disableChangeTrackingShow = WOPIFileInfo::TriState::Unset;
|
|
|
|
_hideChangeTrackingControls = WOPIFileInfo::TriState::Unset;
|
|
|
|
}
|
|
|
|
|
|
|
|
WopiStorage::WOPIFileInfo::WOPIFileInfo(const FileInfo &fileInfo,
|
|
|
|
std::chrono::duration<double> callDuration,
|
|
|
|
Poco::JSON::Object::Ptr &object)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
|
|
|
const std::string &filename = fileInfo.getFilename();
|
|
|
|
const std::string &ownerId = fileInfo.getOwnerId();
|
|
|
|
|
|
|
|
JsonUtil::findJSONValue(object, "UserId", _userId);
|
|
|
|
JsonUtil::findJSONValue(object, "UserFriendlyName", _username);
|
|
|
|
JsonUtil::findJSONValue(object, "TemplateSaveAs", _templateSaveAs);
|
|
|
|
JsonUtil::findJSONValue(object, "TemplateSource", _templateSource);
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2020-06-11 09:54:27 -05:00
|
|
|
std::ostringstream wopiResponse;
|
2017-05-19 03:32:24 -05:00
|
|
|
|
2020-06-11 09:54:27 -05:00
|
|
|
// Anonymize key values.
|
|
|
|
if (LOOLWSD::AnonymizeUserData)
|
|
|
|
{
|
|
|
|
JsonUtil::findJSONValue(object, "ObfuscatedUserId", _obfuscatedUserId, false);
|
|
|
|
if (!_obfuscatedUserId.empty())
|
|
|
|
{
|
|
|
|
Util::mapAnonymized(ownerId, _obfuscatedUserId);
|
|
|
|
Util::mapAnonymized(_userId, _obfuscatedUserId);
|
|
|
|
Util::mapAnonymized(_username, _obfuscatedUserId);
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::JSON::Object::Ptr anonObject(object);
|
|
|
|
|
|
|
|
// Set anonymized version of the above fields before logging.
|
|
|
|
// Note: anonymization caches the result, so we don't need to store here.
|
|
|
|
if (LOOLWSD::AnonymizeUserData)
|
|
|
|
anonObject->set("BaseFileName", LOOLWSD::anonymizeUrl(filename));
|
|
|
|
|
|
|
|
// If obfuscatedUserId is provided, then don't log the originals and use it.
|
|
|
|
if (LOOLWSD::AnonymizeUserData && _obfuscatedUserId.empty())
|
|
|
|
{
|
|
|
|
anonObject->set("OwnerId", LOOLWSD::anonymizeUsername(ownerId));
|
|
|
|
anonObject->set("UserId", LOOLWSD::anonymizeUsername(_userId));
|
|
|
|
anonObject->set("UserFriendlyName", LOOLWSD::anonymizeUsername(_username));
|
|
|
|
}
|
|
|
|
anonObject->stringify(wopiResponse);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
object->stringify(wopiResponse);
|
|
|
|
|
|
|
|
LOG_DBG("WOPI::CheckFileInfo (" << callDuration.count() * 1000. << " ms): " << wopiResponse.str());
|
|
|
|
|
|
|
|
JsonUtil::findJSONValue(object, "UserExtraInfo", _userExtraInfo);
|
|
|
|
JsonUtil::findJSONValue(object, "WatermarkText", _watermarkText);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanWrite", _userCanWrite);
|
|
|
|
JsonUtil::findJSONValue(object, "PostMessageOrigin", _postMessageOrigin);
|
|
|
|
JsonUtil::findJSONValue(object, "HidePrintOption", _hidePrintOption);
|
|
|
|
JsonUtil::findJSONValue(object, "HideSaveOption", _hideSaveOption);
|
|
|
|
JsonUtil::findJSONValue(object, "HideExportOption", _hideExportOption);
|
|
|
|
JsonUtil::findJSONValue(object, "EnableOwnerTermination", _enableOwnerTermination);
|
|
|
|
JsonUtil::findJSONValue(object, "DisablePrint", _disablePrint);
|
|
|
|
JsonUtil::findJSONValue(object, "DisableExport", _disableExport);
|
|
|
|
JsonUtil::findJSONValue(object, "DisableCopy", _disableCopy);
|
|
|
|
JsonUtil::findJSONValue(object, "DisableInactiveMessages", _disableInactiveMessages);
|
|
|
|
JsonUtil::findJSONValue(object, "DownloadAsPostMessage", _downloadAsPostMessage);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanNotWriteRelative", _userCanNotWriteRelative);
|
|
|
|
JsonUtil::findJSONValue(object, "EnableInsertRemoteImage", _enableInsertRemoteImage);
|
|
|
|
JsonUtil::findJSONValue(object, "EnableShare", _enableShare);
|
|
|
|
JsonUtil::findJSONValue(object, "HideUserList", _hideUserList);
|
|
|
|
JsonUtil::findJSONValue(object, "SupportsLocks", _supportsLocks);
|
|
|
|
JsonUtil::findJSONValue(object, "SupportsRename", _supportsRename);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanRename", _userCanRename);
|
2020-06-09 11:43:58 -05:00
|
|
|
JsonUtil::findJSONValue(object, "BreadcrumbDocName", _breadcrumbDocName);
|
2020-06-11 09:54:27 -05:00
|
|
|
bool booleanFlag = false;
|
|
|
|
if (JsonUtil::findJSONValue(object, "DisableChangeTrackingRecord", booleanFlag))
|
|
|
|
_disableChangeTrackingRecord = (booleanFlag ? WOPIFileInfo::TriState::True : WOPIFileInfo::TriState::False);
|
|
|
|
if (JsonUtil::findJSONValue(object, "DisableChangeTrackingShow", booleanFlag))
|
|
|
|
_disableChangeTrackingShow = (booleanFlag ? WOPIFileInfo::TriState::True : WOPIFileInfo::TriState::False);
|
|
|
|
if (JsonUtil::findJSONValue(object, "HideChangeTrackingControls", booleanFlag))
|
|
|
|
_hideChangeTrackingControls = (booleanFlag ? WOPIFileInfo::TriState::True : WOPIFileInfo::TriState::False);
|
2019-11-19 16:51:45 -06:00
|
|
|
|
2019-11-25 06:03:49 -06:00
|
|
|
std::string overrideWatermarks = LOOLWSD::getConfigValue<std::string>("watermark.text", "");
|
|
|
|
if (!overrideWatermarks.empty())
|
2020-06-11 09:54:27 -05:00
|
|
|
_watermarkText = overrideWatermarks;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:04:26 -06:00
|
|
|
bool WopiStorage::updateLockState(const Authorization& auth, const std::string& cookies,
|
|
|
|
LockContext& lockCtx, bool lock)
|
2019-11-19 16:51:45 -06:00
|
|
|
{
|
2020-07-01 03:34:08 -05:00
|
|
|
lockCtx._lockFailureReason.clear();
|
2019-11-19 16:51:45 -06:00
|
|
|
if (!lockCtx._supportsLocks)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Poco::URI uriObject(getUri());
|
|
|
|
auth.authorizeURI(uriObject);
|
|
|
|
|
|
|
|
Poco::URI uriObjectAnonym(getUri());
|
|
|
|
uriObjectAnonym.setPath(LOOLWSD::anonymizeUrl(uriObjectAnonym.getPath()));
|
|
|
|
const std::string uriAnonym = uriObjectAnonym.toString();
|
|
|
|
|
|
|
|
const std::string wopiLog(lock ? "WOPI::Lock" : "WOPI::Unlock");
|
|
|
|
LOG_DBG(wopiLog << " requesting: " << uriAnonym);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> psession(getHTTPClientSession(uriObject));
|
|
|
|
|
2020-06-14 11:44:10 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST,
|
|
|
|
uriObject.getPathAndQuery(),
|
|
|
|
Poco::Net::HTTPMessage::HTTP_1_1);
|
|
|
|
initHttpRequest(request, uriObject, auth, cookies);
|
2019-11-19 16:51:45 -06:00
|
|
|
|
|
|
|
request.set("X-WOPI-Override", lock ? "LOCK" : "UNLOCK");
|
|
|
|
request.set("X-WOPI-Lock", lockCtx._lockToken);
|
|
|
|
if (!getExtendedData().empty())
|
|
|
|
request.set("X-LOOL-WOPI-ExtendedData", getExtendedData());
|
|
|
|
|
2020-06-30 08:37:40 -05:00
|
|
|
// IIS requires content-length for POST requests: see https://forums.iis.net/t/1119456.aspx
|
|
|
|
request.setContentLength(0);
|
|
|
|
|
2019-11-19 16:51:45 -06:00
|
|
|
psession->sendRequest(request);
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::istream& rs = psession->receiveResponse(response);
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
Poco::StreamCopier::copyStream(rs, oss);
|
|
|
|
std::string responseString = oss.str();
|
|
|
|
|
|
|
|
LOG_INF(wopiLog << " response: " << responseString <<
|
|
|
|
" status " << response.getStatus());
|
|
|
|
|
|
|
|
if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
|
|
|
|
{
|
|
|
|
lockCtx._isLocked = lock;
|
2019-11-27 02:43:05 -06:00
|
|
|
lockCtx._lastLockTime = std::chrono::steady_clock::now();
|
2019-11-19 16:51:45 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-30 17:29:58 -05:00
|
|
|
std::string sMoreInfo = response.get("X-WOPI-LockFailureReason", "");
|
|
|
|
if (!sMoreInfo.empty())
|
2020-07-01 03:34:08 -05:00
|
|
|
{
|
|
|
|
lockCtx._lockFailureReason = sMoreInfo;
|
2020-06-30 17:29:58 -05:00
|
|
|
sMoreInfo = ", failure reason: \"" + sMoreInfo + "\"";
|
2020-07-01 03:34:08 -05:00
|
|
|
}
|
2019-11-27 02:43:05 -06:00
|
|
|
LOG_WRN("Un-successful " << wopiLog << " with status " << response.getStatus() <<
|
2020-06-30 17:29:58 -05:00
|
|
|
sMoreInfo << " and response: " << responseString);
|
2019-11-19 16:51:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& pexc)
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot " << wopiLog << " uri [" << uriAnonym << "]. Error: " <<
|
2020-05-24 08:10:18 -05:00
|
|
|
pexc.displayText() << (pexc.nested() ? " (" + pexc.nested()->displayText() + ')' : ""));
|
2019-11-19 16:51:45 -06:00
|
|
|
}
|
2019-12-23 07:45:38 -06:00
|
|
|
catch (const BadRequestException& exc)
|
|
|
|
{
|
2020-10-06 22:29:56 -05:00
|
|
|
LOG_ERR("Cannot " << wopiLog << " uri [" << uriAnonym << "]. Error: " << exc.what());
|
2019-12-23 07:45:38 -06:00
|
|
|
}
|
2019-11-19 16:51:45 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
/// uri format: http://server/<...>/wopi*/files/<id>/content
|
2020-01-18 15:04:26 -06:00
|
|
|
std::string WopiStorage::loadStorageFileToLocal(const Authorization& auth,
|
|
|
|
const std::string& cookies,
|
|
|
|
LockContext& /*lockCtx*/,
|
|
|
|
const std::string& templateUri)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
|
|
|
// WOPI URI to download files ends in '/contents'.
|
|
|
|
// Add it here to get the payload instead of file info.
|
2018-11-28 02:07:29 -06:00
|
|
|
Poco::URI uriObject(getUri());
|
2016-10-05 03:14:38 -05:00
|
|
|
uriObject.setPath(uriObject.getPath() + "/contents");
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeURI(uriObject);
|
2018-07-17 13:22:06 -05:00
|
|
|
|
2018-11-28 02:07:29 -06:00
|
|
|
Poco::URI uriObjectAnonym(getUri());
|
2018-07-17 13:22:06 -05:00
|
|
|
uriObjectAnonym.setPath(LOOLWSD::anonymizeUrl(uriObjectAnonym.getPath()) + "/contents");
|
|
|
|
const std::string uriAnonym = uriObjectAnonym.toString();
|
2017-08-16 09:38:00 -05:00
|
|
|
|
2019-05-21 22:33:26 -05:00
|
|
|
if (!templateUri.empty())
|
|
|
|
{
|
|
|
|
// template are created in kit process, so just obtain a reference
|
|
|
|
setRootFilePath(Poco::Path(getLocalRootPath(), getFileInfo().getFilename()).toString());
|
|
|
|
setRootFilePathAnonym(LOOLWSD::anonymizeUrl(getRootFilePath()));
|
|
|
|
LOG_INF("Template reference " << getRootFilePathAnonym());
|
|
|
|
|
|
|
|
setLoaded(true);
|
|
|
|
return Poco::Path(getJailPath(), getFileInfo().getFilename()).toString();
|
|
|
|
}
|
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_DBG("Wopi requesting: " << uriAnonym);
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-10-14 07:46:49 -05:00
|
|
|
const auto startTime = std::chrono::steady_clock::now();
|
2016-12-23 01:53:50 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> psession(getHTTPClientSession(uriObject));
|
2016-07-18 06:45:36 -05:00
|
|
|
|
2020-06-14 11:44:10 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET,
|
|
|
|
uriObject.getPathAndQuery(),
|
|
|
|
Poco::Net::HTTPMessage::HTTP_1_1);
|
|
|
|
initHttpRequest(request, uriObject, auth, cookies);
|
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
psession->sendRequest(request);
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::istream& rs = psession->receiveResponse(response);
|
|
|
|
const std::chrono::duration<double> diff = (std::chrono::steady_clock::now() - startTime);
|
|
|
|
_wopiLoadDuration += diff;
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
Log::StreamLogger logger = Log::trace();
|
2017-03-11 13:42:34 -06:00
|
|
|
if (logger.enabled())
|
2016-12-23 01:53:50 -06:00
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
logger << "WOPI::GetFile header for URI [" << uriAnonym << "]:\n";
|
2016-12-22 15:41:05 -06:00
|
|
|
for (const auto& pair : response)
|
|
|
|
{
|
|
|
|
logger << '\t' << pair.first << ": " << pair.second << " / ";
|
|
|
|
}
|
|
|
|
|
2018-06-04 09:23:58 -05:00
|
|
|
LOG_END(logger, true);
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2017-05-18 13:05:45 -05:00
|
|
|
if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_OK)
|
|
|
|
{
|
2020-07-07 14:18:13 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
Poco::StreamCopier::copyStream(rs, oss);
|
|
|
|
std::string responseString = oss.str();
|
|
|
|
LOG_ERR("WOPI::GetFile failed with " << response.getStatus() << ' ' << responseString);
|
|
|
|
throw StorageConnectionException("WOPI::GetFile failed: " + responseString);
|
2017-05-18 13:05:45 -05:00
|
|
|
}
|
|
|
|
else // Successful
|
|
|
|
{
|
2018-11-29 01:42:43 -06:00
|
|
|
setRootFilePath(Poco::Path(getLocalRootPath(), getFileInfo().getFilename()).toString());
|
2018-11-28 02:07:29 -06:00
|
|
|
setRootFilePathAnonym(LOOLWSD::anonymizeUrl(getRootFilePath()));
|
|
|
|
std::ofstream ofs(getRootFilePath());
|
2017-05-18 13:05:45 -05:00
|
|
|
std::copy(std::istreambuf_iterator<char>(rs),
|
|
|
|
std::istreambuf_iterator<char>(),
|
|
|
|
std::ostreambuf_iterator<char>(ofs));
|
2018-01-18 09:20:35 -06:00
|
|
|
ofs.close();
|
2020-10-22 18:20:29 -05:00
|
|
|
|
|
|
|
const FileUtil::Stat fileStat(getRootFilePath());
|
|
|
|
const std::size_t filesize = (fileStat.good() ? fileStat.size() : 0);
|
|
|
|
LOG_INF("WOPI::GetFile downloaded " << filesize << " bytes from [" <<
|
2020-05-24 08:10:18 -05:00
|
|
|
uriAnonym << "] -> " << getRootFilePathAnonym() << " in " << diff.count() << 's');
|
2018-11-28 02:07:29 -06:00
|
|
|
setLoaded(true);
|
2019-11-19 14:54:29 -06:00
|
|
|
|
2017-05-18 13:05:45 -05:00
|
|
|
// Now return the jailed path.
|
2020-05-05 08:25:25 -05:00
|
|
|
if (LOOLWSD::NoCapsForKit)
|
|
|
|
return getRootFilePath();
|
|
|
|
else
|
|
|
|
return Poco::Path(getJailPath(), getFileInfo().getFilename()).toString();
|
2017-05-18 13:05:45 -05:00
|
|
|
}
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2018-07-08 21:50:09 -05:00
|
|
|
catch (const Poco::Exception& pexc)
|
2016-12-23 01:53:50 -06:00
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_ERR("Cannot load document from WOPI storage uri [" + uriAnonym + "]. Error: " <<
|
2020-05-24 08:10:18 -05:00
|
|
|
pexc.displayText() << (pexc.nested() ? " (" + pexc.nested()->displayText() + ')' : ""));
|
2016-12-23 01:53:50 -06:00
|
|
|
throw;
|
|
|
|
}
|
2019-12-23 07:45:38 -06:00
|
|
|
catch (const BadRequestException& exc)
|
|
|
|
{
|
2020-10-06 22:29:56 -05:00
|
|
|
LOG_ERR("Cannot load document from WOPI storage uri [" + uriAnonym + "]. Error: " << exc.what());
|
2019-12-23 07:45:38 -06:00
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2017-05-18 13:05:45 -05:00
|
|
|
return "";
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:04:26 -06:00
|
|
|
StorageBase::SaveResult
|
|
|
|
WopiStorage::saveLocalFileToStorage(const Authorization& auth, const std::string& cookies,
|
|
|
|
LockContext& lockCtx, const std::string& saveAsPath,
|
|
|
|
const std::string& saveAsFilename, const bool isRename)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
2016-10-19 09:52:53 -05:00
|
|
|
// TODO: Check if this URI has write permission (canWrite = true)
|
2016-12-22 15:41:05 -06:00
|
|
|
|
2017-10-24 04:31:39 -05:00
|
|
|
const bool isSaveAs = !saveAsPath.empty() && !saveAsFilename.empty();
|
2018-11-28 02:07:29 -06:00
|
|
|
const std::string filePath(isSaveAs ? saveAsPath : getRootFilePath());
|
2018-06-10 19:55:52 -05:00
|
|
|
const std::string filePathAnonym = LOOLWSD::anonymizeUrl(filePath);
|
2017-11-06 05:44:37 -06:00
|
|
|
|
2020-10-22 18:20:29 -05:00
|
|
|
const FileUtil::Stat fileStat(filePath);
|
|
|
|
if (!fileStat.good())
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot access file [" << filePathAnonym << "] to upload to wopi storage.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::size_t size = (fileStat.good() ? fileStat.size() : 0);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2018-11-28 02:07:29 -06:00
|
|
|
Poco::URI uriObject(getUri());
|
2019-04-30 09:21:44 -05:00
|
|
|
uriObject.setPath(isSaveAs || isRename? uriObject.getPath(): uriObject.getPath() + "/contents");
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeURI(uriObject);
|
2019-08-26 20:16:54 -05:00
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
const std::string uriAnonym = LOOLWSD::anonymizeUrl(uriObject.toString());
|
2017-05-12 10:42:03 -05:00
|
|
|
|
2020-10-22 18:20:29 -05:00
|
|
|
LOG_INF("Uploading " << size << " bytes from [" << filePathAnonym << "] to URI via WOPI ["
|
|
|
|
<< uriAnonym << "].");
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2020-11-15 08:33:26 -06:00
|
|
|
StorageBase::SaveResult saveResult(StorageBase::SaveResult::Result::FAILED);
|
2019-11-12 03:50:33 -06:00
|
|
|
const auto startTime = std::chrono::steady_clock::now();
|
2016-12-23 01:53:50 -06:00
|
|
|
try
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
2016-12-23 01:53:50 -06:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> psession(getHTTPClientSession(uriObject));
|
|
|
|
|
2020-06-14 11:44:10 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST,
|
|
|
|
uriObject.getPathAndQuery(),
|
|
|
|
Poco::Net::HTTPMessage::HTTP_1_1);
|
|
|
|
initHttpRequest(request, uriObject, auth, cookies);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2019-04-30 09:21:44 -05:00
|
|
|
if (!isSaveAs && !isRename)
|
2017-10-20 11:12:05 -05:00
|
|
|
{
|
|
|
|
// normal save
|
|
|
|
request.set("X-WOPI-Override", "PUT");
|
2019-11-19 16:51:45 -06:00
|
|
|
if (lockCtx._supportsLocks)
|
|
|
|
request.set("X-WOPI-Lock", lockCtx._lockToken);
|
2018-11-28 02:07:29 -06:00
|
|
|
request.set("X-LOOL-WOPI-IsModifiedByUser", isUserModified()? "true": "false");
|
2020-06-08 08:26:09 -05:00
|
|
|
request.set("X-LOOL-WOPI-IsAutosave", isAutosave()? "true": "false");
|
2018-12-04 01:49:49 -06:00
|
|
|
request.set("X-LOOL-WOPI-IsExitSave", isExitSave()? "true": "false");
|
2019-06-16 13:42:11 -05:00
|
|
|
if (!getExtendedData().empty())
|
|
|
|
request.set("X-LOOL-WOPI-ExtendedData", getExtendedData());
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2018-11-28 02:07:29 -06:00
|
|
|
if (!getForceSave())
|
2017-10-20 11:12:05 -05:00
|
|
|
{
|
|
|
|
// Request WOPI host to not overwrite if timestamps mismatch
|
2019-08-30 12:37:55 -05:00
|
|
|
request.set("X-LOOL-WOPI-Timestamp", Util::getIso8601FracformatTime(getFileInfo().getModifiedTime()));
|
2017-10-20 11:12:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2017-06-01 07:56:54 -05:00
|
|
|
{
|
2017-10-24 04:31:39 -05:00
|
|
|
// the suggested target has to be in UTF-7; default to extension
|
|
|
|
// only when the conversion fails
|
2019-09-21 20:49:58 -05:00
|
|
|
std::string suggestedTarget = '.' + Poco::Path(saveAsFilename).getExtension();
|
2017-10-24 04:31:39 -05:00
|
|
|
|
2019-09-21 20:49:58 -05:00
|
|
|
//TODO: Perhaps we should cache this descriptor and reuse, as iconv_open might be expensive.
|
|
|
|
const iconv_t cd = iconv_open("UTF-7", "UTF-8");
|
2017-10-24 04:31:39 -05:00
|
|
|
if (cd == (iconv_t) -1)
|
|
|
|
LOG_ERR("Failed to initialize iconv for UTF-7 conversion, using '" << suggestedTarget << "'.");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<char> input(saveAsFilename.begin(), saveAsFilename.end());
|
|
|
|
std::vector<char> buffer(8 * saveAsFilename.size());
|
|
|
|
|
|
|
|
char* in = &input[0];
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t in_left = input.size();
|
2017-10-24 04:31:39 -05:00
|
|
|
char* out = &buffer[0];
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t out_left = buffer.size();
|
2017-10-24 04:31:39 -05:00
|
|
|
|
|
|
|
if (iconv(cd, &in, &in_left, &out, &out_left) == (size_t) -1)
|
|
|
|
LOG_ERR("Failed to convert '" << saveAsFilename << "' to UTF-7, using '" << suggestedTarget << "'.");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// conversion succeeded
|
|
|
|
suggestedTarget = std::string(&buffer[0], buffer.size() - out_left);
|
|
|
|
LOG_TRC("Converted '" << saveAsFilename << "' to UTF-7 as '" << suggestedTarget << "'.");
|
|
|
|
}
|
2019-09-21 20:49:58 -05:00
|
|
|
|
|
|
|
iconv_close(cd);
|
2017-10-24 04:31:39 -05:00
|
|
|
}
|
|
|
|
|
2019-04-30 09:21:44 -05:00
|
|
|
if (isRename)
|
|
|
|
{
|
|
|
|
// rename file
|
|
|
|
request.set("X-WOPI-Override", "RENAME_FILE");
|
|
|
|
request.set("X-WOPI-RequestedName", suggestedTarget);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// save as
|
|
|
|
request.set("X-WOPI-Override", "PUT_RELATIVE");
|
|
|
|
request.set("X-WOPI-Size", std::to_string(size));
|
|
|
|
request.set("X-WOPI-SuggestedTarget", suggestedTarget);
|
|
|
|
}
|
2017-06-01 07:56:54 -05:00
|
|
|
}
|
2017-06-01 09:16:03 -05:00
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
request.setContentType("application/octet-stream");
|
|
|
|
request.setContentLength(size);
|
2020-06-14 11:44:10 -05:00
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
std::ostream& os = psession->sendRequest(request);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
|
|
|
std::ifstream ifs(filePath);
|
2016-12-23 01:53:50 -06:00
|
|
|
Poco::StreamCopier::copyStream(ifs, os);
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::istream& rs = psession->receiveResponse(response);
|
2018-06-10 21:40:32 -05:00
|
|
|
|
2019-11-12 03:50:33 -06:00
|
|
|
_wopiSaveDuration = std::chrono::steady_clock::now() - startTime;
|
|
|
|
|
2018-06-10 21:40:32 -05:00
|
|
|
std::ostringstream oss;
|
2016-12-23 01:53:50 -06:00
|
|
|
Poco::StreamCopier::copyStream(rs, oss);
|
2018-06-10 21:40:32 -05:00
|
|
|
std::string responseString = oss.str();
|
2020-07-01 05:46:58 -05:00
|
|
|
saveResult.setErrorMsg(responseString);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2019-04-30 09:21:44 -05:00
|
|
|
const std::string wopiLog(isSaveAs ? "WOPI::PutRelativeFile" : (isRename ? "WOPI::RenameFile":"WOPI::PutFile"));
|
2018-06-10 21:40:32 -05:00
|
|
|
|
|
|
|
if (Log::infoEnabled())
|
|
|
|
{
|
2019-04-14 11:24:45 -05:00
|
|
|
if (LOOLWSD::AnonymizeUserData)
|
2018-06-10 21:40:32 -05:00
|
|
|
{
|
|
|
|
Poco::JSON::Object::Ptr object;
|
|
|
|
if (JsonUtil::parseJSON(responseString, object))
|
|
|
|
{
|
|
|
|
// Anonymize the filename
|
2018-07-17 01:01:05 -05:00
|
|
|
std::string url;
|
2018-06-10 21:40:32 -05:00
|
|
|
std::string filename;
|
2018-07-21 14:58:35 -05:00
|
|
|
if (JsonUtil::findJSONValue(object, "Url", url) &&
|
|
|
|
JsonUtil::findJSONValue(object, "Name", filename))
|
|
|
|
{
|
|
|
|
// Get the FileId form the URL, which we use as the anonymized filename.
|
|
|
|
std::string decodedUrl;
|
|
|
|
Poco::URI::decode(url, decodedUrl);
|
|
|
|
const std::string obfuscatedFileId = Util::getFilenameFromURL(decodedUrl);
|
|
|
|
Util::mapAnonymized(obfuscatedFileId, obfuscatedFileId); // Identity, to avoid re-anonymizing.
|
|
|
|
|
|
|
|
const std::string filenameOnly = Util::getFilenameFromURL(filename);
|
|
|
|
Util::mapAnonymized(filenameOnly, obfuscatedFileId);
|
|
|
|
object->set("Name", LOOLWSD::anonymizeUrl(filename));
|
|
|
|
}
|
|
|
|
|
2018-06-10 21:40:32 -05:00
|
|
|
// Stringify to log.
|
|
|
|
std::ostringstream ossResponse;
|
|
|
|
object->stringify(ossResponse);
|
|
|
|
responseString = ossResponse.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:24:38 -05:00
|
|
|
LOG_INF(wopiLog << " uploaded " << size << " bytes from [" << filePathAnonym << "] -> ["
|
|
|
|
<< uriAnonym << "]: " << response.getStatus() << ' '
|
|
|
|
<< response.getReason() << ": " << responseString);
|
2018-06-10 21:40:32 -05:00
|
|
|
}
|
2016-12-23 01:53:50 -06:00
|
|
|
|
|
|
|
if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
|
|
|
|
{
|
2020-11-15 08:33:26 -06:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::Result::OK);
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
Poco::JSON::Object::Ptr object;
|
2018-02-17 16:32:41 -06:00
|
|
|
if (JsonUtil::parseJSON(oss.str(), object))
|
2017-05-31 12:22:54 -05:00
|
|
|
{
|
2018-02-17 16:32:41 -06:00
|
|
|
const std::string lastModifiedTime = JsonUtil::getJSONValue<std::string>(object, "LastModifiedTime");
|
2017-10-20 11:12:05 -05:00
|
|
|
LOG_TRC(wopiLog << " returns LastModifiedTime [" << lastModifiedTime << "].");
|
2019-08-30 12:37:55 -05:00
|
|
|
getFileInfo().setModifiedTime(Util::iso8601ToTimestamp(lastModifiedTime, "LastModifiedTime"));
|
2017-06-01 09:16:03 -05:00
|
|
|
|
2019-04-30 09:21:44 -05:00
|
|
|
if (isSaveAs || isRename)
|
2017-10-25 07:09:27 -05:00
|
|
|
{
|
2018-02-17 16:32:41 -06:00
|
|
|
const std::string name = JsonUtil::getJSONValue<std::string>(object, "Name");
|
2018-06-10 21:40:32 -05:00
|
|
|
LOG_TRC(wopiLog << " returns Name [" << LOOLWSD::anonymizeUrl(name) << "].");
|
2017-10-25 07:09:27 -05:00
|
|
|
|
2018-02-17 16:32:41 -06:00
|
|
|
const std::string url = JsonUtil::getJSONValue<std::string>(object, "Url");
|
2018-06-10 21:40:32 -05:00
|
|
|
LOG_TRC(wopiLog << " returns Url [" << LOOLWSD::anonymizeUrl(url) << "].");
|
2017-10-25 07:09:27 -05:00
|
|
|
|
|
|
|
saveResult.setSaveAsResult(name, url);
|
|
|
|
}
|
2017-06-01 09:16:03 -05:00
|
|
|
// Reset the force save flag now, if any, since we are done saving
|
|
|
|
// Next saves shouldn't be saved forcefully unless commanded
|
2018-11-28 02:07:29 -06:00
|
|
|
forceSave(false);
|
2017-05-31 12:22:54 -05:00
|
|
|
}
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
else
|
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_WRN("Invalid or missing JSON in " << wopiLog << " HTTP_OK response.");
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
}
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
|
|
|
else if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_REQUESTENTITYTOOLARGE)
|
|
|
|
{
|
2020-11-15 08:33:26 -06:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::Result::DISKFULL);
|
2016-12-23 01:53:50 -06:00
|
|
|
}
|
2019-12-02 07:09:24 -06:00
|
|
|
else if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED ||
|
|
|
|
response.getStatus() == Poco::Net::HTTPResponse::HTTP_FORBIDDEN)
|
2017-05-31 01:21:12 -05:00
|
|
|
{
|
2020-11-15 08:33:26 -06:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::Result::UNAUTHORIZED);
|
2017-05-31 01:21:12 -05:00
|
|
|
}
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
else if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_CONFLICT)
|
|
|
|
{
|
2020-11-15 08:33:26 -06:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::Result::CONFLICT);
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
Poco::JSON::Object::Ptr object;
|
2018-02-17 16:32:41 -06:00
|
|
|
if (JsonUtil::parseJSON(oss.str(), object))
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
{
|
2018-02-17 16:32:41 -06:00
|
|
|
const unsigned loolStatusCode = JsonUtil::getJSONValue<unsigned>(object, "LOOLStatusCode");
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
if (loolStatusCode == static_cast<unsigned>(LOOLStatusCode::DOC_CHANGED))
|
|
|
|
{
|
2020-11-15 08:33:26 -06:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::Result::DOC_CHANGED);
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_WRN("Invalid or missing JSON in " << wopiLog << " HTTP_CONFLICT response.");
|
Inform all clients when document changed behind our back
Introduce a new header X-LOOL-WOPI-Timestamp
This is a WOPI header extension to detect any external document change. For
example, when the file that is already opened by LOOL is changed
in storage.
The WOPI host sends LastModifiedTime field (in WOPI specs) as part
of the CheckFileInfo response. It also expects wsd to send the
same timestamp in X-LOOL-WOPI-Timestamp header during WOPI::PutFile. If
this header is present, then WOPI host checks, before saving the
document, if the timestamp in the header is equal to the timestamp of
the file in its storage. Only upon meeting this condition, it saves the
file back to storage, otherwise it informs us about some change
to the document.
We are supposed to inform the user accordingly. If user is okay
with over-writing the document, then we can omit sending
X-LOOL-WOPI-Timestamp header, in which case, no check as mentioned above
would be performed while saving the file and document will be
overwritten.
Also, use a separate list of LOOL status codes to denote such a change.
It would be wrong to use HTTP_CONFLICT status code for denoting doc
changed in storage scenario. WOPI specs reserves that for WOPI locks
which are not yet implemented. Better to use a separate LOOL specific
status codes synced across WOPI hosts and us to denote scenario that we
expect and are not covered in WOPI specs.
Change-Id: I61539dfae672bc104b8008f030f96e90f9ff48a5
2017-05-31 12:48:33 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-21 12:43:46 -06:00
|
|
|
else
|
|
|
|
{
|
2019-12-02 07:09:24 -06:00
|
|
|
// Internal server error, and other failures.
|
2020-10-22 21:24:38 -05:00
|
|
|
LOG_ERR("Unexpected response to "
|
|
|
|
<< wopiLog << ". Cannot upload file to WOPI storage uri [" << uriAnonym
|
|
|
|
<< "]: " << response.getStatus() << ' ' << response.getReason() << ": "
|
|
|
|
<< responseString);
|
2020-11-15 08:33:26 -06:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::Result::FAILED);
|
2019-11-21 12:43:46 -06:00
|
|
|
}
|
2016-11-23 06:09:54 -06:00
|
|
|
}
|
2018-07-08 21:50:09 -05:00
|
|
|
catch (const Poco::Exception& pexc)
|
2016-11-23 06:09:54 -06:00
|
|
|
{
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_ERR("Cannot save file to WOPI storage uri [" << uriAnonym << "]. Error: " <<
|
2020-05-24 08:10:18 -05:00
|
|
|
pexc.displayText() << (pexc.nested() ? " (" + pexc.nested()->displayText() + ')' : ""));
|
2020-11-15 08:33:26 -06:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::Result::FAILED);
|
2016-11-23 06:09:54 -06:00
|
|
|
}
|
2019-12-23 07:45:38 -06:00
|
|
|
catch (const BadRequestException& exc)
|
|
|
|
{
|
2020-10-06 22:29:56 -05:00
|
|
|
LOG_ERR("Cannot save file to WOPI storage uri [" + uriAnonym + "]. Error: " << exc.what());
|
2019-12-23 07:45:38 -06:00
|
|
|
}
|
2016-11-23 06:09:54 -06:00
|
|
|
|
|
|
|
return saveResult;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2019-11-28 10:09:58 -06:00
|
|
|
#endif // !MOBILEAPP
|
2018-09-05 07:11:05 -05:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|