- *edn=ch_strdup(ndn);
-
- if (method == LDAP_AUTH_SIMPLE)
- {
- dbh=backsql_get_db_conn(be,conn);
-
- if (!dbh)
- {
- Debug(LDAP_DEBUG_TRACE,"backsql_bind(): could not get connection handle - exiting\n",0,0,0);
- send_ldap_result(conn,op,LDAP_OTHER,"","SQL-backend error",NULL,NULL);
- return 1;
- }
-
- res=backsql_dn2id(bi,&user_id,dbh,ndn);
- if (res==NULL)
- {
- Debug(LDAP_DEBUG_TRACE,"backsql_bind(): could not retrieve bind dn id - no such entry\n",0,0,0);
- send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,NULL, NULL, NULL, NULL );
- return 1;
- }
-
- backsql_init_search(&bsi,bi,(char*)ndn,LDAP_SCOPE_BASE,-1,-1,-1,NULL,dbh,
- be,conn,op,NULL);
- e=backsql_id2entry(&bsi,&user_entry,&user_id);
- if (e==NULL)
- {
- Debug(LDAP_DEBUG_TRACE,"backsql_bind(): error in backsql_id2entry() - auth failed\n",0,0,0);
- send_ldap_result( conn, op, LDAP_OTHER,NULL, NULL, NULL, NULL );
- return 1;
- }
-
- if ( ! access_allowed( be, conn, op, e,password, NULL, ACL_AUTH ) )
- {
- send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS, NULL, NULL, NULL, NULL );
- return 1;
- }
-
- if ( (a = attr_find( e->e_attrs, password )) == NULL )
- {
- send_ldap_result( conn, op, LDAP_INAPPROPRIATE_AUTH, NULL, NULL, NULL, NULL );
- return 1;
- }
-
- if ( slap_passwd_check( conn, a, cred ) != 0 )
- {
- send_ldap_result( conn, op, LDAP_INVALID_CREDENTIALS,NULL, NULL, NULL, NULL );
- return 1;
- }
- }
- else /*method != SIMPLE */
- {
- send_ldap_result( conn, op, LDAP_STRONG_AUTH_NOT_SUPPORTED,
- NULL, "authentication method not supported", NULL, NULL );
- return 1;
- }
- Debug(LDAP_DEBUG_TRACE,"<==backsql_bind()\n",0,0,0);
- return 0;
+ Debug( LDAP_DEBUG_TRACE, "==>backsql_bind()\n", 0, 0, 0 );
+
+ if ( be_isroot_pw( op ) ) {
+ ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
+ Debug( LDAP_DEBUG_TRACE, "<==backsql_bind() root bind\n",
+ 0, 0, 0 );
+ return LDAP_SUCCESS;
+ }
+
+ ber_dupbv( &op->oq_bind.rb_edn, &op->o_req_ndn );
+
+ if ( op->oq_bind.rb_method != LDAP_AUTH_SIMPLE ) {
+ rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
+ rs->sr_text = "authentication method not supported";
+ send_ldap_result( op, rs );
+ return rs->sr_err;
+ }
+
+ /*
+ * method = LDAP_AUTH_SIMPLE
+ */
+ rs->sr_err = backsql_get_db_conn( op, &dbh );
+ if ( !dbh ) {
+ Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
+ "could not get connection handle - exiting\n",
+ 0, 0, 0 );
+
+ rs->sr_text = ( rs->sr_err == LDAP_OTHER )
+ ? "SQL-backend error" : NULL;
+ goto error_return;
+ }
+
+ anlist[0].an_name = slap_schema.si_ad_userPassword->ad_cname;
+ anlist[0].an_desc = slap_schema.si_ad_userPassword;
+ anlist[1].an_name.bv_val = NULL;
+
+ bsi.bsi_e = &e;
+ rc = backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE,
+ SLAP_NO_LIMIT, SLAP_NO_LIMIT,
+ (time_t)(-1), NULL, dbh, op, rs, anlist,
+ BACKSQL_ISF_GET_ENTRY );
+ if ( rc != LDAP_SUCCESS ) {
+ Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
+ "could not retrieve bindDN ID - no such entry\n",
+ 0, 0, 0 );
+ rs->sr_err = LDAP_INVALID_CREDENTIALS;
+ goto error_return;
+ }
+
+ a = attr_find( e.e_attrs, slap_schema.si_ad_userPassword );
+ if ( a == NULL ) {
+ rs->sr_err = LDAP_INVALID_CREDENTIALS;
+ goto error_return;
+ }
+
+ if ( slap_passwd_check( op, &e, a, &op->oq_bind.rb_cred,
+ &rs->sr_text ) != 0 )
+ {
+ rs->sr_err = LDAP_INVALID_CREDENTIALS;
+ goto error_return;
+ }
+
+error_return:;
+ if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
+ (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
+ }
+
+ if ( !BER_BVISNULL( &e.e_nname ) ) {
+ backsql_entry_clean( op, &e );
+ }
+
+ if ( bsi.bsi_attrs != NULL ) {
+ op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
+ }
+
+ if ( rs->sr_err != LDAP_SUCCESS ) {
+ send_ldap_result( op, rs );
+ }
+
+ Debug( LDAP_DEBUG_TRACE,"<==backsql_bind()\n", 0, 0, 0 );
+
+ return rs->sr_err;