LDAP operation example

Source: Internet
Author: User
Tags ldap
Queried
# Include "LDAP. H"
# Include "stdio. H"
Int main ()
{
LDAP * LD;
Ldapmessage * res, * E;
Int I, version;
Char * server;
Int * port;
Char * DN;
Char *;
Berelement * PTR;
Char ** Vals;
Char ** ppvalue = NULL;
Char * SDN;
Server = "192.168.1.17 ";
Port = 389;
// Connect to the server
If (LD = ldap_open (server, Port) = NULL)
{
Printf ("no connect ";
Exit (1 );
}
// Set the server version
Version = ldap_version3;
Ldap_set_option (LD, ldap_opt_protocol_version, & version );
// Bind the server
If (ldap_simple_bind_s (LD, "cn = root, Dc = starxing, Dc = com", "secret "! = Ldap_success)
{
Ldap_perror (LD, "ldap_simple_bind_s ");
Exit (1 );
}
// Set the root directory of the query
SDN = "DC = starxing, Dc = com ";
// Perform synchronous Query
If (ldap_search_s (LD, SDN, ldap_scope_subtree, "(objectclass = *)", null, 0, & res)
! = Ldap_success)
{
Ldap_perror (LD, "ldap_search_s ";
Exit (1 );
}
// Analyze entries one by one
For (E = ldap_first_entry (LD, Res); e! = NULL; E = ldap_next_entry (LD, e ))
{
// Retrieve the DN
DN = ldap_get_dn (LD, e );
Printf ("DN: % s/n", DN );
Ldap_memfree (DN );
// Analyze the attributes of entries one by one
For (A = ldap_first_attribute (LD, E, & PTR);! = NULL; A = ldap_next_attribute (LD, E, PTR ))
{
Printf ("% s:", );
Vals = ldap_get_values (LD, E, );
For (I = 0; Vals ! = NULL; I ++ ){
Printf ("% s", Vals);
}
Printf ("/N ";
Ldap_value_free (Vals );
}
Printf ("/N ";
}
Ldap_msgfree (RES );
Ldap_unbind (LD );
}
Inserted
# Include "LDAP. H"
# Include "stdio. H"

Int main ()
{
LDAP * LD;
Char * SDN = "cn = qqq11, Dc = starxing, Dc = com"; // DN of the entry to be added
// List the values of each attribute of the entry to be added
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 };
// Connect to the LDAP server
If (LD = ldap_open ("192.168.1.17", 389) = NULL ){
Ldap_perror (LD, "ldapopen ";
Exit (1 );
Return 1;
}
// Set the LDAP Version
Version = ldap_version3;
Ldap_set_option (LD, ldap_opt_protocol_version, & version );
// Bind the LDAP server
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;
}
// Synchronous binding
If (ldap_add_s (LD, SDN, lmod )! = Ldap_success ){
Ldap_perror (LD, "ldap_add_s error ";
Return (1 );
}
Ldap_unbind (LD );
Return (0 );
}
Deleted
# Include "LDAP. H"
# Include "stdio. H"

Int main ()
{
LDAP * LD;
Int version;
Char ** ppvalue = NULL;
// Connect and bind the server
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 );
}
// Delete
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;
}
Modified
# Include "LDAP. H"
# Include <stdio. h>;

Int main ()
{
LDAP * LD;
Char * SDN;
// Set the value to be changed
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 };
// Connect the server and bind the server
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 = qqq000022, Dc = starxing, Dc = com ";
// Make changes
If (ldap_modify_s (LD, SDN, lmod )! = Ldap_success ){
Ldap_perror (LD, "ldap_modify_s ");
Return (1 );
}
Ldap_unbind (LD );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.