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;
45 func = &oi->oi_bd.bd_info->bi_db_open;
47 rc = func[which]( &oi->oi_bd );
52 for (; on; on=on->on_next) {
53 bd.bd_info = &on->on_bi;
54 func = &on->on_bi.bi_db_open;
56 rc = func[which]( &bd );
72 slap_overinfo *oi = (slap_overinfo *) be->bd_info;
73 slap_overinst *on = oi->oi_list;
77 if ( oi->oi_bd.bd_info->bi_db_config ) {
78 rc = oi->oi_bd.bd_info->bi_db_config( &oi->oi_bd, fname, lineno,
80 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
84 for (; on; on=on->on_next) {
85 bd.bd_info = &on->on_bi;
86 if (on->on_bi.bi_db_config) {
87 rc = on->on_bi.bi_db_config( &bd, fname, lineno,
89 if ( rc != SLAP_CONF_UNKNOWN ) break;
100 return over_db_func( be, db_open );
108 return over_db_func( be, db_close );
116 slap_overinfo *oi = (slap_overinfo *) be->bd_info;
117 slap_overinst *on = oi->oi_list, *next;
120 rc = over_db_func( be, db_destroy );
122 for (next = on->on_next; on; on=next) {
131 over_back_response ( Operation *op, SlapReply *rs )
133 slap_overinfo *oi = op->o_callback->sc_private;
134 slap_overinst *on = oi->oi_list;
135 int rc = SLAP_CB_CONTINUE;
136 BackendDB *be = op->o_bd, db = *op->o_bd;
139 for (; on; on=on->on_next ) {
140 if ( on->on_response ) {
141 db.bd_info = (BackendInfo *)on;
142 rc = on->on_response( op, rs );
143 if ( rc != SLAP_CB_CONTINUE ) break;
150 enum op_which { op_bind = 0, op_unbind, op_search, op_compare,
151 op_modify, op_modrdn, op_add, op_delete, op_abandon,
152 op_cancel, op_extended };
161 slap_overinfo *oi = (slap_overinfo *) op->o_bd->bd_info;
162 slap_overinst *on = oi->oi_list;
164 BackendDB *be = op->o_bd, db = *op->o_bd;
165 slap_callback cb = {NULL, over_back_response, NULL, NULL};
166 int rc = SLAP_CB_CONTINUE;
169 cb.sc_next = op->o_callback;
171 op->o_callback = &cb;
173 for (; on; on=on->on_next ) {
174 func = &on->on_bi.bi_op_bind;
176 db.bd_info = (BackendInfo *)on;
177 rc = func[which]( op, rs );
178 if ( rc != SLAP_CB_CONTINUE ) break;
183 func = &oi->oi_bd.bd_info->bi_op_bind;
184 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
185 rc = func[which]( op, rs );
187 /* should not fall thru this far without anything happening... */
188 if ( rc == SLAP_CB_CONTINUE ) {
189 rc = LDAP_UNWILLING_TO_PERFORM;
191 op->o_callback = cb.sc_next;
196 over_op_bind( Operation *op, SlapReply *rs )
198 return over_op_func( op, rs, op_bind );
202 over_op_unbind( Operation *op, SlapReply *rs )
204 return over_op_func( op, rs, op_unbind );
208 over_op_search( Operation *op, SlapReply *rs )
210 return over_op_func( op, rs, op_search );
214 over_op_compare( Operation *op, SlapReply *rs )
216 return over_op_func( op, rs, op_compare );
220 over_op_modify( Operation *op, SlapReply *rs )
222 return over_op_func( op, rs, op_modify );
226 over_op_modrdn( Operation *op, SlapReply *rs )
228 return over_op_func( op, rs, op_modrdn );
232 over_op_add( Operation *op, SlapReply *rs )
234 return over_op_func( op, rs, op_add );
238 over_op_delete( Operation *op, SlapReply *rs )
240 return over_op_func( op, rs, op_delete );
244 over_op_abandon( Operation *op, SlapReply *rs )
246 return over_op_func( op, rs, op_abandon );
250 over_op_cancel( Operation *op, SlapReply *rs )
252 return over_op_func( op, rs, op_cancel );
256 over_op_extended( Operation *op, SlapReply *rs )
258 return over_op_func( op, rs, op_extended );
266 on->on_next = overlays;
283 static const char overtype[] = "over";
285 /* add an overlay to a particular backend. */
287 overlay_config( BackendDB *be, const char *ov )
289 slap_overinst *on = NULL, *on2 = NULL, *prev = NULL;
290 slap_overinfo *oi = NULL;
291 BackendInfo *bi = NULL;
293 for ( on = overlays; on; on=on->on_next ) {
294 if (!strcmp( ov, on->on_bi.bi_type ) )
298 Debug( LDAP_DEBUG_ANY, "overlay %s not found\n", ov, 0, 0 );
302 /* If this is the first overlay on this backend, set up the
303 * overlay info structure
305 if ( be->bd_info->bi_type != overtype ) {
306 oi = ch_malloc( sizeof(slap_overinfo) );
308 oi->oi_bi = *be->bd_info;
310 bi = (BackendInfo *)oi;
312 bi->bi_type = (char *)overtype;
314 bi->bi_db_config = over_db_config;
315 bi->bi_db_open = over_db_open;
316 bi->bi_db_close = over_db_close;
317 bi->bi_db_destroy = over_db_destroy;
319 bi->bi_op_bind = over_op_bind;
320 bi->bi_op_unbind = over_op_unbind;
321 bi->bi_op_search = over_op_search;
322 bi->bi_op_compare = over_op_compare;
323 bi->bi_op_modify = over_op_modify;
324 bi->bi_op_modrdn = over_op_modrdn;
325 bi->bi_op_add = over_op_add;
326 bi->bi_op_delete = over_op_delete;
327 bi->bi_op_abandon = over_op_abandon;
328 bi->bi_op_cancel = over_op_cancel;
329 bi->bi_extended = over_op_extended;
334 oi = (slap_overinfo *) be->bd_info;
338 /* Walk to the end of the list of overlays, add the new
341 for ( prev=NULL, on2 = oi->oi_list; on2; prev=on2, on2=on2->on_next );
342 on2 = ch_calloc( 1, sizeof(slap_overinst) );
352 /* Insert new overlay on head of list. Overlays are executed
353 * in reverse of config order...
355 on2 = ch_calloc( 1, sizeof(slap_overinst) );
358 on2->on_next = oi->oi_list;
362 /* Any initialization needed? */
363 if ( on->on_bi.bi_db_init ) {
364 be->bd_info = (BackendInfo *)on2;
365 on2->on_bi.bi_db_init( be );
366 be->bd_info = (BackendInfo *)oi;