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_tag_cb( Operation *op, SlapReply *rs )
207 op->o_tag = (ber_tag_t)op->o_callback->sc_private;
208 rs->sr_tag = slap_req2res( op->o_tag );
210 return SLAP_CB_CONTINUE;
214 ** translucent_modify()
215 ** modify in local backend if exists in both;
216 ** otherwise, add to local backend;
217 ** fail if not defined in captive backend;
221 static int translucent_modify(Operation *op, SlapReply *rs) {
222 SlapReply nrs = { REP_RESULT };
225 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
226 overlay_stack *ov = on->on_bi.bi_private;
227 void *private = op->o_bd->be_private;
228 Entry ne, *e, *re = NULL;
230 Modifications *m, *mm;
231 int del, rc, erc = 0;
232 slap_callback cb = { 0 }, *save_cb;
234 Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
235 op->o_req_dn.bv_val, 0, 0);
238 ** fetch entry from the captive backend;
239 ** if it did not exist, fail;
240 ** release it, if captive backend supports this;
244 op->o_bd->bd_info = (BackendInfo *) on->on_info;
245 op->o_bd->be_private = ov->private;
246 rc = ov->info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
247 op->o_bd->be_private = private;
249 /* if(ov->config->no_add && (!re || rc != LDAP_SUCCESS)) */
250 if(!re || rc != LDAP_SUCCESS) {
251 send_ldap_error(op, rs, LDAP_NO_SUCH_OBJECT,
252 "attempt to modify nonexistent local record");
257 ** fetch entry from local backend;
259 ** foreach Modification:
260 ** if attr not present in local:
261 ** if Mod == LDAP_MOD_DELETE:
262 ** if remote attr not present, return NO_SUCH;
263 ** if remote attr present, drop this Mod;
264 ** else force this Mod to LDAP_MOD_ADD;
269 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
271 if(e && rc == LDAP_SUCCESS) {
272 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
273 for(m = op->orm_modlist; m; m = m->sml_next) {
274 for(a = e->e_attrs; a; a = a->a_next)
275 if(a->a_desc == m->sml_desc) break;
276 if(a) continue; /* found local attr */
277 if(m->sml_op == LDAP_MOD_DELETE) {
278 for(a = re->e_attrs; a; a = a->a_next)
279 if(a->a_desc == m->sml_desc) break;
280 /* not found remote attr */
282 erc = LDAP_NO_SUCH_ATTRIBUTE;
285 if(ov->config->strict) {
286 erc = LDAP_CONSTRAINT_VIOLATION;
289 Debug(LDAP_DEBUG_TRACE,
290 "=> translucent_modify: silently dropping delete: %s\n",
291 m->sml_desc->ad_cname.bv_val, 0, 0);
292 for(mm = op->orm_modlist; mm->sml_next != m; mm = mm->sml_next);
293 mm->sml_next = m->sml_next;
296 mm->sml_next = NULL; /* hack */
297 slap_mods_free(mm, 1);
300 m->sml_op = LDAP_MOD_ADD;
302 erc = SLAP_CB_CONTINUE;
305 op->o_bd->be_private = ov->private;
306 if(ov->info->bi_entry_release_rw)
307 ov->info->bi_entry_release_rw(op, re, 0);
310 op->o_bd->be_private = private;
312 be_entry_release_r(op, e);
313 if(erc == SLAP_CB_CONTINUE) {
314 op->o_bd->bd_info = (BackendInfo *) on;
317 send_ldap_error(op, rs, erc,
318 "attempt to delete nonexistent attribute");
324 ** foreach Modification:
325 ** if MOD_ADD or MOD_REPLACE, add Attribute;
326 ** if no Modifications were suitable:
327 ** if config.strict, throw CONSTRAINT_VIOLATION;
328 ** else, return early SUCCESS;
329 ** fabricate Entry with new Attribute chain;
330 ** glue_parent() for this Entry;
331 ** call bi_op_add() in local backend;
335 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
337 for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
338 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
339 ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
340 Debug(LDAP_DEBUG_ANY,
341 "=> translucent_modify: silently dropped modification(%d): %s\n",
342 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
343 if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
346 a = ch_calloc(1, sizeof(Attribute));
347 a->a_desc = m->sml_desc;
348 a->a_vals = m->sml_values;
349 a->a_nvals = m->sml_nvalues;
354 if(del && ov->config->strict) {
356 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
357 "attempt to delete attributes from local database");
362 if(ov->config->strict) {
363 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
364 "modification contained other than ADD or REPLACE");
367 op->o_bd->bd_info = (BackendInfo *) on;
368 /* rs->sr_text = "no valid modification found"; */
369 rs->sr_err = LDAP_SUCCESS;
370 send_ldap_result(op, rs);
375 ne.e_name = op->o_req_dn;
376 ne.e_nname = op->o_req_ndn;
380 ne.e_bv.bv_val = NULL;
383 nop.o_tag = LDAP_REQ_ADD;
384 nop.oq_add.rs_e = ≠
386 op->o_bd->bd_info = (BackendInfo *) on;
389 save_cb = op->o_callback;
390 cb.sc_response = translucent_tag_cb;
391 cb.sc_private = (void *)LDAP_REQ_MODIFY;
392 cb.sc_next = nop.o_callback;
393 nop.o_callback = &cb;
394 rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
395 nop.o_callback = save_cb;
401 static int translucent_compare(Operation *op, SlapReply *rs) {
402 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
403 overlay_stack *ov = on->on_bi.bi_private;
404 void *private = op->o_bd->be_private;
406 AttributeAssertion *ava = op->orc_ava;
410 Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
411 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
414 ** if the local backend has an entry for this attribute:
415 ** CONTINUE and let it do the compare;
419 op->o_bd->bd_info = (BackendInfo *) on->on_info;
420 rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e);
421 if(e && rc == LDAP_SUCCESS) {
422 be_entry_release_r(op, e);
423 op->o_bd->bd_info = (BackendInfo *) on;
424 return(SLAP_CB_CONTINUE);
428 ** call compare() in the captive backend;
429 ** return the result;
433 op->o_bd->be_private = ov->private;
434 rc = ov->info->bi_op_compare(op, rs);
435 op->o_bd->be_private = private;
436 op->o_bd->bd_info = (BackendInfo *) on;
441 ** translucent_search_cb()
442 ** merge local data with the search result
446 static int translucent_search_cb(Operation *op, SlapReply *rs) {
448 Entry *e, *re = NULL;
449 Attribute *a, *ax, *an, *as = NULL;
453 if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
454 return(SLAP_CB_CONTINUE);
456 Debug(LDAP_DEBUG_TRACE, "==> tranclucent_search_cb: %s\n",
457 rs->sr_entry->e_name.bv_val, 0, 0);
459 on = (slap_overinst *) op->o_bd->bd_info;
460 op->o_bd->bd_info = (BackendInfo *) on->on_info;
462 private = op->o_bd->be_private;
463 op->o_bd->be_private = op->o_callback->sc_private;
465 rc = be_entry_get_rw(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e);
468 ** if we got an entry from local backend:
469 ** make a copy of this search result;
470 ** foreach local attr:
471 ** foreach search result attr:
472 ** if match, result attr with local attr;
473 ** if new local, add to list;
474 ** append new local attrs to search result;
478 if(e && rc == LDAP_SUCCESS) {
479 re = entry_dup(rs->sr_entry);
480 for(ax = e->e_attrs; ax; ax = ax->a_next) {
482 if(is_at_operational(ax->a_desc->ad_type)) continue;
484 for(a = re->e_attrs; a; a = a->a_next) {
485 if(a->a_desc == ax->a_desc) {
486 if(a->a_vals != a->a_nvals)
487 ber_bvarray_free(a->a_nvals);
488 ber_bvarray_free(a->a_vals);
489 a->a_vals = dup_bervarray(ax->a_vals);
490 a->a_nvals = (ax->a_vals == ax->a_nvals) ?
491 a->a_vals : dup_bervarray(ax->a_nvals);
500 be_entry_release_r(op, e);
502 /* literally append, so locals are always last */
505 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
512 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
515 op->o_bd->be_private = private;
516 op->o_bd->bd_info = (BackendInfo *) on;
518 return(SLAP_CB_CONTINUE);
522 ** translucent_search()
523 ** search via captive backend;
524 ** override results with any local data;
528 static int translucent_search(Operation *op, SlapReply *rs) {
531 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
532 slap_callback cb = { NULL, NULL, NULL, NULL };
533 overlay_stack *ov = on->on_bi.bi_private;
534 void *private = op->o_bd->be_private;
537 Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
538 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
539 cb.sc_response = (slap_response *) translucent_search_cb;
540 cb.sc_private = private;
542 cb.sc_next = nop.o_callback;
543 nop.o_callback = &cb;
545 op->o_bd->be_private = ov->private;
546 rc = ov->info->bi_op_search(&nop, rs);
547 op->o_bd->be_private = private;
554 ** translucent_bind()
555 ** pass bind request to captive backend;
559 static int translucent_bind(Operation *op, SlapReply *rs) {
560 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
561 overlay_stack *ov = on->on_bi.bi_private;
562 void *private = op->o_bd->be_private;
565 Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
566 op->o_req_dn.bv_val, op->orb_method, 0);
568 op->o_bd->be_private = ov->private;
569 rc = ov->info->bi_op_bind(op, rs);
570 op->o_bd->be_private = private;
576 ** translucent_config()
577 ** pass config directives to captive backend;
578 ** parse unrecognized directives ourselves;
582 static int translucent_config(
590 slap_overinst *on = (slap_overinst *) be->bd_info;
591 overlay_stack *ov = on->on_bi.bi_private;
592 void *private = be->be_private;
593 void *be_cf_ocs = be->be_cf_ocs;
596 /* "this should never happen" */
598 fprintf(stderr, "fatal: captive backend not initialized");
602 be->be_private = ov->private;
603 be->be_cf_ocs = ov->info->bi_cf_ocs;
604 rc = ov->info->bi_db_config ? ov->info->bi_db_config(be, fname, lineno, argc, argv) : 0;
605 be->be_private = private;
606 be->be_cf_ocs = be_cf_ocs;
608 /* pass okay or error up, SLAP_CONF_UNKNOWN might be ours */
609 if(rc == 0 || rc == 1) return(rc);
612 if(!strcasecmp(*argv, "translucent_strict")) {
613 ov->config->strict++;
614 } else if(!strcasecmp(*argv, "translucent_no_add")) {
615 ov->config->no_add++;
616 } else if(!strcasecmp(*argv, "translucent_no_glue")) {
618 } else if(!strcasecmp(*argv, "translucent_debug")) {
620 ov->config->debug = 0xFFFF;
622 } else if(argc == 2) {
623 ov->config->debug = atoi(argv[1]);
626 fprintf(stderr, "%s: line %d: too many arguments (%d) to debug\n",
627 fname, lineno, argc);
631 fprintf(stderr, "%s: line %d: unknown keyword %s\n",
632 fname, lineno, *argv);
633 rc = SLAP_CONF_UNKNOWN;
639 ** translucent_db_init()
640 ** initialize the captive backend;
644 static int translucent_db_init(BackendDB *be) {
645 slap_overinst *on = (slap_overinst *) be->bd_info;
646 void *private = be->be_private;
650 Debug(LDAP_DEBUG_TRACE, "==> translucent_init\n", 0, 0, 0);
652 ov = ch_calloc(1, sizeof(overlay_stack));
653 ov->config = ch_calloc(1, sizeof(translucent_configuration));
654 ov->info = backend_info("ldap");
657 Debug(LDAP_DEBUG_ANY, "translucent: backend_info failed!\n", 0, 0, 0);
661 /* forcibly disable schema checking on the local backend */
662 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
664 be->be_private = NULL;
665 rc = ov->info->bi_db_init ? ov->info->bi_db_init(be) : 0;
667 if(rc) Debug(LDAP_DEBUG_TRACE,
668 "translucent: bi_db_init() returned error %d\n", rc, 0, 0);
670 ov->private = be->be_private;
671 be->be_private = private;
672 on->on_bi.bi_private = ov;
677 ** translucent_open()
678 ** if the captive backend has an open() method, call it;
682 static int translucent_open(BackendDB *be) {
683 slap_overinst *on = (slap_overinst *) be->bd_info;
684 overlay_stack *ov = on->on_bi.bi_private;
685 void *private = be->be_private;
688 /* "should never happen" */
690 Debug(LDAP_DEBUG_ANY, "translucent_open() called with bad ov->info\n", 0, 0, 0);
694 Debug(LDAP_DEBUG_TRACE, "translucent_open\n", 0, 0, 0);
696 be->be_private = ov->private;
697 rc = ov->info->bi_db_open ? ov->info->bi_db_open(be) : 0;
698 be->be_private = private;
700 if(rc) Debug(LDAP_DEBUG_TRACE,
701 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
707 ** translucent_close()
708 ** if the captive backend has a close() method, call it;
709 ** free any config data;
713 static int translucent_close(BackendDB *be) {
714 slap_overinst *on = (slap_overinst *) be->bd_info;
715 overlay_stack *ov = on->on_bi.bi_private;
716 void *private = be->be_private;
719 be->be_private = ov->private;
720 rc = (ov->info && ov->info->bi_db_close) ? ov->info->bi_db_close(be) : 0;
721 be->be_private = private;
722 if(ov->config) ch_free(ov->config);
728 ** translucent_init()
729 ** initialize the slap_overinst with our entry points;
733 int translucent_init() {
735 translucent.on_bi.bi_type = "translucent";
736 translucent.on_bi.bi_db_init = translucent_db_init;
737 translucent.on_bi.bi_db_config = translucent_config;
738 translucent.on_bi.bi_db_open = translucent_open;
739 translucent.on_bi.bi_db_close = translucent_close;
740 translucent.on_bi.bi_op_bind = translucent_bind;
741 translucent.on_bi.bi_op_add = translucent_add;
742 translucent.on_bi.bi_op_modify = translucent_modify;
743 translucent.on_bi.bi_op_modrdn = translucent_modrdn;
744 translucent.on_bi.bi_op_delete = translucent_delete;
745 translucent.on_bi.bi_op_search = translucent_search;
746 translucent.on_bi.bi_op_compare = translucent_compare;
748 return(overlay_register(&translucent));
751 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
752 int init_module(int argc, char *argv[]) {
753 return translucent_init();
757 #endif /* SLAPD_OVER_TRANSLUCENT */