1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_SVL_SOURCE_INC_PASSWORDCONTAINER_HXX
20 #define INCLUDED_SVL_SOURCE_INC_PASSWORDCONTAINER_HXX
25 #include <com/sun/star/task/XPasswordContainer2.hpp>
26 #include <com/sun/star/task/PasswordRequestMode.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
29 #include <com/sun/star/lang/XEventListener.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
31 #include <cppuhelper/implbase3.hxx>
32 #include <cppuhelper/typeprovider.hxx>
33 #include <cppuhelper/queryinterface.hxx>
34 #include <cppuhelper/factory.hxx>
36 #include <tools/stream.hxx>
37 #include <unotools/configitem.hxx>
38 #include <ucbhelper/interactionrequest.hxx>
40 #include <rtl/ref.hxx>
41 #include <osl/mutex.hxx>
43 #include "syscreds.hxx"
45 #define MEMORY_RECORD 0
46 #define PERSISTENT_RECORD 1
53 // there are two lists of passwords, memory passwords and persistent passwords
55 ::std::vector
< OUString
> m_aMemPass
;
57 // persistent passwords are encrypted in one string
61 void InitArrays( bool bHasMemoryList
, const ::std::vector
< OUString
>& aMemoryList
,
62 bool bHasPersistentList
, const OUString
& aPersistentList
)
64 m_bHasMemPass
= bHasMemoryList
;
66 m_aMemPass
= aMemoryList
;
68 m_bHasPersPass
= bHasPersistentList
;
69 if ( bHasPersistentList
)
70 m_aPersPass
= aPersistentList
;
75 NamePassRecord( const OUString
& aName
)
77 , m_bHasMemPass( false )
78 , m_bHasPersPass( false )
82 NamePassRecord( const OUString
& aName
, const ::std::vector
< OUString
>& aMemoryList
)
84 , m_bHasMemPass( true )
85 , m_aMemPass( aMemoryList
)
86 , m_bHasPersPass( false )
90 NamePassRecord( const OUString
& aName
, const OUString
& aPersistentList
)
92 , m_bHasMemPass( false )
93 , m_bHasPersPass( true )
94 , m_aPersPass( aPersistentList
)
98 NamePassRecord( const OUString
& aName
,
99 bool bHasMemoryList
, const ::std::vector
< OUString
>& aMemoryList
,
100 bool bHasPersistentList
, const OUString
& aPersistentList
)
102 , m_bHasMemPass( bHasMemoryList
)
103 , m_bHasPersPass( bHasPersistentList
)
105 InitArrays( bHasMemoryList
, aMemoryList
, bHasPersistentList
, aPersistentList
);
108 NamePassRecord( const NamePassRecord
& aRecord
)
109 : m_aName( aRecord
.m_aName
)
110 , m_bHasMemPass( false )
111 , m_bHasPersPass( false )
113 InitArrays( aRecord
.m_bHasMemPass
, aRecord
.m_aMemPass
, aRecord
.m_bHasPersPass
, aRecord
.m_aPersPass
);
116 NamePassRecord
& operator=( const NamePassRecord
& aRecord
)
118 m_aName
= aRecord
.m_aName
;
122 InitArrays( aRecord
.m_bHasMemPass
, aRecord
.m_aMemPass
, aRecord
.m_bHasPersPass
, aRecord
.m_aPersPass
);
127 OUString
GetUserName() const
132 bool HasPasswords( sal_Int8 nStatus
) const
134 if ( nStatus
== MEMORY_RECORD
)
135 return m_bHasMemPass
;
136 if ( nStatus
== PERSISTENT_RECORD
)
137 return m_bHasPersPass
;
142 ::std::vector
< OUString
> GetMemPasswords() const
147 return ::std::vector
< OUString
>();
150 OUString
GetPersPasswords() const
152 if ( m_bHasPersPass
)
158 void SetMemPasswords( const ::std::vector
< OUString
>& aMemList
)
160 m_aMemPass
= aMemList
;
161 m_bHasMemPass
= true;
164 void SetPersPasswords( const OUString
& aPersList
)
166 m_aPersPass
= aPersList
;
167 m_bHasPersPass
= true;
170 void RemovePasswords( sal_Int8 nStatus
)
172 if ( nStatus
== MEMORY_RECORD
)
174 m_bHasMemPass
= false;
177 else if ( nStatus
== PERSISTENT_RECORD
)
179 m_bHasPersPass
= false;
187 typedef ::std::pair
< const OUString
, ::std::list
< NamePassRecord
> > PairUrlRecord
;
188 typedef ::std::map
< OUString
, ::std::list
< NamePassRecord
> > PassMap
;
191 class PasswordContainer
;
194 : public ::utl::ConfigItem
197 PasswordContainer
* mainCont
;
201 virtual void ImplCommit() SAL_OVERRIDE
;
204 StorageItem( PasswordContainer
* point
, const OUString
& path
) :
205 ConfigItem( path
, ConfigItemMode::ImmediateUpdate
),
209 ::com::sun::star::uno::Sequence
< OUString
> aNode( 1 );
210 *aNode
.getArray() = path
;
211 *aNode
.getArray() += "/Store";
212 EnableNotification( aNode
);
216 void update( const OUString
& url
, const NamePassRecord
& rec
);
217 void remove( const OUString
& url
, const OUString
& rec
);
220 bool getEncodedMP( OUString
& aResult
);
221 void setEncodedMP( const OUString
& aResult
, bool bAcceptEnmpty
= false );
222 void setUseStorage( bool bUse
);
225 virtual void Notify( const ::com::sun::star::uno::Sequence
< OUString
>& aPropertyNames
) SAL_OVERRIDE
;
235 class PasswordContainer
: public ::cppu::WeakImplHelper3
<
236 ::com::sun::star::task::XPasswordContainer2
,
237 ::com::sun::star::lang::XServiceInfo
,
238 ::com::sun::star::lang::XEventListener
>
241 PassMap m_aContainer
;
242 StorageItem
* m_pStorageFile
;
244 OUString m_aMasterPasswd
; // master password is set when the string is not empty
245 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
> mComponent
;
246 SysCredentialsConfig mUrlContainer
;
248 ::com::sun::star::uno::Sequence
< ::com::sun::star::task::UserRecord
> CopyToUserRecordSequence(
249 const ::std::list
< NamePassRecord
>& original
,
250 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
251 throw(::com::sun::star::uno::RuntimeException
);
253 ::com::sun::star::task::UserRecord
CopyToUserRecord(
254 const NamePassRecord
& aRecord
,
255 bool& io_bTryToDecode
,
256 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& aHandler
);
258 ::com::sun::star::uno::Sequence
< ::com::sun::star::task::UserRecord
> FindUsr(
259 const ::std::list
< NamePassRecord
>& userlist
,
260 const OUString
& name
,
261 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
262 throw(::com::sun::star::uno::RuntimeException
);
263 bool createUrlRecord(
264 const PassMap::iterator
& rIter
,
266 const OUString
& aName
,
267 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& aHandler
,
268 ::com::sun::star::task::UrlRecord
& rRec
)
269 throw( ::com::sun::star::uno::RuntimeException
);
271 ::com::sun::star::task::UrlRecord
find(
272 const OUString
& aURL
,
273 const OUString
& aName
,
274 bool bName
, // only needed to support empty user names
275 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& aHandler
) throw(::com::sun::star::uno::RuntimeException
);
277 static OUString
GetDefaultMasterPassword();
279 static OUString
RequestPasswordFromUser(
280 ::com::sun::star::task::PasswordRequestMode aRMode
,
281 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& xHandler
);
283 OUString
GetMasterPassword( const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
284 throw(::com::sun::star::uno::RuntimeException
);
286 void UpdateVector( const OUString
& url
, ::std::list
< NamePassRecord
>& toUpdate
, NamePassRecord
& rec
, bool writeFile
)
287 throw(::com::sun::star::uno::RuntimeException
);
289 void PrivateAdd( const OUString
& aUrl
,
290 const OUString
& aUserName
,
291 const ::com::sun::star::uno::Sequence
< OUString
>& aPasswords
,
293 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
294 throw(::com::sun::star::uno::RuntimeException
);
296 static ::std::vector
< OUString
> DecodePasswords( const OUString
& aLine
, const OUString
& aMasterPassword
)
297 throw(::com::sun::star::uno::RuntimeException
);
299 static OUString
EncodePasswords(const std::vector
< OUString
>& lines
, const OUString
& aMasterPassword
)
300 throw(::com::sun::star::uno::RuntimeException
);
303 PasswordContainer( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& );
304 virtual ~PasswordContainer();
306 virtual void SAL_CALL
add( const OUString
& aUrl
,
307 const OUString
& aUserName
,
308 const ::com::sun::star::uno::Sequence
< OUString
>& aPasswords
,
309 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
310 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
312 virtual void SAL_CALL
addPersistent( const OUString
& aUrl
,
313 const OUString
& aUserName
,
314 const ::com::sun::star::uno::Sequence
< OUString
>& aPasswords
,
315 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
316 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
318 virtual ::com::sun::star::task::UrlRecord SAL_CALL
319 find( const OUString
& aUrl
,
320 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
321 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
323 virtual ::com::sun::star::task::UrlRecord SAL_CALL
324 findForName( const OUString
& aUrl
,
325 const OUString
& aUserName
,
326 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
)
327 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
329 virtual void SAL_CALL
remove( const OUString
& aUrl
,
330 const OUString
& aUserName
)
331 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
333 virtual void SAL_CALL
removePersistent( const OUString
& aUrl
,
334 const OUString
& aUserName
)
335 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
337 virtual void SAL_CALL
removeAllPersistent() throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
339 virtual ::com::sun::star::uno::Sequence
< ::com::sun::star::task::UrlRecord
> SAL_CALL
340 getAllPersistent( const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& Handler
) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
344 static OUString SAL_CALL
impl_getStaticImplementationName( ) throw(::com::sun::star::uno::RuntimeException
);
345 static ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
346 impl_getStaticSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException
);
347 static ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XSingleServiceFactory
> SAL_CALL
348 impl_createFactory( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& ServiceManager
) throw(::com::sun::star::uno::RuntimeException
);
349 static ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> SAL_CALL
350 impl_createInstance( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& xServiceManager
) throw( ::com::sun::star::uno::RuntimeException
);
353 virtual OUString SAL_CALL
getImplementationName( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
354 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
356 virtual ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
357 getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
360 virtual void SAL_CALL
disposing( const ::com::sun::star::lang::EventObject
& Source
)
361 throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
363 // XMasterPasswordHandling
364 virtual sal_Bool SAL_CALL
authorizateWithMasterPassword( const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& xHandler
)
365 throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
366 virtual sal_Bool SAL_CALL
changeMasterPassword( const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& xHandler
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
367 virtual void SAL_CALL
removeMasterPassword() throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
368 virtual sal_Bool SAL_CALL
hasMasterPassword( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
369 virtual sal_Bool SAL_CALL
allowPersistentStoring( sal_Bool bAllow
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
370 virtual sal_Bool SAL_CALL
isPersistentStoringAllowed( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
372 // XMasterPasswordHandling2
373 virtual sal_Bool SAL_CALL
useDefaultMasterPassword( const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& xHandler
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
374 virtual sal_Bool SAL_CALL
isDefaultMasterPasswordUsed( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
377 virtual void SAL_CALL
addUrl( const OUString
& Url
, sal_Bool MakePersistent
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
378 virtual OUString SAL_CALL
findUrl( const OUString
& Url
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
379 virtual void SAL_CALL
removeUrl( const OUString
& Url
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
380 virtual ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
getUrls( sal_Bool OnlyPersistent
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
386 class MasterPasswordRequest_Impl
: public ucbhelper::InteractionRequest
388 ::rtl::Reference
< ucbhelper::InteractionSupplyAuthentication
> m_xAuthSupplier
;
391 MasterPasswordRequest_Impl( ::com::sun::star::task::PasswordRequestMode Mode
);
393 const ::rtl::Reference
< ucbhelper::InteractionSupplyAuthentication
> &
394 getAuthenticationSupplier() const { return m_xAuthSupplier
; }
399 class RW_SvMemoryStream
: public SvMemoryStream
{
401 RW_SvMemoryStream( void* Buf
, sal_uLong Size
, StreamMode eMode
):
402 SvMemoryStream( Buf
, Size
, eMode
){}
404 RW_SvMemoryStream( sal_uLong InitSize
=512, sal_uLong Resize
=64 ):
405 SvMemoryStream( InitSize
, Resize
){}
407 sal_uLong
getActualSize(){ return nEndOfData
; }
412 #endif // INCLUDED_SVL_SOURCE_INC_PASSWORDCONTAINER_HXX
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */