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