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