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 0 : class NamePassRecord
51 : {
52 : ::rtl::OUString m_aName;
53 :
54 : // there are two lists of passwords, memory passwords and persistent passwords
55 : bool m_bHasMemPass;
56 : ::std::vector< ::rtl::OUString > m_aMemPass;
57 :
58 : // persistent passwords are encrypted in one string
59 : bool m_bHasPersPass;
60 : ::rtl::OUString m_aPersPass;
61 :
62 0 : void InitArrays( bool bHasMemoryList, const ::std::vector< ::rtl::OUString >& aMemoryList,
63 : bool bHasPersistentList, const ::rtl::OUString& aPersistentList )
64 : {
65 0 : m_bHasMemPass = bHasMemoryList;
66 0 : if ( bHasMemoryList )
67 0 : m_aMemPass = aMemoryList;
68 :
69 0 : m_bHasPersPass = bHasPersistentList;
70 0 : if ( bHasPersistentList )
71 0 : m_aPersPass = aPersistentList;
72 0 : }
73 :
74 : public:
75 :
76 0 : NamePassRecord( const ::rtl::OUString& aName )
77 : : m_aName( aName )
78 : , m_bHasMemPass( false )
79 0 : , m_bHasPersPass( false )
80 : {
81 0 : }
82 :
83 : NamePassRecord( const ::rtl::OUString& aName, const ::std::vector< ::rtl::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 ::rtl::OUString& aName, const ::rtl::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 ::rtl::OUString& aName,
100 : bool bHasMemoryList, const ::std::vector< ::rtl::OUString >& aMemoryList,
101 : bool bHasPersistentList, const ::rtl::OUString aPersistentList )
102 : : m_aName( aName )
103 : , m_bHasMemPass( bHasMemoryList )
104 : , m_bHasPersPass( bHasPersistentList )
105 : {
106 : InitArrays( bHasMemoryList, aMemoryList, bHasPersistentList, aPersistentList );
107 : }
108 :
109 0 : NamePassRecord( const NamePassRecord& aRecord )
110 : : m_aName( aRecord.m_aName )
111 : , m_bHasMemPass( false )
112 0 : , m_bHasPersPass( false )
113 : {
114 0 : InitArrays( aRecord.m_bHasMemPass, aRecord.m_aMemPass, aRecord.m_bHasPersPass, aRecord.m_aPersPass );
115 0 : }
116 :
117 : NamePassRecord& operator=( const NamePassRecord& aRecord )
118 : {
119 : m_aName = aRecord.m_aName;
120 :
121 : m_aMemPass.clear();
122 : m_aPersPass = ::rtl::OUString();
123 : InitArrays( aRecord.m_bHasMemPass, aRecord.m_aMemPass, aRecord.m_bHasPersPass, aRecord.m_aPersPass );
124 :
125 : return *this;
126 : }
127 :
128 0 : ::rtl::OUString GetUserName() const
129 : {
130 0 : return m_aName;
131 : }
132 :
133 0 : bool HasPasswords( sal_Int8 nStatus ) const
134 : {
135 0 : if ( nStatus == MEMORY_RECORD )
136 0 : return m_bHasMemPass;
137 0 : if ( nStatus == PERSISTENT_RECORD )
138 0 : return m_bHasPersPass;
139 :
140 0 : return sal_False;
141 : }
142 :
143 0 : ::std::vector< ::rtl::OUString > GetMemPasswords() const
144 : {
145 0 : if ( m_bHasMemPass )
146 0 : return m_aMemPass;
147 :
148 0 : return ::std::vector< ::rtl::OUString >();
149 : }
150 :
151 0 : ::rtl::OUString GetPersPasswords() const
152 : {
153 0 : if ( m_bHasPersPass )
154 0 : return m_aPersPass;
155 :
156 0 : return ::rtl::OUString();
157 : }
158 :
159 0 : void SetMemPasswords( const ::std::vector< ::rtl::OUString >& aMemList )
160 : {
161 0 : m_aMemPass = aMemList;
162 0 : m_bHasMemPass = true;
163 0 : }
164 :
165 0 : void SetPersPasswords( const ::rtl::OUString& aPersList )
166 : {
167 0 : m_aPersPass = aPersList;
168 0 : m_bHasPersPass = true;
169 0 : }
170 :
171 0 : void RemovePasswords( sal_Int8 nStatus )
172 : {
173 0 : if ( nStatus == MEMORY_RECORD )
174 : {
175 0 : m_bHasMemPass = false;
176 0 : m_aMemPass.clear();
177 : }
178 0 : else if ( nStatus == PERSISTENT_RECORD )
179 : {
180 0 : m_bHasPersPass = false;
181 0 : m_aPersPass = ::rtl::OUString();
182 : }
183 0 : }
184 :
185 : };
186 :
187 : //----------------------------------------------------------------------------------
188 :
189 : typedef ::std::pair< const ::rtl::OUString, ::std::list< NamePassRecord > > PairUrlRecord;
190 : typedef ::std::map< ::rtl::OUString, ::std::list< NamePassRecord > > PassMap;
191 :
192 : //----------------------------------------------------------------------------------
193 :
194 : class PasswordContainer;
195 :
196 0 : class StorageItem : public ::utl::ConfigItem {
197 : PasswordContainer* mainCont;
198 : bool hasEncoded;
199 : ::rtl::OUString mEncoded;
200 : public:
201 0 : StorageItem( PasswordContainer* point, const ::rtl::OUString& path ) :
202 : ConfigItem( path, CONFIG_MODE_IMMEDIATE_UPDATE ),
203 : mainCont( point ),
204 0 : hasEncoded( false )
205 : {
206 0 : ::com::sun::star::uno::Sequence< ::rtl::OUString > aNode( 1 );
207 0 : *aNode.getArray() = path;
208 0 : *aNode.getArray() += "/Store";
209 0 : EnableNotification( aNode );
210 0 : }
211 :
212 : PassMap getInfo();
213 : void update( const ::rtl::OUString& url, const NamePassRecord& rec );
214 : void remove( const ::rtl::OUString& url, const ::rtl::OUString& rec );
215 : void clear();
216 :
217 : bool getEncodedMP( ::rtl::OUString& aResult );
218 : void setEncodedMP( const ::rtl::OUString& aResult, bool bAcceptEnmpty = false );
219 : void setUseStorage( bool bUse );
220 : bool useStorage();
221 :
222 : virtual void Notify( const ::com::sun::star::uno::Sequence< ::rtl::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 : ::rtl::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 ::rtl::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 ::rtl::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 ::rtl::OUString& aURL,
272 : const ::rtl::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 : ::rtl::OUString GetDefaultMasterPassword();
277 :
278 : ::rtl::OUString RequestPasswordFromUser(
279 : ::com::sun::star::task::PasswordRequestMode aRMode,
280 : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& xHandler );
281 :
282 : ::rtl::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 ::rtl::OUString& url, ::std::list< NamePassRecord >& toUpdate, NamePassRecord& rec, bool writeFile )
286 : throw(::com::sun::star::uno::RuntimeException);
287 :
288 : void PrivateAdd( const ::rtl::OUString& aUrl,
289 : const ::rtl::OUString& aUserName,
290 : const ::com::sun::star::uno::Sequence< ::rtl::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< ::rtl::OUString > DecodePasswords( const ::rtl::OUString& aLine, const ::rtl::OUString& aMasterPassword )
296 : throw(::com::sun::star::uno::RuntimeException);
297 :
298 : ::rtl::OUString EncodePasswords( ::std::vector< ::rtl::OUString > lines, const ::rtl::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 ::rtl::OUString& aUrl,
306 : const ::rtl::OUString& aUserName,
307 : const ::com::sun::star::uno::Sequence< ::rtl::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 ::rtl::OUString& aUrl,
312 : const ::rtl::OUString& aUserName,
313 : const ::com::sun::star::uno::Sequence< ::rtl::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 ::rtl::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 ::rtl::OUString& aUrl,
324 : const ::rtl::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 ::rtl::OUString& aUrl,
329 : const ::rtl::OUString& aUserName )
330 : throw(::com::sun::star::uno::RuntimeException);
331 :
332 : virtual void SAL_CALL removePersistent( const ::rtl::OUString& aUrl,
333 : const ::rtl::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 ::rtl::OUString SAL_CALL impl_getStaticImplementationName( ) throw(::com::sun::star::uno::RuntimeException);
344 : static ::com::sun::star::uno::Sequence< ::rtl::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 ::rtl::OUString SAL_CALL getImplementationName( ) throw(::com::sun::star::uno::RuntimeException);
353 : virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
354 :
355 : virtual ::com::sun::star::uno::Sequence< ::rtl::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 ::rtl::OUString& Url, ::sal_Bool MakePersistent ) throw (::com::sun::star::uno::RuntimeException);
377 : virtual ::rtl::OUString SAL_CALL findUrl( const ::rtl::OUString& Url ) throw (::com::sun::star::uno::RuntimeException);
378 : virtual void SAL_CALL removeUrl( const ::rtl::OUString& Url ) throw (::com::sun::star::uno::RuntimeException);
379 : virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getUrls( ::sal_Bool OnlyPersistent ) throw (::com::sun::star::uno::RuntimeException);
380 :
381 : void Notify();
382 : };
383 :
384 : //----------------------------------------------------------------------------------
385 :
386 0 : 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 0 : getAuthenticationSupplier() const { return m_xAuthSupplier; }
395 :
396 : };
397 :
398 : //----------------------------------------------------------------------------------
399 :
400 0 : 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: */
|