LCOV - code coverage report
Current view: top level - extensions/source/config/ldap - ldapuserprofilebe.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 96 0.0 %
Date: 2014-04-14 Functions: 0 12 0.0 %
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             : 
      20             : 
      21             : #include "ldapaccess.hxx"
      22             : #include "ldapuserprofilebe.hxx"
      23             : #include <osl/file.hxx>
      24             : #include <osl/module.hxx>
      25             : #include <osl/process.h>
      26             : #include <rtl/ustrbuf.hxx>
      27             : #include <rtl/byteseq.h>
      28             : 
      29             : #include <rtl/instance.hxx>
      30             : #include <com/sun/star/beans/NamedValue.hpp>
      31             : #include <com/sun/star/beans/Optional.hpp>
      32             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      33             : #include <cppuhelper/supportsservice.hxx>
      34             : #include <osl/security.hxx>
      35             : 
      36             : 
      37             : namespace extensions { namespace config { namespace ldap {
      38             : 
      39           0 : LdapUserProfileBe::LdapUserProfileBe( const uno::Reference<uno::XComponentContext>& xContext)
      40             : : LdapProfileMutexHolder(),
      41           0 :   BackendBase(mMutex)
      42             : {
      43           0 :     LdapDefinition aDefinition;
      44           0 :     OUString loggedOnUser;
      45             : 
      46             :     // This whole rigmarole is to prevent an infinite recursion where reading
      47             :     // the configuration for the backend would create another instance of the
      48             :     // backend, which would try and read the configuration which would...
      49             :     {
      50           0 :         osl::Mutex & aInitMutex = rtl::Static< osl::Mutex, LdapUserProfileBe >::get();
      51           0 :         osl::MutexGuard aInitGuard(aInitMutex);
      52             : 
      53             :         static bool bReentrantCall; // = false
      54             :         OSL_ENSURE(!bReentrantCall, "configuration: Ldap Backend constructor called reentrantly - probably a registration error.");
      55             : 
      56           0 :         if (!bReentrantCall)
      57             :         {
      58             :             try
      59             :             {
      60           0 :                 bReentrantCall = true ;
      61           0 :                 if (!readLdapConfiguration(
      62           0 :                         xContext, &aDefinition, &loggedOnUser))
      63             :                 {
      64             :                     throw css::uno::RuntimeException(
      65             :                         OUString("LdapUserProfileBe- LDAP not configured"),
      66           0 :                         NULL);
      67             :                 }
      68             : 
      69           0 :                 bReentrantCall = false ;
      70             :             }
      71           0 :             catch (...)
      72             :             {
      73           0 :                 bReentrantCall = false;
      74           0 :                 throw;
      75             :             }
      76           0 :         }
      77             :     }
      78             : 
      79           0 :     LdapConnection connection;
      80           0 :     connection.connectSimple(aDefinition);
      81           0 :     connection.getUserProfile(loggedOnUser, &data_);
      82           0 : }
      83             : 
      84           0 : LdapUserProfileBe::~LdapUserProfileBe()
      85             : {
      86           0 : }
      87             : 
      88             : 
      89           0 : bool LdapUserProfileBe::readLdapConfiguration(
      90             :     css::uno::Reference< css::uno::XComponentContext > const & context,
      91             :     LdapDefinition * definition, OUString * loggedOnUser)
      92             : {
      93             :     OSL_ASSERT(context.is() && definition != 0 && loggedOnUser != 0);
      94           0 :     const OUString kReadOnlyViewService("com.sun.star.configuration.ConfigurationAccess") ;
      95           0 :     const OUString kComponent("org.openoffice.LDAP/UserDirectory");
      96           0 :     const OUString kServerDefiniton("ServerDefinition");
      97           0 :     const OUString kServer("Server");
      98           0 :     const OUString kPort("Port");
      99           0 :     const OUString kBaseDN("BaseDN");
     100           0 :     const OUString kUser("SearchUser");
     101           0 :     const OUString kPassword("SearchPassword");
     102           0 :     const OUString kUserObjectClass("UserObjectClass");
     103           0 :     const OUString kUserUniqueAttr("UserUniqueAttribute");
     104             : 
     105           0 :     uno::Reference< XInterface > xIface;
     106             :     try
     107             :     {
     108             :         uno::Reference< lang::XMultiServiceFactory > xCfgProvider(
     109           0 :             css::configuration::theDefaultProvider::get(context));
     110             : 
     111           0 :         css::beans::NamedValue aPath(OUString("nodepath"), uno::makeAny(kComponent) );
     112             : 
     113           0 :         uno::Sequence< uno::Any > aArgs(1);
     114           0 :         aArgs[0] <<=  aPath;
     115             : 
     116           0 :         xIface = xCfgProvider->createInstanceWithArguments(kReadOnlyViewService, aArgs);
     117             : 
     118           0 :         uno::Reference<container::XNameAccess > xAccess(xIface, uno::UNO_QUERY_THROW);
     119           0 :         xAccess->getByName(kServerDefiniton) >>= xIface;
     120             : 
     121           0 :         uno::Reference<container::XNameAccess > xChildAccess(xIface, uno::UNO_QUERY_THROW);
     122             : 
     123           0 :         if (!getLdapStringParam(xChildAccess, kServer, definition->mServer))
     124           0 :             return false;
     125           0 :         if (!getLdapStringParam(xChildAccess, kBaseDN, definition->mBaseDN))
     126           0 :             return false;
     127             : 
     128           0 :         definition->mPort=0;
     129           0 :         xChildAccess->getByName(kPort) >>= definition->mPort ;
     130           0 :         if (definition->mPort == 0)
     131           0 :             return false;
     132             : 
     133           0 :         if (!getLdapStringParam(xAccess, kUserObjectClass, definition->mUserObjectClass))
     134           0 :             return false;
     135           0 :         if (!getLdapStringParam(xAccess, kUserUniqueAttr, definition->mUserUniqueAttr))
     136           0 :             return false;
     137             : 
     138           0 :         getLdapStringParam(xAccess, kUser, definition->mAnonUser);
     139           0 :         getLdapStringParam(xAccess, kPassword, definition->mAnonCredentials);
     140             :     }
     141           0 :     catch (const uno::Exception & e)
     142             :     {
     143             :         OSL_TRACE("LdapUserProfileBackend: access to configuration data failed: %s",
     144             :                 OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
     145           0 :         return false;
     146             :     }
     147             : 
     148           0 :     osl::Security aSecurityContext;
     149           0 :     if (!aSecurityContext.getUserName(*loggedOnUser))
     150             :         OSL_TRACE("LdapUserProfileBackend - could not get Logged on user from system");
     151             : 
     152           0 :     sal_Int32 nIndex = loggedOnUser->indexOf('/');
     153           0 :     if (nIndex > 0)
     154           0 :         *loggedOnUser = loggedOnUser->copy(nIndex+1);
     155             : 
     156             :     //Remember to remove
     157             :     OSL_TRACE("Logged on user is %s", OUStringToOString(*loggedOnUser,RTL_TEXTENCODING_ASCII_US).getStr());
     158             : 
     159           0 :     return true;
     160             : }
     161             : 
     162             : 
     163           0 : bool LdapUserProfileBe::getLdapStringParam(
     164             :     uno::Reference<container::XNameAccess>& xAccess,
     165             :     const OUString& aLdapSetting,
     166             :     OUString& aServerParameter)
     167             : {
     168           0 :     xAccess->getByName(aLdapSetting) >>= aServerParameter;
     169             : 
     170           0 :     return !aServerParameter.isEmpty();
     171             : }
     172             : 
     173           0 : void LdapUserProfileBe::setPropertyValue(
     174             :     OUString const &, css::uno::Any const &)
     175             :     throw (
     176             :         css::beans::UnknownPropertyException, css::beans::PropertyVetoException,
     177             :         css::lang::IllegalArgumentException, css::lang::WrappedTargetException,
     178             :         css::uno::RuntimeException, std::exception)
     179             : {
     180             :     throw css::lang::IllegalArgumentException(
     181             :         OUString("setPropertyValue not supported"),
     182           0 :         static_cast< cppu::OWeakObject * >(this), -1);
     183             : }
     184             : 
     185           0 : css::uno::Any LdapUserProfileBe::getPropertyValue(
     186             :     OUString const & PropertyName)
     187             :     throw (
     188             :         css::beans::UnknownPropertyException, css::lang::WrappedTargetException,
     189             :         css::uno::RuntimeException, std::exception)
     190             : {
     191           0 :     for (sal_Int32 i = 0;;) {
     192           0 :         sal_Int32 j = PropertyName.indexOf(',', i);
     193           0 :         if (j == -1) {
     194           0 :             j = PropertyName.getLength();
     195             :         }
     196           0 :         if (j == i) {
     197             :             throw css::beans::UnknownPropertyException(
     198           0 :                 PropertyName, static_cast< cppu::OWeakObject * >(this));
     199             :         }
     200           0 :         LdapData::iterator k(data_.find(PropertyName.copy(i, j - i)));
     201           0 :         if (k != data_.end()) {
     202             :             return css::uno::makeAny(
     203             :                 css::beans::Optional< css::uno::Any >(
     204           0 :                     true, css::uno::makeAny(k->second)));
     205             :         }
     206           0 :         if (j == PropertyName.getLength()) {
     207           0 :             break;
     208             :         }
     209           0 :         i = j + 1;
     210           0 :     }
     211           0 :     return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
     212             : }
     213             : 
     214             : 
     215           0 : OUString SAL_CALL LdapUserProfileBe::getLdapUserProfileBeName(void) {
     216           0 :     return OUString("com.sun.star.comp.configuration.backend.LdapUserProfileBe");
     217             : }
     218             : 
     219             : 
     220           0 : OUString SAL_CALL LdapUserProfileBe::getImplementationName(void)
     221             :     throw (uno::RuntimeException, std::exception)
     222             : {
     223           0 :     return getLdapUserProfileBeName() ;
     224             : }
     225             : 
     226             : 
     227           0 : uno::Sequence<OUString> SAL_CALL LdapUserProfileBe::getLdapUserProfileBeServiceNames(void)
     228             : {
     229           0 :     uno::Sequence<OUString> aServices(1) ;
     230           0 :     aServices[0] = "com.sun.star.configuration.backend.LdapUserProfileBe";
     231           0 :     return aServices ;
     232             : }
     233             : 
     234           0 : sal_Bool SAL_CALL LdapUserProfileBe::supportsService(const OUString& aServiceName)
     235             :     throw (uno::RuntimeException, std::exception)
     236             : {
     237           0 :     return cppu::supportsService(this, aServiceName);
     238             : }
     239             : 
     240             : uno::Sequence<OUString>
     241           0 : SAL_CALL LdapUserProfileBe::getSupportedServiceNames(void)
     242             :     throw (uno::RuntimeException, std::exception)
     243             : {
     244           0 :     return getLdapUserProfileBeServiceNames() ;
     245             : }
     246             : 
     247             : }}}
     248             : 
     249             : 
     250             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10