1 /* translucent.c - translucent proxy module */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2005 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>
35 typedef struct translucent_configuration {
40 } translucent_configuration;
42 /* stack of captive backends */
44 typedef struct overlay_stack {
45 BackendInfo *info; /* captive backend */
46 void *private; /* local backend_private */
47 translucent_configuration *config; /* our_private: configuration */
50 /* for translucent_init() */
52 static slap_overinst translucent;
56 ** call syncrepl_add_glue() with the parent suffix;
60 static struct berval glue[] = { BER_BVC("top"), BER_BVC("glue"), BER_BVNULL };
62 void glue_parent(Operation *op) {
64 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
65 struct berval dn = { 0, NULL };
70 dnParent( &op->o_req_ndn, &pdn );
71 ber_dupbv( &dn, &pdn );
73 Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", dn.bv_val, 0, 0);
75 e = ch_calloc(1, sizeof(Entry));
77 ber_dupbv(&e->e_name, &dn);
78 ber_dupbv(&e->e_nname, &dn);
80 a = ch_calloc(1, sizeof(Attribute));
81 a->a_desc = slap_schema.si_ad_objectClass;
82 a->a_vals = ch_malloc(sizeof(struct berval) * 3);
83 ber_dupbv(&a->a_vals[0], &glue[0]);
84 ber_dupbv(&a->a_vals[1], &glue[1]);
85 ber_dupbv(&a->a_vals[2], &glue[2]);
86 a->a_nvals = a->a_vals;
87 a->a_next = e->e_attrs;
90 a = ch_calloc(1, sizeof(Attribute));
91 a->a_desc = slap_schema.si_ad_structuralObjectClass;
92 a->a_vals = ch_malloc(sizeof(struct berval) * 2);
93 ber_dupbv(&a->a_vals[0], &glue[1]);
94 ber_dupbv(&a->a_vals[1], &glue[2]);
95 a->a_nvals = a->a_vals;
96 a->a_next = e->e_attrs;
102 nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
104 syncrepl_add_glue(&nop, e);
113 BerVarray dup_bervarray(BerVarray b) {
116 for(len = 0; b[len].bv_val; len++);
117 nb = ch_malloc((len+1) * sizeof(BerValue));
118 for(i = 0; i < len; i++) ber_dupbv(&nb[i], &b[i]);
119 nb[len].bv_val = NULL;
126 ** free only the Attribute*, not the contents;
129 void free_attr_chain(Attribute *a) {
131 for(ax = NULL; a; a = a->a_next) {
140 ** if not bound as root, send ACCESS error;
141 ** if config.glue, glue_parent();
146 static int translucent_add(Operation *op, SlapReply *rs) {
147 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
148 overlay_stack *ov = on->on_bi.bi_private;
149 Debug(LDAP_DEBUG_TRACE, "==> translucent_add: %s\n",
150 op->o_req_dn.bv_val, 0, 0);
152 op->o_bd->bd_info = (BackendInfo *) on->on_info;
153 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
154 "user modification of overlay database not permitted");
157 if(!ov->config->glue) glue_parent(op);
158 return(SLAP_CB_CONTINUE);
162 ** translucent_modrdn()
163 ** if not bound as root, send ACCESS error;
164 ** if !config.glue, glue_parent();
165 ** else return CONTINUE;
169 static int translucent_modrdn(Operation *op, SlapReply *rs) {
170 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
171 overlay_stack *ov = on->on_bi.bi_private;
172 Debug(LDAP_DEBUG_TRACE, "==> translucent_modrdn: %s -> %s\n",
173 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
175 op->o_bd->bd_info = (BackendInfo *) on->on_info;
176 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
177 "user modification of overlay database not permitted");
180 if(!ov->config->glue) glue_parent(op);
181 return(SLAP_CB_CONTINUE);
185 ** translucent_delete()
186 ** if not bound as root, send ACCESS error;
187 ** else return CONTINUE;
191 static int translucent_delete(Operation *op, SlapReply *rs) {
192 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
193 Debug(LDAP_DEBUG_TRACE, "==> translucent_delete: %s\n",
194 op->o_req_dn.bv_val, 0, 0);
196 op->o_bd->bd_info = (BackendInfo *) on->on_info;
197 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
198 "user modification of overlay database not permitted");
201 return(SLAP_CB_CONTINUE);
205 ** translucent_modify()
206 ** modify in local backend if exists in both;
207 ** otherwise, add to local backend;
208 ** fail if not defined in captive backend;
212 static int translucent_modify(Operation *op, SlapReply *rs) {
213 SlapReply nrs = { REP_RESULT };
216 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
217 overlay_stack *ov = on->on_bi.bi_private;
218 void *private = op->o_bd->be_private;
219 Entry ne, *e, *re = NULL;
221 Modifications *m, *mm;
222 int del, rc, erc = 0;
224 Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
225 op->o_req_dn.bv_val, 0, 0);
228 ** fetch entry from the captive backend;
229 ** if it did not exist, fail;
230 ** release it, if captive backend supports this;
234 op->o_bd->bd_info = (BackendInfo *) on->on_info;
235 op->o_bd->be_private = ov->private;
236 rc = ov->info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
237 op->o_bd->be_private = private;
239 /* if(ov->config->no_add && (!re || rc != LDAP_SUCCESS)) */
240 if(!re || rc != LDAP_SUCCESS) {
241 send_ldap_error(op, rs, LDAP_NO_SUCH_OBJECT,
242 "attempt to modify nonexistent local record");
247 ** fetch entry from local backend;
249 ** foreach Modification:
250 ** if attr not present in local:
251 ** if Mod == LDAP_MOD_DELETE:
252 ** if remote attr not present, return NO_SUCH;
253 ** if remote attr present, drop this Mod;
254 ** else force this Mod to LDAP_MOD_ADD;
259 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
261 if(e && rc == LDAP_SUCCESS) {
262 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
263 for(m = op->orm_modlist; m; m = m->sml_next) {
264 for(a = e->e_attrs; a; a = a->a_next)
265 if(a->a_desc == m->sml_desc) break;
266 if(a) continue; /* found local attr */
267 if(m->sml_op == LDAP_MOD_DELETE) {
268 for(a = re->e_attrs; a; a = a->a_next)
269 if(a->a_desc == m->sml_desc) break;
270 /* not found remote attr */
272 erc = LDAP_NO_SUCH_ATTRIBUTE;
275 if(ov->config->strict) {
276 erc = LDAP_CONSTRAINT_VIOLATION;
279 Debug(LDAP_DEBUG_TRACE,
280 "=> translucent_modify: silently dropping delete: %s\n",
281 m->sml_desc->ad_cname.bv_val, 0, 0);
282 for(mm = op->orm_modlist; mm->sml_next != m; mm = mm->sml_next);
283 mm->sml_next = m->sml_next;
286 mm->sml_next = NULL; /* hack */
287 slap_mods_free(mm, 1);
290 m->sml_op = LDAP_MOD_ADD;
292 erc = SLAP_CB_CONTINUE;
295 op->o_bd->be_private = ov->private;
296 if(ov->info->bi_entry_release_rw)
297 ov->info->bi_entry_release_rw(op, re, 0);
300 op->o_bd->be_private = private;
302 be_entry_release_r(op, e);
303 if(erc == SLAP_CB_CONTINUE) {
304 op->o_bd->bd_info = (BackendInfo *) on;
307 send_ldap_error(op, rs, erc,
308 "attempt to delete nonexistent attribute");
314 ** foreach Modification:
315 ** if MOD_ADD or MOD_REPLACE, add Attribute;
316 ** if no Modifications were suitable:
317 ** if config.strict, throw CONSTRAINT_VIOLATION;
318 ** else, return early SUCCESS;
319 ** fabricate Entry with new Attribute chain;
320 ** glue_parent() for this Entry;
321 ** call bi_op_add() in local backend;
325 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
327 for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
328 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
329 ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
330 Debug(LDAP_DEBUG_ANY,
331 "=> translucent_modify: silently dropped modification(%d): %s\n",
332 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
333 if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
336 a = ch_calloc(1, sizeof(Attribute));
337 a->a_desc = m->sml_desc;
338 a->a_vals = m->sml_values;
339 a->a_nvals = m->sml_nvalues;
344 if(del && ov->config->strict) {
346 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
347 "attempt to delete attributes from local database");
352 if(ov->config->strict) {
353 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
354 "modification contained other than ADD or REPLACE");
357 op->o_bd->bd_info = (BackendInfo *) on;
358 /* rs->sr_text = "no valid modification found"; */
359 rs->sr_err = LDAP_SUCCESS;
360 send_ldap_result(op, rs);
365 ne.e_name = op->o_req_dn;
366 ne.e_nname = op->o_req_ndn;
370 ne.e_bv.bv_val = NULL;
373 nop.o_tag = LDAP_REQ_ADD;
374 nop.oq_add.rs_e = ≠
376 op->o_bd->bd_info = (BackendInfo *) on;
379 rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
385 static int translucent_compare(Operation *op, SlapReply *rs) {
386 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
387 overlay_stack *ov = on->on_bi.bi_private;
388 void *private = op->o_bd->be_private;
390 AttributeAssertion *ava = op->orc_ava;
394 Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
395 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
398 ** if the local backend has an entry for this attribute:
399 ** CONTINUE and let it do the compare;
403 op->o_bd->bd_info = (BackendInfo *) on->on_info;
404 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e);
405 if(e && rc == LDAP_SUCCESS) {
406 be_entry_release_r(op, e);
407 op->o_bd->bd_info = (BackendInfo *) on;
408 return(SLAP_CB_CONTINUE);
412 ** call compare() in the captive backend;
413 ** return the result;
417 op->o_bd->be_private = ov->private;
418 rc = ov->info->bi_op_compare(op, rs);
419 op->o_bd->be_private = private;
420 op->o_bd->bd_info = (BackendInfo *) on;
425 ** translucent_search_cb()
426 ** merge local data with the search result
430 static int translucent_search_cb(Operation *op, SlapReply *rs) {
432 Entry *e, *re = NULL;
433 Attribute *a, *ax, *an, *as = NULL;
437 if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
438 return(SLAP_CB_CONTINUE);
440 Debug(LDAP_DEBUG_TRACE, "==> tranclucent_search_cb: %s\n",
441 rs->sr_entry->e_name.bv_val, 0, 0);
443 on = (slap_overinst *) op->o_bd->bd_info;
444 op->o_bd->bd_info = (BackendInfo *) on->on_info;
446 private = op->o_bd->be_private;
447 op->o_bd->be_private = op->o_callback->sc_private;
449 rc = be_entry_get_rw(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e);
452 ** if we got an entry from local backend:
453 ** make a copy of this search result;
454 ** foreach local attr:
455 ** foreach search result attr:
456 ** if match, result attr with local attr;
457 ** if new local, add to list;
458 ** append new local attrs to search result;
462 if(e && rc == LDAP_SUCCESS) {
463 re = entry_dup(rs->sr_entry);
464 for(ax = e->e_attrs; ax; ax = ax->a_next) {
466 if(is_at_operational(ax->a_desc->ad_type)) continue;
468 for(a = re->e_attrs; a; a = a->a_next) {
469 if(a->a_desc == ax->a_desc) {
470 if(a->a_vals != a->a_nvals)
471 ber_bvarray_free(a->a_nvals);
472 ber_bvarray_free(a->a_vals);
473 a->a_vals = dup_bervarray(ax->a_vals);
474 a->a_nvals = (ax->a_vals == ax->a_nvals) ?
475 a->a_vals : dup_bervarray(ax->a_nvals);
484 be_entry_release_r(op, e);
486 /* literally append, so locals are always last */
489 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
496 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
499 op->o_bd->be_private = private;
500 op->o_bd->bd_info = (BackendInfo *) on;
502 return(SLAP_CB_CONTINUE);
506 ** translucent_search()
507 ** search via captive backend;
508 ** override results with any local data;
512 static int translucent_search(Operation *op, SlapReply *rs) {
515 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
516 slap_callback cb = { NULL, NULL, NULL, NULL };
517 overlay_stack *ov = on->on_bi.bi_private;
518 void *private = op->o_bd->be_private;
521 Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
522 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
523 cb.sc_response = (slap_response *) translucent_search_cb;
524 cb.sc_private = private;
526 cb.sc_next = nop.o_callback;
527 nop.o_callback = &cb;
529 op->o_bd->be_private = ov->private;
530 rc = ov->info->bi_op_search(&nop, rs);
531 op->o_bd->be_private = private;
538 ** translucent_bind()
539 ** pass bind request to captive backend;
543 static int translucent_bind(Operation *op, SlapReply *rs) {
544 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
545 overlay_stack *ov = on->on_bi.bi_private;
546 void *private = op->o_bd->be_private;
549 Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
550 op->o_req_dn.bv_val, op->orb_method, 0);
552 op->o_bd->be_private = ov->private;
553 rc = ov->info->bi_op_bind(op, rs);
554 op->o_bd->be_private = private;
560 ** translucent_config()
561 ** pass config directives to captive backend;
562 ** parse unrecognized directives ourselves;
566 static int translucent_config(
574 slap_overinst *on = (slap_overinst *) be->bd_info;
575 overlay_stack *ov = on->on_bi.bi_private;
576 void *private = be->be_private;
577 void *be_cf_ocs = be->be_cf_ocs;
580 /* "this should never happen" */
582 fprintf(stderr, "fatal: captive backend not initialized");
586 be->be_private = ov->private;
587 be->be_cf_ocs = ov->info->bi_cf_ocs;
588 rc = ov->info->bi_db_config ? ov->info->bi_db_config(be, fname, lineno, argc, argv) : 0;
589 be->be_private = private;
590 be->be_cf_ocs = be_cf_ocs;
592 /* pass okay or error up, SLAP_CONF_UNKNOWN might be ours */
593 if(rc == 0 || rc == 1) return(rc);
596 if(!strcasecmp(*argv, "translucent_strict")) {
597 ov->config->strict++;
598 } else if(!strcasecmp(*argv, "translucent_no_add")) {
599 ov->config->no_add++;
600 } else if(!strcasecmp(*argv, "translucent_no_glue")) {
602 } else if(!strcasecmp(*argv, "translucent_debug")) {
604 ov->config->debug = 0xFFFF;
606 } else if(argc == 2) {
607 ov->config->debug = atoi(argv[1]);
610 fprintf(stderr, "%s: line %d: too many arguments (%d) to debug\n",
611 fname, lineno, argc);
615 fprintf(stderr, "%s: line %d: unknown keyword %s\n",
616 fname, lineno, *argv);
617 rc = SLAP_CONF_UNKNOWN;
623 ** translucent_db_init()
624 ** initialize the captive backend;
628 static int translucent_db_init(BackendDB *be) {
629 slap_overinst *on = (slap_overinst *) be->bd_info;
630 void *private = be->be_private;
634 Debug(LDAP_DEBUG_TRACE, "==> translucent_init\n", 0, 0, 0);
636 ov = ch_calloc(1, sizeof(overlay_stack));
637 ov->config = ch_calloc(1, sizeof(translucent_configuration));
638 ov->info = backend_info("ldap");
641 Debug(LDAP_DEBUG_ANY, "translucent: backend_info failed!\n", 0, 0, 0);
645 /* forcibly disable schema checking on the local backend */
646 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
648 be->be_private = NULL;
649 rc = ov->info->bi_db_init ? ov->info->bi_db_init(be) : 0;
651 if(rc) Debug(LDAP_DEBUG_TRACE,
652 "translucent: bi_db_init() returned error %d\n", rc, 0, 0);
654 ov->private = be->be_private;
655 be->be_private = private;
656 on->on_bi.bi_private = ov;
661 ** translucent_open()
662 ** if the captive backend has an open() method, call it;
666 static int translucent_open(BackendDB *be) {
667 slap_overinst *on = (slap_overinst *) be->bd_info;
668 overlay_stack *ov = on->on_bi.bi_private;
669 void *private = be->be_private;
672 /* "should never happen" */
674 Debug(LDAP_DEBUG_ANY, "translucent_open() called with bad ov->info\n", 0, 0, 0);
678 Debug(LDAP_DEBUG_TRACE, "translucent_open\n", 0, 0, 0);
680 be->be_private = ov->private;
681 rc = ov->info->bi_db_open ? ov->info->bi_db_open(be) : 0;
682 be->be_private = private;
684 if(rc) Debug(LDAP_DEBUG_TRACE,
685 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
691 ** translucent_close()
692 ** if the captive backend has a close() method, call it;
693 ** free any config data;
697 static int translucent_close(BackendDB *be) {
698 slap_overinst *on = (slap_overinst *) be->bd_info;
699 overlay_stack *ov = on->on_bi.bi_private;
700 void *private = be->be_private;
703 be->be_private = ov->private;
704 rc = (ov->info && ov->info->bi_db_close) ? ov->info->bi_db_close(be) : 0;
705 be->be_private = private;
706 if(ov->config) ch_free(ov->config);
712 ** translucent_init()
713 ** initialize the slap_overinst with our entry points;
717 int translucent_init() {
719 translucent.on_bi.bi_type = "translucent";
720 translucent.on_bi.bi_db_init = translucent_db_init;
721 translucent.on_bi.bi_db_config = translucent_config;
722 translucent.on_bi.bi_db_open = translucent_open;
723 translucent.on_bi.bi_db_close = translucent_close;
724 translucent.on_bi.bi_op_bind = translucent_bind;
725 translucent.on_bi.bi_op_add = translucent_add;
726 translucent.on_bi.bi_op_modify = translucent_modify;
727 translucent.on_bi.bi_op_modrdn = translucent_modrdn;
728 translucent.on_bi.bi_op_delete = translucent_delete;
729 translucent.on_bi.bi_op_search = translucent_search;
730 translucent.on_bi.bi_op_compare = translucent_compare;
732 return(overlay_register(&translucent));
735 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
736 int init_module(int argc, char *argv[]) {
737 return translucent_init();
741 #endif /* SLAPD_OVER_TRANSLUCENT */