1 /* translucent.c - translucent proxy module */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2008 The OpenLDAP Foundation.
6 * Portions Copyright 2005 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Symas Corp. for inclusion in
19 * OpenLDAP Software. This work was sponsored by Hewlett-Packard.
24 #ifdef SLAPD_OVER_TRANSLUCENT
28 #include <ac/string.h>
29 #include <ac/socket.h>
37 typedef struct translucent_info {
38 BackendDB db; /* captive backend */
39 AttributeName *local; /* valid attrs for local filters */
40 AttributeName *remote; /* valid attrs for remote filters */
46 static ConfigLDAPadd translucent_ldadd;
47 static ConfigCfAdd translucent_cfadd;
49 static ConfigDriver translucent_cf_gen;
56 static ConfigTable translucentcfg[] = {
57 { "translucent_strict", "on|off", 1, 2, 0,
58 ARG_ON_OFF|ARG_OFFSET,
59 (void *)offsetof(translucent_info, strict),
60 "( OLcfgOvAt:14.1 NAME 'olcTranslucentStrict' "
61 "DESC 'Reveal attribute deletion constraint violations' "
62 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
63 { "translucent_no_glue", "on|off", 1, 2, 0,
64 ARG_ON_OFF|ARG_OFFSET,
65 (void *)offsetof(translucent_info, no_glue),
66 "( OLcfgOvAt:14.2 NAME 'olcTranslucentNoGlue' "
67 "DESC 'Disable automatic glue records for ADD and MODRDN' "
68 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
69 { "translucent_local", "attr[,attr...]", 1, 2, 0,
70 ARG_MAGIC|TRANS_LOCAL,
72 "( OLcfgOvAt:14.3 NAME 'olcTranslucentLocal' "
73 "DESC 'Attributes to use in local search filter' "
74 "SYNTAX OMsDirectoryString )", NULL, NULL },
75 { "translucent_remote", "attr[,attr...]", 1, 2, 0,
76 ARG_MAGIC|TRANS_REMOTE,
78 "( OLcfgOvAt:14.4 NAME 'olcTranslucentRemote' "
79 "DESC 'Attributes to use in remote search filter' "
80 "SYNTAX OMsDirectoryString )", NULL, NULL },
81 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
84 static ConfigTable transdummy[] = {
85 { "", "", 0, 0, 0, ARG_IGNORED,
86 NULL, "( OLcfgGlAt:13 NAME 'olcDatabase' "
87 "DESC 'The backend type for a database instance' "
88 "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
89 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
92 static ConfigOCs translucentocs[] = {
94 "NAME 'olcTranslucentConfig' "
95 "DESC 'Translucent configuration' "
96 "SUP olcOverlayConfig "
97 "MAY ( olcTranslucentStrict $ olcTranslucentNoGlue $"
98 " olcTranslucentLocal $ olcTranslucentRemote ) )",
99 Cft_Overlay, translucentcfg, NULL, translucent_cfadd },
100 { "( OLcfgOvOc:14.2 "
101 "NAME 'olcTranslucentDatabase' "
102 "DESC 'Translucent target database configuration' "
103 "AUXILIARY )", Cft_Misc, transdummy, translucent_ldadd },
106 /* for translucent_init() */
109 translucent_ldadd_cleanup( ConfigArgs *ca )
111 slap_overinst *on = ca->ca_private;
112 translucent_info *ov = on->on_bi.bi_private;
114 ov->defer_db_open = 0;
115 return backend_startup_one( ca->be, &ca->reply );
119 translucent_ldadd( CfEntryInfo *cei, Entry *e, ConfigArgs *ca )
122 translucent_info *ov;
124 Debug(LDAP_DEBUG_TRACE, "==> translucent_ldadd\n", 0, 0, 0);
126 if ( cei->ce_type != Cft_Overlay || !cei->ce_bi ||
127 cei->ce_bi->bi_cf_ocs != translucentocs )
128 return LDAP_CONSTRAINT_VIOLATION;
130 on = (slap_overinst *)cei->ce_bi;
131 ov = on->on_bi.bi_private;
134 if ( CONFIG_ONLINE_ADD( ca ))
135 ca->cleanup = translucent_ldadd_cleanup;
137 ov->defer_db_open = 0;
143 translucent_cfadd( Operation *op, SlapReply *rs, Entry *e, ConfigArgs *ca )
145 CfEntryInfo *cei = e->e_private;
146 slap_overinst *on = (slap_overinst *)cei->ce_bi;
147 translucent_info *ov = on->on_bi.bi_private;
150 Debug(LDAP_DEBUG_TRACE, "==> translucent_cfadd\n", 0, 0, 0);
152 /* FIXME: should not hardcode "olcDatabase" here */
153 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
154 "olcDatabase=%s", ov->db.bd_info->bi_type );
155 if ( bv.bv_len < 0 || bv.bv_len >= sizeof( ca->cr_msg ) ) {
158 bv.bv_val = ca->cr_msg;
160 ov->defer_db_open = 0;
162 /* We can only create this entry if the database is table-driven
164 if ( ov->db.bd_info->bi_cf_ocs )
165 config_build_entry( op, rs, cei, ca, &bv,
166 ov->db.bd_info->bi_cf_ocs,
167 &translucentocs[1] );
173 translucent_cf_gen( ConfigArgs *c )
175 slap_overinst *on = (slap_overinst *)c->bi;
176 translucent_info *ov = on->on_bi.bi_private;
177 AttributeName **an, *a2;
180 if ( c->type == TRANS_LOCAL )
185 if ( c->op == SLAP_CONFIG_EMIT ) {
188 for ( i = 0; !BER_BVISNULL(&(*an)[i].an_name); i++ ) {
189 value_add_one( &c->rvalue_vals, &(*an)[i].an_name );
192 } else if ( c->op == LDAP_MOD_DELETE ) {
194 anlist_free( *an, 1, NULL );
198 ch_free( (*an)[i].an_name.bv_val );
200 (*an)[i] = (*an)[i+1];
201 } while ( !BER_BVISNULL( &(*an)[i].an_name ));
205 a2 = str2anlist( *an, c->argv[1], "," );
207 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s unable to parse attribute %s",
208 c->argv[0], c->argv[1] );
209 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
210 "%s: %s\n", c->log, c->cr_msg, 0 );
217 static slap_overinst translucent;
221 ** call syncrepl_add_glue() with the parent suffix;
225 static struct berval glue[] = { BER_BVC("top"), BER_BVC("glue"), BER_BVNULL };
227 void glue_parent(Operation *op) {
229 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
230 struct berval ndn = BER_BVNULL;
235 dnParent( &op->o_req_ndn, &pdn );
236 ber_dupbv_x( &ndn, &pdn, op->o_tmpmemctx );
238 Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", ndn.bv_val, 0, 0);
242 ber_dupbv(&e->e_name, &ndn);
243 ber_dupbv(&e->e_nname, &ndn);
245 a = attr_alloc( slap_schema.si_ad_objectClass );
247 a->a_vals = ch_malloc(sizeof(struct berval) * 3);
248 ber_dupbv(&a->a_vals[0], &glue[0]);
249 ber_dupbv(&a->a_vals[1], &glue[1]);
250 ber_dupbv(&a->a_vals[2], &glue[2]);
251 a->a_nvals = a->a_vals;
252 a->a_next = e->e_attrs;
255 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
257 a->a_vals = ch_malloc(sizeof(struct berval) * 2);
258 ber_dupbv(&a->a_vals[0], &glue[1]);
259 ber_dupbv(&a->a_vals[1], &glue[2]);
260 a->a_nvals = a->a_vals;
261 a->a_next = e->e_attrs;
268 nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
269 syncrepl_add_glue(&nop, e);
270 nop.o_bd->bd_info = (BackendInfo *) on;
272 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
282 BerVarray dup_bervarray(BerVarray b) {
285 for(len = 0; b[len].bv_val; len++);
286 nb = ch_malloc((len+1) * sizeof(BerValue));
287 for(i = 0; i < len; i++) ber_dupbv(&nb[i], &b[i]);
288 nb[len].bv_val = NULL;
295 ** free only the Attribute*, not the contents;
298 void free_attr_chain(Attribute *b) {
300 for(a=b; a; a=a->a_next) {
310 ** if not bound as root, send ACCESS error;
311 ** if glue, glue_parent();
316 static int translucent_add(Operation *op, SlapReply *rs) {
317 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
318 translucent_info *ov = on->on_bi.bi_private;
319 Debug(LDAP_DEBUG_TRACE, "==> translucent_add: %s\n",
320 op->o_req_dn.bv_val, 0, 0);
322 op->o_bd->bd_info = (BackendInfo *) on->on_info;
323 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
324 "user modification of overlay database not permitted");
325 op->o_bd->bd_info = (BackendInfo *) on;
328 if(!ov->no_glue) glue_parent(op);
329 return(SLAP_CB_CONTINUE);
333 ** translucent_modrdn()
334 ** if not bound as root, send ACCESS error;
335 ** if !glue, glue_parent();
336 ** else return CONTINUE;
340 static int translucent_modrdn(Operation *op, SlapReply *rs) {
341 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
342 translucent_info *ov = on->on_bi.bi_private;
343 Debug(LDAP_DEBUG_TRACE, "==> translucent_modrdn: %s -> %s\n",
344 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
346 op->o_bd->bd_info = (BackendInfo *) on->on_info;
347 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
348 "user modification of overlay database not permitted");
349 op->o_bd->bd_info = (BackendInfo *) on;
353 op->o_tag = LDAP_REQ_ADD;
355 op->o_tag = LDAP_REQ_MODRDN;
357 return(SLAP_CB_CONTINUE);
361 ** translucent_delete()
362 ** if not bound as root, send ACCESS error;
363 ** else return CONTINUE;
367 static int translucent_delete(Operation *op, SlapReply *rs) {
368 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
369 Debug(LDAP_DEBUG_TRACE, "==> translucent_delete: %s\n",
370 op->o_req_dn.bv_val, 0, 0);
372 op->o_bd->bd_info = (BackendInfo *) on->on_info;
373 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
374 "user modification of overlay database not permitted");
375 op->o_bd->bd_info = (BackendInfo *) on;
378 return(SLAP_CB_CONTINUE);
382 translucent_tag_cb( Operation *op, SlapReply *rs )
384 op->o_tag = LDAP_REQ_MODIFY;
385 op->orm_modlist = op->o_callback->sc_private;
386 rs->sr_tag = slap_req2res( op->o_tag );
388 return SLAP_CB_CONTINUE;
392 ** translucent_modify()
393 ** modify in local backend if exists in both;
394 ** otherwise, add to local backend;
395 ** fail if not defined in captive backend;
399 static int translucent_modify(Operation *op, SlapReply *rs) {
400 SlapReply nrs = { REP_RESULT };
402 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
403 translucent_info *ov = on->on_bi.bi_private;
404 Entry *e = NULL, *re = NULL;
406 Modifications *m, **mm;
408 int del, rc, erc = 0;
409 slap_callback cb = { 0 };
411 Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
412 op->o_req_dn.bv_val, 0, 0);
414 if(ov->defer_db_open) {
415 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
416 "remote DB not available");
420 ** fetch entry from the captive backend;
421 ** if it did not exist, fail;
422 ** release it, if captive backend supports this;
428 rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
429 if(rc != LDAP_SUCCESS || re == NULL ) {
430 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
431 "attempt to modify nonexistent local record");
436 ** fetch entry from local backend;
438 ** foreach Modification:
439 ** if attr not present in local:
440 ** if Mod == LDAP_MOD_DELETE:
441 ** if remote attr not present, return NO_SUCH;
442 ** if remote attr present, drop this Mod;
443 ** else force this Mod to LDAP_MOD_ADD;
448 op->o_bd->bd_info = (BackendInfo *) on->on_info;
449 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
450 op->o_bd->bd_info = (BackendInfo *) on;
452 if(e && rc == LDAP_SUCCESS) {
453 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
454 for(mm = &op->orm_modlist; *mm; ) {
456 for(a = e->e_attrs; a; a = a->a_next)
457 if(a->a_desc == m->sml_desc) break;
460 continue; /* found local attr */
462 if(m->sml_op == LDAP_MOD_DELETE) {
463 for(a = re->e_attrs; a; a = a->a_next)
464 if(a->a_desc == m->sml_desc) break;
465 /* not found remote attr */
467 erc = LDAP_NO_SUCH_ATTRIBUTE;
471 erc = LDAP_CONSTRAINT_VIOLATION;
474 Debug(LDAP_DEBUG_TRACE,
475 "=> translucent_modify: silently dropping delete: %s\n",
476 m->sml_desc->ad_cname.bv_val, 0, 0);
479 slap_mods_free(m, 1);
482 m->sml_op = LDAP_MOD_ADD;
485 erc = SLAP_CB_CONTINUE;
488 if(ov->db.bd_info->bi_entry_release_rw) {
490 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
495 op->o_bd->bd_info = (BackendInfo *) on->on_info;
496 be_entry_release_r(op, e);
497 op->o_bd->bd_info = (BackendInfo *) on;
498 if(erc == SLAP_CB_CONTINUE) {
501 send_ldap_error(op, rs, erc,
502 "attempt to delete nonexistent attribute");
507 /* don't leak remote entry copy */
509 if(ov->db.bd_info->bi_entry_release_rw) {
511 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
517 ** foreach Modification:
518 ** if MOD_ADD or MOD_REPLACE, add Attribute;
519 ** if no Modifications were suitable:
520 ** if strict, throw CONSTRAINT_VIOLATION;
521 ** else, return early SUCCESS;
522 ** fabricate Entry with new Attribute chain;
523 ** glue_parent() for this Entry;
524 ** call bi_op_add() in local backend;
528 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
530 for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
532 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
533 ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
534 Debug(LDAP_DEBUG_ANY,
535 "=> translucent_modify: silently dropped modification(%d): %s\n",
536 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
537 if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
540 atmp.a_desc = m->sml_desc;
541 atmp.a_vals = m->sml_values;
542 atmp.a_nvals = m->sml_nvalues ? m->sml_nvalues : atmp.a_vals;
543 atmp.a_numvals = m->sml_numvals;
545 a = attr_dup( &atmp );
550 if(del && ov->strict) {
552 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
553 "attempt to delete attributes from local database");
559 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
560 "modification contained other than ADD or REPLACE");
563 /* rs->sr_text = "no valid modification found"; */
564 rs->sr_err = LDAP_SUCCESS;
565 send_ldap_result(op, rs);
570 ber_dupbv( &e->e_name, &op->o_req_dn );
571 ber_dupbv( &e->e_nname, &op->o_req_ndn );
574 op->o_tag = LDAP_REQ_ADD;
575 cb.sc_response = translucent_tag_cb;
576 cb.sc_private = op->orm_modlist;
581 cb.sc_next = op->o_callback;
582 op->o_callback = &cb;
583 rc = on->on_info->oi_orig->bi_op_add(op, &nrs);
584 if ( op->ora_e == e )
586 op->o_callback = cb.sc_next;
591 static int translucent_compare(Operation *op, SlapReply *rs) {
592 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
593 translucent_info *ov = on->on_bi.bi_private;
594 AttributeAssertion *ava = op->orc_ava;
599 Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
600 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
603 ** if the local backend has an entry for this attribute:
604 ** CONTINUE and let it do the compare;
607 rc = overlay_entry_get_ov(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e, on);
608 if(rc == LDAP_SUCCESS && e) {
609 overlay_entry_release_ov(op, e, 0, on);
610 return(SLAP_CB_CONTINUE);
613 if(ov->defer_db_open) {
614 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
615 "remote DB not available");
619 ** call compare() in the captive backend;
620 ** return the result;
625 rc = ov->db.bd_info->bi_op_compare(op, rs);
632 ** translucent_search_cb()
633 ** merge local data with remote data
636 ** 1: remote search, no local filter
637 ** merge data and send immediately
638 ** 2: remote search, with local filter
639 ** merge data and save
640 ** 3: local search, no remote filter
641 ** merge data and send immediately
642 ** 4: local search, with remote filter
643 ** check list, merge, send, delete
650 typedef struct trans_ctx {
658 static int translucent_search_cb(Operation *op, SlapReply *rs) {
662 translucent_info *ov;
664 Attribute *a, *ax, *an, *as = NULL;
668 tc = op->o_callback->sc_private;
670 /* Don't let the op complete while we're gathering data */
671 if ( rs->sr_type == REP_RESULT && ( tc->step & USE_LIST ))
674 if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
675 return(SLAP_CB_CONTINUE);
677 Debug(LDAP_DEBUG_TRACE, "==> translucent_search_cb: %s\n",
678 rs->sr_entry->e_name.bv_val, 0, 0);
681 ov = on->on_bi.bi_private;
686 /* If we have local, get remote */
687 if ( tc->step & LCL_SIDE ) {
689 /* If entry is already on list, use it */
690 if ( tc->step & USE_LIST ) {
691 re = tavl_delete( &tc->list, le, entry_dn_cmp );
693 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
694 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
695 be_entry_release_r( op, rs->sr_entry );
697 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
698 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
699 entry_free( rs->sr_entry );
701 rc = test_filter( op, re, tc->orig );
702 if ( rc == LDAP_COMPARE_TRUE ) {
703 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
705 return SLAP_CB_CONTINUE;
714 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &re );
715 if ( rc == LDAP_SUCCESS && re ) {
716 Entry *tmp = entry_dup( re );
717 be_entry_release_r( op, re );
722 /* Else we have remote, get local */
724 rc = overlay_entry_get_ov(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &le, on);
725 if ( rc == LDAP_SUCCESS && le ) {
726 re = entry_dup( rs->sr_entry );
727 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
728 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
729 be_entry_release_r( op, rs->sr_entry );
731 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
732 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
733 entry_free( rs->sr_entry );
741 ** if we got remote and local entry:
742 ** foreach local attr:
743 ** foreach remote attr:
744 ** if match, remote attr with local attr;
745 ** if new local, add to list;
746 ** append new local attrs to remote;
751 for(ax = le->e_attrs; ax; ax = ax->a_next) {
752 for(a = re->e_attrs; a; a = a->a_next) {
753 if(a->a_desc == ax->a_desc) {
754 if(a->a_vals != a->a_nvals)
755 ber_bvarray_free(a->a_nvals);
756 ber_bvarray_free(a->a_vals);
757 a->a_vals = dup_bervarray(ax->a_vals);
758 a->a_nvals = (ax->a_vals == ax->a_nvals) ?
759 a->a_vals : dup_bervarray(ax->a_nvals);
768 /* Dispose of local entry */
769 if ( tc->step & LCL_SIDE ) {
770 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
771 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
772 be_entry_release_r( op, rs->sr_entry );
774 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
775 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
776 entry_free( rs->sr_entry );
779 overlay_entry_release_ov(op, le, 0, on);
782 /* literally append, so locals are always last */
785 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
791 /* If both filters, save entry for later */
792 if ( tc->step == (USE_LIST|RMT_SIDE) ) {
793 tavl_insert( &tc->list, re, entry_dn_cmp, avl_dup_error );
799 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
801 rc = test_filter( op, rs->sr_entry, tc->orig );
802 if ( rc == LDAP_COMPARE_TRUE ) {
803 rc = SLAP_CB_CONTINUE;
808 rc = SLAP_CB_CONTINUE;
812 /* Only a local entry: remote was deleted
813 * Ought to delete the local too...
816 } else if ( tc->step & USE_LIST ) {
817 /* Only a remote entry, but both filters:
818 * Test the complete filter
820 rc = test_filter( op, rs->sr_entry, tc->orig );
821 if ( rc == LDAP_COMPARE_TRUE ) {
822 rc = SLAP_CB_CONTINUE;
827 /* Only a remote entry, only remote filter:
830 rc = SLAP_CB_CONTINUE;
837 /* Dup the filter, excluding invalid elements */
839 trans_filter_dup(Operation *op, Filter *f, AttributeName *an)
846 switch( f->f_choice & SLAPD_FILTER_MASK ) {
847 case SLAPD_FILTER_COMPUTED:
848 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
849 n->f_choice = f->f_choice;
850 n->f_result = f->f_result;
854 case LDAP_FILTER_PRESENT:
855 if ( ad_inlist( f->f_desc, an )) {
856 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
857 n->f_choice = f->f_choice;
858 n->f_desc = f->f_desc;
863 case LDAP_FILTER_EQUALITY:
866 case LDAP_FILTER_APPROX:
867 case LDAP_FILTER_SUBSTRINGS:
868 case LDAP_FILTER_EXT:
869 if ( !f->f_av_desc || ad_inlist( f->f_av_desc, an )) {
870 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
871 n->f_choice = f->f_choice;
877 case LDAP_FILTER_AND:
879 case LDAP_FILTER_NOT: {
882 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
883 n->f_choice = f->f_choice;
886 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
887 *p = trans_filter_dup( op, f, an );
892 /* nothing valid in this list */
894 op->o_tmpfree( n, op->o_tmpmemctx );
897 /* Only 1 element in this list */
898 if ((n->f_choice & SLAPD_FILTER_MASK) != LDAP_FILTER_NOT &&
899 !n->f_list->f_next ) {
902 op->o_tmpfree( f, op->o_tmpmemctx );
911 trans_filter_free( Operation *op, Filter *f )
913 Filter *n, *p, *next;
915 f->f_choice &= SLAPD_FILTER_MASK;
917 switch( f->f_choice ) {
918 case LDAP_FILTER_AND:
920 case LDAP_FILTER_NOT:
921 /* Free in reverse order */
923 for ( p = f->f_list; p; p = next ) {
928 for ( p = n; p; p = next ) {
930 trans_filter_free( op, p );
936 op->o_tmpfree( f, op->o_tmpmemctx );
940 ** translucent_search()
941 ** search via captive backend;
942 ** override results with any local data;
946 static int translucent_search(Operation *op, SlapReply *rs) {
947 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
948 translucent_info *ov = on->on_bi.bi_private;
949 slap_callback cb = { NULL, NULL, NULL, NULL };
955 Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
956 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
958 if(ov->defer_db_open) {
959 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
960 "remote DB not available");
964 fr = ov->remote ? trans_filter_dup( op, op->ors_filter, ov->remote ) : NULL;
965 fl = ov->local ? trans_filter_dup( op, op->ors_filter, ov->local ) : NULL;
966 cb.sc_response = (slap_response *) translucent_search_cb;
968 cb.sc_next = op->o_callback;
972 tc.orig = op->ors_filter;
975 fbv = op->ors_filterstr;
977 op->o_callback = &cb;
985 filter2bv_x( op, fr, &op->ors_filterstr );
987 rc = ov->db.bd_info->bi_op_search(op, rs);
990 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
996 filter2bv_x( op, fl, &op->ors_filterstr );
997 rc = overlay_op_walk( op, rs, op_search, on->on_info, on->on_next );
998 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1000 op->ors_filterstr = fbv;
1001 op->ors_filter = tc.orig;
1002 op->o_callback = cb.sc_next;
1003 /* Send out anything remaining on the list and finish */
1004 if ( tc.step & USE_LIST ) {
1008 av = tavl_end( tc.list, TAVL_DIR_LEFT );
1010 rs->sr_entry = av->avl_data;
1011 rc = test_filter( op, rs->sr_entry, op->ors_filter );
1012 if ( rc == LDAP_COMPARE_TRUE ) {
1013 rs->sr_flags = REP_ENTRY_MUSTBEFREED;
1014 rc = send_search_entry( op, rs );
1017 entry_free( rs->sr_entry );
1019 av = tavl_next( av, TAVL_DIR_RIGHT );
1021 tavl_free( tc.list, NULL );
1022 rs->sr_entry = NULL;
1024 send_ldap_result( op, rs );
1027 /* Free in reverse order */
1029 trans_filter_free( op, fl );
1031 trans_filter_free( op, fr );
1038 ** translucent_bind()
1039 ** pass bind request to captive backend;
1043 static int translucent_bind(Operation *op, SlapReply *rs) {
1044 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1045 translucent_info *ov = on->on_bi.bi_private;
1049 Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
1050 op->o_req_dn.bv_val, op->orb_method, 0);
1052 if(ov->defer_db_open) {
1053 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1054 "remote DB not available");
1059 rc = ov->db.bd_info->bi_op_bind(op, rs);
1065 ** translucent_connection_destroy()
1066 ** pass disconnect notification to captive backend;
1070 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
1071 slap_overinst *on = (slap_overinst *) be->bd_info;
1072 translucent_info *ov = on->on_bi.bi_private;
1075 Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
1077 rc = ov->db.bd_info->bi_connection_destroy(&ov->db, conn);
1083 ** translucent_db_config()
1084 ** pass config directives to captive backend;
1085 ** parse unrecognized directives ourselves;
1089 static int translucent_db_config(
1097 slap_overinst *on = (slap_overinst *) be->bd_info;
1098 translucent_info *ov = on->on_bi.bi_private;
1100 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_config: %s\n",
1101 argc ? argv[0] : "", 0, 0);
1103 /* Something for the captive database? */
1104 if ( ov->db.bd_info && ov->db.bd_info->bi_db_config )
1105 return ov->db.bd_info->bi_db_config( &ov->db, fname, lineno,
1107 return SLAP_CONF_UNKNOWN;
1111 ** translucent_db_init()
1112 ** initialize the captive backend;
1116 static int translucent_db_init(BackendDB *be, ConfigReply *cr) {
1117 slap_overinst *on = (slap_overinst *) be->bd_info;
1118 translucent_info *ov;
1120 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_init\n", 0, 0, 0);
1122 ov = ch_calloc(1, sizeof(translucent_info));
1123 on->on_bi.bi_private = ov;
1125 ov->db.be_private = NULL;
1126 ov->db.be_pcl_mutexp = &ov->db.be_pcl_mutex;
1127 ov->defer_db_open = 1;
1129 if ( !backend_db_init( "ldap", &ov->db, -1, NULL )) {
1130 Debug( LDAP_DEBUG_CONFIG, "translucent: unable to open captive back-ldap\n", 0, 0, 0);
1133 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1134 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
1140 ** translucent_db_open()
1141 ** if the captive backend has an open() method, call it;
1145 static int translucent_db_open(BackendDB *be, ConfigReply *cr) {
1146 slap_overinst *on = (slap_overinst *) be->bd_info;
1147 translucent_info *ov = on->on_bi.bi_private;
1150 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_open\n", 0, 0, 0);
1152 /* need to inherit something from the original database... */
1153 ov->db.be_def_limit = be->be_def_limit;
1154 ov->db.be_limits = be->be_limits;
1155 ov->db.be_acl = be->be_acl;
1156 ov->db.be_dfltaccess = be->be_dfltaccess;
1158 if ( ov->defer_db_open )
1161 rc = backend_startup_one( &ov->db, cr );
1163 if(rc) Debug(LDAP_DEBUG_TRACE,
1164 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
1170 ** translucent_db_close()
1171 ** if the captive backend has a close() method, call it
1176 translucent_db_close( BackendDB *be, ConfigReply *cr )
1178 slap_overinst *on = (slap_overinst *) be->bd_info;
1179 translucent_info *ov = on->on_bi.bi_private;
1182 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
1184 if ( ov && ov->db.bd_info && ov->db.bd_info->bi_db_close ) {
1185 rc = ov->db.bd_info->bi_db_close(&ov->db, NULL);
1192 ** translucent_db_destroy()
1193 ** if the captive backend has a db_destroy() method, call it;
1194 ** free any config data
1199 translucent_db_destroy( BackendDB *be, ConfigReply *cr )
1201 slap_overinst *on = (slap_overinst *) be->bd_info;
1202 translucent_info *ov = on->on_bi.bi_private;
1205 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_destroy\n", 0, 0, 0);
1209 anlist_free( ov->remote, 1, NULL );
1211 anlist_free( ov->local, 1, NULL );
1212 if ( ov->db.be_private != NULL ) {
1213 backend_stopdown_one( &ov->db );
1217 on->on_bi.bi_private = NULL;
1224 ** translucent_initialize()
1225 ** initialize the slap_overinst with our entry points;
1229 int translucent_initialize() {
1233 Debug(LDAP_DEBUG_TRACE, "==> translucent_initialize\n", 0, 0, 0);
1235 translucent.on_bi.bi_type = "translucent";
1236 translucent.on_bi.bi_db_init = translucent_db_init;
1237 translucent.on_bi.bi_db_config = translucent_db_config;
1238 translucent.on_bi.bi_db_open = translucent_db_open;
1239 translucent.on_bi.bi_db_close = translucent_db_close;
1240 translucent.on_bi.bi_db_destroy = translucent_db_destroy;
1241 translucent.on_bi.bi_op_bind = translucent_bind;
1242 translucent.on_bi.bi_op_add = translucent_add;
1243 translucent.on_bi.bi_op_modify = translucent_modify;
1244 translucent.on_bi.bi_op_modrdn = translucent_modrdn;
1245 translucent.on_bi.bi_op_delete = translucent_delete;
1246 translucent.on_bi.bi_op_search = translucent_search;
1247 translucent.on_bi.bi_op_compare = translucent_compare;
1248 translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
1250 translucent.on_bi.bi_cf_ocs = translucentocs;
1251 rc = config_register_schema ( translucentcfg, translucentocs );
1252 if ( rc ) return rc;
1254 return(overlay_register(&translucent));
1257 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
1258 int init_module(int argc, char *argv[]) {
1259 return translucent_initialize();
1263 #endif /* SLAPD_OVER_TRANSLUCENT */