slapi_overlay.c 21.3 KB
Newer Older
Luke Howard's avatar
Luke Howard committed
1
/* slapi_overlay.c - SLAPI overlay */
2
3
4
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 2001-2019 The OpenLDAP Foundation.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * 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>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Luke Howard for inclusion
 * in OpenLDAP Software.
 */

#include "portable.h"

#include <stdio.h>

#include <ac/string.h>
#include <ac/socket.h>

#include "slap.h"
#include "slapi.h"
30
#include "config.h"
31
32
33
34

#ifdef LDAP_SLAPI

static slap_overinst slapi;
35
static int slapi_over_initialized = 0;
36

37
38
39
static int slapi_over_response( Operation *op, SlapReply *rs );
static int slapi_over_cleanup( Operation *op, SlapReply *rs );

40
static Slapi_PBlock *
41
slapi_over_pblock_new( Operation *op, SlapReply *rs )
42
43
44
45
{
	Slapi_PBlock		*pb;

	pb = slapi_pblock_new();
46
47
	pb->pb_op = op;
	pb->pb_conn = op->o_conn;
48
	pb->pb_rs = rs;
49
	pb->pb_intop = 0;
50

Luke Howard's avatar
Luke Howard committed
51
	PBLOCK_ASSERT_OP( pb, op->o_tag );
52
53
54
55

	return pb;
}

56
static int
57
slapi_op_internal_p( Operation *op, SlapReply *rs, slap_callback *cb )
58
59
60
61
62
{
	int			internal_op = 0;
	Slapi_PBlock		*pb = NULL;
	slap_callback		*pcb;

63
64
65
66
67
	/*
	 * Abstraction violating check for SLAPI internal operations
	 * allows pblock to remain consistent when invoking internal
	 * op plugins
	 */
68
69
70
	for ( pcb = op->o_callback; pcb != NULL; pcb = pcb->sc_next ) {
		if ( pcb->sc_response == slapi_int_response ) {
			pb = (Slapi_PBlock *)pcb->sc_private;
71
			PBLOCK_ASSERT_INTOP( pb, 0 );
72
73
74
75
76
77
78
			internal_op = 1;
			break;
		}
	}

	if ( cb != NULL ) {
		if ( pb == NULL ) {
79
			pb = slapi_over_pblock_new( op, rs );
80
81
		}

82
83
		cb->sc_response = slapi_over_response;
		cb->sc_cleanup = slapi_over_cleanup;
84
		cb->sc_private = pb;
85
		cb->sc_writewait = 0;
86
87
88
89
90
91
92
		cb->sc_next = op->o_callback;
		op->o_callback = cb;
	}

	return internal_op;
}

93
94
95
96
97
98
99
100
101
static int
slapi_over_compute_output(
	computed_attr_context *c,
	Slapi_Attr *attribute,
	Slapi_Entry *entry
)
{
	Attribute		**a;
	AttributeDescription	*desc;
Howard Chu's avatar
Howard Chu committed
102
	SlapReply		*rs;
103
104
105
106
107

	if ( c == NULL || attribute == NULL || entry == NULL ) {
		return 0;
	}

Howard Chu's avatar
Howard Chu committed
108
109
	rs = (SlapReply *)c->cac_private;

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
	assert( rs->sr_entry == entry );

	desc = attribute->a_desc;

	if ( rs->sr_attrs == NULL ) {
		/* All attrs request, skip operational attributes */
		if ( is_at_operational( desc->ad_type ) ) {
			return 0;
		}
	} else {
		/* Specific attributes requested */
		if ( is_at_operational( desc->ad_type ) ) {
			if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
			     !ad_inlist( desc, rs->sr_attrs ) ) {
				return 0;
			}
		} else {
			if ( !SLAP_USERATTRS( rs->sr_attr_flags ) &&
			     !ad_inlist( desc, rs->sr_attrs ) ) {
				return 0;
			}
		}
	}

Luke Howard's avatar
Luke Howard committed
134
	/* XXX perhaps we should check for existing attributes and merge */
135
136
137
	for ( a = &rs->sr_operational_attrs; *a != NULL; a = &(*a)->a_next )
		;

138
	*a = slapi_attr_dup( attribute );
139
140
141
142
143

	return 0;
}

static int
Luke Howard's avatar
Luke Howard committed
144
slapi_over_aux_operational( Operation *op, SlapReply *rs )
145
146
147
148
149
{
	/* Support for computed attribute plugins */
	computed_attr_context    ctx;
	AttributeName		*anp;

150
	if ( slapi_op_internal_p( op, rs, NULL ) ) {
151
152
153
		return SLAP_CB_CONTINUE;
	}

154
	ctx.cac_pb = slapi_over_pblock_new( op, rs );
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
	ctx.cac_op = op;
	ctx.cac_private = rs;

	if ( rs->sr_entry != NULL ) {
		/*
		 * For each client requested attribute, call the plugins.
		 */
		if ( rs->sr_attrs != NULL ) {
			for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
				if ( compute_evaluator( &ctx, anp->an_name.bv_val,
					rs->sr_entry, slapi_over_compute_output ) == 1 ) {
					break;
				}
			}
		} else {
			/*
			 * Technically we shouldn't be returning operational attributes
			 * when the user requested only user attributes. We'll let the
			 * plugin decide whether to be naughty or not.
			 */
			compute_evaluator( &ctx, "*", rs->sr_entry, slapi_over_compute_output );
		}
	}

179
180
	slapi_pblock_destroy( ctx.cac_pb );

181
182
183
	return SLAP_CB_CONTINUE;
}

184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * We need this function to call frontendDB (global) plugins before
 * database plugins, if we are invoked by a slap_callback.
 */
static int
slapi_over_call_plugins( Slapi_PBlock *pb, int type )
{
	int 			rc = 1; /* means no plugins called */
	Operation		*op;

	PBLOCK_ASSERT_OP( pb, 0 );
	op = pb->pb_op;

197
	if ( !be_match( op->o_bd, frontendDB ) ) {
198
199
200
201
202
203
204
205
206
		rc = slapi_int_call_plugins( frontendDB, type, pb );
	}
	if ( rc >= 0 ) {
		rc = slapi_int_call_plugins( op->o_bd, type, pb );
	}

	return rc;
}

207
208
209
210
211
212
static int
slapi_over_search( Operation *op, SlapReply *rs, int type )
{
	int			rc;
	Slapi_PBlock		*pb;

Luke Howard's avatar
cleanup    
Luke Howard committed
213
214
	assert( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF );

215
	/* create a new pblock to not trample on result controls */
216
	pb = slapi_over_pblock_new( op, rs );
217

218
	rc = slapi_over_call_plugins( pb, type );
Luke Howard's avatar
Luke Howard committed
219
	if ( rc >= 0 ) /* 1 means no plugins called */
220
		rc = SLAP_CB_CONTINUE;
Luke Howard's avatar
Luke Howard committed
221
222
	else
		rc = LDAP_SUCCESS; /* confusing: don't abort, but don't send */
223
224
225
226
227
228

	slapi_pblock_destroy(pb);

	return rc;
}

Luke Howard's avatar
Luke Howard committed
229
230
231
/*
 * Call pre- and post-result plugins
 */
232
static int
233
slapi_over_result( Operation *op, SlapReply *rs, int type )
234
{
235
	Slapi_PBlock		*pb = SLAPI_OPERATION_PBLOCK( op );
236

237
	assert( rs->sr_type == REP_RESULT || rs->sr_type == REP_SASL || rs->sr_type == REP_EXTENDED );
Luke Howard's avatar
cleanup    
Luke Howard committed
238

239
	slapi_over_call_plugins( pb, type );
240

Luke Howard's avatar
Luke Howard committed
241
	return SLAP_CB_CONTINUE;
242
243
}

244

245
static int
Luke Howard's avatar
Luke Howard committed
246
slapi_op_bind_callback( Operation *op, SlapReply *rs, int prc )
247
{
Luke Howard's avatar
Luke Howard committed
248
	switch ( prc ) {
249
250
251
252
253
254
255
256
257
258
	case SLAPI_BIND_SUCCESS:
		/* Continue with backend processing */
		break;
	case SLAPI_BIND_FAIL:
		/* Failure, frontend (that's us) sends result */
		rs->sr_err = LDAP_INVALID_CREDENTIALS;
		send_ldap_result( op, rs );
		return rs->sr_err;
		break;
	case SLAPI_BIND_ANONYMOUS: /* undocumented */
Luke Howard's avatar
Luke Howard committed
259
	default: /* plugin sent result or no plugins called */
260
261
262
		BER_BVZERO( &op->orb_edn );

		if ( rs->sr_err == LDAP_SUCCESS ) {
263
264
265
266
			/*
			 * Plugin will have called slapi_pblock_set(LDAP_CONN_DN) which
			 * will have set conn->c_dn and conn->c_ndn
			 */
Luke Howard's avatar
Luke Howard committed
267
			if ( BER_BVISNULL( &op->o_conn->c_ndn ) && prc == 1 ) {
268
269
				/* No plugins were called; continue processing */
				return LDAP_SUCCESS;
270
271
			}
			ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
272
			if ( !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
273
274
275
276
				ber_len_t max = sockbuf_max_incoming_auth;
				ber_sockbuf_ctrl( op->o_conn->c_sb,
					LBER_SB_OPT_SET_MAX_INCOMING, &max );
			}
277
			ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
278

279
280
281
282
283
284
			/* log authorization identity */
			Statslog( LDAP_DEBUG_STATS,
				"%s BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
				op->o_log_prefix,
				BER_BVISNULL( &op->o_conn->c_dn )
					? "<empty>" : op->o_conn->c_dn.bv_val,
285
286
				BER_BVISNULL( &op->orb_mech )
					? "<empty>" : op->orb_mech.bv_val, 0, 0 );
287

288
289
290
291
292
			return -1;
		}
		break;
	}

Luke Howard's avatar
Luke Howard committed
293
	return rs->sr_err;
294
295
296
}

static int
Luke Howard's avatar
Luke Howard committed
297
slapi_op_search_callback( Operation *op, SlapReply *rs, int prc )
298
{
299
	Slapi_PBlock		*pb = SLAPI_OPERATION_PBLOCK( op );
300
	Filter *f = op->ors_filter;
301

302
	/* check preoperation result code */
Luke Howard's avatar
Luke Howard committed
303
	if ( prc < 0 ) {
304
305
306
		return rs->sr_err;
	}

307
	rs->sr_err = LDAP_SUCCESS;
308

309
310
	if ( pb->pb_intop == 0 && 
	     slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) == 0 ) {
311
312
313
314
		/*
		 * The plugin can set the SLAPI_SEARCH_FILTER.
		 * SLAPI_SEARCH_STRFILER is not normative.
		 */
315
316
317
318
		if (f != op->ors_filter) {
			op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
			filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
		}
319
320
321
322
323
324
	}

	return LDAP_SUCCESS;
}

struct slapi_op_info {
325
326
	int soi_preop;			/* preoperation plugin parameter */
	int soi_postop;			/* postoperation plugin parameter */
327
328
	int soi_internal_preop;		/* internal preoperation plugin parameter */
	int soi_internal_postop;	/* internal postoperation plugin parameter */
Luke Howard's avatar
Luke Howard committed
329
	int (*soi_callback)(Operation *, SlapReply *, int); /* preoperation result handler */
330
331
332
333
} slapi_op_dispatch_table[] = {
	{
		SLAPI_PLUGIN_PRE_BIND_FN,
		SLAPI_PLUGIN_POST_BIND_FN,
334
335
		SLAPI_PLUGIN_INTERNAL_PRE_BIND_FN,
		SLAPI_PLUGIN_INTERNAL_POST_BIND_FN,
336
		slapi_op_bind_callback
337
338
	},
	{
339
		SLAPI_PLUGIN_PRE_UNBIND_FN,
340
		SLAPI_PLUGIN_POST_UNBIND_FN,
341
342
		SLAPI_PLUGIN_INTERNAL_PRE_UNBIND_FN,
		SLAPI_PLUGIN_INTERNAL_POST_UNBIND_FN,
343
344
345
346
347
		NULL
	},
	{
		SLAPI_PLUGIN_PRE_SEARCH_FN,
		SLAPI_PLUGIN_POST_SEARCH_FN,
348
349
		SLAPI_PLUGIN_INTERNAL_PRE_SEARCH_FN,
		SLAPI_PLUGIN_INTERNAL_POST_SEARCH_FN,
350
		slapi_op_search_callback
351
352
353
354
	},
	{
		SLAPI_PLUGIN_PRE_COMPARE_FN,
		SLAPI_PLUGIN_POST_COMPARE_FN,
355
356
		SLAPI_PLUGIN_INTERNAL_PRE_COMPARE_FN,
		SLAPI_PLUGIN_INTERNAL_POST_COMPARE_FN,
357
358
359
360
361
		NULL
	},
	{
		SLAPI_PLUGIN_PRE_MODIFY_FN,
		SLAPI_PLUGIN_POST_MODIFY_FN,
362
363
		SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
		SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
364
		NULL
365
366
367
368
	},
	{
		SLAPI_PLUGIN_PRE_MODRDN_FN,
		SLAPI_PLUGIN_POST_MODRDN_FN,
369
370
		SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN,
		SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
371
372
373
374
375
		NULL
	},
	{
		SLAPI_PLUGIN_PRE_ADD_FN,
		SLAPI_PLUGIN_POST_ADD_FN,
376
377
		SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
		SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
378
379
380
381
382
		NULL
	},
	{
		SLAPI_PLUGIN_PRE_DELETE_FN,
		SLAPI_PLUGIN_POST_DELETE_FN,
383
384
		SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN,
		SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
385
386
387
388
389
		NULL
	},
	{
		SLAPI_PLUGIN_PRE_ABANDON_FN,
		SLAPI_PLUGIN_POST_ABANDON_FN,
390
391
		SLAPI_PLUGIN_INTERNAL_PRE_ABANDON_FN,
		SLAPI_PLUGIN_INTERNAL_POST_ABANDON_FN,
392
393
394
		NULL
	},
	{
395
396
		0,
		0,
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
		0,
		0,
		NULL
	}
};

slap_operation_t
slapi_tag2op( ber_tag_t tag )
{
	slap_operation_t op;

	switch ( tag ) {
	case LDAP_REQ_BIND:
		op = op_bind;
		break;
	case LDAP_REQ_ADD:
		op = op_add;
		break;
	case LDAP_REQ_DELETE:
416
		op = op_delete;
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
		break;
	case LDAP_REQ_MODRDN:
		op = op_modrdn;
		break;
	case LDAP_REQ_MODIFY:
		op = op_modify;
		break;
	case LDAP_REQ_COMPARE:
		op = op_compare;
		break;
	case LDAP_REQ_SEARCH:
		op = op_search;
		break;
	case LDAP_REQ_UNBIND:
		op = op_unbind;
		break;
	default:
		op = op_last;
		break;
	}

	return op;
}

441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/* Add SLAPI_RESCONTROLS to rs->sr_ctrls, with care, because
 * rs->sr_ctrls could be allocated on the stack */
static int
slapi_over_merge_controls( Operation *op, SlapReply *rs )
{
	Slapi_PBlock		*pb = SLAPI_OPERATION_PBLOCK( op );
	LDAPControl		**ctrls = NULL;
	LDAPControl		**slapi_ctrls = NULL;
	size_t			n_slapi_ctrls = 0;
	size_t			n_rs_ctrls = 0;
	size_t			i;

	slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapi_ctrls );

	n_slapi_ctrls = slapi_int_count_controls( slapi_ctrls );
	n_rs_ctrls = slapi_int_count_controls( rs->sr_ctrls );

	if ( n_slapi_ctrls == 0 )
		return LDAP_SUCCESS; /* no SLAPI controls */

461
462
	slapi_pblock_set( pb, SLAPI_X_OLD_RESCONTROLS, (void *)rs->sr_ctrls );

463
464
465
466
467
468
469
470
471
472
473
474
	ctrls = (LDAPControl **) op->o_tmpalloc(
		( n_slapi_ctrls + n_rs_ctrls + 1 ) * sizeof(LDAPControl *),
		op->o_tmpmemctx );

	for ( i = 0; i < n_slapi_ctrls; i++ ) {
		ctrls[i] = slapi_ctrls[i];
	}
	if ( rs->sr_ctrls != NULL ) {
		for ( i = 0; i < n_rs_ctrls; i++ ) {
			ctrls[n_slapi_ctrls + i] = rs->sr_ctrls[i];
		}
	}
475
	ctrls[n_slapi_ctrls + n_rs_ctrls] = NULL;
476
477
478
479
480
481
482
483
484
485
486
487
488
489

	rs->sr_ctrls = ctrls;

	return LDAP_SUCCESS;
}

static int
slapi_over_unmerge_controls( Operation *op, SlapReply *rs )
{
	Slapi_PBlock		*pb = SLAPI_OPERATION_PBLOCK( op );
	LDAPControl		**rs_ctrls = NULL;

	slapi_pblock_get( pb, SLAPI_X_OLD_RESCONTROLS, (void **)&rs_ctrls );

490
	if ( rs_ctrls == NULL || rs->sr_ctrls == rs_ctrls ) {
491
492
493
494
495
496
497
498
499
500
		/* no copying done */
		return LDAP_SUCCESS;
	}

	op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
	rs->sr_ctrls = rs_ctrls;

	return LDAP_SUCCESS;
}

501
static int
Luke Howard's avatar
Luke Howard committed
502
slapi_over_response( Operation *op, SlapReply *rs )
503
{
504
505
	Slapi_PBlock		*pb = SLAPI_OPERATION_PBLOCK( op );
	int			rc = SLAP_CB_CONTINUE;
506

507
	if ( pb->pb_intop == 0 ) {
508
509
		switch ( rs->sr_type ) {
		case REP_RESULT:
510
511
		case REP_SASL:
		case REP_EXTENDED:
512
513
514
515
516
517
518
519
520
521
522
			rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
			break;
		case REP_SEARCH:
			rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
			break;
		case REP_SEARCHREF:
			rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
			break;
		default:
			break;
		}
523
524
	}

525
526
	slapi_over_merge_controls( op, rs );

527
528
529
530
	return rc;
}

static int
Luke Howard's avatar
Luke Howard committed
531
slapi_over_cleanup( Operation *op, SlapReply *rs )
532
{
533
534
	Slapi_PBlock		*pb = SLAPI_OPERATION_PBLOCK( op );
	int			rc = SLAP_CB_CONTINUE;
535

536
537
	slapi_over_unmerge_controls( op, rs );

538
	if ( pb->pb_intop == 0 ) {
539
540
		switch ( rs->sr_type ) {
		case REP_RESULT:
541
542
		case REP_SASL:
		case REP_EXTENDED:
543
544
545
546
547
548
549
550
551
552
553
			rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
			break;
		case REP_SEARCH:
			rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
			break;
		case REP_SEARCHREF:
			rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
			break;
		default:
			break;
		}
554
555
	}

556
	return rc;
557
558
559
560
561
}

static int
slapi_op_func( Operation *op, SlapReply *rs )
{
562
	Slapi_PBlock		*pb;
563
564
	slap_operation_t	which;
	struct slapi_op_info	*opinfo;
565
	int			rc;
566
567
	slap_overinfo		*oi;
	slap_overinst		*on;
568
	slap_callback		cb;
569
570
	int			internal_op;
	int			preop_type, postop_type;
571
	BackendDB		*be;
572

573
	if ( !slapi_plugins_used )
574
575
576
577
578
		return SLAP_CB_CONTINUE;

	/*
	 * Find the SLAPI operation information for this LDAP
	 * operation; this will contain the preop and postop
Luke Howard's avatar
cleanup    
Luke Howard committed
579
580
	 * plugin types, as well as optional callbacks for
	 * setting up the SLAPI environment.
581
582
583
	 */
	which = slapi_tag2op( op->o_tag );
	if ( which >= op_last ) {
Luke Howard's avatar
cleanup    
Luke Howard committed
584
		/* invalid operation, but let someone else deal with it */
585
586
587
588
		return SLAP_CB_CONTINUE;
	}

	opinfo = &slapi_op_dispatch_table[which];
589
	if ( opinfo == NULL ) {
Luke Howard's avatar
cleanup    
Luke Howard committed
590
		/* no SLAPI plugin types for this operation */
591
592
593
		return SLAP_CB_CONTINUE;
	}

594
	internal_op = slapi_op_internal_p( op, rs, &cb );
595
596
597
598
599
600
601
602
603
604
605

	if ( internal_op ) {
		preop_type = opinfo->soi_internal_preop;
		postop_type = opinfo->soi_internal_postop;
	} else {
		preop_type = opinfo->soi_preop;
		postop_type = opinfo->soi_postop;
	}

	if ( preop_type == 0 ) {
		/* no SLAPI plugin types for this operation */
606
		pb = NULL;
Luke Howard's avatar
Luke Howard committed
607
608
		rc = SLAP_CB_CONTINUE;
		goto cleanup;
609
610
611
	}

	pb = SLAPI_OPERATION_PBLOCK( op );
612

613
614
615
616
	/* cache backend so we call correct postop plugins */
	be = pb->pb_op->o_bd;

	rc = slapi_int_call_plugins( be, preop_type, pb );
617
618
619
620
621

	/*
	 * soi_callback is responsible for examining the result code
	 * of the preoperation plugin and determining whether to
	 * abort. This is needed because of special SLAPI behaviour
Luke Howard's avatar
Luke Howard committed
622
	 e with bind preoperation plugins.
623
624
625
626
627
628
	 *
	 * The soi_callback function is also used to reset any values
	 * returned from the preoperation plugin before calling the
	 * backend (for the success case).
	 */
	if ( opinfo->soi_callback == NULL ) {
Luke Howard's avatar
cleanup    
Luke Howard committed
629
		/* default behaviour is preop plugin can abort operation */
Luke Howard's avatar
Luke Howard committed
630
631
		if ( rc < 0 ) {
			rc = rs->sr_err;
632
			goto cleanup;
Luke Howard's avatar
Luke Howard committed
633
		}
634
	} else {
Luke Howard's avatar
Luke Howard committed
635
		rc = (opinfo->soi_callback)( op, rs, rc );
636
		if ( rc )
637
638
639
640
			goto cleanup;
	}

	/*
Luke Howard's avatar
Luke Howard committed
641
642
643
644
645
646
	 * Call actual backend (or next overlay in stack). We need to
	 * do this rather than returning SLAP_CB_CONTINUE and calling
	 * postoperation plugins in a response handler to match the
	 * behaviour of SLAPI in OpenLDAP 2.2, where postoperation
	 * plugins are called after the backend has completely
	 * finished processing the operation.
647
648
649
650
	 */
	on = (slap_overinst *)op->o_bd->bd_info;
	oi = on->on_info;

Luke Howard's avatar
Luke Howard committed
651
	rc = overlay_op_walk( op, rs, which, oi, on->on_next );
652
653
654
655

	/*
	 * Call postoperation plugins
	 */
656
	slapi_int_call_plugins( be, postop_type, pb );
657
658

cleanup:
659
660
661
	if ( !internal_op ) {
		slapi_pblock_destroy(pb);
		cb.sc_private = NULL;
662
663
	}

664
665
	op->o_callback = cb.sc_next;

Luke Howard's avatar
Luke Howard committed
666
	return rc;
667
668
669
670
671
}

static int
slapi_over_extended( Operation *op, SlapReply *rs )
{
672
	Slapi_PBlock	*pb;
673
674
	SLAPI_FUNC	callback;
	int		rc;
675
676
	int		internal_op;
	slap_callback	cb;
677
678
679
680
681
682

	slapi_int_get_extop_plugin( &op->ore_reqoid, &callback );
	if ( callback == NULL ) {
		return SLAP_CB_CONTINUE;
	}

683
	internal_op = slapi_op_internal_p( op, rs, &cb );
684
685
686
687
688
	if ( internal_op ) {
		return SLAP_CB_CONTINUE;
	}

	pb = SLAPI_OPERATION_PBLOCK( op );
689
690
691

	rc = (*callback)( pb );
	if ( rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
692
		goto cleanup;
693
	} else if ( rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
694
695
		rc = SLAP_CB_CONTINUE;
		goto cleanup;
696
697
	}

698
	assert( rs->sr_rspoid != NULL );
699
700
701

	send_ldap_extended( op, rs );

702
#if 0
703
	slapi_ch_free_string( (char **)&rs->sr_rspoid );
704
#endif
705
706
707
708

	if ( rs->sr_rspdata != NULL )
		ber_bvfree( rs->sr_rspdata );

709
710
711
	rc = rs->sr_err;

cleanup:
712
	slapi_pblock_destroy( pb );
713
	op->o_callback = cb.sc_next;
714

715
	return rc;
716
717
718
719
720
721
722
723
724
725
726
727
}

static int
slapi_over_access_allowed(
	Operation		*op,
	Entry			*e,
	AttributeDescription	*desc,
	struct berval		*val,
	slap_access_t		access,
	AccessControlState	*state,
	slap_mask_t		*maskp )
{
728
729
730
	int			rc;
	Slapi_PBlock		*pb;
	slap_callback		cb;
731
	int			internal_op;
732
	SlapReply		rs = { REP_RESULT };
733

734
	internal_op = slapi_op_internal_p( op, &rs, &cb );
735
736
737

	cb.sc_response = NULL;
	cb.sc_cleanup = NULL;
738
	cb.sc_writewait = NULL;
739
740

	pb = SLAPI_OPERATION_PBLOCK( op );
741
742

	rc = slapi_int_access_allowed( op, e, desc, val, access, state );
743
	if ( rc ) {
744
		rc = SLAP_CB_CONTINUE;
Luke Howard's avatar
Luke Howard committed
745
	}
746

747
	if ( !internal_op ) {
748
		slapi_pblock_destroy( pb );
749
750
751
	}

	op->o_callback = cb.sc_next;
752

753
754
755
756
757
758
759
760
761
762
763
764
	return rc;
}

static int
slapi_over_acl_group(
	Operation		*op,
	Entry			*target,
	struct berval		*gr_ndn,
	struct berval		*op_ndn,
	ObjectClass		*group_oc,
	AttributeDescription	*group_at )
{
Luke Howard's avatar
Luke Howard committed
765
766
	Slapi_Entry		*e;
	int			rc;
767
	Slapi_PBlock		*pb;
768
769
	BackendDB		*be = op->o_bd;
	GroupAssertion		*g;
770
	SlapReply		rs = { REP_RESULT };
771

772
	op->o_bd = select_backend( gr_ndn, 0 );
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787

	for ( g = op->o_groups; g; g = g->ga_next ) {
		if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
			g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
		{
			continue;
		}
		if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
			break;
		}
	}
	if ( g != NULL ) {
		rc = g->ga_res;
		goto done;
	}
Luke Howard's avatar
Luke Howard committed
788

789
790
791
792
	if ( target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
		e = target;
		rc = 0;
	} else {
793
		rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
794
	}
795
	if ( e != NULL ) {
796
797
798
		int			internal_op;
		slap_callback		cb;

799
		internal_op = slapi_op_internal_p( op, &rs, &cb );
800
801
802

		cb.sc_response = NULL;
		cb.sc_cleanup = NULL;
803
		cb.sc_writewait = NULL;
804
805

		pb = SLAPI_OPERATION_PBLOCK( op );
806

807
808
809
810
		slapi_pblock_set( pb, SLAPI_X_GROUP_ENTRY,        (void *)e );
		slapi_pblock_set( pb, SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
		slapi_pblock_set( pb, SLAPI_X_GROUP_ATTRIBUTE,    (void *)group_at->ad_cname.bv_val );
		slapi_pblock_set( pb, SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
Luke Howard's avatar
Luke Howard committed
811

812
		rc = slapi_over_call_plugins( pb, SLAPI_X_PLUGIN_PRE_GROUP_FN );
813
814
815
		if ( rc >= 0 ) /* 1 means no plugins called */
			rc = SLAP_CB_CONTINUE;
		else
816
			rc = pb->pb_rs->sr_err;
817

Luke Howard's avatar
Luke Howard committed
818
819
820
821
822
		slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ENTRY );
		slapi_pblock_delete_param( pb, SLAPI_X_GROUP_OPERATION_DN );
		slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ATTRIBUTE );
		slapi_pblock_delete_param( pb, SLAPI_X_GROUP_TARGET_ENTRY );

823
824
		if ( !internal_op )
			slapi_pblock_destroy( pb );
Luke Howard's avatar
Luke Howard committed
825

826
827
828
		if ( e != target ) {
			be_entry_release_r( op, e );
		}
829
830

		op->o_callback = cb.sc_next;
831
832
	} else {
		rc = LDAP_NO_SUCH_OBJECT; /* return SLAP_CB_CONTINUE for correctness? */
833
	}
Luke Howard's avatar
Luke Howard committed
834

835
836
	if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache &&
	     rc != SLAP_CB_CONTINUE ) {
837
838
839
840
841
842
843
844
845
846
847
		g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
			op->o_tmpmemctx );
		g->ga_be = op->o_bd;
		g->ga_oc = group_oc;
		g->ga_at = group_at;
		g->ga_res = rc;
		g->ga_len = gr_ndn->bv_len;
		strcpy( g->ga_ndn, gr_ndn->bv_val );
		g->ga_next = op->o_groups;
		op->o_groups = g;
	}
Luke Howard's avatar
Luke Howard committed
848
849
850
851
	/*
	 * XXX don't call POST_GROUP_FN, I have no idea what the point of
	 * that plugin function was anyway
	 */
852
853
done:
	op->o_bd = be;
Luke Howard's avatar
Luke Howard committed
854
	return rc;
855
856
}

857
static int
858
859
860
slapi_over_db_open(
	BackendDB	*be,
	ConfigReply	*cr )
861
862
863
864
865
866
867
868
869
870
871
872
873
874
{
	Slapi_PBlock		*pb;
	int			rc;

	pb = slapi_pblock_new();

	rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_START_FN, pb );

	slapi_pblock_destroy( pb );

	return rc;
}

static int
875
876
877
slapi_over_db_close(
	BackendDB	*be,
	ConfigReply	*cr )
878
879
880
881
882
883
884
885
886
887
888
889
890
{
	Slapi_PBlock		*pb;
	int			rc;

	pb = slapi_pblock_new();

	rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_CLOSE_FN, pb );

	slapi_pblock_destroy( pb );

	return rc;
}

891
892
static int
slapi_over_init()
893
894
895
{
	memset( &slapi, 0, sizeof(slapi) );

Luke Howard's avatar
Luke Howard committed
896
	slapi.on_bi.bi_type = SLAPI_OVERLAY_NAME;
897

Luke Howard's avatar
Luke Howard committed
898
899
900
901
902
903
904
905
906
907
908
	slapi.on_bi.bi_op_bind 		= slapi_op_func;
	slapi.on_bi.bi_op_unbind	= slapi_op_func;
	slapi.on_bi.bi_op_search	= slapi_op_func;
	slapi.on_bi.bi_op_compare	= slapi_op_func;
	slapi.on_bi.bi_op_modify	= slapi_op_func;
	slapi.on_bi.bi_op_modrdn	= slapi_op_func;
	slapi.on_bi.bi_op_add		= slapi_op_func;
	slapi.on_bi.bi_op_delete	= slapi_op_func;
	slapi.on_bi.bi_op_abandon	= slapi_op_func;
	slapi.on_bi.bi_op_cancel	= slapi_op_func;

909
910
911
	slapi.on_bi.bi_db_open		= slapi_over_db_open;
	slapi.on_bi.bi_db_close		= slapi_over_db_close;

Luke Howard's avatar
Luke Howard committed
912
913
914
915
	slapi.on_bi.bi_extended		= slapi_over_extended;
	slapi.on_bi.bi_access_allowed	= slapi_over_access_allowed;
	slapi.on_bi.bi_operational	= slapi_over_aux_operational;
	slapi.on_bi.bi_acl_group	= slapi_over_acl_group;
916
917
918
919

	return overlay_register( &slapi );
}

920
921
922
923
924
int slapi_over_is_inst( BackendDB *be )
{
	return overlay_is_inst( be, SLAPI_OVERLAY_NAME );
}

925
int slapi_over_config( BackendDB *be, ConfigReply *cr )
926
927
928
929
930
931
932
933
934
{
	if ( slapi_over_initialized == 0 ) {
		int rc;

		/* do global initializaiton */
		ldap_pvt_thread_mutex_init( &slapi_hn_mutex );
		ldap_pvt_thread_mutex_init( &slapi_time_mutex );
		ldap_pvt_thread_mutex_init( &slapi_printmessage_mutex );

Luke Howard's avatar
Luke Howard committed
935
936
		if ( slapi_log_file == NULL )
			slapi_log_file = slapi_ch_strdup( LDAP_RUNDIR LDAP_DIRSEP "errors" );
937
938
939
940
941
942
943
944
945
946
947
948

		rc = slapi_int_init_object_extensions();
		if ( rc != 0 )
			return rc;

		rc = slapi_over_init();
		if ( rc != 0 )
			return rc;

		slapi_over_initialized = 1;
	}

949
	return overlay_config( be, SLAPI_OVERLAY_NAME, -1, NULL, cr );
950
951
}

952
#endif /* LDAP_SLAPI */