]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/distproc.c
cleanup previous commit
[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 'olcCacheURI' "
340                         "DESC 'Enables caching of URIs not present in configuration' "
341                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
342         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
343 };
344
345 static ConfigOCs distproc_ocs[] = {
346         { "( OLcfgOvOc:7.1 "
347                 "NAME 'olcDistProcConfig' "
348                 "DESC 'Distributed procedures <draft-sermersheim-ldap-distproc> configuration' "
349                 "SUP olcOverlayConfig "
350                 "MAY ( "
351                         "olcChainingBehavior $ "
352                         "olcCacheURI "
353                         ") )",
354                 Cft_Overlay, distproc_cfg, NULL, distproc_cfadd },
355         { "( OLcfgOvOc:7.2 "
356                 "NAME 'olcDistProcDatabase' "
357                 "DESC 'Distributed procedure remote server configuration' "
358                 "AUXILIARY )",
359                 Cft_Misc, distproc_cfg, distproc_ldadd },
360         { NULL, 0, NULL }
361 };
362
363 static int
364 distproc_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
365 {
366         slap_overinst           *on;
367         ldap_distproc_t         *lc;
368
369         ldapinfo_t              *li;
370
371         AttributeDescription    *ad = NULL;
372         Attribute               *at;
373         const char              *text;
374
375         int                     rc;
376
377         if ( p->ce_type != Cft_Overlay
378                 || !p->ce_bi
379                 || p->ce_bi->bi_cf_ocs != distproc_ocs )
380         {
381                 return LDAP_CONSTRAINT_VIOLATION;
382         }
383
384         on = (slap_overinst *)p->ce_bi;
385         lc = (ldap_distproc_t *)on->on_bi.bi_private;
386
387         assert( ca->be == NULL );
388         ca->be = (BackendDB *)ch_calloc( 1, sizeof( BackendDB ) );
389
390         ca->be->bd_info = (BackendInfo *)on;
391
392         rc = slap_str2ad( "olcDbURI", &ad, &text );
393         assert( rc == LDAP_SUCCESS );
394
395         at = attr_find( e->e_attrs, ad );
396         if ( lc->lc_common_li == NULL && at != NULL ) {
397                 /* FIXME: we should generate an empty default entry
398                  * if none is supplied */
399                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
400                         "first underlying database \"%s\" "
401                         "cannot contain attribute \"%s\".\n",
402                         e->e_name.bv_val, ad->ad_cname.bv_val, 0 );
403                 rc = LDAP_CONSTRAINT_VIOLATION;
404                 goto done;
405
406         } else if ( lc->lc_common_li != NULL && at == NULL ) {
407                 /* FIXME: we should generate an empty default entry
408                  * if none is supplied */
409                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
410                         "subsequent underlying database \"%s\" "
411                         "must contain attribute \"%s\".\n",
412                         e->e_name.bv_val, ad->ad_cname.bv_val, 0 );
413                 rc = LDAP_CONSTRAINT_VIOLATION;
414                 goto done;
415         }
416
417         if ( lc->lc_common_li == NULL ) {
418                 rc = ldap_distproc_db_init_common( ca->be );
419
420         } else {
421                 rc = ldap_distproc_db_init_one( ca->be );
422         }
423
424         if ( rc != 0 ) {
425                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
426                         "unable to init %sunderlying database \"%s\".\n",
427                         lc->lc_common_li == NULL ? "common " : "", e->e_name.bv_val, 0 );
428                 return LDAP_CONSTRAINT_VIOLATION;
429         }
430
431         li = ca->be->be_private;
432
433         if ( lc->lc_common_li == NULL ) {
434                 lc->lc_common_li = li;
435
436         } else if ( avl_insert( &lc->lc_lai.lai_tree, (caddr_t)li,
437                 ldap_distproc_uri_cmp, ldap_distproc_uri_dup ) )
438         {
439                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
440                         "database \"%s\" insert failed.\n",
441                         e->e_name.bv_val, 0, 0 );
442                 rc = LDAP_CONSTRAINT_VIOLATION;
443                 goto done;
444         }
445
446 done:;
447         if ( rc != LDAP_SUCCESS ) {
448                 (void)ldap_distproc_db_destroy_one( ca->be );
449                 ch_free( ca->be );
450                 ca->be = NULL;
451         }
452
453         return rc;
454 }
455
456 typedef struct ldap_distproc_cfadd_apply_t {
457         Operation       *op;
458         SlapReply       *rs;
459         Entry           *p;
460         ConfigArgs      *ca;
461         int             count;
462 } ldap_distproc_cfadd_apply_t;
463
464 static int
465 ldap_distproc_cfadd_apply( void *datum, void *arg )
466 {
467         ldapinfo_t                      *li = (ldapinfo_t *)datum;
468         ldap_distproc_cfadd_apply_t     *lca = (ldap_distproc_cfadd_apply_t *)arg;
469
470         struct berval                   bv;
471
472         /* FIXME: should not hardcode "olcDatabase" here */
473         bv.bv_len = snprintf( lca->ca->msg, sizeof( lca->ca->msg ),
474                 "olcDatabase={%d}%s", lca->count, lback->bi_type );
475         bv.bv_val = lca->ca->msg;
476
477         lca->ca->be->be_private = (void *)li;
478         config_build_entry( lca->op, lca->rs, lca->p->e_private, lca->ca,
479                 &bv, lback->bi_cf_ocs, &distproc_ocs[ 1 ] );
480
481         lca->count++;
482
483         return 0;
484 }
485
486 static int
487 distproc_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
488 {
489         CfEntryInfo     *pe = p->e_private;
490         slap_overinst   *on = (slap_overinst *)pe->ce_bi;
491         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
492         void            *priv = (void *)ca->be->be_private;
493
494         if ( lback->bi_cf_ocs ) {
495                 ldap_distproc_cfadd_apply_t     lca = { 0 };
496
497                 lca.op = op;
498                 lca.rs = rs;
499                 lca.p = p;
500                 lca.ca = ca;
501                 lca.count = 0;
502
503                 (void)ldap_distproc_cfadd_apply( (void *)lc->lc_common_li, (void *)&lca );
504
505                 (void)avl_apply( lc->lc_lai.lai_tree, ldap_distproc_cfadd_apply,
506                         &lca, 1, AVL_INORDER );
507
508                 ca->be->be_private = priv;
509         }
510
511         return 0;
512 }
513
514 static int
515 distproc_cfgen( ConfigArgs *c )
516 {
517         slap_overinst   *on = (slap_overinst *)c->bi;
518         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
519
520         int             rc = 0;
521
522         if ( c->op == SLAP_CONFIG_EMIT ) {
523                 switch( c->type ) {
524                 case DP_CACHE_URI:
525                         c->value_int = LDAP_DISTPROC_CACHE_URI( lc );
526                         break;
527
528                 default:
529                         assert( 0 );
530                         rc = 1;
531                 }
532                 return rc;
533
534         } else if ( c->op == LDAP_MOD_DELETE ) {
535                 switch( c->type ) {
536                 case DP_CHAINING:
537                         return 1;
538
539                 case DP_CACHE_URI:
540                         lc->lc_flags &= ~LDAP_DISTPROC_F_CACHE_URI;
541                         break;
542
543                 default:
544                         return 1;
545                 }
546                 return rc;
547         }
548
549         switch( c->type ) {
550         case DP_CACHE_URI:
551                 if ( c->value_int ) {
552                         lc->lc_flags |= LDAP_DISTPROC_F_CACHE_URI;
553                 } else {
554                         lc->lc_flags &= ~LDAP_DISTPROC_F_CACHE_URI;
555                 }
556                 break;
557
558         default:
559                 assert( 0 );
560                 return 1;
561         }
562
563         return rc;
564 }
565
566 static int
567 ldap_distproc_db_init(
568         BackendDB *be )
569 {
570         slap_overinst   *on = (slap_overinst *)be->bd_info;
571         ldap_distproc_t *lc = NULL;
572
573         if ( lback == NULL ) {
574                 lback = backend_info( "ldap" );
575
576                 if ( lback == NULL ) {
577                         return 1;
578                 }
579         }
580
581         lc = ch_malloc( sizeof( ldap_distproc_t ) );
582         if ( lc == NULL ) {
583                 return 1;
584         }
585         memset( lc, 0, sizeof( ldap_distproc_t ) );
586         ldap_pvt_thread_mutex_init( &lc->lc_lai.lai_mutex );
587
588         on->on_bi.bi_private = (void *)lc;
589
590         return 0;
591 }
592
593 static int
594 ldap_distproc_db_config(
595         BackendDB       *be,
596         const char      *fname,
597         int             lineno,
598         int             argc,
599         char            **argv )
600 {
601         slap_overinst   *on = (slap_overinst *)be->bd_info;
602         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
603
604         int             rc = SLAP_CONF_UNKNOWN;
605                 
606         if ( lc->lc_common_li == NULL ) {
607                 void    *be_private = be->be_private;
608                 ldap_distproc_db_init_common( be );
609                 lc->lc_common_li = lc->lc_cfg_li = (ldapinfo_t *)be->be_private;
610                 be->be_private = be_private;
611         }
612
613         /* Something for the distproc database? */
614         if ( strncasecmp( argv[ 0 ], "distproc-", STRLENOF( "distproc-" ) ) == 0 ) {
615                 char            *save_argv0 = argv[ 0 ];
616                 BackendInfo     *bd_info = be->bd_info;
617                 void            *be_private = be->be_private;
618                 ConfigOCs       *be_cf_ocs = be->be_cf_ocs;
619                 int             is_uri = 0;
620
621                 argv[ 0 ] += STRLENOF( "distproc-" );
622
623                 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
624                         rc = ldap_distproc_db_init_one( be );
625                         if ( rc != 0 ) {
626                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
627                                         "underlying slapd-ldap initialization failed.\n.",
628                                         fname, lineno, 0 );
629                                 return 1;
630                         }
631                         lc->lc_cfg_li = be->be_private;
632                         is_uri = 1;
633                 }
634
635                 /* TODO: add checks on what other slapd-ldap(5) args
636                  * should be put in the template; this is not quite
637                  * harmful, because attributes that shouldn't don't
638                  * get actually used, but the user should at least
639                  * be warned.
640                  */
641
642                 be->bd_info = lback;
643                 be->be_private = (void *)lc->lc_cfg_li;
644                 be->be_cf_ocs = lback->bi_cf_ocs;
645
646                 rc = config_generic_wrapper( be, fname, lineno, argc, argv );
647
648                 argv[ 0 ] = save_argv0;
649                 be->be_cf_ocs = be_cf_ocs;
650                 be->be_private = be_private;
651                 be->bd_info = bd_info;
652
653                 if ( is_uri ) {
654 private_destroy:;
655                         if ( rc != 0 ) {
656                                 BackendDB               db = *be;
657
658                                 db.bd_info = lback;
659                                 db.be_private = (void *)lc->lc_cfg_li;
660                                 ldap_distproc_db_destroy_one( &db );
661                                 lc->lc_cfg_li = NULL;
662
663                         } else {
664                                 if ( lc->lc_cfg_li->li_bvuri == NULL
665                                         || BER_BVISNULL( &lc->lc_cfg_li->li_bvuri[ 0 ] )
666                                         || !BER_BVISNULL( &lc->lc_cfg_li->li_bvuri[ 1 ] ) )
667                                 {
668                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
669                                                 "no URI list allowed in slapo-distproc.\n",
670                                                 fname, lineno, 0 );
671                                         rc = 1;
672                                         goto private_destroy;
673                                 }
674
675                                 if ( avl_insert( &lc->lc_lai.lai_tree,
676                                         (caddr_t)lc->lc_cfg_li,
677                                         ldap_distproc_uri_cmp, ldap_distproc_uri_dup ) )
678                                 {
679                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
680                                                 "duplicate URI in slapo-distproc.\n",
681                                                 fname, lineno, 0 );
682                                         rc = 1;
683                                         goto private_destroy;
684                                 }
685                         }
686                 }
687         }
688         
689         return rc;
690 }
691
692 enum db_which {
693         db_open = 0,
694         db_close,
695         db_destroy,
696
697         db_last
698 };
699
700 typedef struct ldap_distproc_db_apply_t {
701         BackendDB       *be;
702         BI_db_func      *func;
703 } ldap_distproc_db_apply_t;
704
705 static int
706 ldap_distproc_db_apply( void *datum, void *arg )
707 {
708         ldapinfo_t              *li = (ldapinfo_t *)datum;
709         ldap_distproc_db_apply_t        *lca = (ldap_distproc_db_apply_t *)arg;
710
711         lca->be->be_private = (void *)li;
712
713         return lca->func( lca->be );
714 }
715
716 static int
717 ldap_distproc_db_func(
718         BackendDB *be,
719         enum db_which which
720 )
721 {
722         slap_overinst   *on = (slap_overinst *)be->bd_info;
723         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
724
725         int             rc = 0;
726
727         if ( lc ) {
728                 BI_db_func      *func = (&lback->bi_db_open)[ which ];
729
730                 if ( func != NULL && lc->lc_common_li != NULL ) {
731                         BackendDB               db = *be;
732
733                         db.bd_info = lback;
734                         db.be_private = lc->lc_common_li;
735
736                         rc = func( &db );
737
738                         if ( rc != 0 ) {
739                                 return rc;
740                         }
741
742                         if ( lc->lc_lai.lai_tree != NULL ) {
743                                 ldap_distproc_db_apply_t        lca;
744
745                                 lca.be = &db;
746                                 lca.func = func;
747
748                                 rc = avl_apply( lc->lc_lai.lai_tree,
749                                         ldap_distproc_db_apply, (void *)&lca,
750                                         1, AVL_INORDER ) != AVL_NOMORE;
751                         }
752                 }
753         }
754
755         return rc;
756 }
757
758 static int
759 ldap_distproc_db_open(
760         BackendDB       *be )
761 {
762         return ldap_distproc_db_func( be, db_open );
763 }
764
765 static int
766 ldap_distproc_db_close(
767         BackendDB       *be )
768 {
769         return ldap_distproc_db_func( be, db_close );
770 }
771
772 static int
773 ldap_distproc_db_destroy(
774         BackendDB       *be )
775 {
776         slap_overinst   *on = (slap_overinst *) be->bd_info;
777         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
778
779         int             rc;
780
781         rc = ldap_distproc_db_func( be, db_destroy );
782
783         if ( lc ) {
784                 avl_free( lc->lc_lai.lai_tree, NULL );
785                 ldap_pvt_thread_mutex_destroy( &lc->lc_lai.lai_mutex );
786                 ch_free( lc );
787         }
788
789         return rc;
790 }
791
792 /*
793  * inits one instance of the slapd-ldap backend, and stores
794  * the private info in be_private of the arg
795  */
796 static int
797 ldap_distproc_db_init_common(
798         BackendDB       *be )
799 {
800         BackendInfo     *bi = be->bd_info;
801         int             t;
802
803         be->bd_info = lback;
804         be->be_private = NULL;
805         t = lback->bi_db_init( be );
806         if ( t != 0 ) {
807                 return t;
808         }
809         be->bd_info = bi;
810
811         return 0;
812 }
813
814 /*
815  * inits one instance of the slapd-ldap backend, stores
816  * the private info in be_private of the arg and fills
817  * selected fields with data from the template.
818  *
819  * NOTE: add checks about the other fields of the template,
820  * which are ignored and SHOULD NOT be configured by the user.
821  */
822 static int
823 ldap_distproc_db_init_one(
824         BackendDB       *be )
825 {
826         slap_overinst   *on = (slap_overinst *)be->bd_info;
827         ldap_distproc_t *lc = (ldap_distproc_t *)on->on_bi.bi_private;
828
829         BackendInfo     *bi = be->bd_info;
830         ldapinfo_t      *li;
831
832         int             t;
833
834         be->bd_info = lback;
835         be->be_private = NULL;
836         t = lback->bi_db_init( be );
837         if ( t != 0 ) {
838                 return t;
839         }
840         li = (ldapinfo_t *)be->be_private;
841
842         /* copy common data */
843         li->li_nretries = lc->lc_common_li->li_nretries;
844         li->li_flags = lc->lc_common_li->li_flags;
845         li->li_version = lc->lc_common_li->li_version;
846         for ( t = 0; t < LDAP_BACK_OP_LAST; t++ ) {
847                 li->li_timeout[ t ] = lc->lc_common_li->li_timeout[ t ];
848         }
849         be->bd_info = bi;
850
851         return 0;
852 }
853
854 typedef struct ldap_distproc_conn_apply_t {
855         BackendDB       *be;
856         Connection      *conn;
857 } ldap_distproc_conn_apply_t;
858
859 static int
860 ldap_distproc_conn_apply( void *datum, void *arg )
861 {
862         ldapinfo_t              *li = (ldapinfo_t *)datum;
863         ldap_distproc_conn_apply_t      *lca = (ldap_distproc_conn_apply_t *)arg;
864
865         lca->be->be_private = (void *)li;
866
867         return lback->bi_connection_destroy( lca->be, lca->conn );
868 }
869
870 static int
871 ldap_distproc_connection_destroy(
872         BackendDB *be,
873         Connection *conn
874 )
875 {
876         slap_overinst           *on = (slap_overinst *) be->bd_info;
877         ldap_distproc_t         *lc = (ldap_distproc_t *)on->on_bi.bi_private;
878         void                    *private = be->be_private;
879         ldap_distproc_conn_apply_t      lca;
880         int                     rc;
881
882         be->be_private = NULL;
883         lca.be = be;
884         lca.conn = conn;
885         ldap_pvt_thread_mutex_lock( &lc->lc_lai.lai_mutex );
886         rc = avl_apply( lc->lc_lai.lai_tree, ldap_distproc_conn_apply,
887                 (void *)&lca, 1, AVL_INORDER ) != AVL_NOMORE;
888         ldap_pvt_thread_mutex_unlock( &lc->lc_lai.lai_mutex );
889         be->be_private = private;
890
891         return rc;
892 }
893
894 static int
895 ldap_distproc_parse_returnContRef_ctrl(
896         Operation       *op,
897         SlapReply       *rs,
898         LDAPControl     *ctrl )
899 {
900         if ( get_returnContRef( op ) != SLAP_CONTROL_NONE ) {
901                 rs->sr_text = "returnContinuationReference control specified multiple times";
902                 return LDAP_PROTOCOL_ERROR;
903         }
904
905         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
906                 rs->sr_text = "returnContinuationReference control specified with pagedResults control";
907                 return LDAP_PROTOCOL_ERROR;
908         }
909
910         if ( !BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
911                 rs->sr_text = "returnContinuationReference control: value must be NULL";
912                 return LDAP_PROTOCOL_ERROR;
913         }
914
915         op->o_returnContRef = ctrl->ldctl_iscritical ? SLAP_CONTROL_CRITICAL : SLAP_CONTROL_NONCRITICAL;
916
917         return LDAP_SUCCESS;
918 }
919
920 static int
921 ldap_exop_chained_request(
922                 Operation       *op,
923                 SlapReply       *rs )
924 {
925         Statslog( LDAP_DEBUG_STATS, "%s CHAINED REQUEST\n",
926             op->o_log_prefix, 0, 0, 0, 0 );
927
928         rs->sr_err = backend_check_restrictions( op, rs, 
929                         (struct berval *)&slap_EXOP_CHAINEDREQUEST );
930         if ( rs->sr_err != LDAP_SUCCESS ) {
931                 return rs->sr_err;
932         }
933
934         /* by now, just reject requests */
935         rs->sr_text = "under development";
936         return LDAP_UNWILLING_TO_PERFORM;
937 }
938
939
940 static slap_overinst distproc;
941
942 int
943 distproc_initialize( void )
944 {
945         int     rc;
946
947         /* Make sure we don't exceed the bits reserved for userland */
948         config_check_userland( DP_LAST );
949
950         rc = load_extop( (struct berval *)&slap_EXOP_CHAINEDREQUEST,
951                 SLAP_EXOP_HIDE, ldap_exop_chained_request );
952         if ( rc != LDAP_SUCCESS ) {
953                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
954                         "unable to register chainedRequest exop: %d.\n",
955                         rc, 0, 0 );
956                 return rc;
957         }
958
959         rc = supported_feature_load( &slap_FEATURE_CANCHAINOPS );
960         if ( rc != LDAP_SUCCESS ) {
961                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
962                         "unable to register canChainOperations supported feature: %d.\n",
963                         rc, 0, 0 );
964                 return rc;
965         }
966
967         rc = register_supported_control( LDAP_CONTROL_X_RETURNCONTREF,
968                         SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, NULL,
969                         ldap_distproc_parse_returnContRef_ctrl, &sc_returnContRef );
970         if ( rc != LDAP_SUCCESS ) {
971                 Debug( LDAP_DEBUG_ANY, "slapd-distproc: "
972                         "unable to register returnContinuationReference control: %d.\n",
973                         rc, 0, 0 );
974                 return rc;
975         }
976
977         distproc.on_bi.bi_type = "distproc";
978         distproc.on_bi.bi_db_init = ldap_distproc_db_init;
979         distproc.on_bi.bi_db_config = ldap_distproc_db_config;
980         distproc.on_bi.bi_db_open = ldap_distproc_db_open;
981         distproc.on_bi.bi_db_close = ldap_distproc_db_close;
982         distproc.on_bi.bi_db_destroy = ldap_distproc_db_destroy;
983
984         /* ... otherwise the underlying backend's function would be called,
985          * likely passing an invalid entry; on the contrary, the requested
986          * operational attributes should have been returned while chasing
987          * the referrals.  This all in all is a bit messy, because part
988          * of the operational attributes are generated by the backend;
989          * part by the frontend; back-ldap should receive all the available
990          * ones from the remote server, but then, on its own, it strips those
991          * it assumes will be (re)generated by the frontend (e.g.
992          * subschemaSubentry, entryDN, ...) */
993         distproc.on_bi.bi_operational = ldap_distproc_operational;
994         
995         distproc.on_bi.bi_connection_destroy = ldap_distproc_connection_destroy;
996
997         distproc.on_response = ldap_distproc_response;
998
999         distproc.on_bi.bi_cf_ocs = distproc_ocs;
1000
1001         rc = config_register_schema( distproc_cfg, distproc_ocs );
1002         if ( rc ) {
1003                 return rc;
1004         }
1005
1006         return overlay_register( &distproc );
1007 }
1008
1009 #endif /* LDAP_DEVEL */