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