1 /* result.c - routines to send ldap results, errors, and referrals */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2014 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
33 #include <ac/string.h>
36 #include <ac/unistd.h>
40 const struct berval slap_dummy_bv = BER_BVNULL;
42 int slap_null_cb( Operation *op, SlapReply *rs )
47 int slap_freeself_cb( Operation *op, SlapReply *rs )
49 assert( op->o_callback != NULL );
51 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
52 op->o_callback = NULL;
54 return SLAP_CB_CONTINUE;
57 static char *v2ref( BerVarray ref, const char *text )
59 size_t len = 0, i = 0;
64 return ch_strdup(text);
72 if (text[len-1] != '\n') {
77 v2 = ch_malloc( len+i+sizeof("Referral:") );
85 strcpy( v2+len, "Referral:" );
86 len += sizeof("Referral:");
88 for( i=0; ref[i].bv_val != NULL; i++ ) {
89 v2 = ch_realloc( v2, len + ref[i].bv_len + 1 );
91 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
93 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
103 slap_req2res( ber_tag_t tag )
108 case LDAP_REQ_COMPARE:
109 case LDAP_REQ_EXTENDED:
110 case LDAP_REQ_MODIFY:
111 case LDAP_REQ_MODRDN:
115 case LDAP_REQ_DELETE:
116 tag = LDAP_RES_DELETE;
119 case LDAP_REQ_ABANDON:
120 case LDAP_REQ_UNBIND:
124 case LDAP_REQ_SEARCH:
125 tag = LDAP_RES_SEARCH_RESULT;
136 * SlapReply debugging enabled by USE_RS_ASSERT.
138 * Disabled by default, but compiled in (but still unused) when
139 * LDAP_TEST. #define USE_RS_ASSERT as nonzero to enable some
140 * assertions which check the SlapReply. USE_RS_ASSERT = 2 or higher
141 * check aggressively, currently some code fail these tests.
143 * Environment variable $NO_RS_ASSERT controls how USE_RS_ASSERT handles
144 * errors. > 0: ignore errors, 0: abort (the default), < 0: just warn.
146 * Wrap LDAP operation calls in macros SLAP_OP() & co from proto-slap.h
147 * to check the SlapReply. contrib/slapd-tools/wrap_slap_ops converts
148 * source code to use the macros.
150 #if defined(LDAP_TEST) || (defined(USE_RS_ASSERT) && (USE_RS_ASSERT))
152 int rs_suppress_assert = 0;
154 /* RS_ASSERT() helper function */
155 void rs_assert_(const char*file, unsigned line, const char*fn, const char*cond)
157 int no_assert = rs_suppress_assert, save_errno = errno;
160 if ( no_assert >= 0 ) {
161 if ( no_assert == 0 && (s = getenv( "NO_RS_ASSERT" )) && *s ) {
162 no_assert = rs_suppress_assert = atoi( s );
164 if ( no_assert > 0 ) {
170 #ifdef rs_assert_ /* proto-slap.h #defined away the fn parameter */
171 fprintf( stderr,"%s:%u: " "RS_ASSERT(%s) failed.\n", file,line,cond );
173 fprintf( stderr,"%s:%u: %s: RS_ASSERT(%s) failed.\n", file,line,fn,cond );
178 /* $NO_RS_ASSERT > 0: ignore rs_asserts, 0: abort, < 0: just warn */
179 if ( !no_assert /* from $NO_RS_ASSERT */ ) abort();
182 /* SlapReply is consistent */
184 (rs_assert_ok)( const SlapReply *rs )
186 const slap_mask_t flags = rs->sr_flags;
188 if ( flags & REP_ENTRY_MASK ) {
189 RS_ASSERT( !(flags & REP_ENTRY_MUSTRELEASE)
190 || !(flags & (REP_ENTRY_MASK ^ REP_ENTRY_MUSTRELEASE)) );
191 RS_ASSERT( rs->sr_entry != NULL );
192 RS_ASSERT( (1 << rs->sr_type) &
193 ((1 << REP_SEARCH) | (1 << REP_SEARCHREF) |
194 (1 << REP_RESULT) | (1 << REP_GLUE_RESULT)) );
196 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
197 if ( (flags & (REP_MATCHED_MASK | REP_REF_MASK | REP_CTRLS_MASK)) ) {
198 RS_ASSERT( !(flags & REP_MATCHED_MASK) || rs->sr_matched );
199 RS_ASSERT( !(flags & REP_CTRLS_MASK ) || rs->sr_ctrls );
200 /* Note: LDAP_REFERRAL + !sr_ref is OK, becomes LDAP_NO_SUCH_OBJECT */
202 #if (USE_RS_ASSERT) > 2
203 if ( rs->sr_err == LDAP_SUCCESS ) {
204 RS_ASSERT( rs->sr_text == NULL );
205 RS_ASSERT( rs->sr_matched == NULL );
211 /* Ready for calling a new backend operation */
213 (rs_assert_ready)( const SlapReply *rs )
215 RS_ASSERT( !rs->sr_entry );
216 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
217 RS_ASSERT( !rs->sr_text );
218 RS_ASSERT( !rs->sr_ref );
219 RS_ASSERT( !rs->sr_matched );
220 RS_ASSERT( !rs->sr_ctrls );
221 RS_ASSERT( !rs->sr_flags );
222 #if (USE_RS_ASSERT) > 2
223 RS_ASSERT( rs->sr_err == LDAP_SUCCESS );
226 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
230 /* Backend operation done */
232 (rs_assert_done)( const SlapReply *rs )
234 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
235 RS_ASSERT( !(rs->sr_flags & ~(REP_ENTRY_MODIFIABLE|REP_NO_OPERATIONALS)) );
238 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MUSTFLUSH) );
242 #endif /* LDAP_TEST || USE_RS_ASSERT */
244 /* Reset a used SlapReply whose contents has been flushed (freed/released) */
246 (rs_reinit)( SlapReply *rs, slap_reply_t type )
248 rs_reinit( rs, type ); /* proto-slap.h macro */
251 /* Obey and clear rs->sr_flags & REP_ENTRY_MASK. Clear sr_entry if freed. */
253 rs_flush_entry( Operation *op, SlapReply *rs, slap_overinst *on )
257 if ( (rs->sr_flags & REP_ENTRY_MUSTFLUSH) && rs->sr_entry != NULL ) {
258 if ( !(rs->sr_flags & REP_ENTRY_MUSTRELEASE) ) {
259 entry_free( rs->sr_entry );
260 } else if ( on != NULL ) {
261 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
263 be_entry_release_rw( op, rs->sr_entry, 0 );
268 rs->sr_flags &= ~REP_ENTRY_MASK;
271 /* Set rs->sr_entry after obeying and clearing sr_flags & REP_ENTRY_MASK. */
273 rs_replace_entry( Operation *op, SlapReply *rs, slap_overinst *on, Entry *e )
275 rs_flush_entry( op, rs, on );
280 * Ensure rs->sr_entry is modifiable, by duplicating it if necessary.
281 * Obey sr_flags. Set REP_ENTRY_<MODIFIABLE, and MUSTBEFREED if duplicated>.
282 * Return nonzero if rs->sr_entry was replaced.
285 rs_entry2modifiable( Operation *op, SlapReply *rs, slap_overinst *on )
287 if ( rs->sr_flags & REP_ENTRY_MODIFIABLE ) {
291 rs_replace_entry( op, rs, on, entry_dup( rs->sr_entry ));
292 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
296 static long send_ldap_ber(
300 Connection *conn = op->o_conn;
305 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
307 /* write only one pdu at a time - wait til it's our turn */
308 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
309 if (( op->o_abandon && !op->o_cancel ) || !connection_valid( conn ) ||
310 conn->c_writers < 0 ) {
311 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
317 while ( conn->c_writers > 0 && conn->c_writing ) {
318 ldap_pvt_thread_pool_idle( &connection_pool );
319 ldap_pvt_thread_cond_wait( &conn->c_write1_cv, &conn->c_write1_mutex );
320 ldap_pvt_thread_pool_unidle( &connection_pool );
323 /* connection was closed under us */
324 if ( conn->c_writers < 0 ) {
325 /* we're the last waiter, let the closer continue */
326 if ( conn->c_writers == -1 )
327 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
329 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
340 if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
348 * we got an error. if it's ewouldblock, we need to
349 * wait on the socket being writable. otherwise, figure
350 * it's a hard error and return.
353 Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
354 err, sock_errstr(err), 0 );
356 if ( err != EWOULDBLOCK && err != EAGAIN ) {
357 close_reason = "connection lost on write";
361 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
362 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
363 connection_closing( conn, close_reason );
364 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
368 /* wait for socket to be write-ready */
369 conn->c_writewaiter = 1;
370 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
371 ldap_pvt_thread_pool_idle( &connection_pool );
372 err = slapd_wait_writer( conn->c_sd );
373 conn->c_writewaiter = 0;
374 ldap_pvt_thread_pool_unidle( &connection_pool );
375 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
376 /* 0 is timeout, so we close it.
377 * -1 is an error, close it.
381 close_reason = "writetimeout";
383 close_reason = "connection lost on writewait";
387 if ( conn->c_writers < 0 ) {
394 if ( conn->c_writers < 0 ) {
396 if ( !conn->c_writers )
397 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
400 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
402 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
408 send_ldap_control( BerElement *ber, LDAPControl *c )
414 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
416 if( c->ldctl_iscritical ) {
417 rc = ber_printf( ber, "b",
418 (ber_int_t) c->ldctl_iscritical ) ;
419 if( rc == -1 ) return rc;
422 if( c->ldctl_value.bv_val != NULL ) {
423 rc = ber_printf( ber, "O", &c->ldctl_value );
424 if( rc == -1 ) return rc;
427 rc = ber_printf( ber, /*{*/"N}" );
428 if( rc == -1 ) return rc;
434 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
441 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
442 if( rc == -1 ) return rc;
444 for( ; *c != NULL; c++) {
445 rc = send_ldap_control( ber, *c );
446 if( rc == -1 ) return rc;
449 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
450 /* this is a hack to avoid having to modify op->s_ctrls */
451 if( o->o_sortedresults ) {
452 BerElementBuffer berbuf;
453 BerElement *sber = (BerElement *) &berbuf;
455 BER_BVZERO( &sorted.ldctl_value );
456 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
457 sorted.ldctl_iscritical = 0;
459 ber_init2( sber, NULL, LBER_USE_DER );
461 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
463 if( ber_flatten2( sber, &sorted.ldctl_value, 0 ) == -1 ) {
467 (void) ber_free_buf( sber );
469 rc = send_ldap_control( ber, &sorted );
470 if( rc == -1 ) return rc;
474 rc = ber_printf( ber, /*{*/"N}" );
480 * slap_response_play()
482 * plays the callback list; rationale: a callback can
483 * - remove itself from the list, by setting op->o_callback = NULL;
484 * malloc()'ed callbacks should free themselves from inside the
485 * sc_response() function.
486 * - replace itself with another (list of) callback(s), by setting
487 * op->o_callback = a new (list of) callback(s); in this case, it
488 * is the callback's responsibility to to append existing subsequent
489 * callbacks to the end of the list that is passed to the sc_response()
491 * - modify the list of subsequent callbacks by modifying the value
492 * of the sc_next field from inside the sc_response() function; this
493 * case does not require any handling from inside slap_response_play()
495 * To stop execution of the playlist, the sc_response() function must return
496 * a value different from SLAP_SC_CONTINUE.
498 * The same applies to slap_cleanup_play(); only, there is no means to stop
499 * execution of the playlist, since all cleanup functions must be called.
508 slap_callback *sc = op->o_callback, **scp;
510 rc = SLAP_CB_CONTINUE;
511 for ( scp = ≻ *scp; ) {
512 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
514 op->o_callback = *scp;
515 if ( op->o_callback->sc_response ) {
516 rc = op->o_callback->sc_response( op, rs );
517 if ( op->o_callback == NULL ) {
518 /* the callback has been removed;
523 } else if ( op->o_callback != *scp ) {
524 /* a new callback has been inserted
525 * in place of the existing one; repair the list */
526 *scp = op->o_callback;
529 if ( rc != SLAP_CB_CONTINUE ) break;
543 slap_callback *sc = op->o_callback, **scp;
545 for ( scp = ≻ *scp; ) {
546 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
548 op->o_callback = *scp;
549 if ( op->o_callback->sc_cleanup ) {
550 (void)op->o_callback->sc_cleanup( op, rs );
551 if ( op->o_callback == NULL ) {
552 /* the callback has been removed;
557 } else if ( op->o_callback != *scp ) {
558 /* a new callback has been inserted
559 * after the existing one; repair the list */
560 /* a new callback has been inserted
561 * in place of the existing one; repair the list */
562 *scp = op->o_callback;
565 /* don't care about the result; do all cleanup */
579 BerElementBuffer berbuf;
580 BerElement *ber = (BerElement *) &berbuf;
581 int rc = LDAP_SUCCESS;
584 /* op was actually aborted, bypass everything if client didn't Cancel */
585 if (( rs->sr_err == SLAPD_ABANDON ) && !op->o_cancel ) {
590 if ( op->o_callback ) {
591 rc = slap_response_play( op, rs );
592 if ( rc != SLAP_CB_CONTINUE ) {
597 /* op completed, connection aborted, bypass sending response */
598 if ( op->o_abandon && !op->o_cancel ) {
603 #ifdef LDAP_CONNECTIONLESS
604 if (op->o_conn && op->o_conn->c_is_udp)
609 ber_init_w_nullc( ber, LBER_USE_DER );
610 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
614 if ( rc == SLAPD_ABANDON && op->o_cancel )
617 Debug( LDAP_DEBUG_TRACE,
618 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
619 rs->sr_msgid, rs->sr_tag, rc );
622 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
623 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
627 #ifdef LDAP_CONNECTIONLESS
628 if (op->o_conn && op->o_conn->c_is_udp &&
629 op->o_protocol == LDAP_VERSION2 )
631 rc = ber_printf( ber, "t{ess" /*"}"*/,
633 rs->sr_matched == NULL ? "" : rs->sr_matched,
634 rs->sr_text == NULL ? "" : rs->sr_text );
637 if ( rs->sr_type == REP_INTERMEDIATE ) {
638 rc = ber_printf( ber, "{it{" /*"}}"*/,
639 rs->sr_msgid, rs->sr_tag );
642 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
643 rs->sr_msgid, rs->sr_tag, rc,
644 rs->sr_matched == NULL ? "" : rs->sr_matched,
645 rs->sr_text == NULL ? "" : rs->sr_text );
649 if ( rs->sr_ref != NULL ) {
650 assert( rs->sr_err == LDAP_REFERRAL );
651 rc = ber_printf( ber, "t{W}",
652 LDAP_TAG_REFERRAL, rs->sr_ref );
654 assert( rs->sr_err != LDAP_REFERRAL );
658 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
659 rc = ber_printf( ber, "tO",
660 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
664 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
666 if ( rs->sr_rspoid != NULL ) {
667 rc = ber_printf( ber, "ts",
668 rs->sr_type == REP_EXTENDED
669 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
672 if( rc != -1 && rs->sr_rspdata != NULL ) {
673 rc = ber_printf( ber, "tO",
674 rs->sr_type == REP_EXTENDED
675 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
681 rc = ber_printf( ber, /*"{"*/ "N}" );
685 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
689 rc = ber_printf( ber, /*"{"*/ "N}" );
692 #ifdef LDAP_CONNECTIONLESS
693 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
696 rc = ber_printf( ber, /*"{"*/ "N}" );
701 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
703 #ifdef LDAP_CONNECTIONLESS
704 if (!op->o_conn || op->o_conn->c_is_udp == 0)
713 bytes = send_ldap_ber( op, ber );
714 #ifdef LDAP_CONNECTIONLESS
715 if (!op->o_conn || op->o_conn->c_is_udp == 0)
722 Debug( LDAP_DEBUG_ANY,
723 "send_ldap_response: ber write failed\n",
729 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
730 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
731 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
732 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
735 /* Tell caller that we did this for real, as opposed to being
736 * overridden by a callback
738 rc = SLAP_CB_CONTINUE;
741 if ( op->o_callback ) {
742 (void)slap_cleanup_play( op, rs );
745 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
746 rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
747 if ( rs->sr_matched ) {
748 free( (char *)rs->sr_matched );
749 rs->sr_matched = NULL;
753 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
754 rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
756 ber_bvarray_free( rs->sr_ref );
761 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
762 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
763 if ( rs->sr_ctrls ) {
764 slap_free_ctrls( op, rs->sr_ctrls );
774 send_ldap_disconnect( Operation *op, SlapReply *rs )
776 #define LDAP_UNSOLICITED_ERROR(e) \
777 ( (e) == LDAP_PROTOCOL_ERROR \
778 || (e) == LDAP_STRONG_AUTH_REQUIRED \
779 || (e) == LDAP_UNAVAILABLE )
781 Debug( LDAP_DEBUG_TRACE,
782 "send_ldap_disconnect %d:%s\n",
783 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
784 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
786 /* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
787 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
788 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
790 rs->sr_type = REP_EXTENDED;
791 rs->sr_rspdata = NULL;
793 if ( op->o_protocol < LDAP_VERSION3 ) {
794 rs->sr_rspoid = NULL;
795 rs->sr_tag = slap_req2res( op->o_tag );
796 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
799 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
800 rs->sr_tag = LDAP_RES_EXTENDED;
801 rs->sr_msgid = LDAP_RES_UNSOLICITED;
804 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
805 Statslog( LDAP_DEBUG_STATS,
806 "%s DISCONNECT tag=%lu err=%d text=%s\n",
807 op->o_log_prefix, rs->sr_tag, rs->sr_err,
808 rs->sr_text ? rs->sr_text : "", 0 );
813 slap_send_ldap_result( Operation *op, SlapReply *rs )
816 const char *otext = rs->sr_text;
817 BerVarray oref = rs->sr_ref;
819 rs->sr_type = REP_RESULT;
821 /* Propagate Abandons so that cleanup callbacks can be processed */
822 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
825 Debug( LDAP_DEBUG_TRACE,
826 "send_ldap_result: %s p=%d\n",
827 op->o_log_prefix, op->o_protocol, 0 );
828 Debug( LDAP_DEBUG_ARGS,
829 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
830 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
831 rs->sr_text ? rs->sr_text : "" );
833 Debug( LDAP_DEBUG_ARGS,
834 "send_ldap_result: referral=\"%s\"\n",
835 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
838 assert( !LDAP_API_ERROR( rs->sr_err ) );
839 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
841 if ( rs->sr_err == LDAP_REFERRAL ) {
842 if( op->o_domain_scope ) rs->sr_ref = NULL;
844 if( rs->sr_ref == NULL ) {
845 rs->sr_err = LDAP_NO_SUCH_OBJECT;
846 } else if ( op->o_protocol < LDAP_VERSION3 ) {
847 rs->sr_err = LDAP_PARTIAL_RESULTS;
851 if ( op->o_protocol < LDAP_VERSION3 ) {
852 tmp = v2ref( rs->sr_ref, rs->sr_text );
858 rs->sr_tag = slap_req2res( op->o_tag );
859 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
861 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
862 if ( rs->sr_ref == NULL ) {
863 rs->sr_flags ^= REP_REF_MUSTBEFREED;
864 ber_bvarray_free( oref );
866 oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
869 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
870 if ( op->o_tag == LDAP_REQ_SEARCH ) {
871 Statslog( LDAP_DEBUG_STATS,
872 "%s SEARCH RESULT tag=%lu err=%d nentries=%d text=%s\n",
873 op->o_log_prefix, rs->sr_tag, rs->sr_err,
874 rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
876 Statslog( LDAP_DEBUG_STATS,
877 "%s RESULT tag=%lu err=%d text=%s\n",
878 op->o_log_prefix, rs->sr_tag, rs->sr_err,
879 rs->sr_text ? rs->sr_text : "", 0 );
883 if( tmp != NULL ) ch_free(tmp);
889 send_ldap_sasl( Operation *op, SlapReply *rs )
891 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
893 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
895 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
896 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
898 rs->sr_type = REP_SASL;
899 rs->sr_tag = slap_req2res( op->o_tag );
900 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
902 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
903 Statslog( LDAP_DEBUG_STATS,
904 "%s RESULT tag=%lu err=%d text=%s\n",
905 op->o_log_prefix, rs->sr_tag, rs->sr_err,
906 rs->sr_text ? rs->sr_text : "", 0 );
911 slap_send_ldap_extended( Operation *op, SlapReply *rs )
913 Debug( LDAP_DEBUG_TRACE,
914 "send_ldap_extended: err=%d oid=%s len=%ld\n",
916 rs->sr_rspoid ? rs->sr_rspoid : "",
917 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
919 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
920 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
922 rs->sr_type = REP_EXTENDED;
923 rs->sr_tag = slap_req2res( op->o_tag );
924 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
926 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
927 Statslog( LDAP_DEBUG_STATS,
928 "%s RESULT oid=%s err=%d text=%s\n",
929 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
930 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
935 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
937 Debug( LDAP_DEBUG_TRACE,
938 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
940 rs->sr_rspoid ? rs->sr_rspoid : "",
941 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
943 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
944 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
946 rs->sr_type = REP_INTERMEDIATE;
947 rs->sr_tag = LDAP_RES_INTERMEDIATE;
948 rs->sr_msgid = op->o_msgid;
949 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
950 Statslog( LDAP_DEBUG_STATS2,
951 "%s INTERM oid=%s\n",
953 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
957 #define set_ldap_error( rs, err, text ) do { \
958 (rs)->sr_err = err; (rs)->sr_text = text; } while(0)
963 * LDAP_SUCCESS entry sent
964 * LDAP_OTHER entry not sent (other)
965 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
966 * LDAP_UNAVAILABLE entry not sent (connection closed)
967 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
971 slap_send_search_entry( Operation *op, SlapReply *rs )
973 BerElementBuffer berbuf;
974 BerElement *ber = (BerElement *) &berbuf;
976 int i, j, rc = LDAP_UNAVAILABLE, bytes;
978 AccessControlState acl_state = ACL_STATE_INIT;
980 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
982 /* a_flags: array of flags telling if the i-th element will be
983 * returned or filtered out
984 * e_flags: array of a_flags
986 char **e_flags = NULL;
988 rs->sr_type = REP_SEARCH;
990 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
991 rc = LDAP_SIZELIMIT_EXCEEDED;
995 /* Every 64 entries, check for thread pool pause */
996 if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
997 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
1003 /* eventually will loop through generated operational attribute types
1004 * currently implemented types include:
1005 * entryDN, subschemaSubentry, and hasSubordinates */
1006 /* NOTE: moved before overlays callback circling because
1007 * they may modify entry and other stuff in rs */
1008 /* check for special all operational attributes ("+") type */
1009 /* FIXME: maybe we could set this flag at the operation level;
1010 * however, in principle the caller of send_search_entry() may
1011 * change the attribute list at each call */
1012 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
1014 rc = backend_operational( op, rs );
1019 if ( op->o_callback ) {
1020 rc = slap_response_play( op, rs );
1021 if ( rc != SLAP_CB_CONTINUE ) {
1026 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
1027 op->o_connid, rs->sr_entry->e_name.bv_val,
1028 op->ors_attrsonly ? " (attrsOnly)" : "" );
1030 attrsonly = op->ors_attrsonly;
1032 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
1033 Debug( LDAP_DEBUG_ACL,
1034 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
1035 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
1037 rc = LDAP_INSUFFICIENT_ACCESS;
1041 if ( op->o_res_ber ) {
1042 /* read back control or LDAP_CONNECTIONLESS */
1043 ber = op->o_res_ber;
1047 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1048 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1050 ber_init2( ber, &bv, LBER_USE_DER );
1051 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1054 #ifdef LDAP_CONNECTIONLESS
1055 if ( op->o_conn && op->o_conn->c_is_udp ) {
1056 /* CONNECTIONLESS */
1057 if ( op->o_protocol == LDAP_VERSION2 ) {
1058 rc = ber_printf(ber, "t{O{" /*}}*/,
1059 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1061 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1062 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1066 if ( op->o_res_ber ) {
1067 /* read back control */
1068 rc = ber_printf( ber, "t{O{" /*}}*/,
1069 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1071 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1072 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1076 Debug( LDAP_DEBUG_ANY,
1077 "send_search_entry: conn %lu ber_printf failed\n",
1078 op->o_connid, 0, 0 );
1080 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1081 set_ldap_error( rs, LDAP_OTHER, "encoding DN error" );
1086 /* check for special all user attributes ("*") type */
1087 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
1089 /* create an array of arrays of flags. Each flag corresponds
1090 * to particular value of attribute and equals 1 if value matches
1091 * to ValuesReturnFilter or 0 if not
1093 if ( op->o_vrFilter != NULL ) {
1097 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1098 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1101 size = i * sizeof(char *) + k;
1104 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
1105 if( e_flags == NULL ) {
1106 Debug( LDAP_DEBUG_ANY,
1107 "send_search_entry: conn %lu slap_sl_calloc failed\n",
1108 op->o_connid, 0, 0 );
1111 set_ldap_error( rs, LDAP_OTHER, "out of memory" );
1114 a_flags = (char *)(e_flags + i);
1115 memset( a_flags, 0, k );
1116 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
1117 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1118 e_flags[i] = a_flags;
1122 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
1124 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
1125 "conn %lu matched values filtering failed\n",
1126 op->o_connid, 0, 0 );
1127 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1128 set_ldap_error( rs, LDAP_OTHER,
1129 "matched values filtering error" );
1136 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
1137 AttributeDescription *desc = a->a_desc;
1140 if ( rs->sr_attrs == NULL ) {
1141 /* all user attrs request, skip operational attributes */
1142 if( is_at_operational( desc->ad_type ) ) {
1147 /* specific attrs requested */
1148 if ( is_at_operational( desc->ad_type ) ) {
1149 /* if not explicitly requested */
1150 if ( !ad_inlist( desc, rs->sr_attrs )) {
1151 /* if not all op attrs requested, skip */
1152 if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
1154 /* if DSA-specific and replicating, skip */
1155 if ( op->o_sync != SLAP_CONTROL_NONE &&
1156 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1160 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1167 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1168 ACL_READ, &acl_state ) )
1170 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1171 "conn %lu access to attribute %s not allowed\n",
1172 op->o_connid, desc->ad_cname.bv_val, 0 );
1176 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1177 Debug( LDAP_DEBUG_ANY,
1178 "send_search_entry: conn %lu ber_printf failed\n",
1179 op->o_connid, 0, 0 );
1181 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1182 set_ldap_error( rs, LDAP_OTHER,
1183 "encoding description error");
1191 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1192 if ( ! access_allowed( op, rs->sr_entry,
1193 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1195 Debug( LDAP_DEBUG_ACL,
1196 "send_search_entry: conn %lu "
1197 "access to attribute %s, value #%d not allowed\n",
1198 op->o_connid, desc->ad_cname.bv_val, i );
1203 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1210 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1211 Debug( LDAP_DEBUG_ANY,
1212 "send_search_entry: conn %lu ber_printf failed\n",
1213 op->o_connid, 0, 0 );
1215 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1216 set_ldap_error( rs, LDAP_OTHER,
1217 "encoding description error");
1222 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1223 Debug( LDAP_DEBUG_ANY,
1224 "send_search_entry: conn %lu "
1225 "ber_printf failed.\n", op->o_connid, 0, 0 );
1227 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1228 set_ldap_error( rs, LDAP_OTHER,
1229 "encoding values error" );
1236 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1237 Debug( LDAP_DEBUG_ANY,
1238 "send_search_entry: conn %lu ber_printf failed\n",
1239 op->o_connid, 0, 0 );
1241 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1242 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1248 /* NOTE: moved before overlays callback circling because
1249 * they may modify entry and other stuff in rs */
1250 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1254 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1255 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1258 size = i * sizeof(char *) + k;
1260 char *a_flags, **tmp;
1263 * Reuse previous memory - we likely need less space
1264 * for operational attributes
1266 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1268 if ( tmp == NULL ) {
1269 Debug( LDAP_DEBUG_ANY,
1270 "send_search_entry: conn %lu "
1271 "not enough memory "
1272 "for matched values filtering\n",
1273 op->o_connid, 0, 0 );
1274 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1275 set_ldap_error( rs, LDAP_OTHER,
1276 "not enough memory for matched values filtering" );
1280 a_flags = (char *)(e_flags + i);
1281 memset( a_flags, 0, k );
1282 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1283 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1284 e_flags[i] = a_flags;
1287 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1290 Debug( LDAP_DEBUG_ANY,
1291 "send_search_entry: conn %lu "
1292 "matched values filtering failed\n",
1293 op->o_connid, 0, 0);
1294 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1295 set_ldap_error( rs, LDAP_OTHER,
1296 "matched values filtering error" );
1303 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1304 AttributeDescription *desc = a->a_desc;
1306 if ( rs->sr_attrs == NULL ) {
1307 /* all user attrs request, skip operational attributes */
1308 if( is_at_operational( desc->ad_type ) ) {
1313 /* specific attrs requested */
1314 if( is_at_operational( desc->ad_type ) ) {
1315 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1316 !ad_inlist( desc, rs->sr_attrs ) )
1320 /* if DSA-specific and replicating, skip */
1321 if ( op->o_sync != SLAP_CONTROL_NONE &&
1322 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1325 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1331 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1332 ACL_READ, &acl_state ) )
1334 Debug( LDAP_DEBUG_ACL,
1335 "send_search_entry: conn %lu "
1336 "access to attribute %s not allowed\n",
1337 op->o_connid, desc->ad_cname.bv_val, 0 );
1342 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1344 Debug( LDAP_DEBUG_ANY,
1345 "send_search_entry: conn %lu "
1346 "ber_printf failed\n", op->o_connid, 0, 0 );
1348 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1349 set_ldap_error( rs, LDAP_OTHER,
1350 "encoding description error" );
1355 if ( ! attrsonly ) {
1356 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1357 if ( ! access_allowed( op, rs->sr_entry,
1358 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1360 Debug( LDAP_DEBUG_ACL,
1361 "send_search_entry: conn %lu "
1362 "access to %s, value %d not allowed\n",
1363 op->o_connid, desc->ad_cname.bv_val, i );
1368 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1372 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1373 Debug( LDAP_DEBUG_ANY,
1374 "send_search_entry: conn %lu ber_printf failed\n",
1375 op->o_connid, 0, 0 );
1377 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1378 set_ldap_error( rs, LDAP_OTHER,
1379 "encoding values error" );
1386 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1387 Debug( LDAP_DEBUG_ANY,
1388 "send_search_entry: conn %lu ber_printf failed\n",
1389 op->o_connid, 0, 0 );
1391 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1392 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1400 slap_sl_free( e_flags, op->o_tmpmemctx );
1404 rc = ber_printf( ber, /*{{*/ "}N}" );
1407 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1411 #ifdef LDAP_CONNECTIONLESS
1412 if( op->o_conn && op->o_conn->c_is_udp ) {
1413 if ( op->o_protocol != LDAP_VERSION2 ) {
1414 rc = ber_printf( ber, /*{*/ "N}" );
1418 if ( op->o_res_ber == NULL ) {
1419 rc = ber_printf( ber, /*{*/ "N}" );
1424 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1426 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1427 set_ldap_error( rs, LDAP_OTHER, "encode entry end error" );
1432 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1433 op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1435 rs_flush_entry( op, rs, NULL );
1437 if ( op->o_res_ber == NULL ) {
1438 bytes = send_ldap_ber( op, ber );
1439 ber_free_buf( ber );
1442 Debug( LDAP_DEBUG_ANY,
1443 "send_search_entry: conn %lu ber write failed.\n",
1444 op->o_connid, 0, 0 );
1446 rc = LDAP_UNAVAILABLE;
1451 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1452 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1453 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1454 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1455 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1458 Debug( LDAP_DEBUG_TRACE,
1459 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1464 if ( op->o_callback ) {
1465 (void)slap_cleanup_play( op, rs );
1469 slap_sl_free( e_flags, op->o_tmpmemctx );
1472 /* FIXME: Can break if rs now contains an extended response */
1473 if ( rs->sr_operational_attrs ) {
1474 attrs_free( rs->sr_operational_attrs );
1475 rs->sr_operational_attrs = NULL;
1477 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1479 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1480 rs_flush_entry( op, rs, NULL );
1482 RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
1485 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1486 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1487 if ( rs->sr_ctrls ) {
1488 slap_free_ctrls( op, rs->sr_ctrls );
1489 rs->sr_ctrls = NULL;
1497 slap_send_search_reference( Operation *op, SlapReply *rs )
1499 BerElementBuffer berbuf;
1500 BerElement *ber = (BerElement *) &berbuf;
1503 char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1505 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1506 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1508 rs->sr_type = REP_SEARCHREF;
1509 if ( op->o_callback ) {
1510 rc = slap_response_play( op, rs );
1511 if ( rc != SLAP_CB_CONTINUE ) {
1516 Debug( LDAP_DEBUG_TRACE,
1517 "=> send_search_reference: dn=\"%s\"\n",
1520 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1521 ad_entry, NULL, ACL_READ, NULL ) )
1523 Debug( LDAP_DEBUG_ACL,
1524 "send_search_reference: access to entry not allowed\n",
1530 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1531 ad_ref, NULL, ACL_READ, NULL ) )
1533 Debug( LDAP_DEBUG_ACL,
1534 "send_search_reference: access "
1535 "to reference not allowed\n",
1541 if( op->o_domain_scope ) {
1542 Debug( LDAP_DEBUG_ANY,
1543 "send_search_reference: domainScope control in (%s)\n",
1549 if( rs->sr_ref == NULL ) {
1550 Debug( LDAP_DEBUG_ANY,
1551 "send_search_reference: null ref in (%s)\n",
1557 if( op->o_protocol < LDAP_VERSION3 ) {
1559 /* save the references for the result */
1560 if( rs->sr_ref[0].bv_val != NULL ) {
1561 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1567 #ifdef LDAP_CONNECTIONLESS
1568 if( op->o_conn && op->o_conn->c_is_udp ) {
1569 ber = op->o_res_ber;
1573 ber_init_w_nullc( ber, LBER_USE_DER );
1574 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1577 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1578 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1581 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1585 rc = ber_printf( ber, /*"{"*/ "N}" );
1589 Debug( LDAP_DEBUG_ANY,
1590 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1592 #ifdef LDAP_CONNECTIONLESS
1593 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1595 ber_free_buf( ber );
1596 set_ldap_error( rs, LDAP_OTHER, "encode DN error" );
1601 rs_flush_entry( op, rs, NULL );
1603 #ifdef LDAP_CONNECTIONLESS
1604 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1606 bytes = send_ldap_ber( op, ber );
1607 ber_free_buf( ber );
1610 rc = LDAP_UNAVAILABLE;
1612 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1613 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1614 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1615 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1616 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1618 #ifdef LDAP_CONNECTIONLESS
1621 if ( rs->sr_ref != NULL ) {
1624 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1625 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1626 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1631 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1632 op->o_log_prefix, 0, 0, 0, 0 );
1635 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1639 rs_flush_entry( op, rs, NULL );
1642 if ( op->o_callback ) {
1643 (void)slap_cleanup_play( op, rs );
1646 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1647 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1648 if ( rs->sr_ctrls ) {
1649 slap_free_ctrls( op, rs->sr_ctrls );
1650 rs->sr_ctrls = NULL;
1667 *code = LDAP_SUCCESS;
1671 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1672 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1679 while ( (s = strchr( s, '\n' )) != NULL ) {
1684 if ( (c = strchr( s, ':' )) != NULL ) {
1688 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1693 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1699 while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1700 if ( c[ 0 ] == '\0' ) {
1701 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1707 retcode = strtol( c, &next, 10 );
1708 if ( next == NULL || next == c ) {
1709 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1715 while ( isspace( (unsigned char) next[ 0 ] ) && next[ 0 ] != '\n' )
1717 if ( next[ 0 ] != '\0' && next[ 0 ] != '\n' ) {
1718 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1724 /* FIXME: what if it's larger than max int? */
1725 *code = (int)retcode;
1727 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1731 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1736 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1746 int slap_read_controls(
1750 const struct berval *oid,
1751 LDAPControl **ctrl )
1755 BerElementBuffer berbuf;
1756 BerElement *ber = (BerElement *) &berbuf;
1760 Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1761 op->o_log_prefix, oid->bv_val, e->e_dn );
1764 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1765 op->o_preread_attrs : op->o_postread_attrs;
1767 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1768 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1770 ber_init2( ber, &bv, LBER_USE_DER );
1771 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1773 /* create new operation */
1775 /* FIXME: o_bd needed for ACL */
1776 myop.o_bd = op->o_bd;
1777 myop.o_res_ber = ber;
1778 myop.o_callback = NULL;
1779 myop.ors_slimit = 1;
1780 myop.ors_attrsonly = 0;
1782 rc = slap_send_search_entry( &myop, rs );
1785 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1787 if( rc == -1 ) return LDAP_OTHER;
1789 c.ldctl_oid = oid->bv_val;
1790 c.ldctl_iscritical = 0;
1792 if ( *ctrl == NULL ) {
1794 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1796 /* retry: free previous try */
1797 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1801 return LDAP_SUCCESS;
1804 /* Map API errors to protocol errors... */
1806 slap_map_api2result( SlapReply *rs )
1808 switch(rs->sr_err) {
1809 case LDAP_SERVER_DOWN:
1810 return LDAP_UNAVAILABLE;
1811 case LDAP_LOCAL_ERROR:
1813 case LDAP_ENCODING_ERROR:
1814 case LDAP_DECODING_ERROR:
1815 return LDAP_PROTOCOL_ERROR;
1817 return LDAP_UNAVAILABLE;
1818 case LDAP_AUTH_UNKNOWN:
1819 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1820 case LDAP_FILTER_ERROR:
1821 rs->sr_text = "Filter error";
1823 case LDAP_USER_CANCELLED:
1824 rs->sr_text = "User cancelled";
1826 case LDAP_PARAM_ERROR:
1827 return LDAP_PROTOCOL_ERROR;
1828 case LDAP_NO_MEMORY:
1830 case LDAP_CONNECT_ERROR:
1831 return LDAP_UNAVAILABLE;
1832 case LDAP_NOT_SUPPORTED:
1833 return LDAP_UNWILLING_TO_PERFORM;
1834 case LDAP_CONTROL_NOT_FOUND:
1835 return LDAP_PROTOCOL_ERROR;
1836 case LDAP_NO_RESULTS_RETURNED:
1837 return LDAP_NO_SUCH_OBJECT;
1838 case LDAP_MORE_RESULTS_TO_RETURN:
1839 rs->sr_text = "More results to return";
1841 case LDAP_CLIENT_LOOP:
1842 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1843 return LDAP_LOOP_DETECT;
1845 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1852 slap_attr_flags( AttributeName *an )
1854 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1857 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1860 flags |= an_find( an, slap_bv_all_operational_attrs )
1861 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1862 flags |= an_find( an, slap_bv_all_user_attrs )
1863 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;