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