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 file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
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>
|
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
|
|
|
|
2018-06-10 10:42:15 -05:00
|
|
|
using std::size_t;
|
|
|
|
|
2016-06-07 02:18:49 -05:00
|
|
|
bool StorageBase::FilesystemEnabled;
|
|
|
|
bool StorageBase::WopiEnabled;
|
2019-10-07 06:51:30 -05:00
|
|
|
bool StorageBase::SSLEnabled;
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t StorageBase::getFileSize(const std::string& filename)
|
|
|
|
{
|
|
|
|
return std::ifstream(filename, std::ifstream::ate | std::ifstream::binary).tellg();
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
{
|
2016-11-03 19:26:40 -05:00
|
|
|
LOG_INF("Storage load 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();
|
2017-05-19 03:32:24 -05:00
|
|
|
if (WopiHosts.match(targetHost) || isLocalhost(targetHost))
|
2016-04-16 11:55:56 -05:00
|
|
|
{
|
2016-10-14 05:09:43 -05:00
|
|
|
return std::unique_ptr<StorageBase>(new WopiStorage(uri, jailRoot, jailPath));
|
2016-04-16 11:55:56 -05:00
|
|
|
}
|
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
|
|
|
{
|
2018-11-28 02:07:29 -06:00
|
|
|
const Poco::Path path = Poco::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
|
|
|
|
2019-08-30 12:37:55 -05:00
|
|
|
std::string str_path = path.toString();
|
2016-12-20 09:53:11 -06:00
|
|
|
const auto& filename = path.getFileName();
|
2018-02-07 03:17:59 -06:00
|
|
|
const Poco::File file = Poco::File(path);
|
2019-09-03 05:23:32 -05:00
|
|
|
std::chrono::system_clock::time_point lastModified = Util::getFileTimestamp(str_path);
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t size = file.getSize();
|
2016-10-26 06:15:28 -05:00
|
|
|
|
2018-11-28 02:07:29 -06:00
|
|
|
setFileInfo(FileInfo({filename, "localhost", lastModified, size}));
|
2016-10-26 06:15:28 -05:00
|
|
|
|
|
|
|
// Set automatic userid and username
|
2018-11-06 15:10:21 -06:00
|
|
|
return std::unique_ptr<LocalStorage::LocalFileInfo>(new LocalFileInfo({"localhost" + std::to_string(LastLocalStorageId), "LocalHost#" + std::to_string(LastLocalStorageId++)}));
|
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
|
2018-11-28 02:07:29 -06:00
|
|
|
LOG_WRN("link(\"" << LOOLWSD::anonymizeUrl(publicFilePath) << "\", \"" << getRootFilePathAnonym() << "\") failed. Will copy. "
|
2018-07-11 04:58:10 -05:00
|
|
|
"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)
|
|
|
|
{
|
2018-11-28 02:07:29 -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
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-11-28 02:07:29 -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-01-18 09:56:01 -06:00
|
|
|
FileUtil::copyFileTo(getRootFilePath(), getUri().getPath());
|
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
|
2019-08-30 12:37:55 -05:00
|
|
|
const Poco::Path path = Poco::Path(getUri().getPath());
|
|
|
|
std::string str_path = path.toString();
|
|
|
|
getFileInfo().setModifiedTime(Util::getFileTimestamp(str_path));
|
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)
|
|
|
|
{
|
2018-11-28 02:07:29 -06:00
|
|
|
LOG_ERR("copyTo(\"" << getRootFilePathAnonym() << "\", \"" << LOOLWSD::anonymizeUrl(getUri().getPath()) <<
|
2016-11-03 19:26:40 -05:00
|
|
|
"\") failed: " << exc.displayText());
|
2017-08-22 09:19:20 -05:00
|
|
|
return StorageBase::SaveResult::FAILED;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 07:09:27 -05:00
|
|
|
return StorageBase::SaveResult(StorageBase::SaveResult::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)
|
|
|
|
{
|
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.
|
2019-10-09 05:17:33 -05:00
|
|
|
return (SSLEnabled || LOOLWSD::isSSLTermination())
|
2019-10-07 06:51:30 -05:00
|
|
|
? new Poco::Net::HTTPSClientSession(uri.getHost(), uri.getPort(),
|
|
|
|
Poco::Net::SSLManager::instance().defaultClientContext())
|
|
|
|
: new Poco::Net::HTTPClientSession(uri.getHost(), uri.getPort());
|
|
|
|
}
|
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-02-28 03:50:58 -06:00
|
|
|
StringVector cookieTokens = LOOLProtocol::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);
|
|
|
|
LOG_TRC("Added storage debug cookie [" << cookieTokens[0] << "=" << cookieTokens[1] << "].");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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
|
|
|
{
|
|
|
|
Poco::Net::NameValueCollection nvcCookies;
|
2020-01-18 15:04:26 -06:00
|
|
|
request.getCookies(nvcCookies); // Preserve existing cookies.
|
|
|
|
|
2020-02-28 03:50:58 -06:00
|
|
|
StringVector cookies = LOOLProtocol::tokenize(reuseStorageCookies, ':');
|
2019-08-21 08:23:40 -05:00
|
|
|
for (auto cookie : cookies)
|
|
|
|
{
|
2020-02-28 07:51:22 -06:00
|
|
|
StringVector cookieTokens = LOOLProtocol::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;
|
|
|
|
os << " lock:\n";
|
|
|
|
os << " locked: " << _isLocked << "\n";
|
|
|
|
os << " token: '" << _lockToken << "'\n";
|
|
|
|
os << " last locked: " << Util::getSteadyClockAsString(_lastLockTime) << "\n";
|
|
|
|
}
|
|
|
|
|
2019-11-28 10:09:58 -06:00
|
|
|
#if !MOBILEAPP
|
|
|
|
|
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-11-07 09:24:37 -06:00
|
|
|
std::map<std::string, std::string> params = GetQueryParams(uriObject);
|
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
|
|
|
|
{
|
2017-05-12 10:42:03 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, uriObject.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
|
2017-05-14 14:17:19 -05:00
|
|
|
request.set("User-Agent", WOPI_AGENT_STRING);
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeRequest(request);
|
2017-05-17 03:48:51 -05:00
|
|
|
addStorageDebugCookie(request);
|
2019-08-26 20:16:54 -05:00
|
|
|
if (_reuseCookies)
|
2020-01-18 15:04:26 -06:00
|
|
|
addStorageReuseCookie(request, cookies);
|
2020-02-19 07:38:47 -06:00
|
|
|
addWopiProof(request, uriObject, params["access_token"]);
|
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));
|
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
|
|
|
|
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::CheckFileInfo 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)
|
|
|
|
{
|
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: " <<
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot get file info from WOPI storage uri [" << uriAnonym << "]. Error: Failed HTPP request authorization");
|
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
|
|
|
// Parse the response.
|
|
|
|
std::string filename;
|
|
|
|
size_t size = 0;
|
2016-11-08 02:14:14 -06:00
|
|
|
std::string ownerId;
|
2016-08-29 08:41:37 -05:00
|
|
|
std::string userId;
|
|
|
|
std::string userName;
|
2018-07-08 21:50:09 -05:00
|
|
|
std::string obfuscatedUserId;
|
2017-05-28 11:20:49 -05:00
|
|
|
std::string userExtraInfo;
|
2017-09-04 08:40:04 -05:00
|
|
|
std::string watermarkText;
|
2018-11-14 13:16:26 -06:00
|
|
|
std::string templateSaveAs;
|
2019-05-21 22:33:26 -05:00
|
|
|
std::string templateSource;
|
2016-10-19 09:52:53 -05:00
|
|
|
bool canWrite = false;
|
2016-11-08 07:37:28 -06:00
|
|
|
bool enableOwnerTermination = false;
|
2016-10-25 02:13:00 -05:00
|
|
|
std::string postMessageOrigin;
|
2016-11-10 06:21:39 -06:00
|
|
|
bool hidePrintOption = false;
|
|
|
|
bool hideSaveOption = false;
|
|
|
|
bool hideExportOption = false;
|
2016-12-13 05:30:43 -06:00
|
|
|
bool disablePrint = false;
|
|
|
|
bool disableExport = false;
|
|
|
|
bool disableCopy = false;
|
2017-08-29 11:59:14 -05:00
|
|
|
bool disableInactiveMessages = false;
|
2019-06-04 06:57:53 -05:00
|
|
|
bool downloadAsPostMessage = false;
|
2016-12-16 07:19:27 -06:00
|
|
|
std::string lastModifiedTime;
|
2017-10-03 10:06:02 -05:00
|
|
|
bool userCanNotWriteRelative = true;
|
2018-08-17 16:33:15 -05:00
|
|
|
bool enableInsertRemoteImage = false;
|
2018-08-18 19:17:25 -05:00
|
|
|
bool enableShare = false;
|
2019-11-19 14:54:29 -06:00
|
|
|
bool supportsLocks = false;
|
2019-04-30 09:21:44 -05:00
|
|
|
bool supportsRename = false;
|
|
|
|
bool userCanRename = false;
|
2018-12-12 07:51:37 -06:00
|
|
|
std::string hideUserList("false");
|
2018-04-24 11:09:37 -05:00
|
|
|
WOPIFileInfo::TriState disableChangeTrackingRecord = WOPIFileInfo::TriState::Unset;
|
|
|
|
WOPIFileInfo::TriState disableChangeTrackingShow = WOPIFileInfo::TriState::Unset;
|
|
|
|
WOPIFileInfo::TriState hideChangeTrackingControls = WOPIFileInfo::TriState::Unset;
|
2016-10-14 07:46:49 -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);
|
|
|
|
|
2018-02-17 16:32:41 -06:00
|
|
|
JsonUtil::findJSONValue(object, "BaseFileName", filename);
|
|
|
|
JsonUtil::findJSONValue(object, "OwnerId", ownerId);
|
|
|
|
JsonUtil::findJSONValue(object, "UserId", userId);
|
|
|
|
JsonUtil::findJSONValue(object, "UserFriendlyName", userName);
|
2018-11-14 13:16:26 -06:00
|
|
|
JsonUtil::findJSONValue(object, "TemplateSaveAs", templateSaveAs);
|
2019-05-21 22:33:26 -05:00
|
|
|
JsonUtil::findJSONValue(object, "TemplateSource", templateSource);
|
2018-06-10 10:42:15 -05:00
|
|
|
|
|
|
|
// Anonymize key values.
|
2019-04-14 11:24:45 -05:00
|
|
|
if (LOOLWSD::AnonymizeUserData)
|
2018-06-10 10:42:15 -05:00
|
|
|
{
|
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
|
|
|
|
2018-07-08 21:50:09 -05:00
|
|
|
JsonUtil::findJSONValue(object, "ObfuscatedUserId", obfuscatedUserId, false);
|
2018-07-10 22:09:27 -05:00
|
|
|
if (!obfuscatedUserId.empty())
|
|
|
|
{
|
|
|
|
Util::mapAnonymized(ownerId, obfuscatedUserId);
|
|
|
|
Util::mapAnonymized(userId, obfuscatedUserId);
|
|
|
|
Util::mapAnonymized(userName, obfuscatedUserId);
|
|
|
|
}
|
2018-07-08 21:50:09 -05:00
|
|
|
|
2018-06-10 10:42:15 -05:00
|
|
|
// Set anonymized version of the above fields before logging.
|
|
|
|
// Note: anonymization caches the result, so we don't need to store here.
|
2019-04-14 11:24:45 -05:00
|
|
|
if (LOOLWSD::AnonymizeUserData)
|
2018-06-18 18:10:30 -05:00
|
|
|
object->set("BaseFileName", LOOLWSD::anonymizeUrl(filename));
|
2018-06-10 10:42:15 -05:00
|
|
|
|
2018-07-08 21:50:09 -05:00
|
|
|
// If obfuscatedUserId is provided, then don't log the originals and use it.
|
2019-04-14 11:24:45 -05:00
|
|
|
if (LOOLWSD::AnonymizeUserData && obfuscatedUserId.empty())
|
2018-06-10 10:42:15 -05:00
|
|
|
{
|
2018-06-18 18:10:30 -05:00
|
|
|
object->set("OwnerId", LOOLWSD::anonymizeUsername(ownerId));
|
2018-06-10 10:42:15 -05:00
|
|
|
object->set("UserId", LOOLWSD::anonymizeUsername(userId));
|
|
|
|
object->set("UserFriendlyName", LOOLWSD::anonymizeUsername(userName));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
object->stringify(oss);
|
|
|
|
wopiResponse = oss.str();
|
|
|
|
|
|
|
|
// Remove them for performance reasons; they aren't needed anymore.
|
2018-07-08 21:50:09 -05:00
|
|
|
object->remove("ObfuscatedUserId");
|
|
|
|
|
2019-04-14 11:24:45 -05:00
|
|
|
if (LOOLWSD::AnonymizeUserData)
|
2018-11-14 13:16:26 -06:00
|
|
|
{
|
2018-06-18 18:10:30 -05:00
|
|
|
object->remove("BaseFileName");
|
2018-11-14 13:16:26 -06:00
|
|
|
object->remove("TemplateSaveAs");
|
2019-05-21 22:33:26 -05:00
|
|
|
object->remove("TemplateSource");
|
2019-12-17 08:41:34 -06:00
|
|
|
object->remove("OwnerId");
|
2018-06-10 10:42:15 -05:00
|
|
|
object->remove("UserId");
|
|
|
|
object->remove("UserFriendlyName");
|
|
|
|
}
|
|
|
|
|
2018-08-01 21:04:33 -05:00
|
|
|
LOG_DBG("WOPI::CheckFileInfo (" << callDuration.count() * 1000. << " ms): " << wopiResponse);
|
|
|
|
}
|
2018-06-10 10:42:15 -05:00
|
|
|
|
|
|
|
JsonUtil::findJSONValue(object, "Size", size);
|
2018-02-17 16:32:41 -06:00
|
|
|
JsonUtil::findJSONValue(object, "UserExtraInfo", userExtraInfo);
|
|
|
|
JsonUtil::findJSONValue(object, "WatermarkText", watermarkText);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanWrite", canWrite);
|
|
|
|
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);
|
2019-06-04 06:57:53 -05:00
|
|
|
JsonUtil::findJSONValue(object, "DownloadAsPostMessage", downloadAsPostMessage);
|
2018-02-17 16:32:41 -06:00
|
|
|
JsonUtil::findJSONValue(object, "LastModifiedTime", lastModifiedTime);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanNotWriteRelative", userCanNotWriteRelative);
|
2018-08-17 16:33:15 -05:00
|
|
|
JsonUtil::findJSONValue(object, "EnableInsertRemoteImage", enableInsertRemoteImage);
|
2018-08-18 19:17:25 -05:00
|
|
|
JsonUtil::findJSONValue(object, "EnableShare", enableShare);
|
2018-12-12 04:05:31 -06:00
|
|
|
JsonUtil::findJSONValue(object, "HideUserList", hideUserList);
|
2019-11-19 14:54:29 -06:00
|
|
|
JsonUtil::findJSONValue(object, "SupportsLocks", supportsLocks);
|
2019-04-30 09:21:44 -05:00
|
|
|
JsonUtil::findJSONValue(object, "SupportsRename", supportsRename);
|
|
|
|
JsonUtil::findJSONValue(object, "UserCanRename", userCanRename);
|
2018-04-24 11:09:37 -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);
|
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
|
|
|
}
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2019-08-30 12:37:55 -05:00
|
|
|
const std::chrono::system_clock::time_point modifiedTime = Util::iso8601ToTimestamp(lastModifiedTime, "LastModifiedTime");
|
2018-11-28 02:07:29 -06:00
|
|
|
setFileInfo(FileInfo({filename, ownerId, modifiedTime, size}));
|
2017-05-19 03:32:24 -05:00
|
|
|
|
2019-11-19 16:51:45 -06:00
|
|
|
if (supportsLocks)
|
|
|
|
lockCtx.initSupportsLocks();
|
|
|
|
|
2019-11-25 06:03:49 -06:00
|
|
|
std::string overrideWatermarks = LOOLWSD::getConfigValue<std::string>("watermark.text", "");
|
|
|
|
if (!overrideWatermarks.empty())
|
|
|
|
watermarkText = overrideWatermarks;
|
|
|
|
|
2018-04-24 11:09:37 -05:00
|
|
|
return std::unique_ptr<WopiStorage::WOPIFileInfo>(new WOPIFileInfo(
|
2019-05-21 22:33:26 -05:00
|
|
|
{userId, obfuscatedUserId, userName, userExtraInfo, watermarkText, templateSaveAs, templateSource,
|
|
|
|
canWrite, postMessageOrigin, hidePrintOption, hideSaveOption, hideExportOption,
|
2018-04-24 11:09:37 -05:00
|
|
|
enableOwnerTermination, disablePrint, disableExport, disableCopy,
|
2019-06-04 06:57:53 -05:00
|
|
|
disableInactiveMessages, downloadAsPostMessage, userCanNotWriteRelative, enableInsertRemoteImage, enableShare,
|
2018-12-12 04:05:31 -06:00
|
|
|
hideUserList, disableChangeTrackingShow, disableChangeTrackingRecord,
|
2019-11-19 14:54:29 -06:00
|
|
|
hideChangeTrackingControls, supportsLocks, supportsRename,
|
|
|
|
userCanRename, callDuration}));
|
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
|
|
|
{
|
|
|
|
if (!lockCtx._supportsLocks)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Poco::URI uriObject(getUri());
|
|
|
|
auth.authorizeURI(uriObject);
|
|
|
|
|
2019-11-21 08:34:36 -06:00
|
|
|
std::map<std::string, std::string> params = GetQueryParams(uriObject);
|
2019-11-19 16:51:45 -06:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, uriObject.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
|
|
|
|
request.set("User-Agent", WOPI_AGENT_STRING);
|
|
|
|
auth.authorizeRequest(request);
|
|
|
|
|
|
|
|
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());
|
|
|
|
addStorageDebugCookie(request);
|
|
|
|
if (_reuseCookies)
|
2020-01-18 15:04:26 -06:00
|
|
|
addStorageReuseCookie(request, cookies);
|
2020-02-19 07:38:47 -06:00
|
|
|
addWopiProof(request, uriObject, params["access_token"]);
|
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
|
|
|
|
{
|
2019-11-27 02:43:05 -06:00
|
|
|
LOG_WRN("Un-successful " << wopiLog << " with status " << response.getStatus() <<
|
2019-11-19 16:51:45 -06:00
|
|
|
" and response: " << responseString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& pexc)
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot " << wopiLog << " uri [" << uriAnonym << "]. Error: " <<
|
|
|
|
pexc.displayText() << (pexc.nested() ? " (" + pexc.nested()->displayText() + ")" : ""));
|
|
|
|
}
|
2019-12-23 07:45:38 -06:00
|
|
|
catch (const BadRequestException& exc)
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot " << wopiLog << " uri [" << uriAnonym << "]. Error: Failed HTPP request authorization");
|
|
|
|
}
|
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
|
|
|
|
2019-11-07 09:24:37 -06:00
|
|
|
std::map<std::string, std::string> params = GetQueryParams(uriObject);
|
2019-08-26 20:16:54 -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
|
|
|
|
2016-12-23 01:53:50 -06:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, uriObject.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
|
2017-05-14 14:17:19 -05:00
|
|
|
request.set("User-Agent", WOPI_AGENT_STRING);
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeRequest(request);
|
2017-05-17 03:48:51 -05:00
|
|
|
addStorageDebugCookie(request);
|
2019-08-26 20:16:54 -05:00
|
|
|
if (_reuseCookies)
|
2020-01-18 15:04:26 -06:00
|
|
|
addStorageReuseCookie(request, cookies);
|
2020-02-19 07:38:47 -06:00
|
|
|
addWopiProof(request, uriObject, params["access_token"]);
|
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)
|
|
|
|
{
|
2017-05-19 10:34:03 -05:00
|
|
|
LOG_ERR("WOPI::GetFile failed with " << response.getStatus() << ' ' << response.getReason());
|
2017-05-18 13:05:45 -05:00
|
|
|
throw StorageConnectionException("WOPI::GetFile failed");
|
|
|
|
}
|
|
|
|
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();
|
2018-11-28 02:07:29 -06:00
|
|
|
LOG_INF("WOPI::GetFile downloaded " << getFileSize(getRootFilePath()) << " bytes from [" <<
|
|
|
|
uriAnonym << "] -> " << getRootFilePathAnonym() << " in " << diff.count() << "s");
|
|
|
|
setLoaded(true);
|
2019-11-19 14:54:29 -06:00
|
|
|
|
2017-05-18 13:05:45 -05:00
|
|
|
// Now return the jailed path.
|
2018-11-29 01:42:43 -06:00
|
|
|
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: " <<
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot load document from WOPI storage uri [" + uriAnonym + "]. Error: Failed HTPP request authorization");
|
|
|
|
}
|
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
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t size = getFileSize(filePath);
|
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
|
|
|
|
2019-11-07 09:24:37 -06:00
|
|
|
std::map<std::string, std::string> params = GetQueryParams(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
|
|
|
|
2018-06-10 19:55:52 -05:00
|
|
|
LOG_INF("Uploading URI via WOPI [" << uriAnonym << "] from [" << filePathAnonym + "].");
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2017-10-25 07:09:27 -05:00
|
|
|
StorageBase::SaveResult saveResult(StorageBase::SaveResult::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));
|
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, uriObject.getPathAndQuery(), Poco::Net::HTTPMessage::HTTP_1_1);
|
2017-10-20 11:12:05 -05:00
|
|
|
request.set("User-Agent", WOPI_AGENT_STRING);
|
2017-08-16 09:38:00 -05:00
|
|
|
auth.authorizeRequest(request);
|
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");
|
|
|
|
request.set("X-LOOL-WOPI-IsAutosave", getIsAutosave()? "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];
|
|
|
|
size_t in_left = input.size();
|
|
|
|
char* out = &buffer[0];
|
|
|
|
size_t out_left = buffer.size();
|
|
|
|
|
|
|
|
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);
|
2017-05-17 03:48:51 -05:00
|
|
|
addStorageDebugCookie(request);
|
2019-08-26 20:16:54 -05:00
|
|
|
if (_reuseCookies)
|
2020-01-18 15:04:26 -06:00
|
|
|
addStorageReuseCookie(request, cookies);
|
2020-02-19 07:38:47 -06:00
|
|
|
addWopiProof(request, uriObject, params["access_token"]);
|
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();
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_INF(wopiLog << " response: " << responseString);
|
|
|
|
LOG_INF(wopiLog << " uploaded " << size << " bytes from [" << filePathAnonym <<
|
|
|
|
"] -> [" << uriAnonym << "]: " << response.getStatus() << " " << response.getReason());
|
|
|
|
}
|
2016-12-23 01:53:50 -06:00
|
|
|
|
|
|
|
if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::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)
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::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
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::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)
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::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))
|
|
|
|
{
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::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.
|
2019-11-21 12:43:46 -06:00
|
|
|
LOG_ERR("Unexpected response to " << wopiLog << " : " << response.getStatus() <<
|
|
|
|
"Cannot save file to WOPI storage uri [" << uriAnonym << "]. Error: ");
|
|
|
|
saveResult.setResult(StorageBase::SaveResult::FAILED);
|
|
|
|
}
|
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: " <<
|
|
|
|
pexc.displayText() << (pexc.nested() ? " (" + pexc.nested()->displayText() + ")" : ""));
|
2017-10-25 07:09:27 -05:00
|
|
|
saveResult.setResult(StorageBase::SaveResult::FAILED);
|
2016-11-23 06:09:54 -06:00
|
|
|
}
|
2019-12-23 07:45:38 -06:00
|
|
|
catch (const BadRequestException& exc)
|
|
|
|
{
|
|
|
|
LOG_ERR("Cannot save file to WOPI storage uri [" + uriAnonym + "]. Error: Failed HTPP request authorization");
|
|
|
|
}
|
2016-11-23 06:09:54 -06:00
|
|
|
|
|
|
|
return saveResult;
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:04:26 -06:00
|
|
|
std::string WebDAVStorage::loadStorageFileToLocal(const Authorization& /*auth*/,
|
|
|
|
const std::string& /*cookies*/,
|
|
|
|
LockContext& /*lockCtx*/,
|
|
|
|
const std::string& /*templateUri*/)
|
2016-03-31 01:48:34 -05:00
|
|
|
{
|
|
|
|
// TODO: implement webdav GET.
|
2018-11-28 02:07:29 -06:00
|
|
|
setLoaded(true);
|
|
|
|
return getUri().toString();
|
2016-03-31 01:48:34 -05:00
|
|
|
}
|
|
|
|
|
2020-01-18 15:04:26 -06:00
|
|
|
StorageBase::SaveResult
|
|
|
|
WebDAVStorage::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
|
|
|
{
|
|
|
|
// TODO: implement webdav PUT.
|
2017-10-25 07:09:27 -05:00
|
|
|
return StorageBase::SaveResult(StorageBase::SaveResult::OK);
|
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: */
|