dbaccess/source/core/dataaccess/ModelImpl.cxx | 93 +++++++++++++++++++ dbaccess/source/core/dataaccess/databasedocument.cxx | 90 ------------------ dbaccess/source/core/inc/ModelImpl.hxx | 2 3 files changed, 95 insertions(+), 90 deletions(-)
New commits: commit 77f02e09a9ef9f4c53dfcec481aa0f35c763beb0 Author: Samuel Mehrbrodt <samuel.mehrbr...@cib.de> AuthorDate: Mon Apr 20 16:10:03 2020 +0200 Commit: Thorsten Behrens <thorsten.behr...@cib.de> CommitDate: Fri May 1 17:54:31 2020 +0200 Fix macro signature getting lost when inserting data into table Change-Id: Iec514bfc0edd4f10a09809c68b8501667d978e52 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/92573 Tested-by: Jenkins Reviewed-by: Thorsten Behrens <thorsten.behr...@cib.de> (cherry picked from commit 919bd3e78cce84a3338208e81d7056d8379d1bd5) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/93189 diff --git a/dbaccess/source/core/dataaccess/ModelImpl.cxx b/dbaccess/source/core/dataaccess/ModelImpl.cxx index 083609f5333c..b88e3b06b761 100644 --- a/dbaccess/source/core/dataaccess/ModelImpl.cxx +++ b/dbaccess/source/core/dataaccess/ModelImpl.cxx @@ -56,12 +56,14 @@ #include <sfx2/signaturestate.hxx> #include <tools/debug.hxx> #include <tools/diagnose_ex.h> +#include <osl/file.hxx> #include <osl/diagnose.h> #include <sal/log.hxx> #include <vcl/errcode.hxx> #include <tools/urlobj.hxx> #include <unotools/sharedunocomponent.hxx> #include <unotools/configmgr.hxx> +#include <unotools/tempfile.hxx> #include <i18nlangtag/languagetag.hxx> #include <algorithm> @@ -849,6 +851,23 @@ bool ODatabaseModelImpl::commitEmbeddedStorage( bool _bPreventRootCommits ) bool ODatabaseModelImpl::commitStorageIfWriteable_ignoreErrors( const Reference< XStorage >& _rxStorage ) { + bool bTryToPreserveScriptSignature = false; + utl::TempFile aTempFile; + aTempFile.EnableKillingFile(); + OUString m_sTmpFileUrl = aTempFile.GetURL(); + SignatureState aSignatureState = getScriptingSignatureState(); + if (aSignatureState == SignatureState::OK + || aSignatureState == SignatureState::NOTVALIDATED + || aSignatureState == 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(getDocFileLocation(), m_sTmpFileUrl); + if (rc != osl::FileBase::E_None) + throw uno::RuntimeException("Could not create temp file"); + } + bool bSuccess = false; try { @@ -858,6 +877,80 @@ bool ODatabaseModelImpl::commitStorageIfWriteable_ignoreErrors( const Reference< { DBG_UNHANDLED_EXCEPTION("dbaccess"); } + + // 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(); + OUString aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(_rxStorage)); + uno::Reference<security::XDocumentDigitalSignatures> xDDSigns; + try + { + 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, m_sTmpFileUrl, ElementModes::READ); + if (!xReadOrig.is()) + throw uno::RuntimeException("Could not read " + m_sTmpFileUrl); + uno::Reference<embed::XStorage> xMetaInf + = xReadOrig->openStorageElement("META-INF", embed::ElementModes::READ); + + OUString aURL = getDocFileLocation(); + Reference<XStorage> xTarget + = comphelper::OStorageHelper::GetStorageOfFormatFromURL( + ZIP_STORAGE_FORMAT_STRING, aURL, ElementModes::READWRITE); + if (!xTarget.is()) + throw uno::RuntimeException("Could not read " + aURL); + 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&) + { + TOOLS_WARN_EXCEPTION("dbaccess", ""); + } + } + return bSuccess; } diff --git a/dbaccess/source/core/dataaccess/databasedocument.cxx b/dbaccess/source/core/dataaccess/databasedocument.cxx index 1888634776af..554233d74f2a 100644 --- a/dbaccess/source/core/dataaccess/databasedocument.cxx +++ b/dbaccess/source/core/dataaccess/databasedocument.cxx @@ -1040,22 +1040,6 @@ 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 @@ -1098,81 +1082,9 @@ void ODatabaseDocument::impl_storeAs_throw( const OUString& _rURL, const ::comph // store to current storage 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 - { - 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&) - { - TOOLS_WARN_EXCEPTION("dbaccess", ""); - } - } - // success - tell our impl m_pImpl->setDocFileLocation( _rURL ); m_pImpl->setResource( _rURL, aMediaDescriptor ); @@ -1331,7 +1243,7 @@ void ODatabaseDocument::impl_storeToStorage_throw( const Reference< XStorage >& lcl_triggerStatusIndicator_throw( aWriteArgs, _rDocGuard, false ); // commit target storage - OSL_VERIFY( tools::stor::commitStorageIfWriteable( _rxTargetStorage ) ); + m_pImpl->commitStorageIfWriteable_ignoreErrors(_rxTargetStorage); } catch( const IOException& ) { throw; } catch( const RuntimeException& ) { throw; } diff --git a/dbaccess/source/core/inc/ModelImpl.hxx b/dbaccess/source/core/inc/ModelImpl.hxx index d28899d42ed2..74cc11eb79c0 100644 --- a/dbaccess/source/core/inc/ModelImpl.hxx +++ b/dbaccess/source/core/inc/ModelImpl.hxx @@ -301,7 +301,7 @@ public: void commitRootStorage(); /// commits a given storage if it's not readonly, ignoring (but asserting) all errors - static bool commitStorageIfWriteable_ignoreErrors( + bool commitStorageIfWriteable_ignoreErrors( const css::uno::Reference< css::embed::XStorage >& _rxStorage ); _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits