1d119d52dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3d119d52dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4d119d52dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5d119d52dSAndrew Rist * distributed with this work for additional information 6d119d52dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7d119d52dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8d119d52dSAndrew Rist * "License"); you may not use this file except in compliance 9d119d52dSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11d119d52dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13d119d52dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14d119d52dSAndrew Rist * software distributed under the License is distributed on an 15d119d52dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16d119d52dSAndrew Rist * KIND, either express or implied. See the License for the 17d119d52dSAndrew Rist * specific language governing permissions and limitations 18d119d52dSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20d119d52dSAndrew Rist *************************************************************/ 21d119d52dSAndrew Rist 22d119d52dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sfx2.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "sfx2/docmacromode.hxx" 28cdf0e10cSrcweir #include "sfx2/signaturestate.hxx" 29cdf0e10cSrcweir #include "sfx2/docfile.hxx" 30cdf0e10cSrcweir 31cdf0e10cSrcweir /** === begin UNO includes === **/ 32cdf0e10cSrcweir #include <com/sun/star/document/MacroExecMode.hpp> 33cdf0e10cSrcweir #include <com/sun/star/task/ErrorCodeRequest.hpp> 34cdf0e10cSrcweir #include <com/sun/star/task/DocumentMacroConfirmationRequest.hpp> 35cdf0e10cSrcweir #include <com/sun/star/task/InteractionClassification.hpp> 36cdf0e10cSrcweir #include <com/sun/star/security/XDocumentDigitalSignatures.hpp> 37cdf0e10cSrcweir /** === end UNO includes === **/ 38cdf0e10cSrcweir 39cdf0e10cSrcweir #include <comphelper/componentcontext.hxx> 40cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 41cdf0e10cSrcweir #include <framework/interaction.hxx> 42cdf0e10cSrcweir #include <osl/file.hxx> 43cdf0e10cSrcweir #include <rtl/ref.hxx> 44cdf0e10cSrcweir #include <unotools/securityoptions.hxx> 45cdf0e10cSrcweir #include <svtools/sfxecode.hxx> 46cdf0e10cSrcweir #include <tools/diagnose_ex.h> 47cdf0e10cSrcweir #include <tools/urlobj.hxx> 48cdf0e10cSrcweir 49cdf0e10cSrcweir //........................................................................ 50cdf0e10cSrcweir namespace sfx2 51cdf0e10cSrcweir { 52cdf0e10cSrcweir //........................................................................ 53cdf0e10cSrcweir 54cdf0e10cSrcweir /** === begin UNO using === **/ 55cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 56cdf0e10cSrcweir using ::com::sun::star::task::XInteractionHandler; 57cdf0e10cSrcweir using ::com::sun::star::uno::Any; 58cdf0e10cSrcweir using ::com::sun::star::task::XInteractionHandler; 59cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 60cdf0e10cSrcweir using ::com::sun::star::task::XInteractionContinuation; 61cdf0e10cSrcweir using ::com::sun::star::task::XInteractionRequest; 62cdf0e10cSrcweir using ::com::sun::star::task::DocumentMacroConfirmationRequest; 63cdf0e10cSrcweir using ::com::sun::star::task::ErrorCodeRequest; 64cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 65cdf0e10cSrcweir using ::com::sun::star::security::XDocumentDigitalSignatures; 66cdf0e10cSrcweir using ::com::sun::star::security::DocumentSignatureInformation; 67cdf0e10cSrcweir using ::com::sun::star::embed::XStorage; 68cdf0e10cSrcweir using ::com::sun::star::task::InteractionClassification_QUERY; 69cdf0e10cSrcweir using ::com::sun::star::document::XEmbeddedScripts; 70cdf0e10cSrcweir using ::com::sun::star::uno::UNO_SET_THROW; 71cdf0e10cSrcweir using ::com::sun::star::script::XLibraryContainer; 72cdf0e10cSrcweir using ::com::sun::star::container::XNameAccess; 73cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 74cdf0e10cSrcweir /** === end UNO using === **/ 75cdf0e10cSrcweir namespace MacroExecMode = ::com::sun::star::document::MacroExecMode; 76cdf0e10cSrcweir 77cdf0e10cSrcweir //==================================================================== 78cdf0e10cSrcweir //= DocumentMacroMode_Data 79cdf0e10cSrcweir //==================================================================== 80cdf0e10cSrcweir struct DocumentMacroMode_Data 81cdf0e10cSrcweir { 82cdf0e10cSrcweir IMacroDocumentAccess& m_rDocumentAccess; 83cdf0e10cSrcweir sal_Bool m_bMacroDisabledMessageShown; 84cdf0e10cSrcweir sal_Bool m_bDocMacroDisabledMessageShown; 85cdf0e10cSrcweir DocumentMacroMode_Datasfx2::DocumentMacroMode_Data86cdf0e10cSrcweir DocumentMacroMode_Data( IMacroDocumentAccess& rDocumentAccess ) 87cdf0e10cSrcweir :m_rDocumentAccess( rDocumentAccess ) 88cdf0e10cSrcweir ,m_bMacroDisabledMessageShown( sal_False ) 89cdf0e10cSrcweir ,m_bDocMacroDisabledMessageShown( sal_False ) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir } 92cdf0e10cSrcweir }; 93cdf0e10cSrcweir 94cdf0e10cSrcweir //==================================================================== 95cdf0e10cSrcweir //= helper 96cdf0e10cSrcweir //==================================================================== 97cdf0e10cSrcweir namespace 98cdf0e10cSrcweir { 99cdf0e10cSrcweir //................................................................ lcl_showGeneralSfxErrorOnce(const Reference<XInteractionHandler> & rxHandler,const sal_Int32 nSfxErrorCode,sal_Bool & rbAlreadyShown)100cdf0e10cSrcweir void lcl_showGeneralSfxErrorOnce( const Reference< XInteractionHandler >& rxHandler, const sal_Int32 nSfxErrorCode, sal_Bool& rbAlreadyShown ) 101cdf0e10cSrcweir { 102cdf0e10cSrcweir if ( rbAlreadyShown ) 103cdf0e10cSrcweir return; 104cdf0e10cSrcweir 105cdf0e10cSrcweir ErrorCodeRequest aErrorCodeRequest; 106cdf0e10cSrcweir aErrorCodeRequest.ErrCode = nSfxErrorCode; 107cdf0e10cSrcweir 108cdf0e10cSrcweir SfxMedium::CallApproveHandler( rxHandler, makeAny( aErrorCodeRequest ), sal_False ); 109cdf0e10cSrcweir rbAlreadyShown = sal_True; 110cdf0e10cSrcweir } 111cdf0e10cSrcweir 112cdf0e10cSrcweir //................................................................ lcl_showMacrosDisabledError(const Reference<XInteractionHandler> & rxHandler,sal_Bool & rbAlreadyShown)113cdf0e10cSrcweir void lcl_showMacrosDisabledError( const Reference< XInteractionHandler >& rxHandler, sal_Bool& rbAlreadyShown ) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir lcl_showGeneralSfxErrorOnce( rxHandler, ERRCODE_SFX_MACROS_SUPPORT_DISABLED, rbAlreadyShown ); 116cdf0e10cSrcweir } 117cdf0e10cSrcweir 118cdf0e10cSrcweir //................................................................ lcl_showDocumentMacrosDisabledError(const Reference<XInteractionHandler> & rxHandler,sal_Bool & rbAlreadyShown)119cdf0e10cSrcweir void lcl_showDocumentMacrosDisabledError( const Reference< XInteractionHandler >& rxHandler, sal_Bool& rbAlreadyShown ) 120cdf0e10cSrcweir { 121cdf0e10cSrcweir lcl_showGeneralSfxErrorOnce( rxHandler, ERRCODE_SFX_DOCUMENT_MACRO_DISABLED, rbAlreadyShown ); 122cdf0e10cSrcweir } 123cdf0e10cSrcweir 124cdf0e10cSrcweir //................................................................ lcl_showMacroWarning(const Reference<XInteractionHandler> & rxHandler,const::rtl::OUString & rDocumentLocation)125cdf0e10cSrcweir sal_Bool lcl_showMacroWarning( const Reference< XInteractionHandler >& rxHandler, 126cdf0e10cSrcweir const ::rtl::OUString& rDocumentLocation ) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir DocumentMacroConfirmationRequest aRequest; 129cdf0e10cSrcweir aRequest.DocumentURL = rDocumentLocation; 130cdf0e10cSrcweir return SfxMedium::CallApproveHandler( rxHandler, makeAny( aRequest ), sal_True ); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir //==================================================================== 135cdf0e10cSrcweir //= DocumentMacroMode 136cdf0e10cSrcweir //==================================================================== 137cdf0e10cSrcweir //-------------------------------------------------------------------- DocumentMacroMode(IMacroDocumentAccess & rDocumentAccess)138cdf0e10cSrcweir DocumentMacroMode::DocumentMacroMode( IMacroDocumentAccess& rDocumentAccess ) 139cdf0e10cSrcweir :m_pData( new DocumentMacroMode_Data( rDocumentAccess ) ) 140cdf0e10cSrcweir { 141cdf0e10cSrcweir } 142cdf0e10cSrcweir 143cdf0e10cSrcweir //-------------------------------------------------------------------- ~DocumentMacroMode()144cdf0e10cSrcweir DocumentMacroMode::~DocumentMacroMode() 145cdf0e10cSrcweir { 146cdf0e10cSrcweir } 147cdf0e10cSrcweir 148cdf0e10cSrcweir //-------------------------------------------------------------------- allowMacroExecution()149cdf0e10cSrcweir sal_Bool DocumentMacroMode::allowMacroExecution() 150cdf0e10cSrcweir { 151cdf0e10cSrcweir m_pData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::ALWAYS_EXECUTE_NO_WARN ); 152cdf0e10cSrcweir return sal_True; 153cdf0e10cSrcweir } 154cdf0e10cSrcweir 155cdf0e10cSrcweir //-------------------------------------------------------------------- disallowMacroExecution()156cdf0e10cSrcweir sal_Bool DocumentMacroMode::disallowMacroExecution() 157cdf0e10cSrcweir { 158cdf0e10cSrcweir m_pData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::NEVER_EXECUTE ); 159cdf0e10cSrcweir return sal_False; 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162*b42dd17cSArrigo Marchiori /** Change the indicated macro execution mode depending on the current macro security level. 163*b42dd17cSArrigo Marchiori * 164*b42dd17cSArrigo Marchiori * @param nMacroExecutionMode current execution mode (must be one of MacroExecMode::USE_XXX). 165*b42dd17cSArrigo Marchiori * 166*b42dd17cSArrigo Marchiori * Changes nMacroExecutionMode according to the current security options, if applicable. 167*b42dd17cSArrigo Marchiori */ adjustMacroExecModeSecurity(sal_uInt16 & nMacroExecutionMode)168*b42dd17cSArrigo Marchiori static void adjustMacroExecModeSecurity(sal_uInt16 &nMacroExecutionMode) 169cdf0e10cSrcweir { 170cdf0e10cSrcweir if ( ( nMacroExecutionMode == MacroExecMode::USE_CONFIG ) 171cdf0e10cSrcweir || ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION ) 172cdf0e10cSrcweir || ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION ) 173cdf0e10cSrcweir ) 174cdf0e10cSrcweir { 175cdf0e10cSrcweir SvtSecurityOptions aOpt; 176cdf0e10cSrcweir switch ( aOpt.GetMacroSecurityLevel() ) 177cdf0e10cSrcweir { 178cdf0e10cSrcweir case 3: 179cdf0e10cSrcweir nMacroExecutionMode = MacroExecMode::FROM_LIST_NO_WARN; 180cdf0e10cSrcweir break; 181cdf0e10cSrcweir case 2: 182cdf0e10cSrcweir nMacroExecutionMode = MacroExecMode::FROM_LIST_AND_SIGNED_WARN; 183cdf0e10cSrcweir break; 184cdf0e10cSrcweir case 1: 185cdf0e10cSrcweir nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE; 186cdf0e10cSrcweir break; 187cdf0e10cSrcweir case 0: 188cdf0e10cSrcweir nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE_NO_WARN; 189cdf0e10cSrcweir break; 190cdf0e10cSrcweir default: 191cdf0e10cSrcweir OSL_ENSURE( sal_False, "DocumentMacroMode::adjustMacroMode: unexpected macro security level!" ); 192cdf0e10cSrcweir nMacroExecutionMode = MacroExecMode::NEVER_EXECUTE; 193cdf0e10cSrcweir } 194cdf0e10cSrcweir 195*b42dd17cSArrigo Marchiori } 196*b42dd17cSArrigo Marchiori } 197*b42dd17cSArrigo Marchiori 198*b42dd17cSArrigo Marchiori //-------------------------------------------------------------------- adjustMacroMode(const Reference<XInteractionHandler> & rxInteraction)199*b42dd17cSArrigo Marchiori sal_Bool DocumentMacroMode::adjustMacroMode( const Reference< XInteractionHandler >& rxInteraction ) 200*b42dd17cSArrigo Marchiori { 201*b42dd17cSArrigo Marchiori sal_uInt16 nMacroExecutionMode = m_pData->m_rDocumentAccess.getCurrentMacroExecMode(); 202*b42dd17cSArrigo Marchiori 203*b42dd17cSArrigo Marchiori if ( SvtSecurityOptions().IsMacroDisabled() ) 204*b42dd17cSArrigo Marchiori { 205*b42dd17cSArrigo Marchiori // no macro should be executed at all 206*b42dd17cSArrigo Marchiori lcl_showMacrosDisabledError( rxInteraction, m_pData->m_bMacroDisabledMessageShown ); 207*b42dd17cSArrigo Marchiori return disallowMacroExecution(); 208*b42dd17cSArrigo Marchiori } 209*b42dd17cSArrigo Marchiori 210*b42dd17cSArrigo Marchiori // get setting from configuration if required 211*b42dd17cSArrigo Marchiori enum AutoConfirmation 212*b42dd17cSArrigo Marchiori { 213*b42dd17cSArrigo Marchiori eNoAutoConfirm, 214*b42dd17cSArrigo Marchiori eAutoConfirmApprove, 215*b42dd17cSArrigo Marchiori eAutoConfirmReject 216*b42dd17cSArrigo Marchiori }; 217*b42dd17cSArrigo Marchiori AutoConfirmation eAutoConfirm( eNoAutoConfirm ); 218*b42dd17cSArrigo Marchiori 219*b42dd17cSArrigo Marchiori adjustMacroExecModeSecurity(nMacroExecutionMode); 220cdf0e10cSrcweir if ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION ) 221cdf0e10cSrcweir eAutoConfirm = eAutoConfirmReject; 222cdf0e10cSrcweir else if ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION ) 223cdf0e10cSrcweir eAutoConfirm = eAutoConfirmApprove; 224cdf0e10cSrcweir 225cdf0e10cSrcweir if ( nMacroExecutionMode == MacroExecMode::NEVER_EXECUTE ) 226cdf0e10cSrcweir return sal_False; 227cdf0e10cSrcweir 228cdf0e10cSrcweir if ( nMacroExecutionMode == MacroExecMode::ALWAYS_EXECUTE_NO_WARN ) 229cdf0e10cSrcweir return sal_True; 230cdf0e10cSrcweir 231cdf0e10cSrcweir try 232cdf0e10cSrcweir { 233cdf0e10cSrcweir ::rtl::OUString sReferrer( m_pData->m_rDocumentAccess.getDocumentLocation() ); 234cdf0e10cSrcweir 235cdf0e10cSrcweir // get document location from medium name and check whether it is a trusted one 236cdf0e10cSrcweir // the service is created ohne document version, since it is not of interest here 237cdf0e10cSrcweir ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() ); 238cdf0e10cSrcweir Reference< XDocumentDigitalSignatures > xSignatures; 239cdf0e10cSrcweir if ( aContext.createComponent( "com.sun.star.security.DocumentDigitalSignatures", xSignatures ) ) 240cdf0e10cSrcweir { 241cdf0e10cSrcweir INetURLObject aURLReferer( sReferrer ); 242cdf0e10cSrcweir 243cdf0e10cSrcweir ::rtl::OUString aLocation; 244cdf0e10cSrcweir if ( aURLReferer.removeSegment() ) 245cdf0e10cSrcweir aLocation = aURLReferer.GetMainURL( INetURLObject::NO_DECODE ); 246cdf0e10cSrcweir 247cdf0e10cSrcweir if ( aLocation.getLength() && xSignatures->isLocationTrusted( aLocation ) ) 248cdf0e10cSrcweir { 249cdf0e10cSrcweir return allowMacroExecution(); 250cdf0e10cSrcweir } 251cdf0e10cSrcweir } 252cdf0e10cSrcweir 253cdf0e10cSrcweir // at this point it is clear that the document is not in the secure location 254cdf0e10cSrcweir if ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN ) 255cdf0e10cSrcweir { 256cdf0e10cSrcweir lcl_showDocumentMacrosDisabledError( rxInteraction, m_pData->m_bDocMacroDisabledMessageShown ); 257cdf0e10cSrcweir return disallowMacroExecution(); 258cdf0e10cSrcweir } 259cdf0e10cSrcweir 260cdf0e10cSrcweir // check whether the document is signed with trusted certificate 261cdf0e10cSrcweir if ( nMacroExecutionMode != MacroExecMode::FROM_LIST ) 262cdf0e10cSrcweir { 263cdf0e10cSrcweir // the trusted macro check will also retrieve the signature state ( small optimization ) 264cdf0e10cSrcweir sal_Bool bHasTrustedMacroSignature = m_pData->m_rDocumentAccess.hasTrustedScriptingSignature( nMacroExecutionMode != MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN ); 265cdf0e10cSrcweir 266cdf0e10cSrcweir sal_uInt16 nSignatureState = m_pData->m_rDocumentAccess.getScriptingSignatureState(); 267cdf0e10cSrcweir if ( nSignatureState == SIGNATURESTATE_SIGNATURES_BROKEN ) 268cdf0e10cSrcweir { 269cdf0e10cSrcweir // the signature is broken, no macro execution 270cdf0e10cSrcweir if ( nMacroExecutionMode != MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN ) 271cdf0e10cSrcweir m_pData->m_rDocumentAccess.showBrokenSignatureWarning( rxInteraction ); 272cdf0e10cSrcweir 273cdf0e10cSrcweir return disallowMacroExecution(); 274cdf0e10cSrcweir } 275cdf0e10cSrcweir else if ( bHasTrustedMacroSignature ) 276cdf0e10cSrcweir { 277cdf0e10cSrcweir // there is trusted macro signature, allow macro execution 278cdf0e10cSrcweir return allowMacroExecution(); 279cdf0e10cSrcweir } 280cdf0e10cSrcweir else if ( nSignatureState == SIGNATURESTATE_SIGNATURES_OK 281cdf0e10cSrcweir || nSignatureState == SIGNATURESTATE_SIGNATURES_NOTVALIDATED ) 282cdf0e10cSrcweir { 283cdf0e10cSrcweir // there is valid signature, but it is not from the trusted author 284cdf0e10cSrcweir return disallowMacroExecution(); 285cdf0e10cSrcweir } 286cdf0e10cSrcweir } 287cdf0e10cSrcweir 288cdf0e10cSrcweir // at this point it is clear that the document is neither in secure location nor signed with trusted certificate 289cdf0e10cSrcweir if ( ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN ) 290cdf0e10cSrcweir || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN ) 291cdf0e10cSrcweir ) 292cdf0e10cSrcweir { 293cdf0e10cSrcweir if ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN ) 294cdf0e10cSrcweir lcl_showDocumentMacrosDisabledError( rxInteraction, m_pData->m_bDocMacroDisabledMessageShown ); 295cdf0e10cSrcweir 296cdf0e10cSrcweir return disallowMacroExecution(); 297cdf0e10cSrcweir } 298cdf0e10cSrcweir } 299cdf0e10cSrcweir catch ( Exception& ) 300cdf0e10cSrcweir { 301cdf0e10cSrcweir if ( ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN ) 302cdf0e10cSrcweir || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN ) 303cdf0e10cSrcweir || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN ) 304cdf0e10cSrcweir ) 305cdf0e10cSrcweir { 306cdf0e10cSrcweir return disallowMacroExecution(); 307cdf0e10cSrcweir } 308cdf0e10cSrcweir } 309cdf0e10cSrcweir 310cdf0e10cSrcweir // conformation is required 311cdf0e10cSrcweir sal_Bool bSecure = sal_False; 312cdf0e10cSrcweir 313cdf0e10cSrcweir if ( eAutoConfirm == eNoAutoConfirm ) 314cdf0e10cSrcweir { 315cdf0e10cSrcweir ::rtl::OUString sReferrer( m_pData->m_rDocumentAccess.getDocumentLocation() ); 316cdf0e10cSrcweir 317cdf0e10cSrcweir ::rtl::OUString aSystemFileURL; 318cdf0e10cSrcweir if ( osl::FileBase::getSystemPathFromFileURL( sReferrer, aSystemFileURL ) == osl::FileBase::E_None ) 319cdf0e10cSrcweir sReferrer = aSystemFileURL; 320cdf0e10cSrcweir 321cdf0e10cSrcweir bSecure = lcl_showMacroWarning( rxInteraction, sReferrer ); 322cdf0e10cSrcweir } 323cdf0e10cSrcweir else 324cdf0e10cSrcweir bSecure = ( eAutoConfirm == eAutoConfirmApprove ); 325cdf0e10cSrcweir 326cdf0e10cSrcweir return ( bSecure ? allowMacroExecution() : disallowMacroExecution() ); 327cdf0e10cSrcweir } 328cdf0e10cSrcweir 329cdf0e10cSrcweir //-------------------------------------------------------------------- isMacroExecutionDisallowed() const330cdf0e10cSrcweir sal_Bool DocumentMacroMode::isMacroExecutionDisallowed() const 331cdf0e10cSrcweir { 332cdf0e10cSrcweir return m_pData->m_rDocumentAccess.getCurrentMacroExecMode() == MacroExecMode::NEVER_EXECUTE; 333cdf0e10cSrcweir } 334cdf0e10cSrcweir 335cdf0e10cSrcweir //-------------------------------------------------------------------- hasMacroLibrary() const336cdf0e10cSrcweir sal_Bool DocumentMacroMode::hasMacroLibrary() const 337cdf0e10cSrcweir { 338cdf0e10cSrcweir sal_Bool bHasMacroLib = sal_False; 339cdf0e10cSrcweir try 340cdf0e10cSrcweir { 341cdf0e10cSrcweir Reference< XEmbeddedScripts > xScripts( m_pData->m_rDocumentAccess.getEmbeddedDocumentScripts() ); 342cdf0e10cSrcweir Reference< XLibraryContainer > xContainer; 343cdf0e10cSrcweir if ( xScripts.is() ) 344cdf0e10cSrcweir xContainer.set( xScripts->getBasicLibraries(), UNO_QUERY_THROW ); 345cdf0e10cSrcweir 346cdf0e10cSrcweir if ( xContainer.is() ) 347cdf0e10cSrcweir { 348cdf0e10cSrcweir // a library container exists; check if it's empty 349cdf0e10cSrcweir 350cdf0e10cSrcweir // if there are libraries except the "Standard" library 351cdf0e10cSrcweir // we assume that they are not empty (because they have been created by the user) 352cdf0e10cSrcweir if ( !xContainer->hasElements() ) 353cdf0e10cSrcweir bHasMacroLib = sal_False; 354cdf0e10cSrcweir else 355cdf0e10cSrcweir { 356cdf0e10cSrcweir ::rtl::OUString aStdLibName( RTL_CONSTASCII_USTRINGPARAM( "Standard" ) ); 357cdf0e10cSrcweir Sequence< ::rtl::OUString > aElements = xContainer->getElementNames(); 358cdf0e10cSrcweir if ( aElements.getLength() ) 359cdf0e10cSrcweir { 360cdf0e10cSrcweir if ( aElements.getLength() > 1 || !aElements[0].equals( aStdLibName ) ) 361cdf0e10cSrcweir bHasMacroLib = sal_True; 362cdf0e10cSrcweir else 363cdf0e10cSrcweir { 364cdf0e10cSrcweir // usually a "Standard" library is always present (design) 365cdf0e10cSrcweir // for this reason we must check if it's empty 366cdf0e10cSrcweir // 367cdf0e10cSrcweir // Note: Since #i73229#, this is not true anymore. There's no default 368cdf0e10cSrcweir // "Standard" lib anymore. Wouldn't it be time to get completely 369cdf0e10cSrcweir // rid of the "Standard" thingie - this shouldn't be necessary 370cdf0e10cSrcweir // anymore, should it? 371cdf0e10cSrcweir // 2007-01-25 / frank.schoenheit@sun.com 372cdf0e10cSrcweir Reference < XNameAccess > xLib; 373cdf0e10cSrcweir Any aAny = xContainer->getByName( aStdLibName ); 374cdf0e10cSrcweir aAny >>= xLib; 375cdf0e10cSrcweir if ( xLib.is() ) 376cdf0e10cSrcweir bHasMacroLib = xLib->hasElements(); 377cdf0e10cSrcweir } 378cdf0e10cSrcweir } 379cdf0e10cSrcweir } 380cdf0e10cSrcweir } 381cdf0e10cSrcweir } 382cdf0e10cSrcweir catch( const Exception& ) 383cdf0e10cSrcweir { 384cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 385cdf0e10cSrcweir } 386cdf0e10cSrcweir 387cdf0e10cSrcweir return bHasMacroLib; 388cdf0e10cSrcweir } 389cdf0e10cSrcweir 390cdf0e10cSrcweir //-------------------------------------------------------------------- storageHasMacros(const Reference<XStorage> & rxStorage)391cdf0e10cSrcweir sal_Bool DocumentMacroMode::storageHasMacros( const Reference< XStorage >& rxStorage ) 392cdf0e10cSrcweir { 393cdf0e10cSrcweir sal_Bool bHasMacros = sal_False; 394cdf0e10cSrcweir if ( rxStorage.is() ) 395cdf0e10cSrcweir { 396cdf0e10cSrcweir try 397cdf0e10cSrcweir { 398cdf0e10cSrcweir static const ::rtl::OUString s_sBasicStorageName( ::rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "Basic" ) ) ); 399cdf0e10cSrcweir static const ::rtl::OUString s_sScriptsStorageName( ::rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "Scripts" ) ) ); 400cdf0e10cSrcweir 401cdf0e10cSrcweir bHasMacros =( ( rxStorage->hasByName( s_sBasicStorageName ) 402cdf0e10cSrcweir && rxStorage->isStorageElement( s_sBasicStorageName ) 403cdf0e10cSrcweir ) 404cdf0e10cSrcweir || ( rxStorage->hasByName( s_sScriptsStorageName ) 405cdf0e10cSrcweir && rxStorage->isStorageElement( s_sScriptsStorageName ) 406cdf0e10cSrcweir ) 407cdf0e10cSrcweir ); 408cdf0e10cSrcweir } 409cdf0e10cSrcweir catch ( const Exception& ) 410cdf0e10cSrcweir { 411cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 412cdf0e10cSrcweir } 413cdf0e10cSrcweir } 414cdf0e10cSrcweir return bHasMacros; 415cdf0e10cSrcweir } 416cdf0e10cSrcweir 417cdf0e10cSrcweir //-------------------------------------------------------------------- checkMacrosOnLoading(const Reference<XInteractionHandler> & rxInteraction)418cdf0e10cSrcweir sal_Bool DocumentMacroMode::checkMacrosOnLoading( const Reference< XInteractionHandler >& rxInteraction ) 419cdf0e10cSrcweir { 420cdf0e10cSrcweir sal_Bool bAllow = sal_False; 421cdf0e10cSrcweir if ( SvtSecurityOptions().IsMacroDisabled() ) 422cdf0e10cSrcweir { 423cdf0e10cSrcweir // no macro should be executed at all 424cdf0e10cSrcweir bAllow = disallowMacroExecution(); 425cdf0e10cSrcweir } 426cdf0e10cSrcweir else 427cdf0e10cSrcweir { 428cdf0e10cSrcweir if ( m_pData->m_rDocumentAccess.documentStorageHasMacros() || hasMacroLibrary() ) 429cdf0e10cSrcweir { 430cdf0e10cSrcweir bAllow = adjustMacroMode( rxInteraction ); 431cdf0e10cSrcweir } 432cdf0e10cSrcweir else if ( !isMacroExecutionDisallowed() ) 433cdf0e10cSrcweir { 434*b42dd17cSArrigo Marchiori // There are no macros (yet) but we want to be careful anyway 435*b42dd17cSArrigo Marchiori sal_uInt16 nMacroExecutionMode = m_pData->m_rDocumentAccess.getCurrentMacroExecMode(); 436*b42dd17cSArrigo Marchiori adjustMacroExecModeSecurity(nMacroExecutionMode); 437*b42dd17cSArrigo Marchiori switch (nMacroExecutionMode) { 438*b42dd17cSArrigo Marchiori case MacroExecMode::NEVER_EXECUTE: 439*b42dd17cSArrigo Marchiori case MacroExecMode::USE_CONFIG: 440*b42dd17cSArrigo Marchiori case MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION: 441*b42dd17cSArrigo Marchiori case MacroExecMode::FROM_LIST_NO_WARN: 442*b42dd17cSArrigo Marchiori case MacroExecMode::FROM_LIST_AND_SIGNED_WARN: 443*b42dd17cSArrigo Marchiori case MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN: 444*b42dd17cSArrigo Marchiori bAllow = sal_False; 445*b42dd17cSArrigo Marchiori break; 446*b42dd17cSArrigo Marchiori case MacroExecMode::FROM_LIST: 447*b42dd17cSArrigo Marchiori case MacroExecMode::ALWAYS_EXECUTE: 448*b42dd17cSArrigo Marchiori case MacroExecMode::ALWAYS_EXECUTE_NO_WARN: 449*b42dd17cSArrigo Marchiori case MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION: 450*b42dd17cSArrigo Marchiori bAllow = sal_True; 451*b42dd17cSArrigo Marchiori break; 452*b42dd17cSArrigo Marchiori } 453cdf0e10cSrcweir } 454cdf0e10cSrcweir } 455cdf0e10cSrcweir return bAllow; 456cdf0e10cSrcweir } 457cdf0e10cSrcweir 458cdf0e10cSrcweir //........................................................................ 459cdf0e10cSrcweir } // namespace sfx2 460cdf0e10cSrcweir //........................................................................ 461