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