]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/bind.c
10f871be40b086f86de1a20b0f28909679680287
[openldap] / servers / slapd / back-sql / bind.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
3  *
4  *       Redistribution and use in source and binary forms are permitted only
5  *       as authorized by the OpenLDAP Public License.  A copy of this
6  *       license is available at http://www.OpenLDAP.org/license.html or
7  *       in file LICENSE in the top-level directory of the distribution.
8  */
9
10 #include "portable.h"
11
12 #ifdef SLAPD_SQL
13
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include "slap.h"
17 #include "back-sql.h"
18 #include "sql-wrap.h"
19 #include "util.h"
20 #include "entry-id.h"
21
22 int 
23 backsql_bind( Operation *op, SlapReply *rs )
24 {
25         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
26         backsql_entryID         user_id;
27         SQLHDBC                 dbh;
28         AttributeDescription    *password = slap_schema.si_ad_userPassword;
29         Entry                   *e, user_entry;
30         Attribute               *a;
31         backsql_srch_info       bsi;
32         int                     rc;
33  
34         Debug( LDAP_DEBUG_TRACE, "==>backsql_bind()\n", 0, 0, 0 );
35
36         if ( be_isroot_pw( op ) ) {
37                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
38                 Debug( LDAP_DEBUG_TRACE, "<==backsql_bind() root bind\n", 
39                                 0, 0, 0 );
40                 return 0;
41         }
42
43         ber_dupbv( &op->oq_bind.rb_edn, &op->o_req_ndn );
44
45         if ( op->oq_bind.rb_method != LDAP_AUTH_SIMPLE ) {
46                 rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
47                 rs->sr_text = "authentication method not supported"; 
48                 send_ldap_result( op, rs );
49                 return 1;
50         }
51
52         /*
53          * method = LDAP_AUTH_SIMPLE
54          */
55         rs->sr_err = backsql_get_db_conn( op, &dbh );
56         if (!dbh) {
57                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
58                         "could not get connection handle - exiting\n",
59                         0, 0, 0 );
60
61                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
62                         ? "SQL-backend error" : NULL;
63                 send_ldap_result( op, rs );
64                 return 1;
65         }
66
67         rc = backsql_dn2id( bi, &user_id, dbh, &op->o_req_ndn );
68         if ( rc != LDAP_SUCCESS ) {
69                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
70                         "could not retrieve bind dn id - no such entry\n", 
71                         0, 0, 0 );
72                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
73                 send_ldap_result( op, rs );
74                 return 1;
75         }
76
77         backsql_init_search( &bsi, bi, &op->o_req_ndn, LDAP_SCOPE_BASE, 
78                         -1, -1, -1, NULL, dbh, op->o_bd, op->o_conn, op, NULL );
79         e = backsql_id2entry( &bsi, &user_entry, &user_id );
80         if ( e == NULL ) {
81                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
82                         "error in backsql_id2entry() - auth failed\n",
83                         0, 0, 0 );
84                 rs->sr_err = LDAP_OTHER;
85                 send_ldap_result( op, rs );
86                 return 1;
87         }
88
89         if ( ! access_allowed( op, e, password, NULL, ACL_AUTH, NULL ) ) {
90                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
91                 send_ldap_result( op, rs );
92                 return 1;
93         }
94
95         if ( ( a = attr_find( e->e_attrs, password ) ) == NULL ) {
96                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
97                 send_ldap_result( op, rs );
98                 return 1;
99         }
100
101         if ( slap_passwd_check( op->o_conn, a, &op->oq_bind.rb_cred ) != 0 ) {
102                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
103                 send_ldap_result( op, rs );
104                 return 1;
105         }
106
107         Debug(LDAP_DEBUG_TRACE,"<==backsql_bind()\n",0,0,0);
108         return 0;
109 }
110  
111 #endif /* SLAPD_SQL */
112