]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/translucent.c
ITS#4767 fix bdb_add / translucent interaction
[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-2006 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 #include "lutil.h"
33
34 #include "config.h"
35
36 /* config block */
37 typedef struct translucent_info {
38         BackendDB db;                   /* captive backend */
39         int strict;
40         int no_glue;
41 } translucent_info;
42
43 static ConfigLDAPadd translucent_ldadd;
44 static ConfigCfAdd translucent_cfadd;
45
46 static ConfigTable translucentcfg[] = {
47         { "translucent_strict", "on|off", 1, 2, 0,
48           ARG_ON_OFF|ARG_OFFSET,
49           (void *)offsetof(translucent_info, strict),
50           "( OLcfgOvAt:14.1 NAME 'olcTranslucentStrict' "
51           "DESC 'Reveal attribute deletion constraint violations' "
52           "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
53         { "translucent_no_glue", "on|off", 1, 2, 0,
54           ARG_ON_OFF|ARG_OFFSET,
55           (void *)offsetof(translucent_info, no_glue),
56           "( OLcfgOvAt:14.2 NAME 'olcTranslucentNoGlue' "
57           "DESC 'Disable automatic glue records for ADD and MODRDN' "
58           "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
59         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
60 };
61
62 static ConfigOCs translucentocs[] = {
63         { "( OLcfgOvOc:14.1 "
64           "NAME 'olcTranslucentConfig' "
65           "DESC 'Translucent configuration' "
66           "SUP olcOverlayConfig "
67           "MAY ( olcTranslucentStrict $ olcTranslucentNoGlue ) )",
68           Cft_Overlay, translucentcfg, NULL, translucent_cfadd },
69         { "( OLcfgOvOc:14.2 "
70           "NAME 'olcTranslucentDatabase' "
71           "DESC 'Translucent target database configuration' "
72           "AUXILIARY )", Cft_Misc, translucentcfg, translucent_ldadd },
73         { NULL, 0, NULL }
74 };
75 /* for translucent_init() */
76
77 static int
78 translucent_ldadd( CfEntryInfo *cei, Entry *e, ConfigArgs *ca )
79 {
80         slap_overinst *on;
81         translucent_info *ov;
82
83         Debug(LDAP_DEBUG_TRACE, "==> translucent_ldadd\n", 0, 0, 0);
84
85         if ( cei->ce_type != Cft_Overlay || !cei->ce_bi ||
86              cei->ce_bi->bi_cf_ocs != translucentocs )
87                 return LDAP_CONSTRAINT_VIOLATION;
88
89         on = (slap_overinst *)cei->ce_bi;
90         ov = on->on_bi.bi_private;
91         ca->be = &ov->db;
92         return LDAP_SUCCESS;
93 }
94
95 static int
96 translucent_cfadd( Operation *op, SlapReply *rs, Entry *e, ConfigArgs *ca )
97 {
98         CfEntryInfo *cei = e->e_private;
99         slap_overinst *on = (slap_overinst *)cei->ce_bi;
100         translucent_info *ov = on->on_bi.bi_private;
101         struct berval bv;
102
103         Debug(LDAP_DEBUG_TRACE, "==> translucent_cfadd\n", 0, 0, 0);
104
105         /* FIXME: should not hardcode "olcDatabase" here */
106         bv.bv_len = sprintf( ca->msg, "olcDatabase=%s",
107                              ov->db.bd_info->bi_type );
108         bv.bv_val = ca->msg;
109         ca->be = &ov->db;
110
111         /* We can only create this entry if the database is table-driven
112          */
113         if ( ov->db.bd_info->bi_cf_ocs )
114                 config_build_entry( op, rs, cei, ca, &bv,
115                                     ov->db.bd_info->bi_cf_ocs,
116                                     &translucentocs[1] );
117
118         return 0;
119 }
120
121 static slap_overinst translucent;
122
123 /*
124 ** glue_parent()
125 **      call syncrepl_add_glue() with the parent suffix;
126 **
127 */
128
129 static struct berval glue[] = { BER_BVC("top"), BER_BVC("glue"), BER_BVNULL };
130
131 void glue_parent(Operation *op) {
132         Operation nop = *op;
133         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
134         struct berval ndn = BER_BVNULL;
135         Attribute *a;
136         Entry *e;
137         struct berval   pdn;
138
139         dnParent( &op->o_req_ndn, &pdn );
140         ber_dupbv_x( &ndn, &pdn, op->o_tmpmemctx );
141
142         Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", ndn.bv_val, 0, 0);
143
144         e = entry_alloc();
145         e->e_id = NOID;
146         ber_dupbv(&e->e_name, &ndn);
147         ber_dupbv(&e->e_nname, &ndn);
148
149         a = attr_alloc( slap_schema.si_ad_objectClass );
150         a->a_vals = ch_malloc(sizeof(struct berval) * 3);
151         ber_dupbv(&a->a_vals[0], &glue[0]);
152         ber_dupbv(&a->a_vals[1], &glue[1]);
153         ber_dupbv(&a->a_vals[2], &glue[2]);
154         a->a_nvals = a->a_vals;
155         a->a_next = e->e_attrs;
156         e->e_attrs = a;
157
158         a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
159         a->a_vals = ch_malloc(sizeof(struct berval) * 2);
160         ber_dupbv(&a->a_vals[0], &glue[1]);
161         ber_dupbv(&a->a_vals[1], &glue[2]);
162         a->a_nvals = a->a_vals;
163         a->a_next = e->e_attrs;
164         e->e_attrs = a;
165
166         nop.o_req_dn = ndn;
167         nop.o_req_ndn = ndn;
168         nop.ora_e = e;
169
170         nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
171         syncrepl_add_glue(&nop, e);
172         nop.o_bd->bd_info = (BackendInfo *) on;
173
174         op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
175
176         return;
177 }
178
179 /*
180 ** dup_bervarray()
181 **      copy a BerVarray;
182 */
183
184 BerVarray dup_bervarray(BerVarray b) {
185         int i, len;
186         BerVarray nb;
187         for(len = 0; b[len].bv_val; len++);
188         nb = ch_malloc((len+1) * sizeof(BerValue));
189         for(i = 0; i < len; i++) ber_dupbv(&nb[i], &b[i]);
190         nb[len].bv_val = NULL;
191         nb[len].bv_len = 0;
192         return(nb);
193 }
194
195 /*
196 ** free_attr_chain()
197 **      free only the Attribute*, not the contents;
198 **
199 */
200 void free_attr_chain(Attribute *b) {
201         Attribute *a;
202         for(a=b; a; a=a->a_next) {
203                 a->a_vals = NULL;
204                 a->a_nvals = NULL;
205         }
206         attrs_free( b );
207         return;
208 }
209
210 /*
211 ** translucent_add()
212 **      if not bound as root, send ACCESS error;
213 **      if glue, glue_parent();
214 **      return CONTINUE;
215 **
216 */
217
218 static int translucent_add(Operation *op, SlapReply *rs) {
219         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
220         translucent_info *ov = on->on_bi.bi_private;
221         Debug(LDAP_DEBUG_TRACE, "==> translucent_add: %s\n",
222                 op->o_req_dn.bv_val, 0, 0);
223         if(!be_isroot(op)) {
224                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
225                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
226                         "user modification of overlay database not permitted");
227                 op->o_bd->bd_info = (BackendInfo *) on;
228                 return(rs->sr_err);
229         }
230         if(!ov->no_glue) glue_parent(op);
231         return(SLAP_CB_CONTINUE);
232 }
233
234 /*
235 ** translucent_modrdn()
236 **      if not bound as root, send ACCESS error;
237 **      if !glue, glue_parent();
238 **      else return CONTINUE;
239 **
240 */
241
242 static int translucent_modrdn(Operation *op, SlapReply *rs) {
243         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
244         translucent_info *ov = on->on_bi.bi_private;
245         Debug(LDAP_DEBUG_TRACE, "==> translucent_modrdn: %s -> %s\n",
246                 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
247         if(!be_isroot(op)) {
248                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
249                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
250                         "user modification of overlay database not permitted");
251                 op->o_bd->bd_info = (BackendInfo *) on;
252                 return(rs->sr_err);
253         }
254         if(!ov->no_glue) glue_parent(op);
255         return(SLAP_CB_CONTINUE);
256 }
257
258 /*
259 ** translucent_delete()
260 **      if not bound as root, send ACCESS error;
261 **      else return CONTINUE;
262 **
263 */
264
265 static int translucent_delete(Operation *op, SlapReply *rs) {
266         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
267         Debug(LDAP_DEBUG_TRACE, "==> translucent_delete: %s\n",
268                 op->o_req_dn.bv_val, 0, 0);
269         if(!be_isroot(op)) {
270                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
271                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
272                         "user modification of overlay database not permitted");
273                 op->o_bd->bd_info = (BackendInfo *) on;
274                 return(rs->sr_err);
275         }
276         return(SLAP_CB_CONTINUE);
277 }
278
279 static int
280 translucent_tag_cb( Operation *op, SlapReply *rs )
281 {
282         op->o_tag = LDAP_REQ_MODIFY;
283         op->orm_modlist = op->o_callback->sc_private;
284         rs->sr_tag = slap_req2res( op->o_tag );
285
286         return SLAP_CB_CONTINUE;
287 }
288
289 /*
290 ** translucent_modify()
291 **      modify in local backend if exists in both;
292 **      otherwise, add to local backend;
293 **      fail if not defined in captive backend;
294 **
295 */
296
297 static int translucent_modify(Operation *op, SlapReply *rs) {
298         SlapReply nrs = { REP_RESULT };
299         Operation nop = *op;
300
301         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
302         translucent_info *ov = on->on_bi.bi_private;
303         Entry *e = NULL, *re = NULL;
304         Attribute *a, *ax;
305         Modifications *m, *mm;
306         int del, rc, erc = 0;
307         slap_callback cb = { 0 };
308
309         Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
310                 op->o_req_dn.bv_val, 0, 0);
311
312 /*
313 ** fetch entry from the captive backend;
314 ** if it did not exist, fail;
315 ** release it, if captive backend supports this;
316 **
317 */
318
319         nop.o_bd = &ov->db;
320         rc = ov->db.bd_info->bi_entry_get_rw(&nop, &nop.o_req_ndn, NULL, NULL, 0, &re);
321         if(rc != LDAP_SUCCESS || re == NULL ) {
322                 send_ldap_error((&nop), rs, LDAP_NO_SUCH_OBJECT,
323                         "attempt to modify nonexistent local record");
324                 return(rs->sr_err);
325         }
326         nop = *op;
327 /*
328 ** fetch entry from local backend;
329 ** if it exists:
330 **      foreach Modification:
331 **          if attr not present in local:
332 **              if Mod == LDAP_MOD_DELETE:
333 **                  if remote attr not present, return NO_SUCH;
334 **                  if remote attr present, drop this Mod;
335 **              else force this Mod to LDAP_MOD_ADD;
336 **      return CONTINUE;
337 **
338 */
339
340         op->o_bd->bd_info = (BackendInfo *) on->on_info;
341         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
342         op->o_bd->bd_info = (BackendInfo *) on;
343
344         if(e && rc == LDAP_SUCCESS) {
345                 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
346                 for(m = op->orm_modlist; m; m = m->sml_next) {
347                         for(a = e->e_attrs; a; a = a->a_next)
348                                 if(a->a_desc == m->sml_desc) break;
349                         if(a) continue;         /* found local attr */
350                         if(m->sml_op == LDAP_MOD_DELETE) {
351                                 for(a = re->e_attrs; a; a = a->a_next)
352                                         if(a->a_desc == m->sml_desc) break;
353                                 /* not found remote attr */
354                                 if(!a) {
355                                         erc = LDAP_NO_SUCH_ATTRIBUTE;
356                                         goto release;
357                                 }
358                                 if(ov->strict) {
359                                         erc = LDAP_CONSTRAINT_VIOLATION;
360                                         goto release;
361                                 }
362                                 Debug(LDAP_DEBUG_TRACE,
363                                         "=> translucent_modify: silently dropping delete: %s\n",
364                                         m->sml_desc->ad_cname.bv_val, 0, 0);
365                                 for(mm = op->orm_modlist; mm->sml_next != m; mm = mm->sml_next);
366                                 mm->sml_next = m->sml_next;
367                                 m->sml_next = NULL;
368                                 slap_mods_free(m, 1);
369                                 m = mm;
370                                 continue;
371                         }
372                         m->sml_op = LDAP_MOD_ADD;
373                 }
374                 erc = SLAP_CB_CONTINUE;
375 release:
376                 if(re) {
377                         if(ov->db.bd_info->bi_entry_release_rw)
378                                 ov->db.bd_info->bi_entry_release_rw(&nop, re, 0);
379                         else
380                                 entry_free(re);
381                 }
382                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
383                 be_entry_release_r(op, e);
384                 op->o_bd->bd_info = (BackendInfo *) on;
385                 if(erc == SLAP_CB_CONTINUE) {
386                         return(erc);
387                 } else if(erc) {
388                         send_ldap_error(op, rs, erc,
389                                 "attempt to delete nonexistent attribute");
390                         return(erc);
391                 }
392         }
393
394         /* don't leak remote entry copy */
395         if(re) {
396                 if(ov->db.bd_info->bi_entry_release_rw)
397                         ov->db.bd_info->bi_entry_release_rw(&nop, re, 0);
398                 else
399                         entry_free(re);
400         }
401 /*
402 ** foreach Modification:
403 **      if MOD_ADD or MOD_REPLACE, add Attribute;
404 ** if no Modifications were suitable:
405 **      if strict, throw CONSTRAINT_VIOLATION;
406 **      else, return early SUCCESS;
407 ** fabricate Entry with new Attribute chain;
408 ** glue_parent() for this Entry;
409 ** call bi_op_add() in local backend;
410 **
411 */
412
413         Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
414         a = NULL;
415         for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
416                 Attribute atmp;
417                 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
418                    ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
419                         Debug(LDAP_DEBUG_ANY,
420                                 "=> translucent_modify: silently dropped modification(%d): %s\n",
421                                 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
422                         if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
423                         continue;
424                 }
425                 atmp.a_desc = m->sml_desc;
426                 atmp.a_vals = m->sml_values;
427                 atmp.a_nvals = m->sml_nvalues ? m->sml_nvalues : atmp.a_vals;
428                 a = attr_dup( &atmp );
429                 a->a_next  = ax;
430                 ax = a;
431         }
432
433         if(del && ov->strict) {
434                 attrs_free( a );
435                 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
436                         "attempt to delete attributes from local database");
437                 return(rs->sr_err);
438         }
439
440         if(!ax) {
441                 if(ov->strict) {
442                         send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
443                                 "modification contained other than ADD or REPLACE");
444                         return(rs->sr_err);
445                 }
446                 /* rs->sr_text = "no valid modification found"; */
447                 rs->sr_err = LDAP_SUCCESS;
448                 send_ldap_result(op, rs);
449                 return(rs->sr_err);
450         }
451
452         e = entry_alloc();
453         ber_dupbv( &e->e_name, &op->o_req_dn );
454         ber_dupbv( &e->e_nname, &op->o_req_ndn );
455         e->e_attrs = a;
456
457         nop.o_tag       = LDAP_REQ_ADD;
458         nop.oq_add.rs_e = e;
459
460         glue_parent(&nop);
461
462         cb.sc_response = translucent_tag_cb;
463         cb.sc_private = op->orm_modlist;
464         cb.sc_next = nop.o_callback;
465         nop.o_callback = &cb;
466         rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
467         if ( nop.ora_e == e )
468                 entry_free( e );
469
470         return(rc);
471 }
472
473 static int translucent_compare(Operation *op, SlapReply *rs) {
474         Operation nop = *op;
475         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
476         translucent_info *ov = on->on_bi.bi_private;
477         AttributeAssertion *ava = op->orc_ava;
478         Entry *e;
479         int rc;
480
481         Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
482                 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
483
484 /*
485 ** if the local backend has an entry for this attribute:
486 **      CONTINUE and let it do the compare;
487 **
488 */
489         op->o_bd->bd_info = (BackendInfo *) on->on_info;
490         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e);
491         if(e && rc == LDAP_SUCCESS) {
492                 be_entry_release_r(op, e);
493                 op->o_bd->bd_info = (BackendInfo *) on;
494                 return(SLAP_CB_CONTINUE);
495         }
496         op->o_bd->bd_info = (BackendInfo *) on;
497
498 /*
499 ** call compare() in the captive backend;
500 ** return the result;
501 **
502 */
503         nop.o_bd = &ov->db;
504         nop.o_callback = NULL;
505         rc = ov->db.bd_info->bi_op_compare(&nop, rs);
506
507         return(rc);
508 }
509
510 /*
511 ** translucent_search_cb()
512 **      merge local data with the search result
513 **
514 */
515
516 static int translucent_search_cb(Operation *op, SlapReply *rs) {
517         slap_overinst *on;
518         Entry *e, *re = NULL;
519         Attribute *a, *ax, *an, *as = NULL;
520         Operation * original_op, local_op;
521         int rc;
522
523         if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
524                 return(SLAP_CB_CONTINUE);
525
526         Debug(LDAP_DEBUG_TRACE, "==> translucent_search_cb: %s\n",
527                 rs->sr_entry->e_name.bv_val, 0, 0);
528
529         original_op = op->o_callback->sc_private;
530         on = (slap_overinst *) original_op->o_bd->bd_info;
531         local_op = *original_op;
532
533         local_op.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
534         rc = be_entry_get_rw(&local_op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e);
535         local_op.o_bd->bd_info = (BackendInfo *) on;
536
537 /*
538 ** if we got an entry from local backend:
539 **      make a copy of this search result;
540 **      foreach local attr:
541 **              foreach search result attr:
542 **                      if match, result attr with local attr;
543 **                      if new local, add to list;
544 **      append new local attrs to search result;
545 **
546 */
547
548         if(e && rc == LDAP_SUCCESS) {
549                 re = entry_dup(rs->sr_entry);
550                 for(ax = e->e_attrs; ax; ax = ax->a_next) {
551 #if 0
552                         if(is_at_operational(ax->a_desc->ad_type)) continue;
553 #endif
554                         for(a = re->e_attrs; a; a = a->a_next) {
555                                 if(a->a_desc == ax->a_desc) {
556                                         if(a->a_vals != a->a_nvals)
557                                                 ber_bvarray_free(a->a_nvals);
558                                         ber_bvarray_free(a->a_vals);
559                                         a->a_vals = dup_bervarray(ax->a_vals);
560                                         a->a_nvals = (ax->a_vals == ax->a_nvals) ?
561                                                 a->a_vals : dup_bervarray(ax->a_nvals);
562                                         break;
563                                 }
564                         }
565                         if(a) continue;
566                         an = attr_dup(ax);
567                         an->a_next = as;
568                         as = an;
569                 }
570                 local_op.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
571                 be_entry_release_r(&local_op, e);
572                 local_op.o_bd->bd_info = (BackendInfo *) on;
573
574                 /* literally append, so locals are always last */
575                 if(as) {
576                         if(re->e_attrs) {
577                                 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
578                                 ax->a_next = as;
579                         } else {
580                                 re->e_attrs = as;
581                         }
582                 }
583                 rs->sr_entry = re;
584                 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
585         }
586
587         return(SLAP_CB_CONTINUE);
588 }
589
590 /*
591 ** translucent_search()
592 **      search via captive backend;
593 **      override results with any local data;
594 **
595 */
596
597 static int translucent_search(Operation *op, SlapReply *rs) {
598         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
599         Operation nop = *op;
600         translucent_info *ov = on->on_bi.bi_private;
601         slap_callback cb = { NULL, NULL, NULL, NULL };
602
603         Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
604                 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
605
606         cb.sc_response = (slap_response *) translucent_search_cb;
607         cb.sc_private = op;
608         cb.sc_next = nop.o_callback;
609
610         nop.o_callback = &cb;
611         nop.o_bd = &ov->db;
612         return (ov->db.bd_info->bi_op_search(&nop, rs));
613 }
614
615
616 /*
617 ** translucent_bind()
618 **      pass bind request to captive backend;
619 **
620 */
621
622 static int translucent_bind(Operation *op, SlapReply *rs) {
623         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
624         Operation nop = *op;
625         translucent_info *ov = on->on_bi.bi_private;
626
627         Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
628                 op->o_req_dn.bv_val, op->orb_method, 0);
629
630         nop.o_bd = &ov->db;
631         return (ov->db.bd_info->bi_op_bind(&nop, rs));
632 }
633
634 /*
635 ** translucent_connection_destroy()
636 **      pass disconnect notification to captive backend;
637 **
638 */
639
640 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
641         slap_overinst *on = (slap_overinst *) be->bd_info;
642         translucent_info *ov = on->on_bi.bi_private;
643         int rc = 0;
644
645         Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
646
647         rc = ov->db.bd_info->bi_connection_destroy(&ov->db, conn);
648
649         return(rc);
650 }
651
652 /*
653 ** translucent_db_config()
654 **      pass config directives to captive backend;
655 **      parse unrecognized directives ourselves;
656 **
657 */
658
659 static int translucent_db_config(
660         BackendDB       *be,
661         const char      *fname,
662         int             lineno,
663         int             argc,
664         char            **argv
665 )
666 {
667         slap_overinst *on = (slap_overinst *) be->bd_info;
668         translucent_info *ov = on->on_bi.bi_private;
669
670         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_config: %s\n",
671               argc ? argv[0] : "", 0, 0);
672
673         /* Something for the captive database? */
674         if ( ov->db.bd_info && ov->db.bd_info->bi_db_config )
675                 return ov->db.bd_info->bi_db_config( &ov->db, fname, lineno,
676                         argc, argv );
677         return SLAP_CONF_UNKNOWN;
678 }
679
680 /*
681 ** translucent_db_init()
682 **      initialize the captive backend;
683 **
684 */
685
686 static int translucent_db_init(BackendDB *be) {
687         slap_overinst *on = (slap_overinst *) be->bd_info;
688         translucent_info *ov;
689         int rc;
690
691         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_init\n", 0, 0, 0);
692
693         ov = ch_calloc(1, sizeof(translucent_info));
694         on->on_bi.bi_private = ov;
695         ov->db = *be;
696         ov->db.be_private = NULL;
697         ov->db.be_pcl_mutexp = &ov->db.be_pcl_mutex;
698
699         if ( !backend_db_init( "ldap", &ov->db )) {
700                 Debug( LDAP_DEBUG_CONFIG, "translucent: unable to open captive back-ldap\n", 0, 0, 0);
701                 return 1;
702         }
703         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
704         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
705
706         return 0;
707 }
708
709 /*
710 ** translucent_db_open()
711 **      if the captive backend has an open() method, call it;
712 **
713 */
714
715 static int translucent_db_open(BackendDB *be) {
716         slap_overinst *on = (slap_overinst *) be->bd_info;
717         translucent_info *ov = on->on_bi.bi_private;
718         int rc;
719
720         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_open\n", 0, 0, 0);
721
722         /* need to inherit something from the original database... */
723         ov->db.be_def_limit = be->be_def_limit;
724         ov->db.be_limits = be->be_limits;
725         ov->db.be_acl = be->be_acl;
726         ov->db.be_dfltaccess = be->be_dfltaccess;
727
728         rc = backend_startup_one( &ov->db );
729
730         if(rc) Debug(LDAP_DEBUG_TRACE,
731                 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
732
733         return(rc);
734 }
735
736 /*
737 ** translucent_db_close()
738 **      if the captive backend has a close() method, call it;
739 **      free any config data;
740 **
741 */
742
743 static int
744 translucent_db_close( BackendDB *be )
745 {
746         slap_overinst *on = (slap_overinst *) be->bd_info;
747         translucent_info *ov = on->on_bi.bi_private;
748         int rc = 0;
749
750         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
751
752         if ( ov && ov->db.bd_info && ov->db.bd_info->bi_db_close ) {
753                 rc = ov->db.bd_info->bi_db_close(&ov->db);
754         }
755
756         return(rc);
757 }
758
759 /*
760 ** translucent_db_destroy()
761 **      if the captive backend has a db_destroy() method, call it
762 **
763 */
764
765 static int
766 translucent_db_destroy( BackendDB *be )
767 {
768         slap_overinst *on = (slap_overinst *) be->bd_info;
769         translucent_info *ov = on->on_bi.bi_private;
770         int rc = 0;
771
772         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
773
774         if ( ov ) {
775                 if ( ov->db.be_private != NULL ) {
776                         backend_stopdown_one( &ov->db );
777                 }
778
779                 ch_free(ov);
780                 on->on_bi.bi_private = NULL;
781         }
782
783         return(rc);
784 }
785
786 /*
787 ** translucent_initialize()
788 **      initialize the slap_overinst with our entry points;
789 **
790 */
791
792 int translucent_initialize() {
793
794         int rc;
795
796         Debug(LDAP_DEBUG_TRACE, "==> translucent_initialize\n", 0, 0, 0);
797
798         translucent.on_bi.bi_type       = "translucent";
799         translucent.on_bi.bi_db_init    = translucent_db_init;
800         translucent.on_bi.bi_db_config  = translucent_db_config;
801         translucent.on_bi.bi_db_open    = translucent_db_open;
802         translucent.on_bi.bi_db_close   = translucent_db_close;
803         translucent.on_bi.bi_db_destroy = translucent_db_destroy;
804         translucent.on_bi.bi_op_bind    = translucent_bind;
805         translucent.on_bi.bi_op_add     = translucent_add;
806         translucent.on_bi.bi_op_modify  = translucent_modify;
807         translucent.on_bi.bi_op_modrdn  = translucent_modrdn;
808         translucent.on_bi.bi_op_delete  = translucent_delete;
809         translucent.on_bi.bi_op_search  = translucent_search;
810         translucent.on_bi.bi_op_compare = translucent_compare;
811         translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
812
813         translucent.on_bi.bi_cf_ocs = translucentocs;
814         rc = config_register_schema ( translucentcfg, translucentocs );
815         if ( rc ) return rc;
816
817         return(overlay_register(&translucent));
818 }
819
820 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
821 int init_module(int argc, char *argv[]) {
822         return translucent_initialize();
823 }
824 #endif
825
826 #endif /* SLAPD_OVER_TRANSLUCENT */