1 /* backover.c - backend overlay routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 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 slap_overinfo *oi = be->bd_info->bi_private;
152 slap_overinst *on = oi->oi_list;
153 BackendInfo *bi_orig = be->bd_info;
156 for (; on && rc == 0; on=on->on_next) {
157 be->bd_info = &on->on_bi;
158 if ( be->bd_info->bi_db_close ) {
159 rc = be->bd_info->bi_db_close( be );
163 if ( oi->oi_orig->bi_db_close ) {
164 be->bd_info = oi->oi_orig;
165 rc = be->bd_info->bi_db_close( be );
168 be->bd_info = bi_orig;
177 slap_overinfo *oi = be->bd_info->bi_private;
178 slap_overinst *on = oi->oi_list, *next;
181 rc = over_db_func( be, db_destroy );
183 for (next = on->on_next; on; on=next) {
192 over_back_response ( Operation *op, SlapReply *rs )
194 slap_overinfo *oi = op->o_callback->sc_private;
195 slap_overinst *on = oi->oi_list;
196 int rc = SLAP_CB_CONTINUE;
197 BackendDB *be = op->o_bd, db = *op->o_bd;
199 db.be_flags |= SLAP_DBFLAG_OVERLAY;
201 for (; on; on=on->on_next ) {
202 if ( on->on_response ) {
203 db.bd_info = (BackendInfo *)on;
204 rc = on->on_response( op, rs );
205 if ( rc != SLAP_CB_CONTINUE ) break;
225 op_aux_chk_referrals,
230 * default return code in case of missing backend function
231 * and overlay stack returning SLAP_CB_CONTINUE
233 static int op_rc[] = {
234 LDAP_UNWILLING_TO_PERFORM, /* bind */
235 LDAP_UNWILLING_TO_PERFORM, /* unbind */
236 LDAP_UNWILLING_TO_PERFORM, /* search */
237 LDAP_UNWILLING_TO_PERFORM, /* compare */
238 LDAP_UNWILLING_TO_PERFORM, /* modify */
239 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
240 LDAP_UNWILLING_TO_PERFORM, /* add */
241 LDAP_UNWILLING_TO_PERFORM, /* delete */
242 LDAP_UNWILLING_TO_PERFORM, /* abandon */
243 LDAP_UNWILLING_TO_PERFORM, /* cancel */
244 LDAP_UNWILLING_TO_PERFORM, /* extended */
245 LDAP_SUCCESS, /* aux_operational */
246 LDAP_SUCCESS /* aux_chk_referrals */
259 BackendDB *be = op->o_bd, db;
260 slap_callback cb = {NULL, over_back_response, NULL, NULL};
261 int rc = SLAP_CB_CONTINUE;
263 /* FIXME: used to happen for instance during abandon
264 * when global overlays are used... */
265 assert( op->o_bd != NULL );
267 oi = op->o_bd->bd_info->bi_private;
270 if ( !SLAP_ISOVERLAY( op->o_bd )) {
272 db.be_flags |= SLAP_DBFLAG_OVERLAY;
275 cb.sc_next = op->o_callback;
277 op->o_callback = &cb;
279 for (; on; on=on->on_next ) {
280 func = &on->on_bi.bi_op_bind;
282 op->o_bd->bd_info = (BackendInfo *)on;
283 rc = func[which]( op, rs );
284 if ( rc != SLAP_CB_CONTINUE ) break;
288 func = &oi->oi_orig->bi_op_bind;
289 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
290 op->o_bd->bd_info = oi->oi_orig;
291 rc = func[which]( op, rs );
293 /* should not fall thru this far without anything happening... */
294 if ( rc == SLAP_CB_CONTINUE ) {
298 op->o_callback = cb.sc_next;
303 over_op_bind( Operation *op, SlapReply *rs )
305 return over_op_func( op, rs, op_bind );
309 over_op_unbind( Operation *op, SlapReply *rs )
311 return over_op_func( op, rs, op_unbind );
315 over_op_search( Operation *op, SlapReply *rs )
317 return over_op_func( op, rs, op_search );
321 over_op_compare( Operation *op, SlapReply *rs )
323 return over_op_func( op, rs, op_compare );
327 over_op_modify( Operation *op, SlapReply *rs )
329 return over_op_func( op, rs, op_modify );
333 over_op_modrdn( Operation *op, SlapReply *rs )
335 return over_op_func( op, rs, op_modrdn );
339 over_op_add( Operation *op, SlapReply *rs )
341 return over_op_func( op, rs, op_add );
345 over_op_delete( Operation *op, SlapReply *rs )
347 return over_op_func( op, rs, op_delete );
351 over_op_abandon( Operation *op, SlapReply *rs )
353 return over_op_func( op, rs, op_abandon );
357 over_op_cancel( Operation *op, SlapReply *rs )
359 return over_op_func( op, rs, op_cancel );
363 over_op_extended( Operation *op, SlapReply *rs )
365 return over_op_func( op, rs, op_extended );
369 over_aux_operational( Operation *op, SlapReply *rs )
371 return over_op_func( op, rs, op_aux_operational );
375 over_aux_chk_referrals( Operation *op, SlapReply *rs )
377 return over_op_func( op, rs, op_aux_chk_referrals );
385 on->on_next = overlays;
391 * iterator on registered overlays; overlay_next( NULL ) returns the first
392 * overlay; * subsequent calls with the previously returned value allow to
393 * iterate * over the entire list; returns NULL when no more overlays are
410 * returns a specific registered overlay based on the type; NULL if not
415 overlay_find( const char *over_type )
417 slap_overinst *on = overlays;
421 for ( ; on; on = on->on_next ) {
422 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
430 static const char overtype[] = "over";
433 * returns TRUE (1) if the database is actually an overlay instance;
434 * FALSE (0) otherwise.
438 overlay_is_over( BackendDB *be )
440 return be->bd_info->bi_type == overtype;
444 * returns TRUE (1) if the given database is actually an overlay
445 * instance and, somewhere in the list, contains the requested overlay;
446 * FALSE (0) otherwise.
450 overlay_is_inst( BackendDB *be, const char *over_type )
456 if ( !overlay_is_over( be ) ) {
460 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
461 for ( ; on; on = on->on_next ) {
462 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
472 overlay_is_global( BackendDB *be )
474 BackendInfo *frontendBI;
476 if ( !overlay_is_over( be ) ) {
480 frontendBI = frontendDB->bd_info;
481 if ( overlay_is_over( frontendDB ) ) {
482 frontendBI = ((slap_overinfo *)frontendBI->bi_private)->oi_orig;
485 return ((slap_overinfo *)be->bd_info->bi_private)->oi_orig == frontendBI;
490 overlay_register_control( BackendDB *be, const char *oid )
496 if ( overlay_is_global( be ) ) {
499 /* add to all backends... */
500 for ( i = 0; i < nBackendDB; i++ ) {
501 BackendDB *bd = &backendDB[i];
507 if ( bd->be_controls == NULL ||
508 !ldap_charray_inlist( bd->be_controls, oid ) )
510 rc = ldap_charray_add( &be->be_controls, oid );
520 if ( rc == 0 && !gotit && !ldap_charray_inlist( be->be_controls, oid ) ) {
521 rc = ldap_charray_add( &be->be_controls, oid );
530 /* add an overlay to a particular backend. */
532 overlay_config( BackendDB *be, const char *ov )
534 slap_overinst *on = NULL, *on2 = NULL;
535 slap_overinfo *oi = NULL;
536 BackendInfo *bi = NULL;
538 on = overlay_find( ov );
540 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
544 /* If this is the first overlay on this backend, set up the
545 * overlay info structure
547 if ( !overlay_is_over( be ) ) {
548 oi = ch_malloc( sizeof( slap_overinfo ) );
549 oi->oi_orig = be->bd_info;
550 oi->oi_bi = *be->bd_info;
552 /* Save a pointer to ourself in bi_private.
554 oi->oi_bi.bi_private = oi;
556 bi = (BackendInfo *)oi;
558 bi->bi_type = (char *)overtype;
560 bi->bi_db_config = over_db_config;
561 bi->bi_db_open = over_db_open;
562 bi->bi_db_close = over_db_close;
563 bi->bi_db_destroy = over_db_destroy;
565 bi->bi_op_bind = over_op_bind;
566 bi->bi_op_unbind = over_op_unbind;
567 bi->bi_op_search = over_op_search;
568 bi->bi_op_compare = over_op_compare;
569 bi->bi_op_modify = over_op_modify;
570 bi->bi_op_modrdn = over_op_modrdn;
571 bi->bi_op_add = over_op_add;
572 bi->bi_op_delete = over_op_delete;
573 bi->bi_op_abandon = over_op_abandon;
574 bi->bi_op_cancel = over_op_cancel;
576 bi->bi_extended = over_op_extended;
579 * this is fine because it has the same
580 * args of the operations; we need to rework
581 * all the hooks to share the same args
582 * of the operations...
584 bi->bi_operational = over_aux_operational;
585 bi->bi_chk_referrals = over_aux_chk_referrals;
590 if ( overlay_is_inst( be, ov ) ) {
591 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
592 "warning, overlay \"%s\" "
593 "already in list\n", ov, 0, 0 );
596 oi = be->bd_info->bi_private;
599 /* Insert new overlay on head of list. Overlays are executed
600 * in reverse of config order...
602 on2 = ch_calloc( 1, sizeof(slap_overinst) );
605 on2->on_next = oi->oi_list;
608 /* Any initialization needed? */
609 if ( on->on_bi.bi_db_init ) {
610 be->bd_info = (BackendInfo *)on2;
611 on2->on_bi.bi_db_init( be );
612 be->bd_info = (BackendInfo *)oi;