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 */
316 op_aux_chk_referrals,
322 * default return code in case of missing backend function
323 * and overlay stack returning SLAP_CB_CONTINUE
325 static int op_rc[] = {
326 LDAP_UNWILLING_TO_PERFORM, /* bind */
327 LDAP_UNWILLING_TO_PERFORM, /* unbind */
328 LDAP_UNWILLING_TO_PERFORM, /* search */
329 SLAP_CB_CONTINUE, /* compare; pass to frontend */
330 LDAP_UNWILLING_TO_PERFORM, /* modify */
331 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
332 LDAP_UNWILLING_TO_PERFORM, /* add */
333 LDAP_UNWILLING_TO_PERFORM, /* delete */
334 LDAP_UNWILLING_TO_PERFORM, /* abandon */
335 LDAP_UNWILLING_TO_PERFORM, /* cancel */
336 LDAP_UNWILLING_TO_PERFORM, /* extended */
337 LDAP_SUCCESS, /* aux_operational */
338 LDAP_SUCCESS, /* aux_chk_referrals */
339 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
352 BackendDB *be = op->o_bd, db;
353 slap_callback cb = {NULL, over_back_response, NULL, NULL};
354 int rc = SLAP_CB_CONTINUE;
356 /* FIXME: used to happen for instance during abandon
357 * when global overlays are used... */
358 assert( op->o_bd != NULL );
360 oi = op->o_bd->bd_info->bi_private;
363 if ( !SLAP_ISOVERLAY( op->o_bd )) {
365 db.be_flags |= SLAP_DBFLAG_OVERLAY;
368 cb.sc_next = op->o_callback;
370 op->o_callback = &cb;
372 for (; on; on=on->on_next ) {
373 func = &on->on_bi.bi_op_bind;
375 op->o_bd->bd_info = (BackendInfo *)on;
376 rc = func[which]( op, rs );
377 if ( rc != SLAP_CB_CONTINUE ) break;
381 func = &oi->oi_orig->bi_op_bind;
382 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
383 op->o_bd->bd_info = oi->oi_orig;
384 rc = func[which]( op, rs );
386 /* should not fall thru this far without anything happening... */
387 if ( rc == SLAP_CB_CONTINUE ) {
391 /* The underlying backend didn't handle the request, make sure
392 * overlay cleanup is processed.
394 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
395 slap_callback *sc_next;
396 for ( ; op->o_callback && op->o_callback != cb.sc_next;
397 op->o_callback = sc_next ) {
398 sc_next = op->o_callback->sc_next;
399 if ( op->o_callback->sc_cleanup ) {
400 op->o_callback->sc_cleanup( op, rs );
405 op->o_callback = cb.sc_next;
410 over_op_bind( Operation *op, SlapReply *rs )
412 return over_op_func( op, rs, op_bind );
416 over_op_unbind( Operation *op, SlapReply *rs )
418 return over_op_func( op, rs, op_unbind );
422 over_op_search( Operation *op, SlapReply *rs )
424 return over_op_func( op, rs, op_search );
428 over_op_compare( Operation *op, SlapReply *rs )
430 return over_op_func( op, rs, op_compare );
434 over_op_modify( Operation *op, SlapReply *rs )
436 return over_op_func( op, rs, op_modify );
440 over_op_modrdn( Operation *op, SlapReply *rs )
442 return over_op_func( op, rs, op_modrdn );
446 over_op_add( Operation *op, SlapReply *rs )
448 return over_op_func( op, rs, op_add );
452 over_op_delete( Operation *op, SlapReply *rs )
454 return over_op_func( op, rs, op_delete );
458 over_op_abandon( Operation *op, SlapReply *rs )
460 return over_op_func( op, rs, op_abandon );
464 over_op_cancel( Operation *op, SlapReply *rs )
466 return over_op_func( op, rs, op_cancel );
470 over_op_extended( Operation *op, SlapReply *rs )
472 return over_op_func( op, rs, op_extended );
476 over_aux_operational( Operation *op, SlapReply *rs )
478 return over_op_func( op, rs, op_aux_operational );
482 over_aux_chk_referrals( Operation *op, SlapReply *rs )
484 return over_op_func( op, rs, op_aux_chk_referrals );
488 over_aux_chk_controls( Operation *op, SlapReply *rs )
490 return over_op_func( op, rs, op_aux_chk_controls );
499 over_connection_func(
502 enum conn_which which
508 int rc = SLAP_CB_CONTINUE;
509 BI_connection_init **func;
511 /* FIXME: used to happen for instance during abandon
512 * when global overlays are used... */
513 assert( bd != NULL );
515 oi = bd->bd_info->bi_private;
518 if ( !SLAP_ISOVERLAY( bd ) ) {
520 db.be_flags |= SLAP_DBFLAG_OVERLAY;
524 for ( ; on; on = on->on_next ) {
525 func = &on->on_bi.bi_connection_init;
526 if ( func[ which ] ) {
527 bd->bd_info = (BackendInfo *)on;
528 rc = func[ which ]( bd, conn );
529 if ( rc != SLAP_CB_CONTINUE ) break;
533 func = &oi->oi_orig->bi_connection_init;
534 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
535 bd->bd_info = oi->oi_orig;
536 rc = func[ which ]( bd, conn );
538 /* should not fall thru this far without anything happening... */
539 if ( rc == SLAP_CB_CONTINUE ) {
540 rc = LDAP_UNWILLING_TO_PERFORM;
547 over_connection_init(
552 return over_connection_func( bd, conn, conn_init );
556 over_connection_destroy(
561 return over_connection_func( bd, conn, conn_destroy );
569 on->on_next = overlays;
575 * iterator on registered overlays; overlay_next( NULL ) returns the first
576 * overlay; * subsequent calls with the previously returned value allow to
577 * iterate * over the entire list; returns NULL when no more overlays are
594 * returns a specific registered overlay based on the type; NULL if not
599 overlay_find( const char *over_type )
601 slap_overinst *on = overlays;
605 for ( ; on; on = on->on_next ) {
606 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
614 static const char overtype[] = "over";
617 * returns TRUE (1) if the database is actually an overlay instance;
618 * FALSE (0) otherwise.
622 overlay_is_over( BackendDB *be )
624 return be->bd_info->bi_type == overtype;
628 * returns TRUE (1) if the given database is actually an overlay
629 * instance and, somewhere in the list, contains the requested overlay;
630 * FALSE (0) otherwise.
634 overlay_is_inst( BackendDB *be, const char *over_type )
640 if ( !overlay_is_over( be ) ) {
644 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
645 for ( ; on; on = on->on_next ) {
646 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
655 overlay_register_control( BackendDB *be, const char *oid )
661 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
665 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
668 /* add to all backends... */
669 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
674 bd->be_ctrls[ cid ] = 1;
675 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
680 if ( rc == 0 && !gotit ) {
681 be->be_ctrls[ cid ] = 1;
682 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
689 overlay_destroy_one( BackendDB *be, slap_overinst *on )
691 slap_overinfo *oi = on->on_info;
692 slap_overinst **oidx;
694 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
697 if ( on->on_bi.bi_db_destroy ) {
698 BackendInfo *bi_orig = be->bd_info;
699 be->bd_info = (BackendInfo *)on;
700 on->on_bi.bi_db_destroy( be );
701 be->bd_info = bi_orig;
709 /* add an overlay to a particular backend. */
711 overlay_config( BackendDB *be, const char *ov )
713 slap_overinst *on = NULL, *on2 = NULL;
714 slap_overinfo *oi = NULL;
715 BackendInfo *bi = NULL;
717 on = overlay_find( ov );
719 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
723 /* If this is the first overlay on this backend, set up the
724 * overlay info structure
726 if ( !overlay_is_over( be ) ) {
727 oi = ch_malloc( sizeof( slap_overinfo ) );
728 oi->oi_orig = be->bd_info;
729 oi->oi_bi = *be->bd_info;
731 /* NOTE: the first time a global overlay is configured,
732 * frontendDB gets this flag; it is used later by overlays
733 * to determine if they're stacked on top of the frontendDB */
734 if ( oi->oi_orig == frontendDB->bd_info ) {
735 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
738 /* Save a pointer to ourself in bi_private.
740 oi->oi_bi.bi_private = oi;
742 bi = (BackendInfo *)oi;
744 bi->bi_type = (char *)overtype;
746 bi->bi_db_config = over_db_config;
747 bi->bi_db_open = over_db_open;
748 bi->bi_db_close = over_db_close;
749 bi->bi_db_destroy = over_db_destroy;
751 bi->bi_op_bind = over_op_bind;
752 bi->bi_op_unbind = over_op_unbind;
753 bi->bi_op_search = over_op_search;
754 bi->bi_op_compare = over_op_compare;
755 bi->bi_op_modify = over_op_modify;
756 bi->bi_op_modrdn = over_op_modrdn;
757 bi->bi_op_add = over_op_add;
758 bi->bi_op_delete = over_op_delete;
759 bi->bi_op_abandon = over_op_abandon;
760 bi->bi_op_cancel = over_op_cancel;
762 bi->bi_extended = over_op_extended;
765 * this is fine because it has the same
766 * args of the operations; we need to rework
767 * all the hooks to share the same args
768 * of the operations...
770 bi->bi_operational = over_aux_operational;
771 bi->bi_chk_referrals = over_aux_chk_referrals;
772 bi->bi_chk_controls = over_aux_chk_controls;
774 #ifdef SLAP_OVERLAY_ACCESS
775 /* this has a specific arglist */
776 bi->bi_access_allowed = over_access_allowed;
777 #endif /* SLAP_OVERLAY_ACCESS */
779 bi->bi_connection_init = over_connection_init;
780 bi->bi_connection_destroy = over_connection_destroy;
785 if ( overlay_is_inst( be, ov ) ) {
786 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
787 "warning, overlay \"%s\" "
788 "already in list\n", ov, 0, 0 );
791 oi = be->bd_info->bi_private;
794 /* Insert new overlay on head of list. Overlays are executed
795 * in reverse of config order...
797 on2 = ch_calloc( 1, sizeof(slap_overinst) );
800 on2->on_next = oi->oi_list;
803 /* Any initialization needed? */
804 if ( on->on_bi.bi_db_init ) {
806 be->bd_info = (BackendInfo *)on2;
807 rc = on2->on_bi.bi_db_init( be );
808 be->bd_info = (BackendInfo *)oi;