]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/bind.c
More porting
[openldap] / servers / slapd / back-mdb / bind.c
1 /* bind.c - mdb backend bind routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2011 The OpenLDAP Foundation.
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
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22
23 #include "back-mdb.h"
24
25 int
26 mdb_bind( Operation *op, SlapReply *rs )
27 {
28         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
29         Entry           *e;
30         Attribute       *a;
31
32         AttributeDescription *password = slap_schema.si_ad_userPassword;
33
34         MDB_txn         *rtxn;
35
36         Debug( LDAP_DEBUG_ARGS,
37                 "==> " LDAP_XSTRING(mdb_bind) ": dn: %s\n",
38                 op->o_req_dn.bv_val, 0, 0);
39
40         /* allow noauth binds */
41         switch ( be_rootdn_bind( op, NULL ) ) {
42         case LDAP_SUCCESS:
43                 /* frontend will send result */
44                 return rs->sr_err = LDAP_SUCCESS;
45
46         default:
47                 /* give the database a chance */
48                 /* NOTE: this behavior departs from that of other backends,
49                  * since the others, in case of password checking failure
50                  * do not give the database a chance.  If an entry with
51                  * rootdn's name does not exist in the database the result
52                  * will be the same.  See ITS#4962 for discussion. */
53                 break;
54         }
55
56         rs->sr_err = mdb_reader_get(op, mdb->mi_dbenv, &rtxn);
57         switch(rs->sr_err) {
58         case 0:
59                 break;
60         default:
61                 rs->sr_text = "internal error";
62                 send_ldap_result( op, rs );
63                 return rs->sr_err;
64         }
65
66         /* get entry with reader lock */
67         rs->sr_err = mdb_dn2entry( op, rtxn, &op->o_req_ndn, &e, 0 );
68
69         switch(rs->sr_err) {
70         case MDB_NOTFOUND:
71                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
72                 send_ldap_result( op, rs );
73                 return rs->sr_err;
74         case 0:
75                 break;
76         case LDAP_BUSY:
77                 send_ldap_error( op, rs, LDAP_BUSY, "ldap_server_busy" );
78                 return LDAP_BUSY;
79         default:
80                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
81                 return rs->sr_err;
82         }
83
84         ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
85
86         /* check for deleted */
87         if ( is_entry_subentry( e ) ) {
88                 /* entry is an subentry, don't allow bind */
89                 Debug( LDAP_DEBUG_TRACE, "entry is subentry\n", 0,
90                         0, 0 );
91                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
92                 goto done;
93         }
94
95         if ( is_entry_alias( e ) ) {
96                 /* entry is an alias, don't allow bind */
97                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
98                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
99                 goto done;
100         }
101
102         if ( is_entry_referral( e ) ) {
103                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
104                         0, 0 );
105                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
106                 goto done;
107         }
108
109         switch ( op->oq_bind.rb_method ) {
110         case LDAP_AUTH_SIMPLE:
111                 a = attr_find( e->e_attrs, password );
112                 if ( a == NULL ) {
113                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
114                         goto done;
115                 }
116
117                 if ( slap_passwd_check( op, e, a, &op->oq_bind.rb_cred,
118                                         &rs->sr_text ) != 0 )
119                 {
120                         /* failure; stop front end from sending result */
121                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
122                         goto done;
123                 }
124                         
125                 rs->sr_err = 0;
126                 break;
127
128         default:
129                 assert( 0 ); /* should not be reachable */
130                 rs->sr_err = LDAP_STRONG_AUTH_NOT_SUPPORTED;
131                 rs->sr_text = "authentication method not supported";
132         }
133
134 done:
135         /* free entry and reader lock */
136         if( e != NULL ) {
137                 mdb_entry_return( e );
138         }
139
140         if ( rs->sr_err ) {
141                 send_ldap_result( op, rs );
142                 if ( rs->sr_ref ) {
143                         ber_bvarray_free( rs->sr_ref );
144                         rs->sr_ref = NULL;
145                 }
146         }
147         /* front end will send result on success (rs->sr_err==0) */
148         return rs->sr_err;
149 }