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-2013 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;
135 /* SlapReply debugging, prodo-slap.h overrides it in OpenLDAP releases */
136 #if defined(LDAP_TEST) || (defined(USE_RS_ASSERT) && (USE_RS_ASSERT))
138 int rs_suppress_assert = 0;
140 /* RS_ASSERT() helper function */
141 void rs_assert_(const char*file, unsigned line, const char*fn, const char*cond)
143 int no_assert = rs_suppress_assert, save_errno = errno;
146 if ( no_assert >= 0 ) {
147 if ( no_assert == 0 && (s = getenv( "NO_RS_ASSERT" )) && *s ) {
148 no_assert = rs_suppress_assert = atoi( s );
150 if ( no_assert > 0 ) {
156 #ifdef rs_assert_ /* proto-slap.h #defined away the fn parameter */
157 fprintf( stderr,"%s:%u: " "RS_ASSERT(%s) failed.\n", file,line,cond );
159 fprintf( stderr,"%s:%u: %s: RS_ASSERT(%s) failed.\n", file,line,fn,cond );
164 /* $NO_RS_ASSERT > 0: ignore rs_asserts, 0: abort, < 0: just warn */
165 if ( !no_assert /* from $NO_RS_ASSERT */ ) abort();
168 /* SlapReply is consistent */
170 (rs_assert_ok)( const SlapReply *rs )
172 const slap_mask_t flags = rs->sr_flags;
174 if ( flags & REP_ENTRY_MASK ) {
175 RS_ASSERT( !(flags & REP_ENTRY_MUSTRELEASE)
176 || !(flags & (REP_ENTRY_MASK ^ REP_ENTRY_MUSTRELEASE)) );
177 RS_ASSERT( rs->sr_entry != NULL );
178 RS_ASSERT( (1 << rs->sr_type) &
179 ((1 << REP_SEARCH) | (1 << REP_SEARCHREF) |
180 (1 << REP_RESULT) | (1 << REP_GLUE_RESULT)) );
182 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
183 if ( (flags & (REP_MATCHED_MASK | REP_REF_MASK | REP_CTRLS_MASK)) ) {
184 RS_ASSERT( !(flags & REP_MATCHED_MASK) || rs->sr_matched );
185 RS_ASSERT( !(flags & REP_CTRLS_MASK ) || rs->sr_ctrls );
186 /* Note: LDAP_REFERRAL + !sr_ref is OK, becomes LDAP_NO_SUCH_OBJECT */
188 #if (USE_RS_ASSERT) > 2
189 if ( rs->sr_err == LDAP_SUCCESS ) {
190 RS_ASSERT( rs->sr_text == NULL );
191 RS_ASSERT( rs->sr_matched == NULL );
197 /* Ready for calling a new backend operation */
199 (rs_assert_ready)( const SlapReply *rs )
201 RS_ASSERT( !rs->sr_entry );
202 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
203 RS_ASSERT( !rs->sr_text );
204 RS_ASSERT( !rs->sr_ref );
205 RS_ASSERT( !rs->sr_matched );
206 RS_ASSERT( !rs->sr_ctrls );
207 RS_ASSERT( !rs->sr_flags );
208 #if (USE_RS_ASSERT) > 2
209 RS_ASSERT( rs->sr_err == LDAP_SUCCESS );
212 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
216 /* Backend operation done */
218 (rs_assert_done)( const SlapReply *rs )
220 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
221 RS_ASSERT( !(rs->sr_flags & ~(REP_ENTRY_MODIFIABLE|REP_NO_OPERATIONALS)) );
224 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MUSTFLUSH) );
228 #endif /* LDAP_TEST || USE_RS_ASSERT */
230 /* Reset a used SlapReply whose contents has been flushed (freed/released) */
232 (rs_reinit)( SlapReply *rs, slap_reply_t type )
234 rs_reinit( rs, type ); /* proto-slap.h macro */
237 /* Obey and clear rs->sr_flags & REP_ENTRY_MASK. Clear sr_entry if freed. */
239 rs_flush_entry( Operation *op, SlapReply *rs, slap_overinst *on )
243 if ( (rs->sr_flags & REP_ENTRY_MUSTFLUSH) && rs->sr_entry != NULL ) {
244 if ( !(rs->sr_flags & REP_ENTRY_MUSTRELEASE) ) {
245 entry_free( rs->sr_entry );
246 } else if ( on != NULL ) {
247 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
249 be_entry_release_rw( op, rs->sr_entry, 0 );
254 rs->sr_flags &= ~REP_ENTRY_MASK;
257 /* Set rs->sr_entry after obeying and clearing sr_flags & REP_ENTRY_MASK. */
259 rs_replace_entry( Operation *op, SlapReply *rs, slap_overinst *on, Entry *e )
261 rs_flush_entry( op, rs, on );
266 * Ensure rs->sr_entry is modifiable, by duplicating it if necessary.
267 * Obey sr_flags. Set REP_ENTRY_<MODIFIABLE, and MUSTBEFREED if duplicated>.
268 * Return nonzero if rs->sr_entry was replaced.
271 rs_entry2modifiable( Operation *op, SlapReply *rs, slap_overinst *on )
273 if ( rs->sr_flags & REP_ENTRY_MODIFIABLE ) {
277 rs_replace_entry( op, rs, on, entry_dup( rs->sr_entry ));
278 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
282 static long send_ldap_ber(
286 Connection *conn = op->o_conn;
290 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
292 /* write only one pdu at a time - wait til it's our turn */
293 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
294 if (( op->o_abandon && !op->o_cancel ) || !connection_valid( conn ) ||
295 conn->c_writers < 0 ) {
296 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
302 while ( conn->c_writers > 0 && conn->c_writing ) {
303 ldap_pvt_thread_cond_wait( &conn->c_write1_cv, &conn->c_write1_mutex );
306 /* connection was closed under us */
307 if ( conn->c_writers < 0 ) {
308 /* we're the last waiter, let the closer continue */
309 if ( conn->c_writers == -1 )
310 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
312 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
323 if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
331 * we got an error. if it's ewouldblock, we need to
332 * wait on the socket being writable. otherwise, figure
333 * it's a hard error and return.
336 Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
337 err, sock_errstr(err), 0 );
339 if ( err != EWOULDBLOCK && err != EAGAIN ) {
342 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
343 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
344 connection_closing( conn, "connection lost on write" );
346 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
350 /* wait for socket to be write-ready */
351 ldap_pvt_thread_mutex_lock( &conn->c_write2_mutex );
352 conn->c_writewaiter = 1;
353 slapd_set_write( conn->c_sd, 2 );
355 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
356 ldap_pvt_thread_pool_idle( &connection_pool );
357 ldap_pvt_thread_cond_wait( &conn->c_write2_cv, &conn->c_write2_mutex );
358 conn->c_writewaiter = 0;
359 ldap_pvt_thread_mutex_unlock( &conn->c_write2_mutex );
360 ldap_pvt_thread_pool_unidle( &connection_pool );
361 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
362 if ( conn->c_writers < 0 ) {
369 if ( conn->c_writers < 0 ) {
371 if ( !conn->c_writers )
372 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
375 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
377 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
383 send_ldap_control( BerElement *ber, LDAPControl *c )
389 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
391 if( c->ldctl_iscritical ) {
392 rc = ber_printf( ber, "b",
393 (ber_int_t) c->ldctl_iscritical ) ;
394 if( rc == -1 ) return rc;
397 if( c->ldctl_value.bv_val != NULL ) {
398 rc = ber_printf( ber, "O", &c->ldctl_value );
399 if( rc == -1 ) return rc;
402 rc = ber_printf( ber, /*{*/"N}" );
403 if( rc == -1 ) return rc;
409 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
416 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
417 if( rc == -1 ) return rc;
419 for( ; *c != NULL; c++) {
420 rc = send_ldap_control( ber, *c );
421 if( rc == -1 ) return rc;
424 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
425 /* this is a hack to avoid having to modify op->s_ctrls */
426 if( o->o_sortedresults ) {
427 BerElementBuffer berbuf;
428 BerElement *sber = (BerElement *) &berbuf;
430 BER_BVZERO( &sorted.ldctl_value );
431 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
432 sorted.ldctl_iscritical = 0;
434 ber_init2( sber, NULL, LBER_USE_DER );
436 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
438 if( ber_flatten2( sber, &sorted.ldctl_value, 0 ) == -1 ) {
442 (void) ber_free_buf( sber );
444 rc = send_ldap_control( ber, &sorted );
445 if( rc == -1 ) return rc;
449 rc = ber_printf( ber, /*{*/"N}" );
455 * slap_response_play()
457 * plays the callback list; rationale: a callback can
458 * - remove itself from the list, by setting op->o_callback = NULL;
459 * malloc()'ed callbacks should free themselves from inside the
460 * sc_response() function.
461 * - replace itself with another (list of) callback(s), by setting
462 * op->o_callback = a new (list of) callback(s); in this case, it
463 * is the callback's responsibility to to append existing subsequent
464 * callbacks to the end of the list that is passed to the sc_response()
466 * - modify the list of subsequent callbacks by modifying the value
467 * of the sc_next field from inside the sc_response() function; this
468 * case does not require any handling from inside slap_response_play()
470 * To stop execution of the playlist, the sc_response() function must return
471 * a value different from SLAP_SC_CONTINUE.
473 * The same applies to slap_cleanup_play(); only, there is no means to stop
474 * execution of the playlist, since all cleanup functions must be called.
483 slap_callback *sc = op->o_callback, **scp;
485 rc = SLAP_CB_CONTINUE;
486 for ( scp = ≻ *scp; ) {
487 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
489 op->o_callback = *scp;
490 if ( op->o_callback->sc_response ) {
491 rc = op->o_callback->sc_response( op, rs );
492 if ( op->o_callback == NULL ) {
493 /* the callback has been removed;
498 } else if ( op->o_callback != *scp ) {
499 /* a new callback has been inserted
500 * in place of the existing one; repair the list */
501 *scp = op->o_callback;
504 if ( rc != SLAP_CB_CONTINUE ) break;
518 slap_callback *sc = op->o_callback, **scp;
520 for ( scp = ≻ *scp; ) {
521 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
523 op->o_callback = *scp;
524 if ( op->o_callback->sc_cleanup ) {
525 (void)op->o_callback->sc_cleanup( op, rs );
526 if ( op->o_callback == NULL ) {
527 /* the callback has been removed;
532 } else if ( op->o_callback != *scp ) {
533 /* a new callback has been inserted
534 * after the existing one; repair the list */
535 /* a new callback has been inserted
536 * in place of the existing one; repair the list */
537 *scp = op->o_callback;
540 /* don't care about the result; do all cleanup */
554 BerElementBuffer berbuf;
555 BerElement *ber = (BerElement *) &berbuf;
556 int rc = LDAP_SUCCESS;
559 /* op was actually aborted, bypass everything if client didn't Cancel */
560 if (( rs->sr_err == SLAPD_ABANDON ) && !op->o_cancel ) {
565 if ( op->o_callback ) {
566 rc = slap_response_play( op, rs );
567 if ( rc != SLAP_CB_CONTINUE ) {
572 /* op completed, connection aborted, bypass sending response */
573 if ( op->o_abandon && !op->o_cancel ) {
578 #ifdef LDAP_CONNECTIONLESS
579 if (op->o_conn && op->o_conn->c_is_udp)
584 ber_init_w_nullc( ber, LBER_USE_DER );
585 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
589 if ( rc == SLAPD_ABANDON && op->o_cancel )
592 Debug( LDAP_DEBUG_TRACE,
593 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
594 rs->sr_msgid, rs->sr_tag, rc );
597 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
598 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
602 #ifdef LDAP_CONNECTIONLESS
603 if (op->o_conn && op->o_conn->c_is_udp &&
604 op->o_protocol == LDAP_VERSION2 )
606 rc = ber_printf( ber, "t{ess" /*"}"*/,
608 rs->sr_matched == NULL ? "" : rs->sr_matched,
609 rs->sr_text == NULL ? "" : rs->sr_text );
612 if ( rs->sr_type == REP_INTERMEDIATE ) {
613 rc = ber_printf( ber, "{it{" /*"}}"*/,
614 rs->sr_msgid, rs->sr_tag );
617 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
618 rs->sr_msgid, rs->sr_tag, rc,
619 rs->sr_matched == NULL ? "" : rs->sr_matched,
620 rs->sr_text == NULL ? "" : rs->sr_text );
624 if ( rs->sr_ref != NULL ) {
625 assert( rs->sr_err == LDAP_REFERRAL );
626 rc = ber_printf( ber, "t{W}",
627 LDAP_TAG_REFERRAL, rs->sr_ref );
629 assert( rs->sr_err != LDAP_REFERRAL );
633 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
634 rc = ber_printf( ber, "tO",
635 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
639 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
641 if ( rs->sr_rspoid != NULL ) {
642 rc = ber_printf( ber, "ts",
643 rs->sr_type == REP_EXTENDED
644 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
647 if( rc != -1 && rs->sr_rspdata != NULL ) {
648 rc = ber_printf( ber, "tO",
649 rs->sr_type == REP_EXTENDED
650 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
656 rc = ber_printf( ber, /*"{"*/ "N}" );
660 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
664 rc = ber_printf( ber, /*"{"*/ "N}" );
667 #ifdef LDAP_CONNECTIONLESS
668 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
671 rc = ber_printf( ber, /*"{"*/ "N}" );
676 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
678 #ifdef LDAP_CONNECTIONLESS
679 if (!op->o_conn || op->o_conn->c_is_udp == 0)
688 bytes = send_ldap_ber( op, ber );
689 #ifdef LDAP_CONNECTIONLESS
690 if (!op->o_conn || op->o_conn->c_is_udp == 0)
697 Debug( LDAP_DEBUG_ANY,
698 "send_ldap_response: ber write failed\n",
704 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
705 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
706 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
707 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
710 /* Tell caller that we did this for real, as opposed to being
711 * overridden by a callback
713 rc = SLAP_CB_CONTINUE;
716 if ( op->o_callback ) {
717 (void)slap_cleanup_play( op, rs );
720 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
721 rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
722 if ( rs->sr_matched ) {
723 free( (char *)rs->sr_matched );
724 rs->sr_matched = NULL;
728 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
729 rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
731 ber_bvarray_free( rs->sr_ref );
736 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
737 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
738 if ( rs->sr_ctrls ) {
739 slap_free_ctrls( op, rs->sr_ctrls );
749 send_ldap_disconnect( Operation *op, SlapReply *rs )
751 #define LDAP_UNSOLICITED_ERROR(e) \
752 ( (e) == LDAP_PROTOCOL_ERROR \
753 || (e) == LDAP_STRONG_AUTH_REQUIRED \
754 || (e) == LDAP_UNAVAILABLE )
756 Debug( LDAP_DEBUG_TRACE,
757 "send_ldap_disconnect %d:%s\n",
758 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
759 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
761 /* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
762 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
763 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
765 rs->sr_type = REP_EXTENDED;
766 rs->sr_rspdata = NULL;
768 if ( op->o_protocol < LDAP_VERSION3 ) {
769 rs->sr_rspoid = NULL;
770 rs->sr_tag = slap_req2res( op->o_tag );
771 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
774 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
775 rs->sr_tag = LDAP_RES_EXTENDED;
776 rs->sr_msgid = LDAP_RES_UNSOLICITED;
779 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
780 Statslog( LDAP_DEBUG_STATS,
781 "%s DISCONNECT tag=%lu err=%d text=%s\n",
782 op->o_log_prefix, rs->sr_tag, rs->sr_err,
783 rs->sr_text ? rs->sr_text : "", 0 );
788 slap_send_ldap_result( Operation *op, SlapReply *rs )
791 const char *otext = rs->sr_text;
792 BerVarray oref = rs->sr_ref;
794 rs->sr_type = REP_RESULT;
796 /* Propagate Abandons so that cleanup callbacks can be processed */
797 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
800 Debug( LDAP_DEBUG_TRACE,
801 "send_ldap_result: %s p=%d\n",
802 op->o_log_prefix, op->o_protocol, 0 );
803 Debug( LDAP_DEBUG_ARGS,
804 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
805 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
806 rs->sr_text ? rs->sr_text : "" );
808 Debug( LDAP_DEBUG_ARGS,
809 "send_ldap_result: referral=\"%s\"\n",
810 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
813 assert( !LDAP_API_ERROR( rs->sr_err ) );
814 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
816 if ( rs->sr_err == LDAP_REFERRAL ) {
817 if( op->o_domain_scope ) rs->sr_ref = NULL;
819 if( rs->sr_ref == NULL ) {
820 rs->sr_err = LDAP_NO_SUCH_OBJECT;
821 } else if ( op->o_protocol < LDAP_VERSION3 ) {
822 rs->sr_err = LDAP_PARTIAL_RESULTS;
826 if ( op->o_protocol < LDAP_VERSION3 ) {
827 tmp = v2ref( rs->sr_ref, rs->sr_text );
833 rs->sr_tag = slap_req2res( op->o_tag );
834 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
836 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
837 if ( rs->sr_ref == NULL ) {
838 rs->sr_flags ^= REP_REF_MUSTBEFREED;
839 ber_bvarray_free( oref );
841 oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
844 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
845 if ( op->o_tag == LDAP_REQ_SEARCH ) {
846 Statslog( LDAP_DEBUG_STATS,
847 "%s SEARCH RESULT tag=%lu err=%d nentries=%d text=%s\n",
848 op->o_log_prefix, rs->sr_tag, rs->sr_err,
849 rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
851 Statslog( LDAP_DEBUG_STATS,
852 "%s RESULT tag=%lu err=%d text=%s\n",
853 op->o_log_prefix, rs->sr_tag, rs->sr_err,
854 rs->sr_text ? rs->sr_text : "", 0 );
858 if( tmp != NULL ) ch_free(tmp);
864 send_ldap_sasl( Operation *op, SlapReply *rs )
866 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
868 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
870 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
871 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
873 rs->sr_type = REP_SASL;
874 rs->sr_tag = slap_req2res( op->o_tag );
875 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
877 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
878 Statslog( LDAP_DEBUG_STATS,
879 "%s RESULT tag=%lu err=%d text=%s\n",
880 op->o_log_prefix, rs->sr_tag, rs->sr_err,
881 rs->sr_text ? rs->sr_text : "", 0 );
886 slap_send_ldap_extended( Operation *op, SlapReply *rs )
888 Debug( LDAP_DEBUG_TRACE,
889 "send_ldap_extended: err=%d oid=%s len=%ld\n",
891 rs->sr_rspoid ? rs->sr_rspoid : "",
892 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
894 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
895 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
897 rs->sr_type = REP_EXTENDED;
898 rs->sr_tag = slap_req2res( op->o_tag );
899 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
901 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
902 Statslog( LDAP_DEBUG_STATS,
903 "%s RESULT oid=%s err=%d text=%s\n",
904 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
905 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
910 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
912 Debug( LDAP_DEBUG_TRACE,
913 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
915 rs->sr_rspoid ? rs->sr_rspoid : "",
916 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
918 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
919 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
921 rs->sr_type = REP_INTERMEDIATE;
922 rs->sr_tag = LDAP_RES_INTERMEDIATE;
923 rs->sr_msgid = op->o_msgid;
924 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
925 Statslog( LDAP_DEBUG_STATS2,
926 "%s INTERM oid=%s\n",
928 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
932 #define set_ldap_error( rs, err, text ) do { \
933 (rs)->sr_err = err; (rs)->sr_text = text; } while(0)
938 * LDAP_SUCCESS entry sent
939 * LDAP_OTHER entry not sent (other)
940 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
941 * LDAP_UNAVAILABLE entry not sent (connection closed)
942 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
946 slap_send_search_entry( Operation *op, SlapReply *rs )
948 BerElementBuffer berbuf;
949 BerElement *ber = (BerElement *) &berbuf;
951 int i, j, rc = LDAP_UNAVAILABLE, bytes;
953 AccessControlState acl_state = ACL_STATE_INIT;
955 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
957 /* a_flags: array of flags telling if the i-th element will be
958 * returned or filtered out
959 * e_flags: array of a_flags
961 char **e_flags = NULL;
963 rs->sr_type = REP_SEARCH;
965 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
966 rc = LDAP_SIZELIMIT_EXCEEDED;
970 /* Every 64 entries, check for thread pool pause */
971 if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
972 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
978 /* eventually will loop through generated operational attribute types
979 * currently implemented types include:
980 * entryDN, subschemaSubentry, and hasSubordinates */
981 /* NOTE: moved before overlays callback circling because
982 * they may modify entry and other stuff in rs */
983 /* check for special all operational attributes ("+") type */
984 /* FIXME: maybe we could set this flag at the operation level;
985 * however, in principle the caller of send_search_entry() may
986 * change the attribute list at each call */
987 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
989 rc = backend_operational( op, rs );
994 if ( op->o_callback ) {
995 rc = slap_response_play( op, rs );
996 if ( rc != SLAP_CB_CONTINUE ) {
1001 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
1002 op->o_connid, rs->sr_entry->e_name.bv_val,
1003 op->ors_attrsonly ? " (attrsOnly)" : "" );
1005 attrsonly = op->ors_attrsonly;
1007 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
1008 Debug( LDAP_DEBUG_ACL,
1009 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
1010 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
1012 rc = LDAP_INSUFFICIENT_ACCESS;
1016 if ( op->o_res_ber ) {
1017 /* read back control or LDAP_CONNECTIONLESS */
1018 ber = op->o_res_ber;
1022 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1023 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1025 ber_init2( ber, &bv, LBER_USE_DER );
1026 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1029 #ifdef LDAP_CONNECTIONLESS
1030 if ( op->o_conn && op->o_conn->c_is_udp ) {
1031 /* CONNECTIONLESS */
1032 if ( op->o_protocol == LDAP_VERSION2 ) {
1033 rc = ber_printf(ber, "t{O{" /*}}*/,
1034 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1036 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1037 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1041 if ( op->o_res_ber ) {
1042 /* read back control */
1043 rc = ber_printf( ber, "t{O{" /*}}*/,
1044 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1046 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1047 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1051 Debug( LDAP_DEBUG_ANY,
1052 "send_search_entry: conn %lu ber_printf failed\n",
1053 op->o_connid, 0, 0 );
1055 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1056 set_ldap_error( rs, LDAP_OTHER, "encoding DN error" );
1061 /* check for special all user attributes ("*") type */
1062 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
1064 /* create an array of arrays of flags. Each flag corresponds
1065 * to particular value of attribute and equals 1 if value matches
1066 * to ValuesReturnFilter or 0 if not
1068 if ( op->o_vrFilter != NULL ) {
1072 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1073 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1076 size = i * sizeof(char *) + k;
1079 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
1080 if( e_flags == NULL ) {
1081 Debug( LDAP_DEBUG_ANY,
1082 "send_search_entry: conn %lu slap_sl_calloc failed\n",
1083 op->o_connid, 0, 0 );
1086 set_ldap_error( rs, LDAP_OTHER, "out of memory" );
1089 a_flags = (char *)(e_flags + i);
1090 memset( a_flags, 0, k );
1091 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
1092 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1093 e_flags[i] = a_flags;
1097 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
1099 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
1100 "conn %lu matched values filtering failed\n",
1101 op->o_connid, 0, 0 );
1102 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1103 set_ldap_error( rs, LDAP_OTHER,
1104 "matched values filtering error" );
1111 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
1112 AttributeDescription *desc = a->a_desc;
1115 if ( rs->sr_attrs == NULL ) {
1116 /* all user attrs request, skip operational attributes */
1117 if( is_at_operational( desc->ad_type ) ) {
1122 /* specific attrs requested */
1123 if ( is_at_operational( desc->ad_type ) ) {
1124 /* if not explicitly requested */
1125 if ( !ad_inlist( desc, rs->sr_attrs )) {
1126 /* if not all op attrs requested, skip */
1127 if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
1129 /* if DSA-specific and replicating, skip */
1130 if ( op->o_sync != SLAP_CONTROL_NONE &&
1131 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1135 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1142 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1143 ACL_READ, &acl_state ) )
1145 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1146 "conn %lu access to attribute %s not allowed\n",
1147 op->o_connid, desc->ad_cname.bv_val, 0 );
1151 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1152 Debug( LDAP_DEBUG_ANY,
1153 "send_search_entry: conn %lu ber_printf failed\n",
1154 op->o_connid, 0, 0 );
1156 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1157 set_ldap_error( rs, LDAP_OTHER,
1158 "encoding description error");
1166 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1167 if ( ! access_allowed( op, rs->sr_entry,
1168 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1170 Debug( LDAP_DEBUG_ACL,
1171 "send_search_entry: conn %lu "
1172 "access to attribute %s, value #%d not allowed\n",
1173 op->o_connid, desc->ad_cname.bv_val, i );
1178 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1185 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1186 Debug( LDAP_DEBUG_ANY,
1187 "send_search_entry: conn %lu ber_printf failed\n",
1188 op->o_connid, 0, 0 );
1190 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1191 set_ldap_error( rs, LDAP_OTHER,
1192 "encoding description error");
1197 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1198 Debug( LDAP_DEBUG_ANY,
1199 "send_search_entry: conn %lu "
1200 "ber_printf failed.\n", op->o_connid, 0, 0 );
1202 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1203 set_ldap_error( rs, LDAP_OTHER,
1204 "encoding values error" );
1211 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1212 Debug( LDAP_DEBUG_ANY,
1213 "send_search_entry: conn %lu ber_printf failed\n",
1214 op->o_connid, 0, 0 );
1216 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1217 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1223 /* NOTE: moved before overlays callback circling because
1224 * they may modify entry and other stuff in rs */
1225 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1229 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1230 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1233 size = i * sizeof(char *) + k;
1235 char *a_flags, **tmp;
1238 * Reuse previous memory - we likely need less space
1239 * for operational attributes
1241 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1243 if ( tmp == NULL ) {
1244 Debug( LDAP_DEBUG_ANY,
1245 "send_search_entry: conn %lu "
1246 "not enough memory "
1247 "for matched values filtering\n",
1248 op->o_connid, 0, 0 );
1249 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1250 set_ldap_error( rs, LDAP_OTHER,
1251 "not enough memory for matched values filtering" );
1255 a_flags = (char *)(e_flags + i);
1256 memset( a_flags, 0, k );
1257 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1258 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1259 e_flags[i] = a_flags;
1262 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1265 Debug( LDAP_DEBUG_ANY,
1266 "send_search_entry: conn %lu "
1267 "matched values filtering failed\n",
1268 op->o_connid, 0, 0);
1269 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1270 set_ldap_error( rs, LDAP_OTHER,
1271 "matched values filtering error" );
1278 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1279 AttributeDescription *desc = a->a_desc;
1281 if ( rs->sr_attrs == NULL ) {
1282 /* all user attrs request, skip operational attributes */
1283 if( is_at_operational( desc->ad_type ) ) {
1288 /* specific attrs requested */
1289 if( is_at_operational( desc->ad_type ) ) {
1290 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1291 !ad_inlist( desc, rs->sr_attrs ) )
1295 /* if DSA-specific and replicating, skip */
1296 if ( op->o_sync != SLAP_CONTROL_NONE &&
1297 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1300 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1306 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1307 ACL_READ, &acl_state ) )
1309 Debug( LDAP_DEBUG_ACL,
1310 "send_search_entry: conn %lu "
1311 "access to attribute %s not allowed\n",
1312 op->o_connid, desc->ad_cname.bv_val, 0 );
1317 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1319 Debug( LDAP_DEBUG_ANY,
1320 "send_search_entry: conn %lu "
1321 "ber_printf failed\n", op->o_connid, 0, 0 );
1323 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1324 set_ldap_error( rs, LDAP_OTHER,
1325 "encoding description error" );
1330 if ( ! attrsonly ) {
1331 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1332 if ( ! access_allowed( op, rs->sr_entry,
1333 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1335 Debug( LDAP_DEBUG_ACL,
1336 "send_search_entry: conn %lu "
1337 "access to %s, value %d not allowed\n",
1338 op->o_connid, desc->ad_cname.bv_val, i );
1343 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1347 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1348 Debug( LDAP_DEBUG_ANY,
1349 "send_search_entry: conn %lu ber_printf failed\n",
1350 op->o_connid, 0, 0 );
1352 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1353 set_ldap_error( rs, LDAP_OTHER,
1354 "encoding values error" );
1361 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1362 Debug( LDAP_DEBUG_ANY,
1363 "send_search_entry: conn %lu ber_printf failed\n",
1364 op->o_connid, 0, 0 );
1366 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1367 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1375 slap_sl_free( e_flags, op->o_tmpmemctx );
1379 rc = ber_printf( ber, /*{{*/ "}N}" );
1382 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1386 #ifdef LDAP_CONNECTIONLESS
1387 if( op->o_conn && op->o_conn->c_is_udp ) {
1388 if ( op->o_protocol != LDAP_VERSION2 ) {
1389 rc = ber_printf( ber, /*{*/ "N}" );
1393 if ( op->o_res_ber == NULL ) {
1394 rc = ber_printf( ber, /*{*/ "N}" );
1399 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1401 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1402 set_ldap_error( rs, LDAP_OTHER, "encode entry end error" );
1407 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1408 op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1410 rs_flush_entry( op, rs, NULL );
1412 if ( op->o_res_ber == NULL ) {
1413 bytes = send_ldap_ber( op, ber );
1414 ber_free_buf( ber );
1417 Debug( LDAP_DEBUG_ANY,
1418 "send_search_entry: conn %lu ber write failed.\n",
1419 op->o_connid, 0, 0 );
1421 rc = LDAP_UNAVAILABLE;
1426 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1427 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1428 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1429 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1430 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1433 Debug( LDAP_DEBUG_TRACE,
1434 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1439 if ( op->o_callback ) {
1440 (void)slap_cleanup_play( op, rs );
1444 slap_sl_free( e_flags, op->o_tmpmemctx );
1447 /* FIXME: Can break if rs now contains an extended response */
1448 if ( rs->sr_operational_attrs ) {
1449 attrs_free( rs->sr_operational_attrs );
1450 rs->sr_operational_attrs = NULL;
1452 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1454 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1455 rs_flush_entry( op, rs, NULL );
1457 RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
1460 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1461 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1462 if ( rs->sr_ctrls ) {
1463 slap_free_ctrls( op, rs->sr_ctrls );
1464 rs->sr_ctrls = NULL;
1472 slap_send_search_reference( Operation *op, SlapReply *rs )
1474 BerElementBuffer berbuf;
1475 BerElement *ber = (BerElement *) &berbuf;
1478 char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1480 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1481 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1483 rs->sr_type = REP_SEARCHREF;
1484 if ( op->o_callback ) {
1485 rc = slap_response_play( op, rs );
1486 if ( rc != SLAP_CB_CONTINUE ) {
1491 Debug( LDAP_DEBUG_TRACE,
1492 "=> send_search_reference: dn=\"%s\"\n",
1495 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1496 ad_entry, NULL, ACL_READ, NULL ) )
1498 Debug( LDAP_DEBUG_ACL,
1499 "send_search_reference: access to entry not allowed\n",
1505 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1506 ad_ref, NULL, ACL_READ, NULL ) )
1508 Debug( LDAP_DEBUG_ACL,
1509 "send_search_reference: access "
1510 "to reference not allowed\n",
1516 if( op->o_domain_scope ) {
1517 Debug( LDAP_DEBUG_ANY,
1518 "send_search_reference: domainScope control in (%s)\n",
1524 if( rs->sr_ref == NULL ) {
1525 Debug( LDAP_DEBUG_ANY,
1526 "send_search_reference: null ref in (%s)\n",
1532 if( op->o_protocol < LDAP_VERSION3 ) {
1534 /* save the references for the result */
1535 if( rs->sr_ref[0].bv_val != NULL ) {
1536 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1542 #ifdef LDAP_CONNECTIONLESS
1543 if( op->o_conn && op->o_conn->c_is_udp ) {
1544 ber = op->o_res_ber;
1548 ber_init_w_nullc( ber, LBER_USE_DER );
1549 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1552 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1553 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1556 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1560 rc = ber_printf( ber, /*"{"*/ "N}" );
1564 Debug( LDAP_DEBUG_ANY,
1565 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1567 #ifdef LDAP_CONNECTIONLESS
1568 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1570 ber_free_buf( ber );
1571 set_ldap_error( rs, LDAP_OTHER, "encode DN error" );
1576 rs_flush_entry( op, rs, NULL );
1578 #ifdef LDAP_CONNECTIONLESS
1579 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1581 bytes = send_ldap_ber( op, ber );
1582 ber_free_buf( ber );
1585 rc = LDAP_UNAVAILABLE;
1587 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1588 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1589 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1590 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1591 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1593 #ifdef LDAP_CONNECTIONLESS
1596 if ( rs->sr_ref != NULL ) {
1599 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1600 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1601 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1606 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1607 op->o_log_prefix, 0, 0, 0, 0 );
1610 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1614 rs_flush_entry( op, rs, NULL );
1617 if ( op->o_callback ) {
1618 (void)slap_cleanup_play( op, rs );
1621 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1622 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1623 if ( rs->sr_ctrls ) {
1624 slap_free_ctrls( op, rs->sr_ctrls );
1625 rs->sr_ctrls = NULL;
1642 *code = LDAP_SUCCESS;
1646 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1647 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1654 while ( (s = strchr( s, '\n' )) != NULL ) {
1659 if ( (c = strchr( s, ':' )) != NULL ) {
1663 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1668 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1674 while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1675 if ( c[ 0 ] == '\0' ) {
1676 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1682 retcode = strtol( c, &next, 10 );
1683 if ( next == NULL || next == c ) {
1684 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1690 while ( isspace( (unsigned char) next[ 0 ] ) && next[ 0 ] != '\n' )
1692 if ( next[ 0 ] != '\0' && next[ 0 ] != '\n' ) {
1693 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1699 /* FIXME: what if it's larger than max int? */
1700 *code = (int)retcode;
1702 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1706 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1711 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1721 int slap_read_controls(
1725 const struct berval *oid,
1726 LDAPControl **ctrl )
1730 BerElementBuffer berbuf;
1731 BerElement *ber = (BerElement *) &berbuf;
1735 Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1736 op->o_log_prefix, oid->bv_val, e->e_dn );
1739 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1740 op->o_preread_attrs : op->o_postread_attrs;
1742 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1743 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1745 ber_init2( ber, &bv, LBER_USE_DER );
1746 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1748 /* create new operation */
1750 /* FIXME: o_bd needed for ACL */
1751 myop.o_bd = op->o_bd;
1752 myop.o_res_ber = ber;
1753 myop.o_callback = NULL;
1754 myop.ors_slimit = 1;
1755 myop.ors_attrsonly = 0;
1757 rc = slap_send_search_entry( &myop, rs );
1760 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1762 if( rc == -1 ) return LDAP_OTHER;
1764 c.ldctl_oid = oid->bv_val;
1765 c.ldctl_iscritical = 0;
1767 if ( *ctrl == NULL ) {
1769 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1771 /* retry: free previous try */
1772 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1776 return LDAP_SUCCESS;
1779 /* Map API errors to protocol errors... */
1781 slap_map_api2result( SlapReply *rs )
1783 switch(rs->sr_err) {
1784 case LDAP_SERVER_DOWN:
1785 return LDAP_UNAVAILABLE;
1786 case LDAP_LOCAL_ERROR:
1788 case LDAP_ENCODING_ERROR:
1789 case LDAP_DECODING_ERROR:
1790 return LDAP_PROTOCOL_ERROR;
1792 return LDAP_UNAVAILABLE;
1793 case LDAP_AUTH_UNKNOWN:
1794 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1795 case LDAP_FILTER_ERROR:
1796 rs->sr_text = "Filter error";
1798 case LDAP_USER_CANCELLED:
1799 rs->sr_text = "User cancelled";
1801 case LDAP_PARAM_ERROR:
1802 return LDAP_PROTOCOL_ERROR;
1803 case LDAP_NO_MEMORY:
1805 case LDAP_CONNECT_ERROR:
1806 return LDAP_UNAVAILABLE;
1807 case LDAP_NOT_SUPPORTED:
1808 return LDAP_UNWILLING_TO_PERFORM;
1809 case LDAP_CONTROL_NOT_FOUND:
1810 return LDAP_PROTOCOL_ERROR;
1811 case LDAP_NO_RESULTS_RETURNED:
1812 return LDAP_NO_SUCH_OBJECT;
1813 case LDAP_MORE_RESULTS_TO_RETURN:
1814 rs->sr_text = "More results to return";
1816 case LDAP_CLIENT_LOOP:
1817 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1818 return LDAP_LOOP_DETECT;
1820 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1827 slap_attr_flags( AttributeName *an )
1829 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1832 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1835 flags |= an_find( an, slap_bv_all_operational_attrs )
1836 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1837 flags |= an_find( an, slap_bv_all_user_attrs )
1838 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;