1 /* add.c - add request handler for back-asyncmeta */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2016-2017 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. */
28 #include <ac/string.h>
29 #include <ac/socket.h>
32 #include "../back-ldap/back-ldap.h"
33 #include "back-asyncmeta.h"
35 #include "../../../libraries/liblber/lber-int.h"
36 #include "../../../libraries/libldap/ldap-int.h"
39 asyncmeta_sender_error(Operation *op,
46 send_ldap_result(op, rs);
49 meta_search_candidate_t
50 asyncmeta_back_add_start(Operation *op,
62 a_metainfo_t *mi = mc->mc_info;
63 a_metatarget_t *mt = mi->mi_targets[ candidate ];
65 meta_search_candidate_t retcode = META_SEARCH_CANDIDATE;
66 BerElement *ber = NULL;
67 a_metasingleconn_t *msc = &mc->mc_conns[ candidate ];
68 SlapReply *candidates = bc->candidates;
70 LDAPControl **ctrls = NULL;
81 switch (asyncmeta_dn_massage( &dc, &bc->op->o_req_dn, &mdn ) )
85 case LDAP_UNWILLING_TO_PERFORM:
86 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
87 rs->sr_text = "Operation not allowed";
88 retcode = META_SEARCH_ERR;
91 rs->sr_err = LDAP_NO_SUCH_OBJECT;
92 retcode = META_SEARCH_NOT_CANDIDATE;
96 /* Count number of attributes in entry ( +1 ) */
97 for ( i = 1, a = op->ora_e->e_attrs; a; i++, a = a->a_next );
99 /* Create array of LDAPMods for ldap_add() */
100 attrs = ch_malloc( sizeof( LDAPMod * )*i );
102 dc.ctx = "addAttrDN";
103 isupdate = be_shadow_update( op );
104 for ( i = 0, a = op->ora_e->e_attrs; a; a = a->a_next ) {
107 if ( !isupdate && !get_relax( op ) && a->a_desc->ad_type->sat_no_user_mod )
112 if ( a->a_desc == slap_schema.si_ad_objectClass
113 || a->a_desc == slap_schema.si_ad_structuralObjectClass )
116 mapped = a->a_desc->ad_cname;
119 asyncmeta_map( &mt->mt_rwmap.rwm_at,
120 &a->a_desc->ad_cname, &mapped, BACKLDAP_MAP );
121 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
126 attrs[ i ] = ch_malloc( sizeof( LDAPMod ) );
127 if ( attrs[ i ] == NULL ) {
130 attrs[ i ]->mod_op = LDAP_MOD_BVALUES;
131 attrs[ i ]->mod_type = mapped.bv_val;
134 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ );
136 attrs[ i ]->mod_bvalues =
137 (struct berval **)ch_malloc( ( j + 1 ) *
138 sizeof( struct berval * ) );
139 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); ) {
140 struct ldapmapping *mapping;
142 asyncmeta_mapping( &mt->mt_rwmap.rwm_oc,
143 &a->a_vals[ j ], &mapping, BACKLDAP_MAP );
145 if ( mapping == NULL ) {
146 if ( mt->mt_rwmap.rwm_oc.drop_missing ) {
149 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
152 attrs[ i ]->mod_bvalues[ j ] = &mapping->dst;
156 attrs[ i ]->mod_bvalues[ j ] = NULL;
160 * FIXME: dn-valued attrs should be rewritten
161 * to allow their use in ACLs at the back-ldap
164 if ( a->a_desc->ad_type->sat_syntax ==
165 slap_schema.si_syn_distinguishedName )
167 (void)asyncmeta_dnattr_rewrite( &dc, a->a_vals );
168 if ( a->a_vals == NULL ) {
173 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ )
176 attrs[ i ]->mod_bvalues = ch_malloc( ( j + 1 ) * sizeof( struct berval * ) );
177 for ( j = 0; !BER_BVISNULL( &a->a_vals[ j ] ); j++ ) {
178 attrs[ i ]->mod_bvalues[ j ] = &a->a_vals[ j ];
180 attrs[ i ]->mod_bvalues[ j ] = NULL;
188 if ( asyncmeta_controls_add( op, rs, mc, candidate, &ctrls ) != LDAP_SUCCESS )
190 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
191 retcode = META_SEARCH_ERR;
195 ber = ldap_build_add_req( msc->msc_ld, mdn.bv_val, attrs, ctrls, NULL, &msgid);
197 candidates[ candidate ].sr_msgid = msgid;
198 rc = ldap_send_initial_request( msc->msc_ld, LDAP_REQ_ADD,
199 mdn.bv_val, ber, msgid );
203 rc = LDAP_SERVER_DOWN;
207 retcode = META_SEARCH_CANDIDATE;
208 asyncmeta_set_msc_time(msc);
211 case LDAP_SERVER_DOWN:
212 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
213 asyncmeta_clear_one_msc(NULL, mc, candidate);
214 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
215 if ( nretries && asyncmeta_retry( op, rs, &mc, candidate, LDAP_BACK_DONTSEND ) ) {
217 /* if the identity changed, there might be need to re-authz */
218 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
223 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
224 retcode = META_SEARCH_ERR;
230 (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
232 for ( --i; i >= 0; --i ) {
233 free( attrs[ i ]->mod_bvalues );
237 if ( mdn.bv_val != op->ora_e->e_dn ) {
243 Debug( LDAP_DEBUG_TRACE, "%s <<< asyncmeta_back_add_start[%p]=%d\n", op->o_log_prefix, msc, candidates[candidate].sr_msgid );
249 asyncmeta_back_add( Operation *op, SlapReply *rs )
251 a_metainfo_t *mi = ( a_metainfo_t * )op->o_bd->be_private;
254 int rc, candidate = -1;
255 OperationBuffer opbuf;
257 SlapReply *candidates;
258 slap_callback *cb = op->o_callback;
260 Debug(LDAP_DEBUG_ARGS, "==> asyncmeta_back_add: %s\n",
261 op->o_req_dn.bv_val, 0, 0 );
263 asyncmeta_new_bm_context(op, rs, &bc, mi->mi_ntargets );
265 rs->sr_err = LDAP_OTHER;
266 asyncmeta_sender_error(op, rs, cb);
270 candidates = bc->candidates;
271 mc = asyncmeta_getconn( op, rs, candidates, &candidate, LDAP_BACK_DONTSEND, 0);
272 if ( !mc || rs->sr_err != LDAP_SUCCESS) {
273 asyncmeta_sender_error(op, rs, cb);
274 asyncmeta_clear_bm_context(bc);
278 mt = mi->mi_targets[ candidate ];
279 bc->timeout = mt->mt_timeout[ SLAP_OP_ADD ];
280 bc->retrying = LDAP_BACK_RETRYING;
281 bc->sendok = ( LDAP_BACK_SENDRESULT | bc->retrying );
282 bc->stoptime = op->o_time + bc->timeout;
284 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
285 rc = asyncmeta_add_message_queue(mc, bc);
286 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
288 if (rc != LDAP_SUCCESS) {
289 rs->sr_err = LDAP_BUSY;
290 rs->sr_text = "Maximum pending ops limit exceeded";
291 asyncmeta_clear_bm_context(bc);
292 asyncmeta_sender_error(op, rs, cb);
296 rc = asyncmeta_dobind_init_with_retry(op, rs, bc, mc, candidate);
299 case META_SEARCH_CANDIDATE:
300 /* target is already bound, just send the request */
301 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_add: "
302 "cnd=\"%ld\"\n", op->o_log_prefix, candidate , 0);
304 rc = asyncmeta_back_add_start( op, rs, mc, bc, candidate);
305 if (rc == META_SEARCH_ERR) {
306 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
307 asyncmeta_drop_bc(mc, bc);
308 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
309 asyncmeta_sender_error(op, rs, cb);
310 asyncmeta_clear_bm_context(bc);
315 case META_SEARCH_NOT_CANDIDATE:
316 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_add: NOT_CANDIDATE "
317 "cnd=\"%ld\"\n", op->o_log_prefix, candidate , 0);
318 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
319 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
320 asyncmeta_drop_bc(mc, bc);
321 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
322 asyncmeta_sender_error(op, rs, cb);
323 asyncmeta_clear_bm_context(bc);
326 case META_SEARCH_NEED_BIND:
327 case META_SEARCH_CONNECTING:
328 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_add: NEED_BIND "
329 "cnd=\"%ld\" %p\n", op->o_log_prefix, candidate , &mc->mc_conns[candidate]);
330 rc = asyncmeta_dobind_init(op, rs, bc, mc, candidate);
331 if (rc == META_SEARCH_ERR) {
332 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
333 asyncmeta_drop_bc(mc, bc);
334 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
335 asyncmeta_sender_error(op, rs, cb);
336 asyncmeta_clear_bm_context(bc);
340 case META_SEARCH_BINDING:
341 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_add: BINDING "
342 "cnd=\"%ld\" %p\n", op->o_log_prefix, candidate , &mc->mc_conns[candidate]);
343 /* Todo add the context to the message queue but do not send the request
344 the receiver must send this when we are done binding */
345 /* question - how would do receiver know to which targets??? */
348 case META_SEARCH_ERR:
349 Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_back_add: ERR "
350 "cnd=\"%ldd\"\n", op->o_log_prefix, candidate , 0);
351 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
352 candidates[ candidate ].sr_type = REP_RESULT;
353 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
354 asyncmeta_drop_bc(mc, bc);
355 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);
356 asyncmeta_sender_error(op, rs, cb);
357 asyncmeta_clear_bm_context(bc);
364 ldap_pvt_thread_mutex_lock( &mc->mc_om_mutex);
365 asyncmeta_start_one_listener(mc, candidates, bc, candidate);
366 ldap_pvt_thread_mutex_unlock( &mc->mc_om_mutex);