]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/bind.c
allow backwards compatibility for 'T' option (single char)
[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-2004 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
28 #include "slap.h"
29 #include "proto-sql.h"
30
31 int 
32 backsql_bind( Operation *op, SlapReply *rs )
33 {
34         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
35         backsql_entryID         user_id = BACKSQL_ENTRYID_INIT;
36         SQLHDBC                 dbh;
37         AttributeDescription    *password = slap_schema.si_ad_userPassword;
38         Entry                   *e, user_entry;
39         Attribute               *a;
40         backsql_srch_info       bsi;
41         AttributeName           anlist[2];
42         int                     rc;
43         struct berval           dn;
44  
45         Debug( LDAP_DEBUG_TRACE, "==>backsql_bind()\n", 0, 0, 0 );
46
47         if ( be_isroot_pw( op ) ) {
48                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
49                 Debug( LDAP_DEBUG_TRACE, "<==backsql_bind() root bind\n", 
50                                 0, 0, 0 );
51                 return 0;
52         }
53
54         ber_dupbv( &op->oq_bind.rb_edn, &op->o_req_ndn );
55
56         if ( op->oq_bind.rb_method != LDAP_AUTH_SIMPLE ) {
57                 rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
58                 rs->sr_text = "authentication method not supported"; 
59                 send_ldap_result( op, rs );
60                 return 1;
61         }
62
63         /*
64          * method = LDAP_AUTH_SIMPLE
65          */
66         rs->sr_err = backsql_get_db_conn( op, &dbh );
67         if (!dbh) {
68                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
69                         "could not get connection handle - exiting\n",
70                         0, 0, 0 );
71
72                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
73                         ? "SQL-backend error" : NULL;
74                 send_ldap_result( op, rs );
75                 return 1;
76         }
77
78         dn = op->o_req_dn;
79         if ( backsql_api_dn2odbc( op, rs, &dn ) ) {
80                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
81                         "backsql_api_dn2odbc failed\n", 
82                         0, 0, 0 );
83                 rs->sr_err = LDAP_OTHER;
84                 rs->sr_text = "SQL-backend error";
85                 goto error_return;
86         }
87
88         rc = backsql_dn2id( bi, &user_id, dbh, &dn );
89         if ( rc != LDAP_SUCCESS ) {
90                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
91                         "could not retrieve bind dn id - no such entry\n", 
92                         0, 0, 0 );
93                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
94                 send_ldap_result( op, rs );
95                 return 1;
96         }
97
98         anlist[0].an_name = password->ad_cname;
99         anlist[0].an_desc = password;
100         anlist[1].an_name.bv_val = NULL;
101
102         backsql_init_search( &bsi, &dn, LDAP_SCOPE_BASE, 
103                         -1, -1, -1, NULL, dbh, op, rs, anlist );
104         e = backsql_id2entry( &bsi, &user_entry, &user_id );
105         if ( e == NULL ) {
106                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
107                         "error in backsql_id2entry() - auth failed\n",
108                         0, 0, 0 );
109                 rs->sr_err = LDAP_OTHER;
110                 goto error_return;
111         }
112
113         if ( ! access_allowed( op, e, password, NULL, ACL_AUTH, NULL ) ) {
114                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
115                 goto error_return;
116         }
117
118         if ( ( a = attr_find( e->e_attrs, password ) ) == NULL ) {
119                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
120                 goto error_return;
121         }
122
123         if ( slap_passwd_check( op->o_conn, a, &op->oq_bind.rb_cred, &rs->sr_text ) != 0 ) {
124                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
125                 goto error_return;
126         }
127
128 error_return:;
129         if ( rs->sr_err ) {
130                 send_ldap_result( op, rs );
131                 return 1;
132         }
133         
134         if ( dn.bv_val != op->o_req_dn.bv_val ) {
135                 ch_free( dn.bv_val );
136         }
137
138         Debug(LDAP_DEBUG_TRACE,"<==backsql_bind()\n",0,0,0);
139         return 0;
140 }
141  
142 #endif /* SLAPD_SQL */
143