1 /* compare.c - compare exop handler for back-asyncmeta */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2016 The OpenLDAP Foundation.
6 * Portions Copyright 2016 Symas Corporation.
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>.
19 + * This work was developed by Symas Corporation
20 + * based on back-meta module for inclusion in OpenLDAP Software.
21 + * This work was sponsored by Ericsson. */
27 #include <ac/string.h>
28 #include <ac/socket.h>
31 #include "../back-ldap/back-ldap.h"
32 #include "back-asyncmeta.h"
33 #include "../../../libraries/liblber/lber-int.h"
34 #include "../../../libraries/libldap/ldap-int.h"
36 meta_search_candidate_t
37 asyncmeta_back_compare_start(Operation *op,
44 a_metainfo_t *mi = mc->mc_info;
45 a_metatarget_t *mt = mi->mi_targets[ candidate ];
46 struct berval mdn = BER_BVNULL;
47 struct berval mapped_attr = op->orc_ava->aa_desc->ad_cname;
48 struct berval mapped_value = op->orc_ava->aa_value;
49 int rc = 0, nretries = 1;
50 LDAPControl **ctrls = NULL;
51 meta_search_candidate_t retcode = META_SEARCH_CANDIDATE;
52 BerElement *ber = NULL;
53 a_metasingleconn_t *msc = &mc->mc_conns[ candidate ];
54 SlapReply *candidates = bc->candidates;
62 switch ( asyncmeta_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
63 case LDAP_UNWILLING_TO_PERFORM:
64 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
65 retcode = META_SEARCH_ERR;
72 * if attr is objectClass, try to remap the value
74 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass ) {
75 asyncmeta_map( &mt->mt_rwmap.rwm_oc,
76 &op->orc_ava->aa_value,
77 &mapped_value, BACKLDAP_MAP );
79 if ( BER_BVISNULL( &mapped_value ) || BER_BVISEMPTY( &mapped_value ) ) {
80 rs->sr_err = LDAP_OTHER;
81 retcode = META_SEARCH_ERR;
86 * else try to remap the attribute
89 asyncmeta_map( &mt->mt_rwmap.rwm_at,
90 &op->orc_ava->aa_desc->ad_cname,
91 &mapped_attr, BACKLDAP_MAP );
92 if ( BER_BVISNULL( &mapped_attr ) || BER_BVISEMPTY( &mapped_attr ) ) {
93 rs->sr_err = LDAP_OTHER;
94 retcode = META_SEARCH_ERR;
98 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
100 dc.ctx = "compareAttrDN";
102 switch ( asyncmeta_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value ) )
104 case LDAP_UNWILLING_TO_PERFORM:
105 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
106 retcode = META_SEARCH_ERR;
116 if ( asyncmeta_controls_add( op, rs, mc, candidate, &ctrls ) != LDAP_SUCCESS )
118 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
119 retcode = META_SEARCH_ERR;
123 ber = ldap_build_compare_req( msc->msc_ld, mdn.bv_val, mapped_attr.bv_val, &mapped_value,
124 ctrls, NULL, &msgid);
126 candidates[ candidate ].sr_msgid = msgid;
127 rc = ldap_send_initial_request( msc->msc_ld, LDAP_REQ_COMPARE,
128 mdn.bv_val, ber, msgid );
132 rc = LDAP_SERVER_DOWN;
136 retcode = META_SEARCH_CANDIDATE;
137 asyncmeta_set_msc_time(msc);
140 case LDAP_SERVER_DOWN:
141 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
142 asyncmeta_clear_one_msc(NULL, mc, candidate);
143 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
144 if ( nretries && asyncmeta_retry( op, rs, &mc, candidate, LDAP_BACK_DONTSEND ) ) {
146 /* if the identity changed, there might be need to re-authz */
147 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
152 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
153 retcode = META_SEARCH_ERR;
157 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
159 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
163 if ( op->orc_ava->aa_value.bv_val != mapped_value.bv_val ) {
164 free( mapped_value.bv_val );
168 Debug( LDAP_DEBUG_TRACE, "%s <<< asyncmeta_back_compare_start[%p]=%d\n", op->o_log_prefix, msc, candidates[candidate].sr_msgid );
173 asyncmeta_back_compare( Operation *op, SlapReply *rs )
175 a_metainfo_t *mi = ( a_metainfo_t * )op->o_bd->be_private;
178 int rc, candidate = -1;
179 OperationBuffer opbuf;
181 SlapReply *candidates;
182 slap_callback *cb = op->o_callback;
184 Debug(LDAP_DEBUG_ARGS, "==> asyncmeta_back_compare: %s\n",
185 op->o_req_dn.bv_val, 0, 0 );
187 asyncmeta_new_bm_context(op, rs, &bc, mi->mi_ntargets );
189 rs->sr_err = LDAP_OTHER;
190 asyncmeta_sender_error(op, rs, cb);
194 candidates = bc->candidates;
195 mc = asyncmeta_getconn( op, rs, candidates, &candidate, LDAP_BACK_DONTSEND, 0);
196 if ( !mc || rs->sr_err != LDAP_SUCCESS) {
197 asyncmeta_sender_error(op, rs, cb);
198 asyncmeta_clear_bm_context(bc);
202 mt = mi->mi_targets[ candidate ];
203 bc->timeout = mt->mt_timeout[ SLAP_OP_COMPARE ];
204 bc->retrying = LDAP_BACK_RETRYING;
205 bc->sendok = ( LDAP_BACK_SENDRESULT | bc->retrying );
206 bc->stoptime = op->o_time + bc->timeout;
208 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
209 rc = asyncmeta_add_message_queue(mc, bc);
210 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
212 if (rc != LDAP_SUCCESS) {
213 rs->sr_err = LDAP_BUSY;
214 rs->sr_text = "Maximum pending ops limit exceeded";
215 asyncmeta_clear_bm_context(bc);
216 asyncmeta_sender_error(op, rs, cb);
220 rc = asyncmeta_dobind_init_with_retry(op, rs, bc, mc, candidate);
223 case META_SEARCH_CANDIDATE:
224 /* target is already bound, just send the request */
225 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_compare: "
226 "cnd=\"%ld\"\n", op->o_log_prefix, candidate , 0);
228 rc = asyncmeta_back_compare_start( op, rs, mc, bc, candidate);
229 if (rc == META_SEARCH_ERR) {
230 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
231 asyncmeta_drop_bc(mc, bc);
232 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
233 asyncmeta_sender_error(op, rs, cb);
234 asyncmeta_clear_bm_context(bc);
239 case META_SEARCH_NOT_CANDIDATE:
240 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_compare: NOT_CANDIDATE "
241 "cnd=\"%ld\"\n", op->o_log_prefix, candidate , 0);
242 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
243 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
244 asyncmeta_drop_bc(mc, bc);
245 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
246 asyncmeta_sender_error(op, rs, cb);
247 asyncmeta_clear_bm_context(bc);
250 case META_SEARCH_NEED_BIND:
251 case META_SEARCH_CONNECTING:
252 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_compare: NEED_BIND "
253 "cnd=\"%ld\" %p\n", op->o_log_prefix, candidate , &mc->mc_conns[candidate]);
254 rc = asyncmeta_dobind_init(op, rs, bc, mc, candidate);
255 if (rc == META_SEARCH_ERR) {
256 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
257 asyncmeta_drop_bc(mc, bc);
258 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
259 asyncmeta_sender_error(op, rs, cb);
260 asyncmeta_clear_bm_context(bc);
264 case META_SEARCH_BINDING:
265 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_compare: BINDING "
266 "cnd=\"%ld\" %p\n", op->o_log_prefix, candidate , &mc->mc_conns[candidate]);
267 /* Todo add the context to the message queue but do not send the request
268 the receiver must send this when we are done binding */
269 /* question - how would do receiver know to which targets??? */
272 case META_SEARCH_ERR:
273 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_compare: ERR "
274 "cnd=\"%ldd\"\n", op->o_log_prefix, candidate , 0);
275 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
276 candidates[ candidate ].sr_type = REP_RESULT;
277 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
278 asyncmeta_drop_bc(mc, bc);
279 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
280 asyncmeta_sender_error(op, rs, cb);
281 asyncmeta_clear_bm_context(bc);
287 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
288 asyncmeta_start_one_listener(mc, candidates, candidate);
289 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);