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