]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/translucent.c
Cleanup
[openldap] / servers / slapd / overlays / translucent.c
1 /* translucent.c - translucent proxy module */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2004-2005 The OpenLDAP Foundation.
6  * Portions Copyright 2005 Symas Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Symas Corp. for inclusion in
19  * OpenLDAP Software.  This work was sponsored by Hewlett-Packard.
20  */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_TRANSLUCENT
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32
33 /* config block */
34
35 typedef struct translucent_configuration {
36         int debug;
37         int strict;
38         int no_add;
39         int glue;
40 } translucent_configuration;
41
42 /* stack of captive backends */
43
44 typedef struct overlay_stack {
45         BackendInfo *info;                      /* captive backend */
46         void *private;                          /* local backend_private */
47         translucent_configuration *config;      /* our_private: configuration */
48 } overlay_stack;
49
50 /* for translucent_init() */
51
52 static slap_overinst translucent;
53
54 /*
55 ** glue_parent()
56 **      call syncrepl_add_glue() with the parent suffix;
57 **
58 */
59
60 static struct berval glue[] = { BER_BVC("top"), BER_BVC("glue"), BER_BVNULL };
61
62 void glue_parent(Operation *op) {
63         Operation nop = *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;
67         Attribute *a;
68         Entry *e;
69         int idn, ldn;
70
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 */
74         idn++;
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);
78
79         Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", dn.bv_val, 0, 0);
80
81         e = ch_calloc(1, sizeof(Entry));
82         e->e_id = NOID;
83         ber_dupbv(&e->e_name, &dn);
84         ber_dupbv(&e->e_nname, &dn);
85
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;
94         e->e_attrs = a;
95
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;
103         e->e_attrs = a;
104
105         nop.o_req_dn = dn;
106         nop.o_req_ndn = dn;
107         nop.ora_e = e;
108         nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
109
110         syncrepl_add_glue(&nop, e);
111         return;
112 }
113
114 /*
115 ** dup_bervarray()
116 **      copy a BerVarray;
117 */
118
119 BerVarray dup_bervarray(BerVarray b) {
120         int i, len;
121         BerVarray nb;
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;
126         nb[len].bv_len = 0;
127         return(nb);
128 }
129
130 /*
131 ** free_attr_chain()
132 **      free only the Attribute*, not the contents;
133 **
134 */
135 void free_attr_chain(Attribute *a) {
136         Attribute *ax;
137         for(ax = NULL; a; a = a->a_next) {
138                 if(ax) ch_free(ax);
139                 ax = a;
140         }
141         return;
142 }
143
144 /*
145 ** translucent_add()
146 **      if not bound as root, send ACCESS error;
147 **      if config.glue, glue_parent();
148 **      return CONTINUE;
149 **
150 */
151
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);
157         if(!be_isroot(op)) {
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");
161                 return(rs->sr_err);
162         }
163         if(!ov->config->glue) glue_parent(op);
164         return(SLAP_CB_CONTINUE);
165 }
166
167 /*
168 ** translucent_modrdn()
169 **      if not bound as root, send ACCESS error;
170 **      if !config.glue, glue_parent();
171 **      else return CONTINUE;
172 **
173 */
174
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);
180         if(!be_isroot(op)) {
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");
184                 return(rs->sr_err);
185         }
186         if(!ov->config->glue) glue_parent(op);
187         return(SLAP_CB_CONTINUE);
188 }
189
190 /*
191 ** translucent_delete()
192 **      if not bound as root, send ACCESS error;
193 **      else return CONTINUE;
194 **
195 */
196
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);
201         if(!be_isroot(op)) {
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");
205                 return(rs->sr_err);
206         }
207         return(SLAP_CB_CONTINUE);
208 }
209
210 /*
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;
215 **
216 */
217
218 static int translucent_modify(Operation *op, SlapReply *rs) {
219         SlapReply nrs = { REP_RESULT };
220         Operation nop = *op;
221
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;
226         Attribute *a, *ax;
227         Modifications *m, *mm;
228         int del, rc, erc = 0;
229
230         Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
231                 op->o_req_dn.bv_val, 0, 0);
232
233 /*
234 ** fetch entry from the captive backend;
235 ** if it did not exist, fail;
236 ** release it, if captive backend supports this;
237 **
238 */
239
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;
244
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");
249                 return(rs->sr_err);
250         }
251
252 /*
253 ** fetch entry from local backend;
254 ** if it exists:
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;
261 **      return CONTINUE;
262 **
263 */
264
265         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
266
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 */
277                                 if(!a) {
278                                         erc = LDAP_NO_SUCH_ATTRIBUTE;
279                                         goto release;
280                                 }
281                                 if(ov->config->strict) {
282                                         erc = LDAP_CONSTRAINT_VIOLATION;
283                                         goto release;
284                                 }
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;
290                                 mm = m;
291                                 m = m->sml_next;
292                                 mm->sml_next = NULL;            /* hack */
293                                 slap_mods_free(mm, 1);
294                                 if(m) continue;
295                         }
296                         m->sml_op = LDAP_MOD_ADD;
297                 }
298                 erc = SLAP_CB_CONTINUE;
299 release:
300                 if(re) {
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);
304                         else
305                                 entry_free(re);
306                         op->o_bd->be_private = private;
307                 }
308                 be_entry_release_r(op, e);
309                 if(erc == SLAP_CB_CONTINUE) {
310                         op->o_bd->bd_info = (BackendInfo *) on;
311                         return(erc);
312                 } else if(erc) {
313                         send_ldap_error(op, rs, erc,
314                                 "attempt to delete nonexistent attribute");
315                         return(erc);
316                 }
317         }
318
319 /*
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;
328 **
329 */
330
331         Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
332         a = NULL;
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++;
340                         continue;
341                 }
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;
346                 a->a_next  = ax;
347                 ax = a;
348         }
349
350         if(del && ov->config->strict) {
351                 free_attr_chain(a);
352                 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
353                         "attempt to delete attributes from local database");
354                 return(rs->sr_err);
355         }
356
357         if(!ax) {
358                 if(ov->config->strict) {
359                         send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
360                                 "modification contained other than ADD or REPLACE");
361                         return(rs->sr_err);
362                 }
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);
367                 return(rs->sr_err);
368         }
369
370         ne.e_id         = NOID;
371         ne.e_name       = op->o_req_dn;
372         ne.e_nname      = op->o_req_ndn;
373         ne.e_attrs      = a;
374         ne.e_ocflags    = 0;
375         ne.e_bv.bv_len  = 0;
376         ne.e_bv.bv_val  = NULL;
377         ne.e_private    = NULL;
378
379         nop.o_tag       = LDAP_REQ_ADD;
380         nop.oq_add.rs_e = &ne;
381
382         op->o_bd->bd_info = (BackendInfo *) on;
383         glue_parent(&nop);
384
385         rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
386         free_attr_chain(a);
387
388         return(rc);
389 }
390
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;
395
396         AttributeAssertion *ava = op->orc_ava;
397         Entry *e;
398         int rc;
399
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);
402
403 /*
404 ** if the local backend has an entry for this attribute:
405 **      CONTINUE and let it do the compare;
406 **
407 */
408
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);
415         }
416
417 /*
418 ** call compare() in the captive backend;
419 ** return the result;
420 **
421 */
422
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;
427         return(rc);
428 }
429
430 /*
431 ** translucent_search_cb()
432 **      merge local data with the search result
433 **
434 */
435
436 static int translucent_search_cb(Operation *op, SlapReply *rs) {
437         slap_overinst *on;
438         Entry *e, *re = NULL;
439         Attribute *a, *ax, *an, *as = NULL;
440         void *private;
441         int rc;
442
443         if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
444                 return(SLAP_CB_CONTINUE);
445
446         Debug(LDAP_DEBUG_TRACE, "==> tranclucent_search_cb: %s\n",
447                 rs->sr_entry->e_name.bv_val, 0, 0);
448
449         on = (slap_overinst *) op->o_bd->bd_info;
450         op->o_bd->bd_info = (BackendInfo *) on->on_info;
451
452         private = op->o_bd->be_private;
453         op->o_bd->be_private = op->o_callback->sc_private;
454
455         rc = be_entry_get_rw(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e);
456
457 /*
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;
465 **
466 */
467
468         if(e && rc == LDAP_SUCCESS) {
469                 re = entry_dup(rs->sr_entry);
470                 for(ax = e->e_attrs; ax; ax = ax->a_next) {
471 #if 0
472                         if(is_at_operational(ax->a_desc->ad_type)) continue;
473 #endif
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);
482                                         break;
483                                 }
484                         }
485                         if(a) continue;
486                         an = attr_dup(ax);
487                         an->a_next = as;
488                         as = an;
489                 }
490                 be_entry_release_r(op, e);
491
492                 /* literally append, so locals are always last */
493                 if(as) {
494                         if(re->e_attrs) {
495                                 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
496                                 ax->a_next = as;
497                         } else {
498                                 re->e_attrs = as;
499                         }
500                 }
501                 rs->sr_entry = re;
502                 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
503         }
504
505         op->o_bd->be_private = private;
506         op->o_bd->bd_info = (BackendInfo *) on;
507
508         return(SLAP_CB_CONTINUE);
509 }
510
511 /*
512 ** translucent_search()
513 **      search via captive backend;
514 **      override results with any local data;
515 **
516 */
517
518 static int translucent_search(Operation *op, SlapReply *rs) {
519         Operation nop = *op;
520
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;
525         int rc;
526
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;
531
532         cb.sc_next = nop.o_callback;
533         nop.o_callback = &cb;
534
535         op->o_bd->be_private = ov->private;
536         rc = ov->info->bi_op_search(&nop, rs);
537         op->o_bd->be_private = private;
538
539         return(rs->sr_err);
540 }
541
542
543 /*
544 ** translucent_bind()
545 **      pass bind request to captive backend;
546 **
547 */
548
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;
553         int rc = 0;
554
555         Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
556                 op->o_req_dn.bv_val, op->orb_method, 0);
557
558         op->o_bd->be_private = ov->private;
559         rc = ov->info->bi_op_bind(op, rs);
560         op->o_bd->be_private = private;
561
562         return(rc);
563 }
564
565 /*
566 ** translucent_config()
567 **      pass config directives to captive backend;
568 **      parse unrecognized directives ourselves;
569 **
570 */
571
572 static int translucent_config(
573         BackendDB       *be,
574         const char      *fname,
575         int             lineno,
576         int             argc,
577         char            **argv
578 )
579 {
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;
584         int rc;
585
586         /* "this should never happen" */
587         if(!ov->info) {
588                 fprintf(stderr, "fatal: captive backend not initialized");
589                 return(1);
590         }
591
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;
597
598         /* pass okay or error up, SLAP_CONF_UNKNOWN might be ours */
599         if(rc == 0 || rc == 1) return(rc);
600
601         rc = 0;
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")) {
607                 ov->config->glue++;
608         } else if(!strcasecmp(*argv, "translucent_debug")) {
609                 if(argc == 1) {
610                         ov->config->debug = 0xFFFF;
611                         rc = 0;
612                 } else if(argc == 2) {
613                         ov->config->debug = atoi(argv[1]);
614                         rc = 0;
615                 } else {
616                         fprintf(stderr, "%s: line %d: too many arguments (%d) to debug\n",
617                                 fname, lineno, argc);
618                         rc = 1;
619                 }
620         } else {
621                 fprintf(stderr, "%s: line %d: unknown keyword %s\n",
622                         fname, lineno, *argv);
623                 rc = SLAP_CONF_UNKNOWN;
624         }
625         return(rc);
626 }
627
628 /*
629 ** translucent_db_init()
630 **      initialize the captive backend;
631 **
632 */
633
634 static int translucent_db_init(BackendDB *be) {
635         slap_overinst *on = (slap_overinst *) be->bd_info;
636         void *private = be->be_private;
637         overlay_stack *ov;
638         int rc;
639
640         Debug(LDAP_DEBUG_TRACE, "==> translucent_init\n", 0, 0, 0);
641
642         ov = ch_calloc(1, sizeof(overlay_stack));
643         ov->config = ch_calloc(1, sizeof(translucent_configuration));
644         ov->info = backend_info("ldap");
645
646         if(!ov->info) {
647                 Debug(LDAP_DEBUG_ANY, "translucent: backend_info failed!\n", 0, 0, 0);
648                 return(1);
649         }
650
651         /* forcibly disable schema checking on the local backend */
652         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
653
654         be->be_private = NULL;
655         rc = ov->info->bi_db_init ? ov->info->bi_db_init(be) : 0;
656
657         if(rc) Debug(LDAP_DEBUG_TRACE,
658                 "translucent: bi_db_init() returned error %d\n", rc, 0, 0);
659
660         ov->private = be->be_private;
661         be->be_private = private;
662         on->on_bi.bi_private = ov;
663         return(rc);
664 }
665
666 /*
667 ** translucent_open()
668 **      if the captive backend has an open() method, call it;
669 **
670 */
671
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;
676         int rc;
677
678         /* "should never happen" */
679         if(!ov->info) {
680                 Debug(LDAP_DEBUG_ANY, "translucent_open() called with bad ov->info\n", 0, 0, 0);
681                 return(LDAP_OTHER);
682         }
683
684         Debug(LDAP_DEBUG_TRACE, "translucent_open\n", 0, 0, 0);
685
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;
689
690         if(rc) Debug(LDAP_DEBUG_TRACE,
691                 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
692
693         return(rc);
694 }
695
696 /*
697 ** translucent_close()
698 **      if the captive backend has a close() method, call it;
699 **      free any config data;
700 **
701 */
702
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;
707         int rc;
708
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);
713         ch_free(ov);
714         return(rc);
715 }
716
717 /*
718 ** translucent_init()
719 **      initialize the slap_overinst with our entry points;
720 **
721 */
722
723 int translucent_init() {
724
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;
737
738         return(overlay_register(&translucent));
739 }
740
741 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
742 int init_module(int argc, char *argv[]) {
743         return translucent_init();
744 }
745 #endif
746
747 #endif /* SLAPD_OVER_TRANSLUCENT */
748