2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Dmitry Kovalev for inclusion
19 * by OpenLDAP Software. Additional significant contributors include
20 * Pierangelo Masarati.
26 #include <sys/types.h>
29 #include "proto-sql.h"
32 backsql_bind( Operation *op, SlapReply *rs )
34 SQLHDBC dbh = SQL_NULL_HDBC;
35 AttributeDescription *password = slap_schema.si_ad_userPassword;
38 backsql_srch_info bsi;
39 AttributeName anlist[2];
42 Debug( LDAP_DEBUG_TRACE, "==>backsql_bind()\n", 0, 0, 0 );
44 if ( be_isroot_pw( op ) ) {
45 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
46 Debug( LDAP_DEBUG_TRACE, "<==backsql_bind() root bind\n",
51 ber_dupbv( &op->oq_bind.rb_edn, &op->o_req_ndn );
53 if ( op->oq_bind.rb_method != LDAP_AUTH_SIMPLE ) {
54 rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
55 rs->sr_text = "authentication method not supported";
56 send_ldap_result( op, rs );
61 * method = LDAP_AUTH_SIMPLE
63 rs->sr_err = backsql_get_db_conn( op, &dbh );
65 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
66 "could not get connection handle - exiting\n",
69 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
70 ? "SQL-backend error" : NULL;
71 send_ldap_result( op, rs );
75 anlist[0].an_name = password->ad_cname;
76 anlist[0].an_desc = password;
77 anlist[1].an_name.bv_val = NULL;
79 rc = backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE,
80 SLAP_NO_LIMIT, SLAP_NO_LIMIT,
81 (time_t)(-1), NULL, dbh, op, rs, anlist,
83 if ( rc != LDAP_SUCCESS ) {
84 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
85 "could not retrieve bindDN ID - no such entry\n",
87 rs->sr_err = LDAP_INVALID_CREDENTIALS;
88 send_ldap_result( op, rs );
92 bsi.bsi_e = &user_entry;
93 rc = backsql_id2entry( &bsi, &bsi.bsi_base_id );
94 if ( rc != LDAP_SUCCESS ) {
95 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
96 "error %d in backsql_id2entry() "
97 "- auth failed\n", rc, 0, 0 );
98 rs->sr_err = LDAP_INVALID_CREDENTIALS;
103 if ( ! access_allowed( op, e, password, NULL, ACL_AUTH, NULL ) ) {
105 rs->sr_err = LDAP_INVALID_CREDENTIALS;
107 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
112 a = attr_find( e->e_attrs, password );
115 rs->sr_err = LDAP_INVALID_CREDENTIALS;
117 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
122 if ( slap_passwd_check( op->o_conn, a, &op->oq_bind.rb_cred, &rs->sr_text ) != 0 ) {
123 rs->sr_err = LDAP_INVALID_CREDENTIALS;
128 if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
129 (void)backsql_free_entryID( &bsi.bsi_base_id, 0 );
133 send_ldap_result( op, rs );
137 Debug(LDAP_DEBUG_TRACE,"<==backsql_bind()\n",0,0,0);