1 /* slapi_overlay.c - SLAPI overlay */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2006 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;
34 static int slapi_over_initialized = 0;
36 static int slapi_over_response( Operation *op, SlapReply *rs );
37 static int slapi_over_cleanup( Operation *op, SlapReply *rs );
40 slapi_over_pblock_new( Operation *op, SlapReply *rs )
44 pb = slapi_pblock_new();
46 pb->pb_conn = op->o_conn;
50 PBLOCK_ASSERT_OP( pb, op->o_tag );
56 slapi_op_internal_p( Operation *op, SlapReply *rs, slap_callback *cb )
59 Slapi_PBlock *pb = NULL;
63 * Abstraction violating check for SLAPI internal operations
64 * allows pblock to remain consistent when invoking internal
67 for ( pcb = op->o_callback; pcb != NULL; pcb = pcb->sc_next ) {
68 if ( pcb->sc_response == slapi_int_response ) {
69 pb = (Slapi_PBlock *)pcb->sc_private;
70 PBLOCK_ASSERT_INTOP( pb, 0 );
78 pb = slapi_over_pblock_new( op, rs );
81 cb->sc_response = slapi_over_response;
82 cb->sc_cleanup = slapi_over_cleanup;
84 cb->sc_next = op->o_callback;
92 slapi_over_compute_output(
93 computed_attr_context *c,
94 Slapi_Attr *attribute,
99 AttributeDescription *desc;
100 SlapReply *rs = (SlapReply *)c->cac_private;
102 if ( c == NULL || attribute == NULL || entry == NULL ) {
106 assert( rs->sr_entry == entry );
108 desc = attribute->a_desc;
110 if ( rs->sr_attrs == NULL ) {
111 /* All attrs request, skip operational attributes */
112 if ( is_at_operational( desc->ad_type ) ) {
116 /* Specific attributes requested */
117 if ( is_at_operational( desc->ad_type ) ) {
118 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
119 !ad_inlist( desc, rs->sr_attrs ) ) {
123 if ( !SLAP_USERATTRS( rs->sr_attr_flags ) &&
124 !ad_inlist( desc, rs->sr_attrs ) ) {
130 /* XXX perhaps we should check for existing attributes and merge */
131 for ( a = &rs->sr_operational_attrs; *a != NULL; a = &(*a)->a_next )
134 *a = slapi_attr_dup( attribute );
140 slapi_over_aux_operational( Operation *op, SlapReply *rs )
142 /* Support for computed attribute plugins */
143 computed_attr_context ctx;
146 if ( slapi_op_internal_p( op, rs, NULL ) ) {
147 return SLAP_CB_CONTINUE;
150 ctx.cac_pb = slapi_over_pblock_new( op, rs );
152 ctx.cac_private = rs;
154 if ( rs->sr_entry != NULL ) {
156 * For each client requested attribute, call the plugins.
158 if ( rs->sr_attrs != NULL ) {
159 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
160 if ( compute_evaluator( &ctx, anp->an_name.bv_val,
161 rs->sr_entry, slapi_over_compute_output ) == 1 ) {
167 * Technically we shouldn't be returning operational attributes
168 * when the user requested only user attributes. We'll let the
169 * plugin decide whether to be naughty or not.
171 compute_evaluator( &ctx, "*", rs->sr_entry, slapi_over_compute_output );
175 slapi_pblock_destroy( ctx.cac_pb );
177 return SLAP_CB_CONTINUE;
181 * We need this function to call frontendDB (global) plugins before
182 * database plugins, if we are invoked by a slap_callback.
185 slapi_over_call_plugins( Slapi_PBlock *pb, int type )
187 int rc = 1; /* means no plugins called */
190 PBLOCK_ASSERT_OP( pb, 0 );
193 if ( !be_match( op->o_bd, frontendDB ) ) {
194 rc = slapi_int_call_plugins( frontendDB, type, pb );
197 rc = slapi_int_call_plugins( op->o_bd, type, pb );
204 slapi_over_search( Operation *op, SlapReply *rs, int type )
209 assert( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF );
211 /* create a new pblock to not trample on result controls */
212 pb = slapi_over_pblock_new( op, rs );
214 rc = slapi_over_call_plugins( pb, type );
215 if ( rc >= 0 ) /* 1 means no plugins called */
216 rc = SLAP_CB_CONTINUE;
218 rc = LDAP_SUCCESS; /* confusing: don't abort, but don't send */
220 slapi_pblock_destroy(pb);
226 * Call pre- and post-result plugins
229 slapi_over_result( Operation *op, SlapReply *rs, int type )
231 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
233 assert( rs->sr_type == REP_RESULT || rs->sr_type == REP_SASL || rs->sr_type == REP_EXTENDED );
235 slapi_over_call_plugins( pb, type );
237 return SLAP_CB_CONTINUE;
242 slapi_op_bind_callback( Operation *op, SlapReply *rs, int prc )
245 case SLAPI_BIND_SUCCESS:
246 /* Continue with backend processing */
248 case SLAPI_BIND_FAIL:
249 /* Failure, frontend (that's us) sends result */
250 rs->sr_err = LDAP_INVALID_CREDENTIALS;
251 send_ldap_result( op, rs );
254 case SLAPI_BIND_ANONYMOUS: /* undocumented */
255 default: /* plugin sent result or no plugins called */
256 BER_BVZERO( &op->orb_edn );
258 if ( rs->sr_err == LDAP_SUCCESS ) {
260 * Plugin will have called slapi_pblock_set(LDAP_CONN_DN) which
261 * will have set conn->c_dn and conn->c_ndn
263 if ( BER_BVISNULL( &op->o_conn->c_ndn ) && prc == 1 ) {
264 /* No plugins were called; continue processing */
267 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
268 if ( !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
269 ber_len_t max = sockbuf_max_incoming_auth;
270 ber_sockbuf_ctrl( op->o_conn->c_sb,
271 LBER_SB_OPT_SET_MAX_INCOMING, &max );
273 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
275 /* log authorization identity */
276 Statslog( LDAP_DEBUG_STATS,
277 "%s BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
279 BER_BVISNULL( &op->o_conn->c_dn )
280 ? "<empty>" : op->o_conn->c_dn.bv_val,
281 BER_BVISNULL( &op->orb_tmp_mech )
282 ? "<empty>" : op->orb_tmp_mech.bv_val, 0, 0 );
293 slapi_op_search_callback( Operation *op, SlapReply *rs, int prc )
295 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
297 /* check preoperation result code */
302 rs->sr_err = LDAP_SUCCESS;
304 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) == 0 ) {
306 * The plugin can set the SLAPI_SEARCH_FILTER.
307 * SLAPI_SEARCH_STRFILER is not normative.
309 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
310 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
316 struct slapi_op_info {
317 int soi_preop; /* preoperation plugin parameter */
318 int soi_postop; /* postoperation plugin parameter */
319 int soi_internal_preop; /* internal preoperation plugin parameter */
320 int soi_internal_postop; /* internal postoperation plugin parameter */
321 int (*soi_callback)(Operation *, SlapReply *, int); /* preoperation result handler */
322 } slapi_op_dispatch_table[] = {
324 SLAPI_PLUGIN_PRE_BIND_FN,
325 SLAPI_PLUGIN_POST_BIND_FN,
328 slapi_op_bind_callback
331 SLAPI_PLUGIN_PRE_UNBIND_FN,
332 SLAPI_PLUGIN_POST_UNBIND_FN,
338 SLAPI_PLUGIN_PRE_SEARCH_FN,
339 SLAPI_PLUGIN_POST_SEARCH_FN,
342 slapi_op_search_callback
345 SLAPI_PLUGIN_PRE_COMPARE_FN,
346 SLAPI_PLUGIN_POST_COMPARE_FN,
352 SLAPI_PLUGIN_PRE_MODIFY_FN,
353 SLAPI_PLUGIN_POST_MODIFY_FN,
354 SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
355 SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
359 SLAPI_PLUGIN_PRE_MODRDN_FN,
360 SLAPI_PLUGIN_POST_MODRDN_FN,
361 SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN,
362 SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
366 SLAPI_PLUGIN_PRE_ADD_FN,
367 SLAPI_PLUGIN_POST_ADD_FN,
368 SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
369 SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
373 SLAPI_PLUGIN_PRE_DELETE_FN,
374 SLAPI_PLUGIN_POST_DELETE_FN,
375 SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN,
376 SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
380 SLAPI_PLUGIN_PRE_ABANDON_FN,
381 SLAPI_PLUGIN_POST_ABANDON_FN,
396 slapi_tag2op( ber_tag_t tag )
407 case LDAP_REQ_DELETE:
410 case LDAP_REQ_MODRDN:
413 case LDAP_REQ_MODIFY:
416 case LDAP_REQ_COMPARE:
419 case LDAP_REQ_SEARCH:
422 case LDAP_REQ_UNBIND:
433 /* Add SLAPI_RESCONTROLS to rs->sr_ctrls, with care, because
434 * rs->sr_ctrls could be allocated on the stack */
436 slapi_over_merge_controls( Operation *op, SlapReply *rs )
438 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
439 LDAPControl **ctrls = NULL;
440 LDAPControl **slapi_ctrls = NULL;
441 size_t n_slapi_ctrls = 0;
442 size_t n_rs_ctrls = 0;
445 slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapi_ctrls );
447 n_slapi_ctrls = slapi_int_count_controls( slapi_ctrls );
448 n_rs_ctrls = slapi_int_count_controls( rs->sr_ctrls );
450 slapi_pblock_set( pb, SLAPI_X_OLD_RESCONTROLS, (void *)rs->sr_ctrls );
452 if ( n_slapi_ctrls == 0 )
453 return LDAP_SUCCESS; /* no SLAPI controls */
455 ctrls = (LDAPControl **) op->o_tmpalloc(
456 ( n_slapi_ctrls + n_rs_ctrls + 1 ) * sizeof(LDAPControl *),
459 for ( i = 0; i < n_slapi_ctrls; i++ ) {
460 ctrls[i] = slapi_ctrls[i];
462 if ( rs->sr_ctrls != NULL ) {
463 for ( i = 0; i < n_rs_ctrls; i++ ) {
464 ctrls[n_slapi_ctrls + i] = rs->sr_ctrls[i];
467 ctrls[n_slapi_ctrls + n_rs_ctrls] = NULL;
469 rs->sr_ctrls = ctrls;
475 slapi_over_unmerge_controls( Operation *op, SlapReply *rs )
477 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
478 LDAPControl **rs_ctrls = NULL;
480 slapi_pblock_get( pb, SLAPI_X_OLD_RESCONTROLS, (void **)&rs_ctrls );
482 if ( rs_ctrls == NULL || rs->sr_ctrls == rs_ctrls ) {
483 /* no copying done */
487 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
488 rs->sr_ctrls = rs_ctrls;
494 slapi_over_response( Operation *op, SlapReply *rs )
496 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
497 int rc = SLAP_CB_CONTINUE;
499 if ( pb->pb_intop == 0 ) {
500 switch ( rs->sr_type ) {
504 rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
507 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
510 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
517 slapi_over_merge_controls( op, rs );
523 slapi_over_cleanup( Operation *op, SlapReply *rs )
525 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
526 int rc = SLAP_CB_CONTINUE;
528 slapi_over_unmerge_controls( op, rs );
530 if ( pb->pb_intop == 0 ) {
531 switch ( rs->sr_type ) {
535 rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
538 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
541 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
552 slapi_op_func( Operation *op, SlapReply *rs )
555 slap_operation_t which;
556 struct slapi_op_info *opinfo;
562 int preop_type, postop_type;
565 if ( !slapi_plugins_used )
566 return SLAP_CB_CONTINUE;
569 * Find the SLAPI operation information for this LDAP
570 * operation; this will contain the preop and postop
571 * plugin types, as well as optional callbacks for
572 * setting up the SLAPI environment.
574 which = slapi_tag2op( op->o_tag );
575 if ( which >= op_last ) {
576 /* invalid operation, but let someone else deal with it */
577 return SLAP_CB_CONTINUE;
580 opinfo = &slapi_op_dispatch_table[which];
581 if ( opinfo == NULL ) {
582 /* no SLAPI plugin types for this operation */
583 return SLAP_CB_CONTINUE;
586 internal_op = slapi_op_internal_p( op, rs, &cb );
589 preop_type = opinfo->soi_internal_preop;
590 postop_type = opinfo->soi_internal_postop;
592 preop_type = opinfo->soi_preop;
593 postop_type = opinfo->soi_postop;
596 if ( preop_type == 0 ) {
597 /* no SLAPI plugin types for this operation */
599 rc = SLAP_CB_CONTINUE;
603 pb = SLAPI_OPERATION_PBLOCK( op );
605 /* cache backend so we call correct postop plugins */
606 be = pb->pb_op->o_bd;
608 rc = slapi_int_call_plugins( be, preop_type, pb );
611 * soi_callback is responsible for examining the result code
612 * of the preoperation plugin and determining whether to
613 * abort. This is needed because of special SLAPI behaviour
614 e with bind preoperation plugins.
616 * The soi_callback function is also used to reset any values
617 * returned from the preoperation plugin before calling the
618 * backend (for the success case).
620 if ( opinfo->soi_callback == NULL ) {
621 /* default behaviour is preop plugin can abort operation */
627 rc = (opinfo->soi_callback)( op, rs, rc );
633 * Call actual backend (or next overlay in stack). We need to
634 * do this rather than returning SLAP_CB_CONTINUE and calling
635 * postoperation plugins in a response handler to match the
636 * behaviour of SLAPI in OpenLDAP 2.2, where postoperation
637 * plugins are called after the backend has completely
638 * finished processing the operation.
640 on = (slap_overinst *)op->o_bd->bd_info;
643 rc = overlay_op_walk( op, rs, which, oi, on->on_next );
646 * Call postoperation plugins
648 slapi_int_call_plugins( be, postop_type, pb );
651 if ( !internal_op ) {
652 slapi_pblock_destroy(pb);
653 cb.sc_private = NULL;
656 op->o_callback = cb.sc_next;
662 slapi_over_extended( Operation *op, SlapReply *rs )
670 slapi_int_get_extop_plugin( &op->ore_reqoid, &callback );
671 if ( callback == NULL ) {
672 return SLAP_CB_CONTINUE;
675 internal_op = slapi_op_internal_p( op, rs, &cb );
677 return SLAP_CB_CONTINUE;
680 pb = SLAPI_OPERATION_PBLOCK( op );
682 rc = (*callback)( pb );
683 if ( rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
685 } else if ( rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
686 rc = SLAP_CB_CONTINUE;
690 assert( rs->sr_rspoid != NULL );
692 send_ldap_extended( op, rs );
695 slapi_ch_free_string( (char **)&rs->sr_rspoid );
698 if ( rs->sr_rspdata != NULL )
699 ber_bvfree( rs->sr_rspdata );
704 slapi_pblock_destroy( pb );
705 op->o_callback = cb.sc_next;
711 slapi_over_access_allowed(
714 AttributeDescription *desc,
716 slap_access_t access,
717 AccessControlState *state,
724 SlapReply rs = { REP_RESULT };
726 internal_op = slapi_op_internal_p( op, &rs, &cb );
728 cb.sc_response = NULL;
729 cb.sc_cleanup = NULL;
731 pb = SLAPI_OPERATION_PBLOCK( op );
733 rc = slapi_int_access_allowed( op, e, desc, val, access, state );
735 rc = SLAP_CB_CONTINUE;
738 if ( !internal_op ) {
739 slapi_pblock_destroy( pb );
742 op->o_callback = cb.sc_next;
748 slapi_over_acl_group(
751 struct berval *gr_ndn,
752 struct berval *op_ndn,
753 ObjectClass *group_oc,
754 AttributeDescription *group_at )
759 BackendDB *be = op->o_bd;
761 SlapReply rs = { REP_RESULT };
763 op->o_bd = select_backend( gr_ndn, 0, 0 );
765 for ( g = op->o_groups; g; g = g->ga_next ) {
766 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
767 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
771 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
780 if ( target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
784 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
790 internal_op = slapi_op_internal_p( op, &rs, &cb );
792 cb.sc_response = NULL;
793 cb.sc_cleanup = NULL;
795 pb = SLAPI_OPERATION_PBLOCK( op );
797 slapi_pblock_set( pb, SLAPI_X_GROUP_ENTRY, (void *)e );
798 slapi_pblock_set( pb, SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
799 slapi_pblock_set( pb, SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
800 slapi_pblock_set( pb, SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
802 rc = slapi_over_call_plugins( pb, SLAPI_X_PLUGIN_PRE_GROUP_FN );
803 if ( rc >= 0 ) /* 1 means no plugins called */
804 rc = SLAP_CB_CONTINUE;
806 rc = pb->pb_rs->sr_err;
808 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ENTRY );
809 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_OPERATION_DN );
810 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ATTRIBUTE );
811 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_TARGET_ENTRY );
814 slapi_pblock_destroy( pb );
817 be_entry_release_r( op, e );
820 op->o_callback = cb.sc_next;
822 rc = LDAP_NO_SUCH_OBJECT; /* return SLAP_CB_CONTINUE for correctness? */
825 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache &&
826 rc != SLAP_CB_CONTINUE ) {
827 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
833 g->ga_len = gr_ndn->bv_len;
834 strcpy( g->ga_ndn, gr_ndn->bv_val );
835 g->ga_next = op->o_groups;
839 * XXX don't call POST_GROUP_FN, I have no idea what the point of
840 * that plugin function was anyway
848 slapi_over_db_open( BackendDB *be )
853 pb = slapi_pblock_new();
855 rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_START_FN, pb );
857 slapi_pblock_destroy( pb );
863 slapi_over_db_close( BackendDB *be )
868 pb = slapi_pblock_new();
870 rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_CLOSE_FN, pb );
872 slapi_pblock_destroy( pb );
880 memset( &slapi, 0, sizeof(slapi) );
882 slapi.on_bi.bi_type = SLAPI_OVERLAY_NAME;
884 slapi.on_bi.bi_op_bind = slapi_op_func;
885 slapi.on_bi.bi_op_unbind = slapi_op_func;
886 slapi.on_bi.bi_op_search = slapi_op_func;
887 slapi.on_bi.bi_op_compare = slapi_op_func;
888 slapi.on_bi.bi_op_modify = slapi_op_func;
889 slapi.on_bi.bi_op_modrdn = slapi_op_func;
890 slapi.on_bi.bi_op_add = slapi_op_func;
891 slapi.on_bi.bi_op_delete = slapi_op_func;
892 slapi.on_bi.bi_op_abandon = slapi_op_func;
893 slapi.on_bi.bi_op_cancel = slapi_op_func;
895 slapi.on_bi.bi_db_open = slapi_over_db_open;
896 slapi.on_bi.bi_db_close = slapi_over_db_close;
898 slapi.on_bi.bi_extended = slapi_over_extended;
899 slapi.on_bi.bi_access_allowed = slapi_over_access_allowed;
900 slapi.on_bi.bi_operational = slapi_over_aux_operational;
901 slapi.on_bi.bi_acl_group = slapi_over_acl_group;
903 return overlay_register( &slapi );
906 int slapi_over_is_inst( BackendDB *be )
908 return overlay_is_inst( be, SLAPI_OVERLAY_NAME );
911 int slapi_over_config( BackendDB *be )
913 if ( slapi_over_initialized == 0 ) {
916 /* do global initializaiton */
917 ldap_pvt_thread_mutex_init( &slapi_hn_mutex );
918 ldap_pvt_thread_mutex_init( &slapi_time_mutex );
919 ldap_pvt_thread_mutex_init( &slapi_printmessage_mutex );
921 if ( slapi_log_file == NULL )
922 slapi_log_file = slapi_ch_strdup( LDAP_RUNDIR LDAP_DIRSEP "errors" );
924 rc = slapi_int_init_object_extensions();
928 rc = slapi_over_init();
932 slapi_over_initialized = 1;
935 return overlay_config( be, SLAPI_OVERLAY_NAME );
938 #endif /* LDAP_SLAPI */