lok: impress: show only supported transitions in the side pane
[LibreOffice.git] / sfx2 / source / doc / docmacromode.cxx
blob13954b06f6d87651673d98dc69404bd8ded960ad
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/task/InteractionClassification.hpp>
30 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
31 #include <com/sun/star/script/XLibraryContainer.hpp>
32 #include <com/sun/star/document/XEmbeddedScripts.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <framework/interaction.hxx>
36 #include <osl/file.hxx>
37 #include <rtl/ref.hxx>
38 #include <unotools/securityoptions.hxx>
39 #include <svtools/sfxecode.hxx>
40 #include <tools/diagnose_ex.h>
41 #include <tools/urlobj.hxx>
44 namespace sfx2
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::task::XInteractionHandler;
50 using ::com::sun::star::uno::Any;
51 using ::com::sun::star::uno::Sequence;
52 using ::com::sun::star::task::DocumentMacroConfirmationRequest;
53 using ::com::sun::star::task::ErrorCodeRequest;
54 using ::com::sun::star::uno::Exception;
55 using ::com::sun::star::security::DocumentDigitalSignatures;
56 using ::com::sun::star::security::XDocumentDigitalSignatures;
57 using ::com::sun::star::embed::XStorage;
58 using ::com::sun::star::document::XEmbeddedScripts;
59 using ::com::sun::star::script::XLibraryContainer;
60 using ::com::sun::star::container::XNameAccess;
61 using ::com::sun::star::uno::UNO_QUERY_THROW;
63 namespace MacroExecMode = ::com::sun::star::document::MacroExecMode;
66 //= DocumentMacroMode_Data
68 struct DocumentMacroMode_Data
70 IMacroDocumentAccess& m_rDocumentAccess;
71 bool m_bMacroDisabledMessageShown;
72 bool m_bDocMacroDisabledMessageShown;
74 explicit DocumentMacroMode_Data( IMacroDocumentAccess& rDocumentAccess )
75 :m_rDocumentAccess( rDocumentAccess )
76 ,m_bMacroDisabledMessageShown( false )
77 ,m_bDocMacroDisabledMessageShown( false )
83 //= helper
85 namespace
88 void lcl_showGeneralSfxErrorOnce( const Reference< XInteractionHandler >& rxHandler, ErrCode nSfxErrorCode, bool& rbAlreadyShown )
90 if ( rbAlreadyShown )
91 return;
93 ErrorCodeRequest aErrorCodeRequest;
94 aErrorCodeRequest.ErrCode = sal_uInt32(nSfxErrorCode);
96 SfxMedium::CallApproveHandler( rxHandler, makeAny( aErrorCodeRequest ), false );
97 rbAlreadyShown = true;
101 void lcl_showMacrosDisabledError( const Reference< XInteractionHandler >& rxHandler, bool& rbAlreadyShown )
103 lcl_showGeneralSfxErrorOnce( rxHandler, ERRCODE_SFX_MACROS_SUPPORT_DISABLED, rbAlreadyShown );
107 void lcl_showDocumentMacrosDisabledError( const Reference< XInteractionHandler >& rxHandler, bool& rbAlreadyShown )
109 #ifdef MACOSX
110 lcl_showGeneralSfxErrorOnce( rxHandler, ERRCODE_SFX_DOCUMENT_MACRO_DISABLED_MAC, rbAlreadyShown );
111 #else
112 lcl_showGeneralSfxErrorOnce( rxHandler, ERRCODE_SFX_DOCUMENT_MACRO_DISABLED, rbAlreadyShown );
113 #endif
117 bool lcl_showMacroWarning( const Reference< XInteractionHandler >& rxHandler,
118 const OUString& rDocumentLocation )
120 DocumentMacroConfirmationRequest aRequest;
121 aRequest.DocumentURL = rDocumentLocation;
122 return SfxMedium::CallApproveHandler( rxHandler, makeAny( aRequest ), true );
126 //= DocumentMacroMode
127 DocumentMacroMode::DocumentMacroMode( IMacroDocumentAccess& rDocumentAccess )
128 :m_xData( new DocumentMacroMode_Data( rDocumentAccess ) )
132 bool DocumentMacroMode::allowMacroExecution()
134 m_xData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::ALWAYS_EXECUTE_NO_WARN );
135 return true;
138 bool DocumentMacroMode::disallowMacroExecution()
140 m_xData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::NEVER_EXECUTE );
141 return false;
144 bool DocumentMacroMode::adjustMacroMode( const Reference< XInteractionHandler >& rxInteraction )
146 sal_uInt16 nMacroExecutionMode = m_xData->m_rDocumentAccess.getCurrentMacroExecMode();
148 if ( SvtSecurityOptions().IsMacroDisabled() )
150 // no macro should be executed at all
151 lcl_showMacrosDisabledError( rxInteraction, m_xData->m_bMacroDisabledMessageShown );
152 return disallowMacroExecution();
155 // get setting from configuration if required
156 enum AutoConfirmation
158 eNoAutoConfirm,
159 eAutoConfirmApprove,
160 eAutoConfirmReject
162 AutoConfirmation eAutoConfirm( eNoAutoConfirm );
164 if ( ( nMacroExecutionMode == MacroExecMode::USE_CONFIG )
165 || ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION )
166 || ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION )
169 SvtSecurityOptions aOpt;
170 switch ( aOpt.GetMacroSecurityLevel() )
172 case 3:
173 nMacroExecutionMode = MacroExecMode::FROM_LIST_NO_WARN;
174 break;
175 case 2:
176 nMacroExecutionMode = MacroExecMode::FROM_LIST_AND_SIGNED_WARN;
177 break;
178 case 1:
179 nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE;
180 break;
181 case 0:
182 nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
183 break;
184 default:
185 OSL_FAIL( "DocumentMacroMode::adjustMacroMode: unexpected macro security level!" );
186 nMacroExecutionMode = MacroExecMode::NEVER_EXECUTE;
189 if ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION )
190 eAutoConfirm = eAutoConfirmReject;
191 else if ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION )
192 eAutoConfirm = eAutoConfirmApprove;
195 if ( nMacroExecutionMode == MacroExecMode::NEVER_EXECUTE )
196 return false;
198 if ( nMacroExecutionMode == MacroExecMode::ALWAYS_EXECUTE_NO_WARN )
199 return true;
203 // get document location from medium name and check whether it is a trusted one
204 // the service is created without document version, since it is not of interest here
205 Reference< XDocumentDigitalSignatures > xSignatures(DocumentDigitalSignatures::createDefault(::comphelper::getProcessComponentContext()));
206 INetURLObject aURLReferer( m_xData->m_rDocumentAccess.getDocumentLocation() );
208 OUString aLocation;
209 if ( aURLReferer.removeSegment() )
210 aLocation = aURLReferer.GetMainURL( INetURLObject::DecodeMechanism::NONE );
212 if ( !aLocation.isEmpty() && xSignatures->isLocationTrusted( aLocation ) )
214 return allowMacroExecution();
217 // at this point it is clear that the document is not in the secure location
218 if ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN )
220 lcl_showDocumentMacrosDisabledError( rxInteraction, m_xData->m_bDocMacroDisabledMessageShown );
221 return disallowMacroExecution();
224 // check whether the document is signed with trusted certificate
225 if ( nMacroExecutionMode != MacroExecMode::FROM_LIST )
227 // the trusted macro check will also retrieve the signature state ( small optimization )
228 bool bHasTrustedMacroSignature = m_xData->m_rDocumentAccess.hasTrustedScriptingSignature( nMacroExecutionMode != MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN );
230 SignatureState nSignatureState = m_xData->m_rDocumentAccess.getScriptingSignatureState();
231 if ( nSignatureState == SignatureState::BROKEN )
233 return disallowMacroExecution();
235 else if ( bHasTrustedMacroSignature )
237 // there is trusted macro signature, allow macro execution
238 return allowMacroExecution();
240 else if ( nSignatureState == SignatureState::OK
241 || nSignatureState == SignatureState::NOTVALIDATED )
243 // there is valid signature, but it is not from the trusted author
244 return disallowMacroExecution();
248 // at this point it is clear that the document is neither in secure location nor signed with trusted certificate
249 if ( ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
250 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
253 if ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
254 lcl_showDocumentMacrosDisabledError( rxInteraction, m_xData->m_bDocMacroDisabledMessageShown );
256 return disallowMacroExecution();
259 catch ( const Exception& )
261 if ( ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN )
262 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
263 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
266 return disallowMacroExecution();
270 // confirmation is required
271 bool bSecure = false;
273 if ( eAutoConfirm == eNoAutoConfirm )
275 OUString sReferrer( m_xData->m_rDocumentAccess.getDocumentLocation() );
277 OUString aSystemFileURL;
278 if ( osl::FileBase::getSystemPathFromFileURL( sReferrer, aSystemFileURL ) == osl::FileBase::E_None )
279 sReferrer = aSystemFileURL;
281 bSecure = lcl_showMacroWarning( rxInteraction, sReferrer );
283 else
284 bSecure = ( eAutoConfirm == eAutoConfirmApprove );
286 return ( bSecure ? allowMacroExecution() : disallowMacroExecution() );
290 bool DocumentMacroMode::isMacroExecutionDisallowed() const
292 return m_xData->m_rDocumentAccess.getCurrentMacroExecMode() == MacroExecMode::NEVER_EXECUTE;
296 bool DocumentMacroMode::containerHasBasicMacros( const Reference< XLibraryContainer >& xContainer )
298 bool bHasMacroLib = false;
301 if ( xContainer.is() )
303 // a library container exists; check if it's empty
305 // if there are libraries except the "Standard" library
306 // we assume that they are not empty (because they have been created by the user)
307 if ( !xContainer->hasElements() )
308 bHasMacroLib = false;
309 else
311 const OUString aStdLibName( "Standard" );
312 const OUString aVBAProject( "VBAProject" );
313 const Sequence< OUString > aElements = xContainer->getElementNames();
314 for( const OUString& aElement : aElements )
316 if( aElement == aStdLibName || aElement == aVBAProject )
318 Reference < XNameAccess > xLib;
319 Any aAny = xContainer->getByName( aElement );
320 aAny >>= xLib;
321 if ( xLib.is() && xLib->hasElements() )
322 return true;
324 else
325 return true;
330 catch( const Exception& )
332 DBG_UNHANDLED_EXCEPTION("sfx.doc");
334 return bHasMacroLib;
338 bool DocumentMacroMode::hasMacroLibrary() const
340 bool bHasMacroLib = false;
341 #if HAVE_FEATURE_SCRIPTING
344 Reference< XEmbeddedScripts > xScripts( m_xData->m_rDocumentAccess.getEmbeddedDocumentScripts() );
345 Reference< XLibraryContainer > xContainer;
346 if ( xScripts.is() )
347 xContainer.set( xScripts->getBasicLibraries(), UNO_QUERY_THROW );
348 bHasMacroLib = containerHasBasicMacros( xContainer );
351 catch( const Exception& )
353 DBG_UNHANDLED_EXCEPTION("sfx.doc");
355 #endif
356 return bHasMacroLib;
360 bool DocumentMacroMode::storageHasMacros( const Reference< XStorage >& rxStorage )
362 bool bHasMacros = false;
363 if ( rxStorage.is() )
367 const OUString s_sBasicStorageName( OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "Basic" ) ) );
368 const OUString s_sScriptsStorageName( OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "Scripts" ) ) );
370 bHasMacros =( ( rxStorage->hasByName( s_sBasicStorageName )
371 && rxStorage->isStorageElement( s_sBasicStorageName )
373 || ( rxStorage->hasByName( s_sScriptsStorageName )
374 && rxStorage->isStorageElement( s_sScriptsStorageName )
378 catch ( const Exception& )
380 DBG_UNHANDLED_EXCEPTION("sfx.doc");
383 return bHasMacros;
387 bool DocumentMacroMode::checkMacrosOnLoading( const Reference< XInteractionHandler >& rxInteraction )
389 bool bAllow = false;
390 if ( SvtSecurityOptions().IsMacroDisabled() )
392 // no macro should be executed at all
393 bAllow = disallowMacroExecution();
395 else
397 if (m_xData->m_rDocumentAccess.documentStorageHasMacros() || hasMacroLibrary() || m_xData->m_rDocumentAccess.macroCallsSeenWhileLoading())
399 bAllow = adjustMacroMode( rxInteraction );
401 else if ( !isMacroExecutionDisallowed() )
403 // if macros will be added by the user later, the security check is obsolete
404 bAllow = allowMacroExecution();
407 return bAllow;
411 } // namespace sfx2
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */