]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/bind.c
unifdef -UNEW_LOGGING
[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-2004 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 #ifdef LDBM_SUBENTRIES
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 #endif
84
85         if ( is_entry_alias( e ) ) {
86                 /* entry is an alias, don't allow bind */
87                 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
88
89 #if 1
90                 rc = LDAP_INVALID_CREDENTIALS;
91 #else
92                 rs->sr_text = "entry is alias";
93                 rc = LDAP_ALIAS_PROBLEM;
94 #endif
95                 goto return_results;
96         }
97
98         if ( is_entry_referral( e ) ) {
99                 /* entry is a referral, don't allow bind */
100                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0, 0, 0 );
101
102                 rc = LDAP_INVALID_CREDENTIALS;
103                 goto return_results;
104         }
105
106         switch ( op->oq_bind.rb_method ) {
107         case LDAP_AUTH_SIMPLE:
108                 if ( ! access_allowed( op, e,
109                         password, NULL, ACL_AUTH, NULL ) )
110                 {
111 #if 1
112                         rc = LDAP_INVALID_CREDENTIALS;
113 #else
114                         rc = LDAP_INSUFFICIENT_ACCESS;
115 #endif
116                         goto return_results;
117                 }
118
119                 if ( (a = attr_find( e->e_attrs, password )) == NULL ) {
120                         /* stop front end from sending result */
121 #if 1
122                         rc = LDAP_INVALID_CREDENTIALS;
123 #else
124                         rc = LDAP_INAPPROPRIATE_AUTH;
125 #endif
126                         goto return_results;
127                 }
128
129                 if ( slap_passwd_check( op->o_conn,
130                         a, &op->oq_bind.rb_cred, &rs->sr_text ) != 0 )
131                 {
132                         /* stop front end from sending result */
133                         rc = LDAP_INVALID_CREDENTIALS;
134                         goto return_results;
135                 }
136
137                 rc = 0;
138                 break;
139
140 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
141         case LDAP_AUTH_KRBV41:
142                 if ( krbv4_ldap_auth( op->o_bd, &op->oq_bind.rb_cred, &ad )
143                         != LDAP_SUCCESS )
144                 {
145                         rc = LDAP_INVALID_CREDENTIALS;
146                         goto return_results;
147                 }
148
149                 if ( ! access_allowed( op, e,
150                         krbattr, NULL, ACL_AUTH, NULL ) )
151                 {
152                         rc = LDAP_INSUFFICIENT_ACCESS;
153                         goto return_results;
154                 }
155
156                 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
157                     : "", ad.pinst, ad.prealm );
158
159                 if ( (a = attr_find( e->e_attrs, krbattr )) == NULL ) {
160                         /*
161                          * no krbname values present:  check against DN
162                          */
163                         if ( strcasecmp( op->o_req_dn.bv_val, krbname ) == 0 ) {
164                                 rc = 0;
165                                 break;
166                         }
167                         rc = LDAP_INAPPROPRIATE_AUTH;
168                         goto return_results;
169
170                 } else {        /* look for krbname match */
171                         struct berval   krbval;
172
173                         krbval.bv_val = krbname;
174                         krbval.bv_len = strlen( krbname );
175
176                         if ( value_find( a->a_desc, a->a_vals, &krbval ) != 0 ) {
177                                 rc = LDAP_INVALID_CREDENTIALS;
178                                 goto return_results;
179                         }
180                 }
181                 rc = 0;
182                 break;
183 #endif
184
185         default:
186                 assert( 0 ); /* should not be reachable */
187                 rs->sr_text = "authentication method not supported";
188                 rc = LDAP_STRONG_AUTH_NOT_SUPPORTED;
189                 goto return_results;
190         }
191
192         ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
193
194 return_results:;
195         /* free entry and reader lock */
196         cache_return_entry_r( &li->li_cache, e );
197         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
198
199         if ( rc ) {
200                 rs->sr_err = rc;
201                 send_ldap_result( op, rs );
202                 if ( rs->sr_ref ) {
203                         ber_bvarray_free( rs->sr_ref );
204                         rs->sr_ref = NULL;
205                 }
206         }
207
208         /* front end will send result on success (rc==0) */
209         return( rc );
210 }
211