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;
79 if ( oi->oi_orig->bi_db_config ) {
80 be->bd_info = oi->oi_orig;
81 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
84 if ( be->bd_info != oi->oi_orig ) {
86 slap_overinst *on2, **onp;
90 /* a database added an overlay;
91 * work it around... */
92 assert( overlay_is_over( be ) );
94 oi2 = ( slap_overinfo * )be->bd_info->bi_private;
97 /* need to put a uniqueness check here as well;
98 * note that in principle there could be more than
99 * one overlay as a result of multiple calls to
100 * overlay_config() */
101 be2.bd_info = (BackendInfo *)oi;
103 for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
104 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
105 Debug( LDAP_DEBUG_ANY, "over_db_config(): "
106 "warning, freshly added "
107 "overlay #%d \"%s\" is already in list\n",
108 i, (*onp)->on_bi.bi_type, 0 );
110 /* NOTE: if the overlay already exists,
111 * there is no way to merge the results
112 * of the configuration that may have
113 * occurred during bi_db_config(); we
114 * just issue a warning, and the
115 * administrator should deal with this */
122 ch_free( be->bd_info );
125 be->bd_info = (BackendInfo *)oi;
126 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
134 for (; on; on=on->on_next) {
135 rc = SLAP_CONF_UNKNOWN;
136 if (on->on_bi.bi_cf_ocs) {
139 ct = config_find_keyword( on->on_bi.bi_cf_ocs->co_table, &ca );
141 rc = config_add_vals( ct, &ca );
144 if (on->on_bi.bi_db_config && rc == SLAP_CONF_UNKNOWN) {
145 be->bd_info = &on->on_bi;
146 rc = on->on_bi.bi_db_config( be, fname, lineno,
148 if ( rc != SLAP_CONF_UNKNOWN ) break;
151 be->bd_info = bi_orig;
160 return over_db_func( be, db_open );
168 slap_overinfo *oi = be->bd_info->bi_private;
169 slap_overinst *on = oi->oi_list;
170 BackendInfo *bi_orig = be->bd_info;
173 for (; on && rc == 0; on=on->on_next) {
174 be->bd_info = &on->on_bi;
175 if ( be->bd_info->bi_db_close ) {
176 rc = be->bd_info->bi_db_close( be );
180 if ( oi->oi_orig->bi_db_close ) {
181 be->bd_info = oi->oi_orig;
182 rc = be->bd_info->bi_db_close( be );
185 be->bd_info = bi_orig;
194 slap_overinfo *oi = be->bd_info->bi_private;
195 slap_overinst *on = oi->oi_list, *next;
198 rc = over_db_func( be, db_destroy );
200 for (next = on->on_next; on; on=next) {
209 over_back_response ( Operation *op, SlapReply *rs )
211 slap_overinfo *oi = op->o_callback->sc_private;
212 slap_overinst *on = oi->oi_list;
213 int rc = SLAP_CB_CONTINUE;
214 BackendDB *be = op->o_bd, db = *op->o_bd;
216 db.be_flags |= SLAP_DBFLAG_OVERLAY;
218 for (; on; on=on->on_next ) {
219 if ( on->on_response ) {
220 db.bd_info = (BackendInfo *)on;
221 rc = on->on_response( op, rs );
222 if ( rc != SLAP_CB_CONTINUE ) break;
242 op_aux_chk_referrals,
248 * default return code in case of missing backend function
249 * and overlay stack returning SLAP_CB_CONTINUE
251 static int op_rc[] = {
252 LDAP_UNWILLING_TO_PERFORM, /* bind */
253 LDAP_UNWILLING_TO_PERFORM, /* unbind */
254 LDAP_UNWILLING_TO_PERFORM, /* search */
255 SLAP_CB_CONTINUE, /* compare; pass to frontend */
256 LDAP_UNWILLING_TO_PERFORM, /* modify */
257 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
258 LDAP_UNWILLING_TO_PERFORM, /* add */
259 LDAP_UNWILLING_TO_PERFORM, /* delete */
260 LDAP_UNWILLING_TO_PERFORM, /* abandon */
261 LDAP_UNWILLING_TO_PERFORM, /* cancel */
262 LDAP_UNWILLING_TO_PERFORM, /* extended */
263 LDAP_SUCCESS, /* aux_operational */
264 LDAP_SUCCESS, /* aux_chk_referrals */
265 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
268 #ifdef SLAP_OVERLAY_ACCESS
273 AttributeDescription *desc,
275 slap_access_t access,
276 AccessControlState *state,
281 BackendInfo *bi = op->o_bd->bd_info;
282 BackendDB *be = op->o_bd, db;
283 int rc = SLAP_CB_CONTINUE;
285 /* FIXME: used to happen for instance during abandon
286 * when global overlays are used... */
287 assert( op->o_bd != NULL );
289 oi = op->o_bd->bd_info->bi_private;
292 for ( ; on; on = on->on_next ) {
293 if ( on->on_bi.bi_access_allowed ) {
294 /* NOTE: do not copy the structure until required */
295 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
297 db.be_flags |= SLAP_DBFLAG_OVERLAY;
301 op->o_bd->bd_info = (BackendInfo *)on;
302 rc = on->on_bi.bi_access_allowed( op, e,
303 desc, val, access, state, maskp );
304 if ( rc != SLAP_CB_CONTINUE ) break;
308 if ( rc == SLAP_CB_CONTINUE ) {
309 BI_access_allowed *bi_access_allowed;
311 /* if the database structure was changed, o_bd points to a
312 * copy of the structure; put the original bd_info in place */
313 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
314 op->o_bd->bd_info = oi->oi_orig;
317 if ( oi->oi_orig->bi_access_allowed ) {
318 bi_access_allowed = oi->oi_orig->bi_access_allowed;
320 bi_access_allowed = slap_access_allowed;
323 rc = bi_access_allowed( op, e,
324 desc, val, access, state, maskp );
326 /* should not fall thru this far without anything happening... */
327 if ( rc == SLAP_CB_CONTINUE ) {
328 /* access not allowed */
333 op->o_bd->bd_info = bi;
337 #endif /* SLAP_OVERLAY_ACCESS */
349 BackendDB *be = op->o_bd, db;
350 slap_callback cb = {NULL, over_back_response, NULL, NULL};
351 int rc = SLAP_CB_CONTINUE;
353 /* FIXME: used to happen for instance during abandon
354 * when global overlays are used... */
355 assert( op->o_bd != NULL );
357 oi = op->o_bd->bd_info->bi_private;
360 if ( !SLAP_ISOVERLAY( op->o_bd )) {
362 db.be_flags |= SLAP_DBFLAG_OVERLAY;
365 cb.sc_next = op->o_callback;
367 op->o_callback = &cb;
369 for (; on; on=on->on_next ) {
370 func = &on->on_bi.bi_op_bind;
372 op->o_bd->bd_info = (BackendInfo *)on;
373 rc = func[which]( op, rs );
374 if ( rc != SLAP_CB_CONTINUE ) break;
378 func = &oi->oi_orig->bi_op_bind;
379 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
380 op->o_bd->bd_info = oi->oi_orig;
381 rc = func[which]( op, rs );
383 /* should not fall thru this far without anything happening... */
384 if ( rc == SLAP_CB_CONTINUE ) {
388 /* The underlying backend didn't handle the request, make sure
389 * overlay cleanup is processed.
391 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
392 slap_callback *sc_next;
393 for ( ; op->o_callback && op->o_callback != cb.sc_next;
394 op->o_callback = sc_next ) {
395 sc_next = op->o_callback->sc_next;
396 if ( op->o_callback->sc_cleanup ) {
397 op->o_callback->sc_cleanup( op, rs );
402 op->o_callback = cb.sc_next;
407 over_op_bind( Operation *op, SlapReply *rs )
409 return over_op_func( op, rs, op_bind );
413 over_op_unbind( Operation *op, SlapReply *rs )
415 return over_op_func( op, rs, op_unbind );
419 over_op_search( Operation *op, SlapReply *rs )
421 return over_op_func( op, rs, op_search );
425 over_op_compare( Operation *op, SlapReply *rs )
427 return over_op_func( op, rs, op_compare );
431 over_op_modify( Operation *op, SlapReply *rs )
433 return over_op_func( op, rs, op_modify );
437 over_op_modrdn( Operation *op, SlapReply *rs )
439 return over_op_func( op, rs, op_modrdn );
443 over_op_add( Operation *op, SlapReply *rs )
445 return over_op_func( op, rs, op_add );
449 over_op_delete( Operation *op, SlapReply *rs )
451 return over_op_func( op, rs, op_delete );
455 over_op_abandon( Operation *op, SlapReply *rs )
457 return over_op_func( op, rs, op_abandon );
461 over_op_cancel( Operation *op, SlapReply *rs )
463 return over_op_func( op, rs, op_cancel );
467 over_op_extended( Operation *op, SlapReply *rs )
469 return over_op_func( op, rs, op_extended );
473 over_aux_operational( Operation *op, SlapReply *rs )
475 return over_op_func( op, rs, op_aux_operational );
479 over_aux_chk_referrals( Operation *op, SlapReply *rs )
481 return over_op_func( op, rs, op_aux_chk_referrals );
485 over_aux_chk_controls( Operation *op, SlapReply *rs )
487 return over_op_func( op, rs, op_aux_chk_controls );
491 over_connection_destroy(
499 int rc = SLAP_CB_CONTINUE;
501 /* FIXME: used to happen for instance during abandon
502 * when global overlays are used... */
503 assert( bd != NULL );
505 oi = bd->bd_info->bi_private;
508 if ( !SLAP_ISOVERLAY( bd )) {
510 db.be_flags |= SLAP_DBFLAG_OVERLAY;
514 for (; on; on=on->on_next ) {
515 if ( on->on_bi.bi_connection_destroy ) {
516 bd->bd_info = (BackendInfo *)on;
517 rc = on->on_bi.bi_connection_destroy( bd, conn );
518 if ( rc != SLAP_CB_CONTINUE ) break;
522 if ( oi->oi_orig->bi_connection_destroy && rc == SLAP_CB_CONTINUE ) {
523 bd->bd_info = oi->oi_orig;
524 rc = oi->oi_orig->bi_connection_destroy( bd, conn );
526 /* should not fall thru this far without anything happening... */
527 if ( rc == SLAP_CB_CONTINUE ) {
528 rc = LDAP_UNWILLING_TO_PERFORM;
539 on->on_next = overlays;
545 * iterator on registered overlays; overlay_next( NULL ) returns the first
546 * overlay; * subsequent calls with the previously returned value allow to
547 * iterate * over the entire list; returns NULL when no more overlays are
564 * returns a specific registered overlay based on the type; NULL if not
569 overlay_find( const char *over_type )
571 slap_overinst *on = overlays;
575 for ( ; on; on = on->on_next ) {
576 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
584 static const char overtype[] = "over";
587 * returns TRUE (1) if the database is actually an overlay instance;
588 * FALSE (0) otherwise.
592 overlay_is_over( BackendDB *be )
594 return be->bd_info->bi_type == overtype;
598 * returns TRUE (1) if the given database is actually an overlay
599 * instance and, somewhere in the list, contains the requested overlay;
600 * FALSE (0) otherwise.
604 overlay_is_inst( BackendDB *be, const char *over_type )
610 if ( !overlay_is_over( be ) ) {
614 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
615 for ( ; on; on = on->on_next ) {
616 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
625 overlay_register_control( BackendDB *be, const char *oid )
631 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
635 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
638 /* add to all backends... */
639 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
644 bd->be_ctrls[ cid ] = 1;
645 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
650 if ( rc == 0 && !gotit ) {
651 be->be_ctrls[ cid ] = 1;
652 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
658 /* add an overlay to a particular backend. */
660 overlay_config( BackendDB *be, const char *ov )
662 slap_overinst *on = NULL, *on2 = NULL;
663 slap_overinfo *oi = NULL;
664 BackendInfo *bi = NULL;
666 on = overlay_find( ov );
668 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
672 /* If this is the first overlay on this backend, set up the
673 * overlay info structure
675 if ( !overlay_is_over( be ) ) {
676 oi = ch_malloc( sizeof( slap_overinfo ) );
677 oi->oi_orig = be->bd_info;
678 oi->oi_bi = *be->bd_info;
680 /* NOTE: the first time a global overlay is configured,
681 * frontendDB gets this flag; it is used later by overlays
682 * to determine if they're stacked on top of the frontendDB */
683 if ( oi->oi_orig == frontendDB->bd_info ) {
684 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
687 /* Save a pointer to ourself in bi_private.
689 oi->oi_bi.bi_private = oi;
691 bi = (BackendInfo *)oi;
693 bi->bi_type = (char *)overtype;
695 bi->bi_db_config = over_db_config;
696 bi->bi_db_open = over_db_open;
697 bi->bi_db_close = over_db_close;
698 bi->bi_db_destroy = over_db_destroy;
700 bi->bi_op_bind = over_op_bind;
701 bi->bi_op_unbind = over_op_unbind;
702 bi->bi_op_search = over_op_search;
703 bi->bi_op_compare = over_op_compare;
704 bi->bi_op_modify = over_op_modify;
705 bi->bi_op_modrdn = over_op_modrdn;
706 bi->bi_op_add = over_op_add;
707 bi->bi_op_delete = over_op_delete;
708 bi->bi_op_abandon = over_op_abandon;
709 bi->bi_op_cancel = over_op_cancel;
711 bi->bi_extended = over_op_extended;
714 * this is fine because it has the same
715 * args of the operations; we need to rework
716 * all the hooks to share the same args
717 * of the operations...
719 bi->bi_operational = over_aux_operational;
720 bi->bi_chk_referrals = over_aux_chk_referrals;
721 bi->bi_chk_controls = over_aux_chk_controls;
723 #ifdef SLAP_OVERLAY_ACCESS
724 /* this has a specific arglist */
725 bi->bi_access_allowed = over_access_allowed;
726 #endif /* SLAP_OVERLAY_ACCESS */
728 bi->bi_connection_destroy = over_connection_destroy;
733 if ( overlay_is_inst( be, ov ) ) {
734 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
735 "warning, overlay \"%s\" "
736 "already in list\n", ov, 0, 0 );
739 oi = be->bd_info->bi_private;
742 /* Insert new overlay on head of list. Overlays are executed
743 * in reverse of config order...
745 on2 = ch_calloc( 1, sizeof(slap_overinst) );
748 on2->on_next = oi->oi_list;
751 /* Any initialization needed? */
752 if ( on->on_bi.bi_db_init ) {
754 be->bd_info = (BackendInfo *)on2;
755 rc = on2->on_bi.bi_db_init( be );
756 be->bd_info = (BackendInfo *)oi;