#include <stdio.h>
#include <ldap.h>
#include <string.h>
#if defined(N_PLAT_NLM) && defined(LIBC)
#include <screen.h>
#endif
#if defined(HAVE_WINSOCK)
#include <winsock.h>
#endif
static int dosearch( LDAP *ld, char *container);
typedef struct rebind_cred {
char *dn;
char *pw;
} rebind_cred_t;
rebind_cred_t creds = { NULL, NULL};
int LIBCALL dorebind( LDAP *ld, LDAP_CONST char *url, int request, ber_int_t msgid)
{
int rc;
LDAPURLDesc *srv;
if( (rc = ldap_url_parse( url, &srv)) != LDAP_SUCCESS) {
printf("rebind: parse_url failed, %s\n", ldap_err2string(rc));
return rc;
}
printf("rebind: in rebind proc: referral - host %s:%d, dn \"%s\", request %d, msgid %d\n",
srv->lud_host, srv->lud_port, srv->lud_dn, request, msgid);
if ( creds.pw == NULL) {
printf("rebind: simple bind as anonymous\n");
} else {
printf("rebind: simple bind as %s/%s\n", creds.dn, creds.pw);
}
rc = ldap_simple_bind_s( ld, creds.dn, creds.pw );
if ( rc != LDAP_SUCCESS ) {
printf("rebind: ldap_simple_bind_s: %s\n",ldap_err2string(rc));
}
ldap_free_urldesc( srv);
return rc;
}
void usage()
{
printf("usage rebind [-f] [-r] [-t] <server> <container> [user_dn passwd]\n");
printf("\t-f - disable rebind function (enabled if option absent)\n");
printf("\t-r - disable automatic referral handling (enabled if option absent)\n");
printf("\t-t - set ldap v2 bind semantics (V3 if option absent)\n");
return;
}
int main( int argc, char **argv )
{
LDAP *ld;
char *myContainer;
int rc;
char *host;
int rfunc = 1;
int ref = 1;
int ver = 3;
struct timeval timeOut = {10,0};
#if defined(N_PLAT_NLM) && defined(LIBC)
setscreenmode(SCR_NO_MODE);
#endif
if (argc > 1) {
while( 1 ) {
if( strcmp( argv[1],"-f") ==0) {
rfunc = 0;
argc--;argv++;
continue;
} else
if( strcmp( argv[1],"-r") ==0) {
ref = 0;
argc--;argv++;
continue;
} else
if( strcmp( argv[1],"-t") ==0) {
ver = 2;
argc--;argv++;
continue;
} else
if( argv[1][0] == '-') {
usage();
return 0;
}
break;
}
}
if (argc > 2) {
host = argv[1];
myContainer = argv[2];
} else {
usage();
return 255;
}
if ( argc > 3) {
creds.dn = argv[3];
if ( argc > 4) {
creds.pw = argv[4];
}
}
ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &ver);
printf("rebind: LDAP protocol version %d set\n", ver);
ldap_set_option( NULL, LDAP_OPT_NETWORK_TIMEOUT, &timeOut);
if( ! ref ) {
ldap_set_option( NULL, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
printf("rebind: SDK referral following disabled\n");
} else {
printf("rebind: SDK referral following enabled\n");
}
if( rfunc) {
ldap_set_rebind_proc( NULL, &dorebind);
printf("rebind: rebind procedure enabled\n");
}
ld = ldap_init( host, LDAP_PORT );
if (ld == NULL) {
printf("ldap_init\n");
return 255;
}
if( creds.dn == NULL) {
printf("rebind: Simple bind to server %s as anonymous\n", host);
} else {
printf("rebind: Simple bind to server %s as %s/%s\n",
host, creds.dn, creds.pw);
}
rc = ldap_simple_bind_s( ld, creds.dn, creds.pw );
if ( rc != LDAP_SUCCESS ) {
printf("rebind: ldap_simple_bind_s: %s\n", ldap_err2string(rc));
ldap_unbind( ld );
return 255;
}
if( (rc = dosearch( ld, myContainer)) != LDAP_SUCCESS) {
printf("rebind: Search failed: %s\n", ldap_err2string(rc));
ldap_unbind( ld );
return 255;
}
ldap_unbind( ld );
return 0;
}
static int dosearch( LDAP *ld, char *container)
{
LDAPMessage *result = NULL;
LDAPMessage *msg = NULL;
struct timeval timeout = {20,0};
int rc;
int counte, countr, countm;
int i = 0;
char * ref;
char * dn;
int inentry = 0;
int inref = 0;
int errcode = 0;
char * matcheddn = NULL;
char * errmsg = NULL;
char ** referrals = NULL;
LDAPControl ** servercontrols = NULL;
printf("rebind: Start the search beginning with \"%s\"\n", container);
rc = ldap_search_ext_s( ld,
container,
LDAP_SCOPE_SUBTREE,
"(objectclass=*)",
NULL,
0,
NULL,
NULL,
&timeout,
LDAP_NO_LIMIT,
&result );
counte = -1;
countr = -1;
countm = -1;
if( result == NULL) {
printf("rebind: ldap_search_ext_s: result is NULL, status = 0x%x\n", rc);
printf("rebind: ldap_search_ext_s: %s\n", ldap_err2string( rc));
return rc;
}
counte = ldap_count_entries(ld, result);
countr = ldap_count_references(ld, result);
countm = ldap_count_messages(ld, result);
if ( (rc != LDAP_SUCCESS) && (rc != LDAP_REFERRAL) ) {
if( counte != -1) {
printf("rebind: ldap_search_ext_s: %s\n", ldap_err2string(rc));
printf("rebind: Return from search: # entries: %d, ", counte);
printf("# references: %d, ", countr);
printf("# messages: %d\n", countm);
}
}
if( rc == LDAP_REFERRAL) {
printf("rebind: Referral status in result\n" );
}
printf("rebind: # entries: %d, ", counte);
printf("# references: %d, ", countr);
printf("# messages: %d\n", countm);
for( msg=ldap_first_message(ld, result); msg!=NULL; msg=ldap_next_message(ld, msg)) {
int type = ldap_msgtype(msg);
switch( type) {
case LDAP_RES_SEARCH_REFERENCE:
if ( inref == 0) {
printf("rebind: Search reference\n");
}
inentry = 0;
inref = 1;
rc = ldap_parse_reference( ld, msg, &referrals, NULL, 0);
if( rc != 0) {
printf("rebind: parse_reference returned error: %s\n",
ldap_err2string( rc));
return rc;
}
if( referrals != NULL) {
for( i = 0, ref = referrals[0]; ref != NULL;i++) {
printf("rebind: Search reference %s\n", ref);
ref = referrals[i+1];
}
ldap_value_free( referrals);
}
break;
case LDAP_RES_SEARCH_ENTRY:
if( inentry == 0) {
printf("rebind: Search entry\n");
}
inref = 0;
inentry = 1;
dn = ldap_get_dn(ld, msg);
if (dn) {
printf("rebind: Object %s:\n", dn);
ldap_memfree(dn);
}
break;
case LDAP_RES_SEARCH_RESULT:
inentry = 0;
printf("rebind: Search result\n");
rc = ldap_parse_result( ld, msg, &errcode, &matcheddn,
&errmsg, &referrals, &servercontrols, 0);
printf("rebind: Operation errcode = %d: %s\n",
errcode, ldap_err2string( errcode));
if( matcheddn != NULL) {
printf("rebind: matcheddn = \"%s\"\n", matcheddn);
ldap_memfree( matcheddn);
}
if( errmsg != NULL) {
printf("rebind: errmsg = \"%s\"\n", errmsg);
ldap_memfree( errmsg);
}
if( referrals != NULL) {
while( referrals[i] != NULL) {
printf("rebind: referral[%d]:\"%s\"\n",
i, referrals[i]);
i++;
}
ldap_value_free( referrals);
}
if( servercontrols != NULL) {
printf("rebind: Server controls = 0x%X\n", servercontrols);
ldap_controls_free( servercontrols);
}
break;
default:
printf("rebind: Unknown message type %d\n", type);
break;
}
}
ldap_msgfree( result );
return LDAP_SUCCESS;
}