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
520 BackendDB *be = op->o_bd, db;
521 slap_callback cb = {NULL, over_back_response, NULL, NULL};
522 int rc = SLAP_CB_CONTINUE;
524 /* FIXME: used to happen for instance during abandon
525 * when global overlays are used... */
526 assert( op->o_bd != NULL );
528 oi = op->o_bd->bd_info->bi_private;
531 if ( !SLAP_ISOVERLAY( op->o_bd )) {
533 db.be_flags |= SLAP_DBFLAG_OVERLAY;
536 cb.sc_next = op->o_callback;
538 op->o_callback = &cb;
540 rc = overlay_op_walk( op, rs, which, oi, on );
543 op->o_callback = cb.sc_next;
548 over_op_bind( Operation *op, SlapReply *rs )
550 return over_op_func( op, rs, op_bind );
554 over_op_unbind( Operation *op, SlapReply *rs )
556 return over_op_func( op, rs, op_unbind );
560 over_op_search( Operation *op, SlapReply *rs )
562 return over_op_func( op, rs, op_search );
566 over_op_compare( Operation *op, SlapReply *rs )
568 return over_op_func( op, rs, op_compare );
572 over_op_modify( Operation *op, SlapReply *rs )
574 return over_op_func( op, rs, op_modify );
578 over_op_modrdn( Operation *op, SlapReply *rs )
580 return over_op_func( op, rs, op_modrdn );
584 over_op_add( Operation *op, SlapReply *rs )
586 return over_op_func( op, rs, op_add );
590 over_op_delete( Operation *op, SlapReply *rs )
592 return over_op_func( op, rs, op_delete );
596 over_op_abandon( Operation *op, SlapReply *rs )
598 return over_op_func( op, rs, op_abandon );
602 over_op_cancel( Operation *op, SlapReply *rs )
604 return over_op_func( op, rs, op_cancel );
608 over_op_extended( Operation *op, SlapReply *rs )
610 return over_op_func( op, rs, op_extended );
614 over_aux_operational( Operation *op, SlapReply *rs )
616 return over_op_func( op, rs, op_aux_operational );
620 over_aux_chk_referrals( Operation *op, SlapReply *rs )
622 return over_op_func( op, rs, op_aux_chk_referrals );
626 over_aux_chk_controls( Operation *op, SlapReply *rs )
628 return over_op_func( op, rs, op_aux_chk_controls );
638 over_connection_func(
641 enum conn_which which
647 int rc = SLAP_CB_CONTINUE;
648 BI_connection_init **func;
650 /* FIXME: used to happen for instance during abandon
651 * when global overlays are used... */
652 assert( bd != NULL );
654 oi = bd->bd_info->bi_private;
657 if ( !SLAP_ISOVERLAY( bd ) ) {
659 db.be_flags |= SLAP_DBFLAG_OVERLAY;
663 for ( ; on; on = on->on_next ) {
664 func = &on->on_bi.bi_connection_init;
665 if ( func[ which ] ) {
666 bd->bd_info = (BackendInfo *)on;
667 rc = func[ which ]( bd, conn );
668 if ( rc != SLAP_CB_CONTINUE ) break;
672 func = &oi->oi_orig->bi_connection_init;
673 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
674 bd->bd_info = oi->oi_orig;
675 rc = func[ which ]( bd, conn );
677 /* should not fall thru this far without anything happening... */
678 if ( rc == SLAP_CB_CONTINUE ) {
679 rc = LDAP_UNWILLING_TO_PERFORM;
686 over_connection_init(
691 return over_connection_func( bd, conn, conn_init );
695 over_connection_destroy(
700 return over_connection_func( bd, conn, conn_destroy );
708 on->on_next = overlays;
714 * iterator on registered overlays; overlay_next( NULL ) returns the first
715 * overlay; * subsequent calls with the previously returned value allow to
716 * iterate * over the entire list; returns NULL when no more overlays are
733 * returns a specific registered overlay based on the type; NULL if not
738 overlay_find( const char *over_type )
740 slap_overinst *on = overlays;
742 assert( over_type != NULL );
744 for ( ; on; on = on->on_next ) {
745 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
753 static const char overtype[] = "over";
756 * returns TRUE (1) if the database is actually an overlay instance;
757 * FALSE (0) otherwise.
761 overlay_is_over( BackendDB *be )
763 return be->bd_info->bi_type == overtype;
767 * returns TRUE (1) if the given database is actually an overlay
768 * instance and, somewhere in the list, contains the requested overlay;
769 * FALSE (0) otherwise.
773 overlay_is_inst( BackendDB *be, const char *over_type )
777 assert( be != NULL );
779 if ( !overlay_is_over( be ) ) {
783 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
784 for ( ; on; on = on->on_next ) {
785 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
794 overlay_register_control( BackendDB *be, const char *oid )
800 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
804 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
807 /* add to all backends... */
808 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
813 bd->be_ctrls[ cid ] = 1;
814 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
819 if ( rc == 0 && !gotit ) {
820 be->be_ctrls[ cid ] = 1;
821 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
828 overlay_destroy_one( BackendDB *be, slap_overinst *on )
830 slap_overinfo *oi = on->on_info;
831 slap_overinst **oidx;
833 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
836 if ( on->on_bi.bi_db_destroy ) {
837 BackendInfo *bi_orig = be->bd_info;
838 be->bd_info = (BackendInfo *)on;
839 on->on_bi.bi_db_destroy( be );
840 be->bd_info = bi_orig;
848 /* add an overlay to a particular backend. */
850 overlay_config( BackendDB *be, const char *ov )
852 slap_overinst *on = NULL, *on2 = NULL;
853 slap_overinfo *oi = NULL;
854 BackendInfo *bi = NULL;
856 on = overlay_find( ov );
858 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
862 /* If this is the first overlay on this backend, set up the
863 * overlay info structure
865 if ( !overlay_is_over( be ) ) {
866 oi = ch_malloc( sizeof( slap_overinfo ) );
867 oi->oi_orig = be->bd_info;
868 oi->oi_bi = *be->bd_info;
870 /* NOTE: the first time a global overlay is configured,
871 * frontendDB gets this flag; it is used later by overlays
872 * to determine if they're stacked on top of the frontendDB */
873 if ( oi->oi_orig == frontendDB->bd_info ) {
874 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
877 /* Save a pointer to ourself in bi_private.
879 oi->oi_bi.bi_private = oi;
881 bi = (BackendInfo *)oi;
883 bi->bi_type = (char *)overtype;
885 bi->bi_db_config = over_db_config;
886 bi->bi_db_open = over_db_open;
887 bi->bi_db_close = over_db_close;
888 bi->bi_db_destroy = over_db_destroy;
890 bi->bi_op_bind = over_op_bind;
891 bi->bi_op_unbind = over_op_unbind;
892 bi->bi_op_search = over_op_search;
893 bi->bi_op_compare = over_op_compare;
894 bi->bi_op_modify = over_op_modify;
895 bi->bi_op_modrdn = over_op_modrdn;
896 bi->bi_op_add = over_op_add;
897 bi->bi_op_delete = over_op_delete;
898 bi->bi_op_abandon = over_op_abandon;
899 bi->bi_op_cancel = over_op_cancel;
901 bi->bi_extended = over_op_extended;
904 * this is fine because it has the same
905 * args of the operations; we need to rework
906 * all the hooks to share the same args
907 * of the operations...
909 bi->bi_operational = over_aux_operational;
910 bi->bi_chk_referrals = over_aux_chk_referrals;
911 bi->bi_chk_controls = over_aux_chk_controls;
913 #ifdef SLAP_OVERLAY_ACCESS
914 /* these have specific arglists */
915 bi->bi_access_allowed = over_access_allowed;
916 bi->bi_acl_group = over_acl_group;
917 bi->bi_acl_attribute = over_acl_attribute;
918 #endif /* SLAP_OVERLAY_ACCESS */
920 bi->bi_connection_init = over_connection_init;
921 bi->bi_connection_destroy = over_connection_destroy;
926 if ( overlay_is_inst( be, ov ) ) {
927 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
928 "warning, overlay \"%s\" "
929 "already in list\n", ov, 0, 0 );
932 oi = be->bd_info->bi_private;
935 /* Insert new overlay on head of list. Overlays are executed
936 * in reverse of config order...
938 on2 = ch_calloc( 1, sizeof(slap_overinst) );
941 on2->on_next = oi->oi_list;
944 /* Any initialization needed? */
945 if ( on->on_bi.bi_db_init ) {
947 be->bd_info = (BackendInfo *)on2;
948 rc = on2->on_bi.bi_db_init( be );
949 be->bd_info = (BackendInfo *)oi;