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