1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_scripting.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetException.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/beans/UnknownPropertyException.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/container/XNameReplace.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/util/XChangesBatch.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/util/XMacroExpander.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/util/XStringSubstitution.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/awt/XDialog.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/security/AccessControlException.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/security/RuntimePermission.hpp>
45*cdf0e10cSrcweir #include <drafts/com/sun/star/script/framework/storage/XScriptStorageManager.hpp>
46*cdf0e10cSrcweir #include <drafts/com/sun/star/script/framework/storage/XScriptInfoAccess.hpp>
47*cdf0e10cSrcweir #include "ScriptSecurityManager.hxx"
48*cdf0e10cSrcweir #include <util/util.hxx>
49*cdf0e10cSrcweir #include <util/scriptingconstants.hxx>
50*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir using namespace ::rtl;
53*cdf0e10cSrcweir using namespace ::osl;
54*cdf0e10cSrcweir using namespace ::com::sun::star;
55*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
56*cdf0e10cSrcweir using namespace ::drafts::com::sun::star::script::framework;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir // is this in the utils?
59*cdf0e10cSrcweir const char* const SCRIPTSTORAGEMANAGER_SERVICE =
60*cdf0e10cSrcweir     "/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager";
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir namespace scripting_securitymgr
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir static OUString s_configProv = ::rtl::OUString::createFromAscii(
66*cdf0e10cSrcweir     "com.sun.star.configuration.ConfigurationProvider");
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir static OUString s_configAccess = ::rtl::OUString::createFromAscii(
69*cdf0e10cSrcweir     "com.sun.star.configuration.ConfigurationAccess");
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir static OUString s_configUpdate = ::rtl::OUString::createFromAscii(
72*cdf0e10cSrcweir     "com.sun.star.configuration.ConfigurationUpdateAccess");
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir static OUString s_securityDialog = ::rtl::OUString::createFromAscii(
75*cdf0e10cSrcweir     "com.sun.star.script.framework.security.SecurityDialog");
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir static const int PERMISSION_NEVER = 0;
78*cdf0e10cSrcweir static const int PERMISSION_PATHLIST = 1;
79*cdf0e10cSrcweir static const int PERMISSION_ALWAYS = 2;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir static const int ALLOW_RUN = 1;
82*cdf0e10cSrcweir static const int ADD_TO_PATH = 2;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir //*************************************************************************
85*cdf0e10cSrcweir // ScriptSecurityManager Constructor
86*cdf0e10cSrcweir ScriptSecurityManager::ScriptSecurityManager(
87*cdf0e10cSrcweir     const Reference< XComponentContext > & xContext ) throw ( RuntimeException )
88*cdf0e10cSrcweir     : m_xContext( xContext, UNO_SET_THROW )
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir     OSL_TRACE( "< ScriptSecurityManager ctor called >\n" );
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     // get the service manager from the context
93*cdf0e10cSrcweir     Reference< lang::XMultiComponentFactory > xMgr( m_xContext->getServiceManager(), UNO_SET_THROW );
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     // create an instance of the ConfigurationProvider
96*cdf0e10cSrcweir     m_xConfigProvFactory.set( xMgr->createInstanceWithContext( s_configProv, m_xContext ), UNO_QUERY_THROW );
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir void ScriptSecurityManager::addScriptStorage( rtl::OUString scriptStorageURL,
100*cdf0e10cSrcweir     sal_Int32 storageID)
101*cdf0e10cSrcweir throw ( RuntimeException )
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir     Permission_Hash::const_iterator ph_it = m_permissionSettings.find( scriptStorageURL );
104*cdf0e10cSrcweir     if ( ph_it != m_permissionSettings.end() )
105*cdf0e10cSrcweir     {
106*cdf0e10cSrcweir         OSL_TRACE( "ScriptSecurityManager::addScriptStorage: already called for %s",
107*cdf0e10cSrcweir             ::rtl::OUStringToOString( scriptStorageURL,
108*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US ).pData->buffer);
109*cdf0e10cSrcweir         return;
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir     StoragePerm newPerm;
112*cdf0e10cSrcweir     newPerm.scriptStorageURL=scriptStorageURL;
113*cdf0e10cSrcweir     newPerm.storageID=storageID;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     // we err on the side of caution!!
116*cdf0e10cSrcweir     newPerm.execPermission=sal_False;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     //need to check if storage has any scripts
119*cdf0e10cSrcweir     try
120*cdf0e10cSrcweir     {
121*cdf0e10cSrcweir         // we have some scripts so read config & decide on that basis
122*cdf0e10cSrcweir         // Setup flags: m_runMacroSetting, m_warning, m_confirmationRequired,
123*cdf0e10cSrcweir         readConfiguration();
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir     catch ( RuntimeException & rte )
126*cdf0e10cSrcweir     {
127*cdf0e10cSrcweir         OSL_TRACE( "ScriptSecurityManager::addScriptStorage: caught RuntimeException: %s",
128*cdf0e10cSrcweir             ::rtl::OUStringToOString( rte.Message,
129*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US ).pData->buffer);
130*cdf0e10cSrcweir         throw RuntimeException(
131*cdf0e10cSrcweir             OUSTR( "ScriptSecurityManager::addScriptStorage: caught RuntimeException" ).concat( rte.Message ),
132*cdf0e10cSrcweir         Reference< XInterface >() );
133*cdf0e10cSrcweir     }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     switch( m_runMacroSetting )
136*cdf0e10cSrcweir     {
137*cdf0e10cSrcweir         case PERMISSION_NEVER:         // never
138*cdf0e10cSrcweir         {
139*cdf0e10cSrcweir             OSL_TRACE("never run");
140*cdf0e10cSrcweir             break;
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir         case PERMISSION_PATHLIST:         // according to path list
143*cdf0e10cSrcweir         {
144*cdf0e10cSrcweir             OSL_TRACE("according to path");
145*cdf0e10cSrcweir             // check path
146*cdf0e10cSrcweir             rtl::OUString path = scriptStorageURL.copy( 0, scriptStorageURL.lastIndexOf( '/' ) );
147*cdf0e10cSrcweir             OSL_TRACE( "no of elts in path list = %d",
148*cdf0e10cSrcweir                 (int)m_secureURL.getLength() );
149*cdf0e10cSrcweir             bool match = isSecureURL( path );
150*cdf0e10cSrcweir             if( match &&  ( m_warning == sal_True ) )
151*cdf0e10cSrcweir             {
152*cdf0e10cSrcweir                 OSL_TRACE("path match & warning dialog");
153*cdf0e10cSrcweir                 int result = (int)executeStandardDialog();
154*cdf0e10cSrcweir                 OSL_TRACE("result = %d", (int)result);
155*cdf0e10cSrcweir                 if ( (result&ALLOW_RUN) == ALLOW_RUN )
156*cdf0e10cSrcweir                 {
157*cdf0e10cSrcweir                     newPerm.execPermission=sal_True;
158*cdf0e10cSrcweir                 }
159*cdf0e10cSrcweir                 break;
160*cdf0e10cSrcweir             }
161*cdf0e10cSrcweir             else if ( match )
162*cdf0e10cSrcweir             {
163*cdf0e10cSrcweir                 OSL_TRACE("path match & no warning dialog");
164*cdf0e10cSrcweir                 newPerm.execPermission=sal_True;
165*cdf0e10cSrcweir                 break;
166*cdf0e10cSrcweir             }
167*cdf0e10cSrcweir             else if( m_confirmationRequired == sal_True )
168*cdf0e10cSrcweir             {
169*cdf0e10cSrcweir                 OSL_TRACE("no path match & confirmation dialog");
170*cdf0e10cSrcweir                 int result = (int)executePathDialog( path );
171*cdf0e10cSrcweir                 OSL_TRACE("result = %d", (int)result);
172*cdf0e10cSrcweir                 if ( (result&ALLOW_RUN) == ALLOW_RUN )
173*cdf0e10cSrcweir                 {
174*cdf0e10cSrcweir                     newPerm.execPermission=sal_True;
175*cdf0e10cSrcweir                 }
176*cdf0e10cSrcweir                 if ( (result&ADD_TO_PATH) == ADD_TO_PATH )
177*cdf0e10cSrcweir                 {
178*cdf0e10cSrcweir                     /* if checkbox clicked then need to add path to registry*/
179*cdf0e10cSrcweir                     addToSecurePaths(path);
180*cdf0e10cSrcweir                 }
181*cdf0e10cSrcweir             }
182*cdf0e10cSrcweir             break;
183*cdf0e10cSrcweir         }
184*cdf0e10cSrcweir         case PERMISSION_ALWAYS:         // always
185*cdf0e10cSrcweir             if( m_warning == sal_True )
186*cdf0e10cSrcweir             {
187*cdf0e10cSrcweir                 OSL_TRACE("always & warning dialog");
188*cdf0e10cSrcweir                 short result = executeStandardDialog();
189*cdf0e10cSrcweir                 if ( (result&ALLOW_RUN) == ALLOW_RUN )
190*cdf0e10cSrcweir                 {
191*cdf0e10cSrcweir                     newPerm.execPermission=sal_True;
192*cdf0e10cSrcweir                 }
193*cdf0e10cSrcweir             }
194*cdf0e10cSrcweir             else
195*cdf0e10cSrcweir             {
196*cdf0e10cSrcweir                 OSL_TRACE("always & no warning dialog");
197*cdf0e10cSrcweir                 newPerm.execPermission=sal_True;
198*cdf0e10cSrcweir             }
199*cdf0e10cSrcweir             break;
200*cdf0e10cSrcweir         default:
201*cdf0e10cSrcweir                 //
202*cdf0e10cSrcweir                 throw RuntimeException(
203*cdf0e10cSrcweir                     OUSTR( "ScriptSecurityManager::addScriptStorage got invalid OfficeBasic setting"),
204*cdf0e10cSrcweir                     Reference< XInterface > ());
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir     if ( newPerm.execPermission == sal_True )
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         OSL_TRACE("setting exec permission to true for %s",
210*cdf0e10cSrcweir             ::rtl::OUStringToOString( scriptStorageURL,
211*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir     else
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir         OSL_TRACE("setting exec permission to false for %s",
216*cdf0e10cSrcweir             ::rtl::OUStringToOString( scriptStorageURL,
217*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
218*cdf0e10cSrcweir     }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir     m_permissionSettings[ scriptStorageURL ] = newPerm;
221*cdf0e10cSrcweir }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir bool ScriptSecurityManager::isSecureURL( const OUString & path )
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir     bool match = false;
226*cdf0e10cSrcweir     OSL_TRACE( "no of elts in path list = %d",
227*cdf0e10cSrcweir         (int)m_secureURL.getLength() );
228*cdf0e10cSrcweir     OSL_TRACE("document path: %s",
229*cdf0e10cSrcweir         ::rtl::OUStringToOString( path,
230*cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US ).pData->buffer);
231*cdf0e10cSrcweir     int length = m_secureURL.getLength();
232*cdf0e10cSrcweir     for( int j = 0; j < length ; j++ )
233*cdf0e10cSrcweir     {
234*cdf0e10cSrcweir         OSL_TRACE("path list element: %s",
235*cdf0e10cSrcweir             ::rtl::OUStringToOString( m_secureURL[j],
236*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US ).pData->buffer);
237*cdf0e10cSrcweir #ifdef WIN32
238*cdf0e10cSrcweir         OSL_TRACE("case insensitive comparison");
239*cdf0e10cSrcweir         if( path.equalsIgnoreAsciiCase( m_secureURL[j] ) )
240*cdf0e10cSrcweir #else
241*cdf0e10cSrcweir         OSL_TRACE("case sensitive comparison");
242*cdf0e10cSrcweir         if( path.equals( m_secureURL[j] ) )
243*cdf0e10cSrcweir #endif
244*cdf0e10cSrcweir         {
245*cdf0e10cSrcweir             match = true;
246*cdf0e10cSrcweir             break;
247*cdf0e10cSrcweir         }
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir     return match;
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir short ScriptSecurityManager::executeStandardDialog()
253*cdf0e10cSrcweir throw ( RuntimeException )
254*cdf0e10cSrcweir {
255*cdf0e10cSrcweir     OUString dummyString;
256*cdf0e10cSrcweir     return executeDialog( dummyString );
257*cdf0e10cSrcweir }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir short ScriptSecurityManager::executePathDialog( const OUString & path )
260*cdf0e10cSrcweir throw ( RuntimeException )
261*cdf0e10cSrcweir {
262*cdf0e10cSrcweir     return executeDialog( path );
263*cdf0e10cSrcweir }
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir short ScriptSecurityManager::executeDialog( const OUString & path )
266*cdf0e10cSrcweir throw ( RuntimeException )
267*cdf0e10cSrcweir {
268*cdf0e10cSrcweir     Sequence < Any > aArgs;
269*cdf0e10cSrcweir     if( path.getLength() != 0 )
270*cdf0e10cSrcweir     {
271*cdf0e10cSrcweir         OSL_TRACE("reallocing");
272*cdf0e10cSrcweir         aArgs.realloc(1);
273*cdf0e10cSrcweir         aArgs[ 0 ] <<= path;
274*cdf0e10cSrcweir     }
275*cdf0e10cSrcweir     short result;
276*cdf0e10cSrcweir     try
277*cdf0e10cSrcweir     {
278*cdf0e10cSrcweir         Reference< lang::XMultiComponentFactory > xMgr( m_xContext->getServiceManager(), UNO_SET_THROW );
279*cdf0e10cSrcweir         Reference< awt::XDialog > xDialog(
280*cdf0e10cSrcweir             xMgr->createInstanceWithArgumentsAndContext( s_securityDialog, aArgs, m_xContext ),
281*cdf0e10cSrcweir             UNO_QUERY_THROW );
282*cdf0e10cSrcweir         result = xDialog->execute();
283*cdf0e10cSrcweir         Reference< lang::XComponent > xComponent( xDialog, UNO_QUERY_THROW );
284*cdf0e10cSrcweir         xComponent->dispose();
285*cdf0e10cSrcweir     }
286*cdf0e10cSrcweir     catch ( RuntimeException & rte )
287*cdf0e10cSrcweir     {
288*cdf0e10cSrcweir         throw RuntimeException(
289*cdf0e10cSrcweir             OUSTR( "ScriptSecurityManager::executeDialog: caught RuntimeException: ").concat( rte.Message ),
290*cdf0e10cSrcweir             Reference< XInterface > ());
291*cdf0e10cSrcweir     }
292*cdf0e10cSrcweir     catch ( Exception & e )
293*cdf0e10cSrcweir     {
294*cdf0e10cSrcweir         throw RuntimeException(
295*cdf0e10cSrcweir             OUSTR( "ScriptSecurityManager::executeDialog: caught Exception: ").concat( e.Message ),
296*cdf0e10cSrcweir             Reference< XInterface > ());
297*cdf0e10cSrcweir     }
298*cdf0e10cSrcweir     return result;
299*cdf0e10cSrcweir }
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir /**
302*cdf0e10cSrcweir  * checks to see whether the requested ScriptPermission is allowed.
303*cdf0e10cSrcweir  * This was modelled after the Java AccessController, but at this time
304*cdf0e10cSrcweir  * we can't see a good reason not to return a bool, rather than throw
305*cdf0e10cSrcweir  * an exception if the request is not granted (as is the case in Java).
306*cdf0e10cSrcweir  */
307*cdf0e10cSrcweir void ScriptSecurityManager::checkPermission( const OUString & scriptStorageURL,
308*cdf0e10cSrcweir     const OUString & permissionRequest )
309*cdf0e10cSrcweir     throw ( RuntimeException, lang::IllegalArgumentException, security::AccessControlException )
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir     if( permissionRequest.equals( OUString::createFromAscii( "execute" ) ) )
312*cdf0e10cSrcweir     {
313*cdf0e10cSrcweir         OSL_TRACE(
314*cdf0e10cSrcweir             "ScriptSecurityManager::checkPermission: execute permission request for %s",
315*cdf0e10cSrcweir             ::rtl::OUStringToOString( scriptStorageURL,
316*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US ).pData->buffer);
317*cdf0e10cSrcweir         Permission_Hash::const_iterator ph_it = m_permissionSettings.find( scriptStorageURL );
318*cdf0e10cSrcweir         Permission_Hash::const_iterator ph_itend =
319*cdf0e10cSrcweir             m_permissionSettings.end();
320*cdf0e10cSrcweir         if ( ph_it != ph_itend )
321*cdf0e10cSrcweir         {
322*cdf0e10cSrcweir             if ( ph_it->second.execPermission )
323*cdf0e10cSrcweir             {
324*cdf0e10cSrcweir                 return;
325*cdf0e10cSrcweir             }
326*cdf0e10cSrcweir             else
327*cdf0e10cSrcweir             {
328*cdf0e10cSrcweir                 OSL_TRACE( "permission refused" );
329*cdf0e10cSrcweir                 Any aPermission;
330*cdf0e10cSrcweir                 security::RuntimePermission permission;
331*cdf0e10cSrcweir                 permission.Name = OUString::createFromAscii( "execute" ).concat( scriptStorageURL );
332*cdf0e10cSrcweir                 aPermission <<= permission;
333*cdf0e10cSrcweir                 throw security::AccessControlException(
334*cdf0e10cSrcweir                     OUString::createFromAscii( "ScriptSecurityManager::checkPermission: no execute permission for URL" ).concat( scriptStorageURL ),
335*cdf0e10cSrcweir                     Reference< XInterface > (), aPermission );
336*cdf0e10cSrcweir             }
337*cdf0e10cSrcweir         }
338*cdf0e10cSrcweir         // we should never get here!!
339*cdf0e10cSrcweir         throw lang::IllegalArgumentException( OUString::createFromAscii( "ScriptSecurityManager::checkPermission: storageURL not found" ), Reference< XInterface > (), 0 );
340*cdf0e10cSrcweir     }
341*cdf0e10cSrcweir     // inappropriate permission request
342*cdf0e10cSrcweir     throw lang::IllegalArgumentException( OUString::createFromAscii( "ScriptSecurityManager::checkPermission: storageURL not found" ), Reference< XInterface > (), 1 );
343*cdf0e10cSrcweir }
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir void ScriptSecurityManager::removePermissionSettings ( ::rtl::OUString & scriptStorageURL )
346*cdf0e10cSrcweir {
347*cdf0e10cSrcweir     Permission_Hash::const_iterator ph_it =
348*cdf0e10cSrcweir         m_permissionSettings.find( scriptStorageURL );
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir     if ( ph_it == m_permissionSettings.end() )
351*cdf0e10cSrcweir     {
352*cdf0e10cSrcweir         OSL_TRACE( "Entry for storage url %s doesn't exist in map",
353*cdf0e10cSrcweir             ::rtl::OUStringToOString( scriptStorageURL,
354*cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US ).pData->buffer);
355*cdf0e10cSrcweir         return;
356*cdf0e10cSrcweir     }
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir     // erase the entry from the hash
359*cdf0e10cSrcweir     m_permissionSettings.erase( scriptStorageURL );
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir }
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir void ScriptSecurityManager::readConfiguration()
364*cdf0e10cSrcweir     throw ( RuntimeException)
365*cdf0e10cSrcweir {
366*cdf0e10cSrcweir     try
367*cdf0e10cSrcweir     {
368*cdf0e10cSrcweir         beans::PropertyValue configPath;
369*cdf0e10cSrcweir         configPath.Name = ::rtl::OUString::createFromAscii( "nodepath" );
370*cdf0e10cSrcweir         configPath.Value <<= ::rtl::OUString::createFromAscii( "org.openoffice.Office.Common/Security/Scripting" );
371*cdf0e10cSrcweir         Sequence < Any > aargs( 1 );
372*cdf0e10cSrcweir         aargs[ 0 ] <<= configPath;
373*cdf0e10cSrcweir         ENSURE_OR_THROW( m_xConfigProvFactory.is(),
374*cdf0e10cSrcweir             "ScriptSecurityManager::readConfiguration: ConfigProviderFactory no longer valid!" );
375*cdf0e10cSrcweir         // get the XPropertySet interface from the ConfigurationAccess service
376*cdf0e10cSrcweir         Reference < beans::XPropertySet > xPropSet( m_xConfigProvFactory->createInstanceWithArguments( s_configAccess, aargs ), UNO_QUERY_THROW );
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir         m_confirmationRequired = sal_True;
379*cdf0e10cSrcweir         OSL_VERIFY( xPropSet->getPropertyValue( OUSTR( "Confirmation" ) ) >>= m_confirmationRequired );
380*cdf0e10cSrcweir         if ( m_confirmationRequired == sal_True )
381*cdf0e10cSrcweir         {
382*cdf0e10cSrcweir             OSL_TRACE( "ScriptSecurityManager:readConfiguration: confirmation is true" );
383*cdf0e10cSrcweir         }
384*cdf0e10cSrcweir         else
385*cdf0e10cSrcweir         {
386*cdf0e10cSrcweir             OSL_TRACE( "ScriptSecurityManager:readConfiguration: confirmation is false" );
387*cdf0e10cSrcweir         }
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir         m_warning = true;
390*cdf0e10cSrcweir         OSL_VERIFY( xPropSet->getPropertyValue( OUSTR( "Warning" ) ) >>= m_warning );
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir         if ( m_warning == sal_True )
393*cdf0e10cSrcweir         {
394*cdf0e10cSrcweir             OSL_TRACE( "ScriptSecurityManager:readConfiguration: warning is true" );
395*cdf0e10cSrcweir         }
396*cdf0e10cSrcweir         else
397*cdf0e10cSrcweir         {
398*cdf0e10cSrcweir             OSL_TRACE( "ScriptSecurityManager:readConfiguration: warning is false" );
399*cdf0e10cSrcweir         }
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir         m_runMacroSetting = sal_True;
402*cdf0e10cSrcweir         OSL_VERIFY( xPropSet->getPropertyValue( OUSTR( "OfficeBasic" ) ) >>= m_runMacroSetting );
403*cdf0e10cSrcweir         OSL_TRACE( "ScriptSecurityManager:readConfiguration: OfficeBasic = %d", m_runMacroSetting );
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir         m_secureURL = ::rtl::OUString();
406*cdf0e10cSrcweir         OSL_VERIFY( xPropSet->getPropertyValue( OUSTR( "SecureURL" ) ) >>= m_secureURL );
407*cdf0e10cSrcweir     }
408*cdf0e10cSrcweir     catch ( beans::UnknownPropertyException & upe )
409*cdf0e10cSrcweir     {
410*cdf0e10cSrcweir         throw RuntimeException(
411*cdf0e10cSrcweir             OUSTR( "ScriptSecurityManager:readConfiguration: Attempt to read unknown property: " ).concat( upe.Message ),
412*cdf0e10cSrcweir             Reference< XInterface > () );
413*cdf0e10cSrcweir     }
414*cdf0e10cSrcweir     catch ( lang::WrappedTargetException & wte )
415*cdf0e10cSrcweir     {
416*cdf0e10cSrcweir         throw RuntimeException(
417*cdf0e10cSrcweir             OUSTR( "ScriptSecurityManager:readConfiguration: wrapped target exception? :" ).concat( wte.Message ),
418*cdf0e10cSrcweir             Reference< XInterface > () );
419*cdf0e10cSrcweir     }
420*cdf0e10cSrcweir     catch ( Exception & e )
421*cdf0e10cSrcweir     {
422*cdf0e10cSrcweir         OSL_TRACE( "Unknown exception in readconf: %s",
423*cdf0e10cSrcweir             ::rtl::OUStringToOString(e.Message ,
424*cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US ).pData->buffer  );
425*cdf0e10cSrcweir         throw RuntimeException(
426*cdf0e10cSrcweir             OUSTR( "ScriptSecurityManager:readConfiguration: exception? :" ).concat( e.Message ),
427*cdf0e10cSrcweir             Reference< XInterface > () );
428*cdf0e10cSrcweir     }
429*cdf0e10cSrcweir #ifdef _DEBUG
430*cdf0e10cSrcweir     catch ( ... )
431*cdf0e10cSrcweir     {
432*cdf0e10cSrcweir         OSL_TRACE( "Completely Unknown exception in readconf!!!!!!");
433*cdf0e10cSrcweir         throw RuntimeException(
434*cdf0e10cSrcweir             OUSTR( "ScriptSecurityManager:readConfiguration: exception? :" ),
435*cdf0e10cSrcweir             Reference< XInterface > () );
436*cdf0e10cSrcweir     }
437*cdf0e10cSrcweir #endif
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir     int length = m_secureURL.getLength();
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir     // PathSubstitution needed to interpret variables found in config
442*cdf0e10cSrcweir     Reference< lang::XMultiComponentFactory > xMgr( m_xContext->getServiceManager(), UNO_SET_THROW );
443*cdf0e10cSrcweir     Reference< XInterface > xInterface = );
444*cdf0e10cSrcweir     Reference< util::XStringSubstitution > xStringSubstitution(
445*cdf0e10cSrcweir         xMgr->createInstanceWithContext(
446*cdf0e10cSrcweir             ::rtl::OUString::createFromAscii( "com.sun.star.util.PathSubstitution" ), m_xContext
447*cdf0e10cSrcweir         ),
448*cdf0e10cSrcweir         UNO_QUERY_THROW
449*cdf0e10cSrcweir     );
450*cdf0e10cSrcweir     for( int i = 0; i < length; i++ )
451*cdf0e10cSrcweir     {
452*cdf0e10cSrcweir         OSL_TRACE( "ScriptSecurityManager:readConfiguration path = %s",
453*cdf0e10cSrcweir             ::rtl::OUStringToOString(m_secureURL[i] ,
454*cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US ).pData->buffer  );
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir         OSL_TRACE( "ScriptSecurityManager: subpath = %s",
457*cdf0e10cSrcweir             ::rtl::OUStringToOString(
458*cdf0e10cSrcweir             xStringSubstitution->substituteVariables( m_secureURL[i], true ),
459*cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US ).pData->buffer );
460*cdf0e10cSrcweir         m_secureURL[i] = xStringSubstitution->substituteVariables( m_secureURL[i], true );
461*cdf0e10cSrcweir     }
462*cdf0e10cSrcweir #ifdef _DEBUG
463*cdf0e10cSrcweir     int length2 = m_secureURL.getLength();
464*cdf0e10cSrcweir     for( int j = 0; j < length2 ; j++ )
465*cdf0e10cSrcweir     {
466*cdf0e10cSrcweir         OSL_TRACE( "ScriptSecurityManager: path = %s",
467*cdf0e10cSrcweir             ::rtl::OUStringToOString(m_secureURL[j] ,
468*cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US ).pData->buffer  );
469*cdf0e10cSrcweir     }
470*cdf0e10cSrcweir #endif
471*cdf0e10cSrcweir }
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir void ScriptSecurityManager::addToSecurePaths( const OUString & path )
474*cdf0e10cSrcweir throw ( RuntimeException )
475*cdf0e10cSrcweir {
476*cdf0e10cSrcweir     OSL_TRACE( "--->ScriptSecurityManager::addToSecurePaths" );
477*cdf0e10cSrcweir     beans::PropertyValue configPath;
478*cdf0e10cSrcweir     configPath.Name = ::rtl::OUString::createFromAscii( "nodepath" );
479*cdf0e10cSrcweir     configPath.Value <<= ::rtl::OUString::createFromAscii( "org.openoffice.Office.Common/Security/Scripting" );
480*cdf0e10cSrcweir     Sequence < Any > aargs( 1 );
481*cdf0e10cSrcweir     aargs[ 0 ] <<= configPath;
482*cdf0e10cSrcweir     Reference < container::XNameReplace > xNameReplace(
483*cdf0e10cSrcweir         m_xConfigProvFactory->createInstanceWithArguments( s_configUpdate, aargs ), UNO_QUERY_THROW );
484*cdf0e10cSrcweir     Reference < util::XChangesBatch > xChangesBatch( xNameReplace, UNO_QUERY_THROW );
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir     OSL_TRACE( "--->ScriptSecurityManager::addToSecurePaths: after if stuff" );
487*cdf0e10cSrcweir     Reference < beans::XPropertySet > xPropSet( xInterface, UNO_QUERY );
488*cdf0e10cSrcweir     css::uno::Sequence< rtl::OUString > newSecureURL;
489*cdf0e10cSrcweir     Any value;
490*cdf0e10cSrcweir     OUString pathListPropName = OUSTR ( "SecureURL" );
491*cdf0e10cSrcweir     value=xPropSet->getPropertyValue( pathListPropName );
492*cdf0e10cSrcweir     if ( sal_False == ( value >>= newSecureURL ) )
493*cdf0e10cSrcweir     {
494*cdf0e10cSrcweir         throw RuntimeException(
495*cdf0e10cSrcweir             OUSTR( "ScriptSecurityManager::addToSecurePaths: can't get SecureURL setting" ),
496*cdf0e10cSrcweir             Reference< XInterface > () );
497*cdf0e10cSrcweir     }
498*cdf0e10cSrcweir     try
499*cdf0e10cSrcweir     {
500*cdf0e10cSrcweir         sal_Int32 length = newSecureURL.getLength();
501*cdf0e10cSrcweir         newSecureURL.realloc( length + 1 );
502*cdf0e10cSrcweir         newSecureURL[ length ] = path;
503*cdf0e10cSrcweir         Any aNewSecureURL;
504*cdf0e10cSrcweir         aNewSecureURL <<= newSecureURL;
505*cdf0e10cSrcweir         xNameReplace->replaceByName( pathListPropName, aNewSecureURL );
506*cdf0e10cSrcweir         xChangesBatch->commitChanges();
507*cdf0e10cSrcweir         m_secureURL = newSecureURL;
508*cdf0e10cSrcweir     }
509*cdf0e10cSrcweir     catch ( Exception & e )
510*cdf0e10cSrcweir     {
511*cdf0e10cSrcweir         OSL_TRACE( "Error updating secure paths: " );
512*cdf0e10cSrcweir         throw RuntimeException(
513*cdf0e10cSrcweir             OUSTR( "ScriptSecurityManager::addToSecurePaths: error updating SecureURL setting" ).concat( e.Message ),
514*cdf0e10cSrcweir             Reference< XInterface > () );
515*cdf0e10cSrcweir     }
516*cdf0e10cSrcweir }
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir //*************************************************************************
519*cdf0e10cSrcweir // ScriptSecurityManager Destructor
520*cdf0e10cSrcweir ScriptSecurityManager::~ScriptSecurityManager()
521*cdf0e10cSrcweir {
522*cdf0e10cSrcweir     OSL_TRACE( "< ScriptSecurityManager dtor called >\n" );
523*cdf0e10cSrcweir }
524*cdf0e10cSrcweir 
525*cdf0e10cSrcweir } // Namespace
526