2023年6月21日发(作者:)
LDAPSearch实例代码#include
int nCount = ldap_count_messages( ld, result ); if (nCount <= 0) { cout << " no record " << endl; return nRet; }
cout << "The count of search is :" << nCount << endl;
LDAPMessage *e; char *a, *dn; //void *ptr; struct berval **vals;
int i = 0; /* step through each entry returned */ for ( e = ldap_first_entry( ld, result);
e != NULL; e = ldap_next_entry( ld, e ) )
{ /* print its name */ dn = ldap_get_dn( ld, e ); cout << "dn: " << dn << endl; free( dn ); /* print each attribute */ BerElement *berptr; for ( a = ldap_first_attribute( ld, e, &berptr );
a != NULL; a = ldap_next_attribute( ld, e, berptr ) )
{ cout << "attribute: " << a << endl; /* print each value */ vals = ldap_get_values_len( ld, e, a); for ( i = 0; vals[i] != NULL; i++ )
{ cout << "Length: " << vals[i]->bv_len << " value: " << vals[i]->bv_val << endl; } ldap_value_free_len(vals); } cout << " ==========================one line finished " << endl; } return 0;}// Search for async modeint searchLDAP_as(LDAP *ld ){
cout << "********************************** Enter searchLDAP_as *********************" << endl; LDAPControl *serverctrls = NULL; LDAPControl *clientctrls = NULL; struct timeval tv = {10,0}; // LDAP_SCOPE_CHILDREN int nMsgidp = 0; int nRet = ldap_search_ext(ld, "dc=mydomain,dc=com", LDAP_SCOPE_SUBTREE,
NULL, NULL, 0, NULL/*&ctrls*/, NULL, NULL, 0, &nMsgidp); if (LDAP_SUCCESS != nRet) { cout << "Fail in ldap_search_ext " << endl; return nRet; }
//ldap_control_free( psctrl );
int finished = 0; char *a, *dn; LDAPMessage *result, *e; int num_entries = 0; while ( !finished )
{
/* Poll for results. We call ldap_result with the "all" argument * set to LDAP_MSG_ONE.
This causes ldap_result() to return exactly one * entry if at least one entry is available.
This allows us to * display the entries as they are received. */
result = NULL; int rc = ldap_result( ld, nMsgidp, LDAP_MSG_ONE, NULL /* no timeout */, &result );
switch ( rc )
{ case -1: /* some error occurred */
cout << "error message is :"<< ldap_err2string( rc) << endl;
//ldap_unbind( ld );
return( 1 );
case 0: /* Timeout was exceeded. No entries are ready for retrieval. */
if ( result != NULL )
{
ldap_msgfree( result );
}
break;
default:
/** Either an entry is ready for retrieval, or all entries have been retrieved. */
if (( e = ldap_first_entry( ld, result )) == NULL )
{ /* All done */
finished = 1;
if ( result != NULL )
{ ldap_msgfree( result );
}
continue;
} printResult(ld, result); ldap_msgfree( result );
}
} cout << "********************************** Leave searchLDAP_as *********************/n" << endl; return nRet;}// Search for sync modeint searchLDAP_s(LDAP *ld ){
cout << "********************************** Enter searchLDAP_s *********************" << endl; LDAPControl *serverctrls = NULL;
LDAPControl *clientctrls = NULL; LDAPMessage *res = NULL ; int nRet = ldap_search_ext_s(ld, "dc=mydomain,dc=com", LDAP_SCOPE_SUBTREE, // LDAP_SCOPE_CHILDREN NULL, NULL, 0, NULL, // &serverctrls
NULL, /* no client ctrls */ NULL, /* no time out */ 0, /* no site limit */ &res); if (nRet != LDAP_SUCCESS) { cout << "Fail in ldap_search_ext_s " << endl; return -1; } printResult(ld,res); ldap_msgfree( res ); cout << "********************************** Leave searchLDAP_s *********************/n" << endl; return nRet;}
int main(){ LDAP * ldapInfo = NULL; int nRet = ldap_initialize(&ldapInfo, "ldap://127.0.0.1"); if (nRet != LDAP_SUCCESS) { cout << "Fail in init ldap " << endl; return -1; } int nOldLdapVersion = 0; nRet = ldap_get_option(ldapInfo, LDAP_OPT_PROTOCOL_VERSION, (void *)(&nOldLdapVersion)); if (nRet != LDAP_SUCCESS) { cout << "Fail in ldap_get_option " << endl; return -1; } cout << "LDAP original Version is:" << nOldLdapVersion << endl;
int nLdapVersion = 3; nRet = ldap_set_option(ldapInfo, LDAP_OPT_PROTOCOL_VERSION,(void *)( &nLdapVersion)); if (nRet != LDAP_SUCCESS) { cout << "Fail in ldap_set_option " << endl; return -1; } int nNewLdapVersion = 0; nRet = ldap_get_option(ldapInfo, LDAP_OPT_PROTOCOL_VERSION, (void *)(&nNewLdapVersion)); if (nRet != LDAP_SUCCESS) { cout << "Fail in ldap_get_option " << endl; return -1; } cout << "LDAP New Version is:" << nNewLdapVersion << endl; cout << "========================begin to search ===================" << endl; nRet = searchLDAP_s(ldapInfo); if (nRet != LDAP_SUCCESS) { cout << " Fail in searchLDAP " << endl; return -1; } searchLDAP_as(ldapInfo); if (nRet != LDAP_SUCCESS) { cout << " Fail in searchLDAP_as " << endl; return -1; } cout << "==========================end to search ====================" << endl; ldap_unbind_ext(ldapInfo, NULL, NULL); return 0;}
2023年6月21日发(作者:)
LDAPSearch实例代码#include
int nCount = ldap_count_messages( ld, result ); if (nCount <= 0) { cout << " no record " << endl; return nRet; }
cout << "The count of search is :" << nCount << endl;
LDAPMessage *e; char *a, *dn; //void *ptr; struct berval **vals;
int i = 0; /* step through each entry returned */ for ( e = ldap_first_entry( ld, result);
e != NULL; e = ldap_next_entry( ld, e ) )
{ /* print its name */ dn = ldap_get_dn( ld, e ); cout << "dn: " << dn << endl; free( dn ); /* print each attribute */ BerElement *berptr; for ( a = ldap_first_attribute( ld, e, &berptr );
a != NULL; a = ldap_next_attribute( ld, e, berptr ) )
{ cout << "attribute: " << a << endl; /* print each value */ vals = ldap_get_values_len( ld, e, a); for ( i = 0; vals[i] != NULL; i++ )
{ cout << "Length: " << vals[i]->bv_len << " value: " << vals[i]->bv_val << endl; } ldap_value_free_len(vals); } cout << " ==========================one line finished " << endl; } return 0;}// Search for async modeint searchLDAP_as(LDAP *ld ){
cout << "********************************** Enter searchLDAP_as *********************" << endl; LDAPControl *serverctrls = NULL; LDAPControl *clientctrls = NULL; struct timeval tv = {10,0}; // LDAP_SCOPE_CHILDREN int nMsgidp = 0; int nRet = ldap_search_ext(ld, "dc=mydomain,dc=com", LDAP_SCOPE_SUBTREE,
NULL, NULL, 0, NULL/*&ctrls*/, NULL, NULL, 0, &nMsgidp); if (LDAP_SUCCESS != nRet) { cout << "Fail in ldap_search_ext " << endl; return nRet; }
//ldap_control_free( psctrl );
int finished = 0; char *a, *dn; LDAPMessage *result, *e; int num_entries = 0; while ( !finished )
{
/* Poll for results. We call ldap_result with the "all" argument * set to LDAP_MSG_ONE.
This causes ldap_result() to return exactly one * entry if at least one entry is available.
This allows us to * display the entries as they are received. */
result = NULL; int rc = ldap_result( ld, nMsgidp, LDAP_MSG_ONE, NULL /* no timeout */, &result );
switch ( rc )
{ case -1: /* some error occurred */
cout << "error message is :"<< ldap_err2string( rc) << endl;
//ldap_unbind( ld );
return( 1 );
case 0: /* Timeout was exceeded. No entries are ready for retrieval. */
if ( result != NULL )
{
ldap_msgfree( result );
}
break;
default:
/** Either an entry is ready for retrieval, or all entries have been retrieved. */
if (( e = ldap_first_entry( ld, result )) == NULL )
{ /* All done */
finished = 1;
if ( result != NULL )
{ ldap_msgfree( result );
}
continue;
} printResult(ld, result); ldap_msgfree( result );
}
} cout << "********************************** Leave searchLDAP_as *********************/n" << endl; return nRet;}// Search for sync modeint searchLDAP_s(LDAP *ld ){
cout << "********************************** Enter searchLDAP_s *********************" << endl; LDAPControl *serverctrls = NULL;
LDAPControl *clientctrls = NULL; LDAPMessage *res = NULL ; int nRet = ldap_search_ext_s(ld, "dc=mydomain,dc=com", LDAP_SCOPE_SUBTREE, // LDAP_SCOPE_CHILDREN NULL, NULL, 0, NULL, // &serverctrls
NULL, /* no client ctrls */ NULL, /* no time out */ 0, /* no site limit */ &res); if (nRet != LDAP_SUCCESS) { cout << "Fail in ldap_search_ext_s " << endl; return -1; } printResult(ld,res); ldap_msgfree( res ); cout << "********************************** Leave searchLDAP_s *********************/n" << endl; return nRet;}
int main(){ LDAP * ldapInfo = NULL; int nRet = ldap_initialize(&ldapInfo, "ldap://127.0.0.1"); if (nRet != LDAP_SUCCESS) { cout << "Fail in init ldap " << endl; return -1; } int nOldLdapVersion = 0; nRet = ldap_get_option(ldapInfo, LDAP_OPT_PROTOCOL_VERSION, (void *)(&nOldLdapVersion)); if (nRet != LDAP_SUCCESS) { cout << "Fail in ldap_get_option " << endl; return -1; } cout << "LDAP original Version is:" << nOldLdapVersion << endl;
int nLdapVersion = 3; nRet = ldap_set_option(ldapInfo, LDAP_OPT_PROTOCOL_VERSION,(void *)( &nLdapVersion)); if (nRet != LDAP_SUCCESS) { cout << "Fail in ldap_set_option " << endl; return -1; } int nNewLdapVersion = 0; nRet = ldap_get_option(ldapInfo, LDAP_OPT_PROTOCOL_VERSION, (void *)(&nNewLdapVersion)); if (nRet != LDAP_SUCCESS) { cout << "Fail in ldap_get_option " << endl; return -1; } cout << "LDAP New Version is:" << nNewLdapVersion << endl; cout << "========================begin to search ===================" << endl; nRet = searchLDAP_s(ldapInfo); if (nRet != LDAP_SUCCESS) { cout << " Fail in searchLDAP " << endl; return -1; } searchLDAP_as(ldapInfo); if (nRet != LDAP_SUCCESS) { cout << " Fail in searchLDAP_as " << endl; return -1; } cout << "==========================end to search ====================" << endl; ldap_unbind_ext(ldapInfo, NULL, NULL); return 0;}
发布评论