#if 0
#ifndef lint
static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
#endif
#endif
#include "ldap-int.h"
static int simple_bind_nolock( LDAP *ld, const char *dn, const char *passwd,
int unlock_permitted );
static int simple_bindifnot_s( LDAP *ld, const char *dn, const char *passwd );
int
LDAP_CALL
ldap_simple_bind( LDAP *ld, const char *dn, const char *passwd )
{
int rc;
LDAPDebug( LDAP_DEBUG_TRACE, "ldap_simple_bind\n", 0, 0, 0 );
if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
return( -1 );
}
rc = simple_bind_nolock( ld, dn, passwd, 1 );
return( rc );
}
static int
simple_bind_nolock( LDAP *ld, const char *dn, const char *passwd,
int unlock_permitted )
{
BerElement *ber;
int rc, msgid;
LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
msgid = ++ld->ld_msgid;
LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
if ( dn == NULL )
dn = "";
if ( passwd == NULL )
passwd = "";
if ( ld->ld_cache_on && ld->ld_cache_bind != NULL ) {
struct berval bv;
bv.bv_val = (char *)passwd;
bv.bv_len = strlen( passwd );
LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
rc = (ld->ld_cache_bind)( ld, msgid, LDAP_REQ_BIND, dn, &bv,
LDAP_AUTH_SIMPLE );
LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
if ( rc != 0 ) {
return( rc );
}
}
if (( rc = nsldapi_alloc_ber_with_options( ld, &ber ))
!= LDAP_SUCCESS ) {
return( -1 );
}
if ( ber_printf( ber, "{it{ists}", msgid, LDAP_REQ_BIND,
NSLDAPI_LDAP_VERSION( ld ), dn, LDAP_AUTH_SIMPLE, passwd ) == -1 ) {
LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
ber_free( ber, 1 );
return( -1 );
}
if ( nsldapi_put_controls( ld, NULL, 1, ber ) != LDAP_SUCCESS ) {
ber_free( ber, 1 );
return( -1 );
}
return( nsldapi_send_initial_request( ld, msgid, LDAP_REQ_BIND,
(char *)dn, ber ));
}
int
LDAP_CALL
ldap_simple_bind_s( LDAP *ld, const char *dn, const char *passwd )
{
int msgid;
LDAPMessage *result;
LDAPDebug( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n", 0, 0, 0 );
if ( NSLDAPI_VALID_LDAP_POINTER( ld ) &&
( ld->ld_options & LDAP_BITOPT_RECONNECT ) != 0 ) {
return( simple_bindifnot_s( ld, dn, passwd ));
}
if ( (msgid = ldap_simple_bind( ld, dn, passwd )) == -1 )
return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
if ( ldap_result( ld, msgid, 1, (struct timeval *) 0, &result ) == -1 )
return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
return( ldap_result2error( ld, result, 1 ) );
}
static int
simple_bindifnot_s( LDAP *ld, const char *dn, const char *passwd )
{
int msgid, rc;
LDAPMessage *result;
char *binddn;
LDAPDebug( LDAP_DEBUG_TRACE, "simple_bindifnot_s\n", 0, 0, 0 );
if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
return( LDAP_PARAM_ERROR );
}
if ( dn == NULL ) {
dn = "";
}
if ( NULL != ( binddn = nsldapi_get_binddn( ld ))
&& 0 == strcmp( dn, binddn )) {
rc = LDAP_SUCCESS;
LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
return rc;
}
LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
if ( NULL != ld->ld_defconn ) {
if ( LDAP_CONNST_DEAD == ld->ld_defconn->lconn_status ) {
nsldapi_free_connection( ld, ld->ld_defconn, NULL, NULL, 1, 0 );
ld->ld_defconn = NULL;
} else if ( ld->ld_defconn->lconn_binddn != NULL ) {
NSLDAPI_FREE( ld->ld_defconn->lconn_binddn );
ld->ld_defconn->lconn_binddn = NULL;
ld->ld_defconn->lconn_bound = 0;
}
}
LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
LDAP_MUTEX_LOCK( ld, LDAP_RESULT_LOCK );
if ( (msgid = simple_bind_nolock( ld, dn, passwd, 0 )) == -1 ) {
rc = LDAP_GET_LDERRNO( ld, NULL, NULL );
goto unlock_and_return;
}
if ( nsldapi_result_nolock( ld, msgid, 1, 0, (struct timeval *) 0,
&result ) == -1 ) {
rc = LDAP_GET_LDERRNO( ld, NULL, NULL );
goto unlock_and_return;
}
rc = ldap_result2error( ld, result, 1 );
unlock_and_return:
LDAP_MUTEX_UNLOCK( ld, LDAP_RESULT_LOCK );
return( rc );
}