打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C语言操作OpenLDAP
userphoto

2016.09.09

关注

参考:http://www.cnblogs.com/zhumao/archive/2005/08/13/214258.html

步骤:

包括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。执行操作。

 

连接例子:

  1. #include <stdio.h>    
  2. #include "ldap.h"    
  3.     
  4. /* Adjust these setting for your own LDAP server */    
  5. #define HOSTNAME "192.168.101.205"    
  6. #define PORT_NUMBER 389    
  7. #define FIND_DN "dc=test,dc=com"    
  8.     
  9. int    
  10. main( int argc, char **argv )    
  11. {    
  12.     LDAP *ld;    
  13.     LDAPMessage *result, *e;    
  14.     BerElement *ber;    
  15.     
  16.     char *a;    
  17.     char **vals;    
  18.     int i, rc;    
  19.     int i_version = 3;    
  20.     
  21.     // set protocal version    
  22.     ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &i_version);    
  23.     ldap_set_option(NULL, LDAP_OPT_REFERRALS, LDAP_OPT_ON);    
  24.     
  25.     /* Get a handle to an LDAP connection. */    
  26.     if ( (ld = ldap_init( HOSTNAME, PORT_NUMBER )) == NULL )     
  27.     {    
  28.     
  29.         perror( "ldap_init" );    
  30.         return( 1 );    
  31.     
  32.     }    
  33.     printf( "ldap_init success\n" );    
  34.     
  35.     /* Bind anonymously to the LDAP server. */    
  36.     rc = ldap_simple_bind_s( ld, NULL, NULL );    
  37.     if ( rc != LDAP_SUCCESS )     
  38.     {    
  39.     
  40.         fprintf(stderr, "ldap_simple_bind_s: rc: %d, %s\n", rc, ldap_err2string(rc));    
  41.         return( 1 );    
  42.     
  43.     }    
  44.     printf( "ldap_simple_bind_s success\n" );    
  45.     
  46.     /* Search for the entry. */    
  47.     if ( ( rc = ldap_search_ext_s( ld, FIND_DN, LDAP_SCOPE_BASE,    
  48.         "(objectclass=*)", NULL, 0, NULL, NULL, LDAP_NO_LIMIT,    
  49.         LDAP_NO_LIMIT, &result ) ) != LDAP_SUCCESS )     
  50.     {    
  51.     
  52.         fprintf(stderr, "ldap_search_ext_s: rc: %d, %s\n", rc, ldap_err2string(rc));    
  53.         return( 1 );    
  54.     }    
  55.     printf( "ldap_search_ext_s success\n" );    
  56.     
  57.     /* Since we are doing a base search, there should be only  
  58.     one matching entry. */    
  59.     e = ldap_first_entry( ld, result );    
  60.     if ( e != NULL )     
  61.     {    
  62.         printf( "\nFound %s:\n\n", FIND_DN );    
  63.         /* Iterate through each attribute in the entry. */    
  64.         for ( a = ldap_first_attribute( ld, e, &ber );    
  65.             a != NULL; a = ldap_next_attribute( ld, e, ber ) )     
  66.         {    
  67.             /* For each attribute, print the attribute name and values. */    
  68.             if ((vals = ldap_get_values( ld, e, a)) != NULL )     
  69.             {    
  70.                 for ( i = 0; vals[i] != NULL; i++ )     
  71.                 {    
  72.                     printf( "%s: %s\n", a, vals[i] );    
  73.                 }    
  74.                 ldap_value_free( vals );    
  75.             }    
  76.             ldap_memfree( a );    
  77.         }    
  78.     
  79.         if ( ber != NULL )     
  80.         {    
  81.             ber_free( ber, 0 );    
  82.         }    
  83.     }    
  84.     
  85.     ldap_msgfree( result );    
  86.     ldap_unbind( ld );    
  87.     return( 0 );    
  88. }     

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的支持不够充分。

获取namingContexts属性:

参考:https://wiki.mozilla.org/Mozilla_LDAP_SDK_Programmer%27s_Guide/Getting_Server_Information_With_LDAP_C_SDK

如果是windows ldap,使用上述代码可以查出namingContexts属性,但不能查出openldap的namingContexts属性,这时候代码要稍作修改,ldap_search_ext_s查询时指定namingContexts属性:

  1. char *attrs[2];  
  2. attrs[0] = "namingContexts";  
  3. attrs[1] = NULL;  
  4. rc = ldap_search_ext_s(ld, NULL, LDAP_SCOPE_BASE, "(objectClass=*)",   
  5.         attrs, 0, NULL, NULL, NULL,-1, &result);  




本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
LDAP应用技术简述
LDAP API
ldap samba_大哥
windows下搭建并配置OpenLDAP服务器
有关 JavaScript 的 10 件让人费解的事情
常见LDAP错误
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服