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 /* Check for any callbacks that want to be informed about being blocked
297 * on output. These callbacks are expected to leave the callback list
298 * unmodified. Their result is ignored.
304 slap_callback *sc = op->o_callback;
306 for ( ; sc; sc = sc->sc_next ) {
307 if ( sc->sc_writewait )
308 sc->sc_writewait( op, sc );
312 static long send_ldap_ber(
316 Connection *conn = op->o_conn;
321 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
323 /* write only one pdu at a time - wait til it's our turn */
324 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
325 if (( op->o_abandon && !op->o_cancel ) || !connection_valid( conn ) ||
326 conn->c_writers < 0 ) {
327 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
333 while ( conn->c_writers > 0 && conn->c_writing ) {
334 ldap_pvt_thread_pool_idle( &connection_pool );
335 ldap_pvt_thread_cond_wait( &conn->c_write1_cv, &conn->c_write1_mutex );
336 ldap_pvt_thread_pool_unidle( &connection_pool );
339 /* connection was closed under us */
340 if ( conn->c_writers < 0 ) {
341 /* we're the last waiter, let the closer continue */
342 if ( conn->c_writers == -1 )
343 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
345 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
356 if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
364 * we got an error. if it's ewouldblock, we need to
365 * wait on the socket being writable. otherwise, figure
366 * it's a hard error and return.
369 Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
370 err, sock_errstr(err), 0 );
372 if ( err != EWOULDBLOCK && err != EAGAIN ) {
373 close_reason = "connection lost on write";
377 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
378 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
379 connection_closing( conn, close_reason );
380 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
384 /* wait for socket to be write-ready */
385 conn->c_writewaiter = 1;
386 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
387 ldap_pvt_thread_pool_idle( &connection_pool );
388 slap_writewait_play( op );
389 err = slapd_wait_writer( conn->c_sd );
390 conn->c_writewaiter = 0;
391 ldap_pvt_thread_pool_unidle( &connection_pool );
392 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
393 /* 0 is timeout, so we close it.
394 * -1 is an error, close it.
398 close_reason = "writetimeout";
400 close_reason = "connection lost on writewait";
404 if ( conn->c_writers < 0 ) {
411 if ( conn->c_writers < 0 ) {
413 if ( !conn->c_writers )
414 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
417 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
419 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
425 send_ldap_control( BerElement *ber, LDAPControl *c )
431 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
433 if( c->ldctl_iscritical ) {
434 rc = ber_printf( ber, "b",
435 (ber_int_t) c->ldctl_iscritical ) ;
436 if( rc == -1 ) return rc;
439 if( c->ldctl_value.bv_val != NULL ) {
440 rc = ber_printf( ber, "O", &c->ldctl_value );
441 if( rc == -1 ) return rc;
444 rc = ber_printf( ber, /*{*/"N}" );
445 if( rc == -1 ) return rc;
451 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
458 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
459 if( rc == -1 ) return rc;
461 for( ; *c != NULL; c++) {
462 rc = send_ldap_control( ber, *c );
463 if( rc == -1 ) return rc;
466 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
467 /* this is a hack to avoid having to modify op->s_ctrls */
468 if( o->o_sortedresults ) {
469 BerElementBuffer berbuf;
470 BerElement *sber = (BerElement *) &berbuf;
472 BER_BVZERO( &sorted.ldctl_value );
473 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
474 sorted.ldctl_iscritical = 0;
476 ber_init2( sber, NULL, LBER_USE_DER );
478 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
480 if( ber_flatten2( sber, &sorted.ldctl_value, 0 ) == -1 ) {
484 (void) ber_free_buf( sber );
486 rc = send_ldap_control( ber, &sorted );
487 if( rc == -1 ) return rc;
491 rc = ber_printf( ber, /*{*/"N}" );
497 * slap_response_play()
499 * plays the callback list; rationale: a callback can
500 * - remove itself from the list, by setting op->o_callback = NULL;
501 * malloc()'ed callbacks should free themselves from inside the
502 * sc_response() function.
503 * - replace itself with another (list of) callback(s), by setting
504 * op->o_callback = a new (list of) callback(s); in this case, it
505 * is the callback's responsibility to to append existing subsequent
506 * callbacks to the end of the list that is passed to the sc_response()
508 * - modify the list of subsequent callbacks by modifying the value
509 * of the sc_next field from inside the sc_response() function; this
510 * case does not require any handling from inside slap_response_play()
512 * To stop execution of the playlist, the sc_response() function must return
513 * a value different from SLAP_SC_CONTINUE.
515 * The same applies to slap_cleanup_play(); only, there is no means to stop
516 * execution of the playlist, since all cleanup functions must be called.
525 slap_callback *sc = op->o_callback, **scp;
527 rc = SLAP_CB_CONTINUE;
528 for ( scp = ≻ *scp; ) {
529 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
531 op->o_callback = *scp;
532 if ( op->o_callback->sc_response ) {
533 rc = op->o_callback->sc_response( op, rs );
534 if ( op->o_callback == NULL ) {
535 /* the callback has been removed;
540 } else if ( op->o_callback != *scp ) {
541 /* a new callback has been inserted
542 * in place of the existing one; repair the list */
543 *scp = op->o_callback;
546 if ( rc != SLAP_CB_CONTINUE ) break;
560 slap_callback *sc = op->o_callback, **scp;
562 for ( scp = ≻ *scp; ) {
563 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
565 op->o_callback = *scp;
566 if ( op->o_callback->sc_cleanup ) {
567 (void)op->o_callback->sc_cleanup( op, rs );
568 if ( op->o_callback == NULL ) {
569 /* the callback has been removed;
574 } else if ( op->o_callback != *scp ) {
575 /* a new callback has been inserted
576 * after the existing one; repair the list */
577 /* a new callback has been inserted
578 * in place of the existing one; repair the list */
579 *scp = op->o_callback;
582 /* don't care about the result; do all cleanup */
596 BerElementBuffer berbuf;
597 BerElement *ber = (BerElement *) &berbuf;
598 int rc = LDAP_SUCCESS;
601 /* op was actually aborted, bypass everything if client didn't Cancel */
602 if (( rs->sr_err == SLAPD_ABANDON ) && !op->o_cancel ) {
607 if ( op->o_callback ) {
608 rc = slap_response_play( op, rs );
609 if ( rc != SLAP_CB_CONTINUE ) {
614 /* op completed, connection aborted, bypass sending response */
615 if ( op->o_abandon && !op->o_cancel ) {
620 #ifdef LDAP_CONNECTIONLESS
621 if (op->o_conn && op->o_conn->c_is_udp)
626 ber_init_w_nullc( ber, LBER_USE_DER );
627 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
631 if ( rc == SLAPD_ABANDON && op->o_cancel )
634 Debug( LDAP_DEBUG_TRACE,
635 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
636 rs->sr_msgid, rs->sr_tag, rc );
639 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
640 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
644 #ifdef LDAP_CONNECTIONLESS
645 if (op->o_conn && op->o_conn->c_is_udp &&
646 op->o_protocol == LDAP_VERSION2 )
648 rc = ber_printf( ber, "t{ess" /*"}"*/,
650 rs->sr_matched == NULL ? "" : rs->sr_matched,
651 rs->sr_text == NULL ? "" : rs->sr_text );
654 if ( rs->sr_type == REP_INTERMEDIATE ) {
655 rc = ber_printf( ber, "{it{" /*"}}"*/,
656 rs->sr_msgid, rs->sr_tag );
659 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
660 rs->sr_msgid, rs->sr_tag, rc,
661 rs->sr_matched == NULL ? "" : rs->sr_matched,
662 rs->sr_text == NULL ? "" : rs->sr_text );
666 if ( rs->sr_ref != NULL ) {
667 assert( rs->sr_err == LDAP_REFERRAL );
668 rc = ber_printf( ber, "t{W}",
669 LDAP_TAG_REFERRAL, rs->sr_ref );
671 assert( rs->sr_err != LDAP_REFERRAL );
675 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
676 rc = ber_printf( ber, "tO",
677 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
681 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
683 if ( rs->sr_rspoid != NULL ) {
684 rc = ber_printf( ber, "ts",
685 rs->sr_type == REP_EXTENDED
686 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
689 if( rc != -1 && rs->sr_rspdata != NULL ) {
690 rc = ber_printf( ber, "tO",
691 rs->sr_type == REP_EXTENDED
692 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
698 rc = ber_printf( ber, /*"{"*/ "N}" );
702 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
706 rc = ber_printf( ber, /*"{"*/ "N}" );
709 #ifdef LDAP_CONNECTIONLESS
710 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
713 rc = ber_printf( ber, /*"{"*/ "N}" );
718 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
720 #ifdef LDAP_CONNECTIONLESS
721 if (!op->o_conn || op->o_conn->c_is_udp == 0)
730 bytes = send_ldap_ber( op, ber );
731 #ifdef LDAP_CONNECTIONLESS
732 if (!op->o_conn || op->o_conn->c_is_udp == 0)
739 Debug( LDAP_DEBUG_ANY,
740 "send_ldap_response: ber write failed\n",
746 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
747 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
748 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
749 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
752 /* Tell caller that we did this for real, as opposed to being
753 * overridden by a callback
755 rc = SLAP_CB_CONTINUE;
758 if ( op->o_callback ) {
759 (void)slap_cleanup_play( op, rs );
762 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
763 rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
764 if ( rs->sr_matched ) {
765 free( (char *)rs->sr_matched );
766 rs->sr_matched = NULL;
770 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
771 rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
773 ber_bvarray_free( rs->sr_ref );
778 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
779 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
780 if ( rs->sr_ctrls ) {
781 slap_free_ctrls( op, rs->sr_ctrls );
791 send_ldap_disconnect( Operation *op, SlapReply *rs )
793 #define LDAP_UNSOLICITED_ERROR(e) \
794 ( (e) == LDAP_PROTOCOL_ERROR \
795 || (e) == LDAP_STRONG_AUTH_REQUIRED \
796 || (e) == LDAP_UNAVAILABLE )
798 Debug( LDAP_DEBUG_TRACE,
799 "send_ldap_disconnect %d:%s\n",
800 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
801 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
803 /* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
804 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
805 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
807 rs->sr_type = REP_EXTENDED;
808 rs->sr_rspdata = NULL;
810 if ( op->o_protocol < LDAP_VERSION3 ) {
811 rs->sr_rspoid = NULL;
812 rs->sr_tag = slap_req2res( op->o_tag );
813 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
816 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
817 rs->sr_tag = LDAP_RES_EXTENDED;
818 rs->sr_msgid = LDAP_RES_UNSOLICITED;
821 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
822 Statslog( LDAP_DEBUG_STATS,
823 "%s DISCONNECT tag=%lu err=%d text=%s\n",
824 op->o_log_prefix, rs->sr_tag, rs->sr_err,
825 rs->sr_text ? rs->sr_text : "", 0 );
830 slap_send_ldap_result( Operation *op, SlapReply *rs )
833 const char *otext = rs->sr_text;
834 BerVarray oref = rs->sr_ref;
836 rs->sr_type = REP_RESULT;
838 /* Propagate Abandons so that cleanup callbacks can be processed */
839 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
842 Debug( LDAP_DEBUG_TRACE,
843 "send_ldap_result: %s p=%d\n",
844 op->o_log_prefix, op->o_protocol, 0 );
845 Debug( LDAP_DEBUG_ARGS,
846 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
847 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
848 rs->sr_text ? rs->sr_text : "" );
850 Debug( LDAP_DEBUG_ARGS,
851 "send_ldap_result: referral=\"%s\"\n",
852 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
855 assert( !LDAP_API_ERROR( rs->sr_err ) );
856 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
858 if ( rs->sr_err == LDAP_REFERRAL ) {
859 if( op->o_domain_scope ) rs->sr_ref = NULL;
861 if( rs->sr_ref == NULL ) {
862 rs->sr_err = LDAP_NO_SUCH_OBJECT;
863 } else if ( op->o_protocol < LDAP_VERSION3 ) {
864 rs->sr_err = LDAP_PARTIAL_RESULTS;
868 if ( op->o_protocol < LDAP_VERSION3 ) {
869 tmp = v2ref( rs->sr_ref, rs->sr_text );
875 rs->sr_tag = slap_req2res( op->o_tag );
876 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
878 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
879 if ( rs->sr_ref == NULL ) {
880 rs->sr_flags ^= REP_REF_MUSTBEFREED;
881 ber_bvarray_free( oref );
883 oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
886 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
887 if ( op->o_tag == LDAP_REQ_SEARCH ) {
888 Statslog( LDAP_DEBUG_STATS,
889 "%s SEARCH RESULT tag=%lu err=%d nentries=%d text=%s\n",
890 op->o_log_prefix, rs->sr_tag, rs->sr_err,
891 rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
893 Statslog( LDAP_DEBUG_STATS,
894 "%s RESULT tag=%lu err=%d text=%s\n",
895 op->o_log_prefix, rs->sr_tag, rs->sr_err,
896 rs->sr_text ? rs->sr_text : "", 0 );
900 if( tmp != NULL ) ch_free(tmp);
906 send_ldap_sasl( Operation *op, SlapReply *rs )
908 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
910 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
912 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
913 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
915 rs->sr_type = REP_SASL;
916 rs->sr_tag = slap_req2res( op->o_tag );
917 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
919 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
920 Statslog( LDAP_DEBUG_STATS,
921 "%s RESULT tag=%lu err=%d text=%s\n",
922 op->o_log_prefix, rs->sr_tag, rs->sr_err,
923 rs->sr_text ? rs->sr_text : "", 0 );
928 slap_send_ldap_extended( Operation *op, SlapReply *rs )
930 Debug( LDAP_DEBUG_TRACE,
931 "send_ldap_extended: err=%d oid=%s len=%ld\n",
933 rs->sr_rspoid ? rs->sr_rspoid : "",
934 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
936 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
937 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
939 rs->sr_type = REP_EXTENDED;
940 rs->sr_tag = slap_req2res( op->o_tag );
941 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
943 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
944 Statslog( LDAP_DEBUG_STATS,
945 "%s RESULT oid=%s err=%d text=%s\n",
946 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
947 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
952 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
954 Debug( LDAP_DEBUG_TRACE,
955 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
957 rs->sr_rspoid ? rs->sr_rspoid : "",
958 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
960 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
961 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
963 rs->sr_type = REP_INTERMEDIATE;
964 rs->sr_tag = LDAP_RES_INTERMEDIATE;
965 rs->sr_msgid = op->o_msgid;
966 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
967 Statslog( LDAP_DEBUG_STATS2,
968 "%s INTERM oid=%s\n",
970 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
974 #define set_ldap_error( rs, err, text ) do { \
975 (rs)->sr_err = err; (rs)->sr_text = text; } while(0)
980 * LDAP_SUCCESS entry sent
981 * LDAP_OTHER entry not sent (other)
982 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
983 * LDAP_UNAVAILABLE entry not sent (connection closed)
984 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
988 slap_send_search_entry( Operation *op, SlapReply *rs )
990 BerElementBuffer berbuf;
991 BerElement *ber = (BerElement *) &berbuf;
993 int i, j, rc = LDAP_UNAVAILABLE, bytes;
995 AccessControlState acl_state = ACL_STATE_INIT;
997 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
999 /* a_flags: array of flags telling if the i-th element will be
1000 * returned or filtered out
1001 * e_flags: array of a_flags
1003 char **e_flags = NULL;
1005 rs->sr_type = REP_SEARCH;
1007 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
1008 rc = LDAP_SIZELIMIT_EXCEEDED;
1012 /* Every 64 entries, check for thread pool pause */
1013 if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
1014 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
1020 /* eventually will loop through generated operational attribute types
1021 * currently implemented types include:
1022 * entryDN, subschemaSubentry, and hasSubordinates */
1023 /* NOTE: moved before overlays callback circling because
1024 * they may modify entry and other stuff in rs */
1025 /* check for special all operational attributes ("+") type */
1026 /* FIXME: maybe we could set this flag at the operation level;
1027 * however, in principle the caller of send_search_entry() may
1028 * change the attribute list at each call */
1029 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
1031 rc = backend_operational( op, rs );
1036 if ( op->o_callback ) {
1037 rc = slap_response_play( op, rs );
1038 if ( rc != SLAP_CB_CONTINUE ) {
1043 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
1044 op->o_connid, rs->sr_entry->e_name.bv_val,
1045 op->ors_attrsonly ? " (attrsOnly)" : "" );
1047 attrsonly = op->ors_attrsonly;
1049 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
1050 Debug( LDAP_DEBUG_ACL,
1051 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
1052 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
1054 rc = LDAP_INSUFFICIENT_ACCESS;
1058 if ( op->o_res_ber ) {
1059 /* read back control or LDAP_CONNECTIONLESS */
1060 ber = op->o_res_ber;
1064 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1065 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1067 ber_init2( ber, &bv, LBER_USE_DER );
1068 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1071 #ifdef LDAP_CONNECTIONLESS
1072 if ( op->o_conn && op->o_conn->c_is_udp ) {
1073 /* CONNECTIONLESS */
1074 if ( op->o_protocol == LDAP_VERSION2 ) {
1075 rc = ber_printf(ber, "t{O{" /*}}*/,
1076 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1078 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1079 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1083 if ( op->o_res_ber ) {
1084 /* read back control */
1085 rc = ber_printf( ber, "t{O{" /*}}*/,
1086 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1088 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1089 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1093 Debug( LDAP_DEBUG_ANY,
1094 "send_search_entry: conn %lu ber_printf failed\n",
1095 op->o_connid, 0, 0 );
1097 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1098 set_ldap_error( rs, LDAP_OTHER, "encoding DN error" );
1103 /* check for special all user attributes ("*") type */
1104 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
1106 /* create an array of arrays of flags. Each flag corresponds
1107 * to particular value of attribute and equals 1 if value matches
1108 * to ValuesReturnFilter or 0 if not
1110 if ( op->o_vrFilter != NULL ) {
1114 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1115 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1118 size = i * sizeof(char *) + k;
1121 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
1122 if( e_flags == NULL ) {
1123 Debug( LDAP_DEBUG_ANY,
1124 "send_search_entry: conn %lu slap_sl_calloc failed\n",
1125 op->o_connid, 0, 0 );
1128 set_ldap_error( rs, LDAP_OTHER, "out of memory" );
1131 a_flags = (char *)(e_flags + i);
1132 memset( a_flags, 0, k );
1133 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
1134 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1135 e_flags[i] = a_flags;
1139 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
1141 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
1142 "conn %lu matched values filtering failed\n",
1143 op->o_connid, 0, 0 );
1144 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1145 set_ldap_error( rs, LDAP_OTHER,
1146 "matched values filtering error" );
1153 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
1154 AttributeDescription *desc = a->a_desc;
1157 if ( rs->sr_attrs == NULL ) {
1158 /* all user attrs request, skip operational attributes */
1159 if( is_at_operational( desc->ad_type ) ) {
1164 /* specific attrs requested */
1165 if ( is_at_operational( desc->ad_type ) ) {
1166 /* if not explicitly requested */
1167 if ( !ad_inlist( desc, rs->sr_attrs )) {
1168 /* if not all op attrs requested, skip */
1169 if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
1171 /* if DSA-specific and replicating, skip */
1172 if ( op->o_sync != SLAP_CONTROL_NONE &&
1173 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1177 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1184 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1185 ACL_READ, &acl_state ) )
1187 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1188 "conn %lu access to attribute %s not allowed\n",
1189 op->o_connid, desc->ad_cname.bv_val, 0 );
1193 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1194 Debug( LDAP_DEBUG_ANY,
1195 "send_search_entry: conn %lu ber_printf failed\n",
1196 op->o_connid, 0, 0 );
1198 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1199 set_ldap_error( rs, LDAP_OTHER,
1200 "encoding description error");
1208 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1209 if ( ! access_allowed( op, rs->sr_entry,
1210 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1212 Debug( LDAP_DEBUG_ACL,
1213 "send_search_entry: conn %lu "
1214 "access to attribute %s, value #%d not allowed\n",
1215 op->o_connid, desc->ad_cname.bv_val, i );
1220 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1227 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1228 Debug( LDAP_DEBUG_ANY,
1229 "send_search_entry: conn %lu ber_printf failed\n",
1230 op->o_connid, 0, 0 );
1232 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1233 set_ldap_error( rs, LDAP_OTHER,
1234 "encoding description error");
1239 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1240 Debug( LDAP_DEBUG_ANY,
1241 "send_search_entry: conn %lu "
1242 "ber_printf failed.\n", op->o_connid, 0, 0 );
1244 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1245 set_ldap_error( rs, LDAP_OTHER,
1246 "encoding values error" );
1253 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1254 Debug( LDAP_DEBUG_ANY,
1255 "send_search_entry: conn %lu ber_printf failed\n",
1256 op->o_connid, 0, 0 );
1258 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1259 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1265 /* NOTE: moved before overlays callback circling because
1266 * they may modify entry and other stuff in rs */
1267 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1271 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1272 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1275 size = i * sizeof(char *) + k;
1277 char *a_flags, **tmp;
1280 * Reuse previous memory - we likely need less space
1281 * for operational attributes
1283 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1285 if ( tmp == NULL ) {
1286 Debug( LDAP_DEBUG_ANY,
1287 "send_search_entry: conn %lu "
1288 "not enough memory "
1289 "for matched values filtering\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 "not enough memory for matched values filtering" );
1297 a_flags = (char *)(e_flags + i);
1298 memset( a_flags, 0, k );
1299 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1300 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1301 e_flags[i] = a_flags;
1304 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1307 Debug( LDAP_DEBUG_ANY,
1308 "send_search_entry: conn %lu "
1309 "matched values filtering failed\n",
1310 op->o_connid, 0, 0);
1311 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1312 set_ldap_error( rs, LDAP_OTHER,
1313 "matched values filtering error" );
1320 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1321 AttributeDescription *desc = a->a_desc;
1323 if ( rs->sr_attrs == NULL ) {
1324 /* all user attrs request, skip operational attributes */
1325 if( is_at_operational( desc->ad_type ) ) {
1330 /* specific attrs requested */
1331 if( is_at_operational( desc->ad_type ) ) {
1332 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1333 !ad_inlist( desc, rs->sr_attrs ) )
1337 /* if DSA-specific and replicating, skip */
1338 if ( op->o_sync != SLAP_CONTROL_NONE &&
1339 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1342 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1348 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1349 ACL_READ, &acl_state ) )
1351 Debug( LDAP_DEBUG_ACL,
1352 "send_search_entry: conn %lu "
1353 "access to attribute %s not allowed\n",
1354 op->o_connid, desc->ad_cname.bv_val, 0 );
1359 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1361 Debug( LDAP_DEBUG_ANY,
1362 "send_search_entry: conn %lu "
1363 "ber_printf failed\n", op->o_connid, 0, 0 );
1365 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1366 set_ldap_error( rs, LDAP_OTHER,
1367 "encoding description error" );
1372 if ( ! attrsonly ) {
1373 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1374 if ( ! access_allowed( op, rs->sr_entry,
1375 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1377 Debug( LDAP_DEBUG_ACL,
1378 "send_search_entry: conn %lu "
1379 "access to %s, value %d not allowed\n",
1380 op->o_connid, desc->ad_cname.bv_val, i );
1385 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1389 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1390 Debug( LDAP_DEBUG_ANY,
1391 "send_search_entry: conn %lu ber_printf failed\n",
1392 op->o_connid, 0, 0 );
1394 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1395 set_ldap_error( rs, LDAP_OTHER,
1396 "encoding values error" );
1403 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1404 Debug( LDAP_DEBUG_ANY,
1405 "send_search_entry: conn %lu ber_printf failed\n",
1406 op->o_connid, 0, 0 );
1408 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1409 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1417 slap_sl_free( e_flags, op->o_tmpmemctx );
1421 rc = ber_printf( ber, /*{{*/ "}N}" );
1424 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1428 #ifdef LDAP_CONNECTIONLESS
1429 if( op->o_conn && op->o_conn->c_is_udp ) {
1430 if ( op->o_protocol != LDAP_VERSION2 ) {
1431 rc = ber_printf( ber, /*{*/ "N}" );
1435 if ( op->o_res_ber == NULL ) {
1436 rc = ber_printf( ber, /*{*/ "N}" );
1441 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1443 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1444 set_ldap_error( rs, LDAP_OTHER, "encode entry end error" );
1449 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1450 op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1452 rs_flush_entry( op, rs, NULL );
1454 if ( op->o_res_ber == NULL ) {
1455 bytes = send_ldap_ber( op, ber );
1456 ber_free_buf( ber );
1459 Debug( LDAP_DEBUG_ANY,
1460 "send_search_entry: conn %lu ber write failed.\n",
1461 op->o_connid, 0, 0 );
1463 rc = LDAP_UNAVAILABLE;
1468 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1469 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1470 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1471 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1472 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1475 Debug( LDAP_DEBUG_TRACE,
1476 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1481 if ( op->o_callback ) {
1482 (void)slap_cleanup_play( op, rs );
1486 slap_sl_free( e_flags, op->o_tmpmemctx );
1489 /* FIXME: Can break if rs now contains an extended response */
1490 if ( rs->sr_operational_attrs ) {
1491 attrs_free( rs->sr_operational_attrs );
1492 rs->sr_operational_attrs = NULL;
1494 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1496 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1497 rs_flush_entry( op, rs, NULL );
1499 RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
1502 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1503 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1504 if ( rs->sr_ctrls ) {
1505 slap_free_ctrls( op, rs->sr_ctrls );
1506 rs->sr_ctrls = NULL;
1514 slap_send_search_reference( Operation *op, SlapReply *rs )
1516 BerElementBuffer berbuf;
1517 BerElement *ber = (BerElement *) &berbuf;
1520 char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1522 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1523 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1525 rs->sr_type = REP_SEARCHREF;
1526 if ( op->o_callback ) {
1527 rc = slap_response_play( op, rs );
1528 if ( rc != SLAP_CB_CONTINUE ) {
1533 Debug( LDAP_DEBUG_TRACE,
1534 "=> send_search_reference: dn=\"%s\"\n",
1537 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1538 ad_entry, NULL, ACL_READ, NULL ) )
1540 Debug( LDAP_DEBUG_ACL,
1541 "send_search_reference: access to entry not allowed\n",
1547 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1548 ad_ref, NULL, ACL_READ, NULL ) )
1550 Debug( LDAP_DEBUG_ACL,
1551 "send_search_reference: access "
1552 "to reference not allowed\n",
1558 if( op->o_domain_scope ) {
1559 Debug( LDAP_DEBUG_ANY,
1560 "send_search_reference: domainScope control in (%s)\n",
1566 if( rs->sr_ref == NULL ) {
1567 Debug( LDAP_DEBUG_ANY,
1568 "send_search_reference: null ref in (%s)\n",
1574 if( op->o_protocol < LDAP_VERSION3 ) {
1576 /* save the references for the result */
1577 if( rs->sr_ref[0].bv_val != NULL ) {
1578 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1584 #ifdef LDAP_CONNECTIONLESS
1585 if( op->o_conn && op->o_conn->c_is_udp ) {
1586 ber = op->o_res_ber;
1590 ber_init_w_nullc( ber, LBER_USE_DER );
1591 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1594 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1595 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1598 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1602 rc = ber_printf( ber, /*"{"*/ "N}" );
1606 Debug( LDAP_DEBUG_ANY,
1607 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1609 #ifdef LDAP_CONNECTIONLESS
1610 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1612 ber_free_buf( ber );
1613 set_ldap_error( rs, LDAP_OTHER, "encode DN error" );
1618 rs_flush_entry( op, rs, NULL );
1620 #ifdef LDAP_CONNECTIONLESS
1621 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1623 bytes = send_ldap_ber( op, ber );
1624 ber_free_buf( ber );
1627 rc = LDAP_UNAVAILABLE;
1629 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1630 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1631 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1632 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1633 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1635 #ifdef LDAP_CONNECTIONLESS
1638 if ( rs->sr_ref != NULL ) {
1641 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1642 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1643 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1648 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1649 op->o_log_prefix, 0, 0, 0, 0 );
1652 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1656 rs_flush_entry( op, rs, NULL );
1659 if ( op->o_callback ) {
1660 (void)slap_cleanup_play( op, rs );
1663 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1664 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1665 if ( rs->sr_ctrls ) {
1666 slap_free_ctrls( op, rs->sr_ctrls );
1667 rs->sr_ctrls = NULL;
1684 *code = LDAP_SUCCESS;
1688 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1689 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1696 while ( (s = strchr( s, '\n' )) != NULL ) {
1701 if ( (c = strchr( s, ':' )) != NULL ) {
1705 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1710 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1716 while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1717 if ( c[ 0 ] == '\0' ) {
1718 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1724 retcode = strtol( c, &next, 10 );
1725 if ( next == NULL || next == c ) {
1726 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1732 while ( isspace( (unsigned char) next[ 0 ] ) && next[ 0 ] != '\n' )
1734 if ( next[ 0 ] != '\0' && next[ 0 ] != '\n' ) {
1735 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1741 /* FIXME: what if it's larger than max int? */
1742 *code = (int)retcode;
1744 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1748 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1753 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1763 int slap_read_controls(
1767 const struct berval *oid,
1768 LDAPControl **ctrl )
1772 BerElementBuffer berbuf;
1773 BerElement *ber = (BerElement *) &berbuf;
1777 Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1778 op->o_log_prefix, oid->bv_val, e->e_dn );
1781 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1782 op->o_preread_attrs : op->o_postread_attrs;
1784 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1785 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1787 ber_init2( ber, &bv, LBER_USE_DER );
1788 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1790 /* create new operation */
1792 /* FIXME: o_bd needed for ACL */
1793 myop.o_bd = op->o_bd;
1794 myop.o_res_ber = ber;
1795 myop.o_callback = NULL;
1796 myop.ors_slimit = 1;
1797 myop.ors_attrsonly = 0;
1799 rc = slap_send_search_entry( &myop, rs );
1802 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1804 if( rc == -1 ) return LDAP_OTHER;
1806 c.ldctl_oid = oid->bv_val;
1807 c.ldctl_iscritical = 0;
1809 if ( *ctrl == NULL ) {
1811 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1813 /* retry: free previous try */
1814 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1818 return LDAP_SUCCESS;
1821 /* Map API errors to protocol errors... */
1823 slap_map_api2result( SlapReply *rs )
1825 switch(rs->sr_err) {
1826 case LDAP_SERVER_DOWN:
1827 return LDAP_UNAVAILABLE;
1828 case LDAP_LOCAL_ERROR:
1830 case LDAP_ENCODING_ERROR:
1831 case LDAP_DECODING_ERROR:
1832 return LDAP_PROTOCOL_ERROR;
1834 return LDAP_UNAVAILABLE;
1835 case LDAP_AUTH_UNKNOWN:
1836 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1837 case LDAP_FILTER_ERROR:
1838 rs->sr_text = "Filter error";
1840 case LDAP_USER_CANCELLED:
1841 rs->sr_text = "User cancelled";
1843 case LDAP_PARAM_ERROR:
1844 return LDAP_PROTOCOL_ERROR;
1845 case LDAP_NO_MEMORY:
1847 case LDAP_CONNECT_ERROR:
1848 return LDAP_UNAVAILABLE;
1849 case LDAP_NOT_SUPPORTED:
1850 return LDAP_UNWILLING_TO_PERFORM;
1851 case LDAP_CONTROL_NOT_FOUND:
1852 return LDAP_PROTOCOL_ERROR;
1853 case LDAP_NO_RESULTS_RETURNED:
1854 return LDAP_NO_SUCH_OBJECT;
1855 case LDAP_MORE_RESULTS_TO_RETURN:
1856 rs->sr_text = "More results to return";
1858 case LDAP_CLIENT_LOOP:
1859 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1860 return LDAP_LOOP_DETECT;
1862 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1869 slap_attr_flags( AttributeName *an )
1871 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1874 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1877 flags |= an_find( an, slap_bv_all_operational_attrs )
1878 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1879 flags |= an_find( an, slap_bv_all_user_attrs )
1880 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;