查询的
#include "ldap.h"
#include "stdio.h"
int main()
{
LDAP *ld;
LDAPMessage *res,*e;
int i,version;
char *server;
int *port;
char *dn;
char *a;
BerElement *ptr;
char **vals;
char **ppValue = NULL;
char *sdn;
server="192.168.1.17";
port = 389;
//联接服务器
if( (ld = ldap_open(server, port )) == NULL )
{
printf("NO CONNECT");
exit( 1 );
}
//设置服务器版本
version = LDAP_VERSION3;
ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,&version);
//绑定服务器
if(ldap_simple_bind_s(ld,"cn=root,dc=starxing,dc=com","secret")!=LDAP_SUCCESS)
{
ldap_perror( ld, "ldap_simple_bind_s" );
exit( 1 );
}
//设置查询的根目录
sdn="dc=starxing,dc=com";
//进行同步查询
if (ldap_search_s(ld,sdn,LDAP_SCOPE_SUBTREE,"(objectclass=*)",NULL,0,&res)
!= LDAP_SUCCESS)
{
ldap_perror(ld,"ldap_search_s");
exit(1);
}
//对条目进行逐条分析
for(e=ldap_first_entry(ld,res);e!=NULL;e=ldap_next_entry(ld,e))
{
//取出DN
dn=ldap_get_dn(ld,e);
printf("dn: %s \n",dn);
ldap_memfree( dn );
//对条目的属性进行逐条分析
for ( a = ldap_first_attribute( ld, e, &ptr );a != NULL;a = ldap_next_attribute( ld, e, ptr ) )
{
printf( " %s: ",a );
vals = ldap_get_values( ld, e, a );
for ( i = 0; vals[i] != NULL; i++ ) {
printf(" %s ",vals[i]);
}
printf("\n");
ldap_value_free( vals );
}
printf("\n");
}
ldap_msgfree(res);
ldap_unbind(ld);
}
插入的
#include "ldap.h"
#include "stdio.h"
int main()
{
LDAP *ld;
char *sdn = "cn=qqq11,dc=starxing,dc=com";//要添加条目的DN
//列出要添加条目各个属性的值
char *cn_values[] = {"qqq11",NULL};
char *sn_values[] = {"qqq11",NULL};
char *userPassword_values[] = {"qqqq",NULL};
char *objectClass_values[] = {"person",NULL};
int version;
LDAPMod mod2 = {LDAP_MOD_ADD,"cn",cn_values};
LDAPMod mod1 = {LDAP_MOD_ADD,"sn",sn_values};
LDAPMod mod0 = {LDAP_MOD_ADD,"objectClass",objectClass_values};
LDAPMod mod3 = {LDAP_MOD_ADD,"userPassword",userPassword_values};
LDAPMod *lmod[] = {&mod0,&mod1,&mod2,&mod3,NULL};
//联接ldap服务器
if( (ld = ldap_open( "192.168.1.17", 389 )) == NULL ){
ldap_perror(ld,"ldapopen");
exit( 1 );
return 1;
}
//设置ldap版本
version = LDAP_VERSION3;
ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,&version);
//对ldap服务器进行绑定
if(ldap_simple_bind_s(ld,"cn=root,dc=starxing,dc=com","secret")!=LDAP_SUCCESS)
{
ldap_perror( ld, "ldap_simple_bind_s" );
exit( 1 );
return 1;
}
//进行同步绑定
if(ldap_add_s(ld,sdn,lmod)!=LDAP_SUCCESS){
ldap_perror(ld,"ldap_add_s error");
return( 1 );
}
ldap_unbind(ld);
return( 0 );
}
删除的
#include "ldap.h"
#include "stdio.h"
int main()
{
LDAP *ld;
int version;
char **ppValue = NULL;
//联接和绑定服务器
if( (ld = ldap_open( "192.168.1.17", 389 )) == NULL )
exit( 1 );
version = LDAP_VERSION3;
ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,&version);
if(ldap_simple_bind_s(ld,"cn=root,dc=starxing,dc=com","secret")!=LDAP_SUCCESS)
{
ldap_perror( ld, "ldap_simple_bind_s" );
exit( 1 );
}
//进行删除操作
if( ldap_delete_s(ld,"cn=qqq11,dc=starxing,dc=com") == -1)
{
ldap_perror(ld,"ldap_delete_s");
exit(1);
}
ldap_unbind(ld);
return 0;
}
修改的
#include "ldap.h"
#include <stdio.h>;
int main()
{
LDAP *ld;
char *sdn;
//设置要更改的值
char *sn_values[] = {"eeee",NULL};
char *homePhone_values[] = {"12345678",NULL};
int version;
LDAPMod mod1 = {LDAP_MOD_ADD,"homePhome",homePhone_values};
LDAPMod mod2 = {LDAP_MOD_REPLACE,"sn",sn_values};
LDAPMod mod3 = {LDAP_MOD_DELETE,"mail",NULL};
LDAPMod *lmod[4] = {&mod1,&mod2,&mod3,NULL};
//联接服务器和绑定服务器
if( (ld = ldap_open( "192.168.1.17", 389 )) == NULL )
exit( 1 );
version = LDAP_VERSION3;
ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,&version);
if(ldap_simple_bind_s(ld,"cn=root,dc=starxing,dc=com","secret")!=LDAP_SUCCESS)
{
ldap_perror( ld, "ldap_simple_bind_s" );
exit( 1 );
}
sdn = "cn=qqq11222,dc=starxing,dc=com";
//进行更改
if (ldap_modify_s(ld,sdn,lmod)!=LDAP_SUCCESS) {
ldap_perror( ld, "ldap_modify_s" );
return( 1 );
}
ldap_unbind(ld);
}
#include "ldap.h"
#include "stdio.h"
int main()
{
LDAP *ld;
LDAPMessage *res,*e;
int i,version;
char *server;
int *port;
char *dn;
char *a;
BerElement *ptr;
char **vals;
char **ppValue = NULL;
char *sdn;
server="192.168.1.17";
port = 389;
//联接服务器
if( (ld = ldap_open(server, port )) == NULL )
{
printf("NO CONNECT");
exit( 1 );
}
//设置服务器版本
version = LDAP_VERSION3;
ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,&version);
//绑定服务器
if(ldap_simple_bind_s(ld,"cn=root,dc=starxing,dc=com","secret")!=LDAP_SUCCESS)
{
ldap_perror( ld, "ldap_simple_bind_s" );
exit( 1 );
}
//设置查询的根目录
sdn="dc=starxing,dc=com";
//进行同步查询
if (ldap_search_s(ld,sdn,LDAP_SCOPE_SUBTREE,"(objectclass=*)",NULL,0,&res)
!= LDAP_SUCCESS)
{
ldap_perror(ld,"ldap_search_s");
exit(1);
}
//对条目进行逐条分析
for(e=ldap_first_entry(ld,res);e!=NULL;e=ldap_next_entry(ld,e))
{
//取出DN
dn=ldap_get_dn(ld,e);
printf("dn: %s \n",dn);
ldap_memfree( dn );
//对条目的属性进行逐条分析
for ( a = ldap_first_attribute( ld, e, &ptr );a != NULL;a = ldap_next_attribute( ld, e, ptr ) )
{
printf( " %s: ",a );
vals = ldap_get_values( ld, e, a );
for ( i = 0; vals[i] != NULL; i++ ) {
printf(" %s ",vals[i]);
}
printf("\n");
ldap_value_free( vals );
}
printf("\n");
}
ldap_msgfree(res);
ldap_unbind(ld);
}
插入的
#include "ldap.h"
#include "stdio.h"
int main()
{
LDAP *ld;
char *sdn = "cn=qqq11,dc=starxing,dc=com";//要添加条目的DN
//列出要添加条目各个属性的值
char *cn_values[] = {"qqq11",NULL};
char *sn_values[] = {"qqq11",NULL};
char *userPassword_values[] = {"qqqq",NULL};
char *objectClass_values[] = {"person",NULL};
int version;
LDAPMod mod2 = {LDAP_MOD_ADD,"cn",cn_values};
LDAPMod mod1 = {LDAP_MOD_ADD,"sn",sn_values};
LDAPMod mod0 = {LDAP_MOD_ADD,"objectClass",objectClass_values};
LDAPMod mod3 = {LDAP_MOD_ADD,"userPassword",userPassword_values};
LDAPMod *lmod[] = {&mod0,&mod1,&mod2,&mod3,NULL};
//联接ldap服务器
if( (ld = ldap_open( "192.168.1.17", 389 )) == NULL ){
ldap_perror(ld,"ldapopen");
exit( 1 );
return 1;
}
//设置ldap版本
version = LDAP_VERSION3;
ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,&version);
//对ldap服务器进行绑定
if(ldap_simple_bind_s(ld,"cn=root,dc=starxing,dc=com","secret")!=LDAP_SUCCESS)
{
ldap_perror( ld, "ldap_simple_bind_s" );
exit( 1 );
return 1;
}
//进行同步绑定
if(ldap_add_s(ld,sdn,lmod)!=LDAP_SUCCESS){
ldap_perror(ld,"ldap_add_s error");
return( 1 );
}
ldap_unbind(ld);
return( 0 );
}
删除的
#include "ldap.h"
#include "stdio.h"
int main()
{
LDAP *ld;
int version;
char **ppValue = NULL;
//联接和绑定服务器
if( (ld = ldap_open( "192.168.1.17", 389 )) == NULL )
exit( 1 );
version = LDAP_VERSION3;
ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,&version);
if(ldap_simple_bind_s(ld,"cn=root,dc=starxing,dc=com","secret")!=LDAP_SUCCESS)
{
ldap_perror( ld, "ldap_simple_bind_s" );
exit( 1 );
}
//进行删除操作
if( ldap_delete_s(ld,"cn=qqq11,dc=starxing,dc=com") == -1)
{
ldap_perror(ld,"ldap_delete_s");
exit(1);
}
ldap_unbind(ld);
return 0;
}
修改的
#include "ldap.h"
#include <stdio.h>;
int main()
{
LDAP *ld;
char *sdn;
//设置要更改的值
char *sn_values[] = {"eeee",NULL};
char *homePhone_values[] = {"12345678",NULL};
int version;
LDAPMod mod1 = {LDAP_MOD_ADD,"homePhome",homePhone_values};
LDAPMod mod2 = {LDAP_MOD_REPLACE,"sn",sn_values};
LDAPMod mod3 = {LDAP_MOD_DELETE,"mail",NULL};
LDAPMod *lmod[4] = {&mod1,&mod2,&mod3,NULL};
//联接服务器和绑定服务器
if( (ld = ldap_open( "192.168.1.17", 389 )) == NULL )
exit( 1 );
version = LDAP_VERSION3;
ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,&version);
if(ldap_simple_bind_s(ld,"cn=root,dc=starxing,dc=com","secret")!=LDAP_SUCCESS)
{
ldap_perror( ld, "ldap_simple_bind_s" );
exit( 1 );
}
sdn = "cn=qqq11222,dc=starxing,dc=com";
//进行更改
if (ldap_modify_s(ld,sdn,lmod)!=LDAP_SUCCESS) {
ldap_perror( ld, "ldap_modify_s" );
return( 1 );
}
ldap_unbind(ld);
}
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/987/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2008-4-7 16:52
评论列表