1 /* backover.c - backend overlay routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 /* Functions to overlay other modules over a backend. */
23 #include <ac/string.h>
24 #include <ac/socket.h>
30 static slap_overinst *overlays;
32 enum db_which { db_open = 0, db_close, db_destroy };
40 slap_overinfo *oi = be->bd_info->bi_private;
41 slap_overinst *on = oi->oi_list;
42 BackendInfo *bi_orig = be->bd_info;
46 func = &oi->oi_orig->bi_db_open;
48 be->bd_info = oi->oi_orig;
49 rc = func[which]( be );
52 for (; on && rc == 0; on=on->on_next) {
53 be->bd_info = &on->on_bi;
54 func = &on->on_bi.bi_db_open;
56 rc = func[which]( be );
59 be->bd_info = bi_orig;
72 slap_overinfo *oi = be->bd_info->bi_private;
73 slap_overinst *on = oi->oi_list;
74 BackendInfo *bi_orig = be->bd_info;
75 struct ConfigOCs *be_cf_ocs = be->be_cf_ocs;
80 if ( oi->oi_orig->bi_db_config ) {
81 be->bd_info = oi->oi_orig;
82 be->be_cf_ocs = oi->oi_orig->bi_cf_ocs;
83 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
86 if ( be->bd_info != oi->oi_orig ) {
88 slap_overinst *on2, **onp;
92 /* a database added an overlay;
93 * work it around... */
94 assert( overlay_is_over( be ) );
96 oi2 = ( slap_overinfo * )be->bd_info->bi_private;
99 /* need to put a uniqueness check here as well;
100 * note that in principle there could be more than
101 * one overlay as a result of multiple calls to
102 * overlay_config() */
103 be2.bd_info = (BackendInfo *)oi;
105 for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
106 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
107 Debug( LDAP_DEBUG_ANY, "over_db_config(): "
108 "warning, freshly added "
109 "overlay #%d \"%s\" is already in list\n",
110 i, (*onp)->on_bi.bi_type, 0 );
112 /* NOTE: if the overlay already exists,
113 * there is no way to merge the results
114 * of the configuration that may have
115 * occurred during bi_db_config(); we
116 * just issue a warning, and the
117 * administrator should deal with this */
124 ch_free( be->bd_info );
127 be->bd_info = (BackendInfo *)oi;
128 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
136 for (; on; on=on->on_next) {
137 rc = SLAP_CONF_UNKNOWN;
138 if (on->on_bi.bi_cf_ocs) {
141 ct = config_find_keyword( on->on_bi.bi_cf_ocs->co_table, &ca );
143 rc = config_add_vals( ct, &ca );
146 if (on->on_bi.bi_db_config && rc == SLAP_CONF_UNKNOWN) {
147 be->bd_info = &on->on_bi;
148 rc = on->on_bi.bi_db_config( be, fname, lineno,
150 if ( rc != SLAP_CONF_UNKNOWN ) break;
153 be->bd_info = bi_orig;
154 be->be_cf_ocs = be_cf_ocs;
164 return over_db_func( be, db_open );
172 slap_overinfo *oi = be->bd_info->bi_private;
173 slap_overinst *on = oi->oi_list;
174 BackendInfo *bi_orig = be->bd_info;
177 for (; on && rc == 0; on=on->on_next) {
178 be->bd_info = &on->on_bi;
179 if ( be->bd_info->bi_db_close ) {
180 rc = be->bd_info->bi_db_close( be );
184 if ( oi->oi_orig->bi_db_close ) {
185 be->bd_info = oi->oi_orig;
186 rc = be->bd_info->bi_db_close( be );
189 be->bd_info = bi_orig;
198 slap_overinfo *oi = be->bd_info->bi_private;
199 slap_overinst *on = oi->oi_list, *next;
202 rc = over_db_func( be, db_destroy );
204 for (next = on->on_next; on; on=next) {
213 over_back_response ( Operation *op, SlapReply *rs )
215 slap_overinfo *oi = op->o_callback->sc_private;
216 slap_overinst *on = oi->oi_list;
217 int rc = SLAP_CB_CONTINUE;
218 BackendDB *be = op->o_bd, db = *op->o_bd;
220 db.be_flags |= SLAP_DBFLAG_OVERLAY;
222 for (; on; on=on->on_next ) {
223 if ( on->on_response ) {
224 db.bd_info = (BackendInfo *)on;
225 rc = on->on_response( op, rs );
226 if ( rc != SLAP_CB_CONTINUE ) break;
246 op_aux_chk_referrals,
252 * default return code in case of missing backend function
253 * and overlay stack returning SLAP_CB_CONTINUE
255 static int op_rc[] = {
256 LDAP_UNWILLING_TO_PERFORM, /* bind */
257 LDAP_UNWILLING_TO_PERFORM, /* unbind */
258 LDAP_UNWILLING_TO_PERFORM, /* search */
259 SLAP_CB_CONTINUE, /* compare; pass to frontend */
260 LDAP_UNWILLING_TO_PERFORM, /* modify */
261 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
262 LDAP_UNWILLING_TO_PERFORM, /* add */
263 LDAP_UNWILLING_TO_PERFORM, /* delete */
264 LDAP_UNWILLING_TO_PERFORM, /* abandon */
265 LDAP_UNWILLING_TO_PERFORM, /* cancel */
266 LDAP_UNWILLING_TO_PERFORM, /* extended */
267 LDAP_SUCCESS, /* aux_operational */
268 LDAP_SUCCESS, /* aux_chk_referrals */
269 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
272 #ifdef SLAP_OVERLAY_ACCESS
277 AttributeDescription *desc,
279 slap_access_t access,
280 AccessControlState *state,
285 BackendInfo *bi = op->o_bd->bd_info;
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 oi = op->o_bd->bd_info->bi_private;
296 for ( ; on; on = on->on_next ) {
297 if ( on->on_bi.bi_access_allowed ) {
298 /* NOTE: do not copy the structure until required */
299 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
301 db.be_flags |= SLAP_DBFLAG_OVERLAY;
305 op->o_bd->bd_info = (BackendInfo *)on;
306 rc = on->on_bi.bi_access_allowed( op, e,
307 desc, val, access, state, maskp );
308 if ( rc != SLAP_CB_CONTINUE ) break;
312 if ( rc == SLAP_CB_CONTINUE ) {
313 BI_access_allowed *bi_access_allowed;
315 /* if the database structure was changed, o_bd points to a
316 * copy of the structure; put the original bd_info in place */
317 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
318 op->o_bd->bd_info = oi->oi_orig;
321 if ( oi->oi_orig->bi_access_allowed ) {
322 bi_access_allowed = oi->oi_orig->bi_access_allowed;
324 bi_access_allowed = slap_access_allowed;
327 rc = bi_access_allowed( op, e,
328 desc, val, access, state, maskp );
330 /* should not fall thru this far without anything happening... */
331 if ( rc == SLAP_CB_CONTINUE ) {
332 /* access not allowed */
337 op->o_bd->bd_info = bi;
341 #endif /* SLAP_OVERLAY_ACCESS */
353 BackendDB *be = op->o_bd, db;
354 slap_callback cb = {NULL, over_back_response, NULL, NULL};
355 int rc = SLAP_CB_CONTINUE;
357 /* FIXME: used to happen for instance during abandon
358 * when global overlays are used... */
359 assert( op->o_bd != NULL );
361 oi = op->o_bd->bd_info->bi_private;
364 if ( !SLAP_ISOVERLAY( op->o_bd )) {
366 db.be_flags |= SLAP_DBFLAG_OVERLAY;
369 cb.sc_next = op->o_callback;
371 op->o_callback = &cb;
373 for (; on; on=on->on_next ) {
374 func = &on->on_bi.bi_op_bind;
376 op->o_bd->bd_info = (BackendInfo *)on;
377 rc = func[which]( op, rs );
378 if ( rc != SLAP_CB_CONTINUE ) break;
382 func = &oi->oi_orig->bi_op_bind;
383 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
384 op->o_bd->bd_info = oi->oi_orig;
385 rc = func[which]( op, rs );
387 /* should not fall thru this far without anything happening... */
388 if ( rc == SLAP_CB_CONTINUE ) {
392 /* The underlying backend didn't handle the request, make sure
393 * overlay cleanup is processed.
395 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
396 slap_callback *sc_next;
397 for ( ; op->o_callback && op->o_callback != cb.sc_next;
398 op->o_callback = sc_next ) {
399 sc_next = op->o_callback->sc_next;
400 if ( op->o_callback->sc_cleanup ) {
401 op->o_callback->sc_cleanup( op, rs );
406 op->o_callback = cb.sc_next;
411 over_op_bind( Operation *op, SlapReply *rs )
413 return over_op_func( op, rs, op_bind );
417 over_op_unbind( Operation *op, SlapReply *rs )
419 return over_op_func( op, rs, op_unbind );
423 over_op_search( Operation *op, SlapReply *rs )
425 return over_op_func( op, rs, op_search );
429 over_op_compare( Operation *op, SlapReply *rs )
431 return over_op_func( op, rs, op_compare );
435 over_op_modify( Operation *op, SlapReply *rs )
437 return over_op_func( op, rs, op_modify );
441 over_op_modrdn( Operation *op, SlapReply *rs )
443 return over_op_func( op, rs, op_modrdn );
447 over_op_add( Operation *op, SlapReply *rs )
449 return over_op_func( op, rs, op_add );
453 over_op_delete( Operation *op, SlapReply *rs )
455 return over_op_func( op, rs, op_delete );
459 over_op_abandon( Operation *op, SlapReply *rs )
461 return over_op_func( op, rs, op_abandon );
465 over_op_cancel( Operation *op, SlapReply *rs )
467 return over_op_func( op, rs, op_cancel );
471 over_op_extended( Operation *op, SlapReply *rs )
473 return over_op_func( op, rs, op_extended );
477 over_aux_operational( Operation *op, SlapReply *rs )
479 return over_op_func( op, rs, op_aux_operational );
483 over_aux_chk_referrals( Operation *op, SlapReply *rs )
485 return over_op_func( op, rs, op_aux_chk_referrals );
489 over_aux_chk_controls( Operation *op, SlapReply *rs )
491 return over_op_func( op, rs, op_aux_chk_controls );
495 over_connection_destroy(
503 int rc = SLAP_CB_CONTINUE;
505 /* FIXME: used to happen for instance during abandon
506 * when global overlays are used... */
507 assert( bd != NULL );
509 oi = bd->bd_info->bi_private;
512 if ( !SLAP_ISOVERLAY( bd )) {
514 db.be_flags |= SLAP_DBFLAG_OVERLAY;
518 for (; on; on=on->on_next ) {
519 if ( on->on_bi.bi_connection_destroy ) {
520 bd->bd_info = (BackendInfo *)on;
521 rc = on->on_bi.bi_connection_destroy( bd, conn );
522 if ( rc != SLAP_CB_CONTINUE ) break;
526 if ( oi->oi_orig->bi_connection_destroy && rc == SLAP_CB_CONTINUE ) {
527 bd->bd_info = oi->oi_orig;
528 rc = oi->oi_orig->bi_connection_destroy( bd, conn );
530 /* should not fall thru this far without anything happening... */
531 if ( rc == SLAP_CB_CONTINUE ) {
532 rc = LDAP_UNWILLING_TO_PERFORM;
543 on->on_next = overlays;
549 * iterator on registered overlays; overlay_next( NULL ) returns the first
550 * overlay; * subsequent calls with the previously returned value allow to
551 * iterate * over the entire list; returns NULL when no more overlays are
568 * returns a specific registered overlay based on the type; NULL if not
573 overlay_find( const char *over_type )
575 slap_overinst *on = overlays;
579 for ( ; on; on = on->on_next ) {
580 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
588 static const char overtype[] = "over";
591 * returns TRUE (1) if the database is actually an overlay instance;
592 * FALSE (0) otherwise.
596 overlay_is_over( BackendDB *be )
598 return be->bd_info->bi_type == overtype;
602 * returns TRUE (1) if the given database is actually an overlay
603 * instance and, somewhere in the list, contains the requested overlay;
604 * FALSE (0) otherwise.
608 overlay_is_inst( BackendDB *be, const char *over_type )
614 if ( !overlay_is_over( be ) ) {
618 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
619 for ( ; on; on = on->on_next ) {
620 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
629 overlay_register_control( BackendDB *be, const char *oid )
635 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
639 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
642 /* add to all backends... */
643 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
648 bd->be_ctrls[ cid ] = 1;
649 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
654 if ( rc == 0 && !gotit ) {
655 be->be_ctrls[ cid ] = 1;
656 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
662 /* add an overlay to a particular backend. */
664 overlay_config( BackendDB *be, const char *ov )
666 slap_overinst *on = NULL, *on2 = NULL;
667 slap_overinfo *oi = NULL;
668 BackendInfo *bi = NULL;
670 on = overlay_find( ov );
672 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
676 /* If this is the first overlay on this backend, set up the
677 * overlay info structure
679 if ( !overlay_is_over( be ) ) {
680 oi = ch_malloc( sizeof( slap_overinfo ) );
681 oi->oi_orig = be->bd_info;
682 oi->oi_bi = *be->bd_info;
684 /* NOTE: the first time a global overlay is configured,
685 * frontendDB gets this flag; it is used later by overlays
686 * to determine if they're stacked on top of the frontendDB */
687 if ( oi->oi_orig == frontendDB->bd_info ) {
688 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
691 /* Save a pointer to ourself in bi_private.
693 oi->oi_bi.bi_private = oi;
695 bi = (BackendInfo *)oi;
697 bi->bi_type = (char *)overtype;
699 bi->bi_db_config = over_db_config;
700 bi->bi_db_open = over_db_open;
701 bi->bi_db_close = over_db_close;
702 bi->bi_db_destroy = over_db_destroy;
704 bi->bi_op_bind = over_op_bind;
705 bi->bi_op_unbind = over_op_unbind;
706 bi->bi_op_search = over_op_search;
707 bi->bi_op_compare = over_op_compare;
708 bi->bi_op_modify = over_op_modify;
709 bi->bi_op_modrdn = over_op_modrdn;
710 bi->bi_op_add = over_op_add;
711 bi->bi_op_delete = over_op_delete;
712 bi->bi_op_abandon = over_op_abandon;
713 bi->bi_op_cancel = over_op_cancel;
715 bi->bi_extended = over_op_extended;
718 * this is fine because it has the same
719 * args of the operations; we need to rework
720 * all the hooks to share the same args
721 * of the operations...
723 bi->bi_operational = over_aux_operational;
724 bi->bi_chk_referrals = over_aux_chk_referrals;
725 bi->bi_chk_controls = over_aux_chk_controls;
727 #ifdef SLAP_OVERLAY_ACCESS
728 /* this has a specific arglist */
729 bi->bi_access_allowed = over_access_allowed;
730 #endif /* SLAP_OVERLAY_ACCESS */
732 bi->bi_connection_destroy = over_connection_destroy;
737 if ( overlay_is_inst( be, ov ) ) {
738 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
739 "warning, overlay \"%s\" "
740 "already in list\n", ov, 0, 0 );
743 oi = be->bd_info->bi_private;
746 /* Insert new overlay on head of list. Overlays are executed
747 * in reverse of config order...
749 on2 = ch_calloc( 1, sizeof(slap_overinst) );
752 on2->on_next = oi->oi_list;
755 /* Any initialization needed? */
756 if ( on->on_bi.bi_db_init ) {
758 be->bd_info = (BackendInfo *)on2;
759 rc = on2->on_bi.bi_db_init( be );
760 be->bd_info = (BackendInfo *)oi;