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-2017 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>
44 (void) gettimeofday( &now, NULL ); \
45 now.tv_sec -= op->o_time; \
46 now.tv_usec -= op->o_tincr; \
47 if ( now.tv_usec < 0 ) { \
48 --now.tv_sec; now.tv_usec += 1000000; \
50 sprintf(timestr, "qtime=%d.%06d etime=%d.%06d", \
51 (int)op->o_qtime.tv_sec, (int)op->o_qtime.tv_usec, \
52 (int)now.tv_sec, (int)now.tv_usec);
53 #define ETIME_LOGFMT "%s "
54 #define StatslogEtime(lvl,fmt,pfx,tag,err,etxt,xtra) \
55 Statslog6(lvl,fmt,pfx,tag,err,timestr,etxt,xtra)
58 #define ETIME_LOGFMT ""
59 #define StatslogEtime Statslog
60 #endif /* SLAP_STATS_ETIME */
62 const struct berval slap_dummy_bv = BER_BVNULL;
64 int slap_null_cb( Operation *op, SlapReply *rs )
69 int slap_freeself_cb( Operation *op, SlapReply *rs )
71 assert( op->o_callback != NULL );
73 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
74 op->o_callback = NULL;
76 return SLAP_CB_CONTINUE;
79 static char *v2ref( BerVarray ref, const char *text )
81 size_t len = 0, i = 0;
86 return ch_strdup(text);
94 if (text[len-1] != '\n') {
99 v2 = ch_malloc( len+i+sizeof("Referral:") );
107 strcpy( v2+len, "Referral:" );
108 len += sizeof("Referral:");
110 for( i=0; ref[i].bv_val != NULL; i++ ) {
111 v2 = ch_realloc( v2, len + ref[i].bv_len + 1 );
113 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
114 len += ref[i].bv_len;
115 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
125 slap_req2res( ber_tag_t tag )
130 case LDAP_REQ_COMPARE:
131 case LDAP_REQ_EXTENDED:
132 case LDAP_REQ_MODIFY:
133 case LDAP_REQ_MODRDN:
137 case LDAP_REQ_DELETE:
138 tag = LDAP_RES_DELETE;
141 case LDAP_REQ_ABANDON:
142 case LDAP_REQ_UNBIND:
146 case LDAP_REQ_SEARCH:
147 tag = LDAP_RES_SEARCH_RESULT;
158 * SlapReply debugging enabled by USE_RS_ASSERT.
160 * Disabled by default, but compiled in (but still unused) when
161 * LDAP_TEST. #define USE_RS_ASSERT as nonzero to enable some
162 * assertions which check the SlapReply. USE_RS_ASSERT = 2 or higher
163 * check aggressively, currently some code fail these tests.
165 * Environment variable $NO_RS_ASSERT controls how USE_RS_ASSERT handles
166 * errors. > 0: ignore errors, 0: abort (the default), < 0: just warn.
168 * Wrap LDAP operation calls in macros SLAP_OP() & co from proto-slap.h
169 * to check the SlapReply. contrib/slapd-tools/wrap_slap_ops converts
170 * source code to use the macros.
172 #if defined(LDAP_TEST) || (defined(USE_RS_ASSERT) && (USE_RS_ASSERT))
174 int rs_suppress_assert = 0;
176 /* RS_ASSERT() helper function */
177 void rs_assert_(const char*file, unsigned line, const char*fn, const char*cond)
179 int no_assert = rs_suppress_assert, save_errno = errno;
182 if ( no_assert >= 0 ) {
183 if ( no_assert == 0 && (s = getenv( "NO_RS_ASSERT" )) && *s ) {
184 no_assert = rs_suppress_assert = atoi( s );
186 if ( no_assert > 0 ) {
192 #ifdef rs_assert_ /* proto-slap.h #defined away the fn parameter */
193 fprintf( stderr,"%s:%u: " "RS_ASSERT(%s) failed.\n", file,line,cond );
195 fprintf( stderr,"%s:%u: %s: RS_ASSERT(%s) failed.\n", file,line,fn,cond );
200 /* $NO_RS_ASSERT > 0: ignore rs_asserts, 0: abort, < 0: just warn */
201 if ( !no_assert /* from $NO_RS_ASSERT */ ) abort();
204 /* SlapReply is consistent */
206 (rs_assert_ok)( const SlapReply *rs )
208 const slap_mask_t flags = rs->sr_flags;
210 if ( flags & REP_ENTRY_MASK ) {
211 RS_ASSERT( !(flags & REP_ENTRY_MUSTRELEASE)
212 || !(flags & (REP_ENTRY_MASK ^ REP_ENTRY_MUSTRELEASE)) );
213 RS_ASSERT( rs->sr_entry != NULL );
214 RS_ASSERT( (1 << rs->sr_type) &
215 ((1 << REP_SEARCH) | (1 << REP_SEARCHREF) |
216 (1 << REP_RESULT) | (1 << REP_GLUE_RESULT)) );
218 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
219 if ( (flags & (REP_MATCHED_MASK | REP_REF_MASK | REP_CTRLS_MASK)) ) {
220 RS_ASSERT( !(flags & REP_MATCHED_MASK) || rs->sr_matched );
221 RS_ASSERT( !(flags & REP_CTRLS_MASK ) || rs->sr_ctrls );
222 /* Note: LDAP_REFERRAL + !sr_ref is OK, becomes LDAP_NO_SUCH_OBJECT */
224 #if (USE_RS_ASSERT) > 2
225 if ( rs->sr_err == LDAP_SUCCESS ) {
226 RS_ASSERT( rs->sr_text == NULL );
227 RS_ASSERT( rs->sr_matched == NULL );
233 /* Ready for calling a new backend operation */
235 (rs_assert_ready)( const SlapReply *rs )
237 RS_ASSERT( !rs->sr_entry );
238 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
239 RS_ASSERT( !rs->sr_text );
240 RS_ASSERT( !rs->sr_ref );
241 RS_ASSERT( !rs->sr_matched );
242 RS_ASSERT( !rs->sr_ctrls );
243 RS_ASSERT( !rs->sr_flags );
244 #if (USE_RS_ASSERT) > 2
245 RS_ASSERT( rs->sr_err == LDAP_SUCCESS );
248 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
252 /* Backend operation done */
254 (rs_assert_done)( const SlapReply *rs )
256 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
257 RS_ASSERT( !(rs->sr_flags & ~(REP_ENTRY_MODIFIABLE|REP_NO_OPERATIONALS)) );
260 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MUSTFLUSH) );
264 #endif /* LDAP_TEST || USE_RS_ASSERT */
266 /* Reset a used SlapReply whose contents has been flushed (freed/released) */
268 (rs_reinit)( SlapReply *rs, slap_reply_t type )
270 rs_reinit( rs, type ); /* proto-slap.h macro */
273 /* Obey and clear rs->sr_flags & REP_ENTRY_MASK. Clear sr_entry if freed. */
275 rs_flush_entry( Operation *op, SlapReply *rs, slap_overinst *on )
279 if ( (rs->sr_flags & REP_ENTRY_MUSTFLUSH) && rs->sr_entry != NULL ) {
280 if ( !(rs->sr_flags & REP_ENTRY_MUSTRELEASE) ) {
281 entry_free( rs->sr_entry );
282 } else if ( on != NULL ) {
283 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
285 be_entry_release_rw( op, rs->sr_entry, 0 );
290 rs->sr_flags &= ~REP_ENTRY_MASK;
293 /* Set rs->sr_entry after obeying and clearing sr_flags & REP_ENTRY_MASK. */
295 rs_replace_entry( Operation *op, SlapReply *rs, slap_overinst *on, Entry *e )
297 rs_flush_entry( op, rs, on );
302 * Ensure rs->sr_entry is modifiable, by duplicating it if necessary.
303 * Obey sr_flags. Set REP_ENTRY_<MODIFIABLE, and MUSTBEFREED if duplicated>.
304 * Return nonzero if rs->sr_entry was replaced.
307 rs_entry2modifiable( Operation *op, SlapReply *rs, slap_overinst *on )
309 if ( rs->sr_flags & REP_ENTRY_MODIFIABLE ) {
313 rs_replace_entry( op, rs, on, entry_dup( rs->sr_entry ));
314 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
318 /* Check for any callbacks that want to be informed about being blocked
319 * on output. These callbacks are expected to leave the callback list
320 * unmodified. Their result is ignored.
326 slap_callback *sc = op->o_callback;
328 for ( ; sc; sc = sc->sc_next ) {
329 if ( sc->sc_writewait )
330 sc->sc_writewait( op, sc );
334 static long send_ldap_ber(
338 Connection *conn = op->o_conn;
343 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
345 /* write only one pdu at a time - wait til it's our turn */
346 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
347 if (( op->o_abandon && !op->o_cancel ) || !connection_valid( conn ) ||
348 conn->c_writers < 0 ) {
349 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
355 while ( conn->c_writers > 0 && conn->c_writing ) {
356 ldap_pvt_thread_pool_idle( &connection_pool );
357 ldap_pvt_thread_cond_wait( &conn->c_write1_cv, &conn->c_write1_mutex );
358 ldap_pvt_thread_pool_unidle( &connection_pool );
361 /* connection was closed under us */
362 if ( conn->c_writers < 0 ) {
363 /* we're the last waiter, let the closer continue */
364 if ( conn->c_writers == -1 )
365 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
367 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
378 if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
386 * we got an error. if it's ewouldblock, we need to
387 * wait on the socket being writable. otherwise, figure
388 * it's a hard error and return.
391 Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
392 err, sock_errstr(err), 0 );
394 if ( err != EWOULDBLOCK && err != EAGAIN ) {
395 close_reason = "connection lost on write";
399 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
400 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
401 connection_closing( conn, close_reason );
402 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
406 /* wait for socket to be write-ready */
407 conn->c_writewaiter = 1;
408 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
409 ldap_pvt_thread_pool_idle( &connection_pool );
410 slap_writewait_play( op );
411 err = slapd_wait_writer( conn->c_sd );
412 conn->c_writewaiter = 0;
413 ldap_pvt_thread_pool_unidle( &connection_pool );
414 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
415 /* 0 is timeout, so we close it.
416 * -1 is an error, close it.
420 close_reason = "writetimeout";
422 close_reason = "connection lost on writewait";
426 if ( conn->c_writers < 0 ) {
433 if ( conn->c_writers < 0 ) {
435 if ( !conn->c_writers )
436 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
439 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
441 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
447 send_ldap_control( BerElement *ber, LDAPControl *c )
453 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
455 if( c->ldctl_iscritical ) {
456 rc = ber_printf( ber, "b",
457 (ber_int_t) c->ldctl_iscritical ) ;
458 if( rc == -1 ) return rc;
461 if( c->ldctl_value.bv_val != NULL ) {
462 rc = ber_printf( ber, "O", &c->ldctl_value );
463 if( rc == -1 ) return rc;
466 rc = ber_printf( ber, /*{*/"N}" );
467 if( rc == -1 ) return rc;
473 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
480 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
481 if( rc == -1 ) return rc;
483 for( ; *c != NULL; c++) {
484 rc = send_ldap_control( ber, *c );
485 if( rc == -1 ) return rc;
488 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
489 /* this is a hack to avoid having to modify op->s_ctrls */
490 if( o->o_sortedresults ) {
491 BerElementBuffer berbuf;
492 BerElement *sber = (BerElement *) &berbuf;
494 BER_BVZERO( &sorted.ldctl_value );
495 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
496 sorted.ldctl_iscritical = 0;
498 ber_init2( sber, NULL, LBER_USE_DER );
500 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
502 if( ber_flatten2( sber, &sorted.ldctl_value, 0 ) == -1 ) {
506 (void) ber_free_buf( sber );
508 rc = send_ldap_control( ber, &sorted );
509 if( rc == -1 ) return rc;
513 rc = ber_printf( ber, /*{*/"N}" );
519 * slap_response_play()
521 * plays the callback list; rationale: a callback can
522 * - remove itself from the list, by setting op->o_callback = NULL;
523 * malloc()'ed callbacks should free themselves from inside the
524 * sc_response() function.
525 * - replace itself with another (list of) callback(s), by setting
526 * op->o_callback = a new (list of) callback(s); in this case, it
527 * is the callback's responsibility to to append existing subsequent
528 * callbacks to the end of the list that is passed to the sc_response()
530 * - modify the list of subsequent callbacks by modifying the value
531 * of the sc_next field from inside the sc_response() function; this
532 * case does not require any handling from inside slap_response_play()
534 * To stop execution of the playlist, the sc_response() function must return
535 * a value different from SLAP_SC_CONTINUE.
537 * The same applies to slap_cleanup_play(); only, there is no means to stop
538 * execution of the playlist, since all cleanup functions must be called.
547 slap_callback *sc = op->o_callback, **scp;
549 rc = SLAP_CB_CONTINUE;
550 for ( scp = ≻ *scp; ) {
551 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
553 op->o_callback = *scp;
554 if ( op->o_callback->sc_response ) {
555 rc = op->o_callback->sc_response( op, rs );
556 if ( op->o_callback == NULL ) {
557 /* the callback has been removed;
562 } else if ( op->o_callback != *scp ) {
563 /* a new callback has been inserted
564 * in place of the existing one; repair the list */
565 *scp = op->o_callback;
568 if ( rc != SLAP_CB_CONTINUE ) break;
582 slap_callback *sc = op->o_callback, **scp;
584 for ( scp = ≻ *scp; ) {
585 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
587 op->o_callback = *scp;
588 if ( op->o_callback->sc_cleanup ) {
589 (void)op->o_callback->sc_cleanup( op, rs );
590 if ( op->o_callback == NULL ) {
591 /* the callback has been removed;
596 } else if ( op->o_callback != *scp ) {
597 /* a new callback has been inserted
598 * after the existing one; repair the list */
599 /* a new callback has been inserted
600 * in place of the existing one; repair the list */
601 *scp = op->o_callback;
604 /* don't care about the result; do all cleanup */
618 BerElementBuffer berbuf;
619 BerElement *ber = (BerElement *) &berbuf;
620 int rc = LDAP_SUCCESS;
623 /* op was actually aborted, bypass everything if client didn't Cancel */
624 if (( rs->sr_err == SLAPD_ABANDON ) && !op->o_cancel ) {
629 if ( op->o_callback ) {
630 rc = slap_response_play( op, rs );
631 if ( rc != SLAP_CB_CONTINUE ) {
636 /* op completed, connection aborted, bypass sending response */
637 if ( op->o_abandon && !op->o_cancel ) {
642 #ifdef LDAP_CONNECTIONLESS
643 if (op->o_conn && op->o_conn->c_is_udp)
648 ber_init_w_nullc( ber, LBER_USE_DER );
649 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
653 if ( rc == SLAPD_ABANDON && op->o_cancel )
656 Debug( LDAP_DEBUG_TRACE,
657 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
658 rs->sr_msgid, rs->sr_tag, rc );
661 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
662 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
666 #ifdef LDAP_CONNECTIONLESS
667 if (op->o_conn && op->o_conn->c_is_udp &&
668 op->o_protocol == LDAP_VERSION2 )
670 rc = ber_printf( ber, "t{ess" /*"}"*/,
672 rs->sr_matched == NULL ? "" : rs->sr_matched,
673 rs->sr_text == NULL ? "" : rs->sr_text );
676 if ( rs->sr_type == REP_INTERMEDIATE ) {
677 rc = ber_printf( ber, "{it{" /*"}}"*/,
678 rs->sr_msgid, rs->sr_tag );
681 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
682 rs->sr_msgid, rs->sr_tag, rc,
683 rs->sr_matched == NULL ? "" : rs->sr_matched,
684 rs->sr_text == NULL ? "" : rs->sr_text );
688 if ( rs->sr_ref != NULL ) {
689 assert( rs->sr_err == LDAP_REFERRAL );
690 rc = ber_printf( ber, "t{W}",
691 LDAP_TAG_REFERRAL, rs->sr_ref );
693 assert( rs->sr_err != LDAP_REFERRAL );
697 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
698 rc = ber_printf( ber, "tO",
699 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
703 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
705 if ( rs->sr_rspoid != NULL ) {
706 rc = ber_printf( ber, "ts",
707 rs->sr_type == REP_EXTENDED
708 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
711 if( rc != -1 && rs->sr_rspdata != NULL ) {
712 rc = ber_printf( ber, "tO",
713 rs->sr_type == REP_EXTENDED
714 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
720 rc = ber_printf( ber, /*"{"*/ "N}" );
724 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
728 rc = ber_printf( ber, /*"{"*/ "N}" );
731 #ifdef LDAP_CONNECTIONLESS
732 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
735 rc = ber_printf( ber, /*"{"*/ "N}" );
740 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
742 #ifdef LDAP_CONNECTIONLESS
743 if (!op->o_conn || op->o_conn->c_is_udp == 0)
752 bytes = send_ldap_ber( op, ber );
753 #ifdef LDAP_CONNECTIONLESS
754 if (!op->o_conn || op->o_conn->c_is_udp == 0)
761 Debug( LDAP_DEBUG_ANY,
762 "send_ldap_response: ber write failed\n",
768 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
769 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
770 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
771 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
774 /* Tell caller that we did this for real, as opposed to being
775 * overridden by a callback
777 rc = SLAP_CB_CONTINUE;
780 if ( op->o_callback ) {
781 (void)slap_cleanup_play( op, rs );
784 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
785 rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
786 if ( rs->sr_matched ) {
787 free( (char *)rs->sr_matched );
788 rs->sr_matched = NULL;
792 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
793 rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
795 ber_bvarray_free( rs->sr_ref );
800 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
801 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
802 if ( rs->sr_ctrls ) {
803 slap_free_ctrls( op, rs->sr_ctrls );
813 send_ldap_disconnect( Operation *op, SlapReply *rs )
815 #define LDAP_UNSOLICITED_ERROR(e) \
816 ( (e) == LDAP_PROTOCOL_ERROR \
817 || (e) == LDAP_STRONG_AUTH_REQUIRED \
818 || (e) == LDAP_UNAVAILABLE )
820 Debug( LDAP_DEBUG_TRACE,
821 "send_ldap_disconnect %d:%s\n",
822 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
823 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
825 /* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
826 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
827 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
829 rs->sr_type = REP_EXTENDED;
830 rs->sr_rspdata = NULL;
832 if ( op->o_protocol < LDAP_VERSION3 ) {
833 rs->sr_rspoid = NULL;
834 rs->sr_tag = slap_req2res( op->o_tag );
835 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
838 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
839 rs->sr_tag = LDAP_RES_EXTENDED;
840 rs->sr_msgid = LDAP_RES_UNSOLICITED;
843 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
845 StatslogEtime( LDAP_DEBUG_STATS,
846 "%s DISCONNECT tag=%lu err=%d "ETIME_LOGFMT"text=%s\n",
847 op->o_log_prefix, rs->sr_tag, rs->sr_err,
848 rs->sr_text ? rs->sr_text : "", 0 );
853 slap_send_ldap_result( Operation *op, SlapReply *rs )
856 const char *otext = rs->sr_text;
857 BerVarray oref = rs->sr_ref;
859 rs->sr_type = REP_RESULT;
861 /* Propagate Abandons so that cleanup callbacks can be processed */
862 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
865 Debug( LDAP_DEBUG_TRACE,
866 "send_ldap_result: %s p=%d\n",
867 op->o_log_prefix, op->o_protocol, 0 );
868 Debug( LDAP_DEBUG_ARGS,
869 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
870 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
871 rs->sr_text ? rs->sr_text : "" );
873 Debug( LDAP_DEBUG_ARGS,
874 "send_ldap_result: referral=\"%s\"\n",
875 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
878 assert( !LDAP_API_ERROR( rs->sr_err ) );
879 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
881 if ( rs->sr_err == LDAP_REFERRAL ) {
882 if( op->o_domain_scope ) rs->sr_ref = NULL;
884 if( rs->sr_ref == NULL ) {
885 rs->sr_err = LDAP_NO_SUCH_OBJECT;
886 } else if ( op->o_protocol < LDAP_VERSION3 ) {
887 rs->sr_err = LDAP_PARTIAL_RESULTS;
891 if ( op->o_protocol < LDAP_VERSION3 ) {
892 tmp = v2ref( rs->sr_ref, rs->sr_text );
898 rs->sr_tag = slap_req2res( op->o_tag );
899 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
901 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
902 if ( rs->sr_ref == NULL ) {
903 rs->sr_flags ^= REP_REF_MUSTBEFREED;
904 ber_bvarray_free( oref );
906 oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
909 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
911 if ( op->o_tag == LDAP_REQ_SEARCH ) {
912 StatslogEtime( LDAP_DEBUG_STATS,
913 "%s SEARCH RESULT tag=%lu err=%d "ETIME_LOGFMT"nentries=%d text=%s\n",
914 op->o_log_prefix, rs->sr_tag, rs->sr_err,
915 rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
917 StatslogEtime( LDAP_DEBUG_STATS,
918 "%s RESULT tag=%lu err=%d "ETIME_LOGFMT"text=%s\n",
919 op->o_log_prefix, rs->sr_tag, rs->sr_err,
920 rs->sr_text ? rs->sr_text : "", 0 );
924 if( tmp != NULL ) ch_free(tmp);
930 send_ldap_sasl( Operation *op, SlapReply *rs )
932 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
934 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
936 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
937 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
939 rs->sr_type = REP_SASL;
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 ) {
945 StatslogEtime( LDAP_DEBUG_STATS,
946 "%s RESULT tag=%lu err=%d "ETIME_LOGFMT"text=%s\n",
947 op->o_log_prefix, rs->sr_tag, rs->sr_err,
948 rs->sr_text ? rs->sr_text : "", 0 );
953 slap_send_ldap_extended( Operation *op, SlapReply *rs )
955 Debug( LDAP_DEBUG_TRACE,
956 "send_ldap_extended: err=%d oid=%s len=%ld\n",
958 rs->sr_rspoid ? rs->sr_rspoid : "",
959 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
961 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
962 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
964 rs->sr_type = REP_EXTENDED;
965 rs->sr_tag = slap_req2res( op->o_tag );
966 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
968 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
970 StatslogEtime( LDAP_DEBUG_STATS,
971 "%s RESULT oid=%s err=%d "ETIME_LOGFMT"text=%s\n",
972 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
973 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
978 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
980 Debug( LDAP_DEBUG_TRACE,
981 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
983 rs->sr_rspoid ? rs->sr_rspoid : "",
984 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
986 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
987 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
989 rs->sr_type = REP_INTERMEDIATE;
990 rs->sr_tag = LDAP_RES_INTERMEDIATE;
991 rs->sr_msgid = op->o_msgid;
992 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
993 Statslog( LDAP_DEBUG_STATS2,
994 "%s INTERM oid=%s\n",
996 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
1000 #define set_ldap_error( rs, err, text ) do { \
1001 (rs)->sr_err = err; (rs)->sr_text = text; } while(0)
1006 * LDAP_SUCCESS entry sent
1007 * LDAP_OTHER entry not sent (other)
1008 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
1009 * LDAP_UNAVAILABLE entry not sent (connection closed)
1010 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
1014 slap_send_search_entry( Operation *op, SlapReply *rs )
1016 BerElementBuffer berbuf;
1017 BerElement *ber = (BerElement *) &berbuf;
1019 int i, j, rc = LDAP_UNAVAILABLE, bytes;
1021 AccessControlState acl_state = ACL_STATE_INIT;
1023 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1025 /* a_flags: array of flags telling if the i-th element will be
1026 * returned or filtered out
1027 * e_flags: array of a_flags
1029 char **e_flags = NULL;
1031 rs->sr_type = REP_SEARCH;
1033 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
1034 rc = LDAP_SIZELIMIT_EXCEEDED;
1038 /* Every 64 entries, check for thread pool pause */
1039 if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
1040 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
1046 /* eventually will loop through generated operational attribute types
1047 * currently implemented types include:
1048 * entryDN, subschemaSubentry, and hasSubordinates */
1049 /* NOTE: moved before overlays callback circling because
1050 * they may modify entry and other stuff in rs */
1051 /* check for special all operational attributes ("+") type */
1052 /* FIXME: maybe we could set this flag at the operation level;
1053 * however, in principle the caller of send_search_entry() may
1054 * change the attribute list at each call */
1055 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
1057 rc = backend_operational( op, rs );
1062 if ( op->o_callback ) {
1063 rc = slap_response_play( op, rs );
1064 if ( rc != SLAP_CB_CONTINUE ) {
1069 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
1070 op->o_connid, rs->sr_entry->e_name.bv_val,
1071 op->ors_attrsonly ? " (attrsOnly)" : "" );
1073 attrsonly = op->ors_attrsonly;
1075 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
1076 Debug( LDAP_DEBUG_ACL,
1077 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
1078 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
1080 rc = LDAP_INSUFFICIENT_ACCESS;
1084 if ( op->o_res_ber ) {
1085 /* read back control or LDAP_CONNECTIONLESS */
1086 ber = op->o_res_ber;
1090 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1091 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1093 ber_init2( ber, &bv, LBER_USE_DER );
1094 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1097 #ifdef LDAP_CONNECTIONLESS
1098 if ( op->o_conn && op->o_conn->c_is_udp ) {
1099 /* CONNECTIONLESS */
1100 if ( op->o_protocol == LDAP_VERSION2 ) {
1101 rc = ber_printf(ber, "t{O{" /*}}*/,
1102 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1104 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1105 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1109 if ( op->o_res_ber ) {
1110 /* read back control */
1111 rc = ber_printf( ber, "t{O{" /*}}*/,
1112 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1114 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1115 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1119 Debug( LDAP_DEBUG_ANY,
1120 "send_search_entry: conn %lu ber_printf failed\n",
1121 op->o_connid, 0, 0 );
1123 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1124 set_ldap_error( rs, LDAP_OTHER, "encoding DN error" );
1129 /* check for special all user attributes ("*") type */
1130 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
1132 /* create an array of arrays of flags. Each flag corresponds
1133 * to particular value of attribute and equals 1 if value matches
1134 * to ValuesReturnFilter or 0 if not
1136 if ( op->o_vrFilter != NULL ) {
1140 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1141 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1144 size = i * sizeof(char *) + k;
1147 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
1148 if( e_flags == NULL ) {
1149 Debug( LDAP_DEBUG_ANY,
1150 "send_search_entry: conn %lu slap_sl_calloc failed\n",
1151 op->o_connid, 0, 0 );
1154 set_ldap_error( rs, LDAP_OTHER, "out of memory" );
1157 a_flags = (char *)(e_flags + i);
1158 memset( a_flags, 0, k );
1159 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
1160 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1161 e_flags[i] = a_flags;
1165 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
1167 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
1168 "conn %lu matched values filtering failed\n",
1169 op->o_connid, 0, 0 );
1170 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1171 set_ldap_error( rs, LDAP_OTHER,
1172 "matched values filtering error" );
1179 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
1180 AttributeDescription *desc = a->a_desc;
1183 if ( rs->sr_attrs == NULL ) {
1184 /* all user attrs request, skip operational attributes */
1185 if( is_at_operational( desc->ad_type ) ) {
1190 /* specific attrs requested */
1191 if ( is_at_operational( desc->ad_type ) ) {
1192 /* if not explicitly requested */
1193 if ( !ad_inlist( desc, rs->sr_attrs )) {
1194 /* if not all op attrs requested, skip */
1195 if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
1197 /* if DSA-specific and replicating, skip */
1198 if ( op->o_sync != SLAP_CONTROL_NONE &&
1199 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1203 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1210 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1211 ACL_READ, &acl_state ) )
1213 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1214 "conn %lu access to attribute %s not allowed\n",
1215 op->o_connid, desc->ad_cname.bv_val, 0 );
1219 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1220 Debug( LDAP_DEBUG_ANY,
1221 "send_search_entry: conn %lu ber_printf failed\n",
1222 op->o_connid, 0, 0 );
1224 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1225 set_ldap_error( rs, LDAP_OTHER,
1226 "encoding description error");
1234 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1235 if ( ! access_allowed( op, rs->sr_entry,
1236 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1238 Debug( LDAP_DEBUG_ACL,
1239 "send_search_entry: conn %lu "
1240 "access to attribute %s, value #%d not allowed\n",
1241 op->o_connid, desc->ad_cname.bv_val, i );
1246 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1253 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -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,
1260 "encoding description error");
1265 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1266 Debug( LDAP_DEBUG_ANY,
1267 "send_search_entry: conn %lu "
1268 "ber_printf failed.\n", op->o_connid, 0, 0 );
1270 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1271 set_ldap_error( rs, LDAP_OTHER,
1272 "encoding values error" );
1279 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1280 Debug( LDAP_DEBUG_ANY,
1281 "send_search_entry: conn %lu ber_printf failed\n",
1282 op->o_connid, 0, 0 );
1284 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1285 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1291 /* NOTE: moved before overlays callback circling because
1292 * they may modify entry and other stuff in rs */
1293 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1297 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1298 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1301 size = i * sizeof(char *) + k;
1303 char *a_flags, **tmp;
1306 * Reuse previous memory - we likely need less space
1307 * for operational attributes
1309 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1311 if ( tmp == NULL ) {
1312 Debug( LDAP_DEBUG_ANY,
1313 "send_search_entry: conn %lu "
1314 "not enough memory "
1315 "for matched values filtering\n",
1316 op->o_connid, 0, 0 );
1317 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1318 set_ldap_error( rs, LDAP_OTHER,
1319 "not enough memory for matched values filtering" );
1323 a_flags = (char *)(e_flags + i);
1324 memset( a_flags, 0, k );
1325 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1326 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1327 e_flags[i] = a_flags;
1330 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1333 Debug( LDAP_DEBUG_ANY,
1334 "send_search_entry: conn %lu "
1335 "matched values filtering failed\n",
1336 op->o_connid, 0, 0);
1337 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1338 set_ldap_error( rs, LDAP_OTHER,
1339 "matched values filtering error" );
1346 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1347 AttributeDescription *desc = a->a_desc;
1349 if ( rs->sr_attrs == NULL ) {
1350 /* all user attrs request, skip operational attributes */
1351 if( is_at_operational( desc->ad_type ) ) {
1356 /* specific attrs requested */
1357 if( is_at_operational( desc->ad_type ) ) {
1358 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1359 !ad_inlist( desc, rs->sr_attrs ) )
1363 /* if DSA-specific and replicating, skip */
1364 if ( op->o_sync != SLAP_CONTROL_NONE &&
1365 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1368 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1374 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1375 ACL_READ, &acl_state ) )
1377 Debug( LDAP_DEBUG_ACL,
1378 "send_search_entry: conn %lu "
1379 "access to attribute %s not allowed\n",
1380 op->o_connid, desc->ad_cname.bv_val, 0 );
1385 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1387 Debug( LDAP_DEBUG_ANY,
1388 "send_search_entry: conn %lu "
1389 "ber_printf failed\n", op->o_connid, 0, 0 );
1391 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1392 set_ldap_error( rs, LDAP_OTHER,
1393 "encoding description error" );
1398 if ( ! attrsonly ) {
1399 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1400 if ( ! access_allowed( op, rs->sr_entry,
1401 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1403 Debug( LDAP_DEBUG_ACL,
1404 "send_search_entry: conn %lu "
1405 "access to %s, value %d not allowed\n",
1406 op->o_connid, desc->ad_cname.bv_val, i );
1411 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1415 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1416 Debug( LDAP_DEBUG_ANY,
1417 "send_search_entry: conn %lu ber_printf failed\n",
1418 op->o_connid, 0, 0 );
1420 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1421 set_ldap_error( rs, LDAP_OTHER,
1422 "encoding values error" );
1429 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1430 Debug( LDAP_DEBUG_ANY,
1431 "send_search_entry: conn %lu ber_printf failed\n",
1432 op->o_connid, 0, 0 );
1434 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1435 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1443 slap_sl_free( e_flags, op->o_tmpmemctx );
1447 rc = ber_printf( ber, /*{{*/ "}N}" );
1450 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1454 #ifdef LDAP_CONNECTIONLESS
1455 if( op->o_conn && op->o_conn->c_is_udp ) {
1456 if ( op->o_protocol != LDAP_VERSION2 ) {
1457 rc = ber_printf( ber, /*{*/ "N}" );
1461 if ( op->o_res_ber == NULL ) {
1462 rc = ber_printf( ber, /*{*/ "N}" );
1467 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1469 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1470 set_ldap_error( rs, LDAP_OTHER, "encode entry end error" );
1475 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1476 op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1478 rs_flush_entry( op, rs, NULL );
1480 if ( op->o_res_ber == NULL ) {
1481 bytes = send_ldap_ber( op, ber );
1482 ber_free_buf( ber );
1485 Debug( LDAP_DEBUG_ANY,
1486 "send_search_entry: conn %lu ber write failed.\n",
1487 op->o_connid, 0, 0 );
1489 rc = LDAP_UNAVAILABLE;
1494 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1495 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1496 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1497 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1498 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1501 Debug( LDAP_DEBUG_TRACE,
1502 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1507 if ( op->o_callback ) {
1508 (void)slap_cleanup_play( op, rs );
1512 slap_sl_free( e_flags, op->o_tmpmemctx );
1515 /* FIXME: Can break if rs now contains an extended response */
1516 if ( rs->sr_operational_attrs ) {
1517 attrs_free( rs->sr_operational_attrs );
1518 rs->sr_operational_attrs = NULL;
1520 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1522 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1523 rs_flush_entry( op, rs, NULL );
1525 RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
1528 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1529 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1530 if ( rs->sr_ctrls ) {
1531 slap_free_ctrls( op, rs->sr_ctrls );
1532 rs->sr_ctrls = NULL;
1540 slap_send_search_reference( Operation *op, SlapReply *rs )
1542 BerElementBuffer berbuf;
1543 BerElement *ber = (BerElement *) &berbuf;
1546 char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1548 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1549 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1551 rs->sr_type = REP_SEARCHREF;
1552 if ( op->o_callback ) {
1553 rc = slap_response_play( op, rs );
1554 if ( rc != SLAP_CB_CONTINUE ) {
1559 Debug( LDAP_DEBUG_TRACE,
1560 "=> send_search_reference: dn=\"%s\"\n",
1563 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1564 ad_entry, NULL, ACL_READ, NULL ) )
1566 Debug( LDAP_DEBUG_ACL,
1567 "send_search_reference: access to entry not allowed\n",
1573 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1574 ad_ref, NULL, ACL_READ, NULL ) )
1576 Debug( LDAP_DEBUG_ACL,
1577 "send_search_reference: access "
1578 "to reference not allowed\n",
1584 if( op->o_domain_scope ) {
1585 Debug( LDAP_DEBUG_ANY,
1586 "send_search_reference: domainScope control in (%s)\n",
1592 if( rs->sr_ref == NULL ) {
1593 Debug( LDAP_DEBUG_ANY,
1594 "send_search_reference: null ref in (%s)\n",
1600 if( op->o_protocol < LDAP_VERSION3 ) {
1602 /* save the references for the result */
1603 if( rs->sr_ref[0].bv_val != NULL ) {
1604 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1610 #ifdef LDAP_CONNECTIONLESS
1611 if( op->o_conn && op->o_conn->c_is_udp ) {
1612 ber = op->o_res_ber;
1616 ber_init_w_nullc( ber, LBER_USE_DER );
1617 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1620 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1621 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1624 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1628 rc = ber_printf( ber, /*"{"*/ "N}" );
1632 Debug( LDAP_DEBUG_ANY,
1633 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1635 #ifdef LDAP_CONNECTIONLESS
1636 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1638 ber_free_buf( ber );
1639 set_ldap_error( rs, LDAP_OTHER, "encode DN error" );
1644 rs_flush_entry( op, rs, NULL );
1646 #ifdef LDAP_CONNECTIONLESS
1647 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1649 bytes = send_ldap_ber( op, ber );
1650 ber_free_buf( ber );
1653 rc = LDAP_UNAVAILABLE;
1655 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1656 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1657 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1658 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1659 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1661 #ifdef LDAP_CONNECTIONLESS
1664 if ( rs->sr_ref != NULL ) {
1667 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1668 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1669 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1674 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1675 op->o_log_prefix, 0, 0, 0, 0 );
1678 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1682 rs_flush_entry( op, rs, NULL );
1685 if ( op->o_callback ) {
1686 (void)slap_cleanup_play( op, rs );
1689 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1690 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1691 if ( rs->sr_ctrls ) {
1692 slap_free_ctrls( op, rs->sr_ctrls );
1693 rs->sr_ctrls = NULL;
1710 *code = LDAP_SUCCESS;
1714 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1715 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1722 while ( (s = strchr( s, '\n' )) != NULL ) {
1727 if ( (c = strchr( s, ':' )) != NULL ) {
1731 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1736 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1742 while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1743 if ( c[ 0 ] == '\0' ) {
1744 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1750 retcode = strtol( c, &next, 10 );
1751 if ( next == NULL || next == c ) {
1752 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1758 while ( isspace( (unsigned char) next[ 0 ] ) && next[ 0 ] != '\n' )
1760 if ( next[ 0 ] != '\0' && next[ 0 ] != '\n' ) {
1761 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1767 /* FIXME: what if it's larger than max int? */
1768 *code = (int)retcode;
1770 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1774 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1779 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1789 int slap_read_controls(
1793 const struct berval *oid,
1794 LDAPControl **ctrl )
1798 BerElementBuffer berbuf;
1799 BerElement *ber = (BerElement *) &berbuf;
1803 Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1804 op->o_log_prefix, oid->bv_val, e->e_dn );
1807 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1808 op->o_preread_attrs : op->o_postread_attrs;
1810 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1811 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1813 ber_init2( ber, &bv, LBER_USE_DER );
1814 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1816 /* create new operation */
1818 /* FIXME: o_bd needed for ACL */
1819 myop.o_bd = op->o_bd;
1820 myop.o_res_ber = ber;
1821 myop.o_callback = NULL;
1822 myop.ors_slimit = 1;
1823 myop.ors_attrsonly = 0;
1825 rc = slap_send_search_entry( &myop, rs );
1828 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1830 if( rc == -1 ) return LDAP_OTHER;
1832 c.ldctl_oid = oid->bv_val;
1833 c.ldctl_iscritical = 0;
1835 if ( *ctrl == NULL ) {
1837 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1839 /* retry: free previous try */
1840 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1844 return LDAP_SUCCESS;
1847 /* Map API errors to protocol errors... */
1849 slap_map_api2result( SlapReply *rs )
1851 switch(rs->sr_err) {
1852 case LDAP_SERVER_DOWN:
1853 return LDAP_UNAVAILABLE;
1854 case LDAP_LOCAL_ERROR:
1856 case LDAP_ENCODING_ERROR:
1857 case LDAP_DECODING_ERROR:
1858 return LDAP_PROTOCOL_ERROR;
1860 return LDAP_UNAVAILABLE;
1861 case LDAP_AUTH_UNKNOWN:
1862 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1863 case LDAP_FILTER_ERROR:
1864 rs->sr_text = "Filter error";
1866 case LDAP_USER_CANCELLED:
1867 rs->sr_text = "User cancelled";
1869 case LDAP_PARAM_ERROR:
1870 return LDAP_PROTOCOL_ERROR;
1871 case LDAP_NO_MEMORY:
1873 case LDAP_CONNECT_ERROR:
1874 return LDAP_UNAVAILABLE;
1875 case LDAP_NOT_SUPPORTED:
1876 return LDAP_UNWILLING_TO_PERFORM;
1877 case LDAP_CONTROL_NOT_FOUND:
1878 return LDAP_PROTOCOL_ERROR;
1879 case LDAP_NO_RESULTS_RETURNED:
1880 return LDAP_NO_SUCH_OBJECT;
1881 case LDAP_MORE_RESULTS_TO_RETURN:
1882 rs->sr_text = "More results to return";
1884 case LDAP_CLIENT_LOOP:
1885 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1886 return LDAP_LOOP_DETECT;
1888 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1895 slap_attr_flags( AttributeName *an )
1897 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1900 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1903 flags |= an_find( an, slap_bv_all_operational_attrs )
1904 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1905 flags |= an_find( an, slap_bv_all_user_attrs )
1906 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;