Debian Codegraph @

servers/slapd/back-ldap/distproc.c

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