]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/bind.c
Cleanup result handling
[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;
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  
44         Debug( LDAP_DEBUG_TRACE, "==>backsql_bind()\n", 0, 0, 0 );
45
46         if ( be_isroot_pw( op ) ) {
47                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
48                 Debug( LDAP_DEBUG_TRACE, "<==backsql_bind() root bind\n", 
49                                 0, 0, 0 );
50                 return 0;
51         }
52
53         ber_dupbv( &op->oq_bind.rb_edn, &op->o_req_ndn );
54
55         if ( op->oq_bind.rb_method != LDAP_AUTH_SIMPLE ) {
56                 rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
57                 rs->sr_text = "authentication method not supported"; 
58                 send_ldap_result( op, rs );
59                 return 1;
60         }
61
62         /*
63          * method = LDAP_AUTH_SIMPLE
64          */
65         rs->sr_err = backsql_get_db_conn( op, &dbh );
66         if (!dbh) {
67                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
68                         "could not get connection handle - exiting\n",
69                         0, 0, 0 );
70
71                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
72                         ? "SQL-backend error" : NULL;
73                 send_ldap_result( op, rs );
74                 return 1;
75         }
76
77         rc = backsql_dn2id( bi, &user_id, dbh, &op->o_req_ndn );
78         if ( rc != LDAP_SUCCESS ) {
79                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
80                         "could not retrieve bind dn id - no such entry\n", 
81                         0, 0, 0 );
82                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
83                 send_ldap_result( op, rs );
84                 return 1;
85         }
86
87         anlist[0].an_name = password->ad_cname;
88         anlist[0].an_desc = password;
89         anlist[1].an_name.bv_val = NULL;
90         backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE, 
91                         -1, -1, -1, NULL, dbh, op, anlist );
92         e = backsql_id2entry( &bsi, &user_entry, &user_id );
93         if ( e == NULL ) {
94                 Debug( LDAP_DEBUG_TRACE, "backsql_bind(): "
95                         "error in backsql_id2entry() - auth failed\n",
96                         0, 0, 0 );
97                 rs->sr_err = LDAP_OTHER;
98                 send_ldap_result( op, rs );
99                 return 1;
100         }
101
102         if ( ! access_allowed( op, e, password, NULL, ACL_AUTH, NULL ) ) {
103                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
104                 send_ldap_result( op, rs );
105                 return 1;
106         }
107
108         if ( ( a = attr_find( e->e_attrs, password ) ) == NULL ) {
109                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
110                 send_ldap_result( op, rs );
111                 return 1;
112         }
113
114         if ( slap_passwd_check( op->o_conn, a, &op->oq_bind.rb_cred, &rs->sr_text ) != 0 ) {
115                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
116                 send_ldap_result( op, rs );
117                 return 1;
118         }
119
120         Debug(LDAP_DEBUG_TRACE,"<==backsql_bind()\n",0,0,0);
121         return 0;
122 }
123  
124 #endif /* SLAPD_SQL */
125