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 )
43 pb = slapi_pblock_new();
49 pb->pconn = op->o_conn;
52 PBLOCK_ASSERT_OP( pb, 0 );
58 slapi_op_internal_p( Operation *op, slap_callback *cb )
61 Slapi_PBlock *pb = NULL;
65 * Abstraction violating check for SLAPI internal operations
66 * allows pblock to remain consistent when invoking internal
69 for ( pcb = op->o_callback; pcb != NULL; pcb = pcb->sc_next ) {
70 if ( pcb->sc_response == slapi_int_response ) {
71 pb = (Slapi_PBlock *)pcb->sc_private;
72 PBLOCK_ASSERT_INTOP( pb, 0 );
80 pb = slapi_over_pblock_new( op );
83 cb->sc_response = slapi_over_response;
84 cb->sc_cleanup = slapi_over_cleanup;
86 cb->sc_next = op->o_callback;
94 slapi_over_compute_output(
95 computed_attr_context *c,
96 Slapi_Attr *attribute,
101 AttributeDescription *desc;
102 SlapReply *rs = (SlapReply *)c->cac_private;
104 if ( c == NULL || attribute == NULL || entry == NULL ) {
108 assert( rs->sr_entry == entry );
110 desc = attribute->a_desc;
112 if ( rs->sr_attrs == NULL ) {
113 /* All attrs request, skip operational attributes */
114 if ( is_at_operational( desc->ad_type ) ) {
118 /* Specific attributes requested */
119 if ( is_at_operational( desc->ad_type ) ) {
120 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
121 !ad_inlist( desc, rs->sr_attrs ) ) {
125 if ( !SLAP_USERATTRS( rs->sr_attr_flags ) &&
126 !ad_inlist( desc, rs->sr_attrs ) ) {
132 /* XXX perhaps we should check for existing attributes and merge */
133 for ( a = &rs->sr_operational_attrs; *a != NULL; a = &(*a)->a_next )
136 *a = slapi_attr_dup( attribute );
142 slapi_over_aux_operational( Operation *op, SlapReply *rs )
144 /* Support for computed attribute plugins */
145 computed_attr_context ctx;
148 if ( slapi_op_internal_p( op, NULL ) ) {
149 return SLAP_CB_CONTINUE;
152 ctx.cac_pb = slapi_over_pblock_new( op );
154 ctx.cac_private = rs;
156 if ( rs->sr_entry != NULL ) {
158 * For each client requested attribute, call the plugins.
160 if ( rs->sr_attrs != NULL ) {
161 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
162 if ( compute_evaluator( &ctx, anp->an_name.bv_val,
163 rs->sr_entry, slapi_over_compute_output ) == 1 ) {
169 * Technically we shouldn't be returning operational attributes
170 * when the user requested only user attributes. We'll let the
171 * plugin decide whether to be naughty or not.
173 compute_evaluator( &ctx, "*", rs->sr_entry, slapi_over_compute_output );
177 slapi_pblock_destroy( ctx.cac_pb );
179 return SLAP_CB_CONTINUE;
183 slapi_over_search( Operation *op, SlapReply *rs, int type )
188 assert( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF );
190 /* create a new pblock to not trample on result controls */
191 pb = slapi_over_pblock_new( op );
193 rc = slapi_int_call_plugins( op->o_bd, type, pb );
194 if ( rc >= 0 ) /* 1 means no plugins called */
195 rc = SLAP_CB_CONTINUE;
197 rc = LDAP_SUCCESS; /* confusing: don't abort, but don't send */
199 slapi_pblock_destroy(pb);
205 * Call pre- and post-result plugins
208 slapi_over_result( Operation *op, SlapReply *rs, int type )
210 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
212 assert( rs->sr_type == REP_RESULT );
214 slapi_int_call_plugins( op->o_bd, type, pb );
216 return SLAP_CB_CONTINUE;
221 slapi_op_bind_callback( Operation *op, SlapReply *rs )
226 case SLAPI_BIND_SUCCESS:
227 /* Continue with backend processing */
229 case SLAPI_BIND_FAIL:
230 /* Failure, frontend (that's us) sends result */
231 rs->sr_err = LDAP_INVALID_CREDENTIALS;
232 send_ldap_result( op, rs );
235 case SLAPI_BIND_ANONYMOUS: /* undocumented */
236 default: /* plugin sent result or no plugins called */
237 BER_BVZERO( &op->orb_edn );
239 if ( rs->sr_err == LDAP_SUCCESS ) {
241 * Plugin will have called slapi_pblock_set(LDAP_CONN_DN) which
242 * will have set conn->c_dn and conn->c_ndn
244 if ( BER_BVISNULL( &op->o_conn->c_ndn ) && rc == 1 ) {
245 /* No plugins were called; continue processing */
248 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
249 if ( !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
250 ber_len_t max = sockbuf_max_incoming_auth;
251 ber_sockbuf_ctrl( op->o_conn->c_sb,
252 LBER_SB_OPT_SET_MAX_INCOMING, &max );
254 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
256 /* log authorization identity */
257 Statslog( LDAP_DEBUG_STATS,
258 "%s BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
260 BER_BVISNULL( &op->o_conn->c_dn )
261 ? "<empty>" : op->o_conn->c_dn.bv_val,
262 op->orb_tmp_mech.bv_val, 0, 0 );
273 slapi_op_search_callback( Operation *op, SlapReply *rs )
275 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
277 /* check preoperation result code */
278 if ( rs->sr_err < 0 ) {
279 slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void **)&rs->sr_err );
283 rs->sr_err = LDAP_SUCCESS;
285 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) == 0 ) {
287 * The plugin can set the SLAPI_SEARCH_FILTER.
288 * SLAPI_SEARCH_STRFILER is not normative.
290 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
291 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
297 struct slapi_op_info {
298 int soi_preop; /* preoperation plugin parameter */
299 int soi_postop; /* postoperation plugin parameter */
300 int soi_internal_preop; /* internal preoperation plugin parameter */
301 int soi_internal_postop; /* internal postoperation plugin parameter */
302 slap_response *soi_callback; /* preoperation result handler */
303 } slapi_op_dispatch_table[] = {
305 SLAPI_PLUGIN_PRE_BIND_FN,
306 SLAPI_PLUGIN_POST_BIND_FN,
309 slapi_op_bind_callback
312 SLAPI_PLUGIN_PRE_UNBIND_FN,
313 SLAPI_PLUGIN_POST_UNBIND_FN,
319 SLAPI_PLUGIN_PRE_SEARCH_FN,
320 SLAPI_PLUGIN_POST_SEARCH_FN,
323 slapi_op_search_callback
326 SLAPI_PLUGIN_PRE_COMPARE_FN,
327 SLAPI_PLUGIN_POST_COMPARE_FN,
333 SLAPI_PLUGIN_PRE_MODIFY_FN,
334 SLAPI_PLUGIN_POST_MODIFY_FN,
335 SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
336 SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
340 SLAPI_PLUGIN_PRE_MODRDN_FN,
341 SLAPI_PLUGIN_POST_MODRDN_FN,
342 SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN,
343 SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
347 SLAPI_PLUGIN_PRE_ADD_FN,
348 SLAPI_PLUGIN_POST_ADD_FN,
349 SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
350 SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
354 SLAPI_PLUGIN_PRE_DELETE_FN,
355 SLAPI_PLUGIN_POST_DELETE_FN,
356 SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN,
357 SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
361 SLAPI_PLUGIN_PRE_ABANDON_FN,
362 SLAPI_PLUGIN_POST_ABANDON_FN,
377 slapi_tag2op( ber_tag_t tag )
388 case LDAP_REQ_DELETE:
391 case LDAP_REQ_MODRDN:
394 case LDAP_REQ_MODIFY:
397 case LDAP_REQ_COMPARE:
400 case LDAP_REQ_SEARCH:
403 case LDAP_REQ_UNBIND:
414 /* Add SLAPI_RESCONTROLS to rs->sr_ctrls, with care, because
415 * rs->sr_ctrls could be allocated on the stack */
417 slapi_over_merge_controls( Operation *op, SlapReply *rs )
419 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
420 LDAPControl **ctrls = NULL;
421 LDAPControl **slapi_ctrls = NULL;
422 size_t n_slapi_ctrls = 0;
423 size_t n_rs_ctrls = 0;
426 slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapi_ctrls );
428 n_slapi_ctrls = slapi_int_count_controls( slapi_ctrls );
429 n_rs_ctrls = slapi_int_count_controls( rs->sr_ctrls );
431 slapi_pblock_set( pb, SLAPI_X_OLD_RESCONTROLS, (void *)rs->sr_ctrls );
433 if ( n_slapi_ctrls == 0 )
434 return LDAP_SUCCESS; /* no SLAPI controls */
436 ctrls = (LDAPControl **) op->o_tmpalloc(
437 ( n_slapi_ctrls + n_rs_ctrls + 1 ) * sizeof(LDAPControl *),
440 for ( i = 0; i < n_slapi_ctrls; i++ ) {
441 ctrls[i] = slapi_ctrls[i];
443 if ( rs->sr_ctrls != NULL ) {
444 for ( i = 0; i < n_rs_ctrls; i++ ) {
445 ctrls[n_slapi_ctrls + i] = rs->sr_ctrls[i];
449 rs->sr_ctrls = ctrls;
455 slapi_over_unmerge_controls( Operation *op, SlapReply *rs )
457 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
458 LDAPControl **rs_ctrls = NULL;
460 slapi_pblock_get( pb, SLAPI_X_OLD_RESCONTROLS, (void **)&rs_ctrls );
462 if ( rs->sr_ctrls == NULL || rs->sr_ctrls == rs_ctrls ) {
463 /* no copying done */
467 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
468 rs->sr_ctrls = rs_ctrls;
474 slapi_over_response( Operation *op, SlapReply *rs )
476 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
477 int rc = SLAP_CB_CONTINUE;
479 if ( pb->internal_op == 0 ) {
480 switch ( rs->sr_type ) {
482 rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
485 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
488 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
495 slapi_over_merge_controls( op, rs );
501 slapi_over_cleanup( Operation *op, SlapReply *rs )
503 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
504 int rc = SLAP_CB_CONTINUE;
506 slapi_over_unmerge_controls( op, rs );
508 if ( pb->internal_op == 0 ) {
509 switch ( rs->sr_type ) {
511 rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
514 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
517 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
528 slapi_op_func( Operation *op, SlapReply *rs )
531 slap_operation_t which;
532 struct slapi_op_info *opinfo;
538 int preop_type, postop_type;
540 if ( !slapi_plugins_used )
541 return SLAP_CB_CONTINUE;
544 * Find the SLAPI operation information for this LDAP
545 * operation; this will contain the preop and postop
546 * plugin types, as well as optional callbacks for
547 * setting up the SLAPI environment.
549 which = slapi_tag2op( op->o_tag );
550 if ( which >= op_last ) {
551 /* invalid operation, but let someone else deal with it */
552 return SLAP_CB_CONTINUE;
555 opinfo = &slapi_op_dispatch_table[which];
556 if ( opinfo == NULL ) {
557 /* no SLAPI plugin types for this operation */
558 return SLAP_CB_CONTINUE;
561 internal_op = slapi_op_internal_p( op, &cb );
564 preop_type = opinfo->soi_internal_preop;
565 postop_type = opinfo->soi_internal_postop;
567 preop_type = opinfo->soi_preop;
568 postop_type = opinfo->soi_postop;
571 if ( preop_type == 0 ) {
572 /* no SLAPI plugin types for this operation */
573 if ( !internal_op ) {
574 slapi_pblock_destroy(pb);
575 cb.sc_private = NULL;
577 op->o_callback = cb.sc_next;
578 return SLAP_CB_CONTINUE;
581 pb = SLAPI_OPERATION_PBLOCK( op );
583 rs->sr_err = slapi_int_call_plugins( op->o_bd, preop_type, pb );
586 * soi_callback is responsible for examining the result code
587 * of the preoperation plugin and determining whether to
588 * abort. This is needed because of special SLAPI behaviour
589 * with bind preoperation plugins.
591 * The soi_callback function is also used to reset any values
592 * returned from the preoperation plugin before calling the
593 * backend (for the success case).
595 if ( opinfo->soi_callback == NULL ) {
596 /* default behaviour is preop plugin can abort operation */
597 if ( rs->sr_err < 0 )
600 rs->sr_err = LDAP_SUCCESS;
602 rc = (opinfo->soi_callback)( op, rs );
608 * Call actual backend (or next overlay in stack). We need to
609 * do this rather than returning SLAP_CB_CONTINUE and calling
610 * postoperation plugins in a response handler to match the
611 * behaviour of SLAPI in OpenLDAP 2.2, where postoperation
612 * plugins are called after the backend has completely
613 * finished processing the operation.
615 on = (slap_overinst *)op->o_bd->bd_info;
618 rs->sr_err = overlay_op_walk( op, rs, which, oi, on->on_next );
621 * Call postoperation plugins
623 slapi_int_call_plugins( op->o_bd, postop_type, pb );
626 if ( !internal_op ) {
627 slapi_pblock_destroy(pb);
628 cb.sc_private = NULL;
631 op->o_callback = cb.sc_next;
637 slapi_over_extended( Operation *op, SlapReply *rs )
645 slapi_int_get_extop_plugin( &op->ore_reqoid, &callback );
646 if ( callback == NULL ) {
647 return SLAP_CB_CONTINUE;
650 internal_op = slapi_op_internal_p( op, &cb );
652 return SLAP_CB_CONTINUE;
655 pb = SLAPI_OPERATION_PBLOCK( op );
657 rc = (*callback)( pb );
658 if ( rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
659 slapi_pblock_destroy( pb );
661 } else if ( rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
662 slapi_pblock_destroy( pb );
663 return SLAP_CB_CONTINUE;
666 assert( rs->sr_rspoid != NULL );
668 send_ldap_extended( op, rs );
671 slapi_ch_free_string( (char **)&rs->sr_rspoid );
674 if ( rs->sr_rspdata != NULL )
675 ber_bvfree( rs->sr_rspdata );
677 slapi_pblock_destroy( pb );
683 slapi_over_access_allowed(
686 AttributeDescription *desc,
688 slap_access_t access,
689 AccessControlState *state,
697 internal_op = slapi_op_internal_p( op, &cb );
699 cb.sc_response = NULL;
700 cb.sc_cleanup = NULL;
702 pb = SLAPI_OPERATION_PBLOCK( op );
704 rc = slapi_int_access_allowed( op, e, desc, val, access, state );
706 rc = SLAP_CB_CONTINUE;
709 op->o_callback = cb.sc_next;
712 slapi_pblock_destroy( pb );
718 slapi_over_acl_group(
721 struct berval *gr_ndn,
722 struct berval *op_ndn,
723 ObjectClass *group_oc,
724 AttributeDescription *group_at )
729 BackendDB *be = op->o_bd;
732 op->o_bd = select_backend( gr_ndn, 0, 0 );
734 for ( g = op->o_groups; g; g = g->ga_next ) {
735 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
736 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
740 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
749 if ( target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
753 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
759 internal_op = slapi_op_internal_p( op, &cb );
761 cb.sc_response = NULL;
762 cb.sc_cleanup = NULL;
764 pb = SLAPI_OPERATION_PBLOCK( op );
766 slapi_pblock_set( pb, SLAPI_X_GROUP_ENTRY, (void *)e );
767 slapi_pblock_set( pb, SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
768 slapi_pblock_set( pb, SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
769 slapi_pblock_set( pb, SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
771 rc = slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_PRE_GROUP_FN, pb );
772 if ( rc >= 0 ) /* 1 means no plugins called */
773 rc = SLAP_CB_CONTINUE;
778 slapi_pblock_destroy( pb );
781 be_entry_release_r( op, e );
784 rc = LDAP_NO_SUCH_OBJECT; /* return SLAP_CB_CONTINUE for correctness? */
787 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache &&
788 rc != SLAP_CB_CONTINUE ) {
789 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
795 g->ga_len = gr_ndn->bv_len;
796 strcpy( g->ga_ndn, gr_ndn->bv_val );
797 g->ga_next = op->o_groups;
801 * XXX don't call POST_GROUP_FN, I have no idea what the point of
802 * that plugin function was anyway
810 slapi_int_overlay_init()
812 memset( &slapi, 0, sizeof(slapi) );
814 slapi.on_bi.bi_type = SLAPI_OVERLAY_NAME;
816 slapi.on_bi.bi_op_bind = slapi_op_func;
817 slapi.on_bi.bi_op_unbind = slapi_op_func;
818 slapi.on_bi.bi_op_search = slapi_op_func;
819 slapi.on_bi.bi_op_compare = slapi_op_func;
820 slapi.on_bi.bi_op_modify = slapi_op_func;
821 slapi.on_bi.bi_op_modrdn = slapi_op_func;
822 slapi.on_bi.bi_op_add = slapi_op_func;
823 slapi.on_bi.bi_op_delete = slapi_op_func;
824 slapi.on_bi.bi_op_abandon = slapi_op_func;
825 slapi.on_bi.bi_op_cancel = slapi_op_func;
827 slapi.on_bi.bi_extended = slapi_over_extended;
828 slapi.on_bi.bi_access_allowed = slapi_over_access_allowed;
829 slapi.on_bi.bi_operational = slapi_over_aux_operational;
830 slapi.on_bi.bi_acl_group = slapi_over_acl_group;
832 return overlay_register( &slapi );
835 #endif /* LDAP_SLAPI */