]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/translucent.c
ITS#5173 from HEAD
[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         int strict;
40         int no_glue;
41         int defer_db_open;
42 } translucent_info;
43
44 static ConfigLDAPadd translucent_ldadd;
45 static ConfigCfAdd translucent_cfadd;
46
47 static ConfigTable translucentcfg[] = {
48         { "translucent_strict", "on|off", 1, 2, 0,
49           ARG_ON_OFF|ARG_OFFSET,
50           (void *)offsetof(translucent_info, strict),
51           "( OLcfgOvAt:14.1 NAME 'olcTranslucentStrict' "
52           "DESC 'Reveal attribute deletion constraint violations' "
53           "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
54         { "translucent_no_glue", "on|off", 1, 2, 0,
55           ARG_ON_OFF|ARG_OFFSET,
56           (void *)offsetof(translucent_info, no_glue),
57           "( OLcfgOvAt:14.2 NAME 'olcTranslucentNoGlue' "
58           "DESC 'Disable automatic glue records for ADD and MODRDN' "
59           "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
60         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
61 };
62
63 static ConfigTable transdummy[] = {
64         { "", "", 0, 0, 0, ARG_IGNORED,
65                 NULL, "( OLcfgGlAt:13 NAME 'olcDatabase' "
66                         "DESC 'The backend type for a database instance' "
67                         "SUP olcBackend SINGLE-VALUE X-ORDERED 'SIBLINGS' )", NULL, NULL },
68         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
69 };
70
71 static ConfigOCs translucentocs[] = {
72         { "( OLcfgOvOc:14.1 "
73           "NAME 'olcTranslucentConfig' "
74           "DESC 'Translucent configuration' "
75           "SUP olcOverlayConfig "
76           "MAY ( olcTranslucentStrict $ olcTranslucentNoGlue ) )",
77           Cft_Overlay, translucentcfg, NULL, translucent_cfadd },
78         { "( OLcfgOvOc:14.2 "
79           "NAME 'olcTranslucentDatabase' "
80           "DESC 'Translucent target database configuration' "
81           "AUXILIARY )", Cft_Misc, transdummy, translucent_ldadd },
82         { NULL, 0, NULL }
83 };
84 /* for translucent_init() */
85
86 static int
87 translucent_ldadd_cleanup( ConfigArgs *ca )
88 {
89         slap_overinst *on = ca->private;
90         translucent_info *ov = on->on_bi.bi_private;
91
92         ov->defer_db_open = 0;
93         return backend_startup_one( ca->be, &ca->reply );
94 }
95
96 static int
97 translucent_ldadd( CfEntryInfo *cei, Entry *e, ConfigArgs *ca )
98 {
99         slap_overinst *on;
100         translucent_info *ov;
101
102         Debug(LDAP_DEBUG_TRACE, "==> translucent_ldadd\n", 0, 0, 0);
103
104         if ( cei->ce_type != Cft_Overlay || !cei->ce_bi ||
105              cei->ce_bi->bi_cf_ocs != translucentocs )
106                 return LDAP_CONSTRAINT_VIOLATION;
107
108         on = (slap_overinst *)cei->ce_bi;
109         ov = on->on_bi.bi_private;
110         ca->be = &ov->db;
111         ca->private = on;
112         ca->cleanup = translucent_ldadd_cleanup;
113         return LDAP_SUCCESS;
114 }
115
116 static int
117 translucent_cfadd( Operation *op, SlapReply *rs, Entry *e, ConfigArgs *ca )
118 {
119         CfEntryInfo *cei = e->e_private;
120         slap_overinst *on = (slap_overinst *)cei->ce_bi;
121         translucent_info *ov = on->on_bi.bi_private;
122         struct berval bv;
123
124         Debug(LDAP_DEBUG_TRACE, "==> translucent_cfadd\n", 0, 0, 0);
125
126         /* FIXME: should not hardcode "olcDatabase" here */
127         bv.bv_len = sprintf( ca->cr_msg, "olcDatabase=%s",
128                              ov->db.bd_info->bi_type );
129         bv.bv_val = ca->cr_msg;
130         ca->be = &ov->db;
131         ov->defer_db_open = 0;
132
133         /* We can only create this entry if the database is table-driven
134          */
135         if ( ov->db.bd_info->bi_cf_ocs )
136                 config_build_entry( op, rs, cei, ca, &bv,
137                                     ov->db.bd_info->bi_cf_ocs,
138                                     &translucentocs[1] );
139
140         return 0;
141 }
142
143 static slap_overinst translucent;
144
145 /*
146 ** glue_parent()
147 **      call syncrepl_add_glue() with the parent suffix;
148 **
149 */
150
151 static struct berval glue[] = { BER_BVC("top"), BER_BVC("glue"), BER_BVNULL };
152
153 void glue_parent(Operation *op) {
154         Operation nop = *op;
155         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
156         struct berval ndn = BER_BVNULL;
157         Attribute *a;
158         Entry *e;
159         struct berval   pdn;
160
161         dnParent( &op->o_req_ndn, &pdn );
162         ber_dupbv_x( &ndn, &pdn, op->o_tmpmemctx );
163
164         Debug(LDAP_DEBUG_TRACE, "=> glue_parent: fabricating glue for <%s>\n", ndn.bv_val, 0, 0);
165
166         e = entry_alloc();
167         e->e_id = NOID;
168         ber_dupbv(&e->e_name, &ndn);
169         ber_dupbv(&e->e_nname, &ndn);
170
171         a = attr_alloc( slap_schema.si_ad_objectClass );
172         a->a_numvals = 2;
173         a->a_vals = ch_malloc(sizeof(struct berval) * 3);
174         ber_dupbv(&a->a_vals[0], &glue[0]);
175         ber_dupbv(&a->a_vals[1], &glue[1]);
176         ber_dupbv(&a->a_vals[2], &glue[2]);
177         a->a_nvals = a->a_vals;
178         a->a_next = e->e_attrs;
179         e->e_attrs = a;
180
181         a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
182         a->a_numvals = 1;
183         a->a_vals = ch_malloc(sizeof(struct berval) * 2);
184         ber_dupbv(&a->a_vals[0], &glue[1]);
185         ber_dupbv(&a->a_vals[1], &glue[2]);
186         a->a_nvals = a->a_vals;
187         a->a_next = e->e_attrs;
188         e->e_attrs = a;
189
190         nop.o_req_dn = ndn;
191         nop.o_req_ndn = ndn;
192         nop.ora_e = e;
193
194         nop.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
195         syncrepl_add_glue(&nop, e);
196         nop.o_bd->bd_info = (BackendInfo *) on;
197
198         op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
199
200         return;
201 }
202
203 /*
204 ** dup_bervarray()
205 **      copy a BerVarray;
206 */
207
208 BerVarray dup_bervarray(BerVarray b) {
209         int i, len;
210         BerVarray nb;
211         for(len = 0; b[len].bv_val; len++);
212         nb = ch_malloc((len+1) * sizeof(BerValue));
213         for(i = 0; i < len; i++) ber_dupbv(&nb[i], &b[i]);
214         nb[len].bv_val = NULL;
215         nb[len].bv_len = 0;
216         return(nb);
217 }
218
219 /*
220 ** free_attr_chain()
221 **      free only the Attribute*, not the contents;
222 **
223 */
224 void free_attr_chain(Attribute *b) {
225         Attribute *a;
226         for(a=b; a; a=a->a_next) {
227                 a->a_vals = NULL;
228                 a->a_nvals = NULL;
229         }
230         attrs_free( b );
231         return;
232 }
233
234 /*
235 ** translucent_add()
236 **      if not bound as root, send ACCESS error;
237 **      if glue, glue_parent();
238 **      return CONTINUE;
239 **
240 */
241
242 static int translucent_add(Operation *op, SlapReply *rs) {
243         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
244         translucent_info *ov = on->on_bi.bi_private;
245         Debug(LDAP_DEBUG_TRACE, "==> translucent_add: %s\n",
246                 op->o_req_dn.bv_val, 0, 0);
247         if(!be_isroot(op)) {
248                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
249                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
250                         "user modification of overlay database not permitted");
251                 op->o_bd->bd_info = (BackendInfo *) on;
252                 return(rs->sr_err);
253         }
254         if(!ov->no_glue) glue_parent(op);
255         return(SLAP_CB_CONTINUE);
256 }
257
258 /*
259 ** translucent_modrdn()
260 **      if not bound as root, send ACCESS error;
261 **      if !glue, glue_parent();
262 **      else return CONTINUE;
263 **
264 */
265
266 static int translucent_modrdn(Operation *op, SlapReply *rs) {
267         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
268         translucent_info *ov = on->on_bi.bi_private;
269         Debug(LDAP_DEBUG_TRACE, "==> translucent_modrdn: %s -> %s\n",
270                 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
271         if(!be_isroot(op)) {
272                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
273                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
274                         "user modification of overlay database not permitted");
275                 op->o_bd->bd_info = (BackendInfo *) on;
276                 return(rs->sr_err);
277         }
278         if(!ov->no_glue) glue_parent(op);
279         return(SLAP_CB_CONTINUE);
280 }
281
282 /*
283 ** translucent_delete()
284 **      if not bound as root, send ACCESS error;
285 **      else return CONTINUE;
286 **
287 */
288
289 static int translucent_delete(Operation *op, SlapReply *rs) {
290         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
291         Debug(LDAP_DEBUG_TRACE, "==> translucent_delete: %s\n",
292                 op->o_req_dn.bv_val, 0, 0);
293         if(!be_isroot(op)) {
294                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
295                 send_ldap_error(op, rs, LDAP_INSUFFICIENT_ACCESS,
296                         "user modification of overlay database not permitted");
297                 op->o_bd->bd_info = (BackendInfo *) on;
298                 return(rs->sr_err);
299         }
300         return(SLAP_CB_CONTINUE);
301 }
302
303 static int
304 translucent_tag_cb( Operation *op, SlapReply *rs )
305 {
306         op->o_tag = LDAP_REQ_MODIFY;
307         op->orm_modlist = op->o_callback->sc_private;
308         rs->sr_tag = slap_req2res( op->o_tag );
309
310         return SLAP_CB_CONTINUE;
311 }
312
313 /*
314 ** translucent_modify()
315 **      modify in local backend if exists in both;
316 **      otherwise, add to local backend;
317 **      fail if not defined in captive backend;
318 **
319 */
320
321 static int translucent_modify(Operation *op, SlapReply *rs) {
322         SlapReply nrs = { REP_RESULT };
323         Operation nop = *op;
324
325         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
326         translucent_info *ov = on->on_bi.bi_private;
327         Entry *e = NULL, *re = NULL;
328         Attribute *a, *ax;
329         Modifications *m, **mm;
330         int del, rc, erc = 0;
331         slap_callback cb = { 0 };
332
333         Debug(LDAP_DEBUG_TRACE, "==> translucent_modify: %s\n",
334                 op->o_req_dn.bv_val, 0, 0);
335
336         if(ov->defer_db_open) {
337                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
338                         "remote DB not available");
339                 return(rs->sr_err);
340         }
341 /*
342 ** fetch entry from the captive backend;
343 ** if it did not exist, fail;
344 ** release it, if captive backend supports this;
345 **
346 */
347
348         nop.o_bd = &ov->db;
349         rc = ov->db.bd_info->bi_entry_get_rw(&nop, &nop.o_req_ndn, NULL, NULL, 0, &re);
350         if(rc != LDAP_SUCCESS || re == NULL ) {
351                 send_ldap_error((&nop), rs, LDAP_NO_SUCH_OBJECT,
352                         "attempt to modify nonexistent local record");
353                 return(rs->sr_err);
354         }
355         nop = *op;
356 /*
357 ** fetch entry from local backend;
358 ** if it exists:
359 **      foreach Modification:
360 **          if attr not present in local:
361 **              if Mod == LDAP_MOD_DELETE:
362 **                  if remote attr not present, return NO_SUCH;
363 **                  if remote attr present, drop this Mod;
364 **              else force this Mod to LDAP_MOD_ADD;
365 **      return CONTINUE;
366 **
367 */
368
369         op->o_bd->bd_info = (BackendInfo *) on->on_info;
370         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, NULL, 0, &e);
371         op->o_bd->bd_info = (BackendInfo *) on;
372
373         if(e && rc == LDAP_SUCCESS) {
374                 Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: found local entry\n", 0, 0, 0);
375                 for(mm = &op->orm_modlist; *mm; ) {
376                         m = *mm;
377                         for(a = e->e_attrs; a; a = a->a_next)
378                                 if(a->a_desc == m->sml_desc) break;
379                         if(a) {
380                                 mm = &m->sml_next;
381                                 continue;               /* found local attr */
382                         }
383                         if(m->sml_op == LDAP_MOD_DELETE) {
384                                 for(a = re->e_attrs; a; a = a->a_next)
385                                         if(a->a_desc == m->sml_desc) break;
386                                 /* not found remote attr */
387                                 if(!a) {
388                                         erc = LDAP_NO_SUCH_ATTRIBUTE;
389                                         goto release;
390                                 }
391                                 if(ov->strict) {
392                                         erc = LDAP_CONSTRAINT_VIOLATION;
393                                         goto release;
394                                 }
395                                 Debug(LDAP_DEBUG_TRACE,
396                                         "=> translucent_modify: silently dropping delete: %s\n",
397                                         m->sml_desc->ad_cname.bv_val, 0, 0);
398                                 *mm = m->sml_next;
399                                 m->sml_next = NULL;
400                                 slap_mods_free(m, 1);
401                                 continue;
402                         }
403                         m->sml_op = LDAP_MOD_ADD;
404                         mm = &m->sml_next;
405                 }
406                 erc = SLAP_CB_CONTINUE;
407 release:
408                 if(re) {
409                         if(ov->db.bd_info->bi_entry_release_rw)
410                                 ov->db.bd_info->bi_entry_release_rw(&nop, re, 0);
411                         else
412                                 entry_free(re);
413                 }
414                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
415                 be_entry_release_r(op, e);
416                 op->o_bd->bd_info = (BackendInfo *) on;
417                 if(erc == SLAP_CB_CONTINUE) {
418                         return(erc);
419                 } else if(erc) {
420                         send_ldap_error(op, rs, erc,
421                                 "attempt to delete nonexistent attribute");
422                         return(erc);
423                 }
424         }
425
426         /* don't leak remote entry copy */
427         if(re) {
428                 if(ov->db.bd_info->bi_entry_release_rw)
429                         ov->db.bd_info->bi_entry_release_rw(&nop, re, 0);
430                 else
431                         entry_free(re);
432         }
433 /*
434 ** foreach Modification:
435 **      if MOD_ADD or MOD_REPLACE, add Attribute;
436 ** if no Modifications were suitable:
437 **      if strict, throw CONSTRAINT_VIOLATION;
438 **      else, return early SUCCESS;
439 ** fabricate Entry with new Attribute chain;
440 ** glue_parent() for this Entry;
441 ** call bi_op_add() in local backend;
442 **
443 */
444
445         Debug(LDAP_DEBUG_TRACE, "=> translucent_modify: fabricating local add\n", 0, 0, 0);
446         a = NULL;
447         for(del = 0, ax = NULL, m = op->orm_modlist; m; m = m->sml_next) {
448                 Attribute atmp;
449                 if(((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_ADD) &&
450                    ((m->sml_op & LDAP_MOD_OP) != LDAP_MOD_REPLACE)) {
451                         Debug(LDAP_DEBUG_ANY,
452                                 "=> translucent_modify: silently dropped modification(%d): %s\n",
453                                 m->sml_op, m->sml_desc->ad_cname.bv_val, 0);
454                         if((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) del++;
455                         continue;
456                 }
457                 atmp.a_desc = m->sml_desc;
458                 atmp.a_vals = m->sml_values;
459                 atmp.a_nvals = m->sml_nvalues ? m->sml_nvalues : atmp.a_vals;
460                 atmp.a_numvals = m->sml_numvals;
461                 atmp.a_flags = 0;
462                 a = attr_dup( &atmp );
463                 a->a_next  = ax;
464                 ax = a;
465         }
466
467         if(del && ov->strict) {
468                 attrs_free( a );
469                 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
470                         "attempt to delete attributes from local database");
471                 return(rs->sr_err);
472         }
473
474         if(!ax) {
475                 if(ov->strict) {
476                         send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
477                                 "modification contained other than ADD or REPLACE");
478                         return(rs->sr_err);
479                 }
480                 /* rs->sr_text = "no valid modification found"; */
481                 rs->sr_err = LDAP_SUCCESS;
482                 send_ldap_result(op, rs);
483                 return(rs->sr_err);
484         }
485
486         e = entry_alloc();
487         ber_dupbv( &e->e_name, &op->o_req_dn );
488         ber_dupbv( &e->e_nname, &op->o_req_ndn );
489         e->e_attrs = a;
490
491         nop.o_tag       = LDAP_REQ_ADD;
492         nop.oq_add.rs_e = e;
493
494         glue_parent(&nop);
495
496         cb.sc_response = translucent_tag_cb;
497         cb.sc_private = op->orm_modlist;
498         cb.sc_next = nop.o_callback;
499         nop.o_callback = &cb;
500         rc = on->on_info->oi_orig->bi_op_add(&nop, &nrs);
501         if ( nop.ora_e == e )
502                 entry_free( e );
503
504         return(rc);
505 }
506
507 static int translucent_compare(Operation *op, SlapReply *rs) {
508         Operation nop = *op;
509         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
510         translucent_info *ov = on->on_bi.bi_private;
511         AttributeAssertion *ava = op->orc_ava;
512         Entry *e;
513         int rc;
514
515         Debug(LDAP_DEBUG_TRACE, "==> translucent_compare: <%s> %s:%s\n",
516                 op->o_req_dn.bv_val, ava->aa_desc->ad_cname.bv_val, ava->aa_value.bv_val);
517
518 /*
519 ** if the local backend has an entry for this attribute:
520 **      CONTINUE and let it do the compare;
521 **
522 */
523         op->o_bd->bd_info = (BackendInfo *) on->on_info;
524         rc = be_entry_get_rw(op, &op->o_req_ndn, NULL, ava->aa_desc, 0, &e);
525         if(e && rc == LDAP_SUCCESS) {
526                 be_entry_release_r(op, e);
527                 op->o_bd->bd_info = (BackendInfo *) on;
528                 return(SLAP_CB_CONTINUE);
529         }
530         op->o_bd->bd_info = (BackendInfo *) on;
531
532         if(ov->defer_db_open) {
533                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
534                         "remote DB not available");
535                 return(rs->sr_err);
536         }
537 /*
538 ** call compare() in the captive backend;
539 ** return the result;
540 **
541 */
542         nop.o_bd = &ov->db;
543         nop.o_callback = NULL;
544         rc = ov->db.bd_info->bi_op_compare(&nop, rs);
545
546         return(rc);
547 }
548
549 /*
550 ** translucent_search_cb()
551 **      merge local data with the search result
552 **
553 */
554
555 static int translucent_search_cb(Operation *op, SlapReply *rs) {
556         slap_overinst *on;
557         Entry *e, *re = NULL;
558         Attribute *a, *ax, *an, *as = NULL;
559         Operation * original_op, local_op;
560         int rc;
561
562         if(!op || !rs || rs->sr_type != REP_SEARCH || !rs->sr_entry)
563                 return(SLAP_CB_CONTINUE);
564
565         Debug(LDAP_DEBUG_TRACE, "==> translucent_search_cb: %s\n",
566                 rs->sr_entry->e_name.bv_val, 0, 0);
567
568         original_op = op->o_callback->sc_private;
569         on = (slap_overinst *) original_op->o_bd->bd_info;
570         local_op = *original_op;
571
572         local_op.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
573         rc = be_entry_get_rw(&local_op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e);
574         local_op.o_bd->bd_info = (BackendInfo *) on;
575
576 /*
577 ** if we got an entry from local backend:
578 **      make a copy of this search result;
579 **      foreach local attr:
580 **              foreach search result attr:
581 **                      if match, result attr with local attr;
582 **                      if new local, add to list;
583 **      append new local attrs to search result;
584 **
585 */
586
587         if(e && rc == LDAP_SUCCESS) {
588                 re = entry_dup(rs->sr_entry);
589                 for(ax = e->e_attrs; ax; ax = ax->a_next) {
590 #if 0
591                         if(is_at_operational(ax->a_desc->ad_type)) continue;
592 #endif
593                         for(a = re->e_attrs; a; a = a->a_next) {
594                                 if(a->a_desc == ax->a_desc) {
595                                         if(a->a_vals != a->a_nvals)
596                                                 ber_bvarray_free(a->a_nvals);
597                                         ber_bvarray_free(a->a_vals);
598                                         a->a_vals = dup_bervarray(ax->a_vals);
599                                         a->a_nvals = (ax->a_vals == ax->a_nvals) ?
600                                                 a->a_vals : dup_bervarray(ax->a_nvals);
601                                         break;
602                                 }
603                         }
604                         if(a) continue;
605                         an = attr_dup(ax);
606                         an->a_next = as;
607                         as = an;
608                 }
609                 local_op.o_bd->bd_info = (BackendInfo *) on->on_info->oi_orig;
610                 be_entry_release_r(&local_op, e);
611                 local_op.o_bd->bd_info = (BackendInfo *) on;
612
613                 /* literally append, so locals are always last */
614                 if(as) {
615                         if(re->e_attrs) {
616                                 for(ax = re->e_attrs; ax->a_next; ax = ax->a_next);
617                                 ax->a_next = as;
618                         } else {
619                                 re->e_attrs = as;
620                         }
621                 }
622                 rs->sr_entry = re;
623                 rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
624         }
625
626         return(SLAP_CB_CONTINUE);
627 }
628
629 /*
630 ** translucent_search()
631 **      search via captive backend;
632 **      override results with any local data;
633 **
634 */
635
636 static int translucent_search(Operation *op, SlapReply *rs) {
637         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
638         Operation nop = *op;
639         translucent_info *ov = on->on_bi.bi_private;
640         slap_callback cb = { NULL, NULL, NULL, NULL };
641
642         Debug(LDAP_DEBUG_TRACE, "==> translucent_search: <%s> %s\n",
643                 op->o_req_dn.bv_val, op->ors_filterstr.bv_val, 0);
644
645         if(ov->defer_db_open) {
646                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
647                         "remote DB not available");
648                 return(rs->sr_err);
649         }
650         cb.sc_response = (slap_response *) translucent_search_cb;
651         cb.sc_private = op;
652         cb.sc_next = nop.o_callback;
653
654         nop.o_callback = &cb;
655         nop.o_bd = &ov->db;
656         return (ov->db.bd_info->bi_op_search(&nop, rs));
657 }
658
659
660 /*
661 ** translucent_bind()
662 **      pass bind request to captive backend;
663 **
664 */
665
666 static int translucent_bind(Operation *op, SlapReply *rs) {
667         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
668         Operation nop = *op;
669         translucent_info *ov = on->on_bi.bi_private;
670
671         Debug(LDAP_DEBUG_TRACE, "translucent_bind: <%s> method %d\n",
672                 op->o_req_dn.bv_val, op->orb_method, 0);
673
674         if(ov->defer_db_open) {
675                 send_ldap_error(op, rs, LDAP_UNAVAILABLE,
676                         "remote DB not available");
677                 return(rs->sr_err);
678         }
679         nop.o_bd = &ov->db;
680         return (ov->db.bd_info->bi_op_bind(&nop, rs));
681 }
682
683 /*
684 ** translucent_connection_destroy()
685 **      pass disconnect notification to captive backend;
686 **
687 */
688
689 static int translucent_connection_destroy(BackendDB *be, Connection *conn) {
690         slap_overinst *on = (slap_overinst *) be->bd_info;
691         translucent_info *ov = on->on_bi.bi_private;
692         int rc = 0;
693
694         Debug(LDAP_DEBUG_TRACE, "translucent_connection_destroy\n", 0, 0, 0);
695
696         rc = ov->db.bd_info->bi_connection_destroy(&ov->db, conn);
697
698         return(rc);
699 }
700
701 /*
702 ** translucent_db_config()
703 **      pass config directives to captive backend;
704 **      parse unrecognized directives ourselves;
705 **
706 */
707
708 static int translucent_db_config(
709         BackendDB       *be,
710         const char      *fname,
711         int             lineno,
712         int             argc,
713         char            **argv
714 )
715 {
716         slap_overinst *on = (slap_overinst *) be->bd_info;
717         translucent_info *ov = on->on_bi.bi_private;
718
719         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_config: %s\n",
720               argc ? argv[0] : "", 0, 0);
721
722         /* Something for the captive database? */
723         if ( ov->db.bd_info && ov->db.bd_info->bi_db_config )
724                 return ov->db.bd_info->bi_db_config( &ov->db, fname, lineno,
725                         argc, argv );
726         return SLAP_CONF_UNKNOWN;
727 }
728
729 /*
730 ** translucent_db_init()
731 **      initialize the captive backend;
732 **
733 */
734
735 static int translucent_db_init(BackendDB *be, ConfigReply *cr) {
736         slap_overinst *on = (slap_overinst *) be->bd_info;
737         translucent_info *ov;
738
739         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_init\n", 0, 0, 0);
740
741         ov = ch_calloc(1, sizeof(translucent_info));
742         on->on_bi.bi_private = ov;
743         ov->db = *be;
744         ov->db.be_private = NULL;
745         ov->db.be_pcl_mutexp = &ov->db.be_pcl_mutex;
746         ov->defer_db_open = 1;
747
748         if ( !backend_db_init( "ldap", &ov->db, -1, NULL )) {
749                 Debug( LDAP_DEBUG_CONFIG, "translucent: unable to open captive back-ldap\n", 0, 0, 0);
750                 return 1;
751         }
752         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
753         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
754
755         return 0;
756 }
757
758 /*
759 ** translucent_db_open()
760 **      if the captive backend has an open() method, call it;
761 **
762 */
763
764 static int translucent_db_open(BackendDB *be, ConfigReply *cr) {
765         slap_overinst *on = (slap_overinst *) be->bd_info;
766         translucent_info *ov = on->on_bi.bi_private;
767         int rc;
768
769         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_open\n", 0, 0, 0);
770
771         /* need to inherit something from the original database... */
772         ov->db.be_def_limit = be->be_def_limit;
773         ov->db.be_limits = be->be_limits;
774         ov->db.be_acl = be->be_acl;
775         ov->db.be_dfltaccess = be->be_dfltaccess;
776
777         if ( ov->defer_db_open )
778                 return 0;
779
780         rc = backend_startup_one( &ov->db, NULL );
781
782         if(rc) Debug(LDAP_DEBUG_TRACE,
783                 "translucent: bi_db_open() returned error %d\n", rc, 0, 0);
784
785         return(rc);
786 }
787
788 /*
789 ** translucent_db_close()
790 **      if the captive backend has a close() method, call it;
791 **      free any config data;
792 **
793 */
794
795 static int
796 translucent_db_close( BackendDB *be, ConfigReply *cr )
797 {
798         slap_overinst *on = (slap_overinst *) be->bd_info;
799         translucent_info *ov = on->on_bi.bi_private;
800         int rc = 0;
801
802         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_close\n", 0, 0, 0);
803
804         if ( ov && ov->db.bd_info && ov->db.bd_info->bi_db_close ) {
805                 rc = ov->db.bd_info->bi_db_close(&ov->db, NULL);
806         }
807
808         return(rc);
809 }
810
811 /*
812 ** translucent_db_destroy()
813 **      if the captive backend has a db_destroy() method, call it
814 **
815 */
816
817 static int
818 translucent_db_destroy( BackendDB *be, ConfigReply *cr )
819 {
820         slap_overinst *on = (slap_overinst *) be->bd_info;
821         translucent_info *ov = on->on_bi.bi_private;
822         int rc = 0;
823
824         Debug(LDAP_DEBUG_TRACE, "==> translucent_db_destroy\n", 0, 0, 0);
825
826         if ( ov ) {
827                 if ( ov->db.be_private != NULL ) {
828                         backend_stopdown_one( &ov->db );
829                 }
830
831                 ch_free(ov);
832                 on->on_bi.bi_private = NULL;
833         }
834
835         return(rc);
836 }
837
838 /*
839 ** translucent_initialize()
840 **      initialize the slap_overinst with our entry points;
841 **
842 */
843
844 int translucent_initialize() {
845
846         int rc;
847
848         Debug(LDAP_DEBUG_TRACE, "==> translucent_initialize\n", 0, 0, 0);
849
850         translucent.on_bi.bi_type       = "translucent";
851         translucent.on_bi.bi_db_init    = translucent_db_init;
852         translucent.on_bi.bi_db_config  = translucent_db_config;
853         translucent.on_bi.bi_db_open    = translucent_db_open;
854         translucent.on_bi.bi_db_close   = translucent_db_close;
855         translucent.on_bi.bi_db_destroy = translucent_db_destroy;
856         translucent.on_bi.bi_op_bind    = translucent_bind;
857         translucent.on_bi.bi_op_add     = translucent_add;
858         translucent.on_bi.bi_op_modify  = translucent_modify;
859         translucent.on_bi.bi_op_modrdn  = translucent_modrdn;
860         translucent.on_bi.bi_op_delete  = translucent_delete;
861         translucent.on_bi.bi_op_search  = translucent_search;
862         translucent.on_bi.bi_op_compare = translucent_compare;
863         translucent.on_bi.bi_connection_destroy = translucent_connection_destroy;
864
865         translucent.on_bi.bi_cf_ocs = translucentocs;
866         rc = config_register_schema ( translucentcfg, translucentocs );
867         if ( rc ) return rc;
868
869         return(overlay_register(&translucent));
870 }
871
872 #if SLAPD_OVER_TRANSLUCENT == SLAPD_MOD_DYNAMIC && defined(PIC)
873 int init_module(int argc, char *argv[]) {
874         return translucent_initialize();
875 }
876 #endif
877
878 #endif /* SLAPD_OVER_TRANSLUCENT */