slapi_ops.c 24.1 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* $OpenLDAP$ */
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 2002-2019 The OpenLDAP Foundation.
5
6
7
 * Portions Copyright 1997,2002-2003 IBM Corporation.
 * All rights reserved.
 *
8
9
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
10
11
12
13
14
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
 */
16
17
18
19
20
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by IBM Corporation for use in
 * IBM products and subsequently ported to OpenLDAP Software by
 * Steve Omrani.  Additional significant contributors include:
 *   Luke Howard
Luke Howard's avatar
Luke Howard committed
21
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
22
23

#include "portable.h"
24
25
26
27
28
29

#include <ac/string.h>
#include <ac/stdarg.h>
#include <ac/ctype.h>
#include <ac/unistd.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
30
#include <slap.h>
31
#include <lber_pvt.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
32
33
#include <slapi.h>

34
35
#ifdef LDAP_SLAPI

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
36
static struct Listener slapi_listener = {
37
38
	BER_BVC("slapi://"),
	BER_BVC("slapi://")
39
};
Kurt Zeilenga's avatar
Kurt Zeilenga committed
40

41
42
static LDAPControl **
slapi_int_dup_controls( LDAPControl **controls )
43
{
44
45
	LDAPControl **c;
	size_t i;
46

47
48
	if ( controls == NULL )
		return NULL;
49

50
51
52
53
54
55
56
	for ( i = 0; controls[i] != NULL; i++ )
		;

	c = (LDAPControl **) slapi_ch_calloc( i + 1, sizeof(LDAPControl *) );

	for ( i = 0; controls[i] != NULL; i++ ) {
		c[i] = slapi_dup_control( controls[i] );
57
	}
58
59

	return c;
60
61
62
63
}

static int
slapi_int_result(
Kurt Zeilenga's avatar
Kurt Zeilenga committed
64
	Operation	*op, 
Luke Howard's avatar
Luke Howard committed
65
	SlapReply	*rs )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
66
{
67
	Slapi_PBlock		*pb = SLAPI_OPERATION_PBLOCK( op );
Luke Howard's avatar
Luke Howard committed
68
69
	plugin_result_callback	prc = NULL;
	void			*callback_data = NULL;
70
	LDAPControl		**ctrls = NULL;
Luke Howard's avatar
Luke Howard committed
71

72
	assert( pb != NULL );	
Luke Howard's avatar
Luke Howard committed
73

74
75
	slapi_pblock_get( pb, SLAPI_X_INTOP_RESULT_CALLBACK, (void **)&prc );
	slapi_pblock_get( pb, SLAPI_X_INTOP_CALLBACK_DATA,   &callback_data );
Luke Howard's avatar
Luke Howard committed
76

77
78
79
	/* we need to duplicate controls because they might go out of scope */
	ctrls = slapi_int_dup_controls( rs->sr_ctrls );
	slapi_pblock_set( pb, SLAPI_RESCONTROLS, ctrls );
Luke Howard's avatar
Luke Howard committed
80
81
82

	if ( prc != NULL ) {
		(*prc)( rs->sr_err, callback_data );
83
	}
84

85
	return rs->sr_err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
86
87
88
}

static int
89
slapi_int_search_entry(
Luke Howard's avatar
Luke Howard committed
90
91
	Operation	*op,
	SlapReply	*rs )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
92
{
93
	Slapi_PBlock			*pb = SLAPI_OPERATION_PBLOCK( op );
Luke Howard's avatar
Luke Howard committed
94
95
	plugin_search_entry_callback	psec = NULL;
	void				*callback_data = NULL;
96
	int				rc = LDAP_SUCCESS;
97

98
	assert( pb != NULL );
Luke Howard's avatar
Luke Howard committed
99

100
101
	slapi_pblock_get( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK, (void **)&psec );
	slapi_pblock_get( pb, SLAPI_X_INTOP_CALLBACK_DATA,         &callback_data );
Luke Howard's avatar
Luke Howard committed
102
103

	if ( psec != NULL ) {
104
		rc = (*psec)( rs->sr_entry, callback_data );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
105
106
	}

107
	return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
108
109
110
}

static int
111
slapi_int_search_reference(
Luke Howard's avatar
Luke Howard committed
112
	Operation	*op,	
113
	SlapReply	*rs )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
114
{
Luke Howard's avatar
Luke Howard committed
115
116
117
	int				i, rc = LDAP_SUCCESS;
	plugin_referral_entry_callback	prec = NULL;
	void				*callback_data = NULL;
118
	Slapi_PBlock			*pb = SLAPI_OPERATION_PBLOCK( op );
Luke Howard's avatar
Luke Howard committed
119

120
	assert( pb != NULL );
121

122
123
	slapi_pblock_get( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, (void **)&prec );
	slapi_pblock_get( pb, SLAPI_X_INTOP_CALLBACK_DATA,           &callback_data );
Luke Howard's avatar
Luke Howard committed
124
125

	if ( prec != NULL ) {
126
		for ( i = 0; rs->sr_ref[i].bv_val != NULL; i++ ) {
Luke Howard's avatar
Luke Howard committed
127
			rc = (*prec)( rs->sr_ref[i].bv_val, callback_data );
128
129
130
131
132
133
134
			if ( rc != LDAP_SUCCESS ) {
				break;
			}
		}
	}

	return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
135
136
}

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
int
slapi_int_response( Slapi_Operation *op, SlapReply *rs )
{
	int				rc;

	switch ( rs->sr_type ) {
	case REP_RESULT:
		rc = slapi_int_result( op, rs );
		break;
	case REP_SEARCH:
		rc = slapi_int_search_entry( op, rs );
		break;
	case REP_SEARCHREF:
		rc = slapi_int_search_reference( op, rs );
		break;
	default:
		rc = LDAP_OTHER;
		break;
	}

157
	assert( rc != SLAP_CB_CONTINUE ); /* never try to send a wire response */
158
159
160
161

	return rc;
}

162
static int
163
slapi_int_get_ctrls( Slapi_PBlock *pb )
164
{
165
	LDAPControl		**c;
166
	int			rc = LDAP_SUCCESS;
167

168
169
	if ( pb->pb_op->o_ctrls != NULL ) {
		for ( c = pb->pb_op->o_ctrls; *c != NULL; c++ ) {
170
			rc = slap_parse_ctrl( pb->pb_op, pb->pb_rs, *c, &pb->pb_rs->sr_text );
171
172
173
			if ( rc != LDAP_SUCCESS )
				break;
		}
174
175
176
177
178
	}

	return rc;
}

179
void
Luke Howard's avatar
Luke Howard committed
180
slapi_int_connection_init_pb( Slapi_PBlock *pb, ber_tag_t tag )
181
{
182
183
184
	Connection		*conn;
	Operation		*op;
	ber_len_t		max = sockbuf_max_incoming;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
185

186
	conn = (Connection *) slapi_ch_calloc( 1, sizeof(Connection) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
187

188
	LDAP_STAILQ_INIT( &conn->c_pending_ops );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
189

190
191
192
	op = (Operation *) slapi_ch_calloc( 1, sizeof(OperationBuffer) );
	op->o_hdr = &((OperationBuffer *) op)->ob_hdr;
	op->o_controls = ((OperationBuffer *) op)->ob_controls;
193

194
	op->o_callback = (slap_callback *) slapi_ch_calloc( 1, sizeof(slap_callback) );
195
	op->o_callback->sc_response = slapi_int_response;
196
197
198
199
	op->o_callback->sc_cleanup = NULL;
	op->o_callback->sc_private = pb;
	op->o_callback->sc_next = NULL;

200
	conn->c_pending_ops.stqh_first = op;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
201

202
203
	/* connection object authorization information */
	conn->c_authtype = LDAP_AUTH_NONE;
204
205
206
	BER_BVZERO( &conn->c_authmech );
	BER_BVZERO( &conn->c_dn );
	BER_BVZERO( &conn->c_ndn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
207

208
	conn->c_listener = &slapi_listener;
209
210
	ber_dupbv( &conn->c_peer_domain, (struct berval *)&slap_unknown_bv );
	ber_dupbv( &conn->c_peer_name, (struct berval *)&slap_unknown_bv );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
211

212
	LDAP_STAILQ_INIT( &conn->c_ops );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
213

214
	BER_BVZERO( &conn->c_sasl_bind_mech );
215
216
217
	conn->c_sasl_authctx = NULL;
	conn->c_sasl_sockctx = NULL;
	conn->c_sasl_extra = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
218

219
	conn->c_sb = ber_sockbuf_alloc();
Kurt Zeilenga's avatar
Kurt Zeilenga committed
220

221
	ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
222

223
	conn->c_currentber = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
224
225

	/* should check status of thread calls */
226
	ldap_pvt_thread_mutex_init( &conn->c_mutex );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
227
228
229
230
	ldap_pvt_thread_mutex_init( &conn->c_write1_mutex );
	ldap_pvt_thread_mutex_init( &conn->c_write2_mutex );
	ldap_pvt_thread_cond_init( &conn->c_write1_cv );
	ldap_pvt_thread_cond_init( &conn->c_write2_cv );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
231

232
	ldap_pvt_thread_mutex_lock( &conn->c_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
233

234
235
236
237
	conn->c_n_ops_received = 0;
	conn->c_n_ops_executing = 0;
	conn->c_n_ops_pending = 0;
	conn->c_n_ops_completed = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
238

239
240
241
	conn->c_n_get = 0;
	conn->c_n_read = 0;
	conn->c_n_write = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
242

243
	conn->c_protocol = LDAP_VERSION3; 
Kurt Zeilenga's avatar
Kurt Zeilenga committed
244

245
	conn->c_activitytime = conn->c_starttime = slap_get_time();
Kurt Zeilenga's avatar
Kurt Zeilenga committed
246

247
248
249
250
251
252
	/*
	 * A real connection ID is required, because syncrepl associates
	 * pending CSNs with unique ( connection, operation ) tuples.
	 * Setting a fake connection ID will cause slap_get_commit_csn()
	 * to return a stale value.
	 */
253
	connection_assign_nextid( conn );
254

255
256
	conn->c_conn_state  = 0x01;	/* SLAP_C_ACTIVE */
	conn->c_struct_state = 0x02;	/* SLAP_C_USED */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
257

258
	conn->c_ssf = conn->c_transport_ssf = local_ssf;
259
	conn->c_tls_ssf = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
260

261
	backend_connection_init( conn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
262

263
264
265
266
	conn->c_send_ldap_result = slap_send_ldap_result;
	conn->c_send_search_entry = slap_send_search_entry;
	conn->c_send_ldap_extended = slap_send_ldap_extended;
	conn->c_send_search_reference = slap_send_search_reference;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
267

268
	/* operation object */
Luke Howard's avatar
Luke Howard committed
269
	op->o_tag = tag;
270
	op->o_protocol = LDAP_VERSION3; 
271
	BER_BVZERO( &op->o_authmech );
272
273
274
275
276
277
278
	op->o_time = slap_get_time();
	op->o_do_not_cache = 1;
	op->o_threadctx = ldap_pvt_thread_pool_context();
	op->o_tmpmemctx = NULL;
	op->o_tmpmfuncs = &ch_mfuncs;
	op->o_conn = conn;
	op->o_connid = conn->c_connid;
279
	op->o_bd = frontendDB;
280

281
282
283
284
	/* extensions */
	slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
	slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );

285
	pb->pb_rs = (SlapReply *)slapi_ch_calloc( 1, sizeof(SlapReply) );
286
287
288
	pb->pb_op = op;
	pb->pb_conn = conn;
	pb->pb_intop = 1;
289
290

	ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
291
}
292

293
294
295
296
static void
slapi_int_set_operation_dn( Slapi_PBlock *pb )
{
	Backend			*be;
297
	Operation		*op = pb->pb_op;
298

299
300
	if ( BER_BVISNULL( &op->o_ndn ) ) {
		/* set to root DN */
301
		be = select_backend( &op->o_req_ndn, 1 );
Luke Howard's avatar
Luke Howard committed
302
303
304
305
		if ( be != NULL ) {
			ber_dupbv( &op->o_dn, &be->be_rootdn );
			ber_dupbv( &op->o_ndn, &be->be_rootndn );
		}
306
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
307
308
}

309
310
void
slapi_int_connection_done_pb( Slapi_PBlock *pb )
Luke Howard's avatar
Luke Howard committed
311
{
312
	Connection		*conn;
313
	Operation		*op;
Luke Howard's avatar
Luke Howard committed
314

315
316
	PBLOCK_ASSERT_INTOP( pb, 0 );

317
318
	conn = pb->pb_conn;
	op = pb->pb_op;
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

	/* free allocated DNs */
	if ( !BER_BVISNULL( &op->o_dn ) )
		op->o_tmpfree( op->o_dn.bv_val, op->o_tmpmemctx );
	if ( !BER_BVISNULL( &op->o_ndn ) )
		op->o_tmpfree( op->o_ndn.bv_val, op->o_tmpmemctx );

	if ( !BER_BVISNULL( &op->o_req_dn ) )
		op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
	if ( !BER_BVISNULL( &op->o_req_ndn ) )
		op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );

	switch ( op->o_tag ) {
	case LDAP_REQ_MODRDN:
		if ( !BER_BVISNULL( &op->orr_newrdn ))
			op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
		if ( !BER_BVISNULL( &op->orr_nnewrdn ))
			op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
		if ( op->orr_newSup != NULL ) {
			assert( !BER_BVISNULL( op->orr_newSup ) );
			op->o_tmpfree( op->orr_newSup->bv_val, op->o_tmpmemctx );
			op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
		}
		if ( op->orr_nnewSup != NULL ) {
			assert( !BER_BVISNULL( op->orr_nnewSup ) );
			op->o_tmpfree( op->orr_nnewSup->bv_val, op->o_tmpmemctx );
			op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
		}
347
		slap_mods_free( op->orr_modlist, 1 );
348
349
		break;
	case LDAP_REQ_ADD:
Luke Howard's avatar
Luke Howard committed
350
		slap_mods_free( op->ora_modlist, 0 );
351
352
		break;
	case LDAP_REQ_MODIFY:
353
		slap_mods_free( op->orm_modlist, 1 );
354
355
356
357
358
359
360
361
362
		break;
	case LDAP_REQ_SEARCH:
		if ( op->ors_attrs != NULL ) {
			op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
			op->ors_attrs = NULL;
		}
		break;
	default:
		break;
Luke Howard's avatar
Luke Howard committed
363
364
	}

Luke Howard's avatar
Luke Howard committed
365
366
367
	slapi_ch_free_string( &conn->c_authmech.bv_val );
	slapi_ch_free_string( &conn->c_dn.bv_val );
	slapi_ch_free_string( &conn->c_ndn.bv_val );
Luke Howard's avatar
Luke Howard committed
368
369
	slapi_ch_free_string( &conn->c_peer_domain.bv_val );
	slapi_ch_free_string( &conn->c_peer_name.bv_val );
Luke Howard's avatar
Luke Howard committed
370

371
372
373
	if ( conn->c_sb != NULL ) {
		ber_sockbuf_free( conn->c_sb );
	}
Luke Howard's avatar
Luke Howard committed
374

375
376
377
	slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
	slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, conn );

378
379
380
	slapi_ch_free( (void **)&pb->pb_op->o_callback );
	slapi_ch_free( (void **)&pb->pb_op );
	slapi_ch_free( (void **)&pb->pb_conn );
381
	slapi_ch_free( (void **)&pb->pb_rs );
382
383
384
385
386
387
}

static int
slapi_int_func_internal_pb( Slapi_PBlock *pb, slap_operation_t which )
{
	BI_op_bind		**func;
388
	SlapReply		*rs = pb->pb_rs;
389
	int			rc;
390

391
	PBLOCK_ASSERT_INTOP( pb, 0 );
392

393
394
395
396
	rc = slapi_int_get_ctrls( pb );
	if ( rc != LDAP_SUCCESS ) {
		rs->sr_err = rc;
		return rc;
Luke Howard's avatar
Luke Howard committed
397
	}
398

399
400
	pb->pb_op->o_bd = frontendDB;
	func = &frontendDB->be_bind;
401

402
	return func[which]( pb->pb_op, pb->pb_rs );
Luke Howard's avatar
Luke Howard committed
403
404
}

405
406
int
slapi_delete_internal_pb( Slapi_PBlock *pb )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
407
{
408
409
410
411
	if ( pb == NULL ) {
		return -1;
	}

412
	PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_DELETE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
413

414
	slapi_int_func_internal_pb( pb, op_delete );
Luke Howard's avatar
Luke Howard committed
415

416
	return 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
417
418
}

419
420
int
slapi_add_internal_pb( Slapi_PBlock *pb )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
421
{
422
423
	SlapReply		*rs;
	Slapi_Entry		*entry_orig = NULL;
Howard Chu's avatar
Howard Chu committed
424
425
	OpExtraDB oex;
	int rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
426

427
428
429
430
	if ( pb == NULL ) {
		return -1;
	}

431
432
	PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_ADD );

433
	rs = pb->pb_rs;
434

435
436
	entry_orig = pb->pb_op->ora_e;
	pb->pb_op->ora_e = NULL;
437

438
439
440
441
	/*
	 * The caller can specify a new entry, or a target DN and set
	 * of modifications, but not both.
	 */
442
	if ( entry_orig != NULL ) {
443
		if ( pb->pb_op->ora_modlist != NULL || !BER_BVISNULL( &pb->pb_op->o_req_ndn )) {
444
			rs->sr_err = LDAP_PARAM_ERROR;
445
446
447
			goto cleanup;
		}

448
449
450
451
		assert( BER_BVISNULL( &pb->pb_op->o_req_dn ) ); /* shouldn't get set */
		ber_dupbv( &pb->pb_op->o_req_dn, &entry_orig->e_name );
		ber_dupbv( &pb->pb_op->o_req_ndn, &entry_orig->e_nname );
	} else if ( pb->pb_op->ora_modlist == NULL || BER_BVISNULL( &pb->pb_op->o_req_ndn )) {
452
		rs->sr_err = LDAP_PARAM_ERROR;
453
		goto cleanup;
Luke Howard's avatar
Luke Howard committed
454
	}
455

456
457
458
459
	pb->pb_op->ora_e = (Entry *)slapi_ch_calloc( 1, sizeof(Entry) );
	ber_dupbv( &pb->pb_op->ora_e->e_name,  &pb->pb_op->o_req_dn );
	ber_dupbv( &pb->pb_op->ora_e->e_nname, &pb->pb_op->o_req_ndn );

460
	if ( entry_orig != NULL ) {
461
		assert( pb->pb_op->ora_modlist == NULL );
462

463
464
		rs->sr_err = slap_entry2mods( entry_orig, &pb->pb_op->ora_modlist,
			&rs->sr_text, pb->pb_textbuf, sizeof( pb->pb_textbuf ) );
465
		if ( rs->sr_err != LDAP_SUCCESS ) {
466
467
			goto cleanup;
		}
468
	} else {
469
		assert( pb->pb_op->ora_modlist != NULL );
470
471
	}

Howard Chu's avatar
Howard Chu committed
472
	rs->sr_err = slap_mods_check( pb->pb_op, pb->pb_op->ora_modlist, &rs->sr_text,
473
		pb->pb_textbuf, sizeof( pb->pb_textbuf ), NULL );
474
	if ( rs->sr_err != LDAP_SUCCESS ) {
475
476
477
                goto cleanup;
        }

478
479
	/* Duplicate the values, because we may call slapi_entry_free() */
	rs->sr_err = slap_mods2entry( pb->pb_op->ora_modlist, &pb->pb_op->ora_e,
Luke Howard's avatar
Luke Howard committed
480
		1, 0, &rs->sr_text, pb->pb_textbuf, sizeof( pb->pb_textbuf ) );
481
482
483
484
	if ( rs->sr_err != LDAP_SUCCESS ) {
		goto cleanup;
	}

Howard Chu's avatar
Howard Chu committed
485
486
487
488
489
490
491
492
	oex.oe.oe_key = (void *)do_add;
	oex.oe_db = NULL;
	LDAP_SLIST_INSERT_HEAD(&pb->pb_op->o_extra, &oex.oe, oe_next);
	rc = slapi_int_func_internal_pb( pb, op_add );
	LDAP_SLIST_REMOVE(&pb->pb_op->o_extra, &oex.oe, OpExtra, oe_next);

	if ( !rc ) {
		if ( pb->pb_op->ora_e != NULL && oex.oe_db != NULL ) {
493
			BackendDB	*bd = pb->pb_op->o_bd;
494

Howard Chu's avatar
Howard Chu committed
495
			pb->pb_op->o_bd = oex.oe_db;
496
497
498
			be_entry_release_w( pb->pb_op, pb->pb_op->ora_e );
			pb->pb_op->ora_e = NULL;
			pb->pb_op->o_bd = bd;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
499
500
501
502
		}
	}

cleanup:
503

504
505
506
	if ( pb->pb_op->ora_e != NULL ) {
		slapi_entry_free( pb->pb_op->ora_e );
		pb->pb_op->ora_e = NULL;
507
508
	}
	if ( entry_orig != NULL ) {
509
		pb->pb_op->ora_e = entry_orig;
510
		slap_mods_free( pb->pb_op->ora_modlist, 1 );
511
		pb->pb_op->ora_modlist = NULL;
512
	}
513

514
	return 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
515
516
}

517
518
int
slapi_modrdn_internal_pb( Slapi_PBlock *pb )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
519
{
520
521
522
523
	if ( pb == NULL ) {
		return -1;
	}

524
	PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_MODRDN );
525

526
	if ( BER_BVISEMPTY( &pb->pb_op->o_req_ndn ) ) {
527
		pb->pb_rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
Luke Howard's avatar
Luke Howard committed
528
529
530
		goto cleanup;
	}

531
	slapi_int_func_internal_pb( pb, op_modrdn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
532
533

cleanup:
Luke Howard's avatar
Luke Howard committed
534

535
	return 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
536
537
}

538
539
int
slapi_modify_internal_pb( Slapi_PBlock *pb )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
540
{
541
	SlapReply		*rs;
Luke Howard's avatar
Luke Howard committed
542

543
544
545
546
	if ( pb == NULL ) {
		return -1;
	}

547
	PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_MODIFY );
548

549
	rs = pb->pb_rs;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
550

551
	if ( pb->pb_op->orm_modlist == NULL ) {
552
		rs->sr_err = LDAP_PARAM_ERROR;
Luke Howard's avatar
Luke Howard committed
553
554
		goto cleanup;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
555

556
	if ( BER_BVISEMPTY( &pb->pb_op->o_req_ndn ) ) {
557
		rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
558
559
560
		goto cleanup;
	}

Howard Chu's avatar
Howard Chu committed
561
	rs->sr_err = slap_mods_check( pb->pb_op, pb->pb_op->orm_modlist,
562
		&rs->sr_text, pb->pb_textbuf, sizeof( pb->pb_textbuf ), NULL );
563
	if ( rs->sr_err != LDAP_SUCCESS ) {
564
565
                goto cleanup;
        }
566

567
	slapi_int_func_internal_pb( pb, op_modify );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
568
569
570

cleanup:

571
	return 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
572
573
}

574
575
576
static int
slapi_int_search_entry_callback( Slapi_Entry *entry, void *callback_data )
{
577
578
579
	int		nentries = 0, i = 0;
	Slapi_Entry	**head = NULL, **tp;
	Slapi_PBlock	*pb = (Slapi_PBlock *)callback_data;
580

581
582
	PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_SEARCH );

583
584
	entry = slapi_entry_dup( entry );
	if ( entry == NULL ) {
585
		return LDAP_NO_MEMORY;
586
587
588
589
590
591
592
593
594
595
	}

	slapi_pblock_get( pb, SLAPI_NENTRIES, &nentries );
	slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &head );
	
	i = nentries + 1;
	if ( nentries == 0 ) {
		tp = (Slapi_Entry **)slapi_ch_malloc( 2 * sizeof(Slapi_Entry *) );
		if ( tp == NULL ) {
			slapi_entry_free( entry );
596
			return LDAP_NO_MEMORY;
597
598
		}

599
		tp[0] = entry;
600
601
602
603
604
	} else {
		tp = (Slapi_Entry **)slapi_ch_realloc( (char *)head,
				sizeof(Slapi_Entry *) * ( i + 1 ) );
		if ( tp == NULL ) {
			slapi_entry_free( entry );
605
			return LDAP_NO_MEMORY;
606
		}
607
		tp[i - 1] = entry;
608
	}
609
	tp[i] = NULL;
610
611
	          
	slapi_pblock_set( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, (void *)tp );
612
	slapi_pblock_set( pb, SLAPI_NENTRIES, (void *)&i );
613
614
615
616

	return LDAP_SUCCESS;
}

617
618
int
slapi_search_internal_pb( Slapi_PBlock *pb )
619
620
621
622
623
624
625
626
{
	return slapi_search_internal_callback_pb( pb,
		(void *)pb,
		NULL,
		slapi_int_search_entry_callback,
		NULL );
}

627
628
int
slapi_search_internal_callback_pb( Slapi_PBlock *pb,
629
630
631
632
633
	void *callback_data,
	plugin_result_callback prc,
	plugin_search_entry_callback psec,
	plugin_referral_entry_callback prec )
{
634
635
	int			free_filter = 0;
	SlapReply		*rs;
Luke Howard's avatar
Luke Howard committed
636

637
638
639
640
	if ( pb == NULL ) {
		return -1;
	}

641
	PBLOCK_ASSERT_INTOP( pb, LDAP_REQ_SEARCH );
642

643
	rs = pb->pb_rs;
Luke Howard's avatar
Luke Howard committed
644

645
	/* search callback and arguments */
Luke Howard's avatar
Luke Howard committed
646
647
648
649
	slapi_pblock_set( pb, SLAPI_X_INTOP_RESULT_CALLBACK,         (void *)prc );
	slapi_pblock_set( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK,   (void *)psec );
	slapi_pblock_set( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK, (void *)prec );
	slapi_pblock_set( pb, SLAPI_X_INTOP_CALLBACK_DATA,           (void *)callback_data );
Luke Howard's avatar
Luke Howard committed
650

651
	if ( BER_BVISEMPTY( &pb->pb_op->ors_filterstr )) {
652
653
		rs->sr_err = LDAP_PARAM_ERROR;
		goto cleanup;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
654
655
	}

656
657
658
	if ( pb->pb_op->ors_filter == NULL ) {
		pb->pb_op->ors_filter = slapi_str2filter( pb->pb_op->ors_filterstr.bv_val );
		if ( pb->pb_op->ors_filter == NULL ) {
659
			rs->sr_err = LDAP_PROTOCOL_ERROR;
660
661
662
			goto cleanup;
		}

663
		free_filter = 1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
664
665
	}

666
	slapi_int_func_internal_pb( pb, op_search );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
667

668
669
cleanup:
	if ( free_filter ) {
670
671
		slapi_filter_free( pb->pb_op->ors_filter, 1 );
		pb->pb_op->ors_filter = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
672
673
	}

Luke Howard's avatar
Luke Howard committed
674
675
676
677
	slapi_pblock_delete_param( pb, SLAPI_X_INTOP_RESULT_CALLBACK );
	slapi_pblock_delete_param( pb, SLAPI_X_INTOP_SEARCH_ENTRY_CALLBACK );
	slapi_pblock_delete_param( pb, SLAPI_X_INTOP_REFERRAL_ENTRY_CALLBACK );
	slapi_pblock_delete_param( pb, SLAPI_X_INTOP_CALLBACK_DATA );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
678

679
680
681
682
683
	return 0;
}

/* Wrappers for old API */

684
685
void
slapi_search_internal_set_pb( Slapi_PBlock *pb,
686
687
688
689
690
691
692
693
694
695
	const char *base,
	int scope,
	const char *filter,
	char **attrs,
	int attrsonly,
	LDAPControl **controls,
	const char *uniqueid,
	Slapi_ComponentId *plugin_identity,
	int operation_flags )
{
696
697
698
	int no_limit = SLAP_NO_LIMIT;
	int deref = LDAP_DEREF_NEVER;

699
	slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH );
Luke Howard's avatar
Luke Howard committed
700
	slapi_pblock_set( pb, SLAPI_SEARCH_TARGET,    (void *)base );
701
702
	slapi_pblock_set( pb, SLAPI_SEARCH_SCOPE,     (void *)&scope );
	slapi_pblock_set( pb, SLAPI_SEARCH_FILTER,    (void *)0 );
703
	slapi_pblock_set( pb, SLAPI_SEARCH_STRFILTER, (void *)filter );
Luke Howard's avatar
Luke Howard committed
704
	slapi_pblock_set( pb, SLAPI_SEARCH_ATTRS,     (void *)attrs );
705
	slapi_pblock_set( pb, SLAPI_SEARCH_ATTRSONLY, (void *)&attrsonly );
Luke Howard's avatar
Luke Howard committed
706
707
708
	slapi_pblock_set( pb, SLAPI_REQCONTROLS,      (void *)controls );
	slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID,  (void *)uniqueid );
	slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY,  (void *)plugin_identity );
709
710
711
712
	slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,    (void *)&operation_flags );
	slapi_pblock_set( pb, SLAPI_SEARCH_DEREF,     (void *)&deref );
	slapi_pblock_set( pb, SLAPI_SEARCH_SIZELIMIT, (void *)&no_limit );
	slapi_pblock_set( pb, SLAPI_SEARCH_TIMELIMIT, (void *)&no_limit );
713
714

	slapi_int_set_operation_dn( pb );
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
}

Slapi_PBlock *
slapi_search_internal(
	char *ldn, 
	int scope, 
	char *filStr, 
	LDAPControl **controls, 
	char **attrs, 
	int attrsonly ) 
{
	Slapi_PBlock *pb;

	pb = slapi_pblock_new();

730
731
	slapi_search_internal_set_pb( pb, ldn, scope, filStr,
		attrs, attrsonly,
732
733
734
735
736
737
738
		controls, NULL, NULL, 0 );

	slapi_search_internal_pb( pb );

	return pb;
}

739
740
void
slapi_modify_internal_set_pb( Slapi_PBlock *pb,
741
742
743
744
745
746
747
	const char *dn,
	LDAPMod **mods,
	LDAPControl **controls,
	const char *uniqueid,
	Slapi_ComponentId *plugin_identity,
	int operation_flags )
{
748
	slapi_int_connection_init_pb( pb, LDAP_REQ_MODIFY );
Luke Howard's avatar
Luke Howard committed
749
750
751
	slapi_pblock_set( pb, SLAPI_MODIFY_TARGET,   (void *)dn );
	slapi_pblock_set( pb, SLAPI_MODIFY_MODS,     (void *)mods );
	slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
752
753
	slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
	slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
754
	slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)&operation_flags );
755
	slapi_int_set_operation_dn( pb );
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
}

/* Function : slapi_modify_internal
 *
 * Description:	Plugin functions call this routine to modify an entry 
 *				in the backend directly
 * Return values : LDAP_SUCCESS
 *                 LDAP_PARAM_ERROR
 *                 LDAP_NO_MEMORY
 *                 LDAP_OTHER
 *                 LDAP_UNWILLING_TO_PERFORM
*/
Slapi_PBlock *
slapi_modify_internal(
	char *ldn, 	
	LDAPMod **mods, 
	LDAPControl **controls, 
	int log_change )
{
	Slapi_PBlock *pb;

	pb = slapi_pblock_new();

779
	slapi_modify_internal_set_pb( pb, ldn, mods, controls, NULL, NULL, 0 );
780
	slapi_pblock_set( pb, SLAPI_LOG_OPERATION, (void *)&log_change );
781
782
783
784
785
	slapi_modify_internal_pb( pb );

	return pb;
}

786
787
int
slapi_add_internal_set_pb( Slapi_PBlock *pb,
788
789
790
791
792
793
	const char *dn,
	LDAPMod **attrs,
	LDAPControl **controls,
	Slapi_ComponentId *plugin_identity,
	int operation_flags )
{
794
	slapi_int_connection_init_pb( pb, LDAP_REQ_ADD );
Luke Howard's avatar
Luke Howard committed
795
796
797
	slapi_pblock_set( pb, SLAPI_ADD_TARGET,      (void *)dn );
	slapi_pblock_set( pb, SLAPI_MODIFY_MODS,     (void *)attrs );
	slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
798
	slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
799
	slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)&operation_flags );
800
	slapi_int_set_operation_dn( pb );
801
802
803
804

	return 0;
}

805
806
Slapi_PBlock *
slapi_add_internal(
807
808
809
	char * dn,
	LDAPMod **attrs,
	LDAPControl **controls,
810
	int log_change )
811
812
813
814
815
{
	Slapi_PBlock *pb;

	pb = slapi_pblock_new();

816
	slapi_add_internal_set_pb( pb, dn, attrs, controls, NULL, 0);
817
	slapi_pblock_set( pb, SLAPI_LOG_OPERATION, (void *)&log_change );
818
819
820
821
822
	slapi_add_internal_pb( pb );

	return pb;
}

823
824
void
slapi_add_entry_internal_set_pb( Slapi_PBlock *pb,
825
826
827
828
829
	Slapi_Entry *e,
	LDAPControl **controls,
	Slapi_ComponentId *plugin_identity,
	int operation_flags )
{
830
	slapi_int_connection_init_pb( pb, LDAP_REQ_ADD );
Luke Howard's avatar
Luke Howard committed
831
832
	slapi_pblock_set( pb, SLAPI_ADD_ENTRY,       (void *)e );
	slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
833
	slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
834
	slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)&operation_flags );
835
	slapi_int_set_operation_dn( pb );
836
837
838
839
840
841
}

Slapi_PBlock * 
slapi_add_entry_internal(
	Slapi_Entry *e, 
	LDAPControl **controls, 
842
	int log_change )
843
844
845
846
847
{
	Slapi_PBlock *pb;

	pb = slapi_pblock_new();

848
	slapi_add_entry_internal_set_pb( pb, e, controls, NULL, 0 );
849
	slapi_pblock_set( pb, SLAPI_LOG_OPERATION, (void *)&log_change );
850
851
852
853
854
	slapi_add_internal_pb( pb );

	return pb;
}

855
856
void
slapi_rename_internal_set_pb( Slapi_PBlock *pb,
857
858
859
860
861
862
863
864
865
	const char *olddn,
	const char *newrdn,
	const char *newsuperior,
	int deloldrdn,
	LDAPControl **controls,
	const char *uniqueid,
	Slapi_ComponentId *plugin_identity,
	int operation_flags )
{
866
	slapi_int_connection_init_pb( pb, LDAP_REQ_MODRDN );
Luke Howard's avatar
Luke Howard committed
867
868
	slapi_pblock_set( pb, SLAPI_MODRDN_TARGET,      (void *)olddn );
	slapi_pblock_set( pb, SLAPI_MODRDN_NEWRDN,      (void *)newrdn );
869
	slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR, (void *)newsuperior );
870
	slapi_pblock_set( pb, SLAPI_MODRDN_DELOLDRDN,   (void *)&deloldrdn );
Luke Howard's avatar
Luke Howard committed
871
872
873
	slapi_pblock_set( pb, SLAPI_REQCONTROLS,        (void *)controls );
	slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID,    (void *)uniqueid );
	slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY,    (void *)plugin_identity );
874
	slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,      (void *)&operation_flags );
875
	slap_modrdn2mods( pb->pb_op, pb->pb_rs );
876
	slapi_int_set_operation_dn( pb );
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
}

/* Function : slapi_modrdn_internal
 *
 * Description : Plugin functions call this routine to modify the rdn 
 *				 of an entry in the backend directly
 * Return values : LDAP_SUCCESS
 *                 LDAP_PARAM_ERROR
 *                 LDAP_NO_MEMORY
 *                 LDAP_OTHER
 *                 LDAP_UNWILLING_TO_PERFORM
 *
 * NOTE: This function does not support the "newSuperior" option from LDAP V3.
 */
Slapi_PBlock *
slapi_modrdn_internal(
	char *olddn, 
	char *lnewrdn, 
	int deloldrdn, 
	LDAPControl **controls, 
	int log_change )
{
	Slapi_PBlock *pb;

901
	pb = slapi_pblock_new ();
902
903

	slapi_rename_internal_set_pb( pb, olddn, lnewrdn, NULL,
904
		deloldrdn, controls, NULL, NULL, 0 );
905
	slapi_pblock_set( pb, SLAPI_LOG_OPERATION, (void *)&log_change );
906
907
908
909
910
	slapi_modrdn_internal_pb( pb );

	return pb;
}

911
912
void
slapi_delete_internal_set_pb( Slapi_PBlock *pb,
913
914
915
916
917
918
	const char *dn,
	LDAPControl **controls,
	const char *uniqueid,
	Slapi_ComponentId *plugin_identity,
	int operation_flags )
{
919
	slapi_int_connection_init_pb( pb, LDAP_REQ_DELETE );
Luke Howard's avatar
Luke Howard committed
920
921
	slapi_pblock_set( pb, SLAPI_TARGET_DN,       (void *)dn );
	slapi_pblock_set( pb, SLAPI_REQCONTROLS,     (void *)controls );
922
923
	slapi_pblock_set( pb, SLAPI_TARGET_UNIQUEID, (void *)uniqueid );
	slapi_pblock_set( pb, SLAPI_PLUGIN_IDENTITY, (void *)plugin_identity );
924
	slapi_pblock_set( pb, SLAPI_X_INTOP_FLAGS,   (void *)&operation_flags );
925
	slapi_int_set_operation_dn( pb );
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
}

/* Function : slapi_delete_internal
 *
 * Description : Plugin functions call this routine to delete an entry 
 *               in the backend directly
 * Return values : LDAP_SUCCESS
 *                 LDAP_PARAM_ERROR
 *                 LDAP_NO_MEMORY
 *                 LDAP_OTHER
 *                 LDAP_UNWILLING_TO_PERFORM
*/
Slapi_PBlock *
slapi_delete_internal(
	char *ldn, 
	LDAPControl **controls, 
	int log_change )
{
	Slapi_PBlock *pb;

	pb = slapi_pblock_new();

Luke Howard's avatar