1*549b59edSchristos /* $NetBSD: compare.c,v 1.2 2021/08/14 16:14:59 christos Exp $ */
2e670fd5cSchristos
3e670fd5cSchristos /* compare.c - compare exop handler for back-asyncmeta */
4e670fd5cSchristos /* $OpenLDAP$ */
5e670fd5cSchristos /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6e670fd5cSchristos *
7e670fd5cSchristos * Copyright 2016-2021 The OpenLDAP Foundation.
8e670fd5cSchristos * Portions Copyright 2016 Symas Corporation.
9e670fd5cSchristos * All rights reserved.
10e670fd5cSchristos *
11e670fd5cSchristos * Redistribution and use in source and binary forms, with or without
12e670fd5cSchristos * modification, are permitted only as authorized by the OpenLDAP
13e670fd5cSchristos * Public License.
14e670fd5cSchristos *
15e670fd5cSchristos * A copy of this license is available in the file LICENSE in the
16e670fd5cSchristos * top-level directory of the distribution or, alternatively, at
17e670fd5cSchristos * <http://www.OpenLDAP.org/license.html>.
18e670fd5cSchristos */
19e670fd5cSchristos
20e670fd5cSchristos /* ACKNOWLEDGEMENTS:
21e670fd5cSchristos + * This work was developed by Symas Corporation
22e670fd5cSchristos + * based on back-meta module for inclusion in OpenLDAP Software.
23e670fd5cSchristos + * This work was sponsored by Ericsson. */
24e670fd5cSchristos
25e670fd5cSchristos #include <sys/cdefs.h>
26*549b59edSchristos __RCSID("$NetBSD: compare.c,v 1.2 2021/08/14 16:14:59 christos Exp $");
27e670fd5cSchristos
28e670fd5cSchristos #include "portable.h"
29e670fd5cSchristos
30e670fd5cSchristos #include <stdio.h>
31e670fd5cSchristos
32e670fd5cSchristos #include <ac/string.h>
33e670fd5cSchristos #include <ac/socket.h>
34e670fd5cSchristos #include "slap.h"
35e670fd5cSchristos #include "../../../libraries/liblber/lber-int.h"
36e670fd5cSchristos #include "../../../libraries/libldap/ldap-int.h"
37e670fd5cSchristos #include "../back-ldap/back-ldap.h"
38e670fd5cSchristos #include "back-asyncmeta.h"
39e670fd5cSchristos
40e670fd5cSchristos meta_search_candidate_t
asyncmeta_back_compare_start(Operation * op,SlapReply * rs,a_metaconn_t * mc,bm_context_t * bc,int candidate,int do_lock)41e670fd5cSchristos asyncmeta_back_compare_start(Operation *op,
42e670fd5cSchristos SlapReply *rs,
43e670fd5cSchristos a_metaconn_t *mc,
44e670fd5cSchristos bm_context_t *bc,
45e670fd5cSchristos int candidate,
46e670fd5cSchristos int do_lock)
47e670fd5cSchristos {
48e670fd5cSchristos a_dncookie dc;
49e670fd5cSchristos a_metainfo_t *mi = mc->mc_info;
50e670fd5cSchristos a_metatarget_t *mt = mi->mi_targets[ candidate ];
51e670fd5cSchristos struct berval c_attr = op->orc_ava->aa_desc->ad_cname;
52e670fd5cSchristos struct berval mdn = BER_BVNULL;
53e670fd5cSchristos struct berval mapped_value = op->orc_ava->aa_value;
54e670fd5cSchristos int rc = 0;
55e670fd5cSchristos LDAPControl **ctrls = NULL;
56e670fd5cSchristos meta_search_candidate_t retcode = META_SEARCH_CANDIDATE;
57e670fd5cSchristos BerElement *ber = NULL;
58e670fd5cSchristos a_metasingleconn_t *msc = &mc->mc_conns[ candidate ];
59e670fd5cSchristos SlapReply *candidates = bc->candidates;
60e670fd5cSchristos ber_int_t msgid;
61e670fd5cSchristos
62e670fd5cSchristos dc.op = op;
63e670fd5cSchristos dc.target = mt;
64e670fd5cSchristos dc.memctx = op->o_tmpmemctx;
65e670fd5cSchristos dc.to_from = MASSAGE_REQ;
66e670fd5cSchristos
67e670fd5cSchristos asyncmeta_dn_massage( &dc, &op->o_req_dn, &mdn );
68e670fd5cSchristos
69e670fd5cSchristos if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
70e670fd5cSchristos asyncmeta_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value );
71e670fd5cSchristos
72e670fd5cSchristos asyncmeta_set_msc_time(msc);
73e670fd5cSchristos ctrls = op->o_ctrls;
74e670fd5cSchristos if ( asyncmeta_controls_add( op, rs, mc, candidate, bc->is_root,&ctrls ) != LDAP_SUCCESS )
75e670fd5cSchristos {
76e670fd5cSchristos candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
77e670fd5cSchristos retcode = META_SEARCH_ERR;
78e670fd5cSchristos goto done;
79e670fd5cSchristos }
80e670fd5cSchristos /* someone might have reset the connection */
81e670fd5cSchristos if (!( LDAP_BACK_CONN_ISBOUND( msc )
82e670fd5cSchristos || LDAP_BACK_CONN_ISANON( msc )) || msc->msc_ld == NULL ) {
83e670fd5cSchristos Debug( asyncmeta_debug, "msc %p not initialized at %s:%d\n", msc, __FILE__, __LINE__ );
84e670fd5cSchristos goto error_unavailable;
85e670fd5cSchristos }
86e670fd5cSchristos
87e670fd5cSchristos ber = ldap_build_compare_req( msc->msc_ld, mdn.bv_val, c_attr.bv_val, &mapped_value,
88e670fd5cSchristos ctrls, NULL, &msgid);
89e670fd5cSchristos
90e670fd5cSchristos if (!ber) {
91e670fd5cSchristos Debug( asyncmeta_debug, "%s asyncmeta_back_compare_start: Operation encoding failed with errno %d\n",
92e670fd5cSchristos op->o_log_prefix, msc->msc_ld->ld_errno );
93e670fd5cSchristos rs->sr_err = LDAP_OPERATIONS_ERROR;
94e670fd5cSchristos rs->sr_text = "Failed to encode proxied request";
95e670fd5cSchristos retcode = META_SEARCH_ERR;
96e670fd5cSchristos goto done;
97e670fd5cSchristos }
98e670fd5cSchristos
99e670fd5cSchristos if (ber) {
100e670fd5cSchristos struct timeval tv = {0, mt->mt_network_timeout*1000};
101e670fd5cSchristos ber_socket_t s;
102e670fd5cSchristos if (!( LDAP_BACK_CONN_ISBOUND( msc )
103e670fd5cSchristos || LDAP_BACK_CONN_ISANON( msc )) || msc->msc_ld == NULL ) {
104e670fd5cSchristos Debug( asyncmeta_debug, "msc %p not initialized at %s:%d\n", msc, __FILE__, __LINE__ );
105e670fd5cSchristos goto error_unavailable;
106e670fd5cSchristos }
107e670fd5cSchristos
108e670fd5cSchristos ldap_get_option( msc->msc_ld, LDAP_OPT_DESC, &s );
109e670fd5cSchristos if (s < 0) {
110e670fd5cSchristos Debug( asyncmeta_debug, "msc %p not initialized at %s:%d\n", msc, __FILE__, __LINE__ );
111e670fd5cSchristos goto error_unavailable;
112e670fd5cSchristos }
113e670fd5cSchristos rc = ldap_int_poll( msc->msc_ld, s, &tv, 1);
114e670fd5cSchristos if (rc < 0) {
115e670fd5cSchristos Debug( asyncmeta_debug, "msc %p not writable within network timeout %s:%d\n", msc, __FILE__, __LINE__ );
116e670fd5cSchristos if ((msc->msc_result_time + META_BACK_RESULT_INTERVAL) < slap_get_time()) {
117e670fd5cSchristos rc = LDAP_SERVER_DOWN;
118e670fd5cSchristos } else {
119e670fd5cSchristos goto error_unavailable;
120e670fd5cSchristos }
121e670fd5cSchristos } else {
122e670fd5cSchristos candidates[ candidate ].sr_msgid = msgid;
123e670fd5cSchristos rc = ldap_send_initial_request( msc->msc_ld, LDAP_REQ_COMPARE,
124e670fd5cSchristos mdn.bv_val, ber, msgid );
125e670fd5cSchristos if (rc == msgid)
126e670fd5cSchristos rc = LDAP_SUCCESS;
127e670fd5cSchristos else
128e670fd5cSchristos rc = LDAP_SERVER_DOWN;
129e670fd5cSchristos ber = NULL;
130e670fd5cSchristos }
131e670fd5cSchristos
132e670fd5cSchristos switch ( rc ) {
133e670fd5cSchristos case LDAP_SUCCESS:
134e670fd5cSchristos retcode = META_SEARCH_CANDIDATE;
135e670fd5cSchristos asyncmeta_set_msc_time(msc);
136e670fd5cSchristos goto done;
137e670fd5cSchristos
138e670fd5cSchristos case LDAP_SERVER_DOWN:
139e670fd5cSchristos /* do not lock if called from asyncmeta_handle_bind_result. Also do not reset the connection */
140e670fd5cSchristos if (do_lock > 0) {
141e670fd5cSchristos ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
142e670fd5cSchristos asyncmeta_reset_msc(NULL, mc, candidate, 0, __FUNCTION__);
143e670fd5cSchristos ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
144e670fd5cSchristos }
145e670fd5cSchristos /* fall though*/
146e670fd5cSchristos default:
147e670fd5cSchristos Debug( asyncmeta_debug, "msc %p ldap_send_initial_request failed. %s:%d\n", msc, __FILE__, __LINE__ );
148e670fd5cSchristos goto error_unavailable;
149e670fd5cSchristos }
150e670fd5cSchristos }
151e670fd5cSchristos
152e670fd5cSchristos error_unavailable:
153e670fd5cSchristos if (ber)
154e670fd5cSchristos ber_free(ber, 1);
155e670fd5cSchristos switch (bc->nretries[candidate]) {
156e670fd5cSchristos case -1: /* nretries = forever */
157e670fd5cSchristos retcode = META_SEARCH_NEED_BIND;
158e670fd5cSchristos break;
159e670fd5cSchristos case 0: /* no retries left */
160e670fd5cSchristos candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
161e670fd5cSchristos rs->sr_err = LDAP_UNAVAILABLE;
162e670fd5cSchristos rs->sr_text = "Unable to send compare request to target";
163e670fd5cSchristos retcode = META_SEARCH_ERR;
164e670fd5cSchristos break;
165e670fd5cSchristos default: /* more retries left - try to rebind and go again */
166e670fd5cSchristos retcode = META_SEARCH_NEED_BIND;
167e670fd5cSchristos bc->nretries[candidate]--;
168e670fd5cSchristos break;
169e670fd5cSchristos }
170e670fd5cSchristos done:
171e670fd5cSchristos (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
172e670fd5cSchristos
173e670fd5cSchristos if ( op->orc_ava->aa_value.bv_val != mapped_value.bv_val ) {
174e670fd5cSchristos op->o_tmpfree( mapped_value.bv_val, op->o_tmpmemctx );
175e670fd5cSchristos }
176e670fd5cSchristos
177e670fd5cSchristos if ( mdn.bv_val != op->o_req_dn.bv_val ) {
178e670fd5cSchristos op->o_tmpfree( mdn.bv_val, op->o_tmpmemctx );
179e670fd5cSchristos }
180e670fd5cSchristos
181e670fd5cSchristos Debug( LDAP_DEBUG_TRACE, "%s <<< asyncmeta_back_compare_start[%p]=%d\n", op->o_log_prefix, msc, candidates[candidate].sr_msgid );
182e670fd5cSchristos return retcode;
183e670fd5cSchristos }
184e670fd5cSchristos
185e670fd5cSchristos int
asyncmeta_back_compare(Operation * op,SlapReply * rs)186e670fd5cSchristos asyncmeta_back_compare( Operation *op, SlapReply *rs )
187e670fd5cSchristos {
188e670fd5cSchristos a_metainfo_t *mi = ( a_metainfo_t * )op->o_bd->be_private;
189e670fd5cSchristos a_metatarget_t *mt;
190e670fd5cSchristos a_metaconn_t *mc;
191e670fd5cSchristos int rc, candidate = -1;
192e670fd5cSchristos void *thrctx = op->o_threadctx;
193e670fd5cSchristos bm_context_t *bc;
194e670fd5cSchristos SlapReply *candidates;
195e670fd5cSchristos time_t current_time = slap_get_time();
196e670fd5cSchristos int max_pending_ops = (mi->mi_max_pending_ops == 0) ? META_BACK_CFG_MAX_PENDING_OPS : mi->mi_max_pending_ops;
197e670fd5cSchristos
198e670fd5cSchristos Debug(LDAP_DEBUG_ARGS, "==> asyncmeta_back_compare: %s\n",
199e670fd5cSchristos op->o_req_dn.bv_val );
200e670fd5cSchristos
201e670fd5cSchristos if (current_time > op->o_time) {
202e670fd5cSchristos Debug( asyncmeta_debug, "==> asyncmeta_back_compare[%s]: o_time:[%ld], current time: [%ld]\n",
203e670fd5cSchristos op->o_log_prefix, op->o_time, current_time );
204e670fd5cSchristos }
205e670fd5cSchristos asyncmeta_new_bm_context(op, rs, &bc, mi->mi_ntargets, mi );
206e670fd5cSchristos if (bc == NULL) {
207e670fd5cSchristos rs->sr_err = LDAP_OTHER;
208e670fd5cSchristos send_ldap_result(op, rs);
209e670fd5cSchristos return rs->sr_err;
210e670fd5cSchristos }
211e670fd5cSchristos
212e670fd5cSchristos candidates = bc->candidates;
213e670fd5cSchristos mc = asyncmeta_getconn( op, rs, candidates, &candidate, LDAP_BACK_DONTSEND, 0);
214e670fd5cSchristos if ( !mc || rs->sr_err != LDAP_SUCCESS) {
215e670fd5cSchristos send_ldap_result(op, rs);
216e670fd5cSchristos return rs->sr_err;
217e670fd5cSchristos }
218e670fd5cSchristos
219e670fd5cSchristos mt = mi->mi_targets[ candidate ];
220e670fd5cSchristos bc->timeout = mt->mt_timeout[ SLAP_OP_COMPARE ];
221e670fd5cSchristos bc->retrying = LDAP_BACK_RETRYING;
222e670fd5cSchristos bc->sendok = ( LDAP_BACK_SENDRESULT | bc->retrying );
223e670fd5cSchristos bc->stoptime = op->o_time + bc->timeout;
224e670fd5cSchristos bc->bc_active = 1;
225e670fd5cSchristos
226e670fd5cSchristos if (mc->pending_ops >= max_pending_ops) {
227e670fd5cSchristos rs->sr_err = LDAP_BUSY;
228e670fd5cSchristos rs->sr_text = "Maximum pending ops limit exceeded";
229e670fd5cSchristos send_ldap_result(op, rs);
230e670fd5cSchristos return rs->sr_err;
231e670fd5cSchristos }
232e670fd5cSchristos
233e670fd5cSchristos ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
234e670fd5cSchristos rc = asyncmeta_add_message_queue(mc, bc);
235e670fd5cSchristos mc->mc_conns[candidate].msc_active++;
236e670fd5cSchristos ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
237e670fd5cSchristos
238e670fd5cSchristos if (rc != LDAP_SUCCESS) {
239e670fd5cSchristos rs->sr_err = LDAP_BUSY;
240e670fd5cSchristos rs->sr_text = "Maximum pending ops limit exceeded";
241e670fd5cSchristos send_ldap_result(op, rs);
242e670fd5cSchristos ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
243e670fd5cSchristos mc->mc_conns[candidate].msc_active--;
244e670fd5cSchristos ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
245e670fd5cSchristos goto finish;
246e670fd5cSchristos }
247e670fd5cSchristos
248e670fd5cSchristos retry:
249e670fd5cSchristos if (bc->timeout && bc->stoptime < slap_get_time()) {
250e670fd5cSchristos int timeout_err;
251e670fd5cSchristos timeout_err = op->o_protocol >= LDAP_VERSION3 ?
252e670fd5cSchristos LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
253e670fd5cSchristos rs->sr_err = timeout_err;
254e670fd5cSchristos rs->sr_text = "Operation timed out before it was sent to target";
255e670fd5cSchristos asyncmeta_error_cleanup(op, rs, bc, mc, candidate);
256e670fd5cSchristos goto finish;
257e670fd5cSchristos }
258e670fd5cSchristos
259e670fd5cSchristos rc = asyncmeta_dobind_init_with_retry(op, rs, bc, mc, candidate);
260e670fd5cSchristos switch (rc)
261e670fd5cSchristos {
262e670fd5cSchristos case META_SEARCH_CANDIDATE:
263e670fd5cSchristos /* target is already bound, just send the request */
264e670fd5cSchristos Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_compare: "
265e670fd5cSchristos "cnd=\"%d\"\n", op->o_log_prefix, candidate );
266e670fd5cSchristos
267e670fd5cSchristos rc = asyncmeta_back_compare_start( op, rs, mc, bc, candidate, 1);
268e670fd5cSchristos if (rc == META_SEARCH_ERR) {
269e670fd5cSchristos asyncmeta_error_cleanup(op, rs, bc, mc, candidate);
270e670fd5cSchristos goto finish;
271e670fd5cSchristos
272e670fd5cSchristos } else if (rc == META_SEARCH_NEED_BIND) {
273e670fd5cSchristos goto retry;
274e670fd5cSchristos }
275e670fd5cSchristos break;
276e670fd5cSchristos case META_SEARCH_NOT_CANDIDATE:
277e670fd5cSchristos Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_compare: NOT_CANDIDATE "
278e670fd5cSchristos "cnd=\"%d\"\n", op->o_log_prefix, candidate );
279e670fd5cSchristos asyncmeta_error_cleanup(op, rs, bc, mc, candidate);
280e670fd5cSchristos goto finish;
281e670fd5cSchristos
282e670fd5cSchristos case META_SEARCH_NEED_BIND:
283e670fd5cSchristos case META_SEARCH_BINDING:
284e670fd5cSchristos Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_compare: BINDING "
285e670fd5cSchristos "cnd=\"%d\" %p\n", op->o_log_prefix, candidate , &mc->mc_conns[candidate]);
286e670fd5cSchristos /* Todo add the context to the message queue but do not send the request
287e670fd5cSchristos the receiver must send this when we are done binding */
288e670fd5cSchristos /* question - how would do receiver know to which targets??? */
289e670fd5cSchristos break;
290e670fd5cSchristos
291e670fd5cSchristos case META_SEARCH_ERR:
292e670fd5cSchristos Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_compare: ERR "
293e670fd5cSchristos "cnd=\"%d\"\n", op->o_log_prefix, candidate );
294e670fd5cSchristos asyncmeta_error_cleanup(op, rs, bc, mc, candidate);
295e670fd5cSchristos goto finish;
296e670fd5cSchristos default:
297e670fd5cSchristos assert( 0 );
298e670fd5cSchristos break;
299e670fd5cSchristos }
300e670fd5cSchristos
301e670fd5cSchristos ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
302e670fd5cSchristos mc->mc_conns[candidate].msc_active--;
303e670fd5cSchristos asyncmeta_start_one_listener(mc, candidates, bc, candidate);
304e670fd5cSchristos bc->bc_active--;
305e670fd5cSchristos ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
306e670fd5cSchristos rs->sr_err = SLAPD_ASYNCOP;
307e670fd5cSchristos finish:
308e670fd5cSchristos return rs->sr_err;
309e670fd5cSchristos }
310