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 = (slap_overinfo *) be->bd_info;
40 slap_overinst *on = oi->oi_list;
44 func = &oi->oi_orig->bi_db_open;
46 be->bd_info = oi->oi_orig;
47 rc = func[which]( be );
50 for (; on && rc == 0; on=on->on_next) {
51 be->bd_info = &on->on_bi;
52 func = &on->on_bi.bi_db_open;
54 rc = func[which]( be );
57 be->bd_info = (BackendInfo *)oi;
70 slap_overinfo *oi = (slap_overinfo *) be->bd_info;
71 slap_overinst *on = oi->oi_list;
74 if ( oi->oi_orig->bi_db_config ) {
75 be->bd_info = oi->oi_orig;
76 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
78 be->bd_info = (BackendInfo *)oi;
79 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
82 for (; on; on=on->on_next) {
83 if (on->on_bi.bi_db_config) {
84 be->bd_info = &on->on_bi;
85 rc = on->on_bi.bi_db_config( be, fname, lineno,
87 if ( rc != SLAP_CONF_UNKNOWN ) break;
90 be->bd_info = (BackendInfo *)oi;
99 return over_db_func( be, db_open );
107 return over_db_func( be, db_close );
115 slap_overinfo *oi = (slap_overinfo *) be->bd_info;
116 slap_overinst *on = oi->oi_list, *next;
119 rc = over_db_func( be, db_destroy );
121 for (next = on->on_next; on; on=next) {
130 over_back_response ( Operation *op, SlapReply *rs )
132 slap_overinfo *oi = op->o_callback->sc_private;
133 slap_overinst *on = oi->oi_list;
134 int rc = SLAP_CB_CONTINUE;
135 BackendDB *be = op->o_bd, db = *op->o_bd;
138 for (; on; on=on->on_next ) {
139 if ( on->on_response ) {
140 db.bd_info = (BackendInfo *)on;
141 rc = on->on_response( op, rs );
142 if ( rc != SLAP_CB_CONTINUE ) break;
161 op_aux_chk_referrals,
172 slap_overinfo *oi = (slap_overinfo *) op->o_bd->bd_info;
173 slap_overinst *on = oi->oi_list;
175 BackendDB *be = op->o_bd, db = *op->o_bd;
176 slap_callback cb = {NULL, over_back_response, NULL, NULL};
177 int rc = SLAP_CB_CONTINUE;
180 cb.sc_next = op->o_callback;
182 op->o_callback = &cb;
184 for (; on; on=on->on_next ) {
185 func = &on->on_bi.bi_op_bind;
187 db.bd_info = (BackendInfo *)on;
188 rc = func[which]( op, rs );
189 if ( rc != SLAP_CB_CONTINUE ) break;
194 func = &oi->oi_orig->bi_op_bind;
195 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
196 rc = func[which]( op, rs );
198 /* should not fall thru this far without anything happening... */
199 if ( rc == SLAP_CB_CONTINUE ) {
200 rc = LDAP_UNWILLING_TO_PERFORM;
202 op->o_callback = cb.sc_next;
207 over_op_bind( Operation *op, SlapReply *rs )
209 return over_op_func( op, rs, op_bind );
213 over_op_unbind( Operation *op, SlapReply *rs )
215 return over_op_func( op, rs, op_unbind );
219 over_op_search( Operation *op, SlapReply *rs )
221 return over_op_func( op, rs, op_search );
225 over_op_compare( Operation *op, SlapReply *rs )
227 return over_op_func( op, rs, op_compare );
231 over_op_modify( Operation *op, SlapReply *rs )
233 return over_op_func( op, rs, op_modify );
237 over_op_modrdn( Operation *op, SlapReply *rs )
239 return over_op_func( op, rs, op_modrdn );
243 over_op_add( Operation *op, SlapReply *rs )
245 return over_op_func( op, rs, op_add );
249 over_op_delete( Operation *op, SlapReply *rs )
251 return over_op_func( op, rs, op_delete );
255 over_op_abandon( Operation *op, SlapReply *rs )
257 return over_op_func( op, rs, op_abandon );
261 over_op_cancel( Operation *op, SlapReply *rs )
263 return over_op_func( op, rs, op_cancel );
267 over_op_extended( Operation *op, SlapReply *rs )
269 return over_op_func( op, rs, op_extended );
273 over_chk_referrals( Operation *op, SlapReply *rs )
275 return over_op_func( op, rs, op_aux_chk_referrals );
283 on->on_next = overlays;
300 static const char overtype[] = "over";
302 /* add an overlay to a particular backend. */
304 overlay_config( BackendDB *be, const char *ov )
306 slap_overinst *on = NULL, *on2 = NULL, *prev = NULL;
307 slap_overinfo *oi = NULL;
308 BackendInfo *bi = NULL;
310 for ( on = overlays; on; on=on->on_next ) {
311 if (!strcmp( ov, on->on_bi.bi_type ) )
315 Debug( LDAP_DEBUG_ANY, "overlay %s not found\n", ov, 0, 0 );
319 /* If this is the first overlay on this backend, set up the
320 * overlay info structure
322 if ( be->bd_info->bi_type != overtype ) {
323 oi = ch_malloc( sizeof(slap_overinfo) );
324 oi->oi_orig = be->bd_info;
325 oi->oi_bi = *be->bd_info;
327 bi = (BackendInfo *)oi;
329 bi->bi_type = (char *)overtype;
331 bi->bi_db_config = over_db_config;
332 bi->bi_db_open = over_db_open;
333 bi->bi_db_close = over_db_close;
334 bi->bi_db_destroy = over_db_destroy;
336 bi->bi_op_bind = over_op_bind;
337 bi->bi_op_unbind = over_op_unbind;
338 bi->bi_op_search = over_op_search;
339 bi->bi_op_compare = over_op_compare;
340 bi->bi_op_modify = over_op_modify;
341 bi->bi_op_modrdn = over_op_modrdn;
342 bi->bi_op_add = over_op_add;
343 bi->bi_op_delete = over_op_delete;
344 bi->bi_op_abandon = over_op_abandon;
345 bi->bi_op_cancel = over_op_cancel;
347 bi->bi_extended = over_op_extended;
350 * this is fine because it has the same
351 * args of the operations; we need to rework
352 * all the hooks to share the same args
353 * of the operations...
355 bi->bi_chk_referrals = over_chk_referrals;
360 oi = (slap_overinfo *) be->bd_info;
364 /* Walk to the end of the list of overlays, add the new
367 for ( prev=NULL, on2 = oi->oi_list; on2; prev=on2, on2=on2->on_next );
368 on2 = ch_calloc( 1, sizeof(slap_overinst) );
378 /* Insert new overlay on head of list. Overlays are executed
379 * in reverse of config order...
381 on2 = ch_calloc( 1, sizeof(slap_overinst) );
384 on2->on_next = oi->oi_list;
388 /* Any initialization needed? */
389 if ( on->on_bi.bi_db_init ) {
390 be->bd_info = (BackendInfo *)on2;
391 on2->on_bi.bi_db_init( be );
392 be->bd_info = (BackendInfo *)oi;