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