- if ( verbose ) {
- fprintf( stderr, "ldap_initialize( %s )\n",
- ldapuri != NULL ? ldapuri : "<DEFAULT>" );
- }
-
- rc = ldap_initialize( &ld, ldapuri );
- if( rc != LDAP_SUCCESS ) {
- fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
- rc, ldap_err2string(rc) );
- return EXIT_FAILURE;
- }
- }
-
- {
- /* this seems prudent for searches below */
- int deref = LDAP_DEREF_NEVER;
- ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
- }
-
- /* chase referrals */
- if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
- referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
- {
- fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
- referrals ? "on" : "off" );
- return EXIT_FAILURE;
- }
-
- if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
- != LDAP_OPT_SUCCESS )
- {
- fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
- version );
- return EXIT_FAILURE;
- }
-
- if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
- ldap_perror( ld, "ldap_start_tls" );
- if ( use_tls > 1 ) {
- return EXIT_FAILURE;
- }
- }
-
- if (want_bindpw) {
- passwd.bv_val = getpassphrase("Enter LDAP Password: ");
- passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
- }
-
- if ( authmethod == LDAP_AUTH_SASL ) {
-#ifdef HAVE_CYRUS_SASL
- void *defaults;
-
- if( sasl_secprops != NULL ) {
- rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
- (void *) sasl_secprops );
-
- if( rc != LDAP_OPT_SUCCESS ) {
- fprintf( stderr,
- "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
- sasl_secprops );
- return( EXIT_FAILURE );
- }
- }
-
- defaults = lutil_sasl_defaults( ld,
- sasl_mech,
- sasl_realm,
- sasl_authc_id,
- passwd.bv_val,
- sasl_authz_id );
-
- rc = ldap_sasl_interactive_bind_s( ld, binddn,
- sasl_mech, NULL, NULL,
- sasl_flags, lutil_sasl_interact, defaults );
-
- if( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
- return( EXIT_FAILURE );
- }
-#else
- fprintf( stderr, "%s: not compiled with SASL support\n",
- prog );
- return( EXIT_FAILURE );
-#endif
- }
- else {
- if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
- != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_bind" );
- return( EXIT_FAILURE );