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,
166 * default return code in case of missing backend function
167 * and overlay stack returning SLAP_CB_CONTINUE
169 static int op_rc[] = {
170 LDAP_UNWILLING_TO_PERFORM, /* bind */
171 LDAP_UNWILLING_TO_PERFORM, /* unbind */
172 LDAP_UNWILLING_TO_PERFORM, /* search */
173 LDAP_UNWILLING_TO_PERFORM, /* compare */
174 LDAP_UNWILLING_TO_PERFORM, /* modify */
175 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
176 LDAP_UNWILLING_TO_PERFORM, /* add */
177 LDAP_UNWILLING_TO_PERFORM, /* delete */
178 LDAP_UNWILLING_TO_PERFORM, /* abandon */
179 LDAP_UNWILLING_TO_PERFORM, /* cancel */
180 LDAP_UNWILLING_TO_PERFORM, /* extended */
181 LDAP_SUCCESS /* aux_chk_referrals */
191 slap_overinfo *oi = (slap_overinfo *) op->o_bd->bd_info;
192 slap_overinst *on = oi->oi_list;
194 BackendDB *be = op->o_bd, db = *op->o_bd;
195 slap_callback cb = {NULL, over_back_response, NULL, NULL};
196 int rc = SLAP_CB_CONTINUE;
199 cb.sc_next = op->o_callback;
201 op->o_callback = &cb;
203 for (; on; on=on->on_next ) {
204 func = &on->on_bi.bi_op_bind;
206 db.bd_info = (BackendInfo *)on;
207 rc = func[which]( op, rs );
208 if ( rc != SLAP_CB_CONTINUE ) break;
213 func = &oi->oi_orig->bi_op_bind;
214 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
215 rc = func[which]( op, rs );
217 /* should not fall thru this far without anything happening... */
218 if ( rc == SLAP_CB_CONTINUE ) {
221 op->o_callback = cb.sc_next;
226 over_op_bind( Operation *op, SlapReply *rs )
228 return over_op_func( op, rs, op_bind );
232 over_op_unbind( Operation *op, SlapReply *rs )
234 return over_op_func( op, rs, op_unbind );
238 over_op_search( Operation *op, SlapReply *rs )
240 return over_op_func( op, rs, op_search );
244 over_op_compare( Operation *op, SlapReply *rs )
246 return over_op_func( op, rs, op_compare );
250 over_op_modify( Operation *op, SlapReply *rs )
252 return over_op_func( op, rs, op_modify );
256 over_op_modrdn( Operation *op, SlapReply *rs )
258 return over_op_func( op, rs, op_modrdn );
262 over_op_add( Operation *op, SlapReply *rs )
264 return over_op_func( op, rs, op_add );
268 over_op_delete( Operation *op, SlapReply *rs )
270 return over_op_func( op, rs, op_delete );
274 over_op_abandon( Operation *op, SlapReply *rs )
276 return over_op_func( op, rs, op_abandon );
280 over_op_cancel( Operation *op, SlapReply *rs )
282 return over_op_func( op, rs, op_cancel );
286 over_op_extended( Operation *op, SlapReply *rs )
288 return over_op_func( op, rs, op_extended );
292 over_chk_referrals( Operation *op, SlapReply *rs )
294 return over_op_func( op, rs, op_aux_chk_referrals );
302 on->on_next = overlays;
319 static const char overtype[] = "over";
321 /* add an overlay to a particular backend. */
323 overlay_config( BackendDB *be, const char *ov )
325 slap_overinst *on = NULL, *on2 = NULL, *prev = NULL;
326 slap_overinfo *oi = NULL;
327 BackendInfo *bi = NULL;
329 for ( on = overlays; on; on=on->on_next ) {
330 if (!strcmp( ov, on->on_bi.bi_type ) )
334 Debug( LDAP_DEBUG_ANY, "overlay %s not found\n", ov, 0, 0 );
338 /* If this is the first overlay on this backend, set up the
339 * overlay info structure
341 if ( be->bd_info->bi_type != overtype ) {
342 oi = ch_malloc( sizeof(slap_overinfo) );
343 oi->oi_orig = be->bd_info;
344 oi->oi_bi = *be->bd_info;
346 bi = (BackendInfo *)oi;
348 bi->bi_type = (char *)overtype;
350 bi->bi_db_config = over_db_config;
351 bi->bi_db_open = over_db_open;
352 bi->bi_db_close = over_db_close;
353 bi->bi_db_destroy = over_db_destroy;
355 bi->bi_op_bind = over_op_bind;
356 bi->bi_op_unbind = over_op_unbind;
357 bi->bi_op_search = over_op_search;
358 bi->bi_op_compare = over_op_compare;
359 bi->bi_op_modify = over_op_modify;
360 bi->bi_op_modrdn = over_op_modrdn;
361 bi->bi_op_add = over_op_add;
362 bi->bi_op_delete = over_op_delete;
363 bi->bi_op_abandon = over_op_abandon;
364 bi->bi_op_cancel = over_op_cancel;
366 bi->bi_extended = over_op_extended;
369 * this is fine because it has the same
370 * args of the operations; we need to rework
371 * all the hooks to share the same args
372 * of the operations...
374 bi->bi_chk_referrals = over_chk_referrals;
379 oi = (slap_overinfo *) be->bd_info;
383 /* Walk to the end of the list of overlays, add the new
386 for ( prev=NULL, on2 = oi->oi_list; on2; prev=on2, on2=on2->on_next );
387 on2 = ch_calloc( 1, sizeof(slap_overinst) );
397 /* Insert new overlay on head of list. Overlays are executed
398 * in reverse of config order...
400 on2 = ch_calloc( 1, sizeof(slap_overinst) );
403 on2->on_next = oi->oi_list;
407 /* Any initialization needed? */
408 if ( on->on_bi.bi_db_init ) {
409 be->bd_info = (BackendInfo *)on2;
410 on2->on_bi.bi_db_init( be );
411 be->bd_info = (BackendInfo *)oi;