ubuntu环境下往ldap添加条目,通过C语言调用api

有点乱这个东西

大致意思是,在ubuntu上安装ldap,通过ldapadmin管理ldap,调用linux环境下c语言api实现对ldap条目的crud。

已经搭建的环境下,并且创建并添加了一些条目。

 

初始化、绑定、对要添加的数据处理、添加、断开LDAP连接。

#include <stdio.h>
#include <cstdlib>
//#include <iostream>
//using namespace std;
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;
    //BerElement *ber;
 
    char *a; 
    char **vals;
    int i, rc; 
    int i_version = LDAP_VERSION3;
 
 
    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");
    
    // 创建用户的属性
    //dn: uid=690106015133,ou=Employees,ou=People,dc=upm,dc=nodomain
    const char *dn = "uid=888888,ou=Employees,ou=People,dc=upm,dc=nodomain";  
    
    char* object_class[] = {"person", "inetOrgPerson", NULL};  
    char* cn = "John Doe";
    char* sn = "Doe";
    char* userPassword = "666666";
    char* mail = "john.doe@example.com";
    char* uid = "88888888";
    char* attributes[] = {"cn", cn, "sn", sn, "userPassword", userPassword, "mail", mail, "uid", uid, NULL};
	
    LDAPMod object_class_mod = { LDAP_MOD_ADD, "objectClass", object_class };
    LDAPMod cn_mod = { LDAP_MOD_ADD, "cn", attributes };
    LDAPMod sn_mod = { LDAP_MOD_ADD, "sn", attributes + 1 };
    LDAPMod userPassword_mod = { LDAP_MOD_ADD, "userPassword", attributes + 2};
    LDAPMod mail_mod = { LDAP_MOD_ADD, "mail", attributes + 3 };
    LDAPMod uid_mod = {LDAP_MOD_ADD, "uid", attributes +  4};
    LDAPMod* mods[] = { &object_class_mod, &cn_mod, &sn_mod, &userPassword_mod, &mail_mod, &uid_mod, NULL };
	
    rc = ldap_add_ext_s(ld, dn, mods, NULL, NULL);

    if(rc == LDAP_SUCCESS)
    {
       // std::cout << "LDAP add operation succeeded." << std::endl;
        printf("成功添加\n");
    }
    else
    {
       // std::cout << "LDAP add operation failed: " << ldap_err2string(rc) << std::endl;
	printf("添加失败\n");
    }

    // 断开 LDAP 连接
    ldap_unbind_ext_s(ld, NULL, NULL);
   
 
    return true;
}
 
int main()
{
    auth();
    return 0;
}



编译注意要添加ldap的链接库 

如 gcc  test.cpp -o test -lldap  -llber

运行(./test)结果

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值