1 /* backover.c - backend overlay routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2007 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 );
214 for (next = on->on_next; on; on=next) {
224 over_back_response ( Operation *op, SlapReply *rs )
226 slap_overinfo *oi = op->o_callback->sc_private;
227 slap_overinst *on = oi->oi_list;
228 int rc = SLAP_CB_CONTINUE;
229 BackendDB *be = op->o_bd, db = *op->o_bd;
231 db.be_flags |= SLAP_DBFLAG_OVERLAY;
233 for (; on; on=on->on_next ) {
234 if ( on->on_response ) {
235 db.bd_info = (BackendInfo *)on;
236 rc = on->on_response( op, rs );
237 if ( rc != SLAP_CB_CONTINUE ) break;
240 /* Bypass the remaining on_response layers, but allow
241 * normal execution to continue.
243 if ( rc == SLAP_CB_BYPASS )
244 rc = SLAP_CB_CONTINUE;
249 #ifdef SLAP_OVERLAY_ACCESS
254 AttributeDescription *desc,
256 slap_access_t access,
257 AccessControlState *state,
263 BackendDB *be = op->o_bd, db;
264 int rc = SLAP_CB_CONTINUE;
266 /* FIXME: used to happen for instance during abandon
267 * when global overlays are used... */
268 assert( op->o_bd != NULL );
270 bi = op->o_bd->bd_info;
271 /* Were we invoked on the frontend? */
272 if ( !bi->bi_access_allowed ) {
273 oi = frontendDB->bd_info->bi_private;
275 oi = op->o_bd->bd_info->bi_private;
279 for ( ; on; on = on->on_next ) {
280 if ( on->on_bi.bi_access_allowed ) {
281 /* NOTE: do not copy the structure until required */
282 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
284 db.be_flags |= SLAP_DBFLAG_OVERLAY;
288 op->o_bd->bd_info = (BackendInfo *)on;
289 rc = on->on_bi.bi_access_allowed( op, e,
290 desc, val, access, state, maskp );
291 if ( rc != SLAP_CB_CONTINUE ) break;
295 if ( rc == SLAP_CB_CONTINUE ) {
296 BI_access_allowed *bi_access_allowed;
298 /* if the database structure was changed, o_bd points to a
299 * copy of the structure; put the original bd_info in place */
300 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
301 op->o_bd->bd_info = oi->oi_orig;
304 if ( oi->oi_orig->bi_access_allowed ) {
305 bi_access_allowed = oi->oi_orig->bi_access_allowed;
307 bi_access_allowed = slap_access_allowed;
310 rc = bi_access_allowed( op, e,
311 desc, val, access, state, maskp );
313 /* should not fall thru this far without anything happening... */
314 if ( rc == SLAP_CB_CONTINUE ) {
315 /* access not allowed */
320 op->o_bd->bd_info = bi;
329 struct berval *gr_ndn,
330 struct berval *op_ndn,
331 ObjectClass *group_oc,
332 AttributeDescription *group_at )
336 BackendInfo *bi = op->o_bd->bd_info;
337 BackendDB *be = op->o_bd, db;
338 int rc = SLAP_CB_CONTINUE;
340 /* FIXME: used to happen for instance during abandon
341 * when global overlays are used... */
342 assert( op->o_bd != NULL );
344 oi = op->o_bd->bd_info->bi_private;
347 for ( ; on; on = on->on_next ) {
348 if ( on->on_bi.bi_acl_group ) {
349 /* NOTE: do not copy the structure until required */
350 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
352 db.be_flags |= SLAP_DBFLAG_OVERLAY;
356 op->o_bd->bd_info = (BackendInfo *)on;
357 rc = on->on_bi.bi_acl_group( op, e,
358 gr_ndn, op_ndn, group_oc, group_at );
359 if ( rc != SLAP_CB_CONTINUE ) break;
363 if ( rc == SLAP_CB_CONTINUE ) {
364 BI_acl_group *bi_acl_group;
366 /* if the database structure was changed, o_bd points to a
367 * copy of the structure; put the original bd_info in place */
368 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
369 op->o_bd->bd_info = oi->oi_orig;
372 if ( oi->oi_orig->bi_acl_group ) {
373 bi_acl_group = oi->oi_orig->bi_acl_group;
375 bi_acl_group = backend_group;
378 rc = bi_acl_group( op, e,
379 gr_ndn, op_ndn, group_oc, group_at );
381 /* should not fall thru this far without anything happening... */
382 if ( rc == SLAP_CB_CONTINUE ) {
383 /* access not allowed */
388 op->o_bd->bd_info = bi;
397 struct berval *entry_ndn,
398 AttributeDescription *entry_at,
400 slap_access_t access )
404 BackendInfo *bi = op->o_bd->bd_info;
405 BackendDB *be = op->o_bd, db;
406 int rc = SLAP_CB_CONTINUE;
408 /* FIXME: used to happen for instance during abandon
409 * when global overlays are used... */
410 assert( op->o_bd != NULL );
412 oi = op->o_bd->bd_info->bi_private;
415 for ( ; on; on = on->on_next ) {
416 if ( on->on_bi.bi_acl_attribute ) {
417 /* NOTE: do not copy the structure until required */
418 if ( !SLAP_ISOVERLAY( op->o_bd ) ) {
420 db.be_flags |= SLAP_DBFLAG_OVERLAY;
424 op->o_bd->bd_info = (BackendInfo *)on;
425 rc = on->on_bi.bi_acl_attribute( op, target,
426 entry_ndn, entry_at, vals, access );
427 if ( rc != SLAP_CB_CONTINUE ) break;
431 if ( rc == SLAP_CB_CONTINUE ) {
432 BI_acl_attribute *bi_acl_attribute;
434 /* if the database structure was changed, o_bd points to a
435 * copy of the structure; put the original bd_info in place */
436 if ( SLAP_ISOVERLAY( op->o_bd ) ) {
437 op->o_bd->bd_info = oi->oi_orig;
440 if ( oi->oi_orig->bi_acl_attribute ) {
441 bi_acl_attribute = oi->oi_orig->bi_acl_attribute;
443 bi_acl_attribute = backend_attribute;
446 rc = bi_acl_attribute( op, target,
447 entry_ndn, entry_at, vals, access );
449 /* should not fall thru this far without anything happening... */
450 if ( rc == SLAP_CB_CONTINUE ) {
451 /* access not allowed */
456 op->o_bd->bd_info = bi;
460 #endif /* SLAP_OVERLAY_ACCESS */
463 * default return code in case of missing backend function
464 * and overlay stack returning SLAP_CB_CONTINUE
466 static int op_rc[ op_last ] = {
467 LDAP_UNWILLING_TO_PERFORM, /* bind */
468 LDAP_UNWILLING_TO_PERFORM, /* unbind */
469 LDAP_UNWILLING_TO_PERFORM, /* search */
470 SLAP_CB_CONTINUE, /* compare; pass to frontend */
471 LDAP_UNWILLING_TO_PERFORM, /* modify */
472 LDAP_UNWILLING_TO_PERFORM, /* modrdn */
473 LDAP_UNWILLING_TO_PERFORM, /* add */
474 LDAP_UNWILLING_TO_PERFORM, /* delete */
475 LDAP_UNWILLING_TO_PERFORM, /* abandon */
476 LDAP_UNWILLING_TO_PERFORM, /* cancel */
477 LDAP_UNWILLING_TO_PERFORM, /* extended */
478 LDAP_SUCCESS, /* aux_operational */
479 LDAP_SUCCESS, /* aux_chk_referrals */
480 SLAP_CB_CONTINUE /* aux_chk_controls; pass to frontend */
486 slap_operation_t which,
492 int rc = SLAP_CB_CONTINUE;
494 for (; on; on=on->on_next ) {
495 func = &on->on_bi.bi_op_bind;
497 op->o_bd->bd_info = (BackendInfo *)on;
498 rc = func[which]( op, rs );
499 if ( rc != SLAP_CB_CONTINUE ) break;
502 if ( rc == SLAP_CB_BYPASS )
503 rc = SLAP_CB_CONTINUE;
505 func = &oi->oi_orig->bi_op_bind;
506 if ( func[which] && rc == SLAP_CB_CONTINUE ) {
507 op->o_bd->bd_info = oi->oi_orig;
508 rc = func[which]( op, rs );
510 /* should not fall thru this far without anything happening... */
511 if ( rc == SLAP_CB_CONTINUE ) {
515 /* The underlying backend didn't handle the request, make sure
516 * overlay cleanup is processed.
518 if ( rc == LDAP_UNWILLING_TO_PERFORM ) {
519 slap_callback *sc_next;
520 for ( ; op->o_callback && op->o_callback->sc_response !=
521 over_back_response; op->o_callback = sc_next ) {
522 sc_next = op->o_callback->sc_next;
523 if ( op->o_callback->sc_cleanup ) {
524 op->o_callback->sc_cleanup( op, rs );
535 slap_operation_t which
540 BackendDB *be = op->o_bd, db;
541 slap_callback cb = {NULL, over_back_response, NULL, NULL};
542 int rc = SLAP_CB_CONTINUE;
544 /* FIXME: used to happen for instance during abandon
545 * when global overlays are used... */
546 assert( op->o_bd != NULL );
548 oi = op->o_bd->bd_info->bi_private;
551 if ( !SLAP_ISOVERLAY( op->o_bd )) {
553 db.be_flags |= SLAP_DBFLAG_OVERLAY;
556 cb.sc_next = op->o_callback;
558 op->o_callback = &cb;
560 rc = overlay_op_walk( op, rs, which, oi, on );
563 op->o_callback = cb.sc_next;
568 over_op_bind( Operation *op, SlapReply *rs )
570 return over_op_func( op, rs, op_bind );
574 over_op_unbind( Operation *op, SlapReply *rs )
576 return over_op_func( op, rs, op_unbind );
580 over_op_search( Operation *op, SlapReply *rs )
582 return over_op_func( op, rs, op_search );
586 over_op_compare( Operation *op, SlapReply *rs )
588 return over_op_func( op, rs, op_compare );
592 over_op_modify( Operation *op, SlapReply *rs )
594 return over_op_func( op, rs, op_modify );
598 over_op_modrdn( Operation *op, SlapReply *rs )
600 return over_op_func( op, rs, op_modrdn );
604 over_op_add( Operation *op, SlapReply *rs )
606 return over_op_func( op, rs, op_add );
610 over_op_delete( Operation *op, SlapReply *rs )
612 return over_op_func( op, rs, op_delete );
616 over_op_abandon( Operation *op, SlapReply *rs )
618 return over_op_func( op, rs, op_abandon );
622 over_op_cancel( Operation *op, SlapReply *rs )
624 return over_op_func( op, rs, op_cancel );
628 over_op_extended( Operation *op, SlapReply *rs )
630 return over_op_func( op, rs, op_extended );
634 over_aux_operational( Operation *op, SlapReply *rs )
636 return over_op_func( op, rs, op_aux_operational );
640 over_aux_chk_referrals( Operation *op, SlapReply *rs )
642 return over_op_func( op, rs, op_aux_chk_referrals );
646 over_aux_chk_controls( Operation *op, SlapReply *rs )
648 return over_op_func( op, rs, op_aux_chk_controls );
658 over_connection_func(
661 enum conn_which which
667 int rc = SLAP_CB_CONTINUE;
668 BI_connection_init **func;
670 /* FIXME: used to happen for instance during abandon
671 * when global overlays are used... */
672 assert( bd != NULL );
674 oi = bd->bd_info->bi_private;
677 if ( !SLAP_ISOVERLAY( bd ) ) {
679 db.be_flags |= SLAP_DBFLAG_OVERLAY;
683 for ( ; on; on = on->on_next ) {
684 func = &on->on_bi.bi_connection_init;
685 if ( func[ which ] ) {
686 bd->bd_info = (BackendInfo *)on;
687 rc = func[ which ]( bd, conn );
688 if ( rc != SLAP_CB_CONTINUE ) break;
692 func = &oi->oi_orig->bi_connection_init;
693 if ( func[ which ] && rc == SLAP_CB_CONTINUE ) {
694 bd->bd_info = oi->oi_orig;
695 rc = func[ which ]( bd, conn );
697 /* should not fall thru this far without anything happening... */
698 if ( rc == SLAP_CB_CONTINUE ) {
699 rc = LDAP_UNWILLING_TO_PERFORM;
706 over_connection_init(
711 return over_connection_func( bd, conn, conn_init );
715 over_connection_destroy(
720 return over_connection_func( bd, conn, conn_destroy );
728 on->on_next = overlays;
734 * iterator on registered overlays; overlay_next( NULL ) returns the first
735 * overlay; * subsequent calls with the previously returned value allow to
736 * iterate * over the entire list; returns NULL when no more overlays are
753 * returns a specific registered overlay based on the type; NULL if not
758 overlay_find( const char *over_type )
760 slap_overinst *on = overlays;
762 assert( over_type != NULL );
764 for ( ; on; on = on->on_next ) {
765 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
773 static const char overtype[] = "over";
776 * returns TRUE (1) if the database is actually an overlay instance;
777 * FALSE (0) otherwise.
781 overlay_is_over( BackendDB *be )
783 return be->bd_info->bi_type == overtype;
787 * returns TRUE (1) if the given database is actually an overlay
788 * instance and, somewhere in the list, contains the requested overlay;
789 * FALSE (0) otherwise.
793 overlay_is_inst( BackendDB *be, const char *over_type )
797 assert( be != NULL );
799 if ( !overlay_is_over( be ) ) {
803 on = ((slap_overinfo *)be->bd_info->bi_private)->oi_list;
804 for ( ; on; on = on->on_next ) {
805 if ( strcmp( on->on_bi.bi_type, over_type ) == 0 ) {
814 overlay_register_control( BackendDB *be, const char *oid )
819 if ( slap_find_control_id( oid, &cid ) == LDAP_CONTROL_NOT_FOUND ) {
823 if ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_GLOBAL_OVERLAY ) {
826 /* add to all backends... */
827 LDAP_STAILQ_FOREACH( bd, &backendDB, be_next ) {
832 bd->be_ctrls[ cid ] = 1;
833 bd->be_ctrls[ SLAP_MAX_CIDS ] = 1;
839 be->be_ctrls[ cid ] = 1;
840 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
847 overlay_destroy_one( BackendDB *be, slap_overinst *on )
849 slap_overinfo *oi = on->on_info;
850 slap_overinst **oidx;
852 for ( oidx = &oi->oi_list; *oidx; oidx = &(*oidx)->on_next ) {
855 if ( on->on_bi.bi_db_destroy ) {
856 BackendInfo *bi_orig = be->bd_info;
857 be->bd_info = (BackendInfo *)on;
858 on->on_bi.bi_db_destroy( be );
859 be->bd_info = bi_orig;
867 /* add an overlay to a particular backend. */
869 overlay_config( BackendDB *be, const char *ov )
871 slap_overinst *on = NULL, *on2 = NULL;
872 slap_overinfo *oi = NULL;
873 BackendInfo *bi = NULL;
875 on = overlay_find( ov );
877 Debug( LDAP_DEBUG_ANY, "overlay \"%s\" not found\n", ov, 0, 0 );
881 /* If this is the first overlay on this backend, set up the
882 * overlay info structure
884 if ( !overlay_is_over( be ) ) {
885 oi = ch_malloc( sizeof( slap_overinfo ) );
886 oi->oi_orig = be->bd_info;
887 oi->oi_bi = *be->bd_info;
890 /* NOTE: the first time a global overlay is configured,
891 * frontendDB gets this flag; it is used later by overlays
892 * to determine if they're stacked on top of the frontendDB */
893 if ( oi->oi_orig == frontendDB->bd_info ) {
894 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_GLOBAL_OVERLAY;
897 /* Save a pointer to ourself in bi_private.
899 oi->oi_bi.bi_private = oi;
901 bi = (BackendInfo *)oi;
903 bi->bi_type = (char *)overtype;
905 bi->bi_db_config = over_db_config;
906 bi->bi_db_open = over_db_open;
907 bi->bi_db_close = over_db_close;
908 bi->bi_db_destroy = over_db_destroy;
910 bi->bi_op_bind = over_op_bind;
911 bi->bi_op_unbind = over_op_unbind;
912 bi->bi_op_search = over_op_search;
913 bi->bi_op_compare = over_op_compare;
914 bi->bi_op_modify = over_op_modify;
915 bi->bi_op_modrdn = over_op_modrdn;
916 bi->bi_op_add = over_op_add;
917 bi->bi_op_delete = over_op_delete;
918 bi->bi_op_abandon = over_op_abandon;
919 bi->bi_op_cancel = over_op_cancel;
921 bi->bi_extended = over_op_extended;
924 * this is fine because it has the same
925 * args of the operations; we need to rework
926 * all the hooks to share the same args
927 * of the operations...
929 bi->bi_operational = over_aux_operational;
930 bi->bi_chk_referrals = over_aux_chk_referrals;
931 bi->bi_chk_controls = over_aux_chk_controls;
933 #ifdef SLAP_OVERLAY_ACCESS
934 /* these have specific arglists */
935 bi->bi_access_allowed = over_access_allowed;
936 bi->bi_acl_group = over_acl_group;
937 bi->bi_acl_attribute = over_acl_attribute;
938 #endif /* SLAP_OVERLAY_ACCESS */
940 bi->bi_connection_init = over_connection_init;
941 bi->bi_connection_destroy = over_connection_destroy;
946 if ( overlay_is_inst( be, ov ) ) {
947 Debug( LDAP_DEBUG_ANY, "overlay_config(): "
948 "warning, overlay \"%s\" "
949 "already in list\n", ov, 0, 0 );
952 oi = be->bd_info->bi_private;
955 /* Insert new overlay on head of list. Overlays are executed
956 * in reverse of config order...
958 on2 = ch_calloc( 1, sizeof(slap_overinst) );
961 on2->on_next = oi->oi_list;
964 /* Any initialization needed? */
965 if ( on->on_bi.bi_db_init ) {
967 be->bd_info = (BackendInfo *)on2;
968 rc = on2->on_bi.bi_db_init( be );
969 be->bd_info = (BackendInfo *)oi;
971 oi->oi_list = on2->on_next;