C语言操作OpenLDAP
2013-10-10 15:21
302 查看
参考:/article/5905929.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。执行操作。
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的支持不够充分。
如果是windows ldap,使用上述代码可以查出namingContexts属性,但不能查出openldap的namingContexts属性,这时候代码要稍作修改,ldap_search_ext_s查询时指定namingContexts属性:
步骤:
包括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 "192.168.101.205" #define PORT_NUMBER 389 #define FIND_DN "dc=test,dc=com" int main( int argc, char **argv ) { LDAP *ld; LDAPMessage *result, *e; BerElement *ber; char *a; char **vals; int i, rc; int i_version = 3; // set protocal version ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &i_version); ldap_set_option(NULL, LDAP_OPT_REFERRALS, LDAP_OPT_ON); /* Get a handle to an LDAP connection. */ if ( (ld = ldap_init( HOSTNAME, PORT_NUMBER )) == NULL ) { perror( "ldap_init" ); return( 1 ); } printf( "ldap_init success\n" ); /* Bind anonymously to the LDAP server. */ rc = ldap_simple_bind_s( ld, NULL, NULL ); if ( rc != LDAP_SUCCESS ) { fprintf(stderr, "ldap_simple_bind_s: rc: %d, %s\n", rc, ldap_err2string(rc)); return( 1 ); } printf( "ldap_simple_bind_s success\n" ); /* 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: rc: %d, %s\n", rc, ldap_err2string(rc)); return( 1 ); } printf( "ldap_search_ext_s success\n" ); /* 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的支持不够充分。
获取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属性:
char *attrs[2]; attrs[0] = "namingContexts"; attrs[1] = NULL; rc = ldap_search_ext_s(ld, NULL, LDAP_SCOPE_BASE, "(objectClass=*)", attrs, 0, NULL, NULL, NULL,-1, &result);
相关文章推荐
- C语言操作OpenLDAP
- Ubuntu下C语言实现以子进程号命名的文件及文件夹的新建、删除等操作。
- c语言六种位操作详解
- C语言 文件操作10--配置文件读写
- c语言实现--带头结点单链表操作
- C语言实现二叉树的基本操作
- C语言文件操作详解
- C语言中的指针操作
- C语言零移位操作
- C语言实现单链表的基本操作及其部分面试题
- 顺序栈——9种基本操作和实现(C语言)
- C语言一个关于文件操作的示例
- 链表的基本操作[C语言]
- C语言中常用的文件操作函数
- 栈(C语言实现)基本操作
- c语言文件读写示例(c语言文件操作)
- 有关c语言文件操作
- C语言和Java中的文件操作对比
- 用C语言操作MySQL数据库-通用版
- C语言内存操作函数和一般字符串操作函数