1 /* backover.c - backend overlay routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2016 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;
259 over_back_response_cleanup(Operation *op, SlapReply *rs)
261 if (rs->sr_type == REP_RESULT) {
262 if (op->o_callback != NULL) {
263 slap_callback *sc = op->o_callback;
264 op->o_callback = sc->sc_next;
277 AttributeDescription *desc,
279 slap_access_t access,
280 AccessControlState *state,
286 BackendDB *be = op->o_bd, db;
287 int rc = SLAP_CB_CONTINUE;
289 /* FIXME: used to happen for instance during abandon
290 * when global overlays are used... */
291 assert( op->o_bd != NULL );
293 bi = op->o_bd->bd_info;
294 /* Were we invoked on the frontend? */
295 if ( !bi->bi_access_allowed ) {
296 oi = frontendDB->bd_info->bi_private;
298 oi = op->o_bd->bd_info->bi_private;
302 for ( ; on; on = on->on_next ) {
303 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
305 if ( on->on_bi.bi_access_allowed ) {
306 /* NOTE: do not copy the structure until required */
307 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
309 db.be_flags |= SLAP_DBFLAG_OVERLAY;
313 op->o_bd->bd_info = (BackendInfo *)on;
314 rc = on->on_bi.bi_access_allowed( op, e,
315 desc, val, access, state, maskp );
316 if ( rc != SLAP_CB_CONTINUE ) break;
320 if ( rc == SLAP_CB_CONTINUE ) {
321 BI_access_allowed *bi_access_allowed;
323 /* if the database structure was changed, o_bd points to a
324 * copy of the structure; put the original bd_info in place */
325 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
326 op->o_bd->bd_info = oi->oi_orig;
329 if ( oi->oi_orig->bi_access_allowed ) {
330 bi_access_allowed = oi->oi_orig->bi_access_allowed;
332 bi_access_allowed = slap_access_allowed;
335 rc = bi_access_allowed( op, e,
336 desc, val, access, state, maskp );
338 /* should not fall thru this far without anything happening... */
339 if ( rc == SLAP_CB_CONTINUE ) {
340 /* access not allowed */
345 op->o_bd->bd_info = bi;
351 overlay_entry_get_ov(
355 AttributeDescription *ad,
360 slap_overinfo *oi = on->on_info;
361 BackendDB *be = op->o_bd, db;
362 BackendInfo *bi = op->o_bd->bd_info;
363 int rc = SLAP_CB_CONTINUE;
365 for ( ; on; on = on->on_next ) {
366 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
368 if ( on->on_bi.bi_entry_get_rw ) {
369 /* NOTE: do not copy the structure until required */
370 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
372 db.be_flags |= SLAP_DBFLAG_OVERLAY;
376 op->o_bd->bd_info = (BackendInfo *)on;
377 rc = on->on_bi.bi_entry_get_rw( op, dn,
379 if ( rc != SLAP_CB_CONTINUE ) break;
383 if ( rc == SLAP_CB_CONTINUE ) {
384 /* if the database structure was changed, o_bd points to a
385 * copy of the structure; put the original bd_info in place */
386 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
387 op->o_bd->bd_info = oi->oi_orig;
390 if ( oi->oi_orig->bi_entry_get_rw ) {
391 rc = oi->oi_orig->bi_entry_get_rw( op, dn,
395 /* should not fall thru this far without anything happening... */
396 if ( rc == SLAP_CB_CONTINUE ) {
397 rc = LDAP_UNWILLING_TO_PERFORM;
401 op->o_bd->bd_info = bi;
411 AttributeDescription *ad,
418 assert( op->o_bd != NULL );
420 oi = op->o_bd->bd_info->bi_private;
423 return overlay_entry_get_ov( op, dn, oc, ad, rw, e, on );
427 overlay_entry_release_ov(
433 slap_overinfo *oi = on->on_info;
434 BackendDB *be = op->o_bd, db;
435 BackendInfo *bi = op->o_bd->bd_info;
436 int rc = SLAP_CB_CONTINUE;
438 for ( ; on; on = on->on_next ) {
439 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
441 if ( on->on_bi.bi_entry_release_rw ) {
442 /* NOTE: do not copy the structure until required */
443 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
445 db.be_flags |= SLAP_DBFLAG_OVERLAY;
449 op->o_bd->bd_info = (BackendInfo *)on;
450 rc = on->on_bi.bi_entry_release_rw( op, e, rw );
451 if ( rc != SLAP_CB_CONTINUE ) break;
455 if ( rc == SLAP_CB_CONTINUE ) {
456 /* if the database structure was changed, o_bd points to a
457 * copy of the structure; put the original bd_info in place */
458 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
459 op->o_bd->bd_info = oi->oi_orig;
462 if ( oi->oi_orig->bi_entry_release_rw ) {
463 rc = oi->oi_orig->bi_entry_release_rw( op, e, rw );
466 /* should not fall thru this far without anything happening... */
467 if ( rc == SLAP_CB_CONTINUE ) {
473 op->o_bd->bd_info = bi;
479 over_entry_release_rw(
487 assert( op->o_bd != NULL );
489 oi = op->o_bd->bd_info->bi_private;
492 return overlay_entry_release_ov( op, e, rw, on );
499 struct berval *gr_ndn,
500 struct berval *op_ndn,
501 ObjectClass *group_oc,
502 AttributeDescription *group_at )
507 BackendDB *be = op->o_bd, db;
508 int rc = SLAP_CB_CONTINUE;
510 /* FIXME: used to happen for instance during abandon
511 * when global overlays are used... */
512 assert( be != NULL );
518 for ( ; on; on = on->on_next ) {
519 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
521 if ( on->on_bi.bi_acl_group ) {
522 /* NOTE: do not copy the structure until required */
523 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
525 db.be_flags |= SLAP_DBFLAG_OVERLAY;
529 op->o_bd->bd_info = (BackendInfo *)on;
530 rc = on->on_bi.bi_acl_group( op, e,
531 gr_ndn, op_ndn, group_oc, group_at );
532 if ( rc != SLAP_CB_CONTINUE ) break;
536 if ( rc == SLAP_CB_CONTINUE ) {
537 BI_acl_group *bi_acl_group;
539 /* if the database structure was changed, o_bd points to a
540 * copy of the structure; put the original bd_info in place */
541 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
542 op->o_bd->bd_info = oi->oi_orig;
545 if ( oi->oi_orig->bi_acl_group ) {
546 bi_acl_group = oi->oi_orig->bi_acl_group;
548 bi_acl_group = backend_group;
551 rc = bi_acl_group( op, e,
552 gr_ndn, op_ndn, group_oc, group_at );
554 /* should not fall thru this far without anything happening... */
555 if ( rc == SLAP_CB_CONTINUE ) {
556 /* access not allowed */
561 op->o_bd->bd_info = bi;
570 struct berval *entry_ndn,
571 AttributeDescription *entry_at,
573 slap_access_t access )
578 BackendDB *be = op->o_bd, db;
579 int rc = SLAP_CB_CONTINUE;
581 /* FIXME: used to happen for instance during abandon
582 * when global overlays are used... */
583 assert( be != NULL );
589 for ( ; on; on = on->on_next ) {
590 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
592 if ( on->on_bi.bi_acl_attribute ) {
593 /* NOTE: do not copy the structure until required */
594 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
596 db.be_flags |= SLAP_DBFLAG_OVERLAY;
600 op->o_bd->bd_info = (BackendInfo *)on;
601 rc = on->on_bi.bi_acl_attribute( op, target,
602 entry_ndn, entry_at, vals, access );
603 if ( rc != SLAP_CB_CONTINUE ) break;
607 if ( rc == SLAP_CB_CONTINUE ) {
608 BI_acl_attribute *bi_acl_attribute;
610 /* if the database structure was changed, o_bd points to a
611 * copy of the structure; put the original bd_info in place */
612 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
613 op->o_bd->bd_info = oi->oi_orig;
616 if ( oi->oi_orig->bi_acl_attribute ) {
617 bi_acl_attribute = oi->oi_orig->bi_acl_attribute;
619 bi_acl_attribute = backend_attribute;
622 rc = bi_acl_attribute( op, target,
623 entry_ndn, entry_at, vals, access );
625 /* should not fall thru this far without anything happening... */
626 if ( rc == SLAP_CB_CONTINUE ) {
627 /* access not allowed */
632 op->o_bd->bd_info = bi;
638 overlay_callback_after_backover( Operation *op, slap_callback *sc, int append )
642 for ( scp = &op->o_callback; *scp != NULL; scp = &(*scp)->sc_next ) {
643 if ( (*scp)->sc_response == over_back_response ) {
644 sc->sc_next = (*scp)->sc_next;
645 (*scp)->sc_next = sc;
659 * default return code in case of missing backend function
660 * and overlay stack returning SLAP_CB_CONTINUE
662 static int op_rc[ op_last ] = {
663 LDAP_UNWILLING_TO_PERFORM, /* bind */
664 LDAP_UNWILLING_TO_PERFORM, /* unbind */
665 LDAP_UNWILLING_TO_PERFORM, /* search */
666 SLAP_CB_CONTINUE, /* compare; pass to frontend */
667 LDAP_UNWILLING_TO_PERFORM, /* modify */
668 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
669 LDAP_UNWILLING_TO_PERFORM, /* add */
670 LDAP_UNWILLING_TO_PERFORM, /* delete */
671 LDAP_UNWILLING_TO_PERFORM, /* abandon */
672 LDAP_UNWILLING_TO_PERFORM, /* cancel */
673 LDAP_UNWILLING_TO_PERFORM, /* extended */
674 LDAP_SUCCESS, /* aux_operational */
675 LDAP_SUCCESS, /* aux_chk_referrals */
676 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
682 slap_operation_t which,
688 int rc = SLAP_CB_CONTINUE;
690 for (; on; on=on->on_next ) {
691 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
694 if ( (&bi->bi_op_bind)[ which ] ) {
695 op->o_bd->bd_info = (BackendInfo *)on;
696 rc = (&bi->bi_op_bind)[ which ]( op, rs );
697 if ( rc != SLAP_CB_CONTINUE ) break;
700 if ( rc == SLAP_CB_BYPASS )
701 rc = SLAP_CB_CONTINUE;
702 /* if an overlay halted processing, make sure
703 * any previously set cleanup handlers are run
705 if ( rc != SLAP_CB_CONTINUE )
709 if ( (&bi->bi_op_bind)[ which ] ) {
710 op->o_bd->bd_info = bi;
711 rc = (&bi->bi_op_bind)[ which ]( op, rs );
713 /* should not fall thru this far without anything happening... */
714 if ( rc == SLAP_CB_CONTINUE ) {
718 /* The underlying backend didn't handle the request, make sure
719 * overlay cleanup is processed.
721 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
722 slap_callback *sc_next;
724 for ( ; op->o_callback && op->o_callback->sc_response !=
725 over_back_response; op->o_callback = sc_next ) {
726 sc_next = op->o_callback->sc_next;
727 if ( op->o_callback->sc_cleanup ) {
728 op->o_callback->sc_cleanup( op, rs );
739 slap_operation_t which
744 BackendDB *be = op->o_bd, db;
746 slap_callback *cb = (slap_callback *) ch_malloc( sizeof( slap_callback ));
747 int rc = SLAP_CB_CONTINUE;
749 /* FIXME: used to happen for instance during abandon
750 * when global overlays are used... */
751 assert( op->o_bd != NULL );
753 oi = op->o_bd->bd_info->bi_private;
756 if ( !SLAP_ISOVERLAY( op->o_bd )) {
758 db.be_flags |= SLAP_DBFLAG_OVERLAY;
761 cb->sc_cleanup = over_back_response_cleanup;
762 cb->sc_response = over_back_response;
763 cb->sc_writewait = NULL;
764 cb->sc_next = op->o_callback;
768 rc = overlay_op_walk( op, rs, which, oi, on );
769 for ( sc = &op->o_callback; *sc; sc = &(*sc)->sc_next ) {
782 over_op_bind( Operation *op, SlapReply *rs )
784 return over_op_func( op, rs, op_bind );
788 over_op_unbind( Operation *op, SlapReply *rs )
790 return over_op_func( op, rs, op_unbind );
794 over_op_search( Operation *op, SlapReply *rs )
796 return over_op_func( op, rs, op_search );
800 over_op_compare( Operation *op, SlapReply *rs )
802 return over_op_func( op, rs, op_compare );
806 over_op_modify( Operation *op, SlapReply *rs )
808 return over_op_func( op, rs, op_modify );
812 over_op_modrdn( Operation *op, SlapReply *rs )
814 return over_op_func( op, rs, op_modrdn );
818 over_op_add( Operation *op, SlapReply *rs )
820 return over_op_func( op, rs, op_add );
824 over_op_delete( Operation *op, SlapReply *rs )
826 return over_op_func( op, rs, op_delete );
830 over_op_abandon( Operation *op, SlapReply *rs )
832 return over_op_func( op, rs, op_abandon );
836 over_op_cancel( Operation *op, SlapReply *rs )
838 return over_op_func( op, rs, op_cancel );
842 over_op_extended( Operation *op, SlapReply *rs )
844 return over_op_func( op, rs, op_extended );
848 over_aux_operational( Operation *op, SlapReply *rs )
850 return over_op_func( op, rs, op_aux_operational );
854 over_aux_chk_referrals( Operation *op, SlapReply *rs )
856 return over_op_func( op, rs, op_aux_chk_referrals );
860 over_aux_chk_controls( Operation *op, SlapReply *rs )
862 return over_op_func( op, rs, op_aux_chk_controls );
872 over_connection_func(
875 enum conn_which which
881 int rc = SLAP_CB_CONTINUE;
882 BI_connection_init **func;
884 /* FIXME: used to happen for instance during abandon
885 * when global overlays are used... */
886 assert( bd != NULL );
888 oi = bd->bd_info->bi_private;
891 if ( !SLAP_ISOVERLAY( bd ) ) {
893 db.be_flags |= SLAP_DBFLAG_OVERLAY;
897 for ( ; on; on = on->on_next ) {
898 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
900 func = &on->on_bi.bi_connection_init;
901 if ( func[ which ] ) {
902 bd->bd_info = (BackendInfo *)on;
903 rc = func[ which ]( bd, conn );
904 if ( rc != SLAP_CB_CONTINUE ) break;
908 func = &oi->oi_orig->bi_connection_init;
909 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
910 bd->bd_info = oi->oi_orig;
911 rc = func[ which ]( bd, conn );
913 /* should not fall thru this far without anything happening... */
914 if ( rc == SLAP_CB_CONTINUE ) {
915 rc = LDAP_UNWILLING_TO_PERFORM;
922 over_connection_init(
927 return over_connection_func( bd, conn, conn_init );
931 over_connection_destroy(
936 return over_connection_func( bd, conn, conn_destroy );
946 /* FIXME: check for duplicates? */
947 for ( tmp = overlays; tmp != NULL; tmp = tmp->on_next ) {
948 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_type ) == 0 ) {
949 Debug( LDAP_DEBUG_ANY,
950 "overlay_register(\"%s\"): "
951 "name already in use.\n",
952 on->on_bi.bi_type, 0, 0 );
956 if ( on->on_bi.bi_obsolete_names != NULL ) {
959 for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
960 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], tmp->on_bi.bi_type ) == 0 ) {
961 Debug( LDAP_DEBUG_ANY,
962 "overlay_register(\"%s\"): "
963 "obsolete name \"%s\" already in use "
964 "by overlay \"%s\".\n",
966 on->on_bi.bi_obsolete_names[ i ],
967 tmp->on_bi.bi_type );
973 if ( tmp->on_bi.bi_obsolete_names != NULL ) {
976 for ( i = 0; tmp->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
979 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
980 Debug( LDAP_DEBUG_ANY,
981 "overlay_register(\"%s\"): "
982 "name already in use "
983 "as obsolete by overlay \"%s\".\n",
985 tmp->on_bi.bi_obsolete_names[ i ], 0 );
989 if ( on->on_bi.bi_obsolete_names != NULL ) {
990 for ( j = 0; on->on_bi.bi_obsolete_names[ j ] != NULL; j++ ) {
991 if ( strcmp( on->on_bi.bi_obsolete_names[ j ], tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
992 Debug( LDAP_DEBUG_ANY,
993 "overlay_register(\"%s\"): "
994 "obsolete name \"%s\" already in use "
995 "as obsolete by overlay \"%s\".\n",
997 on->on_bi.bi_obsolete_names[ j ],
998 tmp->on_bi.bi_type );
1007 on->on_next = overlays;
1013 * iterator on registered overlays; overlay_next( NULL ) returns the first
1014 * overlay; subsequent calls with the previously returned value allow to
1015 * iterate over the entire list; returns NULL when no more overlays are
1032 * returns a specific registered overlay based on the type; NULL if not
1037 overlay_find( const char *over_type )
1039 slap_overinst *on = overlays;
1041 assert( over_type != NULL );
1043 for ( ; on; on = on->on_next ) {
1044 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
1048 if ( on->on_bi.bi_obsolete_names != NULL ) {
1051 for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
1052 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], over_type ) == 0 ) {
1053 Debug( LDAP_DEBUG_ANY,
1054 "overlay_find(\"%s\"): "
1055 "obsolete name for \"%s\".\n",
1056 on->on_bi.bi_obsolete_names[ i ],
1057 on->on_bi.bi_type, 0 );
1068 static const char overtype[] = "over";
1071 * returns TRUE (1) if the database is actually an overlay instance;
1072 * FALSE (0) otherwise.
1076 overlay_is_over( BackendDB *be )
1078 return be->bd_info->bi_type == overtype;
1082 * returns TRUE (1) if the given database is actually an overlay
1083 * instance and, somewhere in the list, contains the requested overlay;
1084 * FALSE (0) otherwise.
1088 overlay_is_inst( BackendDB *be, const char *over_type )
1092 assert( be != NULL );
1094 if ( !overlay_is_over( be ) ) {
1098 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
1099 for ( ; on; on = on->on_next ) {
1100 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
1109 overlay_register_control( BackendDB *be, const char *oid )
1114 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1118 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1121 /* add to all backends... */
1122 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1123 if ( bd == be->bd_self ) {
1127 /* overlays can be instanciated multiple times, use
1128 * be_ctrls[ cid ] as an instance counter, so that the
1129 * overlay's controls are only really disabled after the
1130 * last instance called overlay_register_control() */
1131 bd->be_ctrls[ cid ]++;
1132 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1138 /* overlays can be instanciated multiple times, use
1139 * be_ctrls[ cid ] as an instance counter, so that the
1140 * overlay's controls are only really unregistered after the
1141 * last instance called overlay_register_control() */
1142 be->bd_self->be_ctrls[ cid ]++;
1143 be->bd_self->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1149 #ifdef SLAP_CONFIG_DELETE
1151 overlay_unregister_control( BackendDB *be, const char *oid )
1156 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1160 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1163 /* remove from all backends... */
1164 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1165 if ( bd == be->bd_self ) {
1169 bd->be_ctrls[ cid ]--;
1174 be->bd_self->be_ctrls[ cid ]--;
1177 #endif /* SLAP_CONFIG_DELETE */
1180 overlay_destroy_one( BackendDB *be, slap_overinst *on )
1182 slap_overinfo *oi = on->on_info;
1183 slap_overinst **oidx;
1185 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1186 if ( *oidx == on ) {
1187 *oidx = on->on_next;
1188 if ( on->on_bi.bi_db_destroy ) {
1189 BackendInfo *bi_orig = be->bd_info;
1190 be->bd_info = (BackendInfo *)on;
1191 on->on_bi.bi_db_destroy( be, NULL );
1192 be->bd_info = bi_orig;
1200 #ifdef SLAP_CONFIG_DELETE
1201 typedef struct ov_remove_ctx {
1207 overlay_remove_cb( Operation *op, SlapReply *rs )
1209 ov_remove_ctx *rm_ctx = (ov_remove_ctx*) op->o_callback->sc_private;
1210 BackendInfo *bi_orig = rm_ctx->be->bd_info;
1212 rm_ctx->be->bd_info = (BackendInfo*) rm_ctx->on;
1214 if ( rm_ctx->on->on_bi.bi_db_close ) {
1215 rm_ctx->on->on_bi.bi_db_close( rm_ctx->be, NULL );
1217 if ( rm_ctx->on->on_bi.bi_db_destroy ) {
1218 rm_ctx->on->on_bi.bi_db_destroy( rm_ctx->be, NULL );
1221 /* clean up after removing last overlay */
1222 if ( ! rm_ctx->on->on_info->oi_list ) {
1223 /* reset db flags and bd_info to orig */
1224 SLAP_DBFLAGS( rm_ctx->be ) &= ~SLAP_DBFLAG_GLOBAL_OVERLAY;
1225 rm_ctx->be->bd_info = rm_ctx->on->on_info->oi_orig;
1226 ch_free(rm_ctx->on->on_info);
1228 rm_ctx->be->bd_info = bi_orig;
1231 op->o_tmpfree(rm_ctx, op->o_tmpmemctx);
1232 return SLAP_CB_CONTINUE;
1236 overlay_remove( BackendDB *be, slap_overinst *on, Operation *op )
1238 slap_overinfo *oi = on->on_info;
1239 slap_overinst **oidx;
1240 ov_remove_ctx *rm_ctx;
1241 slap_callback *rm_cb, *cb;
1243 /* remove overlay from oi_list */
1244 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1245 if ( *oidx == on ) {
1246 *oidx = on->on_next;
1251 /* The db_close and db_destroy handlers to cleanup a release
1252 * the overlay's resources are called from the cleanup callback
1254 rm_ctx = op->o_tmpalloc( sizeof( ov_remove_ctx ), op->o_tmpmemctx );
1258 rm_cb = op->o_tmpalloc( sizeof( slap_callback ), op->o_tmpmemctx );
1259 rm_cb->sc_next = NULL;
1260 rm_cb->sc_cleanup = overlay_remove_cb;
1261 rm_cb->sc_response = NULL;
1262 rm_cb->sc_private = (void*) rm_ctx;
1264 /* Append callback to the end of the list */
1265 if ( !op->o_callback ) {
1266 op->o_callback = rm_cb;
1268 for ( cb = op->o_callback; cb->sc_next; cb = cb->sc_next );
1269 cb->sc_next = rm_cb;
1272 #endif /* SLAP_CONFIG_DELETE */
1275 overlay_insert( BackendDB *be, slap_overinst *on2, slap_overinst ***prev,
1278 slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1281 on2->on_next = oi->oi_list;
1285 slap_overinst *on, **prev;
1287 /* Since the list is in reverse order and is singly linked,
1288 * we have to count the overlays and then insert backwards.
1289 * Adding on overlay at a specific point should be a pretty
1290 * infrequent occurrence.
1293 for ( on = oi->oi_list; on; on=on->on_next )
1301 /* advance to insertion point */
1302 prev = &oi->oi_list;
1303 for ( i=0; i<idx; i++ ) {
1305 prev = &on->on_next;
1308 on2->on_next = *prev;
1314 overlay_move( BackendDB *be, slap_overinst *on, int idx )
1316 slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1317 slap_overinst **onp;
1319 for (onp = &oi->oi_list; *onp; onp= &(*onp)->on_next) {
1325 overlay_insert( be, on, &onp, idx );
1328 /* add an overlay to a particular backend. */
1330 overlay_config( BackendDB *be, const char *ov, int idx, BackendInfo **res, ConfigReply *cr )
1332 slap_overinst *on = NULL, *on2 = NULL, **prev;
1333 slap_overinfo *oi = NULL;
1334 BackendInfo *bi = NULL;
1339 on = overlay_find( ov );
1341 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
1345 /* If this is the first overlay on this backend, set up the
1346 * overlay info structure
1348 if ( !overlay_is_over( be ) ) {
1351 /* NOTE: the first time a global overlay is configured,
1352 * frontendDB gets this flag; it is used later by overlays
1353 * to determine if they're stacked on top of the frontendDB */
1354 if ( be->bd_info == frontendDB->bd_info || SLAP_ISGLOBALOVERLAY( be ) ) {
1356 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DBONLY ) {
1357 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1358 "overlay \"%s\" cannot be global.\n",
1363 } else if ( on->on_bi.bi_flags & SLAPO_BFLAG_GLOBONLY ) {
1364 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1365 "overlay \"%s\" can only be global.\n",
1370 oi = ch_malloc( sizeof( slap_overinfo ) );
1371 oi->oi_orig = be->bd_info;
1372 oi->oi_bi = *be->bd_info;
1376 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
1379 /* Save a pointer to ourself in bi_private.
1381 oi->oi_bi.bi_private = oi;
1383 bi = (BackendInfo *)oi;
1385 bi->bi_type = (char *)overtype;
1387 bi->bi_db_config = over_db_config;
1388 bi->bi_db_open = over_db_open;
1389 bi->bi_db_close = over_db_close;
1390 bi->bi_db_destroy = over_db_destroy;
1392 bi->bi_op_bind = over_op_bind;
1393 bi->bi_op_unbind = over_op_unbind;
1394 bi->bi_op_search = over_op_search;
1395 bi->bi_op_compare = over_op_compare;
1396 bi->bi_op_modify = over_op_modify;
1397 bi->bi_op_modrdn = over_op_modrdn;
1398 bi->bi_op_add = over_op_add;
1399 bi->bi_op_delete = over_op_delete;
1400 bi->bi_op_abandon = over_op_abandon;
1401 bi->bi_op_cancel = over_op_cancel;
1403 bi->bi_extended = over_op_extended;
1406 * this is fine because it has the same
1407 * args of the operations; we need to rework
1408 * all the hooks to share the same args
1409 * of the operations...
1411 bi->bi_operational = over_aux_operational;
1412 bi->bi_chk_referrals = over_aux_chk_referrals;
1413 bi->bi_chk_controls = over_aux_chk_controls;
1415 /* these have specific arglists */
1416 bi->bi_entry_get_rw = over_entry_get_rw;
1417 bi->bi_entry_release_rw = over_entry_release_rw;
1418 bi->bi_access_allowed = over_access_allowed;
1419 bi->bi_acl_group = over_acl_group;
1420 bi->bi_acl_attribute = over_acl_attribute;
1422 bi->bi_connection_init = over_connection_init;
1423 bi->bi_connection_destroy = over_connection_destroy;
1428 if ( overlay_is_inst( be, ov ) ) {
1429 if ( SLAPO_SINGLE( be ) ) {
1430 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1431 "overlay \"%s\" already in list\n",
1437 oi = be->bd_info->bi_private;
1440 /* Insert new overlay into list. By default overlays are
1441 * added to head of list and executed in LIFO order.
1443 on2 = ch_calloc( 1, sizeof(slap_overinst) );
1447 prev = &oi->oi_list;
1448 /* Do we need to find the insertion point? */
1452 /* count current overlays */
1453 for ( i=0, on=oi->oi_list; on; on=on->on_next, i++ );
1455 /* are we just appending a new one? */
1459 overlay_insert( be, on2, &prev, idx );
1461 /* Any initialization needed? */
1462 if ( on2->on_bi.bi_db_init ) {
1464 be->bd_info = (BackendInfo *)on2;
1465 rc = on2->on_bi.bi_db_init( be, cr);
1466 be->bd_info = (BackendInfo *)oi;
1468 *prev = on2->on_next;