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