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