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>
29 static slap_overinst *overlays;
31 enum db_which { db_open = 0, db_close, db_destroy };
39 slap_overinfo *oi = be->bd_info->bi_private;
40 slap_overinst *on = oi->oi_list;
41 BackendInfo *bi_orig = be->bd_info;
45 func = &oi->oi_orig->bi_db_open;
47 be->bd_info = oi->oi_orig;
48 rc = func[which]( be );
51 for (; on && rc == 0; on=on->on_next) {
52 be->bd_info = &on->on_bi;
53 func = &on->on_bi.bi_db_open;
55 rc = func[which]( be );
58 be->bd_info = bi_orig;
71 slap_overinfo *oi = be->bd_info->bi_private;
72 slap_overinst *on = oi->oi_list;
73 BackendInfo *bi_orig = be->bd_info;
76 if ( oi->oi_orig->bi_db_config ) {
77 be->bd_info = oi->oi_orig;
78 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
81 if ( be->bd_info != oi->oi_orig ) {
83 slap_overinst *on2, **onp;
87 /* a database added an overlay;
88 * work it around... */
89 assert( overlay_is_over( be ) );
91 oi2 = ( slap_overinfo * )be->bd_info->bi_private;
94 /* need to put a uniqueness check here as well;
95 * note that in principle there could be more than
96 * one overlay as a result of multiple calls to
98 be2.bd_info = (BackendInfo *)oi;
100 for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
101 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
102 Debug( LDAP_DEBUG_ANY, "over_db_config(): "
103 "warning, freshly added "
104 "overlay #%d \"%s\" is already in list\n",
105 i, (*onp)->on_bi.bi_type, 0 );
107 /* NOTE: if the overlay already exists,
108 * there is no way to merge the results
109 * of the configuration that may have
110 * occurred during bi_db_config(); we
111 * just issue a warning, and the
112 * administrator should deal with this */
119 ch_free( be->bd_info );
122 be->bd_info = (BackendInfo *)oi;
123 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
126 for (; on; on=on->on_next) {
127 if (on->on_bi.bi_db_config) {
128 be->bd_info = &on->on_bi;
129 rc = on->on_bi.bi_db_config( be, fname, lineno,
131 if ( rc != SLAP_CONF_UNKNOWN ) break;
134 be->bd_info = bi_orig;
143 return over_db_func( be, db_open );
151 slap_overinfo *oi = be->bd_info->bi_private;
152 slap_overinst *on = oi->oi_list;
153 BackendInfo *bi_orig = be->bd_info;
156 for (; on && rc == 0; on=on->on_next) {
157 be->bd_info = &on->on_bi;
158 if ( be->bd_info->bi_db_close ) {
159 rc = be->bd_info->bi_db_close( be );
163 if ( oi->oi_orig->bi_db_close ) {
164 be->bd_info = oi->oi_orig;
165 rc = be->bd_info->bi_db_close( be );
168 be->bd_info = bi_orig;
177 slap_overinfo *oi = be->bd_info->bi_private;
178 slap_overinst *on = oi->oi_list, *next;
181 rc = over_db_func( be, db_destroy );
183 for (next = on->on_next; on; on=next) {
192 over_back_response ( Operation *op, SlapReply *rs )
194 slap_overinfo *oi = op->o_callback->sc_private;
195 slap_overinst *on = oi->oi_list;
196 int rc = SLAP_CB_CONTINUE;
197 BackendDB *be = op->o_bd, db = *op->o_bd;
199 db.be_flags |= SLAP_DBFLAG_OVERLAY;
201 for (; on; on=on->on_next ) {
202 if ( on->on_response ) {
203 db.bd_info = (BackendInfo *)on;
204 rc = on->on_response( op, rs );
205 if ( rc != SLAP_CB_CONTINUE ) break;
225 op_aux_chk_referrals,
231 * default return code in case of missing backend function
232 * and overlay stack returning SLAP_CB_CONTINUE
234 static int op_rc[] = {
235 LDAP_UNWILLING_TO_PERFORM, /* bind */
236 LDAP_UNWILLING_TO_PERFORM, /* unbind */
237 LDAP_UNWILLING_TO_PERFORM, /* search */
238 SLAP_CB_CONTINUE, /* compare; pass to frontend */
239 LDAP_UNWILLING_TO_PERFORM, /* modify */
240 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
241 LDAP_UNWILLING_TO_PERFORM, /* add */
242 LDAP_UNWILLING_TO_PERFORM, /* delete */
243 LDAP_UNWILLING_TO_PERFORM, /* abandon */
244 LDAP_UNWILLING_TO_PERFORM, /* cancel */
245 LDAP_UNWILLING_TO_PERFORM, /* extended */
246 LDAP_SUCCESS, /* aux_operational */
247 LDAP_SUCCESS, /* aux_chk_referrals */
248 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
251 #ifdef SLAP_OVERLAY_ACCESS
256 AttributeDescription *desc,
258 slap_access_t access,
259 AccessControlState *state,
264 BackendInfo *bi = op->o_bd->bd_info;
265 BackendDB *be = op->o_bd, db;
266 int rc = SLAP_CB_CONTINUE;
268 /* FIXME: used to happen for instance during abandon
269 * when global overlays are used... */
270 assert( op->o_bd != NULL );
272 oi = op->o_bd->bd_info->bi_private;
275 for ( ; on; on = on->on_next ) {
276 if ( on->on_bi.bi_access_allowed ) {
277 /* NOTE: do not copy the structure until required */
278 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
280 db.be_flags |= SLAP_DBFLAG_OVERLAY;
284 op->o_bd->bd_info = (BackendInfo *)on;
285 rc = on->on_bi.bi_access_allowed( op, e,
286 desc, val, access, state, maskp );
287 if ( rc != SLAP_CB_CONTINUE ) break;
291 if ( rc == SLAP_CB_CONTINUE ) {
292 BI_access_allowed *bi_access_allowed;
294 /* if the database structure was changed, o_bd points to a
295 * copy of the structure; put the original bd_info in place */
296 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
297 op->o_bd->bd_info = oi->oi_orig;
300 if ( oi->oi_orig->bi_access_allowed ) {
301 bi_access_allowed = oi->oi_orig->bi_access_allowed;
303 bi_access_allowed = slap_access_allowed;
306 rc = bi_access_allowed( op, e,
307 desc, val, access, state, maskp );
309 /* should not fall thru this far without anything happening... */
310 if ( rc == SLAP_CB_CONTINUE ) {
311 /* access not allowed */
316 op->o_bd->bd_info = bi;
320 #endif /* SLAP_OVERLAY_ACCESS */
332 BackendDB *be = op->o_bd, db;
333 slap_callback cb = {NULL, over_back_response, NULL, NULL};
334 int rc = SLAP_CB_CONTINUE;
336 /* FIXME: used to happen for instance during abandon
337 * when global overlays are used... */
338 assert( op->o_bd != NULL );
340 oi = op->o_bd->bd_info->bi_private;
343 if ( !SLAP_ISOVERLAY( op->o_bd )) {
345 db.be_flags |= SLAP_DBFLAG_OVERLAY;
348 cb.sc_next = op->o_callback;
350 op->o_callback = &cb;
352 for (; on; on=on->on_next ) {
353 func = &on->on_bi.bi_op_bind;
355 op->o_bd->bd_info = (BackendInfo *)on;
356 rc = func[which]( op, rs );
357 if ( rc != SLAP_CB_CONTINUE ) break;
361 func = &oi->oi_orig->bi_op_bind;
362 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
363 op->o_bd->bd_info = oi->oi_orig;
364 rc = func[which]( op, rs );
366 /* should not fall thru this far without anything happening... */
367 if ( rc == SLAP_CB_CONTINUE ) {
371 /* The underlying backend didn't handle the request, make sure
372 * overlay cleanup is processed.
374 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
375 slap_callback *sc_next;
376 for ( ; op->o_callback && op->o_callback != cb.sc_next;
377 op->o_callback = sc_next ) {
378 sc_next = op->o_callback->sc_next;
379 if ( op->o_callback->sc_cleanup ) {
380 op->o_callback->sc_cleanup( op, rs );
385 op->o_callback = cb.sc_next;
390 over_op_bind( Operation *op, SlapReply *rs )
392 return over_op_func( op, rs, op_bind );
396 over_op_unbind( Operation *op, SlapReply *rs )
398 return over_op_func( op, rs, op_unbind );
402 over_op_search( Operation *op, SlapReply *rs )
404 return over_op_func( op, rs, op_search );
408 over_op_compare( Operation *op, SlapReply *rs )
410 return over_op_func( op, rs, op_compare );
414 over_op_modify( Operation *op, SlapReply *rs )
416 return over_op_func( op, rs, op_modify );
420 over_op_modrdn( Operation *op, SlapReply *rs )
422 return over_op_func( op, rs, op_modrdn );
426 over_op_add( Operation *op, SlapReply *rs )
428 return over_op_func( op, rs, op_add );
432 over_op_delete( Operation *op, SlapReply *rs )
434 return over_op_func( op, rs, op_delete );
438 over_op_abandon( Operation *op, SlapReply *rs )
440 return over_op_func( op, rs, op_abandon );
444 over_op_cancel( Operation *op, SlapReply *rs )
446 return over_op_func( op, rs, op_cancel );
450 over_op_extended( Operation *op, SlapReply *rs )
452 return over_op_func( op, rs, op_extended );
456 over_aux_operational( Operation *op, SlapReply *rs )
458 return over_op_func( op, rs, op_aux_operational );
462 over_aux_chk_referrals( Operation *op, SlapReply *rs )
464 return over_op_func( op, rs, op_aux_chk_referrals );
468 over_aux_chk_controls( Operation *op, SlapReply *rs )
470 return over_op_func( op, rs, op_aux_chk_controls );
474 over_connection_destroy(
482 int rc = SLAP_CB_CONTINUE;
484 /* FIXME: used to happen for instance during abandon
485 * when global overlays are used... */
486 assert( bd != NULL );
488 oi = bd->bd_info->bi_private;
491 if ( !SLAP_ISOVERLAY( bd )) {
493 db.be_flags |= SLAP_DBFLAG_OVERLAY;
497 for (; on; on=on->on_next ) {
498 if ( on->on_bi.bi_connection_destroy ) {
499 bd->bd_info = (BackendInfo *)on;
500 rc = on->on_bi.bi_connection_destroy( bd, conn );
501 if ( rc != SLAP_CB_CONTINUE ) break;
505 if ( oi->oi_orig->bi_connection_destroy && rc == SLAP_CB_CONTINUE ) {
506 bd->bd_info = oi->oi_orig;
507 rc = oi->oi_orig->bi_connection_destroy( bd, conn );
509 /* should not fall thru this far without anything happening... */
510 if ( rc == SLAP_CB_CONTINUE ) {
511 rc = LDAP_UNWILLING_TO_PERFORM;
522 on->on_next = overlays;
528 * iterator on registered overlays; overlay_next( NULL ) returns the first
529 * overlay; * subsequent calls with the previously returned value allow to
530 * iterate * over the entire list; returns NULL when no more overlays are
547 * returns a specific registered overlay based on the type; NULL if not
552 overlay_find( const char *over_type )
554 slap_overinst *on = overlays;
558 for ( ; on; on = on->on_next ) {
559 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
567 static const char overtype[] = "over";
570 * returns TRUE (1) if the database is actually an overlay instance;
571 * FALSE (0) otherwise.
575 overlay_is_over( BackendDB *be )
577 return be->bd_info->bi_type == overtype;
581 * returns TRUE (1) if the given database is actually an overlay
582 * instance and, somewhere in the list, contains the requested overlay;
583 * FALSE (0) otherwise.
587 overlay_is_inst( BackendDB *be, const char *over_type )
593 if ( !overlay_is_over( be ) ) {
597 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
598 for ( ; on; on = on->on_next ) {
599 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
608 overlay_register_control( BackendDB *be, const char *oid )
614 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
618 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
621 /* add to all backends... */
622 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
627 bd->be_ctrls[ cid ] = 1;
628 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
633 if ( rc == 0 && !gotit ) {
634 be->be_ctrls[ cid ] = 1;
635 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
641 /* add an overlay to a particular backend. */
643 overlay_config( BackendDB *be, const char *ov )
645 slap_overinst *on = NULL, *on2 = NULL;
646 slap_overinfo *oi = NULL;
647 BackendInfo *bi = NULL;
649 on = overlay_find( ov );
651 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
655 /* If this is the first overlay on this backend, set up the
656 * overlay info structure
658 if ( !overlay_is_over( be ) ) {
659 oi = ch_malloc( sizeof( slap_overinfo ) );
660 oi->oi_orig = be->bd_info;
661 oi->oi_bi = *be->bd_info;
663 /* NOTE: the first time a global overlay is configured,
664 * frontendDB gets this flag; it is used later by overlays
665 * to determine if they're stacked on top of the frontendDB */
666 if ( oi->oi_orig == frontendDB->bd_info ) {
667 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
670 /* Save a pointer to ourself in bi_private.
672 oi->oi_bi.bi_private = oi;
674 bi = (BackendInfo *)oi;
676 bi->bi_type = (char *)overtype;
678 bi->bi_db_config = over_db_config;
679 bi->bi_db_open = over_db_open;
680 bi->bi_db_close = over_db_close;
681 bi->bi_db_destroy = over_db_destroy;
683 bi->bi_op_bind = over_op_bind;
684 bi->bi_op_unbind = over_op_unbind;
685 bi->bi_op_search = over_op_search;
686 bi->bi_op_compare = over_op_compare;
687 bi->bi_op_modify = over_op_modify;
688 bi->bi_op_modrdn = over_op_modrdn;
689 bi->bi_op_add = over_op_add;
690 bi->bi_op_delete = over_op_delete;
691 bi->bi_op_abandon = over_op_abandon;
692 bi->bi_op_cancel = over_op_cancel;
694 bi->bi_extended = over_op_extended;
697 * this is fine because it has the same
698 * args of the operations; we need to rework
699 * all the hooks to share the same args
700 * of the operations...
702 bi->bi_operational = over_aux_operational;
703 bi->bi_chk_referrals = over_aux_chk_referrals;
704 bi->bi_chk_controls = over_aux_chk_controls;
706 #ifdef SLAP_OVERLAY_ACCESS
707 /* this has a specific arglist */
708 bi->bi_access_allowed = over_access_allowed;
709 #endif /* SLAP_OVERLAY_ACCESS */
711 bi->bi_connection_destroy = over_connection_destroy;
716 if ( overlay_is_inst( be, ov ) ) {
717 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
718 "warning, overlay \"%s\" "
719 "already in list\n", ov, 0, 0 );
722 oi = be->bd_info->bi_private;
725 /* Insert new overlay on head of list. Overlays are executed
726 * in reverse of config order...
728 on2 = ch_calloc( 1, sizeof(slap_overinst) );
731 on2->on_next = oi->oi_list;
734 /* Any initialization needed? */
735 if ( on->on_bi.bi_db_init ) {
737 be->bd_info = (BackendInfo *)on2;
738 rc = on2->on_bi.bi_db_init( be );
739 be->bd_info = (BackendInfo *)oi;