LCOV - code coverage report
Current view: top level - extensions/source/config/ldap - ldapaccess.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 0 86 0.0 %
Date: 2014-11-03 Functions: 0 10 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             : 
      23             : #include <boost/noncopyable.hpp>
      24             : #include <rtl/ustrbuf.hxx>
      25             : #include <rtl/strbuf.hxx>
      26             : 
      27             : 
      28             : namespace extensions { namespace config { namespace ldap {
      29             : 
      30             : 
      31             : typedef int LdapErrCode;
      32             : 
      33             : struct LdapMessageHolder: private boost::noncopyable
      34             : {
      35           0 :     LdapMessageHolder() : msg(0) {}
      36           0 :     ~LdapMessageHolder()
      37             :     {
      38           0 :         if (msg)
      39           0 :             ldap_msgfree(msg);
      40           0 :     }
      41             : 
      42             :     LDAPMessage * msg;
      43             : };
      44             : 
      45           0 : LdapConnection::~LdapConnection()
      46             : {
      47           0 :     if (isValid()) disconnect();
      48           0 : }
      49             : 
      50             : 
      51           0 : void LdapConnection::disconnect()
      52             : {
      53           0 :     if (mConnection != NULL)
      54             :     {
      55           0 :         ldap_unbind_s(mConnection) ;
      56           0 :         mConnection = NULL;
      57             :     }
      58           0 : }
      59             : 
      60             : 
      61           0 : static void checkLdapReturnCode(const sal_Char *aOperation,
      62             :                                 LdapErrCode aRetCode,
      63             :                                 LDAP * /*aConnection*/)
      64             : {
      65           0 :     if (aRetCode == LDAP_SUCCESS) { return ; }
      66             : 
      67             :     static const sal_Char *kNoSpecificMessage = "No additional information" ;
      68           0 :     OUStringBuffer message ;
      69             : 
      70           0 :     if (aOperation != NULL)
      71             :     {
      72           0 :         message.appendAscii(aOperation).appendAscii(": ") ;
      73             :     }
      74           0 :     message.appendAscii(ldap_err2string(aRetCode)).appendAscii(" (") ;
      75           0 :     sal_Char *stub = NULL ;
      76             : 
      77             : #ifndef LDAP_OPT_SIZELIMIT // for use with OpenLDAP
      78             :     ldap_get_lderrno(aConnection, NULL, &stub) ;
      79             : #endif
      80           0 :     if (stub != NULL)
      81             :     {
      82           0 :         message.appendAscii(stub) ;
      83             :         // It would seem the message returned is actually
      84             :         // not a copy of a string but rather some static
      85             :         // string itself. At any rate freeing it seems to
      86             :         // cause some undue problems at least on Windows.
      87             :         // This call is thus disabled for the moment.
      88             :         //ldap_memfree(stub) ;
      89             :     }
      90           0 :     else { message.appendAscii(kNoSpecificMessage) ; }
      91           0 :     message.appendAscii(")") ;
      92             :     throw ldap::LdapGenericException(message.makeStringAndClear(),
      93           0 :                                      NULL, aRetCode) ;
      94             : }
      95             : 
      96           0 : void  LdapConnection::connectSimple(const LdapDefinition& aDefinition)
      97             :    throw (ldap::LdapConnectionException, ldap::LdapGenericException)
      98             : {
      99             :     OSL_ENSURE(!isValid(), "Recoonecting an LDAP connection that is already established");
     100           0 :     if (isValid()) disconnect();
     101             : 
     102           0 :     mLdapDefinition = aDefinition;
     103           0 :     connectSimple();
     104           0 : }
     105             : 
     106           0 : void  LdapConnection::connectSimple()
     107             :    throw (ldap::LdapConnectionException, ldap::LdapGenericException)
     108             : {
     109           0 :     if (!isValid())
     110             :     {
     111             :         // Connect to the server
     112           0 :         initConnection() ;
     113             :         // Set Protocol V3
     114           0 :         int version = LDAP_VERSION3;
     115             :         ldap_set_option(mConnection,
     116             :                         LDAP_OPT_PROTOCOL_VERSION,
     117           0 :                         &version);
     118             : 
     119             : #ifdef LDAP_X_OPT_CONNECT_TIMEOUT // OpenLDAP doesn't support this and the func
     120             :         /* timeout is specified in milliseconds -> 4 seconds*/
     121             :         int timeout = 4000;
     122             : #ifdef WNT
     123             :         ldap_set_optionW( mConnection,
     124             :                         LDAP_X_OPT_CONNECT_TIMEOUT,
     125             :                         &timeout );
     126             : #else
     127             :         ldap_set_option( mConnection,
     128             :                         LDAP_X_OPT_CONNECT_TIMEOUT,
     129             :                         &timeout );
     130             : #endif
     131             : #endif
     132             : 
     133             :         // Do the bind
     134             : #ifdef WNT
     135             :         LdapErrCode retCode = ldap_simple_bind_sW(mConnection,
     136             :                                                (PWCHAR) mLdapDefinition.mAnonUser.getStr(),
     137             :                                                (PWCHAR) mLdapDefinition.mAnonCredentials.getStr() );
     138             : #else
     139             :         LdapErrCode retCode = ldap_simple_bind_s(mConnection,
     140             :                                                OUStringToOString( mLdapDefinition.mAnonUser, RTL_TEXTENCODING_UTF8 ).getStr(),
     141           0 :                                                OUStringToOString( mLdapDefinition.mAnonCredentials, RTL_TEXTENCODING_UTF8 ).getStr()) ;
     142             : #endif
     143             : 
     144           0 :         checkLdapReturnCode("SimpleBind", retCode, mConnection) ;
     145             :     }
     146           0 : }
     147             : 
     148           0 : void LdapConnection::initConnection()
     149             :     throw (ldap::LdapConnectionException)
     150             : {
     151           0 :     if (mLdapDefinition.mServer.isEmpty())
     152             :     {
     153           0 :         OUStringBuffer message ;
     154             : 
     155           0 :         message.appendAscii("Cannot initialise connection to LDAP: No server specified.") ;
     156           0 :         throw ldap::LdapConnectionException(message.makeStringAndClear()) ;
     157             :     }
     158             : 
     159           0 :     if (mLdapDefinition.mPort == 0) mLdapDefinition.mPort = LDAP_PORT;
     160             : 
     161             : #ifdef WNT
     162             :     mConnection = ldap_initW((PWCHAR) mLdapDefinition.mServer.getStr(),
     163             :                             mLdapDefinition.mPort) ;
     164             : #else
     165             :     mConnection = ldap_init(OUStringToOString( mLdapDefinition.mServer, RTL_TEXTENCODING_UTF8 ).getStr(),
     166           0 :                             mLdapDefinition.mPort) ;
     167             : #endif
     168           0 :     if (mConnection == NULL)
     169             :     {
     170           0 :         OUStringBuffer message ;
     171             : 
     172           0 :         message.appendAscii("Cannot initialise connection to LDAP server ") ;
     173           0 :         message.append(mLdapDefinition.mServer) ;
     174           0 :         message.appendAscii(":") ;
     175           0 :         message.append(mLdapDefinition.mPort) ;
     176           0 :         throw ldap::LdapConnectionException(message.makeStringAndClear());
     177             :     }
     178           0 : }
     179             : 
     180           0 :  void LdapConnection::getUserProfile(
     181             :      const OUString& aUser, LdapData * data)
     182             :     throw (lang::IllegalArgumentException,
     183             :             ldap::LdapConnectionException, ldap::LdapGenericException)
     184             : {
     185             :     OSL_ASSERT(data != 0);
     186           0 :     if (!isValid()) { connectSimple(); }
     187             : 
     188           0 :     OUString aUserDn =findUserDn( aUser );
     189             : 
     190           0 :     LdapMessageHolder result;
     191             : #ifdef WNT
     192             :     LdapErrCode retCode = ldap_search_sW(mConnection,
     193             :                                       (PWCHAR) aUserDn.getStr(),
     194             :                                       LDAP_SCOPE_BASE,
     195             :                                       const_cast<PWCHAR>( L"(objectclass=*)" ),
     196             :                                       0,
     197             :                                       0, // Attributes + values
     198             :                                       &result.msg) ;
     199             : #else
     200             :     LdapErrCode retCode = ldap_search_s(mConnection,
     201             :                                       OUStringToOString( aUserDn, RTL_TEXTENCODING_UTF8 ).getStr(),
     202             :                                       LDAP_SCOPE_BASE,
     203             :                                       "(objectclass=*)",
     204             :                                       0,
     205             :                                       0, // Attributes + values
     206           0 :                                       &result.msg) ;
     207             : #endif
     208           0 :     checkLdapReturnCode("getUserProfile", retCode,mConnection) ;
     209             : 
     210             :     BerElement * ptr;
     211             : #ifdef WNT
     212             :     PWCHAR attr = ldap_first_attributeW(mConnection, result.msg, &ptr);
     213             :     while (attr) {
     214             :         PWCHAR * values = ldap_get_valuesW(mConnection, result.msg, attr);
     215             :         if (values) {
     216             :             const OUString aAttr( reinterpret_cast<sal_Unicode*>( attr ) );
     217             :             const OUString aValues( reinterpret_cast<sal_Unicode*>( *values ) );
     218             :             data->insert(
     219             :                 LdapData::value_type( aAttr, aValues ));
     220             :             ldap_value_freeW(values);
     221             :         }
     222             :         attr = ldap_next_attributeW(mConnection, result.msg, ptr);
     223             : #else
     224           0 :     char * attr = ldap_first_attribute(mConnection, result.msg, &ptr);
     225           0 :     while (attr) {
     226           0 :         char ** values = ldap_get_values(mConnection, result.msg, attr);
     227           0 :         if (values) {
     228             :             data->insert(
     229             :                 LdapData::value_type(
     230             :                     OStringToOUString(attr, RTL_TEXTENCODING_ASCII_US),
     231           0 :                     OStringToOUString(*values, RTL_TEXTENCODING_UTF8)));
     232           0 :             ldap_value_free(values);
     233             :         }
     234           0 :         attr = ldap_next_attribute(mConnection, result.msg, ptr);
     235             : #endif
     236           0 :     }
     237           0 : }
     238             : 
     239           0 :  OUString LdapConnection::findUserDn(const OUString& aUser)
     240             :     throw (lang::IllegalArgumentException,
     241             :             ldap::LdapConnectionException, ldap::LdapGenericException)
     242             : {
     243           0 :     if (!isValid()) { connectSimple(); }
     244             : 
     245           0 :     if (aUser.isEmpty())
     246             :     {
     247             :         throw lang::IllegalArgumentException(
     248             :             "LdapConnection::findUserDn -User id is empty",
     249           0 :                 NULL, 0) ;
     250             :     }
     251             : 
     252             : 
     253             : 
     254           0 :     OUStringBuffer filter( "(&(objectclass=" );
     255             : 
     256           0 :     filter.append( mLdapDefinition.mUserObjectClass ).append(")(") ;
     257           0 :     filter.append( mLdapDefinition.mUserUniqueAttr ).append("=").append(aUser).append("))") ;
     258             : 
     259           0 :     LdapMessageHolder result;
     260             : #ifdef WNT
     261             :     PWCHAR attributes [2] = { const_cast<PWCHAR>( L"1.1" ), NULL };
     262             :     LdapErrCode retCode = ldap_search_sW(mConnection,
     263             :                                       (PWCHAR) mLdapDefinition.mBaseDN.getStr(),
     264             :                                       LDAP_SCOPE_SUBTREE,
     265             :                                       (PWCHAR) filter.makeStringAndClear().getStr(), attributes, 0, &result.msg) ;
     266             : #else
     267           0 :     sal_Char * attributes [2] = { const_cast<sal_Char *>(LDAP_NO_ATTRS), NULL };
     268             :     LdapErrCode retCode = ldap_search_s(mConnection,
     269             :                                       OUStringToOString( mLdapDefinition.mBaseDN, RTL_TEXTENCODING_UTF8 ).getStr(),
     270             :                                       LDAP_SCOPE_SUBTREE,
     271           0 :                                       OUStringToOString( filter.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ).getStr(), attributes, 0, &result.msg) ;
     272             : #endif
     273           0 :     checkLdapReturnCode("FindUserDn", retCode,mConnection) ;
     274           0 :     OUString userDn ;
     275           0 :     LDAPMessage *entry = ldap_first_entry(mConnection, result.msg) ;
     276             : 
     277           0 :     if (entry != NULL)
     278             :     {
     279             : #ifdef WNT
     280             :         PWCHAR charsDn = ldap_get_dnW(mConnection, entry) ;
     281             : 
     282             :         userDn = OUString( reinterpret_cast<const sal_Unicode*>( charsDn ) );
     283             :         ldap_memfreeW(charsDn) ;
     284             : #else
     285           0 :         sal_Char *charsDn = ldap_get_dn(mConnection, entry) ;
     286             : 
     287           0 :         userDn = OStringToOUString( charsDn, RTL_TEXTENCODING_UTF8 );
     288           0 :         ldap_memfree(charsDn) ;
     289             : #endif
     290             :     }
     291             :     else
     292             :     {
     293             :         OSL_FAIL( "LdapConnection::findUserDn-could not get DN for User ");
     294             :     }
     295             : 
     296           0 :     return userDn ;
     297             : }
     298             : 
     299             : 
     300             : 
     301             : } } } // extensions.config.ldap
     302             : 
     303             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10