1 /* slapi_overlay.c - SLAPI overlay */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Luke Howard for inclusion
18 * in OpenLDAP Software.
25 #include <ac/string.h>
26 #include <ac/socket.h>
33 static slap_overinst slapi;
35 static int slapi_over_response( Operation *op, SlapReply *rs );
36 static int slapi_over_cleanup( Operation *op, SlapReply *rs );
39 slapi_over_pblock_new( Operation *op, SlapReply *rs )
43 pb = slapi_pblock_new();
45 pb->pb_conn = op->o_conn;
49 PBLOCK_ASSERT_OP( pb, op->o_tag );
55 slapi_op_internal_p( Operation *op, SlapReply *rs, slap_callback *cb )
58 Slapi_PBlock *pb = NULL;
62 * Abstraction violating check for SLAPI internal operations
63 * allows pblock to remain consistent when invoking internal
66 for ( pcb = op->o_callback; pcb != NULL; pcb = pcb->sc_next ) {
67 if ( pcb->sc_response == slapi_int_response ) {
68 pb = (Slapi_PBlock *)pcb->sc_private;
69 PBLOCK_ASSERT_INTOP( pb, 0 );
77 pb = slapi_over_pblock_new( op, rs );
80 cb->sc_response = slapi_over_response;
81 cb->sc_cleanup = slapi_over_cleanup;
83 cb->sc_next = op->o_callback;
91 slapi_over_compute_output(
92 computed_attr_context *c,
93 Slapi_Attr *attribute,
98 AttributeDescription *desc;
99 SlapReply *rs = (SlapReply *)c->cac_private;
101 if ( c == NULL || attribute == NULL || entry == NULL ) {
105 assert( rs->sr_entry == entry );
107 desc = attribute->a_desc;
109 if ( rs->sr_attrs == NULL ) {
110 /* All attrs request, skip operational attributes */
111 if ( is_at_operational( desc->ad_type ) ) {
115 /* Specific attributes requested */
116 if ( is_at_operational( desc->ad_type ) ) {
117 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
118 !ad_inlist( desc, rs->sr_attrs ) ) {
122 if ( !SLAP_USERATTRS( rs->sr_attr_flags ) &&
123 !ad_inlist( desc, rs->sr_attrs ) ) {
129 /* XXX perhaps we should check for existing attributes and merge */
130 for ( a = &rs->sr_operational_attrs; *a != NULL; a = &(*a)->a_next )
133 *a = slapi_attr_dup( attribute );
139 slapi_over_aux_operational( Operation *op, SlapReply *rs )
141 /* Support for computed attribute plugins */
142 computed_attr_context ctx;
145 if ( slapi_op_internal_p( op, rs, NULL ) ) {
146 return SLAP_CB_CONTINUE;
149 ctx.cac_pb = slapi_over_pblock_new( op, rs );
151 ctx.cac_private = rs;
153 if ( rs->sr_entry != NULL ) {
155 * For each client requested attribute, call the plugins.
157 if ( rs->sr_attrs != NULL ) {
158 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
159 if ( compute_evaluator( &ctx, anp->an_name.bv_val,
160 rs->sr_entry, slapi_over_compute_output ) == 1 ) {
166 * Technically we shouldn't be returning operational attributes
167 * when the user requested only user attributes. We'll let the
168 * plugin decide whether to be naughty or not.
170 compute_evaluator( &ctx, "*", rs->sr_entry, slapi_over_compute_output );
174 slapi_pblock_destroy( ctx.cac_pb );
176 return SLAP_CB_CONTINUE;
180 * We need this function to call frontendDB (global) plugins before
181 * database plugins, if we are invoked by a slap_callback.
184 slapi_over_call_plugins( Slapi_PBlock *pb, int type )
186 int rc = 1; /* means no plugins called */
189 PBLOCK_ASSERT_OP( pb, 0 );
192 if ( op->o_bd != frontendDB ) {
193 rc = slapi_int_call_plugins( frontendDB, type, pb );
196 rc = slapi_int_call_plugins( op->o_bd, type, pb );
203 slapi_over_search( Operation *op, SlapReply *rs, int type )
208 assert( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF );
210 /* create a new pblock to not trample on result controls */
211 pb = slapi_over_pblock_new( op, rs );
213 rc = slapi_over_call_plugins( pb, type );
214 if ( rc >= 0 ) /* 1 means no plugins called */
215 rc = SLAP_CB_CONTINUE;
217 rc = LDAP_SUCCESS; /* confusing: don't abort, but don't send */
219 slapi_pblock_destroy(pb);
225 * Call pre- and post-result plugins
228 slapi_over_result( Operation *op, SlapReply *rs, int type )
230 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
232 assert( rs->sr_type == REP_RESULT );
234 slapi_over_call_plugins( pb, type );
236 return SLAP_CB_CONTINUE;
241 slapi_op_bind_callback( Operation *op, SlapReply *rs, int prc )
244 case SLAPI_BIND_SUCCESS:
245 /* Continue with backend processing */
247 case SLAPI_BIND_FAIL:
248 /* Failure, frontend (that's us) sends result */
249 rs->sr_err = LDAP_INVALID_CREDENTIALS;
250 send_ldap_result( op, rs );
253 case SLAPI_BIND_ANONYMOUS: /* undocumented */
254 default: /* plugin sent result or no plugins called */
255 BER_BVZERO( &op->orb_edn );
257 if ( rs->sr_err == LDAP_SUCCESS ) {
259 * Plugin will have called slapi_pblock_set(LDAP_CONN_DN) which
260 * will have set conn->c_dn and conn->c_ndn
262 if ( BER_BVISNULL( &op->o_conn->c_ndn ) && prc == 1 ) {
263 /* No plugins were called; continue processing */
266 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
267 if ( !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
268 ber_len_t max = sockbuf_max_incoming_auth;
269 ber_sockbuf_ctrl( op->o_conn->c_sb,
270 LBER_SB_OPT_SET_MAX_INCOMING, &max );
272 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
274 /* log authorization identity */
275 Statslog( LDAP_DEBUG_STATS,
276 "%s BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
278 BER_BVISNULL( &op->o_conn->c_dn )
279 ? "<empty>" : op->o_conn->c_dn.bv_val,
280 op->orb_tmp_mech.bv_val, 0, 0 );
291 slapi_op_search_callback( Operation *op, SlapReply *rs, int prc )
293 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
295 /* check preoperation result code */
300 rs->sr_err = LDAP_SUCCESS;
302 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) == 0 ) {
304 * The plugin can set the SLAPI_SEARCH_FILTER.
305 * SLAPI_SEARCH_STRFILER is not normative.
307 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
308 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
314 struct slapi_op_info {
315 int soi_preop; /* preoperation plugin parameter */
316 int soi_postop; /* postoperation plugin parameter */
317 int soi_internal_preop; /* internal preoperation plugin parameter */
318 int soi_internal_postop; /* internal postoperation plugin parameter */
319 int (*soi_callback)(Operation *, SlapReply *, int); /* preoperation result handler */
320 } slapi_op_dispatch_table[] = {
322 SLAPI_PLUGIN_PRE_BIND_FN,
323 SLAPI_PLUGIN_POST_BIND_FN,
326 slapi_op_bind_callback
329 SLAPI_PLUGIN_PRE_UNBIND_FN,
330 SLAPI_PLUGIN_POST_UNBIND_FN,
336 SLAPI_PLUGIN_PRE_SEARCH_FN,
337 SLAPI_PLUGIN_POST_SEARCH_FN,
340 slapi_op_search_callback
343 SLAPI_PLUGIN_PRE_COMPARE_FN,
344 SLAPI_PLUGIN_POST_COMPARE_FN,
350 SLAPI_PLUGIN_PRE_MODIFY_FN,
351 SLAPI_PLUGIN_POST_MODIFY_FN,
352 SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
353 SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
357 SLAPI_PLUGIN_PRE_MODRDN_FN,
358 SLAPI_PLUGIN_POST_MODRDN_FN,
359 SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN,
360 SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
364 SLAPI_PLUGIN_PRE_ADD_FN,
365 SLAPI_PLUGIN_POST_ADD_FN,
366 SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
367 SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
371 SLAPI_PLUGIN_PRE_DELETE_FN,
372 SLAPI_PLUGIN_POST_DELETE_FN,
373 SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN,
374 SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
378 SLAPI_PLUGIN_PRE_ABANDON_FN,
379 SLAPI_PLUGIN_POST_ABANDON_FN,
394 slapi_tag2op( ber_tag_t tag )
405 case LDAP_REQ_DELETE:
408 case LDAP_REQ_MODRDN:
411 case LDAP_REQ_MODIFY:
414 case LDAP_REQ_COMPARE:
417 case LDAP_REQ_SEARCH:
420 case LDAP_REQ_UNBIND:
431 /* Add SLAPI_RESCONTROLS to rs->sr_ctrls, with care, because
432 * rs->sr_ctrls could be allocated on the stack */
434 slapi_over_merge_controls( Operation *op, SlapReply *rs )
436 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
437 LDAPControl **ctrls = NULL;
438 LDAPControl **slapi_ctrls = NULL;
439 size_t n_slapi_ctrls = 0;
440 size_t n_rs_ctrls = 0;
443 slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapi_ctrls );
445 n_slapi_ctrls = slapi_int_count_controls( slapi_ctrls );
446 n_rs_ctrls = slapi_int_count_controls( rs->sr_ctrls );
448 slapi_pblock_set( pb, SLAPI_X_OLD_RESCONTROLS, (void *)rs->sr_ctrls );
450 if ( n_slapi_ctrls == 0 )
451 return LDAP_SUCCESS; /* no SLAPI controls */
453 ctrls = (LDAPControl **) op->o_tmpalloc(
454 ( n_slapi_ctrls + n_rs_ctrls + 1 ) * sizeof(LDAPControl *),
457 for ( i = 0; i < n_slapi_ctrls; i++ ) {
458 ctrls[i] = slapi_ctrls[i];
460 if ( rs->sr_ctrls != NULL ) {
461 for ( i = 0; i < n_rs_ctrls; i++ ) {
462 ctrls[n_slapi_ctrls + i] = rs->sr_ctrls[i];
465 ctrls[n_slapi_ctrls + n_rs_ctrls] = NULL;
467 rs->sr_ctrls = ctrls;
473 slapi_over_unmerge_controls( Operation *op, SlapReply *rs )
475 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
476 LDAPControl **rs_ctrls = NULL;
478 slapi_pblock_get( pb, SLAPI_X_OLD_RESCONTROLS, (void **)&rs_ctrls );
480 if ( rs->sr_ctrls == NULL || rs->sr_ctrls == rs_ctrls ) {
481 /* no copying done */
485 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
486 rs->sr_ctrls = rs_ctrls;
492 slapi_over_response( Operation *op, SlapReply *rs )
494 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
495 int rc = SLAP_CB_CONTINUE;
497 if ( pb->pb_intop == 0 ) {
498 switch ( rs->sr_type ) {
500 rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
503 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
506 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
513 slapi_over_merge_controls( op, rs );
519 slapi_over_cleanup( Operation *op, SlapReply *rs )
521 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
522 int rc = SLAP_CB_CONTINUE;
524 slapi_over_unmerge_controls( op, rs );
526 if ( pb->pb_intop == 0 ) {
527 switch ( rs->sr_type ) {
529 rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
532 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
535 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
546 slapi_op_func( Operation *op, SlapReply *rs )
549 slap_operation_t which;
550 struct slapi_op_info *opinfo;
556 int preop_type, postop_type;
558 if ( !slapi_plugins_used )
559 return SLAP_CB_CONTINUE;
562 * Find the SLAPI operation information for this LDAP
563 * operation; this will contain the preop and postop
564 * plugin types, as well as optional callbacks for
565 * setting up the SLAPI environment.
567 which = slapi_tag2op( op->o_tag );
568 if ( which >= op_last ) {
569 /* invalid operation, but let someone else deal with it */
570 return SLAP_CB_CONTINUE;
573 opinfo = &slapi_op_dispatch_table[which];
574 if ( opinfo == NULL ) {
575 /* no SLAPI plugin types for this operation */
576 return SLAP_CB_CONTINUE;
579 internal_op = slapi_op_internal_p( op, rs, &cb );
582 preop_type = opinfo->soi_internal_preop;
583 postop_type = opinfo->soi_internal_postop;
585 preop_type = opinfo->soi_preop;
586 postop_type = opinfo->soi_postop;
589 if ( preop_type == 0 ) {
590 /* no SLAPI plugin types for this operation */
592 rc = SLAP_CB_CONTINUE;
596 pb = SLAPI_OPERATION_PBLOCK( op );
598 rc = slapi_int_call_plugins( op->o_bd, preop_type, pb );
601 * soi_callback is responsible for examining the result code
602 * of the preoperation plugin and determining whether to
603 * abort. This is needed because of special SLAPI behaviour
604 e with bind preoperation plugins.
606 * The soi_callback function is also used to reset any values
607 * returned from the preoperation plugin before calling the
608 * backend (for the success case).
610 if ( opinfo->soi_callback == NULL ) {
611 /* default behaviour is preop plugin can abort operation */
617 rc = (opinfo->soi_callback)( op, rs, rc );
623 * Call actual backend (or next overlay in stack). We need to
624 * do this rather than returning SLAP_CB_CONTINUE and calling
625 * postoperation plugins in a response handler to match the
626 * behaviour of SLAPI in OpenLDAP 2.2, where postoperation
627 * plugins are called after the backend has completely
628 * finished processing the operation.
630 on = (slap_overinst *)op->o_bd->bd_info;
633 rc = overlay_op_walk( op, rs, which, oi, on->on_next );
636 * Call postoperation plugins
638 slapi_int_call_plugins( op->o_bd, postop_type, pb );
641 if ( !internal_op ) {
642 slapi_pblock_destroy(pb);
643 cb.sc_private = NULL;
646 op->o_callback = cb.sc_next;
652 slapi_over_extended( Operation *op, SlapReply *rs )
660 slapi_int_get_extop_plugin( &op->ore_reqoid, &callback );
661 if ( callback == NULL ) {
662 return SLAP_CB_CONTINUE;
665 internal_op = slapi_op_internal_p( op, rs, &cb );
667 return SLAP_CB_CONTINUE;
670 pb = SLAPI_OPERATION_PBLOCK( op );
672 rc = (*callback)( pb );
673 if ( rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
674 slapi_pblock_destroy( pb );
676 } else if ( rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
677 slapi_pblock_destroy( pb );
678 return SLAP_CB_CONTINUE;
681 assert( rs->sr_rspoid != NULL );
683 send_ldap_extended( op, rs );
686 slapi_ch_free_string( (char **)&rs->sr_rspoid );
689 if ( rs->sr_rspdata != NULL )
690 ber_bvfree( rs->sr_rspdata );
692 slapi_pblock_destroy( pb );
698 slapi_over_access_allowed(
701 AttributeDescription *desc,
703 slap_access_t access,
704 AccessControlState *state,
711 SlapReply rs = { REP_RESULT };
713 internal_op = slapi_op_internal_p( op, &rs, &cb );
715 cb.sc_response = NULL;
716 cb.sc_cleanup = NULL;
718 pb = SLAPI_OPERATION_PBLOCK( op );
720 rc = slapi_int_access_allowed( op, e, desc, val, access, state );
722 rc = SLAP_CB_CONTINUE;
725 op->o_callback = cb.sc_next;
728 slapi_pblock_destroy( pb );
734 slapi_over_acl_group(
737 struct berval *gr_ndn,
738 struct berval *op_ndn,
739 ObjectClass *group_oc,
740 AttributeDescription *group_at )
745 BackendDB *be = op->o_bd;
747 SlapReply rs = { REP_RESULT };
749 op->o_bd = select_backend( gr_ndn, 0, 0 );
751 for ( g = op->o_groups; g; g = g->ga_next ) {
752 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
753 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
757 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
766 if ( target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
770 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
776 internal_op = slapi_op_internal_p( op, &rs, &cb );
778 cb.sc_response = NULL;
779 cb.sc_cleanup = NULL;
781 pb = SLAPI_OPERATION_PBLOCK( op );
783 slapi_pblock_set( pb, SLAPI_X_GROUP_ENTRY, (void *)e );
784 slapi_pblock_set( pb, SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
785 slapi_pblock_set( pb, SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
786 slapi_pblock_set( pb, SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
788 rc = slapi_over_call_plugins( pb, SLAPI_X_PLUGIN_PRE_GROUP_FN );
789 if ( rc >= 0 ) /* 1 means no plugins called */
790 rc = SLAP_CB_CONTINUE;
792 rc = pb->pb_rs->sr_err;
794 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ENTRY );
795 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_OPERATION_DN );
796 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ATTRIBUTE );
797 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_TARGET_ENTRY );
800 slapi_pblock_destroy( pb );
803 be_entry_release_r( op, e );
806 rc = LDAP_NO_SUCH_OBJECT; /* return SLAP_CB_CONTINUE for correctness? */
809 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache &&
810 rc != SLAP_CB_CONTINUE ) {
811 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
817 g->ga_len = gr_ndn->bv_len;
818 strcpy( g->ga_ndn, gr_ndn->bv_val );
819 g->ga_next = op->o_groups;
823 * XXX don't call POST_GROUP_FN, I have no idea what the point of
824 * that plugin function was anyway
832 slapi_int_overlay_init()
834 memset( &slapi, 0, sizeof(slapi) );
836 slapi.on_bi.bi_type = SLAPI_OVERLAY_NAME;
838 slapi.on_bi.bi_op_bind = slapi_op_func;
839 slapi.on_bi.bi_op_unbind = slapi_op_func;
840 slapi.on_bi.bi_op_search = slapi_op_func;
841 slapi.on_bi.bi_op_compare = slapi_op_func;
842 slapi.on_bi.bi_op_modify = slapi_op_func;
843 slapi.on_bi.bi_op_modrdn = slapi_op_func;
844 slapi.on_bi.bi_op_add = slapi_op_func;
845 slapi.on_bi.bi_op_delete = slapi_op_func;
846 slapi.on_bi.bi_op_abandon = slapi_op_func;
847 slapi.on_bi.bi_op_cancel = slapi_op_func;
849 slapi.on_bi.bi_extended = slapi_over_extended;
850 slapi.on_bi.bi_access_allowed = slapi_over_access_allowed;
851 slapi.on_bi.bi_operational = slapi_over_aux_operational;
852 slapi.on_bi.bi_acl_group = slapi_over_acl_group;
854 return overlay_register( &slapi );
857 #endif /* LDAP_SLAPI */