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