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