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;
102 if ( c == NULL || attribute == NULL || entry == NULL ) {
106 rs = (SlapReply *)c->cac_private;
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, rs, NULL ) ) {
149 return SLAP_CB_CONTINUE;
152 ctx.cac_pb = slapi_over_pblock_new( op, rs );
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 * We need this function to call frontendDB (global) plugins before
184 * database plugins, if we are invoked by a slap_callback.
187 slapi_over_call_plugins( Slapi_PBlock *pb, int type )
189 int rc = 1; /* means no plugins called */
192 PBLOCK_ASSERT_OP( pb, 0 );
195 if ( !be_match( op->o_bd, frontendDB ) ) {
196 rc = slapi_int_call_plugins( frontendDB, type, pb );
199 rc = slapi_int_call_plugins( op->o_bd, type, pb );
206 slapi_over_search( Operation *op, SlapReply *rs, int type )
211 assert( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF );
213 /* create a new pblock to not trample on result controls */
214 pb = slapi_over_pblock_new( op, rs );
216 rc = slapi_over_call_plugins( pb, type );
217 if ( rc >= 0 ) /* 1 means no plugins called */
218 rc = SLAP_CB_CONTINUE;
220 rc = LDAP_SUCCESS; /* confusing: don't abort, but don't send */
222 slapi_pblock_destroy(pb);
228 * Call pre- and post-result plugins
231 slapi_over_result( Operation *op, SlapReply *rs, int type )
233 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
235 assert( rs->sr_type == REP_RESULT || rs->sr_type == REP_SASL || rs->sr_type == REP_EXTENDED );
237 slapi_over_call_plugins( pb, type );
239 return SLAP_CB_CONTINUE;
244 slapi_op_bind_callback( Operation *op, SlapReply *rs, int prc )
247 case SLAPI_BIND_SUCCESS:
248 /* Continue with backend processing */
250 case SLAPI_BIND_FAIL:
251 /* Failure, frontend (that's us) sends result */
252 rs->sr_err = LDAP_INVALID_CREDENTIALS;
253 send_ldap_result( op, rs );
256 case SLAPI_BIND_ANONYMOUS: /* undocumented */
257 default: /* plugin sent result or no plugins called */
258 BER_BVZERO( &op->orb_edn );
260 if ( rs->sr_err == LDAP_SUCCESS ) {
262 * Plugin will have called slapi_pblock_set(LDAP_CONN_DN) which
263 * will have set conn->c_dn and conn->c_ndn
265 if ( BER_BVISNULL( &op->o_conn->c_ndn ) && prc == 1 ) {
266 /* No plugins were called; continue processing */
269 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
270 if ( !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
271 ber_len_t max = sockbuf_max_incoming_auth;
272 ber_sockbuf_ctrl( op->o_conn->c_sb,
273 LBER_SB_OPT_SET_MAX_INCOMING, &max );
275 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
277 /* log authorization identity */
278 Statslog( LDAP_DEBUG_STATS,
279 "%s BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
281 BER_BVISNULL( &op->o_conn->c_dn )
282 ? "<empty>" : op->o_conn->c_dn.bv_val,
283 BER_BVISNULL( &op->orb_tmp_mech )
284 ? "<empty>" : op->orb_tmp_mech.bv_val, 0, 0 );
295 slapi_op_search_callback( Operation *op, SlapReply *rs, int prc )
297 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
299 /* check preoperation result code */
304 rs->sr_err = LDAP_SUCCESS;
306 if ( slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb ) == 0 ) {
308 * The plugin can set the SLAPI_SEARCH_FILTER.
309 * SLAPI_SEARCH_STRFILER is not normative.
311 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
312 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
318 struct slapi_op_info {
319 int soi_preop; /* preoperation plugin parameter */
320 int soi_postop; /* postoperation plugin parameter */
321 int soi_internal_preop; /* internal preoperation plugin parameter */
322 int soi_internal_postop; /* internal postoperation plugin parameter */
323 int (*soi_callback)(Operation *, SlapReply *, int); /* preoperation result handler */
324 } slapi_op_dispatch_table[] = {
326 SLAPI_PLUGIN_PRE_BIND_FN,
327 SLAPI_PLUGIN_POST_BIND_FN,
330 slapi_op_bind_callback
333 SLAPI_PLUGIN_PRE_UNBIND_FN,
334 SLAPI_PLUGIN_POST_UNBIND_FN,
340 SLAPI_PLUGIN_PRE_SEARCH_FN,
341 SLAPI_PLUGIN_POST_SEARCH_FN,
344 slapi_op_search_callback
347 SLAPI_PLUGIN_PRE_COMPARE_FN,
348 SLAPI_PLUGIN_POST_COMPARE_FN,
354 SLAPI_PLUGIN_PRE_MODIFY_FN,
355 SLAPI_PLUGIN_POST_MODIFY_FN,
356 SLAPI_PLUGIN_INTERNAL_PRE_MODIFY_FN,
357 SLAPI_PLUGIN_INTERNAL_POST_MODIFY_FN,
361 SLAPI_PLUGIN_PRE_MODRDN_FN,
362 SLAPI_PLUGIN_POST_MODRDN_FN,
363 SLAPI_PLUGIN_INTERNAL_PRE_MODRDN_FN,
364 SLAPI_PLUGIN_INTERNAL_POST_MODRDN_FN,
368 SLAPI_PLUGIN_PRE_ADD_FN,
369 SLAPI_PLUGIN_POST_ADD_FN,
370 SLAPI_PLUGIN_INTERNAL_PRE_ADD_FN,
371 SLAPI_PLUGIN_INTERNAL_POST_ADD_FN,
375 SLAPI_PLUGIN_PRE_DELETE_FN,
376 SLAPI_PLUGIN_POST_DELETE_FN,
377 SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN,
378 SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN,
382 SLAPI_PLUGIN_PRE_ABANDON_FN,
383 SLAPI_PLUGIN_POST_ABANDON_FN,
398 slapi_tag2op( ber_tag_t tag )
409 case LDAP_REQ_DELETE:
412 case LDAP_REQ_MODRDN:
415 case LDAP_REQ_MODIFY:
418 case LDAP_REQ_COMPARE:
421 case LDAP_REQ_SEARCH:
424 case LDAP_REQ_UNBIND:
435 /* Add SLAPI_RESCONTROLS to rs->sr_ctrls, with care, because
436 * rs->sr_ctrls could be allocated on the stack */
438 slapi_over_merge_controls( Operation *op, SlapReply *rs )
440 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
441 LDAPControl **ctrls = NULL;
442 LDAPControl **slapi_ctrls = NULL;
443 size_t n_slapi_ctrls = 0;
444 size_t n_rs_ctrls = 0;
447 slapi_pblock_get( pb, SLAPI_RESCONTROLS, (void **)&slapi_ctrls );
449 n_slapi_ctrls = slapi_int_count_controls( slapi_ctrls );
450 n_rs_ctrls = slapi_int_count_controls( rs->sr_ctrls );
452 slapi_pblock_set( pb, SLAPI_X_OLD_RESCONTROLS, (void *)rs->sr_ctrls );
454 if ( n_slapi_ctrls == 0 )
455 return LDAP_SUCCESS; /* no SLAPI controls */
457 ctrls = (LDAPControl **) op->o_tmpalloc(
458 ( n_slapi_ctrls + n_rs_ctrls + 1 ) * sizeof(LDAPControl *),
461 for ( i = 0; i < n_slapi_ctrls; i++ ) {
462 ctrls[i] = slapi_ctrls[i];
464 if ( rs->sr_ctrls != NULL ) {
465 for ( i = 0; i < n_rs_ctrls; i++ ) {
466 ctrls[n_slapi_ctrls + i] = rs->sr_ctrls[i];
469 ctrls[n_slapi_ctrls + n_rs_ctrls] = NULL;
471 rs->sr_ctrls = ctrls;
477 slapi_over_unmerge_controls( Operation *op, SlapReply *rs )
479 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
480 LDAPControl **rs_ctrls = NULL;
482 slapi_pblock_get( pb, SLAPI_X_OLD_RESCONTROLS, (void **)&rs_ctrls );
484 if ( rs_ctrls == NULL || rs->sr_ctrls == rs_ctrls ) {
485 /* no copying done */
489 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
490 rs->sr_ctrls = rs_ctrls;
496 slapi_over_response( Operation *op, SlapReply *rs )
498 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
499 int rc = SLAP_CB_CONTINUE;
501 if ( pb->pb_intop == 0 ) {
502 switch ( rs->sr_type ) {
506 rc = slapi_over_result( op, rs, SLAPI_PLUGIN_PRE_RESULT_FN );
509 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_ENTRY_FN );
512 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_PRE_REFERRAL_FN );
519 slapi_over_merge_controls( op, rs );
525 slapi_over_cleanup( Operation *op, SlapReply *rs )
527 Slapi_PBlock *pb = SLAPI_OPERATION_PBLOCK( op );
528 int rc = SLAP_CB_CONTINUE;
530 slapi_over_unmerge_controls( op, rs );
532 if ( pb->pb_intop == 0 ) {
533 switch ( rs->sr_type ) {
537 rc = slapi_over_result( op, rs, SLAPI_PLUGIN_POST_RESULT_FN );
540 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_ENTRY_FN );
543 rc = slapi_over_search( op, rs, SLAPI_PLUGIN_POST_REFERRAL_FN );
554 slapi_op_func( Operation *op, SlapReply *rs )
557 slap_operation_t which;
558 struct slapi_op_info *opinfo;
564 int preop_type, postop_type;
567 if ( !slapi_plugins_used )
568 return SLAP_CB_CONTINUE;
571 * Find the SLAPI operation information for this LDAP
572 * operation; this will contain the preop and postop
573 * plugin types, as well as optional callbacks for
574 * setting up the SLAPI environment.
576 which = slapi_tag2op( op->o_tag );
577 if ( which >= op_last ) {
578 /* invalid operation, but let someone else deal with it */
579 return SLAP_CB_CONTINUE;
582 opinfo = &slapi_op_dispatch_table[which];
583 if ( opinfo == NULL ) {
584 /* no SLAPI plugin types for this operation */
585 return SLAP_CB_CONTINUE;
588 internal_op = slapi_op_internal_p( op, rs, &cb );
591 preop_type = opinfo->soi_internal_preop;
592 postop_type = opinfo->soi_internal_postop;
594 preop_type = opinfo->soi_preop;
595 postop_type = opinfo->soi_postop;
598 if ( preop_type == 0 ) {
599 /* no SLAPI plugin types for this operation */
601 rc = SLAP_CB_CONTINUE;
605 pb = SLAPI_OPERATION_PBLOCK( op );
607 /* cache backend so we call correct postop plugins */
608 be = pb->pb_op->o_bd;
610 rc = slapi_int_call_plugins( be, preop_type, pb );
613 * soi_callback is responsible for examining the result code
614 * of the preoperation plugin and determining whether to
615 * abort. This is needed because of special SLAPI behaviour
616 e with bind preoperation plugins.
618 * The soi_callback function is also used to reset any values
619 * returned from the preoperation plugin before calling the
620 * backend (for the success case).
622 if ( opinfo->soi_callback == NULL ) {
623 /* default behaviour is preop plugin can abort operation */
629 rc = (opinfo->soi_callback)( op, rs, rc );
635 * Call actual backend (or next overlay in stack). We need to
636 * do this rather than returning SLAP_CB_CONTINUE and calling
637 * postoperation plugins in a response handler to match the
638 * behaviour of SLAPI in OpenLDAP 2.2, where postoperation
639 * plugins are called after the backend has completely
640 * finished processing the operation.
642 on = (slap_overinst *)op->o_bd->bd_info;
645 rc = overlay_op_walk( op, rs, which, oi, on->on_next );
648 * Call postoperation plugins
650 slapi_int_call_plugins( be, postop_type, pb );
653 if ( !internal_op ) {
654 slapi_pblock_destroy(pb);
655 cb.sc_private = NULL;
658 op->o_callback = cb.sc_next;
664 slapi_over_extended( Operation *op, SlapReply *rs )
672 slapi_int_get_extop_plugin( &op->ore_reqoid, &callback );
673 if ( callback == NULL ) {
674 return SLAP_CB_CONTINUE;
677 internal_op = slapi_op_internal_p( op, rs, &cb );
679 return SLAP_CB_CONTINUE;
682 pb = SLAPI_OPERATION_PBLOCK( op );
684 rc = (*callback)( pb );
685 if ( rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
687 } else if ( rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
688 rc = SLAP_CB_CONTINUE;
692 assert( rs->sr_rspoid != NULL );
694 send_ldap_extended( op, rs );
697 slapi_ch_free_string( (char **)&rs->sr_rspoid );
700 if ( rs->sr_rspdata != NULL )
701 ber_bvfree( rs->sr_rspdata );
706 slapi_pblock_destroy( pb );
707 op->o_callback = cb.sc_next;
713 slapi_over_access_allowed(
716 AttributeDescription *desc,
718 slap_access_t access,
719 AccessControlState *state,
726 SlapReply rs = { REP_RESULT };
728 internal_op = slapi_op_internal_p( op, &rs, &cb );
730 cb.sc_response = NULL;
731 cb.sc_cleanup = NULL;
733 pb = SLAPI_OPERATION_PBLOCK( op );
735 rc = slapi_int_access_allowed( op, e, desc, val, access, state );
737 rc = SLAP_CB_CONTINUE;
740 if ( !internal_op ) {
741 slapi_pblock_destroy( pb );
744 op->o_callback = cb.sc_next;
750 slapi_over_acl_group(
753 struct berval *gr_ndn,
754 struct berval *op_ndn,
755 ObjectClass *group_oc,
756 AttributeDescription *group_at )
761 BackendDB *be = op->o_bd;
763 SlapReply rs = { REP_RESULT };
765 op->o_bd = select_backend( gr_ndn, 0, 0 );
767 for ( g = op->o_groups; g; g = g->ga_next ) {
768 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
769 g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
773 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
782 if ( target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
786 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
792 internal_op = slapi_op_internal_p( op, &rs, &cb );
794 cb.sc_response = NULL;
795 cb.sc_cleanup = NULL;
797 pb = SLAPI_OPERATION_PBLOCK( op );
799 slapi_pblock_set( pb, SLAPI_X_GROUP_ENTRY, (void *)e );
800 slapi_pblock_set( pb, SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
801 slapi_pblock_set( pb, SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
802 slapi_pblock_set( pb, SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
804 rc = slapi_over_call_plugins( pb, SLAPI_X_PLUGIN_PRE_GROUP_FN );
805 if ( rc >= 0 ) /* 1 means no plugins called */
806 rc = SLAP_CB_CONTINUE;
808 rc = pb->pb_rs->sr_err;
810 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ENTRY );
811 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_OPERATION_DN );
812 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_ATTRIBUTE );
813 slapi_pblock_delete_param( pb, SLAPI_X_GROUP_TARGET_ENTRY );
816 slapi_pblock_destroy( pb );
819 be_entry_release_r( op, e );
822 op->o_callback = cb.sc_next;
824 rc = LDAP_NO_SUCH_OBJECT; /* return SLAP_CB_CONTINUE for correctness? */
827 if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache &&
828 rc != SLAP_CB_CONTINUE ) {
829 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
835 g->ga_len = gr_ndn->bv_len;
836 strcpy( g->ga_ndn, gr_ndn->bv_val );
837 g->ga_next = op->o_groups;
841 * XXX don't call POST_GROUP_FN, I have no idea what the point of
842 * that plugin function was anyway
850 slapi_over_db_open( BackendDB *be )
855 pb = slapi_pblock_new();
857 rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_START_FN, pb );
859 slapi_pblock_destroy( pb );
865 slapi_over_db_close( BackendDB *be )
870 pb = slapi_pblock_new();
872 rc = slapi_int_call_plugins( be, SLAPI_PLUGIN_CLOSE_FN, pb );
874 slapi_pblock_destroy( pb );
882 memset( &slapi, 0, sizeof(slapi) );
884 slapi.on_bi.bi_type = SLAPI_OVERLAY_NAME;
886 slapi.on_bi.bi_op_bind = slapi_op_func;
887 slapi.on_bi.bi_op_unbind = slapi_op_func;
888 slapi.on_bi.bi_op_search = slapi_op_func;
889 slapi.on_bi.bi_op_compare = slapi_op_func;
890 slapi.on_bi.bi_op_modify = slapi_op_func;
891 slapi.on_bi.bi_op_modrdn = slapi_op_func;
892 slapi.on_bi.bi_op_add = slapi_op_func;
893 slapi.on_bi.bi_op_delete = slapi_op_func;
894 slapi.on_bi.bi_op_abandon = slapi_op_func;
895 slapi.on_bi.bi_op_cancel = slapi_op_func;
897 slapi.on_bi.bi_db_open = slapi_over_db_open;
898 slapi.on_bi.bi_db_close = slapi_over_db_close;
900 slapi.on_bi.bi_extended = slapi_over_extended;
901 slapi.on_bi.bi_access_allowed = slapi_over_access_allowed;
902 slapi.on_bi.bi_operational = slapi_over_aux_operational;
903 slapi.on_bi.bi_acl_group = slapi_over_acl_group;
905 return overlay_register( &slapi );
908 int slapi_over_is_inst( BackendDB *be )
910 return overlay_is_inst( be, SLAPI_OVERLAY_NAME );
913 int slapi_over_config( BackendDB *be )
915 if ( slapi_over_initialized == 0 ) {
918 /* do global initializaiton */
919 ldap_pvt_thread_mutex_init( &slapi_hn_mutex );
920 ldap_pvt_thread_mutex_init( &slapi_time_mutex );
921 ldap_pvt_thread_mutex_init( &slapi_printmessage_mutex );
923 if ( slapi_log_file == NULL )
924 slapi_log_file = slapi_ch_strdup( LDAP_RUNDIR LDAP_DIRSEP "errors" );
926 rc = slapi_int_init_object_extensions();
930 rc = slapi_over_init();
934 slapi_over_initialized = 1;
937 return overlay_config( be, SLAPI_OVERLAY_NAME );
940 #endif /* LDAP_SLAPI */