1 /* bind.c - ldbm backend bind and unbind routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
27 #include "back-ldbm.h"
28 #include "proto-back-ldbm.h"
35 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
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;
46 AttributeDescription *password = slap_schema.si_ad_userPassword;
49 LDAP_LOG( BACK_LDBM, ENTRY,
50 "ldbm_back_bind: dn: %s.\n", op->o_req_dn.bv_val, 0, 0 );
52 Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_bind: dn: %s\n", op->o_req_dn.bv_val, 0, 0);
55 if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
56 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
57 /* front end will send result */
61 /* grab giant lock for reading */
62 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
64 /* get entry with reader lock */
65 if ( (e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched )) == NULL ) {
66 if( matched != NULL ) {
67 rs->sr_matched = ch_strdup( matched->e_dn );
69 rs->sr_ref = is_entry_referral( matched )
70 ? get_entry_referrals( op, matched )
73 cache_return_entry_r( &li->li_cache, matched );
76 rs->sr_ref = referral_rewrite( default_referral,
77 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
80 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
82 /* allow noauth binds */
84 if ( rs->sr_ref != NULL ) {
85 rs->sr_err = LDAP_REFERRAL;
87 rs->sr_err = LDAP_INVALID_CREDENTIALS;
89 send_ldap_result( op, rs );
91 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
92 if ( rs->sr_matched ) free( (char *)rs->sr_matched );
94 rs->sr_matched = NULL;
98 ber_dupbv( &op->oq_bind.rb_edn, &e->e_name );
100 /* check for deleted */
101 #ifdef LDBM_SUBENTRIES
102 if ( is_entry_subentry( e ) ) {
103 /* entry is an subentry, don't allow bind */
105 LDAP_LOG ( OPERATION, DETAIL1,
106 "bdb_bind: entry is subentry\n", 0, 0, 0 );
108 Debug( LDAP_DEBUG_TRACE,
109 "entry is subentry\n", 0, 0, 0 );
111 rs->sr_err = LDAP_INVALID_CREDENTIALS;
112 send_ldap_result( op, rs );
113 rc = LDAP_INVALID_CREDENTIALS;
118 if ( is_entry_alias( e ) ) {
119 /* entry is an alias, don't allow bind */
121 LDAP_LOG( BACK_LDBM, INFO,
122 "ldbm_back_bind: entry (%s) is an alias.\n", e->e_name.bv_val, 0, 0 );
124 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0,
129 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
132 rc = LDAP_ALIAS_PROBLEM;
136 if ( is_entry_referral( e ) ) {
137 /* entry is a referral, don't allow bind */
138 rs->sr_ref = get_entry_referrals( op, e );
141 LDAP_LOG( BACK_LDBM, INFO,
142 "ldbm_back_bind: entry(%s) is a referral.\n", e->e_dn, 0, 0 );
144 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
149 if( rs->sr_ref != NULL ) {
150 rs->sr_err = LDAP_REFERRAL;
151 rs->sr_matched = e->e_name.bv_val;
154 rs->sr_err = LDAP_INVALID_CREDENTIALS;
156 send_ldap_result( op, rs );
158 ber_bvarray_free( rs->sr_ref );
159 rs->sr_matched = NULL;
165 switch ( op->oq_bind.rb_method ) {
166 case LDAP_AUTH_SIMPLE:
167 if ( ! access_allowed( op, e,
168 password, NULL, ACL_AUTH, NULL ) )
170 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
171 rc = LDAP_INSUFFICIENT_ACCESS;
175 if ( (a = attr_find( e->e_attrs, password )) == NULL ) {
176 send_ldap_error( op, rs, LDAP_INAPPROPRIATE_AUTH, NULL );
178 /* stop front end from sending result */
179 rc = LDAP_INAPPROPRIATE_AUTH;
183 if ( slap_passwd_check( op->o_conn, a, &op->oq_bind.rb_cred, &rs->sr_text ) != 0 ) {
184 send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
185 /* stop front end from sending result */
186 rc = LDAP_INVALID_CREDENTIALS;
193 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
194 case LDAP_AUTH_KRBV41:
195 if ( krbv4_ldap_auth( op->o_bd, &op->oq_bind.rb_cred, &ad ) != LDAP_SUCCESS ) {
196 send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
197 rc = LDAP_INVALID_CREDENTIALS;
201 if ( ! access_allowed( op, e,
202 krbattr, NULL, ACL_AUTH, NULL ) )
204 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
206 rc = LDAP_INSUFFICIENT_ACCESS;
210 sprintf( krbname, "%s%s%s@%s", ad.pname, *ad.pinst ? "."
211 : "", ad.pinst, ad.prealm );
213 if ( (a = attr_find( e->e_attrs, krbattr )) == NULL ) {
215 * no krbname values present: check against DN
217 if ( strcasecmp( op->o_req_dn.bv_val, krbname ) == 0 ) {
221 send_ldap_error( op, rs, LDAP_INAPPROPRIATE_AUTH, NULL );
222 rc = LDAP_INAPPROPRIATE_AUTH;
225 } else { /* look for krbname match */
226 struct berval krbval;
228 krbval.bv_val = krbname;
229 krbval.bv_len = strlen( krbname );
231 if ( value_find( a->a_desc, a->a_vals, &krbval ) != 0 ) {
232 send_ldap_error( op, rs,
233 LDAP_INVALID_CREDENTIALS, NULL );
234 rc = LDAP_INVALID_CREDENTIALS;
241 case LDAP_AUTH_KRBV42:
242 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
243 "Kerberos bind step 2 not supported" );
244 /* stop front end from sending result */
245 rc = LDAP_UNWILLING_TO_PERFORM;
250 send_ldap_error( op, rs, LDAP_STRONG_AUTH_NOT_SUPPORTED,
251 "authentication method not supported" );
252 rc = LDAP_STRONG_AUTH_NOT_SUPPORTED;
257 /* free entry and reader lock */
258 cache_return_entry_r( &li->li_cache, e );
259 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
261 /* front end will send result on success (rc==0) */