LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / doc / docmacromode.cxx
blob10630d0f3eae8f28d2c66dcede321f685b210f60
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 ) )
134 bool DocumentMacroMode::allowMacroExecution()
136 m_xData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::ALWAYS_EXECUTE_NO_WARN );
137 return true;
140 bool DocumentMacroMode::disallowMacroExecution()
142 m_xData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::NEVER_EXECUTE );
143 return false;
146 bool DocumentMacroMode::adjustMacroMode( const Reference< XInteractionHandler >& rxInteraction, bool bHasValidContentSignature )
148 sal_uInt16 nMacroExecutionMode = m_xData->m_rDocumentAccess.getCurrentMacroExecMode();
150 if ( SvtSecurityOptions::IsMacroDisabled() )
152 // no macro should be executed at all
153 lcl_showMacrosDisabledError( rxInteraction, m_xData->m_bMacroDisabledMessageShown );
154 return disallowMacroExecution();
157 // get setting from configuration if required
158 enum AutoConfirmation
160 eNoAutoConfirm,
161 eAutoConfirmApprove,
162 eAutoConfirmReject
164 AutoConfirmation eAutoConfirm( eNoAutoConfirm );
166 if ( ( nMacroExecutionMode == MacroExecMode::USE_CONFIG )
167 || ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION )
168 || ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION )
171 // check confirm first, as nMacroExecutionMode is always overwritten by the GetMacroSecurityLevel() switch
172 if (nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION)
173 eAutoConfirm = eAutoConfirmReject;
174 else if (nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION)
175 eAutoConfirm = eAutoConfirmApprove;
177 switch ( SvtSecurityOptions::GetMacroSecurityLevel() )
179 case 3:
180 nMacroExecutionMode = MacroExecMode::FROM_LIST_NO_WARN;
181 break;
182 case 2:
183 nMacroExecutionMode = MacroExecMode::FROM_LIST_AND_SIGNED_WARN;
184 break;
185 case 1:
186 nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE;
187 break;
188 case 0:
189 nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
190 break;
191 default:
192 OSL_FAIL( "DocumentMacroMode::adjustMacroMode: unexpected macro security level!" );
193 nMacroExecutionMode = MacroExecMode::NEVER_EXECUTE;
197 if ( nMacroExecutionMode == MacroExecMode::NEVER_EXECUTE )
198 return false;
200 if ( nMacroExecutionMode == MacroExecMode::ALWAYS_EXECUTE_NO_WARN )
201 return true;
205 // get document location from medium name and check whether it is a trusted one
206 // the service is created without document version, since it is not of interest here
207 Reference< XDocumentDigitalSignatures > xSignatures(DocumentDigitalSignatures::createDefault(::comphelper::getProcessComponentContext()));
208 INetURLObject aURLReferer( m_xData->m_rDocumentAccess.getDocumentLocation() );
210 OUString aLocation;
211 if ( aURLReferer.removeSegment() )
212 aLocation = aURLReferer.GetMainURL( INetURLObject::DecodeMechanism::NONE );
214 if ( !aLocation.isEmpty() && xSignatures->isLocationTrusted( aLocation ) )
216 return allowMacroExecution();
219 // at this point it is clear that the document is not in the secure location
220 if ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN )
222 lcl_showDocumentMacrosDisabledError( rxInteraction, m_xData->m_bDocMacroDisabledMessageShown );
223 return disallowMacroExecution();
226 // check whether the document is signed with trusted certificate
227 if ( nMacroExecutionMode != MacroExecMode::FROM_LIST )
229 // the trusted macro check will also retrieve the signature state ( small optimization )
230 const bool bAllowUIToAddAuthor = nMacroExecutionMode != MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN
231 && (nMacroExecutionMode == MacroExecMode::ALWAYS_EXECUTE
232 || !SvtSecurityOptions::IsReadOnly(SvtSecurityOptions::EOption::MacroTrustedAuthors));
233 const bool bHasTrustedMacroSignature = m_xData->m_rDocumentAccess.hasTrustedScriptingSignature(bAllowUIToAddAuthor);
235 SignatureState nSignatureState = m_xData->m_rDocumentAccess.getScriptingSignatureState();
236 if ( nSignatureState == SignatureState::BROKEN )
238 if (!bAllowUIToAddAuthor)
239 lcl_showDocumentMacrosDisabledError(rxInteraction, m_xData->m_bDocMacroDisabledMessageShown);
240 return disallowMacroExecution();
242 else if ( m_xData->m_rDocumentAccess.macroCallsSeenWhileLoading() &&
243 bHasTrustedMacroSignature &&
244 !bHasValidContentSignature)
246 // When macros are signed, and the document has events which call macros, the document content needs to be signed too.
247 lcl_showMacrosDisabledUnsignedContentError(rxInteraction, m_xData->m_bDocMacroDisabledMessageShown);
248 return disallowMacroExecution();
250 else if ( bHasTrustedMacroSignature )
252 // there is trusted macro signature, allow macro execution
253 return allowMacroExecution();
255 else if ( nSignatureState == SignatureState::OK
256 || nSignatureState == SignatureState::NOTVALIDATED )
258 // there is valid signature, but it is not from the trusted author
259 if (!bAllowUIToAddAuthor)
260 lcl_showDocumentMacrosDisabledError(rxInteraction, m_xData->m_bDocMacroDisabledMessageShown);
261 return disallowMacroExecution();
265 // at this point it is clear that the document is neither in secure location nor signed with trusted certificate
266 if ( ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
267 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
270 if ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
271 lcl_showDocumentMacrosDisabledError( rxInteraction, m_xData->m_bDocMacroDisabledMessageShown );
273 return disallowMacroExecution();
276 catch ( const Exception& )
278 if ( ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN )
279 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
280 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
283 return disallowMacroExecution();
287 // confirmation is required
288 bool bSecure = false;
290 if ( eAutoConfirm == eNoAutoConfirm )
292 OUString sReferrer( m_xData->m_rDocumentAccess.getDocumentLocation() );
294 OUString aSystemFileURL;
295 if ( osl::FileBase::getSystemPathFromFileURL( sReferrer, aSystemFileURL ) == osl::FileBase::E_None )
296 sReferrer = aSystemFileURL;
298 bSecure = lcl_showMacroWarning( rxInteraction, sReferrer );
300 else
301 bSecure = ( eAutoConfirm == eAutoConfirmApprove );
303 return ( bSecure ? allowMacroExecution() : disallowMacroExecution() );
307 bool DocumentMacroMode::isMacroExecutionDisallowed() const
309 return m_xData->m_rDocumentAccess.getCurrentMacroExecMode() == MacroExecMode::NEVER_EXECUTE;
313 bool DocumentMacroMode::containerHasBasicMacros( const Reference< XLibraryContainer >& xContainer )
315 bool bHasMacroLib = false;
318 if ( xContainer.is() )
320 // a library container exists; check if it's empty
322 // if there are libraries except the "Standard" library
323 // we assume that they are not empty (because they have been created by the user)
324 if ( !xContainer->hasElements() )
325 bHasMacroLib = false;
326 else
328 static const OUStringLiteral aStdLibName( u"Standard" );
329 static const OUStringLiteral aVBAProject( u"VBAProject" );
330 const Sequence< OUString > aElements = xContainer->getElementNames();
331 for( const OUString& aElement : aElements )
333 if( aElement == aStdLibName || aElement == aVBAProject )
335 Reference < XNameAccess > xLib;
336 Any aAny = xContainer->getByName( aElement );
337 aAny >>= xLib;
338 if ( xLib.is() && xLib->hasElements() )
339 return true;
341 else
342 return true;
347 catch( const Exception& )
349 DBG_UNHANDLED_EXCEPTION("sfx.doc");
351 return bHasMacroLib;
355 bool DocumentMacroMode::hasMacroLibrary() const
357 bool bHasMacroLib = false;
358 #if HAVE_FEATURE_SCRIPTING
361 Reference< XEmbeddedScripts > xScripts( m_xData->m_rDocumentAccess.getEmbeddedDocumentScripts() );
362 Reference< XLibraryContainer > xContainer;
363 if ( xScripts.is() )
364 xContainer.set( xScripts->getBasicLibraries(), UNO_QUERY_THROW );
365 bHasMacroLib = containerHasBasicMacros( xContainer );
368 catch( const Exception& )
370 DBG_UNHANDLED_EXCEPTION("sfx.doc");
372 #endif
373 return bHasMacroLib;
377 bool DocumentMacroMode::storageHasMacros( const Reference< XStorage >& rxStorage )
379 bool bHasMacros = false;
380 if ( rxStorage.is() )
384 static constexpr OUStringLiteral s_sBasicStorageName( u"Basic" );
385 static constexpr OUStringLiteral s_sScriptsStorageName( u"Scripts" );
387 bHasMacros =( ( rxStorage->hasByName( s_sBasicStorageName )
388 && rxStorage->isStorageElement( s_sBasicStorageName )
390 || ( rxStorage->hasByName( s_sScriptsStorageName )
391 && rxStorage->isStorageElement( s_sScriptsStorageName )
395 catch ( const Exception& )
397 DBG_UNHANDLED_EXCEPTION("sfx.doc");
400 return bHasMacros;
404 bool DocumentMacroMode::checkMacrosOnLoading( const Reference< XInteractionHandler >& rxInteraction, bool bHasValidContentSignature )
406 bool bAllow = false;
407 if ( SvtSecurityOptions::IsMacroDisabled() )
409 // no macro should be executed at all
410 bAllow = disallowMacroExecution();
412 else
414 if (m_xData->m_rDocumentAccess.documentStorageHasMacros() || hasMacroLibrary() || m_xData->m_rDocumentAccess.macroCallsSeenWhileLoading())
416 bAllow = adjustMacroMode( rxInteraction, bHasValidContentSignature );
418 else if ( !isMacroExecutionDisallowed() )
420 // if macros will be added by the user later, the security check is obsolete
421 bAllow = allowMacroExecution();
424 return bAllow;
428 } // namespace sfx2
431 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */