2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/socket.h>
28 #include <ac/string.h>
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
35 /* return 0 IFF op_dn is a value in group_at (member) attribute
36 * of entry with gr_dn AND that entry has an objectClass
37 * value of group_oc (groupOfNames)
45 struct berval *gr_ndn,
46 struct berval *op_ndn,
47 ObjectClass *group_oc,
48 AttributeDescription *group_at
51 struct metainfo *li = ( struct metainfo * )be->be_private;
52 int rc = 1, candidate;
54 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
57 char *filter = NULL, *ptr;
59 struct berval mop_ndn = BER_BVNULL, mgr_ndn = BER_BVNULL;
61 struct berval group_oc_name = BER_BVNULL;
62 struct berval group_at_name = group_at->ad_cname;
64 if ( group_oc->soc_names && group_oc->soc_names[ 0 ] ) {
65 group_oc_name.bv_val = group_oc->soc_names[ 0 ];
67 group_oc_name.bv_val = group_oc->soc_oid;
70 if ( group_oc_name.bv_val ) {
71 group_oc_name.bv_len = strlen( group_oc_name.bv_val );
74 if ( target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
75 /* we already have a copy of the entry */
76 /* attribute and objectclass mapping has already been done */
79 * first we need to check if the objectClass attribute
80 * has been retrieved; otherwise we need to repeat the search
82 attr = attr_find( target->e_attrs, ad_objectClass );
86 * Now we can check for the group objectClass value
88 if ( !is_entry_objectclass( target, group_oc, 0 ) ) {
93 * This part has been reworked: the group attr compare
94 * fails only if the attribute is PRESENT but the value
95 * is NOT PRESENT; if the attribute is NOT PRESENT, the
96 * search must be repeated as well.
97 * This may happen if a search for an entry has already
98 * been performed (target is not null) but the group
99 * attribute has not been required
101 attr = attr_find( target->e_attrs, group_at );
102 if ( attr != NULL ) {
103 rc = value_find_ex( group_at,
104 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
105 attr->a_vals, op_ndn );
106 if ( rc != LDAP_SUCCESS ) {
110 } /* else: repeat the search */
111 } /* else: repeat the search */
112 } /* else: do the search */
114 candidate = meta_back_select_unique_candidate( li, gr_ndn );
115 if ( candidate == META_TARGET_NONE ) {
120 * Rewrite the op ndn if needed
122 switch ( rewrite_session( li->targets[ candidate ]->rwinfo, "bindDn",
123 op_ndn->bv_val, conn, &mop_ndn.bv_val ) ) {
124 case REWRITE_REGEXEC_OK:
125 if ( mop_ndn.bv_val != NULL && mop_ndn.bv_val[ 0 ] != '\0' ) {
126 mop_ndn.bv_len = strlen( mop_ndn.bv_val );
130 Debug( LDAP_DEBUG_ARGS,
131 "rw> bindDn (op ndn in group):"
132 " \"%s\" -> \"%s\"\n%s",
133 op_ndn->bv_val, mop_ndn.bv_val, "" );
136 case REWRITE_REGEXEC_UNWILLING:
137 /* continues to next case */
139 case REWRITE_REGEXEC_ERR:
144 * Rewrite the gr ndn if needed
146 switch ( rewrite_session( li->targets[ candidate ]->rwinfo,
148 gr_ndn->bv_val, conn, &mgr_ndn.bv_val ) ) {
149 case REWRITE_REGEXEC_OK:
150 if ( mgr_ndn.bv_val != NULL && mgr_ndn.bv_val[ 0 ] != '\0' ) {
151 mgr_ndn.bv_len = strlen( mgr_ndn.bv_val );
155 Debug( LDAP_DEBUG_ARGS,
156 "rw> searchBase (gr ndn in group):"
157 " \"%s\" -> \"%s\"\n%s",
158 gr_ndn->bv_val, mgr_ndn.bv_val, "" );
161 case REWRITE_REGEXEC_UNWILLING:
162 /* continues to next case */
164 case REWRITE_REGEXEC_ERR:
168 ldap_back_map( &li->targets[ candidate ]->oc_map,
169 &group_oc_name, &group_oc_name, BACKLDAP_MAP );
170 if ( group_oc_name.bv_val == NULL || group_oc_name.bv_val[0] == '\0' ) {
173 ldap_back_map( &li->targets[ candidate ]->at_map,
174 &group_at_name, &group_at_name, BACKLDAP_MAP );
175 if ( group_at_name.bv_val == NULL || group_at_name.bv_val[0] == '\0' ) {
179 filter = ch_malloc( sizeof( "(&(objectclass=)(=))" )
180 + group_oc_name.bv_len
181 + group_at_name.bv_len
182 + mop_ndn.bv_len + 1 );
183 if ( filter == NULL ) {
187 rc = ldap_initialize( &ld, li->targets[ candidate ]->uri );
188 if ( rc != LDAP_SUCCESS ) {
192 rc = ldap_bind_s( ld, li->targets[ candidate ]->binddn.bv_val,
193 li->targets[ candidate ]->bindpw.bv_val,
195 if ( rc != LDAP_SUCCESS ) {
199 ptr = lutil_strcopy( filter, "(&(objectclass=" );
200 ptr = lutil_strcopy( ptr , group_oc_name.bv_val );
201 ptr = lutil_strcopy( ptr , ")(" );
202 ptr = lutil_strcopy( ptr , group_at_name.bv_val );
203 ptr = lutil_strcopy( ptr , "=" );
204 ptr = lutil_strcopy( ptr , mop_ndn.bv_val );
205 strcpy( ptr , "))" );
207 gattr[ 0 ] = "objectclass";
210 if ( ldap_search_ext_s( ld, mgr_ndn.bv_val, LDAP_SCOPE_BASE, filter,
211 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
212 LDAP_NO_LIMIT, &result ) == LDAP_SUCCESS ) {
213 if ( ldap_first_entry( ld, result ) != NULL ) {
216 ldap_msgfree( result );
223 if ( filter != NULL ) {
226 if ( mop_ndn.bv_val != op_ndn->bv_val ) {
227 free( mop_ndn.bv_val );
229 if ( mgr_ndn.bv_val != gr_ndn->bv_val ) {
230 free( mgr_ndn.bv_val );