zoukankan      html  css  js  c++  java
  • C语言操作OpenLDAP zhumao

    包括openldap,netscape(sun),mozilla, novell,ibm等,都提供了LDAP的C SDK和接口函数。作为RFC标准的LDAP结构,struct LDAP是定义为对用户隐藏的,并在各个实现函数中各自定义,以便适应不同的LDAP访问。

    #typedef struct ldap LDAP在ldap.h中定义;在2.0版以后,struct LDAP改为隐藏,只能能过函数ldap_set_option 和ldap_get_option访问。(draft-ldapext-ldap-c-api-xx.txt)

    使用时:

    如:

    LDAP *Ld=null;  //声明并初始化LDAP类型的变量指针 *ld;

    Ld       =ldap_init( ldaphost, ldapport );  //获取LDAP的会话;

     

    获得会话后,调用ldap_simple_bind_s获得访问LDAP的权限,然后就可以调用不同的函数,如

    ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,

                 sctrls, cctrls, timeout, sizelimit, &msgid );

    即可完成相关的操作。

    即步骤为:1。获得会话;2。绑定对象;3。执行操作。

     

    连接例子:

    #include <stdio.h>

    #include "ldap.h"

    /* Adjust these setting for your own LDAP server */

    #define HOSTNAME "localhost"

    #define PORT_NUMBERLDAP_PORT

    #define FIND_DN "uid=bjensen, ou=People, o=Airius.com"

    int

    main( int argc, char **argv )

    {

    LDAP*ld;

    LDAPMessage*result, *e;

    BerElement*ber;

    char*a;

    char**vals;

    int i, rc;

    /* Get a handle to an LDAP connection. */

    if ( (ld = ldap_init( HOSTNAME, PORT_NUMBER )) == NULL ) {

    perror( "ldap_init" );

    return( 1 );

    }

    /* Bind anonymously to the LDAP server. */

    rc = ldap_simple_bind_s( ld, NULL, NULL );

    if ( rc != LDAP_SUCCESS ) {

    fprintf(stderr, "ldap_simple_bind_s: %s\n", ldap_err2string(rc));

    return( 1 );

    }

    /* Search for the entry. */

    if ( ( rc = ldap_search_ext_s( ld, FIND_DN, LDAP_SCOPE_BASE,

    "(objectclass=*)", NULL, 0, NULL, NULL, LDAP_NO_LIMIT,

    LDAP_NO_LIMIT, &result ) ) != LDAP_SUCCESS ) {

    fprintf(stderr, "ldap_search_ext_s: %s\n", ldap_err2string(rc));

    return( 1 );

    }

    /* Since we are doing a base search, there should be only

    one matching entry. */

    e = ldap_first_entry( ld, result );

    if ( e != NULL ) {

    printf( "\nFound %s:\n\n", FIND_DN );

    /* Iterate through each attribute in the entry. */

    for ( a = ldap_first_attribute( ld, e, &ber );

    a != NULL; a = ldap_next_attribute( ld, e, ber ) ) {

    /* For each attribute, print the attribute name and values. */

    if ((vals = ldap_get_values( ld, e, a)) != NULL ) {

    for ( i = 0; vals[i] != NULL; i++ ) {

    printf( "%s: %s\n", a, vals[i] );

    }

    ldap_value_free( vals );

    }

    ldap_memfree( a );

    }

    if ( ber != NULL ) {

    ber_free( ber, 0 );

    }

    }

    ldap_msgfree( result );

    ldap_unbind( ld );

    return( 0 );

    }

             i.     Novell函数库:

    Novel提供了基于普通LDAP函数库的扩展,主要包括两个部分:针对Novel eDirectory服务器产品的扩展,其次是对如ldapsearch等常用函数的扩展。详情可从:http://developer.novell.com/ndk/qstart/opensource.htm#ldapc  获得帮助;

              ii.     Netscape函数库;

    Netscape一度是企业级目录服务提供者,许多LDAP的C例子,实际上都是基于Netscape服务器的。但在Netscape被收购后,其目录服务成了iPlanet和SUN eDirectory产品的一部分,出于支持JAVA和iplanet产品的缘故,SUN对该产品和相关库的支持远不够积极,特别是对linux的支持不够充分,估计也与保护solaris产品有关。

            iii. Mozilla函数库:

    Mozilla可以看作是Netscape的另一个分支。准确地说,Netscape本来就是源于Mozilla。Mozilla是也是一个开源的项目,提供完整的C-SDK,缺点是对linux的支持不够充分。

  • 相关阅读:
    寒假Day37:设计模式(封装+继承+多态等)
    INF ClassInstall32 Section详解
    VS2008编译的程序运行提示“由于应用程序配置不正确,应用程序未能启动”
    INF Models Section
    INF DDInstall.Services Section
    INF ClassInstall32.Services Section详解
    INF DDInstall Section
    INF SourceDisksNames Section 和 SourceDisksFiles Section详解
    sys文件查看DbgPrint函数打印的信息
    IRP(I/O Request Package)详解
  • 原文地址:https://www.cnblogs.com/zhumao/p/214258.html
Copyright © 2011-2022 走看看