Debian Codegraph @

servers/slapd/back-ldap/chain.c

raw
1 /* chain.c - chain LDAP operations */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2003-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 the Howard Chu for inclusion
19 * in OpenLDAP Software.
20 * This work was subsequently modified by Pierangelo Masarati.
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 "lutil.h"
31 #include "slap.h"
32 #include "back-ldap.h"
33 #include "slap-config.h"
34
35 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
36 #define SLAP_CHAINING_DEFAULT LDAP_CHAINING_PREFERRED
37 #define SLAP_CH_RESOLVE_SHIFT SLAP_CONTROL_SHIFT
38 #define SLAP_CH_RESOLVE_MASK (0x3 << SLAP_CH_RESOLVE_SHIFT)
39 #define SLAP_CH_RESOLVE_CHAINING_PREFERRED (LDAP_CHAINING_PREFERRED << SLAP_CH_RESOLVE_SHIFT)
40 #define SLAP_CH_RESOLVE_CHAINING_REQUIRED (LDAP_CHAINING_REQUIRED << SLAP_CH_RESOLVE_SHIFT)
41 #define SLAP_CH_RESOLVE_REFERRALS_PREFERRED (LDAP_REFERRALS_PREFERRED << SLAP_CH_RESOLVE_SHIFT)
42 #define SLAP_CH_RESOLVE_REFERRALS_REQUIRED (LDAP_REFERRALS_REQUIRED << SLAP_CH_RESOLVE_SHIFT)
43 #define SLAP_CH_RESOLVE_DEFAULT (SLAP_CHAINING_DEFAULT << SLAP_CH_RESOLVE_SHIFT)
44 #define SLAP_CH_CONTINUATION_SHIFT (SLAP_CH_RESOLVE_SHIFT + 2)
45 #define SLAP_CH_CONTINUATION_MASK (0x3 << SLAP_CH_CONTINUATION_SHIFT)
46 #define SLAP_CH_CONTINUATION_CHAINING_PREFERRED (LDAP_CHAINING_PREFERRED << SLAP_CH_CONTINUATION_SHIFT)
47 #define SLAP_CH_CONTINUATION_CHAINING_REQUIRED (LDAP_CHAINING_REQUIRED << SLAP_CH_CONTINUATION_SHIFT)
48 #define SLAP_CH_CONTINUATION_REFERRALS_PREFERRED (LDAP_REFERRALS_PREFERRED << SLAP_CH_CONTINUATION_SHIFT)
49 #define SLAP_CH_CONTINUATION_REFERRALS_REQUIRED (LDAP_REFERRALS_REQUIRED << SLAP_CH_CONTINUATION_SHIFT)
50 #define SLAP_CH_CONTINUATION_DEFAULT (SLAP_CHAINING_DEFAULT << SLAP_CH_CONTINUATION_SHIFT)
51
52 #define o_chaining o_ctrlflag[sc_chainingBehavior]
53 #define get_chaining(op) ((op)->o_chaining & SLAP_CONTROL_MASK)
54 #define get_chainingBehavior(op) ((op)->o_chaining & (SLAP_CH_RESOLVE_MASK|SLAP_CH_CONTINUATION_MASK))
55 #define get_resolveBehavior(op) ((op)->o_chaining & SLAP_CH_RESOLVE_MASK)
56 #define get_continuationBehavior(op) ((op)->o_chaining & SLAP_CH_CONTINUATION_MASK)
57
58 static int sc_chainingBehavior;
59 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
60
61 typedef enum {
62 LDAP_CH_NONE = 0,
63 LDAP_CH_RES,
64 LDAP_CH_ERR
65 } ldap_chain_status_t;
66
67 static BackendInfo *lback;
68
69 typedef struct ldap_chain_t {
70 /*
71 * A "template" ldapinfo_t gets all common configuration items;
72 * then, for each configured URI, an entry is created in the tree;
73 * all the specific configuration items get in the current URI
74 * structure.
75 *
76 * Then, for each referral, extract the URI and lookup the
77 * related structure. If configured to do so, allow URIs
78 * not found in the structure to create a temporary one
79 * that chains anonymously; maybe it can also be added to
80 * the tree? Should be all configurable.
81 */
82
83 /* "common" configuration info (anything occurring before an "uri") */
84 ldapinfo_t *lc_common_li;
85
86 /* current configuration info */
87 ldapinfo_t *lc_cfg_li;
88
89 /* tree of configured[/generated?] "uri" info */
90 ldap_avl_info_t lc_lai;
91
92 /* max depth in nested referrals chaining */
93 int lc_max_depth;
94
95 unsigned lc_flags;
96 #define LDAP_CHAIN_F_NONE (0x00U)
97 #define LDAP_CHAIN_F_CHAINING (0x01U)
98 #define LDAP_CHAIN_F_CACHE_URI (0x02U)
99 #define LDAP_CHAIN_F_RETURN_ERR (0x04U)
100
101 #define LDAP_CHAIN_ISSET(lc, f) ( ( (lc)->lc_flags & (f) ) == (f) )
102 #define LDAP_CHAIN_CHAINING( lc ) LDAP_CHAIN_ISSET( (lc), LDAP_CHAIN_F_CHAINING )
103 #define LDAP_CHAIN_CACHE_URI( lc ) LDAP_CHAIN_ISSET( (lc), LDAP_CHAIN_F_CACHE_URI )
104 #define LDAP_CHAIN_RETURN_ERR( lc ) LDAP_CHAIN_ISSET( (lc), LDAP_CHAIN_F_RETURN_ERR )
105
106 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
107 LDAPControl lc_chaining_ctrl;
108 char lc_chaining_ctrlflag;
109 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
110 } ldap_chain_t;
111
112 static int ldap_chain_db_init_common( BackendDB *be );
113 static int ldap_chain_db_init_one( BackendDB *be );
114 static int ldap_chain_db_open_one( BackendDB *be );
115 #define ldap_chain_db_close_one(be) (0)
116 #define ldap_chain_db_destroy_one(be, rs) (lback)->bi_db_destroy( (be), (rs) )
117
118 typedef struct ldap_chain_cb_t {
119 ldap_chain_status_t lb_status;
120 ldap_chain_t *lb_lc;
121 slap_operation_t lb_op_type;
122 char *lb_text;
123 int lb_depth;
124 } ldap_chain_cb_t;
125
126 static int
127 ldap_chain_op(
128 Operation *op,
129 SlapReply *rs,
130 slap_operation_t op_type,
131 BerVarray ref,
132 int depth );
133
134 static int
135 ldap_chain_search(
136 Operation *op,
137 SlapReply *rs,
138 BerVarray ref,
139 int depth );
140
141 static slap_overinst ldapchain;
142
143 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
144 static int
145 chaining_control_add(
146 ldap_chain_t *lc,
147 Operation *op,
148 LDAPControl ***oldctrlsp )
149 {
150 LDAPControl **ctrls = NULL;
151 int c = 0;
152
153 *oldctrlsp = op->o_ctrls;
154
155 /* default chaining control not defined */
156 if ( !LDAP_CHAIN_CHAINING( lc ) ) {
157 return 0;
158 }
159
160 /* already present */
161 if ( get_chaining( op ) > SLAP_CONTROL_IGNORED ) {
162 return 0;
163 }
164
165 /* FIXME: check other incompatibilities */
166
167 /* add to other controls */
168 if ( op->o_ctrls ) {
169 for ( c = 0; op->o_ctrls[ c ]; c++ )
170 /* count them */ ;
171 }
172
173 ctrls = ch_calloc( sizeof( LDAPControl *), c + 2 );
174 ctrls[ 0 ] = &lc->lc_chaining_ctrl;
175 if ( op->o_ctrls ) {
176 for ( c = 0; op->o_ctrls[ c ]; c++ ) {
177 ctrls[ c + 1 ] = op->o_ctrls[ c ];
178 }
179 }
180 ctrls[ c + 1 ] = NULL;
181
182 op->o_ctrls = ctrls;
183
184 op->o_chaining = lc->lc_chaining_ctrlflag;
185
186 return 0;
187 }
188
189 static int
190 chaining_control_remove(
191 Operation *op,
192 LDAPControl ***oldctrlsp )
193 {
194 LDAPControl **oldctrls = *oldctrlsp;
195
196 /* we assume that the first control is the chaining control
197 * added by the chain overlay, so it's the only one we explicitly
198 * free */
199 if ( op->o_ctrls != oldctrls ) {
200 if ( op->o_ctrls != NULL ) {
201 assert( op->o_ctrls[ 0 ] != NULL );
202
203 free( op->o_ctrls );
204
205 op->o_chaining = 0;
206 }
207 op->o_ctrls = oldctrls;
208 }
209
210 *oldctrlsp = NULL;
211
212 return 0;
213 }
214 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
215
216 static int
217 ldap_chain_uri_cmp( const void *c1, const void *c2 )
218 {
219 const ldapinfo_t *li1 = (const ldapinfo_t *)c1;
220 const ldapinfo_t *li2 = (const ldapinfo_t *)c2;
221
222 assert( li1->li_bvuri != NULL );
223 assert( !BER_BVISNULL( &li1->li_bvuri[ 0 ] ) );
224 assert( BER_BVISNULL( &li1->li_bvuri[ 1 ] ) );
225
226 assert( li2->li_bvuri != NULL );
227 assert( !BER_BVISNULL( &li2->li_bvuri[ 0 ] ) );
228 assert( BER_BVISNULL( &li2->li_bvuri[ 1 ] ) );
229
230 return ber_bvcmp( &li1->li_bvuri[ 0 ], &li2->li_bvuri[ 0 ] );
231 }
232
233 static int
234 ldap_chain_uri_dup( void *c1, void *c2 )
235 {
236 ldapinfo_t *li1 = (ldapinfo_t *)c1;
237 ldapinfo_t *li2 = (ldapinfo_t *)c2;
238
239 assert( li1->li_bvuri != NULL );
240 assert( !BER_BVISNULL( &li1->li_bvuri[ 0 ] ) );
241 assert( BER_BVISNULL( &li1->li_bvuri[ 1 ] ) );
242
243 assert( li2->li_bvuri != NULL );
244 assert( !BER_BVISNULL( &li2->li_bvuri[ 0 ] ) );
245 assert( BER_BVISNULL( &li2->li_bvuri[ 1 ] ) );
246
247 if ( ber_bvcmp( &li1->li_bvuri[ 0 ], &li2->li_bvuri[ 0 ] ) == 0 ) {
248 return -1;
249 }
250
251 return 0;
252 }
253
254 /*
255 * Search specific response that strips entryDN from entries
256 */
257 static int
258 ldap_chain_cb_search_response( Operation *op, SlapReply *rs )
259 {
260 ldap_chain_cb_t *lb = (ldap_chain_cb_t *)op->o_callback->sc_private;
261
262 assert( op->o_tag == LDAP_REQ_SEARCH );
263
264 /* if in error, don't proceed any further */
265 if ( lb->lb_status == LDAP_CH_ERR ) {
266 return 0;
267 }
268
269 if ( rs->sr_type == REP_SEARCH ) {
270 Attribute **ap = &rs->sr_entry->e_attrs;
271
272 for ( ; *ap != NULL; ap = &(*ap)->a_next ) {
273 /* will be generated later by frontend
274 * (a cleaner solution would be that
275 * the frontend checks if it already exists */
276 if ( ad_cmp( (*ap)->a_desc, slap_schema.si_ad_entryDN ) == 0 )
277 {
278 Attribute *a = *ap;
279
280 *ap = (*ap)->a_next;
281 attr_free( a );
282
283 /* there SHOULD be one only! */
284 break;
285 }
286 }
287
288 /* tell the frontend not to add generated
289 * operational attributes */
290 rs->sr_flags |= REP_NO_OPERATIONALS;
291
292 return SLAP_CB_CONTINUE;
293
294 } else if ( rs->sr_type == REP_SEARCHREF ) {
295 /* if we get it here, it means the library was unable
296 * to chase the referral... */
297 if ( lb->lb_depth < lb->lb_lc->lc_max_depth && rs->sr_ref != NULL ) {
298 rs->sr_err = ldap_chain_search( op, rs, rs->sr_ref, lb->lb_depth );
299 }
300
301 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
302 if ( rs->sr_err == LDAP_REFERRAL && get_chaining( op ) > SLAP_CONTROL_IGNORED ) {
303 switch ( get_continuationBehavior( op ) ) {
304 case SLAP_CH_RESOLVE_CHAINING_REQUIRED:
305 lb->lb_status = LDAP_CH_ERR;
306 return rs->sr_err = LDAP_X_CANNOT_CHAIN;
307
308 default:
309 break;
310 }
311 }
312 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
313 return SLAP_CB_CONTINUE;
314
315 } else if ( rs->sr_type == REP_RESULT ) {
316 if ( rs->sr_err == LDAP_REFERRAL
317 && lb->lb_depth < lb->lb_lc->lc_max_depth
318 && rs->sr_ref != NULL )
319 {
320 rs->sr_err = ldap_chain_op( op, rs, lb->lb_op_type,
321 rs->sr_ref, lb->lb_depth );
322 }
323
324 /* back-ldap tried to send result */
325 lb->lb_status = LDAP_CH_RES;
326 /* don't let other callbacks run, this isn't
327 * the real result for this op.
328 */
329 op->o_callback->sc_next = NULL;
330 }
331
332 return 0;
333 }
334
335 /*
336 * Dummy response that simply traces if back-ldap tried to send
337 * anything to the client
338 */
339 static int
340 ldap_chain_cb_response( Operation *op, SlapReply *rs )
341 {
342 ldap_chain_cb_t *lb = (ldap_chain_cb_t *)op->o_callback->sc_private;
343
344 /* if in error, don't proceed any further */
345 if ( lb->lb_status == LDAP_CH_ERR ) {
346 return 0;
347 }
348
349 if ( rs->sr_type == REP_RESULT ) {
350 retry:;
351 switch ( rs->sr_err ) {
352 case LDAP_COMPARE_TRUE:
353 case LDAP_COMPARE_FALSE:
354 if ( op->o_tag != LDAP_REQ_COMPARE ) {
355 return rs->sr_err;
356 }
357 /* fallthru */
358
359 case LDAP_SUCCESS:
360 lb->lb_status = LDAP_CH_RES;
361 break;
362
363 case LDAP_REFERRAL:
364 if ( lb->lb_depth < lb->lb_lc->lc_max_depth && rs->sr_ref != NULL ) {
365 rs->sr_err = ldap_chain_op( op, rs, lb->lb_op_type,
366 rs->sr_ref, lb->lb_depth );
367 goto retry;
368 }
369
370 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
371 if ( get_chaining( op ) > SLAP_CONTROL_IGNORED ) {
372 switch ( get_continuationBehavior( op ) ) {
373 case SLAP_CH_RESOLVE_CHAINING_REQUIRED:
374 lb->lb_status = LDAP_CH_ERR;
375 return rs->sr_err = LDAP_X_CANNOT_CHAIN;
376
377 default:
378 break;
379 }
380 }
381 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
382 break;
383
384 default:
385 /* remember the text before it's freed in ldap_back_op_result */
386 if ( lb->lb_text ) {
387 ber_memfree_x( lb->lb_text, op->o_tmpmemctx );
388 }
389 lb->lb_text = ber_strdup_x( rs->sr_text, op->o_tmpmemctx );
390 return rs->sr_err;
391 }
392
393 } else if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH )
394 {
395 /* strip the entryDN attribute, but keep returning results */
396 (void)ldap_chain_cb_search_response( op, rs );
397 }
398
399 return SLAP_CB_CONTINUE;
400 }
401
402 static int
403 ldap_chain_op(
404 Operation *op,
405 SlapReply *rs,
406 slap_operation_t op_type,
407 BerVarray ref,
408 int depth )
409 {
410 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
411 ldap_chain_cb_t *lb = (ldap_chain_cb_t *)op->o_callback->sc_private;
412 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
413 struct berval odn = op->o_req_dn,
414 ondn = op->o_req_ndn;
415 ldapinfo_t li = { 0 }, *lip = NULL;
416 struct berval bvuri[ 2 ] = { { 0 } };
417
418 /* NOTE: returned if ref is empty... */
419 int rc = LDAP_OTHER,
420 first_rc;
421
422 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
423 LDAPControl **ctrls = NULL;
424
425 (void)chaining_control_add( lc, op, &ctrls );
426 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
427
428 li.li_bvuri = bvuri;
429 first_rc = -1;
430 for ( ; !BER_BVISNULL( ref ); ref++ ) {
431 SlapReply rs2 = { 0 };
432 LDAPURLDesc *srv = NULL;
433 req_search_s save_oq_search = op->oq_search,
434 tmp_oq_search = { 0 };
435 struct berval dn = BER_BVNULL,
436 pdn = odn,
437 ndn = ondn;
438 char *filter = NULL;
439 int temporary = 0;
440 int free_dn = 0;
441
442 /* We're setting the URI of the first referral;
443 * what if there are more?
444
445 Document: RFC 4511
446
447 4.1.10. Referral
448 ...
449 If the client wishes to progress the operation, it MUST follow the
450 referral by contacting one of the supported services. If multiple
451 URIs are present, the client assumes that any supported URI may be
452 used to progress the operation.
453
454 * so we actually need to follow exactly one,
455 * and we can assume any is fine.
456 */
457
458 /* parse reference and use
459 * proto://[host][:port]/ only */
460 rc = ldap_url_parse_ext( ref->bv_val, &srv, LDAP_PVT_URL_PARSE_NONE );
461 if ( rc != LDAP_URL_SUCCESS ) {
462 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_op: unable to parse ref=\"%s\"\n",
463 op->o_log_prefix, ref->bv_val );
464
465 /* try next */
466 rc = LDAP_OTHER;
467 continue;
468 }
469
470 if ( op->o_tag == LDAP_REQ_SEARCH ) {
471 if ( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
472 /* RFC 4511: if scope is present, use it */
473 tmp_oq_search.rs_scope = srv->lud_scope;
474
475 } else {
476 /* RFC 4511: if scope is absent, use original */
477 tmp_oq_search.rs_scope = op->ors_scope;
478 }
479 }
480
481 rc = LDAP_SUCCESS;
482 srv->lud_scope = LDAP_SCOPE_DEFAULT;
483 dn.bv_val = srv->lud_dn;
484 filter = srv->lud_filter;
485
486 /* normalize DN */
487 if ( srv->lud_dn == NULL || srv->lud_dn[0] == '\0' ) {
488 if ( srv->lud_dn == NULL ) {
489 srv->lud_dn = "";
490 }
491
492 } else {
493 ber_str2bv( srv->lud_dn, 0, 0, &dn );
494 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, op->o_tmpmemctx );
495 if ( rc == LDAP_SUCCESS ) {
496 /* remove DN essentially because later on
497 * ldap_initialize() will parse the URL
498 * as a comma-separated URL list */
499 srv->lud_dn = "";
500 free_dn = 1;
501 }
502 }
503
504 /* prepare filter */
505 if ( rc == LDAP_SUCCESS && op->o_tag == LDAP_REQ_SEARCH ) {
506 /* filter */
507 if ( srv->lud_filter != NULL
508 && srv->lud_filter[0] != '\0'
509 && strcasecmp( srv->lud_filter, "(objectClass=*)" ) != 0 )
510 {
511 /* RFC 4511: if filter is present, use it;
512 * otherwise, use original */
513 tmp_oq_search.rs_filter = str2filter_x( op, srv->lud_filter );
514 if ( tmp_oq_search.rs_filter != NULL ) {
515 filter2bv_x( op, tmp_oq_search.rs_filter, &tmp_oq_search.rs_filterstr );
516
517 } else {
518 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_op: ref=\"%s\": unable to parse filter=\"%s\"\n",
519 op->o_log_prefix, ref->bv_val, srv->lud_filter );
520 rc = LDAP_OTHER;
521 }
522 }
523 }
524 srv->lud_filter = NULL;
525
526 if ( rc == LDAP_SUCCESS ) {
527 li.li_uri = ldap_url_desc2str( srv );
528 }
529
530 srv->lud_dn = dn.bv_val;
531 srv->lud_filter = filter;
532 ldap_free_urldesc( srv );
533
534 if ( rc != LDAP_SUCCESS ) {
535 /* try next */
536 rc = LDAP_OTHER;
537 continue;
538 }
539
540 if ( li.li_uri == NULL ) {
541 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_op: ref=\"%s\" unable to reconstruct URI\n",
542 op->o_log_prefix, ref->bv_val );
543
544 /* try next */
545 rc = LDAP_OTHER;
546 goto further_cleanup;
547 }
548
549 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_op: ref=\"%s\" -> \"%s\"\n",
550 op->o_log_prefix, ref->bv_val, li.li_uri );
551
552 op->o_req_dn = pdn;
553 op->o_req_ndn = ndn;
554
555 if ( op->o_tag == LDAP_REQ_SEARCH ) {
556 op->ors_scope = tmp_oq_search.rs_scope;
557 if ( tmp_oq_search.rs_filter != NULL ) {
558 op->ors_filter = tmp_oq_search.rs_filter;
559 op->ors_filterstr = tmp_oq_search.rs_filterstr;
560 }
561 }
562
563 ber_str2bv( li.li_uri, 0, 0, &li.li_bvuri[ 0 ] );
564
565 /* Searches for a ldapinfo in the avl tree */
566 ldap_pvt_thread_mutex_lock( &lc->lc_lai.lai_mutex );
567 lip = (ldapinfo_t *)ldap_tavl_find( lc->lc_lai.lai_tree,
568 (caddr_t)&li, ldap_chain_uri_cmp );
569 ldap_pvt_thread_mutex_unlock( &lc->lc_lai.lai_mutex );
570
571 if ( lip != NULL ) {
572 op->o_bd->be_private = (void *)lip;
573
574 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_op: ref=\"%s\": URI=\"%s\" found in cache\n",
575 op->o_log_prefix, ref->bv_val, li.li_uri );
576
577 } else {
578 rc = ldap_chain_db_init_one( op->o_bd );
579 if ( rc != 0 ) {
580 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_op: ref=\"%s\" unable to init back-ldap for URI=\"%s\"\n",
581 op->o_log_prefix, ref->bv_val, li.li_uri );
582 goto cleanup;
583 }
584 lip = (ldapinfo_t *)op->o_bd->be_private;
585 lip->li_uri = li.li_uri;
586 lip->li_bvuri = bvuri;
587 rc = ldap_chain_db_open_one( op->o_bd );
588 if ( rc != 0 ) {
589 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_op: ref=\"%s\" unable to open back-ldap for URI=\"%s\"\n",
590 op->o_log_prefix, ref->bv_val, li.li_uri );
591 lip->li_uri = NULL;
592 lip->li_bvuri = NULL;
593 (void)ldap_chain_db_destroy_one( op->o_bd, NULL);
594 goto cleanup;
595 }
596
597 if ( LDAP_CHAIN_CACHE_URI( lc ) ) {
598 ldap_pvt_thread_mutex_lock( &lc->lc_lai.lai_mutex );
599 if ( ldap_tavl_insert( &lc->lc_lai.lai_tree,
600 (caddr_t)lip, ldap_chain_uri_cmp, ldap_chain_uri_dup ) )
601 {
602 /* someone just inserted another;
603 * don't bother, use this and then
604 * just free it */
605 temporary = 1;
606 }
607 ldap_pvt_thread_mutex_unlock( &lc->lc_lai.lai_mutex );
608
609 } else {
610 temporary = 1;
611 }
612
613 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_op: ref=\"%s\" %s\n",
614 op->o_log_prefix, ref->bv_val, temporary ? "temporary" : "caching" );
615 }
616
617 lb->lb_op_type = op_type;
618 lb->lb_depth = depth + 1;
619
620 rc = (&lback->bi_op_bind)[ op_type ]( op, &rs2 );
621
622 /* note the first error */
623 if ( first_rc == -1 ) {
624 first_rc = rc;
625 }
626
627 cleanup:;
628 ldap_memfree( li.li_uri );
629 li.li_uri = NULL;
630
631 if ( temporary ) {
632 lip->li_uri = NULL;
633 lip->li_bvuri = NULL;
634 (void)ldap_chain_db_close_one( op->o_bd );
635 (void)ldap_chain_db_destroy_one( op->o_bd, NULL );
636 }
637
638 further_cleanup:;
639 if ( op->o_req_dn.bv_val == pdn.bv_val ) {
640 op->o_req_dn = odn;
641 op->o_req_ndn = ondn;
642 }
643
644 if ( free_dn ) {
645 op->o_tmpfree( pdn.bv_val, op->o_tmpmemctx );
646 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
647 }
648
649 if ( op->o_tag == LDAP_REQ_SEARCH ) {
650 if ( tmp_oq_search.rs_filter != NULL ) {
651 filter_free_x( op, tmp_oq_search.rs_filter, 1 );
652 }
653
654 if ( !BER_BVISNULL( &tmp_oq_search.rs_filterstr ) ) {
655 slap_sl_free( tmp_oq_search.rs_filterstr.bv_val, op->o_tmpmemctx );
656 }
657
658 op->oq_search = save_oq_search;
659 }
660
661 if ( rc == LDAP_SUCCESS && rs2.sr_err == LDAP_SUCCESS ) {
662 *rs = rs2;
663 break;
664 }
665
666 rc = rs2.sr_err;
667 }
668
669 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
670 (void)chaining_control_remove( op, &ctrls );
671 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
672
673 if ( rc != LDAP_SUCCESS && first_rc > 0 ) {
674 rc = first_rc;
675 }
676
677 return rc;
678 }
679
680 static int
681 ldap_chain_search(
682 Operation *op,
683 SlapReply *rs,
684 BerVarray ref,
685 int depth )
686
687 {
688 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
689 ldap_chain_cb_t *lb = (ldap_chain_cb_t *)op->o_callback->sc_private;
690 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
691 ldapinfo_t li = { 0 }, *lip = NULL;
692 struct berval bvuri[ 2 ] = { { 0 } };
693
694 struct berval odn = op->o_req_dn,
695 ondn = op->o_req_ndn;
696 Entry *save_entry = rs->sr_entry;
697 slap_mask_t save_flags = rs->sr_flags;
698
699 int rc = LDAP_OTHER,
700 first_rc = -1;
701
702 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
703 LDAPControl **ctrls = NULL;
704
705 (void)chaining_control_add( lc, op, &ctrls );
706 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
707
708 assert( rs->sr_type == REP_SEARCHREF );
709
710 rs->sr_type = REP_SEARCH;
711
712 /* if we parse the URI then by no means
713 * we can cache stuff or reuse connections,
714 * because in back-ldap there's no caching
715 * based on the URI value, which is supposed
716 * to be set once for all (correct?) */
717 li.li_bvuri = bvuri;
718 for ( ; !BER_BVISNULL( &ref[0] ); ref++ ) {
719 SlapReply rs2 = { REP_RESULT };
720 LDAPURLDesc *srv;
721 req_search_s save_oq_search = op->oq_search,
722 tmp_oq_search = { 0 };
723 struct berval dn,
724 pdn = op->o_req_dn,
725 ndn = op->o_req_ndn;
726 char *filter = NULL;
727 int temporary = 0;
728 int free_dn = 0;
729
730 /* parse reference and use
731 * proto://[host][:port]/ only */
732 rc = ldap_url_parse_ext( ref[0].bv_val, &srv, LDAP_PVT_URL_PARSE_NONE );
733 if ( rc != LDAP_URL_SUCCESS ) {
734 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_search: unable to parse ref=\"%s\"\n",
735 op->o_log_prefix, ref->bv_val );
736
737 /* try next */
738 rs->sr_err = LDAP_OTHER;
739 continue;
740 }
741
742 if ( srv->lud_scope != LDAP_SCOPE_DEFAULT ) {
743 /* RFC 4511: if scope is present, use it */
744 tmp_oq_search.rs_scope = srv->lud_scope;
745
746 } else {
747 /* RFC 4511: if scope is absent, use original */
748 /* Section 4.5.3: if scope is onelevel, use base */
749 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
750 tmp_oq_search.rs_scope = LDAP_SCOPE_BASE;
751 else
752 tmp_oq_search.rs_scope = op->ors_scope;
753 }
754
755 rc = LDAP_SUCCESS;
756 srv->lud_scope = LDAP_SCOPE_DEFAULT;
757 dn.bv_val = srv->lud_dn;
758 filter = srv->lud_filter;
759
760 /* normalize DN */
761 if ( srv->lud_dn == NULL || srv->lud_dn[0] == '\0' ) {
762 if ( srv->lud_dn == NULL ) {
763 srv->lud_dn = "";
764 }
765
766 if ( save_entry != NULL ) {
767 /* use the "right" DN, if available */
768 pdn = save_entry->e_name;
769 ndn = save_entry->e_nname;
770 } /* else leave the original req DN in place, if any RFC 4511 */
771
772 } else {
773 /* RFC 4511: if DN is present, use it */
774 ber_str2bv( srv->lud_dn, 0, 0, &dn );
775 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, op->o_tmpmemctx );
776 if ( rc == LDAP_SUCCESS ) {
777 /* remove DN essentially because later on
778 * ldap_initialize() will parse the URL
779 * as a comma-separated URL list */
780 srv->lud_dn = "";
781 free_dn = 1;
782 }
783 }
784
785 /* prepare filter */
786 if ( rc == LDAP_SUCCESS ) {
787 /* filter */
788 if ( srv->lud_filter != NULL
789 && srv->lud_filter[0] != '\0'
790 && strcasecmp( srv->lud_filter, "(objectClass=*)" ) != 0 )
791 {
792 /* RFC 4511: if filter is present, use it;
793 * otherwise, use original */
794 tmp_oq_search.rs_filter = str2filter_x( op, srv->lud_filter );
795 if ( tmp_oq_search.rs_filter != NULL ) {
796 filter2bv_x( op, tmp_oq_search.rs_filter, &tmp_oq_search.rs_filterstr );
797
798 } else {
799 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_search: ref=\"%s\": unable to parse filter=\"%s\"\n",
800 op->o_log_prefix, ref->bv_val, srv->lud_filter );
801 rc = LDAP_OTHER;
802 }
803 }
804 }
805 srv->lud_filter = NULL;
806
807 if ( rc == LDAP_SUCCESS ) {
808 li.li_uri = ldap_url_desc2str( srv );
809 }
810
811 srv->lud_dn = dn.bv_val;
812 srv->lud_filter = filter;
813 ldap_free_urldesc( srv );
814
815 if ( rc != LDAP_SUCCESS || li.li_uri == NULL ) {
816 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_search: ref=\"%s\" unable to reconstruct URI\n",
817 op->o_log_prefix, ref->bv_val );
818
819 /* try next */
820 rc = LDAP_OTHER;
821 goto further_cleanup;
822 }
823
824 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_search: ref=\"%s\" -> \"%s\"\n",
825 op->o_log_prefix, ref->bv_val, li.li_uri );
826
827 op->o_req_dn = pdn;
828 op->o_req_ndn = ndn;
829 op->ors_scope = tmp_oq_search.rs_scope;
830 if ( tmp_oq_search.rs_filter != NULL ) {
831 op->ors_filter = tmp_oq_search.rs_filter;
832 op->ors_filterstr = tmp_oq_search.rs_filterstr;
833 }
834
835 ber_str2bv( li.li_uri, 0, 0, &li.li_bvuri[ 0 ] );
836
837 /* Searches for a ldapinfo in the avl tree */
838 ldap_pvt_thread_mutex_lock( &lc->lc_lai.lai_mutex );
839 lip = (ldapinfo_t *)ldap_tavl_find( lc->lc_lai.lai_tree,
840 (caddr_t)&li, ldap_chain_uri_cmp );
841 ldap_pvt_thread_mutex_unlock( &lc->lc_lai.lai_mutex );
842
843 if ( lip != NULL ) {
844 op->o_bd->be_private = (void *)lip;
845
846 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_search: ref=\"%s\": URI=\"%s\" found in cache\n",
847 op->o_log_prefix, ref->bv_val, li.li_uri );
848
849 } else {
850 /* if none is found, create a temporary... */
851 rc = ldap_chain_db_init_one( op->o_bd );
852 if ( rc != 0 ) {
853 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_search: ref=\"%s\" unable to init back-ldap for URI=\"%s\"\n",
854 op->o_log_prefix, ref->bv_val, li.li_uri );
855 goto cleanup;
856 }
857 lip = (ldapinfo_t *)op->o_bd->be_private;
858 lip->li_uri = li.li_uri;
859 lip->li_bvuri = bvuri;
860 rc = ldap_chain_db_open_one( op->o_bd );
861 if ( rc != 0 ) {
862 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_search: ref=\"%s\" unable to open back-ldap for URI=\"%s\"\n",
863 op->o_log_prefix, ref->bv_val, li.li_uri );
864 lip->li_uri = NULL;
865 lip->li_bvuri = NULL;
866 (void)ldap_chain_db_destroy_one( op->o_bd, NULL );
867 goto cleanup;
868 }
869
870 if ( LDAP_CHAIN_CACHE_URI( lc ) ) {
871 ldap_pvt_thread_mutex_lock( &lc->lc_lai.lai_mutex );
872 if ( ldap_tavl_insert( &lc->lc_lai.lai_tree,
873 (caddr_t)lip, ldap_chain_uri_cmp, ldap_chain_uri_dup ) )
874 {
875 /* someone just inserted another;
876 * don't bother, use this and then
877 * just free it */
878 temporary = 1;
879 }
880 ldap_pvt_thread_mutex_unlock( &lc->lc_lai.lai_mutex );
881
882 } else {
883 temporary = 1;
884 }
885
886 Debug( LDAP_DEBUG_TRACE, "%s ldap_chain_search: ref=\"%s\" %s\n",
887 op->o_log_prefix, ref->bv_val, temporary ? "temporary" : "caching" );
888 }
889
890 lb->lb_op_type = op_search;
891 lb->lb_depth = depth + 1;
892
893 /* FIXME: should we also copy filter and scope?
894 * according to RFC3296, no */
895 rc = lback->bi_op_search( op, &rs2 );
896 if ( first_rc == -1 ) {
897 first_rc = rc;
898 }
899
900 cleanup:;
901 ldap_memfree( li.li_uri );
902 li.li_uri = NULL;
903
904 if ( temporary ) {
905 lip->li_uri = NULL;
906 lip->li_bvuri = NULL;
907 (void)ldap_chain_db_close_one( op->o_bd );
908 (void)ldap_chain_db_destroy_one( op->o_bd, NULL );
909 }
910
911 further_cleanup:;
912 if ( op->o_req_dn.bv_val == pdn.bv_val ) {
913 op->o_req_dn = odn;
914 op->o_req_ndn = ondn;
915 }
916
917 if ( free_dn ) {
918 op->o_tmpfree( pdn.bv_val, op->o_tmpmemctx );
919 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
920 }
921
922 if ( tmp_oq_search.rs_filter != NULL ) {
923 filter_free_x( op, tmp_oq_search.rs_filter, 1 );
924 }
925
926 if ( !BER_BVISNULL( &tmp_oq_search.rs_filterstr ) ) {
927 slap_sl_free( tmp_oq_search.rs_filterstr.bv_val, op->o_tmpmemctx );
928 }
929
930 op->oq_search = save_oq_search;
931
932 if ( rc == LDAP_SUCCESS && rs2.sr_err == LDAP_SUCCESS ) {
933 *rs = rs2;
934 break;
935 }
936
937 rc = rs2.sr_err;
938 }
939
940 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
941 (void)chaining_control_remove( op, &ctrls );
942 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
943
944 rs->sr_type = REP_SEARCHREF;
945 rs->sr_entry = save_entry;
946 rs->sr_flags = save_flags;
947
948 if ( rc != LDAP_SUCCESS ) {
949 /* couldn't chase any of the referrals */
950 if ( first_rc != -1 ) {
951 rc = first_rc;
952
953 } else {
954 rc = SLAP_CB_CONTINUE;
955 }
956 }
957
958 return rc;
959 }
960
961 static int
962 ldap_chain_response( Operation *op, SlapReply *rs )
963 {
964 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
965 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
966 BackendDB db, *bd = op->o_bd;
967 ldap_chain_cb_t lb = { 0 };
968 slap_callback *sc = op->o_callback,
969 sc2 = { 0 };
970 int rc = 0;
971 const char *text = NULL;
972 const char *matched;
973 BerVarray ref;
974 slap_mask_t flags = 0;
975 struct berval ndn = op->o_ndn;
976
977 int sr_err = rs->sr_err;
978 slap_reply_t sr_type = rs->sr_type;
979 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
980 slap_mask_t chain_mask = 0;
981 ber_len_t chain_shift = 0;
982 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
983
984 if ( rs->sr_err != LDAP_REFERRAL && rs->sr_type != REP_SEARCHREF ) {
985 return SLAP_CB_CONTINUE;
986 }
987 if ( !rs->sr_ref ) {
988 return SLAP_CB_CONTINUE;
989 }
990
991 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
992 if ( rs->sr_err == LDAP_REFERRAL && get_chaining( op ) > SLAP_CONTROL_IGNORED ) {
993 switch ( get_resolveBehavior( op ) ) {
994 case SLAP_CH_RESOLVE_REFERRALS_PREFERRED:
995 case SLAP_CH_RESOLVE_REFERRALS_REQUIRED:
996 return SLAP_CB_CONTINUE;
997
998 default:
999 chain_mask = SLAP_CH_RESOLVE_MASK;
1000 chain_shift = SLAP_CH_RESOLVE_SHIFT;
1001 break;
1002 }
1003
1004 } else if ( rs->sr_type == REP_SEARCHREF && get_chaining( op ) > SLAP_CONTROL_IGNORED ) {
1005 switch ( get_continuationBehavior( op ) ) {
1006 case SLAP_CH_CONTINUATION_REFERRALS_PREFERRED:
1007 case SLAP_CH_CONTINUATION_REFERRALS_REQUIRED:
1008 return SLAP_CB_CONTINUE;
1009
1010 default:
1011 chain_mask = SLAP_CH_CONTINUATION_MASK;
1012 chain_shift = SLAP_CH_CONTINUATION_SHIFT;
1013 break;
1014 }
1015 }
1016 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1017
1018 /*
1019 * TODO: add checks on who/when chain operations; e.g.:
1020 * a) what identities are authorized
1021 * b) what request DN (e.g. only chain requests rooted at <DN>)
1022 * c) what referral URIs
1023 * d) what protocol scheme (e.g. only ldaps://)
1024 * e) what ssf
1025 */
1026
1027 db = *op->o_bd;
1028 SLAP_DBFLAGS( &db ) &= ~SLAP_DBFLAG_MONITORING;
1029 op->o_bd = &db;
1030
1031 text = rs->sr_text;
1032 rs->sr_text = NULL;
1033 matched = rs->sr_matched;
1034 rs->sr_matched = NULL;
1035 ref = rs->sr_ref;
1036 rs->sr_ref = NULL;
1037
1038 flags = rs->sr_flags & (REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED);
1039 rs->sr_flags &= ~flags;
1040
1041 /* we need this to know if back-ldap returned any result */
1042 lb.lb_lc = lc;
1043 sc2.sc_next = sc;
1044 sc2.sc_private = &lb;
1045 sc2.sc_response = ldap_chain_cb_response;
1046 op->o_callback = &sc2;
1047
1048 /* Chaining can be performed by a privileged user on behalf
1049 * of normal users, using the ProxyAuthz control, by exploiting
1050 * the identity assertion feature of back-ldap; see idassert-*
1051 * directives in slapd-ldap(5).
1052 *
1053 * FIXME: the idassert-authcDN is one, will it be fine regardless
1054 * of the URI we obtain from the referral?
1055 */
1056
1057 switch ( op->o_tag ) {
1058 case LDAP_REQ_BIND: {
1059 struct berval rndn = op->o_req_ndn;
1060 Connection *conn = op->o_conn;
1061
1062 /* FIXME: can we really get a referral for binds? */
1063 op->o_req_ndn = slap_empty_bv;
1064 op->o_conn = NULL;
1065 rc = ldap_chain_op( op, rs, op_bind, ref, 0 );
1066 op->o_req_ndn = rndn;
1067 op->o_conn = conn;
1068 }
1069 break;
1070
1071 case LDAP_REQ_ADD:
1072 rc = ldap_chain_op( op, rs, op_add, ref, 0 );
1073 break;
1074
1075 case LDAP_REQ_DELETE:
1076 rc = ldap_chain_op( op, rs, op_delete, ref, 0 );
1077 break;
1078
1079 case LDAP_REQ_MODRDN:
1080 rc = ldap_chain_op( op, rs, op_modrdn, ref, 0 );
1081 break;
1082
1083 case LDAP_REQ_MODIFY:
1084 rc = ldap_chain_op( op, rs, op_modify, ref, 0 );
1085 break;
1086
1087 case LDAP_REQ_COMPARE:
1088 rc = ldap_chain_op( op, rs, op_compare, ref, 0 );
1089 if ( rs->sr_err == LDAP_COMPARE_TRUE || rs->sr_err == LDAP_COMPARE_FALSE ) {
1090 rc = LDAP_SUCCESS;
1091 }
1092 break;
1093
1094 case LDAP_REQ_SEARCH:
1095 if ( rs->sr_type == REP_SEARCHREF ) {
1096 sc2.sc_response = ldap_chain_cb_search_response;
1097 rc = ldap_chain_search( op, rs, ref, 0 );
1098
1099 } else {
1100 /* we might get here before any database actually
1101 * performed a search; in those cases, we need
1102 * to check limits, to make sure safe defaults
1103 * are in place */
1104 if ( op->ors_limit != NULL || limits_check( op, rs ) == 0 ) {
1105 rc = ldap_chain_op( op, rs, op_search, ref, 0 );
1106 } else {
1107 rc = SLAP_CB_CONTINUE;
1108 }
1109 }
1110 break;
1111
1112 case LDAP_REQ_EXTENDED:
1113 rc = ldap_chain_op( op, rs, op_extended, ref, 0 );
1114 /* FIXME: ldap_back_extended() by design
1115 * doesn't send result; frontend is expected
1116 * to send it... */
1117 /* FIXME: what about chaining? */
1118 if ( rc != SLAPD_ABANDON ) {
1119 rs->sr_err = rc;
1120 send_ldap_extended( op, rs );
1121 rc = LDAP_SUCCESS;
1122 }
1123 lb.lb_status = LDAP_CH_RES;
1124 break;
1125
1126 default:
1127 rc = SLAP_CB_CONTINUE;
1128 break;
1129 }
1130
1131 switch ( rc ) {
1132 case SLAPD_ABANDON:
1133 goto dont_chain;
1134
1135 case LDAP_SUCCESS:
1136 case LDAP_REFERRAL:
1137 sr_err = rs->sr_err;
1138 /* slapd-ldap sent response */
1139 if ( !op->o_abandon && lb.lb_status != LDAP_CH_RES ) {
1140 /* FIXME: should we send response? */
1141 Debug( LDAP_DEBUG_ANY,
1142 "%s: ldap_chain_response: "
1143 "overlay should have sent result.\n",
1144 op->o_log_prefix );
1145 }
1146 break;
1147
1148 default:
1149 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1150 if ( lb.lb_status == LDAP_CH_ERR && rs->sr_err == LDAP_X_CANNOT_CHAIN ) {
1151 goto cannot_chain;
1152 }
1153
1154 switch ( ( get_chainingBehavior( op ) & chain_mask ) >> chain_shift ) {
1155 case LDAP_CHAINING_REQUIRED:
1156 cannot_chain:;
1157 op->o_callback = NULL;
1158 send_ldap_error( op, rs, LDAP_X_CANNOT_CHAIN,
1159 "operation cannot be completed without chaining" );
1160 goto dont_chain;
1161
1162 default:
1163 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1164 if ( LDAP_CHAIN_RETURN_ERR( lc ) ) {
1165 sr_err = rs->sr_err = rc;
1166 rs->sr_text = lb.lb_text;
1167 rs->sr_type = sr_type;
1168
1169 } else {
1170 rc = SLAP_CB_CONTINUE;
1171 rs->sr_err = sr_err;
1172 rs->sr_type = sr_type;
1173 rs->sr_text = text;
1174 rs->sr_matched = matched;
1175 rs->sr_ref = ref;
1176 rs->sr_flags |= flags;
1177 }
1178 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1179 break;
1180 }
1181 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1182 }
1183
1184 if ( lb.lb_status == LDAP_CH_NONE && rc != SLAPD_ABANDON ) {
1185 /* give the remaining callbacks a chance */
1186 op->o_callback = sc->sc_next;
1187 rc = rs->sr_err = slap_map_api2result( rs );
1188 send_ldap_result( op, rs );
1189 }
1190
1191 dont_chain:;
1192 rs->sr_err = sr_err;
1193 rs->sr_type = sr_type;
1194 rs->sr_text = text;
1195 rs->sr_matched = matched;
1196 rs->sr_ref = ref;
1197 rs->sr_flags |= flags;
1198
1199 op->o_bd = bd;
1200 op->o_callback = sc;
1201 op->o_ndn = ndn;
1202
1203 if ( rs->sr_text == lb.lb_text ) {
1204 rs->sr_text = NULL;
1205 }
1206 if ( lb.lb_text ) {
1207 ber_memfree_x( lb.lb_text, op->o_tmpmemctx );
1208 }
1209
1210 return rc;
1211 }
1212
1213 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1214 static int
1215 ldap_chain_parse_ctrl(
1216 Operation *op,
1217 SlapReply *rs,
1218 LDAPControl *ctrl );
1219
1220 static int
1221 str2chain( const char *s )
1222 {
1223 if ( strcasecmp( s, "chainingPreferred" ) == 0 ) {
1224 return LDAP_CHAINING_PREFERRED;
1225
1226 } else if ( strcasecmp( s, "chainingRequired" ) == 0 ) {
1227 return LDAP_CHAINING_REQUIRED;
1228
1229 } else if ( strcasecmp( s, "referralsPreferred" ) == 0 ) {
1230 return LDAP_REFERRALS_PREFERRED;
1231
1232 } else if ( strcasecmp( s, "referralsRequired" ) == 0 ) {
1233 return LDAP_REFERRALS_REQUIRED;
1234 }
1235
1236 return -1;
1237 }
1238 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1239
1240 /*
1241 * configuration...
1242 */
1243
1244 enum {
1245 CH_CHAINING = 1,
1246 CH_CACHE_URI,
1247 CH_MAX_DEPTH,
1248 CH_RETURN_ERR,
1249
1250 CH_LAST
1251 };
1252
1253 static ConfigDriver chain_cf_gen;
1254 static ConfigCfAdd chain_cfadd;
1255 static ConfigLDAPadd chain_ldadd;
1256 #ifdef SLAP_CONFIG_DELETE
1257 static ConfigLDAPdel chain_lddel;
1258 #endif
1259
1260 static ConfigTable chaincfg[] = {
1261 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1262 { "chain-chaining", "args",
1263 2, 4, 0, ARG_MAGIC|ARG_BERVAL|CH_CHAINING, chain_cf_gen,
1264 "( OLcfgOvAt:3.1 NAME 'olcChainingBehavior' "
1265 "DESC 'Chaining behavior control parameters (draft-sermersheim-ldap-chaining)' "
1266 "EQUALITY caseIgnoreMatch "
1267 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
1268 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1269 { "chain-cache-uri", "TRUE/FALSE",
1270 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|CH_CACHE_URI, chain_cf_gen,
1271 "( OLcfgOvAt:3.2 NAME 'olcChainCacheURI' "
1272 "DESC 'Enables caching of URIs not present in configuration' "
1273 "EQUALITY booleanMatch "
1274 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
1275 { "chain-max-depth", "args",
1276 2, 2, 0, ARG_MAGIC|ARG_INT|CH_MAX_DEPTH, chain_cf_gen,
1277 "( OLcfgOvAt:3.3 NAME 'olcChainMaxReferralDepth' "
1278 "DESC 'max referral depth' "
1279 "SYNTAX OMsInteger "
1280 "EQUALITY integerMatch "
1281 "SINGLE-VALUE )", NULL, NULL },
1282 { "chain-return-error", "TRUE/FALSE",
1283 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|CH_RETURN_ERR, chain_cf_gen,
1284 "( OLcfgOvAt:3.4 NAME 'olcChainReturnError' "
1285 "DESC 'Errors are returned instead of the original referral' "
1286 "EQUALITY booleanMatch "
1287 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
1288 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1289 };
1290
1291 static ConfigOCs chainocs[] = {
1292 { "( OLcfgOvOc:3.1 "
1293 "NAME 'olcChainConfig' "
1294 "DESC 'Chain configuration' "
1295 "SUP olcOverlayConfig "
1296 "MAY ( "
1297 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1298 "olcChainingBehavior $ "
1299 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1300 "olcChainCacheURI $ "
1301 "olcChainMaxReferralDepth $ "
1302 "olcChainReturnError "
1303 ") )",
1304 Cft_Overlay, chaincfg, NULL, chain_cfadd },
1305 { "( OLcfgOvOc:3.2 "
1306 "NAME 'olcChainDatabase' "
1307 "DESC 'Chain remote server configuration' "
1308 "AUXILIARY )",
1309 Cft_Misc, NULL, chain_ldadd
1310 #ifdef SLAP_CONFIG_DELETE
1311 , NULL, chain_lddel
1312 #endif
1313 },
1314 { NULL, 0, NULL }
1315 };
1316
1317 static int
1318 chain_ldadd( CfEntryInfo *p, Entry *e, ConfigArgs *ca )
1319 {
1320 slap_overinst *on;
1321 ldap_chain_t *lc;
1322
1323 ldapinfo_t *li;
1324
1325 AttributeDescription *ad = NULL;
1326 Attribute *at;
1327 const char *text;
1328
1329 int rc;
1330
1331 if ( p->ce_type != Cft_Overlay
1332 || !p->ce_bi
1333 || p->ce_bi->bi_cf_ocs != chainocs )
1334 {
1335 return LDAP_CONSTRAINT_VIOLATION;
1336 }
1337
1338 on = (slap_overinst *)p->ce_bi;
1339 lc = (ldap_chain_t *)on->on_bi.bi_private;
1340
1341 assert( ca->be == NULL );
1342 ca->be = (BackendDB *)ch_calloc( 1, sizeof( BackendDB ) );
1343
1344 ca->be->bd_info = (BackendInfo *)on;
1345
1346 rc = slap_str2ad( "olcDbURI", &ad, &text );
1347 assert( rc == LDAP_SUCCESS );
1348
1349 at = attr_find( e->e_attrs, ad );
1350 #if 0
1351 if ( lc->lc_common_li == NULL && at != NULL ) {
1352 /* FIXME: we should generate an empty default entry
1353 * if none is supplied */
1354 Debug( LDAP_DEBUG_ANY, "slapd-chain: "
1355 "first underlying database \"%s\" "
1356 "cannot contain attribute \"%s\".\n",
1357 e->e_name.bv_val, ad->ad_cname.bv_val );
1358 rc = LDAP_CONSTRAINT_VIOLATION;
1359 goto done;
1360
1361 } else
1362 #endif
1363 if ( lc->lc_common_li != NULL && lc->lc_common_li != lc->lc_cfg_li && at == NULL ) {
1364 /* FIXME: we should generate an empty default entry
1365 * if none is supplied */
1366 Debug( LDAP_DEBUG_ANY, "slapd-chain: "
1367 "subsequent underlying database \"%s\" "
1368 "must contain attribute \"%s\".\n",
1369 e->e_name.bv_val, ad->ad_cname.bv_val );
1370 rc = LDAP_CONSTRAINT_VIOLATION;
1371 goto done;
1372 }
1373
1374 if ( lc->lc_common_li == NULL ) {
1375 rc = ldap_chain_db_init_common( ca->be );
1376 if ( rc != 0 )
1377 goto fail;
1378 li = ca->be->be_private;
1379 lc->lc_common_li = lc->lc_cfg_li = li;
1380
1381 }
1382 rc = ldap_chain_db_init_one( ca->be );
1383 lc->lc_cfg_li = NULL;
1384
1385 if ( rc != 0 ) {
1386 fail:
1387 Debug( LDAP_DEBUG_ANY, "slapd-chain: "
1388 "unable to init %sunderlying database \"%s\".\n",
1389 lc->lc_common_li == NULL ? "common " : "", e->e_name.bv_val );
1390 return LDAP_CONSTRAINT_VIOLATION;
1391 }
1392
1393 li = ca->be->be_private;
1394
1395 if ( at ) {
1396 char **urls;
1397
1398 urls = ldap_str2charray( at->a_vals[ 0 ].bv_val, ", \t" );
1399 if ( !urls || !urls[0] || urls[1] ) {
1400 ldap_charray_free( urls );
1401 Debug( LDAP_DEBUG_ANY, "slapd-chain: "
1402 "olcDbURI must contain exactly one url, got %s\n",
1403 at->a_vals[ 0 ].bv_val );
1404 rc = LDAP_CONSTRAINT_VIOLATION;
1405 goto done;
1406 }
1407 ldap_charray_free( urls );
1408
1409 li->li_uri = ch_strdup( at->a_vals[ 0 ].bv_val );
1410 value_add_one( &li->li_bvuri, &at->a_vals[ 0 ] );
1411 if ( ldap_tavl_insert( &lc->lc_lai.lai_tree, (caddr_t)li,
1412 ldap_chain_uri_cmp, ldap_chain_uri_dup ) )
1413 {
1414 Debug( LDAP_DEBUG_ANY, "slapd-chain: "
1415 "database \"%s\" insert failed.\n",
1416 e->e_name.bv_val );
1417 rc = LDAP_CONSTRAINT_VIOLATION;
1418 goto done;
1419 }
1420 }
1421
1422 ca->ca_private = on;
1423
1424 done:;
1425 if ( rc != LDAP_SUCCESS ) {
1426 (void)ldap_chain_db_destroy_one( ca->be, NULL );
1427 ch_free( ca->be );
1428 ca->be = NULL;
1429 }
1430
1431 return rc;
1432 }
1433
1434 static void
1435 ldap_chain_cfadd_apply(
1436 ldapinfo_t *li,
1437 Operation *op,
1438 SlapReply *rs,
1439 Entry *p,
1440 ConfigArgs *ca,
1441 int count )
1442 {
1443 struct berval bv;
1444
1445 /* FIXME: should not hardcode "olcDatabase" here */
1446 bv.bv_len = snprintf( ca->cr_msg, sizeof( ca->cr_msg ),
1447 "olcDatabase={%d}%s", count, lback->bi_type );
1448 bv.bv_val = ca->cr_msg;
1449
1450 ca->be->be_private = (void *)li;
1451 config_build_entry( op, rs, p->e_private, ca,
1452 &bv, lback->bi_cf_ocs, &chainocs[1] );
1453
1454 return;
1455 }
1456
1457 static int
1458 chain_cfadd( Operation *op, SlapReply *rs, Entry *p, ConfigArgs *ca )
1459 {
1460 CfEntryInfo *pe = p->e_private;
1461 slap_overinst *on = (slap_overinst *)pe->ce_bi;
1462 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
1463 void *priv = (void *)ca->be->be_private;
1464 TAvlnode *edge;
1465 int count = 0;
1466
1467 if ( lback->bi_cf_ocs ) {
1468
1469 ldap_chain_cfadd_apply( lc->lc_common_li, op, rs, p, ca, count++ );
1470
1471 edge = ldap_tavl_end( lc->lc_lai.lai_tree, TAVL_DIR_LEFT );
1472 while ( edge ) {
1473 TAvlnode *next = ldap_tavl_next( edge, TAVL_DIR_RIGHT );
1474 ldapinfo_t *li = (ldapinfo_t *)edge->avl_data;
1475 ldap_chain_cfadd_apply( li, op, rs, p, ca, count++ );
1476 edge = next;
1477 }
1478
1479 ca->be->be_private = priv;
1480 }
1481
1482 lc->lc_cfg_li = NULL;
1483
1484 return 0;
1485 }
1486
1487 #ifdef SLAP_CONFIG_DELETE
1488 static int
1489 chain_lddel( CfEntryInfo *ce, Operation *op )
1490 {
1491 CfEntryInfo *pe = ce->ce_parent;
1492 slap_overinst *on = (slap_overinst *)pe->ce_bi;
1493 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
1494 ldapinfo_t *li = (ldapinfo_t *) ce->ce_be->be_private;
1495
1496 if ( li != lc->lc_common_li ) {
1497 if (! ldap_tavl_delete( &lc->lc_lai.lai_tree, li, ldap_chain_uri_cmp ) ) {
1498 Debug( LDAP_DEBUG_ANY, "slapd-chain: ldap_avl_delete failed. "
1499 "\"%s\" not found.\n", li->li_uri );
1500 return -1;
1501 }
1502 } else if ( lc->lc_lai.lai_tree ) {
1503 Debug( LDAP_DEBUG_ANY, "slapd-chain: cannot delete first underlying "
1504 "LDAP database when other databases are still present.\n" );
1505 return -1;
1506 } else {
1507 lc->lc_common_li = NULL;
1508 }
1509
1510 ce->ce_be->bd_info = lback;
1511
1512 if ( ce->ce_be->bd_info->bi_db_close ) {
1513 ce->ce_be->bd_info->bi_db_close( ce->ce_be, NULL );
1514 }
1515 if ( ce->ce_be->bd_info->bi_db_destroy ) {
1516 ce->ce_be->bd_info->bi_db_destroy( ce->ce_be, NULL );
1517 }
1518
1519 ch_free(ce->ce_be);
1520 ce->ce_be = NULL;
1521
1522 return LDAP_SUCCESS;
1523 }
1524 #endif /* SLAP_CONFIG_DELETE */
1525
1526 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1527 static slap_verbmasks chaining_mode[] = {
1528 { BER_BVC("referralsRequired"), LDAP_REFERRALS_REQUIRED },
1529 { BER_BVC("referralsPreferred"), LDAP_REFERRALS_PREFERRED },
1530 { BER_BVC("chainingRequired"), LDAP_CHAINING_REQUIRED },
1531 { BER_BVC("chainingPreferred"), LDAP_CHAINING_PREFERRED },
1532 { BER_BVNULL, 0 }
1533 };
1534 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1535
1536 static int
1537 chain_cf_gen( ConfigArgs *c )
1538 {
1539 slap_overinst *on = (slap_overinst *)c->bi;
1540 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
1541
1542 int rc = 0;
1543
1544 if ( c->op == SLAP_CONFIG_EMIT ) {
1545 switch( c->type ) {
1546 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1547 case CH_CHAINING: {
1548 struct berval resolve = BER_BVNULL,
1549 continuation = BER_BVNULL;
1550
1551 if ( !LDAP_CHAIN_CHAINING( lc ) ) {
1552 return 1;
1553 }
1554
1555 enum_to_verb( chaining_mode, ( ( lc->lc_chaining_ctrlflag & SLAP_CH_RESOLVE_MASK ) >> SLAP_CH_RESOLVE_SHIFT ), &resolve );
1556 enum_to_verb( chaining_mode, ( ( lc->lc_chaining_ctrlflag & SLAP_CH_CONTINUATION_MASK ) >> SLAP_CH_CONTINUATION_SHIFT ), &continuation );
1557
1558 c->value_bv.bv_len = STRLENOF( "resolve=" ) + resolve.bv_len
1559 + STRLENOF( " " )
1560 + STRLENOF( "continuation=" ) + continuation.bv_len;
1561 c->value_bv.bv_val = ch_malloc( c->value_bv.bv_len + 1 );
1562 snprintf( c->value_bv.bv_val, c->value_bv.bv_len + 1,
1563 "resolve=%s continuation=%s",
1564 resolve.bv_val, continuation.bv_val );
1565
1566 if ( lc->lc_chaining_ctrl.ldctl_iscritical ) {
1567 c->value_bv.bv_val = ch_realloc( c->value_bv.bv_val,
1568 c->value_bv.bv_len + STRLENOF( " critical" ) + 1 );
1569 AC_MEMCPY( &c->value_bv.bv_val[ c->value_bv.bv_len ],
1570 " critical", STRLENOF( " critical" ) + 1 );
1571 c->value_bv.bv_len += STRLENOF( " critical" );
1572 }
1573
1574 break;
1575 }
1576 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1577
1578 case CH_CACHE_URI:
1579 c->value_int = LDAP_CHAIN_CACHE_URI( lc );
1580 break;
1581
1582 case CH_MAX_DEPTH:
1583 c->value_int = lc->lc_max_depth;
1584 break;
1585
1586 case CH_RETURN_ERR:
1587 c->value_int = LDAP_CHAIN_RETURN_ERR( lc );
1588 break;
1589
1590 default:
1591 assert( 0 );
1592 rc = 1;
1593 }
1594 return rc;
1595
1596 } else if ( c->op == LDAP_MOD_DELETE ) {
1597 switch( c->type ) {
1598 case CH_CHAINING:
1599 return 1;
1600
1601 case CH_CACHE_URI:
1602 lc->lc_flags &= ~LDAP_CHAIN_F_CACHE_URI;
1603 break;
1604
1605 case CH_MAX_DEPTH:
1606 c->value_int = 0;
1607 break;
1608
1609 case CH_RETURN_ERR:
1610 lc->lc_flags &= ~LDAP_CHAIN_F_RETURN_ERR;
1611 break;
1612
1613 default:
1614 return 1;
1615 }
1616 return rc;
1617 }
1618
1619 switch( c->type ) {
1620 case CH_CHAINING: {
1621 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1622 char **argv = c->argv;
1623 int argc = c->argc;
1624 BerElementBuffer berbuf;
1625 BerElement *ber = (BerElement *)&berbuf;
1626 int resolve = -1,
1627 continuation = -1,
1628 iscritical = 0;
1629 Operation op = { 0 };
1630 SlapReply rs = { 0 };
1631
1632 lc->lc_chaining_ctrlflag = 0;
1633
1634 for ( argc--, argv++; argc > 0; argc--, argv++ ) {
1635 if ( strncasecmp( argv[ 0 ], "resolve=", STRLENOF( "resolve=" ) ) == 0 ) {
1636 resolve = str2chain( argv[ 0 ] + STRLENOF( "resolve=" ) );
1637 if ( resolve == -1 ) {
1638 Debug( LDAP_DEBUG_ANY, "%s: "
1639 "illegal <resolve> value %s "
1640 "in \"chain-chaining>\".\n",
1641 c->log, argv[ 0 ] );
1642 return 1;
1643 }
1644
1645 } else if ( strncasecmp( argv[ 0 ], "continuation=", STRLENOF( "continuation=" ) ) == 0 ) {
1646 continuation = str2chain( argv[ 0 ] + STRLENOF( "continuation=" ) );
1647 if ( continuation == -1 ) {
1648 Debug( LDAP_DEBUG_ANY, "%s: "
1649 "illegal <continuation> value %s "
1650 "in \"chain-chaining\".\n",
1651 c->log, argv[ 0 ] );
1652 return 1;
1653 }
1654
1655 } else if ( strcasecmp( argv[ 0 ], "critical" ) == 0 ) {
1656 iscritical = 1;
1657
1658 } else {
1659 Debug( LDAP_DEBUG_ANY, "%s: "
1660 "unknown option in \"chain-chaining\".\n",
1661 c->log );
1662 return 1;
1663 }
1664 }
1665
1666 if ( resolve != -1 || continuation != -1 ) {
1667 int err;
1668
1669 if ( resolve == -1 ) {
1670 /* default */
1671 resolve = SLAP_CHAINING_DEFAULT;
1672 }
1673
1674 ber_init2( ber, NULL, LBER_USE_DER );
1675
1676 err = ber_printf( ber, "{e" /* } */, resolve );
1677 if ( err == -1 ) {
1678 ber_free( ber, 1 );
1679 Debug( LDAP_DEBUG_ANY, "%s: "
1680 "chaining behavior control encoding error!\n",
1681 c->log );
1682 return 1;
1683 }
1684
1685 if ( continuation > -1 ) {
1686 err = ber_printf( ber, "e", continuation );
1687 if ( err == -1 ) {
1688 ber_free( ber, 1 );
1689 Debug( LDAP_DEBUG_ANY, "%s: "
1690 "chaining behavior control encoding error!\n",
1691 c->log );
1692 return 1;
1693 }
1694 }
1695
1696 err = ber_printf( ber, /* { */ "N}" );
1697 if ( err == -1 ) {
1698 ber_free( ber, 1 );
1699 Debug( LDAP_DEBUG_ANY, "%s: "
1700 "chaining behavior control encoding error!\n",
1701 c->log );
1702 return 1;
1703 }
1704
1705 if ( ber_flatten2( ber, &lc->lc_chaining_ctrl.ldctl_value, 0 ) == -1 ) {
1706 exit( EXIT_FAILURE );
1707 }
1708
1709 } else {
1710 BER_BVZERO( &lc->lc_chaining_ctrl.ldctl_value );
1711 }
1712
1713 lc->lc_chaining_ctrl.ldctl_oid = LDAP_CONTROL_X_CHAINING_BEHAVIOR;
1714 lc->lc_chaining_ctrl.ldctl_iscritical = iscritical;
1715
1716 if ( ldap_chain_parse_ctrl( &op, &rs, &lc->lc_chaining_ctrl ) != LDAP_SUCCESS )
1717 {
1718 Debug( LDAP_DEBUG_ANY, "%s: "
1719 "unable to parse chaining control%s%s.\n",
1720 c->log, rs.sr_text ? ": " : "",
1721 rs.sr_text ? rs.sr_text : "" );
1722 return 1;
1723 }
1724
1725 lc->lc_chaining_ctrlflag = op.o_chaining;
1726
1727 lc->lc_flags |= LDAP_CHAIN_F_CHAINING;
1728
1729 rc = 0;
1730 #else /* ! LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1731 Debug( LDAP_DEBUG_ANY, "%s: "
1732 "\"chaining\" control unsupported (ignored).\n",
1733 c->log );
1734 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1735 } break;
1736
1737 case CH_CACHE_URI:
1738 if ( c->value_int ) {
1739 lc->lc_flags |= LDAP_CHAIN_F_CACHE_URI;
1740 } else {
1741 lc->lc_flags &= ~LDAP_CHAIN_F_CACHE_URI;
1742 }
1743 break;
1744
1745 case CH_MAX_DEPTH:
1746 if ( c->value_int < 0 ) {
1747 snprintf( c->cr_msg, sizeof( c->cr_msg ),
1748 "<%s> invalid max referral depth %d",
1749 c->argv[0], c->value_int );
1750 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
1751 c->log, c->cr_msg );
1752 rc = 1;
1753 break;
1754 }
1755 lc->lc_max_depth = c->value_int;
1756
1757 case CH_RETURN_ERR:
1758 if ( c->value_int ) {
1759 lc->lc_flags |= LDAP_CHAIN_F_RETURN_ERR;
1760 } else {
1761 lc->lc_flags &= ~LDAP_CHAIN_F_RETURN_ERR;
1762 }
1763 break;
1764
1765 default:
1766 assert( 0 );
1767 return 1;
1768 }
1769 return rc;
1770 }
1771
1772 static int
1773 ldap_chain_db_init(
1774 BackendDB *be,
1775 ConfigReply *cr )
1776 {
1777 slap_overinst *on = (slap_overinst *)be->bd_info;
1778 ldap_chain_t *lc = NULL;
1779
1780 if ( lback == NULL ) {
1781 lback = backend_info( "ldap" );
1782
1783 if ( lback == NULL ) {
1784 return 1;
1785 }
1786 }
1787
1788 lc = ch_malloc( sizeof( ldap_chain_t ) );
1789 if ( lc == NULL ) {
1790 return 1;
1791 }
1792 memset( lc, 0, sizeof( ldap_chain_t ) );
1793 lc->lc_max_depth = 1;
1794 ldap_pvt_thread_mutex_init( &lc->lc_lai.lai_mutex );
1795
1796 on->on_bi.bi_private = (void *)lc;
1797
1798 return 0;
1799 }
1800
1801 static int
1802 ldap_chain_db_config(
1803 BackendDB *be,
1804 const char *fname,
1805 int lineno,
1806 int argc,
1807 char **argv )
1808 {
1809 slap_overinst *on = (slap_overinst *)be->bd_info;
1810 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
1811
1812 int rc = SLAP_CONF_UNKNOWN;
1813
1814 if ( lc->lc_common_li == NULL ) {
1815 BackendDB db = *be;
1816 ldap_chain_db_init_common( &db );
1817 lc->lc_common_li = lc->lc_cfg_li = (ldapinfo_t *)db.be_private;
1818 }
1819
1820 /* Something for the chain database? */
1821 if ( strncasecmp( argv[ 0 ], "chain-", STRLENOF( "chain-" ) ) == 0 ) {
1822 char *save_argv0 = argv[ 0 ];
1823 BackendDB db = *be;
1824 static char *allowed_argv[] = {
1825 /* special: put URI here, so in the meanwhile
1826 * it detects whether a new URI is being provided */
1827 "uri",
1828 "nretries",
1829 "timeout",
1830 /* flags */
1831 "tls",
1832 /* FIXME: maybe rebind-as-user should be allowed
1833 * only within known URIs... */
1834 "rebind-as-user",
1835 "chase-referrals",
1836 "t-f-support",
1837 "proxy-whoami",
1838 NULL
1839 };
1840 int which_argv = -1;
1841
1842 argv[ 0 ] += STRLENOF( "chain-" );
1843
1844 for ( which_argv = 0; allowed_argv[ which_argv ]; which_argv++ ) {
1845 if ( strcasecmp( argv[ 0 ], allowed_argv[ which_argv ] ) == 0 ) {
1846 break;
1847 }
1848 }
1849
1850 if ( allowed_argv[ which_argv ] == NULL ) {
1851 which_argv = -1;
1852
1853 if ( lc->lc_cfg_li == lc->lc_common_li ) {
1854 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1855 "\"%s\" only allowed within a URI directive.\n.",
1856 fname, lineno, argv[ 0 ] );
1857 return 1;
1858 }
1859 }
1860
1861 if ( which_argv == 0 ) {
1862 rc = ldap_chain_db_init_one( &db );
1863 if ( rc != 0 ) {
1864 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1865 "underlying slapd-ldap initialization failed.\n.",
1866 fname, lineno );
1867 return 1;
1868 }
1869 lc->lc_cfg_li = db.be_private;
1870 }
1871
1872 /* TODO: add checks on what other slapd-ldap(5) args
1873 * should be put in the template; this is not quite
1874 * harmful, because attributes that shouldn't don't
1875 * get actually used, but the user should at least
1876 * be warned.
1877 */
1878
1879 db.bd_info = lback;
1880 db.be_private = (void *)lc->lc_cfg_li;
1881 db.be_cf_ocs = lback->bi_cf_ocs;
1882
1883 rc = config_generic_wrapper( &db, fname, lineno, argc, argv );
1884
1885 argv[ 0 ] = save_argv0;
1886
1887 if ( which_argv == 0 ) {
1888 private_destroy:;
1889 if ( rc != 0 ) {
1890 db.bd_info = lback;
1891 db.be_private = (void *)lc->lc_cfg_li;
1892 ldap_chain_db_destroy_one( &db, NULL );
1893 lc->lc_cfg_li = NULL;
1894 } else {
1895 if ( lc->lc_cfg_li->li_bvuri == NULL
1896 || BER_BVISNULL( &lc->lc_cfg_li->li_bvuri[ 0 ] )
1897 || !BER_BVISNULL( &lc->lc_cfg_li->li_bvuri[ 1 ] ) )
1898 {
1899 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1900 "no URI list allowed in slapo-chain.\n",
1901 fname, lineno );
1902 rc = 1;
1903 goto private_destroy;
1904 }
1905
1906 if ( ldap_tavl_insert( &lc->lc_lai.lai_tree,
1907 (caddr_t)lc->lc_cfg_li,
1908 ldap_chain_uri_cmp, ldap_chain_uri_dup ) )
1909 {
1910 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1911 "duplicate URI in slapo-chain.\n",
1912 fname, lineno );
1913 rc = 1;
1914 goto private_destroy;
1915 }
1916 }
1917 }
1918 }
1919
1920 return rc;
1921 }
1922
1923 enum db_which {
1924 db_open = 0,
1925 db_close,
1926 db_destroy,
1927
1928 db_last
1929 };
1930
1931 static int
1932 ldap_chain_db_func(
1933 BackendDB *be,
1934 enum db_which which
1935 )
1936 {
1937 slap_overinst *on = (slap_overinst *)be->bd_info;
1938 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
1939
1940 int rc = 0;
1941
1942 if ( lc ) {
1943 BI_db_func *func = (&lback->bi_db_open)[ which ];
1944
1945 if ( func != NULL && lc->lc_common_li != NULL ) {
1946 BackendDB db = *be;
1947
1948 db.bd_info = lback;
1949 db.be_private = lc->lc_common_li;
1950
1951 rc = func( &db, NULL );
1952
1953 if ( rc != 0 ) {
1954 return rc;
1955 }
1956
1957 if ( lc->lc_lai.lai_tree != NULL ) {
1958 TAvlnode *edge = ldap_tavl_end( lc->lc_lai.lai_tree, TAVL_DIR_LEFT );
1959 while ( edge ) {
1960 TAvlnode *next = ldap_tavl_next( edge, TAVL_DIR_RIGHT );
1961 ldapinfo_t *li = (ldapinfo_t *)edge->avl_data;
1962 db.be_private = (void *)li;
1963 rc = func( &db, NULL );
1964 if ( rc == 1 ) {
1965 break;
1966 }
1967 edge = next;
1968 }
1969 }
1970 }
1971 }
1972
1973 return rc;
1974 }
1975
1976 static int
1977 ldap_chain_db_open(
1978 BackendDB *be,
1979 ConfigReply *cr )
1980 {
1981 slap_overinst *on = (slap_overinst *) be->bd_info;
1982 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
1983 slap_mask_t monitoring;
1984 int rc = 0;
1985
1986 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
1987 rc = overlay_register_control( be, LDAP_CONTROL_X_CHAINING_BEHAVIOR );
1988 if ( rc != 0 ) {
1989 return rc;
1990 }
1991 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
1992
1993 if ( lc->lc_common_li == NULL ) {
1994 void *be_private = be->be_private;
1995 ldap_chain_db_init_common( be );
1996 lc->lc_common_li = lc->lc_cfg_li = (ldapinfo_t *)be->be_private;
1997 be->be_private = be_private;
1998 }
1999
2000 /* filter out and restore monitoring */
2001 monitoring = ( SLAP_DBFLAGS( be ) & SLAP_DBFLAG_MONITORING );
2002 SLAP_DBFLAGS( be ) &= ~SLAP_DBFLAG_MONITORING;
2003 rc = ldap_chain_db_func( be, db_open );
2004 SLAP_DBFLAGS( be ) |= monitoring;
2005
2006 return rc;
2007 }
2008
2009 static int
2010 ldap_chain_db_close(
2011 BackendDB *be,
2012 ConfigReply *cr )
2013 {
2014 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
2015 #ifdef SLAP_CONFIG_DELETE
2016 overlay_unregister_control( be, LDAP_CONTROL_X_CHAINING_BEHAVIOR );
2017 #endif /* SLAP_CONFIG_DELETE */
2018 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
2019 return ldap_chain_db_func( be, db_close );
2020 }
2021
2022 static int
2023 ldap_chain_db_destroy(
2024 BackendDB *be,
2025 ConfigReply *cr )
2026 {
2027 slap_overinst *on = (slap_overinst *) be->bd_info;
2028 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
2029
2030 int rc;
2031
2032 rc = ldap_chain_db_func( be, db_destroy );
2033
2034 if ( lc ) {
2035 ldap_tavl_free( lc->lc_lai.lai_tree, NULL );
2036 ldap_pvt_thread_mutex_destroy( &lc->lc_lai.lai_mutex );
2037 ch_free( lc );
2038 }
2039
2040 return rc;
2041 }
2042
2043 /*
2044 * inits one instance of the slapd-ldap backend, and stores
2045 * the private info in be_private of the arg
2046 */
2047 static int
2048 ldap_chain_db_init_common(
2049 BackendDB *be )
2050 {
2051 BackendInfo *bi = be->bd_info;
2052 ldapinfo_t *li;
2053 int rc;
2054
2055 be->bd_info = lback;
2056 be->be_private = NULL;
2057 rc = lback->bi_db_init( be, NULL );
2058 if ( rc != 0 ) {
2059 return rc;
2060 }
2061 li = (ldapinfo_t *)be->be_private;
2062 li->li_urllist_f = NULL;
2063 li->li_urllist_p = NULL;
2064
2065 be->bd_info = bi;
2066
2067 return 0;
2068 }
2069
2070 /*
2071 * inits one instance of the slapd-ldap backend, stores
2072 * the private info in be_private of the arg and fills
2073 * selected fields with data from the template.
2074 *
2075 * NOTE: add checks about the other fields of the template,
2076 * which are ignored and SHOULD NOT be configured by the user.
2077 */
2078 static int
2079 ldap_chain_db_init_one(
2080 BackendDB *be )
2081 {
2082 slap_overinst *on = (slap_overinst *)be->bd_info;
2083 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
2084
2085 BackendInfo *bi = be->bd_info;
2086 ldapinfo_t *li;
2087
2088 slap_op_t t;
2089
2090 be->bd_info = lback;
2091 be->be_private = NULL;
2092 t = lback->bi_db_init( be, NULL );
2093 if ( t != 0 ) {
2094 return t;
2095 }
2096 li = (ldapinfo_t *)be->be_private;
2097 li->li_urllist_f = NULL;
2098 li->li_urllist_p = NULL;
2099
2100 /* copy common data */
2101 li->li_nretries = lc->lc_common_li->li_nretries;
2102 li->li_flags = lc->lc_common_li->li_flags;
2103 li->li_version = lc->lc_common_li->li_version;
2104 for ( t = 0; t < SLAP_OP_LAST; t++ ) {
2105 li->li_timeout[ t ] = lc->lc_common_li->li_timeout[ t ];
2106 }
2107 be->bd_info = bi;
2108
2109 return 0;
2110 }
2111
2112 static int
2113 ldap_chain_db_open_one(
2114 BackendDB *be )
2115 {
2116 if ( SLAP_DBMONITORING( be ) ) {
2117 ldapinfo_t *li = (ldapinfo_t *)be->be_private;
2118
2119 if ( li->li_uri == NULL ) {
2120 ber_str2bv( "cn=Common Connections", 0, 1,
2121 &li->li_monitor_info.lmi_conn_rdn );
2122 ber_str2bv( "cn=Operations on Common Connections", 0, 1,
2123 &li->li_monitor_info.lmi_conn_rdn );
2124
2125 } else {
2126 char *ptr;
2127
2128 li->li_monitor_info.lmi_conn_rdn.bv_len
2129 = STRLENOF( "cn=" ) + strlen( li->li_uri );
2130 ptr = li->li_monitor_info.lmi_conn_rdn.bv_val
2131 = ch_malloc( li->li_monitor_info.lmi_conn_rdn.bv_len + 1 );
2132 ptr = lutil_strcopy( ptr, "cn=" );
2133 ptr = lutil_strcopy( ptr, li->li_uri );
2134 ptr[ 0 ] = '\0';
2135
2136 li->li_monitor_info.lmi_ops_rdn.bv_len
2137 = STRLENOF( "cn=Operations on " ) + strlen( li->li_uri );
2138 ptr = li->li_monitor_info.lmi_ops_rdn.bv_val
2139 = ch_malloc( li->li_monitor_info.lmi_ops_rdn.bv_len + 1 );
2140 ptr = lutil_strcopy( ptr, "cn=Operations on " );
2141 ptr = lutil_strcopy( ptr, li->li_uri );
2142 ptr[ 0 ] = '\0';
2143 }
2144 }
2145
2146 return lback->bi_db_open( be, NULL );
2147 }
2148
2149 static int
2150 ldap_chain_connection_destroy(
2151 BackendDB *be,
2152 Connection *conn
2153 )
2154 {
2155 slap_overinst *on = (slap_overinst *) be->bd_info;
2156 ldap_chain_t *lc = (ldap_chain_t *)on->on_bi.bi_private;
2157 void *private = be->be_private;
2158 TAvlnode *edge;
2159 int rc;
2160
2161 be->be_private = NULL;
2162 ldap_pvt_thread_mutex_lock( &lc->lc_lai.lai_mutex );
2163 edge = ldap_tavl_end( lc->lc_lai.lai_tree, TAVL_DIR_LEFT );
2164 while ( edge ) {
2165 TAvlnode *next = ldap_tavl_next( edge, TAVL_DIR_RIGHT );
2166 ldapinfo_t *li = (ldapinfo_t *)edge->avl_data;
2167 be->be_private = (void *)li;
2168 rc = lback->bi_connection_destroy( be, conn );
2169 if ( rc == 1 ) {
2170 break;
2171 }
2172 edge = next;
2173 }
2174
2175
2176 ldap_pvt_thread_mutex_unlock( &lc->lc_lai.lai_mutex );
2177 be->be_private = private;
2178
2179 return rc;
2180 }
2181
2182 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
2183 static int
2184 ldap_chain_parse_ctrl(
2185 Operation *op,
2186 SlapReply *rs,
2187 LDAPControl *ctrl )
2188 {
2189 ber_tag_t tag;
2190 BerElement *ber;
2191 ber_int_t mode,
2192 behavior;
2193
2194 if ( get_chaining( op ) != SLAP_CONTROL_NONE ) {
2195 rs->sr_text = "Chaining behavior control specified multiple times";
2196 return LDAP_PROTOCOL_ERROR;
2197 }
2198
2199 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
2200 rs->sr_text = "Chaining behavior control specified with pagedResults control";
2201 return LDAP_PROTOCOL_ERROR;
2202 }
2203
2204 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
2205 mode = (SLAP_CH_RESOLVE_DEFAULT|SLAP_CH_CONTINUATION_DEFAULT);
2206
2207 } else {
2208 ber_len_t len;
2209
2210 /* Parse the control value
2211 * ChainingBehavior ::= SEQUENCE {
2212 * resolveBehavior Behavior OPTIONAL,
2213 * continuationBehavior Behavior OPTIONAL }
2214 *
2215 * Behavior :: = ENUMERATED {
2216 * chainingPreferred (0),
2217 * chainingRequired (1),
2218 * referralsPreferred (2),
2219 * referralsRequired (3) }
2220 */
2221
2222 ber = ber_init( &ctrl->ldctl_value );
2223 if( ber == NULL ) {
2224 rs->sr_text = "internal error";
2225 return LDAP_OTHER;
2226 }
2227
2228 tag = ber_scanf( ber, "{e" /* } */, &behavior );
2229 /* FIXME: since the whole SEQUENCE is optional,
2230 * should we accept no enumerations at all? */
2231 if ( tag != LBER_ENUMERATED ) {
2232 rs->sr_text = "Chaining behavior control: resolveBehavior decoding error";
2233 return LDAP_PROTOCOL_ERROR;
2234 }
2235
2236 switch ( behavior ) {
2237 case LDAP_CHAINING_PREFERRED:
2238 mode = SLAP_CH_RESOLVE_CHAINING_PREFERRED;
2239 break;
2240
2241 case LDAP_CHAINING_REQUIRED:
2242 mode = SLAP_CH_RESOLVE_CHAINING_REQUIRED;
2243 break;
2244
2245 case LDAP_REFERRALS_PREFERRED:
2246 mode = SLAP_CH_RESOLVE_REFERRALS_PREFERRED;
2247 break;
2248
2249 case LDAP_REFERRALS_REQUIRED:
2250 mode = SLAP_CH_RESOLVE_REFERRALS_REQUIRED;
2251 break;
2252
2253 default:
2254 rs->sr_text = "Chaining behavior control: unknown resolveBehavior";
2255 return LDAP_PROTOCOL_ERROR;
2256 }
2257
2258 tag = ber_peek_tag( ber, &len );
2259 if ( tag == LBER_ENUMERATED ) {
2260 tag = ber_scanf( ber, "e", &behavior );
2261 if ( tag == LBER_ERROR ) {
2262 rs->sr_text = "Chaining behavior control: continuationBehavior decoding error";
2263 return LDAP_PROTOCOL_ERROR;
2264 }
2265 }
2266
2267 if ( tag == LBER_DEFAULT ) {
2268 mode |= SLAP_CH_CONTINUATION_DEFAULT;
2269
2270 } else {
2271 switch ( behavior ) {
2272 case LDAP_CHAINING_PREFERRED:
2273 mode |= SLAP_CH_CONTINUATION_CHAINING_PREFERRED;
2274 break;
2275
2276 case LDAP_CHAINING_REQUIRED:
2277 mode |= SLAP_CH_CONTINUATION_CHAINING_REQUIRED;
2278 break;
2279
2280 case LDAP_REFERRALS_PREFERRED:
2281 mode |= SLAP_CH_CONTINUATION_REFERRALS_PREFERRED;
2282 break;
2283
2284 case LDAP_REFERRALS_REQUIRED:
2285 mode |= SLAP_CH_CONTINUATION_REFERRALS_REQUIRED;
2286 break;
2287
2288 default:
2289 rs->sr_text = "Chaining behavior control: unknown continuationBehavior";
2290 return LDAP_PROTOCOL_ERROR;
2291 }
2292 }
2293
2294 if ( ( ber_scanf( ber, /* { */ "}") ) == LBER_ERROR ) {
2295 rs->sr_text = "Chaining behavior control: decoding error";
2296 return LDAP_PROTOCOL_ERROR;
2297 }
2298
2299 (void) ber_free( ber, 1 );
2300 }
2301
2302 op->o_chaining = mode | ( ctrl->ldctl_iscritical
2303 ? SLAP_CONTROL_CRITICAL
2304 : SLAP_CONTROL_NONCRITICAL );
2305
2306 return LDAP_SUCCESS;
2307 }
2308 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
2309
2310 int
2311 chain_initialize( void )
2312 {
2313 int rc;
2314
2315 /* Make sure we don't exceed the bits reserved for userland */
2316 config_check_userland( CH_LAST );
2317
2318 /* olcDatabaseDummy is defined in slapd, and Windows
2319 will not let us initialize a struct element with a data pointer
2320 from another library, so we have to initialize this element
2321 "by hand". */
2322 chainocs[1].co_table = olcDatabaseDummy;
2323
2324 #ifdef LDAP_CONTROL_X_CHAINING_BEHAVIOR
2325 rc = register_supported_control( LDAP_CONTROL_X_CHAINING_BEHAVIOR,
2326 /* SLAP_CTRL_GLOBAL| */ SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE, NULL,
2327 ldap_chain_parse_ctrl, &sc_chainingBehavior );
2328 if ( rc != LDAP_SUCCESS ) {
2329 Debug( LDAP_DEBUG_ANY, "slapd-chain: "
2330 "unable to register chaining behavior control: %d.\n",
2331 rc );
2332 return rc;
2333 }
2334 #endif /* LDAP_CONTROL_X_CHAINING_BEHAVIOR */
2335
2336 ldapchain.on_bi.bi_type = "chain";
2337 ldapchain.on_bi.bi_db_init = ldap_chain_db_init;
2338 ldapchain.on_bi.bi_db_config = ldap_chain_db_config;
2339 ldapchain.on_bi.bi_db_open = ldap_chain_db_open;
2340 ldapchain.on_bi.bi_db_close = ldap_chain_db_close;
2341 ldapchain.on_bi.bi_db_destroy = ldap_chain_db_destroy;
2342
2343 ldapchain.on_bi.bi_connection_destroy = ldap_chain_connection_destroy;
2344
2345 ldapchain.on_response = ldap_chain_response;
2346
2347 ldapchain.on_bi.bi_cf_ocs = chainocs;
2348
2349 rc = config_register_schema( chaincfg, chainocs );
2350 if ( rc ) {
2351 return rc;
2352 }
2353
2354 return overlay_register( &ldapchain );
2355 }
2356