LCOV - code coverage report
Current view: top level - extensions/source/config/ldap - ldapuserprofilebe.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 98 0.0 %
Date: 2012-08-25 Functions: 0 12 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : 
      30                 :            : #include "ldapaccess.hxx"
      31                 :            : #include "ldapuserprofilebe.hxx"
      32                 :            : #include <osl/file.hxx>
      33                 :            : #include <osl/module.hxx>
      34                 :            : #include <osl/process.h>
      35                 :            : #include <rtl/ustrbuf.hxx>
      36                 :            : #include <rtl/byteseq.h>
      37                 :            : 
      38                 :            : #include <rtl/instance.hxx>
      39                 :            : #include <com/sun/star/beans/NamedValue.hpp>
      40                 :            : #include <com/sun/star/beans/Optional.hpp>
      41                 :            : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      42                 :            : #include <osl/security.hxx>
      43                 :            : 
      44                 :            : //==============================================================================
      45                 :            : namespace extensions { namespace config { namespace ldap {
      46                 :            : 
      47                 :          0 : LdapUserProfileBe::LdapUserProfileBe( const uno::Reference<uno::XComponentContext>& xContext)
      48                 :            : : LdapProfileMutexHolder(),
      49                 :          0 :   BackendBase(mMutex)
      50                 :            : {
      51                 :          0 :     LdapDefinition aDefinition;
      52                 :          0 :     rtl::OUString loggedOnUser;
      53                 :            : 
      54                 :            :     // This whole rigmarole is to prevent an infinite recursion where reading
      55                 :            :     // the configuration for the backend would create another instance of the
      56                 :            :     // backend, which would try and read the configuration which would...
      57                 :            :     {
      58                 :          0 :         osl::Mutex & aInitMutex = rtl::Static< osl::Mutex, LdapUserProfileBe >::get();
      59                 :          0 :         osl::MutexGuard aInitGuard(aInitMutex);
      60                 :            : 
      61                 :            :         static bool bReentrantCall; // = false
      62                 :            :         OSL_ENSURE(!bReentrantCall, "configuration: Ldap Backend constructor called reentrantly - probably a registration error.");
      63                 :            : 
      64                 :          0 :         if (!bReentrantCall)
      65                 :            :         {
      66                 :            :             try
      67                 :            :             {
      68                 :          0 :                 bReentrantCall = true ;
      69                 :          0 :                 if (!readLdapConfiguration(
      70                 :          0 :                         xContext, &aDefinition, &loggedOnUser))
      71                 :            :                 {
      72                 :            :                     throw css::uno::RuntimeException(
      73                 :            :                         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("LdapUserProfileBe- LDAP not configured")),
      74                 :          0 :                         NULL);
      75                 :            :                 }
      76                 :            : 
      77                 :          0 :                 bReentrantCall = false ;
      78                 :            :             }
      79                 :          0 :             catch (...)
      80                 :            :             {
      81                 :          0 :                 bReentrantCall = false;
      82                 :          0 :                 throw;
      83                 :            :             }
      84                 :          0 :         }
      85                 :            :     }
      86                 :            : 
      87                 :          0 :     LdapConnection connection;
      88                 :          0 :     connection.connectSimple(aDefinition);
      89                 :          0 :     connection.getUserProfile(loggedOnUser, &data_);
      90                 :          0 : }
      91                 :            : //------------------------------------------------------------------------------
      92                 :          0 : LdapUserProfileBe::~LdapUserProfileBe()
      93                 :            : {
      94                 :          0 : }
      95                 :            : //------------------------------------------------------------------------------
      96                 :            : 
      97                 :          0 : bool LdapUserProfileBe::readLdapConfiguration(
      98                 :            :     css::uno::Reference< css::uno::XComponentContext > const & context,
      99                 :            :     LdapDefinition * definition, rtl::OUString * loggedOnUser)
     100                 :            : {
     101                 :            :     OSL_ASSERT(context.is() && definition != 0 && loggedOnUser != 0);
     102                 :          0 :     const rtl::OUString kReadOnlyViewService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")) ;
     103                 :          0 :     const rtl::OUString kComponent( RTL_CONSTASCII_USTRINGPARAM("org.openoffice.LDAP/UserDirectory"));
     104                 :          0 :     const rtl::OUString kServerDefiniton(RTL_CONSTASCII_USTRINGPARAM ("ServerDefinition"));
     105                 :          0 :     const rtl::OUString kServer(RTL_CONSTASCII_USTRINGPARAM ("Server"));
     106                 :          0 :     const rtl::OUString kPort(RTL_CONSTASCII_USTRINGPARAM("Port"));
     107                 :          0 :     const rtl::OUString kBaseDN(RTL_CONSTASCII_USTRINGPARAM("BaseDN"));
     108                 :          0 :     const rtl::OUString kUser(RTL_CONSTASCII_USTRINGPARAM("SearchUser"));
     109                 :          0 :     const rtl::OUString kPassword(RTL_CONSTASCII_USTRINGPARAM("SearchPassword"));
     110                 :          0 :     const rtl::OUString kUserObjectClass(RTL_CONSTASCII_USTRINGPARAM("UserObjectClass"));
     111                 :          0 :     const rtl::OUString kUserUniqueAttr(RTL_CONSTASCII_USTRINGPARAM("UserUniqueAttribute"));
     112                 :            : 
     113                 :          0 :     uno::Reference< XInterface > xIface;
     114                 :            :     try
     115                 :            :     {
     116                 :            :         uno::Reference< lang::XMultiServiceFactory > xCfgProvider(
     117                 :          0 :             css::configuration::theDefaultProvider::get(context));
     118                 :            : 
     119                 :          0 :         css::beans::NamedValue aPath(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("nodepath")), uno::makeAny(kComponent) );
     120                 :            : 
     121                 :          0 :         uno::Sequence< uno::Any > aArgs(1);
     122                 :          0 :         aArgs[0] <<=  aPath;
     123                 :            : 
     124                 :          0 :         xIface = xCfgProvider->createInstanceWithArguments(kReadOnlyViewService, aArgs);
     125                 :            : 
     126                 :          0 :         uno::Reference<container::XNameAccess > xAccess(xIface, uno::UNO_QUERY_THROW);
     127                 :          0 :         xAccess->getByName(kServerDefiniton) >>= xIface;
     128                 :            : 
     129                 :          0 :         uno::Reference<container::XNameAccess > xChildAccess(xIface, uno::UNO_QUERY_THROW);
     130                 :            : 
     131                 :          0 :         if (!getLdapStringParam(xChildAccess, kServer, definition->mServer))
     132                 :          0 :             return false;
     133                 :          0 :         if (!getLdapStringParam(xChildAccess, kBaseDN, definition->mBaseDN))
     134                 :          0 :             return false;
     135                 :            : 
     136                 :          0 :         definition->mPort=0;
     137                 :          0 :         xChildAccess->getByName(kPort) >>= definition->mPort ;
     138                 :          0 :         if (definition->mPort == 0)
     139                 :          0 :             return false;
     140                 :            : 
     141                 :          0 :         if (!getLdapStringParam(xAccess, kUserObjectClass, definition->mUserObjectClass))
     142                 :          0 :             return false;
     143                 :          0 :         if (!getLdapStringParam(xAccess, kUserUniqueAttr, definition->mUserUniqueAttr))
     144                 :          0 :             return false;
     145                 :            : 
     146                 :          0 :         getLdapStringParam(xAccess, kUser, definition->mAnonUser);
     147                 :          0 :         getLdapStringParam(xAccess, kPassword, definition->mAnonCredentials);
     148                 :            :     }
     149                 :          0 :     catch (const uno::Exception & e)
     150                 :            :     {
     151                 :            :         OSL_TRACE("LdapUserProfileBackend: access to configuration data failed: %s",
     152                 :            :                 rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
     153                 :          0 :         return false;
     154                 :            :     }
     155                 :            : 
     156                 :          0 :     osl::Security aSecurityContext;
     157                 :          0 :     if (!aSecurityContext.getUserName(*loggedOnUser))
     158                 :            :         OSL_TRACE("LdapUserProfileBackend - could not get Logged on user from system");
     159                 :            : 
     160                 :          0 :     sal_Int32 nIndex = loggedOnUser->indexOf('/');
     161                 :          0 :     if (nIndex > 0)
     162                 :          0 :         *loggedOnUser = loggedOnUser->copy(nIndex+1);
     163                 :            : 
     164                 :            :     //Remember to remove
     165                 :            :     OSL_TRACE("Logged on user is %s", rtl::OUStringToOString(*loggedOnUser,RTL_TEXTENCODING_ASCII_US).getStr());
     166                 :            : 
     167                 :          0 :     return true;
     168                 :            : }
     169                 :            : 
     170                 :            : //------------------------------------------------------------------------------
     171                 :          0 : bool LdapUserProfileBe::getLdapStringParam(
     172                 :            :     uno::Reference<container::XNameAccess>& xAccess,
     173                 :            :     const rtl::OUString& aLdapSetting,
     174                 :            :     rtl::OUString& aServerParameter)
     175                 :            : {
     176                 :          0 :     xAccess->getByName(aLdapSetting) >>= aServerParameter;
     177                 :            : 
     178                 :          0 :     return !aServerParameter.isEmpty();
     179                 :            : }
     180                 :            : //------------------------------------------------------------------------------
     181                 :          0 : void LdapUserProfileBe::setPropertyValue(
     182                 :            :     rtl::OUString const &, css::uno::Any const &)
     183                 :            :     throw (
     184                 :            :         css::beans::UnknownPropertyException, css::beans::PropertyVetoException,
     185                 :            :         css::lang::IllegalArgumentException, css::lang::WrappedTargetException,
     186                 :            :         css::uno::RuntimeException)
     187                 :            : {
     188                 :            :     throw css::lang::IllegalArgumentException(
     189                 :            :         rtl::OUString(
     190                 :            :             RTL_CONSTASCII_USTRINGPARAM("setPropertyValue not supported")),
     191                 :          0 :         static_cast< cppu::OWeakObject * >(this), -1);
     192                 :            : }
     193                 :            : 
     194                 :          0 : css::uno::Any LdapUserProfileBe::getPropertyValue(
     195                 :            :     rtl::OUString const & PropertyName)
     196                 :            :     throw (
     197                 :            :         css::beans::UnknownPropertyException, css::lang::WrappedTargetException,
     198                 :            :         css::uno::RuntimeException)
     199                 :            : {
     200                 :          0 :     for (sal_Int32 i = 0;;) {
     201                 :          0 :         sal_Int32 j = PropertyName.indexOf(',', i);
     202                 :          0 :         if (j == -1) {
     203                 :          0 :             j = PropertyName.getLength();
     204                 :            :         }
     205                 :          0 :         if (j == i) {
     206                 :            :             throw css::beans::UnknownPropertyException(
     207                 :          0 :                 PropertyName, static_cast< cppu::OWeakObject * >(this));
     208                 :            :         }
     209                 :          0 :         LdapData::iterator k(data_.find(PropertyName.copy(i, j - i)));
     210                 :          0 :         if (k != data_.end()) {
     211                 :            :             return css::uno::makeAny(
     212                 :            :                 css::beans::Optional< css::uno::Any >(
     213                 :          0 :                     true, css::uno::makeAny(k->second)));
     214                 :            :         }
     215                 :          0 :         if (j == PropertyName.getLength()) {
     216                 :            :             break;
     217                 :            :         }
     218                 :          0 :         i = j + 1;
     219                 :            :     }
     220                 :          0 :     return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
     221                 :            : }
     222                 :            : 
     223                 :            : //------------------------------------------------------------------------------
     224                 :          0 : rtl::OUString SAL_CALL LdapUserProfileBe::getLdapUserProfileBeName(void) {
     225                 :          0 :     return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.configuration.backend.LdapUserProfileBe"));
     226                 :            : }
     227                 :            : //------------------------------------------------------------------------------
     228                 :            : 
     229                 :          0 : rtl::OUString SAL_CALL LdapUserProfileBe::getImplementationName(void)
     230                 :            :     throw (uno::RuntimeException)
     231                 :            : {
     232                 :          0 :     return getLdapUserProfileBeName() ;
     233                 :            : }
     234                 :            : //------------------------------------------------------------------------------
     235                 :            : 
     236                 :          0 : uno::Sequence<rtl::OUString> SAL_CALL LdapUserProfileBe::getLdapUserProfileBeServiceNames(void)
     237                 :            : {
     238                 :          0 :     uno::Sequence<rtl::OUString> aServices(1) ;
     239                 :          0 :     aServices[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.backend.LdapUserProfileBe")) ;
     240                 :          0 :     return aServices ;
     241                 :            : }
     242                 :            : //------------------------------------------------------------------------------
     243                 :            : 
     244                 :          0 : sal_Bool SAL_CALL LdapUserProfileBe::supportsService(const rtl::OUString& aServiceName)
     245                 :            :     throw (uno::RuntimeException)
     246                 :            : {
     247                 :          0 :     uno::Sequence< rtl::OUString > const svc = getLdapUserProfileBeServiceNames();
     248                 :            : 
     249                 :          0 :     for(sal_Int32 i = 0; i < svc.getLength(); ++i )
     250                 :          0 :         if(svc[i] == aServiceName)
     251                 :          0 :             return true;
     252                 :          0 :     return false;
     253                 :            : }
     254                 :            : 
     255                 :            : //------------------------------------------------------------------------------
     256                 :            : 
     257                 :            : uno::Sequence<rtl::OUString>
     258                 :          0 : SAL_CALL LdapUserProfileBe::getSupportedServiceNames(void)
     259                 :            :     throw (uno::RuntimeException)
     260                 :            : {
     261                 :          0 :     return getLdapUserProfileBeServiceNames() ;
     262                 :            : }
     263                 :            : // ---------------------------------------------------------------------------------------
     264                 :            : }}}
     265                 :            : // ---------------------------------------------------------------------------------------
     266                 :            : 
     267                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10