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