c语言Ldap查询的真实例子[站长原创]

jackxiang 2008-4-10 15:05 | |
#include <stdlib.h>
#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="10.210.72.141";
  port = 389;
  //联接服务器
  printf("hello the world");
  
  if( (ld = ldap_open(server, port ))  == NULL )
  {
    printf("NO CONNECT");
    exit( 1 );
  }else{
      printf("ldap_open successful\n");  
     }
  version = LDAP_VERSION2;
  ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,&version);
  if(ldap_simple_bind_s(ld,"cn=Directory Manager","1234qwer")!=LDAP_SUCCESS)
  {
    ldap_perror( ld, "ldap_simple_bind_s by xiangdong2" );
    exit( 1 );
  }else{
      printf("Ldap_bind succesful\n");  
     }
  sdn="dc=sina,dc=net";
  if (ldap_search_s(ld,sdn,LDAP_SCOPE_SUBTREE,"(entDefaultDomain=testquanjun1.sina.net)",NULL,0,&res)
      != LDAP_SUCCESS)
  {
    ldap_perror(ld,"ldap_search_s");
    exit(1);
  }else{
      printf("ldap_search_s is successful\n");  
       }

  for(e=ldap_first_entry(ld,res);e!=NULL;e=ldap_next_entry(ld,res))
  {
    //取出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);
}














作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/995/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2008-4-10 15:10
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]