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