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