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