]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/translucent.c
Fix void* pointer arithmetic from ber_bvchr()
[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         Attribute *a;
67         Entry *e;
68         struct berval   pdn;
69
70         dnParent( &op->o_req_ndn, &pdn );
71         ber_dupbv( &dn, &pdn );
72
73         Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", dn.bv_val, 0, 0);
74
75         e = ch_calloc(1, sizeof(Entry));
76         e->e_id = NOID;
77         ber_dupbv(&e->e_name, &dn);
78         ber_dupbv(&e->e_nname, &dn);
79
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;
88         e->e_attrs = a;
89
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;
97         e->e_attrs = a;
98
99         nop.o_req_dn = dn;
100         nop.o_req_ndn = dn;
101         nop.ora_e = e;
102         nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
103
104         syncrepl_add_glue(&nop, e);
105         return;
106 }
107
108 /*
109 ** dup_bervarray()
110 **      copy a BerVarray;
111 */
112
113 BerVarray dup_bervarray(BerVarray b) {
114         int i, len;
115         BerVarray nb;
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;
120         nb[len].bv_len = 0;
121         return(nb);
122 }
123
124 /*
125 ** free_attr_chain()
126 **      free only the Attribute*, not the contents;
127 **
128 */
129 void free_attr_chain(Attribute *a) {
130         Attribute *ax;
131         for(ax = NULL; a; a = a->a_next) {
132                 if(ax) ch_free(ax);
133                 ax = a;
134         }
135         return;
136 }
137
138 /*
139 ** translucent_add()
140 **      if not bound as root, send ACCESS error;
141 **      if config.glue, glue_parent();
142 **      return CONTINUE;
143 **
144 */
145
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);
151         if(!be_isroot(op)) {
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");
155                 return(rs->sr_err);
156         }
157         if(!ov->config->glue) glue_parent(op);
158         return(SLAP_CB_CONTINUE);
159 }
160
161 /*
162 ** translucent_modrdn()
163 **      if not bound as root, send ACCESS error;
164 **      if !config.glue, glue_parent();
165 **      else return CONTINUE;
166 **
167 */
168
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);
174         if(!be_isroot(op)) {
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");
178                 return(rs->sr_err);
179         }
180         if(!ov->config->glue) glue_parent(op);
181         return(SLAP_CB_CONTINUE);
182 }
183
184 /*
185 ** translucent_delete()
186 **      if not bound as root, send ACCESS error;
187 **      else return CONTINUE;
188 **
189 */
190
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);
195         if(!be_isroot(op)) {
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");
199                 return(rs->sr_err);
200         }
201         return(SLAP_CB_CONTINUE);
202 }
203
204 /*
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;
209 **
210 */
211
212 static int translucent_modify(Operation *op, SlapReply *rs) {
213         SlapReply nrs = { REP_RESULT };
214         Operation nop = *op;
215
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;
220         Attribute *a, *ax;
221         Modifications *m, *mm;
222         int del, rc, erc = 0;
223
224         Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
225                 op->o_req_dn.bv_val, 0, 0);
226
227 /*
228 ** fetch entry from the captive backend;
229 ** if it did not exist, fail;
230 ** release it, if captive backend supports this;
231 **
232 */
233
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;
238
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");
243                 return(rs->sr_err);
244         }
245
246 /*
247 ** fetch entry from local backend;
248 ** if it exists:
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;
255 **      return CONTINUE;
256 **
257 */
258
259         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
260
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 */
271                                 if(!a) {
272                                         erc = LDAP_NO_SUCH_ATTRIBUTE;
273                                         goto release;
274                                 }
275                                 if(ov->config->strict) {
276                                         erc = LDAP_CONSTRAINT_VIOLATION;
277                                         goto release;
278                                 }
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;
284                                 mm = m;
285                                 m = m->sml_next;
286                                 mm->sml_next = NULL;            /* hack */
287                                 slap_mods_free(mm, 1);
288                                 if(m) continue;
289                         }
290                         m->sml_op = LDAP_MOD_ADD;
291                 }
292                 erc = SLAP_CB_CONTINUE;
293 release:
294                 if(re) {
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);
298                         else
299                                 entry_free(re);
300                         op->o_bd->be_private = private;
301                 }
302                 be_entry_release_r(op, e);
303                 if(erc == SLAP_CB_CONTINUE) {
304                         op->o_bd->bd_info = (BackendInfo *) on;
305                         return(erc);
306                 } else if(erc) {
307                         send_ldap_error(op, rs, erc,
308                                 "attempt to delete nonexistent attribute");
309                         return(erc);
310                 }
311         }
312
313 /*
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;
322 **
323 */
324
325         Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
326         a = NULL;
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++;
334                         continue;
335                 }
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;
340                 a->a_next  = ax;
341                 ax = a;
342         }
343
344         if(del && ov->config->strict) {
345                 free_attr_chain(a);
346                 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
347                         "attempt to delete attributes from local database");
348                 return(rs->sr_err);
349         }
350
351         if(!ax) {
352                 if(ov->config->strict) {
353                         send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
354                                 "modification contained other than ADD or REPLACE");
355                         return(rs->sr_err);
356                 }
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);
361                 return(rs->sr_err);
362         }
363
364         ne.e_id         = NOID;
365         ne.e_name       = op->o_req_dn;
366         ne.e_nname      = op->o_req_ndn;
367         ne.e_attrs      = a;
368         ne.e_ocflags    = 0;
369         ne.e_bv.bv_len  = 0;
370         ne.e_bv.bv_val  = NULL;
371         ne.e_private    = NULL;
372
373         nop.o_tag       = LDAP_REQ_ADD;
374         nop.oq_add.rs_e = &ne;
375
376         op->o_bd->bd_info = (BackendInfo *) on;
377         glue_parent(&nop);
378
379         rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
380         free_attr_chain(a);
381
382         return(rc);
383 }
384
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;
389
390         AttributeAssertion *ava = op->orc_ava;
391         Entry *e;
392         int rc;
393
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);
396
397 /*
398 ** if the local backend has an entry for this attribute:
399 **      CONTINUE and let it do the compare;
400 **
401 */
402
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);
409         }
410
411 /*
412 ** call compare() in the captive backend;
413 ** return the result;
414 **
415 */
416
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;
421         return(rc);
422 }
423
424 /*
425 ** translucent_search_cb()
426 **      merge local data with the search result
427 **
428 */
429
430 static int translucent_search_cb(Operation *op, SlapReply *rs) {
431         slap_overinst *on;
432         Entry *e, *re = NULL;
433         Attribute *a, *ax, *an, *as = NULL;
434         void *private;
435         int rc;
436
437         if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
438                 return(SLAP_CB_CONTINUE);
439
440         Debug(LDAP_DEBUG_TRACE, "==> tranclucent_search_cb: %s\n",
441                 rs->sr_entry->e_name.bv_val, 0, 0);
442
443         on = (slap_overinst *) op->o_bd->bd_info;
444         op->o_bd->bd_info = (BackendInfo *) on->on_info;
445
446         private = op->o_bd->be_private;
447         op->o_bd->be_private = op->o_callback->sc_private;
448
449         rc = be_entry_get_rw(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e);
450
451 /*
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;
459 **
460 */
461
462         if(e && rc == LDAP_SUCCESS) {
463                 re = entry_dup(rs->sr_entry);
464                 for(ax = e->e_attrs; ax; ax = ax->a_next) {
465 #if 0
466                         if(is_at_operational(ax->a_desc->ad_type)) continue;
467 #endif
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);
476                                         break;
477                                 }
478                         }
479                         if(a) continue;
480                         an = attr_dup(ax);
481                         an->a_next = as;
482                         as = an;
483                 }
484                 be_entry_release_r(op, e);
485
486                 /* literally append, so locals are always last */
487                 if(as) {
488                         if(re->e_attrs) {
489                                 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
490                                 ax->a_next = as;
491                         } else {
492                                 re->e_attrs = as;
493                         }
494                 }
495                 rs->sr_entry = re;
496                 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
497         }
498
499         op->o_bd->be_private = private;
500         op->o_bd->bd_info = (BackendInfo *) on;
501
502         return(SLAP_CB_CONTINUE);
503 }
504
505 /*
506 ** translucent_search()
507 **      search via captive backend;
508 **      override results with any local data;
509 **
510 */
511
512 static int translucent_search(Operation *op, SlapReply *rs) {
513         Operation nop = *op;
514
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;
519         int rc;
520
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;
525
526         cb.sc_next = nop.o_callback;
527         nop.o_callback = &cb;
528
529         op->o_bd->be_private = ov->private;
530         rc = ov->info->bi_op_search(&nop, rs);
531         op->o_bd->be_private = private;
532
533         return(rs->sr_err);
534 }
535
536
537 /*
538 ** translucent_bind()
539 **      pass bind request to captive backend;
540 **
541 */
542
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;
547         int rc = 0;
548
549         Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
550                 op->o_req_dn.bv_val, op->orb_method, 0);
551
552         op->o_bd->be_private = ov->private;
553         rc = ov->info->bi_op_bind(op, rs);
554         op->o_bd->be_private = private;
555
556         return(rc);
557 }
558
559 /*
560 ** translucent_config()
561 **      pass config directives to captive backend;
562 **      parse unrecognized directives ourselves;
563 **
564 */
565
566 static int translucent_config(
567         BackendDB       *be,
568         const char      *fname,
569         int             lineno,
570         int             argc,
571         char            **argv
572 )
573 {
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;
578         int rc;
579
580         /* "this should never happen" */
581         if(!ov->info) {
582                 fprintf(stderr, "fatal: captive backend not initialized");
583                 return(1);
584         }
585
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;
591
592         /* pass okay or error up, SLAP_CONF_UNKNOWN might be ours */
593         if(rc == 0 || rc == 1) return(rc);
594
595         rc = 0;
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")) {
601                 ov->config->glue++;
602         } else if(!strcasecmp(*argv, "translucent_debug")) {
603                 if(argc == 1) {
604                         ov->config->debug = 0xFFFF;
605                         rc = 0;
606                 } else if(argc == 2) {
607                         ov->config->debug = atoi(argv[1]);
608                         rc = 0;
609                 } else {
610                         fprintf(stderr, "%s: line %d: too many arguments (%d) to debug\n",
611                                 fname, lineno, argc);
612                         rc = 1;
613                 }
614         } else {
615                 fprintf(stderr, "%s: line %d: unknown keyword %s\n",
616                         fname, lineno, *argv);
617                 rc = SLAP_CONF_UNKNOWN;
618         }
619         return(rc);
620 }
621
622 /*
623 ** translucent_db_init()
624 **      initialize the captive backend;
625 **
626 */
627
628 static int translucent_db_init(BackendDB *be) {
629         slap_overinst *on = (slap_overinst *) be->bd_info;
630         void *private = be->be_private;
631         overlay_stack *ov;
632         int rc;
633
634         Debug(LDAP_DEBUG_TRACE, "==> translucent_init\n", 0, 0, 0);
635
636         ov = ch_calloc(1, sizeof(overlay_stack));
637         ov->config = ch_calloc(1, sizeof(translucent_configuration));
638         ov->info = backend_info("ldap");
639
640         if(!ov->info) {
641                 Debug(LDAP_DEBUG_ANY, "translucent: backend_info failed!\n", 0, 0, 0);
642                 return(1);
643         }
644
645         /* forcibly disable schema checking on the local backend */
646         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
647
648         be->be_private = NULL;
649         rc = ov->info->bi_db_init ? ov->info->bi_db_init(be) : 0;
650
651         if(rc) Debug(LDAP_DEBUG_TRACE,
652                 "translucent: bi_db_init() returned error %d\n", rc, 0, 0);
653
654         ov->private = be->be_private;
655         be->be_private = private;
656         on->on_bi.bi_private = ov;
657         return(rc);
658 }
659
660 /*
661 ** translucent_open()
662 **      if the captive backend has an open() method, call it;
663 **
664 */
665
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;
670         int rc;
671
672         /* "should never happen" */
673         if(!ov->info) {
674                 Debug(LDAP_DEBUG_ANY, "translucent_open() called with bad ov->info\n", 0, 0, 0);
675                 return(LDAP_OTHER);
676         }
677
678         Debug(LDAP_DEBUG_TRACE, "translucent_open\n", 0, 0, 0);
679
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;
683
684         if(rc) Debug(LDAP_DEBUG_TRACE,
685                 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
686
687         return(rc);
688 }
689
690 /*
691 ** translucent_close()
692 **      if the captive backend has a close() method, call it;
693 **      free any config data;
694 **
695 */
696
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;
701         int rc;
702
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);
707         ch_free(ov);
708         return(rc);
709 }
710
711 /*
712 ** translucent_init()
713 **      initialize the slap_overinst with our entry points;
714 **
715 */
716
717 int translucent_init() {
718
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;
731
732         return(overlay_register(&translucent));
733 }
734
735 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
736 int init_module(int argc, char *argv[]) {
737         return translucent_init();
738 }
739 #endif
740
741 #endif /* SLAPD_OVER_TRANSLUCENT */
742