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-2012 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_pool_idle( &connection_pool );
373 ldap_pvt_thread_cond_wait( &conn->c_write2_cv, &conn->c_write2_mutex );
374 conn->c_writewaiter = 0;
375 ldap_pvt_thread_mutex_unlock( &conn->c_write2_mutex );
376 ldap_pvt_thread_pool_unidle( &connection_pool );
377 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
378 if ( conn->c_writers < 0 ) {
385 if ( conn->c_writers < 0 ) {
387 if ( !conn->c_writers )
388 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
391 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
393 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
399 send_ldap_control( BerElement *ber, LDAPControl *c )
405 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
407 if( c->ldctl_iscritical ) {
408 rc = ber_printf( ber, "b",
409 (ber_int_t) c->ldctl_iscritical ) ;
410 if( rc == -1 ) return rc;
413 if( c->ldctl_value.bv_val != NULL ) {
414 rc = ber_printf( ber, "O", &c->ldctl_value );
415 if( rc == -1 ) return rc;
418 rc = ber_printf( ber, /*{*/"N}" );
419 if( rc == -1 ) return rc;
425 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
432 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
433 if( rc == -1 ) return rc;
435 for( ; *c != NULL; c++) {
436 rc = send_ldap_control( ber, *c );
437 if( rc == -1 ) return rc;
440 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
441 /* this is a hack to avoid having to modify op->s_ctrls */
442 if( o->o_sortedresults ) {
443 BerElementBuffer berbuf;
444 BerElement *sber = (BerElement *) &berbuf;
446 BER_BVZERO( &sorted.ldctl_value );
447 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
448 sorted.ldctl_iscritical = 0;
450 ber_init2( sber, NULL, LBER_USE_DER );
452 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
454 if( ber_flatten2( sber, &sorted.ldctl_value, 0 ) == -1 ) {
458 (void) ber_free_buf( sber );
460 rc = send_ldap_control( ber, &sorted );
461 if( rc == -1 ) return rc;
465 rc = ber_printf( ber, /*{*/"N}" );
471 * slap_response_play()
473 * plays the callback list; rationale: a callback can
474 * - remove itself from the list, by setting op->o_callback = NULL;
475 * malloc()'ed callbacks should free themselves from inside the
476 * sc_response() function.
477 * - replace itself with another (list of) callback(s), by setting
478 * op->o_callback = a new (list of) callback(s); in this case, it
479 * is the callback's responsibility to to append existing subsequent
480 * callbacks to the end of the list that is passed to the sc_response()
482 * - modify the list of subsequent callbacks by modifying the value
483 * of the sc_next field from inside the sc_response() function; this
484 * case does not require any handling from inside slap_response_play()
486 * To stop execution of the playlist, the sc_response() function must return
487 * a value different from SLAP_SC_CONTINUE.
489 * The same applies to slap_cleanup_play(); only, there is no means to stop
490 * execution of the playlist, since all cleanup functions must be called.
499 slap_callback *sc = op->o_callback, **scp;
501 rc = SLAP_CB_CONTINUE;
502 for ( scp = ≻ *scp; ) {
503 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
505 op->o_callback = *scp;
506 if ( op->o_callback->sc_response ) {
507 rc = op->o_callback->sc_response( op, rs );
508 if ( op->o_callback == NULL ) {
509 /* the callback has been removed;
514 } else if ( op->o_callback != *scp ) {
515 /* a new callback has been inserted
516 * in place of the existing one; repair the list */
517 *scp = op->o_callback;
520 if ( rc != SLAP_CB_CONTINUE ) break;
534 slap_callback *sc = op->o_callback, **scp;
536 for ( scp = ≻ *scp; ) {
537 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
539 op->o_callback = *scp;
540 if ( op->o_callback->sc_cleanup ) {
541 (void)op->o_callback->sc_cleanup( op, rs );
542 if ( op->o_callback == NULL ) {
543 /* the callback has been removed;
548 } else if ( op->o_callback != *scp ) {
549 /* a new callback has been inserted
550 * after the existing one; repair the list */
551 /* a new callback has been inserted
552 * in place of the existing one; repair the list */
553 *scp = op->o_callback;
556 /* don't care about the result; do all cleanup */
570 BerElementBuffer berbuf;
571 BerElement *ber = (BerElement *) &berbuf;
572 int rc = LDAP_SUCCESS;
575 /* op was actually aborted, bypass everything if client didn't Cancel */
576 if (( rs->sr_err == SLAPD_ABANDON ) && !op->o_cancel ) {
581 if ( op->o_callback ) {
582 rc = slap_response_play( op, rs );
583 if ( rc != SLAP_CB_CONTINUE ) {
588 /* op completed, connection aborted, bypass sending response */
589 if ( op->o_abandon && !op->o_cancel ) {
594 #ifdef LDAP_CONNECTIONLESS
595 if (op->o_conn && op->o_conn->c_is_udp)
600 ber_init_w_nullc( ber, LBER_USE_DER );
601 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
605 if ( rc == SLAPD_ABANDON && op->o_cancel )
608 Debug( LDAP_DEBUG_TRACE,
609 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
610 rs->sr_msgid, rs->sr_tag, rc );
613 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
614 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
618 #ifdef LDAP_CONNECTIONLESS
619 if (op->o_conn && op->o_conn->c_is_udp &&
620 op->o_protocol == LDAP_VERSION2 )
622 rc = ber_printf( ber, "t{ess" /*"}"*/,
624 rs->sr_matched == NULL ? "" : rs->sr_matched,
625 rs->sr_text == NULL ? "" : rs->sr_text );
628 if ( rs->sr_type == REP_INTERMEDIATE ) {
629 rc = ber_printf( ber, "{it{" /*"}}"*/,
630 rs->sr_msgid, rs->sr_tag );
633 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
634 rs->sr_msgid, rs->sr_tag, rc,
635 rs->sr_matched == NULL ? "" : rs->sr_matched,
636 rs->sr_text == NULL ? "" : rs->sr_text );
640 if ( rs->sr_ref != NULL ) {
641 assert( rs->sr_err == LDAP_REFERRAL );
642 rc = ber_printf( ber, "t{W}",
643 LDAP_TAG_REFERRAL, rs->sr_ref );
645 assert( rs->sr_err != LDAP_REFERRAL );
649 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
650 rc = ber_printf( ber, "tO",
651 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
655 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
657 if ( rs->sr_rspoid != NULL ) {
658 rc = ber_printf( ber, "ts",
659 rs->sr_type == REP_EXTENDED
660 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
663 if( rc != -1 && rs->sr_rspdata != NULL ) {
664 rc = ber_printf( ber, "tO",
665 rs->sr_type == REP_EXTENDED
666 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
672 rc = ber_printf( ber, /*"{"*/ "N}" );
676 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
680 rc = ber_printf( ber, /*"{"*/ "N}" );
683 #ifdef LDAP_CONNECTIONLESS
684 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
687 rc = ber_printf( ber, /*"{"*/ "N}" );
692 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
694 #ifdef LDAP_CONNECTIONLESS
695 if (!op->o_conn || op->o_conn->c_is_udp == 0)
704 bytes = send_ldap_ber( op, ber );
705 #ifdef LDAP_CONNECTIONLESS
706 if (!op->o_conn || op->o_conn->c_is_udp == 0)
713 Debug( LDAP_DEBUG_ANY,
714 "send_ldap_response: ber write failed\n",
720 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
721 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
722 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
723 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
726 /* Tell caller that we did this for real, as opposed to being
727 * overridden by a callback
729 rc = SLAP_CB_CONTINUE;
732 if ( op->o_callback ) {
733 (void)slap_cleanup_play( op, rs );
736 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
737 rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
738 if ( rs->sr_matched ) {
739 free( (char *)rs->sr_matched );
740 rs->sr_matched = NULL;
744 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
745 rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
747 ber_bvarray_free( rs->sr_ref );
752 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
753 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
754 if ( rs->sr_ctrls ) {
755 slap_free_ctrl_vals( op, rs->sr_ctrls );
765 send_ldap_disconnect( Operation *op, SlapReply *rs )
767 #define LDAP_UNSOLICITED_ERROR(e) \
768 ( (e) == LDAP_PROTOCOL_ERROR \
769 || (e) == LDAP_STRONG_AUTH_REQUIRED \
770 || (e) == LDAP_UNAVAILABLE )
772 Debug( LDAP_DEBUG_TRACE,
773 "send_ldap_disconnect %d:%s\n",
774 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
775 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
777 /* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
778 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
779 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
781 rs->sr_type = REP_EXTENDED;
782 rs->sr_rspdata = NULL;
784 if ( op->o_protocol < LDAP_VERSION3 ) {
785 rs->sr_rspoid = NULL;
786 rs->sr_tag = slap_req2res( op->o_tag );
787 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
790 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
791 rs->sr_tag = LDAP_RES_EXTENDED;
792 rs->sr_msgid = LDAP_RES_UNSOLICITED;
795 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
796 Statslog( LDAP_DEBUG_STATS,
797 "%s DISCONNECT tag=%lu err=%d text=%s\n",
798 op->o_log_prefix, rs->sr_tag, rs->sr_err,
799 rs->sr_text ? rs->sr_text : "", 0 );
804 slap_send_ldap_result( Operation *op, SlapReply *rs )
807 const char *otext = rs->sr_text;
808 BerVarray oref = rs->sr_ref;
810 rs->sr_type = REP_RESULT;
812 /* Propagate Abandons so that cleanup callbacks can be processed */
813 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
816 Debug( LDAP_DEBUG_TRACE,
817 "send_ldap_result: %s p=%d\n",
818 op->o_log_prefix, op->o_protocol, 0 );
819 Debug( LDAP_DEBUG_ARGS,
820 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
821 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
822 rs->sr_text ? rs->sr_text : "" );
824 Debug( LDAP_DEBUG_ARGS,
825 "send_ldap_result: referral=\"%s\"\n",
826 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
829 assert( !LDAP_API_ERROR( rs->sr_err ) );
830 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
832 if ( rs->sr_err == LDAP_REFERRAL ) {
833 if( op->o_domain_scope ) rs->sr_ref = NULL;
835 if( rs->sr_ref == NULL ) {
836 rs->sr_err = LDAP_NO_SUCH_OBJECT;
837 } else if ( op->o_protocol < LDAP_VERSION3 ) {
838 rs->sr_err = LDAP_PARTIAL_RESULTS;
842 if ( op->o_protocol < LDAP_VERSION3 ) {
843 tmp = v2ref( rs->sr_ref, rs->sr_text );
849 rs->sr_tag = slap_req2res( op->o_tag );
850 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
852 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
853 if ( rs->sr_ref == NULL ) {
854 rs->sr_flags ^= REP_REF_MUSTBEFREED;
855 ber_bvarray_free( oref );
857 oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
860 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
861 if ( op->o_tag == LDAP_REQ_SEARCH ) {
862 Statslog( LDAP_DEBUG_STATS,
863 "%s SEARCH RESULT tag=%lu err=%d nentries=%d text=%s\n",
864 op->o_log_prefix, rs->sr_tag, rs->sr_err,
865 rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
867 Statslog( LDAP_DEBUG_STATS,
868 "%s RESULT tag=%lu err=%d text=%s\n",
869 op->o_log_prefix, rs->sr_tag, rs->sr_err,
870 rs->sr_text ? rs->sr_text : "", 0 );
874 if( tmp != NULL ) ch_free(tmp);
880 send_ldap_sasl( Operation *op, SlapReply *rs )
882 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
884 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
886 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
887 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
889 rs->sr_type = REP_SASL;
890 rs->sr_tag = slap_req2res( op->o_tag );
891 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
893 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
894 Statslog( LDAP_DEBUG_STATS,
895 "%s RESULT tag=%lu err=%d text=%s\n",
896 op->o_log_prefix, rs->sr_tag, rs->sr_err,
897 rs->sr_text ? rs->sr_text : "", 0 );
902 slap_send_ldap_extended( Operation *op, SlapReply *rs )
904 Debug( LDAP_DEBUG_TRACE,
905 "send_ldap_extended: err=%d oid=%s len=%ld\n",
907 rs->sr_rspoid ? rs->sr_rspoid : "",
908 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
910 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
911 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
913 rs->sr_type = REP_EXTENDED;
914 rs->sr_tag = slap_req2res( op->o_tag );
915 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
917 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
918 Statslog( LDAP_DEBUG_STATS,
919 "%s RESULT oid=%s err=%d text=%s\n",
920 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
921 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
926 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
928 Debug( LDAP_DEBUG_TRACE,
929 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
931 rs->sr_rspoid ? rs->sr_rspoid : "",
932 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
934 RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
935 rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia */
937 rs->sr_type = REP_INTERMEDIATE;
938 rs->sr_tag = LDAP_RES_INTERMEDIATE;
939 rs->sr_msgid = op->o_msgid;
940 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
941 Statslog( LDAP_DEBUG_STATS2,
942 "%s INTERM oid=%s\n",
944 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
948 #define set_ldap_error( rs, err, text ) do { \
949 (rs)->sr_err = err; (rs)->sr_text = text; } while(0)
954 * LDAP_SUCCESS entry sent
955 * LDAP_OTHER entry not sent (other)
956 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
957 * LDAP_UNAVAILABLE entry not sent (connection closed)
958 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
962 slap_send_search_entry( Operation *op, SlapReply *rs )
964 BerElementBuffer berbuf;
965 BerElement *ber = (BerElement *) &berbuf;
967 int i, j, rc = LDAP_UNAVAILABLE, bytes;
969 AccessControlState acl_state = ACL_STATE_INIT;
971 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
973 /* a_flags: array of flags telling if the i-th element will be
974 * returned or filtered out
975 * e_flags: array of a_flags
977 char **e_flags = NULL;
979 rs->sr_type = REP_SEARCH;
981 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
982 rc = LDAP_SIZELIMIT_EXCEEDED;
986 /* Every 64 entries, check for thread pool pause */
987 if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
988 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
994 /* eventually will loop through generated operational attribute types
995 * currently implemented types include:
996 * entryDN, subschemaSubentry, and hasSubordinates */
997 /* NOTE: moved before overlays callback circling because
998 * they may modify entry and other stuff in rs */
999 /* check for special all operational attributes ("+") type */
1000 /* FIXME: maybe we could set this flag at the operation level;
1001 * however, in principle the caller of send_search_entry() may
1002 * change the attribute list at each call */
1003 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
1005 rc = backend_operational( op, rs );
1010 if ( op->o_callback ) {
1011 rc = slap_response_play( op, rs );
1012 if ( rc != SLAP_CB_CONTINUE ) {
1017 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
1018 op->o_connid, rs->sr_entry->e_name.bv_val,
1019 op->ors_attrsonly ? " (attrsOnly)" : "" );
1021 attrsonly = op->ors_attrsonly;
1023 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
1024 Debug( LDAP_DEBUG_ACL,
1025 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
1026 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
1028 rc = LDAP_INSUFFICIENT_ACCESS;
1032 if ( op->o_res_ber ) {
1033 /* read back control or LDAP_CONNECTIONLESS */
1034 ber = op->o_res_ber;
1038 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1039 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1041 ber_init2( ber, &bv, LBER_USE_DER );
1042 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1045 #ifdef LDAP_CONNECTIONLESS
1046 if ( op->o_conn && op->o_conn->c_is_udp ) {
1047 /* CONNECTIONLESS */
1048 if ( op->o_protocol == LDAP_VERSION2 ) {
1049 rc = ber_printf(ber, "t{O{" /*}}*/,
1050 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1052 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1053 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1057 if ( op->o_res_ber ) {
1058 /* read back control */
1059 rc = ber_printf( ber, "t{O{" /*}}*/,
1060 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1062 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1063 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1067 Debug( LDAP_DEBUG_ANY,
1068 "send_search_entry: conn %lu ber_printf failed\n",
1069 op->o_connid, 0, 0 );
1071 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1072 set_ldap_error( rs, LDAP_OTHER, "encoding DN error" );
1077 /* check for special all user attributes ("*") type */
1078 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
1080 /* create an array of arrays of flags. Each flag corresponds
1081 * to particular value of attribute and equals 1 if value matches
1082 * to ValuesReturnFilter or 0 if not
1084 if ( op->o_vrFilter != NULL ) {
1088 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1089 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1092 size = i * sizeof(char *) + k;
1095 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
1096 if( e_flags == NULL ) {
1097 Debug( LDAP_DEBUG_ANY,
1098 "send_search_entry: conn %lu slap_sl_calloc failed\n",
1099 op->o_connid, 0, 0 );
1102 set_ldap_error( rs, LDAP_OTHER, "out of memory" );
1105 a_flags = (char *)(e_flags + i);
1106 memset( a_flags, 0, k );
1107 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
1108 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1109 e_flags[i] = a_flags;
1113 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
1115 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
1116 "conn %lu matched values filtering failed\n",
1117 op->o_connid, 0, 0 );
1118 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1119 set_ldap_error( rs, LDAP_OTHER,
1120 "matched values filtering error" );
1127 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
1128 AttributeDescription *desc = a->a_desc;
1131 if ( rs->sr_attrs == NULL ) {
1132 /* all user attrs request, skip operational attributes */
1133 if( is_at_operational( desc->ad_type ) ) {
1138 /* specific attrs requested */
1139 if ( is_at_operational( desc->ad_type ) ) {
1140 /* if not explicitly requested */
1141 if ( !ad_inlist( desc, rs->sr_attrs )) {
1142 /* if not all op attrs requested, skip */
1143 if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
1145 /* if DSA-specific and replicating, skip */
1146 if ( op->o_sync != SLAP_CONTROL_NONE &&
1147 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1151 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1158 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1159 ACL_READ, &acl_state ) )
1161 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1162 "conn %lu access to attribute %s not allowed\n",
1163 op->o_connid, desc->ad_cname.bv_val, 0 );
1167 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1168 Debug( LDAP_DEBUG_ANY,
1169 "send_search_entry: conn %lu ber_printf failed\n",
1170 op->o_connid, 0, 0 );
1172 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1173 set_ldap_error( rs, LDAP_OTHER,
1174 "encoding description error");
1182 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1183 if ( ! access_allowed( op, rs->sr_entry,
1184 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1186 Debug( LDAP_DEBUG_ACL,
1187 "send_search_entry: conn %lu "
1188 "access to attribute %s, value #%d not allowed\n",
1189 op->o_connid, desc->ad_cname.bv_val, i );
1194 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1201 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1202 Debug( LDAP_DEBUG_ANY,
1203 "send_search_entry: conn %lu ber_printf failed\n",
1204 op->o_connid, 0, 0 );
1206 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1207 set_ldap_error( rs, LDAP_OTHER,
1208 "encoding description error");
1213 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1214 Debug( LDAP_DEBUG_ANY,
1215 "send_search_entry: conn %lu "
1216 "ber_printf failed.\n", op->o_connid, 0, 0 );
1218 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1219 set_ldap_error( rs, LDAP_OTHER,
1220 "encoding values error" );
1227 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1228 Debug( LDAP_DEBUG_ANY,
1229 "send_search_entry: conn %lu ber_printf failed\n",
1230 op->o_connid, 0, 0 );
1232 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1233 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1239 /* NOTE: moved before overlays callback circling because
1240 * they may modify entry and other stuff in rs */
1241 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1245 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1246 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1249 size = i * sizeof(char *) + k;
1251 char *a_flags, **tmp;
1254 * Reuse previous memory - we likely need less space
1255 * for operational attributes
1257 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1259 if ( tmp == NULL ) {
1260 Debug( LDAP_DEBUG_ANY,
1261 "send_search_entry: conn %lu "
1262 "not enough memory "
1263 "for matched values filtering\n",
1264 op->o_connid, 0, 0 );
1265 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1266 set_ldap_error( rs, LDAP_OTHER,
1267 "not enough memory for matched values filtering" );
1271 a_flags = (char *)(e_flags + i);
1272 memset( a_flags, 0, k );
1273 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1274 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1275 e_flags[i] = a_flags;
1278 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1281 Debug( LDAP_DEBUG_ANY,
1282 "send_search_entry: conn %lu "
1283 "matched values filtering failed\n",
1284 op->o_connid, 0, 0);
1285 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1286 set_ldap_error( rs, LDAP_OTHER,
1287 "matched values filtering error" );
1294 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1295 AttributeDescription *desc = a->a_desc;
1297 if ( rs->sr_attrs == NULL ) {
1298 /* all user attrs request, skip operational attributes */
1299 if( is_at_operational( desc->ad_type ) ) {
1304 /* specific attrs requested */
1305 if( is_at_operational( desc->ad_type ) ) {
1306 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1307 !ad_inlist( desc, rs->sr_attrs ) )
1311 /* if DSA-specific and replicating, skip */
1312 if ( op->o_sync != SLAP_CONTROL_NONE &&
1313 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1316 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1322 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1323 ACL_READ, &acl_state ) )
1325 Debug( LDAP_DEBUG_ACL,
1326 "send_search_entry: conn %lu "
1327 "access to attribute %s not allowed\n",
1328 op->o_connid, desc->ad_cname.bv_val, 0 );
1333 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1335 Debug( LDAP_DEBUG_ANY,
1336 "send_search_entry: conn %lu "
1337 "ber_printf failed\n", op->o_connid, 0, 0 );
1339 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1340 set_ldap_error( rs, LDAP_OTHER,
1341 "encoding description error" );
1346 if ( ! attrsonly ) {
1347 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1348 if ( ! access_allowed( op, rs->sr_entry,
1349 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1351 Debug( LDAP_DEBUG_ACL,
1352 "send_search_entry: conn %lu "
1353 "access to %s, value %d not allowed\n",
1354 op->o_connid, desc->ad_cname.bv_val, i );
1359 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1363 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1364 Debug( LDAP_DEBUG_ANY,
1365 "send_search_entry: conn %lu ber_printf failed\n",
1366 op->o_connid, 0, 0 );
1368 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1369 set_ldap_error( rs, LDAP_OTHER,
1370 "encoding values error" );
1377 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1378 Debug( LDAP_DEBUG_ANY,
1379 "send_search_entry: conn %lu ber_printf failed\n",
1380 op->o_connid, 0, 0 );
1382 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1383 set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1391 slap_sl_free( e_flags, op->o_tmpmemctx );
1395 rc = ber_printf( ber, /*{{*/ "}N}" );
1398 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1402 #ifdef LDAP_CONNECTIONLESS
1403 if( op->o_conn && op->o_conn->c_is_udp ) {
1404 if ( op->o_protocol != LDAP_VERSION2 ) {
1405 rc = ber_printf( ber, /*{*/ "N}" );
1409 if ( op->o_res_ber == NULL ) {
1410 rc = ber_printf( ber, /*{*/ "N}" );
1415 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1417 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1418 set_ldap_error( rs, LDAP_OTHER, "encode entry end error" );
1423 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1424 op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1426 rs_flush_entry( op, rs, NULL );
1428 if ( op->o_res_ber == NULL ) {
1429 bytes = send_ldap_ber( op, ber );
1430 ber_free_buf( ber );
1433 Debug( LDAP_DEBUG_ANY,
1434 "send_search_entry: conn %lu ber write failed.\n",
1435 op->o_connid, 0, 0 );
1437 rc = LDAP_UNAVAILABLE;
1442 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1443 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1444 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1445 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1446 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1449 Debug( LDAP_DEBUG_TRACE,
1450 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1455 if ( op->o_callback ) {
1456 (void)slap_cleanup_play( op, rs );
1460 slap_sl_free( e_flags, op->o_tmpmemctx );
1463 /* FIXME: Can break if rs now contains an extended response */
1464 if ( rs->sr_operational_attrs ) {
1465 attrs_free( rs->sr_operational_attrs );
1466 rs->sr_operational_attrs = NULL;
1468 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1470 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1471 rs_flush_entry( op, rs, NULL );
1473 RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
1476 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1477 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1478 if ( rs->sr_ctrls ) {
1479 slap_free_ctrl_vals( op, rs->sr_ctrls );
1480 rs->sr_ctrls = NULL;
1488 slap_send_search_reference( Operation *op, SlapReply *rs )
1490 BerElementBuffer berbuf;
1491 BerElement *ber = (BerElement *) &berbuf;
1494 char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1496 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1497 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1499 rs->sr_type = REP_SEARCHREF;
1500 if ( op->o_callback ) {
1501 rc = slap_response_play( op, rs );
1502 if ( rc != SLAP_CB_CONTINUE ) {
1507 Debug( LDAP_DEBUG_TRACE,
1508 "=> send_search_reference: dn=\"%s\"\n",
1511 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1512 ad_entry, NULL, ACL_READ, NULL ) )
1514 Debug( LDAP_DEBUG_ACL,
1515 "send_search_reference: access to entry not allowed\n",
1521 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1522 ad_ref, NULL, ACL_READ, NULL ) )
1524 Debug( LDAP_DEBUG_ACL,
1525 "send_search_reference: access "
1526 "to reference not allowed\n",
1532 if( op->o_domain_scope ) {
1533 Debug( LDAP_DEBUG_ANY,
1534 "send_search_reference: domainScope control in (%s)\n",
1540 if( rs->sr_ref == NULL ) {
1541 Debug( LDAP_DEBUG_ANY,
1542 "send_search_reference: null ref in (%s)\n",
1548 if( op->o_protocol < LDAP_VERSION3 ) {
1550 /* save the references for the result */
1551 if( rs->sr_ref[0].bv_val != NULL ) {
1552 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1558 #ifdef LDAP_CONNECTIONLESS
1559 if( op->o_conn && op->o_conn->c_is_udp ) {
1560 ber = op->o_res_ber;
1564 ber_init_w_nullc( ber, LBER_USE_DER );
1565 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1568 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1569 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1572 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1576 rc = ber_printf( ber, /*"{"*/ "N}" );
1580 Debug( LDAP_DEBUG_ANY,
1581 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1583 #ifdef LDAP_CONNECTIONLESS
1584 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1586 ber_free_buf( ber );
1587 set_ldap_error( rs, LDAP_OTHER, "encode DN error" );
1592 rs_flush_entry( op, rs, NULL );
1594 #ifdef LDAP_CONNECTIONLESS
1595 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1597 bytes = send_ldap_ber( op, ber );
1598 ber_free_buf( ber );
1601 rc = LDAP_UNAVAILABLE;
1603 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1604 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1605 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1606 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1607 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1609 #ifdef LDAP_CONNECTIONLESS
1612 if ( rs->sr_ref != NULL ) {
1615 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1616 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1617 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1622 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1623 op->o_log_prefix, 0, 0, 0, 0 );
1626 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1630 rs_flush_entry( op, rs, NULL );
1633 if ( op->o_callback ) {
1634 (void)slap_cleanup_play( op, rs );
1637 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1638 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1639 if ( rs->sr_ctrls ) {
1640 slap_free_ctrl_vals( op, rs->sr_ctrls );
1641 rs->sr_ctrls = NULL;
1658 *code = LDAP_SUCCESS;
1662 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1663 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1670 while ( (s = strchr( s, '\n' )) != NULL ) {
1675 if ( (c = strchr( s, ':' )) != NULL ) {
1679 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1684 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1690 while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1691 if ( c[ 0 ] == '\0' ) {
1692 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1698 retcode = strtol( c, &next, 10 );
1699 if ( next == NULL || next == c ) {
1700 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1706 while ( isspace( (unsigned char) next[ 0 ] ) && next[ 0 ] != '\n' )
1708 if ( next[ 0 ] != '\0' && next[ 0 ] != '\n' ) {
1709 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1715 /* FIXME: what if it's larger than max int? */
1716 *code = (int)retcode;
1718 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1722 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1727 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1737 int slap_read_controls(
1741 const struct berval *oid,
1742 LDAPControl **ctrl )
1746 BerElementBuffer berbuf;
1747 BerElement *ber = (BerElement *) &berbuf;
1751 Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1752 op->o_log_prefix, oid->bv_val, e->e_dn );
1755 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1756 op->o_preread_attrs : op->o_postread_attrs;
1758 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1759 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1761 ber_init2( ber, &bv, LBER_USE_DER );
1762 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1764 /* create new operation */
1766 /* FIXME: o_bd needed for ACL */
1767 myop.o_bd = op->o_bd;
1768 myop.o_res_ber = ber;
1769 myop.o_callback = NULL;
1770 myop.ors_slimit = 1;
1771 myop.ors_attrsonly = 0;
1773 rc = slap_send_search_entry( &myop, rs );
1776 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1778 if( rc == -1 ) return LDAP_OTHER;
1780 c.ldctl_oid = oid->bv_val;
1781 c.ldctl_iscritical = 0;
1783 if ( *ctrl == NULL ) {
1785 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1787 /* retry: free previous try */
1788 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1792 return LDAP_SUCCESS;
1795 /* Map API errors to protocol errors... */
1797 slap_map_api2result( SlapReply *rs )
1799 switch(rs->sr_err) {
1800 case LDAP_SERVER_DOWN:
1801 return LDAP_UNAVAILABLE;
1802 case LDAP_LOCAL_ERROR:
1804 case LDAP_ENCODING_ERROR:
1805 case LDAP_DECODING_ERROR:
1806 return LDAP_PROTOCOL_ERROR;
1808 return LDAP_UNAVAILABLE;
1809 case LDAP_AUTH_UNKNOWN:
1810 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1811 case LDAP_FILTER_ERROR:
1812 rs->sr_text = "Filter error";
1814 case LDAP_USER_CANCELLED:
1815 rs->sr_text = "User cancelled";
1817 case LDAP_PARAM_ERROR:
1818 return LDAP_PROTOCOL_ERROR;
1819 case LDAP_NO_MEMORY:
1821 case LDAP_CONNECT_ERROR:
1822 return LDAP_UNAVAILABLE;
1823 case LDAP_NOT_SUPPORTED:
1824 return LDAP_UNWILLING_TO_PERFORM;
1825 case LDAP_CONTROL_NOT_FOUND:
1826 return LDAP_PROTOCOL_ERROR;
1827 case LDAP_NO_RESULTS_RETURNED:
1828 return LDAP_NO_SUCH_OBJECT;
1829 case LDAP_MORE_RESULTS_TO_RETURN:
1830 rs->sr_text = "More results to return";
1832 case LDAP_CLIENT_LOOP:
1833 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1834 return LDAP_LOOP_DETECT;
1836 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1843 slap_attr_flags( AttributeName *an )
1845 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1848 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1851 flags |= an_find( an, slap_bv_all_operational_attrs )
1852 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1853 flags |= an_find( an, slap_bv_all_user_attrs )
1854 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;