//Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/7dc83345-68a3-4f0f-9806-411aabc6b121/ldaphmo.c

//Warning: This code has been marked up for HTML

//*++======================================================================

// Program Name:

//

//

// File Name:        ldaphmo.c

//

// Version:

//

// Author:

//

// Abstract:

//

// Notes:

//

// Revision History:

//

// Copyright (C) 2001  Novell, Inc.  All Rights Reserved.

//

// No part of this file may be duplicated, revised, translated, localized,

// or modified in any manner or compiled, linked or uploaded or downloaded

// to or from any computer system without the prior written consent of

// Novell, Inc.

//

//=====================================================================--*/

               
/******************************************************************************/

class='cKeyword'>#define N_PLAT_NLM
class='cKeyword'>#include <os_dep.h>
class='cKeyword'>#include <procdefs.h>
class='cKeyword'>#include <ncssdk.h>
class='cKeyword'>#include <stdio.h>
class='cKeyword'>#include <stdlib.h>
typedef unsigned short wchar_t;
class='cKeyword'>#define _WCHAR_T_DEFINED

class='cKeyword'>#include <ldap.h>
class='cKeyword'>#include <hmo.h>

extern struct ScreenStruct *CScreen;

void *hmoHandle;

UINT32 testmain( int numParams, UINT8 *pList[]);

UINT8 *nameList[2] ={MSG("VENDORNAME", 24), NULL};
UINT32 debugFail;


UINT32 StartHmoApp( void){
   UINT32 retVal;
   UINT8 *ParamList[5];
   
   hmoHandle = NULL;

   ParamList[0] = MSG("HOST", 25);
   ParamList[1] = MSG("PORT", 26);
   ParamList[2] = MSG("DN", 27);
   ParamList[3] = MSG("PSW", 28);
   debugFail = 0;               // for debugging only!!!


   retVal = HmoRegister( &hmoHandle, testmain, testmain, 4, ParamList);
   OutputToScreen( 0, MSG("HMOREG=%x ret=%d\r\n", 29), hmoHandle, retVal);   
   if( retVal != HMO_OK){
      OutputToScreen( CScreen, MSG("Failed to Launch HMO. Err=\r\n", 30), retVal);
   }
   return retVal;
}

UINT32 testmain( int numParams, UINT8 *pList[]){

    int         version, ldapPort, i, rc; 
    class='cKeyword'>char        *attribute;
    class='cKeyword'>char        **values;       
    struct      timeval timeOut;
    BerElement  *ber;
    LDAP        *ld;
    class='cKeyword'>char        *ldapHost, *sldapPort, *loginDN, *password;
    LDAPMessage *searchResult = NULL, *dse;    

   if( numParams != 4){
      return 1;
   }
   
   debugFail++;
   
   if( debugFail > 2){
      return 1;
   }
   ldapHost = pList[0];
   sldapPort = pList[1];
   loginDN = pList[2];
   password = pList[3];

    ldapPort          = atoi( sldapPort);
    timeOut.tv_sec    = 10L;
    timeOut.tv_usec   = 0L;

    /* Set LDAP version to 3 */
    version = LDAP_VERSION3;
    ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version);

    /* Initialize the LDAP session */
    if (( ld = ldap_init( ldapHost, ldapPort )) == NULL)
    {
        OutputToScreen( CScreen, MSG("\n\tLDAP session initialization failed\n", 31));
        return( 1 );
    }
    OutputToScreen( CScreen, MSG("\n\tLDAP session initialized\n", 32));

    /* Bind to the server */
    rc = ldap_simple_bind_s( ld, loginDN, password );
    if (rc != LDAP_SUCCESS )
    {
        OutputToScreen( CScreen, MSG("ldap_simple_bind_s: %s\n", 33), ldap_err2string( rc ));
        ldap_unbind_s ( ld );
        return( 1 );
    }
    OutputToScreen( CScreen, MSG("\n\tBind successful\n", 34));

    /*
     * To search for rootDSE,
     *  1. Must be set for LDAP v3 before binding.
     *  2. Set search base = NULL or empty string
     *  3. Set search filter = (objectclass=*)
     *  4. Set search scope = LDAP_SCOPE_BASE
     */
    rc = ldap_search_ext_s(
                ld,                 /* LDAP session handle */
                NULL,               /* search base */
                LDAP_SCOPE_BASE,    /* scope */ 
                MSG("(objectclass=*)", 35),  /* search filter */
                nameList,       /* just vendor name */
                0,                  /* return both attrs and values */
                NULL,               /* server controls */
                NULL,               /* client controls */
                &timeOut,           /* time out */
                LDAP_NO_LIMIT,      /* no limit on entries */
                &searchResult );    /* returned search results */

    if ( rc != LDAP_SUCCESS )
    {
        OutputToScreen( CScreen, MSG("ldap_search_ext_s: %s\n", 36), ldap_err2string( rc ));          
        ldap_unbind_s( ld );
        return( 1 );
    }


    /* We have only one entry - root dse in search result */
    dse = ldap_first_entry( ld, searchResult );  

    /* print out all the attributes and attribute values */
    OutputToScreen( CScreen, MSG("\n\tRoot DSE", 37));
    OutputToScreen( CScreen, MSG("\n\t-----------------------------------------\n", 38));
    for (   attribute = ldap_first_attribute( ld, searchResult, &ber );
            attribute != NULL; 
            attribute = ldap_next_attribute( ld, searchResult, ber ))
    {
        if (( values = ldap_get_values( ld, dse, attribute)) != NULL )
        {
            for ( i = 0; values[ i ] != NULL; i++ )
                OutputToScreen( CScreen, MSG("\t%s: %s\n", 39), attribute, values[ i ] );

            ldap_value_free( values );
        }
        ldap_memfree( attribute );
    }                     

    ber_free(ber, 0);
    ldap_msgfree( searchResult );
    ldap_unbind_s( ld );   

    return( 0 );
}


void StopHmoApp( void){
OutputToScreen( 0, MSG("StopHmoApp called %x\r\n", 40), hmoHandle);   

   HmoDeregister( hmoHandle);
}