xref: /AOO41X/main/sfx2/source/doc/docmacromode.cxx (revision d119d52d53d0b2180f2ae51341d882123be2af2b)
1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew 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 
86cdf0e10cSrcweir         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         //................................................................
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 	    //................................................................
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         //................................................................
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         //................................................................
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 	//--------------------------------------------------------------------
138cdf0e10cSrcweir     DocumentMacroMode::DocumentMacroMode( IMacroDocumentAccess& rDocumentAccess )
139cdf0e10cSrcweir         :m_pData( new DocumentMacroMode_Data( rDocumentAccess ) )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	//--------------------------------------------------------------------
144cdf0e10cSrcweir     DocumentMacroMode::~DocumentMacroMode()
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	//--------------------------------------------------------------------
149cdf0e10cSrcweir     sal_Bool DocumentMacroMode::allowMacroExecution()
150cdf0e10cSrcweir     {
151cdf0e10cSrcweir         m_pData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::ALWAYS_EXECUTE_NO_WARN );
152cdf0e10cSrcweir         return sal_True;
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	//--------------------------------------------------------------------
156cdf0e10cSrcweir     sal_Bool DocumentMacroMode::disallowMacroExecution()
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         m_pData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::NEVER_EXECUTE );
159cdf0e10cSrcweir         return sal_False;
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	//--------------------------------------------------------------------
163cdf0e10cSrcweir     sal_Bool DocumentMacroMode::adjustMacroMode( const Reference< XInteractionHandler >& rxInteraction )
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir         sal_uInt16 nMacroExecutionMode = m_pData->m_rDocumentAccess.getCurrentMacroExecMode();
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         if ( SvtSecurityOptions().IsMacroDisabled() )
168cdf0e10cSrcweir         {
169cdf0e10cSrcweir             // no macro should be executed at all
170cdf0e10cSrcweir             lcl_showMacrosDisabledError( rxInteraction, m_pData->m_bMacroDisabledMessageShown );
171cdf0e10cSrcweir             return disallowMacroExecution();
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         // get setting from configuration if required
175cdf0e10cSrcweir         enum AutoConfirmation
176cdf0e10cSrcweir         {
177cdf0e10cSrcweir             eNoAutoConfirm,
178cdf0e10cSrcweir             eAutoConfirmApprove,
179cdf0e10cSrcweir             eAutoConfirmReject
180cdf0e10cSrcweir         };
181cdf0e10cSrcweir         AutoConfirmation eAutoConfirm( eNoAutoConfirm );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         if  (   ( nMacroExecutionMode == MacroExecMode::USE_CONFIG )
184cdf0e10cSrcweir             ||  ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION )
185cdf0e10cSrcweir             ||  ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION )
186cdf0e10cSrcweir             )
187cdf0e10cSrcweir         {
188cdf0e10cSrcweir             SvtSecurityOptions aOpt;
189cdf0e10cSrcweir             switch ( aOpt.GetMacroSecurityLevel() )
190cdf0e10cSrcweir             {
191cdf0e10cSrcweir                 case 3:
192cdf0e10cSrcweir                     nMacroExecutionMode = MacroExecMode::FROM_LIST_NO_WARN;
193cdf0e10cSrcweir                     break;
194cdf0e10cSrcweir                 case 2:
195cdf0e10cSrcweir                     nMacroExecutionMode = MacroExecMode::FROM_LIST_AND_SIGNED_WARN;
196cdf0e10cSrcweir                     break;
197cdf0e10cSrcweir                 case 1:
198cdf0e10cSrcweir                     nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE;
199cdf0e10cSrcweir                     break;
200cdf0e10cSrcweir                 case 0:
201cdf0e10cSrcweir                     nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
202cdf0e10cSrcweir                     break;
203cdf0e10cSrcweir                 default:
204cdf0e10cSrcweir                     OSL_ENSURE( sal_False, "DocumentMacroMode::adjustMacroMode: unexpected macro security level!" );
205cdf0e10cSrcweir                     nMacroExecutionMode = MacroExecMode::NEVER_EXECUTE;
206cdf0e10cSrcweir             }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir             if ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION )
209cdf0e10cSrcweir                 eAutoConfirm = eAutoConfirmReject;
210cdf0e10cSrcweir             else if ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION )
211cdf0e10cSrcweir                 eAutoConfirm = eAutoConfirmApprove;
212cdf0e10cSrcweir         }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir         if ( nMacroExecutionMode == MacroExecMode::NEVER_EXECUTE )
215cdf0e10cSrcweir             return sal_False;
216cdf0e10cSrcweir 
217cdf0e10cSrcweir         if ( nMacroExecutionMode == MacroExecMode::ALWAYS_EXECUTE_NO_WARN )
218cdf0e10cSrcweir             return sal_True;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         try
221cdf0e10cSrcweir         {
222cdf0e10cSrcweir             ::rtl::OUString sReferrer( m_pData->m_rDocumentAccess.getDocumentLocation() );
223cdf0e10cSrcweir 
224cdf0e10cSrcweir             // get document location from medium name and check whether it is a trusted one
225cdf0e10cSrcweir             // the service is created ohne document version, since it is not of interest here
226cdf0e10cSrcweir             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
227cdf0e10cSrcweir             Reference< XDocumentDigitalSignatures > xSignatures;
228cdf0e10cSrcweir             if ( aContext.createComponent( "com.sun.star.security.DocumentDigitalSignatures", xSignatures ) )
229cdf0e10cSrcweir             {
230cdf0e10cSrcweir                 INetURLObject aURLReferer( sReferrer );
231cdf0e10cSrcweir 
232cdf0e10cSrcweir                 ::rtl::OUString aLocation;
233cdf0e10cSrcweir                 if ( aURLReferer.removeSegment() )
234cdf0e10cSrcweir                     aLocation = aURLReferer.GetMainURL( INetURLObject::NO_DECODE );
235cdf0e10cSrcweir 
236cdf0e10cSrcweir                 if ( aLocation.getLength() && xSignatures->isLocationTrusted( aLocation ) )
237cdf0e10cSrcweir                 {
238cdf0e10cSrcweir                     return allowMacroExecution();
239cdf0e10cSrcweir                 }
240cdf0e10cSrcweir             }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir             // at this point it is clear that the document is not in the secure location
243cdf0e10cSrcweir             if ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN )
244cdf0e10cSrcweir             {
245cdf0e10cSrcweir                 lcl_showDocumentMacrosDisabledError( rxInteraction, m_pData->m_bDocMacroDisabledMessageShown );
246cdf0e10cSrcweir                 return disallowMacroExecution();
247cdf0e10cSrcweir             }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir             // check whether the document is signed with trusted certificate
250cdf0e10cSrcweir             if ( nMacroExecutionMode != MacroExecMode::FROM_LIST )
251cdf0e10cSrcweir             {
252cdf0e10cSrcweir                 // the trusted macro check will also retrieve the signature state ( small optimization )
253cdf0e10cSrcweir                 sal_Bool bHasTrustedMacroSignature = m_pData->m_rDocumentAccess.hasTrustedScriptingSignature( nMacroExecutionMode != MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN );
254cdf0e10cSrcweir 
255cdf0e10cSrcweir                 sal_uInt16 nSignatureState = m_pData->m_rDocumentAccess.getScriptingSignatureState();
256cdf0e10cSrcweir                 if ( nSignatureState == SIGNATURESTATE_SIGNATURES_BROKEN )
257cdf0e10cSrcweir                 {
258cdf0e10cSrcweir                     // the signature is broken, no macro execution
259cdf0e10cSrcweir                     if ( nMacroExecutionMode != MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
260cdf0e10cSrcweir                         m_pData->m_rDocumentAccess.showBrokenSignatureWarning( rxInteraction );
261cdf0e10cSrcweir 
262cdf0e10cSrcweir                     return disallowMacroExecution();
263cdf0e10cSrcweir                 }
264cdf0e10cSrcweir                 else if ( bHasTrustedMacroSignature )
265cdf0e10cSrcweir                 {
266cdf0e10cSrcweir                     // there is trusted macro signature, allow macro execution
267cdf0e10cSrcweir                     return allowMacroExecution();
268cdf0e10cSrcweir                 }
269cdf0e10cSrcweir                 else if ( nSignatureState == SIGNATURESTATE_SIGNATURES_OK
270cdf0e10cSrcweir                        || nSignatureState == SIGNATURESTATE_SIGNATURES_NOTVALIDATED )
271cdf0e10cSrcweir                 {
272cdf0e10cSrcweir                     // there is valid signature, but it is not from the trusted author
273cdf0e10cSrcweir                     return disallowMacroExecution();
274cdf0e10cSrcweir                 }
275cdf0e10cSrcweir             }
276cdf0e10cSrcweir 
277cdf0e10cSrcweir             // at this point it is clear that the document is neither in secure location nor signed with trusted certificate
278cdf0e10cSrcweir             if  (   ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
279cdf0e10cSrcweir                 ||  ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
280cdf0e10cSrcweir                 )
281cdf0e10cSrcweir             {
282cdf0e10cSrcweir                 if  ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
283cdf0e10cSrcweir                     lcl_showDocumentMacrosDisabledError( rxInteraction, m_pData->m_bDocMacroDisabledMessageShown );
284cdf0e10cSrcweir 
285cdf0e10cSrcweir                 return disallowMacroExecution();
286cdf0e10cSrcweir             }
287cdf0e10cSrcweir         }
288cdf0e10cSrcweir         catch ( Exception& )
289cdf0e10cSrcweir         {
290cdf0e10cSrcweir             if  (   ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN )
291cdf0e10cSrcweir                 ||  ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
292cdf0e10cSrcweir                 ||  ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
293cdf0e10cSrcweir                 )
294cdf0e10cSrcweir             {
295cdf0e10cSrcweir                 return disallowMacroExecution();
296cdf0e10cSrcweir             }
297cdf0e10cSrcweir         }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir         // conformation is required
300cdf0e10cSrcweir         sal_Bool bSecure = sal_False;
301cdf0e10cSrcweir 
302cdf0e10cSrcweir         if ( eAutoConfirm == eNoAutoConfirm )
303cdf0e10cSrcweir         {
304cdf0e10cSrcweir             ::rtl::OUString sReferrer( m_pData->m_rDocumentAccess.getDocumentLocation() );
305cdf0e10cSrcweir 
306cdf0e10cSrcweir             ::rtl::OUString aSystemFileURL;
307cdf0e10cSrcweir             if ( osl::FileBase::getSystemPathFromFileURL( sReferrer, aSystemFileURL ) == osl::FileBase::E_None )
308cdf0e10cSrcweir                 sReferrer = aSystemFileURL;
309cdf0e10cSrcweir 
310cdf0e10cSrcweir             bSecure = lcl_showMacroWarning( rxInteraction, sReferrer );
311cdf0e10cSrcweir         }
312cdf0e10cSrcweir         else
313cdf0e10cSrcweir             bSecure = ( eAutoConfirm == eAutoConfirmApprove );
314cdf0e10cSrcweir 
315cdf0e10cSrcweir         return ( bSecure ? allowMacroExecution() : disallowMacroExecution() );
316cdf0e10cSrcweir     }
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 	//--------------------------------------------------------------------
319cdf0e10cSrcweir     sal_Bool DocumentMacroMode::isMacroExecutionDisallowed() const
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         return m_pData->m_rDocumentAccess.getCurrentMacroExecMode() == MacroExecMode::NEVER_EXECUTE;
322cdf0e10cSrcweir     }
323cdf0e10cSrcweir 
324cdf0e10cSrcweir     //--------------------------------------------------------------------
325cdf0e10cSrcweir     sal_Bool DocumentMacroMode::hasMacroLibrary() const
326cdf0e10cSrcweir     {
327cdf0e10cSrcweir         sal_Bool bHasMacroLib = sal_False;
328cdf0e10cSrcweir         try
329cdf0e10cSrcweir         {
330cdf0e10cSrcweir             Reference< XEmbeddedScripts > xScripts( m_pData->m_rDocumentAccess.getEmbeddedDocumentScripts() );
331cdf0e10cSrcweir             Reference< XLibraryContainer > xContainer;
332cdf0e10cSrcweir             if ( xScripts.is() )
333cdf0e10cSrcweir                 xContainer.set( xScripts->getBasicLibraries(), UNO_QUERY_THROW );
334cdf0e10cSrcweir 
335cdf0e10cSrcweir             if ( xContainer.is() )
336cdf0e10cSrcweir             {
337cdf0e10cSrcweir                 // a library container exists; check if it's empty
338cdf0e10cSrcweir 
339cdf0e10cSrcweir 			    // if there are libraries except the "Standard" library
340cdf0e10cSrcweir 			    // we assume that they are not empty (because they have been created by the user)
341cdf0e10cSrcweir 			    if ( !xContainer->hasElements() )
342cdf0e10cSrcweir                     bHasMacroLib = sal_False;
343cdf0e10cSrcweir                 else
344cdf0e10cSrcweir 			    {
345cdf0e10cSrcweir 				    ::rtl::OUString aStdLibName( RTL_CONSTASCII_USTRINGPARAM( "Standard" ) );
346cdf0e10cSrcweir 				    Sequence< ::rtl::OUString > aElements = xContainer->getElementNames();
347cdf0e10cSrcweir 				    if ( aElements.getLength() )
348cdf0e10cSrcweir 				    {
349cdf0e10cSrcweir 					    if ( aElements.getLength() > 1 || !aElements[0].equals( aStdLibName ) )
350cdf0e10cSrcweir 						    bHasMacroLib = sal_True;
351cdf0e10cSrcweir 					    else
352cdf0e10cSrcweir 					    {
353cdf0e10cSrcweir 						    // usually a "Standard" library is always present (design)
354cdf0e10cSrcweir 						    // for this reason we must check if it's empty
355cdf0e10cSrcweir                             //
356cdf0e10cSrcweir                             // Note: Since #i73229#, this is not true anymore. There's no default
357cdf0e10cSrcweir                             // "Standard" lib anymore. Wouldn't it be time to get completely
358cdf0e10cSrcweir                             // rid of the "Standard" thingie - this shouldn't be necessary
359cdf0e10cSrcweir                             // anymore, should it?
360cdf0e10cSrcweir                             // 2007-01-25 / frank.schoenheit@sun.com
361cdf0e10cSrcweir 						    Reference < XNameAccess > xLib;
362cdf0e10cSrcweir 						    Any aAny = xContainer->getByName( aStdLibName );
363cdf0e10cSrcweir 						    aAny >>= xLib;
364cdf0e10cSrcweir 						    if ( xLib.is() )
365cdf0e10cSrcweir 							    bHasMacroLib = xLib->hasElements();
366cdf0e10cSrcweir 					    }
367cdf0e10cSrcweir 				    }
368cdf0e10cSrcweir 			    }
369cdf0e10cSrcweir 		    }
370cdf0e10cSrcweir         }
371cdf0e10cSrcweir         catch( const Exception& )
372cdf0e10cSrcweir         {
373cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
374cdf0e10cSrcweir         }
375cdf0e10cSrcweir 
376cdf0e10cSrcweir         return bHasMacroLib;
377cdf0e10cSrcweir     }
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 	//--------------------------------------------------------------------
380cdf0e10cSrcweir     sal_Bool DocumentMacroMode::storageHasMacros( const Reference< XStorage >& rxStorage )
381cdf0e10cSrcweir     {
382cdf0e10cSrcweir 	    sal_Bool bHasMacros = sal_False;
383cdf0e10cSrcweir 	    if ( rxStorage.is() )
384cdf0e10cSrcweir 	    {
385cdf0e10cSrcweir 		    try
386cdf0e10cSrcweir 		    {
387cdf0e10cSrcweir                 static const ::rtl::OUString s_sBasicStorageName( ::rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "Basic" ) ) );
388cdf0e10cSrcweir                 static const ::rtl::OUString s_sScriptsStorageName( ::rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "Scripts" ) ) );
389cdf0e10cSrcweir 
390cdf0e10cSrcweir                 bHasMacros =(   (   rxStorage->hasByName( s_sBasicStorageName )
391cdf0e10cSrcweir                                 &&  rxStorage->isStorageElement( s_sBasicStorageName )
392cdf0e10cSrcweir                                 )
393cdf0e10cSrcweir                             ||  (   rxStorage->hasByName( s_sScriptsStorageName )
394cdf0e10cSrcweir                                 &&  rxStorage->isStorageElement( s_sScriptsStorageName )
395cdf0e10cSrcweir                                 )
396cdf0e10cSrcweir                             );
397cdf0e10cSrcweir 		    }
398cdf0e10cSrcweir 		    catch ( const Exception& )
399cdf0e10cSrcweir 		    {
400cdf0e10cSrcweir 			    DBG_UNHANDLED_EXCEPTION();
401cdf0e10cSrcweir 		    }
402cdf0e10cSrcweir 	    }
403cdf0e10cSrcweir         return bHasMacros;
404cdf0e10cSrcweir     }
405cdf0e10cSrcweir 
406cdf0e10cSrcweir 	//--------------------------------------------------------------------
407cdf0e10cSrcweir     sal_Bool DocumentMacroMode::checkMacrosOnLoading( const Reference< XInteractionHandler >& rxInteraction )
408cdf0e10cSrcweir     {
409cdf0e10cSrcweir         sal_Bool bAllow = sal_False;
410cdf0e10cSrcweir         if ( SvtSecurityOptions().IsMacroDisabled() )
411cdf0e10cSrcweir         {
412cdf0e10cSrcweir             // no macro should be executed at all
413cdf0e10cSrcweir             bAllow = disallowMacroExecution();
414cdf0e10cSrcweir         }
415cdf0e10cSrcweir         else
416cdf0e10cSrcweir         {
417cdf0e10cSrcweir             if ( m_pData->m_rDocumentAccess.documentStorageHasMacros() || hasMacroLibrary() )
418cdf0e10cSrcweir             {
419cdf0e10cSrcweir                 bAllow = adjustMacroMode( rxInteraction );
420cdf0e10cSrcweir             }
421cdf0e10cSrcweir             else if ( !isMacroExecutionDisallowed() )
422cdf0e10cSrcweir             {
423cdf0e10cSrcweir                 // if macros will be added by the user later, the security check is obsolete
424cdf0e10cSrcweir                 bAllow = allowMacroExecution();
425cdf0e10cSrcweir             }
426cdf0e10cSrcweir         }
427cdf0e10cSrcweir         return bAllow;
428cdf0e10cSrcweir     }
429cdf0e10cSrcweir 
430cdf0e10cSrcweir //........................................................................
431cdf0e10cSrcweir } // namespace sfx2
432cdf0e10cSrcweir //........................................................................
433