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