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