📄 ldapbrower1.cpp
字号:
#include <stdio.h>
#include "ldap.h"
/////
//#include <lber_types.h.in>
/* Specify the search criteria here. */
#define HOSTNAME1 "172.19.1.175"
//"172.19.1.175""127.0.0.1"
#define HOSTNAME "10.72.55.85"
#define PORTNUMBER 389
#define BASEDN "dc=huawei,dc=com"
#define SCOPE LDAP_SCOPE_SUBTREE
#define FILTER "(mail=*)"
#define NUM_MODS 10
//
typedef unsigned long ber_tag_t;
typedef unsigned long ber_len_t;
//////////
struct lber_options {
short lbo_valid;
unsigned short lbo_options;
int lbo_debug;
long lbo_meminuse;
};
///////////////////////////////////
struct berelement {
struct lber_options ber_opts;
#define ber_valid ber_opts.lbo_valid
#define ber_options ber_opts.lbo_options
#define ber_debug ber_opts.lbo_debug
/* Do not change the order of these 3 fields! see ber_get_next */
ber_tag_t ber_tag;
ber_len_t ber_len;
ber_tag_t ber_usertag;
char *ber_buf;
char *ber_ptr;
char *ber_end;
struct seqorset *ber_sos;
char *ber_rwptr;
};
typedef struct
{
char *pszName ;
char *pszValue ;
unsigned long valueL;
struct berval binar_berval;
struct berval *binary_values[2];
char *updateVals[2];
} VSAA_NAME ;
/////////
bool createEntry (LDAPMod *mods,VSAA_NAME input[],int pos)
{
mods->mod_op=LDAP_MOD_REPLACE |LDAP_MOD_ADD;
mods->mod_type =input[pos].pszName;
if (strcmp(input[pos].pszName,"userCertificate")==0)
{
mods->mod_op=LDAP_MOD_REPLACE |LDAP_MOD_ADD|LDAP_MOD_BVALUES;
(input[pos].binar_berval).bv_len =input[pos].valueL ;
(input[pos].binar_berval).bv_val =input[pos].pszValue ;
input[pos].binary_values[0]=&(input[pos].binar_berval);
input[pos].binary_values[1]=NULL;
mods->mod_bvalues = input[pos].binary_values;
}
else
{
input[pos].updateVals[0]=input[pos].pszValue ;
input[pos].updateVals[1]=NULL;
mods->mod_values =input[pos].updateVals;
}
return true;
}
int main( int argc, char **argv )
{
LDAP *ld,*ld1;
//struct ldapmsg *res, *msg,*;
LDAPMessage *res, *msg,*e;
LDAPControl **serverctrls;
struct berval **ber1;
struct berelement *ber;
//////////
LDAPMod mods[20];
LDAPMod *mod[20];
///////////////////////
struct berval binar_berval;
struct berval *binary_values[2];
////////////////////////
VSAA_NAME input[50];
//////////
char *a, *dn, *matched_msg = NULL, *error_msg = NULL;
char **vals, **referrals;
int version, i, rc, parse_rc, msgtype, num_entries = 0,
num_refs = 0;
/* Get a handle to an LDAP connection. */
if ( (ld = ldap_init( HOSTNAME, /*PORTNUMBER*/389 )) == NULL ) {
perror( "ldap_init" );
return( 1 );
}
///////////////
if ( (ld1 = ldap_init( HOSTNAME1, /*PORTNUMBER*/389 )) == NULL ) {
perror( "ldap_init" );
return( 1 );
}
/////////////////
version = LDAP_VERSION3;
if ( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) !=
LDAP_SUCCESS ) {
rc = ldap_get_lderrno( ld, NULL, NULL );
fprintf( stderr, "ldap_set_option: %s\n", ldap_err2string( rc ) );
ldap_unbind( ld );
return( 1 );
}
///////////
if ( ldap_set_option( ld1, LDAP_OPT_PROTOCOL_VERSION, &version ) !=
LDAP_SUCCESS ) {
rc = ldap_get_lderrno( ld1, NULL, NULL );
fprintf( stderr, "ldap_set_option: %s\n", ldap_err2string( rc ) );
ldap_unbind( ld1 );
return( 1 );
}
/////////////
/* Bind to the server anonymously. */
rc = ldap_simple_bind_s( ld, NULL, NULL );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "ldap_simple_bind_s: %s\n", ldap_err2string( rc ) );
ldap_get_lderrno( ld, &matched_msg, &error_msg );
if ( error_msg != NULL && *error_msg != '\0' ) {
fprintf( stderr, "%s\n", error_msg );
}
if ( matched_msg != NULL && *matched_msg != '\0' ) {
fprintf( stderr, "Part of the DN that matches an existing entry: %s\n", matched_msg );
}
ldap_unbind_s( ld );
return( 1 );
}
//////////////////
rc = ldap_simple_bind_s( ld1, "cn=Directory Manager","8isd28isd2" );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "ldap_simple_bind_s: %s\n", ldap_err2string( rc ) );
ldap_get_lderrno( ld, &matched_msg, &error_msg );
if ( error_msg != NULL && *error_msg != '\0' ) {
fprintf( stderr, "%s\n", error_msg );
}
if ( matched_msg != NULL && *matched_msg != '\0' ) {
fprintf( stderr, "Part of the DN that matches an existing entry: %s\n", matched_msg );
}
ldap_unbind_s(ld1);
return( 1 );
}
//////////////////
/* Perform the search operation. */
rc = ldap_search_ext_s( ld, BASEDN , SCOPE,FILTER, NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "ldap_search_ext_s: %s\n", ldap_err2string( rc ) );
if ( error_msg != NULL && *error_msg != '\0' ) {
fprintf( stderr, "%s\n", error_msg );
}
if ( matched_msg != NULL && *matched_msg != '\0' ) {
fprintf( stderr, "Part of the DN that matches an existing entry: %s\n", matched_msg );
}
ldap_unbind_s( ld );
return( 1 );
}
unsigned char *cerbuf;
cerbuf=(unsigned char *)malloc(2000);
unsigned long cerlen;
num_entries = ldap_count_entries( ld, res );
num_refs = ldap_count_references( ld, res );
/* Iterate through the results. An LDAPMessage structure sent back from
a search operation can contain either an entry found by the search,
a search reference, or the final result of the search operation. */
char dnbuf[100];
for ( msg = ldap_first_message( ld, res ); msg != NULL; msg = ldap_next_message( ld, msg ) )
{
/* Determine what type of message was sent from the server. */
msgtype = ldap_msgtype( msg );
switch( msgtype ) {
/* If the result was an entry found by the search, get and print the
attributes and values of the entry. */
case LDAP_RES_SEARCH_ENTRY:
///////
/////////
/* Get and print the DN of the entry. */
if (( dn = ldap_get_dn( ld, res )) != NULL )
{
printf( "dn: %s\n", dn );
strcpy(dnbuf,dn);
//dn作绑定的标志
ldap_memfree( dn );
}
/* Iterate through each attribute in the entry. */
int attnum;
attnum=0;
for ( a = ldap_first_attribute( ld, res, &ber );
a != NULL; a = ldap_next_attribute( ld, res, ber ) )
{//列举属性
if (( vals = ldap_get_values( ld, res, a )) != NULL )
{ //得到数据值
for ( i = 0; vals[ i ] != NULL; i++ )
{ ////多个值
printf( "%s::: %s\n", a, vals[ i ] );
///////////////////////////////////
input[attnum].pszName=(char *)malloc(strlen(a)+1);
strcpy(input[attnum].pszName,a);
if (strcmp("userCertificate",a)==0)
{
ber1=ldap_get_values_len(ld,res,a);
/*
input[attnum].valueL=(*ber1)->bv_len;
input[attnum].pszValue=(char *)malloc(input[attnum].valueL+1);
memcpy(input[attnum].pszValue,vals[i],input[attnum].valueL);
*/
attnum=attnum-1;
///////////////
cerlen=(*ber1)->bv_len;;
memcpy(cerbuf,vals[i],cerlen);
printf( "certificate len: %d\n",(*ber1)->bv_len );
/////////////
}
else
{
input[attnum].valueL=strlen(vals[i]);
input[attnum].pszValue=(char *)malloc(input[attnum].valueL+1);
strcpy(input[attnum].pszValue,vals[i]);
}
}
////多个值
attnum++;
ldap_value_free( vals );
}
//得到数据值
ldap_memfree( a );
//列举属性
}
////////////////////
//一个条目完毕
int ii;
for (ii=0;ii<attnum;ii++)
{
createEntry(&mods[ii],input,ii);
mod[ii]=&mods[ii];
}
mod[ii]=NULL;
/////////////
rc = ldap_add_ext_s( ld1, dnbuf, mod, NULL, NULL );
if ( rc != LDAP_SUCCESS ) {
//rc=68,entry has
fprintf( stderr, "ldap_add_ext_s: %s\n", ldap_err2string( rc ) );
ldap_get_lderrno( ld1, &matched_msg, &error_msg );
if ( error_msg != NULL & *error_msg != '\0' ) {
fprintf( stderr, "%s\n", error_msg );
}
if ( matched_msg != NULL && *matched_msg != '\0' ) {
fprintf( stderr,
"Part of the DN that matches an existing entry: %s\n",
matched_msg );
}
} else {
printf( "%s added successfully.\n", dnbuf );
}
//ldap_unbind_s( ld1 );
///////////
mods[19].mod_op=LDAP_MOD_REPLACE |LDAP_MOD_ADD;
mods[19].mod_type="userCertificate";
LDAPMod mods1;
LDAPMod *mod1[2];
struct berval binar_berval;
struct berval *binary_values[2];
char *updateVals[2];
mods1.mod_op=LDAP_MOD_REPLACE|LDAP_MOD_ADD|LDAP_MOD_BVALUES;
mods1.mod_type="userCertificate";
binar_berval.bv_len =(unsigned long)cerlen;
binar_berval.bv_val =(char *)cerbuf;
binary_values[0]=&binar_berval;
binary_values[1]=NULL;
mods1.mod_bvalues = binary_values;
mod1[0]=&mods1;
mod1[1]=NULL;
//rc = ldap_modify_s(ld1, dnbuf, mod1);
/////////
/////////////
//////////////////////
if ( ber != NULL ) {
ber_free( ber, 0 );
}
printf( "\n" );
break;
case LDAP_RES_SEARCH_REFERENCE:
/* The server sent a search reference encountered during the
search operation. */
/* Parse the result and print the search references.
Ideally, rather than print them out, you would follow the
references. */
parse_rc = ldap_parse_reference( ld, msg, &referrals, NULL, 0 );
if ( parse_rc != LDAP_SUCCESS ) {
fprintf( stderr, "ldap_parse_result: %s\n", ldap_err2string( parse_rc ) );
ldap_unbind( ld );
return( 1 );
}
if ( referrals != NULL ) {
for ( i = 0; referrals[ i ] != NULL; i++ ) {
printf( "Search reference: %s\n\n", referrals[ i ] );
}
ldap_value_free( referrals );
}
break;
case LDAP_RES_SEARCH_RESULT:
/* Parse the final result received from the server. Note the last
argument is a non-zero value, which indicates that the
LDAPMessage structure will be freed when done. (No need
to call ldap_msgfree().) */
parse_rc = ldap_parse_result( ld, msg, &rc, &matched_msg, &error_msg, NULL, &serverctrls, 0 );
if ( parse_rc != LDAP_SUCCESS ) {
fprintf( stderr, "ldap_parse_result: %s\n", ldap_err2string( parse_rc ) );
ldap_unbind( ld );
return( 1 );
}
/* Check the results of the LDAP search operation. */
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, "ldap_search_ext: %s\n", ldap_err2string( rc ) );
if ( error_msg != NULL & *error_msg != '\0' ) {
fprintf( stderr, "%s\n", error_msg );
}
if ( matched_msg != NULL && *matched_msg != '\0' ) {
fprintf( stderr, "Part of the DN that matches an existing entry: %s\n", matched_msg );
}
} else {
printf( "Search completed successfully.\n"
"Entries found: %d\n"
"Search references returned: %d\n",
num_entries, num_refs );
}
break;
default:
break;
}
}
/* Disconnect when done. */
ldap_unbind( ld );
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -