用C语言操作LDAP服务器[1]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 sagely 的 blog

   毕竟用php操作ldap有局限性,因为当我们用生成证书的函数生成证书以后不可能再用php去给ldap增加条目,所以最近研究了一下c语言操作ldap,希望能对大家有点借鉴意义,有错误的地方还请原谅。至于如何安装,运行和测试ldap服务器请看http://www.infosecurity.org.cn/forum/read.php?fid=12&tid=47&fpage=1
毕竟用php操作ldap有局限性,因为当我们用生成证书的函数生成证书以后不可能再用php去给ldap增加条目,所以最近研究了一下c语言操作ldap,希望能对大家有点借鉴意义,有错误的地方还请原谅。至于如何安装,运行和测试ldap服务器请看http://www.infosecurity.org.cn/forum/read.php?fid=12&tid=47&fpage=1

一 初始化ldap库
#include
#include
ld=ldap_init(ldap_host,ldap_port)
假如没有进行端口修改的话,用默认的ldap_port就可以了,在ldap.h中定义为389

二 绑定ldap服务器
if(ldap_bind_s(ld,user_dn,user_pw,authmethod)!=ldap_success)
authmethod是使用的验证方法,一般为ldap_auth_simple,至于user_dn,user_pw就是用户和密码拉
可以使用ldap_unbind_s(ldap *ld)来关闭绑定。

三 执行查询
ldap_search_s(ldap ld,char *base_dn,int scope ,char *filter ,char *attrs_reqired[],int attributesonly,ldapmessage **result)
base_dn是指向查询开始处对象的指针,它可以作为数的顶端,或者某一个低的点
scope有三个,为
ldap_scope_base,只对基点dn指定的对象进行搜索
ldap_scope_onelevel,可以搜索处理基点dn指向对象以及树中低于基点对象一级的所有对象
ldap_scope_subtree,可以搜索树中该对象及以下的所有对象
filter是过滤器,这里有详细的解释http://www.infosecurity.org.cn/forum/read.php?fid=12&tid=49&fpage=1
attrs_required是一个应该返回的null的属性终止数组,可以只返回几个属性(如就dn和uid),指定null时将返回所有属性
attributesonly设置为1,只返回属性的类型,通常设定为0,返回属性类型和值。
成功返回ldap_success,否则返回出错代码。
例子:ldap_search_s(ld,base_dn,ldap_scope_subtree,null,null,0,&ldap_message_set)
稍后必须释放ldapmessage **result,int ldap_msgfree(ldapmessage *msg)
查询过后,我们就需要得到我们具体的感兴趣的东西
ldap_count_entries(ld,ldap_message_set)获取搜索得到的条目个数
ldap_first_entry(ld,ldap_message_set)获取第一条
ldap_next_entry(ld,ldap_one_message);获取下一条
ldap_first_attribute(ld,ldap_one_message,&ber_element_ptr);获取第一条目的属性
values= ldap_get_values(ld,ldap_one_message,attribute)获取该属性的值
ldap_value_free(values);
ldap_next_attribute(ld,ldap_one_message,ber_element_ptr);获取下一属性
可能看这么多函数有点眼花,那我们来看个例子:
res=ldap_search_s(ld,base_dn,ldap_scope_subtree,null,null,0,&ldap_message_set);
if(res!=ldap_success)
  {
  ldap_perror(ld,"failure of ldap_search_s");
  exit(0);
  }

printf("there were %d objects found\n",ldap_count_entries(ld,ldap_message_set));
//获取第一个条目
ldap_one_message=ldap_first_entry(ld,ldap_message_set);
while(ldap_one_message)
  {
  char *dn_str;
  //获取条目的dn
  dn_str=ldap_get_dn(ld,ldap_one_message);
  printf("found dn %s\n",dn_str);
  free(dn_str);
  //获取条目的属性
  attribute=ldap_first_attribute(ld,ldap_one_message,&ber_element_ptr);
  //获取每个属性的具体值
  while(attribute!=null)
    {
    if((values=ldap_get_values(ld,ldap_one_message,attribute))!=null)
    {
    for(i=0;values!=null;i++)
      {
        printf("%s:%s\n",attribute,values);
      }
    ldap_value_free(values);
    }
    //继续下一属性
    attribute=ldap_next_attribute(ld,ldap_one_message,ber_element_ptr);
    }
  //继续下一条目
  ldap_one_message=ldap_next_entry(ld,ldap_one_message);
  printf("\n");
  }
  //释放查询信息
(void)ldap_msgfree(ldap_message_set);
由上面的例子可以清楚的看出,可以查询出所有的搜索搜索结果的属性以及属性值

四:增加条目
增加条目需要干的事情就是构造一个ldapmod结构,它包括对象单个属性的构造块
typedef struct ldapmod
{
int mod_op;
char *mod_type;
union
{
  char **modv_strvals;
  struct berval **modv_bvals;
}mod_vals;
struct ldapmod *mod_next;
}ldapmod;
#define mod_values mod_vals,modv_strvals
#define mod_bvalues mod_vals.modv_bvals
可能你看起来很复杂,那我给你看个例子你就能找到中间的规律拉。
1,   添加一个国家的条目ldapmod
char *c_vals[]={"cn",null};
ldapmod c_attribute;
c_attribute.mod_op=ldap_mod_add;
c_attribute.mod_type="c";
c_attribute.mod_values=c_vals;

本文关键:用C语言操作LDAP服务器
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top