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 ConfigTable transdummy[] = {
99 { "", "", 0, 0, 0, ARG_IGNORED,
100 NULL, "( OLcfgGlAt:13 NAME 'olcDatabase' "
101 "DESC 'The backend type for a database instance' "
102 "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
103 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
106 static ConfigOCs translucentocs[] = {
107 { "( OLcfgOvOc:14.1 "
108 "NAME 'olcTranslucentConfig' "
109 "DESC 'Translucent configuration' "
110 "SUP olcOverlayConfig "
111 "MAY ( olcTranslucentStrict $ olcTranslucentNoGlue $"
112 " olcTranslucentLocal $ olcTranslucentRemote $"
113 " olcTranslucentBindLocal $ olcTranslucentPwModLocal ) )",
114 Cft_Overlay, translucentcfg, NULL, translucent_cfadd },
115 { "( OLcfgOvOc:14.2 "
116 "NAME 'olcTranslucentDatabase' "
117 "DESC 'Translucent target database configuration' "
118 "AUXILIARY )", Cft_Misc, transdummy, translucent_ldadd },
121 /* for translucent_init() */
124 translucent_ldadd_cleanup( ConfigArgs *ca )
126 slap_overinst *on = ca->ca_private;
127 translucent_info *ov = on->on_bi.bi_private;
129 ov->defer_db_open = 0;
130 return backend_startup_one( ca->be, &ca->reply );
134 translucent_ldadd( CfEntryInfo *cei, Entry *e, ConfigArgs *ca )
137 translucent_info *ov;
139 Debug(LDAP_DEBUG_TRACE, "==> translucent_ldadd\n", 0, 0, 0);
141 if ( cei->ce_type != Cft_Overlay || !cei->ce_bi ||
142 cei->ce_bi->bi_cf_ocs != translucentocs )
143 return LDAP_CONSTRAINT_VIOLATION;
145 on = (slap_overinst *)cei->ce_bi;
146 ov = on->on_bi.bi_private;
149 if ( CONFIG_ONLINE_ADD( ca ))
150 ca->cleanup = translucent_ldadd_cleanup;
152 ov->defer_db_open = 0;
158 translucent_cfadd( Operation *op, SlapReply *rs, Entry *e, ConfigArgs *ca )
160 CfEntryInfo *cei = e->e_private;
161 slap_overinst *on = (slap_overinst *)cei->ce_bi;
162 translucent_info *ov = on->on_bi.bi_private;
165 Debug(LDAP_DEBUG_TRACE, "==> translucent_cfadd\n", 0, 0, 0);
167 /* FIXME: should not hardcode "olcDatabase" here */
168 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
169 "olcDatabase=%s", ov->db.bd_info->bi_type );
170 if ( bv.bv_len < 0 || bv.bv_len >= sizeof( ca->cr_msg ) ) {
173 bv.bv_val = ca->cr_msg;
175 ov->defer_db_open = 0;
177 /* We can only create this entry if the database is table-driven
179 if ( ov->db.bd_info->bi_cf_ocs )
180 config_build_entry( op, rs, cei, ca, &bv,
181 ov->db.bd_info->bi_cf_ocs,
182 &translucentocs[1] );
188 translucent_cf_gen( ConfigArgs *c )
190 slap_overinst *on = (slap_overinst *)c->bi;
191 translucent_info *ov = on->on_bi.bi_private;
192 AttributeName **an, *a2;
195 if ( c->type == TRANS_LOCAL )
200 if ( c->op == SLAP_CONFIG_EMIT ) {
203 for ( i = 0; !BER_BVISNULL(&(*an)[i].an_name); i++ ) {
204 value_add_one( &c->rvalue_vals, &(*an)[i].an_name );
207 } else if ( c->op == LDAP_MOD_DELETE ) {
209 anlist_free( *an, 1, NULL );
213 ch_free( (*an)[i].an_name.bv_val );
215 (*an)[i] = (*an)[i+1];
216 } while ( !BER_BVISNULL( &(*an)[i].an_name ));
220 a2 = str2anlist( *an, c->argv[1], "," );
222 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s unable to parse attribute %s",
223 c->argv[0], c->argv[1] );
224 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
225 "%s: %s\n", c->log, c->cr_msg, 0 );
232 static slap_overinst translucent;
236 ** call syncrepl_add_glue() with the parent suffix;
240 static struct berval glue[] = { BER_BVC("top"), BER_BVC("glue"), BER_BVNULL };
242 void glue_parent(Operation *op) {
244 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
245 struct berval ndn = BER_BVNULL;
250 dnParent( &op->o_req_ndn, &pdn );
251 ber_dupbv_x( &ndn, &pdn, op->o_tmpmemctx );
253 Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", ndn.bv_val, 0, 0);
257 ber_dupbv(&e->e_name, &ndn);
258 ber_dupbv(&e->e_nname, &ndn);
260 a = attr_alloc( slap_schema.si_ad_objectClass );
262 a->a_vals = ch_malloc(sizeof(struct berval) * 3);
263 ber_dupbv(&a->a_vals[0], &glue[0]);
264 ber_dupbv(&a->a_vals[1], &glue[1]);
265 ber_dupbv(&a->a_vals[2], &glue[2]);
266 a->a_nvals = a->a_vals;
267 a->a_next = e->e_attrs;
270 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
272 a->a_vals = ch_malloc(sizeof(struct berval) * 2);
273 ber_dupbv(&a->a_vals[0], &glue[1]);
274 ber_dupbv(&a->a_vals[1], &glue[2]);
275 a->a_nvals = a->a_vals;
276 a->a_next = e->e_attrs;
283 nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
284 syncrepl_add_glue(&nop, e);
285 nop.o_bd->bd_info = (BackendInfo *) on;
287 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
297 BerVarray dup_bervarray(BerVarray b) {
300 for(len = 0; b[len].bv_val; len++);
301 nb = ch_malloc((len+1) * sizeof(BerValue));
302 for(i = 0; i < len; i++) ber_dupbv(&nb[i], &b[i]);
303 nb[len].bv_val = NULL;
310 ** free only the Attribute*, not the contents;
313 void free_attr_chain(Attribute *b) {
315 for(a=b; a; a=a->a_next) {
325 ** if not bound as root, send ACCESS error;
326 ** if glue, glue_parent();
331 static int translucent_add(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_add: %s\n",
335 op->o_req_dn.bv_val, 0, 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;
343 if(!ov->no_glue) glue_parent(op);
344 return(SLAP_CB_CONTINUE);
348 ** translucent_modrdn()
349 ** if not bound as root, send ACCESS error;
350 ** if !glue, glue_parent();
351 ** else return CONTINUE;
355 static int translucent_modrdn(Operation *op, SlapReply *rs) {
356 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
357 translucent_info *ov = on->on_bi.bi_private;
358 Debug(LDAP_DEBUG_TRACE, "==> translucent_modrdn: %s -> %s\n",
359 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
361 op->o_bd->bd_info = (BackendInfo *) on->on_info;
362 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
363 "user modification of overlay database not permitted");
364 op->o_bd->bd_info = (BackendInfo *) on;
368 op->o_tag = LDAP_REQ_ADD;
370 op->o_tag = LDAP_REQ_MODRDN;
372 return(SLAP_CB_CONTINUE);
376 ** translucent_delete()
377 ** if not bound as root, send ACCESS error;
378 ** else return CONTINUE;
382 static int translucent_delete(Operation *op, SlapReply *rs) {
383 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
384 Debug(LDAP_DEBUG_TRACE, "==> translucent_delete: %s\n",
385 op->o_req_dn.bv_val, 0, 0);
387 op->o_bd->bd_info = (BackendInfo *) on->on_info;
388 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
389 "user modification of overlay database not permitted");
390 op->o_bd->bd_info = (BackendInfo *) on;
393 return(SLAP_CB_CONTINUE);
397 translucent_tag_cb( Operation *op, SlapReply *rs )
399 op->o_tag = LDAP_REQ_MODIFY;
400 op->orm_modlist = op->o_callback->sc_private;
401 rs->sr_tag = slap_req2res( op->o_tag );
403 return SLAP_CB_CONTINUE;
407 ** translucent_modify()
408 ** modify in local backend if exists in both;
409 ** otherwise, add to local backend;
410 ** fail if not defined in captive backend;
414 static int translucent_modify(Operation *op, SlapReply *rs) {
415 SlapReply nrs = { REP_RESULT };
417 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
418 translucent_info *ov = on->on_bi.bi_private;
419 Entry *e = NULL, *re = NULL;
421 Modifications *m, **mm;
423 int del, rc, erc = 0;
424 slap_callback cb = { 0 };
426 Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
427 op->o_req_dn.bv_val, 0, 0);
429 if(ov->defer_db_open) {
430 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
431 "remote DB not available");
435 ** fetch entry from the captive backend;
436 ** if it did not exist, fail;
437 ** release it, if captive backend supports this;
443 rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
444 if(rc != LDAP_SUCCESS || re == NULL ) {
445 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
446 "attempt to modify nonexistent local record");
451 ** fetch entry from local backend;
453 ** foreach Modification:
454 ** if attr not present in local:
455 ** if Mod == LDAP_MOD_DELETE:
456 ** if remote attr not present, return NO_SUCH;
457 ** if remote attr present, drop this Mod;
458 ** else force this Mod to LDAP_MOD_ADD;
463 op->o_bd->bd_info = (BackendInfo *) on->on_info;
464 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
465 op->o_bd->bd_info = (BackendInfo *) on;
467 if(e && rc == LDAP_SUCCESS) {
468 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
469 for(mm = &op->orm_modlist; *mm; ) {
471 for(a = e->e_attrs; a; a = a->a_next)
472 if(a->a_desc == m->sml_desc) break;
475 continue; /* found local attr */
477 if(m->sml_op == LDAP_MOD_DELETE) {
478 for(a = re->e_attrs; a; a = a->a_next)
479 if(a->a_desc == m->sml_desc) break;
480 /* not found remote attr */
482 erc = LDAP_NO_SUCH_ATTRIBUTE;
486 erc = LDAP_CONSTRAINT_VIOLATION;
489 Debug(LDAP_DEBUG_TRACE,
490 "=> translucent_modify: silently dropping delete: %s\n",
491 m->sml_desc->ad_cname.bv_val, 0, 0);
494 slap_mods_free(m, 1);
497 m->sml_op = LDAP_MOD_ADD;
500 erc = SLAP_CB_CONTINUE;
503 if(ov->db.bd_info->bi_entry_release_rw) {
505 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
510 op->o_bd->bd_info = (BackendInfo *) on->on_info;
511 be_entry_release_r(op, e);
512 op->o_bd->bd_info = (BackendInfo *) on;
513 if(erc == SLAP_CB_CONTINUE) {
516 send_ldap_error(op, rs, erc,
517 "attempt to delete nonexistent attribute");
522 /* don't leak remote entry copy */
524 if(ov->db.bd_info->bi_entry_release_rw) {
526 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
532 ** foreach Modification:
533 ** if MOD_ADD or MOD_REPLACE, add Attribute;
534 ** if no Modifications were suitable:
535 ** if strict, throw CONSTRAINT_VIOLATION;
536 ** else, return early SUCCESS;
537 ** fabricate Entry with new Attribute chain;
538 ** glue_parent() for this Entry;
539 ** call bi_op_add() in local backend;
543 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
545 for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
547 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
548 ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
549 Debug(LDAP_DEBUG_ANY,
550 "=> translucent_modify: silently dropped modification(%d): %s\n",
551 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
552 if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
555 atmp.a_desc = m->sml_desc;
556 atmp.a_vals = m->sml_values;
557 atmp.a_nvals = m->sml_nvalues ? m->sml_nvalues : atmp.a_vals;
558 atmp.a_numvals = m->sml_numvals;
560 a = attr_dup( &atmp );
565 if(del && ov->strict) {
567 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
568 "attempt to delete attributes from local database");
574 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
575 "modification contained other than ADD or REPLACE");
578 /* rs->sr_text = "no valid modification found"; */
579 rs->sr_err = LDAP_SUCCESS;
580 send_ldap_result(op, rs);
585 ber_dupbv( &e->e_name, &op->o_req_dn );
586 ber_dupbv( &e->e_nname, &op->o_req_ndn );
589 op->o_tag = LDAP_REQ_ADD;
590 cb.sc_response = translucent_tag_cb;
591 cb.sc_private = op->orm_modlist;
596 cb.sc_next = op->o_callback;
597 op->o_callback = &cb;
598 rc = on->on_info->oi_orig->bi_op_add(op, &nrs);
599 if ( op->ora_e == e )
601 op->o_callback = cb.sc_next;
606 static int translucent_compare(Operation *op, SlapReply *rs) {
607 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
608 translucent_info *ov = on->on_bi.bi_private;
609 AttributeAssertion *ava = op->orc_ava;
614 Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
615 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
618 ** if the local backend has an entry for this attribute:
619 ** CONTINUE and let it do the compare;
622 rc = overlay_entry_get_ov(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e, on);
623 if(rc == LDAP_SUCCESS && e) {
624 overlay_entry_release_ov(op, e, 0, on);
625 return(SLAP_CB_CONTINUE);
628 if(ov->defer_db_open) {
629 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
630 "remote DB not available");
634 ** call compare() in the captive backend;
635 ** return the result;
640 rc = ov->db.bd_info->bi_op_compare(op, rs);
646 static int translucent_pwmod(Operation *op, SlapReply *rs) {
647 SlapReply nrs = { REP_RESULT };
650 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
651 translucent_info *ov = on->on_bi.bi_private;
652 const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
653 Entry *e = NULL, *re = NULL;
656 slap_callback cb = { 0 };
658 if (!ov->pwmod_local) {
659 rs->sr_err = LDAP_CONSTRAINT_VIOLATION,
660 rs->sr_text = "attempt to modify password in local database";
665 ** fetch entry from the captive backend;
666 ** if it did not exist, fail;
667 ** release it, if captive backend supports this;
672 rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
673 if(rc != LDAP_SUCCESS || re == NULL ) {
674 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
675 "attempt to modify nonexistent local record");
680 ** fetch entry from local backend;
685 op->o_bd->bd_info = (BackendInfo *) on->on_info;
686 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
687 op->o_bd->bd_info = (BackendInfo *) on;
689 if(e && rc == LDAP_SUCCESS) {
691 if(ov->db.bd_info->bi_entry_release_rw) {
693 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
699 op->o_bd->bd_info = (BackendInfo *) on->on_info;
700 be_entry_release_r(op, e);
701 op->o_bd->bd_info = (BackendInfo *) on;
702 return SLAP_CB_CONTINUE;
705 /* don't leak remote entry copy */
707 if(ov->db.bd_info->bi_entry_release_rw) {
709 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
716 ** glue_parent() for this Entry;
717 ** call bi_op_add() in local backend;
721 ber_dupbv( &e->e_name, &op->o_req_dn );
722 ber_dupbv( &e->e_nname, &op->o_req_ndn );
726 nop.o_tag = LDAP_REQ_ADD;
727 cb.sc_response = slap_null_cb;
732 nop.o_callback = &cb;
733 rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
734 if ( nop.ora_e == e ) {
738 if ( rc == LDAP_SUCCESS ) {
739 return SLAP_CB_CONTINUE;
745 static int translucent_exop(Operation *op, SlapReply *rs) {
746 SlapReply nrs = { REP_RESULT };
748 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
749 translucent_info *ov = on->on_bi.bi_private;
750 const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
752 Debug(LDAP_DEBUG_TRACE, "==> translucent_exop: %s\n",
753 op->o_req_dn.bv_val, 0, 0);
755 if(ov->defer_db_open) {
756 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
757 "remote DB not available");
761 if ( bvmatch( &bv_exop_pwmod, &op->ore_reqoid ) ) {
762 return translucent_pwmod( op, rs );
765 return SLAP_CB_CONTINUE;
769 ** translucent_search_cb()
770 ** merge local data with remote data
773 ** 1: remote search, no local filter
774 ** merge data and send immediately
775 ** 2: remote search, with local filter
776 ** merge data and save
777 ** 3: local search, no remote filter
778 ** merge data and send immediately
779 ** 4: local search, with remote filter
780 ** check list, merge, send, delete
787 typedef struct trans_ctx {
795 static int translucent_search_cb(Operation *op, SlapReply *rs) {
799 translucent_info *ov;
801 Attribute *a, *ax, *an, *as = NULL;
805 tc = op->o_callback->sc_private;
807 /* Don't let the op complete while we're gathering data */
808 if ( rs->sr_type == REP_RESULT && ( tc->step & USE_LIST ))
811 if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
812 return(SLAP_CB_CONTINUE);
814 Debug(LDAP_DEBUG_TRACE, "==> translucent_search_cb: %s\n",
815 rs->sr_entry->e_name.bv_val, 0, 0);
818 ov = on->on_bi.bi_private;
823 /* If we have local, get remote */
824 if ( tc->step & LCL_SIDE ) {
826 /* If entry is already on list, use it */
827 if ( tc->step & USE_LIST ) {
828 re = tavl_delete( &tc->list, le, entry_dn_cmp );
830 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
831 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
832 be_entry_release_r( op, rs->sr_entry );
834 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
835 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
836 entry_free( rs->sr_entry );
838 rc = test_filter( op, re, tc->orig );
839 if ( rc == LDAP_COMPARE_TRUE ) {
840 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
842 return SLAP_CB_CONTINUE;
851 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &re );
852 if ( rc == LDAP_SUCCESS && re ) {
853 Entry *tmp = entry_dup( re );
854 be_entry_release_r( op, re );
859 /* Else we have remote, get local */
861 rc = overlay_entry_get_ov(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &le, on);
862 if ( rc == LDAP_SUCCESS && le ) {
863 re = entry_dup( rs->sr_entry );
864 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
865 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
866 be_entry_release_r( op, rs->sr_entry );
868 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
869 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
870 entry_free( rs->sr_entry );
878 ** if we got remote and local entry:
879 ** foreach local attr:
880 ** foreach remote attr:
881 ** if match, remote attr with local attr;
882 ** if new local, add to list;
883 ** append new local attrs to remote;
888 for(ax = le->e_attrs; ax; ax = ax->a_next) {
889 for(a = re->e_attrs; a; a = a->a_next) {
890 if(a->a_desc == ax->a_desc) {
891 if(a->a_vals != a->a_nvals)
892 ber_bvarray_free(a->a_nvals);
893 ber_bvarray_free(a->a_vals);
894 a->a_vals = dup_bervarray(ax->a_vals);
895 a->a_nvals = (ax->a_vals == ax->a_nvals) ?
896 a->a_vals : dup_bervarray(ax->a_nvals);
905 /* Dispose of local entry */
906 if ( tc->step & LCL_SIDE ) {
907 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
908 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
909 be_entry_release_r( op, rs->sr_entry );
911 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
912 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
913 entry_free( rs->sr_entry );
916 overlay_entry_release_ov(op, le, 0, on);
919 /* literally append, so locals are always last */
922 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
928 /* If both filters, save entry for later */
929 if ( tc->step == (USE_LIST|RMT_SIDE) ) {
930 tavl_insert( &tc->list, re, entry_dn_cmp, avl_dup_error );
936 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
938 rc = test_filter( op, rs->sr_entry, tc->orig );
939 if ( rc == LDAP_COMPARE_TRUE ) {
940 rc = SLAP_CB_CONTINUE;
945 rc = SLAP_CB_CONTINUE;
949 /* Only a local entry: remote was deleted
950 * Ought to delete the local too...
953 } else if ( tc->step & USE_LIST ) {
954 /* Only a remote entry, but both filters:
955 * Test the complete filter
957 rc = test_filter( op, rs->sr_entry, tc->orig );
958 if ( rc == LDAP_COMPARE_TRUE ) {
959 rc = SLAP_CB_CONTINUE;
964 /* Only a remote entry, only remote filter:
967 rc = SLAP_CB_CONTINUE;
974 /* Dup the filter, excluding invalid elements */
976 trans_filter_dup(Operation *op, Filter *f, AttributeName *an)
983 switch( f->f_choice & SLAPD_FILTER_MASK ) {
984 case SLAPD_FILTER_COMPUTED:
985 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
986 n->f_choice = f->f_choice;
987 n->f_result = f->f_result;
991 case LDAP_FILTER_PRESENT:
992 if ( ad_inlist( f->f_desc, an )) {
993 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
994 n->f_choice = f->f_choice;
995 n->f_desc = f->f_desc;
1000 case LDAP_FILTER_EQUALITY:
1001 case LDAP_FILTER_GE:
1002 case LDAP_FILTER_LE:
1003 case LDAP_FILTER_APPROX:
1004 case LDAP_FILTER_SUBSTRINGS:
1005 case LDAP_FILTER_EXT:
1006 if ( !f->f_av_desc || ad_inlist( f->f_av_desc, an )) {
1007 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1008 n->f_choice = f->f_choice;
1009 n->f_ava = f->f_ava;
1014 case LDAP_FILTER_AND:
1015 case LDAP_FILTER_OR:
1016 case LDAP_FILTER_NOT: {
1019 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1020 n->f_choice = f->f_choice;
1023 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
1024 *p = trans_filter_dup( op, f, an );
1029 /* nothing valid in this list */
1031 op->o_tmpfree( n, op->o_tmpmemctx );
1034 /* Only 1 element in this list */
1035 if ((n->f_choice & SLAPD_FILTER_MASK) != LDAP_FILTER_NOT &&
1036 !n->f_list->f_next ) {
1039 op->o_tmpfree( f, op->o_tmpmemctx );
1048 trans_filter_free( Operation *op, Filter *f )
1050 Filter *n, *p, *next;
1052 f->f_choice &= SLAPD_FILTER_MASK;
1054 switch( f->f_choice ) {
1055 case LDAP_FILTER_AND:
1056 case LDAP_FILTER_OR:
1057 case LDAP_FILTER_NOT:
1058 /* Free in reverse order */
1060 for ( p = f->f_list; p; p = next ) {
1065 for ( p = n; p; p = next ) {
1067 trans_filter_free( op, p );
1073 op->o_tmpfree( f, op->o_tmpmemctx );
1077 ** translucent_search()
1078 ** search via captive backend;
1079 ** override results with any local data;
1083 static int translucent_search(Operation *op, SlapReply *rs) {
1084 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1085 translucent_info *ov = on->on_bi.bi_private;
1086 slap_callback cb = { NULL, NULL, NULL, NULL };
1092 Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
1093 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
1095 if(ov->defer_db_open) {
1096 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1097 "remote DB not available");
1101 fr = ov->remote ? trans_filter_dup( op, op->ors_filter, ov->remote ) : NULL;
1102 fl = ov->local ? trans_filter_dup( op, op->ors_filter, ov->local ) : NULL;
1103 cb.sc_response = (slap_response *) translucent_search_cb;
1104 cb.sc_private = &tc;
1105 cb.sc_next = op->o_callback;
1109 tc.orig = op->ors_filter;
1112 fbv = op->ors_filterstr;
1114 op->o_callback = &cb;
1118 tc.step |= RMT_SIDE;
1120 tc.step |= USE_LIST;
1121 op->ors_filter = fr;
1122 filter2bv_x( op, fr, &op->ors_filterstr );
1124 rc = ov->db.bd_info->bi_op_search(op, rs);
1127 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1131 tc.step |= LCL_SIDE;
1132 op->ors_filter = fl;
1133 filter2bv_x( op, fl, &op->ors_filterstr );
1134 rc = overlay_op_walk( op, rs, op_search, on->on_info, on->on_next );
1135 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1137 op->ors_filterstr = fbv;
1138 op->ors_filter = tc.orig;
1139 op->o_callback = cb.sc_next;
1140 /* Send out anything remaining on the list and finish */
1141 if ( tc.step & USE_LIST ) {
1145 av = tavl_end( tc.list, TAVL_DIR_LEFT );
1147 rs->sr_entry = av->avl_data;
1148 rc = test_filter( op, rs->sr_entry, op->ors_filter );
1149 if ( rc == LDAP_COMPARE_TRUE ) {
1150 rs->sr_flags = REP_ENTRY_MUSTBEFREED;
1151 rc = send_search_entry( op, rs );
1154 entry_free( rs->sr_entry );
1156 av = tavl_next( av, TAVL_DIR_RIGHT );
1158 tavl_free( tc.list, NULL );
1159 rs->sr_entry = NULL;
1161 send_ldap_result( op, rs );
1164 /* Free in reverse order */
1166 trans_filter_free( op, fl );
1168 trans_filter_free( op, fr );
1175 ** translucent_bind()
1176 ** pass bind request to captive backend;
1180 static int translucent_bind(Operation *op, SlapReply *rs) {
1181 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1182 translucent_info *ov = on->on_bi.bi_private;
1184 slap_callback sc = { 0 }, *save_cb;
1187 Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
1188 op->o_req_dn.bv_val, op->orb_method, 0);
1190 if(ov->defer_db_open) {
1191 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1192 "remote DB not available");
1196 if (ov->bind_local) {
1197 sc.sc_response = slap_null_cb;
1198 save_cb = op->o_callback;
1199 op->o_callback = ≻
1204 rc = ov->db.bd_info->bi_op_bind(op, rs);
1207 if (ov->bind_local) {
1208 op->o_callback = save_cb;
1209 if (rc != LDAP_SUCCESS) {
1210 rc = SLAP_CB_CONTINUE;
1218 ** translucent_connection_destroy()
1219 ** pass disconnect notification to captive backend;
1223 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
1224 slap_overinst *on = (slap_overinst *) be->bd_info;
1225 translucent_info *ov = on->on_bi.bi_private;
1228 Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
1230 rc = ov->db.bd_info->bi_connection_destroy(&ov->db, conn);
1236 ** translucent_db_config()
1237 ** pass config directives to captive backend;
1238 ** parse unrecognized directives ourselves;
1242 static int translucent_db_config(
1250 slap_overinst *on = (slap_overinst *) be->bd_info;
1251 translucent_info *ov = on->on_bi.bi_private;
1253 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_config: %s\n",
1254 argc ? argv[0] : "", 0, 0);
1256 /* Something for the captive database? */
1257 if ( ov->db.bd_info && ov->db.bd_info->bi_db_config )
1258 return ov->db.bd_info->bi_db_config( &ov->db, fname, lineno,
1260 return SLAP_CONF_UNKNOWN;
1264 ** translucent_db_init()
1265 ** initialize the captive backend;
1269 static int translucent_db_init(BackendDB *be, ConfigReply *cr) {
1270 slap_overinst *on = (slap_overinst *) be->bd_info;
1271 translucent_info *ov;
1273 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_init\n", 0, 0, 0);
1275 ov = ch_calloc(1, sizeof(translucent_info));
1276 on->on_bi.bi_private = ov;
1278 ov->db.be_private = NULL;
1279 ov->db.be_pcl_mutexp = &ov->db.be_pcl_mutex;
1280 ov->defer_db_open = 1;
1282 if ( !backend_db_init( "ldap", &ov->db, -1, NULL )) {
1283 Debug( LDAP_DEBUG_CONFIG, "translucent: unable to open captive back-ldap\n", 0, 0, 0);
1286 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1287 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
1293 ** translucent_db_open()
1294 ** if the captive backend has an open() method, call it;
1298 static int translucent_db_open(BackendDB *be, ConfigReply *cr) {
1299 slap_overinst *on = (slap_overinst *) be->bd_info;
1300 translucent_info *ov = on->on_bi.bi_private;
1303 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_open\n", 0, 0, 0);
1305 /* need to inherit something from the original database... */
1306 ov->db.be_def_limit = be->be_def_limit;
1307 ov->db.be_limits = be->be_limits;
1308 ov->db.be_acl = be->be_acl;
1309 ov->db.be_dfltaccess = be->be_dfltaccess;
1311 if ( ov->defer_db_open )
1314 rc = backend_startup_one( &ov->db, cr );
1316 if(rc) Debug(LDAP_DEBUG_TRACE,
1317 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
1323 ** translucent_db_close()
1324 ** if the captive backend has a close() method, call it
1329 translucent_db_close( BackendDB *be, ConfigReply *cr )
1331 slap_overinst *on = (slap_overinst *) be->bd_info;
1332 translucent_info *ov = on->on_bi.bi_private;
1335 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
1337 if ( ov && ov->db.bd_info && ov->db.bd_info->bi_db_close ) {
1338 rc = ov->db.bd_info->bi_db_close(&ov->db, NULL);
1345 ** translucent_db_destroy()
1346 ** if the captive backend has a db_destroy() method, call it;
1347 ** free any config data
1352 translucent_db_destroy( BackendDB *be, ConfigReply *cr )
1354 slap_overinst *on = (slap_overinst *) be->bd_info;
1355 translucent_info *ov = on->on_bi.bi_private;
1358 Debug(LDAP_DEBUG_TRACE, "==> translucent_db_destroy\n", 0, 0, 0);
1362 anlist_free( ov->remote, 1, NULL );
1364 anlist_free( ov->local, 1, NULL );
1365 if ( ov->db.be_private != NULL ) {
1366 backend_stopdown_one( &ov->db );
1370 on->on_bi.bi_private = NULL;
1377 ** translucent_initialize()
1378 ** initialize the slap_overinst with our entry points;
1382 int translucent_initialize() {
1386 Debug(LDAP_DEBUG_TRACE, "==> translucent_initialize\n", 0, 0, 0);
1388 translucent.on_bi.bi_type = "translucent";
1389 translucent.on_bi.bi_db_init = translucent_db_init;
1390 translucent.on_bi.bi_db_config = translucent_db_config;
1391 translucent.on_bi.bi_db_open = translucent_db_open;
1392 translucent.on_bi.bi_db_close = translucent_db_close;
1393 translucent.on_bi.bi_db_destroy = translucent_db_destroy;
1394 translucent.on_bi.bi_op_bind = translucent_bind;
1395 translucent.on_bi.bi_op_add = translucent_add;
1396 translucent.on_bi.bi_op_modify = translucent_modify;
1397 translucent.on_bi.bi_op_modrdn = translucent_modrdn;
1398 translucent.on_bi.bi_op_delete = translucent_delete;
1399 translucent.on_bi.bi_op_search = translucent_search;
1400 translucent.on_bi.bi_op_compare = translucent_compare;
1401 translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
1402 translucent.on_bi.bi_extended = translucent_exop;
1404 translucent.on_bi.bi_cf_ocs = translucentocs;
1405 rc = config_register_schema ( translucentcfg, translucentocs );
1406 if ( rc ) return rc;
1408 return(overlay_register(&translucent));
1411 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
1412 int init_module(int argc, char *argv[]) {
1413 return translucent_initialize();
1417 #endif /* SLAPD_OVER_TRANSLUCENT */