xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/result.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /*	$NetBSD: result.c,v 1.1.1.7 2018/02/06 01:53:15 christos Exp $	*/
2 
3 /* result.c - routines to send ldap results, errors, and referrals */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 1998-2017 The OpenLDAP Foundation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms are permitted
22  * provided that this notice is preserved and that due credit is given
23  * to the University of Michigan at Ann Arbor. The name of the University
24  * may not be used to endorse or promote products derived from this
25  * software without specific prior written permission. This software
26  * is provided ``as is'' without express or implied warranty.
27  */
28 
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: result.c,v 1.1.1.7 2018/02/06 01:53:15 christos Exp $");
31 
32 #include "portable.h"
33 
34 #include <stdio.h>
35 
36 #include <ac/socket.h>
37 #include <ac/errno.h>
38 #include <ac/string.h>
39 #include <ac/ctype.h>
40 #include <ac/time.h>
41 #include <ac/unistd.h>
42 
43 #include "slap.h"
44 
45 const struct berval slap_dummy_bv = BER_BVNULL;
46 
47 int slap_null_cb( Operation *op, SlapReply *rs )
48 {
49 	return 0;
50 }
51 
52 int slap_freeself_cb( Operation *op, SlapReply *rs )
53 {
54 	assert( op->o_callback != NULL );
55 
56 	op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
57 	op->o_callback = NULL;
58 
59 	return SLAP_CB_CONTINUE;
60 }
61 
62 static char *v2ref( BerVarray ref, const char *text )
63 {
64 	size_t len = 0, i = 0;
65 	char *v2;
66 
67 	if(ref == NULL) {
68 		if (text) {
69 			return ch_strdup(text);
70 		} else {
71 			return NULL;
72 		}
73 	}
74 
75 	if ( text != NULL ) {
76 		len = strlen( text );
77 		if (text[len-1] != '\n') {
78 		    i = 1;
79 		}
80 	}
81 
82 	v2 = ch_malloc( len+i+sizeof("Referral:") );
83 
84 	if( text != NULL ) {
85 		strcpy(v2, text);
86 		if( i ) {
87 			v2[len++] = '\n';
88 		}
89 	}
90 	strcpy( v2+len, "Referral:" );
91 	len += sizeof("Referral:");
92 
93 	for( i=0; ref[i].bv_val != NULL; i++ ) {
94 		v2 = ch_realloc( v2, len + ref[i].bv_len + 1 );
95 		v2[len-1] = '\n';
96 		AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
97 		len += ref[i].bv_len;
98 		if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
99 			++len;
100 		}
101 	}
102 
103 	v2[len-1] = '\0';
104 	return v2;
105 }
106 
107 ber_tag_t
108 slap_req2res( ber_tag_t tag )
109 {
110 	switch( tag ) {
111 	case LDAP_REQ_ADD:
112 	case LDAP_REQ_BIND:
113 	case LDAP_REQ_COMPARE:
114 	case LDAP_REQ_EXTENDED:
115 	case LDAP_REQ_MODIFY:
116 	case LDAP_REQ_MODRDN:
117 		tag++;
118 		break;
119 
120 	case LDAP_REQ_DELETE:
121 		tag = LDAP_RES_DELETE;
122 		break;
123 
124 	case LDAP_REQ_ABANDON:
125 	case LDAP_REQ_UNBIND:
126 		tag = LBER_SEQUENCE;
127 		break;
128 
129 	case LDAP_REQ_SEARCH:
130 		tag = LDAP_RES_SEARCH_RESULT;
131 		break;
132 
133 	default:
134 		tag = LBER_SEQUENCE;
135 	}
136 
137 	return tag;
138 }
139 
140 /* SlapReply debugging, prodo-slap.h overrides it in OpenLDAP releases */
141 #if defined(LDAP_TEST) || (defined(USE_RS_ASSERT) && (USE_RS_ASSERT))
142 
143 int rs_suppress_assert = 0;
144 
145 /* RS_ASSERT() helper function */
146 void rs_assert_(const char*file, unsigned line, const char*fn, const char*cond)
147 {
148 	int no_assert = rs_suppress_assert, save_errno = errno;
149 	const char *s;
150 
151 	if ( no_assert >= 0 ) {
152 		if ( no_assert == 0 && (s = getenv( "NO_RS_ASSERT" )) && *s ) {
153 			no_assert = rs_suppress_assert = atoi( s );
154 		}
155 		if ( no_assert > 0 ) {
156 			errno = save_errno;
157 			return;
158 		}
159 	}
160 
161 #ifdef rs_assert_	/* proto-slap.h #defined away the fn parameter */
162 	fprintf( stderr,"%s:%u: "  "RS_ASSERT(%s) failed.\n", file,line,cond );
163 #else
164 	fprintf( stderr,"%s:%u: %s: RS_ASSERT(%s) failed.\n", file,line,fn,cond );
165 #endif
166 	fflush( stderr );
167 
168 	errno = save_errno;
169 	/* $NO_RS_ASSERT > 0: ignore rs_asserts, 0: abort, < 0: just warn */
170 	if ( !no_assert /* from $NO_RS_ASSERT */ ) abort();
171 }
172 
173 /* SlapReply is consistent */
174 void
175 (rs_assert_ok)( const SlapReply *rs )
176 {
177 	const slap_mask_t flags = rs->sr_flags;
178 
179 	if ( flags & REP_ENTRY_MASK ) {
180 		RS_ASSERT( !(flags & REP_ENTRY_MUSTRELEASE)
181 			|| !(flags & (REP_ENTRY_MASK ^ REP_ENTRY_MUSTRELEASE)) );
182 		RS_ASSERT( rs->sr_entry != NULL );
183 		RS_ASSERT( (1 << rs->sr_type) &
184 			((1 << REP_SEARCH) | (1 << REP_SEARCHREF) |
185 			 (1 << REP_RESULT) | (1 << REP_GLUE_RESULT)) );
186 	}
187 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
188 	if ( (flags & (REP_MATCHED_MASK | REP_REF_MASK | REP_CTRLS_MASK)) ) {
189 		RS_ASSERT( !(flags & REP_MATCHED_MASK) || rs->sr_matched );
190 		RS_ASSERT( !(flags & REP_CTRLS_MASK  ) || rs->sr_ctrls   );
191 		/* Note: LDAP_REFERRAL + !sr_ref is OK, becomes LDAP_NO_SUCH_OBJECT */
192 	}
193 #if (USE_RS_ASSERT) > 2
194 	if ( rs->sr_err == LDAP_SUCCESS ) {
195 		RS_ASSERT( rs->sr_text == NULL );
196 		RS_ASSERT( rs->sr_matched == NULL );
197 	}
198 #endif
199 #endif
200 }
201 
202 /* Ready for calling a new backend operation */
203 void
204 (rs_assert_ready)( const SlapReply *rs )
205 {
206 	RS_ASSERT( !rs->sr_entry   );
207 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
208 	RS_ASSERT( !rs->sr_text    );
209 	RS_ASSERT( !rs->sr_ref     );
210 	RS_ASSERT( !rs->sr_matched );
211 	RS_ASSERT( !rs->sr_ctrls   );
212 	RS_ASSERT( !rs->sr_flags   );
213 #if (USE_RS_ASSERT) > 2
214 	RS_ASSERT( rs->sr_err == LDAP_SUCCESS );
215 #endif
216 #else
217 	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
218 #endif
219 }
220 
221 /* Backend operation done */
222 void
223 (rs_assert_done)( const SlapReply *rs )
224 {
225 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
226 	RS_ASSERT( !(rs->sr_flags & ~(REP_ENTRY_MODIFIABLE|REP_NO_OPERATIONALS)) );
227 	rs_assert_ok( rs );
228 #else
229 	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MUSTFLUSH) );
230 #endif
231 }
232 
233 #endif /* LDAP_TEST || USE_RS_ASSERT */
234 
235 /* Reset a used SlapReply whose contents has been flushed (freed/released) */
236 void
237 (rs_reinit)( SlapReply *rs, slap_reply_t type )
238 {
239 	rs_reinit( rs, type );		/* proto-slap.h macro */
240 }
241 
242 /* Obey and clear rs->sr_flags & REP_ENTRY_MASK.  Clear sr_entry if freed. */
243 void
244 rs_flush_entry( Operation *op, SlapReply *rs, slap_overinst *on )
245 {
246 	rs_assert_ok( rs );
247 
248 	if ( (rs->sr_flags & REP_ENTRY_MUSTFLUSH) && rs->sr_entry != NULL ) {
249 		if ( !(rs->sr_flags & REP_ENTRY_MUSTRELEASE) ) {
250 			entry_free( rs->sr_entry );
251 		} else if ( on != NULL ) {
252 			overlay_entry_release_ov( op, rs->sr_entry, 0, on );
253 		} else {
254 			be_entry_release_rw( op, rs->sr_entry, 0 );
255 		}
256 		rs->sr_entry = NULL;
257 	}
258 
259 	rs->sr_flags &= ~REP_ENTRY_MASK;
260 }
261 
262 /* Set rs->sr_entry after obeying and clearing sr_flags & REP_ENTRY_MASK. */
263 void
264 rs_replace_entry( Operation *op, SlapReply *rs, slap_overinst *on, Entry *e )
265 {
266 	rs_flush_entry( op, rs, on );
267 	rs->sr_entry = e;
268 }
269 
270 /*
271  * Ensure rs->sr_entry is modifiable, by duplicating it if necessary.
272  * Obey sr_flags.  Set REP_ENTRY_<MODIFIABLE, and MUSTBEFREED if duplicated>.
273  * Return nonzero if rs->sr_entry was replaced.
274  */
275 int
276 rs_entry2modifiable( Operation *op, SlapReply *rs, slap_overinst *on )
277 {
278 	if ( rs->sr_flags & REP_ENTRY_MODIFIABLE ) {
279 		rs_assert_ok( rs );
280 		return 0;
281 	}
282 	rs_replace_entry( op, rs, on, entry_dup( rs->sr_entry ));
283 	rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
284 	return 1;
285 }
286 
287 /* Check for any callbacks that want to be informed about being blocked
288  * on output. These callbacks are expected to leave the callback list
289  * unmodified. Their result is ignored.
290  */
291 static void
292 slap_writewait_play(
293 	Operation *op )
294 {
295 	slap_callback	*sc = op->o_callback;
296 
297 	for ( ; sc; sc = sc->sc_next ) {
298 		if ( sc->sc_writewait )
299 			sc->sc_writewait( op, sc );
300 	}
301 }
302 
303 static long send_ldap_ber(
304 	Operation *op,
305 	BerElement *ber )
306 {
307 	Connection *conn = op->o_conn;
308 	ber_len_t bytes;
309 	long ret = 0;
310 
311 	ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
312 
313 	/* write only one pdu at a time - wait til it's our turn */
314 	ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
315 	if (( op->o_abandon && !op->o_cancel ) || !connection_valid( conn ) ||
316 		conn->c_writers < 0 ) {
317 		ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
318 		return 0;
319 	}
320 
321 	conn->c_writers++;
322 
323 	while ( conn->c_writers > 0 && conn->c_writing ) {
324 		ldap_pvt_thread_cond_wait( &conn->c_write1_cv, &conn->c_write1_mutex );
325 	}
326 
327 	/* connection was closed under us */
328 	if ( conn->c_writers < 0 ) {
329 		/* we're the last waiter, let the closer continue */
330 		if ( conn->c_writers == -1 )
331 			ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
332 		conn->c_writers++;
333 		ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
334 		return 0;
335 	}
336 
337 	/* Our turn */
338 	conn->c_writing = 1;
339 
340 	/* write the pdu */
341 	while( 1 ) {
342 		int err;
343 
344 		if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
345 			ret = bytes;
346 			break;
347 		}
348 
349 		err = sock_errno();
350 
351 		/*
352 		 * we got an error.  if it's ewouldblock, we need to
353 		 * wait on the socket being writable.  otherwise, figure
354 		 * it's a hard error and return.
355 		 */
356 
357 		Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
358 		    err, sock_errstr(err), 0 );
359 
360 		if ( err != EWOULDBLOCK && err != EAGAIN ) {
361 			conn->c_writers--;
362 			conn->c_writing = 0;
363 			ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
364 			ldap_pvt_thread_mutex_lock( &conn->c_mutex );
365 			connection_closing( conn, "connection lost on write" );
366 
367 			ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
368 			return -1;
369 		}
370 
371 		/* wait for socket to be write-ready */
372 		slap_writewait_play( op );
373 		ldap_pvt_thread_mutex_lock( &conn->c_write2_mutex );
374 		conn->c_writewaiter = 1;
375 		slapd_set_write( conn->c_sd, 2 );
376 
377 		ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
378 		ldap_pvt_thread_pool_idle( &connection_pool );
379 		ldap_pvt_thread_cond_wait( &conn->c_write2_cv, &conn->c_write2_mutex );
380 		conn->c_writewaiter = 0;
381 		ldap_pvt_thread_mutex_unlock( &conn->c_write2_mutex );
382 		ldap_pvt_thread_pool_unidle( &connection_pool );
383 		ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
384 		if ( conn->c_writers < 0 ) {
385 			ret = 0;
386 			break;
387 		}
388 	}
389 
390 	conn->c_writing = 0;
391 	if ( conn->c_writers < 0 ) {
392 		conn->c_writers++;
393 		if ( !conn->c_writers )
394 			ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
395 	} else {
396 		conn->c_writers--;
397 		ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
398 	}
399 	ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
400 
401 	return ret;
402 }
403 
404 static int
405 send_ldap_control( BerElement *ber, LDAPControl *c )
406 {
407 	int rc;
408 
409 	assert( c != NULL );
410 
411 	rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
412 
413 	if( c->ldctl_iscritical ) {
414 		rc = ber_printf( ber, "b",
415 			(ber_int_t) c->ldctl_iscritical ) ;
416 		if( rc == -1 ) return rc;
417 	}
418 
419 	if( c->ldctl_value.bv_val != NULL ) {
420 		rc = ber_printf( ber, "O", &c->ldctl_value );
421 		if( rc == -1 ) return rc;
422 	}
423 
424 	rc = ber_printf( ber, /*{*/"N}" );
425 	if( rc == -1 ) return rc;
426 
427 	return 0;
428 }
429 
430 static int
431 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
432 {
433 	int rc;
434 
435 	if( c == NULL )
436 		return 0;
437 
438 	rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
439 	if( rc == -1 ) return rc;
440 
441 	for( ; *c != NULL; c++) {
442 		rc = send_ldap_control( ber, *c );
443 		if( rc == -1 ) return rc;
444 	}
445 
446 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
447 	/* this is a hack to avoid having to modify op->s_ctrls */
448 	if( o->o_sortedresults ) {
449 		BerElementBuffer berbuf;
450 		BerElement *sber = (BerElement *) &berbuf;
451 		LDAPControl sorted;
452 		BER_BVZERO( &sorted.ldctl_value );
453 		sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
454 		sorted.ldctl_iscritical = 0;
455 
456 		ber_init2( sber, NULL, LBER_USE_DER );
457 
458 		ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
459 
460 		if( ber_flatten2( sber, &sorted.ldctl_value, 0 ) == -1 ) {
461 			return -1;
462 		}
463 
464 		(void) ber_free_buf( sber );
465 
466 		rc = send_ldap_control( ber, &sorted );
467 		if( rc == -1 ) return rc;
468 	}
469 #endif
470 
471 	rc = ber_printf( ber, /*{*/"N}" );
472 
473 	return rc;
474 }
475 
476 /*
477  * slap_response_play()
478  *
479  * plays the callback list; rationale: a callback can
480  *   - remove itself from the list, by setting op->o_callback = NULL;
481  *     malloc()'ed callbacks should free themselves from inside the
482  *     sc_response() function.
483  *   - replace itself with another (list of) callback(s), by setting
484  *     op->o_callback = a new (list of) callback(s); in this case, it
485  *     is the callback's responsibility to to append existing subsequent
486  *     callbacks to the end of the list that is passed to the sc_response()
487  *     function.
488  *   - modify the list of subsequent callbacks by modifying the value
489  *     of the sc_next field from inside the sc_response() function; this
490  *     case does not require any handling from inside slap_response_play()
491  *
492  * To stop execution of the playlist, the sc_response() function must return
493  * a value different from SLAP_SC_CONTINUE.
494  *
495  * The same applies to slap_cleanup_play(); only, there is no means to stop
496  * execution of the playlist, since all cleanup functions must be called.
497  */
498 static int
499 slap_response_play(
500 	Operation *op,
501 	SlapReply *rs )
502 {
503 	int rc;
504 
505 	slap_callback	*sc = op->o_callback, **scp;
506 
507 	rc = SLAP_CB_CONTINUE;
508 	for ( scp = &sc; *scp; ) {
509 		slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
510 
511 		op->o_callback = *scp;
512 		if ( op->o_callback->sc_response ) {
513 			rc = op->o_callback->sc_response( op, rs );
514 			if ( op->o_callback == NULL ) {
515 				/* the callback has been removed;
516 				 * repair the list */
517 				*scp = sc_next;
518 				sc_nextp = scp;
519 
520 			} else if ( op->o_callback != *scp ) {
521 				/* a new callback has been inserted
522 				 * in place of the existing one; repair the list */
523 				*scp = op->o_callback;
524 				sc_nextp = scp;
525 			}
526 			if ( rc != SLAP_CB_CONTINUE ) break;
527 		}
528 		scp = sc_nextp;
529 	}
530 
531 	op->o_callback = sc;
532 	return rc;
533 }
534 
535 static int
536 slap_cleanup_play(
537 	Operation *op,
538 	SlapReply *rs )
539 {
540 	slap_callback	*sc = op->o_callback, **scp;
541 
542 	for ( scp = &sc; *scp; ) {
543 		slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
544 
545 		op->o_callback = *scp;
546 		if ( op->o_callback->sc_cleanup ) {
547 			(void)op->o_callback->sc_cleanup( op, rs );
548 			if ( op->o_callback == NULL ) {
549 				/* the callback has been removed;
550 				 * repair the list */
551 				*scp = sc_next;
552 				sc_nextp = scp;
553 
554 			} else if ( op->o_callback != *scp ) {
555 				/* a new callback has been inserted
556 				 * after the existing one; repair the list */
557 				/* a new callback has been inserted
558 				 * in place of the existing one; repair the list */
559 				*scp = op->o_callback;
560 				sc_nextp = scp;
561 			}
562 			/* don't care about the result; do all cleanup */
563 		}
564 		scp = sc_nextp;
565 	}
566 
567 	op->o_callback = sc;
568 	return LDAP_SUCCESS;
569 }
570 
571 static int
572 send_ldap_response(
573 	Operation *op,
574 	SlapReply *rs )
575 {
576 	BerElementBuffer berbuf;
577 	BerElement	*ber = (BerElement *) &berbuf;
578 	int		rc = LDAP_SUCCESS;
579 	long	bytes;
580 
581 	/* op was actually aborted, bypass everything if client didn't Cancel */
582 	if (( rs->sr_err == SLAPD_ABANDON ) && !op->o_cancel ) {
583 		rc = SLAPD_ABANDON;
584 		goto clean2;
585 	}
586 
587 	if ( op->o_callback ) {
588 		rc = slap_response_play( op, rs );
589 		if ( rc != SLAP_CB_CONTINUE ) {
590 			goto clean2;
591 		}
592 	}
593 
594 	/* op completed, connection aborted, bypass sending response */
595 	if ( op->o_abandon && !op->o_cancel ) {
596 		rc = SLAPD_ABANDON;
597 		goto clean2;
598 	}
599 
600 #ifdef LDAP_CONNECTIONLESS
601 	if (op->o_conn && op->o_conn->c_is_udp)
602 		ber = op->o_res_ber;
603 	else
604 #endif
605 	{
606 		ber_init_w_nullc( ber, LBER_USE_DER );
607 		ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
608 	}
609 
610 	rc = rs->sr_err;
611 	if ( rc == SLAPD_ABANDON && op->o_cancel )
612 		rc = LDAP_CANCELLED;
613 
614 	Debug( LDAP_DEBUG_TRACE,
615 		"send_ldap_response: msgid=%d tag=%lu err=%d\n",
616 		rs->sr_msgid, rs->sr_tag, rc );
617 
618 	if( rs->sr_ref ) {
619 		Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
620 			rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
621 			NULL, NULL );
622 	}
623 
624 #ifdef LDAP_CONNECTIONLESS
625 	if (op->o_conn && op->o_conn->c_is_udp &&
626 		op->o_protocol == LDAP_VERSION2 )
627 	{
628 		rc = ber_printf( ber, "t{ess" /*"}"*/,
629 			rs->sr_tag, rc,
630 		rs->sr_matched == NULL ? "" : rs->sr_matched,
631 		rs->sr_text == NULL ? "" : rs->sr_text );
632 	} else
633 #endif
634 	if ( rs->sr_type == REP_INTERMEDIATE ) {
635 	    rc = ber_printf( ber, "{it{" /*"}}"*/,
636 			rs->sr_msgid, rs->sr_tag );
637 
638 	} else {
639 	    rc = ber_printf( ber, "{it{ess" /*"}}"*/,
640 		rs->sr_msgid, rs->sr_tag, rc,
641 		rs->sr_matched == NULL ? "" : rs->sr_matched,
642 		rs->sr_text == NULL ? "" : rs->sr_text );
643 	}
644 
645 	if( rc != -1 ) {
646 		if ( rs->sr_ref != NULL ) {
647 			assert( rs->sr_err == LDAP_REFERRAL );
648 			rc = ber_printf( ber, "t{W}",
649 				LDAP_TAG_REFERRAL, rs->sr_ref );
650 		} else {
651 			assert( rs->sr_err != LDAP_REFERRAL );
652 		}
653 	}
654 
655 	if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
656 		rc = ber_printf( ber, "tO",
657 			LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
658 	}
659 
660 	if( rc != -1 &&
661 		( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
662 	{
663 		if ( rs->sr_rspoid != NULL ) {
664 			rc = ber_printf( ber, "ts",
665 				rs->sr_type == REP_EXTENDED
666 					? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
667 				rs->sr_rspoid );
668 		}
669 		if( rc != -1 && rs->sr_rspdata != NULL ) {
670 			rc = ber_printf( ber, "tO",
671 				rs->sr_type == REP_EXTENDED
672 					? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
673 				rs->sr_rspdata );
674 		}
675 	}
676 
677 	if( rc != -1 ) {
678 		rc = ber_printf( ber, /*"{"*/ "N}" );
679 	}
680 
681 	if( rc != -1 ) {
682 		rc = send_ldap_controls( op, ber, rs->sr_ctrls );
683 	}
684 
685 	if( rc != -1 ) {
686 		rc = ber_printf( ber, /*"{"*/ "N}" );
687 	}
688 
689 #ifdef LDAP_CONNECTIONLESS
690 	if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
691 		&& rc != -1 )
692 	{
693 		rc = ber_printf( ber, /*"{"*/ "N}" );
694 	}
695 #endif
696 
697 	if ( rc == -1 ) {
698 		Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
699 
700 #ifdef LDAP_CONNECTIONLESS
701 		if (!op->o_conn || op->o_conn->c_is_udp == 0)
702 #endif
703 		{
704 			ber_free_buf( ber );
705 		}
706 		goto cleanup;
707 	}
708 
709 	/* send BER */
710 	bytes = send_ldap_ber( op, ber );
711 #ifdef LDAP_CONNECTIONLESS
712 	if (!op->o_conn || op->o_conn->c_is_udp == 0)
713 #endif
714 	{
715 		ber_free_buf( ber );
716 	}
717 
718 	if ( bytes < 0 ) {
719 		Debug( LDAP_DEBUG_ANY,
720 			"send_ldap_response: ber write failed\n",
721 			0, 0, 0 );
722 
723 		goto cleanup;
724 	}
725 
726 	ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
727 	ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
728 	ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
729 	ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
730 
731 cleanup:;
732 	/* Tell caller that we did this for real, as opposed to being
733 	 * overridden by a callback
734 	 */
735 	rc = SLAP_CB_CONTINUE;
736 
737 clean2:;
738 	if ( op->o_callback ) {
739 		(void)slap_cleanup_play( op, rs );
740 	}
741 
742 	if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
743 		rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
744 		if ( rs->sr_matched ) {
745 			free( (char *)rs->sr_matched );
746 			rs->sr_matched = NULL;
747 		}
748 	}
749 
750 	if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
751 		rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
752 		if ( rs->sr_ref ) {
753 			ber_bvarray_free( rs->sr_ref );
754 			rs->sr_ref = NULL;
755 		}
756 	}
757 
758 	if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
759 		rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
760 		if ( rs->sr_ctrls ) {
761 			slap_free_ctrls( op, rs->sr_ctrls );
762 			rs->sr_ctrls = NULL;
763 		}
764 	}
765 
766 	return rc;
767 }
768 
769 
770 void
771 send_ldap_disconnect( Operation	*op, SlapReply *rs )
772 {
773 #define LDAP_UNSOLICITED_ERROR(e) \
774 	(  (e) == LDAP_PROTOCOL_ERROR \
775 	|| (e) == LDAP_STRONG_AUTH_REQUIRED \
776 	|| (e) == LDAP_UNAVAILABLE )
777 
778 	Debug( LDAP_DEBUG_TRACE,
779 		"send_ldap_disconnect %d:%s\n",
780 		rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
781 	assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
782 
783 	/* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
784 	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
785 	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */
786 
787 	rs->sr_type = REP_EXTENDED;
788 	rs->sr_rspdata = NULL;
789 
790 	if ( op->o_protocol < LDAP_VERSION3 ) {
791 		rs->sr_rspoid = NULL;
792 		rs->sr_tag = slap_req2res( op->o_tag );
793 		rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
794 
795 	} else {
796 		rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
797 		rs->sr_tag = LDAP_RES_EXTENDED;
798 		rs->sr_msgid = LDAP_RES_UNSOLICITED;
799 	}
800 
801 	if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
802 		Statslog( LDAP_DEBUG_STATS,
803 			"%s DISCONNECT tag=%lu err=%d text=%s\n",
804 			op->o_log_prefix, rs->sr_tag, rs->sr_err,
805 			rs->sr_text ? rs->sr_text : "", 0 );
806 	}
807 }
808 
809 void
810 slap_send_ldap_result( Operation *op, SlapReply *rs )
811 {
812 	char *tmp = NULL;
813 	const char *otext = rs->sr_text;
814 	BerVarray oref = rs->sr_ref;
815 
816 	rs->sr_type = REP_RESULT;
817 
818 	/* Propagate Abandons so that cleanup callbacks can be processed */
819 	if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
820 		goto abandon;
821 
822 	Debug( LDAP_DEBUG_TRACE,
823 		"send_ldap_result: %s p=%d\n",
824 		op->o_log_prefix, op->o_protocol, 0 );
825 	Debug( LDAP_DEBUG_ARGS,
826 		"send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
827 		rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
828 		rs->sr_text ? rs->sr_text : "" );
829 	if( rs->sr_ref ) {
830 		Debug( LDAP_DEBUG_ARGS,
831 			"send_ldap_result: referral=\"%s\"\n",
832 			rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
833 			NULL, NULL );
834 	}
835 	assert( !LDAP_API_ERROR( rs->sr_err ) );
836 	assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
837 
838 	if ( rs->sr_err == LDAP_REFERRAL ) {
839 		if( op->o_domain_scope ) rs->sr_ref = NULL;
840 
841 		if( rs->sr_ref == NULL ) {
842 			rs->sr_err = LDAP_NO_SUCH_OBJECT;
843 		} else if ( op->o_protocol < LDAP_VERSION3 ) {
844 			rs->sr_err = LDAP_PARTIAL_RESULTS;
845 		}
846 	}
847 
848 	if ( op->o_protocol < LDAP_VERSION3 ) {
849 		tmp = v2ref( rs->sr_ref, rs->sr_text );
850 		rs->sr_text = tmp;
851 		rs->sr_ref = NULL;
852 	}
853 
854 abandon:
855 	rs->sr_tag = slap_req2res( op->o_tag );
856 	rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
857 
858 	if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
859 		if ( rs->sr_ref == NULL ) {
860 			rs->sr_flags ^= REP_REF_MUSTBEFREED;
861 			ber_bvarray_free( oref );
862 		}
863 		oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
864 	}
865 
866 	if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
867 		if ( op->o_tag == LDAP_REQ_SEARCH ) {
868 			Statslog( LDAP_DEBUG_STATS,
869 				"%s SEARCH RESULT tag=%lu err=%d nentries=%d text=%s\n",
870 				op->o_log_prefix, rs->sr_tag, rs->sr_err,
871 				rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
872 		} else {
873 			Statslog( LDAP_DEBUG_STATS,
874 				"%s RESULT tag=%lu err=%d text=%s\n",
875 				op->o_log_prefix, rs->sr_tag, rs->sr_err,
876 				rs->sr_text ? rs->sr_text : "", 0 );
877 		}
878 	}
879 
880 	if( tmp != NULL ) ch_free(tmp);
881 	rs->sr_text = otext;
882 	rs->sr_ref = oref;
883 }
884 
885 void
886 send_ldap_sasl( Operation *op, SlapReply *rs )
887 {
888 	Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
889 		rs->sr_err,
890 		rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
891 
892 	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
893 	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */
894 
895 	rs->sr_type = REP_SASL;
896 	rs->sr_tag = slap_req2res( op->o_tag );
897 	rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
898 
899 	if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
900 		Statslog( LDAP_DEBUG_STATS,
901 			"%s RESULT tag=%lu err=%d text=%s\n",
902 			op->o_log_prefix, rs->sr_tag, rs->sr_err,
903 			rs->sr_text ? rs->sr_text : "", 0 );
904 	}
905 }
906 
907 void
908 slap_send_ldap_extended( Operation *op, SlapReply *rs )
909 {
910 	Debug( LDAP_DEBUG_TRACE,
911 		"send_ldap_extended: err=%d oid=%s len=%ld\n",
912 		rs->sr_err,
913 		rs->sr_rspoid ? rs->sr_rspoid : "",
914 		rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
915 
916 	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
917 	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */
918 
919 	rs->sr_type = REP_EXTENDED;
920 	rs->sr_tag = slap_req2res( op->o_tag );
921 	rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
922 
923 	if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
924 		Statslog( LDAP_DEBUG_STATS,
925 			"%s RESULT oid=%s err=%d text=%s\n",
926 			op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
927 			rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
928 	}
929 }
930 
931 void
932 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
933 {
934 	Debug( LDAP_DEBUG_TRACE,
935 		"send_ldap_intermediate: err=%d oid=%s len=%ld\n",
936 		rs->sr_err,
937 		rs->sr_rspoid ? rs->sr_rspoid : "",
938 		rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
939 
940 	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
941 	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */
942 
943 	rs->sr_type = REP_INTERMEDIATE;
944 	rs->sr_tag = LDAP_RES_INTERMEDIATE;
945 	rs->sr_msgid = op->o_msgid;
946 	if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
947 		Statslog( LDAP_DEBUG_STATS2,
948 			"%s INTERM oid=%s\n",
949 			op->o_log_prefix,
950 			rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
951 	}
952 }
953 
954 #define set_ldap_error( rs, err, text ) do { \
955 		(rs)->sr_err = err; (rs)->sr_text = text; } while(0)
956 
957 /*
958  * returns:
959  *
960  * LDAP_SUCCESS			entry sent
961  * LDAP_OTHER			entry not sent (other)
962  * LDAP_INSUFFICIENT_ACCESS	entry not sent (ACL)
963  * LDAP_UNAVAILABLE		entry not sent (connection closed)
964  * LDAP_SIZELIMIT_EXCEEDED	entry not sent (caller must send sizelimitExceeded)
965  */
966 
967 int
968 slap_send_search_entry( Operation *op, SlapReply *rs )
969 {
970 	BerElementBuffer berbuf;
971 	BerElement	*ber = (BerElement *) &berbuf;
972 	Attribute	*a;
973 	int		i, j, rc = LDAP_UNAVAILABLE, bytes;
974 	int		userattrs;
975 	AccessControlState acl_state = ACL_STATE_INIT;
976 	int			 attrsonly;
977 	AttributeDescription *ad_entry = slap_schema.si_ad_entry;
978 
979 	/* a_flags: array of flags telling if the i-th element will be
980 	 *          returned or filtered out
981 	 * e_flags: array of a_flags
982 	 */
983 	char **e_flags = NULL;
984 
985 	rs->sr_type = REP_SEARCH;
986 
987 	if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
988 		rc = LDAP_SIZELIMIT_EXCEEDED;
989 		goto error_return;
990 	}
991 
992 	/* Every 64 entries, check for thread pool pause */
993 	if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
994 		ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
995 	{
996 		rc = LDAP_BUSY;
997 		goto error_return;
998 	}
999 
1000 	/* eventually will loop through generated operational attribute types
1001 	 * currently implemented types include:
1002 	 *	entryDN, subschemaSubentry, and hasSubordinates */
1003 	/* NOTE: moved before overlays callback circling because
1004 	 * they may modify entry and other stuff in rs */
1005 	/* check for special all operational attributes ("+") type */
1006 	/* FIXME: maybe we could set this flag at the operation level;
1007 	 * however, in principle the caller of send_search_entry() may
1008 	 * change the attribute list at each call */
1009 	rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
1010 
1011 	rc = backend_operational( op, rs );
1012 	if ( rc ) {
1013 		goto error_return;
1014 	}
1015 
1016 	if ( op->o_callback ) {
1017 		rc = slap_response_play( op, rs );
1018 		if ( rc != SLAP_CB_CONTINUE ) {
1019 			goto error_return;
1020 		}
1021 	}
1022 
1023 	Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
1024 		op->o_connid, rs->sr_entry->e_name.bv_val,
1025 		op->ors_attrsonly ? " (attrsOnly)" : "" );
1026 
1027 	attrsonly = op->ors_attrsonly;
1028 
1029 	if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
1030 		Debug( LDAP_DEBUG_ACL,
1031 			"send_search_entry: conn %lu access to entry (%s) not allowed\n",
1032 			op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
1033 
1034 		rc = LDAP_INSUFFICIENT_ACCESS;
1035 		goto error_return;
1036 	}
1037 
1038 	if ( op->o_res_ber ) {
1039 		/* read back control or LDAP_CONNECTIONLESS */
1040 	    ber = op->o_res_ber;
1041 	} else {
1042 		struct berval	bv;
1043 
1044 		bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1045 		bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1046 
1047 		ber_init2( ber, &bv, LBER_USE_DER );
1048 		ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1049 	}
1050 
1051 #ifdef LDAP_CONNECTIONLESS
1052 	if ( op->o_conn && op->o_conn->c_is_udp ) {
1053 		/* CONNECTIONLESS */
1054 		if ( op->o_protocol == LDAP_VERSION2 ) {
1055 	    	rc = ber_printf(ber, "t{O{" /*}}*/,
1056 				LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1057 		} else {
1058 	    	rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1059 				LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1060 		}
1061 	} else
1062 #endif
1063 	if ( op->o_res_ber ) {
1064 		/* read back control */
1065 	    rc = ber_printf( ber, "t{O{" /*}}*/,
1066 			LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1067 	} else {
1068 	    rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1069 			LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1070 	}
1071 
1072 	if ( rc == -1 ) {
1073 		Debug( LDAP_DEBUG_ANY,
1074 			"send_search_entry: conn %lu  ber_printf failed\n",
1075 			op->o_connid, 0, 0 );
1076 
1077 		if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1078 		set_ldap_error( rs, LDAP_OTHER, "encoding DN error" );
1079 		rc = rs->sr_err;
1080 		goto error_return;
1081 	}
1082 
1083 	/* check for special all user attributes ("*") type */
1084 	userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
1085 
1086 	/* create an array of arrays of flags. Each flag corresponds
1087 	 * to particular value of attribute and equals 1 if value matches
1088 	 * to ValuesReturnFilter or 0 if not
1089 	 */
1090 	if ( op->o_vrFilter != NULL ) {
1091 		int	k = 0;
1092 		size_t	size;
1093 
1094 		for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1095 			for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1096 		}
1097 
1098 		size = i * sizeof(char *) + k;
1099 		if ( size > 0 ) {
1100 			char	*a_flags;
1101 			e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
1102 			if( e_flags == NULL ) {
1103 		    	Debug( LDAP_DEBUG_ANY,
1104 					"send_search_entry: conn %lu slap_sl_calloc failed\n",
1105 					op->o_connid, 0, 0 );
1106 				ber_free( ber, 1 );
1107 
1108 				set_ldap_error( rs, LDAP_OTHER, "out of memory" );
1109 				goto error_return;
1110 			}
1111 			a_flags = (char *)(e_flags + i);
1112 			memset( a_flags, 0, k );
1113 			for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
1114 				for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1115 				e_flags[i] = a_flags;
1116 				a_flags += j;
1117 			}
1118 
1119 			rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
1120 			if ( rc == -1 ) {
1121 			    	Debug( LDAP_DEBUG_ANY, "send_search_entry: "
1122 					"conn %lu matched values filtering failed\n",
1123 					op->o_connid, 0, 0 );
1124 				if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1125 				set_ldap_error( rs, LDAP_OTHER,
1126 					"matched values filtering error" );
1127 				rc = rs->sr_err;
1128 				goto error_return;
1129 			}
1130 		}
1131 	}
1132 
1133 	for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
1134 		AttributeDescription *desc = a->a_desc;
1135 		int finish = 0;
1136 
1137 		if ( rs->sr_attrs == NULL ) {
1138 			/* all user attrs request, skip operational attributes */
1139 			if( is_at_operational( desc->ad_type ) ) {
1140 				continue;
1141 			}
1142 
1143 		} else {
1144 			/* specific attrs requested */
1145 			if ( is_at_operational( desc->ad_type ) ) {
1146 				/* if not explicitly requested */
1147 				if ( !ad_inlist( desc, rs->sr_attrs )) {
1148 					/* if not all op attrs requested, skip */
1149 					if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
1150 						continue;
1151 					/* if DSA-specific and replicating, skip */
1152 					if ( op->o_sync != SLAP_CONTROL_NONE &&
1153 						desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1154 						continue;
1155 				}
1156 			} else {
1157 				if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1158 					continue;
1159 				}
1160 			}
1161 		}
1162 
1163 		if ( attrsonly ) {
1164 			if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1165 				ACL_READ, &acl_state ) )
1166 			{
1167 				Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1168 					"conn %lu access to attribute %s not allowed\n",
1169 				        op->o_connid, desc->ad_cname.bv_val, 0 );
1170 				continue;
1171 			}
1172 
1173 			if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1174 				Debug( LDAP_DEBUG_ANY,
1175 					"send_search_entry: conn %lu  ber_printf failed\n",
1176 					op->o_connid, 0, 0 );
1177 
1178 				if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1179 				set_ldap_error( rs, LDAP_OTHER,
1180 					"encoding description error");
1181 				rc = rs->sr_err;
1182 				goto error_return;
1183 			}
1184 			finish = 1;
1185 
1186 		} else {
1187 			int first = 1;
1188 			for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1189 				if ( ! access_allowed( op, rs->sr_entry,
1190 					desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1191 				{
1192 					Debug( LDAP_DEBUG_ACL,
1193 						"send_search_entry: conn %lu "
1194 						"access to attribute %s, value #%d not allowed\n",
1195 						op->o_connid, desc->ad_cname.bv_val, i );
1196 
1197 					continue;
1198 				}
1199 
1200 				if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1201 					continue;
1202 				}
1203 
1204 				if ( first ) {
1205 					first = 0;
1206 					finish = 1;
1207 					if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1208 						Debug( LDAP_DEBUG_ANY,
1209 							"send_search_entry: conn %lu  ber_printf failed\n",
1210 							op->o_connid, 0, 0 );
1211 
1212 						if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1213 						set_ldap_error( rs, LDAP_OTHER,
1214 							"encoding description error");
1215 						rc = rs->sr_err;
1216 						goto error_return;
1217 					}
1218 				}
1219 				if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1220 					Debug( LDAP_DEBUG_ANY,
1221 						"send_search_entry: conn %lu  "
1222 						"ber_printf failed.\n", op->o_connid, 0, 0 );
1223 
1224 					if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1225 					set_ldap_error( rs, LDAP_OTHER,
1226 						"encoding values error" );
1227 					rc = rs->sr_err;
1228 					goto error_return;
1229 				}
1230 			}
1231 		}
1232 
1233 		if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1234 			Debug( LDAP_DEBUG_ANY,
1235 				"send_search_entry: conn %lu ber_printf failed\n",
1236 				op->o_connid, 0, 0 );
1237 
1238 			if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1239 			set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1240 			rc = rs->sr_err;
1241 			goto error_return;
1242 		}
1243 	}
1244 
1245 	/* NOTE: moved before overlays callback circling because
1246 	 * they may modify entry and other stuff in rs */
1247 	if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1248 		int	k = 0;
1249 		size_t	size;
1250 
1251 		for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1252 			for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1253 		}
1254 
1255 		size = i * sizeof(char *) + k;
1256 		if ( size > 0 ) {
1257 			char	*a_flags, **tmp;
1258 
1259 			/*
1260 			 * Reuse previous memory - we likely need less space
1261 			 * for operational attributes
1262 			 */
1263 			tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1264 				op->o_tmpmemctx );
1265 			if ( tmp == NULL ) {
1266 			    	Debug( LDAP_DEBUG_ANY,
1267 					"send_search_entry: conn %lu "
1268 					"not enough memory "
1269 					"for matched values filtering\n",
1270 					op->o_connid, 0, 0 );
1271 				if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1272 				set_ldap_error( rs, LDAP_OTHER,
1273 					"not enough memory for matched values filtering" );
1274 				goto error_return;
1275 			}
1276 			e_flags = tmp;
1277 			a_flags = (char *)(e_flags + i);
1278 			memset( a_flags, 0, k );
1279 			for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1280 				for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1281 				e_flags[i] = a_flags;
1282 				a_flags += j;
1283 			}
1284 			rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1285 
1286 			if ( rc == -1 ) {
1287 			    	Debug( LDAP_DEBUG_ANY,
1288 					"send_search_entry: conn %lu "
1289 					"matched values filtering failed\n",
1290 					op->o_connid, 0, 0);
1291 				if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1292 				set_ldap_error( rs, LDAP_OTHER,
1293 					"matched values filtering error" );
1294 				rc = rs->sr_err;
1295 				goto error_return;
1296 			}
1297 		}
1298 	}
1299 
1300 	for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1301 		AttributeDescription *desc = a->a_desc;
1302 
1303 		if ( rs->sr_attrs == NULL ) {
1304 			/* all user attrs request, skip operational attributes */
1305 			if( is_at_operational( desc->ad_type ) ) {
1306 				continue;
1307 			}
1308 
1309 		} else {
1310 			/* specific attrs requested */
1311 			if( is_at_operational( desc->ad_type ) ) {
1312 				if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1313 					!ad_inlist( desc, rs->sr_attrs ) )
1314 				{
1315 					continue;
1316 				}
1317 				/* if DSA-specific and replicating, skip */
1318 				if ( op->o_sync != SLAP_CONTROL_NONE &&
1319 					desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1320 					continue;
1321 			} else {
1322 				if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1323 					continue;
1324 				}
1325 			}
1326 		}
1327 
1328 		if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1329 			ACL_READ, &acl_state ) )
1330 		{
1331 			Debug( LDAP_DEBUG_ACL,
1332 				"send_search_entry: conn %lu "
1333 				"access to attribute %s not allowed\n",
1334 				op->o_connid, desc->ad_cname.bv_val, 0 );
1335 
1336 			continue;
1337 		}
1338 
1339 		rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1340 		if ( rc == -1 ) {
1341 			Debug( LDAP_DEBUG_ANY,
1342 				"send_search_entry: conn %lu  "
1343 				"ber_printf failed\n", op->o_connid, 0, 0 );
1344 
1345 			if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1346 			set_ldap_error( rs, LDAP_OTHER,
1347 				"encoding description error" );
1348 			rc = rs->sr_err;
1349 			goto error_return;
1350 		}
1351 
1352 		if ( ! attrsonly ) {
1353 			for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1354 				if ( ! access_allowed( op, rs->sr_entry,
1355 					desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1356 				{
1357 					Debug( LDAP_DEBUG_ACL,
1358 						"send_search_entry: conn %lu "
1359 						"access to %s, value %d not allowed\n",
1360 						op->o_connid, desc->ad_cname.bv_val, i );
1361 
1362 					continue;
1363 				}
1364 
1365 				if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1366 					continue;
1367 				}
1368 
1369 				if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1370 					Debug( LDAP_DEBUG_ANY,
1371 						"send_search_entry: conn %lu  ber_printf failed\n",
1372 						op->o_connid, 0, 0 );
1373 
1374 					if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1375 					set_ldap_error( rs, LDAP_OTHER,
1376 						"encoding values error" );
1377 					rc = rs->sr_err;
1378 					goto error_return;
1379 				}
1380 			}
1381 		}
1382 
1383 		if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1384 			Debug( LDAP_DEBUG_ANY,
1385 				"send_search_entry: conn %lu  ber_printf failed\n",
1386 				op->o_connid, 0, 0 );
1387 
1388 			if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1389 			set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1390 			rc = rs->sr_err;
1391 			goto error_return;
1392 		}
1393 	}
1394 
1395 	/* free e_flags */
1396 	if ( e_flags ) {
1397 		slap_sl_free( e_flags, op->o_tmpmemctx );
1398 		e_flags = NULL;
1399 	}
1400 
1401 	rc = ber_printf( ber, /*{{*/ "}N}" );
1402 
1403 	if( rc != -1 ) {
1404 		rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1405 	}
1406 
1407 	if( rc != -1 ) {
1408 #ifdef LDAP_CONNECTIONLESS
1409 		if( op->o_conn && op->o_conn->c_is_udp ) {
1410 			if ( op->o_protocol != LDAP_VERSION2 ) {
1411 				rc = ber_printf( ber, /*{*/ "N}" );
1412 			}
1413 		} else
1414 #endif
1415 		if ( op->o_res_ber == NULL ) {
1416 			rc = ber_printf( ber, /*{*/ "N}" );
1417 		}
1418 	}
1419 
1420 	if ( rc == -1 ) {
1421 		Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1422 
1423 		if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1424 		set_ldap_error( rs, LDAP_OTHER, "encode entry end error" );
1425 		rc = rs->sr_err;
1426 		goto error_return;
1427 	}
1428 
1429 	Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1430 	    op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1431 
1432 	rs_flush_entry( op, rs, NULL );
1433 
1434 	if ( op->o_res_ber == NULL ) {
1435 		bytes = send_ldap_ber( op, ber );
1436 		ber_free_buf( ber );
1437 
1438 		if ( bytes < 0 ) {
1439 			Debug( LDAP_DEBUG_ANY,
1440 				"send_search_entry: conn %lu  ber write failed.\n",
1441 				op->o_connid, 0, 0 );
1442 
1443 			rc = LDAP_UNAVAILABLE;
1444 			goto error_return;
1445 		}
1446 		rs->sr_nentries++;
1447 
1448 		ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1449 		ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1450 		ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1451 		ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1452 		ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1453 	}
1454 
1455 	Debug( LDAP_DEBUG_TRACE,
1456 		"<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1457 
1458 	rc = LDAP_SUCCESS;
1459 
1460 error_return:;
1461 	if ( op->o_callback ) {
1462 		(void)slap_cleanup_play( op, rs );
1463 	}
1464 
1465 	if ( e_flags ) {
1466 		slap_sl_free( e_flags, op->o_tmpmemctx );
1467 	}
1468 
1469 	/* FIXME: Can break if rs now contains an extended response */
1470 	if ( rs->sr_operational_attrs ) {
1471 		attrs_free( rs->sr_operational_attrs );
1472 		rs->sr_operational_attrs = NULL;
1473 	}
1474 	rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1475 
1476 	if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1477 		rs_flush_entry( op, rs, NULL );
1478 	} else {
1479 		RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
1480 	}
1481 
1482 	if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1483 		rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1484 		if ( rs->sr_ctrls ) {
1485 			slap_free_ctrls( op, rs->sr_ctrls );
1486 			rs->sr_ctrls = NULL;
1487 		}
1488 	}
1489 
1490 	return( rc );
1491 }
1492 
1493 int
1494 slap_send_search_reference( Operation *op, SlapReply *rs )
1495 {
1496 	BerElementBuffer berbuf;
1497 	BerElement	*ber = (BerElement *) &berbuf;
1498 	int rc = 0;
1499 	int bytes;
1500 	char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1501 
1502 	AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1503 	AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1504 
1505 	rs->sr_type = REP_SEARCHREF;
1506 	if ( op->o_callback ) {
1507 		rc = slap_response_play( op, rs );
1508 		if ( rc != SLAP_CB_CONTINUE ) {
1509 			goto rel;
1510 		}
1511 	}
1512 
1513 	Debug( LDAP_DEBUG_TRACE,
1514 		"=> send_search_reference: dn=\"%s\"\n",
1515 		edn, 0, 0 );
1516 
1517 	if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1518 		ad_entry, NULL, ACL_READ, NULL ) )
1519 	{
1520 		Debug( LDAP_DEBUG_ACL,
1521 			"send_search_reference: access to entry not allowed\n",
1522 		    0, 0, 0 );
1523 		rc = 1;
1524 		goto rel;
1525 	}
1526 
1527 	if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1528 		ad_ref, NULL, ACL_READ, NULL ) )
1529 	{
1530 		Debug( LDAP_DEBUG_ACL,
1531 			"send_search_reference: access "
1532 			"to reference not allowed\n",
1533 		    0, 0, 0 );
1534 		rc = 1;
1535 		goto rel;
1536 	}
1537 
1538 	if( op->o_domain_scope ) {
1539 		Debug( LDAP_DEBUG_ANY,
1540 			"send_search_reference: domainScope control in (%s)\n",
1541 			edn, 0, 0 );
1542 		rc = 0;
1543 		goto rel;
1544 	}
1545 
1546 	if( rs->sr_ref == NULL ) {
1547 		Debug( LDAP_DEBUG_ANY,
1548 			"send_search_reference: null ref in (%s)\n",
1549 			edn, 0, 0 );
1550 		rc = 1;
1551 		goto rel;
1552 	}
1553 
1554 	if( op->o_protocol < LDAP_VERSION3 ) {
1555 		rc = 0;
1556 		/* save the references for the result */
1557 		if( rs->sr_ref[0].bv_val != NULL ) {
1558 			if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1559 				rc = LDAP_OTHER;
1560 		}
1561 		goto rel;
1562 	}
1563 
1564 #ifdef LDAP_CONNECTIONLESS
1565 	if( op->o_conn && op->o_conn->c_is_udp ) {
1566 		ber = op->o_res_ber;
1567 	} else
1568 #endif
1569 	{
1570 		ber_init_w_nullc( ber, LBER_USE_DER );
1571 		ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1572 	}
1573 
1574 	rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1575 		LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1576 
1577 	if( rc != -1 ) {
1578 		rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1579 	}
1580 
1581 	if( rc != -1 ) {
1582 		rc = ber_printf( ber, /*"{"*/ "N}" );
1583 	}
1584 
1585 	if ( rc == -1 ) {
1586 		Debug( LDAP_DEBUG_ANY,
1587 			"send_search_reference: ber_printf failed\n", 0, 0, 0 );
1588 
1589 #ifdef LDAP_CONNECTIONLESS
1590 		if (!op->o_conn || op->o_conn->c_is_udp == 0)
1591 #endif
1592 		ber_free_buf( ber );
1593 		set_ldap_error( rs, LDAP_OTHER, "encode DN error" );
1594 		goto rel;
1595 	}
1596 
1597 	rc = 0;
1598 	rs_flush_entry( op, rs, NULL );
1599 
1600 #ifdef LDAP_CONNECTIONLESS
1601 	if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1602 #endif
1603 	bytes = send_ldap_ber( op, ber );
1604 	ber_free_buf( ber );
1605 
1606 	if ( bytes < 0 ) {
1607 		rc = LDAP_UNAVAILABLE;
1608 	} else {
1609 		ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1610 		ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1611 		ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1612 		ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1613 		ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1614 	}
1615 #ifdef LDAP_CONNECTIONLESS
1616 	}
1617 #endif
1618 	if ( rs->sr_ref != NULL ) {
1619 		int	r;
1620 
1621 		for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1622 			Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1623 				op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1624 				0, 0 );
1625 		}
1626 
1627 	} else {
1628 		Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1629 			op->o_log_prefix, 0, 0, 0, 0 );
1630 	}
1631 
1632 	Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1633 
1634 	if ( 0 ) {
1635 rel:
1636 	    rs_flush_entry( op, rs, NULL );
1637 	}
1638 
1639 	if ( op->o_callback ) {
1640 		(void)slap_cleanup_play( op, rs );
1641 	}
1642 
1643 	if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1644 		rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1645 		if ( rs->sr_ctrls ) {
1646 			slap_free_ctrls( op, rs->sr_ctrls );
1647 			rs->sr_ctrls = NULL;
1648 		}
1649 	}
1650 
1651 	return rc;
1652 }
1653 
1654 int
1655 str2result(
1656     char	*s,
1657     int		*code,
1658     char	**matched,
1659     char	**info )
1660 {
1661 	int	rc;
1662 	char	*c;
1663 
1664 	*code = LDAP_SUCCESS;
1665 	*matched = NULL;
1666 	*info = NULL;
1667 
1668 	if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1669 		Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1670 		    s, 0, 0 );
1671 
1672 		return( -1 );
1673 	}
1674 
1675 	rc = 0;
1676 	while ( (s = strchr( s, '\n' )) != NULL ) {
1677 		*s++ = '\0';
1678 		if ( *s == '\0' ) {
1679 			break;
1680 		}
1681 		if ( (c = strchr( s, ':' )) != NULL ) {
1682 			c++;
1683 		}
1684 
1685 		if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1686 			char	*next = NULL;
1687 			long	retcode;
1688 
1689 			if ( c == NULL ) {
1690 				Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1691 				    s, 0, 0 );
1692 				rc = -1;
1693 				continue;
1694 			}
1695 
1696 			while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1697 			if ( c[ 0 ] == '\0' ) {
1698 				Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1699 				    s, 0, 0 );
1700 				rc = -1;
1701 				continue;
1702 			}
1703 
1704 			retcode = strtol( c, &next, 10 );
1705 			if ( next == NULL || next == c ) {
1706 				Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1707 				    s, 0, 0 );
1708 				rc = -1;
1709 				continue;
1710 			}
1711 
1712 			while ( isspace( (unsigned char) next[ 0 ] ) && next[ 0 ] != '\n' )
1713 				next++;
1714 			if ( next[ 0 ] != '\0' && next[ 0 ] != '\n' ) {
1715 				Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1716 				    s, 0, 0 );
1717 				rc = -1;
1718 				continue;
1719 			}
1720 
1721 			/* FIXME: what if it's larger than max int? */
1722 			*code = (int)retcode;
1723 
1724 		} else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1725 			if ( c != NULL ) {
1726 				*matched = c;
1727 			}
1728 		} else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1729 			if ( c != NULL ) {
1730 				*info = c;
1731 			}
1732 		} else {
1733 			Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1734 			    s, 0, 0 );
1735 
1736 			rc = -1;
1737 		}
1738 	}
1739 
1740 	return( rc );
1741 }
1742 
1743 int slap_read_controls(
1744 	Operation *op,
1745 	SlapReply *rs,
1746 	Entry *e,
1747 	const struct berval *oid,
1748 	LDAPControl **ctrl )
1749 {
1750 	int rc;
1751 	struct berval bv;
1752 	BerElementBuffer berbuf;
1753 	BerElement *ber = (BerElement *) &berbuf;
1754 	LDAPControl c;
1755 	Operation myop;
1756 
1757 	Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1758 		op->o_log_prefix, oid->bv_val, e->e_dn );
1759 
1760 	rs->sr_entry = e;
1761 	rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1762 		op->o_preread_attrs : op->o_postread_attrs;
1763 
1764 	bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1765 	bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1766 
1767 	ber_init2( ber, &bv, LBER_USE_DER );
1768 	ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1769 
1770 	/* create new operation */
1771 	myop = *op;
1772 	/* FIXME: o_bd needed for ACL */
1773 	myop.o_bd = op->o_bd;
1774 	myop.o_res_ber = ber;
1775 	myop.o_callback = NULL;
1776 	myop.ors_slimit = 1;
1777 	myop.ors_attrsonly = 0;
1778 
1779 	rc = slap_send_search_entry( &myop, rs );
1780 	if( rc ) return rc;
1781 
1782 	rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1783 
1784 	if( rc == -1 ) return LDAP_OTHER;
1785 
1786 	c.ldctl_oid = oid->bv_val;
1787 	c.ldctl_iscritical = 0;
1788 
1789 	if ( *ctrl == NULL ) {
1790 		/* first try */
1791 		*ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1792 	} else {
1793 		/* retry: free previous try */
1794 		slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1795 	}
1796 
1797 	**ctrl = c;
1798 	return LDAP_SUCCESS;
1799 }
1800 
1801 /* Map API errors to protocol errors... */
1802 int
1803 slap_map_api2result( SlapReply *rs )
1804 {
1805 	switch(rs->sr_err) {
1806 	case LDAP_SERVER_DOWN:
1807 		return LDAP_UNAVAILABLE;
1808 	case LDAP_LOCAL_ERROR:
1809 		return LDAP_OTHER;
1810 	case LDAP_ENCODING_ERROR:
1811 	case LDAP_DECODING_ERROR:
1812 		return LDAP_PROTOCOL_ERROR;
1813 	case LDAP_TIMEOUT:
1814 		return LDAP_UNAVAILABLE;
1815 	case LDAP_AUTH_UNKNOWN:
1816 		return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1817 	case LDAP_FILTER_ERROR:
1818 		rs->sr_text = "Filter error";
1819 		return LDAP_OTHER;
1820 	case LDAP_USER_CANCELLED:
1821 		rs->sr_text = "User cancelled";
1822 		return LDAP_OTHER;
1823 	case LDAP_PARAM_ERROR:
1824 		return LDAP_PROTOCOL_ERROR;
1825 	case LDAP_NO_MEMORY:
1826 		return LDAP_OTHER;
1827 	case LDAP_CONNECT_ERROR:
1828 		return LDAP_UNAVAILABLE;
1829 	case LDAP_NOT_SUPPORTED:
1830 		return LDAP_UNWILLING_TO_PERFORM;
1831 	case LDAP_CONTROL_NOT_FOUND:
1832 		return LDAP_PROTOCOL_ERROR;
1833 	case LDAP_NO_RESULTS_RETURNED:
1834 		return LDAP_NO_SUCH_OBJECT;
1835 	case LDAP_MORE_RESULTS_TO_RETURN:
1836 		rs->sr_text = "More results to return";
1837 		return LDAP_OTHER;
1838 	case LDAP_CLIENT_LOOP:
1839 	case LDAP_REFERRAL_LIMIT_EXCEEDED:
1840 		return LDAP_LOOP_DETECT;
1841 	default:
1842 		if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1843 		return rs->sr_err;
1844 	}
1845 }
1846 
1847 
1848 slap_mask_t
1849 slap_attr_flags( AttributeName *an )
1850 {
1851 	slap_mask_t	flags = SLAP_ATTRS_UNDEFINED;
1852 
1853 	if ( an == NULL ) {
1854 		flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1855 
1856 	} else {
1857 		flags |= an_find( an, slap_bv_all_operational_attrs )
1858 			? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1859 		flags |= an_find( an, slap_bv_all_user_attrs )
1860 			? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
1861 	}
1862 
1863 	return flags;
1864 }
1865