#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ldap.h>
#include <ldap_ssl.h>
#if defined(N_PLAT_NLM) && defined(LIBC)
#include <screen.h>
#endif
static char usage[] =
"\n Usage: searchURL <search URL>\n"
"\n Examples: "
"\n 1. searchURL ldap:
"\n 2. searchURL ldap:
"\n 3. searchURL ldaps:
int main( int argc, char **argv )
{
int version, i, rc, entryCount, isSecure=0;
char *ldapURL;
char *attribute, *dn, **values, *sortAttribute = "sn";
struct timeval timeOut = {10,0};
BerElement *ber;
LDAP *ld;
LDAPMessage *searchResult, *entry;
LDAPURLDesc *ludpp;
#if defined(N_PLAT_NLM) && defined(LIBC)
setscreenmode(SCR_NO_MODE);
#endif
if ( argc != 2 )
{
printf("%s", usage);
return (1);
}
ldapURL = argv[1];
if ( ldap_is_ldap_url( ldapURL ))
printf( "\n %s is a valid URL\n", ldapURL);
else
{
printf( "\n %s is not a valid LDAP URL.", ldapURL);
printf( "\n URL had bad prefix. Should be ldap:
return ( 1 );
}
rc = ldap_url_parse(ldapURL, &ludpp);
if ( rc != LDAP_SUCCESS )
{
switch ( rc )
{
case LDAP_URL_ERR_MEM :
printf("\n Cannot allocate memory space.\n");
break;
case LDAP_URL_ERR_PARAM :
printf("\n Invalid parameter.\n");
break;
case LDAP_URL_ERR_BADSCHEME :
printf("\n URL doesnt begin with \"ldap[s]:
break;
case LDAP_URL_ERR_BADENCLOSURE :
printf("\n URL is missing trailing \">\".\n");
break;
case LDAP_URL_ERR_BADURL :
printf("\n Invalid URL.\n");
break;
case LDAP_URL_ERR_BADHOST :
printf("\n Host port is invalid.\n");
break;
case LDAP_URL_ERR_BADATTRS :
printf("\n Invalid or missing attributes.\n");
break;
case LDAP_URL_ERR_BADSCOPE :
printf("\n Invalid or missing scope string.\n");
break;
case LDAP_URL_ERR_BADFILTER :
printf("\n Invalid or missing filter.\n");
break;
case LDAP_URL_ERR_BADEXTS :
printf("\n Invalid or missing extensions.\n");
break;
}
return ( 1 );
}
version = LDAP_VERSION3;
ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version);
ldap_set_option( NULL, LDAP_OPT_NETWORK_TIMEOUT, &timeOut);
if ( 0 == strcmp ( ludpp->lud_scheme, "ldaps" ) )
isSecure = 1;
if ( !isSecure )
{
printf("\n Making clear-text connection\n");
if (( ld = ldap_init( ludpp->lud_host,
ludpp->lud_port?ludpp->lud_port:LDAP_PORT )) == NULL)
{
printf ( "\n LDAP session initialization failed\n");
ldap_free_urldesc( ludpp );
return( 1 );
}
printf ( "\n LDAP session initialized\n");
}
else
{
printf("\n Making encrypted connection\n");
rc = ldapssl_client_init( NULL,
NULL );
if (rc != LDAP_SUCCESS)
{
printf("ldapssl_client_init error: %d\n", rc);
ldap_free_urldesc( ludpp );
return (1);
}
rc = ldapssl_set_verify_mode(LDAPSSL_VERIFY_NONE);
if (rc != LDAP_SUCCESS)
{
printf("ldapssl_set_verify_mode error: %d\n", rc);
ldapssl_client_deinit();
ldap_free_urldesc( ludpp );
return (1);
}
ld = ldapssl_init( ludpp->lud_host,
ludpp->lud_port?ludpp->lud_port:LDAPS_PORT,
1 );
if (ld == NULL )
{
printf("ldapssl_init error\n" );
ldapssl_client_deinit();
ldap_free_urldesc( ludpp );
return (1);
}
}
rc = ldap_simple_bind_s( ld, NULL, NULL);
if (rc != LDAP_SUCCESS )
{
printf("ldap_simple_bind_s error: %d, %s\n", rc, ldap_err2string( rc ));
ldap_unbind_s( ld );
if (isSecure) ldapssl_client_deinit();
ldap_free_urldesc( ludpp );
return (1);
}
printf("\n Bind successful - performing search\n");
rc = ldap_url_search_st(
ld,
ldapURL,
0,
&timeOut,
&searchResult );
if ( rc != LDAP_SUCCESS )
{
printf("ldap_search_ext_s: %s\n", ldap_err2string( rc ));
ldap_msgfree( searchResult );
ldap_unbind_s( ld );
if (isSecure) ldapssl_client_deinit();
ldap_free_urldesc( ludpp );
return ( 1 );
}
ldap_sort_entries( ld, &searchResult, sortAttribute, strcmp );
for ( entry = ldap_first_entry( ld, searchResult );
entry != NULL;
entry = ldap_next_entry( ld, entry ) )
{
if (( dn = ldap_get_dn( ld, entry )) != NULL )
{
printf("\n dn: %s\n", dn );
ldap_memfree( dn );
}
for ( attribute = ldap_first_attribute( ld, entry, &ber );
attribute != NULL;
attribute = ldap_next_attribute( ld, entry, ber ) )
{
if (( values = ldap_get_values( ld, entry, attribute)) != NULL )
{
for ( i = 0; values[i] != NULL; i++ )
printf(" %s: %s\n", attribute, values[i] );
ldap_value_free( values );
}
ldap_memfree( attribute );
}
ber_free(ber, 0);
}
entryCount = ldap_count_entries( ld, searchResult );
printf("\n Search completed successfully.\n Entries returned: %d\n",
entryCount);
ldap_msgfree( searchResult );
ldap_unbind_s( ld );
if (isSecure) ldapssl_client_deinit();
ldap_free_urldesc( ludpp );
return( 0 );
}