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>
30 static slap_overinst *overlays;
45 slap_overinfo *oi = be->bd_info->bi_private;
46 slap_overinst *on = oi->oi_list;
47 BackendInfo *bi_orig = be->bd_info;
51 func = &oi->oi_orig->bi_db_open;
53 be->bd_info = oi->oi_orig;
54 rc = func[which]( be );
57 for (; on && rc == 0; on=on->on_next) {
58 be->bd_info = &on->on_bi;
59 func = &on->on_bi.bi_db_open;
61 rc = func[which]( be );
64 be->bd_info = bi_orig;
77 slap_overinfo *oi = be->bd_info->bi_private;
78 slap_overinst *on = oi->oi_list;
79 BackendInfo *bi_orig = be->bd_info;
80 struct ConfigOCs *be_cf_ocs = be->be_cf_ocs;
84 if ( oi->oi_orig->bi_db_config ) {
85 be->bd_info = oi->oi_orig;
86 be->be_cf_ocs = oi->oi_orig->bi_cf_ocs;
87 rc = oi->oi_orig->bi_db_config( be, fname, lineno,
90 if ( be->bd_info != oi->oi_orig ) {
92 slap_overinst *on2, **onp;
96 /* a database added an overlay;
97 * work it around... */
98 assert( overlay_is_over( be ) );
100 oi2 = ( slap_overinfo * )be->bd_info->bi_private;
103 /* need to put a uniqueness check here as well;
104 * note that in principle there could be more than
105 * one overlay as a result of multiple calls to
106 * overlay_config() */
107 be2.bd_info = (BackendInfo *)oi;
109 for ( i = 0, onp = &on2; *onp; i++, onp = &(*onp)->on_next ) {
110 if ( overlay_is_inst( &be2, (*onp)->on_bi.bi_type ) ) {
111 Debug( LDAP_DEBUG_ANY, "over_db_config(): "
112 "warning, freshly added "
113 "overlay #%d \"%s\" is already in list\n",
114 i, (*onp)->on_bi.bi_type, 0 );
116 /* NOTE: if the overlay already exists,
117 * there is no way to merge the results
118 * of the configuration that may have
119 * occurred during bi_db_config(); we
120 * just issue a warning, and the
121 * administrator should deal with this */
128 ch_free( be->bd_info );
131 be->bd_info = (BackendInfo *)oi;
132 if ( rc != SLAP_CONF_UNKNOWN ) return rc;
140 for (; on; on=on->on_next) {
141 rc = SLAP_CONF_UNKNOWN;
142 if (on->on_bi.bi_cf_ocs) {
145 ct = config_find_keyword( on->on_bi.bi_cf_ocs->co_table, &ca );
147 rc = config_add_vals( ct, &ca );
150 if (on->on_bi.bi_db_config && rc == SLAP_CONF_UNKNOWN) {
151 be->bd_info = &on->on_bi;
152 rc = on->on_bi.bi_db_config( be, fname, lineno,
154 if ( rc != SLAP_CONF_UNKNOWN ) break;
157 be->bd_info = bi_orig;
158 be->be_cf_ocs = be_cf_ocs;
168 return over_db_func( be, db_open );
176 slap_overinfo *oi = be->bd_info->bi_private;
177 slap_overinst *on = oi->oi_list;
178 BackendInfo *bi_orig = be->bd_info;
181 for (; on && rc == 0; on=on->on_next) {
182 be->bd_info = &on->on_bi;
183 if ( be->bd_info->bi_db_close ) {
184 rc = be->bd_info->bi_db_close( be );
188 if ( oi->oi_orig->bi_db_close ) {
189 be->bd_info = oi->oi_orig;
190 rc = be->bd_info->bi_db_close( be );
193 be->bd_info = bi_orig;
202 slap_overinfo *oi = be->bd_info->bi_private;
203 slap_overinst *on = oi->oi_list, *next;
206 rc = over_db_func( be, db_destroy );
208 for (next = on->on_next; on; on=next) {
217 over_back_response ( Operation *op, SlapReply *rs )
219 slap_overinfo *oi = op->o_callback->sc_private;
220 slap_overinst *on = oi->oi_list;
221 int rc = SLAP_CB_CONTINUE;
222 BackendDB *be = op->o_bd, db = *op->o_bd;
224 db.be_flags |= SLAP_DBFLAG_OVERLAY;
226 for (; on; on=on->on_next ) {
227 if ( on->on_response ) {
228 db.bd_info = (BackendInfo *)on;
229 rc = on->on_response( op, rs );
230 if ( rc != SLAP_CB_CONTINUE ) break;
237 #ifdef SLAP_OVERLAY_ACCESS
242 AttributeDescription *desc,
244 slap_access_t access,
245 AccessControlState *state,
250 BackendInfo *bi = op->o_bd->bd_info;
251 BackendDB *be = op->o_bd, db;
252 int rc = SLAP_CB_CONTINUE;
254 /* FIXME: used to happen for instance during abandon
255 * when global overlays are used... */
256 assert( op->o_bd != NULL );
258 oi = op->o_bd->bd_info->bi_private;
261 for ( ; on; on = on->on_next ) {
262 if ( on->on_bi.bi_access_allowed ) {
263 /* NOTE: do not copy the structure until required */
264 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
266 db.be_flags |= SLAP_DBFLAG_OVERLAY;
270 op->o_bd->bd_info = (BackendInfo *)on;
271 rc = on->on_bi.bi_access_allowed( op, e,
272 desc, val, access, state, maskp );
273 if ( rc != SLAP_CB_CONTINUE ) break;
277 if ( rc == SLAP_CB_CONTINUE ) {
278 BI_access_allowed *bi_access_allowed;
280 /* if the database structure was changed, o_bd points to a
281 * copy of the structure; put the original bd_info in place */
282 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
283 op->o_bd->bd_info = oi->oi_orig;
286 if ( oi->oi_orig->bi_access_allowed ) {
287 bi_access_allowed = oi->oi_orig->bi_access_allowed;
289 bi_access_allowed = slap_access_allowed;
292 rc = bi_access_allowed( op, e,
293 desc, val, access, state, maskp );
295 /* should not fall thru this far without anything happening... */
296 if ( rc == SLAP_CB_CONTINUE ) {
297 /* access not allowed */
302 op->o_bd->bd_info = bi;
311 struct berval *gr_ndn,
312 struct berval *op_ndn,
313 ObjectClass *group_oc,
314 AttributeDescription *group_at )
318 BackendInfo *bi = op->o_bd->bd_info;
319 BackendDB *be = op->o_bd, db;
320 int rc = SLAP_CB_CONTINUE;
322 /* FIXME: used to happen for instance during abandon
323 * when global overlays are used... */
324 assert( op->o_bd != NULL );
326 oi = op->o_bd->bd_info->bi_private;
329 for ( ; on; on = on->on_next ) {
330 if ( on->on_bi.bi_acl_group ) {
331 /* NOTE: do not copy the structure until required */
332 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
334 db.be_flags |= SLAP_DBFLAG_OVERLAY;
338 op->o_bd->bd_info = (BackendInfo *)on;
339 rc = on->on_bi.bi_acl_group( op, e,
340 gr_ndn, op_ndn, group_oc, group_at );
341 if ( rc != SLAP_CB_CONTINUE ) break;
345 if ( rc == SLAP_CB_CONTINUE ) {
346 BI_acl_group *bi_acl_group;
348 /* if the database structure was changed, o_bd points to a
349 * copy of the structure; put the original bd_info in place */
350 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
351 op->o_bd->bd_info = oi->oi_orig;
354 if ( oi->oi_orig->bi_acl_group ) {
355 bi_acl_group = oi->oi_orig->bi_acl_group;
357 bi_acl_group = backend_group;
360 rc = bi_acl_group( op, e,
361 gr_ndn, op_ndn, group_oc, group_at );
363 /* should not fall thru this far without anything happening... */
364 if ( rc == SLAP_CB_CONTINUE ) {
365 /* access not allowed */
370 op->o_bd->bd_info = bi;
379 struct berval *entry_ndn,
380 AttributeDescription *entry_at,
382 slap_access_t access )
386 BackendInfo *bi = op->o_bd->bd_info;
387 BackendDB *be = op->o_bd, db;
388 int rc = SLAP_CB_CONTINUE;
390 /* FIXME: used to happen for instance during abandon
391 * when global overlays are used... */
392 assert( op->o_bd != NULL );
394 oi = op->o_bd->bd_info->bi_private;
397 for ( ; on; on = on->on_next ) {
398 if ( on->on_bi.bi_acl_attribute ) {
399 /* NOTE: do not copy the structure until required */
400 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
402 db.be_flags |= SLAP_DBFLAG_OVERLAY;
406 op->o_bd->bd_info = (BackendInfo *)on;
407 rc = on->on_bi.bi_acl_attribute( op, target,
408 entry_ndn, entry_at, vals, access );
409 if ( rc != SLAP_CB_CONTINUE ) break;
413 if ( rc == SLAP_CB_CONTINUE ) {
414 BI_acl_attribute *bi_acl_attribute;
416 /* if the database structure was changed, o_bd points to a
417 * copy of the structure; put the original bd_info in place */
418 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
419 op->o_bd->bd_info = oi->oi_orig;
422 if ( oi->oi_orig->bi_acl_attribute ) {
423 bi_acl_attribute = oi->oi_orig->bi_acl_attribute;
425 bi_acl_attribute = backend_attribute;
428 rc = bi_acl_attribute( op, target,
429 entry_ndn, entry_at, vals, access );
431 /* should not fall thru this far without anything happening... */
432 if ( rc == SLAP_CB_CONTINUE ) {
433 /* access not allowed */
438 op->o_bd->bd_info = bi;
442 #endif /* SLAP_OVERLAY_ACCESS */
445 * default return code in case of missing backend function
446 * and overlay stack returning SLAP_CB_CONTINUE
448 static int op_rc[ op_last ] = {
449 LDAP_UNWILLING_TO_PERFORM, /* bind */
450 LDAP_UNWILLING_TO_PERFORM, /* unbind */
451 LDAP_UNWILLING_TO_PERFORM, /* search */
452 SLAP_CB_CONTINUE, /* compare; pass to frontend */
453 LDAP_UNWILLING_TO_PERFORM, /* modify */
454 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
455 LDAP_UNWILLING_TO_PERFORM, /* add */
456 LDAP_UNWILLING_TO_PERFORM, /* delete */
457 LDAP_UNWILLING_TO_PERFORM, /* abandon */
458 LDAP_UNWILLING_TO_PERFORM, /* cancel */
459 LDAP_UNWILLING_TO_PERFORM, /* extended */
460 LDAP_SUCCESS, /* aux_operational */
461 LDAP_SUCCESS, /* aux_chk_referrals */
462 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
468 slap_operation_t which,
474 int rc = SLAP_CB_CONTINUE;
476 for (; on; on=on->on_next ) {
477 func = &on->on_bi.bi_op_bind;
479 op->o_bd->bd_info = (BackendInfo *)on;
480 rc = func[which]( op, rs );
481 if ( rc != SLAP_CB_CONTINUE ) break;
485 func = &oi->oi_orig->bi_op_bind;
486 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
487 op->o_bd->bd_info = oi->oi_orig;
488 rc = func[which]( op, rs );
490 /* should not fall thru this far without anything happening... */
491 if ( rc == SLAP_CB_CONTINUE ) {
495 /* The underlying backend didn't handle the request, make sure
496 * overlay cleanup is processed.
498 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
499 slap_callback *sc_next;
500 for ( ; op->o_callback && op->o_callback->sc_response !=
501 over_back_response; op->o_callback = sc_next ) {
502 sc_next = op->o_callback->sc_next;
503 if ( op->o_callback->sc_cleanup ) {
504 op->o_callback->sc_cleanup( op, rs );
515 slap_operation_t which
521 BackendDB *be = op->o_bd, db;
522 slap_callback cb = {NULL, over_back_response, NULL, NULL};
523 int rc = SLAP_CB_CONTINUE;
525 /* FIXME: used to happen for instance during abandon
526 * when global overlays are used... */
527 assert( op->o_bd != NULL );
529 oi = op->o_bd->bd_info->bi_private;
532 if ( !SLAP_ISOVERLAY( op->o_bd )) {
534 db.be_flags |= SLAP_DBFLAG_OVERLAY;
537 cb.sc_next = op->o_callback;
539 op->o_callback = &cb;
541 rc = overlay_op_walk( op, rs, which, oi, on );
544 op->o_callback = cb.sc_next;
549 over_op_bind( Operation *op, SlapReply *rs )
551 return over_op_func( op, rs, op_bind );
555 over_op_unbind( Operation *op, SlapReply *rs )
557 return over_op_func( op, rs, op_unbind );
561 over_op_search( Operation *op, SlapReply *rs )
563 return over_op_func( op, rs, op_search );
567 over_op_compare( Operation *op, SlapReply *rs )
569 return over_op_func( op, rs, op_compare );
573 over_op_modify( Operation *op, SlapReply *rs )
575 return over_op_func( op, rs, op_modify );
579 over_op_modrdn( Operation *op, SlapReply *rs )
581 return over_op_func( op, rs, op_modrdn );
585 over_op_add( Operation *op, SlapReply *rs )
587 return over_op_func( op, rs, op_add );
591 over_op_delete( Operation *op, SlapReply *rs )
593 return over_op_func( op, rs, op_delete );
597 over_op_abandon( Operation *op, SlapReply *rs )
599 return over_op_func( op, rs, op_abandon );
603 over_op_cancel( Operation *op, SlapReply *rs )
605 return over_op_func( op, rs, op_cancel );
609 over_op_extended( Operation *op, SlapReply *rs )
611 return over_op_func( op, rs, op_extended );
615 over_aux_operational( Operation *op, SlapReply *rs )
617 return over_op_func( op, rs, op_aux_operational );
621 over_aux_chk_referrals( Operation *op, SlapReply *rs )
623 return over_op_func( op, rs, op_aux_chk_referrals );
627 over_aux_chk_controls( Operation *op, SlapReply *rs )
629 return over_op_func( op, rs, op_aux_chk_controls );
639 over_connection_func(
642 enum conn_which which
648 int rc = SLAP_CB_CONTINUE;
649 BI_connection_init **func;
651 /* FIXME: used to happen for instance during abandon
652 * when global overlays are used... */
653 assert( bd != NULL );
655 oi = bd->bd_info->bi_private;
658 if ( !SLAP_ISOVERLAY( bd ) ) {
660 db.be_flags |= SLAP_DBFLAG_OVERLAY;
664 for ( ; on; on = on->on_next ) {
665 func = &on->on_bi.bi_connection_init;
666 if ( func[ which ] ) {
667 bd->bd_info = (BackendInfo *)on;
668 rc = func[ which ]( bd, conn );
669 if ( rc != SLAP_CB_CONTINUE ) break;
673 func = &oi->oi_orig->bi_connection_init;
674 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
675 bd->bd_info = oi->oi_orig;
676 rc = func[ which ]( bd, conn );
678 /* should not fall thru this far without anything happening... */
679 if ( rc == SLAP_CB_CONTINUE ) {
680 rc = LDAP_UNWILLING_TO_PERFORM;
687 over_connection_init(
692 return over_connection_func( bd, conn, conn_init );
696 over_connection_destroy(
701 return over_connection_func( bd, conn, conn_destroy );
709 on->on_next = overlays;
715 * iterator on registered overlays; overlay_next( NULL ) returns the first
716 * overlay; * subsequent calls with the previously returned value allow to
717 * iterate * over the entire list; returns NULL when no more overlays are
734 * returns a specific registered overlay based on the type; NULL if not
739 overlay_find( const char *over_type )
741 slap_overinst *on = overlays;
743 assert( over_type != NULL );
745 for ( ; on; on = on->on_next ) {
746 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
754 static const char overtype[] = "over";
757 * returns TRUE (1) if the database is actually an overlay instance;
758 * FALSE (0) otherwise.
762 overlay_is_over( BackendDB *be )
764 return be->bd_info->bi_type == overtype;
768 * returns TRUE (1) if the given database is actually an overlay
769 * instance and, somewhere in the list, contains the requested overlay;
770 * FALSE (0) otherwise.
774 overlay_is_inst( BackendDB *be, const char *over_type )
778 assert( be != NULL );
780 if ( !overlay_is_over( be ) ) {
784 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
785 for ( ; on; on = on->on_next ) {
786 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
795 overlay_register_control( BackendDB *be, const char *oid )
801 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
805 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
808 /* add to all backends... */
809 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
814 bd->be_ctrls[ cid ] = 1;
815 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
820 if ( rc == 0 && !gotit ) {
821 be->be_ctrls[ cid ] = 1;
822 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
829 overlay_destroy_one( BackendDB *be, slap_overinst *on )
831 slap_overinfo *oi = on->on_info;
832 slap_overinst **oidx;
834 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
837 if ( on->on_bi.bi_db_destroy ) {
838 BackendInfo *bi_orig = be->bd_info;
839 be->bd_info = (BackendInfo *)on;
840 on->on_bi.bi_db_destroy( be );
841 be->bd_info = bi_orig;
849 /* add an overlay to a particular backend. */
851 overlay_config( BackendDB *be, const char *ov )
853 slap_overinst *on = NULL, *on2 = NULL;
854 slap_overinfo *oi = NULL;
855 BackendInfo *bi = NULL;
857 on = overlay_find( ov );
859 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
863 /* If this is the first overlay on this backend, set up the
864 * overlay info structure
866 if ( !overlay_is_over( be ) ) {
867 oi = ch_malloc( sizeof( slap_overinfo ) );
868 oi->oi_orig = be->bd_info;
869 oi->oi_bi = *be->bd_info;
871 /* NOTE: the first time a global overlay is configured,
872 * frontendDB gets this flag; it is used later by overlays
873 * to determine if they're stacked on top of the frontendDB */
874 if ( oi->oi_orig == frontendDB->bd_info ) {
875 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
878 /* Save a pointer to ourself in bi_private.
880 oi->oi_bi.bi_private = oi;
882 bi = (BackendInfo *)oi;
884 bi->bi_type = (char *)overtype;
886 bi->bi_db_config = over_db_config;
887 bi->bi_db_open = over_db_open;
888 bi->bi_db_close = over_db_close;
889 bi->bi_db_destroy = over_db_destroy;
891 bi->bi_op_bind = over_op_bind;
892 bi->bi_op_unbind = over_op_unbind;
893 bi->bi_op_search = over_op_search;
894 bi->bi_op_compare = over_op_compare;
895 bi->bi_op_modify = over_op_modify;
896 bi->bi_op_modrdn = over_op_modrdn;
897 bi->bi_op_add = over_op_add;
898 bi->bi_op_delete = over_op_delete;
899 bi->bi_op_abandon = over_op_abandon;
900 bi->bi_op_cancel = over_op_cancel;
902 bi->bi_extended = over_op_extended;
905 * this is fine because it has the same
906 * args of the operations; we need to rework
907 * all the hooks to share the same args
908 * of the operations...
910 bi->bi_operational = over_aux_operational;
911 bi->bi_chk_referrals = over_aux_chk_referrals;
912 bi->bi_chk_controls = over_aux_chk_controls;
914 #ifdef SLAP_OVERLAY_ACCESS
915 /* these have specific arglists */
916 bi->bi_access_allowed = over_access_allowed;
917 bi->bi_acl_group = over_acl_group;
918 bi->bi_acl_attribute = over_acl_attribute;
919 #endif /* SLAP_OVERLAY_ACCESS */
921 bi->bi_connection_init = over_connection_init;
922 bi->bi_connection_destroy = over_connection_destroy;
927 if ( overlay_is_inst( be, ov ) ) {
928 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
929 "warning, overlay \"%s\" "
930 "already in list\n", ov, 0, 0 );
933 oi = be->bd_info->bi_private;
936 /* Insert new overlay on head of list. Overlays are executed
937 * in reverse of config order...
939 on2 = ch_calloc( 1, sizeof(slap_overinst) );
942 on2->on_next = oi->oi_list;
945 /* Any initialization needed? */
946 if ( on->on_bi.bi_db_init ) {
948 be->bd_info = (BackendInfo *)on2;
949 rc = on2->on_bi.bi_db_init( be );
950 be->bd_info = (BackendInfo *)oi;