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;
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 /* Check for any callbacks that want to be informed about being blocked
283 * on output. These callbacks are expected to leave the callback list
284 * unmodified. Their result is ignored.
290 slap_callback *sc = op->o_callback;
292 for ( ; sc; sc = sc->sc_next ) {
293 if ( sc->sc_writewait )
294 sc->sc_writewait( op, sc );
298 static long send_ldap_ber(
302 Connection *conn = op->o_conn;
306 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
308 /* write only one pdu at a time - wait til it's our turn */
309 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
310 if (( op->o_abandon && !op->o_cancel ) || !connection_valid( conn ) ||
311 conn->c_writers < 0 ) {
312 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
318 while ( conn->c_writers > 0 && conn->c_writing ) {
319 ldap_pvt_thread_cond_wait( &conn->c_write1_cv, &conn->c_write1_mutex );
322 /* connection was closed under us */
323 if ( conn->c_writers < 0 ) {
324 /* we're the last waiter, let the closer continue */
325 if ( conn->c_writers == -1 )
326 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
328 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
339 if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
347 * we got an error. if it's ewouldblock, we need to
348 * wait on the socket being writable. otherwise, figure
349 * it's a hard error and return.
352 Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
353 err, sock_errstr(err), 0 );
355 if ( err != EWOULDBLOCK && err != EAGAIN ) {
358 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
359 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
360 connection_closing( conn, "connection lost on write" );
362 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
366 /* wait for socket to be write-ready */
367 slap_writewait_play( op );
368 ldap_pvt_thread_mutex_lock( &conn->c_write2_mutex );
369 conn->c_writewaiter = 1;
370 slapd_set_write( conn->c_sd, 2 );
372 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
373 ldap_pvt_thread_pool_idle( &connection_pool );
374 ldap_pvt_thread_cond_wait( &conn->c_write2_cv, &conn->c_write2_mutex );
375 conn->c_writewaiter = 0;
376 ldap_pvt_thread_mutex_unlock( &conn->c_write2_mutex );
377 ldap_pvt_thread_pool_unidle( &connection_pool );
378 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
379 if ( conn->c_writers < 0 ) {
386 if ( conn->c_writers < 0 ) {
388 if ( !conn->c_writers )
389 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
392 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
394 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
400 send_ldap_control( BerElement *ber, LDAPControl *c )
406 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
408 if( c->ldctl_iscritical ) {
409 rc = ber_printf( ber, "b",
410 (ber_int_t) c->ldctl_iscritical ) ;
411 if( rc == -1 ) return rc;
414 if( c->ldctl_value.bv_val != NULL ) {
415 rc = ber_printf( ber, "O", &c->ldctl_value );
416 if( rc == -1 ) return rc;
419 rc = ber_printf( ber, /*{*/"N}" );
420 if( rc == -1 ) return rc;
426 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
433 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
434 if( rc == -1 ) return rc;
436 for( ; *c != NULL; c++) {
437 rc = send_ldap_control( ber, *c );
438 if( rc == -1 ) return rc;
441 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
442 /* this is a hack to avoid having to modify op->s_ctrls */
443 if( o->o_sortedresults ) {
444 BerElementBuffer berbuf;
445 BerElement *sber = (BerElement *) &berbuf;
447 BER_BVZERO( &sorted.ldctl_value );
448 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
449 sorted.ldctl_iscritical = 0;
451 ber_init2( sber, NULL, LBER_USE_DER );
453 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
455 if( ber_flatten2( sber, &sorted.ldctl_value, 0 ) == -1 ) {
459 (void) ber_free_buf( sber );
461 rc = send_ldap_control( ber, &sorted );
462 if( rc == -1 ) return rc;
466 rc = ber_printf( ber, /*{*/"N}" );
472 * slap_response_play()
474 * plays the callback list; rationale: a callback can
475 * - remove itself from the list, by setting op->o_callback = NULL;
476 * malloc()'ed callbacks should free themselves from inside the
477 * sc_response() function.
478 * - replace itself with another (list of) callback(s), by setting
479 * op->o_callback = a new (list of) callback(s); in this case, it
480 * is the callback's responsibility to to append existing subsequent
481 * callbacks to the end of the list that is passed to the sc_response()
483 * - modify the list of subsequent callbacks by modifying the value
484 * of the sc_next field from inside the sc_response() function; this
485 * case does not require any handling from inside slap_response_play()
487 * To stop execution of the playlist, the sc_response() function must return
488 * a value different from SLAP_SC_CONTINUE.
490 * The same applies to slap_cleanup_play(); only, there is no means to stop
491 * execution of the playlist, since all cleanup functions must be called.
500 slap_callback *sc = op->o_callback, **scp;
502 rc = SLAP_CB_CONTINUE;
503 for ( scp = ≻ *scp; ) {
504 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
506 op->o_callback = *scp;
507 if ( op->o_callback->sc_response ) {
508 rc = op->o_callback->sc_response( op, rs );
509 if ( op->o_callback == NULL ) {
510 /* the callback has been removed;
515 } else if ( op->o_callback != *scp ) {
516 /* a new callback has been inserted
517 * in place of the existing one; repair the list */
518 *scp = op->o_callback;
521 if ( rc != SLAP_CB_CONTINUE ) break;
535 slap_callback *sc = op->o_callback, **scp;
537 for ( scp = ≻ *scp; ) {
538 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
540 op->o_callback = *scp;
541 if ( op->o_callback->sc_cleanup ) {
542 (void)op->o_callback->sc_cleanup( op, rs );
543 if ( op->o_callback == NULL ) {
544 /* the callback has been removed;
549 } else if ( op->o_callback != *scp ) {
550 /* a new callback has been inserted
551 * after the existing one; repair the list */
552 /* a new callback has been inserted
553 * in place of the existing one; repair the list */
554 *scp = op->o_callback;
557 /* don't care about the result; do all cleanup */
571 BerElementBuffer berbuf;
572 BerElement *ber = (BerElement *) &berbuf;
573 int rc = LDAP_SUCCESS;
576 /* op was actually aborted, bypass everything if client didn't Cancel */
577 if (( rs->sr_err == SLAPD_ABANDON ) && !op->o_cancel ) {
582 if ( op->o_callback ) {
583 rc = slap_response_play( op, rs );
584 if ( rc != SLAP_CB_CONTINUE ) {
589 /* op completed, connection aborted, bypass sending response */
590 if ( op->o_abandon && !op->o_cancel ) {
595 #ifdef LDAP_CONNECTIONLESS
596 if (op->o_conn && op->o_conn->c_is_udp)
601 ber_init_w_nullc( ber, LBER_USE_DER );
602 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
606 if ( rc == SLAPD_ABANDON && op->o_cancel )
609 Debug( LDAP_DEBUG_TRACE,
610 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
611 rs->sr_msgid, rs->sr_tag, rc );
614 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
615 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
619 #ifdef LDAP_CONNECTIONLESS
620 if (op->o_conn && op->o_conn->c_is_udp &&
621 op->o_protocol == LDAP_VERSION2 )
623 rc = ber_printf( ber, "t{ess" /*"}"*/,
625 rs->sr_matched == NULL ? "" : rs->sr_matched,
626 rs->sr_text == NULL ? "" : rs->sr_text );
629 if ( rs->sr_type == REP_INTERMEDIATE ) {
630 rc = ber_printf( ber, "{it{" /*"}}"*/,
631 rs->sr_msgid, rs->sr_tag );
634 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
635 rs->sr_msgid, rs->sr_tag, rc,
636 rs->sr_matched == NULL ? "" : rs->sr_matched,
637 rs->sr_text == NULL ? "" : rs->sr_text );
641 if ( rs->sr_ref != NULL ) {
642 assert( rs->sr_err == LDAP_REFERRAL );
643 rc = ber_printf( ber, "t{W}",
644 LDAP_TAG_REFERRAL, rs->sr_ref );
646 assert( rs->sr_err != LDAP_REFERRAL );
650 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
651 rc = ber_printf( ber, "tO",
652 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
656 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
658 if ( rs->sr_rspoid != NULL ) {
659 rc = ber_printf( ber, "ts",
660 rs->sr_type == REP_EXTENDED
661 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
664 if( rc != -1 && rs->sr_rspdata != NULL ) {
665 rc = ber_printf( ber, "tO",
666 rs->sr_type == REP_EXTENDED
667 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
673 rc = ber_printf( ber, /*"{"*/ "N}" );
677 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
681 rc = ber_printf( ber, /*"{"*/ "N}" );
684 #ifdef LDAP_CONNECTIONLESS
685 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
688 rc = ber_printf( ber, /*"{"*/ "N}" );
693 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
695 #ifdef LDAP_CONNECTIONLESS
696 if (!op->o_conn || op->o_conn->c_is_udp == 0)
705 bytes = send_ldap_ber( op, ber );
706 #ifdef LDAP_CONNECTIONLESS
707 if (!op->o_conn || op->o_conn->c_is_udp == 0)
714 Debug( LDAP_DEBUG_ANY,
715 "send_ldap_response: ber write failed\n",
721 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
722 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
723 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
724 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
727 /* Tell caller that we did this for real, as opposed to being
728 * overridden by a callback
730 rc = SLAP_CB_CONTINUE;
733 if ( op->o_callback ) {
734 (void)slap_cleanup_play( op, rs );
737 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
738 rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
739 if ( rs->sr_matched ) {
740 free( (char *)rs->sr_matched );
741 rs->sr_matched = NULL;
745 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
746 rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
748 ber_bvarray_free( rs->sr_ref );
753 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
754 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
755 if ( rs->sr_ctrls ) {
756 slap_free_ctrls( op, rs->sr_ctrls );
766 send_ldap_disconnect( Operation *op, SlapReply *rs )
768 #define LDAP_UNSOLICITED_ERROR(e) \
769 ( (e) == LDAP_PROTOCOL_ERROR \
770 || (e) == LDAP_STRONG_AUTH_REQUIRED \
771 || (e) == LDAP_UNAVAILABLE )
773 Debug( LDAP_DEBUG_TRACE,
774 "send_ldap_disconnect %d:%s\n",
775 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
776 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
778 /* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
779 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
780 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
782 rs->sr_type = REP_EXTENDED;
783 rs->sr_rspdata = NULL;
785 if ( op->o_protocol < LDAP_VERSION3 ) {
786 rs->sr_rspoid = NULL;
787 rs->sr_tag = slap_req2res( op->o_tag );
788 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
791 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
792 rs->sr_tag = LDAP_RES_EXTENDED;
793 rs->sr_msgid = LDAP_RES_UNSOLICITED;
796 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
797 Statslog( LDAP_DEBUG_STATS,
798 "%s DISCONNECT tag=%lu err=%d text=%s\n",
799 op->o_log_prefix, rs->sr_tag, rs->sr_err,
800 rs->sr_text ? rs->sr_text : "", 0 );
805 slap_send_ldap_result( Operation *op, SlapReply *rs )
808 const char *otext = rs->sr_text;
809 BerVarray oref = rs->sr_ref;
811 rs->sr_type = REP_RESULT;
813 /* Propagate Abandons so that cleanup callbacks can be processed */
814 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
817 Debug( LDAP_DEBUG_TRACE,
818 "send_ldap_result: %s p=%d\n",
819 op->o_log_prefix, op->o_protocol, 0 );
820 Debug( LDAP_DEBUG_ARGS,
821 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
822 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
823 rs->sr_text ? rs->sr_text : "" );
825 Debug( LDAP_DEBUG_ARGS,
826 "send_ldap_result: referral=\"%s\"\n",
827 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
830 assert( !LDAP_API_ERROR( rs->sr_err ) );
831 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
833 if ( rs->sr_err == LDAP_REFERRAL ) {
834 if( op->o_domain_scope ) rs->sr_ref = NULL;
836 if( rs->sr_ref == NULL ) {
837 rs->sr_err = LDAP_NO_SUCH_OBJECT;
838 } else if ( op->o_protocol < LDAP_VERSION3 ) {
839 rs->sr_err = LDAP_PARTIAL_RESULTS;
843 if ( op->o_protocol < LDAP_VERSION3 ) {
844 tmp = v2ref( rs->sr_ref, rs->sr_text );
850 rs->sr_tag = slap_req2res( op->o_tag );
851 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
853 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
854 if ( rs->sr_ref == NULL ) {
855 rs->sr_flags ^= REP_REF_MUSTBEFREED;
856 ber_bvarray_free( oref );
858 oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
861 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
862 if ( op->o_tag == LDAP_REQ_SEARCH ) {
863 Statslog( LDAP_DEBUG_STATS,
864 "%s SEARCH RESULT tag=%lu err=%d nentries=%d text=%s\n",
865 op->o_log_prefix, rs->sr_tag, rs->sr_err,
866 rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
868 Statslog( LDAP_DEBUG_STATS,
869 "%s RESULT tag=%lu err=%d text=%s\n",
870 op->o_log_prefix, rs->sr_tag, rs->sr_err,
871 rs->sr_text ? rs->sr_text : "", 0 );
875 if( tmp != NULL ) ch_free(tmp);
881 send_ldap_sasl( Operation *op, SlapReply *rs )
883 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
885 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
887 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
888 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
890 rs->sr_type = REP_SASL;
891 rs->sr_tag = slap_req2res( op->o_tag );
892 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
894 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
895 Statslog( LDAP_DEBUG_STATS,
896 "%s RESULT tag=%lu err=%d text=%s\n",
897 op->o_log_prefix, rs->sr_tag, rs->sr_err,
898 rs->sr_text ? rs->sr_text : "", 0 );
903 slap_send_ldap_extended( Operation *op, SlapReply *rs )
905 Debug( LDAP_DEBUG_TRACE,
906 "send_ldap_extended: err=%d oid=%s len=%ld\n",
908 rs->sr_rspoid ? rs->sr_rspoid : "",
909 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
911 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
912 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
914 rs->sr_type = REP_EXTENDED;
915 rs->sr_tag = slap_req2res( op->o_tag );
916 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
918 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
919 Statslog( LDAP_DEBUG_STATS,
920 "%s RESULT oid=%s err=%d text=%s\n",
921 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
922 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
927 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
929 Debug( LDAP_DEBUG_TRACE,
930 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
932 rs->sr_rspoid ? rs->sr_rspoid : "",
933 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
935 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
936 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
938 rs->sr_type = REP_INTERMEDIATE;
939 rs->sr_tag = LDAP_RES_INTERMEDIATE;
940 rs->sr_msgid = op->o_msgid;
941 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
942 Statslog( LDAP_DEBUG_STATS2,
943 "%s INTERM oid=%s\n",
945 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
949 #define set_ldap_error( rs, err, text ) do { \
950 (rs)->sr_err = err; (rs)->sr_text = text; } while(0)
955 * LDAP_SUCCESS entry sent
956 * LDAP_OTHER entry not sent (other)
957 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
958 * LDAP_UNAVAILABLE entry not sent (connection closed)
959 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
963 slap_send_search_entry( Operation *op, SlapReply *rs )
965 BerElementBuffer berbuf;
966 BerElement *ber = (BerElement *) &berbuf;
968 int i, j, rc = LDAP_UNAVAILABLE, bytes;
970 AccessControlState acl_state = ACL_STATE_INIT;
972 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
974 /* a_flags: array of flags telling if the i-th element will be
975 * returned or filtered out
976 * e_flags: array of a_flags
978 char **e_flags = NULL;
980 rs->sr_type = REP_SEARCH;
982 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
983 rc = LDAP_SIZELIMIT_EXCEEDED;
987 /* Every 64 entries, check for thread pool pause */
988 if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
989 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
995 /* eventually will loop through generated operational attribute types
996 * currently implemented types include:
997 * entryDN, subschemaSubentry, and hasSubordinates */
998 /* NOTE: moved before overlays callback circling because
999 * they may modify entry and other stuff in rs */
1000 /* check for special all operational attributes ("+") type */
1001 /* FIXME: maybe we could set this flag at the operation level;
1002 * however, in principle the caller of send_search_entry() may
1003 * change the attribute list at each call */
1004 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
1006 rc = backend_operational( op, rs );
1011 if ( op->o_callback ) {
1012 rc = slap_response_play( op, rs );
1013 if ( rc != SLAP_CB_CONTINUE ) {
1018 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
1019 op->o_connid, rs->sr_entry->e_name.bv_val,
1020 op->ors_attrsonly ? " (attrsOnly)" : "" );
1022 attrsonly = op->ors_attrsonly;
1024 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
1025 Debug( LDAP_DEBUG_ACL,
1026 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
1027 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
1029 rc = LDAP_INSUFFICIENT_ACCESS;
1033 if ( op->o_res_ber ) {
1034 /* read back control or LDAP_CONNECTIONLESS */
1035 ber = op->o_res_ber;
1039 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1040 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1042 ber_init2( ber, &bv, LBER_USE_DER );
1043 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1046 #ifdef LDAP_CONNECTIONLESS
1047 if ( op->o_conn && op->o_conn->c_is_udp ) {
1048 /* CONNECTIONLESS */
1049 if ( op->o_protocol == LDAP_VERSION2 ) {
1050 rc = ber_printf(ber, "t{O{" /*}}*/,
1051 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1053 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1054 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1058 if ( op->o_res_ber ) {
1059 /* read back control */
1060 rc = ber_printf( ber, "t{O{" /*}}*/,
1061 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1063 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1064 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1068 Debug( LDAP_DEBUG_ANY,
1069 "send_search_entry: conn %lu ber_printf failed\n",
1070 op->o_connid, 0, 0 );
1072 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1073 set_ldap_error( rs, LDAP_OTHER, "encoding DN error" );
1078 /* check for special all user attributes ("*") type */
1079 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
1081 /* create an array of arrays of flags. Each flag corresponds
1082 * to particular value of attribute and equals 1 if value matches
1083 * to ValuesReturnFilter or 0 if not
1085 if ( op->o_vrFilter != NULL ) {
1089 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1090 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1093 size = i * sizeof(char *) + k;
1096 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
1097 if( e_flags == NULL ) {
1098 Debug( LDAP_DEBUG_ANY,
1099 "send_search_entry: conn %lu slap_sl_calloc failed\n",
1100 op->o_connid, 0, 0 );
1103 set_ldap_error( rs, LDAP_OTHER, "out of memory" );
1106 a_flags = (char *)(e_flags + i);
1107 memset( a_flags, 0, k );
1108 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
1109 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1110 e_flags[i] = a_flags;
1114 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
1116 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
1117 "conn %lu matched values filtering failed\n",
1118 op->o_connid, 0, 0 );
1119 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1120 set_ldap_error( rs, LDAP_OTHER,
1121 "matched values filtering error" );
1128 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
1129 AttributeDescription *desc = a->a_desc;
1132 if ( rs->sr_attrs == NULL ) {
1133 /* all user attrs request, skip operational attributes */
1134 if( is_at_operational( desc->ad_type ) ) {
1139 /* specific attrs requested */
1140 if ( is_at_operational( desc->ad_type ) ) {
1141 /* if not explicitly requested */
1142 if ( !ad_inlist( desc, rs->sr_attrs )) {
1143 /* if not all op attrs requested, skip */
1144 if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
1146 /* if DSA-specific and replicating, skip */
1147 if ( op->o_sync != SLAP_CONTROL_NONE &&
1148 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1152 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1159 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1160 ACL_READ, &acl_state ) )
1162 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1163 "conn %lu access to attribute %s not allowed\n",
1164 op->o_connid, desc->ad_cname.bv_val, 0 );
1168 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1169 Debug( LDAP_DEBUG_ANY,
1170 "send_search_entry: conn %lu ber_printf failed\n",
1171 op->o_connid, 0, 0 );
1173 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1174 set_ldap_error( rs, LDAP_OTHER,
1175 "encoding description error");
1183 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1184 if ( ! access_allowed( op, rs->sr_entry,
1185 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1187 Debug( LDAP_DEBUG_ACL,
1188 "send_search_entry: conn %lu "
1189 "access to attribute %s, value #%d not allowed\n",
1190 op->o_connid, desc->ad_cname.bv_val, i );
1195 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1202 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1203 Debug( LDAP_DEBUG_ANY,
1204 "send_search_entry: conn %lu ber_printf failed\n",
1205 op->o_connid, 0, 0 );
1207 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1208 set_ldap_error( rs, LDAP_OTHER,
1209 "encoding description error");
1214 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1215 Debug( LDAP_DEBUG_ANY,
1216 "send_search_entry: conn %lu "
1217 "ber_printf failed.\n", op->o_connid, 0, 0 );
1219 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1220 set_ldap_error( rs, LDAP_OTHER,
1221 "encoding values error" );
1228 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1229 Debug( LDAP_DEBUG_ANY,
1230 "send_search_entry: conn %lu ber_printf failed\n",
1231 op->o_connid, 0, 0 );
1233 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1234 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1240 /* NOTE: moved before overlays callback circling because
1241 * they may modify entry and other stuff in rs */
1242 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1246 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1247 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1250 size = i * sizeof(char *) + k;
1252 char *a_flags, **tmp;
1255 * Reuse previous memory - we likely need less space
1256 * for operational attributes
1258 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1260 if ( tmp == NULL ) {
1261 Debug( LDAP_DEBUG_ANY,
1262 "send_search_entry: conn %lu "
1263 "not enough memory "
1264 "for matched values filtering\n",
1265 op->o_connid, 0, 0 );
1266 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1267 set_ldap_error( rs, LDAP_OTHER,
1268 "not enough memory for matched values filtering" );
1272 a_flags = (char *)(e_flags + i);
1273 memset( a_flags, 0, k );
1274 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1275 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1276 e_flags[i] = a_flags;
1279 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1282 Debug( LDAP_DEBUG_ANY,
1283 "send_search_entry: conn %lu "
1284 "matched values filtering failed\n",
1285 op->o_connid, 0, 0);
1286 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1287 set_ldap_error( rs, LDAP_OTHER,
1288 "matched values filtering error" );
1295 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1296 AttributeDescription *desc = a->a_desc;
1298 if ( rs->sr_attrs == NULL ) {
1299 /* all user attrs request, skip operational attributes */
1300 if( is_at_operational( desc->ad_type ) ) {
1305 /* specific attrs requested */
1306 if( is_at_operational( desc->ad_type ) ) {
1307 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1308 !ad_inlist( desc, rs->sr_attrs ) )
1312 /* if DSA-specific and replicating, skip */
1313 if ( op->o_sync != SLAP_CONTROL_NONE &&
1314 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1317 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1323 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1324 ACL_READ, &acl_state ) )
1326 Debug( LDAP_DEBUG_ACL,
1327 "send_search_entry: conn %lu "
1328 "access to attribute %s not allowed\n",
1329 op->o_connid, desc->ad_cname.bv_val, 0 );
1334 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1336 Debug( LDAP_DEBUG_ANY,
1337 "send_search_entry: conn %lu "
1338 "ber_printf failed\n", op->o_connid, 0, 0 );
1340 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1341 set_ldap_error( rs, LDAP_OTHER,
1342 "encoding description error" );
1347 if ( ! attrsonly ) {
1348 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1349 if ( ! access_allowed( op, rs->sr_entry,
1350 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1352 Debug( LDAP_DEBUG_ACL,
1353 "send_search_entry: conn %lu "
1354 "access to %s, value %d not allowed\n",
1355 op->o_connid, desc->ad_cname.bv_val, i );
1360 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1364 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1365 Debug( LDAP_DEBUG_ANY,
1366 "send_search_entry: conn %lu ber_printf failed\n",
1367 op->o_connid, 0, 0 );
1369 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1370 set_ldap_error( rs, LDAP_OTHER,
1371 "encoding values error" );
1378 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1379 Debug( LDAP_DEBUG_ANY,
1380 "send_search_entry: conn %lu ber_printf failed\n",
1381 op->o_connid, 0, 0 );
1383 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1384 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1392 slap_sl_free( e_flags, op->o_tmpmemctx );
1396 rc = ber_printf( ber, /*{{*/ "}N}" );
1399 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1403 #ifdef LDAP_CONNECTIONLESS
1404 if( op->o_conn && op->o_conn->c_is_udp ) {
1405 if ( op->o_protocol != LDAP_VERSION2 ) {
1406 rc = ber_printf( ber, /*{*/ "N}" );
1410 if ( op->o_res_ber == NULL ) {
1411 rc = ber_printf( ber, /*{*/ "N}" );
1416 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1418 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1419 set_ldap_error( rs, LDAP_OTHER, "encode entry end error" );
1424 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1425 op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1427 rs_flush_entry( op, rs, NULL );
1429 if ( op->o_res_ber == NULL ) {
1430 bytes = send_ldap_ber( op, ber );
1431 ber_free_buf( ber );
1434 Debug( LDAP_DEBUG_ANY,
1435 "send_search_entry: conn %lu ber write failed.\n",
1436 op->o_connid, 0, 0 );
1438 rc = LDAP_UNAVAILABLE;
1443 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1444 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1445 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1446 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1447 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1450 Debug( LDAP_DEBUG_TRACE,
1451 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1456 if ( op->o_callback ) {
1457 (void)slap_cleanup_play( op, rs );
1461 slap_sl_free( e_flags, op->o_tmpmemctx );
1464 /* FIXME: Can break if rs now contains an extended response */
1465 if ( rs->sr_operational_attrs ) {
1466 attrs_free( rs->sr_operational_attrs );
1467 rs->sr_operational_attrs = NULL;
1469 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1471 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1472 rs_flush_entry( op, rs, NULL );
1474 RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
1477 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1478 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1479 if ( rs->sr_ctrls ) {
1480 slap_free_ctrls( op, rs->sr_ctrls );
1481 rs->sr_ctrls = NULL;
1489 slap_send_search_reference( Operation *op, SlapReply *rs )
1491 BerElementBuffer berbuf;
1492 BerElement *ber = (BerElement *) &berbuf;
1495 char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1497 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1498 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1500 rs->sr_type = REP_SEARCHREF;
1501 if ( op->o_callback ) {
1502 rc = slap_response_play( op, rs );
1503 if ( rc != SLAP_CB_CONTINUE ) {
1508 Debug( LDAP_DEBUG_TRACE,
1509 "=> send_search_reference: dn=\"%s\"\n",
1512 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1513 ad_entry, NULL, ACL_READ, NULL ) )
1515 Debug( LDAP_DEBUG_ACL,
1516 "send_search_reference: access to entry not allowed\n",
1522 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1523 ad_ref, NULL, ACL_READ, NULL ) )
1525 Debug( LDAP_DEBUG_ACL,
1526 "send_search_reference: access "
1527 "to reference not allowed\n",
1533 if( op->o_domain_scope ) {
1534 Debug( LDAP_DEBUG_ANY,
1535 "send_search_reference: domainScope control in (%s)\n",
1541 if( rs->sr_ref == NULL ) {
1542 Debug( LDAP_DEBUG_ANY,
1543 "send_search_reference: null ref in (%s)\n",
1549 if( op->o_protocol < LDAP_VERSION3 ) {
1551 /* save the references for the result */
1552 if( rs->sr_ref[0].bv_val != NULL ) {
1553 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1559 #ifdef LDAP_CONNECTIONLESS
1560 if( op->o_conn && op->o_conn->c_is_udp ) {
1561 ber = op->o_res_ber;
1565 ber_init_w_nullc( ber, LBER_USE_DER );
1566 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1569 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1570 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1573 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1577 rc = ber_printf( ber, /*"{"*/ "N}" );
1581 Debug( LDAP_DEBUG_ANY,
1582 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1584 #ifdef LDAP_CONNECTIONLESS
1585 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1587 ber_free_buf( ber );
1588 set_ldap_error( rs, LDAP_OTHER, "encode DN error" );
1593 rs_flush_entry( op, rs, NULL );
1595 #ifdef LDAP_CONNECTIONLESS
1596 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1598 bytes = send_ldap_ber( op, ber );
1599 ber_free_buf( ber );
1602 rc = LDAP_UNAVAILABLE;
1604 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1605 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1606 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1607 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1608 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1610 #ifdef LDAP_CONNECTIONLESS
1613 if ( rs->sr_ref != NULL ) {
1616 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1617 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1618 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1623 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1624 op->o_log_prefix, 0, 0, 0, 0 );
1627 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1631 rs_flush_entry( op, rs, NULL );
1634 if ( op->o_callback ) {
1635 (void)slap_cleanup_play( op, rs );
1638 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1639 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1640 if ( rs->sr_ctrls ) {
1641 slap_free_ctrls( op, rs->sr_ctrls );
1642 rs->sr_ctrls = NULL;
1659 *code = LDAP_SUCCESS;
1663 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1664 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1671 while ( (s = strchr( s, '\n' )) != NULL ) {
1676 if ( (c = strchr( s, ':' )) != NULL ) {
1680 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1685 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1691 while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1692 if ( c[ 0 ] == '\0' ) {
1693 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1699 retcode = strtol( c, &next, 10 );
1700 if ( next == NULL || next == c ) {
1701 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1707 while ( isspace( (unsigned char) next[ 0 ] ) && next[ 0 ] != '\n' )
1709 if ( next[ 0 ] != '\0' && next[ 0 ] != '\n' ) {
1710 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1716 /* FIXME: what if it's larger than max int? */
1717 *code = (int)retcode;
1719 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1723 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1728 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1738 int slap_read_controls(
1742 const struct berval *oid,
1743 LDAPControl **ctrl )
1747 BerElementBuffer berbuf;
1748 BerElement *ber = (BerElement *) &berbuf;
1752 Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1753 op->o_log_prefix, oid->bv_val, e->e_dn );
1756 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1757 op->o_preread_attrs : op->o_postread_attrs;
1759 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1760 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1762 ber_init2( ber, &bv, LBER_USE_DER );
1763 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1765 /* create new operation */
1767 /* FIXME: o_bd needed for ACL */
1768 myop.o_bd = op->o_bd;
1769 myop.o_res_ber = ber;
1770 myop.o_callback = NULL;
1771 myop.ors_slimit = 1;
1772 myop.ors_attrsonly = 0;
1774 rc = slap_send_search_entry( &myop, rs );
1777 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1779 if( rc == -1 ) return LDAP_OTHER;
1781 c.ldctl_oid = oid->bv_val;
1782 c.ldctl_iscritical = 0;
1784 if ( *ctrl == NULL ) {
1786 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1788 /* retry: free previous try */
1789 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1793 return LDAP_SUCCESS;
1796 /* Map API errors to protocol errors... */
1798 slap_map_api2result( SlapReply *rs )
1800 switch(rs->sr_err) {
1801 case LDAP_SERVER_DOWN:
1802 return LDAP_UNAVAILABLE;
1803 case LDAP_LOCAL_ERROR:
1805 case LDAP_ENCODING_ERROR:
1806 case LDAP_DECODING_ERROR:
1807 return LDAP_PROTOCOL_ERROR;
1809 return LDAP_UNAVAILABLE;
1810 case LDAP_AUTH_UNKNOWN:
1811 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1812 case LDAP_FILTER_ERROR:
1813 rs->sr_text = "Filter error";
1815 case LDAP_USER_CANCELLED:
1816 rs->sr_text = "User cancelled";
1818 case LDAP_PARAM_ERROR:
1819 return LDAP_PROTOCOL_ERROR;
1820 case LDAP_NO_MEMORY:
1822 case LDAP_CONNECT_ERROR:
1823 return LDAP_UNAVAILABLE;
1824 case LDAP_NOT_SUPPORTED:
1825 return LDAP_UNWILLING_TO_PERFORM;
1826 case LDAP_CONTROL_NOT_FOUND:
1827 return LDAP_PROTOCOL_ERROR;
1828 case LDAP_NO_RESULTS_RETURNED:
1829 return LDAP_NO_SUCH_OBJECT;
1830 case LDAP_MORE_RESULTS_TO_RETURN:
1831 rs->sr_text = "More results to return";
1833 case LDAP_CLIENT_LOOP:
1834 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1835 return LDAP_LOOP_DETECT;
1837 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1844 slap_attr_flags( AttributeName *an )
1846 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1849 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1852 flags |= an_find( an, slap_bv_all_operational_attrs )
1853 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1854 flags |= an_find( an, slap_bv_all_user_attrs )
1855 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;