LCOV - code coverage report
Current view: top level - svl/source/inc - passwordcontainer.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 53 61 86.9 %
Date: 2015-06-13 12:38:46 Functions: 17 18 94.4 %
Legend: Lines: hit not hit

          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          93 : 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          48 :     void InitArrays( bool bHasMemoryList, const ::std::vector< OUString >& aMemoryList,
      62             :                      bool bHasPersistentList, const OUString& aPersistentList )
      63             :     {
      64          48 :         m_bHasMemPass = bHasMemoryList;
      65          48 :         if ( bHasMemoryList )
      66          21 :             m_aMemPass = aMemoryList;
      67             : 
      68          48 :         m_bHasPersPass = bHasPersistentList;
      69          48 :         if ( bHasPersistentList )
      70          27 :             m_aPersPass = aPersistentList;
      71          48 :     }
      72             : 
      73             : public:
      74             : 
      75          45 :     NamePassRecord( const OUString& aName )
      76             :         : m_aName( aName )
      77             :         , m_bHasMemPass( false )
      78          45 :         , m_bHasPersPass( false )
      79             :     {
      80          45 :     }
      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          48 :     NamePassRecord( const NamePassRecord& aRecord )
     109             :         : m_aName( aRecord.m_aName )
     110             :         , m_bHasMemPass( false )
     111          48 :         , m_bHasPersPass( false )
     112             :     {
     113          48 :         InitArrays( aRecord.m_bHasMemPass, aRecord.m_aMemPass, aRecord.m_bHasPersPass, aRecord.m_aPersPass );
     114          48 :     }
     115             : 
     116             :     NamePassRecord& operator=( const NamePassRecord& aRecord )
     117             :     {
     118             :         m_aName = aRecord.m_aName;
     119             : 
     120             :         m_aMemPass.clear();
     121             :         m_aPersPass.clear();
     122             :         InitArrays( aRecord.m_bHasMemPass, aRecord.m_aMemPass, aRecord.m_bHasPersPass, aRecord.m_aPersPass );
     123             : 
     124             :         return *this;
     125             :     }
     126             : 
     127         930 :     OUString GetUserName() const
     128             :     {
     129         930 :         return m_aName;
     130             :     }
     131             : 
     132         212 :     bool HasPasswords( sal_Int8 nStatus ) const
     133             :     {
     134         212 :         if ( nStatus == MEMORY_RECORD )
     135          55 :             return m_bHasMemPass;
     136         157 :         if ( nStatus == PERSISTENT_RECORD )
     137         157 :             return m_bHasPersPass;
     138             : 
     139           0 :         return false;
     140             :     }
     141             : 
     142          20 :     ::std::vector< OUString > GetMemPasswords() const
     143             :     {
     144          20 :         if ( m_bHasMemPass )
     145          20 :             return m_aMemPass;
     146             : 
     147           0 :         return ::std::vector< OUString >();
     148             :     }
     149             : 
     150          45 :     OUString GetPersPasswords() const
     151             :     {
     152          45 :         if ( m_bHasPersPass )
     153          45 :             return m_aPersPass;
     154             : 
     155           0 :         return OUString();
     156             :     }
     157             : 
     158          20 :     void SetMemPasswords( const ::std::vector< OUString >& aMemList )
     159             :     {
     160          20 :         m_aMemPass = aMemList;
     161          20 :         m_bHasMemPass = true;
     162          20 :     }
     163             : 
     164          25 :     void SetPersPasswords( const OUString& aPersList )
     165             :     {
     166          25 :         m_aPersPass = aPersList;
     167          25 :         m_bHasPersPass = true;
     168          25 :     }
     169             : 
     170          15 :     void RemovePasswords( sal_Int8 nStatus )
     171             :     {
     172          15 :         if ( nStatus == MEMORY_RECORD )
     173             :         {
     174           0 :             m_bHasMemPass = false;
     175           0 :             m_aMemPass.clear();
     176             :         }
     177          15 :         else if ( nStatus == PERSISTENT_RECORD )
     178             :         {
     179          15 :             m_bHasPersPass = false;
     180          15 :             m_aPersPass.clear();
     181             :         }
     182          15 :     }
     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           4 : class StorageItem
     194             :     : public ::utl::ConfigItem
     195             : {
     196             : private:
     197             :     PasswordContainer*     mainCont;
     198             :     bool                   hasEncoded;
     199             :     OUString        mEncoded;
     200             : 
     201             :     virtual void            ImplCommit() SAL_OVERRIDE;
     202             : 
     203             : public:
     204           2 :     StorageItem( PasswordContainer* point, const OUString& path ) :
     205             :         ConfigItem( path, ConfigItemMode::ImmediateUpdate ),
     206             :         mainCont( point ),
     207           2 :         hasEncoded( false )
     208             :     {
     209           2 :         ::com::sun::star::uno::Sequence< OUString > aNode( 1 );
     210           2 :         *aNode.getArray()  = path;
     211           2 :         *aNode.getArray() += "/Store";
     212           2 :         EnableNotification( aNode );
     213           2 :     }
     214             : 
     215             :     PassMap getInfo();
     216             :     void update( const OUString& url, const NamePassRecord& rec );
     217             :     void remove( const OUString& url, const OUString& rec );
     218             :     void clear();
     219             : 
     220             :     bool getEncodedMP( OUString& aResult );
     221             :     void setEncodedMP( const OUString& aResult, bool bAcceptEnmpty = false );
     222             :     void setUseStorage( bool bUse );
     223             :     bool useStorage();
     224             : 
     225             :     virtual void            Notify( const ::com::sun::star::uno::Sequence< OUString >& aPropertyNames ) SAL_OVERRIDE;
     226             : };
     227             : 
     228             : 
     229             : enum PasswordState {
     230             :     no_password,
     231             :     entered,
     232             :     cancelled
     233             : };
     234             : 
     235             : class PasswordContainer : public ::cppu::WeakImplHelper3<
     236             :         ::com::sun::star::task::XPasswordContainer2,
     237             :         ::com::sun::star::lang::XServiceInfo,
     238             :         ::com::sun::star::lang::XEventListener >
     239             : {
     240             : private:
     241             :     PassMap      m_aContainer;
     242             :     StorageItem* m_pStorageFile;
     243             :     ::osl::Mutex mMutex;
     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;
     247             : 
     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);
     252             : 
     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 );
     257             : 
     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,
     265             :     bool bName,
     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 );
     270             : 
     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);
     276             : 
     277             :     static OUString GetDefaultMasterPassword();
     278             : 
     279             :     static OUString RequestPasswordFromUser(
     280             :                     ::com::sun::star::task::PasswordRequestMode aRMode,
     281             :                     const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& xHandler );
     282             : 
     283             :     OUString GetMasterPassword( const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler )
     284             :                                                         throw(::com::sun::star::uno::RuntimeException);
     285             : 
     286             :     void UpdateVector( const OUString& url, ::std::list< NamePassRecord >& toUpdate, NamePassRecord& rec, bool writeFile )
     287             :                                                         throw(::com::sun::star::uno::RuntimeException);
     288             : 
     289             :     void PrivateAdd( const OUString& aUrl,
     290             :                               const OUString& aUserName,
     291             :                               const ::com::sun::star::uno::Sequence< OUString >& aPasswords,
     292             :                               char  aMode,
     293             :                               const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& Handler )
     294             :                                                         throw(::com::sun::star::uno::RuntimeException);
     295             : 
     296             :     static ::std::vector< OUString > DecodePasswords( const OUString& aLine, const OUString& aMasterPassword )
     297             :                                                         throw(::com::sun::star::uno::RuntimeException);
     298             : 
     299             :     static OUString EncodePasswords(const std::vector< OUString >& lines, const OUString& aMasterPassword )
     300             :                                                         throw(::com::sun::star::uno::RuntimeException);
     301             : 
     302             : public:
     303             :     PasswordContainer( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& );
     304             :     virtual ~PasswordContainer();
     305             : 
     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;
     311             : 
     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;
     317             : 
     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;
     322             : 
     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;
     328             : 
     329             :     virtual void SAL_CALL remove( const OUString& aUrl,
     330             :                                   const OUString& aUserName )
     331             :                                                         throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     332             : 
     333             :     virtual void SAL_CALL removePersistent( const OUString& aUrl,
     334             :                                             const OUString& aUserName )
     335             :                                                         throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     336             : 
     337             :     virtual void SAL_CALL removeAllPersistent() throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     338             : 
     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;
     341             : 
     342             : 
     343             :     // provide factory
     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 );
     351             : 
     352             :     // XServiceInfo
     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;
     355             : 
     356             :     virtual ::com::sun::star::uno::Sequence< OUString > SAL_CALL
     357             :                                         getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     358             : 
     359             :     // XEventListener
     360             :     virtual void SAL_CALL        disposing( const ::com::sun::star::lang::EventObject& Source )
     361             :                                     throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     362             : 
     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;
     371             : 
     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;
     375             : 
     376             :     // XUrlContainer
     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;
     381             : 
     382             :     void            Notify();
     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             : class RW_SvMemoryStream : public SvMemoryStream {
     400             : public:
     401             :     RW_SvMemoryStream( void* Buf, sal_uLong Size, StreamMode eMode ):
     402             :             SvMemoryStream( Buf, Size, eMode){}
     403             : 
     404             :     RW_SvMemoryStream( sal_uLong InitSize=512, sal_uLong Resize=64 ):
     405             :             SvMemoryStream( InitSize, Resize ){}
     406             : 
     407             :     sal_uLong getActualSize(){ return nEndOfData; }
     408             : };
     409             : 
     410             : 
     411             : 
     412             : #endif // INCLUDED_SVL_SOURCE_INC_PASSWORDCONTAINER_HXX
     413             : 
     414             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11