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