Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : : #ifndef INCLUDED_COMPHELPER_PASSWORDCONTAINER_HXX
20 : : #define INCLUDED_COMPHELPER_PASSWORDCONTAINER_HXX
21 : :
22 : : #include <list>
23 : : #include <vector>
24 : : #include <map>
25 : : #include <com/sun/star/task/XPasswordContainer.hpp>
26 : : #include <com/sun/star/task/XUrlContainer.hpp>
27 : : #include <com/sun/star/task/PasswordRequestMode.hpp>
28 : : #include <com/sun/star/lang/XServiceInfo.hpp>
29 : : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
30 : : #include <com/sun/star/lang/XEventListener.hpp>
31 : : #include <com/sun/star/lang/XComponent.hpp>
32 : : #include <com/sun/star/task/XMasterPasswordHandling2.hpp>
33 : : #include <cppuhelper/implbase5.hxx>
34 : : #include <cppuhelper/typeprovider.hxx>
35 : : #include <cppuhelper/queryinterface.hxx>
36 : : #include <cppuhelper/factory.hxx>
37 : :
38 : : #include <tools/stream.hxx>
39 : : #include <unotools/configitem.hxx>
40 : : #include <ucbhelper/interactionrequest.hxx>
41 : :
42 : : #include <rtl/ref.hxx>
43 : : #include <osl/mutex.hxx>
44 : :
45 : : #include "syscreds.hxx"
46 : :
47 : : #define MEMORY_RECORD 0
48 : : #define PERSISTENT_RECORD 1
49 : :
50 : : //----------------------------------------------------------------------------------
51 : :
52 : 186 : class NamePassRecord
53 : : {
54 : : ::rtl::OUString m_aName;
55 : :
56 : : // there are two lists of passwords, memory passwords and persistent passwords
57 : : sal_Bool m_bHasMemPass;
58 : : ::std::vector< ::rtl::OUString > m_aMemPass;
59 : :
60 : : // persistent passwords are encrypted in one string
61 : : sal_Bool m_bHasPersPass;
62 : : ::rtl::OUString m_aPersPass;
63 : :
64 : 96 : void InitArrays( sal_Bool bHasMemoryList, const ::std::vector< ::rtl::OUString >& aMemoryList,
65 : : sal_Bool bHasPersistentList, const ::rtl::OUString& aPersistentList )
66 : : {
67 : 96 : m_bHasMemPass = bHasMemoryList;
68 [ + + ]: 96 : if ( bHasMemoryList )
69 : 42 : m_aMemPass = aMemoryList;
70 : :
71 : 96 : m_bHasPersPass = bHasPersistentList;
72 [ + + ]: 96 : if ( bHasPersistentList )
73 : 54 : m_aPersPass = aPersistentList;
74 : 96 : }
75 : :
76 : : public:
77 : :
78 : 90 : NamePassRecord( const ::rtl::OUString& aName )
79 : : : m_aName( aName )
80 : : , m_bHasMemPass( sal_False )
81 [ + - ]: 90 : , m_bHasPersPass( sal_False )
82 : : {
83 : 90 : }
84 : :
85 : : NamePassRecord( const ::rtl::OUString& aName, const ::std::vector< ::rtl::OUString >& aMemoryList )
86 : : : m_aName( aName )
87 : : , m_bHasMemPass( sal_True )
88 : : , m_aMemPass( aMemoryList )
89 : : , m_bHasPersPass( sal_False )
90 : : {
91 : : }
92 : :
93 : 0 : NamePassRecord( const ::rtl::OUString& aName, const ::rtl::OUString& aPersistentList )
94 : : : m_aName( aName )
95 : : , m_bHasMemPass( sal_False )
96 : : , m_bHasPersPass( sal_True )
97 [ # # ]: 0 : , m_aPersPass( aPersistentList )
98 : : {
99 : 0 : }
100 : :
101 : : NamePassRecord( const ::rtl::OUString& aName,
102 : : sal_Bool bHasMemoryList, const ::std::vector< ::rtl::OUString >& aMemoryList,
103 : : sal_Bool bHasPersistentList, const ::rtl::OUString aPersistentList )
104 : : : m_aName( aName )
105 : : , m_bHasMemPass( bHasMemoryList )
106 : : , m_bHasPersPass( bHasPersistentList )
107 : : {
108 : : InitArrays( bHasMemoryList, aMemoryList, bHasPersistentList, aPersistentList );
109 : : }
110 : :
111 : 96 : NamePassRecord( const NamePassRecord& aRecord )
112 : : : m_aName( aRecord.m_aName )
113 : : , m_bHasMemPass( sal_False )
114 [ + - ]: 96 : , m_bHasPersPass( sal_False )
115 : : {
116 [ + - ]: 96 : InitArrays( aRecord.m_bHasMemPass, aRecord.m_aMemPass, aRecord.m_bHasPersPass, aRecord.m_aPersPass );
117 : 96 : }
118 : :
119 : : NamePassRecord& operator=( const NamePassRecord& aRecord )
120 : : {
121 : : m_aName = aRecord.m_aName;
122 : :
123 : : m_aMemPass.clear();
124 : : m_aPersPass = ::rtl::OUString();
125 : : InitArrays( aRecord.m_bHasMemPass, aRecord.m_aMemPass, aRecord.m_bHasPersPass, aRecord.m_aPersPass );
126 : :
127 : : return *this;
128 : : }
129 : :
130 : 1860 : ::rtl::OUString GetUserName() const
131 : : {
132 : 1860 : return m_aName;
133 : : }
134 : :
135 : 424 : sal_Bool HasPasswords( sal_Int8 nStatus ) const
136 : : {
137 [ + + ]: 424 : if ( nStatus == MEMORY_RECORD )
138 : 110 : return m_bHasMemPass;
139 [ + - ]: 314 : if ( nStatus == PERSISTENT_RECORD )
140 : 314 : return m_bHasPersPass;
141 : :
142 : 424 : return sal_False;
143 : : }
144 : :
145 : 40 : ::std::vector< ::rtl::OUString > GetMemPasswords() const
146 : : {
147 [ + - ]: 40 : if ( m_bHasMemPass )
148 : 40 : return m_aMemPass;
149 : :
150 : 40 : return ::std::vector< ::rtl::OUString >();
151 : : }
152 : :
153 : 90 : ::rtl::OUString GetPersPasswords() const
154 : : {
155 [ + - ]: 90 : if ( m_bHasPersPass )
156 : 90 : return m_aPersPass;
157 : :
158 : 90 : return ::rtl::OUString();
159 : : }
160 : :
161 : 40 : void SetMemPasswords( const ::std::vector< ::rtl::OUString >& aMemList )
162 : : {
163 : 40 : m_aMemPass = aMemList;
164 : 40 : m_bHasMemPass = sal_True;
165 : 40 : }
166 : :
167 : 50 : void SetPersPasswords( const ::rtl::OUString& aPersList )
168 : : {
169 : 50 : m_aPersPass = aPersList;
170 : 50 : m_bHasPersPass = sal_True;
171 : 50 : }
172 : :
173 : 30 : void RemovePasswords( sal_Int8 nStatus )
174 : : {
175 [ - + ]: 30 : if ( nStatus == MEMORY_RECORD )
176 : : {
177 : 0 : m_bHasMemPass = sal_False;
178 : 0 : m_aMemPass.clear();
179 : : }
180 [ + - ]: 30 : else if ( nStatus == PERSISTENT_RECORD )
181 : : {
182 : 30 : m_bHasPersPass = sal_False;
183 : 30 : m_aPersPass = ::rtl::OUString();
184 : : }
185 : 30 : }
186 : :
187 : : };
188 : :
189 : : //----------------------------------------------------------------------------------
190 : :
191 : : typedef ::std::pair< const ::rtl::OUString, ::std::list< NamePassRecord > > PairUrlRecord;
192 : : typedef ::std::map< ::rtl::OUString, ::std::list< NamePassRecord > > PassMap;
193 : :
194 : : //----------------------------------------------------------------------------------
195 : :
196 : : class PasswordContainer;
197 : :
198 [ - + ]: 4 : class StorageItem : public ::utl::ConfigItem {
199 : : PasswordContainer* mainCont;
200 : : sal_Bool hasEncoded;
201 : : ::rtl::OUString mEncoded;
202 : : public:
203 : 2 : StorageItem( PasswordContainer* point, const ::rtl::OUString& path ) :
204 : : ConfigItem( path, CONFIG_MODE_IMMEDIATE_UPDATE ),
205 : : mainCont( point ),
206 : 2 : hasEncoded( sal_False )
207 : : {
208 [ + - ]: 2 : ::com::sun::star::uno::Sequence< ::rtl::OUString > aNode( 1 );
209 [ + - ]: 2 : *aNode.getArray() = path;
210 [ + - ]: 2 : *aNode.getArray() += "/Store";
211 [ + - ][ + - ]: 2 : EnableNotification( aNode );
212 : 2 : }
213 : :
214 : : PassMap getInfo();
215 : : void update( const ::rtl::OUString& url, const NamePassRecord& rec );
216 : : void remove( const ::rtl::OUString& url, const ::rtl::OUString& rec );
217 : : void clear();
218 : :
219 : : sal_Bool getEncodedMP( ::rtl::OUString& aResult );
220 : : void setEncodedMP( const ::rtl::OUString& aResult, sal_Bool bAcceptEnmpty = sal_False );
221 : : void setUseStorage( sal_Bool bUse );
222 : : sal_Bool useStorage();
223 : :
224 : : virtual void Notify( const ::com::sun::star::uno::Sequence< ::rtl::OUString >& aPropertyNames );
225 : : virtual void Commit();
226 : : };
227 : :
228 : : //----------------------------------------------------------------------------------
229 : :
230 : : enum PasswordState {
231 : : no_password,
232 : : entered,
233 : : cancelled
234 : : };
235 : :
236 : : class PasswordContainer : public ::cppu::WeakImplHelper5<
237 : : ::com::sun::star::task::XPasswordContainer,
238 : : ::com::sun::star::task::XMasterPasswordHandling2,
239 : : ::com::sun::star::task::XUrlContainer,
240 : : ::com::sun::star::lang::XServiceInfo,
241 : : ::com::sun::star::lang::XEventListener >
242 : : {
243 : : private:
244 : : PassMap m_aContainer;
245 : : StorageItem* m_pStorageFile;
246 : : ::osl::Mutex mMutex;
247 : : ::rtl::OUString m_aMasterPasswd; // master password is set when the string is not empty
248 : : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > mComponent;
249 : : SysCredentialsConfig mUrlContainer;
250 : :
251 : : ::com::sun::star::uno::Sequence< ::com::sun::star::task::UserRecord > CopyToUserRecordSequence(
252 : : const ::std::list< NamePassRecord >& original,
253 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler )
254 : : throw(::com::sun::star::uno::RuntimeException);
255 : :
256 : : ::com::sun::star::task::UserRecord CopyToUserRecord(
257 : : const NamePassRecord& aRecord,
258 : : sal_Bool& io_bTryToDecode,
259 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& aHandler );
260 : :
261 : : ::com::sun::star::uno::Sequence< ::com::sun::star::task::UserRecord > FindUsr(
262 : : const ::std::list< NamePassRecord >& userlist,
263 : : const ::rtl::OUString& name,
264 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler )
265 : : throw(::com::sun::star::uno::RuntimeException);
266 : : bool createUrlRecord(
267 : : const PassMap::iterator & rIter,
268 : : bool bName,
269 : : const ::rtl::OUString & aName,
270 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& aHandler,
271 : : ::com::sun::star::task::UrlRecord & rRec )
272 : : throw( ::com::sun::star::uno::RuntimeException );
273 : :
274 : : ::com::sun::star::task::UrlRecord find(
275 : : const ::rtl::OUString& aURL,
276 : : const ::rtl::OUString& aName,
277 : : bool bName, // only needed to support empty user names
278 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& aHandler ) throw(::com::sun::star::uno::RuntimeException);
279 : :
280 : : ::rtl::OUString GetDefaultMasterPassword();
281 : :
282 : : ::rtl::OUString RequestPasswordFromUser(
283 : : ::com::sun::star::task::PasswordRequestMode aRMode,
284 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& xHandler );
285 : :
286 : : ::rtl::OUString GetMasterPassword( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler )
287 : : throw(::com::sun::star::uno::RuntimeException);
288 : :
289 : : void UpdateVector( const ::rtl::OUString& url, ::std::list< NamePassRecord >& toUpdate, NamePassRecord& rec, sal_Bool writeFile )
290 : : throw(::com::sun::star::uno::RuntimeException);
291 : :
292 : : void PrivateAdd( const ::rtl::OUString& aUrl,
293 : : const ::rtl::OUString& aUserName,
294 : : const ::com::sun::star::uno::Sequence< ::rtl::OUString >& aPasswords,
295 : : char aMode,
296 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler )
297 : : throw(::com::sun::star::uno::RuntimeException);
298 : :
299 : : ::std::vector< ::rtl::OUString > DecodePasswords( const ::rtl::OUString& aLine, const ::rtl::OUString& aMasterPassword )
300 : : throw(::com::sun::star::uno::RuntimeException);
301 : :
302 : : ::rtl::OUString EncodePasswords( ::std::vector< ::rtl::OUString > lines, const ::rtl::OUString& aMasterPassword )
303 : : throw(::com::sun::star::uno::RuntimeException);
304 : :
305 : : public:
306 : : PasswordContainer( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& );
307 : : ~PasswordContainer();
308 : :
309 : : virtual void SAL_CALL add( const ::rtl::OUString& aUrl,
310 : : const ::rtl::OUString& aUserName,
311 : : const ::com::sun::star::uno::Sequence< ::rtl::OUString >& aPasswords,
312 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler )
313 : : throw(::com::sun::star::uno::RuntimeException);
314 : :
315 : : virtual void SAL_CALL addPersistent( const ::rtl::OUString& aUrl,
316 : : const ::rtl::OUString& aUserName,
317 : : const ::com::sun::star::uno::Sequence< ::rtl::OUString >& aPasswords,
318 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler )
319 : : throw(::com::sun::star::uno::RuntimeException);
320 : :
321 : : virtual ::com::sun::star::task::UrlRecord SAL_CALL
322 : : find( const ::rtl::OUString& aUrl,
323 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler )
324 : : throw(::com::sun::star::uno::RuntimeException);
325 : :
326 : : virtual ::com::sun::star::task::UrlRecord SAL_CALL
327 : : findForName( const ::rtl::OUString& aUrl,
328 : : const ::rtl::OUString& aUserName,
329 : : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler )
330 : : throw(::com::sun::star::uno::RuntimeException);
331 : :
332 : : virtual void SAL_CALL remove( const ::rtl::OUString& aUrl,
333 : : const ::rtl::OUString& aUserName )
334 : : throw(::com::sun::star::uno::RuntimeException);
335 : :
336 : : virtual void SAL_CALL removePersistent( const ::rtl::OUString& aUrl,
337 : : const ::rtl::OUString& aUserName )
338 : : throw(::com::sun::star::uno::RuntimeException);
339 : :
340 : : virtual void SAL_CALL removeAllPersistent() throw(::com::sun::star::uno::RuntimeException);
341 : :
342 : : virtual ::com::sun::star::uno::Sequence< ::com::sun::star::task::UrlRecord > SAL_CALL
343 : : getAllPersistent( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler ) throw(::com::sun::star::uno::RuntimeException);
344 : :
345 : :
346 : : // provide factory
347 : : static ::rtl::OUString SAL_CALL impl_getStaticImplementationName( ) throw(::com::sun::star::uno::RuntimeException);
348 : : static ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
349 : : impl_getStaticSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException);
350 : : static ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory > SAL_CALL
351 : : impl_createFactory( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& ServiceManager ) throw(::com::sun::star::uno::RuntimeException);
352 : : static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL
353 : : impl_createInstance( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) throw( ::com::sun::star::uno::RuntimeException );
354 : :
355 : : // XServiceInfo
356 : : virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw(::com::sun::star::uno::RuntimeException);
357 : : virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
358 : :
359 : : virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL
360 : : getSupportedServiceNames( ) throw(::com::sun::star::uno::RuntimeException);
361 : :
362 : : // XEventListener
363 : : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source )
364 : : throw(::com::sun::star::uno::RuntimeException);
365 : :
366 : : // XMasterPasswordHandling
367 : : virtual ::sal_Bool SAL_CALL authorizateWithMasterPassword( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& xHandler )
368 : : throw (::com::sun::star::uno::RuntimeException);
369 : : virtual ::sal_Bool SAL_CALL changeMasterPassword( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& xHandler ) throw (::com::sun::star::uno::RuntimeException);
370 : : virtual void SAL_CALL removeMasterPassword() throw (::com::sun::star::uno::RuntimeException);
371 : : virtual ::sal_Bool SAL_CALL hasMasterPassword( ) throw (::com::sun::star::uno::RuntimeException);
372 : : virtual ::sal_Bool SAL_CALL allowPersistentStoring( ::sal_Bool bAllow ) throw (::com::sun::star::uno::RuntimeException);
373 : : virtual ::sal_Bool SAL_CALL isPersistentStoringAllowed( ) throw (::com::sun::star::uno::RuntimeException);
374 : :
375 : : // XMasterPasswordHandling2
376 : : virtual ::sal_Bool SAL_CALL useDefaultMasterPassword( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& xHandler ) throw (::com::sun::star::uno::RuntimeException);
377 : : virtual ::sal_Bool SAL_CALL isDefaultMasterPasswordUsed( ) throw (::com::sun::star::uno::RuntimeException);
378 : :
379 : : // XUrlContainer
380 : : virtual void SAL_CALL addUrl( const ::rtl::OUString& Url, ::sal_Bool MakePersistent ) throw (::com::sun::star::uno::RuntimeException);
381 : : virtual ::rtl::OUString SAL_CALL findUrl( const ::rtl::OUString& Url ) throw (::com::sun::star::uno::RuntimeException);
382 : : virtual void SAL_CALL removeUrl( const ::rtl::OUString& Url ) throw (::com::sun::star::uno::RuntimeException);
383 : : virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getUrls( ::sal_Bool OnlyPersistent ) throw (::com::sun::star::uno::RuntimeException);
384 : :
385 : : void Notify();
386 : : };
387 : :
388 : : //----------------------------------------------------------------------------------
389 : :
390 [ - + ]: 8 : class MasterPasswordRequest_Impl : public ucbhelper::InteractionRequest
391 : : {
392 : : ::rtl::Reference< ucbhelper::InteractionSupplyAuthentication > m_xAuthSupplier;
393 : :
394 : : public:
395 : : MasterPasswordRequest_Impl( ::com::sun::star::task::PasswordRequestMode Mode );
396 : :
397 : : const ::rtl::Reference< ucbhelper::InteractionSupplyAuthentication > &
398 : 4 : getAuthenticationSupplier() const { return m_xAuthSupplier; }
399 : :
400 : : };
401 : :
402 : : //----------------------------------------------------------------------------------
403 : :
404 [ # # ]: 0 : class RW_SvMemoryStream : public SvMemoryStream {
405 : : public:
406 : : RW_SvMemoryStream( void* Buf, sal_uLong Size, StreamMode eMode ):
407 : : SvMemoryStream( Buf, Size, eMode){}
408 : :
409 : : RW_SvMemoryStream( sal_uLong InitSize=512, sal_uLong Resize=64 ):
410 : : SvMemoryStream( InitSize, Resize ){}
411 : :
412 : : sal_uLong getActualSize(){ return nEndOfData; }
413 : : };
414 : :
415 : :
416 : :
417 : : #endif // #ifndef INCLUDED_COMPHELPER_PASSWORDCONTAINER_HXX
418 : :
419 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|