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