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,
80 be->bd_info = (BackendInfo *)oi;
81 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
84 for (; on; on=on->on_next) {
85 if (on->on_bi.bi_db_config) {
86 be->bd_info = &on->on_bi;
87 rc = on->on_bi.bi_db_config( be, fname, lineno,
89 if ( rc != SLAP_CONF_UNKNOWN ) break;
92 be->bd_info = bi_orig;
101 return over_db_func( be, db_open );
109 return over_db_func( be, db_close );
117 slap_overinfo *oi = be->bd_info->bi_private;
118 slap_overinst *on = oi->oi_list, *next;
121 rc = over_db_func( be, db_destroy );
123 for (next = on->on_next; on; on=next) {
132 over_back_response ( Operation *op, SlapReply *rs )
134 slap_overinfo *oi = op->o_callback->sc_private;
135 slap_overinst *on = oi->oi_list;
136 int rc = SLAP_CB_CONTINUE;
137 BackendDB *be = op->o_bd, db = *op->o_bd;
140 for (; on; on=on->on_next ) {
141 if ( on->on_response ) {
142 db.bd_info = (BackendInfo *)on;
143 rc = on->on_response( op, rs );
144 if ( rc != SLAP_CB_CONTINUE ) break;
164 op_aux_chk_referrals,
169 * default return code in case of missing backend function
170 * and overlay stack returning SLAP_CB_CONTINUE
172 static int op_rc[] = {
173 LDAP_UNWILLING_TO_PERFORM, /* bind */
174 LDAP_UNWILLING_TO_PERFORM, /* unbind */
175 LDAP_UNWILLING_TO_PERFORM, /* search */
176 LDAP_UNWILLING_TO_PERFORM, /* compare */
177 LDAP_UNWILLING_TO_PERFORM, /* modify */
178 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
179 LDAP_UNWILLING_TO_PERFORM, /* add */
180 LDAP_UNWILLING_TO_PERFORM, /* delete */
181 LDAP_UNWILLING_TO_PERFORM, /* abandon */
182 LDAP_UNWILLING_TO_PERFORM, /* cancel */
183 LDAP_UNWILLING_TO_PERFORM, /* extended */
184 LDAP_SUCCESS, /* aux_operational */
185 LDAP_SUCCESS /* aux_chk_referrals */
195 slap_overinfo *oi = op->o_bd->bd_info->bi_private;
196 slap_overinst *on = oi->oi_list;
198 BackendDB *be = op->o_bd, db = *op->o_bd;
199 slap_callback cb = {NULL, over_back_response, NULL, NULL};
200 int rc = SLAP_CB_CONTINUE;
203 cb.sc_next = op->o_callback;
205 op->o_callback = &cb;
207 for (; on; on=on->on_next ) {
208 func = &on->on_bi.bi_op_bind;
210 db.bd_info = (BackendInfo *)on;
211 rc = func[which]( op, rs );
212 if ( rc != SLAP_CB_CONTINUE ) break;
216 func = &oi->oi_orig->bi_op_bind;
217 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
218 db.bd_info = oi->oi_orig;
219 rc = func[which]( op, rs );
221 /* should not fall thru this far without anything happening... */
222 if ( rc == SLAP_CB_CONTINUE ) {
226 op->o_callback = cb.sc_next;
231 over_op_bind( Operation *op, SlapReply *rs )
233 return over_op_func( op, rs, op_bind );
237 over_op_unbind( Operation *op, SlapReply *rs )
239 return over_op_func( op, rs, op_unbind );
243 over_op_search( Operation *op, SlapReply *rs )
245 return over_op_func( op, rs, op_search );
249 over_op_compare( Operation *op, SlapReply *rs )
251 return over_op_func( op, rs, op_compare );
255 over_op_modify( Operation *op, SlapReply *rs )
257 return over_op_func( op, rs, op_modify );
261 over_op_modrdn( Operation *op, SlapReply *rs )
263 return over_op_func( op, rs, op_modrdn );
267 over_op_add( Operation *op, SlapReply *rs )
269 return over_op_func( op, rs, op_add );
273 over_op_delete( Operation *op, SlapReply *rs )
275 return over_op_func( op, rs, op_delete );
279 over_op_abandon( Operation *op, SlapReply *rs )
281 return over_op_func( op, rs, op_abandon );
285 over_op_cancel( Operation *op, SlapReply *rs )
287 return over_op_func( op, rs, op_cancel );
291 over_op_extended( Operation *op, SlapReply *rs )
293 return over_op_func( op, rs, op_extended );
297 over_aux_operational( Operation *op, SlapReply *rs )
299 return over_op_func( op, rs, op_aux_operational );
303 over_aux_chk_referrals( Operation *op, SlapReply *rs )
305 return over_op_func( op, rs, op_aux_chk_referrals );
313 on->on_next = overlays;
330 static const char overtype[] = "over";
332 /* add an overlay to a particular backend. */
334 overlay_config( BackendDB *be, const char *ov )
336 slap_overinst *on = NULL, *on2 = NULL;
337 slap_overinfo *oi = NULL;
338 BackendInfo *bi = NULL;
340 for ( on = overlays; on; on=on->on_next ) {
341 if (!strcmp( ov, on->on_bi.bi_type ) )
345 Debug( LDAP_DEBUG_ANY, "overlay %s not found\n", ov, 0, 0 );
349 /* If this is the first overlay on this backend, set up the
350 * overlay info structure
352 if ( be->bd_info->bi_type != overtype ) {
353 oi = ch_malloc( sizeof(slap_overinfo) );
354 oi->oi_orig = be->bd_info;
355 oi->oi_bi = *be->bd_info;
357 /* Save a pointer to ourself in bi_private.
358 * This allows us to keep working in conjunction
361 oi->oi_bi.bi_private = oi;
363 bi = (BackendInfo *)oi;
365 bi->bi_type = (char *)overtype;
367 bi->bi_db_config = over_db_config;
368 bi->bi_db_open = over_db_open;
369 bi->bi_db_close = over_db_close;
370 bi->bi_db_destroy = over_db_destroy;
372 bi->bi_op_bind = over_op_bind;
373 bi->bi_op_unbind = over_op_unbind;
374 bi->bi_op_search = over_op_search;
375 bi->bi_op_compare = over_op_compare;
376 bi->bi_op_modify = over_op_modify;
377 bi->bi_op_modrdn = over_op_modrdn;
378 bi->bi_op_add = over_op_add;
379 bi->bi_op_delete = over_op_delete;
380 bi->bi_op_abandon = over_op_abandon;
381 bi->bi_op_cancel = over_op_cancel;
383 bi->bi_extended = over_op_extended;
386 * this is fine because it has the same
387 * args of the operations; we need to rework
388 * all the hooks to share the same args
389 * of the operations...
391 bi->bi_operational = over_aux_operational;
392 bi->bi_chk_referrals = over_aux_chk_referrals;
397 oi = be->bd_info->bi_private;
400 /* Insert new overlay on head of list. Overlays are executed
401 * in reverse of config order...
403 on2 = ch_calloc( 1, sizeof(slap_overinst) );
406 on2->on_next = oi->oi_list;
409 /* Any initialization needed? */
410 if ( on->on_bi.bi_db_init ) {
411 be->bd_info = (BackendInfo *)on2;
412 on2->on_bi.bi_db_init( be );
413 be->bd_info = (BackendInfo *)oi;