1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A/*
1N/A * The contents of this file are subject to the Netscape Public
1N/A * License Version 1.1 (the "License"); you may not use this file
1N/A * except in compliance with the License. You may obtain a copy of
1N/A * the License at http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS
1N/A * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1N/A * implied. See the License for the specific language governing
1N/A * rights and limitations under the License.
1N/A *
1N/A * The Original Code is Mozilla Communicator client code, released
1N/A * March 31, 1998.
1N/A *
1N/A * The Initial Developer of the Original Code is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998-1999 Netscape Communications Corporation. All
1N/A * Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A */
1N/A/*
1N/A * Copyright (c) 1993 Regents of the University of Michigan.
1N/A * All rights reserved.
1N/A */
1N/A/*
1N/A * sbind.c
1N/A */
1N/A
1N/A#if 0
1N/A#ifndef lint
1N/Astatic char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
1N/A#endif
1N/A#endif
1N/A
1N/A#include "ldap-int.h"
1N/A
1N/Astatic int simple_bind_nolock( LDAP *ld, const char *dn, const char *passwd,
1N/A int unlock_permitted );
1N/Astatic int simple_bindifnot_s( LDAP *ld, const char *dn, const char *passwd );
1N/A
1N/A/*
1N/A * ldap_simple_bind - bind to the ldap server. The dn and
1N/A * password of the entry to which to bind are supplied. The message id
1N/A * of the request initiated is returned.
1N/A *
1N/A * Example:
1N/A * ldap_simple_bind( ld, "cn=manager, o=university of michigan, c=us",
1N/A * "secret" )
1N/A */
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_simple_bind( LDAP *ld, const char *dn, const char *passwd )
1N/A{
1N/A int rc;
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_simple_bind\n", 0, 0, 0 );
1N/A
1N/A if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1N/A return( -1 );
1N/A }
1N/A
1N/A rc = simple_bind_nolock( ld, dn, passwd, 1 );
1N/A
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Asimple_bind_nolock( LDAP *ld, const char *dn, const char *passwd,
1N/A int unlock_permitted )
1N/A{
1N/A BerElement *ber;
1N/A int rc, msgid;
1N/A
1N/A /*
1N/A * The bind request looks like this:
1N/A * BindRequest ::= SEQUENCE {
1N/A * version INTEGER,
1N/A * name DistinguishedName, -- who
1N/A * authentication CHOICE {
1N/A * simple [0] OCTET STRING -- passwd
1N/A * }
1N/A * }
1N/A * all wrapped up in an LDAPMessage sequence.
1N/A */
1N/A
1N/A LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
1N/A msgid = ++ld->ld_msgid;
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
1N/A
1N/A if ( dn == NULL )
1N/A dn = "";
1N/A if ( passwd == NULL )
1N/A passwd = "";
1N/A
1N/A if ( ld->ld_cache_on && ld->ld_cache_bind != NULL ) {
1N/A struct berval bv;
1N/A
1N/A bv.bv_val = (char *)passwd;
1N/A bv.bv_len = strlen( passwd );
1N/A /* if ( unlock_permitted ) LDAP_MUTEX_UNLOCK( ld ); */
1N/A LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
1N/A rc = (ld->ld_cache_bind)( ld, msgid, LDAP_REQ_BIND, dn, &bv,
1N/A LDAP_AUTH_SIMPLE );
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1N/A /* if ( unlock_permitted ) LDAP_MUTEX_LOCK( ld ); */
1N/A if ( rc != 0 ) {
1N/A return( rc );
1N/A }
1N/A }
1N/A
1N/A /* create a message to send */
1N/A if (( rc = nsldapi_alloc_ber_with_options( ld, &ber ))
1N/A != LDAP_SUCCESS ) {
1N/A return( -1 );
1N/A }
1N/A
1N/A /* fill it in */
1N/A if ( ber_printf( ber, "{it{ists}", msgid, LDAP_REQ_BIND,
1N/A NSLDAPI_LDAP_VERSION( ld ), dn, LDAP_AUTH_SIMPLE, passwd ) == -1 ) {
1N/A LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
1N/A ber_free( ber, 1 );
1N/A return( -1 );
1N/A }
1N/A
1N/A if ( nsldapi_put_controls( ld, NULL, 1, ber ) != LDAP_SUCCESS ) {
1N/A ber_free( ber, 1 );
1N/A return( -1 );
1N/A }
1N/A
1N/A /* send the message */
1N/A return( nsldapi_send_initial_request( ld, msgid, LDAP_REQ_BIND,
1N/A (char *)dn, ber ));
1N/A}
1N/A
1N/A
1N/A/*
1N/A * ldap_simple_bind - bind to the ldap server using simple
1N/A * authentication. The dn and password of the entry to which to bind are
1N/A * supplied. LDAP_SUCCESS is returned upon success, the ldap error code
1N/A * otherwise.
1N/A *
1N/A * Example:
1N/A * ldap_simple_bind_s( ld, "cn=manager, o=university of michigan, c=us",
1N/A * "secret" )
1N/A */
1N/Aint
1N/ALDAP_CALL
1N/Aldap_simple_bind_s( LDAP *ld, const char *dn, const char *passwd )
1N/A{
1N/A int msgid;
1N/A LDAPMessage *result;
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_simple_bind_s\n", 0, 0, 0 );
1N/A
1N/A if ( NSLDAPI_VALID_LDAP_POINTER( ld ) &&
1N/A ( ld->ld_options & LDAP_BITOPT_RECONNECT ) != 0 ) {
1N/A return( simple_bindifnot_s( ld, dn, passwd ));
1N/A }
1N/A
1N/A if ( (msgid = ldap_simple_bind( ld, dn, passwd )) == -1 )
1N/A return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1N/A
1N/A if ( ldap_result( ld, msgid, 1, (struct timeval *) 0, &result ) == -1 )
1N/A return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1N/A
1N/A return( ldap_result2error( ld, result, 1 ) );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * simple_bindifnot_s() is like ldap_simple_bind_s() except that it only does
1N/A * a bind if the default connection is not currently bound.
1N/A * If a successful bind using the same DN has already taken place we just
1N/A * return LDAP_SUCCESS without conversing with the server at all.
1N/A */
1N/Astatic int
1N/Asimple_bindifnot_s( LDAP *ld, const char *dn, const char *passwd )
1N/A{
1N/A int msgid, rc;
1N/A LDAPMessage *result;
1N/A char *binddn;
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "simple_bindifnot_s\n", 0, 0, 0 );
1N/A
1N/A if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( dn == NULL ) {
1N/A dn = ""; /* to make comparisons simpler */
1N/A }
1N/A
1N/A /*
1N/A * if we are already bound using the same DN, just return LDAP_SUCCESS.
1N/A */
1N/A if ( NULL != ( binddn = nsldapi_get_binddn( ld ))
1N/A && 0 == strcmp( dn, binddn )) {
1N/A rc = LDAP_SUCCESS;
1N/A LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
1N/A return rc;
1N/A }
1N/A
1N/A /*
1N/A * if the default connection has been lost and is now marked dead,
1N/A * dispose of the default connection so it will get re-established.
1N/A *
1N/A * if not, clear the bind DN and status to ensure that we don't
1N/A * report the wrong bind DN to a different thread while waiting
1N/A * for our bind result to return from the server.
1N/A */
1N/A LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
1N/A if ( NULL != ld->ld_defconn ) {
1N/A if ( LDAP_CONNST_DEAD == ld->ld_defconn->lconn_status ) {
1N/A nsldapi_free_connection( ld, ld->ld_defconn, NULL, NULL, 1, 0 );
1N/A ld->ld_defconn = NULL;
1N/A } else if ( ld->ld_defconn->lconn_binddn != NULL ) {
1N/A NSLDAPI_FREE( ld->ld_defconn->lconn_binddn );
1N/A ld->ld_defconn->lconn_binddn = NULL;
1N/A ld->ld_defconn->lconn_bound = 0;
1N/A }
1N/A }
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
1N/A
1N/A /*
1N/A * finally, bind (this will open a new connection if necessary)
1N/A *
1N/A * do everything under the protection of the result lock to
1N/A * ensure that only one thread will be in this code at a time.
1N/A * XXXmcs: we should use a condition variable instead?
1N/A */
1N/A LDAP_MUTEX_LOCK( ld, LDAP_RESULT_LOCK );
1N/A if ( (msgid = simple_bind_nolock( ld, dn, passwd, 0 )) == -1 ) {
1N/A rc = LDAP_GET_LDERRNO( ld, NULL, NULL );
1N/A goto unlock_and_return;
1N/A }
1N/A
1N/A /*
1N/A * Note that at this point the bind request is on its way to the
1N/A * server and at any time now we will either be bound as the new
1N/A * DN (if the bind succeeded) or we will be bound as anonymous (if
1N/A * the bind failed).
1N/A */
1N/A
1N/A /*
1N/A * Wait for the bind result. Code inside result.c:read1msg()
1N/A * takes care of setting the connection's bind DN and status.
1N/A */
1N/A if ( nsldapi_result_nolock( ld, msgid, 1, 0, (struct timeval *) 0,
1N/A &result ) == -1 ) {
1N/A rc = LDAP_GET_LDERRNO( ld, NULL, NULL );
1N/A goto unlock_and_return;
1N/A }
1N/A
1N/A rc = ldap_result2error( ld, result, 1 );
1N/A
1N/Aunlock_and_return:
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_RESULT_LOCK );
1N/A return( rc );
1N/A}