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 snprintf( ca.log, sizeof( ca.log ), "%s: line %d",
141 ca.fname, ca.lineno );
143 for (; on; on=on->on_next) {
144 rc = SLAP_CONF_UNKNOWN;
145 if (on->on_bi.bi_cf_ocs) {
148 ct = config_find_keyword( on->on_bi.bi_cf_ocs->co_table, &ca );
150 rc = config_add_vals( ct, &ca );
151 if ( rc != SLAP_CONF_UNKNOWN )
155 if (on->on_bi.bi_db_config && rc == SLAP_CONF_UNKNOWN) {
156 be->bd_info = &on->on_bi;
157 rc = on->on_bi.bi_db_config( be, fname, lineno,
159 if ( rc != SLAP_CONF_UNKNOWN ) break;
162 be->bd_info = bi_orig;
163 be->be_cf_ocs = be_cf_ocs;
173 return over_db_func( be, db_open );
181 slap_overinfo *oi = be->bd_info->bi_private;
182 slap_overinst *on = oi->oi_list;
183 BackendInfo *bi_orig = be->bd_info;
186 for (; on && rc == 0; on=on->on_next) {
187 be->bd_info = &on->on_bi;
188 if ( be->bd_info->bi_db_close ) {
189 rc = be->bd_info->bi_db_close( be );
193 if ( oi->oi_orig->bi_db_close ) {
194 be->bd_info = oi->oi_orig;
195 rc = be->bd_info->bi_db_close( be );
198 be->bd_info = bi_orig;
207 slap_overinfo *oi = be->bd_info->bi_private;
208 slap_overinst *on = oi->oi_list, *next;
211 rc = over_db_func( be, db_destroy );
213 for (next = on->on_next; on; on=next) {
222 over_back_response ( Operation *op, SlapReply *rs )
224 slap_overinfo *oi = op->o_callback->sc_private;
225 slap_overinst *on = oi->oi_list;
226 int rc = SLAP_CB_CONTINUE;
227 BackendDB *be = op->o_bd, db = *op->o_bd;
229 db.be_flags |= SLAP_DBFLAG_OVERLAY;
231 for (; on; on=on->on_next ) {
232 if ( on->on_response ) {
233 db.bd_info = (BackendInfo *)on;
234 rc = on->on_response( op, rs );
235 if ( rc != SLAP_CB_CONTINUE ) break;
242 #ifdef SLAP_OVERLAY_ACCESS
247 AttributeDescription *desc,
249 slap_access_t access,
250 AccessControlState *state,
255 BackendInfo *bi = op->o_bd->bd_info;
256 BackendDB *be = op->o_bd, db;
257 int rc = SLAP_CB_CONTINUE;
259 /* FIXME: used to happen for instance during abandon
260 * when global overlays are used... */
261 assert( op->o_bd != NULL );
263 oi = op->o_bd->bd_info->bi_private;
266 for ( ; on; on = on->on_next ) {
267 if ( on->on_bi.bi_access_allowed ) {
268 /* NOTE: do not copy the structure until required */
269 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
271 db.be_flags |= SLAP_DBFLAG_OVERLAY;
275 op->o_bd->bd_info = (BackendInfo *)on;
276 rc = on->on_bi.bi_access_allowed( op, e,
277 desc, val, access, state, maskp );
278 if ( rc != SLAP_CB_CONTINUE ) break;
282 if ( rc == SLAP_CB_CONTINUE ) {
283 BI_access_allowed *bi_access_allowed;
285 /* if the database structure was changed, o_bd points to a
286 * copy of the structure; put the original bd_info in place */
287 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
288 op->o_bd->bd_info = oi->oi_orig;
291 if ( oi->oi_orig->bi_access_allowed ) {
292 bi_access_allowed = oi->oi_orig->bi_access_allowed;
294 bi_access_allowed = slap_access_allowed;
297 rc = bi_access_allowed( op, e,
298 desc, val, access, state, maskp );
300 /* should not fall thru this far without anything happening... */
301 if ( rc == SLAP_CB_CONTINUE ) {
302 /* access not allowed */
307 op->o_bd->bd_info = bi;
316 struct berval *gr_ndn,
317 struct berval *op_ndn,
318 ObjectClass *group_oc,
319 AttributeDescription *group_at )
323 BackendInfo *bi = op->o_bd->bd_info;
324 BackendDB *be = op->o_bd, db;
325 int rc = SLAP_CB_CONTINUE;
327 /* FIXME: used to happen for instance during abandon
328 * when global overlays are used... */
329 assert( op->o_bd != NULL );
331 oi = op->o_bd->bd_info->bi_private;
334 for ( ; on; on = on->on_next ) {
335 if ( on->on_bi.bi_acl_group ) {
336 /* NOTE: do not copy the structure until required */
337 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
339 db.be_flags |= SLAP_DBFLAG_OVERLAY;
343 op->o_bd->bd_info = (BackendInfo *)on;
344 rc = on->on_bi.bi_acl_group( op, e,
345 gr_ndn, op_ndn, group_oc, group_at );
346 if ( rc != SLAP_CB_CONTINUE ) break;
350 if ( rc == SLAP_CB_CONTINUE ) {
351 BI_acl_group *bi_acl_group;
353 /* if the database structure was changed, o_bd points to a
354 * copy of the structure; put the original bd_info in place */
355 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
356 op->o_bd->bd_info = oi->oi_orig;
359 if ( oi->oi_orig->bi_acl_group ) {
360 bi_acl_group = oi->oi_orig->bi_acl_group;
362 bi_acl_group = backend_group;
365 rc = bi_acl_group( op, e,
366 gr_ndn, op_ndn, group_oc, group_at );
368 /* should not fall thru this far without anything happening... */
369 if ( rc == SLAP_CB_CONTINUE ) {
370 /* access not allowed */
375 op->o_bd->bd_info = bi;
384 struct berval *entry_ndn,
385 AttributeDescription *entry_at,
387 slap_access_t access )
391 BackendInfo *bi = op->o_bd->bd_info;
392 BackendDB *be = op->o_bd, db;
393 int rc = SLAP_CB_CONTINUE;
395 /* FIXME: used to happen for instance during abandon
396 * when global overlays are used... */
397 assert( op->o_bd != NULL );
399 oi = op->o_bd->bd_info->bi_private;
402 for ( ; on; on = on->on_next ) {
403 if ( on->on_bi.bi_acl_attribute ) {
404 /* NOTE: do not copy the structure until required */
405 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
407 db.be_flags |= SLAP_DBFLAG_OVERLAY;
411 op->o_bd->bd_info = (BackendInfo *)on;
412 rc = on->on_bi.bi_acl_attribute( op, target,
413 entry_ndn, entry_at, vals, access );
414 if ( rc != SLAP_CB_CONTINUE ) break;
418 if ( rc == SLAP_CB_CONTINUE ) {
419 BI_acl_attribute *bi_acl_attribute;
421 /* if the database structure was changed, o_bd points to a
422 * copy of the structure; put the original bd_info in place */
423 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
424 op->o_bd->bd_info = oi->oi_orig;
427 if ( oi->oi_orig->bi_acl_attribute ) {
428 bi_acl_attribute = oi->oi_orig->bi_acl_attribute;
430 bi_acl_attribute = backend_attribute;
433 rc = bi_acl_attribute( op, target,
434 entry_ndn, entry_at, vals, access );
436 /* should not fall thru this far without anything happening... */
437 if ( rc == SLAP_CB_CONTINUE ) {
438 /* access not allowed */
443 op->o_bd->bd_info = bi;
447 #endif /* SLAP_OVERLAY_ACCESS */
450 * default return code in case of missing backend function
451 * and overlay stack returning SLAP_CB_CONTINUE
453 static int op_rc[ op_last ] = {
454 LDAP_UNWILLING_TO_PERFORM, /* bind */
455 LDAP_UNWILLING_TO_PERFORM, /* unbind */
456 LDAP_UNWILLING_TO_PERFORM, /* search */
457 SLAP_CB_CONTINUE, /* compare; pass to frontend */
458 LDAP_UNWILLING_TO_PERFORM, /* modify */
459 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
460 LDAP_UNWILLING_TO_PERFORM, /* add */
461 LDAP_UNWILLING_TO_PERFORM, /* delete */
462 LDAP_UNWILLING_TO_PERFORM, /* abandon */
463 LDAP_UNWILLING_TO_PERFORM, /* cancel */
464 LDAP_UNWILLING_TO_PERFORM, /* extended */
465 LDAP_SUCCESS, /* aux_operational */
466 LDAP_SUCCESS, /* aux_chk_referrals */
467 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
473 slap_operation_t which,
479 int rc = SLAP_CB_CONTINUE;
481 for (; on; on=on->on_next ) {
482 func = &on->on_bi.bi_op_bind;
484 op->o_bd->bd_info = (BackendInfo *)on;
485 rc = func[which]( op, rs );
486 if ( rc != SLAP_CB_CONTINUE ) break;
490 func = &oi->oi_orig->bi_op_bind;
491 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
492 op->o_bd->bd_info = oi->oi_orig;
493 rc = func[which]( op, rs );
495 /* should not fall thru this far without anything happening... */
496 if ( rc == SLAP_CB_CONTINUE ) {
500 /* The underlying backend didn't handle the request, make sure
501 * overlay cleanup is processed.
503 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
504 slap_callback *sc_next;
505 for ( ; op->o_callback && op->o_callback->sc_response !=
506 over_back_response; op->o_callback = sc_next ) {
507 sc_next = op->o_callback->sc_next;
508 if ( op->o_callback->sc_cleanup ) {
509 op->o_callback->sc_cleanup( op, rs );
520 slap_operation_t which
525 BackendDB *be = op->o_bd, db;
526 slap_callback cb = {NULL, over_back_response, NULL, NULL};
527 int rc = SLAP_CB_CONTINUE;
529 /* FIXME: used to happen for instance during abandon
530 * when global overlays are used... */
531 assert( op->o_bd != NULL );
533 oi = op->o_bd->bd_info->bi_private;
536 if ( !SLAP_ISOVERLAY( op->o_bd )) {
538 db.be_flags |= SLAP_DBFLAG_OVERLAY;
541 cb.sc_next = op->o_callback;
543 op->o_callback = &cb;
545 rc = overlay_op_walk( op, rs, which, oi, on );
548 op->o_callback = cb.sc_next;
553 over_op_bind( Operation *op, SlapReply *rs )
555 return over_op_func( op, rs, op_bind );
559 over_op_unbind( Operation *op, SlapReply *rs )
561 return over_op_func( op, rs, op_unbind );
565 over_op_search( Operation *op, SlapReply *rs )
567 return over_op_func( op, rs, op_search );
571 over_op_compare( Operation *op, SlapReply *rs )
573 return over_op_func( op, rs, op_compare );
577 over_op_modify( Operation *op, SlapReply *rs )
579 return over_op_func( op, rs, op_modify );
583 over_op_modrdn( Operation *op, SlapReply *rs )
585 return over_op_func( op, rs, op_modrdn );
589 over_op_add( Operation *op, SlapReply *rs )
591 return over_op_func( op, rs, op_add );
595 over_op_delete( Operation *op, SlapReply *rs )
597 return over_op_func( op, rs, op_delete );
601 over_op_abandon( Operation *op, SlapReply *rs )
603 return over_op_func( op, rs, op_abandon );
607 over_op_cancel( Operation *op, SlapReply *rs )
609 return over_op_func( op, rs, op_cancel );
613 over_op_extended( Operation *op, SlapReply *rs )
615 return over_op_func( op, rs, op_extended );
619 over_aux_operational( Operation *op, SlapReply *rs )
621 return over_op_func( op, rs, op_aux_operational );
625 over_aux_chk_referrals( Operation *op, SlapReply *rs )
627 return over_op_func( op, rs, op_aux_chk_referrals );
631 over_aux_chk_controls( Operation *op, SlapReply *rs )
633 return over_op_func( op, rs, op_aux_chk_controls );
643 over_connection_func(
646 enum conn_which which
652 int rc = SLAP_CB_CONTINUE;
653 BI_connection_init **func;
655 /* FIXME: used to happen for instance during abandon
656 * when global overlays are used... */
657 assert( bd != NULL );
659 oi = bd->bd_info->bi_private;
662 if ( !SLAP_ISOVERLAY( bd ) ) {
664 db.be_flags |= SLAP_DBFLAG_OVERLAY;
668 for ( ; on; on = on->on_next ) {
669 func = &on->on_bi.bi_connection_init;
670 if ( func[ which ] ) {
671 bd->bd_info = (BackendInfo *)on;
672 rc = func[ which ]( bd, conn );
673 if ( rc != SLAP_CB_CONTINUE ) break;
677 func = &oi->oi_orig->bi_connection_init;
678 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
679 bd->bd_info = oi->oi_orig;
680 rc = func[ which ]( bd, conn );
682 /* should not fall thru this far without anything happening... */
683 if ( rc == SLAP_CB_CONTINUE ) {
684 rc = LDAP_UNWILLING_TO_PERFORM;
691 over_connection_init(
696 return over_connection_func( bd, conn, conn_init );
700 over_connection_destroy(
705 return over_connection_func( bd, conn, conn_destroy );
713 on->on_next = overlays;
719 * iterator on registered overlays; overlay_next( NULL ) returns the first
720 * overlay; * subsequent calls with the previously returned value allow to
721 * iterate * over the entire list; returns NULL when no more overlays are
738 * returns a specific registered overlay based on the type; NULL if not
743 overlay_find( const char *over_type )
745 slap_overinst *on = overlays;
747 assert( over_type != NULL );
749 for ( ; on; on = on->on_next ) {
750 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
758 static const char overtype[] = "over";
761 * returns TRUE (1) if the database is actually an overlay instance;
762 * FALSE (0) otherwise.
766 overlay_is_over( BackendDB *be )
768 return be->bd_info->bi_type == overtype;
772 * returns TRUE (1) if the given database is actually an overlay
773 * instance and, somewhere in the list, contains the requested overlay;
774 * FALSE (0) otherwise.
778 overlay_is_inst( BackendDB *be, const char *over_type )
782 assert( be != NULL );
784 if ( !overlay_is_over( be ) ) {
788 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
789 for ( ; on; on = on->on_next ) {
790 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
799 overlay_register_control( BackendDB *be, const char *oid )
804 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
808 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
811 /* add to all backends... */
812 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
817 bd->be_ctrls[ cid ] = 1;
818 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
824 be->be_ctrls[ cid ] = 1;
825 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
832 overlay_destroy_one( BackendDB *be, slap_overinst *on )
834 slap_overinfo *oi = on->on_info;
835 slap_overinst **oidx;
837 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
840 if ( on->on_bi.bi_db_destroy ) {
841 BackendInfo *bi_orig = be->bd_info;
842 be->bd_info = (BackendInfo *)on;
843 on->on_bi.bi_db_destroy( be );
844 be->bd_info = bi_orig;
852 /* add an overlay to a particular backend. */
854 overlay_config( BackendDB *be, const char *ov )
856 slap_overinst *on = NULL, *on2 = NULL;
857 slap_overinfo *oi = NULL;
858 BackendInfo *bi = NULL;
860 on = overlay_find( ov );
862 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
866 /* If this is the first overlay on this backend, set up the
867 * overlay info structure
869 if ( !overlay_is_over( be ) ) {
870 oi = ch_malloc( sizeof( slap_overinfo ) );
871 oi->oi_orig = be->bd_info;
872 oi->oi_bi = *be->bd_info;
875 /* NOTE: the first time a global overlay is configured,
876 * frontendDB gets this flag; it is used later by overlays
877 * to determine if they're stacked on top of the frontendDB */
878 if ( oi->oi_orig == frontendDB->bd_info ) {
879 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
882 /* Save a pointer to ourself in bi_private.
884 oi->oi_bi.bi_private = oi;
886 bi = (BackendInfo *)oi;
888 bi->bi_type = (char *)overtype;
890 bi->bi_db_config = over_db_config;
891 bi->bi_db_open = over_db_open;
892 bi->bi_db_close = over_db_close;
893 bi->bi_db_destroy = over_db_destroy;
895 bi->bi_op_bind = over_op_bind;
896 bi->bi_op_unbind = over_op_unbind;
897 bi->bi_op_search = over_op_search;
898 bi->bi_op_compare = over_op_compare;
899 bi->bi_op_modify = over_op_modify;
900 bi->bi_op_modrdn = over_op_modrdn;
901 bi->bi_op_add = over_op_add;
902 bi->bi_op_delete = over_op_delete;
903 bi->bi_op_abandon = over_op_abandon;
904 bi->bi_op_cancel = over_op_cancel;
906 bi->bi_extended = over_op_extended;
909 * this is fine because it has the same
910 * args of the operations; we need to rework
911 * all the hooks to share the same args
912 * of the operations...
914 bi->bi_operational = over_aux_operational;
915 bi->bi_chk_referrals = over_aux_chk_referrals;
916 bi->bi_chk_controls = over_aux_chk_controls;
918 #ifdef SLAP_OVERLAY_ACCESS
919 /* these have specific arglists */
920 bi->bi_access_allowed = over_access_allowed;
921 bi->bi_acl_group = over_acl_group;
922 bi->bi_acl_attribute = over_acl_attribute;
923 #endif /* SLAP_OVERLAY_ACCESS */
925 bi->bi_connection_init = over_connection_init;
926 bi->bi_connection_destroy = over_connection_destroy;
931 if ( overlay_is_inst( be, ov ) ) {
932 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
933 "warning, overlay \"%s\" "
934 "already in list\n", ov, 0, 0 );
937 oi = be->bd_info->bi_private;
940 /* Insert new overlay on head of list. Overlays are executed
941 * in reverse of config order...
943 on2 = ch_calloc( 1, sizeof(slap_overinst) );
946 on2->on_next = oi->oi_list;
949 /* Any initialization needed? */
950 if ( on->on_bi.bi_db_init ) {
952 be->bd_info = (BackendInfo *)on2;
953 rc = on2->on_bi.bi_db_init( be );
954 be->bd_info = (BackendInfo *)oi;