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;
32 enum db_which { db_open = 0, db_close, db_destroy };
40 slap_overinfo *oi = be->bd_info->bi_private;
41 slap_overinst *on = oi->oi_list;
42 BackendInfo *bi_orig = be->bd_info;
46 func = &oi->oi_orig->bi_db_open;
48 be->bd_info = oi->oi_orig;
49 rc = func[which]( be );
52 for (; on && rc == 0; on=on->on_next) {
53 be->bd_info = &on->on_bi;
54 func = &on->on_bi.bi_db_open;
56 rc = func[which]( be );
59 be->bd_info = bi_orig;
72 slap_overinfo *oi = be->bd_info->bi_private;
73 slap_overinst *on = oi->oi_list;
74 BackendInfo *bi_orig = be->bd_info;
75 struct ConfigOCs *be_cf_ocs = be->be_cf_ocs;
79 if ( oi->oi_orig->bi_db_config ) {
80 be->bd_info = oi->oi_orig;
81 be->be_cf_ocs = oi->oi_orig->bi_cf_ocs;
82 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
85 if ( be->bd_info != oi->oi_orig ) {
87 slap_overinst *on2, **onp;
91 /* a database added an overlay;
92 * work it around... */
93 assert( overlay_is_over( be ) );
95 oi2 = ( slap_overinfo * )be->bd_info->bi_private;
98 /* need to put a uniqueness check here as well;
99 * note that in principle there could be more than
100 * one overlay as a result of multiple calls to
101 * overlay_config() */
102 be2.bd_info = (BackendInfo *)oi;
104 for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
105 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
106 Debug( LDAP_DEBUG_ANY, "over_db_config(): "
107 "warning, freshly added "
108 "overlay #%d \"%s\" is already in list\n",
109 i, (*onp)->on_bi.bi_type, 0 );
111 /* NOTE: if the overlay already exists,
112 * there is no way to merge the results
113 * of the configuration that may have
114 * occurred during bi_db_config(); we
115 * just issue a warning, and the
116 * administrator should deal with this */
123 ch_free( be->bd_info );
126 be->bd_info = (BackendInfo *)oi;
127 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
135 for (; on; on=on->on_next) {
136 rc = SLAP_CONF_UNKNOWN;
137 if (on->on_bi.bi_cf_ocs) {
140 ct = config_find_keyword( on->on_bi.bi_cf_ocs->co_table, &ca );
142 rc = config_add_vals( ct, &ca );
145 if (on->on_bi.bi_db_config && rc == SLAP_CONF_UNKNOWN) {
146 be->bd_info = &on->on_bi;
147 rc = on->on_bi.bi_db_config( be, fname, lineno,
149 if ( rc != SLAP_CONF_UNKNOWN ) break;
152 be->bd_info = bi_orig;
153 be->be_cf_ocs = be_cf_ocs;
163 return over_db_func( be, db_open );
171 slap_overinfo *oi = be->bd_info->bi_private;
172 slap_overinst *on = oi->oi_list;
173 BackendInfo *bi_orig = be->bd_info;
176 for (; on && rc == 0; on=on->on_next) {
177 be->bd_info = &on->on_bi;
178 if ( be->bd_info->bi_db_close ) {
179 rc = be->bd_info->bi_db_close( be );
183 if ( oi->oi_orig->bi_db_close ) {
184 be->bd_info = oi->oi_orig;
185 rc = be->bd_info->bi_db_close( be );
188 be->bd_info = bi_orig;
197 slap_overinfo *oi = be->bd_info->bi_private;
198 slap_overinst *on = oi->oi_list, *next;
201 rc = over_db_func( be, db_destroy );
203 for (next = on->on_next; on; on=next) {
212 over_back_response ( Operation *op, SlapReply *rs )
214 slap_overinfo *oi = op->o_callback->sc_private;
215 slap_overinst *on = oi->oi_list;
216 int rc = SLAP_CB_CONTINUE;
217 BackendDB *be = op->o_bd, db = *op->o_bd;
219 db.be_flags |= SLAP_DBFLAG_OVERLAY;
221 for (; on; on=on->on_next ) {
222 if ( on->on_response ) {
223 db.bd_info = (BackendInfo *)on;
224 rc = on->on_response( op, rs );
225 if ( rc != SLAP_CB_CONTINUE ) break;
232 #ifdef SLAP_OVERLAY_ACCESS
237 AttributeDescription *desc,
239 slap_access_t access,
240 AccessControlState *state,
245 BackendInfo *bi = op->o_bd->bd_info;
246 BackendDB *be = op->o_bd, db;
247 int rc = SLAP_CB_CONTINUE;
249 /* FIXME: used to happen for instance during abandon
250 * when global overlays are used... */
251 assert( op->o_bd != NULL );
253 oi = op->o_bd->bd_info->bi_private;
256 for ( ; on; on = on->on_next ) {
257 if ( on->on_bi.bi_access_allowed ) {
258 /* NOTE: do not copy the structure until required */
259 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
261 db.be_flags |= SLAP_DBFLAG_OVERLAY;
265 op->o_bd->bd_info = (BackendInfo *)on;
266 rc = on->on_bi.bi_access_allowed( op, e,
267 desc, val, access, state, maskp );
268 if ( rc != SLAP_CB_CONTINUE ) break;
272 if ( rc == SLAP_CB_CONTINUE ) {
273 BI_access_allowed *bi_access_allowed;
275 /* if the database structure was changed, o_bd points to a
276 * copy of the structure; put the original bd_info in place */
277 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
278 op->o_bd->bd_info = oi->oi_orig;
281 if ( oi->oi_orig->bi_access_allowed ) {
282 bi_access_allowed = oi->oi_orig->bi_access_allowed;
284 bi_access_allowed = slap_access_allowed;
287 rc = bi_access_allowed( op, e,
288 desc, val, access, state, maskp );
290 /* should not fall thru this far without anything happening... */
291 if ( rc == SLAP_CB_CONTINUE ) {
292 /* access not allowed */
297 op->o_bd->bd_info = bi;
301 #endif /* SLAP_OVERLAY_ACCESS */
304 * default return code in case of missing backend function
305 * and overlay stack returning SLAP_CB_CONTINUE
307 static int op_rc[ op_last ] = {
308 LDAP_UNWILLING_TO_PERFORM, /* bind */
309 LDAP_UNWILLING_TO_PERFORM, /* unbind */
310 LDAP_UNWILLING_TO_PERFORM, /* search */
311 SLAP_CB_CONTINUE, /* compare; pass to frontend */
312 LDAP_UNWILLING_TO_PERFORM, /* modify */
313 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
314 LDAP_UNWILLING_TO_PERFORM, /* add */
315 LDAP_UNWILLING_TO_PERFORM, /* delete */
316 LDAP_UNWILLING_TO_PERFORM, /* abandon */
317 LDAP_UNWILLING_TO_PERFORM, /* cancel */
318 LDAP_UNWILLING_TO_PERFORM, /* extended */
319 LDAP_SUCCESS, /* aux_operational */
320 LDAP_SUCCESS, /* aux_chk_referrals */
321 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
327 slap_operation_t which,
333 int rc = SLAP_CB_CONTINUE;
335 for (; on; on=on->on_next ) {
336 func = &on->on_bi.bi_op_bind;
338 op->o_bd->bd_info = (BackendInfo *)on;
339 rc = func[which]( op, rs );
340 if ( rc != SLAP_CB_CONTINUE ) break;
344 func = &oi->oi_orig->bi_op_bind;
345 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
346 op->o_bd->bd_info = oi->oi_orig;
347 rc = func[which]( op, rs );
349 /* should not fall thru this far without anything happening... */
350 if ( rc == SLAP_CB_CONTINUE ) {
354 /* The underlying backend didn't handle the request, make sure
355 * overlay cleanup is processed.
357 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
358 slap_callback *sc_next;
359 for ( ; op->o_callback && op->o_callback->sc_response !=
360 over_back_response; op->o_callback = sc_next ) {
361 sc_next = op->o_callback->sc_next;
362 if ( op->o_callback->sc_cleanup ) {
363 op->o_callback->sc_cleanup( op, rs );
374 slap_operation_t which
380 BackendDB *be = op->o_bd, db;
381 slap_callback cb = {NULL, over_back_response, NULL, NULL};
382 int rc = SLAP_CB_CONTINUE;
384 /* FIXME: used to happen for instance during abandon
385 * when global overlays are used... */
386 assert( op->o_bd != NULL );
388 oi = op->o_bd->bd_info->bi_private;
391 if ( !SLAP_ISOVERLAY( op->o_bd )) {
393 db.be_flags |= SLAP_DBFLAG_OVERLAY;
396 cb.sc_next = op->o_callback;
398 op->o_callback = &cb;
400 rc = overlay_op_walk( op, rs, which, oi, on );
403 op->o_callback = cb.sc_next;
408 over_op_bind( Operation *op, SlapReply *rs )
410 return over_op_func( op, rs, op_bind );
414 over_op_unbind( Operation *op, SlapReply *rs )
416 return over_op_func( op, rs, op_unbind );
420 over_op_search( Operation *op, SlapReply *rs )
422 return over_op_func( op, rs, op_search );
426 over_op_compare( Operation *op, SlapReply *rs )
428 return over_op_func( op, rs, op_compare );
432 over_op_modify( Operation *op, SlapReply *rs )
434 return over_op_func( op, rs, op_modify );
438 over_op_modrdn( Operation *op, SlapReply *rs )
440 return over_op_func( op, rs, op_modrdn );
444 over_op_add( Operation *op, SlapReply *rs )
446 return over_op_func( op, rs, op_add );
450 over_op_delete( Operation *op, SlapReply *rs )
452 return over_op_func( op, rs, op_delete );
456 over_op_abandon( Operation *op, SlapReply *rs )
458 return over_op_func( op, rs, op_abandon );
462 over_op_cancel( Operation *op, SlapReply *rs )
464 return over_op_func( op, rs, op_cancel );
468 over_op_extended( Operation *op, SlapReply *rs )
470 return over_op_func( op, rs, op_extended );
474 over_aux_operational( Operation *op, SlapReply *rs )
476 return over_op_func( op, rs, op_aux_operational );
480 over_aux_chk_referrals( Operation *op, SlapReply *rs )
482 return over_op_func( op, rs, op_aux_chk_referrals );
486 over_aux_chk_controls( Operation *op, SlapReply *rs )
488 return over_op_func( op, rs, op_aux_chk_controls );
497 over_connection_func(
500 enum conn_which which
506 int rc = SLAP_CB_CONTINUE;
507 BI_connection_init **func;
509 /* FIXME: used to happen for instance during abandon
510 * when global overlays are used... */
511 assert( bd != NULL );
513 oi = bd->bd_info->bi_private;
516 if ( !SLAP_ISOVERLAY( bd ) ) {
518 db.be_flags |= SLAP_DBFLAG_OVERLAY;
522 for ( ; on; on = on->on_next ) {
523 func = &on->on_bi.bi_connection_init;
524 if ( func[ which ] ) {
525 bd->bd_info = (BackendInfo *)on;
526 rc = func[ which ]( bd, conn );
527 if ( rc != SLAP_CB_CONTINUE ) break;
531 func = &oi->oi_orig->bi_connection_init;
532 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
533 bd->bd_info = oi->oi_orig;
534 rc = func[ which ]( bd, conn );
536 /* should not fall thru this far without anything happening... */
537 if ( rc == SLAP_CB_CONTINUE ) {
538 rc = LDAP_UNWILLING_TO_PERFORM;
545 over_connection_init(
550 return over_connection_func( bd, conn, conn_init );
554 over_connection_destroy(
559 return over_connection_func( bd, conn, conn_destroy );
567 on->on_next = overlays;
573 * iterator on registered overlays; overlay_next( NULL ) returns the first
574 * overlay; * subsequent calls with the previously returned value allow to
575 * iterate * over the entire list; returns NULL when no more overlays are
592 * returns a specific registered overlay based on the type; NULL if not
597 overlay_find( const char *over_type )
599 slap_overinst *on = overlays;
601 assert( over_type != NULL );
603 for ( ; on; on = on->on_next ) {
604 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
612 static const char overtype[] = "over";
615 * returns TRUE (1) if the database is actually an overlay instance;
616 * FALSE (0) otherwise.
620 overlay_is_over( BackendDB *be )
622 return be->bd_info->bi_type == overtype;
626 * returns TRUE (1) if the given database is actually an overlay
627 * instance and, somewhere in the list, contains the requested overlay;
628 * FALSE (0) otherwise.
632 overlay_is_inst( BackendDB *be, const char *over_type )
636 assert( be != NULL );
638 if ( !overlay_is_over( be ) ) {
642 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
643 for ( ; on; on = on->on_next ) {
644 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
653 overlay_register_control( BackendDB *be, const char *oid )
659 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
663 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
666 /* add to all backends... */
667 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
672 bd->be_ctrls[ cid ] = 1;
673 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
678 if ( rc == 0 && !gotit ) {
679 be->be_ctrls[ cid ] = 1;
680 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
687 overlay_destroy_one( BackendDB *be, slap_overinst *on )
689 slap_overinfo *oi = on->on_info;
690 slap_overinst **oidx;
692 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
695 if ( on->on_bi.bi_db_destroy ) {
696 BackendInfo *bi_orig = be->bd_info;
697 be->bd_info = (BackendInfo *)on;
698 on->on_bi.bi_db_destroy( be );
699 be->bd_info = bi_orig;
707 /* add an overlay to a particular backend. */
709 overlay_config( BackendDB *be, const char *ov )
711 slap_overinst *on = NULL, *on2 = NULL;
712 slap_overinfo *oi = NULL;
713 BackendInfo *bi = NULL;
715 on = overlay_find( ov );
717 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
721 /* If this is the first overlay on this backend, set up the
722 * overlay info structure
724 if ( !overlay_is_over( be ) ) {
725 oi = ch_malloc( sizeof( slap_overinfo ) );
726 oi->oi_orig = be->bd_info;
727 oi->oi_bi = *be->bd_info;
729 /* NOTE: the first time a global overlay is configured,
730 * frontendDB gets this flag; it is used later by overlays
731 * to determine if they're stacked on top of the frontendDB */
732 if ( oi->oi_orig == frontendDB->bd_info ) {
733 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
736 /* Save a pointer to ourself in bi_private.
738 oi->oi_bi.bi_private = oi;
740 bi = (BackendInfo *)oi;
742 bi->bi_type = (char *)overtype;
744 bi->bi_db_config = over_db_config;
745 bi->bi_db_open = over_db_open;
746 bi->bi_db_close = over_db_close;
747 bi->bi_db_destroy = over_db_destroy;
749 bi->bi_op_bind = over_op_bind;
750 bi->bi_op_unbind = over_op_unbind;
751 bi->bi_op_search = over_op_search;
752 bi->bi_op_compare = over_op_compare;
753 bi->bi_op_modify = over_op_modify;
754 bi->bi_op_modrdn = over_op_modrdn;
755 bi->bi_op_add = over_op_add;
756 bi->bi_op_delete = over_op_delete;
757 bi->bi_op_abandon = over_op_abandon;
758 bi->bi_op_cancel = over_op_cancel;
760 bi->bi_extended = over_op_extended;
763 * this is fine because it has the same
764 * args of the operations; we need to rework
765 * all the hooks to share the same args
766 * of the operations...
768 bi->bi_operational = over_aux_operational;
769 bi->bi_chk_referrals = over_aux_chk_referrals;
770 bi->bi_chk_controls = over_aux_chk_controls;
772 #ifdef SLAP_OVERLAY_ACCESS
773 /* this has a specific arglist */
774 bi->bi_access_allowed = over_access_allowed;
775 #endif /* SLAP_OVERLAY_ACCESS */
777 bi->bi_connection_init = over_connection_init;
778 bi->bi_connection_destroy = over_connection_destroy;
783 if ( overlay_is_inst( be, ov ) ) {
784 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
785 "warning, overlay \"%s\" "
786 "already in list\n", ov, 0, 0 );
789 oi = be->bd_info->bi_private;
792 /* Insert new overlay on head of list. Overlays are executed
793 * in reverse of config order...
795 on2 = ch_calloc( 1, sizeof(slap_overinst) );
798 on2->on_next = oi->oi_list;
801 /* Any initialization needed? */
802 if ( on->on_bi.bi_db_init ) {
804 be->bd_info = (BackendInfo *)on2;
805 rc = on2->on_bi.bi_db_init( be );
806 be->bd_info = (BackendInfo *)oi;