1 /* backover.c - backend overlay routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2011 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;
41 slap_overinfo *oi = be->bd_info->bi_private;
42 slap_overinst *on = oi->oi_list;
43 BackendInfo *bi_orig = be->bd_info;
44 struct ConfigOCs *be_cf_ocs = be->be_cf_ocs;
48 if ( oi->oi_orig->bi_db_config ) {
49 be->bd_info = oi->oi_orig;
50 be->be_cf_ocs = oi->oi_orig->bi_cf_ocs;
51 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
54 if ( be->bd_info != oi->oi_orig ) {
56 slap_overinst *on2, **onp;
60 /* a database added an overlay;
61 * work it around... */
62 assert( overlay_is_over( be ) );
64 oi2 = ( slap_overinfo * )be->bd_info->bi_private;
67 /* need to put a uniqueness check here as well;
68 * note that in principle there could be more than
69 * one overlay as a result of multiple calls to
71 be2.bd_info = (BackendInfo *)oi;
73 for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
74 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
75 Debug( LDAP_DEBUG_ANY, "over_db_config(): "
76 "warning, freshly added "
77 "overlay #%d \"%s\" is already in list\n",
78 i, (*onp)->on_bi.bi_type, 0 );
80 /* NOTE: if the overlay already exists,
81 * there is no way to merge the results
82 * of the configuration that may have
83 * occurred during bi_db_config(); we
84 * just issue a warning, and the
85 * administrator should deal with this */
92 ch_free( be->bd_info );
95 be->bd_info = (BackendInfo *)oi;
96 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
104 snprintf( ca.log, sizeof( ca.log ), "%s: line %d",
105 ca.fname, ca.lineno );
106 ca.op = SLAP_CONFIG_ADD;
109 for (; on; on=on->on_next) {
110 rc = SLAP_CONF_UNKNOWN;
111 if (on->on_bi.bi_cf_ocs) {
114 ct = config_find_keyword( on->on_bi.bi_cf_ocs->co_table, &ca );
116 ca.table = on->on_bi.bi_cf_ocs->co_type;
117 rc = config_add_vals( ct, &ca );
118 if ( rc != SLAP_CONF_UNKNOWN )
122 if (on->on_bi.bi_db_config && rc == SLAP_CONF_UNKNOWN) {
123 be->bd_info = &on->on_bi;
124 rc = on->on_bi.bi_db_config( be, fname, lineno,
126 if ( rc != SLAP_CONF_UNKNOWN ) break;
129 be->bd_info = bi_orig;
130 be->be_cf_ocs = be_cf_ocs;
141 slap_overinfo *oi = be->bd_info->bi_private;
142 slap_overinst *on = oi->oi_list;
146 db.be_flags |= SLAP_DBFLAG_OVERLAY;
147 db.bd_info = oi->oi_orig;
148 if ( db.bd_info->bi_db_open ) {
149 rc = db.bd_info->bi_db_open( &db, cr );
152 for (; on && rc == 0; on=on->on_next) {
153 db.bd_info = &on->on_bi;
154 if ( db.bd_info->bi_db_open ) {
155 rc = db.bd_info->bi_db_open( &db, cr );
168 slap_overinfo *oi = be->bd_info->bi_private;
169 slap_overinst *on = oi->oi_list;
170 BackendInfo *bi_orig = be->bd_info;
173 for (; on && rc == 0; on=on->on_next) {
174 be->bd_info = &on->on_bi;
175 if ( be->bd_info->bi_db_close ) {
176 rc = be->bd_info->bi_db_close( be, cr );
180 if ( oi->oi_orig->bi_db_close ) {
181 be->bd_info = oi->oi_orig;
182 rc = be->bd_info->bi_db_close( be, cr );
185 be->bd_info = bi_orig;
195 slap_overinfo *oi = be->bd_info->bi_private;
196 slap_overinst *on = oi->oi_list, *next;
197 BackendInfo *bi_orig = be->bd_info;
200 be->bd_info = oi->oi_orig;
201 if ( be->bd_info->bi_db_destroy ) {
202 rc = be->bd_info->bi_db_destroy( be, cr );
205 for (; on && rc == 0; on=on->on_next) {
206 be->bd_info = &on->on_bi;
207 if ( be->bd_info->bi_db_destroy ) {
208 rc = be->bd_info->bi_db_destroy( be, cr );
214 for (next = on->on_next; on; on=next) {
219 be->bd_info = bi_orig;
225 over_back_response ( Operation *op, SlapReply *rs )
227 slap_overinfo *oi = op->o_callback->sc_private;
228 slap_overinst *on = oi->oi_list;
229 int rc = SLAP_CB_CONTINUE;
230 BackendDB *be = op->o_bd, db = *op->o_bd;
232 db.be_flags |= SLAP_DBFLAG_OVERLAY;
234 for (; on; on=on->on_next ) {
235 if ( on->on_response ) {
236 db.bd_info = (BackendInfo *)on;
237 rc = on->on_response( op, rs );
238 if ( rc != SLAP_CB_CONTINUE ) break;
241 /* Bypass the remaining on_response layers, but allow
242 * normal execution to continue.
244 if ( rc == SLAP_CB_BYPASS )
245 rc = SLAP_CB_CONTINUE;
254 AttributeDescription *desc,
256 slap_access_t access,
257 AccessControlState *state,
263 BackendDB *be = op->o_bd, db;
264 int rc = SLAP_CB_CONTINUE;
266 /* FIXME: used to happen for instance during abandon
267 * when global overlays are used... */
268 assert( op->o_bd != NULL );
270 bi = op->o_bd->bd_info;
271 /* Were we invoked on the frontend? */
272 if ( !bi->bi_access_allowed ) {
273 oi = frontendDB->bd_info->bi_private;
275 oi = op->o_bd->bd_info->bi_private;
279 for ( ; on; on = on->on_next ) {
280 if ( on->on_bi.bi_access_allowed ) {
281 /* NOTE: do not copy the structure until required */
282 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
284 db.be_flags |= SLAP_DBFLAG_OVERLAY;
288 op->o_bd->bd_info = (BackendInfo *)on;
289 rc = on->on_bi.bi_access_allowed( op, e,
290 desc, val, access, state, maskp );
291 if ( rc != SLAP_CB_CONTINUE ) break;
295 if ( rc == SLAP_CB_CONTINUE ) {
296 BI_access_allowed *bi_access_allowed;
298 /* if the database structure was changed, o_bd points to a
299 * copy of the structure; put the original bd_info in place */
300 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
301 op->o_bd->bd_info = oi->oi_orig;
304 if ( oi->oi_orig->bi_access_allowed ) {
305 bi_access_allowed = oi->oi_orig->bi_access_allowed;
307 bi_access_allowed = slap_access_allowed;
310 rc = bi_access_allowed( op, e,
311 desc, val, access, state, maskp );
313 /* should not fall thru this far without anything happening... */
314 if ( rc == SLAP_CB_CONTINUE ) {
315 /* access not allowed */
320 op->o_bd->bd_info = bi;
326 overlay_entry_get_ov(
330 AttributeDescription *ad,
335 slap_overinfo *oi = on->on_info;
336 BackendDB *be = op->o_bd, db;
337 BackendInfo *bi = op->o_bd->bd_info;
338 int rc = SLAP_CB_CONTINUE;
340 for ( ; on; on = on->on_next ) {
341 if ( on->on_bi.bi_entry_get_rw ) {
342 /* NOTE: do not copy the structure until required */
343 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
345 db.be_flags |= SLAP_DBFLAG_OVERLAY;
349 op->o_bd->bd_info = (BackendInfo *)on;
350 rc = on->on_bi.bi_entry_get_rw( op, dn,
352 if ( rc != SLAP_CB_CONTINUE ) break;
356 if ( rc == SLAP_CB_CONTINUE ) {
357 /* if the database structure was changed, o_bd points to a
358 * copy of the structure; put the original bd_info in place */
359 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
360 op->o_bd->bd_info = oi->oi_orig;
363 if ( oi->oi_orig->bi_entry_get_rw ) {
364 rc = oi->oi_orig->bi_entry_get_rw( op, dn,
368 /* should not fall thru this far without anything happening... */
369 if ( rc == SLAP_CB_CONTINUE ) {
370 rc = LDAP_UNWILLING_TO_PERFORM;
374 op->o_bd->bd_info = bi;
384 AttributeDescription *ad,
391 assert( op->o_bd != NULL );
393 oi = op->o_bd->bd_info->bi_private;
396 return overlay_entry_get_ov( op, dn, oc, ad, rw, e, on );
400 overlay_entry_release_ov(
406 slap_overinfo *oi = on->on_info;
407 BackendDB *be = op->o_bd, db;
408 BackendInfo *bi = op->o_bd->bd_info;
409 int rc = SLAP_CB_CONTINUE;
411 for ( ; on; on = on->on_next ) {
412 if ( on->on_bi.bi_entry_release_rw ) {
413 /* NOTE: do not copy the structure until required */
414 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
416 db.be_flags |= SLAP_DBFLAG_OVERLAY;
420 op->o_bd->bd_info = (BackendInfo *)on;
421 rc = on->on_bi.bi_entry_release_rw( op, e, rw );
422 if ( rc != SLAP_CB_CONTINUE ) break;
426 if ( rc == SLAP_CB_CONTINUE ) {
427 /* if the database structure was changed, o_bd points to a
428 * copy of the structure; put the original bd_info in place */
429 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
430 op->o_bd->bd_info = oi->oi_orig;
433 if ( oi->oi_orig->bi_entry_release_rw ) {
434 rc = oi->oi_orig->bi_entry_release_rw( op, e, rw );
437 /* should not fall thru this far without anything happening... */
438 if ( rc == SLAP_CB_CONTINUE ) {
444 op->o_bd->bd_info = bi;
450 over_entry_release_rw(
458 assert( op->o_bd != NULL );
460 oi = op->o_bd->bd_info->bi_private;
463 return overlay_entry_release_ov( op, e, rw, on );
470 struct berval *gr_ndn,
471 struct berval *op_ndn,
472 ObjectClass *group_oc,
473 AttributeDescription *group_at )
478 BackendDB *be = op->o_bd, db;
479 int rc = SLAP_CB_CONTINUE;
481 /* FIXME: used to happen for instance during abandon
482 * when global overlays are used... */
483 assert( be != NULL );
489 for ( ; on; on = on->on_next ) {
490 if ( on->on_bi.bi_acl_group ) {
491 /* NOTE: do not copy the structure until required */
492 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
494 db.be_flags |= SLAP_DBFLAG_OVERLAY;
498 op->o_bd->bd_info = (BackendInfo *)on;
499 rc = on->on_bi.bi_acl_group( op, e,
500 gr_ndn, op_ndn, group_oc, group_at );
501 if ( rc != SLAP_CB_CONTINUE ) break;
505 if ( rc == SLAP_CB_CONTINUE ) {
506 BI_acl_group *bi_acl_group;
508 /* if the database structure was changed, o_bd points to a
509 * copy of the structure; put the original bd_info in place */
510 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
511 op->o_bd->bd_info = oi->oi_orig;
514 if ( oi->oi_orig->bi_acl_group ) {
515 bi_acl_group = oi->oi_orig->bi_acl_group;
517 bi_acl_group = backend_group;
520 rc = bi_acl_group( op, e,
521 gr_ndn, op_ndn, group_oc, group_at );
523 /* should not fall thru this far without anything happening... */
524 if ( rc == SLAP_CB_CONTINUE ) {
525 /* access not allowed */
530 op->o_bd->bd_info = bi;
539 struct berval *entry_ndn,
540 AttributeDescription *entry_at,
542 slap_access_t access )
547 BackendDB *be = op->o_bd, db;
548 int rc = SLAP_CB_CONTINUE;
550 /* FIXME: used to happen for instance during abandon
551 * when global overlays are used... */
552 assert( be != NULL );
558 for ( ; on; on = on->on_next ) {
559 if ( on->on_bi.bi_acl_attribute ) {
560 /* NOTE: do not copy the structure until required */
561 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
563 db.be_flags |= SLAP_DBFLAG_OVERLAY;
567 op->o_bd->bd_info = (BackendInfo *)on;
568 rc = on->on_bi.bi_acl_attribute( op, target,
569 entry_ndn, entry_at, vals, access );
570 if ( rc != SLAP_CB_CONTINUE ) break;
574 if ( rc == SLAP_CB_CONTINUE ) {
575 BI_acl_attribute *bi_acl_attribute;
577 /* if the database structure was changed, o_bd points to a
578 * copy of the structure; put the original bd_info in place */
579 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
580 op->o_bd->bd_info = oi->oi_orig;
583 if ( oi->oi_orig->bi_acl_attribute ) {
584 bi_acl_attribute = oi->oi_orig->bi_acl_attribute;
586 bi_acl_attribute = backend_attribute;
589 rc = bi_acl_attribute( op, target,
590 entry_ndn, entry_at, vals, access );
592 /* should not fall thru this far without anything happening... */
593 if ( rc == SLAP_CB_CONTINUE ) {
594 /* access not allowed */
599 op->o_bd->bd_info = bi;
605 overlay_callback_after_backover( Operation *op, slap_callback *sc, int append )
609 for ( scp = &op->o_callback; *scp != NULL; scp = &(*scp)->sc_next ) {
610 if ( (*scp)->sc_response == over_back_response ) {
611 sc->sc_next = (*scp)->sc_next;
612 (*scp)->sc_next = sc;
626 * default return code in case of missing backend function
627 * and overlay stack returning SLAP_CB_CONTINUE
629 static int op_rc[ op_last ] = {
630 LDAP_UNWILLING_TO_PERFORM, /* bind */
631 LDAP_UNWILLING_TO_PERFORM, /* unbind */
632 LDAP_UNWILLING_TO_PERFORM, /* search */
633 SLAP_CB_CONTINUE, /* compare; pass to frontend */
634 LDAP_UNWILLING_TO_PERFORM, /* modify */
635 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
636 LDAP_UNWILLING_TO_PERFORM, /* add */
637 LDAP_UNWILLING_TO_PERFORM, /* delete */
638 LDAP_UNWILLING_TO_PERFORM, /* abandon */
639 LDAP_UNWILLING_TO_PERFORM, /* cancel */
640 LDAP_UNWILLING_TO_PERFORM, /* extended */
641 LDAP_SUCCESS, /* aux_operational */
642 LDAP_SUCCESS, /* aux_chk_referrals */
643 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
649 slap_operation_t which,
655 int rc = SLAP_CB_CONTINUE;
657 for (; on; on=on->on_next ) {
658 func = &on->on_bi.bi_op_bind;
660 op->o_bd->bd_info = (BackendInfo *)on;
661 rc = func[which]( op, rs );
662 if ( rc != SLAP_CB_CONTINUE ) break;
665 if ( rc == SLAP_CB_BYPASS )
666 rc = SLAP_CB_CONTINUE;
668 func = &oi->oi_orig->bi_op_bind;
669 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
670 op->o_bd->bd_info = oi->oi_orig;
671 rc = func[which]( op, rs );
673 /* should not fall thru this far without anything happening... */
674 if ( rc == SLAP_CB_CONTINUE ) {
678 /* The underlying backend didn't handle the request, make sure
679 * overlay cleanup is processed.
681 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
682 slap_callback *sc_next;
683 for ( ; op->o_callback && op->o_callback->sc_response !=
684 over_back_response; op->o_callback = sc_next ) {
685 sc_next = op->o_callback->sc_next;
686 if ( op->o_callback->sc_cleanup ) {
687 op->o_callback->sc_cleanup( op, rs );
698 slap_operation_t which
703 BackendDB *be = op->o_bd, db;
704 slap_callback cb = {NULL, over_back_response, NULL, NULL}, **sc;
705 int rc = SLAP_CB_CONTINUE;
707 /* FIXME: used to happen for instance during abandon
708 * when global overlays are used... */
709 assert( op->o_bd != NULL );
711 oi = op->o_bd->bd_info->bi_private;
714 if ( !SLAP_ISOVERLAY( op->o_bd )) {
716 db.be_flags |= SLAP_DBFLAG_OVERLAY;
719 cb.sc_next = op->o_callback;
721 op->o_callback = &cb;
723 rc = overlay_op_walk( op, rs, which, oi, on );
724 for ( sc = &op->o_callback; *sc; sc = &(*sc)->sc_next ) {
736 over_op_bind( Operation *op, SlapReply *rs )
738 return over_op_func( op, rs, op_bind );
742 over_op_unbind( Operation *op, SlapReply *rs )
744 return over_op_func( op, rs, op_unbind );
748 over_op_search( Operation *op, SlapReply *rs )
750 return over_op_func( op, rs, op_search );
754 over_op_compare( Operation *op, SlapReply *rs )
756 return over_op_func( op, rs, op_compare );
760 over_op_modify( Operation *op, SlapReply *rs )
762 return over_op_func( op, rs, op_modify );
766 over_op_modrdn( Operation *op, SlapReply *rs )
768 return over_op_func( op, rs, op_modrdn );
772 over_op_add( Operation *op, SlapReply *rs )
774 return over_op_func( op, rs, op_add );
778 over_op_delete( Operation *op, SlapReply *rs )
780 return over_op_func( op, rs, op_delete );
784 over_op_abandon( Operation *op, SlapReply *rs )
786 return over_op_func( op, rs, op_abandon );
790 over_op_cancel( Operation *op, SlapReply *rs )
792 return over_op_func( op, rs, op_cancel );
796 over_op_extended( Operation *op, SlapReply *rs )
798 return over_op_func( op, rs, op_extended );
802 over_aux_operational( Operation *op, SlapReply *rs )
804 return over_op_func( op, rs, op_aux_operational );
808 over_aux_chk_referrals( Operation *op, SlapReply *rs )
810 return over_op_func( op, rs, op_aux_chk_referrals );
814 over_aux_chk_controls( Operation *op, SlapReply *rs )
816 return over_op_func( op, rs, op_aux_chk_controls );
826 over_connection_func(
829 enum conn_which which
835 int rc = SLAP_CB_CONTINUE;
836 BI_connection_init **func;
838 /* FIXME: used to happen for instance during abandon
839 * when global overlays are used... */
840 assert( bd != NULL );
842 oi = bd->bd_info->bi_private;
845 if ( !SLAP_ISOVERLAY( bd ) ) {
847 db.be_flags |= SLAP_DBFLAG_OVERLAY;
851 for ( ; on; on = on->on_next ) {
852 func = &on->on_bi.bi_connection_init;
853 if ( func[ which ] ) {
854 bd->bd_info = (BackendInfo *)on;
855 rc = func[ which ]( bd, conn );
856 if ( rc != SLAP_CB_CONTINUE ) break;
860 func = &oi->oi_orig->bi_connection_init;
861 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
862 bd->bd_info = oi->oi_orig;
863 rc = func[ which ]( bd, conn );
865 /* should not fall thru this far without anything happening... */
866 if ( rc == SLAP_CB_CONTINUE ) {
867 rc = LDAP_UNWILLING_TO_PERFORM;
874 over_connection_init(
879 return over_connection_func( bd, conn, conn_init );
883 over_connection_destroy(
888 return over_connection_func( bd, conn, conn_destroy );
898 /* FIXME: check for duplicates? */
899 for ( tmp = overlays; tmp != NULL; tmp = tmp->on_next ) {
900 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_type ) == 0 ) {
901 Debug( LDAP_DEBUG_ANY,
902 "overlay_register(\"%s\"): "
903 "name already in use.\n",
904 on->on_bi.bi_type, 0, 0 );
908 if ( on->on_bi.bi_obsolete_names != NULL ) {
911 for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
912 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], tmp->on_bi.bi_type ) == 0 ) {
913 Debug( LDAP_DEBUG_ANY,
914 "overlay_register(\"%s\"): "
915 "obsolete name \"%s\" already in use "
916 "by overlay \"%s\".\n",
918 on->on_bi.bi_obsolete_names[ i ],
919 tmp->on_bi.bi_type );
925 if ( tmp->on_bi.bi_obsolete_names != NULL ) {
928 for ( i = 0; tmp->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
931 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
932 Debug( LDAP_DEBUG_ANY,
933 "overlay_register(\"%s\"): "
934 "name already in use "
935 "as obsolete by overlay \"%s\".\n",
937 tmp->on_bi.bi_obsolete_names[ i ], 0 );
941 if ( on->on_bi.bi_obsolete_names != NULL ) {
942 for ( j = 0; on->on_bi.bi_obsolete_names[ j ] != NULL; j++ ) {
943 if ( strcmp( on->on_bi.bi_obsolete_names[ j ], tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
944 Debug( LDAP_DEBUG_ANY,
945 "overlay_register(\"%s\"): "
946 "obsolete name \"%s\" already in use "
947 "as obsolete by overlay \"%s\".\n",
949 on->on_bi.bi_obsolete_names[ j ],
950 tmp->on_bi.bi_type );
959 on->on_next = overlays;
965 * iterator on registered overlays; overlay_next( NULL ) returns the first
966 * overlay; subsequent calls with the previously returned value allow to
967 * iterate over the entire list; returns NULL when no more overlays are
984 * returns a specific registered overlay based on the type; NULL if not
989 overlay_find( const char *over_type )
991 slap_overinst *on = overlays;
993 assert( over_type != NULL );
995 for ( ; on; on = on->on_next ) {
996 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
1000 if ( on->on_bi.bi_obsolete_names != NULL ) {
1003 for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
1004 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], over_type ) == 0 ) {
1005 Debug( LDAP_DEBUG_ANY,
1006 "overlay_find(\"%s\"): "
1007 "obsolete name for \"%s\".\n",
1008 on->on_bi.bi_obsolete_names[ i ],
1009 on->on_bi.bi_type, 0 );
1020 static const char overtype[] = "over";
1023 * returns TRUE (1) if the database is actually an overlay instance;
1024 * FALSE (0) otherwise.
1028 overlay_is_over( BackendDB *be )
1030 return be->bd_info->bi_type == overtype;
1034 * returns TRUE (1) if the given database is actually an overlay
1035 * instance and, somewhere in the list, contains the requested overlay;
1036 * FALSE (0) otherwise.
1040 overlay_is_inst( BackendDB *be, const char *over_type )
1044 assert( be != NULL );
1046 if ( !overlay_is_over( be ) ) {
1050 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
1051 for ( ; on; on = on->on_next ) {
1052 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
1061 overlay_register_control( BackendDB *be, const char *oid )
1066 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1070 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1073 /* add to all backends... */
1074 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1075 if ( bd == be->bd_self ) {
1079 /* overlays can be instanciated multiple times, use
1080 * be_ctrls[ cid ] as an instance counter, so that the
1081 * overlay's controls are only really disabled after the
1082 * last instance called overlay_register_control() */
1083 bd->be_ctrls[ cid ]++;
1084 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1090 /* overlays can be instanciated multiple times, use
1091 * be_ctrls[ cid ] as an instance counter, so that the
1092 * overlay's controls are only really unregistered after the
1093 * last instance called overlay_register_control() */
1094 be->bd_self->be_ctrls[ cid ]++;
1095 be->bd_self->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1101 #ifdef SLAP_CONFIG_DELETE
1103 overlay_unregister_control( BackendDB *be, const char *oid )
1108 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1112 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1115 /* remove from all backends... */
1116 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1117 if ( bd == be->bd_self ) {
1121 bd->be_ctrls[ cid ]--;
1126 be->bd_self->be_ctrls[ cid ]--;
1129 #endif /* SLAP_CONFIG_DELETE */
1132 overlay_destroy_one( BackendDB *be, slap_overinst *on )
1134 slap_overinfo *oi = on->on_info;
1135 slap_overinst **oidx;
1137 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1138 if ( *oidx == on ) {
1139 *oidx = on->on_next;
1140 if ( on->on_bi.bi_db_destroy ) {
1141 BackendInfo *bi_orig = be->bd_info;
1142 be->bd_info = (BackendInfo *)on;
1143 on->on_bi.bi_db_destroy( be, NULL );
1144 be->bd_info = bi_orig;
1152 #ifdef SLAP_CONFIG_DELETE
1153 typedef struct ov_remove_ctx {
1159 overlay_remove_cb( Operation *op, SlapReply *rs )
1161 ov_remove_ctx *rm_ctx = (ov_remove_ctx*) op->o_callback->sc_private;
1162 BackendInfo *bi_orig = rm_ctx->be->bd_info;
1164 rm_ctx->be->bd_info = (BackendInfo*) rm_ctx->on;
1166 if ( rm_ctx->on->on_bi.bi_db_close ) {
1167 rm_ctx->on->on_bi.bi_db_close( rm_ctx->be, NULL );
1169 if ( rm_ctx->on->on_bi.bi_db_destroy ) {
1170 rm_ctx->on->on_bi.bi_db_destroy( rm_ctx->be, NULL );
1173 /* clean up after removing last overlay */
1174 if ( ! rm_ctx->on->on_info->oi_list ) {
1175 /* reset db flags and bd_info to orig */
1176 SLAP_DBFLAGS( rm_ctx->be ) &= ~SLAP_DBFLAG_GLOBAL_OVERLAY;
1177 rm_ctx->be->bd_info = rm_ctx->on->on_info->oi_orig;
1178 ch_free(rm_ctx->on->on_info);
1180 rm_ctx->be->bd_info = bi_orig;
1183 op->o_tmpfree(rm_ctx, op->o_tmpmemctx);
1184 return SLAP_CB_CONTINUE;
1188 overlay_remove( BackendDB *be, slap_overinst *on, Operation *op )
1190 slap_overinfo *oi = on->on_info;
1191 slap_overinst **oidx;
1192 ov_remove_ctx *rm_ctx;
1193 slap_callback *rm_cb, *cb;
1195 /* remove overlay from oi_list */
1196 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1197 if ( *oidx == on ) {
1198 *oidx = on->on_next;
1203 /* The db_close and db_destroy handlers to cleanup a release
1204 * the overlay's resources are called from the cleanup callback
1206 rm_ctx = op->o_tmpalloc( sizeof( ov_remove_ctx ), op->o_tmpmemctx );
1210 rm_cb = op->o_tmpalloc( sizeof( slap_callback ), op->o_tmpmemctx );
1211 rm_cb->sc_next = NULL;
1212 rm_cb->sc_cleanup = overlay_remove_cb;
1213 rm_cb->sc_response = NULL;
1214 rm_cb->sc_private = (void*) rm_ctx;
1216 /* Append callback to the end of the list */
1217 for ( cb = op->o_callback; cb->sc_next; cb = cb->sc_next );
1218 cb->sc_next = rm_cb;
1220 #endif /* SLAP_CONFIG_DELETE */
1223 overlay_insert( BackendDB *be, slap_overinst *on2, slap_overinst ***prev,
1226 slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1229 on2->on_next = oi->oi_list;
1233 slap_overinst *on, **prev;
1235 /* Since the list is in reverse order and is singly linked,
1236 * we have to count the overlays and then insert backwards.
1237 * Adding on overlay at a specific point should be a pretty
1238 * infrequent occurrence.
1241 for ( on = oi->oi_list; on; on=on->on_next )
1249 /* advance to insertion point */
1250 prev = &oi->oi_list;
1251 for ( i=0; i<idx; i++ ) {
1253 prev = &on->on_next;
1256 on2->on_next = *prev;
1262 overlay_move( BackendDB *be, slap_overinst *on, int idx )
1264 slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1265 slap_overinst **onp;
1267 for (onp = &oi->oi_list; *onp; onp= &(*onp)->on_next) {
1273 overlay_insert( be, on, &onp, idx );
1276 /* add an overlay to a particular backend. */
1278 overlay_config( BackendDB *be, const char *ov, int idx, BackendInfo **res, ConfigReply *cr )
1280 slap_overinst *on = NULL, *on2 = NULL, **prev;
1281 slap_overinfo *oi = NULL;
1282 BackendInfo *bi = NULL;
1287 on = overlay_find( ov );
1289 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
1293 /* If this is the first overlay on this backend, set up the
1294 * overlay info structure
1296 if ( !overlay_is_over( be ) ) {
1299 /* NOTE: the first time a global overlay is configured,
1300 * frontendDB gets this flag; it is used later by overlays
1301 * to determine if they're stacked on top of the frontendDB */
1302 if ( be->bd_info == frontendDB->bd_info || SLAP_ISGLOBALOVERLAY( be ) ) {
1304 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DBONLY ) {
1305 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1306 "overlay \"%s\" cannot be global.\n",
1311 } else if ( on->on_bi.bi_flags & SLAPO_BFLAG_GLOBONLY ) {
1312 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1313 "overlay \"%s\" can only be global.\n",
1318 oi = ch_malloc( sizeof( slap_overinfo ) );
1319 oi->oi_orig = be->bd_info;
1320 oi->oi_bi = *be->bd_info;
1324 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
1327 /* Save a pointer to ourself in bi_private.
1329 oi->oi_bi.bi_private = oi;
1331 bi = (BackendInfo *)oi;
1333 bi->bi_type = (char *)overtype;
1335 bi->bi_db_config = over_db_config;
1336 bi->bi_db_open = over_db_open;
1337 bi->bi_db_close = over_db_close;
1338 bi->bi_db_destroy = over_db_destroy;
1340 bi->bi_op_bind = over_op_bind;
1341 bi->bi_op_unbind = over_op_unbind;
1342 bi->bi_op_search = over_op_search;
1343 bi->bi_op_compare = over_op_compare;
1344 bi->bi_op_modify = over_op_modify;
1345 bi->bi_op_modrdn = over_op_modrdn;
1346 bi->bi_op_add = over_op_add;
1347 bi->bi_op_delete = over_op_delete;
1348 bi->bi_op_abandon = over_op_abandon;
1349 bi->bi_op_cancel = over_op_cancel;
1351 bi->bi_extended = over_op_extended;
1354 * this is fine because it has the same
1355 * args of the operations; we need to rework
1356 * all the hooks to share the same args
1357 * of the operations...
1359 bi->bi_operational = over_aux_operational;
1360 bi->bi_chk_referrals = over_aux_chk_referrals;
1361 bi->bi_chk_controls = over_aux_chk_controls;
1363 /* these have specific arglists */
1364 bi->bi_entry_get_rw = over_entry_get_rw;
1365 bi->bi_entry_release_rw = over_entry_release_rw;
1366 bi->bi_access_allowed = over_access_allowed;
1367 bi->bi_acl_group = over_acl_group;
1368 bi->bi_acl_attribute = over_acl_attribute;
1370 bi->bi_connection_init = over_connection_init;
1371 bi->bi_connection_destroy = over_connection_destroy;
1376 if ( overlay_is_inst( be, ov ) ) {
1377 if ( SLAPO_SINGLE( be ) ) {
1378 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1379 "overlay \"%s\" already in list\n",
1385 oi = be->bd_info->bi_private;
1388 /* Insert new overlay into list. By default overlays are
1389 * added to head of list and executed in LIFO order.
1391 on2 = ch_calloc( 1, sizeof(slap_overinst) );
1395 prev = &oi->oi_list;
1396 /* Do we need to find the insertion point? */
1400 /* count current overlays */
1401 for ( i=0, on=oi->oi_list; on; on=on->on_next, i++ );
1403 /* are we just appending a new one? */
1407 overlay_insert( be, on2, &prev, idx );
1409 /* Any initialization needed? */
1410 if ( on2->on_bi.bi_db_init ) {
1412 be->bd_info = (BackendInfo *)on2;
1413 rc = on2->on_bi.bi_db_init( be, cr);
1414 be->bd_info = (BackendInfo *)oi;
1416 *prev = on2->on_next;