1 /* backover.c - backend overlay routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2017 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 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
155 db.bd_info = &on->on_bi;
156 if ( db.bd_info->bi_db_open ) {
157 rc = db.bd_info->bi_db_open( &db, cr );
170 slap_overinfo *oi = be->bd_info->bi_private;
171 slap_overinst *on = oi->oi_list;
172 BackendInfo *bi_orig = be->bd_info;
175 for (; on && rc == 0; on=on->on_next) {
176 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
178 be->bd_info = &on->on_bi;
179 if ( be->bd_info->bi_db_close ) {
180 rc = be->bd_info->bi_db_close( be, cr );
184 if ( oi->oi_orig->bi_db_close ) {
185 be->bd_info = oi->oi_orig;
186 rc = be->bd_info->bi_db_close( be, cr );
189 be->bd_info = bi_orig;
199 slap_overinfo *oi = be->bd_info->bi_private;
200 slap_overinst *on = oi->oi_list, *next;
201 BackendInfo *bi_orig = be->bd_info;
204 be->bd_info = oi->oi_orig;
205 if ( be->bd_info->bi_db_destroy ) {
206 rc = be->bd_info->bi_db_destroy( be, cr );
209 for (; on && rc == 0; on=on->on_next) {
210 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
212 be->bd_info = &on->on_bi;
213 if ( be->bd_info->bi_db_destroy ) {
214 rc = be->bd_info->bi_db_destroy( be, cr );
220 for (next = on->on_next; on; on=next) {
225 be->bd_info = bi_orig;
231 over_back_response ( Operation *op, SlapReply *rs )
233 slap_overinfo *oi = op->o_callback->sc_private;
234 slap_overinst *on = oi->oi_list;
235 int rc = SLAP_CB_CONTINUE;
236 BackendDB *be = op->o_bd, db = *op->o_bd;
238 db.be_flags |= SLAP_DBFLAG_OVERLAY;
240 for (; on; on=on->on_next ) {
241 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
243 if ( on->on_response ) {
244 db.bd_info = (BackendInfo *)on;
245 rc = on->on_response( op, rs );
246 if ( rc != SLAP_CB_CONTINUE ) break;
249 /* Bypass the remaining on_response layers, but allow
250 * normal execution to continue.
252 if ( rc == SLAP_CB_BYPASS )
253 rc = SLAP_CB_CONTINUE;
262 AttributeDescription *desc,
264 slap_access_t access,
265 AccessControlState *state,
271 BackendDB *be = op->o_bd, db;
272 int rc = SLAP_CB_CONTINUE;
274 /* FIXME: used to happen for instance during abandon
275 * when global overlays are used... */
276 assert( op->o_bd != NULL );
278 bi = op->o_bd->bd_info;
279 /* Were we invoked on the frontend? */
280 if ( !bi->bi_access_allowed ) {
281 oi = frontendDB->bd_info->bi_private;
283 oi = op->o_bd->bd_info->bi_private;
287 for ( ; on; on = on->on_next ) {
288 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
290 if ( on->on_bi.bi_access_allowed ) {
291 /* NOTE: do not copy the structure until required */
292 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
294 db.be_flags |= SLAP_DBFLAG_OVERLAY;
298 op->o_bd->bd_info = (BackendInfo *)on;
299 rc = on->on_bi.bi_access_allowed( op, e,
300 desc, val, access, state, maskp );
301 if ( rc != SLAP_CB_CONTINUE ) break;
305 if ( rc == SLAP_CB_CONTINUE ) {
306 BI_access_allowed *bi_access_allowed;
308 /* if the database structure was changed, o_bd points to a
309 * copy of the structure; put the original bd_info in place */
310 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
311 op->o_bd->bd_info = oi->oi_orig;
314 if ( oi->oi_orig->bi_access_allowed ) {
315 bi_access_allowed = oi->oi_orig->bi_access_allowed;
317 bi_access_allowed = slap_access_allowed;
320 rc = bi_access_allowed( op, e,
321 desc, val, access, state, maskp );
323 /* should not fall thru this far without anything happening... */
324 if ( rc == SLAP_CB_CONTINUE ) {
325 /* access not allowed */
330 op->o_bd->bd_info = bi;
336 overlay_entry_get_ov(
340 AttributeDescription *ad,
345 slap_overinfo *oi = on->on_info;
346 BackendDB *be = op->o_bd, db;
347 BackendInfo *bi = op->o_bd->bd_info;
348 int rc = SLAP_CB_CONTINUE;
350 for ( ; on; on = on->on_next ) {
351 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
353 if ( on->on_bi.bi_entry_get_rw ) {
354 /* NOTE: do not copy the structure until required */
355 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
357 db.be_flags |= SLAP_DBFLAG_OVERLAY;
361 op->o_bd->bd_info = (BackendInfo *)on;
362 rc = on->on_bi.bi_entry_get_rw( op, dn,
364 if ( rc != SLAP_CB_CONTINUE ) break;
368 if ( rc == SLAP_CB_CONTINUE ) {
369 /* if the database structure was changed, o_bd points to a
370 * copy of the structure; put the original bd_info in place */
371 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
372 op->o_bd->bd_info = oi->oi_orig;
375 if ( oi->oi_orig->bi_entry_get_rw ) {
376 rc = oi->oi_orig->bi_entry_get_rw( op, dn,
380 /* should not fall thru this far without anything happening... */
381 if ( rc == SLAP_CB_CONTINUE ) {
382 rc = LDAP_UNWILLING_TO_PERFORM;
386 op->o_bd->bd_info = bi;
396 AttributeDescription *ad,
403 assert( op->o_bd != NULL );
405 oi = op->o_bd->bd_info->bi_private;
408 return overlay_entry_get_ov( op, dn, oc, ad, rw, e, on );
412 overlay_entry_release_ov(
418 slap_overinfo *oi = on->on_info;
419 BackendDB *be = op->o_bd, db;
420 BackendInfo *bi = op->o_bd->bd_info;
421 int rc = SLAP_CB_CONTINUE;
423 for ( ; on; on = on->on_next ) {
424 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
426 if ( on->on_bi.bi_entry_release_rw ) {
427 /* NOTE: do not copy the structure until required */
428 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
430 db.be_flags |= SLAP_DBFLAG_OVERLAY;
434 op->o_bd->bd_info = (BackendInfo *)on;
435 rc = on->on_bi.bi_entry_release_rw( op, e, rw );
436 if ( rc != SLAP_CB_CONTINUE ) break;
440 if ( rc == SLAP_CB_CONTINUE ) {
441 /* if the database structure was changed, o_bd points to a
442 * copy of the structure; put the original bd_info in place */
443 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
444 op->o_bd->bd_info = oi->oi_orig;
447 if ( oi->oi_orig->bi_entry_release_rw ) {
448 rc = oi->oi_orig->bi_entry_release_rw( op, e, rw );
451 /* should not fall thru this far without anything happening... */
452 if ( rc == SLAP_CB_CONTINUE ) {
458 op->o_bd->bd_info = bi;
464 over_entry_release_rw(
472 assert( op->o_bd != NULL );
474 oi = op->o_bd->bd_info->bi_private;
477 return overlay_entry_release_ov( op, e, rw, on );
484 struct berval *gr_ndn,
485 struct berval *op_ndn,
486 ObjectClass *group_oc,
487 AttributeDescription *group_at )
492 BackendDB *be = op->o_bd, db;
493 int rc = SLAP_CB_CONTINUE;
495 /* FIXME: used to happen for instance during abandon
496 * when global overlays are used... */
497 assert( be != NULL );
503 for ( ; on; on = on->on_next ) {
504 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
506 if ( on->on_bi.bi_acl_group ) {
507 /* NOTE: do not copy the structure until required */
508 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
510 db.be_flags |= SLAP_DBFLAG_OVERLAY;
514 op->o_bd->bd_info = (BackendInfo *)on;
515 rc = on->on_bi.bi_acl_group( op, e,
516 gr_ndn, op_ndn, group_oc, group_at );
517 if ( rc != SLAP_CB_CONTINUE ) break;
521 if ( rc == SLAP_CB_CONTINUE ) {
522 BI_acl_group *bi_acl_group;
524 /* if the database structure was changed, o_bd points to a
525 * copy of the structure; put the original bd_info in place */
526 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
527 op->o_bd->bd_info = oi->oi_orig;
530 if ( oi->oi_orig->bi_acl_group ) {
531 bi_acl_group = oi->oi_orig->bi_acl_group;
533 bi_acl_group = backend_group;
536 rc = bi_acl_group( op, e,
537 gr_ndn, op_ndn, group_oc, group_at );
539 /* should not fall thru this far without anything happening... */
540 if ( rc == SLAP_CB_CONTINUE ) {
541 /* access not allowed */
546 op->o_bd->bd_info = bi;
555 struct berval *entry_ndn,
556 AttributeDescription *entry_at,
558 slap_access_t access )
563 BackendDB *be = op->o_bd, db;
564 int rc = SLAP_CB_CONTINUE;
566 /* FIXME: used to happen for instance during abandon
567 * when global overlays are used... */
568 assert( be != NULL );
574 for ( ; on; on = on->on_next ) {
575 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
577 if ( on->on_bi.bi_acl_attribute ) {
578 /* NOTE: do not copy the structure until required */
579 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
581 db.be_flags |= SLAP_DBFLAG_OVERLAY;
585 op->o_bd->bd_info = (BackendInfo *)on;
586 rc = on->on_bi.bi_acl_attribute( op, target,
587 entry_ndn, entry_at, vals, access );
588 if ( rc != SLAP_CB_CONTINUE ) break;
592 if ( rc == SLAP_CB_CONTINUE ) {
593 BI_acl_attribute *bi_acl_attribute;
595 /* if the database structure was changed, o_bd points to a
596 * copy of the structure; put the original bd_info in place */
597 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
598 op->o_bd->bd_info = oi->oi_orig;
601 if ( oi->oi_orig->bi_acl_attribute ) {
602 bi_acl_attribute = oi->oi_orig->bi_acl_attribute;
604 bi_acl_attribute = backend_attribute;
607 rc = bi_acl_attribute( op, target,
608 entry_ndn, entry_at, vals, access );
610 /* should not fall thru this far without anything happening... */
611 if ( rc == SLAP_CB_CONTINUE ) {
612 /* access not allowed */
617 op->o_bd->bd_info = bi;
623 overlay_callback_after_backover( Operation *op, slap_callback *sc, int append )
627 for ( scp = &op->o_callback; *scp != NULL; scp = &(*scp)->sc_next ) {
628 if ( (*scp)->sc_response == over_back_response ) {
629 sc->sc_next = (*scp)->sc_next;
630 (*scp)->sc_next = sc;
644 * default return code in case of missing backend function
645 * and overlay stack returning SLAP_CB_CONTINUE
647 static int op_rc[ op_last ] = {
648 LDAP_UNWILLING_TO_PERFORM, /* bind */
649 LDAP_UNWILLING_TO_PERFORM, /* unbind */
650 LDAP_UNWILLING_TO_PERFORM, /* search */
651 SLAP_CB_CONTINUE, /* compare; pass to frontend */
652 LDAP_UNWILLING_TO_PERFORM, /* modify */
653 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
654 LDAP_UNWILLING_TO_PERFORM, /* add */
655 LDAP_UNWILLING_TO_PERFORM, /* delete */
656 LDAP_UNWILLING_TO_PERFORM, /* abandon */
657 LDAP_UNWILLING_TO_PERFORM, /* cancel */
658 LDAP_UNWILLING_TO_PERFORM, /* extended */
659 LDAP_SUCCESS, /* aux_operational */
660 LDAP_SUCCESS, /* aux_chk_referrals */
661 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
667 slap_operation_t which,
673 int rc = SLAP_CB_CONTINUE;
675 for (; on; on=on->on_next ) {
676 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
679 if ( (&bi->bi_op_bind)[ which ] ) {
680 op->o_bd->bd_info = (BackendInfo *)on;
681 rc = (&bi->bi_op_bind)[ which ]( op, rs );
682 if ( rc != SLAP_CB_CONTINUE ) break;
685 if ( rc == SLAP_CB_BYPASS )
686 rc = SLAP_CB_CONTINUE;
687 /* if an overlay halted processing, make sure
688 * any previously set cleanup handlers are run
690 if ( rc != SLAP_CB_CONTINUE )
694 if ( (&bi->bi_op_bind)[ which ] ) {
695 op->o_bd->bd_info = bi;
696 rc = (&bi->bi_op_bind)[ which ]( op, rs );
698 /* should not fall thru this far without anything happening... */
699 if ( rc == SLAP_CB_CONTINUE ) {
703 /* The underlying backend didn't handle the request, make sure
704 * overlay cleanup is processed.
706 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
707 slap_callback *sc_next;
709 for ( ; op->o_callback && op->o_callback->sc_response !=
710 over_back_response; op->o_callback = sc_next ) {
711 sc_next = op->o_callback->sc_next;
712 if ( op->o_callback->sc_cleanup ) {
713 op->o_callback->sc_cleanup( op, rs );
724 slap_operation_t which
729 BackendDB *be = op->o_bd, db;
731 slap_callback *cb = (slap_callback *) ch_malloc( sizeof( slap_callback ));
732 int rc = SLAP_CB_CONTINUE;
734 /* FIXME: used to happen for instance during abandon
735 * when global overlays are used... */
736 assert( op->o_bd != NULL );
738 oi = op->o_bd->bd_info->bi_private;
741 if ( !SLAP_ISOVERLAY( op->o_bd )) {
743 db.be_flags |= SLAP_DBFLAG_OVERLAY;
746 cb->sc_cleanup = NULL;
747 cb->sc_response = over_back_response;
748 cb->sc_writewait = NULL;
749 cb->sc_next = op->o_callback;
753 rc = overlay_op_walk( op, rs, which, oi, on );
754 for ( sc = &op->o_callback; *sc; sc = &(*sc)->sc_next ) {
767 over_op_bind( Operation *op, SlapReply *rs )
769 return over_op_func( op, rs, op_bind );
773 over_op_unbind( Operation *op, SlapReply *rs )
775 return over_op_func( op, rs, op_unbind );
779 over_op_search( Operation *op, SlapReply *rs )
781 return over_op_func( op, rs, op_search );
785 over_op_compare( Operation *op, SlapReply *rs )
787 return over_op_func( op, rs, op_compare );
791 over_op_modify( Operation *op, SlapReply *rs )
793 return over_op_func( op, rs, op_modify );
797 over_op_modrdn( Operation *op, SlapReply *rs )
799 return over_op_func( op, rs, op_modrdn );
803 over_op_add( Operation *op, SlapReply *rs )
805 return over_op_func( op, rs, op_add );
809 over_op_delete( Operation *op, SlapReply *rs )
811 return over_op_func( op, rs, op_delete );
815 over_op_abandon( Operation *op, SlapReply *rs )
817 return over_op_func( op, rs, op_abandon );
821 over_op_cancel( Operation *op, SlapReply *rs )
823 return over_op_func( op, rs, op_cancel );
827 over_op_extended( Operation *op, SlapReply *rs )
829 return over_op_func( op, rs, op_extended );
833 over_aux_operational( Operation *op, SlapReply *rs )
835 return over_op_func( op, rs, op_aux_operational );
839 over_aux_chk_referrals( Operation *op, SlapReply *rs )
841 return over_op_func( op, rs, op_aux_chk_referrals );
845 over_aux_chk_controls( Operation *op, SlapReply *rs )
847 return over_op_func( op, rs, op_aux_chk_controls );
857 over_connection_func(
860 enum conn_which which
866 int rc = SLAP_CB_CONTINUE;
867 BI_connection_init **func;
869 /* FIXME: used to happen for instance during abandon
870 * when global overlays are used... */
871 assert( bd != NULL );
873 oi = bd->bd_info->bi_private;
876 if ( !SLAP_ISOVERLAY( bd ) ) {
878 db.be_flags |= SLAP_DBFLAG_OVERLAY;
882 for ( ; on; on = on->on_next ) {
883 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
885 func = &on->on_bi.bi_connection_init;
886 if ( func[ which ] ) {
887 bd->bd_info = (BackendInfo *)on;
888 rc = func[ which ]( bd, conn );
889 if ( rc != SLAP_CB_CONTINUE ) break;
893 func = &oi->oi_orig->bi_connection_init;
894 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
895 bd->bd_info = oi->oi_orig;
896 rc = func[ which ]( bd, conn );
898 /* should not fall thru this far without anything happening... */
899 if ( rc == SLAP_CB_CONTINUE ) {
900 rc = LDAP_UNWILLING_TO_PERFORM;
907 over_connection_init(
912 return over_connection_func( bd, conn, conn_init );
916 over_connection_destroy(
921 return over_connection_func( bd, conn, conn_destroy );
931 /* FIXME: check for duplicates? */
932 for ( tmp = overlays; tmp != NULL; tmp = tmp->on_next ) {
933 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_type ) == 0 ) {
934 Debug( LDAP_DEBUG_ANY,
935 "overlay_register(\"%s\"): "
936 "name already in use.\n",
937 on->on_bi.bi_type, 0, 0 );
941 if ( on->on_bi.bi_obsolete_names != NULL ) {
944 for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
945 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], tmp->on_bi.bi_type ) == 0 ) {
946 Debug( LDAP_DEBUG_ANY,
947 "overlay_register(\"%s\"): "
948 "obsolete name \"%s\" already in use "
949 "by overlay \"%s\".\n",
951 on->on_bi.bi_obsolete_names[ i ],
952 tmp->on_bi.bi_type );
958 if ( tmp->on_bi.bi_obsolete_names != NULL ) {
961 for ( i = 0; tmp->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
964 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
965 Debug( LDAP_DEBUG_ANY,
966 "overlay_register(\"%s\"): "
967 "name already in use "
968 "as obsolete by overlay \"%s\".\n",
970 tmp->on_bi.bi_obsolete_names[ i ], 0 );
974 if ( on->on_bi.bi_obsolete_names != NULL ) {
975 for ( j = 0; on->on_bi.bi_obsolete_names[ j ] != NULL; j++ ) {
976 if ( strcmp( on->on_bi.bi_obsolete_names[ j ], tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
977 Debug( LDAP_DEBUG_ANY,
978 "overlay_register(\"%s\"): "
979 "obsolete name \"%s\" already in use "
980 "as obsolete by overlay \"%s\".\n",
982 on->on_bi.bi_obsolete_names[ j ],
983 tmp->on_bi.bi_type );
992 on->on_next = overlays;
998 * iterator on registered overlays; overlay_next( NULL ) returns the first
999 * overlay; subsequent calls with the previously returned value allow to
1000 * iterate over the entire list; returns NULL when no more overlays are
1017 * returns a specific registered overlay based on the type; NULL if not
1022 overlay_find( const char *over_type )
1024 slap_overinst *on = overlays;
1026 assert( over_type != NULL );
1028 for ( ; on; on = on->on_next ) {
1029 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
1033 if ( on->on_bi.bi_obsolete_names != NULL ) {
1036 for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
1037 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], over_type ) == 0 ) {
1038 Debug( LDAP_DEBUG_ANY,
1039 "overlay_find(\"%s\"): "
1040 "obsolete name for \"%s\".\n",
1041 on->on_bi.bi_obsolete_names[ i ],
1042 on->on_bi.bi_type, 0 );
1053 static const char overtype[] = "over";
1056 * returns TRUE (1) if the database is actually an overlay instance;
1057 * FALSE (0) otherwise.
1061 overlay_is_over( BackendDB *be )
1063 return be->bd_info->bi_type == overtype;
1067 * returns TRUE (1) if the given database is actually an overlay
1068 * instance and, somewhere in the list, contains the requested overlay;
1069 * FALSE (0) otherwise.
1073 overlay_is_inst( BackendDB *be, const char *over_type )
1077 assert( be != NULL );
1079 if ( !overlay_is_over( be ) ) {
1083 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
1084 for ( ; on; on = on->on_next ) {
1085 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
1094 overlay_register_control( BackendDB *be, const char *oid )
1099 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1103 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1106 /* add to all backends... */
1107 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1108 if ( bd == be->bd_self ) {
1112 /* overlays can be instanciated multiple times, use
1113 * be_ctrls[ cid ] as an instance counter, so that the
1114 * overlay's controls are only really disabled after the
1115 * last instance called overlay_register_control() */
1116 bd->be_ctrls[ cid ]++;
1117 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1123 /* overlays can be instanciated multiple times, use
1124 * be_ctrls[ cid ] as an instance counter, so that the
1125 * overlay's controls are only really unregistered after the
1126 * last instance called overlay_register_control() */
1127 be->bd_self->be_ctrls[ cid ]++;
1128 be->bd_self->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1134 #ifdef SLAP_CONFIG_DELETE
1136 overlay_unregister_control( BackendDB *be, const char *oid )
1141 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1145 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1148 /* remove from all backends... */
1149 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1150 if ( bd == be->bd_self ) {
1154 bd->be_ctrls[ cid ]--;
1159 be->bd_self->be_ctrls[ cid ]--;
1162 #endif /* SLAP_CONFIG_DELETE */
1165 overlay_destroy_one( BackendDB *be, slap_overinst *on )
1167 slap_overinfo *oi = on->on_info;
1168 slap_overinst **oidx;
1170 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1171 if ( *oidx == on ) {
1172 *oidx = on->on_next;
1173 if ( on->on_bi.bi_db_destroy ) {
1174 BackendInfo *bi_orig = be->bd_info;
1175 be->bd_info = (BackendInfo *)on;
1176 on->on_bi.bi_db_destroy( be, NULL );
1177 be->bd_info = bi_orig;
1185 #ifdef SLAP_CONFIG_DELETE
1186 typedef struct ov_remove_ctx {
1192 overlay_remove_cb( Operation *op, SlapReply *rs )
1194 ov_remove_ctx *rm_ctx = (ov_remove_ctx*) op->o_callback->sc_private;
1195 BackendInfo *bi_orig = rm_ctx->be->bd_info;
1197 rm_ctx->be->bd_info = (BackendInfo*) rm_ctx->on;
1199 if ( rm_ctx->on->on_bi.bi_db_close ) {
1200 rm_ctx->on->on_bi.bi_db_close( rm_ctx->be, NULL );
1202 if ( rm_ctx->on->on_bi.bi_db_destroy ) {
1203 rm_ctx->on->on_bi.bi_db_destroy( rm_ctx->be, NULL );
1206 /* clean up after removing last overlay */
1207 if ( ! rm_ctx->on->on_info->oi_list ) {
1208 /* reset db flags and bd_info to orig */
1209 SLAP_DBFLAGS( rm_ctx->be ) &= ~SLAP_DBFLAG_GLOBAL_OVERLAY;
1210 rm_ctx->be->bd_info = rm_ctx->on->on_info->oi_orig;
1211 ch_free(rm_ctx->on->on_info);
1213 rm_ctx->be->bd_info = bi_orig;
1216 op->o_tmpfree(rm_ctx, op->o_tmpmemctx);
1217 return SLAP_CB_CONTINUE;
1221 overlay_remove( BackendDB *be, slap_overinst *on, Operation *op )
1223 slap_overinfo *oi = on->on_info;
1224 slap_overinst **oidx;
1225 ov_remove_ctx *rm_ctx;
1226 slap_callback *rm_cb, *cb;
1228 /* remove overlay from oi_list */
1229 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1230 if ( *oidx == on ) {
1231 *oidx = on->on_next;
1236 /* The db_close and db_destroy handlers to cleanup a release
1237 * the overlay's resources are called from the cleanup callback
1239 rm_ctx = op->o_tmpalloc( sizeof( ov_remove_ctx ), op->o_tmpmemctx );
1243 rm_cb = op->o_tmpalloc( sizeof( slap_callback ), op->o_tmpmemctx );
1244 rm_cb->sc_next = NULL;
1245 rm_cb->sc_cleanup = overlay_remove_cb;
1246 rm_cb->sc_response = NULL;
1247 rm_cb->sc_private = (void*) rm_ctx;
1248 rm_cb->sc_writewait = NULL;
1250 /* Append callback to the end of the list */
1251 if ( !op->o_callback ) {
1252 op->o_callback = rm_cb;
1254 for ( cb = op->o_callback; cb->sc_next; cb = cb->sc_next );
1255 cb->sc_next = rm_cb;
1258 #endif /* SLAP_CONFIG_DELETE */
1261 overlay_insert( BackendDB *be, slap_overinst *on2, slap_overinst ***prev,
1264 slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1267 on2->on_next = oi->oi_list;
1271 slap_overinst *on, **prev;
1273 /* Since the list is in reverse order and is singly linked,
1274 * we have to count the overlays and then insert backwards.
1275 * Adding on overlay at a specific point should be a pretty
1276 * infrequent occurrence.
1279 for ( on = oi->oi_list; on; on=on->on_next )
1287 /* advance to insertion point */
1288 prev = &oi->oi_list;
1289 for ( i=0; i<idx; i++ ) {
1291 prev = &on->on_next;
1294 on2->on_next = *prev;
1300 overlay_move( BackendDB *be, slap_overinst *on, int idx )
1302 slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1303 slap_overinst **onp;
1305 for (onp = &oi->oi_list; *onp; onp= &(*onp)->on_next) {
1311 overlay_insert( be, on, &onp, idx );
1314 /* add an overlay to a particular backend. */
1316 overlay_config( BackendDB *be, const char *ov, int idx, BackendInfo **res, ConfigReply *cr )
1318 slap_overinst *on = NULL, *on2 = NULL, **prev;
1319 slap_overinfo *oi = NULL;
1320 BackendInfo *bi = NULL;
1325 on = overlay_find( ov );
1327 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
1331 /* If this is the first overlay on this backend, set up the
1332 * overlay info structure
1334 if ( !overlay_is_over( be ) ) {
1337 /* NOTE: the first time a global overlay is configured,
1338 * frontendDB gets this flag; it is used later by overlays
1339 * to determine if they're stacked on top of the frontendDB */
1340 if ( be->bd_info == frontendDB->bd_info || SLAP_ISGLOBALOVERLAY( be ) ) {
1342 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DBONLY ) {
1343 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1344 "overlay \"%s\" cannot be global.\n",
1349 } else if ( on->on_bi.bi_flags & SLAPO_BFLAG_GLOBONLY ) {
1350 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1351 "overlay \"%s\" can only be global.\n",
1356 oi = ch_malloc( sizeof( slap_overinfo ) );
1357 oi->oi_orig = be->bd_info;
1358 oi->oi_bi = *be->bd_info;
1362 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
1365 /* Save a pointer to ourself in bi_private.
1367 oi->oi_bi.bi_private = oi;
1369 bi = (BackendInfo *)oi;
1371 bi->bi_type = (char *)overtype;
1373 bi->bi_db_config = over_db_config;
1374 bi->bi_db_open = over_db_open;
1375 bi->bi_db_close = over_db_close;
1376 bi->bi_db_destroy = over_db_destroy;
1378 bi->bi_op_bind = over_op_bind;
1379 bi->bi_op_unbind = over_op_unbind;
1380 bi->bi_op_search = over_op_search;
1381 bi->bi_op_compare = over_op_compare;
1382 bi->bi_op_modify = over_op_modify;
1383 bi->bi_op_modrdn = over_op_modrdn;
1384 bi->bi_op_add = over_op_add;
1385 bi->bi_op_delete = over_op_delete;
1386 bi->bi_op_abandon = over_op_abandon;
1387 bi->bi_op_cancel = over_op_cancel;
1389 bi->bi_extended = over_op_extended;
1392 * this is fine because it has the same
1393 * args of the operations; we need to rework
1394 * all the hooks to share the same args
1395 * of the operations...
1397 bi->bi_operational = over_aux_operational;
1398 bi->bi_chk_referrals = over_aux_chk_referrals;
1399 bi->bi_chk_controls = over_aux_chk_controls;
1401 /* these have specific arglists */
1402 bi->bi_entry_get_rw = over_entry_get_rw;
1403 bi->bi_entry_release_rw = over_entry_release_rw;
1404 bi->bi_access_allowed = over_access_allowed;
1405 bi->bi_acl_group = over_acl_group;
1406 bi->bi_acl_attribute = over_acl_attribute;
1408 bi->bi_connection_init = over_connection_init;
1409 bi->bi_connection_destroy = over_connection_destroy;
1414 if ( overlay_is_inst( be, ov ) ) {
1415 if ( SLAPO_SINGLE( be ) ) {
1416 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1417 "overlay \"%s\" already in list\n",
1423 oi = be->bd_info->bi_private;
1426 /* Insert new overlay into list. By default overlays are
1427 * added to head of list and executed in LIFO order.
1429 on2 = ch_calloc( 1, sizeof(slap_overinst) );
1433 prev = &oi->oi_list;
1434 /* Do we need to find the insertion point? */
1438 /* count current overlays */
1439 for ( i=0, on=oi->oi_list; on; on=on->on_next, i++ );
1441 /* are we just appending a new one? */
1445 overlay_insert( be, on2, &prev, idx );
1447 /* Any initialization needed? */
1448 if ( on2->on_bi.bi_db_init ) {
1450 be->bd_info = (BackendInfo *)on2;
1451 rc = on2->on_bi.bi_db_init( be, cr);
1452 be->bd_info = (BackendInfo *)oi;
1454 *prev = on2->on_next;