1 /* backover.c - backend overlay routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 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 return over_db_func( be, db_close );
159 slap_overinfo *oi = be->bd_info->bi_private;
160 slap_overinst *on = oi->oi_list, *next;
163 rc = over_db_func( be, db_destroy );
165 for (next = on->on_next; on; on=next) {
174 over_back_response ( Operation *op, SlapReply *rs )
176 slap_overinfo *oi = op->o_callback->sc_private;
177 slap_overinst *on = oi->oi_list;
178 int rc = SLAP_CB_CONTINUE;
179 BackendDB *be = op->o_bd, db = *op->o_bd;
181 db.be_flags |= SLAP_DBFLAG_OVERLAY;
183 for (; on; on=on->on_next ) {
184 if ( on->on_response ) {
185 db.bd_info = (BackendInfo *)on;
186 rc = on->on_response( op, rs );
187 if ( rc != SLAP_CB_CONTINUE ) break;
207 op_aux_chk_referrals,
212 * default return code in case of missing backend function
213 * and overlay stack returning SLAP_CB_CONTINUE
215 static int op_rc[] = {
216 LDAP_UNWILLING_TO_PERFORM, /* bind */
217 LDAP_UNWILLING_TO_PERFORM, /* unbind */
218 LDAP_UNWILLING_TO_PERFORM, /* search */
219 LDAP_UNWILLING_TO_PERFORM, /* compare */
220 LDAP_UNWILLING_TO_PERFORM, /* modify */
221 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
222 LDAP_UNWILLING_TO_PERFORM, /* add */
223 LDAP_UNWILLING_TO_PERFORM, /* delete */
224 LDAP_UNWILLING_TO_PERFORM, /* abandon */
225 LDAP_UNWILLING_TO_PERFORM, /* cancel */
226 LDAP_UNWILLING_TO_PERFORM, /* extended */
227 LDAP_SUCCESS, /* aux_operational */
228 LDAP_SUCCESS /* aux_chk_referrals */
238 slap_overinfo *oi = op->o_bd->bd_info->bi_private;
239 slap_overinst *on = oi->oi_list;
241 BackendDB *be = op->o_bd, db = *op->o_bd;
242 slap_callback cb = {NULL, over_back_response, NULL, NULL};
243 int rc = SLAP_CB_CONTINUE;
245 db.be_flags |= SLAP_DBFLAG_OVERLAY;
247 cb.sc_next = op->o_callback;
249 op->o_callback = &cb;
251 for (; on; on=on->on_next ) {
252 func = &on->on_bi.bi_op_bind;
254 db.bd_info = (BackendInfo *)on;
255 rc = func[which]( op, rs );
256 if ( rc != SLAP_CB_CONTINUE ) break;
260 func = &oi->oi_orig->bi_op_bind;
261 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
262 db.bd_info = oi->oi_orig;
263 rc = func[which]( op, rs );
265 /* should not fall thru this far without anything happening... */
266 if ( rc == SLAP_CB_CONTINUE ) {
270 op->o_callback = cb.sc_next;
275 over_op_bind( Operation *op, SlapReply *rs )
277 return over_op_func( op, rs, op_bind );
281 over_op_unbind( Operation *op, SlapReply *rs )
283 return over_op_func( op, rs, op_unbind );
287 over_op_search( Operation *op, SlapReply *rs )
289 return over_op_func( op, rs, op_search );
293 over_op_compare( Operation *op, SlapReply *rs )
295 return over_op_func( op, rs, op_compare );
299 over_op_modify( Operation *op, SlapReply *rs )
301 return over_op_func( op, rs, op_modify );
305 over_op_modrdn( Operation *op, SlapReply *rs )
307 return over_op_func( op, rs, op_modrdn );
311 over_op_add( Operation *op, SlapReply *rs )
313 return over_op_func( op, rs, op_add );
317 over_op_delete( Operation *op, SlapReply *rs )
319 return over_op_func( op, rs, op_delete );
323 over_op_abandon( Operation *op, SlapReply *rs )
325 return over_op_func( op, rs, op_abandon );
329 over_op_cancel( Operation *op, SlapReply *rs )
331 return over_op_func( op, rs, op_cancel );
335 over_op_extended( Operation *op, SlapReply *rs )
337 return over_op_func( op, rs, op_extended );
341 over_aux_operational( Operation *op, SlapReply *rs )
343 return over_op_func( op, rs, op_aux_operational );
347 over_aux_chk_referrals( Operation *op, SlapReply *rs )
349 return over_op_func( op, rs, op_aux_chk_referrals );
357 on->on_next = overlays;
363 * iterator on registered overlays; overlay_next( NULL ) returns the first
364 * overlay; * subsequent calls with the previously returned value allow to
365 * iterate * over the entire list; returns NULL when no more overlays are
382 * returns a specific registered overlay based on the type; NULL if not
387 overlay_find( const char *over_type )
389 slap_overinst *on = overlays;
393 for ( ; on; on = on->on_next ) {
394 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
402 static const char overtype[] = "over";
405 * returns TRUE (1) if the database is actually an overlay instance;
406 * FALSE (0) otherwise.
410 overlay_is_over( BackendDB *be )
412 return be->bd_info->bi_type == overtype;
416 * returns TRUE (1) if the given database is actually an overlay
417 * instance and, somewhere in the list, contains the requested overlay;
418 * FALSE (0) otherwise.
422 overlay_is_inst( BackendDB *be, const char *over_type )
428 if ( !overlay_is_over( be ) ) {
432 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
433 for ( ; on; on = on->on_next ) {
434 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
442 /* add an overlay to a particular backend. */
444 overlay_config( BackendDB *be, const char *ov )
446 slap_overinst *on = NULL, *on2 = NULL;
447 slap_overinfo *oi = NULL;
448 BackendInfo *bi = NULL;
450 on = overlay_find( ov );
452 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
456 /* If this is the first overlay on this backend, set up the
457 * overlay info structure
459 if ( !overlay_is_over( be ) ) {
460 oi = ch_malloc( sizeof( slap_overinfo ) );
461 oi->oi_orig = be->bd_info;
462 oi->oi_bi = *be->bd_info;
464 /* Save a pointer to ourself in bi_private.
465 * This allows us to keep working in conjunction
468 oi->oi_bi.bi_private = oi;
470 bi = (BackendInfo *)oi;
472 bi->bi_type = (char *)overtype;
474 bi->bi_db_config = over_db_config;
475 bi->bi_db_open = over_db_open;
476 bi->bi_db_close = over_db_close;
477 bi->bi_db_destroy = over_db_destroy;
479 bi->bi_op_bind = over_op_bind;
480 bi->bi_op_unbind = over_op_unbind;
481 bi->bi_op_search = over_op_search;
482 bi->bi_op_compare = over_op_compare;
483 bi->bi_op_modify = over_op_modify;
484 bi->bi_op_modrdn = over_op_modrdn;
485 bi->bi_op_add = over_op_add;
486 bi->bi_op_delete = over_op_delete;
487 bi->bi_op_abandon = over_op_abandon;
488 bi->bi_op_cancel = over_op_cancel;
490 bi->bi_extended = over_op_extended;
493 * this is fine because it has the same
494 * args of the operations; we need to rework
495 * all the hooks to share the same args
496 * of the operations...
498 bi->bi_operational = over_aux_operational;
499 bi->bi_chk_referrals = over_aux_chk_referrals;
504 if ( overlay_is_inst( be, ov ) ) {
505 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
506 "warning, overlay \"%s\" "
507 "already in list\n", ov, 0, 0 );
510 oi = be->bd_info->bi_private;
513 /* Insert new overlay on head of list. Overlays are executed
514 * in reverse of config order...
516 on2 = ch_calloc( 1, sizeof(slap_overinst) );
519 on2->on_next = oi->oi_list;
522 /* Any initialization needed? */
523 if ( on->on_bi.bi_db_init ) {
524 be->bd_info = (BackendInfo *)on2;
525 on2->on_bi.bi_db_init( be );
526 be->bd_info = (BackendInfo *)oi;