Home | History | Annotate | Line # | Download | only in back-sql
bind.c revision 1.1
      1  1.1  lukem /* $OpenLDAP: pkg/ldap/servers/slapd/back-sql/bind.c,v 1.41.2.3 2008/02/11 23:26:48 kurt Exp $ */
      2  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      3  1.1  lukem  *
      4  1.1  lukem  * Copyright 1999-2008 The OpenLDAP Foundation.
      5  1.1  lukem  * Portions Copyright 1999 Dmitry Kovalev.
      6  1.1  lukem  * Portions Copyright 2002 Pierangelo Masarati.
      7  1.1  lukem  * All rights reserved.
      8  1.1  lukem  *
      9  1.1  lukem  * Redistribution and use in source and binary forms, with or without
     10  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     11  1.1  lukem  * Public License.
     12  1.1  lukem  *
     13  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     14  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     15  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     16  1.1  lukem  */
     17  1.1  lukem /* ACKNOWLEDGEMENTS:
     18  1.1  lukem  * This work was initially developed by Dmitry Kovalev for inclusion
     19  1.1  lukem  * by OpenLDAP Software.  Additional significant contributors include
     20  1.1  lukem  * Pierangelo Masarati.
     21  1.1  lukem  */
     22  1.1  lukem 
     23  1.1  lukem #include "portable.h"
     24  1.1  lukem 
     25  1.1  lukem #include <stdio.h>
     26  1.1  lukem #include <sys/types.h>
     27  1.1  lukem 
     28  1.1  lukem #include "slap.h"
     29  1.1  lukem #include "proto-sql.h"
     30  1.1  lukem 
     31  1.1  lukem int
     32  1.1  lukem backsql_bind( Operation *op, SlapReply *rs )
     33  1.1  lukem {
     34  1.1  lukem 	SQLHDBC			dbh = SQL_NULL_HDBC;
     35  1.1  lukem 	Entry			e = { 0 };
     36  1.1  lukem 	Attribute		*a;
     37  1.1  lukem 	backsql_srch_info	bsi = { 0 };
     38  1.1  lukem 	AttributeName		anlist[2];
     39  1.1  lukem 	int			rc;
     40  1.1  lukem 
     41  1.1  lukem  	Debug( LDAP_DEBUG_TRACE, "==>backsql_bind()\n", 0, 0, 0 );
     42  1.1  lukem 
     43  1.1  lukem 	switch ( be_rootdn_bind( op, rs ) ) {
     44  1.1  lukem 	case SLAP_CB_CONTINUE:
     45  1.1  lukem 		break;
     46  1.1  lukem 
     47  1.1  lukem 	default:
     48  1.1  lukem 		/* in case of success, front end will send result;
     49  1.1  lukem 		 * otherwise, be_rootdn_bind() did */
     50  1.1  lukem  		Debug( LDAP_DEBUG_TRACE, "<==backsql_bind(%d)\n",
     51  1.1  lukem 			rs->sr_err, 0, 0 );
     52  1.1  lukem 		return rs->sr_err;
     53  1.1  lukem 	}
     54  1.1  lukem 
     55  1.1  lukem 	rs->sr_err = backsql_get_db_conn( op, &dbh );
     56  1.1  lukem 	if ( rs->sr_err != LDAP_SUCCESS ) {
     57  1.1  lukem      		Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
     58  1.1  lukem 			"could not get connection handle - exiting\n",
     59  1.1  lukem 			0, 0, 0 );
     60  1.1  lukem 
     61  1.1  lukem 		rs->sr_text = ( rs->sr_err == LDAP_OTHER )
     62  1.1  lukem 			? "SQL-backend error" : NULL;
     63  1.1  lukem 		goto error_return;
     64  1.1  lukem 	}
     65  1.1  lukem 
     66  1.1  lukem 	anlist[0].an_name = slap_schema.si_ad_userPassword->ad_cname;
     67  1.1  lukem 	anlist[0].an_desc = slap_schema.si_ad_userPassword;
     68  1.1  lukem 	anlist[1].an_name.bv_val = NULL;
     69  1.1  lukem 
     70  1.1  lukem 	bsi.bsi_e = &e;
     71  1.1  lukem 	rc = backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE,
     72  1.1  lukem 			(time_t)(-1), NULL, dbh, op, rs, anlist,
     73  1.1  lukem 			BACKSQL_ISF_GET_ENTRY );
     74  1.1  lukem 	if ( rc != LDAP_SUCCESS ) {
     75  1.1  lukem 		Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
     76  1.1  lukem 			"could not retrieve bindDN ID - no such entry\n",
     77  1.1  lukem 			0, 0, 0 );
     78  1.1  lukem 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
     79  1.1  lukem 		goto error_return;
     80  1.1  lukem 	}
     81  1.1  lukem 
     82  1.1  lukem 	a = attr_find( e.e_attrs, slap_schema.si_ad_userPassword );
     83  1.1  lukem 	if ( a == NULL ) {
     84  1.1  lukem 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
     85  1.1  lukem 		goto error_return;
     86  1.1  lukem 	}
     87  1.1  lukem 
     88  1.1  lukem 	if ( slap_passwd_check( op, &e, a, &op->oq_bind.rb_cred,
     89  1.1  lukem 				&rs->sr_text ) != 0 )
     90  1.1  lukem 	{
     91  1.1  lukem 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
     92  1.1  lukem 		goto error_return;
     93  1.1  lukem 	}
     94  1.1  lukem 
     95  1.1  lukem error_return:;
     96  1.1  lukem 	if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
     97  1.1  lukem 		(void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
     98  1.1  lukem 	}
     99  1.1  lukem 
    100  1.1  lukem 	if ( !BER_BVISNULL( &e.e_nname ) ) {
    101  1.1  lukem 		backsql_entry_clean( op, &e );
    102  1.1  lukem 	}
    103  1.1  lukem 
    104  1.1  lukem 	if ( bsi.bsi_attrs != NULL ) {
    105  1.1  lukem 		op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
    106  1.1  lukem 	}
    107  1.1  lukem 
    108  1.1  lukem 	if ( rs->sr_err != LDAP_SUCCESS ) {
    109  1.1  lukem 		send_ldap_result( op, rs );
    110  1.1  lukem 	}
    111  1.1  lukem 
    112  1.1  lukem 	Debug( LDAP_DEBUG_TRACE,"<==backsql_bind()\n", 0, 0, 0 );
    113  1.1  lukem 
    114  1.1  lukem 	return rs->sr_err;
    115  1.1  lukem }
    116  1.1  lukem 
    117