1*40df464eSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*40df464eSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*40df464eSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*40df464eSAndrew Rist * distributed with this work for additional information 6*40df464eSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*40df464eSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*40df464eSAndrew Rist * "License"); you may not use this file except in compliance 9*40df464eSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*40df464eSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*40df464eSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*40df464eSAndrew Rist * software distributed under the License is distributed on an 15*40df464eSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*40df464eSAndrew Rist * KIND, either express or implied. See the License for the 17*40df464eSAndrew Rist * specific language governing permissions and limitations 18*40df464eSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*40df464eSAndrew Rist *************************************************************/ 21*40df464eSAndrew Rist 22*40df464eSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "syscreds.hxx" 25cdf0e10cSrcweir #include "com/sun/star/beans/PropertyValue.hpp" 26cdf0e10cSrcweir 27cdf0e10cSrcweir using namespace com::sun::star; 28cdf0e10cSrcweir 29cdf0e10cSrcweir SysCredentialsConfigItem::SysCredentialsConfigItem( 30cdf0e10cSrcweir SysCredentialsConfig * pOwner ) 31cdf0e10cSrcweir : utl::ConfigItem( rtl::OUString::createFromAscii( "Office.Common/Passwords" ), 32cdf0e10cSrcweir CONFIG_MODE_IMMEDIATE_UPDATE ), 33cdf0e10cSrcweir m_bInited( false ), 34cdf0e10cSrcweir m_pOwner( pOwner ) 35cdf0e10cSrcweir { 36cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > aNode( 1 ); 37cdf0e10cSrcweir aNode[ 0 ] = rtl::OUString::createFromAscii( 38cdf0e10cSrcweir "Office.Common/Passwords/AuthenticateUsingSystemCredentials" ); 39cdf0e10cSrcweir EnableNotification( aNode ); 40cdf0e10cSrcweir } 41cdf0e10cSrcweir 42cdf0e10cSrcweir //virtual 43cdf0e10cSrcweir void SysCredentialsConfigItem::Notify( 44cdf0e10cSrcweir const uno::Sequence< rtl::OUString > & /*seqPropertyNames*/ ) 45cdf0e10cSrcweir { 46cdf0e10cSrcweir { 47cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 48cdf0e10cSrcweir m_bInited = false; 49cdf0e10cSrcweir // rebuild m_seqURLs 50cdf0e10cSrcweir getSystemCredentialsURLs(); 51cdf0e10cSrcweir } 52cdf0e10cSrcweir m_pOwner->persistentConfigChanged(); 53cdf0e10cSrcweir } 54cdf0e10cSrcweir 55cdf0e10cSrcweir void SysCredentialsConfigItem::Commit() 56cdf0e10cSrcweir { 57cdf0e10cSrcweir // does nothing 58cdf0e10cSrcweir } 59cdf0e10cSrcweir 60cdf0e10cSrcweir uno::Sequence< rtl::OUString > 61cdf0e10cSrcweir SysCredentialsConfigItem::getSystemCredentialsURLs() 62cdf0e10cSrcweir { 63cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 64cdf0e10cSrcweir if ( !m_bInited ) 65cdf0e10cSrcweir { 66cdf0e10cSrcweir // read config item 67cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > aPropNames( 1 ); 68cdf0e10cSrcweir aPropNames[ 0 ] = rtl::OUString::createFromAscii( 69cdf0e10cSrcweir "AuthenticateUsingSystemCredentials" ); 70cdf0e10cSrcweir uno::Sequence< uno::Any > aAnyValues( 71cdf0e10cSrcweir utl::ConfigItem::GetProperties( aPropNames ) ); 72cdf0e10cSrcweir 73cdf0e10cSrcweir OSL_ENSURE( 74cdf0e10cSrcweir aAnyValues.getLength() == 1, 75cdf0e10cSrcweir "SysCredentialsConfigItem::getSystemCredentialsURLs: " 76cdf0e10cSrcweir "Error reading config item!" ); 77cdf0e10cSrcweir 78cdf0e10cSrcweir uno::Sequence< rtl::OUString > aValues; 79cdf0e10cSrcweir if ( ( aAnyValues[ 0 ] >>= aValues ) || 80cdf0e10cSrcweir ( !aAnyValues[ 0 ].hasValue() ) ) 81cdf0e10cSrcweir { 82cdf0e10cSrcweir m_seqURLs = aValues; 83cdf0e10cSrcweir m_bInited = true; 84cdf0e10cSrcweir } 85cdf0e10cSrcweir } 86cdf0e10cSrcweir return m_seqURLs; 87cdf0e10cSrcweir } 88cdf0e10cSrcweir 89cdf0e10cSrcweir void SysCredentialsConfigItem::setSystemCredentialsURLs( 90cdf0e10cSrcweir const uno::Sequence< rtl::OUString > & seqURLList ) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 93cdf0e10cSrcweir 94cdf0e10cSrcweir // write config item. 95cdf0e10cSrcweir uno::Sequence< rtl::OUString > aPropNames( 1 ); 96cdf0e10cSrcweir uno::Sequence< uno::Any > aPropValues( 1 ); 97cdf0e10cSrcweir aPropNames[ 0 ] 98cdf0e10cSrcweir = ::rtl::OUString::createFromAscii( 99cdf0e10cSrcweir "AuthenticateUsingSystemCredentials" ); 100cdf0e10cSrcweir aPropValues[ 0 ] <<= seqURLList; 101cdf0e10cSrcweir 102cdf0e10cSrcweir utl::ConfigItem::SetModified(); 103cdf0e10cSrcweir utl::ConfigItem::PutProperties( aPropNames, aPropValues ); 104cdf0e10cSrcweir 105cdf0e10cSrcweir m_seqURLs = seqURLList; 106cdf0e10cSrcweir m_bInited = true; 107cdf0e10cSrcweir } 108cdf0e10cSrcweir 109cdf0e10cSrcweir //============================================================================ 110cdf0e10cSrcweir 111cdf0e10cSrcweir namespace 112cdf0e10cSrcweir { 113cdf0e10cSrcweir // TODO: This code is actually copied from svl/source/passwordcontainer.cxx 114cdf0e10cSrcweir bool removeLastSegment( ::rtl::OUString & aURL ) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir sal_Int32 aInd = aURL.lastIndexOf( sal_Unicode( '/' ) ); 117cdf0e10cSrcweir 118cdf0e10cSrcweir if( aInd > 0 ) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir sal_Int32 aPrevInd = aURL.lastIndexOf( sal_Unicode( '/' ), aInd ); 121cdf0e10cSrcweir if ( aURL.indexOf( ::rtl::OUString::createFromAscii( "://" ) ) 122cdf0e10cSrcweir != aPrevInd - 2 || 123cdf0e10cSrcweir aInd != aURL.getLength() - 1 ) 124cdf0e10cSrcweir { 125cdf0e10cSrcweir aURL = aURL.copy( 0, aInd ); 126cdf0e10cSrcweir return true; 127cdf0e10cSrcweir } 128cdf0e10cSrcweir } 129cdf0e10cSrcweir 130cdf0e10cSrcweir return false; 131cdf0e10cSrcweir } 132cdf0e10cSrcweir 133cdf0e10cSrcweir bool findURL( StringSet const & rContainer, rtl::OUString const & aURL, rtl::OUString & aResult ) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir // TODO: This code is actually copied from svl/source/passwordcontainer.cxx 136cdf0e10cSrcweir if( !rContainer.empty() && aURL.getLength() ) 137cdf0e10cSrcweir { 138cdf0e10cSrcweir ::rtl::OUString aUrl( aURL ); 139cdf0e10cSrcweir 140cdf0e10cSrcweir // each iteration remove last '/...' section from the aUrl 141cdf0e10cSrcweir // while it's possible, up to the most left '://' 142cdf0e10cSrcweir do 143cdf0e10cSrcweir { 144cdf0e10cSrcweir // first look for <url>/somename and then look for <url>/somename/... 145cdf0e10cSrcweir StringSet::const_iterator aIter = rContainer.find( aUrl ); 146cdf0e10cSrcweir if( aIter != rContainer.end() ) 147cdf0e10cSrcweir { 148cdf0e10cSrcweir aResult = *aIter; 149cdf0e10cSrcweir return true; 150cdf0e10cSrcweir } 151cdf0e10cSrcweir else 152cdf0e10cSrcweir { 153cdf0e10cSrcweir ::rtl::OUString tmpUrl( aUrl ); 154cdf0e10cSrcweir if ( tmpUrl.getStr()[tmpUrl.getLength() - 1] != (sal_Unicode)'/' ) 155cdf0e10cSrcweir tmpUrl += ::rtl::OUString::createFromAscii( "/" ); 156cdf0e10cSrcweir 157cdf0e10cSrcweir aIter = rContainer.lower_bound( tmpUrl ); 158cdf0e10cSrcweir if( aIter != rContainer.end() && aIter->match( tmpUrl ) ) 159cdf0e10cSrcweir { 160cdf0e10cSrcweir aResult = *aIter; 161cdf0e10cSrcweir return true; 162cdf0e10cSrcweir } 163cdf0e10cSrcweir } 164cdf0e10cSrcweir } 165cdf0e10cSrcweir while( removeLastSegment( aUrl ) && aUrl.getLength() ); 166cdf0e10cSrcweir } 167cdf0e10cSrcweir aResult = rtl::OUString(); 168cdf0e10cSrcweir return false; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir } // namespace 172cdf0e10cSrcweir 173cdf0e10cSrcweir SysCredentialsConfig::SysCredentialsConfig() 174cdf0e10cSrcweir : m_aConfigItem( this ), 175cdf0e10cSrcweir m_bCfgInited( false ) 176cdf0e10cSrcweir { 177cdf0e10cSrcweir } 178cdf0e10cSrcweir 179cdf0e10cSrcweir void SysCredentialsConfig::initCfg() 180cdf0e10cSrcweir { 181cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 182cdf0e10cSrcweir if ( !m_bCfgInited ) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir uno::Sequence< rtl::OUString > aURLs( 185cdf0e10cSrcweir m_aConfigItem.getSystemCredentialsURLs() ); 186cdf0e10cSrcweir for ( sal_Int32 n = 0; n < aURLs.getLength(); ++n ) 187cdf0e10cSrcweir m_aCfgContainer.insert( aURLs[ n ] ); 188cdf0e10cSrcweir 189cdf0e10cSrcweir m_bCfgInited = true; 190cdf0e10cSrcweir } 191cdf0e10cSrcweir } 192cdf0e10cSrcweir 193cdf0e10cSrcweir void SysCredentialsConfig::writeCfg() 194cdf0e10cSrcweir { 195cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 196cdf0e10cSrcweir 197cdf0e10cSrcweir OSL_ENSURE( m_bCfgInited, "SysCredentialsConfig::writeCfg : not initialized!" ); 198cdf0e10cSrcweir 199cdf0e10cSrcweir uno::Sequence< rtl::OUString > aURLs( m_aCfgContainer.size() ); 200cdf0e10cSrcweir StringSet::const_iterator it = m_aCfgContainer.begin(); 201cdf0e10cSrcweir const StringSet::const_iterator end = m_aCfgContainer.end(); 202cdf0e10cSrcweir sal_Int32 n = 0; 203cdf0e10cSrcweir 204cdf0e10cSrcweir while ( it != end ) 205cdf0e10cSrcweir { 206cdf0e10cSrcweir aURLs[ n ] = *it; 207cdf0e10cSrcweir ++it; 208cdf0e10cSrcweir ++n; 209cdf0e10cSrcweir } 210cdf0e10cSrcweir 211cdf0e10cSrcweir m_aConfigItem.setSystemCredentialsURLs( aURLs ); 212cdf0e10cSrcweir } 213cdf0e10cSrcweir 214cdf0e10cSrcweir rtl::OUString SysCredentialsConfig::find( rtl::OUString const & aURL ) 215cdf0e10cSrcweir { 216cdf0e10cSrcweir osl::MutexGuard aGuard( m_aMutex ); 217cdf0e10cSrcweir rtl::OUString aResult; 218cdf0e10cSrcweir if ( findURL( m_aMemContainer, aURL, aResult ) ) 219cdf0e10cSrcweir return aResult; 220cdf0e10cSrcweir 221cdf0e10cSrcweir initCfg(); 222cdf0e10cSrcweir if ( findURL( m_aCfgContainer, aURL, aResult ) ) 223cdf0e10cSrcweir return aResult; 224cdf0e10cSrcweir 225cdf0e10cSrcweir return rtl::OUString(); 226cdf0e10cSrcweir } 227cdf0e10cSrcweir 228cdf0e10cSrcweir void SysCredentialsConfig::add( rtl::OUString const & rURL, bool bPersistent ) 229cdf0e10cSrcweir { 230cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 231cdf0e10cSrcweir 232cdf0e10cSrcweir if ( bPersistent ) 233cdf0e10cSrcweir { 234cdf0e10cSrcweir m_aMemContainer.erase( rURL ); 235cdf0e10cSrcweir 236cdf0e10cSrcweir initCfg(); 237cdf0e10cSrcweir m_aCfgContainer.insert( rURL ); 238cdf0e10cSrcweir writeCfg(); 239cdf0e10cSrcweir } 240cdf0e10cSrcweir else 241cdf0e10cSrcweir { 242cdf0e10cSrcweir initCfg(); 243cdf0e10cSrcweir if ( m_aCfgContainer.erase( rURL ) > 0 ) 244cdf0e10cSrcweir writeCfg(); 245cdf0e10cSrcweir 246cdf0e10cSrcweir m_aMemContainer.insert( rURL ); 247cdf0e10cSrcweir } 248cdf0e10cSrcweir } 249cdf0e10cSrcweir 250cdf0e10cSrcweir void SysCredentialsConfig::remove( rtl::OUString const & rURL ) 251cdf0e10cSrcweir { 252cdf0e10cSrcweir m_aMemContainer.erase( rURL ); 253cdf0e10cSrcweir 254cdf0e10cSrcweir initCfg(); 255cdf0e10cSrcweir if ( m_aCfgContainer.erase( rURL ) > 0 ) 256cdf0e10cSrcweir writeCfg(); 257cdf0e10cSrcweir } 258cdf0e10cSrcweir 259cdf0e10cSrcweir uno::Sequence< rtl::OUString > SysCredentialsConfig::list( bool bOnlyPersistent ) 260cdf0e10cSrcweir { 261cdf0e10cSrcweir initCfg(); 262cdf0e10cSrcweir sal_Int32 nCount = m_aCfgContainer.size() 263cdf0e10cSrcweir + ( bOnlyPersistent ? 0 : m_aMemContainer.size() ); 264cdf0e10cSrcweir uno::Sequence< rtl::OUString > aResult( nCount ); 265cdf0e10cSrcweir 266cdf0e10cSrcweir StringSet::const_iterator it = m_aCfgContainer.begin(); 267cdf0e10cSrcweir StringSet::const_iterator end = m_aCfgContainer.end(); 268cdf0e10cSrcweir sal_Int32 n = 0; 269cdf0e10cSrcweir 270cdf0e10cSrcweir while ( it != end ) 271cdf0e10cSrcweir { 272cdf0e10cSrcweir aResult[ n ] = *it; 273cdf0e10cSrcweir ++it; 274cdf0e10cSrcweir ++n; 275cdf0e10cSrcweir } 276cdf0e10cSrcweir 277cdf0e10cSrcweir if ( !bOnlyPersistent ) 278cdf0e10cSrcweir { 279cdf0e10cSrcweir it = m_aMemContainer.begin(); 280cdf0e10cSrcweir end = m_aMemContainer.end(); 281cdf0e10cSrcweir 282cdf0e10cSrcweir while ( it != end ) 283cdf0e10cSrcweir { 284cdf0e10cSrcweir aResult[ n ] = *it; 285cdf0e10cSrcweir ++it; 286cdf0e10cSrcweir ++n; 287cdf0e10cSrcweir } 288cdf0e10cSrcweir } 289cdf0e10cSrcweir return aResult; 290cdf0e10cSrcweir } 291cdf0e10cSrcweir 292cdf0e10cSrcweir void SysCredentialsConfig::persistentConfigChanged() 293cdf0e10cSrcweir { 294cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 295cdf0e10cSrcweir m_bCfgInited = false; // re-init on demand. 296cdf0e10cSrcweir } 297