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-2011 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 /* lock the connection */
324 if ( ldap_pvt_thread_mutex_trylock( &conn->c_mutex )) {
325 if ( !connection_valid(conn)) {
329 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
330 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
331 if ( conn->c_writers < 0 ) {
338 if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
339 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
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 connection_closing( conn, "connection lost on write" );
361 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
365 /* wait for socket to be write-ready */
366 ldap_pvt_thread_mutex_lock( &conn->c_write2_mutex );
367 conn->c_writewaiter = 1;
368 slapd_set_write( conn->c_sd, 2 );
370 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
371 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
372 ldap_pvt_thread_cond_wait( &conn->c_write2_cv, &conn->c_write2_mutex );
373 conn->c_writewaiter = 0;
374 ldap_pvt_thread_mutex_unlock( &conn->c_write2_mutex );
375 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
376 if ( conn->c_writers < 0 ) {
383 if ( conn->c_writers < 0 ) {
385 if ( !conn->c_writers )
386 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
389 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
391 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
397 send_ldap_control( BerElement *ber, LDAPControl *c )
403 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
405 if( c->ldctl_iscritical ) {
406 rc = ber_printf( ber, "b",
407 (ber_int_t) c->ldctl_iscritical ) ;
408 if( rc == -1 ) return rc;
411 if( c->ldctl_value.bv_val != NULL ) {
412 rc = ber_printf( ber, "O", &c->ldctl_value );
413 if( rc == -1 ) return rc;
416 rc = ber_printf( ber, /*{*/"N}" );
417 if( rc == -1 ) return rc;
423 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
430 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
431 if( rc == -1 ) return rc;
433 for( ; *c != NULL; c++) {
434 rc = send_ldap_control( ber, *c );
435 if( rc == -1 ) return rc;
438 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
439 /* this is a hack to avoid having to modify op->s_ctrls */
440 if( o->o_sortedresults ) {
441 BerElementBuffer berbuf;
442 BerElement *sber = (BerElement *) &berbuf;
444 BER_BVZERO( &sorted.ldctl_value );
445 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
446 sorted.ldctl_iscritical = 0;
448 ber_init2( sber, NULL, LBER_USE_DER );
450 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
452 if( ber_flatten2( sber, &sorted.ldctl_value, 0 ) == -1 ) {
456 (void) ber_free_buf( sber );
458 rc = send_ldap_control( ber, &sorted );
459 if( rc == -1 ) return rc;
463 rc = ber_printf( ber, /*{*/"N}" );
469 * slap_response_play()
471 * plays the callback list; rationale: a callback can
472 * - remove itself from the list, by setting op->o_callback = NULL;
473 * malloc()'ed callbacks should free themselves from inside the
474 * sc_response() function.
475 * - replace itself with another (list of) callback(s), by setting
476 * op->o_callback = a new (list of) callback(s); in this case, it
477 * is the callback's responsibility to to append existing subsequent
478 * callbacks to the end of the list that is passed to the sc_response()
480 * - modify the list of subsequent callbacks by modifying the value
481 * of the sc_next field from inside the sc_response() function; this
482 * case does not require any handling from inside slap_response_play()
484 * To stop execution of the playlist, the sc_response() function must return
485 * a value different from SLAP_SC_CONTINUE.
487 * The same applies to slap_cleanup_play(); only, there is no means to stop
488 * execution of the playlist, since all cleanup functions must be called.
497 slap_callback *sc = op->o_callback, **scp;
499 rc = SLAP_CB_CONTINUE;
500 for ( scp = ≻ *scp; ) {
501 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
503 op->o_callback = *scp;
504 if ( op->o_callback->sc_response ) {
505 rc = op->o_callback->sc_response( op, rs );
506 if ( op->o_callback == NULL ) {
507 /* the callback has been removed;
512 } else if ( op->o_callback != *scp ) {
513 /* a new callback has been inserted
514 * in place of the existing one; repair the list */
515 *scp = op->o_callback;
518 if ( rc != SLAP_CB_CONTINUE ) break;
532 slap_callback *sc = op->o_callback, **scp;
534 for ( scp = ≻ *scp; ) {
535 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
537 op->o_callback = *scp;
538 if ( op->o_callback->sc_cleanup ) {
539 (void)op->o_callback->sc_cleanup( op, rs );
540 if ( op->o_callback == NULL ) {
541 /* the callback has been removed;
546 } else if ( op->o_callback != *scp ) {
547 /* a new callback has been inserted
548 * after the existing one; repair the list */
549 /* a new callback has been inserted
550 * in place of the existing one; repair the list */
551 *scp = op->o_callback;
554 /* don't care about the result; do all cleanup */
568 BerElementBuffer berbuf;
569 BerElement *ber = (BerElement *) &berbuf;
570 int rc = LDAP_SUCCESS;
573 if (( rs->sr_err == SLAPD_ABANDON || op->o_abandon ) && !op->o_cancel ) {
578 if ( op->o_callback ) {
579 rc = slap_response_play( op, rs );
580 if ( rc != SLAP_CB_CONTINUE ) {
585 #ifdef LDAP_CONNECTIONLESS
586 if (op->o_conn && op->o_conn->c_is_udp)
591 ber_init_w_nullc( ber, LBER_USE_DER );
592 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
596 if ( rc == SLAPD_ABANDON && op->o_cancel )
599 Debug( LDAP_DEBUG_TRACE,
600 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
601 rs->sr_msgid, rs->sr_tag, rc );
604 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
605 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
609 #ifdef LDAP_CONNECTIONLESS
610 if (op->o_conn && op->o_conn->c_is_udp &&
611 op->o_protocol == LDAP_VERSION2 )
613 rc = ber_printf( ber, "t{ess" /*"}"*/,
615 rs->sr_matched == NULL ? "" : rs->sr_matched,
616 rs->sr_text == NULL ? "" : rs->sr_text );
619 if ( rs->sr_type == REP_INTERMEDIATE ) {
620 rc = ber_printf( ber, "{it{" /*"}}"*/,
621 rs->sr_msgid, rs->sr_tag );
624 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
625 rs->sr_msgid, rs->sr_tag, rc,
626 rs->sr_matched == NULL ? "" : rs->sr_matched,
627 rs->sr_text == NULL ? "" : rs->sr_text );
631 if ( rs->sr_ref != NULL ) {
632 assert( rs->sr_err == LDAP_REFERRAL );
633 rc = ber_printf( ber, "t{W}",
634 LDAP_TAG_REFERRAL, rs->sr_ref );
636 assert( rs->sr_err != LDAP_REFERRAL );
640 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
641 rc = ber_printf( ber, "tO",
642 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
646 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
648 if ( rs->sr_rspoid != NULL ) {
649 rc = ber_printf( ber, "ts",
650 rs->sr_type == REP_EXTENDED
651 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
654 if( rc != -1 && rs->sr_rspdata != NULL ) {
655 rc = ber_printf( ber, "tO",
656 rs->sr_type == REP_EXTENDED
657 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
663 rc = ber_printf( ber, /*"{"*/ "N}" );
667 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
671 rc = ber_printf( ber, /*"{"*/ "N}" );
674 #ifdef LDAP_CONNECTIONLESS
675 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
678 rc = ber_printf( ber, /*"{"*/ "N}" );
683 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
685 #ifdef LDAP_CONNECTIONLESS
686 if (!op->o_conn || op->o_conn->c_is_udp == 0)
695 bytes = send_ldap_ber( op, ber );
696 #ifdef LDAP_CONNECTIONLESS
697 if (!op->o_conn || op->o_conn->c_is_udp == 0)
704 Debug( LDAP_DEBUG_ANY,
705 "send_ldap_response: ber write failed\n",
711 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
712 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
713 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
714 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
717 /* Tell caller that we did this for real, as opposed to being
718 * overridden by a callback
720 rc = SLAP_CB_CONTINUE;
723 if ( op->o_callback ) {
724 (void)slap_cleanup_play( op, rs );
727 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
728 rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
729 if ( rs->sr_matched ) {
730 free( (char *)rs->sr_matched );
731 rs->sr_matched = NULL;
735 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
736 rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
738 ber_bvarray_free( rs->sr_ref );
743 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
744 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
745 if ( rs->sr_ctrls ) {
746 slap_free_ctrls( op, rs->sr_ctrls );
756 send_ldap_disconnect( Operation *op, SlapReply *rs )
758 #define LDAP_UNSOLICITED_ERROR(e) \
759 ( (e) == LDAP_PROTOCOL_ERROR \
760 || (e) == LDAP_STRONG_AUTH_REQUIRED \
761 || (e) == LDAP_UNAVAILABLE )
763 Debug( LDAP_DEBUG_TRACE,
764 "send_ldap_disconnect %d:%s\n",
765 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
766 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
768 /* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
769 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
770 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
772 rs->sr_type = REP_EXTENDED;
773 rs->sr_rspdata = NULL;
775 if ( op->o_protocol < LDAP_VERSION3 ) {
776 rs->sr_rspoid = NULL;
777 rs->sr_tag = slap_req2res( op->o_tag );
778 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
781 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
782 rs->sr_tag = LDAP_RES_EXTENDED;
783 rs->sr_msgid = LDAP_RES_UNSOLICITED;
786 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
787 Statslog( LDAP_DEBUG_STATS,
788 "%s DISCONNECT tag=%lu err=%d text=%s\n",
789 op->o_log_prefix, rs->sr_tag, rs->sr_err,
790 rs->sr_text ? rs->sr_text : "", 0 );
795 slap_send_ldap_result( Operation *op, SlapReply *rs )
798 const char *otext = rs->sr_text;
799 BerVarray oref = rs->sr_ref;
801 rs->sr_type = REP_RESULT;
803 /* Propagate Abandons so that cleanup callbacks can be processed */
804 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
807 Debug( LDAP_DEBUG_TRACE,
808 "send_ldap_result: %s p=%d\n",
809 op->o_log_prefix, op->o_protocol, 0 );
810 Debug( LDAP_DEBUG_ARGS,
811 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
812 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
813 rs->sr_text ? rs->sr_text : "" );
815 Debug( LDAP_DEBUG_ARGS,
816 "send_ldap_result: referral=\"%s\"\n",
817 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
820 assert( !LDAP_API_ERROR( rs->sr_err ) );
821 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
823 if ( rs->sr_err == LDAP_REFERRAL ) {
824 if( op->o_domain_scope ) rs->sr_ref = NULL;
826 if( rs->sr_ref == NULL ) {
827 rs->sr_err = LDAP_NO_SUCH_OBJECT;
828 } else if ( op->o_protocol < LDAP_VERSION3 ) {
829 rs->sr_err = LDAP_PARTIAL_RESULTS;
833 if ( op->o_protocol < LDAP_VERSION3 ) {
834 tmp = v2ref( rs->sr_ref, rs->sr_text );
840 rs->sr_tag = slap_req2res( op->o_tag );
841 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
843 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
844 if ( rs->sr_ref == NULL ) {
845 rs->sr_flags ^= REP_REF_MUSTBEFREED;
846 ber_bvarray_free( oref );
848 oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
851 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
852 if ( op->o_tag == LDAP_REQ_SEARCH ) {
853 Statslog( LDAP_DEBUG_STATS,
854 "%s SEARCH RESULT tag=%lu err=%d nentries=%d text=%s\n",
855 op->o_log_prefix, rs->sr_tag, rs->sr_err,
856 rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
858 Statslog( LDAP_DEBUG_STATS,
859 "%s RESULT tag=%lu err=%d text=%s\n",
860 op->o_log_prefix, rs->sr_tag, rs->sr_err,
861 rs->sr_text ? rs->sr_text : "", 0 );
865 if( tmp != NULL ) ch_free(tmp);
871 send_ldap_sasl( Operation *op, SlapReply *rs )
873 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
875 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
877 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
878 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
880 rs->sr_type = REP_SASL;
881 rs->sr_tag = slap_req2res( op->o_tag );
882 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
884 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
885 Statslog( LDAP_DEBUG_STATS,
886 "%s RESULT tag=%lu err=%d text=%s\n",
887 op->o_log_prefix, rs->sr_tag, rs->sr_err,
888 rs->sr_text ? rs->sr_text : "", 0 );
893 slap_send_ldap_extended( Operation *op, SlapReply *rs )
895 Debug( LDAP_DEBUG_TRACE,
896 "send_ldap_extended: err=%d oid=%s len=%ld\n",
898 rs->sr_rspoid ? rs->sr_rspoid : "",
899 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
901 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
902 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
904 rs->sr_type = REP_EXTENDED;
905 rs->sr_tag = slap_req2res( op->o_tag );
906 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
908 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
909 Statslog( LDAP_DEBUG_STATS,
910 "%s RESULT oid=%s err=%d text=%s\n",
911 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
912 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
917 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
919 Debug( LDAP_DEBUG_TRACE,
920 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
922 rs->sr_rspoid ? rs->sr_rspoid : "",
923 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
925 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
926 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
928 rs->sr_type = REP_INTERMEDIATE;
929 rs->sr_tag = LDAP_RES_INTERMEDIATE;
930 rs->sr_msgid = op->o_msgid;
931 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
932 Statslog( LDAP_DEBUG_STATS2,
933 "%s INTERM oid=%s\n",
935 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
942 * LDAP_SUCCESS entry sent
943 * LDAP_OTHER entry not sent (other)
944 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
945 * LDAP_UNAVAILABLE entry not sent (connection closed)
946 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
950 slap_send_search_entry( Operation *op, SlapReply *rs )
952 BerElementBuffer berbuf;
953 BerElement *ber = (BerElement *) &berbuf;
955 int i, j, rc = LDAP_UNAVAILABLE, bytes;
957 AccessControlState acl_state = ACL_STATE_INIT;
959 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
961 /* a_flags: array of flags telling if the i-th element will be
962 * returned or filtered out
963 * e_flags: array of a_flags
965 char **e_flags = NULL;
967 rs->sr_type = REP_SEARCH;
969 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
970 rc = LDAP_SIZELIMIT_EXCEEDED;
974 /* Every 64 entries, check for thread pool pause */
975 if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
976 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
982 /* eventually will loop through generated operational attribute types
983 * currently implemented types include:
984 * entryDN, subschemaSubentry, and hasSubordinates */
985 /* NOTE: moved before overlays callback circling because
986 * they may modify entry and other stuff in rs */
987 /* check for special all operational attributes ("+") type */
988 /* FIXME: maybe we could set this flag at the operation level;
989 * however, in principle the caller of send_search_entry() may
990 * change the attribute list at each call */
991 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
993 rc = backend_operational( op, rs );
998 if ( op->o_callback ) {
999 rc = slap_response_play( op, rs );
1000 if ( rc != SLAP_CB_CONTINUE ) {
1005 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
1006 op->o_connid, rs->sr_entry->e_name.bv_val,
1007 op->ors_attrsonly ? " (attrsOnly)" : "" );
1009 attrsonly = op->ors_attrsonly;
1011 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
1012 Debug( LDAP_DEBUG_ACL,
1013 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
1014 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
1016 rc = LDAP_INSUFFICIENT_ACCESS;
1020 if ( op->o_res_ber ) {
1021 /* read back control or LDAP_CONNECTIONLESS */
1022 ber = op->o_res_ber;
1026 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1027 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1029 ber_init2( ber, &bv, LBER_USE_DER );
1030 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1033 #ifdef LDAP_CONNECTIONLESS
1034 if ( op->o_conn && op->o_conn->c_is_udp ) {
1035 /* CONNECTIONLESS */
1036 if ( op->o_protocol == LDAP_VERSION2 ) {
1037 rc = ber_printf(ber, "t{O{" /*}}*/,
1038 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1040 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1041 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1045 if ( op->o_res_ber ) {
1046 /* read back control */
1047 rc = ber_printf( ber, "t{O{" /*}}*/,
1048 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1050 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1051 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1055 Debug( LDAP_DEBUG_ANY,
1056 "send_search_entry: conn %lu ber_printf failed\n",
1057 op->o_connid, 0, 0 );
1059 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1060 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
1065 /* check for special all user attributes ("*") type */
1066 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
1068 /* create an array of arrays of flags. Each flag corresponds
1069 * to particular value of attribute and equals 1 if value matches
1070 * to ValuesReturnFilter or 0 if not
1072 if ( op->o_vrFilter != NULL ) {
1076 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1077 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1080 size = i * sizeof(char *) + k;
1083 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
1084 if( e_flags == NULL ) {
1085 Debug( LDAP_DEBUG_ANY,
1086 "send_search_entry: conn %lu slap_sl_calloc failed\n",
1087 op->o_connid, 0, 0 );
1090 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
1093 a_flags = (char *)(e_flags + i);
1094 memset( a_flags, 0, k );
1095 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
1096 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1097 e_flags[i] = a_flags;
1101 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
1103 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
1104 "conn %lu matched values filtering failed\n",
1105 op->o_connid, 0, 0 );
1106 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1107 send_ldap_error( op, rs, LDAP_OTHER,
1108 "matched values filtering error" );
1115 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
1116 AttributeDescription *desc = a->a_desc;
1119 if ( rs->sr_attrs == NULL ) {
1120 /* all user attrs request, skip operational attributes */
1121 if( is_at_operational( desc->ad_type ) ) {
1126 /* specific attrs requested */
1127 if ( is_at_operational( desc->ad_type ) ) {
1128 /* if not explicitly requested */
1129 if ( !ad_inlist( desc, rs->sr_attrs )) {
1130 /* if not all op attrs requested, skip */
1131 if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
1133 /* if DSA-specific and replicating, skip */
1134 if ( op->o_sync != SLAP_CONTROL_NONE &&
1135 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1139 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1146 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1147 ACL_READ, &acl_state ) )
1149 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1150 "conn %lu access to attribute %s not allowed\n",
1151 op->o_connid, desc->ad_cname.bv_val, 0 );
1155 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1156 Debug( LDAP_DEBUG_ANY,
1157 "send_search_entry: conn %lu ber_printf failed\n",
1158 op->o_connid, 0, 0 );
1160 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1161 send_ldap_error( op, rs, LDAP_OTHER,
1162 "encoding description error");
1170 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1171 if ( ! access_allowed( op, rs->sr_entry,
1172 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1174 Debug( LDAP_DEBUG_ACL,
1175 "send_search_entry: conn %lu "
1176 "access to attribute %s, value #%d not allowed\n",
1177 op->o_connid, desc->ad_cname.bv_val, i );
1182 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1189 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1190 Debug( LDAP_DEBUG_ANY,
1191 "send_search_entry: conn %lu ber_printf failed\n",
1192 op->o_connid, 0, 0 );
1194 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1195 send_ldap_error( op, rs, LDAP_OTHER,
1196 "encoding description error");
1201 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1202 Debug( LDAP_DEBUG_ANY,
1203 "send_search_entry: conn %lu "
1204 "ber_printf failed.\n", op->o_connid, 0, 0 );
1206 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1207 send_ldap_error( op, rs, LDAP_OTHER,
1208 "encoding values error" );
1215 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1216 Debug( LDAP_DEBUG_ANY,
1217 "send_search_entry: conn %lu ber_printf failed\n",
1218 op->o_connid, 0, 0 );
1220 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1221 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1227 /* NOTE: moved before overlays callback circling because
1228 * they may modify entry and other stuff in rs */
1229 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1233 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1234 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1237 size = i * sizeof(char *) + k;
1239 char *a_flags, **tmp;
1242 * Reuse previous memory - we likely need less space
1243 * for operational attributes
1245 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1247 if ( tmp == NULL ) {
1248 Debug( LDAP_DEBUG_ANY,
1249 "send_search_entry: conn %lu "
1250 "not enough memory "
1251 "for matched values filtering\n",
1252 op->o_connid, 0, 0 );
1253 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1254 send_ldap_error( op, rs, LDAP_OTHER,
1255 "not enough memory for matched values filtering" );
1259 a_flags = (char *)(e_flags + i);
1260 memset( a_flags, 0, k );
1261 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1262 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1263 e_flags[i] = a_flags;
1266 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1269 Debug( LDAP_DEBUG_ANY,
1270 "send_search_entry: conn %lu "
1271 "matched values filtering failed\n",
1272 op->o_connid, 0, 0);
1273 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1274 send_ldap_error( op, rs, LDAP_OTHER,
1275 "matched values filtering error" );
1282 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1283 AttributeDescription *desc = a->a_desc;
1285 if ( rs->sr_attrs == NULL ) {
1286 /* all user attrs request, skip operational attributes */
1287 if( is_at_operational( desc->ad_type ) ) {
1292 /* specific attrs requested */
1293 if( is_at_operational( desc->ad_type ) ) {
1294 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1295 !ad_inlist( desc, rs->sr_attrs ) )
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 send_ldap_error( op, 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 send_ldap_error( op, 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 send_ldap_error( op, 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 send_ldap_error( op, 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 send_ldap_error( op, 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++;
1691 if ( next[ 0 ] != '\0' ) {
1692 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1698 /* FIXME: what if it's larger that max int? */
1699 *code = (int)retcode;
1701 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1705 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1710 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1720 int slap_read_controls(
1724 const struct berval *oid,
1725 LDAPControl **ctrl )
1729 BerElementBuffer berbuf;
1730 BerElement *ber = (BerElement *) &berbuf;
1734 Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1735 op->o_log_prefix, oid->bv_val, e->e_dn );
1738 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1739 op->o_preread_attrs : op->o_postread_attrs;
1741 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1742 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1744 ber_init2( ber, &bv, LBER_USE_DER );
1745 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1747 /* create new operation */
1749 /* FIXME: o_bd needed for ACL */
1750 myop.o_bd = op->o_bd;
1751 myop.o_res_ber = ber;
1752 myop.o_callback = NULL;
1753 myop.ors_slimit = 1;
1754 myop.ors_attrsonly = 0;
1756 rc = slap_send_search_entry( &myop, rs );
1759 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1761 if( rc == -1 ) return LDAP_OTHER;
1763 c.ldctl_oid = oid->bv_val;
1764 c.ldctl_iscritical = 0;
1766 if ( *ctrl == NULL ) {
1768 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1770 /* retry: free previous try */
1771 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1775 return LDAP_SUCCESS;
1778 /* Map API errors to protocol errors... */
1780 slap_map_api2result( SlapReply *rs )
1782 switch(rs->sr_err) {
1783 case LDAP_SERVER_DOWN:
1784 return LDAP_UNAVAILABLE;
1785 case LDAP_LOCAL_ERROR:
1787 case LDAP_ENCODING_ERROR:
1788 case LDAP_DECODING_ERROR:
1789 return LDAP_PROTOCOL_ERROR;
1791 return LDAP_UNAVAILABLE;
1792 case LDAP_AUTH_UNKNOWN:
1793 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1794 case LDAP_FILTER_ERROR:
1795 rs->sr_text = "Filter error";
1797 case LDAP_USER_CANCELLED:
1798 rs->sr_text = "User cancelled";
1800 case LDAP_PARAM_ERROR:
1801 return LDAP_PROTOCOL_ERROR;
1802 case LDAP_NO_MEMORY:
1804 case LDAP_CONNECT_ERROR:
1805 return LDAP_UNAVAILABLE;
1806 case LDAP_NOT_SUPPORTED:
1807 return LDAP_UNWILLING_TO_PERFORM;
1808 case LDAP_CONTROL_NOT_FOUND:
1809 return LDAP_PROTOCOL_ERROR;
1810 case LDAP_NO_RESULTS_RETURNED:
1811 return LDAP_NO_SUCH_OBJECT;
1812 case LDAP_MORE_RESULTS_TO_RETURN:
1813 rs->sr_text = "More results to return";
1815 case LDAP_CLIENT_LOOP:
1816 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1817 return LDAP_LOOP_DETECT;
1819 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1826 slap_attr_flags( AttributeName *an )
1828 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1831 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1834 flags |= an_find( an, slap_bv_all_operational_attrs )
1835 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1836 flags |= an_find( an, slap_bv_all_user_attrs )
1837 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;