]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/translucent.c
Don't test filter before saving, need the copy for local lookup
[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         Avlnode *list;
652         int step;
653 } trans_ctx;
654
655 static int translucent_search_cb(Operation *op, SlapReply *rs) {
656         trans_ctx *tc;
657         BackendDB *db;
658         slap_overinst *on;
659         translucent_info *ov;
660         Entry *le, *re;
661         Attribute *a, *ax, *an, *as = NULL;
662         int rc;
663
664         tc = op->o_callback->sc_private;
665
666         /* Don't let the op complete while we're gathering data */
667         if ( rs->sr_type == REP_RESULT && ( tc->step & USE_LIST ))
668                 return 0;
669
670         if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
671                 return(SLAP_CB_CONTINUE);
672
673         Debug(LDAP_DEBUG_TRACE, "==> translucent_search_cb: %s\n",
674                 rs->sr_entry->e_name.bv_val, 0, 0);
675
676         on = tc->on;
677         ov = on->on_bi.bi_private;
678
679         db = op->o_bd;
680         re = NULL;
681
682         /* If we have local, get remote */
683         if ( tc->step & LCL_SIDE ) {
684                 le = rs->sr_entry;
685                 /* If entry is already on list, use it */
686                 if ( tc->step & USE_LIST ) {
687                         re = tavl_delete( &tc->list, le, entry_dn_cmp );
688                         if ( re ) {
689                                 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
690                                         rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
691                                         be_entry_release_r( op, rs->sr_entry );
692                                 }
693                                 if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
694                                         rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
695                                         entry_free( rs->sr_entry );
696                                 }
697                                 rc = test_filter( op, re, tc->orig );
698                                 if ( rc == LDAP_COMPARE_TRUE ) {
699                                         rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
700                                         rs->sr_entry = re;
701                                         return SLAP_CB_CONTINUE;
702                                 } else {
703                                         entry_free( re );
704                                         rs->sr_entry = NULL;
705                                         return 0;
706                                 }
707                         }
708                 }
709                 op->o_bd = &ov->db;
710                 rc = be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &re );
711                 if ( rc == LDAP_SUCCESS && re ) {
712                         Entry *tmp = entry_dup( re );
713                         be_entry_release_r( op, re );
714                         re = tmp;
715                 }
716         } else {
717         /* Else we have remote, get local */
718                 op->o_bd = tc->db;
719                 rc = overlay_entry_get_ov(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &le, on);
720                 if ( rc == LDAP_SUCCESS && le ) {
721                         re = entry_dup( rs->sr_entry );
722                         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
723                                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
724                                 be_entry_release_r( op, rs->sr_entry );
725                         }
726                         if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
727                                 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
728                                 entry_free( rs->sr_entry );
729                         }
730                 } else {
731                         le = NULL;
732                 }
733         }
734
735 /*
736 ** if we got remote and local entry:
737 **      foreach local attr:
738 **              foreach remote attr:
739 **                      if match, remote attr with local attr;
740 **                      if new local, add to list;
741 **      append new local attrs to remote;
742 **
743 */
744
745         if ( re && le ) {
746                 for(ax = le->e_attrs; ax; ax = ax->a_next) {
747                         for(a = re->e_attrs; a; a = a->a_next) {
748                                 if(a->a_desc == ax->a_desc) {
749                                         if(a->a_vals != a->a_nvals)
750                                                 ber_bvarray_free(a->a_nvals);
751                                         ber_bvarray_free(a->a_vals);
752                                         a->a_vals = dup_bervarray(ax->a_vals);
753                                         a->a_nvals = (ax->a_vals == ax->a_nvals) ?
754                                                 a->a_vals : dup_bervarray(ax->a_nvals);
755                                         break;
756                                 }
757                         }
758                         if(a) continue;
759                         an = attr_dup(ax);
760                         an->a_next = as;
761                         as = an;
762                 }
763                 /* Dispose of local entry */
764                 if ( tc->step & LCL_SIDE ) {
765                         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
766                                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
767                                 be_entry_release_r( op, rs->sr_entry );
768                         }
769                         if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
770                                 rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
771                                 entry_free( rs->sr_entry );
772                         }
773                 } else {
774                         overlay_entry_release_ov(op, le, 0, on);
775                 }
776
777                 /* literally append, so locals are always last */
778                 if(as) {
779                         if(re->e_attrs) {
780                                 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
781                                 ax->a_next = as;
782                         } else {
783                                 re->e_attrs = as;
784                         }
785                 }
786                 /* If both filters, save entry for later */
787                 if ( tc->step == (USE_LIST|RMT_SIDE) ) {
788                         tavl_insert( &tc->list, re, entry_dn_cmp, avl_dup_error );
789                         rs->sr_entry = NULL;
790                         rc = 0;
791                 } else {
792                 /* send it now */
793                         rs->sr_entry = re;
794                         rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
795                         rc = SLAP_CB_CONTINUE;
796                 }
797         } else if ( le ) {
798         /* Only a local entry: remote was deleted
799          * Ought to delete the local too...
800          */
801                 rc = 0;
802         } else if ( tc->step & USE_LIST ) {
803         /* Only a remote entry, but both filters:
804          * Test the complete filter
805          */
806                 rc = test_filter( op, rs->sr_entry, tc->orig );
807                 if ( rc == LDAP_COMPARE_TRUE ) {
808                         rc = SLAP_CB_CONTINUE;
809                 } else {
810                         rc = 0;
811                 }
812         } else {
813         /* Only a remote entry, only remote filter:
814          * just pass thru
815          */
816                 rc = SLAP_CB_CONTINUE;
817         }
818
819         op->o_bd = db;
820         return rc;
821 }
822
823 /* Dup the filter, excluding invalid elements */
824 static Filter *
825 trans_filter_dup(Operation *op, Filter *f, AttributeName *an)
826 {
827         Filter *n = NULL;
828
829         if ( !f )
830                 return NULL;
831
832         switch( f->f_choice & SLAPD_FILTER_MASK ) {
833         case SLAPD_FILTER_COMPUTED:
834                 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
835                 n->f_choice = f->f_choice;
836                 n->f_result = f->f_result;
837                 n->f_next = NULL;
838                 break;
839
840         case LDAP_FILTER_PRESENT:
841                 if ( ad_inlist( f->f_desc, an )) {
842                         n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
843                         n->f_choice = f->f_choice;
844                         n->f_desc = f->f_desc;
845                         n->f_next = NULL;
846                 }
847                 break;
848
849         case LDAP_FILTER_EQUALITY:
850         case LDAP_FILTER_GE:
851         case LDAP_FILTER_LE:
852         case LDAP_FILTER_APPROX:
853         case LDAP_FILTER_SUBSTRINGS:
854         case LDAP_FILTER_EXT:
855                 if ( !f->f_av_desc || ad_inlist( f->f_av_desc, an )) {
856                         n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
857                         n->f_choice = f->f_choice;
858                         n->f_ava = f->f_ava;
859                         n->f_next = NULL;
860                 }
861                 break;
862
863         case LDAP_FILTER_AND:
864         case LDAP_FILTER_OR:
865         case LDAP_FILTER_NOT: {
866                 Filter **p;
867
868                 n = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
869                 n->f_choice = f->f_choice;
870                 n->f_next = NULL;
871
872                 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
873                         *p = trans_filter_dup( op, f, an );
874                         if ( !*p )
875                                 continue;
876                         p = &(*p)->f_next;
877                 }
878                 /* nothing valid in this list */
879                 if ( !n->f_list ) {
880                         op->o_tmpfree( n, op->o_tmpmemctx );
881                         return NULL;
882                 }
883                 /* Only 1 element in this list */
884                 if ((n->f_choice & SLAPD_FILTER_MASK) != LDAP_FILTER_NOT &&
885                         !n->f_list->f_next ) {
886                         f = n->f_list;
887                         *n = *f;
888                         op->o_tmpfree( f, op->o_tmpmemctx );
889                 }
890                 break;
891         }
892         }
893         return n;
894 }
895
896 static void
897 trans_filter_free( Operation *op, Filter *f )
898 {
899         Filter *n, *p, *next;
900
901         f->f_choice &= SLAPD_FILTER_MASK;
902
903         switch( f->f_choice ) {
904         case LDAP_FILTER_AND:
905         case LDAP_FILTER_OR:
906         case LDAP_FILTER_NOT:
907                 /* Free in reverse order */
908                 n = NULL;
909                 for ( p = f->f_list; p; p = next ) {
910                         next = p->f_next;
911                         p->f_next = n;
912                         n = p;
913                 }
914                 for ( p = n; p; p = next ) {
915                         next = p->f_next;
916                         trans_filter_free( op, p );
917                 }
918                 break;
919         default:
920                 break;
921         }
922         op->o_tmpfree( f, op->o_tmpmemctx );
923 }
924
925 /*
926 ** translucent_search()
927 **      search via captive backend;
928 **      override results with any local data;
929 **
930 */
931
932 static int translucent_search(Operation *op, SlapReply *rs) {
933         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
934         translucent_info *ov = on->on_bi.bi_private;
935         slap_callback cb = { NULL, NULL, NULL, NULL };
936         trans_ctx tc;
937         Filter *fl, *fr;
938         struct berval fbv;
939         int rc = 0;
940
941         Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
942                 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
943
944         if(ov->defer_db_open) {
945                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
946                         "remote DB not available");
947                 return(rs->sr_err);
948         }
949
950         fr = ov->remote ? trans_filter_dup( op, op->ors_filter, ov->remote ) : NULL;
951         fl = ov->local ? trans_filter_dup( op, op->ors_filter, ov->local ) : NULL;
952         cb.sc_response = (slap_response *) translucent_search_cb;
953         cb.sc_private = &tc;
954         cb.sc_next = op->o_callback;
955
956         tc.db = op->o_bd;
957         tc.on = on;
958         tc.orig = op->ors_filter;
959         tc.list = NULL;
960         tc.step = 0;
961         fbv = op->ors_filterstr;
962
963         op->o_callback = &cb;
964
965         if ( fr || !fl ) {
966                 op->o_bd = &ov->db;
967                 tc.step |= RMT_SIDE;
968                 if ( fl ) {
969                         tc.step |= USE_LIST;
970                         op->ors_filter = fr;
971                         filter2bv_x( op, fr, &op->ors_filterstr );
972                 }
973                 rc = ov->db.bd_info->bi_op_search(op, rs);
974                 op->o_bd = tc.db;
975                 if ( fl ) {
976                         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
977                 }
978         }
979         if ( fl && !rc ) {
980                 tc.step |= LCL_SIDE;
981                 op->ors_filter = fl;
982                 filter2bv_x( op, fl, &op->ors_filterstr );
983                 rc = overlay_op_walk( op, rs, op_search, on->on_info, on->on_next );
984                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
985         }
986         op->ors_filterstr = fbv;
987         op->ors_filter = tc.orig;
988         op->o_callback = cb.sc_next;
989         /* Send out anything remaining on the list and finish */
990         if ( tc.step & USE_LIST ) {
991                 if ( tc.list ) {
992                         Avlnode *av;
993
994                         av = tavl_end( tc.list, TAVL_DIR_LEFT );
995                         while ( av ) {
996                                 rs->sr_flags = REP_ENTRY_MUSTBEFREED;
997                                 rs->sr_entry = av->avl_data;
998                                 rc = send_search_entry( op, rs );
999                                 if ( rc ) break;
1000                                 av = tavl_next( av, TAVL_DIR_RIGHT );
1001                         }
1002                         tavl_free( tc.list, NULL );
1003                 }
1004                 send_ldap_result( op, rs );
1005         }
1006
1007         /* Free in reverse order */
1008         if ( fl )
1009                 trans_filter_free( op, fl );
1010         if ( fr )
1011                 trans_filter_free( op, fr );
1012
1013         return rc;
1014 }
1015
1016
1017 /*
1018 ** translucent_bind()
1019 **      pass bind request to captive backend;
1020 **
1021 */
1022
1023 static int translucent_bind(Operation *op, SlapReply *rs) {
1024         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1025         translucent_info *ov = on->on_bi.bi_private;
1026         BackendDB *db;
1027         int rc;
1028
1029         Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
1030                 op->o_req_dn.bv_val, op->orb_method, 0);
1031
1032         if(ov->defer_db_open) {
1033                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
1034                         "remote DB not available");
1035                 return(rs->sr_err);
1036         }
1037         db = op->o_bd;
1038         op->o_bd = &ov->db;
1039         rc = ov->db.bd_info->bi_op_bind(op, rs);
1040         op->o_bd = db;
1041         return rc;
1042 }
1043
1044 /*
1045 ** translucent_connection_destroy()
1046 **      pass disconnect notification to captive backend;
1047 **
1048 */
1049
1050 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
1051         slap_overinst *on = (slap_overinst *) be->bd_info;
1052         translucent_info *ov = on->on_bi.bi_private;
1053         int rc = 0;
1054
1055         Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
1056
1057         rc = ov->db.bd_info->bi_connection_destroy(&ov->db, conn);
1058
1059         return(rc);
1060 }
1061
1062 /*
1063 ** translucent_db_config()
1064 **      pass config directives to captive backend;
1065 **      parse unrecognized directives ourselves;
1066 **
1067 */
1068
1069 static int translucent_db_config(
1070         BackendDB       *be,
1071         const char      *fname,
1072         int             lineno,
1073         int             argc,
1074         char            **argv
1075 )
1076 {
1077         slap_overinst *on = (slap_overinst *) be->bd_info;
1078         translucent_info *ov = on->on_bi.bi_private;
1079
1080         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_config: %s\n",
1081               argc ? argv[0] : "", 0, 0);
1082
1083         /* Something for the captive database? */
1084         if ( ov->db.bd_info && ov->db.bd_info->bi_db_config )
1085                 return ov->db.bd_info->bi_db_config( &ov->db, fname, lineno,
1086                         argc, argv );
1087         return SLAP_CONF_UNKNOWN;
1088 }
1089
1090 /*
1091 ** translucent_db_init()
1092 **      initialize the captive backend;
1093 **
1094 */
1095
1096 static int translucent_db_init(BackendDB *be, ConfigReply *cr) {
1097         slap_overinst *on = (slap_overinst *) be->bd_info;
1098         translucent_info *ov;
1099
1100         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_init\n", 0, 0, 0);
1101
1102         ov = ch_calloc(1, sizeof(translucent_info));
1103         on->on_bi.bi_private = ov;
1104         ov->db = *be;
1105         ov->db.be_private = NULL;
1106         ov->db.be_pcl_mutexp = &ov->db.be_pcl_mutex;
1107         ov->defer_db_open = 1;
1108
1109         if ( !backend_db_init( "ldap", &ov->db, -1, NULL )) {
1110                 Debug( LDAP_DEBUG_CONFIG, "translucent: unable to open captive back-ldap\n", 0, 0, 0);
1111                 return 1;
1112         }
1113         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
1114         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
1115
1116         return 0;
1117 }
1118
1119 /*
1120 ** translucent_db_open()
1121 **      if the captive backend has an open() method, call it;
1122 **
1123 */
1124
1125 static int translucent_db_open(BackendDB *be, ConfigReply *cr) {
1126         slap_overinst *on = (slap_overinst *) be->bd_info;
1127         translucent_info *ov = on->on_bi.bi_private;
1128         int rc;
1129
1130         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_open\n", 0, 0, 0);
1131
1132         /* need to inherit something from the original database... */
1133         ov->db.be_def_limit = be->be_def_limit;
1134         ov->db.be_limits = be->be_limits;
1135         ov->db.be_acl = be->be_acl;
1136         ov->db.be_dfltaccess = be->be_dfltaccess;
1137
1138         if ( ov->defer_db_open )
1139                 return 0;
1140
1141         rc = backend_startup_one( &ov->db, NULL );
1142
1143         if(rc) Debug(LDAP_DEBUG_TRACE,
1144                 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
1145
1146         return(rc);
1147 }
1148
1149 /*
1150 ** translucent_db_close()
1151 **      if the captive backend has a close() method, call it
1152 **
1153 */
1154
1155 static int
1156 translucent_db_close( BackendDB *be, ConfigReply *cr )
1157 {
1158         slap_overinst *on = (slap_overinst *) be->bd_info;
1159         translucent_info *ov = on->on_bi.bi_private;
1160         int rc = 0;
1161
1162         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
1163
1164         if ( ov && ov->db.bd_info && ov->db.bd_info->bi_db_close ) {
1165                 rc = ov->db.bd_info->bi_db_close(&ov->db, NULL);
1166         }
1167
1168         return(rc);
1169 }
1170
1171 /*
1172 ** translucent_db_destroy()
1173 **      if the captive backend has a db_destroy() method, call it;
1174 **      free any config data
1175 **
1176 */
1177
1178 static int
1179 translucent_db_destroy( BackendDB *be, ConfigReply *cr )
1180 {
1181         slap_overinst *on = (slap_overinst *) be->bd_info;
1182         translucent_info *ov = on->on_bi.bi_private;
1183         int rc = 0;
1184
1185         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_destroy\n", 0, 0, 0);
1186
1187         if ( ov ) {
1188                 if ( ov->remote )
1189                         anlist_free( ov->remote, 1, NULL );
1190                 if ( ov->local )
1191                         anlist_free( ov->local, 1, NULL );
1192                 if ( ov->db.be_private != NULL ) {
1193                         backend_stopdown_one( &ov->db );
1194                 }
1195
1196                 ch_free(ov);
1197                 on->on_bi.bi_private = NULL;
1198         }
1199
1200         return(rc);
1201 }
1202
1203 /*
1204 ** translucent_initialize()
1205 **      initialize the slap_overinst with our entry points;
1206 **
1207 */
1208
1209 int translucent_initialize() {
1210
1211         int rc;
1212
1213         Debug(LDAP_DEBUG_TRACE, "==> translucent_initialize\n", 0, 0, 0);
1214
1215         translucent.on_bi.bi_type       = "translucent";
1216         translucent.on_bi.bi_db_init    = translucent_db_init;
1217         translucent.on_bi.bi_db_config  = translucent_db_config;
1218         translucent.on_bi.bi_db_open    = translucent_db_open;
1219         translucent.on_bi.bi_db_close   = translucent_db_close;
1220         translucent.on_bi.bi_db_destroy = translucent_db_destroy;
1221         translucent.on_bi.bi_op_bind    = translucent_bind;
1222         translucent.on_bi.bi_op_add     = translucent_add;
1223         translucent.on_bi.bi_op_modify  = translucent_modify;
1224         translucent.on_bi.bi_op_modrdn  = translucent_modrdn;
1225         translucent.on_bi.bi_op_delete  = translucent_delete;
1226         translucent.on_bi.bi_op_search  = translucent_search;
1227         translucent.on_bi.bi_op_compare = translucent_compare;
1228         translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
1229
1230         translucent.on_bi.bi_cf_ocs = translucentocs;
1231         rc = config_register_schema ( translucentcfg, translucentocs );
1232         if ( rc ) return rc;
1233
1234         return(overlay_register(&translucent));
1235 }
1236
1237 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
1238 int init_module(int argc, char *argv[]) {
1239         return translucent_initialize();
1240 }
1241 #endif
1242
1243 #endif /* SLAPD_OVER_TRANSLUCENT */