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 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
331 op->o_bd->bd_info = bi;
338 overlay_entry_get_ov(
342 AttributeDescription *ad,
347 slap_overinfo *oi = on->on_info;
348 BackendDB *be = op->o_bd, db;
349 BackendInfo *bi = op->o_bd->bd_info;
350 int rc = SLAP_CB_CONTINUE;
352 for ( ; on; on = on->on_next ) {
353 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
355 if ( on->on_bi.bi_entry_get_rw ) {
356 /* NOTE: do not copy the structure until required */
357 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
359 db.be_flags |= SLAP_DBFLAG_OVERLAY;
363 op->o_bd->bd_info = (BackendInfo *)on;
364 rc = on->on_bi.bi_entry_get_rw( op, dn,
366 if ( rc != SLAP_CB_CONTINUE ) break;
370 if ( rc == SLAP_CB_CONTINUE ) {
371 /* if the database structure was changed, o_bd points to a
372 * copy of the structure; put the original bd_info in place */
373 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
374 op->o_bd->bd_info = oi->oi_orig;
377 if ( oi->oi_orig->bi_entry_get_rw ) {
378 rc = oi->oi_orig->bi_entry_get_rw( op, dn,
382 /* should not fall thru this far without anything happening... */
383 if ( rc == SLAP_CB_CONTINUE ) {
384 rc = LDAP_UNWILLING_TO_PERFORM;
388 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
389 op->o_bd->bd_info = bi;
400 AttributeDescription *ad,
407 assert( op->o_bd != NULL );
409 oi = op->o_bd->bd_info->bi_private;
412 return overlay_entry_get_ov( op, dn, oc, ad, rw, e, on );
416 overlay_entry_release_ov(
422 slap_overinfo *oi = on->on_info;
423 BackendDB *be = op->o_bd, db;
424 BackendInfo *bi = op->o_bd->bd_info;
425 int rc = SLAP_CB_CONTINUE;
427 for ( ; on; on = on->on_next ) {
428 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
430 if ( on->on_bi.bi_entry_release_rw ) {
431 /* NOTE: do not copy the structure until required */
432 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
434 db.be_flags |= SLAP_DBFLAG_OVERLAY;
438 op->o_bd->bd_info = (BackendInfo *)on;
439 rc = on->on_bi.bi_entry_release_rw( op, e, rw );
440 if ( rc != SLAP_CB_CONTINUE ) break;
444 if ( rc == SLAP_CB_CONTINUE ) {
445 /* if the database structure was changed, o_bd points to a
446 * copy of the structure; put the original bd_info in place */
447 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
448 op->o_bd->bd_info = oi->oi_orig;
451 if ( oi->oi_orig->bi_entry_release_rw ) {
452 rc = oi->oi_orig->bi_entry_release_rw( op, e, rw );
455 /* should not fall thru this far without anything happening... */
456 if ( rc == SLAP_CB_CONTINUE ) {
462 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
463 op->o_bd->bd_info = bi;
470 over_entry_release_rw(
478 assert( op->o_bd != NULL );
480 oi = op->o_bd->bd_info->bi_private;
483 return overlay_entry_release_ov( op, e, rw, on );
490 struct berval *gr_ndn,
491 struct berval *op_ndn,
492 ObjectClass *group_oc,
493 AttributeDescription *group_at )
498 BackendDB *be = op->o_bd, db;
499 int rc = SLAP_CB_CONTINUE;
501 /* FIXME: used to happen for instance during abandon
502 * when global overlays are used... */
503 assert( be != NULL );
509 for ( ; on; on = on->on_next ) {
510 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
512 if ( on->on_bi.bi_acl_group ) {
513 /* NOTE: do not copy the structure until required */
514 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
516 db.be_flags |= SLAP_DBFLAG_OVERLAY;
520 op->o_bd->bd_info = (BackendInfo *)on;
521 rc = on->on_bi.bi_acl_group( op, e,
522 gr_ndn, op_ndn, group_oc, group_at );
523 if ( rc != SLAP_CB_CONTINUE ) break;
527 if ( rc == SLAP_CB_CONTINUE ) {
528 BI_acl_group *bi_acl_group;
530 /* if the database structure was changed, o_bd points to a
531 * copy of the structure; put the original bd_info in place */
532 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
533 op->o_bd->bd_info = oi->oi_orig;
536 if ( oi->oi_orig->bi_acl_group ) {
537 bi_acl_group = oi->oi_orig->bi_acl_group;
539 bi_acl_group = backend_group;
542 rc = bi_acl_group( op, e,
543 gr_ndn, op_ndn, group_oc, group_at );
545 /* should not fall thru this far without anything happening... */
546 if ( rc == SLAP_CB_CONTINUE ) {
547 /* access not allowed */
552 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
553 op->o_bd->bd_info = bi;
563 struct berval *entry_ndn,
564 AttributeDescription *entry_at,
566 slap_access_t access )
571 BackendDB *be = op->o_bd, db;
572 int rc = SLAP_CB_CONTINUE;
574 /* FIXME: used to happen for instance during abandon
575 * when global overlays are used... */
576 assert( be != NULL );
582 for ( ; on; on = on->on_next ) {
583 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
585 if ( on->on_bi.bi_acl_attribute ) {
586 /* NOTE: do not copy the structure until required */
587 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
589 db.be_flags |= SLAP_DBFLAG_OVERLAY;
593 op->o_bd->bd_info = (BackendInfo *)on;
594 rc = on->on_bi.bi_acl_attribute( op, target,
595 entry_ndn, entry_at, vals, access );
596 if ( rc != SLAP_CB_CONTINUE ) break;
600 if ( rc == SLAP_CB_CONTINUE ) {
601 BI_acl_attribute *bi_acl_attribute;
603 /* if the database structure was changed, o_bd points to a
604 * copy of the structure; put the original bd_info in place */
605 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
606 op->o_bd->bd_info = oi->oi_orig;
609 if ( oi->oi_orig->bi_acl_attribute ) {
610 bi_acl_attribute = oi->oi_orig->bi_acl_attribute;
612 bi_acl_attribute = backend_attribute;
615 rc = bi_acl_attribute( op, target,
616 entry_ndn, entry_at, vals, access );
618 /* should not fall thru this far without anything happening... */
619 if ( rc == SLAP_CB_CONTINUE ) {
620 /* access not allowed */
625 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
626 op->o_bd->bd_info = bi;
633 overlay_callback_after_backover( Operation *op, slap_callback *sc, int append )
637 for ( scp = &op->o_callback; *scp != NULL; scp = &(*scp)->sc_next ) {
638 if ( (*scp)->sc_response == over_back_response ) {
639 sc->sc_next = (*scp)->sc_next;
640 (*scp)->sc_next = sc;
654 * default return code in case of missing backend function
655 * and overlay stack returning SLAP_CB_CONTINUE
657 static int op_rc[ op_last ] = {
658 LDAP_UNWILLING_TO_PERFORM, /* bind */
659 LDAP_UNWILLING_TO_PERFORM, /* unbind */
660 LDAP_UNWILLING_TO_PERFORM, /* search */
661 SLAP_CB_CONTINUE, /* compare; pass to frontend */
662 LDAP_UNWILLING_TO_PERFORM, /* modify */
663 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
664 LDAP_UNWILLING_TO_PERFORM, /* add */
665 LDAP_UNWILLING_TO_PERFORM, /* delete */
666 LDAP_UNWILLING_TO_PERFORM, /* abandon */
667 LDAP_UNWILLING_TO_PERFORM, /* cancel */
668 LDAP_UNWILLING_TO_PERFORM, /* extended */
669 LDAP_SUCCESS, /* aux_operational */
670 LDAP_SUCCESS, /* aux_chk_referrals */
671 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
677 slap_operation_t which,
683 int rc = SLAP_CB_CONTINUE;
685 for (; on; on=on->on_next ) {
686 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
689 if ( (&bi->bi_op_bind)[ which ] ) {
690 op->o_bd->bd_info = (BackendInfo *)on;
691 rc = (&bi->bi_op_bind)[ which ]( op, rs );
692 if ( rc != SLAP_CB_CONTINUE ) break;
695 if ( rc == SLAP_CB_BYPASS )
696 rc = SLAP_CB_CONTINUE;
697 /* if an overlay halted processing, make sure
698 * any previously set cleanup handlers are run
700 if ( rc != SLAP_CB_CONTINUE )
704 if ( (&bi->bi_op_bind)[ which ] ) {
705 op->o_bd->bd_info = bi;
706 rc = (&bi->bi_op_bind)[ which ]( op, rs );
708 /* should not fall thru this far without anything happening... */
709 if ( rc == SLAP_CB_CONTINUE ) {
713 /* The underlying backend didn't handle the request, make sure
714 * overlay cleanup is processed.
716 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
717 slap_callback *sc_next;
719 for ( ; op->o_callback && op->o_callback->sc_response !=
720 over_back_response; op->o_callback = sc_next ) {
721 sc_next = op->o_callback->sc_next;
722 if ( op->o_callback->sc_cleanup ) {
723 op->o_callback->sc_cleanup( op, rs );
731 over_op_func_cleanup( Operation *op, SlapReply *rs )
733 slap_callback *cb = op->o_callback;
734 if ( rs->sr_type == REP_RESULT && cb != NULL) {
735 op->o_callback = cb->sc_next;
736 op->o_tmpfree( cb, op->o_tmpmemctx );
744 slap_operation_t which
749 BackendDB *be = op->o_bd, db;
752 int rc = SLAP_CB_CONTINUE;
754 /* FIXME: used to happen for instance during abandon
755 * when global overlays are used... */
756 assert( op->o_bd != NULL );
758 oi = op->o_bd->bd_info->bi_private;
761 if ( !SLAP_ISOVERLAY( op->o_bd )) {
763 db.be_flags |= SLAP_DBFLAG_OVERLAY;
766 if ( op->o_tag != LDAP_REQ_ABANDON && op->o_tag != LDAP_REQ_UNBIND ) {
767 cb = (slap_callback *)op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
768 cb->sc_cleanup = over_op_func_cleanup;
769 cb->sc_response = over_back_response;
770 cb->sc_writewait = NULL;
771 cb->sc_next = op->o_callback;
776 rc = overlay_op_walk( op, rs, which, oi, on );
777 if ( rc != SLAPD_ASYNCOP && op->o_tag != LDAP_REQ_ABANDON && op->o_tag != LDAP_REQ_UNBIND ) {
778 for ( sc = &op->o_callback; *sc; sc = &(*sc)->sc_next ) {
781 op->o_tmpfree( cb, op->o_tmpmemctx );
792 over_op_bind( Operation *op, SlapReply *rs )
794 return over_op_func( op, rs, op_bind );
798 over_op_unbind( Operation *op, SlapReply *rs )
800 return over_op_func( op, rs, op_unbind );
804 over_op_search( Operation *op, SlapReply *rs )
806 return over_op_func( op, rs, op_search );
810 over_op_compare( Operation *op, SlapReply *rs )
812 return over_op_func( op, rs, op_compare );
816 over_op_modify( Operation *op, SlapReply *rs )
818 return over_op_func( op, rs, op_modify );
822 over_op_modrdn( Operation *op, SlapReply *rs )
824 return over_op_func( op, rs, op_modrdn );
828 over_op_add( Operation *op, SlapReply *rs )
830 return over_op_func( op, rs, op_add );
834 over_op_delete( Operation *op, SlapReply *rs )
836 return over_op_func( op, rs, op_delete );
840 over_op_abandon( Operation *op, SlapReply *rs )
842 return over_op_func( op, rs, op_abandon );
846 over_op_cancel( Operation *op, SlapReply *rs )
848 return over_op_func( op, rs, op_cancel );
852 over_op_extended( Operation *op, SlapReply *rs )
854 return over_op_func( op, rs, op_extended );
858 over_aux_operational( Operation *op, SlapReply *rs )
860 return over_op_func( op, rs, op_aux_operational );
864 over_aux_chk_referrals( Operation *op, SlapReply *rs )
866 return over_op_func( op, rs, op_aux_chk_referrals );
870 over_aux_chk_controls( Operation *op, SlapReply *rs )
872 return over_op_func( op, rs, op_aux_chk_controls );
882 over_connection_func(
885 enum conn_which which
891 int rc = SLAP_CB_CONTINUE;
892 BI_connection_init **func;
894 /* FIXME: used to happen for instance during abandon
895 * when global overlays are used... */
896 assert( bd != NULL );
898 oi = bd->bd_info->bi_private;
901 if ( !SLAP_ISOVERLAY( bd ) ) {
903 db.be_flags |= SLAP_DBFLAG_OVERLAY;
907 for ( ; on; on = on->on_next ) {
908 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DISABLED )
910 func = &on->on_bi.bi_connection_init;
911 if ( func[ which ] ) {
912 bd->bd_info = (BackendInfo *)on;
913 rc = func[ which ]( bd, conn );
914 if ( rc != SLAP_CB_CONTINUE ) break;
918 func = &oi->oi_orig->bi_connection_init;
919 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
920 bd->bd_info = oi->oi_orig;
921 rc = func[ which ]( bd, conn );
923 /* should not fall thru this far without anything happening... */
924 if ( rc == SLAP_CB_CONTINUE ) {
925 rc = LDAP_UNWILLING_TO_PERFORM;
932 over_connection_init(
937 return over_connection_func( bd, conn, conn_init );
941 over_connection_destroy(
946 return over_connection_func( bd, conn, conn_destroy );
956 /* FIXME: check for duplicates? */
957 for ( tmp = overlays; tmp != NULL; tmp = tmp->on_next ) {
958 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_type ) == 0 ) {
959 Debug( LDAP_DEBUG_ANY,
960 "overlay_register(\"%s\"): "
961 "name already in use.\n",
962 on->on_bi.bi_type, 0, 0 );
966 if ( on->on_bi.bi_obsolete_names != NULL ) {
969 for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
970 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], tmp->on_bi.bi_type ) == 0 ) {
971 Debug( LDAP_DEBUG_ANY,
972 "overlay_register(\"%s\"): "
973 "obsolete name \"%s\" already in use "
974 "by overlay \"%s\".\n",
976 on->on_bi.bi_obsolete_names[ i ],
977 tmp->on_bi.bi_type );
983 if ( tmp->on_bi.bi_obsolete_names != NULL ) {
986 for ( i = 0; tmp->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
989 if ( strcmp( on->on_bi.bi_type, tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
990 Debug( LDAP_DEBUG_ANY,
991 "overlay_register(\"%s\"): "
992 "name already in use "
993 "as obsolete by overlay \"%s\".\n",
995 tmp->on_bi.bi_obsolete_names[ i ], 0 );
999 if ( on->on_bi.bi_obsolete_names != NULL ) {
1000 for ( j = 0; on->on_bi.bi_obsolete_names[ j ] != NULL; j++ ) {
1001 if ( strcmp( on->on_bi.bi_obsolete_names[ j ], tmp->on_bi.bi_obsolete_names[ i ] ) == 0 ) {
1002 Debug( LDAP_DEBUG_ANY,
1003 "overlay_register(\"%s\"): "
1004 "obsolete name \"%s\" already in use "
1005 "as obsolete by overlay \"%s\".\n",
1007 on->on_bi.bi_obsolete_names[ j ],
1008 tmp->on_bi.bi_type );
1017 on->on_next = overlays;
1023 * iterator on registered overlays; overlay_next( NULL ) returns the first
1024 * overlay; subsequent calls with the previously returned value allow to
1025 * iterate over the entire list; returns NULL when no more overlays are
1042 * returns a specific registered overlay based on the type; NULL if not
1047 overlay_find( const char *over_type )
1049 slap_overinst *on = overlays;
1051 assert( over_type != NULL );
1053 for ( ; on; on = on->on_next ) {
1054 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
1058 if ( on->on_bi.bi_obsolete_names != NULL ) {
1061 for ( i = 0; on->on_bi.bi_obsolete_names[ i ] != NULL; i++ ) {
1062 if ( strcmp( on->on_bi.bi_obsolete_names[ i ], over_type ) == 0 ) {
1063 Debug( LDAP_DEBUG_ANY,
1064 "overlay_find(\"%s\"): "
1065 "obsolete name for \"%s\".\n",
1066 on->on_bi.bi_obsolete_names[ i ],
1067 on->on_bi.bi_type, 0 );
1078 static const char overtype[] = "over";
1081 * returns TRUE (1) if the database is actually an overlay instance;
1082 * FALSE (0) otherwise.
1086 overlay_is_over( BackendDB *be )
1088 return be->bd_info->bi_type == overtype;
1092 * returns TRUE (1) if the given database is actually an overlay
1093 * instance and, somewhere in the list, contains the requested overlay;
1094 * FALSE (0) otherwise.
1098 overlay_is_inst( BackendDB *be, const char *over_type )
1102 assert( be != NULL );
1104 if ( !overlay_is_over( be ) ) {
1108 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
1109 for ( ; on; on = on->on_next ) {
1110 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
1119 overlay_register_control( BackendDB *be, const char *oid )
1124 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1128 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1131 /* add to all backends... */
1132 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1133 if ( bd == be->bd_self ) {
1137 /* overlays can be instanciated multiple times, use
1138 * be_ctrls[ cid ] as an instance counter, so that the
1139 * overlay's controls are only really disabled after the
1140 * last instance called overlay_register_control() */
1141 bd->be_ctrls[ cid ]++;
1142 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1148 /* overlays can be instanciated multiple times, use
1149 * be_ctrls[ cid ] as an instance counter, so that the
1150 * overlay's controls are only really unregistered after the
1151 * last instance called overlay_register_control() */
1152 be->bd_self->be_ctrls[ cid ]++;
1153 be->bd_self->be_ctrls[ SLAP_MAX_CIDS ] = 1;
1159 #ifdef SLAP_CONFIG_DELETE
1161 overlay_unregister_control( BackendDB *be, const char *oid )
1166 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
1170 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
1173 /* remove from all backends... */
1174 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
1175 if ( bd == be->bd_self ) {
1179 bd->be_ctrls[ cid ]--;
1184 be->bd_self->be_ctrls[ cid ]--;
1187 #endif /* SLAP_CONFIG_DELETE */
1190 overlay_destroy_one( BackendDB *be, slap_overinst *on )
1192 slap_overinfo *oi = on->on_info;
1193 slap_overinst **oidx;
1195 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1196 if ( *oidx == on ) {
1197 *oidx = on->on_next;
1198 if ( on->on_bi.bi_db_destroy ) {
1199 BackendInfo *bi_orig = be->bd_info;
1200 be->bd_info = (BackendInfo *)on;
1201 on->on_bi.bi_db_destroy( be, NULL );
1202 be->bd_info = bi_orig;
1210 #ifdef SLAP_CONFIG_DELETE
1211 typedef struct ov_remove_ctx {
1217 overlay_remove_cb( Operation *op, SlapReply *rs )
1219 ov_remove_ctx *rm_ctx = (ov_remove_ctx*) op->o_callback->sc_private;
1220 BackendInfo *bi_orig = rm_ctx->be->bd_info;
1222 rm_ctx->be->bd_info = (BackendInfo*) rm_ctx->on;
1224 if ( rm_ctx->on->on_bi.bi_db_close ) {
1225 rm_ctx->on->on_bi.bi_db_close( rm_ctx->be, NULL );
1227 if ( rm_ctx->on->on_bi.bi_db_destroy ) {
1228 rm_ctx->on->on_bi.bi_db_destroy( rm_ctx->be, NULL );
1231 /* clean up after removing last overlay */
1232 if ( ! rm_ctx->on->on_info->oi_list ) {
1233 /* reset db flags and bd_info to orig */
1234 SLAP_DBFLAGS( rm_ctx->be ) &= ~SLAP_DBFLAG_GLOBAL_OVERLAY;
1235 rm_ctx->be->bd_info = rm_ctx->on->on_info->oi_orig;
1236 ch_free(rm_ctx->on->on_info);
1238 rm_ctx->be->bd_info = bi_orig;
1241 op->o_tmpfree(rm_ctx, op->o_tmpmemctx);
1242 return SLAP_CB_CONTINUE;
1246 overlay_remove( BackendDB *be, slap_overinst *on, Operation *op )
1248 slap_overinfo *oi = on->on_info;
1249 slap_overinst **oidx;
1250 ov_remove_ctx *rm_ctx;
1251 slap_callback *rm_cb, *cb;
1253 /* remove overlay from oi_list */
1254 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
1255 if ( *oidx == on ) {
1256 *oidx = on->on_next;
1261 /* The db_close and db_destroy handlers to cleanup a release
1262 * the overlay's resources are called from the cleanup callback
1264 rm_ctx = op->o_tmpalloc( sizeof( ov_remove_ctx ), op->o_tmpmemctx );
1268 rm_cb = op->o_tmpalloc( sizeof( slap_callback ), op->o_tmpmemctx );
1269 rm_cb->sc_next = NULL;
1270 rm_cb->sc_cleanup = overlay_remove_cb;
1271 rm_cb->sc_response = NULL;
1272 rm_cb->sc_private = (void*) rm_ctx;
1273 rm_cb->sc_writewait = NULL;
1275 /* Append callback to the end of the list */
1276 if ( !op->o_callback ) {
1277 op->o_callback = rm_cb;
1279 for ( cb = op->o_callback; cb->sc_next; cb = cb->sc_next );
1280 cb->sc_next = rm_cb;
1283 #endif /* SLAP_CONFIG_DELETE */
1286 overlay_insert( BackendDB *be, slap_overinst *on2, slap_overinst ***prev,
1289 slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1292 on2->on_next = oi->oi_list;
1296 slap_overinst *on, **prev;
1298 /* Since the list is in reverse order and is singly linked,
1299 * we have to count the overlays and then insert backwards.
1300 * Adding on overlay at a specific point should be a pretty
1301 * infrequent occurrence.
1304 for ( on = oi->oi_list; on; on=on->on_next )
1312 /* advance to insertion point */
1313 prev = &oi->oi_list;
1314 for ( i=0; i<idx; i++ ) {
1316 prev = &on->on_next;
1319 on2->on_next = *prev;
1325 overlay_move( BackendDB *be, slap_overinst *on, int idx )
1327 slap_overinfo *oi = (slap_overinfo *)be->bd_info;
1328 slap_overinst **onp;
1330 for (onp = &oi->oi_list; *onp; onp= &(*onp)->on_next) {
1336 overlay_insert( be, on, &onp, idx );
1339 /* add an overlay to a particular backend. */
1341 overlay_config( BackendDB *be, const char *ov, int idx, BackendInfo **res, ConfigReply *cr )
1343 slap_overinst *on = NULL, *on2 = NULL, **prev;
1344 slap_overinfo *oi = NULL;
1345 BackendInfo *bi = NULL;
1350 on = overlay_find( ov );
1352 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
1356 /* If this is the first overlay on this backend, set up the
1357 * overlay info structure
1359 if ( !overlay_is_over( be ) ) {
1362 /* NOTE: the first time a global overlay is configured,
1363 * frontendDB gets this flag; it is used later by overlays
1364 * to determine if they're stacked on top of the frontendDB */
1365 if ( be->bd_info == frontendDB->bd_info || SLAP_ISGLOBALOVERLAY( be ) ) {
1367 if ( on->on_bi.bi_flags & SLAPO_BFLAG_DBONLY ) {
1368 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1369 "overlay \"%s\" cannot be global.\n",
1374 } else if ( on->on_bi.bi_flags & SLAPO_BFLAG_GLOBONLY ) {
1375 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1376 "overlay \"%s\" can only be global.\n",
1381 oi = ch_malloc( sizeof( slap_overinfo ) );
1382 oi->oi_orig = be->bd_info;
1383 oi->oi_bi = *be->bd_info;
1387 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
1390 /* Save a pointer to ourself in bi_private.
1392 oi->oi_bi.bi_private = oi;
1394 bi = (BackendInfo *)oi;
1396 bi->bi_type = (char *)overtype;
1398 bi->bi_db_config = over_db_config;
1399 bi->bi_db_open = over_db_open;
1400 bi->bi_db_close = over_db_close;
1401 bi->bi_db_destroy = over_db_destroy;
1403 bi->bi_op_bind = over_op_bind;
1404 bi->bi_op_unbind = over_op_unbind;
1405 bi->bi_op_search = over_op_search;
1406 bi->bi_op_compare = over_op_compare;
1407 bi->bi_op_modify = over_op_modify;
1408 bi->bi_op_modrdn = over_op_modrdn;
1409 bi->bi_op_add = over_op_add;
1410 bi->bi_op_delete = over_op_delete;
1411 bi->bi_op_abandon = over_op_abandon;
1412 bi->bi_op_cancel = over_op_cancel;
1414 bi->bi_extended = over_op_extended;
1417 * this is fine because it has the same
1418 * args of the operations; we need to rework
1419 * all the hooks to share the same args
1420 * of the operations...
1422 bi->bi_operational = over_aux_operational;
1423 bi->bi_chk_referrals = over_aux_chk_referrals;
1424 bi->bi_chk_controls = over_aux_chk_controls;
1426 /* these have specific arglists */
1427 bi->bi_entry_get_rw = over_entry_get_rw;
1428 bi->bi_entry_release_rw = over_entry_release_rw;
1429 bi->bi_access_allowed = over_access_allowed;
1430 bi->bi_acl_group = over_acl_group;
1431 bi->bi_acl_attribute = over_acl_attribute;
1433 bi->bi_connection_init = over_connection_init;
1434 bi->bi_connection_destroy = over_connection_destroy;
1439 if ( overlay_is_inst( be, ov ) ) {
1440 if ( SLAPO_SINGLE( be ) ) {
1441 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
1442 "overlay \"%s\" already in list\n",
1448 oi = be->bd_info->bi_private;
1451 /* Insert new overlay into list. By default overlays are
1452 * added to head of list and executed in LIFO order.
1454 on2 = ch_calloc( 1, sizeof(slap_overinst) );
1458 prev = &oi->oi_list;
1459 /* Do we need to find the insertion point? */
1463 /* count current overlays */
1464 for ( i=0, on=oi->oi_list; on; on=on->on_next, i++ );
1466 /* are we just appending a new one? */
1470 overlay_insert( be, on2, &prev, idx );
1472 /* Any initialization needed? */
1473 if ( on2->on_bi.bi_db_init ) {
1475 be->bd_info = (BackendInfo *)on2;
1476 rc = on2->on_bi.bi_db_init( be, cr);
1477 be->bd_info = (BackendInfo *)oi;
1479 *prev = on2->on_next;