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 };
66 char *odn = op->o_req_ndn.bv_val;
71 /* tis more work to use strchr() for a berval... */
72 for(idn = 0; odn[idn] && odn[idn] != ','; idn++);
73 if(!idn || !odn[idn]) return; /* because you never know */
75 ldn = dn.bv_len = op->o_req_ndn.bv_len - idn;
76 dn.bv_val = ch_malloc(ldn + 1);
77 strcpy(dn.bv_val, odn + idn);
79 Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", dn.bv_val, 0, 0);
81 e = ch_calloc(1, sizeof(Entry));
83 ber_dupbv(&e->e_name, &dn);
84 ber_dupbv(&e->e_nname, &dn);
86 a = ch_calloc(1, sizeof(Attribute));
87 a->a_desc = slap_schema.si_ad_objectClass;
88 a->a_vals = ch_malloc(sizeof(struct berval) * 3);
89 ber_dupbv(&a->a_vals[0], &glue[0]);
90 ber_dupbv(&a->a_vals[1], &glue[1]);
91 ber_dupbv(&a->a_vals[2], &glue[2]);
92 a->a_nvals = a->a_vals;
93 a->a_next = e->e_attrs;
96 a = ch_calloc(1, sizeof(Attribute));
97 a->a_desc = slap_schema.si_ad_structuralObjectClass;
98 a->a_vals = ch_malloc(sizeof(struct berval) * 2);
99 ber_dupbv(&a->a_vals[0], &glue[1]);
100 ber_dupbv(&a->a_vals[1], &glue[2]);
101 a->a_nvals = a->a_vals;
102 a->a_next = e->e_attrs;
108 nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
110 syncrepl_add_glue(&nop, e);
119 BerVarray dup_bervarray(BerVarray b) {
122 for(len = 0; b[len].bv_val; len++);
123 nb = ch_malloc((len+1) * sizeof(BerValue));
124 for(i = 0; i < len; i++) ber_dupbv(&nb[i], &b[i]);
125 nb[len].bv_val = NULL;
132 ** free only the Attribute*, not the contents;
135 void free_attr_chain(Attribute *a) {
137 for(ax = NULL; a; a = a->a_next) {
146 ** if not bound as root, send ACCESS error;
147 ** if config.glue, glue_parent();
152 static int translucent_add(Operation *op, SlapReply *rs) {
153 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
154 overlay_stack *ov = on->on_bi.bi_private;
155 Debug(LDAP_DEBUG_TRACE, "==> translucent_add: %s\n",
156 op->o_req_dn.bv_val, 0, 0);
158 op->o_bd->bd_info = (BackendInfo *) on->on_info;
159 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
160 "user modification of overlay database not permitted");
163 if(!ov->config->glue) glue_parent(op);
164 return(SLAP_CB_CONTINUE);
168 ** translucent_modrdn()
169 ** if not bound as root, send ACCESS error;
170 ** if !config.glue, glue_parent();
171 ** else return CONTINUE;
175 static int translucent_modrdn(Operation *op, SlapReply *rs) {
176 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
177 overlay_stack *ov = on->on_bi.bi_private;
178 Debug(LDAP_DEBUG_TRACE, "==> translucent_modrdn: %s -> %s\n",
179 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
181 op->o_bd->bd_info = (BackendInfo *) on->on_info;
182 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
183 "user modification of overlay database not permitted");
186 if(!ov->config->glue) glue_parent(op);
187 return(SLAP_CB_CONTINUE);
191 ** translucent_delete()
192 ** if not bound as root, send ACCESS error;
193 ** else return CONTINUE;
197 static int translucent_delete(Operation *op, SlapReply *rs) {
198 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
199 Debug(LDAP_DEBUG_TRACE, "==> translucent_delete: %s\n",
200 op->o_req_dn.bv_val, 0, 0);
202 op->o_bd->bd_info = (BackendInfo *) on->on_info;
203 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
204 "user modification of overlay database not permitted");
207 return(SLAP_CB_CONTINUE);
211 ** translucent_modify()
212 ** modify in local backend if exists in both;
213 ** otherwise, add to local backend;
214 ** fail if not defined in captive backend;
218 static int translucent_modify(Operation *op, SlapReply *rs) {
219 SlapReply nrs = { REP_RESULT };
222 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
223 overlay_stack *ov = on->on_bi.bi_private;
224 void *private = op->o_bd->be_private;
225 Entry ne, *e, *re = NULL;
227 Modifications *m, *mm;
228 int del, rc, erc = 0;
230 Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
231 op->o_req_dn.bv_val, 0, 0);
234 ** fetch entry from the captive backend;
235 ** if it did not exist, fail;
236 ** release it, if captive backend supports this;
240 op->o_bd->bd_info = (BackendInfo *) on->on_info;
241 op->o_bd->be_private = ov->private;
242 rc = ov->info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
243 op->o_bd->be_private = private;
245 /* if(ov->config->no_add && (!re || rc != LDAP_SUCCESS)) */
246 if(!re || rc != LDAP_SUCCESS) {
247 send_ldap_error(op, rs, LDAP_NO_SUCH_OBJECT,
248 "attempt to modify nonexistent local record");
253 ** fetch entry from local backend;
255 ** foreach Modification:
256 ** if attr not present in local:
257 ** if Mod == LDAP_MOD_DELETE:
258 ** if remote attr not present, return NO_SUCH;
259 ** if remote attr present, drop this Mod;
260 ** else force this Mod to LDAP_MOD_ADD;
265 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
267 if(e && rc == LDAP_SUCCESS) {
268 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
269 for(m = op->orm_modlist; m; m = m->sml_next) {
270 for(a = e->e_attrs; a; a = a->a_next)
271 if(a->a_desc == m->sml_desc) break;
272 if(a) continue; /* found local attr */
273 if(m->sml_op == LDAP_MOD_DELETE) {
274 for(a = re->e_attrs; a; a = a->a_next)
275 if(a->a_desc == m->sml_desc) break;
276 /* not found remote attr */
278 erc = LDAP_NO_SUCH_ATTRIBUTE;
281 if(ov->config->strict) {
282 erc = LDAP_CONSTRAINT_VIOLATION;
285 Debug(LDAP_DEBUG_TRACE,
286 "=> translucent_modify: silently dropping delete: %s\n",
287 m->sml_desc->ad_cname.bv_val, 0, 0);
288 for(mm = op->orm_modlist; mm->sml_next != m; mm = mm->sml_next);
289 mm->sml_next = m->sml_next;
292 mm->sml_next = NULL; /* hack */
293 slap_mods_free(mm, 1);
296 m->sml_op = LDAP_MOD_ADD;
298 erc = SLAP_CB_CONTINUE;
301 op->o_bd->be_private = ov->private;
302 if(ov->info->bi_entry_release_rw)
303 ov->info->bi_entry_release_rw(op, re, 0);
306 op->o_bd->be_private = private;
308 be_entry_release_r(op, e);
309 if(erc == SLAP_CB_CONTINUE) {
310 op->o_bd->bd_info = (BackendInfo *) on;
313 send_ldap_error(op, rs, erc,
314 "attempt to delete nonexistent attribute");
320 ** foreach Modification:
321 ** if MOD_ADD or MOD_REPLACE, add Attribute;
322 ** if no Modifications were suitable:
323 ** if config.strict, throw CONSTRAINT_VIOLATION;
324 ** else, return early SUCCESS;
325 ** fabricate Entry with new Attribute chain;
326 ** glue_parent() for this Entry;
327 ** call bi_op_add() in local backend;
331 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
333 for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
334 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
335 ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
336 Debug(LDAP_DEBUG_ANY,
337 "=> translucent_modify: silently dropped modification(%d): %s\n",
338 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
339 if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
342 a = ch_calloc(1, sizeof(Attribute));
343 a->a_desc = m->sml_desc;
344 a->a_vals = m->sml_values;
345 a->a_nvals = m->sml_nvalues;
350 if(del && ov->config->strict) {
352 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
353 "attempt to delete attributes from local database");
358 if(ov->config->strict) {
359 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
360 "modification contained other than ADD or REPLACE");
363 op->o_bd->bd_info = (BackendInfo *) on;
364 /* rs->sr_text = "no valid modification found"; */
365 rs->sr_err = LDAP_SUCCESS;
366 send_ldap_result(op, rs);
371 ne.e_name = op->o_req_dn;
372 ne.e_nname = op->o_req_ndn;
376 ne.e_bv.bv_val = NULL;
379 nop.o_tag = LDAP_REQ_ADD;
380 nop.oq_add.rs_e = ≠
382 op->o_bd->bd_info = (BackendInfo *) on;
385 rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
391 static int translucent_compare(Operation *op, SlapReply *rs) {
392 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
393 overlay_stack *ov = on->on_bi.bi_private;
394 void *private = op->o_bd->be_private;
396 AttributeAssertion *ava = op->orc_ava;
400 Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
401 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
404 ** if the local backend has an entry for this attribute:
405 ** CONTINUE and let it do the compare;
409 op->o_bd->bd_info = (BackendInfo *) on->on_info;
410 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e);
411 if(e && rc == LDAP_SUCCESS) {
412 be_entry_release_r(op, e);
413 op->o_bd->bd_info = (BackendInfo *) on;
414 return(SLAP_CB_CONTINUE);
418 ** call compare() in the captive backend;
419 ** return the result;
423 op->o_bd->be_private = ov->private;
424 rc = ov->info->bi_op_compare(op, rs);
425 op->o_bd->be_private = private;
426 op->o_bd->bd_info = (BackendInfo *) on;
431 ** translucent_search_cb()
432 ** merge local data with the search result
436 static int translucent_search_cb(Operation *op, SlapReply *rs) {
438 Entry *e, *re = NULL;
439 Attribute *a, *ax, *an, *as = NULL;
443 if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
444 return(SLAP_CB_CONTINUE);
446 Debug(LDAP_DEBUG_TRACE, "==> tranclucent_search_cb: %s\n",
447 rs->sr_entry->e_name.bv_val, 0, 0);
449 on = (slap_overinst *) op->o_bd->bd_info;
450 op->o_bd->bd_info = (BackendInfo *) on->on_info;
452 private = op->o_bd->be_private;
453 op->o_bd->be_private = op->o_callback->sc_private;
455 rc = be_entry_get_rw(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e);
458 ** if we got an entry from local backend:
459 ** make a copy of this search result;
460 ** foreach local attr:
461 ** foreach search result attr:
462 ** if match, result attr with local attr;
463 ** if new local, add to list;
464 ** append new local attrs to search result;
468 if(e && rc == LDAP_SUCCESS) {
469 re = entry_dup(rs->sr_entry);
470 for(ax = e->e_attrs; ax; ax = ax->a_next) {
472 if(is_at_operational(ax->a_desc->ad_type)) continue;
474 for(a = re->e_attrs; a; a = a->a_next) {
475 if(a->a_desc == ax->a_desc) {
476 if(a->a_vals != a->a_nvals)
477 ber_bvarray_free(a->a_nvals);
478 ber_bvarray_free(a->a_vals);
479 a->a_vals = dup_bervarray(ax->a_vals);
480 a->a_nvals = (ax->a_vals == ax->a_nvals) ?
481 a->a_vals : dup_bervarray(ax->a_nvals);
490 be_entry_release_r(op, e);
492 /* literally append, so locals are always last */
495 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
502 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
505 op->o_bd->be_private = private;
506 op->o_bd->bd_info = (BackendInfo *) on;
508 return(SLAP_CB_CONTINUE);
512 ** translucent_search()
513 ** search via captive backend;
514 ** override results with any local data;
518 static int translucent_search(Operation *op, SlapReply *rs) {
521 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
522 slap_callback cb = { NULL, NULL, NULL, NULL };
523 overlay_stack *ov = on->on_bi.bi_private;
524 void *private = op->o_bd->be_private;
527 Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
528 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
529 cb.sc_response = (slap_response *) translucent_search_cb;
530 cb.sc_private = private;
532 cb.sc_next = nop.o_callback;
533 nop.o_callback = &cb;
535 op->o_bd->be_private = ov->private;
536 rc = ov->info->bi_op_search(&nop, rs);
537 op->o_bd->be_private = private;
544 ** translucent_bind()
545 ** pass bind request to captive backend;
549 static int translucent_bind(Operation *op, SlapReply *rs) {
550 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
551 overlay_stack *ov = on->on_bi.bi_private;
552 void *private = op->o_bd->be_private;
555 Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
556 op->o_req_dn.bv_val, op->orb_method, 0);
558 op->o_bd->be_private = ov->private;
559 rc = ov->info->bi_op_bind(op, rs);
560 op->o_bd->be_private = private;
566 ** translucent_config()
567 ** pass config directives to captive backend;
568 ** parse unrecognized directives ourselves;
572 static int translucent_config(
580 slap_overinst *on = (slap_overinst *) be->bd_info;
581 overlay_stack *ov = on->on_bi.bi_private;
582 void *private = be->be_private;
583 void *be_cf_ocs = be->be_cf_ocs;
586 /* "this should never happen" */
588 fprintf(stderr, "fatal: captive backend not initialized");
592 be->be_private = ov->private;
593 be->be_cf_ocs = ov->info->bi_cf_ocs;
594 rc = ov->info->bi_db_config ? ov->info->bi_db_config(be, fname, lineno, argc, argv) : 0;
595 be->be_private = private;
596 be->be_cf_ocs = be_cf_ocs;
598 /* pass okay or error up, SLAP_CONF_UNKNOWN might be ours */
599 if(rc == 0 || rc == 1) return(rc);
602 if(!strcasecmp(*argv, "translucent_strict")) {
603 ov->config->strict++;
604 } else if(!strcasecmp(*argv, "translucent_no_add")) {
605 ov->config->no_add++;
606 } else if(!strcasecmp(*argv, "translucent_no_glue")) {
608 } else if(!strcasecmp(*argv, "translucent_debug")) {
610 ov->config->debug = 0xFFFF;
612 } else if(argc == 2) {
613 ov->config->debug = atoi(argv[1]);
616 fprintf(stderr, "%s: line %d: too many arguments (%d) to debug\n",
617 fname, lineno, argc);
621 fprintf(stderr, "%s: line %d: unknown keyword %s\n",
622 fname, lineno, *argv);
623 rc = SLAP_CONF_UNKNOWN;
629 ** translucent_db_init()
630 ** initialize the captive backend;
634 static int translucent_db_init(BackendDB *be) {
635 slap_overinst *on = (slap_overinst *) be->bd_info;
636 void *private = be->be_private;
640 Debug(LDAP_DEBUG_TRACE, "==> translucent_init\n", 0, 0, 0);
642 ov = ch_calloc(1, sizeof(overlay_stack));
643 ov->config = ch_calloc(1, sizeof(translucent_configuration));
644 ov->info = backend_info("ldap");
647 Debug(LDAP_DEBUG_ANY, "translucent: backend_info failed!\n", 0, 0, 0);
651 /* forcibly disable schema checking on the local backend */
652 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
654 be->be_private = NULL;
655 rc = ov->info->bi_db_init ? ov->info->bi_db_init(be) : 0;
657 if(rc) Debug(LDAP_DEBUG_TRACE,
658 "translucent: bi_db_init() returned error %d\n", rc, 0, 0);
660 ov->private = be->be_private;
661 be->be_private = private;
662 on->on_bi.bi_private = ov;
667 ** translucent_open()
668 ** if the captive backend has an open() method, call it;
672 static int translucent_open(BackendDB *be) {
673 slap_overinst *on = (slap_overinst *) be->bd_info;
674 overlay_stack *ov = on->on_bi.bi_private;
675 void *private = be->be_private;
678 /* "should never happen" */
680 Debug(LDAP_DEBUG_ANY, "translucent_open() called with bad ov->info\n", 0, 0, 0);
684 Debug(LDAP_DEBUG_TRACE, "translucent_open\n", 0, 0, 0);
686 be->be_private = ov->private;
687 rc = ov->info->bi_db_open ? ov->info->bi_db_open(be) : 0;
688 be->be_private = private;
690 if(rc) Debug(LDAP_DEBUG_TRACE,
691 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
697 ** translucent_close()
698 ** if the captive backend has a close() method, call it;
699 ** free any config data;
703 static int translucent_close(BackendDB *be) {
704 slap_overinst *on = (slap_overinst *) be->bd_info;
705 overlay_stack *ov = on->on_bi.bi_private;
706 void *private = be->be_private;
709 be->be_private = ov->private;
710 rc = (ov->info && ov->info->bi_db_close) ? ov->info->bi_db_close(be) : 0;
711 be->be_private = private;
712 if(ov->config) ch_free(ov->config);
718 ** translucent_init()
719 ** initialize the slap_overinst with our entry points;
723 int translucent_init() {
725 translucent.on_bi.bi_type = "translucent";
726 translucent.on_bi.bi_db_init = translucent_db_init;
727 translucent.on_bi.bi_db_config = translucent_config;
728 translucent.on_bi.bi_db_open = translucent_open;
729 translucent.on_bi.bi_db_close = translucent_close;
730 translucent.on_bi.bi_op_bind = translucent_bind;
731 translucent.on_bi.bi_op_add = translucent_add;
732 translucent.on_bi.bi_op_modify = translucent_modify;
733 translucent.on_bi.bi_op_modrdn = translucent_modrdn;
734 translucent.on_bi.bi_op_delete = translucent_delete;
735 translucent.on_bi.bi_op_search = translucent_search;
736 translucent.on_bi.bi_op_compare = translucent_compare;
738 return(overlay_register(&translucent));
741 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
742 int init_module(int argc, char *argv[]) {
743 return translucent_init();
747 #endif /* SLAPD_OVER_TRANSLUCENT */