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 */
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 );
289 BerVarray dup_bervarray(BerVarray b) {
292 for(len = 0; b[len].bv_val; len++);
293 nb = ch_malloc((len+1) * sizeof(BerValue));
294 for(i = 0; i < len; i++) ber_dupbv(&nb[i], &b[i]);
295 nb[len].bv_val = NULL;
302 ** free only the Attribute*, not the contents;
305 void free_attr_chain(Attribute *b) {
307 for(a=b; a; a=a->a_next) {
317 ** if not bound as root, send ACCESS error;
318 ** if glue, glue_parent();
323 static int translucent_add(Operation *op, SlapReply *rs) {
324 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
325 translucent_info *ov = on->on_bi.bi_private;
326 Debug(LDAP_DEBUG_TRACE, "==> translucent_add: %s\n",
327 op->o_req_dn.bv_val, 0, 0);
329 op->o_bd->bd_info = (BackendInfo *) on->on_info;
330 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
331 "user modification of overlay database not permitted");
332 op->o_bd->bd_info = (BackendInfo *) on;
335 if(!ov->no_glue) glue_parent(op);
336 return(SLAP_CB_CONTINUE);
340 ** translucent_modrdn()
341 ** if not bound as root, send ACCESS error;
342 ** if !glue, glue_parent();
343 ** else return CONTINUE;
347 static int translucent_modrdn(Operation *op, SlapReply *rs) {
348 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
349 translucent_info *ov = on->on_bi.bi_private;
350 Debug(LDAP_DEBUG_TRACE, "==> translucent_modrdn: %s -> %s\n",
351 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
353 op->o_bd->bd_info = (BackendInfo *) on->on_info;
354 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
355 "user modification of overlay database not permitted");
356 op->o_bd->bd_info = (BackendInfo *) on;
360 op->o_tag = LDAP_REQ_ADD;
362 op->o_tag = LDAP_REQ_MODRDN;
364 return(SLAP_CB_CONTINUE);
368 ** translucent_delete()
369 ** if not bound as root, send ACCESS error;
370 ** else return CONTINUE;
374 static int translucent_delete(Operation *op, SlapReply *rs) {
375 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
376 Debug(LDAP_DEBUG_TRACE, "==> translucent_delete: %s\n",
377 op->o_req_dn.bv_val, 0, 0);
379 op->o_bd->bd_info = (BackendInfo *) on->on_info;
380 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
381 "user modification of overlay database not permitted");
382 op->o_bd->bd_info = (BackendInfo *) on;
385 return(SLAP_CB_CONTINUE);
389 translucent_tag_cb( Operation *op, SlapReply *rs )
391 op->o_tag = LDAP_REQ_MODIFY;
392 op->orm_modlist = op->o_callback->sc_private;
393 rs->sr_tag = slap_req2res( op->o_tag );
395 return SLAP_CB_CONTINUE;
399 ** translucent_modify()
400 ** modify in local backend if exists in both;
401 ** otherwise, add to local backend;
402 ** fail if not defined in captive backend;
406 static int translucent_modify(Operation *op, SlapReply *rs) {
407 SlapReply nrs = { REP_RESULT };
409 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
410 translucent_info *ov = on->on_bi.bi_private;
411 Entry *e = NULL, *re = NULL;
413 Modifications *m, **mm;
415 int del, rc, erc = 0;
416 slap_callback cb = { 0 };
418 Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
419 op->o_req_dn.bv_val, 0, 0);
421 if(ov->defer_db_open) {
422 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
423 "remote DB not available");
427 ** fetch entry from the captive backend;
428 ** if it did not exist, fail;
429 ** release it, if captive backend supports this;
435 rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
436 if(rc != LDAP_SUCCESS || re == NULL ) {
437 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
438 "attempt to modify nonexistent local record");
443 ** fetch entry from local backend;
445 ** foreach Modification:
446 ** if attr not present in local:
447 ** if Mod == LDAP_MOD_DELETE:
448 ** if remote attr not present, return NO_SUCH;
449 ** if remote attr present, drop this Mod;
450 ** else force this Mod to LDAP_MOD_ADD;
455 op->o_bd->bd_info = (BackendInfo *) on->on_info;
456 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
457 op->o_bd->bd_info = (BackendInfo *) on;
459 if(e && rc == LDAP_SUCCESS) {
460 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
461 for(mm = &op->orm_modlist; *mm; ) {
463 for(a = e->e_attrs; a; a = a->a_next)
464 if(a->a_desc == m->sml_desc) break;
467 continue; /* found local attr */
469 if(m->sml_op == LDAP_MOD_DELETE) {
470 for(a = re->e_attrs; a; a = a->a_next)
471 if(a->a_desc == m->sml_desc) break;
472 /* not found remote attr */
474 erc = LDAP_NO_SUCH_ATTRIBUTE;
478 erc = LDAP_CONSTRAINT_VIOLATION;
481 Debug(LDAP_DEBUG_TRACE,
482 "=> translucent_modify: silently dropping delete: %s\n",
483 m->sml_desc->ad_cname.bv_val, 0, 0);
486 slap_mods_free(m, 1);
489 m->sml_op = LDAP_MOD_ADD;
492 erc = SLAP_CB_CONTINUE;
495 if(ov->db.bd_info->bi_entry_release_rw) {
497 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
502 op->o_bd->bd_info = (BackendInfo *) on->on_info;
503 be_entry_release_r(op, e);
504 op->o_bd->bd_info = (BackendInfo *) on;
505 if(erc == SLAP_CB_CONTINUE) {
508 send_ldap_error(op, rs, erc,
509 "attempt to delete nonexistent attribute");
514 /* don't leak remote entry copy */
516 if(ov->db.bd_info->bi_entry_release_rw) {
518 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
524 ** foreach Modification:
525 ** if MOD_ADD or MOD_REPLACE, add Attribute;
526 ** if no Modifications were suitable:
527 ** if strict, throw CONSTRAINT_VIOLATION;
528 ** else, return early SUCCESS;
529 ** fabricate Entry with new Attribute chain;
530 ** glue_parent() for this Entry;
531 ** call bi_op_add() in local backend;
535 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
537 for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
539 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
540 ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
541 Debug(LDAP_DEBUG_ANY,
542 "=> translucent_modify: silently dropped modification(%d): %s\n",
543 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
544 if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
547 atmp.a_desc = m->sml_desc;
548 atmp.a_vals = m->sml_values;
549 atmp.a_nvals = m->sml_nvalues ? m->sml_nvalues : atmp.a_vals;
550 atmp.a_numvals = m->sml_numvals;
552 a = attr_dup( &atmp );
557 if(del && ov->strict) {
559 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
560 "attempt to delete attributes from local database");
566 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
567 "modification contained other than ADD or REPLACE");
570 /* rs->sr_text = "no valid modification found"; */
571 rs->sr_err = LDAP_SUCCESS;
572 send_ldap_result(op, rs);
577 ber_dupbv( &e->e_name, &op->o_req_dn );
578 ber_dupbv( &e->e_nname, &op->o_req_ndn );
581 op->o_tag = LDAP_REQ_ADD;
582 cb.sc_response = translucent_tag_cb;
583 cb.sc_private = op->orm_modlist;
588 cb.sc_next = op->o_callback;
589 op->o_callback = &cb;
590 rc = on->on_info->oi_orig->bi_op_add(op, &nrs);
591 if ( op->ora_e == e )
593 op->o_callback = cb.sc_next;
598 static int translucent_compare(Operation *op, SlapReply *rs) {
599 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
600 translucent_info *ov = on->on_bi.bi_private;
601 AttributeAssertion *ava = op->orc_ava;
606 Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
607 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
610 ** if the local backend has an entry for this attribute:
611 ** CONTINUE and let it do the compare;
614 rc = overlay_entry_get_ov(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e, on);
615 if(rc == LDAP_SUCCESS && e) {
616 overlay_entry_release_ov(op, e, 0, on);
617 return(SLAP_CB_CONTINUE);
620 if(ov->defer_db_open) {
621 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
622 "remote DB not available");
626 ** call compare() in the captive backend;
627 ** return the result;
632 rc = ov->db.bd_info->bi_op_compare(op, rs);
638 static int translucent_pwmod(Operation *op, SlapReply *rs) {
639 SlapReply nrs = { REP_RESULT };
642 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
643 translucent_info *ov = on->on_bi.bi_private;
644 const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
645 Entry *e = NULL, *re = NULL;
648 slap_callback cb = { 0 };
650 if (!ov->pwmod_local) {
651 rs->sr_err = LDAP_CONSTRAINT_VIOLATION,
652 rs->sr_text = "attempt to modify password in local database";
657 ** fetch entry from the captive backend;
658 ** if it did not exist, fail;
659 ** release it, if captive backend supports this;
664 rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
665 if(rc != LDAP_SUCCESS || re == NULL ) {
666 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
667 "attempt to modify nonexistent local record");
672 ** fetch entry from local backend;
677 op->o_bd->bd_info = (BackendInfo *) on->on_info;
678 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
679 op->o_bd->bd_info = (BackendInfo *) on;
681 if(e && rc == LDAP_SUCCESS) {
683 if(ov->db.bd_info->bi_entry_release_rw) {
685 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
691 op->o_bd->bd_info = (BackendInfo *) on->on_info;
692 be_entry_release_r(op, e);
693 op->o_bd->bd_info = (BackendInfo *) on;
694 return SLAP_CB_CONTINUE;
697 /* don't leak remote entry copy */
699 if(ov->db.bd_info->bi_entry_release_rw) {
701 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
708 ** glue_parent() for this Entry;
709 ** call bi_op_add() in local backend;
713 ber_dupbv( &e->e_name, &op->o_req_dn );
714 ber_dupbv( &e->e_nname, &op->o_req_ndn );
718 nop.o_tag = LDAP_REQ_ADD;
719 cb.sc_response = slap_null_cb;
724 nop.o_callback = &cb;
725 rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
726 if ( nop.ora_e == e ) {
730 if ( rc == LDAP_SUCCESS ) {
731 return SLAP_CB_CONTINUE;
737 static int translucent_exop(Operation *op, SlapReply *rs) {
738 SlapReply nrs = { REP_RESULT };
740 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
741 translucent_info *ov = on->on_bi.bi_private;
742 const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
744 Debug(LDAP_DEBUG_TRACE, "==> translucent_exop: %s\n",
745 op->o_req_dn.bv_val, 0, 0);
747 if(ov->defer_db_open) {
748 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
749 "remote DB not available");
753 if ( bvmatch( &bv_exop_pwmod, &op->ore_reqoid ) ) {
754 return translucent_pwmod( op, rs );
757 return SLAP_CB_CONTINUE;
761 ** translucent_search_cb()
762 ** merge local data with remote data
765 ** 1: remote search, no local filter
766 ** merge data and send immediately
767 ** 2: remote search, with local filter
768 ** merge data and save
769 ** 3: local search, no remote filter
770 ** merge data and send immediately
771 ** 4: local search, with remote filter
772 ** check list, merge, send, delete
779 typedef struct trans_ctx {
787 static int translucent_search_cb(Operation *op, SlapReply *rs) {
791 translucent_info *ov;
793 Attribute *a, *ax, *an, *as = NULL;
797 tc = op->o_callback->sc_private;
799 /* Don't let the op complete while we're gathering data */
800 if ( rs->sr_type == REP_RESULT && ( tc->step & USE_LIST ))
803 if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
804 return(SLAP_CB_CONTINUE);
806 Debug(LDAP_DEBUG_TRACE, "==> translucent_search_cb: %s\n",
807 rs->sr_entry->e_name.bv_val, 0, 0);
810 ov = on->on_bi.bi_private;
815 /* If we have local, get remote */
816 if ( tc->step & LCL_SIDE ) {
818 /* If entry is already on list, use it */
819 if ( tc->step & USE_LIST ) {
820 re = tavl_delete( &tc->list, le, entry_dn_cmp );
822 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
823 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
824 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
826 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
827 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
828 entry_free( rs->sr_entry );
830 rc = test_filter( op, re, tc->orig );
831 if ( rc == LDAP_COMPARE_TRUE ) {
832 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
834 return SLAP_CB_CONTINUE;
843 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &re );
844 if ( rc == LDAP_SUCCESS && re ) {
845 Entry *tmp = entry_dup( re );
846 be_entry_release_r( op, re );
851 /* Else we have remote, get local */
853 rc = overlay_entry_get_ov(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &le, on);
854 if ( rc == LDAP_SUCCESS && le ) {
855 re = entry_dup( rs->sr_entry );
856 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
857 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
858 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
860 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
861 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
862 entry_free( rs->sr_entry );
870 ** if we got remote and local entry:
871 ** foreach local attr:
872 ** foreach remote attr:
873 ** if match, remote attr with local attr;
874 ** if new local, add to list;
875 ** append new local attrs to remote;
880 for(ax = le->e_attrs; ax; ax = ax->a_next) {
881 for(a = re->e_attrs; a; a = a->a_next) {
882 if(a->a_desc == ax->a_desc) {
883 if(a->a_vals != a->a_nvals)
884 ber_bvarray_free(a->a_nvals);
885 ber_bvarray_free(a->a_vals);
886 a->a_vals = dup_bervarray(ax->a_vals);
887 a->a_nvals = (ax->a_vals == ax->a_nvals) ?
888 a->a_vals : dup_bervarray(ax->a_nvals);
897 /* Dispose of local entry */
898 if ( tc->step & LCL_SIDE ) {
899 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
900 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
901 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
903 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
904 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
905 entry_free( rs->sr_entry );
908 overlay_entry_release_ov(op, le, 0, on);
911 /* literally append, so locals are always last */
914 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
920 /* If both filters, save entry for later */
921 if ( tc->step == (USE_LIST|RMT_SIDE) ) {
922 tavl_insert( &tc->list, re, entry_dn_cmp, avl_dup_error );
928 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
930 rc = test_filter( op, rs->sr_entry, tc->orig );
931 if ( rc == LDAP_COMPARE_TRUE ) {
932 rc = SLAP_CB_CONTINUE;
937 rc = SLAP_CB_CONTINUE;
941 /* Only a local entry: remote was deleted
942 * Ought to delete the local too...
945 } else if ( tc->step & USE_LIST ) {
946 /* Only a remote entry, but both filters:
947 * Test the complete filter
949 rc = test_filter( op, rs->sr_entry, tc->orig );
950 if ( rc == LDAP_COMPARE_TRUE ) {
951 rc = SLAP_CB_CONTINUE;
956 /* Only a remote entry, only remote filter:
959 rc = SLAP_CB_CONTINUE;
966 /* Dup the filter, excluding invalid elements */
968 trans_filter_dup(Operation *op, Filter *f, AttributeName *an)
975 switch( f->f_choice & SLAPD_FILTER_MASK ) {
976 case SLAPD_FILTER_COMPUTED:
977 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
978 n->f_choice = f->f_choice;
979 n->f_result = f->f_result;
983 case LDAP_FILTER_PRESENT:
984 if ( ad_inlist( f->f_desc, an )) {
985 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
986 n->f_choice = f->f_choice;
987 n->f_desc = f->f_desc;
992 case LDAP_FILTER_EQUALITY:
995 case LDAP_FILTER_APPROX:
996 case LDAP_FILTER_SUBSTRINGS:
997 case LDAP_FILTER_EXT:
998 if ( !f->f_av_desc || ad_inlist( f->f_av_desc, an )) {
999 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1000 n->f_choice = f->f_choice;
1001 n->f_ava = f->f_ava;
1006 case LDAP_FILTER_AND:
1007 case LDAP_FILTER_OR:
1008 case LDAP_FILTER_NOT: {
1011 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1012 n->f_choice = f->f_choice;
1015 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
1016 *p = trans_filter_dup( op, f, an );
1021 /* nothing valid in this list */
1023 op->o_tmpfree( n, op->o_tmpmemctx );
1026 /* Only 1 element in this list */
1027 if ((n->f_choice & SLAPD_FILTER_MASK) != LDAP_FILTER_NOT &&
1028 !n->f_list->f_next ) {
1031 op->o_tmpfree( f, op->o_tmpmemctx );
1040 trans_filter_free( Operation *op, Filter *f )
1042 Filter *n, *p, *next;
1044 f->f_choice &= SLAPD_FILTER_MASK;
1046 switch( f->f_choice ) {
1047 case LDAP_FILTER_AND:
1048 case LDAP_FILTER_OR:
1049 case LDAP_FILTER_NOT:
1050 /* Free in reverse order */
1052 for ( p = f->f_list; p; p = next ) {
1057 for ( p = n; p; p = next ) {
1059 trans_filter_free( op, p );
1065 op->o_tmpfree( f, op->o_tmpmemctx );
1069 ** translucent_search()
1070 ** search via captive backend;
1071 ** override results with any local data;
1075 static int translucent_search(Operation *op, SlapReply *rs) {
1076 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1077 translucent_info *ov = on->on_bi.bi_private;
1078 slap_callback cb = { NULL, NULL, NULL, NULL };
1084 Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
1085 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
1087 if(ov->defer_db_open) {
1088 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1089 "remote DB not available");
1093 fr = ov->remote ? trans_filter_dup( op, op->ors_filter, ov->remote ) : NULL;
1094 fl = ov->local ? trans_filter_dup( op, op->ors_filter, ov->local ) : NULL;
1095 cb.sc_response = (slap_response *) translucent_search_cb;
1096 cb.sc_private = &tc;
1097 cb.sc_next = op->o_callback;
1101 tc.orig = op->ors_filter;
1104 fbv = op->ors_filterstr;
1106 op->o_callback = &cb;
1110 tc.step |= RMT_SIDE;
1112 tc.step |= USE_LIST;
1113 op->ors_filter = fr;
1114 filter2bv_x( op, fr, &op->ors_filterstr );
1116 rc = ov->db.bd_info->bi_op_search(op, rs);
1119 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1123 tc.step |= LCL_SIDE;
1124 op->ors_filter = fl;
1125 filter2bv_x( op, fl, &op->ors_filterstr );
1126 rc = overlay_op_walk( op, rs, op_search, on->on_info, on->on_next );
1127 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1129 op->ors_filterstr = fbv;
1130 op->ors_filter = tc.orig;
1131 op->o_callback = cb.sc_next;
1132 /* Send out anything remaining on the list and finish */
1133 if ( tc.step & USE_LIST ) {
1137 av = tavl_end( tc.list, TAVL_DIR_LEFT );
1139 rs->sr_entry = av->avl_data;
1140 rc = test_filter( op, rs->sr_entry, op->ors_filter );
1141 if ( rc == LDAP_COMPARE_TRUE ) {
1142 rs->sr_flags = REP_ENTRY_MUSTBEFREED;
1143 rc = send_search_entry( op, rs );
1146 entry_free( rs->sr_entry );
1148 av = tavl_next( av, TAVL_DIR_RIGHT );
1150 tavl_free( tc.list, NULL );
1151 rs->sr_entry = NULL;
1153 send_ldap_result( op, rs );
1156 /* Free in reverse order */
1158 trans_filter_free( op, fl );
1160 trans_filter_free( op, fr );
1167 ** translucent_bind()
1168 ** pass bind request to captive backend;
1172 static int translucent_bind(Operation *op, SlapReply *rs) {
1173 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1174 translucent_info *ov = on->on_bi.bi_private;
1176 slap_callback sc = { 0 }, *save_cb;
1179 Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
1180 op->o_req_dn.bv_val, op->orb_method, 0);
1182 if(ov->defer_db_open) {
1183 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1184 "remote DB not available");
1188 if (ov->bind_local) {
1189 sc.sc_response = slap_null_cb;
1190 save_cb = op->o_callback;
1191 op->o_callback = ≻
1196 rc = ov->db.bd_info->bi_op_bind(op, rs);
1199 if (ov->bind_local) {
1200 op->o_callback = save_cb;
1201 if (rc != LDAP_SUCCESS) {
1202 rc = SLAP_CB_CONTINUE;
1210 ** translucent_connection_destroy()
1211 ** pass disconnect notification to captive backend;
1215 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
1216 slap_overinst *on = (slap_overinst *) be->bd_info;
1217 translucent_info *ov = on->on_bi.bi_private;
1220 Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
1222 rc = ov->db.bd_info->bi_connection_destroy(&ov->db, conn);
1228 ** translucent_db_config()
1229 ** pass config directives to captive backend;
1230 ** parse unrecognized directives ourselves;
1234 static int translucent_db_config(
1242 slap_overinst *on = (slap_overinst *) be->bd_info;
1243 translucent_info *ov = on->on_bi.bi_private;
1245 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_config: %s\n",
1246 argc ? argv[0] : "", 0, 0);
1248 /* Something for the captive database? */
1249 if ( ov->db.bd_info && ov->db.bd_info->bi_db_config )
1250 return ov->db.bd_info->bi_db_config( &ov->db, fname, lineno,
1252 return SLAP_CONF_UNKNOWN;
1256 ** translucent_db_init()
1257 ** initialize the captive backend;
1261 static int translucent_db_init(BackendDB *be, ConfigReply *cr) {
1262 slap_overinst *on = (slap_overinst *) be->bd_info;
1263 translucent_info *ov;
1265 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_init\n", 0, 0, 0);
1267 ov = ch_calloc(1, sizeof(translucent_info));
1268 on->on_bi.bi_private = ov;
1270 ov->db.be_private = NULL;
1271 ov->db.be_pcl_mutexp = &ov->db.be_pcl_mutex;
1272 ov->defer_db_open = 1;
1274 if ( !backend_db_init( "ldap", &ov->db, -1, NULL )) {
1275 Debug( LDAP_DEBUG_CONFIG, "translucent: unable to open captive back-ldap\n", 0, 0, 0);
1278 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1279 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
1285 ** translucent_db_open()
1286 ** if the captive backend has an open() method, call it;
1290 static int translucent_db_open(BackendDB *be, ConfigReply *cr) {
1291 slap_overinst *on = (slap_overinst *) be->bd_info;
1292 translucent_info *ov = on->on_bi.bi_private;
1295 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_open\n", 0, 0, 0);
1297 /* need to inherit something from the original database... */
1298 ov->db.be_def_limit = be->be_def_limit;
1299 ov->db.be_limits = be->be_limits;
1300 ov->db.be_acl = be->be_acl;
1301 ov->db.be_dfltaccess = be->be_dfltaccess;
1303 if ( ov->defer_db_open )
1306 rc = backend_startup_one( &ov->db, cr );
1308 if(rc) Debug(LDAP_DEBUG_TRACE,
1309 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
1315 ** translucent_db_close()
1316 ** if the captive backend has a close() method, call it
1321 translucent_db_close( BackendDB *be, ConfigReply *cr )
1323 slap_overinst *on = (slap_overinst *) be->bd_info;
1324 translucent_info *ov = on->on_bi.bi_private;
1327 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
1329 if ( ov && ov->db.bd_info && ov->db.bd_info->bi_db_close ) {
1330 rc = ov->db.bd_info->bi_db_close(&ov->db, NULL);
1337 ** translucent_db_destroy()
1338 ** if the captive backend has a db_destroy() method, call it;
1339 ** free any config data
1344 translucent_db_destroy( BackendDB *be, ConfigReply *cr )
1346 slap_overinst *on = (slap_overinst *) be->bd_info;
1347 translucent_info *ov = on->on_bi.bi_private;
1350 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_destroy\n", 0, 0, 0);
1354 anlist_free( ov->remote, 1, NULL );
1356 anlist_free( ov->local, 1, NULL );
1357 if ( ov->db.be_private != NULL ) {
1358 backend_stopdown_one( &ov->db );
1362 on->on_bi.bi_private = NULL;
1369 ** translucent_initialize()
1370 ** initialize the slap_overinst with our entry points;
1374 int translucent_initialize() {
1378 Debug(LDAP_DEBUG_TRACE, "==> translucent_initialize\n", 0, 0, 0);
1380 translucent.on_bi.bi_type = "translucent";
1381 translucent.on_bi.bi_db_init = translucent_db_init;
1382 translucent.on_bi.bi_db_config = translucent_db_config;
1383 translucent.on_bi.bi_db_open = translucent_db_open;
1384 translucent.on_bi.bi_db_close = translucent_db_close;
1385 translucent.on_bi.bi_db_destroy = translucent_db_destroy;
1386 translucent.on_bi.bi_op_bind = translucent_bind;
1387 translucent.on_bi.bi_op_add = translucent_add;
1388 translucent.on_bi.bi_op_modify = translucent_modify;
1389 translucent.on_bi.bi_op_modrdn = translucent_modrdn;
1390 translucent.on_bi.bi_op_delete = translucent_delete;
1391 translucent.on_bi.bi_op_search = translucent_search;
1392 translucent.on_bi.bi_op_compare = translucent_compare;
1393 translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
1394 translucent.on_bi.bi_extended = translucent_exop;
1396 translucent.on_bi.bi_cf_ocs = translucentocs;
1397 rc = config_register_schema ( translucentcfg, translucentocs );
1398 if ( rc ) return rc;
1400 return(overlay_register(&translucent));
1403 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
1404 int init_module(int argc, char *argv[]) {
1405 return translucent_initialize();
1409 #endif /* SLAPD_OVER_TRANSLUCENT */