1 /* translucent.c - translucent proxy module */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2009 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 */
48 static ConfigLDAPadd translucent_ldadd;
49 static ConfigCfAdd translucent_cfadd;
51 static ConfigDriver translucent_cf_gen;
58 static ConfigTable translucentcfg[] = {
59 { "translucent_strict", "on|off", 1, 2, 0,
60 ARG_ON_OFF|ARG_OFFSET,
61 (void *)offsetof(translucent_info, strict),
62 "( OLcfgOvAt:14.1 NAME 'olcTranslucentStrict' "
63 "DESC 'Reveal attribute deletion constraint violations' "
64 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
65 { "translucent_no_glue", "on|off", 1, 2, 0,
66 ARG_ON_OFF|ARG_OFFSET,
67 (void *)offsetof(translucent_info, no_glue),
68 "( OLcfgOvAt:14.2 NAME 'olcTranslucentNoGlue' "
69 "DESC 'Disable automatic glue records for ADD and MODRDN' "
70 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
71 { "translucent_local", "attr[,attr...]", 1, 2, 0,
72 ARG_MAGIC|TRANS_LOCAL,
74 "( OLcfgOvAt:14.3 NAME 'olcTranslucentLocal' "
75 "DESC 'Attributes to use in local search filter' "
76 "SYNTAX OMsDirectoryString )", NULL, NULL },
77 { "translucent_remote", "attr[,attr...]", 1, 2, 0,
78 ARG_MAGIC|TRANS_REMOTE,
80 "( OLcfgOvAt:14.4 NAME 'olcTranslucentRemote' "
81 "DESC 'Attributes to use in remote search filter' "
82 "SYNTAX OMsDirectoryString )", NULL, NULL },
83 { "translucent_bind_local", "on|off", 1, 2, 0,
84 ARG_ON_OFF|ARG_OFFSET,
85 (void *)offsetof(translucent_info, bind_local),
86 "( OLcfgOvAt:14.5 NAME 'olcTranslucentBindLocal' "
87 "DESC 'Enable local bind' "
88 "SYNTAX OMsBoolean SINGLE-VALUE)", NULL, NULL },
89 { "translucent_pwmod_local", "on|off", 1, 2, 0,
90 ARG_ON_OFF|ARG_OFFSET,
91 (void *)offsetof(translucent_info, pwmod_local),
92 "( OLcfgOvAt:14.6 NAME 'olcTranslucentPwModLocal' "
93 "DESC 'Enable local RFC 3062 Password Modify extended operation' "
94 "SYNTAX OMsBoolean SINGLE-VALUE)", NULL, NULL },
95 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
98 static ConfigOCs translucentocs[] = {
100 "NAME 'olcTranslucentConfig' "
101 "DESC 'Translucent configuration' "
102 "SUP olcOverlayConfig "
103 "MAY ( olcTranslucentStrict $ olcTranslucentNoGlue $"
104 " olcTranslucentLocal $ olcTranslucentRemote $"
105 " olcTranslucentBindLocal $ olcTranslucentPwModLocal ) )",
106 Cft_Overlay, translucentcfg, NULL, translucent_cfadd },
107 { "( OLcfgOvOc:14.2 "
108 "NAME 'olcTranslucentDatabase' "
109 "DESC 'Translucent target database configuration' "
110 "AUXILIARY )", Cft_Misc, olcDatabaseDummy, translucent_ldadd },
113 /* for translucent_init() */
116 translucent_ldadd_cleanup( ConfigArgs *ca )
118 slap_overinst *on = ca->ca_private;
119 translucent_info *ov = on->on_bi.bi_private;
121 ov->defer_db_open = 0;
122 return backend_startup_one( ca->be, &ca->reply );
126 translucent_ldadd( CfEntryInfo *cei, Entry *e, ConfigArgs *ca )
129 translucent_info *ov;
131 Debug(LDAP_DEBUG_TRACE, "==> translucent_ldadd\n", 0, 0, 0);
133 if ( cei->ce_type != Cft_Overlay || !cei->ce_bi ||
134 cei->ce_bi->bi_cf_ocs != translucentocs )
135 return LDAP_CONSTRAINT_VIOLATION;
137 on = (slap_overinst *)cei->ce_bi;
138 ov = on->on_bi.bi_private;
141 if ( CONFIG_ONLINE_ADD( ca ))
142 ca->cleanup = translucent_ldadd_cleanup;
144 ov->defer_db_open = 0;
150 translucent_cfadd( Operation *op, SlapReply *rs, Entry *e, ConfigArgs *ca )
152 CfEntryInfo *cei = e->e_private;
153 slap_overinst *on = (slap_overinst *)cei->ce_bi;
154 translucent_info *ov = on->on_bi.bi_private;
157 Debug(LDAP_DEBUG_TRACE, "==> translucent_cfadd\n", 0, 0, 0);
159 /* FIXME: should not hardcode "olcDatabase" here */
160 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
161 "olcDatabase=%s", ov->db.bd_info->bi_type );
162 if ( bv.bv_len >= sizeof( ca->cr_msg ) ) {
165 bv.bv_val = ca->cr_msg;
167 ov->defer_db_open = 0;
169 /* We can only create this entry if the database is table-driven
171 if ( ov->db.bd_info->bi_cf_ocs )
172 config_build_entry( op, rs, cei, ca, &bv,
173 ov->db.bd_info->bi_cf_ocs,
174 &translucentocs[1] );
180 translucent_cf_gen( ConfigArgs *c )
182 slap_overinst *on = (slap_overinst *)c->bi;
183 translucent_info *ov = on->on_bi.bi_private;
184 AttributeName **an, *a2;
187 if ( c->type == TRANS_LOCAL )
192 if ( c->op == SLAP_CONFIG_EMIT ) {
195 for ( i = 0; !BER_BVISNULL(&(*an)[i].an_name); i++ ) {
196 value_add_one( &c->rvalue_vals, &(*an)[i].an_name );
199 } else if ( c->op == LDAP_MOD_DELETE ) {
201 anlist_free( *an, 1, NULL );
205 ch_free( (*an)[i].an_name.bv_val );
207 (*an)[i] = (*an)[i+1];
208 } while ( !BER_BVISNULL( &(*an)[i].an_name ));
212 a2 = str2anlist( *an, c->argv[1], "," );
214 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s unable to parse attribute %s",
215 c->argv[0], c->argv[1] );
216 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
217 "%s: %s\n", c->log, c->cr_msg, 0 );
224 static slap_overinst translucent;
228 ** call syncrepl_add_glue() with the parent suffix;
232 static struct berval glue[] = { BER_BVC("top"), BER_BVC("glue"), BER_BVNULL };
234 void glue_parent(Operation *op) {
236 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
237 struct berval ndn = BER_BVNULL;
242 dnParent( &op->o_req_ndn, &pdn );
243 ber_dupbv_x( &ndn, &pdn, op->o_tmpmemctx );
245 Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", ndn.bv_val, 0, 0);
249 ber_dupbv(&e->e_name, &ndn);
250 ber_dupbv(&e->e_nname, &ndn);
252 a = attr_alloc( slap_schema.si_ad_objectClass );
254 a->a_vals = ch_malloc(sizeof(struct berval) * 3);
255 ber_dupbv(&a->a_vals[0], &glue[0]);
256 ber_dupbv(&a->a_vals[1], &glue[1]);
257 ber_dupbv(&a->a_vals[2], &glue[2]);
258 a->a_nvals = a->a_vals;
259 a->a_next = e->e_attrs;
262 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
264 a->a_vals = ch_malloc(sizeof(struct berval) * 2);
265 ber_dupbv(&a->a_vals[0], &glue[1]);
266 ber_dupbv(&a->a_vals[1], &glue[2]);
267 a->a_nvals = a->a_vals;
268 a->a_next = e->e_attrs;
275 nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
276 syncrepl_add_glue(&nop, e);
277 nop.o_bd->bd_info = (BackendInfo *) on;
279 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
286 ** free only the Attribute*, not the contents;
289 void free_attr_chain(Attribute *b) {
291 for(a=b; a; a=a->a_next) {
301 ** if not bound as root, send ACCESS error;
302 ** if glue, glue_parent();
307 static int translucent_add(Operation *op, SlapReply *rs) {
308 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
309 translucent_info *ov = on->on_bi.bi_private;
310 Debug(LDAP_DEBUG_TRACE, "==> translucent_add: %s\n",
311 op->o_req_dn.bv_val, 0, 0);
313 op->o_bd->bd_info = (BackendInfo *) on->on_info;
314 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
315 "user modification of overlay database not permitted");
316 op->o_bd->bd_info = (BackendInfo *) on;
319 if(!ov->no_glue) glue_parent(op);
320 return(SLAP_CB_CONTINUE);
324 ** translucent_modrdn()
325 ** if not bound as root, send ACCESS error;
326 ** if !glue, glue_parent();
327 ** else return CONTINUE;
331 static int translucent_modrdn(Operation *op, SlapReply *rs) {
332 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
333 translucent_info *ov = on->on_bi.bi_private;
334 Debug(LDAP_DEBUG_TRACE, "==> translucent_modrdn: %s -> %s\n",
335 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
337 op->o_bd->bd_info = (BackendInfo *) on->on_info;
338 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
339 "user modification of overlay database not permitted");
340 op->o_bd->bd_info = (BackendInfo *) on;
344 op->o_tag = LDAP_REQ_ADD;
346 op->o_tag = LDAP_REQ_MODRDN;
348 return(SLAP_CB_CONTINUE);
352 ** translucent_delete()
353 ** if not bound as root, send ACCESS error;
354 ** else return CONTINUE;
358 static int translucent_delete(Operation *op, SlapReply *rs) {
359 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
360 Debug(LDAP_DEBUG_TRACE, "==> translucent_delete: %s\n",
361 op->o_req_dn.bv_val, 0, 0);
363 op->o_bd->bd_info = (BackendInfo *) on->on_info;
364 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
365 "user modification of overlay database not permitted");
366 op->o_bd->bd_info = (BackendInfo *) on;
369 return(SLAP_CB_CONTINUE);
373 translucent_tag_cb( Operation *op, SlapReply *rs )
375 op->o_tag = LDAP_REQ_MODIFY;
376 op->orm_modlist = op->o_callback->sc_private;
377 rs->sr_tag = slap_req2res( op->o_tag );
379 return SLAP_CB_CONTINUE;
383 ** translucent_modify()
384 ** modify in local backend if exists in both;
385 ** otherwise, add to local backend;
386 ** fail if not defined in captive backend;
390 static int translucent_modify(Operation *op, SlapReply *rs) {
391 SlapReply nrs = { REP_RESULT };
393 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
394 translucent_info *ov = on->on_bi.bi_private;
395 Entry *e = NULL, *re = NULL;
397 Modifications *m, **mm;
399 int del, rc, erc = 0;
400 slap_callback cb = { 0 };
402 Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
403 op->o_req_dn.bv_val, 0, 0);
405 if(ov->defer_db_open) {
406 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
407 "remote DB not available");
411 ** fetch entry from the captive backend;
412 ** if it did not exist, fail;
413 ** release it, if captive backend supports this;
419 ov->db.be_acl = op->o_bd->be_acl;
420 rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
421 if(rc != LDAP_SUCCESS || re == NULL ) {
422 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
423 "attempt to modify nonexistent local record");
428 ** fetch entry from local backend;
430 ** foreach Modification:
431 ** if attr not present in local:
432 ** if Mod == LDAP_MOD_DELETE:
433 ** if remote attr not present, return NO_SUCH;
434 ** if remote attr present, drop this Mod;
435 ** else force this Mod to LDAP_MOD_ADD;
440 op->o_bd->bd_info = (BackendInfo *) on->on_info;
441 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
442 op->o_bd->bd_info = (BackendInfo *) on;
444 if(e && rc == LDAP_SUCCESS) {
445 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
446 for(mm = &op->orm_modlist; *mm; ) {
448 for(a = e->e_attrs; a; a = a->a_next)
449 if(a->a_desc == m->sml_desc) break;
452 continue; /* found local attr */
454 if(m->sml_op == LDAP_MOD_DELETE) {
455 for(a = re->e_attrs; a; a = a->a_next)
456 if(a->a_desc == m->sml_desc) break;
457 /* not found remote attr */
459 erc = LDAP_NO_SUCH_ATTRIBUTE;
463 erc = LDAP_CONSTRAINT_VIOLATION;
466 Debug(LDAP_DEBUG_TRACE,
467 "=> translucent_modify: silently dropping delete: %s\n",
468 m->sml_desc->ad_cname.bv_val, 0, 0);
471 slap_mods_free(m, 1);
474 m->sml_op = LDAP_MOD_ADD;
477 erc = SLAP_CB_CONTINUE;
480 if(ov->db.bd_info->bi_entry_release_rw) {
482 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
487 op->o_bd->bd_info = (BackendInfo *) on->on_info;
488 be_entry_release_r(op, e);
489 op->o_bd->bd_info = (BackendInfo *) on;
490 if(erc == SLAP_CB_CONTINUE) {
493 send_ldap_error(op, rs, erc,
494 "attempt to delete nonexistent attribute");
499 /* don't leak remote entry copy */
501 if(ov->db.bd_info->bi_entry_release_rw) {
503 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
509 ** foreach Modification:
510 ** if MOD_ADD or MOD_REPLACE, add Attribute;
511 ** if no Modifications were suitable:
512 ** if strict, throw CONSTRAINT_VIOLATION;
513 ** else, return early SUCCESS;
514 ** fabricate Entry with new Attribute chain;
515 ** glue_parent() for this Entry;
516 ** call bi_op_add() in local backend;
520 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
522 for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
524 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
525 ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
526 Debug(LDAP_DEBUG_ANY,
527 "=> translucent_modify: silently dropped modification(%d): %s\n",
528 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
529 if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
532 atmp.a_desc = m->sml_desc;
533 atmp.a_vals = m->sml_values;
534 atmp.a_nvals = m->sml_nvalues ? m->sml_nvalues : atmp.a_vals;
535 atmp.a_numvals = m->sml_numvals;
537 a = attr_dup( &atmp );
542 if(del && ov->strict) {
544 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
545 "attempt to delete attributes from local database");
551 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
552 "modification contained other than ADD or REPLACE");
555 /* rs->sr_text = "no valid modification found"; */
556 rs->sr_err = LDAP_SUCCESS;
557 send_ldap_result(op, rs);
562 ber_dupbv( &e->e_name, &op->o_req_dn );
563 ber_dupbv( &e->e_nname, &op->o_req_ndn );
566 op->o_tag = LDAP_REQ_ADD;
567 cb.sc_response = translucent_tag_cb;
568 cb.sc_private = op->orm_modlist;
573 cb.sc_next = op->o_callback;
574 op->o_callback = &cb;
575 rc = on->on_info->oi_orig->bi_op_add(op, &nrs);
576 if ( op->ora_e == e )
578 op->o_callback = cb.sc_next;
583 static int translucent_compare(Operation *op, SlapReply *rs) {
584 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
585 translucent_info *ov = on->on_bi.bi_private;
586 AttributeAssertion *ava = op->orc_ava;
591 Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
592 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
595 ** if the local backend has an entry for this attribute:
596 ** CONTINUE and let it do the compare;
599 rc = overlay_entry_get_ov(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e, on);
600 if(rc == LDAP_SUCCESS && e) {
601 overlay_entry_release_ov(op, e, 0, on);
602 return(SLAP_CB_CONTINUE);
605 if(ov->defer_db_open) {
606 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
607 "remote DB not available");
611 ** call compare() in the captive backend;
612 ** return the result;
617 ov->db.be_acl = op->o_bd->be_acl;
618 rc = ov->db.bd_info->bi_op_compare(op, rs);
624 static int translucent_pwmod(Operation *op, SlapReply *rs) {
625 SlapReply nrs = { REP_RESULT };
628 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
629 translucent_info *ov = on->on_bi.bi_private;
630 const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
631 Entry *e = NULL, *re = NULL;
634 slap_callback cb = { 0 };
636 if (!ov->pwmod_local) {
637 rs->sr_err = LDAP_CONSTRAINT_VIOLATION,
638 rs->sr_text = "attempt to modify password in local database";
643 ** fetch entry from the captive backend;
644 ** if it did not exist, fail;
645 ** release it, if captive backend supports this;
650 ov->db.be_acl = op->o_bd->be_acl;
651 rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
652 if(rc != LDAP_SUCCESS || re == NULL ) {
653 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
654 "attempt to modify nonexistent local record");
659 ** fetch entry from local backend;
664 op->o_bd->bd_info = (BackendInfo *) on->on_info;
665 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
666 op->o_bd->bd_info = (BackendInfo *) on;
668 if(e && rc == LDAP_SUCCESS) {
670 if(ov->db.bd_info->bi_entry_release_rw) {
672 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
678 op->o_bd->bd_info = (BackendInfo *) on->on_info;
679 be_entry_release_r(op, e);
680 op->o_bd->bd_info = (BackendInfo *) on;
681 return SLAP_CB_CONTINUE;
684 /* don't leak remote entry copy */
686 if(ov->db.bd_info->bi_entry_release_rw) {
688 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
695 ** glue_parent() for this Entry;
696 ** call bi_op_add() in local backend;
700 ber_dupbv( &e->e_name, &op->o_req_dn );
701 ber_dupbv( &e->e_nname, &op->o_req_ndn );
705 nop.o_tag = LDAP_REQ_ADD;
706 cb.sc_response = slap_null_cb;
711 nop.o_callback = &cb;
712 rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
713 if ( nop.ora_e == e ) {
717 if ( rc == LDAP_SUCCESS ) {
718 return SLAP_CB_CONTINUE;
724 static int translucent_exop(Operation *op, SlapReply *rs) {
725 SlapReply nrs = { REP_RESULT };
727 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
728 translucent_info *ov = on->on_bi.bi_private;
729 const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
731 Debug(LDAP_DEBUG_TRACE, "==> translucent_exop: %s\n",
732 op->o_req_dn.bv_val, 0, 0);
734 if(ov->defer_db_open) {
735 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
736 "remote DB not available");
740 if ( bvmatch( &bv_exop_pwmod, &op->ore_reqoid ) ) {
741 return translucent_pwmod( op, rs );
744 return SLAP_CB_CONTINUE;
748 ** translucent_search_cb()
749 ** merge local data with remote data
752 ** 1: remote search, no local filter
753 ** merge data and send immediately
754 ** 2: remote search, with local filter
755 ** merge data and save
756 ** 3: local search, no remote filter
757 ** merge data and send immediately
758 ** 4: local search, with remote filter
759 ** check list, merge, send, delete
766 typedef struct trans_ctx {
774 static int translucent_search_cb(Operation *op, SlapReply *rs) {
778 translucent_info *ov;
780 Attribute *a, *ax, *an, *as = NULL;
784 tc = op->o_callback->sc_private;
786 /* Don't let the op complete while we're gathering data */
787 if ( rs->sr_type == REP_RESULT && ( tc->step & USE_LIST ))
790 if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
791 return(SLAP_CB_CONTINUE);
793 Debug(LDAP_DEBUG_TRACE, "==> translucent_search_cb: %s\n",
794 rs->sr_entry->e_name.bv_val, 0, 0);
797 ov = on->on_bi.bi_private;
802 /* If we have local, get remote */
803 if ( tc->step & LCL_SIDE ) {
805 /* If entry is already on list, use it */
806 if ( tc->step & USE_LIST ) {
807 re = tavl_delete( &tc->list, le, entry_dn_cmp );
809 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
810 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
811 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
813 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
814 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
815 entry_free( rs->sr_entry );
817 rc = test_filter( op, re, tc->orig );
818 if ( rc == LDAP_COMPARE_TRUE ) {
819 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
821 return SLAP_CB_CONTINUE;
830 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &re );
831 if ( rc == LDAP_SUCCESS && re ) {
832 Entry *tmp = entry_dup( re );
833 be_entry_release_r( op, re );
838 /* Else we have remote, get local */
840 rc = overlay_entry_get_ov(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &le, on);
841 if ( rc == LDAP_SUCCESS && le ) {
842 re = entry_dup( rs->sr_entry );
843 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
844 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
845 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
847 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
848 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
849 entry_free( rs->sr_entry );
857 ** if we got remote and local entry:
858 ** foreach local attr:
859 ** foreach remote attr:
860 ** if match, remote attr with local attr;
861 ** if new local, add to list;
862 ** append new local attrs to remote;
867 for(ax = le->e_attrs; ax; ax = ax->a_next) {
868 for(a = re->e_attrs; a; a = a->a_next) {
869 if(a->a_desc == ax->a_desc) {
870 if(a->a_vals != a->a_nvals)
871 ber_bvarray_free(a->a_nvals);
872 ber_bvarray_free(a->a_vals);
873 ber_bvarray_dup_x( &a->a_vals, ax->a_vals, NULL );
874 if ( ax->a_vals == ax->a_nvals ) {
875 a->a_nvals = a->a_vals;
877 ber_bvarray_dup_x( &a->a_nvals, ax->a_nvals, NULL );
887 /* Dispose of local entry */
888 if ( tc->step & LCL_SIDE ) {
889 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
890 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
891 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
893 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
894 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
895 entry_free( rs->sr_entry );
898 overlay_entry_release_ov(op, le, 0, on);
901 /* literally append, so locals are always last */
904 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
910 /* If both filters, save entry for later */
911 if ( tc->step == (USE_LIST|RMT_SIDE) ) {
912 tavl_insert( &tc->list, re, entry_dn_cmp, avl_dup_error );
918 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
920 rc = test_filter( op, rs->sr_entry, tc->orig );
921 if ( rc == LDAP_COMPARE_TRUE ) {
922 rc = SLAP_CB_CONTINUE;
927 rc = SLAP_CB_CONTINUE;
931 /* Only a local entry: remote was deleted
932 * Ought to delete the local too...
935 } else if ( tc->step & USE_LIST ) {
936 /* Only a remote entry, but both filters:
937 * Test the complete filter
939 rc = test_filter( op, rs->sr_entry, tc->orig );
940 if ( rc == LDAP_COMPARE_TRUE ) {
941 rc = SLAP_CB_CONTINUE;
946 /* Only a remote entry, only remote filter:
949 rc = SLAP_CB_CONTINUE;
956 /* Dup the filter, excluding invalid elements */
958 trans_filter_dup(Operation *op, Filter *f, AttributeName *an)
965 switch( f->f_choice & SLAPD_FILTER_MASK ) {
966 case SLAPD_FILTER_COMPUTED:
967 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
968 n->f_choice = f->f_choice;
969 n->f_result = f->f_result;
973 case LDAP_FILTER_PRESENT:
974 if ( ad_inlist( f->f_desc, an )) {
975 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
976 n->f_choice = f->f_choice;
977 n->f_desc = f->f_desc;
982 case LDAP_FILTER_EQUALITY:
985 case LDAP_FILTER_APPROX:
986 case LDAP_FILTER_SUBSTRINGS:
987 case LDAP_FILTER_EXT:
988 if ( !f->f_av_desc || ad_inlist( f->f_av_desc, an )) {
989 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
990 n->f_choice = f->f_choice;
996 case LDAP_FILTER_AND:
998 case LDAP_FILTER_NOT: {
1001 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1002 n->f_choice = f->f_choice;
1005 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
1006 *p = trans_filter_dup( op, f, an );
1011 /* nothing valid in this list */
1013 op->o_tmpfree( n, op->o_tmpmemctx );
1016 /* Only 1 element in this list */
1017 if ((n->f_choice & SLAPD_FILTER_MASK) != LDAP_FILTER_NOT &&
1018 !n->f_list->f_next ) {
1021 op->o_tmpfree( f, op->o_tmpmemctx );
1030 trans_filter_free( Operation *op, Filter *f )
1032 Filter *n, *p, *next;
1034 f->f_choice &= SLAPD_FILTER_MASK;
1036 switch( f->f_choice ) {
1037 case LDAP_FILTER_AND:
1038 case LDAP_FILTER_OR:
1039 case LDAP_FILTER_NOT:
1040 /* Free in reverse order */
1042 for ( p = f->f_list; p; p = next ) {
1047 for ( p = n; p; p = next ) {
1049 trans_filter_free( op, p );
1055 op->o_tmpfree( f, op->o_tmpmemctx );
1059 ** translucent_search()
1060 ** search via captive backend;
1061 ** override results with any local data;
1065 static int translucent_search(Operation *op, SlapReply *rs) {
1066 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1067 translucent_info *ov = on->on_bi.bi_private;
1068 slap_callback cb = { NULL, NULL, NULL, NULL };
1074 Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
1075 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
1077 if(ov->defer_db_open) {
1078 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1079 "remote DB not available");
1083 fr = ov->remote ? trans_filter_dup( op, op->ors_filter, ov->remote ) : NULL;
1084 fl = ov->local ? trans_filter_dup( op, op->ors_filter, ov->local ) : NULL;
1085 cb.sc_response = (slap_response *) translucent_search_cb;
1086 cb.sc_private = &tc;
1087 cb.sc_next = op->o_callback;
1089 ov->db.be_acl = op->o_bd->be_acl;
1092 tc.orig = op->ors_filter;
1095 fbv = op->ors_filterstr;
1097 op->o_callback = &cb;
1100 AttributeName *attrs = op->ors_attrs;
1101 op->ors_attrs = NULL;
1103 tc.step |= RMT_SIDE;
1105 tc.step |= USE_LIST;
1106 op->ors_filter = fr;
1107 filter2bv_x( op, fr, &op->ors_filterstr );
1109 rc = ov->db.bd_info->bi_op_search(op, rs);
1110 op->ors_attrs = attrs;
1113 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1117 tc.step |= LCL_SIDE;
1118 op->ors_filter = fl;
1119 filter2bv_x( op, fl, &op->ors_filterstr );
1120 rc = overlay_op_walk( op, rs, op_search, on->on_info, on->on_next );
1121 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1123 op->ors_filterstr = fbv;
1124 op->ors_filter = tc.orig;
1125 op->o_callback = cb.sc_next;
1126 rs->sr_attrs = op->ors_attrs;
1128 /* Send out anything remaining on the list and finish */
1129 if ( tc.step & USE_LIST ) {
1133 av = tavl_end( tc.list, TAVL_DIR_LEFT );
1135 rs->sr_entry = av->avl_data;
1136 rc = test_filter( op, rs->sr_entry, op->ors_filter );
1137 if ( rc == LDAP_COMPARE_TRUE ) {
1138 rs->sr_flags = REP_ENTRY_MUSTBEFREED;
1139 rc = send_search_entry( op, rs );
1142 entry_free( rs->sr_entry );
1144 av = tavl_next( av, TAVL_DIR_RIGHT );
1146 tavl_free( tc.list, NULL );
1147 rs->sr_entry = NULL;
1149 send_ldap_result( op, rs );
1152 /* Free in reverse order */
1154 trans_filter_free( op, fl );
1156 trans_filter_free( op, fr );
1163 ** translucent_bind()
1164 ** pass bind request to captive backend;
1168 static int translucent_bind(Operation *op, SlapReply *rs) {
1169 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1170 translucent_info *ov = on->on_bi.bi_private;
1172 slap_callback sc = { 0 }, *save_cb;
1175 Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
1176 op->o_req_dn.bv_val, op->orb_method, 0);
1178 if(ov->defer_db_open) {
1179 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1180 "remote DB not available");
1184 if (ov->bind_local) {
1185 sc.sc_response = slap_null_cb;
1186 save_cb = op->o_callback;
1187 op->o_callback = ≻
1192 ov->db.be_acl = op->o_bd->be_acl;
1193 rc = ov->db.bd_info->bi_op_bind(op, rs);
1196 if (ov->bind_local) {
1197 op->o_callback = save_cb;
1198 if (rc != LDAP_SUCCESS) {
1199 rc = SLAP_CB_CONTINUE;
1207 ** translucent_connection_destroy()
1208 ** pass disconnect notification to captive backend;
1212 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
1213 slap_overinst *on = (slap_overinst *) be->bd_info;
1214 translucent_info *ov = on->on_bi.bi_private;
1217 Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
1219 rc = ov->db.bd_info->bi_connection_destroy(&ov->db, conn);
1225 ** translucent_db_config()
1226 ** pass config directives to captive backend;
1227 ** parse unrecognized directives ourselves;
1231 static int translucent_db_config(
1239 slap_overinst *on = (slap_overinst *) be->bd_info;
1240 translucent_info *ov = on->on_bi.bi_private;
1242 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_config: %s\n",
1243 argc ? argv[0] : "", 0, 0);
1245 /* Something for the captive database? */
1246 if ( ov->db.bd_info && ov->db.bd_info->bi_db_config )
1247 return ov->db.bd_info->bi_db_config( &ov->db, fname, lineno,
1249 return SLAP_CONF_UNKNOWN;
1253 ** translucent_db_init()
1254 ** initialize the captive backend;
1258 static int translucent_db_init(BackendDB *be, ConfigReply *cr) {
1259 slap_overinst *on = (slap_overinst *) be->bd_info;
1260 translucent_info *ov;
1262 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_init\n", 0, 0, 0);
1264 ov = ch_calloc(1, sizeof(translucent_info));
1265 on->on_bi.bi_private = ov;
1267 ov->db.be_private = NULL;
1268 ov->defer_db_open = 1;
1270 if ( !backend_db_init( "ldap", &ov->db, -1, NULL )) {
1271 Debug( LDAP_DEBUG_CONFIG, "translucent: unable to open captive back-ldap\n", 0, 0, 0);
1274 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1275 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
1281 ** translucent_db_open()
1282 ** if the captive backend has an open() method, call it;
1286 static int translucent_db_open(BackendDB *be, ConfigReply *cr) {
1287 slap_overinst *on = (slap_overinst *) be->bd_info;
1288 translucent_info *ov = on->on_bi.bi_private;
1291 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_open\n", 0, 0, 0);
1293 /* need to inherit something from the original database... */
1294 ov->db.be_def_limit = be->be_def_limit;
1295 ov->db.be_limits = be->be_limits;
1296 ov->db.be_acl = be->be_acl;
1297 ov->db.be_dfltaccess = be->be_dfltaccess;
1299 if ( ov->defer_db_open )
1302 rc = backend_startup_one( &ov->db, cr );
1304 if(rc) Debug(LDAP_DEBUG_TRACE,
1305 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
1311 ** translucent_db_close()
1312 ** if the captive backend has a close() method, call it
1317 translucent_db_close( BackendDB *be, ConfigReply *cr )
1319 slap_overinst *on = (slap_overinst *) be->bd_info;
1320 translucent_info *ov = on->on_bi.bi_private;
1323 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
1325 if ( ov && ov->db.bd_info && ov->db.bd_info->bi_db_close ) {
1326 rc = ov->db.bd_info->bi_db_close(&ov->db, NULL);
1333 ** translucent_db_destroy()
1334 ** if the captive backend has a db_destroy() method, call it;
1335 ** free any config data
1340 translucent_db_destroy( BackendDB *be, ConfigReply *cr )
1342 slap_overinst *on = (slap_overinst *) be->bd_info;
1343 translucent_info *ov = on->on_bi.bi_private;
1346 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_destroy\n", 0, 0, 0);
1350 anlist_free( ov->remote, 1, NULL );
1352 anlist_free( ov->local, 1, NULL );
1353 if ( ov->db.be_private != NULL ) {
1354 backend_stopdown_one( &ov->db );
1358 on->on_bi.bi_private = NULL;
1365 ** translucent_initialize()
1366 ** initialize the slap_overinst with our entry points;
1370 int translucent_initialize() {
1374 Debug(LDAP_DEBUG_TRACE, "==> translucent_initialize\n", 0, 0, 0);
1376 translucent.on_bi.bi_type = "translucent";
1377 translucent.on_bi.bi_db_init = translucent_db_init;
1378 translucent.on_bi.bi_db_config = translucent_db_config;
1379 translucent.on_bi.bi_db_open = translucent_db_open;
1380 translucent.on_bi.bi_db_close = translucent_db_close;
1381 translucent.on_bi.bi_db_destroy = translucent_db_destroy;
1382 translucent.on_bi.bi_op_bind = translucent_bind;
1383 translucent.on_bi.bi_op_add = translucent_add;
1384 translucent.on_bi.bi_op_modify = translucent_modify;
1385 translucent.on_bi.bi_op_modrdn = translucent_modrdn;
1386 translucent.on_bi.bi_op_delete = translucent_delete;
1387 translucent.on_bi.bi_op_search = translucent_search;
1388 translucent.on_bi.bi_op_compare = translucent_compare;
1389 translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
1390 translucent.on_bi.bi_extended = translucent_exop;
1392 translucent.on_bi.bi_cf_ocs = translucentocs;
1393 rc = config_register_schema ( translucentcfg, translucentocs );
1394 if ( rc ) return rc;
1396 return(overlay_register(&translucent));
1399 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
1400 int init_module(int argc, char *argv[]) {
1401 return translucent_initialize();
1405 #endif /* SLAPD_OVER_TRANSLUCENT */