1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: extendedsecurityoptions.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
38 #include <svtools/extendedsecurityoptions.hxx>
39 #include <unotools/configmgr.hxx>
40 #include <unotools/configitem.hxx>
41 #include <tools/debug.hxx>
42 #include <com/sun/star/uno/Any.hxx>
43 #include <com/sun/star/uno/Sequence.hxx>
44 #include <tools/urlobj.hxx>
45 #include <tools/wldcrd.hxx>
46 #include <rtl/ustrbuf.hxx>
48 #include <svtools/pathoptions.hxx>
52 #include <rtl/logfile.hxx>
53 #include "itemholder1.hxx"
55 //_________________________________________________________________________________________________________________
57 //_________________________________________________________________________________________________________________
59 using namespace ::utl
;
60 using namespace ::rtl
;
61 using namespace ::osl
;
62 using namespace ::com::sun::star::uno
;
64 //_________________________________________________________________________________________________________________
66 //_________________________________________________________________________________________________________________
68 #define ROOTNODE_SECURITY OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Security"))
70 #define SECURE_EXTENSIONS_SET OUString(RTL_CONSTASCII_USTRINGPARAM("SecureExtensions"))
71 #define EXTENSION_PROPNAME OUString(RTL_CONSTASCII_USTRINGPARAM("/Extension"))
73 #define PROPERTYNAME_HYPERLINKS_OPEN OUString(RTL_CONSTASCII_USTRINGPARAM("Hyperlinks/Open"))
75 #define PROPERTYHANDLE_HYPERLINKS_OPEN 0
77 #define PROPERTYCOUNT 1
79 //_________________________________________________________________________________________________________________
80 // private declarations!
81 //_________________________________________________________________________________________________________________
83 struct OUStringHashCode
85 size_t operator()( const ::rtl::OUString
& sString
) const
87 return sString
.hashCode();
91 class ExtensionHashMap
: public ::std::hash_map
< ::rtl::OUString
,
94 ::std::equal_to
< ::rtl::OUString
> >
99 ExtensionHashMap().swap( *this );
103 class SvtExtendedSecurityOptions_Impl
: public ConfigItem
105 //-------------------------------------------------------------------------------------------------------------
107 //-------------------------------------------------------------------------------------------------------------
111 //---------------------------------------------------------------------------------------------------------
112 // constructor / destructor
113 //---------------------------------------------------------------------------------------------------------
115 SvtExtendedSecurityOptions_Impl();
116 ~SvtExtendedSecurityOptions_Impl();
118 //---------------------------------------------------------------------------------------------------------
119 // overloaded methods of baseclass
120 //---------------------------------------------------------------------------------------------------------
122 /*-****************************************************************************************************//**
123 @short called for notify of configmanager
124 @descr These method is called from the ConfigManager before application ends or from the
125 PropertyChangeListener if the sub tree broadcasts changes. You must update your
128 @seealso baseclass ConfigItem
130 @param "seqPropertyNames" is the list of properties which should be updated.
134 *//*-*****************************************************************************************************/
136 virtual void Notify( const Sequence
< OUString
>& seqPropertyNames
);
138 /*-****************************************************************************************************//**
139 @short write changes to configuration
140 @descr These method writes the changed values into the sub tree
141 and should always called in our destructor to guarantee consistency of config data.
143 @seealso baseclass ConfigItem
149 *//*-*****************************************************************************************************/
151 virtual void Commit();
153 //---------------------------------------------------------------------------------------------------------
155 //---------------------------------------------------------------------------------------------------------
157 /*-****************************************************************************************************//**
158 @short Access method to check for security problems
159 @descr Different methods to check for security related problems.
167 *//*-*****************************************************************************************************/
169 sal_Bool
IsSecureHyperlink( const rtl::OUString
& aURL
) const;
170 Sequence
< rtl::OUString
> GetSecureExtensionList() const;
172 SvtExtendedSecurityOptions::OpenHyperlinkMode
GetOpenHyperlinkMode();
173 void SetOpenHyperlinkMode( SvtExtendedSecurityOptions::OpenHyperlinkMode aMode
);
174 sal_Bool
IsOpenHyperlinkModeReadOnly() const;
176 //-------------------------------------------------------------------------------------------------------------
178 //-------------------------------------------------------------------------------------------------------------
182 /*-****************************************************************************************************//**
183 @short return list of key names of ouer configuration management which represent oue module tree
184 @descr These methods return a static const list of key names. We need it to get needed values from our
185 configuration management.
190 @return A list of needed configuration keys is returned.
193 *//*-*****************************************************************************************************/
195 static Sequence
< OUString
> GetPropertyNames();
197 /*-****************************************************************************************************//**
198 @short Fills the hash map with all extensions known to be secure
199 @descr These methods fills the given hash map object with all extensions known to be secure.
204 A hash map to be filled with secure extension strings.
208 *//*-*****************************************************************************************************/
209 void FillExtensionHashMap( ExtensionHashMap
& aHashMap
);
211 //-------------------------------------------------------------------------------------------------------------
213 //-------------------------------------------------------------------------------------------------------------
216 OUString m_aSecureExtensionsSetName
;
217 OUString m_aExtensionPropName
;
219 SvtExtendedSecurityOptions::OpenHyperlinkMode m_eOpenHyperlinkMode
;
220 sal_Bool m_bROOpenHyperlinkMode
;
221 ExtensionHashMap m_aExtensionHashMap
;
224 //_________________________________________________________________________________________________________________
226 //_________________________________________________________________________________________________________________
228 //*****************************************************************************************************************
230 //*****************************************************************************************************************
231 SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()
232 // Init baseclasses first
233 : ConfigItem ( ROOTNODE_SECURITY
),
234 m_aSecureExtensionsSetName( SECURE_EXTENSIONS_SET
),
235 m_aExtensionPropName( EXTENSION_PROPNAME
),
236 m_bROOpenHyperlinkMode(sal_False
)
239 // Fill the extension hash map with all secure extension strings
240 FillExtensionHashMap( m_aExtensionHashMap
);
242 Sequence
< OUString
> seqNames
= GetPropertyNames();
243 Sequence
< Any
> seqValues
= GetProperties( seqNames
);
244 Sequence
< sal_Bool
> seqRO
= GetReadOnlyStates ( seqNames
);
246 sal_Int32 nPropertyCount
= seqValues
.getLength();
247 for( sal_Int32 nProperty
=0; nProperty
<nPropertyCount
; ++nProperty
)
249 // Safe impossible cases.
250 // Check any for valid value.
251 DBG_ASSERT( !(seqValues
[nProperty
].hasValue()==sal_False
), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nInvalid property value detected!\n" );
254 case PROPERTYHANDLE_HYPERLINKS_OPEN
:
256 DBG_ASSERT( ( seqValues
[nProperty
].getValueTypeClass() == TypeClass_LONG
), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nWho has changed the value type of 'Hyperlink/Open'?" );
258 sal_Int32 nMode
= SvtExtendedSecurityOptions::OPEN_WITHSECURITYCHECK
;
259 if ( seqValues
[nProperty
] >>= nMode
)
260 m_eOpenHyperlinkMode
= (SvtExtendedSecurityOptions::OpenHyperlinkMode
)nMode
;
262 DBG_ERROR("Wrong type for Open mode!");
264 m_bROOpenHyperlinkMode
= seqRO
[nProperty
];
270 // Enable notification mechanism of our baseclass.
271 // We need it to get information about changes outside these class on ouer used configuration keys!
272 Sequence
< OUString
> seqNotifyNames( 1 );
273 seqNotifyNames
[0] = m_aSecureExtensionsSetName
;
274 EnableNotification( seqNotifyNames
);
277 //*****************************************************************************************************************
279 //*****************************************************************************************************************
280 SvtExtendedSecurityOptions_Impl::~SvtExtendedSecurityOptions_Impl()
282 // We must save our current values .. if user forget it!
283 if( IsModified() == sal_True
)
289 //*****************************************************************************************************************
291 //*****************************************************************************************************************
292 void SvtExtendedSecurityOptions_Impl::Notify( const Sequence
< OUString
>& )
297 //*****************************************************************************************************************
299 //*****************************************************************************************************************
300 void SvtExtendedSecurityOptions_Impl::Commit()
302 // Get names of supported properties, create a list for values and copy current values to it.
303 Sequence
< OUString
> seqNames
= GetPropertyNames ();
304 sal_Int32 nCount
= seqNames
.getLength();
305 Sequence
< Any
> seqValues ( nCount
);
306 for( sal_Int32 nProperty
=0; nProperty
<nCount
; ++nProperty
)
310 case PROPERTYHANDLE_HYPERLINKS_OPEN
: {
311 seqValues
[nProperty
] <<= (sal_Int32
)m_eOpenHyperlinkMode
;
317 // Set properties in configuration.
318 PutProperties( seqNames
, seqValues
);
321 //*****************************************************************************************************************
323 //*****************************************************************************************************************
324 sal_Bool
SvtExtendedSecurityOptions_Impl::IsSecureHyperlink( const OUString
& aURL
) const
326 INetURLObject
aURLObject( aURL
);
328 String aExtension
= aURLObject
.getExtension();
329 aExtension
.ToLowerAscii();
331 ExtensionHashMap::const_iterator pIter
= m_aExtensionHashMap
.find( aExtension
);
332 if ( pIter
!= m_aExtensionHashMap
.end() )
338 //*****************************************************************************************************************
340 //*****************************************************************************************************************
341 Sequence
< OUString
> SvtExtendedSecurityOptions_Impl::GetSecureExtensionList() const
343 Sequence
< OUString
> aResult( m_aExtensionHashMap
.size() );
345 sal_Int32 nIndex
= 0;
346 for ( ExtensionHashMap::const_iterator pIter
= m_aExtensionHashMap
.begin();
347 pIter
!= m_aExtensionHashMap
.end(); pIter
++ )
349 aResult
[nIndex
++] = pIter
->first
;
355 //*****************************************************************************************************************
357 //*****************************************************************************************************************
358 SvtExtendedSecurityOptions::OpenHyperlinkMode
SvtExtendedSecurityOptions_Impl::GetOpenHyperlinkMode()
360 return m_eOpenHyperlinkMode
;
362 /* -----------------09.07.2003 11:26-----------------
364 --------------------------------------------------*/
365 sal_Bool
SvtExtendedSecurityOptions_Impl::IsOpenHyperlinkModeReadOnly() const
367 return m_bROOpenHyperlinkMode
;
370 //*****************************************************************************************************************
372 //*****************************************************************************************************************
373 void SvtExtendedSecurityOptions_Impl::SetOpenHyperlinkMode( SvtExtendedSecurityOptions::OpenHyperlinkMode eNewMode
)
375 m_eOpenHyperlinkMode
= eNewMode
;
379 //*****************************************************************************************************************
381 //*****************************************************************************************************************
382 void SvtExtendedSecurityOptions_Impl::FillExtensionHashMap( ExtensionHashMap
& aHashMap
)
384 // Get sequence with secure extensions from configuration
385 Sequence
< OUString
> seqNodes
= GetNodeNames( m_aSecureExtensionsSetName
);
388 Sequence
< Any
> aValues
;
389 Sequence
< OUString
> aPropSeq( 1 );
390 for ( int i
= 0; i
< seqNodes
.getLength(); i
++ )
392 // Create access name for property
393 OUStringBuffer
aExtEntryProp( m_aSecureExtensionsSetName
);
394 aExtEntryProp
.appendAscii( "/" );
395 aExtEntryProp
.append( seqNodes
[i
] );
396 aExtEntryProp
.append( m_aExtensionPropName
);
398 aPropSeq
[0] = aExtEntryProp
.makeStringAndClear();
399 aValues
= GetProperties( aPropSeq
);
400 if ( aValues
.getLength() == 1 )
402 // Don't use value if sequence has not the correct length
403 if ( aValues
[0] >>= aValue
)
404 // Add extension into secure extensions hash map
405 aHashMap
.insert( ExtensionHashMap::value_type( aValue
.toAsciiLowerCase(), 1 ) );
408 DBG_ERRORFILE( "SvtExtendedSecurityOptions_Impl::FillExtensionHashMap(): not string value?" );
414 //*****************************************************************************************************************
415 // private method (currently not used)
416 //*****************************************************************************************************************
417 Sequence
< OUString
> SvtExtendedSecurityOptions_Impl::GetPropertyNames()
419 // Build static list of configuration key names.
420 static const OUString pProperties
[] =
422 PROPERTYNAME_HYPERLINKS_OPEN
424 // Initialize return sequence with these list ...
425 static const Sequence
< OUString
> seqPropertyNames( pProperties
, PROPERTYCOUNT
);
426 // ... and return it.
427 return seqPropertyNames
;
430 //*****************************************************************************************************************
431 // initialize static member
432 // DON'T DO IT IN YOUR HEADER!
433 // see definition for further informations
434 //*****************************************************************************************************************
435 SvtExtendedSecurityOptions_Impl
* SvtExtendedSecurityOptions::m_pDataContainer
= NULL
;
436 sal_Int32
SvtExtendedSecurityOptions::m_nRefCount
= 0 ;
438 //*****************************************************************************************************************
440 //*****************************************************************************************************************
441 SvtExtendedSecurityOptions::SvtExtendedSecurityOptions()
443 // Global access, must be guarded (multithreading!).
444 MutexGuard
aGuard( GetInitMutex() );
445 // Increase ouer refcount ...
447 // ... and initialize ouer data container only if it not already exist!
448 if( m_pDataContainer
== NULL
)
450 RTL_LOGFILE_CONTEXT(aLog
, "svtools ( ??? ) ::SvtExtendedSecurityOptions_Impl::ctor()");
451 m_pDataContainer
= new SvtExtendedSecurityOptions_Impl
;
453 ItemHolder1::holdConfigItem(E_EXTENDEDSECURITYOPTIONS
);
457 //*****************************************************************************************************************
459 //*****************************************************************************************************************
460 SvtExtendedSecurityOptions::~SvtExtendedSecurityOptions()
462 // Global access, must be guarded (multithreading!)
463 MutexGuard
aGuard( GetInitMutex() );
464 // Decrease ouer refcount.
466 // If last instance was deleted ...
467 // we must destroy ouer static data container!
468 if( m_nRefCount
<= 0 )
470 delete m_pDataContainer
;
471 m_pDataContainer
= NULL
;
475 //*****************************************************************************************************************
477 //*****************************************************************************************************************
478 sal_Bool
SvtExtendedSecurityOptions::IsSecureHyperlink( const rtl::OUString
& aURL
) const
480 MutexGuard
aGuard( GetInitMutex() );
481 return m_pDataContainer
->IsSecureHyperlink( aURL
);
484 //*****************************************************************************************************************
486 //*****************************************************************************************************************
487 Sequence
< rtl::OUString
> SvtExtendedSecurityOptions::GetSecureExtensionList() const
489 MutexGuard
aGuard( GetInitMutex() );
490 return m_pDataContainer
->GetSecureExtensionList();
493 //*****************************************************************************************************************
495 //*****************************************************************************************************************
496 SvtExtendedSecurityOptions::OpenHyperlinkMode
SvtExtendedSecurityOptions::GetOpenHyperlinkMode()
498 MutexGuard
aGuard( GetInitMutex() );
499 return m_pDataContainer
->GetOpenHyperlinkMode();
501 /* -----------------09.07.2003 11:26-----------------
503 --------------------------------------------------*/
504 sal_Bool
SvtExtendedSecurityOptions::IsOpenHyperlinkModeReadOnly() const
506 return m_pDataContainer
->IsOpenHyperlinkModeReadOnly();
509 //*****************************************************************************************************************
511 //*****************************************************************************************************************
512 void SvtExtendedSecurityOptions::SetOpenHyperlinkMode( SvtExtendedSecurityOptions::OpenHyperlinkMode eMode
)
514 MutexGuard
aGuard( GetInitMutex() );
515 m_pDataContainer
->SetOpenHyperlinkMode( eMode
);
518 //*****************************************************************************************************************
520 //*****************************************************************************************************************
521 Mutex
& SvtExtendedSecurityOptions::GetInitMutex()
523 // Initialize static mutex only for one time!
524 static Mutex
* pMutex
= NULL
;
525 // If these method first called (Mutex not already exist!) ...
528 // ... we must create a new one. Protect follow code with the global mutex -
529 // It must be - we create a static variable!
530 MutexGuard
aGuard( Mutex::getGlobalMutex() );
531 // We must check our pointer again - because it can be that another instance of ouer class will be fastr then these!
534 // Create the new mutex and set it for return on static variable.
539 // Return new created or already existing mutex object.