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: passwordcontainer.hxx,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 ************************************************************************/
30 #ifndef INCLUDED_COMPHELPER_PASSWORDCONTAINER_HXX
31 #define INCLUDED_COMPHELPER_PASSWORDCONTAINER_HXX
36 #include <com/sun/star/task/XPasswordContainer.hpp>
37 #include <com/sun/star/task/XUrlContainer.hpp>
38 #include <com/sun/star/task/PasswordRequestMode.hpp>
39 #include <com/sun/star/lang/XServiceInfo.hpp>
40 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
41 #include <com/sun/star/lang/XEventListener.hpp>
42 #include <com/sun/star/lang/XComponent.hpp>
43 #include <com/sun/star/task/XMasterPasswordHandling2.hpp>
44 #include <cppuhelper/implbase5.hxx>
45 #include <cppuhelper/typeprovider.hxx>
46 #include <cppuhelper/queryinterface.hxx>
47 #include <cppuhelper/factory.hxx>
49 #include <tools/stream.hxx>
50 #include <unotools/configitem.hxx>
51 #include <ucbhelper/interactionrequest.hxx>
53 #include <rtl/ref.hxx>
54 #include <osl/mutex.hxx>
56 #include "syscreds.hxx"
58 #define MEMORY_RECORD 0
59 #define PERSISTENT_RECORD 1
61 //----------------------------------------------------------------------------------
65 ::rtl::OUString m_aName
;
67 // there are two lists of passwords, memory passwords and persistent passwords
68 sal_Bool m_bHasMemPass
;
69 ::std::vector
< ::rtl::OUString
> m_aMemPass
;
71 // persistent passwords are encrypted in one string
72 sal_Bool m_bHasPersPass
;
73 ::rtl::OUString m_aPersPass
;
75 void InitArrays( sal_Bool bHasMemoryList
, const ::std::vector
< ::rtl::OUString
>& aMemoryList
,
76 sal_Bool bHasPersistentList
, const ::rtl::OUString
& aPersistentList
)
78 m_bHasMemPass
= bHasMemoryList
;
80 m_aMemPass
= aMemoryList
;
82 m_bHasPersPass
= bHasPersistentList
;
83 if ( bHasPersistentList
)
84 m_aPersPass
= aPersistentList
;
89 NamePassRecord( const ::rtl::OUString
& aName
)
91 , m_bHasMemPass( sal_False
)
92 , m_bHasPersPass( sal_False
)
96 NamePassRecord( const ::rtl::OUString
& aName
, const ::std::vector
< ::rtl::OUString
>& aMemoryList
)
98 , m_bHasMemPass( sal_True
)
99 , m_aMemPass( aMemoryList
)
100 , m_bHasPersPass( sal_False
)
104 NamePassRecord( const ::rtl::OUString
& aName
, const ::rtl::OUString
& aPersistentList
)
106 , m_bHasMemPass( sal_False
)
107 , m_bHasPersPass( sal_True
)
108 , m_aPersPass( aPersistentList
)
112 NamePassRecord( const ::rtl::OUString
& aName
,
113 sal_Bool bHasMemoryList
, const ::std::vector
< ::rtl::OUString
>& aMemoryList
,
114 sal_Bool bHasPersistentList
, const ::rtl::OUString aPersistentList
)
116 , m_bHasMemPass( bHasMemoryList
)
117 , m_bHasPersPass( bHasPersistentList
)
119 InitArrays( bHasMemoryList
, aMemoryList
, bHasPersistentList
, aPersistentList
);
122 NamePassRecord( const NamePassRecord
& aRecord
)
123 : m_aName( aRecord
.m_aName
)
124 , m_bHasMemPass( sal_False
)
125 , m_bHasPersPass( sal_False
)
127 InitArrays( aRecord
.m_bHasMemPass
, aRecord
.m_aMemPass
, aRecord
.m_bHasPersPass
, aRecord
.m_aPersPass
);
130 NamePassRecord
& operator=( const NamePassRecord
& aRecord
)
132 m_aName
= aRecord
.m_aName
;
135 m_aPersPass
= ::rtl::OUString();
136 InitArrays( aRecord
.m_bHasMemPass
, aRecord
.m_aMemPass
, aRecord
.m_bHasPersPass
, aRecord
.m_aPersPass
);
141 ::rtl::OUString
GetUserName() const
146 sal_Bool
HasPasswords( sal_Int8 nStatus
) const
148 if ( nStatus
== MEMORY_RECORD
)
149 return m_bHasMemPass
;
150 if ( nStatus
== PERSISTENT_RECORD
)
151 return m_bHasPersPass
;
156 ::std::vector
< ::rtl::OUString
> GetMemPasswords() const
161 return ::std::vector
< ::rtl::OUString
>();
164 ::rtl::OUString
GetPersPasswords() const
166 if ( m_bHasPersPass
)
169 return ::rtl::OUString();
172 void SetMemPasswords( const ::std::vector
< ::rtl::OUString
>& aMemList
)
174 m_aMemPass
= aMemList
;
175 m_bHasMemPass
= sal_True
;
178 void SetPersPasswords( const ::rtl::OUString
& aPersList
)
180 m_aPersPass
= aPersList
;
181 m_bHasPersPass
= sal_True
;
184 void RemovePasswords( sal_Int8 nStatus
)
186 if ( nStatus
== MEMORY_RECORD
)
188 m_bHasMemPass
= sal_False
;
191 else if ( nStatus
== PERSISTENT_RECORD
)
193 m_bHasPersPass
= sal_False
;
194 m_aPersPass
= ::rtl::OUString();
200 //----------------------------------------------------------------------------------
202 typedef ::std::pair
< const ::rtl::OUString
, ::std::list
< NamePassRecord
> > PairUrlRecord
;
203 typedef ::std::map
< ::rtl::OUString
, ::std::list
< NamePassRecord
> > PassMap
;
205 //----------------------------------------------------------------------------------
207 class PasswordContainer
;
209 class StorageItem
: public ::utl::ConfigItem
{
210 PasswordContainer
* mainCont
;
212 ::rtl::OUString mEncoded
;
214 StorageItem( PasswordContainer
* point
, const ::rtl::OUString
& path
) :
215 ConfigItem( path
, CONFIG_MODE_IMMEDIATE_UPDATE
),
217 hasEncoded( sal_False
)
219 ::com::sun::star::uno::Sequence
< ::rtl::OUString
> aNode( 1 );
220 *aNode
.getArray() = path
;
221 *aNode
.getArray() += ::rtl::OUString::createFromAscii( "/Store" );
222 EnableNotification( aNode
);
226 void update( const ::rtl::OUString
& url
, const NamePassRecord
& rec
);
227 void remove( const ::rtl::OUString
& url
, const ::rtl::OUString
& rec
);
230 sal_Bool
getEncodedMP( ::rtl::OUString
& aResult
);
231 void setEncodedMP( const ::rtl::OUString
& aResult
, sal_Bool bAcceptEnmpty
= sal_False
);
232 void setUseStorage( sal_Bool bUse
);
233 sal_Bool
useStorage();
235 virtual void Notify( const ::com::sun::star::uno::Sequence
< ::rtl::OUString
>& aPropertyNames
);
236 virtual void Commit();
239 //----------------------------------------------------------------------------------
247 class PasswordContainer
: public ::cppu::WeakImplHelper5
<
248 ::com::sun::star::task::XPasswordContainer
,
249 ::com::sun::star::task::XMasterPasswordHandling2
,
250 ::com::sun::star::task::XUrlContainer
,
251 ::com::sun::star::lang::XServiceInfo
,
252 ::com::sun::star::lang::XEventListener
>
255 PassMap m_aContainer
;
256 StorageItem
* m_pStorageFile
;
258 ::rtl::OUString m_aMasterPasswd
; // master password is set when the string is not empty
259 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
> mComponent
;
260 SysCredentialsConfig mUrlContainer
;
262 ::com::sun::star::uno::Sequence
< ::com::sun::star::task::UserRecord
> CopyToUserRecordSequence(
263 const ::std::list
< NamePassRecord
>& original
,
264 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
265 throw(::com::sun::star::uno::RuntimeException
);
267 ::com::sun::star::task::UserRecord
CopyToUserRecord(
268 const NamePassRecord
& aRecord
,
269 sal_Bool
& io_bTryToDecode
,
270 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& aHandler
);
272 ::com::sun::star::uno::Sequence
< ::com::sun::star::task::UserRecord
> FindUsr(
273 const ::std::list
< NamePassRecord
>& userlist
,
274 const ::rtl::OUString
& name
,
275 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
276 throw(::com::sun::star::uno::RuntimeException
);
277 bool createUrlRecord(
278 const PassMap::iterator
& rIter
,
280 const ::rtl::OUString
& aName
,
281 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& aHandler
,
282 ::com::sun::star::task::UrlRecord
& rRec
)
283 throw( ::com::sun::star::uno::RuntimeException
);
285 ::com::sun::star::task::UrlRecord
find(
286 const ::rtl::OUString
& aURL
,
287 const ::rtl::OUString
& aName
,
288 bool bName
, // only needed to support empty user names
289 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& aHandler
) throw(::com::sun::star::uno::RuntimeException
);
291 ::rtl::OUString
GetDefaultMasterPassword();
293 ::rtl::OUString
RequestPasswordFromUser(
294 ::com::sun::star::task::PasswordRequestMode aRMode
,
295 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& xHandler
);
297 ::rtl::OUString
GetMasterPassword( const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
298 throw(::com::sun::star::uno::RuntimeException
);
300 void UpdateVector( const ::rtl::OUString
& url
, ::std::list
< NamePassRecord
>& toUpdate
, NamePassRecord
& rec
, sal_Bool writeFile
)
301 throw(::com::sun::star::uno::RuntimeException
);
303 void PrivateAdd( const ::rtl::OUString
& aUrl
,
304 const ::rtl::OUString
& aUserName
,
305 const ::com::sun::star::uno::Sequence
< ::rtl::OUString
>& aPasswords
,
307 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
308 throw(::com::sun::star::uno::RuntimeException
);
310 ::std::vector
< ::rtl::OUString
> DecodePasswords( const ::rtl::OUString
& aLine
, const ::rtl::OUString
& aMasterPassword
)
311 throw(::com::sun::star::uno::RuntimeException
);
313 ::rtl::OUString
EncodePasswords( ::std::vector
< ::rtl::OUString
> lines
, const ::rtl::OUString
& aMasterPassword
)
314 throw(::com::sun::star::uno::RuntimeException
);
317 PasswordContainer( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& );
318 ~PasswordContainer();
320 virtual void SAL_CALL
add( const ::rtl::OUString
& aUrl
,
321 const ::rtl::OUString
& aUserName
,
322 const ::com::sun::star::uno::Sequence
< ::rtl::OUString
>& aPasswords
,
323 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
324 throw(::com::sun::star::uno::RuntimeException
);
326 virtual void SAL_CALL
addPersistent( const ::rtl::OUString
& aUrl
,
327 const ::rtl::OUString
& aUserName
,
328 const ::com::sun::star::uno::Sequence
< ::rtl::OUString
>& aPasswords
,
329 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
330 throw(::com::sun::star::uno::RuntimeException
);
332 virtual ::com::sun::star::task::UrlRecord SAL_CALL
333 find( const ::rtl::OUString
& aUrl
,
334 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
335 throw(::com::sun::star::uno::RuntimeException
);
337 virtual ::com::sun::star::task::UrlRecord SAL_CALL
338 findForName( const ::rtl::OUString
& aUrl
,
339 const ::rtl::OUString
& aUserName
,
340 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
341 throw(::com::sun::star::uno::RuntimeException
);
343 virtual void SAL_CALL
remove( const ::rtl::OUString
& aUrl
,
344 const ::rtl::OUString
& aUserName
)
345 throw(::com::sun::star::uno::RuntimeException
);
347 virtual void SAL_CALL
removePersistent( const ::rtl::OUString
& aUrl
,
348 const ::rtl::OUString
& aUserName
)
349 throw(::com::sun::star::uno::RuntimeException
);
351 virtual void SAL_CALL
removeAllPersistent() throw(::com::sun::star::uno::RuntimeException
);
353 virtual ::com::sun::star::uno::Sequence
< ::com::sun::star::task::UrlRecord
> SAL_CALL
354 getAllPersistent( const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
) throw(::com::sun::star::uno::RuntimeException
);
358 static ::rtl::OUString SAL_CALL
impl_getStaticImplementationName( ) throw(::com::sun::star::uno::RuntimeException
);
359 static ::com::sun::star::uno::Sequence
< ::rtl::OUString
> SAL_CALL
360 impl_getStaticSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException
);
361 static ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XSingleServiceFactory
> SAL_CALL
362 impl_createFactory( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& ServiceManager
) throw(::com::sun::star::uno::RuntimeException
);
363 static ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> SAL_CALL
364 impl_createInstance( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& xServiceManager
) throw( ::com::sun::star::uno::RuntimeException
);
367 virtual ::rtl::OUString SAL_CALL
getImplementationName( ) throw(::com::sun::star::uno::RuntimeException
);
368 virtual sal_Bool SAL_CALL
supportsService( const ::rtl::OUString
& ServiceName
) throw(::com::sun::star::uno::RuntimeException
);
370 virtual ::com::sun::star::uno::Sequence
< ::rtl::OUString
> SAL_CALL
371 getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException
);
374 virtual void SAL_CALL
disposing( const ::com::sun::star::lang::EventObject
& Source
)
375 throw(::com::sun::star::uno::RuntimeException
);
377 // XMasterPasswordHandling
378 virtual ::sal_Bool SAL_CALL
authorizateWithMasterPassword( const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& xHandler
)
379 throw (::com::sun::star::uno::RuntimeException
);
380 virtual ::sal_Bool SAL_CALL
changeMasterPassword( const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& xHandler
) throw (::com::sun::star::uno::RuntimeException
);
381 virtual void SAL_CALL
removeMasterPassword() throw (::com::sun::star::uno::RuntimeException
);
382 virtual ::sal_Bool SAL_CALL
hasMasterPassword( ) throw (::com::sun::star::uno::RuntimeException
);
383 virtual ::sal_Bool SAL_CALL
allowPersistentStoring( ::sal_Bool bAllow
) throw (::com::sun::star::uno::RuntimeException
);
384 virtual ::sal_Bool SAL_CALL
isPersistentStoringAllowed( ) throw (::com::sun::star::uno::RuntimeException
);
386 // XMasterPasswordHandling2
387 virtual ::sal_Bool SAL_CALL
useDefaultMasterPassword( const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& xHandler
) throw (::com::sun::star::uno::RuntimeException
);
388 virtual ::sal_Bool SAL_CALL
isDefaultMasterPasswordUsed( ) throw (::com::sun::star::uno::RuntimeException
);
391 virtual void SAL_CALL
addUrl( const ::rtl::OUString
& Url
, ::sal_Bool MakePersistent
) throw (::com::sun::star::uno::RuntimeException
);
392 virtual ::rtl::OUString SAL_CALL
findUrl( const ::rtl::OUString
& Url
) throw (::com::sun::star::uno::RuntimeException
);
393 virtual void SAL_CALL
removeUrl( const ::rtl::OUString
& Url
) throw (::com::sun::star::uno::RuntimeException
);
394 virtual ::com::sun::star::uno::Sequence
< ::rtl::OUString
> SAL_CALL
getUrls( ::sal_Bool OnlyPersistent
) throw (::com::sun::star::uno::RuntimeException
);
399 //----------------------------------------------------------------------------------
401 class MasterPasswordRequest_Impl
: public ucbhelper::InteractionRequest
403 ::rtl::Reference
< ucbhelper::InteractionSupplyAuthentication
> m_xAuthSupplier
;
406 MasterPasswordRequest_Impl( ::com::sun::star::task::PasswordRequestMode Mode
);
408 const ::rtl::Reference
< ucbhelper::InteractionSupplyAuthentication
> &
409 getAuthenticationSupplier() const { return m_xAuthSupplier
; }
413 //----------------------------------------------------------------------------------
415 class RW_SvMemoryStream
: public SvMemoryStream
{
417 RW_SvMemoryStream( void* Buf
, ULONG Size
, StreamMode eMode
):
418 SvMemoryStream( Buf
, Size
, eMode
){}
420 RW_SvMemoryStream( ULONG InitSize
=512, ULONG Resize
=64 ):
421 SvMemoryStream( InitSize
, Resize
){}
423 ULONG
getActualSize(){ return nEndOfData
; }
428 #endif // #ifndef INCLUDED_COMPHELPER_PASSWORDCONTAINER_HXX