]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/distproc.c
a7aaabe989b8d0da5313e8ba46ab99b7533f5877
[openldap] / servers / slapd / back-ldap / distproc.c
1 /* distproc.c - implement distributed procedures */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005-2006 The OpenLDAP Foundation.
6  * Portions Copyright 2003 Howard Chu.
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 Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  * Based on back-ldap and slapo-chain, developed by Howard Chu
21  */
22
23 #include "portable.h"
24
25 #ifdef LDAP_DEVEL
26
27 #include <stdio.h>
28
29 #include <ac/string.h>
30 #include <ac/socket.h>
31
32 #include "slap.h"
33 #include "back-ldap.h"
34
35 #include "config.h"
36
37 /*
38  * From <draft-sermersheim-ldap-distproc>
39  *
40
41       ContinuationReference ::= SET {
42          referralURI      [0] SET SIZE (1..MAX) OF URI,
43          localReference   [2] LDAPDN,
44          referenceType    [3] ReferenceType,
45          remainingName    [4] RelativeLDAPDN OPTIONAL,
46          searchScope      [5] SearchScope OPTIONAL,
47          searchedSubtrees [6] SearchedSubtrees OPTIONAL,
48          failedName       [7] LDAPDN OPTIONAL,
49          ...  }
50
51       ReferenceType ::= ENUMERATED {
52          superior               (0),
53          subordinate            (1),
54          cross                  (2),
55          nonSpecificSubordinate (3),
56          supplier               (4),
57          master                 (5),
58          immediateSuperior      (6),
59          self                   (7),
60          ...  }
61
62       SearchScope ::= ENUMERATED {
63          baseObject         (0),
64          singleLevel        (1),
65          wholeSubtree       (2),
66          subordinateSubtree (3),
67          ...  }
68
69    SearchedSubtrees ::= SET OF RelativeLDAPDN
70
71    LDAPDN, RelativeLDAPDN, and LDAPString, are defined in [RFC2251].
72
73  */
74
75 typedef enum ReferenceType_t {
76         LDAP_DP_RT_UNKNOWN                      = -1,
77         LDAP_DP_RT_SUPERIOR                     = 0,
78         LDAP_DP_RT_SUBORDINATE                  = 1,
79         LDAP_DP_RT_CROSS                        = 2,
80         LDAP_DP_RT_NONSPECIFICSUBORDINATE       = 3,
81         LDAP_DP_RT_SUPPLIER                     = 4,
82         LDAP_DP_RT_MASTER                       = 5,
83         LDAP_DP_RT_IMMEDIATESUPERIOR            = 6,
84         LDAP_DP_RT_SELF                         = 7,
85         LDAP_DP_RT_LAST
86 } ReferenceType_t;
87
88 typedef enum SearchScope_t {
89         LDAP_DP_SS_UNKNOWN                      = -1,
90         LDAP_DP_SS_BASEOBJECT                   = 0,
91         LDAP_DP_SS_SINGLELEVEL                  = 1,
92         LDAP_DP_SS_WHOLESUBTREE                 = 2,
93         LDAP_DP_SS_SUBORDINATESUBTREE           = 3,
94         LDAP_DP_SS_LAST
95 } SearchScope_t;
96
97 typedef struct ContinuationReference_t {
98         BerVarray               cr_referralURI;
99         /* ?                    [1] ? */
100         struct berval           cr_localReference;
101         ReferenceType_t         cr_referenceType;
102         struct berval           cr_remainingName;
103         SearchScope_t           cr_searchScope;
104         BerVarray               cr_searchedSubtrees;
105         struct berval           cr_failedName;
106 } ContinuationReference_t;
107 #define CR_INIT         { NULL, BER_BVNULL, LDAP_DP_RT_UNKNOWN, BER_BVNULL, LDAP_DP_SS_UNKNOWN, NULL, BER_BVNULL }
108
109 static struct berval    bv2rt[] = {
110         BER_BVC( "superior" ),
111         BER_BVC( "subordinate" ),
112         BER_BVC( "cross" ),
113         BER_BVC( "nonSpecificSubordinate" ),
114         BER_BVC( "supplier" ),
115         BER_BVC( "master" ),
116         BER_BVC( "immediateSuperior" ),
117         BER_BVC( "self" ),
118         BER_BVNULL
119 };
120
121 static struct berval    bv2ss[] = {
122         BER_BVC( "baseObject" ),
123         BER_BVC( "singleLevel" ),
124         BER_BVC( "wholeSubtree" ),
125         BER_BVC( "subordinateSubtree" ),
126         BER_BVNULL
127 };
128
129 static struct berval *
130 ldap_distproc_rt2bv( ReferenceType_t rt )
131 {
132         return &bv2rt[ rt ];
133 }
134
135 static const char *
136 ldap_distproc_rt2str( ReferenceType_t rt )
137 {
138         return bv2rt[ rt ].bv_val;
139 }
140
141 static ReferenceType_t
142 ldap_distproc_bv2rt( struct berval *bv )
143 {
144         ReferenceType_t         rt;
145
146         for ( rt = 0; !BER_BVISNULL( &bv2rt[ rt ] ); rt++ ) {
147                 if ( ber_bvstrcasecmp( bv, &bv2rt[ rt ] ) == 0 ) {
148                         return rt;
149                 }
150         }
151
152         return LDAP_DP_RT_UNKNOWN;
153 }
154
155 static ReferenceType_t
156 ldap_distproc_str2rt( const char *s )
157 {
158         struct berval   bv;
159
160         ber_str2bv( s, 0, 0, &bv );
161         return ldap_distproc_bv2rt( &bv );
162 }
163
164 static struct berval *
165 ldap_distproc_ss2bv( SearchScope_t ss )
166 {
167         return &bv2ss[ ss ];
168 }
169
170 static const char *
171 ldap_distproc_ss2str( SearchScope_t ss )
172 {
173         return bv2ss[ ss ].bv_val;
174 }
175
176 static SearchScope_t
177 ldap_distproc_bv2ss( struct berval *bv )
178 {
179         ReferenceType_t         ss;
180
181         for ( ss = 0; !BER_BVISNULL( &bv2ss[ ss ] ); ss++ ) {
182                 if ( ber_bvstrcasecmp( bv, &bv2ss[ ss ] ) == 0 ) {
183                         return ss;
184                 }
185         }
186
187         return LDAP_DP_SS_UNKNOWN;
188 }
189
190 static SearchScope_t
191 ldap_distproc_str2ss( const char *s )
192 {
193         struct berval   bv;
194
195         ber_str2bv( s, 0, 0, &bv );
196         return ldap_distproc_bv2ss( &bv );
197 }
198
199 /*
200  * NOTE: this overlay assumes that the chainingBehavior control
201  * is registered by the chain overlay; it may move here some time.
202  * This overlay provides support for that control as well.
203  */
204
205
206 static int              sc_returnContRef;
207 #define o_returnContRef                 o_ctrlflag[sc_returnContRef]
208 #define get_returnContRef(op)           ((op)->o_returnContRef & SLAP_CONTROL_MASK)
209
210 static struct berval    slap_EXOP_CHAINEDREQUEST = BER_BVC( LDAP_EXOP_X_CHAINEDREQUEST );
211 static struct berval    slap_FEATURE_CANCHAINOPS = BER_BVC( LDAP_FEATURE_X_CANCHAINOPS );
212
213 static BackendInfo      *lback;
214
215 typedef struct ldap_distproc_t {
216         /* "common" configuration info (anything occurring before an "uri") */
217         ldapinfo_t              *lc_common_li;
218
219         /* current configuration info */
220         ldapinfo_t              *lc_cfg_li;
221
222         /* tree of configured[/generated?] "uri" info */
223         ldap_avl_info_t         lc_lai;
224
225         unsigned                lc_flags;
226 #define LDAP_DISTPROC_F_NONE            (0x00U)
227 #define LDAP_DISTPROC_F_CHAINING        (0x01U)
228 #define LDAP_DISTPROC_F_CACHE_URI       (0x10U)
229
230 #define LDAP_DISTPROC_CHAINING( lc )    ( ( (lc)->lc_flags & LDAP_DISTPROC_F_CHAINING ) == LDAP_DISTPROC_F_CHAINING )
231 #define LDAP_DISTPROC_CACHE_URI( lc )   ( ( (lc)->lc_flags & LDAP_DISTPROC_F_CACHE_URI ) == LDAP_DISTPROC_F_CACHE_URI )
232
233 } ldap_distproc_t;
234
235 static int ldap_distproc_db_init_common( BackendDB      *be );
236 static int ldap_distproc_db_init_one( BackendDB *be );
237 #define ldap_distproc_db_open_one(be)           (lback)->bi_db_open( (be) )
238 #define ldap_distproc_db_close_one(be)          (0)
239 #define ldap_distproc_db_destroy_one(be)        (lback)->bi_db_destroy( (be) )
240
241 static int
242 ldap_distproc_parse_ctrl(
243         Operation       *op,
244         SlapReply       *rs,
245         LDAPControl     *ctrl );
246
247 static int
248 ldap_distproc_uri_cmp( const void *c1, const void *c2 )
249 {
250         const ldapinfo_t        *li1 = (const ldapinfo_t *)c1;
251         const ldapinfo_t        *li2 = (const ldapinfo_t *)c2;
252
253         assert( li1->li_bvuri != NULL );
254         assert( !BER_BVISNULL( &li1->li_bvuri[ 0 ] ) );
255         assert( BER_BVISNULL( &li1->li_bvuri[ 1 ] ) );
256
257         assert( li2->li_bvuri != NULL );
258         assert( !BER_BVISNULL( &li2->li_bvuri[ 0 ] ) );
259         assert( BER_BVISNULL( &li2->li_bvuri[ 1 ] ) );
260
261         /* If local DNs don't match, it is definitely not a match */
262         return ber_bvcmp( &li1->li_bvuri[ 0 ], &li2->li_bvuri[ 0 ] );
263 }
264
265 static int
266 ldap_distproc_uri_dup( void *c1, void *c2 )
267 {
268         ldapinfo_t      *li1 = (ldapinfo_t *)c1;
269         ldapinfo_t      *li2 = (ldapinfo_t *)c2;
270
271         assert( li1->li_bvuri != NULL );
272         assert( !BER_BVISNULL( &li1->li_bvuri[ 0 ] ) );
273         assert( BER_BVISNULL( &li1->li_bvuri[ 1 ] ) );
274
275         assert( li2->li_bvuri != NULL );
276         assert( !BER_BVISNULL( &li2->li_bvuri[ 0 ] ) );
277         assert( BER_BVISNULL( &li2->li_bvuri[ 1 ] ) );
278
279         /* Cannot have more than one shared session with same DN */
280         if ( ber_bvcmp( &li1->li_bvuri[ 0 ], &li2->li_bvuri[ 0 ] ) == 0 ) {
281                 return -1;
282         }
283                 
284         return 0;
285 }
286
287 static int
288 ldap_distproc_operational( Operation *op, SlapReply *rs )
289 {
290         /* Trap entries generated by back-ldap.
291          * 
292          * FIXME: we need a better way to recognize them; a cleaner
293          * solution would be to be able to intercept the response
294          * of be_operational(), so that we can divert only those
295          * calls that fail because operational attributes were
296          * requested for entries that do not belong to the underlying
297          * database.  This fix is likely to intercept also entries
298          * generated by back-perl and so. */
299         if ( rs->sr_entry->e_private == NULL ) {
300                 return 0;
301         }
302
303         return SLAP_CB_CONTINUE;
304 }
305
306 static int
307 ldap_distproc_response( Operation *op, SlapReply *rs )
308 {
309         return SLAP_CB_CONTINUE;
310 }
311
312 /*
313  * configuration...
314  */
315
316 enum {
317         /* NOTE: the chaining behavior control is registered
318          * by the chain overlay; it may move here some time */
319         DP_CHAINING = 1,
320         DP_CACHE_URI,
321
322         DP_LAST
323 };
324
325 static ConfigDriver distproc_cfgen;
326 static ConfigCfAdd distproc_cfadd;
327 static ConfigLDAPadd distproc_ldadd;
328
329 static ConfigTable distproc_cfg[] = {
330         { "distproc-chaining", "args",
331                 2, 4, 0, ARG_MAGIC|ARG_BERVAL|DP_CHAINING, distproc_cfgen,
332                 /* NOTE: using the same attributeTypes defined
333                  * for the "chain" overlay */
334                 "( OLcfgOvAt:3.1 NAME 'olcChainingBehavior' "
335                         "DESC 'Chaining behavior control parameters (draft-sermersheim-ldap-chaining)' "
336                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
337         { "distproc-cache-uri", "TRUE/FALSE",
338                 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|DP_CACHE_URI, distproc_cfgen,
339                 "( OLcfgOvAt:3.2 NAME 'olcChainCacheURI' "
340                         "DESC 'Enables caching of URIs not present in configuration' "
341                         "SYNTAX OMsBoolean "
342                         "SINGLE-VALUE )", NULL, NULL },
343         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
344 };
345
346 static ConfigOCs distproc_ocs[] = {
347         { "( OLcfgOvOc:7.1 "
348                 "NAME 'olcDistProcConfig' "
349                 "DESC 'Distributed procedures <draft-sermersheim-ldap-distproc> configuration' "
350                 "SUP olcOverlayConfig "
351                 "MAY ( "
352                         "olcChainingBehavior $ "
353                         "olcChainCacheURI "
354                         ") )",
355                 Cft_Overlay, distproc_cfg, NULL, distproc_cfadd },
356         { "( OLcfgOvOc:7.2 "
357                 "NAME 'olcDistProcDatabase' "
358                 "DESC 'Distributed procedure remote server configuration' "
359                 "AUXILIARY )",
360                 Cft_Misc, distproc_cfg, distproc_ldadd },
361         { NULL, 0, NULL }
362 };
363
364 static int
365 distproc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
366 {
367         slap_overinst           *on;
368         ldap_distproc_t         *lc;
369
370         ldapinfo_t              *li;
371
372         AttributeDescription    *ad = NULL;
373         Attribute               *at;
374         const char              *text;
375
376         int                     rc;
377
378         if ( p->ce_type != Cft_Overlay
379                 || !p->ce_bi
380                 || p->ce_bi->bi_cf_ocs != distproc_ocs )
381         {
382                 return LDAP_CONSTRAINT_VIOLATION;
383         }
384
385         on = (slap_overinst *)p->ce_bi;
386         lc = (ldap_distproc_t *)on->on_bi.bi_private;
387
388         assert( ca->be == NULL );
389         ca->be = (BackendDB *)ch_calloc( 1, sizeof( BackendDB ) );
390
391         ca->be->bd_info = (BackendInfo *)on;
392
393         rc = slap_str2ad( "olcDbURI", &ad, &text );
394         assert( rc == LDAP_SUCCESS );
395
396         at = attr_find( e->e_attrs, ad );
397         if ( lc->lc_common_li == NULL && at != NULL ) {
398                 /* FIXME: we should generate an empty default entry
399                  * if none is supplied */
400                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
401                         "first underlying database \"%s\" "
402                         "cannot contain attribute \"%s\".\n",
403                         e->e_name.bv_val, ad->ad_cname.bv_val, 0 );
404                 rc = LDAP_CONSTRAINT_VIOLATION;
405                 goto done;
406
407         } else if ( lc->lc_common_li != NULL && at == NULL ) {
408                 /* FIXME: we should generate an empty default entry
409                  * if none is supplied */
410                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
411                         "subsequent underlying database \"%s\" "
412                         "must contain attribute \"%s\".\n",
413                         e->e_name.bv_val, ad->ad_cname.bv_val, 0 );
414                 rc = LDAP_CONSTRAINT_VIOLATION;
415                 goto done;
416         }
417
418         if ( lc->lc_common_li == NULL ) {
419                 rc = ldap_distproc_db_init_common( ca->be );
420
421         } else {
422                 rc = ldap_distproc_db_init_one( ca->be );
423         }
424
425         if ( rc != 0 ) {
426                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
427                         "unable to init %sunderlying database \"%s\".\n",
428                         lc->lc_common_li == NULL ? "common " : "", e->e_name.bv_val, 0 );
429                 return LDAP_CONSTRAINT_VIOLATION;
430         }
431
432         li = ca->be->be_private;
433
434         if ( lc->lc_common_li == NULL ) {
435                 lc->lc_common_li = li;
436
437         } else if ( avl_insert( &lc->lc_lai.lai_tree, (caddr_t)li,
438                 ldap_distproc_uri_cmp, ldap_distproc_uri_dup ) )
439         {
440                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
441                         "database \"%s\" insert failed.\n",
442                         e->e_name.bv_val, 0, 0 );
443                 rc = LDAP_CONSTRAINT_VIOLATION;
444                 goto done;
445         }
446
447 done:;
448         if ( rc != LDAP_SUCCESS ) {
449                 (void)ldap_distproc_db_destroy_one( ca->be );
450                 ch_free( ca->be );
451                 ca->be = NULL;
452         }
453
454         return rc;
455 }
456
457 typedef struct ldap_distproc_cfadd_apply_t {
458         Operation       *op;
459         SlapReply       *rs;
460         Entry           *p;
461         ConfigArgs      *ca;
462         int             count;
463 } ldap_distproc_cfadd_apply_t;
464
465 static int
466 ldap_distproc_cfadd_apply( void *datum, void *arg )
467 {
468         ldapinfo_t                      *li = (ldapinfo_t *)datum;
469         ldap_distproc_cfadd_apply_t     *lca = (ldap_distproc_cfadd_apply_t *)arg;
470
471         struct berval                   bv;
472
473         /* FIXME: should not hardcode "olcDatabase" here */
474         bv.bv_len = snprintf( lca->ca->msg, sizeof( lca->ca->msg ),
475                 "olcDatabase={%d}%s", lca->count, lback->bi_type );
476         bv.bv_val = lca->ca->msg;
477
478         lca->ca->be->be_private = (void *)li;
479         config_build_entry( lca->op, lca->rs, lca->p->e_private, lca->ca,
480                 &bv, lback->bi_cf_ocs, &distproc_ocs[ 1 ] );
481
482         lca->count++;
483
484         return 0;
485 }
486
487 static int
488 distproc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
489 {
490         CfEntryInfo     *pe = p->e_private;
491         slap_overinst   *on = (slap_overinst *)pe->ce_bi;
492         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
493         void            *priv = (void *)ca->be->be_private;
494
495         if ( lback->bi_cf_ocs ) {
496                 ldap_distproc_cfadd_apply_t     lca = { 0 };
497
498                 lca.op = op;
499                 lca.rs = rs;
500                 lca.p = p;
501                 lca.ca = ca;
502                 lca.count = 0;
503
504                 (void)ldap_distproc_cfadd_apply( (void *)lc->lc_common_li, (void *)&lca );
505
506                 (void)avl_apply( lc->lc_lai.lai_tree, ldap_distproc_cfadd_apply,
507                         &lca, 1, AVL_INORDER );
508
509                 ca->be->be_private = priv;
510         }
511
512         return 0;
513 }
514
515 static int
516 distproc_cfgen( ConfigArgs *c )
517 {
518         slap_overinst   *on = (slap_overinst *)c->bi;
519         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
520
521         int             rc = 0;
522
523         if ( c->op == SLAP_CONFIG_EMIT ) {
524                 switch( c->type ) {
525                 case DP_CACHE_URI:
526                         c->value_int = LDAP_DISTPROC_CACHE_URI( lc );
527                         break;
528
529                 default:
530                         assert( 0 );
531                         rc = 1;
532                 }
533                 return rc;
534
535         } else if ( c->op == LDAP_MOD_DELETE ) {
536                 switch( c->type ) {
537                 case DP_CHAINING:
538                         return 1;
539
540                 case DP_CACHE_URI:
541                         lc->lc_flags &= ~LDAP_DISTPROC_F_CACHE_URI;
542                         break;
543
544                 default:
545                         return 1;
546                 }
547                 return rc;
548         }
549
550         switch( c->type ) {
551         case DP_CACHE_URI:
552                 if ( c->value_int ) {
553                         lc->lc_flags |= LDAP_DISTPROC_F_CACHE_URI;
554                 } else {
555                         lc->lc_flags &= ~LDAP_DISTPROC_F_CACHE_URI;
556                 }
557                 break;
558
559         default:
560                 assert( 0 );
561                 return 1;
562         }
563
564         return rc;
565 }
566
567 static int
568 ldap_distproc_db_init(
569         BackendDB *be )
570 {
571         slap_overinst   *on = (slap_overinst *)be->bd_info;
572         ldap_distproc_t *lc = NULL;
573
574         if ( lback == NULL ) {
575                 lback = backend_info( "ldap" );
576
577                 if ( lback == NULL ) {
578                         return 1;
579                 }
580         }
581
582         lc = ch_malloc( sizeof( ldap_distproc_t ) );
583         if ( lc == NULL ) {
584                 return 1;
585         }
586         memset( lc, 0, sizeof( ldap_distproc_t ) );
587         ldap_pvt_thread_mutex_init( &lc->lc_lai.lai_mutex );
588
589         on->on_bi.bi_private = (void *)lc;
590
591         return 0;
592 }
593
594 static int
595 ldap_distproc_db_config(
596         BackendDB       *be,
597         const char      *fname,
598         int             lineno,
599         int             argc,
600         char            **argv )
601 {
602         slap_overinst   *on = (slap_overinst *)be->bd_info;
603         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
604
605         int             rc = SLAP_CONF_UNKNOWN;
606                 
607         if ( lc->lc_common_li == NULL ) {
608                 void    *be_private = be->be_private;
609                 ldap_distproc_db_init_common( be );
610                 lc->lc_common_li = lc->lc_cfg_li = (ldapinfo_t *)be->be_private;
611                 be->be_private = be_private;
612         }
613
614         /* Something for the distproc database? */
615         if ( strncasecmp( argv[ 0 ], "distproc-", STRLENOF( "distproc-" ) ) == 0 ) {
616                 char            *save_argv0 = argv[ 0 ];
617                 BackendInfo     *bd_info = be->bd_info;
618                 void            *be_private = be->be_private;
619                 ConfigOCs       *be_cf_ocs = be->be_cf_ocs;
620                 int             is_uri = 0;
621
622                 argv[ 0 ] += STRLENOF( "distproc-" );
623
624                 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
625                         rc = ldap_distproc_db_init_one( be );
626                         if ( rc != 0 ) {
627                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
628                                         "underlying slapd-ldap initialization failed.\n.",
629                                         fname, lineno, 0 );
630                                 return 1;
631                         }
632                         lc->lc_cfg_li = be->be_private;
633                         is_uri = 1;
634                 }
635
636                 /* TODO: add checks on what other slapd-ldap(5) args
637                  * should be put in the template; this is not quite
638                  * harmful, because attributes that shouldn't don't
639                  * get actually used, but the user should at least
640                  * be warned.
641                  */
642
643                 be->bd_info = lback;
644                 be->be_private = (void *)lc->lc_cfg_li;
645                 be->be_cf_ocs = lback->bi_cf_ocs;
646
647                 rc = config_generic_wrapper( be, fname, lineno, argc, argv );
648
649                 argv[ 0 ] = save_argv0;
650                 be->be_cf_ocs = be_cf_ocs;
651                 be->be_private = be_private;
652                 be->bd_info = bd_info;
653
654                 if ( is_uri ) {
655 private_destroy:;
656                         if ( rc != 0 ) {
657                                 BackendDB               db = *be;
658
659                                 db.bd_info = lback;
660                                 db.be_private = (void *)lc->lc_cfg_li;
661                                 ldap_distproc_db_destroy_one( &db );
662                                 lc->lc_cfg_li = NULL;
663
664                         } else {
665                                 if ( lc->lc_cfg_li->li_bvuri == NULL
666                                         || BER_BVISNULL( &lc->lc_cfg_li->li_bvuri[ 0 ] )
667                                         || !BER_BVISNULL( &lc->lc_cfg_li->li_bvuri[ 1 ] ) )
668                                 {
669                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
670                                                 "no URI list allowed in slapo-distproc.\n",
671                                                 fname, lineno, 0 );
672                                         rc = 1;
673                                         goto private_destroy;
674                                 }
675
676                                 if ( avl_insert( &lc->lc_lai.lai_tree,
677                                         (caddr_t)lc->lc_cfg_li,
678                                         ldap_distproc_uri_cmp, ldap_distproc_uri_dup ) )
679                                 {
680                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
681                                                 "duplicate URI in slapo-distproc.\n",
682                                                 fname, lineno, 0 );
683                                         rc = 1;
684                                         goto private_destroy;
685                                 }
686                         }
687                 }
688         }
689         
690         return rc;
691 }
692
693 enum db_which {
694         db_open = 0,
695         db_close,
696         db_destroy,
697
698         db_last
699 };
700
701 typedef struct ldap_distproc_db_apply_t {
702         BackendDB       *be;
703         BI_db_func      *func;
704 } ldap_distproc_db_apply_t;
705
706 static int
707 ldap_distproc_db_apply( void *datum, void *arg )
708 {
709         ldapinfo_t              *li = (ldapinfo_t *)datum;
710         ldap_distproc_db_apply_t        *lca = (ldap_distproc_db_apply_t *)arg;
711
712         lca->be->be_private = (void *)li;
713
714         return lca->func( lca->be );
715 }
716
717 static int
718 ldap_distproc_db_func(
719         BackendDB *be,
720         enum db_which which
721 )
722 {
723         slap_overinst   *on = (slap_overinst *)be->bd_info;
724         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
725
726         int             rc = 0;
727
728         if ( lc ) {
729                 BI_db_func      *func = (&lback->bi_db_open)[ which ];
730
731                 if ( func != NULL && lc->lc_common_li != NULL ) {
732                         BackendDB               db = *be;
733
734                         db.bd_info = lback;
735                         db.be_private = lc->lc_common_li;
736
737                         rc = func( &db );
738
739                         if ( rc != 0 ) {
740                                 return rc;
741                         }
742
743                         if ( lc->lc_lai.lai_tree != NULL ) {
744                                 ldap_distproc_db_apply_t        lca;
745
746                                 lca.be = &db;
747                                 lca.func = func;
748
749                                 rc = avl_apply( lc->lc_lai.lai_tree,
750                                         ldap_distproc_db_apply, (void *)&lca,
751                                         1, AVL_INORDER ) != AVL_NOMORE;
752                         }
753                 }
754         }
755
756         return rc;
757 }
758
759 static int
760 ldap_distproc_db_open(
761         BackendDB       *be )
762 {
763         return ldap_distproc_db_func( be, db_open );
764 }
765
766 static int
767 ldap_distproc_db_close(
768         BackendDB       *be )
769 {
770         return ldap_distproc_db_func( be, db_close );
771 }
772
773 static int
774 ldap_distproc_db_destroy(
775         BackendDB       *be )
776 {
777         slap_overinst   *on = (slap_overinst *) be->bd_info;
778         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
779
780         int             rc;
781
782         rc = ldap_distproc_db_func( be, db_destroy );
783
784         if ( lc ) {
785                 avl_free( lc->lc_lai.lai_tree, NULL );
786                 ldap_pvt_thread_mutex_destroy( &lc->lc_lai.lai_mutex );
787                 ch_free( lc );
788         }
789
790         return rc;
791 }
792
793 /*
794  * inits one instance of the slapd-ldap backend, and stores
795  * the private info in be_private of the arg
796  */
797 static int
798 ldap_distproc_db_init_common(
799         BackendDB       *be )
800 {
801         BackendInfo     *bi = be->bd_info;
802         int             t;
803
804         be->bd_info = lback;
805         be->be_private = NULL;
806         t = lback->bi_db_init( be );
807         if ( t != 0 ) {
808                 return t;
809         }
810         be->bd_info = bi;
811
812         return 0;
813 }
814
815 /*
816  * inits one instance of the slapd-ldap backend, stores
817  * the private info in be_private of the arg and fills
818  * selected fields with data from the template.
819  *
820  * NOTE: add checks about the other fields of the template,
821  * which are ignored and SHOULD NOT be configured by the user.
822  */
823 static int
824 ldap_distproc_db_init_one(
825         BackendDB       *be )
826 {
827         slap_overinst   *on = (slap_overinst *)be->bd_info;
828         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
829
830         BackendInfo     *bi = be->bd_info;
831         ldapinfo_t      *li;
832
833         int             t;
834
835         be->bd_info = lback;
836         be->be_private = NULL;
837         t = lback->bi_db_init( be );
838         if ( t != 0 ) {
839                 return t;
840         }
841         li = (ldapinfo_t *)be->be_private;
842
843         /* copy common data */
844         li->li_nretries = lc->lc_common_li->li_nretries;
845         li->li_flags = lc->lc_common_li->li_flags;
846         li->li_version = lc->lc_common_li->li_version;
847         for ( t = 0; t < LDAP_BACK_OP_LAST; t++ ) {
848                 li->li_timeout[ t ] = lc->lc_common_li->li_timeout[ t ];
849         }
850         be->bd_info = bi;
851
852         return 0;
853 }
854
855 typedef struct ldap_distproc_conn_apply_t {
856         BackendDB       *be;
857         Connection      *conn;
858 } ldap_distproc_conn_apply_t;
859
860 static int
861 ldap_distproc_conn_apply( void *datum, void *arg )
862 {
863         ldapinfo_t              *li = (ldapinfo_t *)datum;
864         ldap_distproc_conn_apply_t      *lca = (ldap_distproc_conn_apply_t *)arg;
865
866         lca->be->be_private = (void *)li;
867
868         return lback->bi_connection_destroy( lca->be, lca->conn );
869 }
870
871 static int
872 ldap_distproc_connection_destroy(
873         BackendDB *be,
874         Connection *conn
875 )
876 {
877         slap_overinst           *on = (slap_overinst *) be->bd_info;
878         ldap_distproc_t         *lc = (ldap_distproc_t *)on->on_bi.bi_private;
879         void                    *private = be->be_private;
880         ldap_distproc_conn_apply_t      lca;
881         int                     rc;
882
883         be->be_private = NULL;
884         lca.be = be;
885         lca.conn = conn;
886         ldap_pvt_thread_mutex_lock( &lc->lc_lai.lai_mutex );
887         rc = avl_apply( lc->lc_lai.lai_tree, ldap_distproc_conn_apply,
888                 (void *)&lca, 1, AVL_INORDER ) != AVL_NOMORE;
889         ldap_pvt_thread_mutex_unlock( &lc->lc_lai.lai_mutex );
890         be->be_private = private;
891
892         return rc;
893 }
894
895 static int
896 ldap_distproc_parse_returnContRef_ctrl(
897         Operation       *op,
898         SlapReply       *rs,
899         LDAPControl     *ctrl )
900 {
901         if ( get_returnContRef( op ) != SLAP_CONTROL_NONE ) {
902                 rs->sr_text = "returnContinuationReference control specified multiple times";
903                 return LDAP_PROTOCOL_ERROR;
904         }
905
906         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
907                 rs->sr_text = "returnContinuationReference control specified with pagedResults control";
908                 return LDAP_PROTOCOL_ERROR;
909         }
910
911         if ( !BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
912                 rs->sr_text = "returnContinuationReference control: value must be NULL";
913                 return LDAP_PROTOCOL_ERROR;
914         }
915
916         op->o_returnContRef = ctrl->ldctl_iscritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
917
918         return LDAP_SUCCESS;
919 }
920
921 static int
922 ldap_exop_chained_request(
923                 Operation       *op,
924                 SlapReply       *rs )
925 {
926         Statslog( LDAP_DEBUG_STATS, "%s CHAINED REQUEST\n",
927             op->o_log_prefix, 0, 0, 0, 0 );
928
929         rs->sr_err = backend_check_restrictions( op, rs, 
930                         (struct berval *)&slap_EXOP_CHAINEDREQUEST );
931         if ( rs->sr_err != LDAP_SUCCESS ) {
932                 return rs->sr_err;
933         }
934
935         /* by now, just reject requests */
936         rs->sr_text = "under development";
937         return LDAP_UNWILLING_TO_PERFORM;
938 }
939
940
941 static slap_overinst distproc;
942
943 int
944 distproc_initialize( void )
945 {
946         int     rc;
947
948         /* Make sure we don't exceed the bits reserved for userland */
949         config_check_userland( DP_LAST );
950
951         rc = load_extop( (struct berval *)&slap_EXOP_CHAINEDREQUEST,
952                 SLAP_EXOP_HIDE, ldap_exop_chained_request );
953         if ( rc != LDAP_SUCCESS ) {
954                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
955                         "unable to register chainedRequest exop: %d.\n",
956                         rc, 0, 0 );
957                 return rc;
958         }
959
960         rc = supported_feature_load( &slap_FEATURE_CANCHAINOPS );
961         if ( rc != LDAP_SUCCESS ) {
962                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
963                         "unable to register canChainOperations supported feature: %d.\n",
964                         rc, 0, 0 );
965                 return rc;
966         }
967
968         rc = register_supported_control( LDAP_CONTROL_X_RETURNCONTREF,
969                         SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, NULL,
970                         ldap_distproc_parse_returnContRef_ctrl, &sc_returnContRef );
971         if ( rc != LDAP_SUCCESS ) {
972                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
973                         "unable to register returnContinuationReference control: %d.\n",
974                         rc, 0, 0 );
975                 return rc;
976         }
977
978         distproc.on_bi.bi_type = "distproc";
979         distproc.on_bi.bi_db_init = ldap_distproc_db_init;
980         distproc.on_bi.bi_db_config = ldap_distproc_db_config;
981         distproc.on_bi.bi_db_open = ldap_distproc_db_open;
982         distproc.on_bi.bi_db_close = ldap_distproc_db_close;
983         distproc.on_bi.bi_db_destroy = ldap_distproc_db_destroy;
984
985         /* ... otherwise the underlying backend's function would be called,
986          * likely passing an invalid entry; on the contrary, the requested
987          * operational attributes should have been returned while chasing
988          * the referrals.  This all in all is a bit messy, because part
989          * of the operational attributes are generated by the backend;
990          * part by the frontend; back-ldap should receive all the available
991          * ones from the remote server, but then, on its own, it strips those
992          * it assumes will be (re)generated by the frontend (e.g.
993          * subschemaSubentry, entryDN, ...) */
994         distproc.on_bi.bi_operational = ldap_distproc_operational;
995         
996         distproc.on_bi.bi_connection_destroy = ldap_distproc_connection_destroy;
997
998         distproc.on_response = ldap_distproc_response;
999
1000         distproc.on_bi.bi_cf_ocs = distproc_ocs;
1001
1002         rc = config_register_schema( distproc_cfg, distproc_ocs );
1003         if ( rc ) {
1004                 return rc;
1005         }
1006
1007         return overlay_register( &distproc );
1008 }
1009
1010 #endif /* LDAP_DEVEL */