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 ov->db.be_acl = op->o_bd->be_acl;
436 rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
437 if(rc != LDAP_SUCCESS || re == NULL ) {
438 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
439 "attempt to modify nonexistent local record");
444 ** fetch entry from local backend;
446 ** foreach Modification:
447 ** if attr not present in local:
448 ** if Mod == LDAP_MOD_DELETE:
449 ** if remote attr not present, return NO_SUCH;
450 ** if remote attr present, drop this Mod;
451 ** else force this Mod to LDAP_MOD_ADD;
456 op->o_bd->bd_info = (BackendInfo *) on->on_info;
457 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
458 op->o_bd->bd_info = (BackendInfo *) on;
460 if(e && rc == LDAP_SUCCESS) {
461 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
462 for(mm = &op->orm_modlist; *mm; ) {
464 for(a = e->e_attrs; a; a = a->a_next)
465 if(a->a_desc == m->sml_desc) break;
468 continue; /* found local attr */
470 if(m->sml_op == LDAP_MOD_DELETE) {
471 for(a = re->e_attrs; a; a = a->a_next)
472 if(a->a_desc == m->sml_desc) break;
473 /* not found remote attr */
475 erc = LDAP_NO_SUCH_ATTRIBUTE;
479 erc = LDAP_CONSTRAINT_VIOLATION;
482 Debug(LDAP_DEBUG_TRACE,
483 "=> translucent_modify: silently dropping delete: %s\n",
484 m->sml_desc->ad_cname.bv_val, 0, 0);
487 slap_mods_free(m, 1);
490 m->sml_op = LDAP_MOD_ADD;
493 erc = SLAP_CB_CONTINUE;
496 if(ov->db.bd_info->bi_entry_release_rw) {
498 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
503 op->o_bd->bd_info = (BackendInfo *) on->on_info;
504 be_entry_release_r(op, e);
505 op->o_bd->bd_info = (BackendInfo *) on;
506 if(erc == SLAP_CB_CONTINUE) {
509 send_ldap_error(op, rs, erc,
510 "attempt to delete nonexistent attribute");
515 /* don't leak remote entry copy */
517 if(ov->db.bd_info->bi_entry_release_rw) {
519 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
525 ** foreach Modification:
526 ** if MOD_ADD or MOD_REPLACE, add Attribute;
527 ** if no Modifications were suitable:
528 ** if strict, throw CONSTRAINT_VIOLATION;
529 ** else, return early SUCCESS;
530 ** fabricate Entry with new Attribute chain;
531 ** glue_parent() for this Entry;
532 ** call bi_op_add() in local backend;
536 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
538 for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
540 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
541 ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
542 Debug(LDAP_DEBUG_ANY,
543 "=> translucent_modify: silently dropped modification(%d): %s\n",
544 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
545 if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
548 atmp.a_desc = m->sml_desc;
549 atmp.a_vals = m->sml_values;
550 atmp.a_nvals = m->sml_nvalues ? m->sml_nvalues : atmp.a_vals;
551 atmp.a_numvals = m->sml_numvals;
553 a = attr_dup( &atmp );
558 if(del && ov->strict) {
560 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
561 "attempt to delete attributes from local database");
567 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
568 "modification contained other than ADD or REPLACE");
571 /* rs->sr_text = "no valid modification found"; */
572 rs->sr_err = LDAP_SUCCESS;
573 send_ldap_result(op, rs);
578 ber_dupbv( &e->e_name, &op->o_req_dn );
579 ber_dupbv( &e->e_nname, &op->o_req_ndn );
582 op->o_tag = LDAP_REQ_ADD;
583 cb.sc_response = translucent_tag_cb;
584 cb.sc_private = op->orm_modlist;
589 cb.sc_next = op->o_callback;
590 op->o_callback = &cb;
591 rc = on->on_info->oi_orig->bi_op_add(op, &nrs);
592 if ( op->ora_e == e )
594 op->o_callback = cb.sc_next;
599 static int translucent_compare(Operation *op, SlapReply *rs) {
600 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
601 translucent_info *ov = on->on_bi.bi_private;
602 AttributeAssertion *ava = op->orc_ava;
607 Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
608 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
611 ** if the local backend has an entry for this attribute:
612 ** CONTINUE and let it do the compare;
615 rc = overlay_entry_get_ov(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e, on);
616 if(rc == LDAP_SUCCESS && e) {
617 overlay_entry_release_ov(op, e, 0, on);
618 return(SLAP_CB_CONTINUE);
621 if(ov->defer_db_open) {
622 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
623 "remote DB not available");
627 ** call compare() in the captive backend;
628 ** return the result;
633 ov->db.be_acl = op->o_bd->be_acl;
634 rc = ov->db.bd_info->bi_op_compare(op, rs);
640 static int translucent_pwmod(Operation *op, SlapReply *rs) {
641 SlapReply nrs = { REP_RESULT };
644 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
645 translucent_info *ov = on->on_bi.bi_private;
646 const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
647 Entry *e = NULL, *re = NULL;
650 slap_callback cb = { 0 };
652 if (!ov->pwmod_local) {
653 rs->sr_err = LDAP_CONSTRAINT_VIOLATION,
654 rs->sr_text = "attempt to modify password in local database";
659 ** fetch entry from the captive backend;
660 ** if it did not exist, fail;
661 ** release it, if captive backend supports this;
666 ov->db.be_acl = op->o_bd->be_acl;
667 rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
668 if(rc != LDAP_SUCCESS || re == NULL ) {
669 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
670 "attempt to modify nonexistent local record");
675 ** fetch entry from local backend;
680 op->o_bd->bd_info = (BackendInfo *) on->on_info;
681 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
682 op->o_bd->bd_info = (BackendInfo *) on;
684 if(e && rc == LDAP_SUCCESS) {
686 if(ov->db.bd_info->bi_entry_release_rw) {
688 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
694 op->o_bd->bd_info = (BackendInfo *) on->on_info;
695 be_entry_release_r(op, e);
696 op->o_bd->bd_info = (BackendInfo *) on;
697 return SLAP_CB_CONTINUE;
700 /* don't leak remote entry copy */
702 if(ov->db.bd_info->bi_entry_release_rw) {
704 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
711 ** glue_parent() for this Entry;
712 ** call bi_op_add() in local backend;
716 ber_dupbv( &e->e_name, &op->o_req_dn );
717 ber_dupbv( &e->e_nname, &op->o_req_ndn );
721 nop.o_tag = LDAP_REQ_ADD;
722 cb.sc_response = slap_null_cb;
727 nop.o_callback = &cb;
728 rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
729 if ( nop.ora_e == e ) {
733 if ( rc == LDAP_SUCCESS ) {
734 return SLAP_CB_CONTINUE;
740 static int translucent_exop(Operation *op, SlapReply *rs) {
741 SlapReply nrs = { REP_RESULT };
743 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
744 translucent_info *ov = on->on_bi.bi_private;
745 const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
747 Debug(LDAP_DEBUG_TRACE, "==> translucent_exop: %s\n",
748 op->o_req_dn.bv_val, 0, 0);
750 if(ov->defer_db_open) {
751 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
752 "remote DB not available");
756 if ( bvmatch( &bv_exop_pwmod, &op->ore_reqoid ) ) {
757 return translucent_pwmod( op, rs );
760 return SLAP_CB_CONTINUE;
764 ** translucent_search_cb()
765 ** merge local data with remote data
768 ** 1: remote search, no local filter
769 ** merge data and send immediately
770 ** 2: remote search, with local filter
771 ** merge data and save
772 ** 3: local search, no remote filter
773 ** merge data and send immediately
774 ** 4: local search, with remote filter
775 ** check list, merge, send, delete
782 typedef struct trans_ctx {
790 static int translucent_search_cb(Operation *op, SlapReply *rs) {
794 translucent_info *ov;
796 Attribute *a, *ax, *an, *as = NULL;
800 tc = op->o_callback->sc_private;
802 /* Don't let the op complete while we're gathering data */
803 if ( rs->sr_type == REP_RESULT && ( tc->step & USE_LIST ))
806 if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
807 return(SLAP_CB_CONTINUE);
809 Debug(LDAP_DEBUG_TRACE, "==> translucent_search_cb: %s\n",
810 rs->sr_entry->e_name.bv_val, 0, 0);
813 ov = on->on_bi.bi_private;
818 /* If we have local, get remote */
819 if ( tc->step & LCL_SIDE ) {
821 /* If entry is already on list, use it */
822 if ( tc->step & USE_LIST ) {
823 re = tavl_delete( &tc->list, le, entry_dn_cmp );
825 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
826 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
827 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
829 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
830 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
831 entry_free( rs->sr_entry );
833 rc = test_filter( op, re, tc->orig );
834 if ( rc == LDAP_COMPARE_TRUE ) {
835 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
837 return SLAP_CB_CONTINUE;
846 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &re );
847 if ( rc == LDAP_SUCCESS && re ) {
848 Entry *tmp = entry_dup( re );
849 be_entry_release_r( op, re );
854 /* Else we have remote, get local */
856 rc = overlay_entry_get_ov(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &le, on);
857 if ( rc == LDAP_SUCCESS && le ) {
858 re = entry_dup( rs->sr_entry );
859 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
860 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
861 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
863 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
864 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
865 entry_free( rs->sr_entry );
873 ** if we got remote and local entry:
874 ** foreach local attr:
875 ** foreach remote attr:
876 ** if match, remote attr with local attr;
877 ** if new local, add to list;
878 ** append new local attrs to remote;
883 for(ax = le->e_attrs; ax; ax = ax->a_next) {
884 for(a = re->e_attrs; a; a = a->a_next) {
885 if(a->a_desc == ax->a_desc) {
886 if(a->a_vals != a->a_nvals)
887 ber_bvarray_free(a->a_nvals);
888 ber_bvarray_free(a->a_vals);
889 a->a_vals = dup_bervarray(ax->a_vals);
890 a->a_nvals = (ax->a_vals == ax->a_nvals) ?
891 a->a_vals : dup_bervarray(ax->a_nvals);
900 /* Dispose of local entry */
901 if ( tc->step & LCL_SIDE ) {
902 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
903 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
904 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
906 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
907 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
908 entry_free( rs->sr_entry );
911 overlay_entry_release_ov(op, le, 0, on);
914 /* literally append, so locals are always last */
917 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
923 /* If both filters, save entry for later */
924 if ( tc->step == (USE_LIST|RMT_SIDE) ) {
925 tavl_insert( &tc->list, re, entry_dn_cmp, avl_dup_error );
931 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
933 rc = test_filter( op, rs->sr_entry, tc->orig );
934 if ( rc == LDAP_COMPARE_TRUE ) {
935 rc = SLAP_CB_CONTINUE;
940 rc = SLAP_CB_CONTINUE;
944 /* Only a local entry: remote was deleted
945 * Ought to delete the local too...
948 } else if ( tc->step & USE_LIST ) {
949 /* Only a remote entry, but both filters:
950 * Test the complete filter
952 rc = test_filter( op, rs->sr_entry, tc->orig );
953 if ( rc == LDAP_COMPARE_TRUE ) {
954 rc = SLAP_CB_CONTINUE;
959 /* Only a remote entry, only remote filter:
962 rc = SLAP_CB_CONTINUE;
969 /* Dup the filter, excluding invalid elements */
971 trans_filter_dup(Operation *op, Filter *f, AttributeName *an)
978 switch( f->f_choice & SLAPD_FILTER_MASK ) {
979 case SLAPD_FILTER_COMPUTED:
980 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
981 n->f_choice = f->f_choice;
982 n->f_result = f->f_result;
986 case LDAP_FILTER_PRESENT:
987 if ( ad_inlist( f->f_desc, an )) {
988 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
989 n->f_choice = f->f_choice;
990 n->f_desc = f->f_desc;
995 case LDAP_FILTER_EQUALITY:
998 case LDAP_FILTER_APPROX:
999 case LDAP_FILTER_SUBSTRINGS:
1000 case LDAP_FILTER_EXT:
1001 if ( !f->f_av_desc || ad_inlist( f->f_av_desc, an )) {
1002 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1003 n->f_choice = f->f_choice;
1004 n->f_ava = f->f_ava;
1009 case LDAP_FILTER_AND:
1010 case LDAP_FILTER_OR:
1011 case LDAP_FILTER_NOT: {
1014 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1015 n->f_choice = f->f_choice;
1018 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
1019 *p = trans_filter_dup( op, f, an );
1024 /* nothing valid in this list */
1026 op->o_tmpfree( n, op->o_tmpmemctx );
1029 /* Only 1 element in this list */
1030 if ((n->f_choice & SLAPD_FILTER_MASK) != LDAP_FILTER_NOT &&
1031 !n->f_list->f_next ) {
1034 op->o_tmpfree( f, op->o_tmpmemctx );
1043 trans_filter_free( Operation *op, Filter *f )
1045 Filter *n, *p, *next;
1047 f->f_choice &= SLAPD_FILTER_MASK;
1049 switch( f->f_choice ) {
1050 case LDAP_FILTER_AND:
1051 case LDAP_FILTER_OR:
1052 case LDAP_FILTER_NOT:
1053 /* Free in reverse order */
1055 for ( p = f->f_list; p; p = next ) {
1060 for ( p = n; p; p = next ) {
1062 trans_filter_free( op, p );
1068 op->o_tmpfree( f, op->o_tmpmemctx );
1072 ** translucent_search()
1073 ** search via captive backend;
1074 ** override results with any local data;
1078 static int translucent_search(Operation *op, SlapReply *rs) {
1079 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1080 translucent_info *ov = on->on_bi.bi_private;
1081 slap_callback cb = { NULL, NULL, NULL, NULL };
1087 Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
1088 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
1090 if(ov->defer_db_open) {
1091 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1092 "remote DB not available");
1096 fr = ov->remote ? trans_filter_dup( op, op->ors_filter, ov->remote ) : NULL;
1097 fl = ov->local ? trans_filter_dup( op, op->ors_filter, ov->local ) : NULL;
1098 cb.sc_response = (slap_response *) translucent_search_cb;
1099 cb.sc_private = &tc;
1100 cb.sc_next = op->o_callback;
1102 ov->db.be_acl = op->o_bd->be_acl;
1105 tc.orig = op->ors_filter;
1108 fbv = op->ors_filterstr;
1110 op->o_callback = &cb;
1114 tc.step |= RMT_SIDE;
1116 tc.step |= USE_LIST;
1117 op->ors_filter = fr;
1118 filter2bv_x( op, fr, &op->ors_filterstr );
1120 rc = ov->db.bd_info->bi_op_search(op, rs);
1123 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1127 tc.step |= LCL_SIDE;
1128 op->ors_filter = fl;
1129 filter2bv_x( op, fl, &op->ors_filterstr );
1130 rc = overlay_op_walk( op, rs, op_search, on->on_info, on->on_next );
1131 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1133 op->ors_filterstr = fbv;
1134 op->ors_filter = tc.orig;
1135 op->o_callback = cb.sc_next;
1136 /* Send out anything remaining on the list and finish */
1137 if ( tc.step & USE_LIST ) {
1141 av = tavl_end( tc.list, TAVL_DIR_LEFT );
1143 rs->sr_entry = av->avl_data;
1144 rc = test_filter( op, rs->sr_entry, op->ors_filter );
1145 if ( rc == LDAP_COMPARE_TRUE ) {
1146 rs->sr_flags = REP_ENTRY_MUSTBEFREED;
1147 rc = send_search_entry( op, rs );
1150 entry_free( rs->sr_entry );
1152 av = tavl_next( av, TAVL_DIR_RIGHT );
1154 tavl_free( tc.list, NULL );
1155 rs->sr_entry = NULL;
1157 send_ldap_result( op, rs );
1160 /* Free in reverse order */
1162 trans_filter_free( op, fl );
1164 trans_filter_free( op, fr );
1171 ** translucent_bind()
1172 ** pass bind request to captive backend;
1176 static int translucent_bind(Operation *op, SlapReply *rs) {
1177 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1178 translucent_info *ov = on->on_bi.bi_private;
1180 slap_callback sc = { 0 }, *save_cb;
1183 Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
1184 op->o_req_dn.bv_val, op->orb_method, 0);
1186 if(ov->defer_db_open) {
1187 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1188 "remote DB not available");
1192 if (ov->bind_local) {
1193 sc.sc_response = slap_null_cb;
1194 save_cb = op->o_callback;
1195 op->o_callback = ≻
1200 ov->db.be_acl = op->o_bd->be_acl;
1201 rc = ov->db.bd_info->bi_op_bind(op, rs);
1204 if (ov->bind_local) {
1205 op->o_callback = save_cb;
1206 if (rc != LDAP_SUCCESS) {
1207 rc = SLAP_CB_CONTINUE;
1215 ** translucent_connection_destroy()
1216 ** pass disconnect notification to captive backend;
1220 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
1221 slap_overinst *on = (slap_overinst *) be->bd_info;
1222 translucent_info *ov = on->on_bi.bi_private;
1225 Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
1227 rc = ov->db.bd_info->bi_connection_destroy(&ov->db, conn);
1233 ** translucent_db_config()
1234 ** pass config directives to captive backend;
1235 ** parse unrecognized directives ourselves;
1239 static int translucent_db_config(
1247 slap_overinst *on = (slap_overinst *) be->bd_info;
1248 translucent_info *ov = on->on_bi.bi_private;
1250 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_config: %s\n",
1251 argc ? argv[0] : "", 0, 0);
1253 /* Something for the captive database? */
1254 if ( ov->db.bd_info && ov->db.bd_info->bi_db_config )
1255 return ov->db.bd_info->bi_db_config( &ov->db, fname, lineno,
1257 return SLAP_CONF_UNKNOWN;
1261 ** translucent_db_init()
1262 ** initialize the captive backend;
1266 static int translucent_db_init(BackendDB *be, ConfigReply *cr) {
1267 slap_overinst *on = (slap_overinst *) be->bd_info;
1268 translucent_info *ov;
1270 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_init\n", 0, 0, 0);
1272 ov = ch_calloc(1, sizeof(translucent_info));
1273 on->on_bi.bi_private = ov;
1275 ov->db.be_private = NULL;
1276 ov->defer_db_open = 1;
1278 if ( !backend_db_init( "ldap", &ov->db, -1, NULL )) {
1279 Debug( LDAP_DEBUG_CONFIG, "translucent: unable to open captive back-ldap\n", 0, 0, 0);
1282 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1283 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
1289 ** translucent_db_open()
1290 ** if the captive backend has an open() method, call it;
1294 static int translucent_db_open(BackendDB *be, ConfigReply *cr) {
1295 slap_overinst *on = (slap_overinst *) be->bd_info;
1296 translucent_info *ov = on->on_bi.bi_private;
1299 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_open\n", 0, 0, 0);
1301 /* need to inherit something from the original database... */
1302 ov->db.be_def_limit = be->be_def_limit;
1303 ov->db.be_limits = be->be_limits;
1304 ov->db.be_acl = be->be_acl;
1305 ov->db.be_dfltaccess = be->be_dfltaccess;
1307 if ( ov->defer_db_open )
1310 rc = backend_startup_one( &ov->db, cr );
1312 if(rc) Debug(LDAP_DEBUG_TRACE,
1313 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
1319 ** translucent_db_close()
1320 ** if the captive backend has a close() method, call it
1325 translucent_db_close( BackendDB *be, ConfigReply *cr )
1327 slap_overinst *on = (slap_overinst *) be->bd_info;
1328 translucent_info *ov = on->on_bi.bi_private;
1331 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
1333 if ( ov && ov->db.bd_info && ov->db.bd_info->bi_db_close ) {
1334 rc = ov->db.bd_info->bi_db_close(&ov->db, NULL);
1341 ** translucent_db_destroy()
1342 ** if the captive backend has a db_destroy() method, call it;
1343 ** free any config data
1348 translucent_db_destroy( BackendDB *be, ConfigReply *cr )
1350 slap_overinst *on = (slap_overinst *) be->bd_info;
1351 translucent_info *ov = on->on_bi.bi_private;
1354 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_destroy\n", 0, 0, 0);
1358 anlist_free( ov->remote, 1, NULL );
1360 anlist_free( ov->local, 1, NULL );
1361 if ( ov->db.be_private != NULL ) {
1362 backend_stopdown_one( &ov->db );
1366 on->on_bi.bi_private = NULL;
1373 ** translucent_initialize()
1374 ** initialize the slap_overinst with our entry points;
1378 int translucent_initialize() {
1382 Debug(LDAP_DEBUG_TRACE, "==> translucent_initialize\n", 0, 0, 0);
1384 translucent.on_bi.bi_type = "translucent";
1385 translucent.on_bi.bi_db_init = translucent_db_init;
1386 translucent.on_bi.bi_db_config = translucent_db_config;
1387 translucent.on_bi.bi_db_open = translucent_db_open;
1388 translucent.on_bi.bi_db_close = translucent_db_close;
1389 translucent.on_bi.bi_db_destroy = translucent_db_destroy;
1390 translucent.on_bi.bi_op_bind = translucent_bind;
1391 translucent.on_bi.bi_op_add = translucent_add;
1392 translucent.on_bi.bi_op_modify = translucent_modify;
1393 translucent.on_bi.bi_op_modrdn = translucent_modrdn;
1394 translucent.on_bi.bi_op_delete = translucent_delete;
1395 translucent.on_bi.bi_op_search = translucent_search;
1396 translucent.on_bi.bi_op_compare = translucent_compare;
1397 translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
1398 translucent.on_bi.bi_extended = translucent_exop;
1400 translucent.on_bi.bi_cf_ocs = translucentocs;
1401 rc = config_register_schema ( translucentcfg, translucentocs );
1402 if ( rc ) return rc;
1404 return(overlay_register(&translucent));
1407 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
1408 int init_module(int argc, char *argv[]) {
1409 return translucent_initialize();
1413 #endif /* SLAPD_OVER_TRANSLUCENT */