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