]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/bind.c
96c06fdd1e735863194bc9df5cbe100c03e5e6a1
[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-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * Portions Copyright 2002 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Dmitry Kovalev for inclusion
19  * by OpenLDAP Software.  Additional significant contributors include
20  * Pierangelo Masarati.
21  */
22
23 #include "portable.h"
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         SQLHDBC                 dbh = SQL_NULL_HDBC;
35         Entry                   *e = NULL,
36                                 user_entry = { 0 };
37         Attribute               *a;
38         backsql_srch_info       bsi;
39         AttributeName           anlist[2];
40         int                     rc;
41  
42         Debug( LDAP_DEBUG_TRACE, "==>backsql_bind()\n", 0, 0, 0 );
43
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", 
47                                 0, 0, 0 );
48                 return 0;
49         }
50
51         ber_dupbv( &op->oq_bind.rb_edn, &op->o_req_ndn );
52
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 );
57                 return 1;
58         }
59
60         /*
61          * method = LDAP_AUTH_SIMPLE
62          */
63         rs->sr_err = backsql_get_db_conn( op, &dbh );
64         if ( !dbh ) {
65                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
66                         "could not get connection handle - exiting\n",
67                         0, 0, 0 );
68
69                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
70                         ? "SQL-backend error" : NULL;
71                 send_ldap_result( op, rs );
72                 return 1;
73         }
74
75         anlist[0].an_name = slap_schema.si_ad_userPassword->ad_cname;
76         anlist[0].an_desc = slap_schema.si_ad_userPassword;
77         anlist[1].an_name.bv_val = NULL;
78
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,
82                         BACKSQL_ISF_GET_ID );
83         if ( rc != LDAP_SUCCESS ) {
84                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
85                         "could not retrieve bindDN ID - no such entry\n", 
86                         0, 0, 0 );
87                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
88                 send_ldap_result( op, rs );
89                 return 1;
90         }
91
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;
99                 goto error_return;
100         }
101         e = &user_entry;
102
103         a = attr_find( e->e_attrs, slap_schema.si_ad_userPassword );
104         if ( a == NULL ) {
105                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
106                 goto error_return;
107         }
108
109         if ( slap_passwd_check( op, e, a, &op->oq_bind.rb_cred,
110                                 &rs->sr_text ) != 0 )
111         {
112                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
113                 goto error_return;
114         }
115
116 error_return:;
117         if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
118                 (void)backsql_free_entryID( &bsi.bsi_base_id, 0 );
119         }
120
121         if ( e != NULL ) {
122                 entry_clean( e );
123         }
124
125         if ( bsi.bsi_attrs != NULL ) {
126                 op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
127         }
128
129         if ( rs->sr_err ) {
130                 send_ldap_result( op, rs );
131                 return 1;
132         }
133         
134         Debug(LDAP_DEBUG_TRACE,"<==backsql_bind()\n",0,0,0);
135
136         return 0;
137 }
138