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