Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / doc / docmacromode.cxx
blobc49f7cec00c61f14653bf9556f8dce4af9d069a9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include <sfx2/docmacromode.hxx>
23 #include <sfx2/signaturestate.hxx>
24 #include <sfx2/docfile.hxx>
26 #include <com/sun/star/document/MacroExecMode.hpp>
27 #include <com/sun/star/task/ErrorCodeRequest.hpp>
28 #include <com/sun/star/task/DocumentMacroConfirmationRequest.hpp>
29 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
30 #include <com/sun/star/script/XLibraryContainer.hpp>
31 #include <com/sun/star/document/XEmbeddedScripts.hpp>
33 #include <comphelper/processfactory.hxx>
34 #include <framework/interaction.hxx>
35 #include <osl/file.hxx>
36 #include <unotools/securityoptions.hxx>
37 #include <svtools/sfxecode.hxx>
38 #include <tools/diagnose_ex.h>
39 #include <tools/urlobj.hxx>
42 namespace sfx2
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::task::XInteractionHandler;
48 using ::com::sun::star::uno::Any;
49 using ::com::sun::star::uno::Sequence;
50 using ::com::sun::star::task::DocumentMacroConfirmationRequest;
51 using ::com::sun::star::task::ErrorCodeRequest;
52 using ::com::sun::star::uno::Exception;
53 using ::com::sun::star::security::DocumentDigitalSignatures;
54 using ::com::sun::star::security::XDocumentDigitalSignatures;
55 using ::com::sun::star::embed::XStorage;
56 using ::com::sun::star::document::XEmbeddedScripts;
57 using ::com::sun::star::script::XLibraryContainer;
58 using ::com::sun::star::container::XNameAccess;
59 using ::com::sun::star::uno::UNO_QUERY_THROW;
61 namespace MacroExecMode = ::com::sun::star::document::MacroExecMode;
64 //= DocumentMacroMode_Data
66 struct DocumentMacroMode_Data
68 IMacroDocumentAccess& m_rDocumentAccess;
69 bool m_bMacroDisabledMessageShown;
70 bool m_bDocMacroDisabledMessageShown;
72 explicit DocumentMacroMode_Data( IMacroDocumentAccess& rDocumentAccess )
73 :m_rDocumentAccess( rDocumentAccess )
74 ,m_bMacroDisabledMessageShown( false )
75 ,m_bDocMacroDisabledMessageShown( false )
81 //= helper
83 namespace
86 void lcl_showGeneralSfxErrorOnce( const Reference< XInteractionHandler >& rxHandler, ErrCode nSfxErrorCode, bool& rbAlreadyShown )
88 if ( rbAlreadyShown )
89 return;
91 ErrorCodeRequest aErrorCodeRequest;
92 aErrorCodeRequest.ErrCode = sal_uInt32(nSfxErrorCode);
94 SfxMedium::CallApproveHandler( rxHandler, makeAny( aErrorCodeRequest ), false );
95 rbAlreadyShown = true;
99 void lcl_showMacrosDisabledError( const Reference< XInteractionHandler >& rxHandler, bool& rbAlreadyShown )
101 lcl_showGeneralSfxErrorOnce( rxHandler, ERRCODE_SFX_MACROS_SUPPORT_DISABLED, rbAlreadyShown );
105 void lcl_showDocumentMacrosDisabledError( const Reference< XInteractionHandler >& rxHandler, bool& rbAlreadyShown )
107 #ifdef MACOSX
108 lcl_showGeneralSfxErrorOnce( rxHandler, ERRCODE_SFX_DOCUMENT_MACRO_DISABLED_MAC, rbAlreadyShown );
109 #else
110 lcl_showGeneralSfxErrorOnce( rxHandler, ERRCODE_SFX_DOCUMENT_MACRO_DISABLED, rbAlreadyShown );
111 #endif
114 void lcl_showMacrosDisabledUnsignedContentError( const Reference< XInteractionHandler >& rxHandler, bool& rbAlreadyShown )
116 lcl_showGeneralSfxErrorOnce( rxHandler, ERRCODE_SFX_DOCUMENT_MACRO_DISABLED_CONTENT_UNSIGNED, rbAlreadyShown );
119 bool lcl_showMacroWarning( const Reference< XInteractionHandler >& rxHandler,
120 const OUString& rDocumentLocation )
122 DocumentMacroConfirmationRequest aRequest;
123 aRequest.DocumentURL = rDocumentLocation;
124 return SfxMedium::CallApproveHandler( rxHandler, makeAny( aRequest ), true );
128 //= DocumentMacroMode
129 DocumentMacroMode::DocumentMacroMode( IMacroDocumentAccess& rDocumentAccess )
130 :m_xData( std::make_shared<DocumentMacroMode_Data>( rDocumentAccess ) ),
131 m_bNeedsContentSigned(false)
135 bool DocumentMacroMode::allowMacroExecution()
137 m_xData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::ALWAYS_EXECUTE_NO_WARN );
138 return true;
141 bool DocumentMacroMode::disallowMacroExecution()
143 m_xData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::NEVER_EXECUTE );
144 return false;
147 bool DocumentMacroMode::adjustMacroMode( const Reference< XInteractionHandler >& rxInteraction, bool bHasValidContentSignature )
149 sal_uInt16 nMacroExecutionMode = m_xData->m_rDocumentAccess.getCurrentMacroExecMode();
151 if ( SvtSecurityOptions().IsMacroDisabled() )
153 // no macro should be executed at all
154 lcl_showMacrosDisabledError( rxInteraction, m_xData->m_bMacroDisabledMessageShown );
155 return disallowMacroExecution();
158 // get setting from configuration if required
159 enum AutoConfirmation
161 eNoAutoConfirm,
162 eAutoConfirmApprove,
163 eAutoConfirmReject
165 AutoConfirmation eAutoConfirm( eNoAutoConfirm );
167 if ( ( nMacroExecutionMode == MacroExecMode::USE_CONFIG )
168 || ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION )
169 || ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION )
172 // check confirm first, as nMacroExecutionMode is always overwritten by the GetMacroSecurityLevel() switch
173 if (nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION)
174 eAutoConfirm = eAutoConfirmReject;
175 else if (nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION)
176 eAutoConfirm = eAutoConfirmApprove;
178 SvtSecurityOptions aOpt;
179 switch ( aOpt.GetMacroSecurityLevel() )
181 case 3:
182 nMacroExecutionMode = MacroExecMode::FROM_LIST_NO_WARN;
183 break;
184 case 2:
185 nMacroExecutionMode = MacroExecMode::FROM_LIST_AND_SIGNED_WARN;
186 break;
187 case 1:
188 nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE;
189 break;
190 case 0:
191 nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
192 break;
193 default:
194 OSL_FAIL( "DocumentMacroMode::adjustMacroMode: unexpected macro security level!" );
195 nMacroExecutionMode = MacroExecMode::NEVER_EXECUTE;
199 if ( nMacroExecutionMode == MacroExecMode::NEVER_EXECUTE )
200 return false;
202 if ( nMacroExecutionMode == MacroExecMode::ALWAYS_EXECUTE_NO_WARN )
203 return true;
207 // get document location from medium name and check whether it is a trusted one
208 // the service is created without document version, since it is not of interest here
209 Reference< XDocumentDigitalSignatures > xSignatures(DocumentDigitalSignatures::createDefault(::comphelper::getProcessComponentContext()));
210 INetURLObject aURLReferer( m_xData->m_rDocumentAccess.getDocumentLocation() );
212 OUString aLocation;
213 if ( aURLReferer.removeSegment() )
214 aLocation = aURLReferer.GetMainURL( INetURLObject::DecodeMechanism::NONE );
216 if ( !aLocation.isEmpty() && xSignatures->isLocationTrusted( aLocation ) )
218 return allowMacroExecution();
221 // at this point it is clear that the document is not in the secure location
222 if ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN )
224 lcl_showDocumentMacrosDisabledError( rxInteraction, m_xData->m_bDocMacroDisabledMessageShown );
225 return disallowMacroExecution();
228 // check whether the document is signed with trusted certificate
229 if ( nMacroExecutionMode != MacroExecMode::FROM_LIST )
231 // the trusted macro check will also retrieve the signature state ( small optimization )
232 const SvtSecurityOptions aSecOption;
233 const bool bAllowUIToAddAuthor = nMacroExecutionMode != MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN
234 && (nMacroExecutionMode == MacroExecMode::ALWAYS_EXECUTE
235 || !aSecOption.IsReadOnly(SvtSecurityOptions::EOption::MacroTrustedAuthors));
236 const bool bHasTrustedMacroSignature = m_xData->m_rDocumentAccess.hasTrustedScriptingSignature(bAllowUIToAddAuthor);
238 SignatureState nSignatureState = m_xData->m_rDocumentAccess.getScriptingSignatureState();
239 if ( nSignatureState == SignatureState::BROKEN )
241 if (!bAllowUIToAddAuthor)
242 lcl_showDocumentMacrosDisabledError(rxInteraction, m_xData->m_bDocMacroDisabledMessageShown);
243 return disallowMacroExecution();
245 else if ( m_xData->m_rDocumentAccess.macroCallsSeenWhileLoading() &&
246 bHasTrustedMacroSignature &&
247 !bHasValidContentSignature)
249 // When macros are signed, and the document has events which call macros, the document content needs to be signed too.
250 lcl_showMacrosDisabledUnsignedContentError(rxInteraction, m_xData->m_bDocMacroDisabledMessageShown);
251 return disallowMacroExecution();
253 else if ( bHasTrustedMacroSignature )
255 // there is trusted macro signature, allow macro execution
256 return allowMacroExecution();
258 else if ( nSignatureState == SignatureState::OK
259 || nSignatureState == SignatureState::NOTVALIDATED )
261 // there is valid signature, but it is not from the trusted author
262 if (!bAllowUIToAddAuthor)
263 lcl_showDocumentMacrosDisabledError(rxInteraction, m_xData->m_bDocMacroDisabledMessageShown);
264 return disallowMacroExecution();
268 // at this point it is clear that the document is neither in secure location nor signed with trusted certificate
269 if ( ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
270 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
273 if ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
274 lcl_showDocumentMacrosDisabledError( rxInteraction, m_xData->m_bDocMacroDisabledMessageShown );
276 return disallowMacroExecution();
279 catch ( const Exception& )
281 if ( ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN )
282 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
283 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
286 return disallowMacroExecution();
290 // confirmation is required
291 bool bSecure = false;
293 if ( eAutoConfirm == eNoAutoConfirm )
295 OUString sReferrer( m_xData->m_rDocumentAccess.getDocumentLocation() );
297 OUString aSystemFileURL;
298 if ( osl::FileBase::getSystemPathFromFileURL( sReferrer, aSystemFileURL ) == osl::FileBase::E_None )
299 sReferrer = aSystemFileURL;
301 bSecure = lcl_showMacroWarning( rxInteraction, sReferrer );
303 else
304 bSecure = ( eAutoConfirm == eAutoConfirmApprove );
306 return ( bSecure ? allowMacroExecution() : disallowMacroExecution() );
310 bool DocumentMacroMode::isMacroExecutionDisallowed() const
312 return m_xData->m_rDocumentAccess.getCurrentMacroExecMode() == MacroExecMode::NEVER_EXECUTE;
316 bool DocumentMacroMode::containerHasBasicMacros( const Reference< XLibraryContainer >& xContainer )
318 bool bHasMacroLib = false;
321 if ( xContainer.is() )
323 // a library container exists; check if it's empty
325 // if there are libraries except the "Standard" library
326 // we assume that they are not empty (because they have been created by the user)
327 if ( !xContainer->hasElements() )
328 bHasMacroLib = false;
329 else
331 const OUString aStdLibName( "Standard" );
332 const OUString aVBAProject( "VBAProject" );
333 const Sequence< OUString > aElements = xContainer->getElementNames();
334 for( const OUString& aElement : aElements )
336 if( aElement == aStdLibName || aElement == aVBAProject )
338 Reference < XNameAccess > xLib;
339 Any aAny = xContainer->getByName( aElement );
340 aAny >>= xLib;
341 if ( xLib.is() && xLib->hasElements() )
342 return true;
344 else
345 return true;
350 catch( const Exception& )
352 DBG_UNHANDLED_EXCEPTION("sfx.doc");
354 return bHasMacroLib;
358 bool DocumentMacroMode::hasMacroLibrary() const
360 bool bHasMacroLib = false;
361 #if HAVE_FEATURE_SCRIPTING
364 Reference< XEmbeddedScripts > xScripts( m_xData->m_rDocumentAccess.getEmbeddedDocumentScripts() );
365 Reference< XLibraryContainer > xContainer;
366 if ( xScripts.is() )
367 xContainer.set( xScripts->getBasicLibraries(), UNO_QUERY_THROW );
368 bHasMacroLib = containerHasBasicMacros( xContainer );
371 catch( const Exception& )
373 DBG_UNHANDLED_EXCEPTION("sfx.doc");
375 #endif
376 return bHasMacroLib;
380 bool DocumentMacroMode::storageHasMacros( const Reference< XStorage >& rxStorage )
382 bool bHasMacros = false;
383 if ( rxStorage.is() )
387 const OUString s_sBasicStorageName( OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "Basic" ) ) );
388 const OUString s_sScriptsStorageName( OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "Scripts" ) ) );
390 bHasMacros =( ( rxStorage->hasByName( s_sBasicStorageName )
391 && rxStorage->isStorageElement( s_sBasicStorageName )
393 || ( rxStorage->hasByName( s_sScriptsStorageName )
394 && rxStorage->isStorageElement( s_sScriptsStorageName )
398 catch ( const Exception& )
400 DBG_UNHANDLED_EXCEPTION("sfx.doc");
403 return bHasMacros;
407 bool DocumentMacroMode::checkMacrosOnLoading( const Reference< XInteractionHandler >& rxInteraction, bool bHasValidContentSignature )
409 bool bAllow = false;
410 if ( SvtSecurityOptions().IsMacroDisabled() )
412 // no macro should be executed at all
413 bAllow = disallowMacroExecution();
415 else
417 if (m_xData->m_rDocumentAccess.documentStorageHasMacros() || hasMacroLibrary() || m_xData->m_rDocumentAccess.macroCallsSeenWhileLoading())
419 if (m_xData->m_rDocumentAccess.macroCallsSeenWhileLoading())
420 m_bNeedsContentSigned = true;
421 bAllow = adjustMacroMode( rxInteraction, bHasValidContentSignature );
423 else if ( !isMacroExecutionDisallowed() )
425 // if macros will be added by the user later, the security check is obsolete
426 bAllow = allowMacroExecution();
429 return bAllow;
433 } // namespace sfx2
436 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */