1 /* backover.c - backend overlay routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-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 /* Functions to overlay other modules over a backend. */
23 #include <ac/string.h>
24 #include <ac/socket.h>
30 static slap_overinst *overlays;
45 slap_overinfo *oi = be->bd_info->bi_private;
46 slap_overinst *on = oi->oi_list;
47 BackendInfo *bi_orig = be->bd_info;
51 func = &oi->oi_orig->bi_db_open;
53 be->bd_info = oi->oi_orig;
54 rc = func[which]( be );
57 for (; on && rc == 0; on=on->on_next) {
58 be->bd_info = &on->on_bi;
59 func = &on->on_bi.bi_db_open;
61 rc = func[which]( be );
64 be->bd_info = bi_orig;
77 slap_overinfo *oi = be->bd_info->bi_private;
78 slap_overinst *on = oi->oi_list;
79 BackendInfo *bi_orig = be->bd_info;
80 struct ConfigOCs *be_cf_ocs = be->be_cf_ocs;
84 if ( oi->oi_orig->bi_db_config ) {
85 be->bd_info = oi->oi_orig;
86 be->be_cf_ocs = oi->oi_orig->bi_cf_ocs;
87 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
90 if ( be->bd_info != oi->oi_orig ) {
92 slap_overinst *on2, **onp;
96 /* a database added an overlay;
97 * work it around... */
98 assert( overlay_is_over( be ) );
100 oi2 = ( slap_overinfo * )be->bd_info->bi_private;
103 /* need to put a uniqueness check here as well;
104 * note that in principle there could be more than
105 * one overlay as a result of multiple calls to
106 * overlay_config() */
107 be2.bd_info = (BackendInfo *)oi;
109 for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
110 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
111 Debug( LDAP_DEBUG_ANY, "over_db_config(): "
112 "warning, freshly added "
113 "overlay #%d \"%s\" is already in list\n",
114 i, (*onp)->on_bi.bi_type, 0 );
116 /* NOTE: if the overlay already exists,
117 * there is no way to merge the results
118 * of the configuration that may have
119 * occurred during bi_db_config(); we
120 * just issue a warning, and the
121 * administrator should deal with this */
128 ch_free( be->bd_info );
131 be->bd_info = (BackendInfo *)oi;
132 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
140 for (; on; on=on->on_next) {
141 rc = SLAP_CONF_UNKNOWN;
142 if (on->on_bi.bi_cf_ocs) {
145 ct = config_find_keyword( on->on_bi.bi_cf_ocs->co_table, &ca );
147 rc = config_add_vals( ct, &ca );
150 if (on->on_bi.bi_db_config && rc == SLAP_CONF_UNKNOWN) {
151 be->bd_info = &on->on_bi;
152 rc = on->on_bi.bi_db_config( be, fname, lineno,
154 if ( rc != SLAP_CONF_UNKNOWN ) break;
157 be->bd_info = bi_orig;
158 be->be_cf_ocs = be_cf_ocs;
168 return over_db_func( be, db_open );
176 slap_overinfo *oi = be->bd_info->bi_private;
177 slap_overinst *on = oi->oi_list;
178 BackendInfo *bi_orig = be->bd_info;
181 for (; on && rc == 0; on=on->on_next) {
182 be->bd_info = &on->on_bi;
183 if ( be->bd_info->bi_db_close ) {
184 rc = be->bd_info->bi_db_close( be );
188 if ( oi->oi_orig->bi_db_close ) {
189 be->bd_info = oi->oi_orig;
190 rc = be->bd_info->bi_db_close( be );
193 be->bd_info = bi_orig;
202 slap_overinfo *oi = be->bd_info->bi_private;
203 slap_overinst *on = oi->oi_list, *next;
206 rc = over_db_func( be, db_destroy );
208 for (next = on->on_next; on; on=next) {
217 over_back_response ( Operation *op, SlapReply *rs )
219 slap_overinfo *oi = op->o_callback->sc_private;
220 slap_overinst *on = oi->oi_list;
221 int rc = SLAP_CB_CONTINUE;
222 BackendDB *be = op->o_bd, db = *op->o_bd;
224 db.be_flags |= SLAP_DBFLAG_OVERLAY;
226 for (; on; on=on->on_next ) {
227 if ( on->on_response ) {
228 db.bd_info = (BackendInfo *)on;
229 rc = on->on_response( op, rs );
230 if ( rc != SLAP_CB_CONTINUE ) break;
237 #ifdef SLAP_OVERLAY_ACCESS
242 AttributeDescription *desc,
244 slap_access_t access,
245 AccessControlState *state,
250 BackendInfo *bi = op->o_bd->bd_info;
251 BackendDB *be = op->o_bd, db;
252 int rc = SLAP_CB_CONTINUE;
254 /* FIXME: used to happen for instance during abandon
255 * when global overlays are used... */
256 assert( op->o_bd != NULL );
258 oi = op->o_bd->bd_info->bi_private;
261 for ( ; on; on = on->on_next ) {
262 if ( on->on_bi.bi_access_allowed ) {
263 /* NOTE: do not copy the structure until required */
264 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
266 db.be_flags |= SLAP_DBFLAG_OVERLAY;
270 op->o_bd->bd_info = (BackendInfo *)on;
271 rc = on->on_bi.bi_access_allowed( op, e,
272 desc, val, access, state, maskp );
273 if ( rc != SLAP_CB_CONTINUE ) break;
277 if ( rc == SLAP_CB_CONTINUE ) {
278 BI_access_allowed *bi_access_allowed;
280 /* if the database structure was changed, o_bd points to a
281 * copy of the structure; put the original bd_info in place */
282 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
283 op->o_bd->bd_info = oi->oi_orig;
286 if ( oi->oi_orig->bi_access_allowed ) {
287 bi_access_allowed = oi->oi_orig->bi_access_allowed;
289 bi_access_allowed = slap_access_allowed;
292 rc = bi_access_allowed( op, e,
293 desc, val, access, state, maskp );
295 /* should not fall thru this far without anything happening... */
296 if ( rc == SLAP_CB_CONTINUE ) {
297 /* access not allowed */
302 op->o_bd->bd_info = bi;
306 #endif /* SLAP_OVERLAY_ACCESS */
309 * default return code in case of missing backend function
310 * and overlay stack returning SLAP_CB_CONTINUE
312 static int op_rc[ op_last ] = {
313 LDAP_UNWILLING_TO_PERFORM, /* bind */
314 LDAP_UNWILLING_TO_PERFORM, /* unbind */
315 LDAP_UNWILLING_TO_PERFORM, /* search */
316 SLAP_CB_CONTINUE, /* compare; pass to frontend */
317 LDAP_UNWILLING_TO_PERFORM, /* modify */
318 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
319 LDAP_UNWILLING_TO_PERFORM, /* add */
320 LDAP_UNWILLING_TO_PERFORM, /* delete */
321 LDAP_UNWILLING_TO_PERFORM, /* abandon */
322 LDAP_UNWILLING_TO_PERFORM, /* cancel */
323 LDAP_UNWILLING_TO_PERFORM, /* extended */
324 LDAP_SUCCESS, /* aux_operational */
325 LDAP_SUCCESS, /* aux_chk_referrals */
326 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
332 slap_operation_t which,
338 int rc = SLAP_CB_CONTINUE;
340 for (; on; on=on->on_next ) {
341 func = &on->on_bi.bi_op_bind;
343 op->o_bd->bd_info = (BackendInfo *)on;
344 rc = func[which]( op, rs );
345 if ( rc != SLAP_CB_CONTINUE ) break;
349 func = &oi->oi_orig->bi_op_bind;
350 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
351 op->o_bd->bd_info = oi->oi_orig;
352 rc = func[which]( op, rs );
354 /* should not fall thru this far without anything happening... */
355 if ( rc == SLAP_CB_CONTINUE ) {
359 /* The underlying backend didn't handle the request, make sure
360 * overlay cleanup is processed.
362 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
363 slap_callback *sc_next;
364 for ( ; op->o_callback && op->o_callback->sc_response !=
365 over_back_response; op->o_callback = sc_next ) {
366 sc_next = op->o_callback->sc_next;
367 if ( op->o_callback->sc_cleanup ) {
368 op->o_callback->sc_cleanup( op, rs );
379 slap_operation_t which
385 BackendDB *be = op->o_bd, db;
386 slap_callback cb = {NULL, over_back_response, NULL, NULL};
387 int rc = SLAP_CB_CONTINUE;
389 /* FIXME: used to happen for instance during abandon
390 * when global overlays are used... */
391 assert( op->o_bd != NULL );
393 oi = op->o_bd->bd_info->bi_private;
396 if ( !SLAP_ISOVERLAY( op->o_bd )) {
398 db.be_flags |= SLAP_DBFLAG_OVERLAY;
401 cb.sc_next = op->o_callback;
403 op->o_callback = &cb;
405 rc = overlay_op_walk( op, rs, which, oi, on );
408 op->o_callback = cb.sc_next;
413 over_op_bind( Operation *op, SlapReply *rs )
415 return over_op_func( op, rs, op_bind );
419 over_op_unbind( Operation *op, SlapReply *rs )
421 return over_op_func( op, rs, op_unbind );
425 over_op_search( Operation *op, SlapReply *rs )
427 return over_op_func( op, rs, op_search );
431 over_op_compare( Operation *op, SlapReply *rs )
433 return over_op_func( op, rs, op_compare );
437 over_op_modify( Operation *op, SlapReply *rs )
439 return over_op_func( op, rs, op_modify );
443 over_op_modrdn( Operation *op, SlapReply *rs )
445 return over_op_func( op, rs, op_modrdn );
449 over_op_add( Operation *op, SlapReply *rs )
451 return over_op_func( op, rs, op_add );
455 over_op_delete( Operation *op, SlapReply *rs )
457 return over_op_func( op, rs, op_delete );
461 over_op_abandon( Operation *op, SlapReply *rs )
463 return over_op_func( op, rs, op_abandon );
467 over_op_cancel( Operation *op, SlapReply *rs )
469 return over_op_func( op, rs, op_cancel );
473 over_op_extended( Operation *op, SlapReply *rs )
475 return over_op_func( op, rs, op_extended );
479 over_aux_operational( Operation *op, SlapReply *rs )
481 return over_op_func( op, rs, op_aux_operational );
485 over_aux_chk_referrals( Operation *op, SlapReply *rs )
487 return over_op_func( op, rs, op_aux_chk_referrals );
491 over_aux_chk_controls( Operation *op, SlapReply *rs )
493 return over_op_func( op, rs, op_aux_chk_controls );
503 over_connection_func(
506 enum conn_which which
512 int rc = SLAP_CB_CONTINUE;
513 BI_connection_init **func;
515 /* FIXME: used to happen for instance during abandon
516 * when global overlays are used... */
517 assert( bd != NULL );
519 oi = bd->bd_info->bi_private;
522 if ( !SLAP_ISOVERLAY( bd ) ) {
524 db.be_flags |= SLAP_DBFLAG_OVERLAY;
528 for ( ; on; on = on->on_next ) {
529 func = &on->on_bi.bi_connection_init;
530 if ( func[ which ] ) {
531 bd->bd_info = (BackendInfo *)on;
532 rc = func[ which ]( bd, conn );
533 if ( rc != SLAP_CB_CONTINUE ) break;
537 func = &oi->oi_orig->bi_connection_init;
538 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
539 bd->bd_info = oi->oi_orig;
540 rc = func[ which ]( bd, conn );
542 /* should not fall thru this far without anything happening... */
543 if ( rc == SLAP_CB_CONTINUE ) {
544 rc = LDAP_UNWILLING_TO_PERFORM;
551 over_connection_init(
556 return over_connection_func( bd, conn, conn_init );
560 over_connection_destroy(
565 return over_connection_func( bd, conn, conn_destroy );
573 on->on_next = overlays;
579 * iterator on registered overlays; overlay_next( NULL ) returns the first
580 * overlay; * subsequent calls with the previously returned value allow to
581 * iterate * over the entire list; returns NULL when no more overlays are
598 * returns a specific registered overlay based on the type; NULL if not
603 overlay_find( const char *over_type )
605 slap_overinst *on = overlays;
607 assert( over_type != NULL );
609 for ( ; on; on = on->on_next ) {
610 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
618 static const char overtype[] = "over";
621 * returns TRUE (1) if the database is actually an overlay instance;
622 * FALSE (0) otherwise.
626 overlay_is_over( BackendDB *be )
628 return be->bd_info->bi_type == overtype;
632 * returns TRUE (1) if the given database is actually an overlay
633 * instance and, somewhere in the list, contains the requested overlay;
634 * FALSE (0) otherwise.
638 overlay_is_inst( BackendDB *be, const char *over_type )
642 assert( be != NULL );
644 if ( !overlay_is_over( be ) ) {
648 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
649 for ( ; on; on = on->on_next ) {
650 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
659 overlay_register_control( BackendDB *be, const char *oid )
665 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
669 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
672 /* add to all backends... */
673 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
678 bd->be_ctrls[ cid ] = 1;
679 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
684 if ( rc == 0 && !gotit ) {
685 be->be_ctrls[ cid ] = 1;
686 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
693 overlay_destroy_one( BackendDB *be, slap_overinst *on )
695 slap_overinfo *oi = on->on_info;
696 slap_overinst **oidx;
698 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
701 if ( on->on_bi.bi_db_destroy ) {
702 BackendInfo *bi_orig = be->bd_info;
703 be->bd_info = (BackendInfo *)on;
704 on->on_bi.bi_db_destroy( be );
705 be->bd_info = bi_orig;
713 /* add an overlay to a particular backend. */
715 overlay_config( BackendDB *be, const char *ov )
717 slap_overinst *on = NULL, *on2 = NULL;
718 slap_overinfo *oi = NULL;
719 BackendInfo *bi = NULL;
721 on = overlay_find( ov );
723 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
727 /* If this is the first overlay on this backend, set up the
728 * overlay info structure
730 if ( !overlay_is_over( be ) ) {
731 oi = ch_malloc( sizeof( slap_overinfo ) );
732 oi->oi_orig = be->bd_info;
733 oi->oi_bi = *be->bd_info;
735 /* NOTE: the first time a global overlay is configured,
736 * frontendDB gets this flag; it is used later by overlays
737 * to determine if they're stacked on top of the frontendDB */
738 if ( oi->oi_orig == frontendDB->bd_info ) {
739 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
742 /* Save a pointer to ourself in bi_private.
744 oi->oi_bi.bi_private = oi;
746 bi = (BackendInfo *)oi;
748 bi->bi_type = (char *)overtype;
750 bi->bi_db_config = over_db_config;
751 bi->bi_db_open = over_db_open;
752 bi->bi_db_close = over_db_close;
753 bi->bi_db_destroy = over_db_destroy;
755 bi->bi_op_bind = over_op_bind;
756 bi->bi_op_unbind = over_op_unbind;
757 bi->bi_op_search = over_op_search;
758 bi->bi_op_compare = over_op_compare;
759 bi->bi_op_modify = over_op_modify;
760 bi->bi_op_modrdn = over_op_modrdn;
761 bi->bi_op_add = over_op_add;
762 bi->bi_op_delete = over_op_delete;
763 bi->bi_op_abandon = over_op_abandon;
764 bi->bi_op_cancel = over_op_cancel;
766 bi->bi_extended = over_op_extended;
769 * this is fine because it has the same
770 * args of the operations; we need to rework
771 * all the hooks to share the same args
772 * of the operations...
774 bi->bi_operational = over_aux_operational;
775 bi->bi_chk_referrals = over_aux_chk_referrals;
776 bi->bi_chk_controls = over_aux_chk_controls;
778 #ifdef SLAP_OVERLAY_ACCESS
779 /* this has a specific arglist */
780 bi->bi_access_allowed = over_access_allowed;
781 #endif /* SLAP_OVERLAY_ACCESS */
783 bi->bi_connection_init = over_connection_init;
784 bi->bi_connection_destroy = over_connection_destroy;
789 if ( overlay_is_inst( be, ov ) ) {
790 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
791 "warning, overlay \"%s\" "
792 "already in list\n", ov, 0, 0 );
795 oi = be->bd_info->bi_private;
798 /* Insert new overlay on head of list. Overlays are executed
799 * in reverse of config order...
801 on2 = ch_calloc( 1, sizeof(slap_overinst) );
804 on2->on_next = oi->oi_list;
807 /* Any initialization needed? */
808 if ( on->on_bi.bi_db_init ) {
810 be->bd_info = (BackendInfo *)on2;
811 rc = on2->on_bi.bi_db_init( be );
812 be->bd_info = (BackendInfo *)oi;