直接贴代码吧
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
extern "C"
{
#define LDAP_DEPRECATED 1
#include <ldap.h>
#include <lber.h>
}
#define HOST "192.168.99.99"
#define PORT 389
#define WHO "cn=admin,dc=nodomain"
#define PASSWD "secret"
#define FIND_DN "dc=nodomain"
bool auth(){
LDAP *ld;
char* a;
int rc, l_entries, i_version = LDAP_VERSION3;
int morePages, l_errcode = 0, page_nbr, l_entry_count =0;
LDAPControl *pageCtrl = NULL;
char pagingCriticality = 'T', *l_dn;
unsigned long totalCount;
ber_int_t count;
LDAPControl *pageControl=NULL, *M_controls[2] = { NULL, NULL }, **returnedControls = NULL;
LDAPMessage *l_result, *l_entry;
ld = ldap_init(HOST, PORT);
if(ld == NULL){
perror("ldap_init");
return false;
}
printf("ldap_init success\n");
ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &i_version);
ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON);
rc = ldap_simple_bind_s(ld, WHO, PASSWD);
if(rc != LDAP_SUCCESS){
fprintf(stderr, "ldap_simple_bind_s :rc :%d, &s \n", rc, ldap_err2string(rc));
return false;
}
printf("ldap_simple_bind_s success !\n");
//2
unsigned long pageSize = 100; //每页记录数
char* searchBase = "dc=upm,dc=nodomain"; //查询范围
char* filter = "(objectClass=person)"; //筛选条件
int searchResult;
LDAPControl **ctrls = NULL;
struct berval *cookie = NULL;
char *attrs[] = {"uid", "cn", "mail", "userPassword", NULL}; // 查询的属性
page_nbr = 1;
do
{
rc = ldap_create_page_control(ld, pageSize, cookie, pagingCriticality, &pageCtrl);
//插入控件 用来查找
M_controls[0] = pageCtrl;
//根据参数在目录中查找目标实体
rc = ldap_search_ext_s(ld, searchBase, LDAP_SCOPE_SUBTREE, filter, attrs, 0, M_controls, NULL, NULL, 0, &l_result);
//是否查找成功
if((rc != LDAP_SUCCESS) & (rc != LDAP_PARTIAL_RESULTS))
{
printf("==Error==");
printf(" Failure during a search. Return code is %d.\n",rc);
ldap_unbind(ld);
break;
}
//解析结果 用来检索返回的控件
rc = ldap_parse_result(ld, l_result, &l_errcode, NULL, NULL, NULL, &returnedControls, 0);
if (cookie != NULL)
{
ber_bvfree(cookie);
cookie = NULL;
}
//解析控件 返回给cookie 并确认
//rc = ldap_parse_page_control(ld, returnedControls, &totalCount, &cookie);
rc = ldap_parse_page_control(ld, returnedControls, &count, &cookie);
//确认cookie不为空 有很多页 ?
if (cookie && cookie->bv_val != NULL && (strlen(cookie->bv_val) > 0))
{
morePages = 1; //LDAP_TRUE
}
else
{
morePages = 0; //LDAP_FALSE
}
//清理用过的控件
if (returnedControls != NULL)
{
ldap_controls_free(returnedControls);
returnedControls = NULL;
}
M_controls[0] = NULL;
ldap_control_free(pageControl);
pageControl = NULL;
//显示返回的结果
//确认有多少实体被找到
if(morePages == 1)
printf("===page :%d ===\n", page_nbr);
l_entries = ldap_count_entries(ld, l_result);
if(l_entries > 0)
{
l_entry_count = l_entry_count + l_entries;
}
for ( l_entry = ldap_first_entry(ld, l_result);
l_entry != NULL;
l_entry = ldap_next_entry(ld, l_entry) )
{
l_dn = ldap_get_dn(ld, l_entry);
printf(" %s\n",l_dn);
}
/* Free the search results. */
ldap_msgfree(l_result);
page_nbr = page_nbr + 1;
}while(morePages == 1);
printf("\n %d entries found during the search\n===结束===\n",l_entry_count);
ber_bvfree(cookie);
cookie = NULL;
/* 断开与 LDAP 服务器的连接 */
ldap_unbind_ext_s(ld, NULL, NULL);
return true;
}
int main(){
bool flag = auth();
if(!flag){
printf("操作失败 !\n");
}
return 0;
}
分页查表,每页设置大小100
出现的一些问题
LDAP_TRUE
LDAP_FALSE
这些关键字都无法识别,不是没导头文件的原因,后面只能直接写死。因为它们分别表示的是1和0,在bool中分别表示true 和 false
参考文档:ldap_create_page_control、ldap_parse_result、ldap_parse_page_control