]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/translucent.c
fix overlays with private instances of databases (ITS#5736)
[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-2008 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         AttributeName *local;   /* valid attrs for local filters */
40         AttributeName *remote;  /* valid attrs for remote filters */
41         int strict;
42         int no_glue;
43         int defer_db_open;
44         int bind_local;
45         int pwmod_local;
46 } translucent_info;
47
48 static ConfigLDAPadd translucent_ldadd;
49 static ConfigCfAdd translucent_cfadd;
50
51 static ConfigDriver translucent_cf_gen;
52
53 enum {
54         TRANS_LOCAL = 1,
55         TRANS_REMOTE
56 };
57
58 static ConfigTable translucentcfg[] = {
59         { "translucent_strict", "on|off", 1, 2, 0,
60           ARG_ON_OFF|ARG_OFFSET,
61           (void *)offsetof(translucent_info, strict),
62           "( OLcfgOvAt:14.1 NAME 'olcTranslucentStrict' "
63           "DESC 'Reveal attribute deletion constraint violations' "
64           "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
65         { "translucent_no_glue", "on|off", 1, 2, 0,
66           ARG_ON_OFF|ARG_OFFSET,
67           (void *)offsetof(translucent_info, no_glue),
68           "( OLcfgOvAt:14.2 NAME 'olcTranslucentNoGlue' "
69           "DESC 'Disable automatic glue records for ADD and MODRDN' "
70           "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
71         { "translucent_local", "attr[,attr...]", 1, 2, 0,
72           ARG_MAGIC|TRANS_LOCAL,
73           translucent_cf_gen,
74           "( OLcfgOvAt:14.3 NAME 'olcTranslucentLocal' "
75           "DESC 'Attributes to use in local search filter' "
76           "SYNTAX OMsDirectoryString )", NULL, NULL },
77         { "translucent_remote", "attr[,attr...]", 1, 2, 0,
78           ARG_MAGIC|TRANS_REMOTE,
79           translucent_cf_gen,
80           "( OLcfgOvAt:14.4 NAME 'olcTranslucentRemote' "
81           "DESC 'Attributes to use in remote search filter' "
82           "SYNTAX OMsDirectoryString )", NULL, NULL },
83         { "translucent_bind_local", "on|off", 1, 2, 0,
84           ARG_ON_OFF|ARG_OFFSET,
85           (void *)offsetof(translucent_info, bind_local),
86           "( OLcfgOvAt:14.5 NAME 'olcTranslucentBindLocal' "
87           "DESC 'Enable local bind' "
88           "SYNTAX OMsBoolean SINGLE-VALUE)", NULL, NULL },
89         { "translucent_pwmod_local", "on|off", 1, 2, 0,
90           ARG_ON_OFF|ARG_OFFSET,
91           (void *)offsetof(translucent_info, pwmod_local),
92           "( OLcfgOvAt:14.6 NAME 'olcTranslucentPwModLocal' "
93           "DESC 'Enable local RFC 3062 Password Modify extended operation' "
94           "SYNTAX OMsBoolean SINGLE-VALUE)", NULL, NULL },
95         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
96 };
97
98 static ConfigOCs translucentocs[] = {
99         { "( OLcfgOvOc:14.1 "
100           "NAME 'olcTranslucentConfig' "
101           "DESC 'Translucent configuration' "
102           "SUP olcOverlayConfig "
103           "MAY ( olcTranslucentStrict $ olcTranslucentNoGlue $"
104           " olcTranslucentLocal $ olcTranslucentRemote $"
105           " olcTranslucentBindLocal $ olcTranslucentPwModLocal ) )",
106           Cft_Overlay, translucentcfg, NULL, translucent_cfadd },
107         { "( OLcfgOvOc:14.2 "
108           "NAME 'olcTranslucentDatabase' "
109           "DESC 'Translucent target database configuration' "
110           "AUXILIARY )", Cft_Misc, olcDatabaseDummy, translucent_ldadd },
111         { NULL, 0, NULL }
112 };
113 /* for translucent_init() */
114
115 static int
116 translucent_ldadd_cleanup( ConfigArgs *ca )
117 {
118         slap_overinst *on = ca->ca_private;
119         translucent_info *ov = on->on_bi.bi_private;
120
121         ov->defer_db_open = 0;
122         return backend_startup_one( ca->be, &ca->reply );
123 }
124
125 static int
126 translucent_ldadd( CfEntryInfo *cei, Entry *e, ConfigArgs *ca )
127 {
128         slap_overinst *on;
129         translucent_info *ov;
130
131         Debug(LDAP_DEBUG_TRACE, "==> translucent_ldadd\n", 0, 0, 0);
132
133         if ( cei->ce_type != Cft_Overlay || !cei->ce_bi ||
134              cei->ce_bi->bi_cf_ocs != translucentocs )
135                 return LDAP_CONSTRAINT_VIOLATION;
136
137         on = (slap_overinst *)cei->ce_bi;
138         ov = on->on_bi.bi_private;
139         ca->be = &ov->db;
140         ca->ca_private = on;
141         if ( CONFIG_ONLINE_ADD( ca ))
142                 ca->cleanup = translucent_ldadd_cleanup;
143         else
144                 ov->defer_db_open = 0;
145
146         return LDAP_SUCCESS;
147 }
148
149 static int
150 translucent_cfadd( Operation *op, SlapReply *rs, Entry *e, ConfigArgs *ca )
151 {
152         CfEntryInfo *cei = e->e_private;
153         slap_overinst *on = (slap_overinst *)cei->ce_bi;
154         translucent_info *ov = on->on_bi.bi_private;
155         struct berval bv;
156
157         Debug(LDAP_DEBUG_TRACE, "==> translucent_cfadd\n", 0, 0, 0);
158
159         /* FIXME: should not hardcode "olcDatabase" here */
160         bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
161                 "olcDatabase=%s", ov->db.bd_info->bi_type );
162         if ( bv.bv_len < 0 || bv.bv_len >= sizeof( ca->cr_msg ) ) {
163                 return -1;
164         }
165         bv.bv_val = ca->cr_msg;
166         ca->be = &ov->db;
167         ov->defer_db_open = 0;
168
169         /* We can only create this entry if the database is table-driven
170          */
171         if ( ov->db.bd_info->bi_cf_ocs )
172                 config_build_entry( op, rs, cei, ca, &bv,
173                                     ov->db.bd_info->bi_cf_ocs,
174                                     &translucentocs[1] );
175
176         return 0;
177 }
178
179 static int
180 translucent_cf_gen( ConfigArgs *c )
181 {
182         slap_overinst   *on = (slap_overinst *)c->bi;
183         translucent_info *ov = on->on_bi.bi_private;
184         AttributeName **an, *a2;
185         int i;
186
187         if ( c->type == TRANS_LOCAL )
188                 an = &ov->local;
189         else
190                 an = &ov->remote;
191
192         if ( c->op == SLAP_CONFIG_EMIT ) {
193                 if ( !*an )
194                         return 1;
195                 for ( i = 0; !BER_BVISNULL(&(*an)[i].an_name); i++ ) {
196                         value_add_one( &c->rvalue_vals, &(*an)[i].an_name );
197                 }
198                 return ( i < 1 );
199         } else if ( c->op == LDAP_MOD_DELETE ) {
200                 if ( c->valx < 0 ) {
201                         anlist_free( *an, 1, NULL );
202                         *an = NULL;
203                 } else {
204                         i = c->valx;
205                         ch_free( (*an)[i].an_name.bv_val );
206                         do {
207                                 (*an)[i] = (*an)[i+1];
208                         } while ( !BER_BVISNULL( &(*an)[i].an_name ));
209                 }
210                 return 0;
211         }
212         a2 = str2anlist( *an, c->argv[1], "," );
213         if ( !a2 ) {
214                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s unable to parse attribute %s",
215                         c->argv[0], c->argv[1] );
216                 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
217                         "%s: %s\n", c->log, c->cr_msg, 0 );
218                 return ARG_BAD_CONF;
219         }
220         *an = a2;
221         return 0;
222 }
223
224 static slap_overinst translucent;
225
226 /*
227 ** glue_parent()
228 **      call syncrepl_add_glue() with the parent suffix;
229 **
230 */
231
232 static struct berval glue[] = { BER_BVC("top"), BER_BVC("glue"), BER_BVNULL };
233
234 void glue_parent(Operation *op) {
235         Operation nop = *op;
236         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
237         struct berval ndn = BER_BVNULL;
238         Attribute *a;
239         Entry *e;
240         struct berval   pdn;
241
242         dnParent( &op->o_req_ndn, &pdn );
243         ber_dupbv_x( &ndn, &pdn, op->o_tmpmemctx );
244
245         Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", ndn.bv_val, 0, 0);
246
247         e = entry_alloc();
248         e->e_id = NOID;
249         ber_dupbv(&e->e_name, &ndn);
250         ber_dupbv(&e->e_nname, &ndn);
251
252         a = attr_alloc( slap_schema.si_ad_objectClass );
253         a->a_numvals = 2;
254         a->a_vals = ch_malloc(sizeof(struct berval) * 3);
255         ber_dupbv(&a->a_vals[0], &glue[0]);
256         ber_dupbv(&a->a_vals[1], &glue[1]);
257         ber_dupbv(&a->a_vals[2], &glue[2]);
258         a->a_nvals = a->a_vals;
259         a->a_next = e->e_attrs;
260         e->e_attrs = a;
261
262         a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
263         a->a_numvals = 1;
264         a->a_vals = ch_malloc(sizeof(struct berval) * 2);
265         ber_dupbv(&a->a_vals[0], &glue[1]);
266         ber_dupbv(&a->a_vals[1], &glue[2]);
267         a->a_nvals = a->a_vals;
268         a->a_next = e->e_attrs;
269         e->e_attrs = a;
270
271         nop.o_req_dn = ndn;
272         nop.o_req_ndn = ndn;
273         nop.ora_e = e;
274
275         nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
276         syncrepl_add_glue(&nop, e);
277         nop.o_bd->bd_info = (BackendInfo *) on;
278
279         op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
280
281         return;
282 }
283
284 /*
285 ** dup_bervarray()
286 **      copy a BerVarray;
287 */
288
289 BerVarray dup_bervarray(BerVarray b) {
290         int i, len;
291         BerVarray nb;
292         for(len = 0; b[len].bv_val; len++);
293         nb = ch_malloc((len+1) * sizeof(BerValue));
294         for(i = 0; i < len; i++) ber_dupbv(&nb[i], &b[i]);
295         nb[len].bv_val = NULL;
296         nb[len].bv_len = 0;
297         return(nb);
298 }
299
300 /*
301 ** free_attr_chain()
302 **      free only the Attribute*, not the contents;
303 **
304 */
305 void free_attr_chain(Attribute *b) {
306         Attribute *a;
307         for(a=b; a; a=a->a_next) {
308                 a->a_vals = NULL;
309                 a->a_nvals = NULL;
310         }
311         attrs_free( b );
312         return;
313 }
314
315 /*
316 ** translucent_add()
317 **      if not bound as root, send ACCESS error;
318 **      if glue, glue_parent();
319 **      return CONTINUE;
320 **
321 */
322
323 static int translucent_add(Operation *op, SlapReply *rs) {
324         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
325         translucent_info *ov = on->on_bi.bi_private;
326         Debug(LDAP_DEBUG_TRACE, "==> translucent_add: %s\n",
327                 op->o_req_dn.bv_val, 0, 0);
328         if(!be_isroot(op)) {
329                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
330                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
331                         "user modification of overlay database not permitted");
332                 op->o_bd->bd_info = (BackendInfo *) on;
333                 return(rs->sr_err);
334         }
335         if(!ov->no_glue) glue_parent(op);
336         return(SLAP_CB_CONTINUE);
337 }
338
339 /*
340 ** translucent_modrdn()
341 **      if not bound as root, send ACCESS error;
342 **      if !glue, glue_parent();
343 **      else return CONTINUE;
344 **
345 */
346
347 static int translucent_modrdn(Operation *op, SlapReply *rs) {
348         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
349         translucent_info *ov = on->on_bi.bi_private;
350         Debug(LDAP_DEBUG_TRACE, "==> translucent_modrdn: %s -> %s\n",
351                 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
352         if(!be_isroot(op)) {
353                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
354                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
355                         "user modification of overlay database not permitted");
356                 op->o_bd->bd_info = (BackendInfo *) on;
357                 return(rs->sr_err);
358         }
359         if(!ov->no_glue) {
360                 op->o_tag = LDAP_REQ_ADD;
361                 glue_parent(op);
362                 op->o_tag = LDAP_REQ_MODRDN;
363         }
364         return(SLAP_CB_CONTINUE);
365 }
366
367 /*
368 ** translucent_delete()
369 **      if not bound as root, send ACCESS error;
370 **      else return CONTINUE;
371 **
372 */
373
374 static int translucent_delete(Operation *op, SlapReply *rs) {
375         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
376         Debug(LDAP_DEBUG_TRACE, "==> translucent_delete: %s\n",
377                 op->o_req_dn.bv_val, 0, 0);
378         if(!be_isroot(op)) {
379                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
380                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
381                         "user modification of overlay database not permitted");
382                 op->o_bd->bd_info = (BackendInfo *) on;
383                 return(rs->sr_err);
384         }
385         return(SLAP_CB_CONTINUE);
386 }
387
388 static int
389 translucent_tag_cb( Operation *op, SlapReply *rs )
390 {
391         op->o_tag = LDAP_REQ_MODIFY;
392         op->orm_modlist = op->o_callback->sc_private;
393         rs->sr_tag = slap_req2res( op->o_tag );
394
395         return SLAP_CB_CONTINUE;
396 }
397
398 /*
399 ** translucent_modify()
400 **      modify in local backend if exists in both;
401 **      otherwise, add to local backend;
402 **      fail if not defined in captive backend;
403 **
404 */
405
406 static int translucent_modify(Operation *op, SlapReply *rs) {
407         SlapReply nrs = { REP_RESULT };
408
409         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
410         translucent_info *ov = on->on_bi.bi_private;
411         Entry *e = NULL, *re = NULL;
412         Attribute *a, *ax;
413         Modifications *m, **mm;
414         BackendDB *db;
415         int del, rc, erc = 0;
416         slap_callback cb = { 0 };
417
418         Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
419                 op->o_req_dn.bv_val, 0, 0);
420
421         if(ov->defer_db_open) {
422                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
423                         "remote DB not available");
424                 return(rs->sr_err);
425         }
426 /*
427 ** fetch entry from the captive backend;
428 ** if it did not exist, fail;
429 ** release it, if captive backend supports this;
430 **
431 */
432
433         db = op->o_bd;
434         op->o_bd = &ov->db;
435         rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
436         if(rc != LDAP_SUCCESS || re == NULL ) {
437                 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
438                         "attempt to modify nonexistent local record");
439                 return(rs->sr_err);
440         }
441         op->o_bd = db;
442 /*
443 ** fetch entry from local backend;
444 ** if it exists:
445 **      foreach Modification:
446 **          if attr not present in local:
447 **              if Mod == LDAP_MOD_DELETE:
448 **                  if remote attr not present, return NO_SUCH;
449 **                  if remote attr present, drop this Mod;
450 **              else force this Mod to LDAP_MOD_ADD;
451 **      return CONTINUE;
452 **
453 */
454
455         op->o_bd->bd_info = (BackendInfo *) on->on_info;
456         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
457         op->o_bd->bd_info = (BackendInfo *) on;
458
459         if(e && rc == LDAP_SUCCESS) {
460                 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
461                 for(mm = &op->orm_modlist; *mm; ) {
462                         m = *mm;
463                         for(a = e->e_attrs; a; a = a->a_next)
464                                 if(a->a_desc == m->sml_desc) break;
465                         if(a) {
466                                 mm = &m->sml_next;
467                                 continue;               /* found local attr */
468                         }
469                         if(m->sml_op == LDAP_MOD_DELETE) {
470                                 for(a = re->e_attrs; a; a = a->a_next)
471                                         if(a->a_desc == m->sml_desc) break;
472                                 /* not found remote attr */
473                                 if(!a) {
474                                         erc = LDAP_NO_SUCH_ATTRIBUTE;
475                                         goto release;
476                                 }
477                                 if(ov->strict) {
478                                         erc = LDAP_CONSTRAINT_VIOLATION;
479                                         goto release;
480                                 }
481                                 Debug(LDAP_DEBUG_TRACE,
482                                         "=> translucent_modify: silently dropping delete: %s\n",
483                                         m->sml_desc->ad_cname.bv_val, 0, 0);
484                                 *mm = m->sml_next;
485                                 m->sml_next = NULL;
486                                 slap_mods_free(m, 1);
487                                 continue;
488                         }
489                         m->sml_op = LDAP_MOD_ADD;
490                         mm = &m->sml_next;
491                 }
492                 erc = SLAP_CB_CONTINUE;
493 release:
494                 if(re) {
495                         if(ov->db.bd_info->bi_entry_release_rw) {
496                                 op->o_bd = &ov->db;
497                                 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
498                                 op->o_bd = db;
499                         } else
500                                 entry_free(re);
501                 }
502                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
503                 be_entry_release_r(op, e);
504                 op->o_bd->bd_info = (BackendInfo *) on;
505                 if(erc == SLAP_CB_CONTINUE) {
506                         return(erc);
507                 } else if(erc) {
508                         send_ldap_error(op, rs, erc,
509                                 "attempt to delete nonexistent attribute");
510                         return(erc);
511                 }
512         }
513
514         /* don't leak remote entry copy */
515         if(re) {
516                 if(ov->db.bd_info->bi_entry_release_rw) {
517                         op->o_bd = &ov->db;
518                         ov->db.bd_info->bi_entry_release_rw(op, re, 0);
519                         op->o_bd = db;
520                 } else
521                         entry_free(re);
522         }
523 /*
524 ** foreach Modification:
525 **      if MOD_ADD or MOD_REPLACE, add Attribute;
526 ** if no Modifications were suitable:
527 **      if strict, throw CONSTRAINT_VIOLATION;
528 **      else, return early SUCCESS;
529 ** fabricate Entry with new Attribute chain;
530 ** glue_parent() for this Entry;
531 ** call bi_op_add() in local backend;
532 **
533 */
534
535         Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
536         a = NULL;
537         for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
538                 Attribute atmp;
539                 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
540                    ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
541                         Debug(LDAP_DEBUG_ANY,
542                                 "=> translucent_modify: silently dropped modification(%d): %s\n",
543                                 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
544                         if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
545                         continue;
546                 }
547                 atmp.a_desc = m->sml_desc;
548                 atmp.a_vals = m->sml_values;
549                 atmp.a_nvals = m->sml_nvalues ? m->sml_nvalues : atmp.a_vals;
550                 atmp.a_numvals = m->sml_numvals;
551                 atmp.a_flags = 0;
552                 a = attr_dup( &atmp );
553                 a->a_next  = ax;
554                 ax = a;
555         }
556
557         if(del && ov->strict) {
558                 attrs_free( a );
559                 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
560                         "attempt to delete attributes from local database");
561                 return(rs->sr_err);
562         }
563
564         if(!ax) {
565                 if(ov->strict) {
566                         send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
567                                 "modification contained other than ADD or REPLACE");
568                         return(rs->sr_err);
569                 }
570                 /* rs->sr_text = "no valid modification found"; */
571                 rs->sr_err = LDAP_SUCCESS;
572                 send_ldap_result(op, rs);
573                 return(rs->sr_err);
574         }
575
576         e = entry_alloc();
577         ber_dupbv( &e->e_name, &op->o_req_dn );
578         ber_dupbv( &e->e_nname, &op->o_req_ndn );
579         e->e_attrs = a;
580
581         op->o_tag       = LDAP_REQ_ADD;
582         cb.sc_response = translucent_tag_cb;
583         cb.sc_private = op->orm_modlist;
584         op->oq_add.rs_e = e;
585
586         glue_parent(op);
587
588         cb.sc_next = op->o_callback;
589         op->o_callback = &cb;
590         rc = on->on_info->oi_orig->bi_op_add(op, &nrs);
591         if ( op->ora_e == e )
592                 entry_free( e );
593         op->o_callback = cb.sc_next;
594
595         return(rc);
596 }
597
598 static int translucent_compare(Operation *op, SlapReply *rs) {
599         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
600         translucent_info *ov = on->on_bi.bi_private;
601         AttributeAssertion *ava = op->orc_ava;
602         Entry *e;
603         BackendDB *db;
604         int rc;
605
606         Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
607                 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
608
609 /*
610 ** if the local backend has an entry for this attribute:
611 **      CONTINUE and let it do the compare;
612 **
613 */
614         rc = overlay_entry_get_ov(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e, on);
615         if(rc == LDAP_SUCCESS && e) {
616                 overlay_entry_release_ov(op, e, 0, on);
617                 return(SLAP_CB_CONTINUE);
618         }
619
620         if(ov->defer_db_open) {
621                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
622                         "remote DB not available");
623                 return(rs->sr_err);
624         }
625 /*
626 ** call compare() in the captive backend;
627 ** return the result;
628 **
629 */
630         db = op->o_bd;
631         op->o_bd = &ov->db;
632         rc = ov->db.bd_info->bi_op_compare(op, rs);
633         op->o_bd = db;
634
635         return(rc);
636 }
637
638 static int translucent_pwmod(Operation *op, SlapReply *rs) {
639         SlapReply nrs = { REP_RESULT };
640         Operation nop;
641
642         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
643         translucent_info *ov = on->on_bi.bi_private;
644         const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
645         Entry *e = NULL, *re = NULL;
646         BackendDB *db;
647         int rc = 0;
648         slap_callback cb = { 0 };
649
650         if (!ov->pwmod_local) {
651                 rs->sr_err = LDAP_CONSTRAINT_VIOLATION,
652                 rs->sr_text = "attempt to modify password in local database";
653                 return rs->sr_err;
654         }
655
656 /*
657 ** fetch entry from the captive backend;
658 ** if it did not exist, fail;
659 ** release it, if captive backend supports this;
660 **
661 */
662         db = op->o_bd;
663         op->o_bd = &ov->db;
664         rc = ov->db.bd_info->bi_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &re);
665         if(rc != LDAP_SUCCESS || re == NULL ) {
666                 send_ldap_error((op), rs, LDAP_NO_SUCH_OBJECT,
667                         "attempt to modify nonexistent local record");
668                 return(rs->sr_err);
669         }
670         op->o_bd = db;
671 /*
672 ** fetch entry from local backend;
673 ** if it exists:
674 **      return CONTINUE;
675 */
676
677         op->o_bd->bd_info = (BackendInfo *) on->on_info;
678         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
679         op->o_bd->bd_info = (BackendInfo *) on;
680
681         if(e && rc == LDAP_SUCCESS) {
682                 if(re) {
683                         if(ov->db.bd_info->bi_entry_release_rw) {
684                                 op->o_bd = &ov->db;
685                                 ov->db.bd_info->bi_entry_release_rw(op, re, 0);
686                                 op->o_bd = db;
687                         } else {
688                                 entry_free(re);
689                         }
690                 }
691                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
692                 be_entry_release_r(op, e);
693                 op->o_bd->bd_info = (BackendInfo *) on;
694                 return SLAP_CB_CONTINUE;
695         }
696
697         /* don't leak remote entry copy */
698         if(re) {
699                 if(ov->db.bd_info->bi_entry_release_rw) {
700                         op->o_bd = &ov->db;
701                         ov->db.bd_info->bi_entry_release_rw(op, re, 0);
702                         op->o_bd = db;
703                 } else {
704                         entry_free(re);
705                 }
706         }
707 /*
708 ** glue_parent() for this Entry;
709 ** call bi_op_add() in local backend;
710 **
711 */
712         e = entry_alloc();
713         ber_dupbv( &e->e_name, &op->o_req_dn );
714         ber_dupbv( &e->e_nname, &op->o_req_ndn );
715         e->e_attrs = NULL;
716
717         nop = *op;
718         nop.o_tag = LDAP_REQ_ADD;
719         cb.sc_response = slap_null_cb;
720         nop.oq_add.rs_e = e;
721
722         glue_parent(&nop);
723
724         nop.o_callback = &cb;
725         rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
726         if ( nop.ora_e == e ) {
727                 entry_free( e );
728         }
729
730         if ( rc == LDAP_SUCCESS ) {
731                 return SLAP_CB_CONTINUE;
732         }
733
734         return rc;
735 }
736
737 static int translucent_exop(Operation *op, SlapReply *rs) {
738         SlapReply nrs = { REP_RESULT };
739
740         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
741         translucent_info *ov = on->on_bi.bi_private;
742         const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
743
744         Debug(LDAP_DEBUG_TRACE, "==> translucent_exop: %s\n",
745                 op->o_req_dn.bv_val, 0, 0);
746
747         if(ov->defer_db_open) {
748                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
749                         "remote DB not available");
750                 return(rs->sr_err);
751         }
752
753         if ( bvmatch( &bv_exop_pwmod, &op->ore_reqoid ) ) {
754                 return translucent_pwmod( op, rs );
755         }
756
757         return SLAP_CB_CONTINUE;
758 }
759
760 /*
761 ** translucent_search_cb()
762 **      merge local data with remote data
763 **
764 ** Four cases:
765 ** 1: remote search, no local filter
766 **      merge data and send immediately
767 ** 2: remote search, with local filter
768 **      merge data and save
769 ** 3: local search, no remote filter
770 **      merge data and send immediately
771 ** 4: local search, with remote filter
772 **      check list, merge, send, delete
773 */
774
775 #define RMT_SIDE        0
776 #define LCL_SIDE        1
777 #define USE_LIST        2
778
779 typedef struct trans_ctx {
780         BackendDB *db;
781         slap_overinst *on;
782         Filter *orig;
783         Avlnode *list;
784         int step;
785 } trans_ctx;
786
787 static int translucent_search_cb(Operation *op, SlapReply *rs) {
788         trans_ctx *tc;
789         BackendDB *db;
790         slap_overinst *on;
791         translucent_info *ov;
792         Entry *le, *re;
793         Attribute *a, *ax, *an, *as = NULL;
794         int rc;
795         int test_f = 0;
796
797         tc = op->o_callback->sc_private;
798
799         /* Don't let the op complete while we're gathering data */
800         if ( rs->sr_type == REP_RESULT && ( tc->step & USE_LIST ))
801                 return 0;
802
803         if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
804                 return(SLAP_CB_CONTINUE);
805
806         Debug(LDAP_DEBUG_TRACE, "==> translucent_search_cb: %s\n",
807                 rs->sr_entry->e_name.bv_val, 0, 0);
808
809         on = tc->on;
810         ov = on->on_bi.bi_private;
811
812         db = op->o_bd;
813         re = NULL;
814
815         /* If we have local, get remote */
816         if ( tc->step & LCL_SIDE ) {
817                 le = rs->sr_entry;
818                 /* If entry is already on list, use it */
819                 if ( tc->step & USE_LIST ) {
820                         re = tavl_delete( &tc->list, le, entry_dn_cmp );
821                         if ( re ) {
822                                 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
823                                         rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
824                                         overlay_entry_release_ov( op, rs->sr_entry, 0, on );
825                                 }
826                                 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
827                                         rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
828                                         entry_free( rs->sr_entry );
829                                 }
830                                 rc = test_filter( op, re, tc->orig );
831                                 if ( rc == LDAP_COMPARE_TRUE ) {
832                                         rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
833                                         rs->sr_entry = re;
834                                         return SLAP_CB_CONTINUE;
835                                 } else {
836                                         entry_free( re );
837                                         rs->sr_entry = NULL;
838                                         return 0;
839                                 }
840                         }
841                 }
842                 op->o_bd = &ov->db;
843                 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &re );
844                 if ( rc == LDAP_SUCCESS && re ) {
845                         Entry *tmp = entry_dup( re );
846                         be_entry_release_r( op, re );
847                         re = tmp;
848                         test_f = 1;
849                 }
850         } else {
851         /* Else we have remote, get local */
852                 op->o_bd = tc->db;
853                 rc = overlay_entry_get_ov(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &le, on);
854                 if ( rc == LDAP_SUCCESS && le ) {
855                         re = entry_dup( rs->sr_entry );
856                         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
857                                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
858                                 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
859                         }
860                         if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
861                                 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
862                                 entry_free( rs->sr_entry );
863                         }
864                 } else {
865                         le = NULL;
866                 }
867         }
868
869 /*
870 ** if we got remote and local entry:
871 **      foreach local attr:
872 **              foreach remote attr:
873 **                      if match, remote attr with local attr;
874 **                      if new local, add to list;
875 **      append new local attrs to remote;
876 **
877 */
878
879         if ( re && le ) {
880                 for(ax = le->e_attrs; ax; ax = ax->a_next) {
881                         for(a = re->e_attrs; a; a = a->a_next) {
882                                 if(a->a_desc == ax->a_desc) {
883                                         if(a->a_vals != a->a_nvals)
884                                                 ber_bvarray_free(a->a_nvals);
885                                         ber_bvarray_free(a->a_vals);
886                                         a->a_vals = dup_bervarray(ax->a_vals);
887                                         a->a_nvals = (ax->a_vals == ax->a_nvals) ?
888                                                 a->a_vals : dup_bervarray(ax->a_nvals);
889                                         break;
890                                 }
891                         }
892                         if(a) continue;
893                         an = attr_dup(ax);
894                         an->a_next = as;
895                         as = an;
896                 }
897                 /* Dispose of local entry */
898                 if ( tc->step & LCL_SIDE ) {
899                         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
900                                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
901                                 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
902                         }
903                         if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
904                                 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
905                                 entry_free( rs->sr_entry );
906                         }
907                 } else {
908                         overlay_entry_release_ov(op, le, 0, on);
909                 }
910
911                 /* literally append, so locals are always last */
912                 if(as) {
913                         if(re->e_attrs) {
914                                 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
915                                 ax->a_next = as;
916                         } else {
917                                 re->e_attrs = as;
918                         }
919                 }
920                 /* If both filters, save entry for later */
921                 if ( tc->step == (USE_LIST|RMT_SIDE) ) {
922                         tavl_insert( &tc->list, re, entry_dn_cmp, avl_dup_error );
923                         rs->sr_entry = NULL;
924                         rc = 0;
925                 } else {
926                 /* send it now */
927                         rs->sr_entry = re;
928                         rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
929                         if ( test_f ) {
930                                 rc = test_filter( op, rs->sr_entry, tc->orig );
931                                 if ( rc == LDAP_COMPARE_TRUE ) {
932                                         rc = SLAP_CB_CONTINUE;
933                                 } else {
934                                         rc = 0;
935                                 }
936                         } else {
937                                 rc = SLAP_CB_CONTINUE;
938                         }
939                 }
940         } else if ( le ) {
941         /* Only a local entry: remote was deleted
942          * Ought to delete the local too...
943          */
944                 rc = 0;
945         } else if ( tc->step & USE_LIST ) {
946         /* Only a remote entry, but both filters:
947          * Test the complete filter
948          */
949                 rc = test_filter( op, rs->sr_entry, tc->orig );
950                 if ( rc == LDAP_COMPARE_TRUE ) {
951                         rc = SLAP_CB_CONTINUE;
952                 } else {
953                         rc = 0;
954                 }
955         } else {
956         /* Only a remote entry, only remote filter:
957          * just pass thru
958          */
959                 rc = SLAP_CB_CONTINUE;
960         }
961
962         op->o_bd = db;
963         return rc;
964 }
965
966 /* Dup the filter, excluding invalid elements */
967 static Filter *
968 trans_filter_dup(Operation *op, Filter *f, AttributeName *an)
969 {
970         Filter *n = NULL;
971
972         if ( !f )
973                 return NULL;
974
975         switch( f->f_choice & SLAPD_FILTER_MASK ) {
976         case SLAPD_FILTER_COMPUTED:
977                 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
978                 n->f_choice = f->f_choice;
979                 n->f_result = f->f_result;
980                 n->f_next = NULL;
981                 break;
982
983         case LDAP_FILTER_PRESENT:
984                 if ( ad_inlist( f->f_desc, an )) {
985                         n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
986                         n->f_choice = f->f_choice;
987                         n->f_desc = f->f_desc;
988                         n->f_next = NULL;
989                 }
990                 break;
991
992         case LDAP_FILTER_EQUALITY:
993         case LDAP_FILTER_GE:
994         case LDAP_FILTER_LE:
995         case LDAP_FILTER_APPROX:
996         case LDAP_FILTER_SUBSTRINGS:
997         case LDAP_FILTER_EXT:
998                 if ( !f->f_av_desc || ad_inlist( f->f_av_desc, an )) {
999                         n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1000                         n->f_choice = f->f_choice;
1001                         n->f_ava = f->f_ava;
1002                         n->f_next = NULL;
1003                 }
1004                 break;
1005
1006         case LDAP_FILTER_AND:
1007         case LDAP_FILTER_OR:
1008         case LDAP_FILTER_NOT: {
1009                 Filter **p;
1010
1011                 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1012                 n->f_choice = f->f_choice;
1013                 n->f_next = NULL;
1014
1015                 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
1016                         *p = trans_filter_dup( op, f, an );
1017                         if ( !*p )
1018                                 continue;
1019                         p = &(*p)->f_next;
1020                 }
1021                 /* nothing valid in this list */
1022                 if ( !n->f_list ) {
1023                         op->o_tmpfree( n, op->o_tmpmemctx );
1024                         return NULL;
1025                 }
1026                 /* Only 1 element in this list */
1027                 if ((n->f_choice & SLAPD_FILTER_MASK) != LDAP_FILTER_NOT &&
1028                         !n->f_list->f_next ) {
1029                         f = n->f_list;
1030                         *n = *f;
1031                         op->o_tmpfree( f, op->o_tmpmemctx );
1032                 }
1033                 break;
1034         }
1035         }
1036         return n;
1037 }
1038
1039 static void
1040 trans_filter_free( Operation *op, Filter *f )
1041 {
1042         Filter *n, *p, *next;
1043
1044         f->f_choice &= SLAPD_FILTER_MASK;
1045
1046         switch( f->f_choice ) {
1047         case LDAP_FILTER_AND:
1048         case LDAP_FILTER_OR:
1049         case LDAP_FILTER_NOT:
1050                 /* Free in reverse order */
1051                 n = NULL;
1052                 for ( p = f->f_list; p; p = next ) {
1053                         next = p->f_next;
1054                         p->f_next = n;
1055                         n = p;
1056                 }
1057                 for ( p = n; p; p = next ) {
1058                         next = p->f_next;
1059                         trans_filter_free( op, p );
1060                 }
1061                 break;
1062         default:
1063                 break;
1064         }
1065         op->o_tmpfree( f, op->o_tmpmemctx );
1066 }
1067
1068 /*
1069 ** translucent_search()
1070 **      search via captive backend;
1071 **      override results with any local data;
1072 **
1073 */
1074
1075 static int translucent_search(Operation *op, SlapReply *rs) {
1076         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1077         translucent_info *ov = on->on_bi.bi_private;
1078         slap_callback cb = { NULL, NULL, NULL, NULL };
1079         trans_ctx tc;
1080         Filter *fl, *fr;
1081         struct berval fbv;
1082         int rc = 0;
1083
1084         Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
1085                 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
1086
1087         if(ov->defer_db_open) {
1088                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1089                         "remote DB not available");
1090                 return(rs->sr_err);
1091         }
1092
1093         fr = ov->remote ? trans_filter_dup( op, op->ors_filter, ov->remote ) : NULL;
1094         fl = ov->local ? trans_filter_dup( op, op->ors_filter, ov->local ) : NULL;
1095         cb.sc_response = (slap_response *) translucent_search_cb;
1096         cb.sc_private = &tc;
1097         cb.sc_next = op->o_callback;
1098
1099         tc.db = op->o_bd;
1100         tc.on = on;
1101         tc.orig = op->ors_filter;
1102         tc.list = NULL;
1103         tc.step = 0;
1104         fbv = op->ors_filterstr;
1105
1106         op->o_callback = &cb;
1107
1108         if ( fr || !fl ) {
1109                 op->o_bd = &ov->db;
1110                 tc.step |= RMT_SIDE;
1111                 if ( fl ) {
1112                         tc.step |= USE_LIST;
1113                         op->ors_filter = fr;
1114                         filter2bv_x( op, fr, &op->ors_filterstr );
1115                 }
1116                 rc = ov->db.bd_info->bi_op_search(op, rs);
1117                 op->o_bd = tc.db;
1118                 if ( fl ) {
1119                         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1120                 }
1121         }
1122         if ( fl && !rc ) {
1123                 tc.step |= LCL_SIDE;
1124                 op->ors_filter = fl;
1125                 filter2bv_x( op, fl, &op->ors_filterstr );
1126                 rc = overlay_op_walk( op, rs, op_search, on->on_info, on->on_next );
1127                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1128         }
1129         op->ors_filterstr = fbv;
1130         op->ors_filter = tc.orig;
1131         op->o_callback = cb.sc_next;
1132         /* Send out anything remaining on the list and finish */
1133         if ( tc.step & USE_LIST ) {
1134                 if ( tc.list ) {
1135                         Avlnode *av;
1136
1137                         av = tavl_end( tc.list, TAVL_DIR_LEFT );
1138                         while ( av ) {
1139                                 rs->sr_entry = av->avl_data;
1140                                 rc = test_filter( op, rs->sr_entry, op->ors_filter );
1141                                 if ( rc == LDAP_COMPARE_TRUE ) {
1142                                         rs->sr_flags = REP_ENTRY_MUSTBEFREED;
1143                                         rc = send_search_entry( op, rs );
1144                                         if ( rc ) break;
1145                                 } else {
1146                                         entry_free( rs->sr_entry );
1147                                 }
1148                                 av = tavl_next( av, TAVL_DIR_RIGHT );
1149                         }
1150                         tavl_free( tc.list, NULL );
1151                         rs->sr_entry = NULL;
1152                 }
1153                 send_ldap_result( op, rs );
1154         }
1155
1156         /* Free in reverse order */
1157         if ( fl )
1158                 trans_filter_free( op, fl );
1159         if ( fr )
1160                 trans_filter_free( op, fr );
1161
1162         return rc;
1163 }
1164
1165
1166 /*
1167 ** translucent_bind()
1168 **      pass bind request to captive backend;
1169 **
1170 */
1171
1172 static int translucent_bind(Operation *op, SlapReply *rs) {
1173         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1174         translucent_info *ov = on->on_bi.bi_private;
1175         BackendDB *db;
1176         slap_callback sc = { 0 }, *save_cb;
1177         int rc;
1178
1179         Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
1180                 op->o_req_dn.bv_val, op->orb_method, 0);
1181
1182         if(ov->defer_db_open) {
1183                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1184                         "remote DB not available");
1185                 return(rs->sr_err);
1186         }
1187
1188         if (ov->bind_local) {
1189                 sc.sc_response = slap_null_cb;
1190                 save_cb = op->o_callback;
1191                 op->o_callback = &sc;
1192         }
1193
1194         db = op->o_bd;
1195         op->o_bd = &ov->db;
1196         rc = ov->db.bd_info->bi_op_bind(op, rs);
1197         op->o_bd = db;
1198
1199         if (ov->bind_local) {
1200                 op->o_callback = save_cb;
1201                 if (rc != LDAP_SUCCESS) {
1202                         rc = SLAP_CB_CONTINUE;
1203                 }
1204         }
1205
1206         return rc;
1207 }
1208
1209 /*
1210 ** translucent_connection_destroy()
1211 **      pass disconnect notification to captive backend;
1212 **
1213 */
1214
1215 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
1216         slap_overinst *on = (slap_overinst *) be->bd_info;
1217         translucent_info *ov = on->on_bi.bi_private;
1218         int rc = 0;
1219
1220         Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
1221
1222         rc = ov->db.bd_info->bi_connection_destroy(&ov->db, conn);
1223
1224         return(rc);
1225 }
1226
1227 /*
1228 ** translucent_db_config()
1229 **      pass config directives to captive backend;
1230 **      parse unrecognized directives ourselves;
1231 **
1232 */
1233
1234 static int translucent_db_config(
1235         BackendDB       *be,
1236         const char      *fname,
1237         int             lineno,
1238         int             argc,
1239         char            **argv
1240 )
1241 {
1242         slap_overinst *on = (slap_overinst *) be->bd_info;
1243         translucent_info *ov = on->on_bi.bi_private;
1244
1245         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_config: %s\n",
1246               argc ? argv[0] : "", 0, 0);
1247
1248         /* Something for the captive database? */
1249         if ( ov->db.bd_info && ov->db.bd_info->bi_db_config )
1250                 return ov->db.bd_info->bi_db_config( &ov->db, fname, lineno,
1251                         argc, argv );
1252         return SLAP_CONF_UNKNOWN;
1253 }
1254
1255 /*
1256 ** translucent_db_init()
1257 **      initialize the captive backend;
1258 **
1259 */
1260
1261 static int translucent_db_init(BackendDB *be, ConfigReply *cr) {
1262         slap_overinst *on = (slap_overinst *) be->bd_info;
1263         translucent_info *ov;
1264
1265         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_init\n", 0, 0, 0);
1266
1267         ov = ch_calloc(1, sizeof(translucent_info));
1268         on->on_bi.bi_private = ov;
1269         ov->db = *be;
1270         ov->db.be_private = NULL;
1271         ov->db.be_pcl_mutexp = &ov->db.be_pcl_mutex;
1272         ov->defer_db_open = 1;
1273
1274         if ( !backend_db_init( "ldap", &ov->db, -1, NULL )) {
1275                 Debug( LDAP_DEBUG_CONFIG, "translucent: unable to open captive back-ldap\n", 0, 0, 0);
1276                 return 1;
1277         }
1278         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1279         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
1280
1281         return 0;
1282 }
1283
1284 /*
1285 ** translucent_db_open()
1286 **      if the captive backend has an open() method, call it;
1287 **
1288 */
1289
1290 static int translucent_db_open(BackendDB *be, ConfigReply *cr) {
1291         slap_overinst *on = (slap_overinst *) be->bd_info;
1292         translucent_info *ov = on->on_bi.bi_private;
1293         int rc;
1294
1295         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_open\n", 0, 0, 0);
1296
1297         /* need to inherit something from the original database... */
1298         ov->db.be_def_limit = be->be_def_limit;
1299         ov->db.be_limits = be->be_limits;
1300         ov->db.be_acl = be->be_acl;
1301         ov->db.be_dfltaccess = be->be_dfltaccess;
1302
1303         if ( ov->defer_db_open )
1304                 return 0;
1305
1306         rc = backend_startup_one( &ov->db, cr );
1307
1308         if(rc) Debug(LDAP_DEBUG_TRACE,
1309                 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
1310
1311         return(rc);
1312 }
1313
1314 /*
1315 ** translucent_db_close()
1316 **      if the captive backend has a close() method, call it
1317 **
1318 */
1319
1320 static int
1321 translucent_db_close( BackendDB *be, ConfigReply *cr )
1322 {
1323         slap_overinst *on = (slap_overinst *) be->bd_info;
1324         translucent_info *ov = on->on_bi.bi_private;
1325         int rc = 0;
1326
1327         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
1328
1329         if ( ov && ov->db.bd_info && ov->db.bd_info->bi_db_close ) {
1330                 rc = ov->db.bd_info->bi_db_close(&ov->db, NULL);
1331         }
1332
1333         return(rc);
1334 }
1335
1336 /*
1337 ** translucent_db_destroy()
1338 **      if the captive backend has a db_destroy() method, call it;
1339 **      free any config data
1340 **
1341 */
1342
1343 static int
1344 translucent_db_destroy( BackendDB *be, ConfigReply *cr )
1345 {
1346         slap_overinst *on = (slap_overinst *) be->bd_info;
1347         translucent_info *ov = on->on_bi.bi_private;
1348         int rc = 0;
1349
1350         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_destroy\n", 0, 0, 0);
1351
1352         if ( ov ) {
1353                 if ( ov->remote )
1354                         anlist_free( ov->remote, 1, NULL );
1355                 if ( ov->local )
1356                         anlist_free( ov->local, 1, NULL );
1357                 if ( ov->db.be_private != NULL ) {
1358                         backend_stopdown_one( &ov->db );
1359                 }
1360
1361                 ch_free(ov);
1362                 on->on_bi.bi_private = NULL;
1363         }
1364
1365         return(rc);
1366 }
1367
1368 /*
1369 ** translucent_initialize()
1370 **      initialize the slap_overinst with our entry points;
1371 **
1372 */
1373
1374 int translucent_initialize() {
1375
1376         int rc;
1377
1378         Debug(LDAP_DEBUG_TRACE, "==> translucent_initialize\n", 0, 0, 0);
1379
1380         translucent.on_bi.bi_type       = "translucent";
1381         translucent.on_bi.bi_db_init    = translucent_db_init;
1382         translucent.on_bi.bi_db_config  = translucent_db_config;
1383         translucent.on_bi.bi_db_open    = translucent_db_open;
1384         translucent.on_bi.bi_db_close   = translucent_db_close;
1385         translucent.on_bi.bi_db_destroy = translucent_db_destroy;
1386         translucent.on_bi.bi_op_bind    = translucent_bind;
1387         translucent.on_bi.bi_op_add     = translucent_add;
1388         translucent.on_bi.bi_op_modify  = translucent_modify;
1389         translucent.on_bi.bi_op_modrdn  = translucent_modrdn;
1390         translucent.on_bi.bi_op_delete  = translucent_delete;
1391         translucent.on_bi.bi_op_search  = translucent_search;
1392         translucent.on_bi.bi_op_compare = translucent_compare;
1393         translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
1394         translucent.on_bi.bi_extended   = translucent_exop;
1395
1396         translucent.on_bi.bi_cf_ocs = translucentocs;
1397         rc = config_register_schema ( translucentcfg, translucentocs );
1398         if ( rc ) return rc;
1399
1400         return(overlay_register(&translucent));
1401 }
1402
1403 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
1404 int init_module(int argc, char *argv[]) {
1405         return translucent_initialize();
1406 }
1407 #endif
1408
1409 #endif /* SLAPD_OVER_TRANSLUCENT */