]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/translucent.c
ITS#6255
[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 } trans_ctx;
774
775 static int translucent_search_cb(Operation *op, SlapReply *rs) {
776         trans_ctx *tc;
777         BackendDB *db;
778         slap_overinst *on;
779         translucent_info *ov;
780         Entry *le, *re;
781         Attribute *a, *ax, *an, *as = NULL;
782         int rc;
783         int test_f = 0;
784
785         tc = op->o_callback->sc_private;
786
787         /* Don't let the op complete while we're gathering data */
788         if ( rs->sr_type == REP_RESULT && ( tc->step & USE_LIST ))
789                 return 0;
790
791         if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
792                 return(SLAP_CB_CONTINUE);
793
794         Debug(LDAP_DEBUG_TRACE, "==> translucent_search_cb: %s\n",
795                 rs->sr_entry->e_name.bv_val, 0, 0);
796
797         op->ors_slimit = tc->slimit + ( tc->slimit > 0 ? 1 : 0 );
798
799         on = tc->on;
800         ov = on->on_bi.bi_private;
801
802         db = op->o_bd;
803         re = NULL;
804
805         /* If we have local, get remote */
806         if ( tc->step & LCL_SIDE ) {
807                 le = rs->sr_entry;
808                 /* If entry is already on list, use it */
809                 if ( tc->step & USE_LIST ) {
810                         re = tavl_delete( &tc->list, le, entry_dn_cmp );
811                         if ( re ) {
812                                 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
813                                         rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
814                                         overlay_entry_release_ov( op, rs->sr_entry, 0, on );
815                                 }
816                                 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
817                                         rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
818                                         entry_free( rs->sr_entry );
819                                 }
820                                 rc = test_filter( op, re, tc->orig );
821                                 if ( rc == LDAP_COMPARE_TRUE ) {
822                                         rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
823                                         rs->sr_entry = re;
824
825                                         if ( tc->slimit >= 0 && rs->sr_nentries >= tc->slimit ) {
826                                                 return LDAP_SIZELIMIT_EXCEEDED;
827                                         }
828
829                                         return SLAP_CB_CONTINUE;
830                                 } else {
831                                         entry_free( re );
832                                         rs->sr_entry = NULL;
833                                         return 0;
834                                 }
835                         }
836                 }
837                 op->o_bd = &ov->db;
838                 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &re );
839                 if ( rc == LDAP_SUCCESS && re ) {
840                         Entry *tmp = entry_dup( re );
841                         be_entry_release_r( op, re );
842                         re = tmp;
843                         test_f = 1;
844                 }
845         } else {
846         /* Else we have remote, get local */
847                 op->o_bd = tc->db;
848                 rc = overlay_entry_get_ov(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &le, on);
849                 if ( rc == LDAP_SUCCESS && le ) {
850                         re = entry_dup( rs->sr_entry );
851                         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
852                                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
853                                 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
854                         }
855                         if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
856                                 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
857                                 entry_free( rs->sr_entry );
858                         }
859                 } else {
860                         le = NULL;
861                 }
862         }
863
864 /*
865 ** if we got remote and local entry:
866 **      foreach local attr:
867 **              foreach remote attr:
868 **                      if match, remote attr with local attr;
869 **                      if new local, add to list;
870 **      append new local attrs to remote;
871 **
872 */
873
874         if ( re && le ) {
875                 for(ax = le->e_attrs; ax; ax = ax->a_next) {
876                         for(a = re->e_attrs; a; a = a->a_next) {
877                                 if(a->a_desc == ax->a_desc) {
878                                         test_f = 1;
879                                         if(a->a_vals != a->a_nvals)
880                                                 ber_bvarray_free(a->a_nvals);
881                                         ber_bvarray_free(a->a_vals);
882                                         ber_bvarray_dup_x( &a->a_vals, ax->a_vals, NULL );
883                                         if ( ax->a_vals == ax->a_nvals ) {
884                                                 a->a_nvals = a->a_vals;
885                                         } else {
886                                                 ber_bvarray_dup_x( &a->a_nvals, ax->a_nvals, NULL );
887                                         }
888                                         break;
889                                 }
890                         }
891                         if(a) continue;
892                         an = attr_dup(ax);
893                         an->a_next = as;
894                         as = an;
895                 }
896                 /* Dispose of local entry */
897                 if ( tc->step & LCL_SIDE ) {
898                         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
899                                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
900                                 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
901                         }
902                         if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
903                                 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
904                                 entry_free( rs->sr_entry );
905                         }
906                 } else {
907                         overlay_entry_release_ov(op, le, 0, on);
908                 }
909
910                 /* literally append, so locals are always last */
911                 if(as) {
912                         if(re->e_attrs) {
913                                 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
914                                 ax->a_next = as;
915                         } else {
916                                 re->e_attrs = as;
917                         }
918                 }
919                 /* If both filters, save entry for later */
920                 if ( tc->step == (USE_LIST|RMT_SIDE) ) {
921                         tavl_insert( &tc->list, re, entry_dn_cmp, avl_dup_error );
922                         rs->sr_entry = NULL;
923                         rc = 0;
924                 } else {
925                 /* send it now */
926                         rs->sr_entry = re;
927                         rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
928                         if ( test_f ) {
929                                 rc = test_filter( op, rs->sr_entry, tc->orig );
930                                 if ( rc == LDAP_COMPARE_TRUE ) {
931                                         rc = SLAP_CB_CONTINUE;
932                                 } else {
933                                         rc = 0;
934                                 }
935                         } else {
936                                 rc = SLAP_CB_CONTINUE;
937                         }
938                 }
939         } else if ( le ) {
940         /* Only a local entry: remote was deleted
941          * Ought to delete the local too...
942          */
943                 rc = 0;
944         } else if ( tc->step & USE_LIST ) {
945         /* Only a remote entry, but both filters:
946          * Test the complete filter
947          */
948                 rc = test_filter( op, rs->sr_entry, tc->orig );
949                 if ( rc == LDAP_COMPARE_TRUE ) {
950                         rc = SLAP_CB_CONTINUE;
951                 } else {
952                         rc = 0;
953                 }
954         } else {
955         /* Only a remote entry, only remote filter:
956          * just pass thru
957          */
958                 rc = SLAP_CB_CONTINUE;
959         }
960
961         op->o_bd = db;
962
963         if ( rc == SLAP_CB_CONTINUE && tc->slimit >= 0 && rs->sr_nentries >= tc->slimit ) {
964                 return LDAP_SIZELIMIT_EXCEEDED;
965         }
966
967         return rc;
968 }
969
970 /* Dup the filter, excluding invalid elements */
971 static Filter *
972 trans_filter_dup(Operation *op, Filter *f, AttributeName *an)
973 {
974         Filter *n = NULL;
975
976         if ( !f )
977                 return NULL;
978
979         switch( f->f_choice & SLAPD_FILTER_MASK ) {
980         case SLAPD_FILTER_COMPUTED:
981                 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
982                 n->f_choice = f->f_choice;
983                 n->f_result = f->f_result;
984                 n->f_next = NULL;
985                 break;
986
987         case LDAP_FILTER_PRESENT:
988                 if ( ad_inlist( f->f_desc, an )) {
989                         n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
990                         n->f_choice = f->f_choice;
991                         n->f_desc = f->f_desc;
992                         n->f_next = NULL;
993                 }
994                 break;
995
996         case LDAP_FILTER_EQUALITY:
997         case LDAP_FILTER_GE:
998         case LDAP_FILTER_LE:
999         case LDAP_FILTER_APPROX:
1000         case LDAP_FILTER_SUBSTRINGS:
1001         case LDAP_FILTER_EXT:
1002                 if ( !f->f_av_desc || ad_inlist( f->f_av_desc, an )) {
1003                         n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1004                         n->f_choice = f->f_choice;
1005                         n->f_ava = f->f_ava;
1006                         n->f_next = NULL;
1007                 }
1008                 break;
1009
1010         case LDAP_FILTER_AND:
1011         case LDAP_FILTER_OR:
1012         case LDAP_FILTER_NOT: {
1013                 Filter **p;
1014
1015                 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1016                 n->f_choice = f->f_choice;
1017                 n->f_next = NULL;
1018
1019                 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
1020                         *p = trans_filter_dup( op, f, an );
1021                         if ( !*p )
1022                                 continue;
1023                         p = &(*p)->f_next;
1024                 }
1025                 /* nothing valid in this list */
1026                 if ( !n->f_list ) {
1027                         op->o_tmpfree( n, op->o_tmpmemctx );
1028                         return NULL;
1029                 }
1030                 /* Only 1 element in this list */
1031                 if ((n->f_choice & SLAPD_FILTER_MASK) != LDAP_FILTER_NOT &&
1032                         !n->f_list->f_next ) {
1033                         f = n->f_list;
1034                         *n = *f;
1035                         op->o_tmpfree( f, op->o_tmpmemctx );
1036                 }
1037                 break;
1038         }
1039         }
1040         return n;
1041 }
1042
1043 static void
1044 trans_filter_free( Operation *op, Filter *f )
1045 {
1046         Filter *n, *p, *next;
1047
1048         f->f_choice &= SLAPD_FILTER_MASK;
1049
1050         switch( f->f_choice ) {
1051         case LDAP_FILTER_AND:
1052         case LDAP_FILTER_OR:
1053         case LDAP_FILTER_NOT:
1054                 /* Free in reverse order */
1055                 n = NULL;
1056                 for ( p = f->f_list; p; p = next ) {
1057                         next = p->f_next;
1058                         p->f_next = n;
1059                         n = p;
1060                 }
1061                 for ( p = n; p; p = next ) {
1062                         next = p->f_next;
1063                         trans_filter_free( op, p );
1064                 }
1065                 break;
1066         default:
1067                 break;
1068         }
1069         op->o_tmpfree( f, op->o_tmpmemctx );
1070 }
1071
1072 /*
1073 ** translucent_search()
1074 **      search via captive backend;
1075 **      override results with any local data;
1076 **
1077 */
1078
1079 static int translucent_search(Operation *op, SlapReply *rs) {
1080         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1081         translucent_info *ov = on->on_bi.bi_private;
1082         slap_callback cb = { NULL, NULL, NULL, NULL };
1083         trans_ctx tc;
1084         Filter *fl, *fr;
1085         struct berval fbv;
1086         int rc = 0;
1087
1088         Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
1089                 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
1090
1091         if(ov->defer_db_open) {
1092                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1093                         "remote DB not available");
1094                 return(rs->sr_err);
1095         }
1096
1097         fr = ov->remote ? trans_filter_dup( op, op->ors_filter, ov->remote ) : NULL;
1098         fl = ov->local ? trans_filter_dup( op, op->ors_filter, ov->local ) : NULL;
1099         cb.sc_response = (slap_response *) translucent_search_cb;
1100         cb.sc_private = &tc;
1101         cb.sc_next = op->o_callback;
1102
1103         ov->db.be_acl = op->o_bd->be_acl;
1104         tc.db = op->o_bd;
1105         tc.on = on;
1106         tc.orig = op->ors_filter;
1107         tc.list = NULL;
1108         tc.step = 0;
1109         fbv = op->ors_filterstr;
1110
1111         op->o_callback = &cb;
1112
1113         tc.slimit = op->ors_slimit;
1114
1115         if ( fr || !fl ) {
1116                 AttributeName *attrs = op->ors_attrs;
1117                 op->ors_slimit = SLAP_NO_LIMIT;
1118                 op->ors_attrs = NULL;
1119                 op->o_bd = &ov->db;
1120                 tc.step |= RMT_SIDE;
1121                 if ( fl ) {
1122                         tc.step |= USE_LIST;
1123                         op->ors_filter = fr;
1124                         filter2bv_x( op, fr, &op->ors_filterstr );
1125                 }
1126                 rc = ov->db.bd_info->bi_op_search(op, rs);
1127                 op->ors_attrs = attrs;
1128                 op->o_bd = tc.db;
1129                 if ( fl ) {
1130                         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1131                 }
1132         }
1133         if ( fl && !rc ) {
1134                 tc.step |= LCL_SIDE;
1135                 op->ors_filter = fl;
1136                 filter2bv_x( op, fl, &op->ors_filterstr );
1137                 rc = overlay_op_walk( op, rs, op_search, on->on_info, on->on_next );
1138                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1139         }
1140         op->ors_filterstr = fbv;
1141         op->ors_filter = tc.orig;
1142         op->o_callback = cb.sc_next;
1143         rs->sr_attrs = op->ors_attrs;
1144
1145         /* Send out anything remaining on the list and finish */
1146         if ( tc.step & USE_LIST ) {
1147                 if ( tc.list ) {
1148                         Avlnode *av;
1149
1150                         av = tavl_end( tc.list, TAVL_DIR_LEFT );
1151                         while ( av ) {
1152                                 rs->sr_entry = av->avl_data;
1153                                 rc = test_filter( op, rs->sr_entry, op->ors_filter );
1154                                 if ( rc == LDAP_COMPARE_TRUE ) {
1155                                         rs->sr_flags = REP_ENTRY_MUSTBEFREED;
1156                                         rc = send_search_entry( op, rs );
1157                                         if ( rc ) break;
1158                                 } else {
1159                                         entry_free( rs->sr_entry );
1160                                 }
1161                                 av = tavl_next( av, TAVL_DIR_RIGHT );
1162                         }
1163                         tavl_free( tc.list, NULL );
1164                         rs->sr_entry = NULL;
1165                 }
1166                 send_ldap_result( op, rs );
1167         }
1168
1169         op->ors_slimit = tc.slimit;
1170
1171         /* Free in reverse order */
1172         if ( fl )
1173                 trans_filter_free( op, fl );
1174         if ( fr )
1175                 trans_filter_free( op, fr );
1176
1177         return rc;
1178 }
1179
1180
1181 /*
1182 ** translucent_bind()
1183 **      pass bind request to captive backend;
1184 **
1185 */
1186
1187 static int translucent_bind(Operation *op, SlapReply *rs) {
1188         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1189         translucent_info *ov = on->on_bi.bi_private;
1190         BackendDB *db;
1191         slap_callback sc = { 0 }, *save_cb;
1192         int rc;
1193
1194         Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
1195                 op->o_req_dn.bv_val, op->orb_method, 0);
1196
1197         if(ov->defer_db_open) {
1198                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1199                         "remote DB not available");
1200                 return(rs->sr_err);
1201         }
1202
1203         if (ov->bind_local) {
1204                 sc.sc_response = slap_null_cb;
1205                 save_cb = op->o_callback;
1206                 op->o_callback = &sc;
1207         }
1208
1209         db = op->o_bd;
1210         op->o_bd = &ov->db;
1211         ov->db.be_acl = op->o_bd->be_acl;
1212         rc = ov->db.bd_info->bi_op_bind(op, rs);
1213         op->o_bd = db;
1214
1215         if (ov->bind_local) {
1216                 op->o_callback = save_cb;
1217                 if (rc != LDAP_SUCCESS) {
1218                         rc = SLAP_CB_CONTINUE;
1219                 }
1220         }
1221
1222         return rc;
1223 }
1224
1225 /*
1226 ** translucent_connection_destroy()
1227 **      pass disconnect notification to captive backend;
1228 **
1229 */
1230
1231 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
1232         slap_overinst *on = (slap_overinst *) be->bd_info;
1233         translucent_info *ov = on->on_bi.bi_private;
1234         int rc = 0;
1235
1236         Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
1237
1238         rc = ov->db.bd_info->bi_connection_destroy(&ov->db, conn);
1239
1240         return(rc);
1241 }
1242
1243 /*
1244 ** translucent_db_config()
1245 **      pass config directives to captive backend;
1246 **      parse unrecognized directives ourselves;
1247 **
1248 */
1249
1250 static int translucent_db_config(
1251         BackendDB       *be,
1252         const char      *fname,
1253         int             lineno,
1254         int             argc,
1255         char            **argv
1256 )
1257 {
1258         slap_overinst *on = (slap_overinst *) be->bd_info;
1259         translucent_info *ov = on->on_bi.bi_private;
1260
1261         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_config: %s\n",
1262               argc ? argv[0] : "", 0, 0);
1263
1264         /* Something for the captive database? */
1265         if ( ov->db.bd_info && ov->db.bd_info->bi_db_config )
1266                 return ov->db.bd_info->bi_db_config( &ov->db, fname, lineno,
1267                         argc, argv );
1268         return SLAP_CONF_UNKNOWN;
1269 }
1270
1271 /*
1272 ** translucent_db_init()
1273 **      initialize the captive backend;
1274 **
1275 */
1276
1277 static int translucent_db_init(BackendDB *be, ConfigReply *cr) {
1278         slap_overinst *on = (slap_overinst *) be->bd_info;
1279         translucent_info *ov;
1280
1281         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_init\n", 0, 0, 0);
1282
1283         ov = ch_calloc(1, sizeof(translucent_info));
1284         on->on_bi.bi_private = ov;
1285         ov->db = *be;
1286         ov->db.be_private = NULL;
1287         ov->defer_db_open = 1;
1288
1289         if ( !backend_db_init( "ldap", &ov->db, -1, NULL )) {
1290                 Debug( LDAP_DEBUG_CONFIG, "translucent: unable to open captive back-ldap\n", 0, 0, 0);
1291                 return 1;
1292         }
1293         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1294         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
1295
1296         return 0;
1297 }
1298
1299 /*
1300 ** translucent_db_open()
1301 **      if the captive backend has an open() method, call it;
1302 **
1303 */
1304
1305 static int translucent_db_open(BackendDB *be, ConfigReply *cr) {
1306         slap_overinst *on = (slap_overinst *) be->bd_info;
1307         translucent_info *ov = on->on_bi.bi_private;
1308         int rc;
1309
1310         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_open\n", 0, 0, 0);
1311
1312         /* need to inherit something from the original database... */
1313         ov->db.be_def_limit = be->be_def_limit;
1314         ov->db.be_limits = be->be_limits;
1315         ov->db.be_acl = be->be_acl;
1316         ov->db.be_dfltaccess = be->be_dfltaccess;
1317
1318         if ( ov->defer_db_open )
1319                 return 0;
1320
1321         rc = backend_startup_one( &ov->db, cr );
1322
1323         if(rc) Debug(LDAP_DEBUG_TRACE,
1324                 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
1325
1326         return(rc);
1327 }
1328
1329 /*
1330 ** translucent_db_close()
1331 **      if the captive backend has a close() method, call it
1332 **
1333 */
1334
1335 static int
1336 translucent_db_close( BackendDB *be, ConfigReply *cr )
1337 {
1338         slap_overinst *on = (slap_overinst *) be->bd_info;
1339         translucent_info *ov = on->on_bi.bi_private;
1340         int rc = 0;
1341
1342         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
1343
1344         if ( ov && ov->db.bd_info && ov->db.bd_info->bi_db_close ) {
1345                 rc = ov->db.bd_info->bi_db_close(&ov->db, NULL);
1346         }
1347
1348         return(rc);
1349 }
1350
1351 /*
1352 ** translucent_db_destroy()
1353 **      if the captive backend has a db_destroy() method, call it;
1354 **      free any config data
1355 **
1356 */
1357
1358 static int
1359 translucent_db_destroy( BackendDB *be, ConfigReply *cr )
1360 {
1361         slap_overinst *on = (slap_overinst *) be->bd_info;
1362         translucent_info *ov = on->on_bi.bi_private;
1363         int rc = 0;
1364
1365         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_destroy\n", 0, 0, 0);
1366
1367         if ( ov ) {
1368                 if ( ov->remote )
1369                         anlist_free( ov->remote, 1, NULL );
1370                 if ( ov->local )
1371                         anlist_free( ov->local, 1, NULL );
1372                 if ( ov->db.be_private != NULL ) {
1373                         backend_stopdown_one( &ov->db );
1374                 }
1375
1376                 ch_free(ov);
1377                 on->on_bi.bi_private = NULL;
1378         }
1379
1380         return(rc);
1381 }
1382
1383 /*
1384 ** translucent_initialize()
1385 **      initialize the slap_overinst with our entry points;
1386 **
1387 */
1388
1389 int translucent_initialize() {
1390
1391         int rc;
1392
1393         Debug(LDAP_DEBUG_TRACE, "==> translucent_initialize\n", 0, 0, 0);
1394
1395         translucent.on_bi.bi_type       = "translucent";
1396         translucent.on_bi.bi_db_init    = translucent_db_init;
1397         translucent.on_bi.bi_db_config  = translucent_db_config;
1398         translucent.on_bi.bi_db_open    = translucent_db_open;
1399         translucent.on_bi.bi_db_close   = translucent_db_close;
1400         translucent.on_bi.bi_db_destroy = translucent_db_destroy;
1401         translucent.on_bi.bi_op_bind    = translucent_bind;
1402         translucent.on_bi.bi_op_add     = translucent_add;
1403         translucent.on_bi.bi_op_modify  = translucent_modify;
1404         translucent.on_bi.bi_op_modrdn  = translucent_modrdn;
1405         translucent.on_bi.bi_op_delete  = translucent_delete;
1406         translucent.on_bi.bi_op_search  = translucent_search;
1407         translucent.on_bi.bi_op_compare = translucent_compare;
1408         translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
1409         translucent.on_bi.bi_extended   = translucent_exop;
1410
1411         translucent.on_bi.bi_cf_ocs = translucentocs;
1412         rc = config_register_schema ( translucentcfg, translucentocs );
1413         if ( rc ) return rc;
1414
1415         return(overlay_register(&translucent));
1416 }
1417
1418 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
1419 int init_module(int argc, char *argv[]) {
1420         return translucent_initialize();
1421 }
1422 #endif
1423
1424 #endif /* SLAPD_OVER_TRANSLUCENT */