]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/bind.c
ITS#4310 seems to affect also back-ldbm
[openldap] / servers / slapd / back-ldbm / bind.c
1 /* bind.c - ldbm backend bind and unbind routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2006 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
21 #include <ac/krb.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
25
26 #include "slap.h"
27 #include "back-ldbm.h"
28 #include "proto-back-ldbm.h"
29
30 int
31 ldbm_back_bind(
32     Operation           *op,
33     SlapReply           *rs )
34 {
35         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
36         Entry           *e;
37         Attribute       *a;
38         int             rc;
39         Entry           *matched;
40 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
41         char            krbname[MAX_K_NAME_SZ + 1];
42         AttributeDescription *krbattr = slap_schema.si_ad_krbName;
43         AUTH_DAT        ad;
44 #endif
45
46         AttributeDescription *password = slap_schema.si_ad_userPassword;
47
48         Debug(LDAP_DEBUG_ARGS,
49                 "==> ldbm_back_bind: dn: %s\n", op->o_req_dn.bv_val, 0, 0);
50
51         if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
52                 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
53                 /* front end will send result */
54                 return LDAP_SUCCESS;
55         }
56
57         /* grab giant lock for reading */
58         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
59
60         /* get entry with reader lock */
61         if ( (e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched )) == NULL ) {
62                 if( matched != NULL ) {
63                         cache_return_entry_r( &li->li_cache, matched );
64                 }
65                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
66
67                 /* allow noauth binds */
68                 rc = 1;
69                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
70                 send_ldap_result( op, rs );
71                 return rs->sr_err;
72         }
73
74         /* check for deleted */
75
76         if ( is_entry_subentry( e ) ) {
77                 /* entry is an subentry, don't allow bind */
78                 Debug( LDAP_DEBUG_TRACE,
79                                 "entry is subentry\n", 0, 0, 0 );
80                 rc = LDAP_INVALID_CREDENTIALS;
81                 goto return_results;
82         }
83
84         if ( is_entry_alias( e ) ) {
85                 /* entry is an alias, don't allow bind */
86                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
87
88 #if 1
89                 rc = LDAP_INVALID_CREDENTIALS;
90 #else
91                 rs->sr_text = "entry is alias";
92                 rc = LDAP_ALIAS_PROBLEM;
93 #endif
94                 goto return_results;
95         }
96
97         if ( is_entry_referral( e ) ) {
98                 /* entry is a referral, don't allow bind */
99                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0, 0, 0 );
100
101                 rc = LDAP_INVALID_CREDENTIALS;
102                 goto return_results;
103         }
104
105         switch ( op->oq_bind.rb_method ) {
106         case LDAP_AUTH_SIMPLE:
107                 if ( (a = attr_find( e->e_attrs, password )) == NULL ) {
108                         /* stop front end from sending result */
109                         rc = LDAP_INVALID_CREDENTIALS;
110                         goto return_results;
111                 }
112
113                 if ( slap_passwd_check( op, e, a, &op->oq_bind.rb_cred,
114                                         &rs->sr_text ) != 0 )
115                 {
116                         /* failure; stop front end from sending result */
117                         rc = LDAP_INVALID_CREDENTIALS;
118                         goto return_results;
119                 }
120
121                 rc = 0;
122                 break;
123
124 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
125         case LDAP_AUTH_KRBV41:
126                 if ( krbv4_ldap_auth( op->o_bd, &op->oq_bind.rb_cred, &ad )
127                         != LDAP_SUCCESS )
128                 {
129                         rc = LDAP_INVALID_CREDENTIALS;
130                         goto return_results;
131                 }
132
133                 if ( ! access_allowed( op, e,
134                         krbattr, NULL, ACL_AUTH, NULL ) )
135                 {
136                         rc = LDAP_INSUFFICIENT_ACCESS;
137                         goto return_results;
138                 }
139
140                 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
141                     : "", ad.pinst, ad.prealm );
142
143                 if ( (a = attr_find( e->e_attrs, krbattr )) == NULL ) {
144                         /*
145                          * no krbname values present:  check against DN
146                          */
147                         if ( strcasecmp( op->o_req_dn.bv_val, krbname ) == 0 ) {
148                                 rc = 0;
149                                 break;
150                         }
151                         rc = LDAP_INAPPROPRIATE_AUTH;
152                         goto return_results;
153
154                 } else {        /* look for krbname match */
155                         struct berval   krbval;
156
157                         krbval.bv_val = krbname;
158                         krbval.bv_len = strlen( krbname );
159
160                         if ( value_find( a->a_desc, a->a_vals, &krbval ) != 0 ) {
161                                 rc = LDAP_INVALID_CREDENTIALS;
162                                 goto return_results;
163                         }
164                 }
165                 rc = 0;
166                 break;
167 #endif
168
169         default:
170                 assert( 0 ); /* should not be reachable */
171                 rs->sr_text = "authentication method not supported";
172                 rc = LDAP_STRONG_AUTH_NOT_SUPPORTED;
173                 goto return_results;
174         }
175
176         ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
177
178 return_results:;
179         /* free entry and reader lock */
180         cache_return_entry_r( &li->li_cache, e );
181         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
182
183         if ( rc ) {
184                 rs->sr_err = rc;
185                 send_ldap_result( op, rs );
186                 if ( rs->sr_ref ) {
187                         ber_bvarray_free( rs->sr_ref );
188                         rs->sr_ref = NULL;
189                 }
190         }
191
192         /* front end will send result on success (rc==0) */
193         return( rc );
194 }
195