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;
80 if ( oi->oi_orig->bi_db_config ) {
81 be->bd_info = oi->oi_orig;
82 be->be_cf_ocs = oi->oi_orig->bi_cf_ocs;
83 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
86 if ( be->bd_info != oi->oi_orig ) {
88 slap_overinst *on2, **onp;
92 /* a database added an overlay;
93 * work it around... */
94 assert( overlay_is_over( be ) );
96 oi2 = ( slap_overinfo * )be->bd_info->bi_private;
99 /* need to put a uniqueness check here as well;
100 * note that in principle there could be more than
101 * one overlay as a result of multiple calls to
102 * overlay_config() */
103 be2.bd_info = (BackendInfo *)oi;
105 for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
106 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
107 Debug( LDAP_DEBUG_ANY, "over_db_config(): "
108 "warning, freshly added "
109 "overlay #%d \"%s\" is already in list\n",
110 i, (*onp)->on_bi.bi_type, 0 );
112 /* NOTE: if the overlay already exists,
113 * there is no way to merge the results
114 * of the configuration that may have
115 * occurred during bi_db_config(); we
116 * just issue a warning, and the
117 * administrator should deal with this */
124 ch_free( be->bd_info );
127 be->bd_info = (BackendInfo *)oi;
128 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
136 for (; on; on=on->on_next) {
137 rc = SLAP_CONF_UNKNOWN;
138 if (on->on_bi.bi_cf_ocs) {
141 ct = config_find_keyword( on->on_bi.bi_cf_ocs->co_table, &ca );
143 rc = config_add_vals( ct, &ca );
146 if (on->on_bi.bi_db_config && rc == SLAP_CONF_UNKNOWN) {
147 be->bd_info = &on->on_bi;
148 rc = on->on_bi.bi_db_config( be, fname, lineno,
150 if ( rc != SLAP_CONF_UNKNOWN ) break;
153 be->bd_info = bi_orig;
154 be->be_cf_ocs = be_cf_ocs;
164 return over_db_func( be, db_open );
172 slap_overinfo *oi = be->bd_info->bi_private;
173 slap_overinst *on = oi->oi_list;
174 BackendInfo *bi_orig = be->bd_info;
177 for (; on && rc == 0; on=on->on_next) {
178 be->bd_info = &on->on_bi;
179 if ( be->bd_info->bi_db_close ) {
180 rc = be->bd_info->bi_db_close( be );
184 if ( oi->oi_orig->bi_db_close ) {
185 be->bd_info = oi->oi_orig;
186 rc = be->bd_info->bi_db_close( be );
189 be->bd_info = bi_orig;
198 slap_overinfo *oi = be->bd_info->bi_private;
199 slap_overinst *on = oi->oi_list, *next;
202 rc = over_db_func( be, db_destroy );
204 for (next = on->on_next; on; on=next) {
213 over_back_response ( Operation *op, SlapReply *rs )
215 slap_overinfo *oi = op->o_callback->sc_private;
216 slap_overinst *on = oi->oi_list;
217 int rc = SLAP_CB_CONTINUE;
218 BackendDB *be = op->o_bd, db = *op->o_bd;
220 db.be_flags |= SLAP_DBFLAG_OVERLAY;
222 for (; on; on=on->on_next ) {
223 if ( on->on_response ) {
224 db.bd_info = (BackendInfo *)on;
225 rc = on->on_response( op, rs );
226 if ( rc != SLAP_CB_CONTINUE ) break;
233 #ifdef SLAP_OVERLAY_ACCESS
238 AttributeDescription *desc,
240 slap_access_t access,
241 AccessControlState *state,
246 BackendInfo *bi = op->o_bd->bd_info;
247 BackendDB *be = op->o_bd, db;
248 int rc = SLAP_CB_CONTINUE;
250 /* FIXME: used to happen for instance during abandon
251 * when global overlays are used... */
252 assert( op->o_bd != NULL );
254 oi = op->o_bd->bd_info->bi_private;
257 for ( ; on; on = on->on_next ) {
258 if ( on->on_bi.bi_access_allowed ) {
259 /* NOTE: do not copy the structure until required */
260 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
262 db.be_flags |= SLAP_DBFLAG_OVERLAY;
266 op->o_bd->bd_info = (BackendInfo *)on;
267 rc = on->on_bi.bi_access_allowed( op, e,
268 desc, val, access, state, maskp );
269 if ( rc != SLAP_CB_CONTINUE ) break;
273 if ( rc == SLAP_CB_CONTINUE ) {
274 BI_access_allowed *bi_access_allowed;
276 /* if the database structure was changed, o_bd points to a
277 * copy of the structure; put the original bd_info in place */
278 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
279 op->o_bd->bd_info = oi->oi_orig;
282 if ( oi->oi_orig->bi_access_allowed ) {
283 bi_access_allowed = oi->oi_orig->bi_access_allowed;
285 bi_access_allowed = slap_access_allowed;
288 rc = bi_access_allowed( op, e,
289 desc, val, access, state, maskp );
291 /* should not fall thru this far without anything happening... */
292 if ( rc == SLAP_CB_CONTINUE ) {
293 /* access not allowed */
298 op->o_bd->bd_info = bi;
302 #endif /* SLAP_OVERLAY_ACCESS */
317 op_aux_chk_referrals,
323 * default return code in case of missing backend function
324 * and overlay stack returning SLAP_CB_CONTINUE
326 static int op_rc[] = {
327 LDAP_UNWILLING_TO_PERFORM, /* bind */
328 LDAP_UNWILLING_TO_PERFORM, /* unbind */
329 LDAP_UNWILLING_TO_PERFORM, /* search */
330 SLAP_CB_CONTINUE, /* compare; pass to frontend */
331 LDAP_UNWILLING_TO_PERFORM, /* modify */
332 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
333 LDAP_UNWILLING_TO_PERFORM, /* add */
334 LDAP_UNWILLING_TO_PERFORM, /* delete */
335 LDAP_UNWILLING_TO_PERFORM, /* abandon */
336 LDAP_UNWILLING_TO_PERFORM, /* cancel */
337 LDAP_UNWILLING_TO_PERFORM, /* extended */
338 LDAP_SUCCESS, /* aux_operational */
339 LDAP_SUCCESS, /* aux_chk_referrals */
340 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
353 BackendDB *be = op->o_bd, db;
354 slap_callback cb = {NULL, over_back_response, NULL, NULL};
355 int rc = SLAP_CB_CONTINUE;
357 /* FIXME: used to happen for instance during abandon
358 * when global overlays are used... */
359 assert( op->o_bd != NULL );
361 oi = op->o_bd->bd_info->bi_private;
364 if ( !SLAP_ISOVERLAY( op->o_bd )) {
366 db.be_flags |= SLAP_DBFLAG_OVERLAY;
369 cb.sc_next = op->o_callback;
371 op->o_callback = &cb;
373 for (; on; on=on->on_next ) {
374 func = &on->on_bi.bi_op_bind;
376 op->o_bd->bd_info = (BackendInfo *)on;
377 rc = func[which]( op, rs );
378 if ( rc != SLAP_CB_CONTINUE ) break;
382 func = &oi->oi_orig->bi_op_bind;
383 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
384 op->o_bd->bd_info = oi->oi_orig;
385 rc = func[which]( op, rs );
387 /* should not fall thru this far without anything happening... */
388 if ( rc == SLAP_CB_CONTINUE ) {
392 /* The underlying backend didn't handle the request, make sure
393 * overlay cleanup is processed.
395 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
396 slap_callback *sc_next;
397 for ( ; op->o_callback && op->o_callback != cb.sc_next;
398 op->o_callback = sc_next ) {
399 sc_next = op->o_callback->sc_next;
400 if ( op->o_callback->sc_cleanup ) {
401 op->o_callback->sc_cleanup( op, rs );
406 op->o_callback = cb.sc_next;
411 over_op_bind( Operation *op, SlapReply *rs )
413 return over_op_func( op, rs, op_bind );
417 over_op_unbind( Operation *op, SlapReply *rs )
419 return over_op_func( op, rs, op_unbind );
423 over_op_search( Operation *op, SlapReply *rs )
425 return over_op_func( op, rs, op_search );
429 over_op_compare( Operation *op, SlapReply *rs )
431 return over_op_func( op, rs, op_compare );
435 over_op_modify( Operation *op, SlapReply *rs )
437 return over_op_func( op, rs, op_modify );
441 over_op_modrdn( Operation *op, SlapReply *rs )
443 return over_op_func( op, rs, op_modrdn );
447 over_op_add( Operation *op, SlapReply *rs )
449 return over_op_func( op, rs, op_add );
453 over_op_delete( Operation *op, SlapReply *rs )
455 return over_op_func( op, rs, op_delete );
459 over_op_abandon( Operation *op, SlapReply *rs )
461 return over_op_func( op, rs, op_abandon );
465 over_op_cancel( Operation *op, SlapReply *rs )
467 return over_op_func( op, rs, op_cancel );
471 over_op_extended( Operation *op, SlapReply *rs )
473 return over_op_func( op, rs, op_extended );
477 over_aux_operational( Operation *op, SlapReply *rs )
479 return over_op_func( op, rs, op_aux_operational );
483 over_aux_chk_referrals( Operation *op, SlapReply *rs )
485 return over_op_func( op, rs, op_aux_chk_referrals );
489 over_aux_chk_controls( Operation *op, SlapReply *rs )
491 return over_op_func( op, rs, op_aux_chk_controls );
500 over_connection_func(
503 enum conn_which which
509 int rc = SLAP_CB_CONTINUE;
510 BI_connection_init **func;
512 /* FIXME: used to happen for instance during abandon
513 * when global overlays are used... */
514 assert( bd != NULL );
516 oi = bd->bd_info->bi_private;
519 if ( !SLAP_ISOVERLAY( bd ) ) {
521 db.be_flags |= SLAP_DBFLAG_OVERLAY;
525 for ( ; on; on = on->on_next ) {
526 func = &on->on_bi.bi_connection_init;
527 if ( func[ which ] ) {
528 bd->bd_info = (BackendInfo *)on;
529 rc = func[ which ]( bd, conn );
530 if ( rc != SLAP_CB_CONTINUE ) break;
534 func = &oi->oi_orig->bi_connection_init;
535 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
536 bd->bd_info = oi->oi_orig;
537 rc = func[ which ]( bd, conn );
539 /* should not fall thru this far without anything happening... */
540 if ( rc == SLAP_CB_CONTINUE ) {
541 rc = LDAP_UNWILLING_TO_PERFORM;
548 over_connection_init(
553 return over_connection_func( bd, conn, conn_init );
557 over_connection_destroy(
562 return over_connection_func( bd, conn, conn_destroy );
570 on->on_next = overlays;
576 * iterator on registered overlays; overlay_next( NULL ) returns the first
577 * overlay; * subsequent calls with the previously returned value allow to
578 * iterate * over the entire list; returns NULL when no more overlays are
595 * returns a specific registered overlay based on the type; NULL if not
600 overlay_find( const char *over_type )
602 slap_overinst *on = overlays;
606 for ( ; on; on = on->on_next ) {
607 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
615 static const char overtype[] = "over";
618 * returns TRUE (1) if the database is actually an overlay instance;
619 * FALSE (0) otherwise.
623 overlay_is_over( BackendDB *be )
625 return be->bd_info->bi_type == overtype;
629 * returns TRUE (1) if the given database is actually an overlay
630 * instance and, somewhere in the list, contains the requested overlay;
631 * FALSE (0) otherwise.
635 overlay_is_inst( BackendDB *be, const char *over_type )
641 if ( !overlay_is_over( be ) ) {
645 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
646 for ( ; on; on = on->on_next ) {
647 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
656 overlay_register_control( BackendDB *be, const char *oid )
662 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
666 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
669 /* add to all backends... */
670 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
675 bd->be_ctrls[ cid ] = 1;
676 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
681 if ( rc == 0 && !gotit ) {
682 be->be_ctrls[ cid ] = 1;
683 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
690 overlay_destroy_one( BackendDB *be, slap_overinst *on )
692 slap_overinfo *oi = on->on_info;
693 slap_overinst **oidx;
695 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
698 if ( on->on_bi.bi_db_destroy ) {
699 BackendInfo *bi_orig = be->bd_info;
700 be->bd_info = (BackendInfo *)on;
701 on->on_bi.bi_db_destroy( be );
702 be->bd_info = bi_orig;
710 /* add an overlay to a particular backend. */
712 overlay_config( BackendDB *be, const char *ov )
714 slap_overinst *on = NULL, *on2 = NULL;
715 slap_overinfo *oi = NULL;
716 BackendInfo *bi = NULL;
718 on = overlay_find( ov );
720 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
724 /* If this is the first overlay on this backend, set up the
725 * overlay info structure
727 if ( !overlay_is_over( be ) ) {
728 oi = ch_malloc( sizeof( slap_overinfo ) );
729 oi->oi_orig = be->bd_info;
730 oi->oi_bi = *be->bd_info;
732 /* NOTE: the first time a global overlay is configured,
733 * frontendDB gets this flag; it is used later by overlays
734 * to determine if they're stacked on top of the frontendDB */
735 if ( oi->oi_orig == frontendDB->bd_info ) {
736 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
739 /* Save a pointer to ourself in bi_private.
741 oi->oi_bi.bi_private = oi;
743 bi = (BackendInfo *)oi;
745 bi->bi_type = (char *)overtype;
747 bi->bi_db_config = over_db_config;
748 bi->bi_db_open = over_db_open;
749 bi->bi_db_close = over_db_close;
750 bi->bi_db_destroy = over_db_destroy;
752 bi->bi_op_bind = over_op_bind;
753 bi->bi_op_unbind = over_op_unbind;
754 bi->bi_op_search = over_op_search;
755 bi->bi_op_compare = over_op_compare;
756 bi->bi_op_modify = over_op_modify;
757 bi->bi_op_modrdn = over_op_modrdn;
758 bi->bi_op_add = over_op_add;
759 bi->bi_op_delete = over_op_delete;
760 bi->bi_op_abandon = over_op_abandon;
761 bi->bi_op_cancel = over_op_cancel;
763 bi->bi_extended = over_op_extended;
766 * this is fine because it has the same
767 * args of the operations; we need to rework
768 * all the hooks to share the same args
769 * of the operations...
771 bi->bi_operational = over_aux_operational;
772 bi->bi_chk_referrals = over_aux_chk_referrals;
773 bi->bi_chk_controls = over_aux_chk_controls;
775 #ifdef SLAP_OVERLAY_ACCESS
776 /* this has a specific arglist */
777 bi->bi_access_allowed = over_access_allowed;
778 #endif /* SLAP_OVERLAY_ACCESS */
780 bi->bi_connection_init = over_connection_init;
781 bi->bi_connection_destroy = over_connection_destroy;
786 if ( overlay_is_inst( be, ov ) ) {
787 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
788 "warning, overlay \"%s\" "
789 "already in list\n", ov, 0, 0 );
792 oi = be->bd_info->bi_private;
795 /* Insert new overlay on head of list. Overlays are executed
796 * in reverse of config order...
798 on2 = ch_calloc( 1, sizeof(slap_overinst) );
801 on2->on_next = oi->oi_list;
804 /* Any initialization needed? */
805 if ( on->on_bi.bi_db_init ) {
807 be->bd_info = (BackendInfo *)on2;
808 rc = on2->on_bi.bi_db_init( be );
809 be->bd_info = (BackendInfo *)oi;