dbaccess/source/core/dataaccess/databasedocument.cxx | 96 ++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-)
New commits: commit 4cabddd4dd838f5b1a8b64dd8d6c8da7563a1c01 Author: Samuel Mehrbrodt <[email protected]> AuthorDate: Tue Apr 7 09:05:05 2020 +0200 Commit: Thorsten Behrens <[email protected]> CommitDate: Fri Jun 26 16:53:29 2020 +0200 Related tdf#97694 Fix macro preservation on Windows Reviewed-on: https://gerrit.libreoffice.org/c/core/+/91795 Tested-by: Jenkins Reviewed-by: Samuel Mehrbrodt <[email protected]> (cherry picked from commit 792c730f3cda4f5c312921a62ecae3d322ad9b72) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/91920 Tested-by: Thorsten Behrens <[email protected]> Reviewed-by: Thorsten Behrens <[email protected]> (cherry picked from commit 984a240975b87eff10ccda394de9e7ea0078ed30) Change-Id: I9c91c98417503084cacbabb0bc4071286e6fc9eb Reviewed-on: https://gerrit.libreoffice.org/c/core/+/97122 diff --git a/dbaccess/source/core/dataaccess/databasedocument.cxx b/dbaccess/source/core/dataaccess/databasedocument.cxx index cc571a0a6457..f76104e93aa9 100644 --- a/dbaccess/source/core/dataaccess/databasedocument.cxx +++ b/dbaccess/source/core/dataaccess/databasedocument.cxx @@ -1093,18 +1093,21 @@ void ODatabaseDocument::impl_storeAs_throw( const OUString& _rURL, const ::comph } // store to current storage - Reference< XStorage > xCurrentStorage( m_pImpl->getOrCreateRootStorage(), UNO_QUERY_THROW ); + Reference< XStorage > xCurrentStorage( m_pImpl->getOrCreateRootStorage(), UNO_SET_THROW ); + OUString aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(xCurrentStorage)); Sequence< PropertyValue > aMediaDescriptor( lcl_appendFileNameToDescriptor( _rArguments, _rURL ) ); impl_storeToStorage_throw( xCurrentStorage, aMediaDescriptor, _rGuard ); // Preserve script signature if the script has not changed if (bTryToPreserveScriptSignature) { + // Need to close this storage, otherwise we can't open it for signing below + // (Windows needs exclusive file access) + uno::Reference < lang::XComponent > xComp = xCurrentStorage; + xComp->dispose(); uno::Reference<security::XDocumentDigitalSignatures> xDDSigns; try { - OUString aODFVersion( - comphelper::OStorageHelper::GetODFVersionFromStorage(xCurrentStorage)); xDDSigns = security::DocumentDigitalSignatures::createWithVersion( comphelper::getProcessComponentContext(), aODFVersion); commit e23418ddbd45a910f074e81cda7aa892c4fc5f19 Author: Samuel Mehrbrodt <[email protected]> AuthorDate: Wed Apr 1 09:58:09 2020 +0200 Commit: Thorsten Behrens <[email protected]> CommitDate: Fri Jun 26 16:53:17 2020 +0200 Related tdf#97694 Preserve macro signatures in Base Reviewed-on: https://gerrit.libreoffice.org/c/core/+/91466 Tested-by: Jenkins Reviewed-by: Samuel Mehrbrodt <[email protected]> (cherry picked from commit 8d27d495a733cfc29753e2e78000b199632695f7) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/91919 Tested-by: Thorsten Behrens <[email protected]> Reviewed-by: Thorsten Behrens <[email protected]> (cherry picked from commit 8a7d8eeeb0ea75881913b7c672cb4bc5d8893114) Change-Id: I69d408ad105759877a3d2786d3bc224abed83b64 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/97121 diff --git a/dbaccess/source/core/dataaccess/databasedocument.cxx b/dbaccess/source/core/dataaccess/databasedocument.cxx index eb20c56ed849..cc571a0a6457 100644 --- a/dbaccess/source/core/dataaccess/databasedocument.cxx +++ b/dbaccess/source/core/dataaccess/databasedocument.cxx @@ -46,6 +46,8 @@ #include <com/sun/star/io/XTruncate.hpp> #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp> #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp> +#include <com/sun/star/security/DocumentDigitalSignatures.hpp> +#include <com/sun/star/security/XDocumentDigitalSignatures.hpp> #include <com/sun/star/sdb/DatabaseContext.hpp> #include <com/sun/star/sdb/application/XDatabaseDocumentUI.hpp> #include <com/sun/star/task/XStatusIndicator.hpp> @@ -70,7 +72,7 @@ #include <comphelper/numberedcollection.hxx> #include <comphelper/property.hxx> #include <comphelper/storagehelper.hxx> - +#include <comphelper/processfactory.hxx> #include <connectivity/dbtools.hxx> #include <cppuhelper/exc_hlp.hxx> @@ -78,7 +80,9 @@ #include <framework/titlehelper.hxx> #include <unotools/saveopt.hxx> #include <tools/debug.hxx> +#include <unotools/tempfile.hxx> #include <tools/diagnose_ex.h> +#include <osl/file.hxx> #include <osl/diagnose.h> #include <vcl/errcode.hxx> @@ -1025,6 +1029,22 @@ void ODatabaseDocument::impl_storeAs_throw( const OUString& _rURL, const ::comph _rGuard.reset(); } + bool bTryToPreserveScriptSignature = false; + utl::TempFile aTempFile; + aTempFile.EnableKillingFile(); + OUString aTmpFileURL = aTempFile.GetURL(); + if (m_pImpl->getScriptingSignatureState() == SignatureState::OK + || m_pImpl->getScriptingSignatureState() == SignatureState::NOTVALIDATED + || m_pImpl->getScriptingSignatureState() == SignatureState::INVALID) + { + bTryToPreserveScriptSignature = true; + // We need to first save the file (which removes the macro signature), then add the macro signature again. + // For that, we need a temporary copy of the original file. + osl::File::RC rc = osl::File::copy(m_pImpl->getDocFileLocation(), aTmpFileURL); + if (rc != osl::FileBase::E_None) + throw uno::RuntimeException("Could not create temp file"); + } + Reference< XStorage > xNewRootStorage; // will be non-NULL if our storage changed @@ -1077,6 +1097,75 @@ void ODatabaseDocument::impl_storeAs_throw( const OUString& _rURL, const ::comph Sequence< PropertyValue > aMediaDescriptor( lcl_appendFileNameToDescriptor( _rArguments, _rURL ) ); impl_storeToStorage_throw( xCurrentStorage, aMediaDescriptor, _rGuard ); + // Preserve script signature if the script has not changed + if (bTryToPreserveScriptSignature) + { + uno::Reference<security::XDocumentDigitalSignatures> xDDSigns; + try + { + OUString aODFVersion( + comphelper::OStorageHelper::GetODFVersionFromStorage(xCurrentStorage)); + xDDSigns = security::DocumentDigitalSignatures::createWithVersion( + comphelper::getProcessComponentContext(), aODFVersion); + + const OUString aScriptSignName + = xDDSigns->getScriptingContentSignatureDefaultStreamName(); + + if (!aScriptSignName.isEmpty()) + { + Reference<XStorage> xReadOrig + = comphelper::OStorageHelper::GetStorageOfFormatFromURL( + ZIP_STORAGE_FORMAT_STRING, aTmpFileURL, ElementModes::READ); + if (!xReadOrig.is()) + throw uno::RuntimeException("Could not read " + aTmpFileURL); + uno::Reference<embed::XStorage> xMetaInf + = xReadOrig->openStorageElement("META-INF", embed::ElementModes::READ); + + Reference<XStorage> xTarget + = comphelper::OStorageHelper::GetStorageOfFormatFromURL( + ZIP_STORAGE_FORMAT_STRING, _rURL, ElementModes::READWRITE); + if (!xTarget.is()) + throw uno::RuntimeException("Could not read " + _rURL); + uno::Reference<embed::XStorage> xTargetMetaInf + = xTarget->openStorageElement("META-INF", embed::ElementModes::READWRITE); + + if (xMetaInf.is() && xTargetMetaInf.is()) + { + xMetaInf->copyElementTo(aScriptSignName, xTargetMetaInf, aScriptSignName); + + uno::Reference<embed::XTransactedObject> xTransact(xTargetMetaInf, + uno::UNO_QUERY); + if (xTransact.is()) + xTransact->commit(); + + xTargetMetaInf->dispose(); + + // now check the copied signature + uno::Sequence<security::DocumentSignatureInformation> aInfos + = xDDSigns->verifyScriptingContentSignatures( + xTarget, uno::Reference<io::XInputStream>()); + SignatureState nState = DocumentSignatures::getSignatureState(aInfos); + if (nState == SignatureState::OK || nState == SignatureState::NOTVALIDATED + || nState == SignatureState::PARTIAL_OK) + { + // commit the ZipStorage from target medium + xTransact.set(xTarget, uno::UNO_QUERY); + if (xTransact.is()) + xTransact->commit(); + } + else + { + SAL_WARN("dbaccess", "An invalid signature was copied!"); + } + } + } + } + catch (uno::Exception&) + { + SAL_WARN("dbaccess", "Preserving macro signature failed!"); + } + } + // success - tell our impl m_pImpl->setDocFileLocation( _rURL ); m_pImpl->setResource( _rURL, aMediaDescriptor ); _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
