1 /* result.c - routines to send ldap results, errors, and referrals */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/string.h>
17 #include <ac/unistd.h>
25 static char *v2ref( BerVarray ref, const char *text )
27 size_t len = 0, i = 0;
32 return ch_strdup(text);
40 if (text[len-1] != '\n') {
45 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
48 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
50 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
61 strcpy( v2+len, "Referral:" );
62 len += sizeof("Referral:");
64 for( i=0; ref[i].bv_val != NULL; i++ ) {
65 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
68 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
70 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
75 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
77 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
86 static ber_tag_t req2res( ber_tag_t tag )
91 case LDAP_REQ_COMPARE:
92 case LDAP_REQ_EXTENDED:
99 tag = LDAP_RES_DELETE;
102 case LDAP_REQ_ABANDON:
103 case LDAP_REQ_UNBIND:
107 case LDAP_REQ_SEARCH:
108 tag = LDAP_RES_SEARCH_RESULT;
118 static long send_ldap_ber(
124 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
126 /* write only one pdu at a time - wait til it's our turn */
127 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
129 /* lock the connection */
130 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
137 if ( connection_state_closing( conn ) ) {
138 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
139 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
144 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
151 * we got an error. if it's ewouldblock, we need to
152 * wait on the socket being writable. otherwise, figure
153 * it's a hard error and return.
157 LDAP_LOG( OPERATION, ERR,
158 "send_ldap_ber: conn %lu ber_flush failed err=%d (%s)\n",
159 conn ? conn->c_connid : 0, err, sock_errstr(err) );
161 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
162 err, sock_errstr(err), 0 );
165 if ( err != EWOULDBLOCK && err != EAGAIN ) {
166 connection_closing( conn );
168 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
169 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
174 /* wait for socket to be write-ready */
175 conn->c_writewaiter = 1;
176 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
177 slapd_set_write( sd, 1 );
179 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
180 conn->c_writewaiter = 0;
183 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
184 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
190 send_ldap_controls( BerElement *ber, LDAPControl **c )
193 if( c == NULL ) return 0;
195 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
196 if( rc == -1 ) return rc;
198 for( ; *c != NULL; c++) {
199 rc = ber_printf( ber, "{s" /*}*/, (*c)->ldctl_oid );
201 if( (*c)->ldctl_iscritical ) {
202 rc = ber_printf( ber, "b",
203 (ber_int_t) (*c)->ldctl_iscritical ) ;
204 if( rc == -1 ) return rc;
207 if( (*c)->ldctl_value.bv_val != NULL ) {
208 rc = ber_printf( ber, "O", &((*c)->ldctl_value));
209 if( rc == -1 ) return rc;
212 rc = ber_printf( ber, /*{*/"N}" );
213 if( rc == -1 ) return rc;
216 rc = ber_printf( ber, /*{*/"N}" );
226 char berbuf[LBER_ELEMENT_SIZEOF];
227 BerElement *ber = (BerElement *)berbuf;
231 if (op->o_callback && op->o_callback->sc_response) {
232 op->o_callback->sc_response( op, rs );
236 #ifdef LDAP_CONNECTIONLESS
237 if (op->o_conn && op->o_conn->c_is_udp)
241 ber_init_w_nullc( ber, LBER_USE_DER );
244 LDAP_LOG( OPERATION, ENTRY,
245 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
246 rs->sr_msgid, rs->sr_tag, rs->sr_err );
248 Debug( LDAP_DEBUG_TRACE,
249 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
250 rs->sr_msgid, rs->sr_tag, rs->sr_err );
255 LDAP_LOG( OPERATION, ARGS,
256 "send_ldap_response: conn %lu ref=\"%s\"\n",
258 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
260 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
261 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
266 #ifdef LDAP_CONNECTIONLESS
267 if (op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
268 rc = ber_printf( ber, "t{ess" /*"}}"*/,
269 rs->sr_tag, rs->sr_err,
270 rs->sr_matched == NULL ? "" : rs->sr_matched,
271 rs->sr_text == NULL ? "" : rs->sr_text );
275 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
276 rs->sr_msgid, rs->sr_tag, rs->sr_err,
277 rs->sr_matched == NULL ? "" : rs->sr_matched,
278 rs->sr_text == NULL ? "" : rs->sr_text );
282 if ( rs->sr_ref != NULL ) {
283 assert( rs->sr_err == LDAP_REFERRAL );
284 rc = ber_printf( ber, "t{W}",
285 LDAP_TAG_REFERRAL, rs->sr_ref );
287 assert( rs->sr_err != LDAP_REFERRAL );
291 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
292 rc = ber_printf( ber, "tO",
293 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
296 if( rc != -1 && rs->sr_type == REP_EXTENDED ) {
297 if ( rs->sr_rspoid != NULL ) {
298 rc = ber_printf( ber, "ts",
299 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
301 if( rc != -1 && rs->sr_rspdata != NULL ) {
302 rc = ber_printf( ber, "tO",
303 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
308 rc = ber_printf( ber, /*"{"*/ "N}" );
311 if( rc != -1 && rs->sr_ctrls != NULL ) {
312 rc = send_ldap_controls( ber, rs->sr_ctrls );
316 rc = ber_printf( ber, /*"{"*/ "N}" );
319 #ifdef LDAP_CONNECTIONLESS
320 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
321 rc = ber_printf( ber, /*"{"*/ "N}" );
327 LDAP_LOG( OPERATION, ERR,
328 "send_ldap_response: conn %lu ber_printf failed\n",
329 op->o_connid, 0, 0 );
331 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
334 #ifdef LDAP_CONNECTIONLESS
335 if (!op->o_conn || op->o_conn->c_is_udp == 0)
342 bytes = send_ldap_ber( op->o_conn, ber );
343 #ifdef LDAP_CONNECTIONLESS
344 if (!op->o_conn || op->o_conn->c_is_udp == 0)
350 LDAP_LOG( OPERATION, ERR,
351 "send_ldap_response: conn %lu ber write failed\n",
352 op->o_connid : 0, 0, 0 );
354 Debug( LDAP_DEBUG_ANY,
355 "send_ldap_response: ber write failed\n",
363 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
364 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, ( rs->sr_matched != NULL ) ? (void *)ch_strdup( rs->sr_matched ) : NULL );
365 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, ( rs->sr_text != NULL ) ? (void *)ch_strdup( rs->sr_text ) : NULL );
366 #endif /* LDAP_SLAPI */
368 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
369 num_bytes_sent += bytes;
371 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
377 send_ldap_disconnect( Operation *op, SlapReply *rs )
379 #define LDAP_UNSOLICITED_ERROR(e) \
380 ( (e) == LDAP_PROTOCOL_ERROR \
381 || (e) == LDAP_STRONG_AUTH_REQUIRED \
382 || (e) == LDAP_UNAVAILABLE )
384 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
386 rs->sr_type = REP_EXTENDED;
389 LDAP_LOG( OPERATION, ENTRY,
390 "send_ldap_disconnect: conn %lu %d:%s\n",
391 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
393 Debug( LDAP_DEBUG_TRACE,
394 "send_ldap_disconnect %d:%s\n",
395 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
399 if ( op->o_protocol < LDAP_VERSION3 ) {
400 rs->sr_rspoid = NULL;
401 rs->sr_tag = req2res( op->o_tag );
402 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
405 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
406 rs->sr_tag = LDAP_RES_EXTENDED;
410 send_ldap_response( op, rs );
412 Statslog( LDAP_DEBUG_STATS,
413 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
414 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
418 slap_send_ldap_result( Operation *op, SlapReply *rs )
421 const char *otext = rs->sr_text;
422 BerVarray oref = rs->sr_ref;
424 rs->sr_type = REP_RESULT;
426 assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
429 LDAP_LOG( OPERATION, ENTRY,
430 "send_ldap_result: conn %lu op=%lu p=%d\n",
431 op->o_connid, op->o_opid, op->o_protocol );
433 Debug( LDAP_DEBUG_TRACE,
434 "send_ldap_result: conn=%lu op=%lu p=%d\n",
435 op->o_connid, op->o_opid, op->o_protocol );
439 LDAP_LOG( OPERATION, ARGS,
440 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
441 rs->sr_err, rs->sr_matched ? rs->sr_matched : "", rs->sr_text ? rs->sr_text : "" );
443 Debug( LDAP_DEBUG_ARGS,
444 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
445 rs->sr_err, rs->sr_matched ? rs->sr_matched : "", rs->sr_text ? rs->sr_text : "" );
451 LDAP_LOG( OPERATION, ARGS,
452 "send_ldap_result: referral=\"%s\"\n",
453 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
455 Debug( LDAP_DEBUG_ARGS,
456 "send_ldap_result: referral=\"%s\"\n",
457 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
462 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
464 if ( rs->sr_err == LDAP_REFERRAL ) {
465 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
466 if( op->o_domain_scope ) {
470 if( rs->sr_ref == NULL ) {
471 rs->sr_err = LDAP_NO_SUCH_OBJECT;
472 } else if ( op->o_protocol < LDAP_VERSION3 ) {
473 rs->sr_err = LDAP_PARTIAL_RESULTS;
477 if ( op->o_protocol < LDAP_VERSION3 ) {
478 tmp = v2ref( rs->sr_ref, rs->sr_text );
483 rs->sr_tag = req2res( op->o_tag );
484 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
486 send_ldap_response( op, rs );
488 if ( op->o_tag == LDAP_REQ_SEARCH ) {
490 snprintf( nbuf, sizeof nbuf, "%d nentries=%d", rs->sr_err, rs->sr_nentries );
492 Statslog( LDAP_DEBUG_STATS,
493 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
494 op->o_connid, op->o_opid, rs->sr_tag, nbuf, rs->sr_text ? rs->sr_text : "" );
496 Statslog( LDAP_DEBUG_STATS,
497 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
498 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
509 send_ldap_sasl( Operation *op, SlapReply *rs )
511 rs->sr_type = REP_SASL;
513 LDAP_LOG( OPERATION, ENTRY,
514 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
515 op->o_connid, rs->sr_err, rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
517 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
518 rs->sr_err, rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
521 rs->sr_tag = req2res( op->o_tag );
522 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
524 send_ldap_response( op, rs );
528 slap_send_ldap_extended( Operation *op, SlapReply *rs )
530 rs->sr_type = REP_EXTENDED;
533 LDAP_LOG( OPERATION, ENTRY,
534 "send_ldap_extended: err=%d oid=%s len=%ld\n",
535 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
536 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
538 Debug( LDAP_DEBUG_TRACE,
539 "send_ldap_extended: err=%d oid=%s len=%ld\n",
541 rs->sr_rspoid ? rs->sr_rspoid : "",
542 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
545 rs->sr_tag = req2res( op->o_tag );
546 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
548 send_ldap_response( op, rs );
551 #ifdef LDAP_RES_INTERMEDIATE_RESP
553 slap_send_ldap_intermediate_resp( Operation *op, SlapReply *rs )
555 rs->sr_type = REP_EXTENDED;
557 LDAP_LOG( OPERATION, ENTRY,
558 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
559 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
560 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
562 Debug( LDAP_DEBUG_TRACE,
563 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
565 rs->sr_rspoid ? rs->sr_rspoid : "",
566 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
568 rs->sr_tag = LDAP_RES_INTERMEDIATE_RESP;
569 rs->sr_msgid = op->o_msgid;
570 send_ldap_response( op, rs );
575 slap_send_search_entry( Operation *op, SlapReply *rs )
577 char berbuf[LBER_ELEMENT_SIZEOF];
578 BerElement *ber = (BerElement *)berbuf;
580 int i, j, rc=-1, bytes;
584 AccessControlState acl_state = ACL_STATE_INIT;
586 /* Support for computed attribute plugins */
587 computed_attr_context ctx;
592 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
594 /* a_flags: array of flags telling if the i-th element will be
595 * returned or filtered out
596 * e_flags: array of a_flags
598 char **e_flags = NULL;
600 rs->sr_type = REP_SEARCH;
601 if (op->o_callback && op->o_callback->sc_response) {
602 return op->o_callback->sc_response( op, rs );
606 LDAP_LOG( OPERATION, ENTRY,
607 "send_search_entry: conn %lu dn=\"%s\"%s\n",
608 op->o_connid, rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "" );
610 Debug( LDAP_DEBUG_TRACE,
611 "=> send_search_entry: dn=\"%s\"%s\n",
612 rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
615 mark = sl_mark( op->o_tmpmemctx );
617 if ( ! access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL ) )
621 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
622 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
624 Debug( LDAP_DEBUG_ACL,
625 "send_search_entry: access to entry not allowed\n",
629 sl_release( mark, op->o_tmpmemctx );
633 edn = rs->sr_entry->e_nname.bv_val;
635 #ifdef LDAP_CONNECTIONLESS
636 if (op->o_conn && op->o_conn->c_is_udp)
644 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
645 bv.bv_len = siz + len;
646 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
648 ber_init2( ber, &bv, LBER_USE_DER );
649 ber_set_option( ber, LBER_OPT_BER_MEMCTX, op->o_tmpmemctx );
652 #ifdef LDAP_CONNECTIONLESS
653 if (op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
654 rc = ber_printf(ber, "t{O{" /*}}*/,
655 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name);
659 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
660 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
665 LDAP_LOG( OPERATION, ERR,
666 "send_search_entry: conn %lu ber_printf failed\n",
667 op->o_connid, 0, 0 );
669 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
672 #ifdef LDAP_CONNECTIONLESS
673 if (!op->o_conn || op->o_conn->c_is_udp == 0)
676 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
680 /* check for special all user attributes ("*") type */
681 userattrs = ( rs->sr_attrs == NULL ) ? 1
682 : an_find( rs->sr_attrs, &AllUser );
684 /* check for special all operational attributes ("+") type */
685 opattrs = ( rs->sr_attrs == NULL ) ? 0
686 : an_find( rs->sr_attrs, &AllOper );
688 /* create an array of arrays of flags. Each flag corresponds
689 * to particular value of attribute and equals 1 if value matches
690 * to ValuesReturnFilter or 0 if not
692 if ( op->vrFilter != NULL ) {
696 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
697 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
700 size = i * sizeof(char *) + k;
703 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
704 if( e_flags == NULL ) {
706 LDAP_LOG( OPERATION, ERR,
707 "send_search_entry: conn %lu sl_calloc failed\n",
708 op->o_connid : 0, 0, 0 );
710 Debug( LDAP_DEBUG_ANY,
711 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
715 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
718 a_flags = (char *)(e_flags + i);
719 memset( a_flags, 0, k );
720 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
721 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
722 e_flags[i] = a_flags;
726 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
729 LDAP_LOG( OPERATION, ERR,
730 "send_search_entry: conn %lu matched values filtering failed\n",
731 op->o_connid : 0, 0, 0 );
733 Debug( LDAP_DEBUG_ANY,
734 "matched values filtering failed\n", 0, 0, 0 );
736 #ifdef LDAP_CONNECTIONLESS
737 if (!op->o_conn || op->o_conn->c_is_udp == 0)
740 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
746 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
747 AttributeDescription *desc = a->a_desc;
749 if ( rs->sr_attrs == NULL ) {
750 /* all attrs request, skip operational attributes */
751 if( is_at_operational( desc->ad_type ) ) {
756 /* specific attrs requested */
757 if ( is_at_operational( desc->ad_type ) ) {
758 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
763 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
769 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
770 ACL_READ, &acl_state ) )
774 "send_search_entry: conn %lu access to attribute %s not "
775 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
777 Debug( LDAP_DEBUG_ACL, "acl: "
778 "access to attribute %s not allowed\n",
779 desc->ad_cname.bv_val, 0, 0 );
784 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
786 LDAP_LOG( OPERATION, ERR,
787 "send_search_entry: conn %lu ber_printf failed\n",
788 op->o_connid, 0, 0 );
790 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
793 #ifdef LDAP_CONNECTIONLESS
794 if (!op->o_conn || op->o_conn->c_is_udp == 0)
797 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
801 if ( ! op->ors_attrsonly ) {
802 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
803 if ( ! access_allowed( op, rs->sr_entry,
804 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
808 "send_search_entry: conn %lu "
809 "access to attribute %s, value %d not allowed\n",
810 op->o_connid, desc->ad_cname.bv_val, i );
812 Debug( LDAP_DEBUG_ACL,
813 "acl: access to attribute %s, "
814 "value %d not allowed\n",
815 desc->ad_cname.bv_val, i, 0 );
821 if ( op->vrFilter && e_flags[j][i] == 0 ){
825 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
827 LDAP_LOG( OPERATION, ERR,
828 "send_search_entry: conn %lu "
829 "ber_printf failed.\n", op->o_connid, 0, 0 );
831 Debug( LDAP_DEBUG_ANY,
832 "ber_printf failed\n", 0, 0, 0 );
835 #ifdef LDAP_CONNECTIONLESS
836 if (!op->o_conn || op->o_conn->c_is_udp == 0)
839 send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
845 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
847 LDAP_LOG( OPERATION, ERR,
848 "send_search_entry: conn %lu ber_printf failed\n",
849 op->o_connid, 0, 0 );
851 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
854 #ifdef LDAP_CONNECTIONLESS
855 if (!op->o_conn || op->o_conn->c_is_udp == 0)
858 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
863 /* eventually will loop through generated operational attributes */
864 /* only have subschemaSubentry implemented */
865 aa = backend_operational( op, rs, opattrs );
867 if ( aa != NULL && op->vrFilter != NULL ) {
871 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
872 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
875 size = i * sizeof(char *) + k;
877 char *a_flags, **tmp;
880 * Reuse previous memory - we likely need less space
881 * for operational attributes
883 tmp = sl_realloc( e_flags, i * sizeof(char *) + k, op->o_tmpmemctx );
886 LDAP_LOG( OPERATION, ERR,
887 "send_search_entry: conn %lu "
889 "for matched values filtering\n",
892 Debug( LDAP_DEBUG_ANY,
893 "send_search_entry: conn %lu "
895 "for matched values filtering\n",
896 op->o_connid, 0, 0 );
900 send_ldap_error( op, rs, LDAP_OTHER, "not enough memory for matched values filtering" );
904 a_flags = (char *)(e_flags + i);
905 memset( a_flags, 0, k );
906 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
907 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
908 e_flags[i] = a_flags;
911 rc = filter_matched_values(op, aa, &e_flags) ;
915 LDAP_LOG( OPERATION, ERR,
916 "send_search_entry: conn %lu "
917 "matched values filtering failed\n",
918 op->o_connid : 0, 0, 0);
920 Debug( LDAP_DEBUG_ANY,
921 "matched values filtering failed\n", 0, 0, 0 );
923 #ifdef LDAP_CONNECTIONLESS
924 if (!op->o_conn || op->o_conn->c_is_udp == 0)
928 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
934 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
935 AttributeDescription *desc = a->a_desc;
937 if ( rs->sr_attrs == NULL ) {
938 /* all attrs request, skip operational attributes */
939 if( is_at_operational( desc->ad_type ) ) {
944 /* specific attrs requested */
945 if( is_at_operational( desc->ad_type ) ) {
946 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
950 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) )
957 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
958 ACL_READ, &acl_state ) )
962 "send_search_entry: conn %lu "
963 "access to attribute %s not allowed\n",
964 op->o_connid, desc->ad_cname.bv_val, 0 );
966 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
968 desc->ad_cname.bv_val, 0, 0 );
974 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
977 LDAP_LOG( OPERATION, ERR,
978 "send_search_entry: conn %lu "
979 "ber_printf failed\n", op->o_connid, 0, 0 );
981 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
984 #ifdef LDAP_CONNECTIONLESS
985 if (!op->o_conn || op->o_conn->c_is_udp == 0)
988 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
994 if ( ! op->ors_attrsonly ) {
995 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
996 if ( ! access_allowed( op, rs->sr_entry,
997 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1000 LDAP_LOG( ACL, INFO,
1001 "send_search_entry: conn %lu "
1002 "access to %s, value %d not allowed\n",
1003 op->o_connid, desc->ad_cname.bv_val, i );
1005 Debug( LDAP_DEBUG_ACL,
1006 "send_search_entry: access to attribute %s, "
1007 "value %d not allowed\n",
1008 desc->ad_cname.bv_val, i, 0 );
1014 if ( op->vrFilter && e_flags[j][i] == 0 ){
1018 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1020 LDAP_LOG( OPERATION, ERR,
1021 "send_search_entry: conn %lu ber_printf failed\n",
1022 op->o_connid, 0, 0 );
1024 Debug( LDAP_DEBUG_ANY,
1025 "ber_printf failed\n", 0, 0, 0 );
1028 #ifdef LDAP_CONNECTIONLESS
1029 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1031 ber_free_buf( ber );
1032 send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
1039 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1041 LDAP_LOG( OPERATION, ERR,
1042 "send_search_entry: conn %lu ber_printf failed\n",
1043 op->o_connid, 0, 0 );
1045 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1048 #ifdef LDAP_CONNECTIONLESS
1049 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1051 ber_free_buf( ber );
1052 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1061 * First, setup the computed attribute context that is
1062 * passed to all plugins.
1064 ctx.cac_pb = op->o_pb;
1065 ctx.cac_attrs = rs->sr_attrs;
1066 ctx.cac_attrsonly = op->ors_attrsonly;
1067 ctx.cac_userattrs = userattrs;
1068 ctx.cac_opattrs = opattrs;
1069 ctx.cac_acl_state = acl_state;
1070 ctx.cac_private = (void *)ber;
1073 * For each client requested attribute, call the plugins.
1075 if ( rs->sr_attrs != NULL ) {
1076 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1077 rc = compute_evaluator( &ctx, anp->an_name.bv_val, rs->sr_entry, slapi_x_compute_output_ber );
1084 * Technically we shouldn't be returning operational attributes
1085 * when the user requested only user attributes. We'll let the
1086 * plugin decide whether to be naughty or not.
1088 rc = compute_evaluator( &ctx, "*", rs->sr_entry, slapi_x_compute_output_ber );
1091 ber_free_buf( ber );
1092 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1095 #endif /* LDAP_SLAPI */
1099 sl_free( e_flags, op->o_tmpmemctx );
1104 rc = ber_printf( ber, /*{{*/ "}N}" );
1106 if( rc != -1 && rs->sr_ctrls != NULL ) {
1107 rc = send_ldap_controls( ber, rs->sr_ctrls );
1110 #ifdef LDAP_CONNECTIONLESS
1111 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 ) {
1112 ; /* empty, skip following if */
1116 rc = ber_printf( ber, /*{*/ "N}" );
1121 LDAP_LOG( OPERATION, ERR,
1122 "send_search_entry: conn %lu ber_printf failed\n",
1123 op->o_connid, 0, 0 );
1125 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1128 #ifdef LDAP_CONNECTIONLESS
1129 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1131 ber_free_buf( ber );
1132 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1133 sl_release( mark, op->o_tmpmemctx );
1137 #ifdef LDAP_CONNECTIONLESS
1138 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1140 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1141 ber_free_buf( ber );
1145 LDAP_LOG( OPERATION, ERR,
1146 "send_search_entry: conn %lu ber write failed.\n",
1147 op->o_connid, 0, 0 );
1149 Debug( LDAP_DEBUG_ANY,
1150 "send_search_entry: ber write failed\n",
1154 sl_release( mark, op->o_tmpmemctx );
1159 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1160 num_bytes_sent += bytes;
1163 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1165 #ifdef LDAP_CONNECTIONLESS
1169 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1170 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1173 LDAP_LOG( OPERATION, ENTRY,
1174 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1176 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1182 sl_release( mark, op->o_tmpmemctx );
1183 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1188 slap_send_search_reference( Operation *op, SlapReply *rs )
1190 char berbuf[LBER_ELEMENT_SIZEOF];
1191 BerElement *ber = (BerElement *)berbuf;
1195 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1196 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1198 rs->sr_type = REP_SEARCHREF;
1199 if (op->o_callback && op->o_callback->sc_response) {
1200 return op->o_callback->sc_response( op, rs );
1204 LDAP_LOG( OPERATION, ENTRY,
1205 "send_search_reference: conn %lu dn=\"%s\"\n",
1206 op->o_connid, rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1208 Debug( LDAP_DEBUG_TRACE,
1209 "=> send_search_reference: dn=\"%s\"\n",
1210 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1213 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1214 ad_entry, NULL, ACL_READ, NULL ) )
1217 LDAP_LOG( ACL, INFO,
1218 "send_search_reference: conn %lu "
1219 "access to entry %s not allowed\n",
1220 op->o_connid, rs->sr_entry->e_dn, 0 );
1222 Debug( LDAP_DEBUG_ACL,
1223 "send_search_reference: access to entry not allowed\n",
1230 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1231 ad_ref, NULL, ACL_READ, NULL ) )
1234 LDAP_LOG( ACL, INFO,
1235 "send_search_reference: conn %lu access "
1236 "to reference not allowed.\n", op->o_connid, 0, 0 );
1238 Debug( LDAP_DEBUG_ACL,
1239 "send_search_reference: access "
1240 "to reference not allowed\n",
1247 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1248 if( op->o_domain_scope ) {
1250 LDAP_LOG( OPERATION, ERR,
1251 "send_search_reference: conn %lu domainScope control in (%s).\n",
1252 op->o_connid, rs->sr_entry->e_dn, 0 );
1254 Debug( LDAP_DEBUG_ANY,
1255 "send_search_reference: domainScope control in (%s)\n",
1256 rs->sr_entry->e_dn, 0, 0 );
1263 if( rs->sr_ref == NULL ) {
1265 LDAP_LOG( OPERATION, ERR,
1266 "send_search_reference: conn %lu null ref in (%s).\n",
1267 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1269 Debug( LDAP_DEBUG_ANY,
1270 "send_search_reference: null ref in (%s)\n",
1271 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1277 if( op->o_protocol < LDAP_VERSION3 ) {
1278 /* save the references for the result */
1279 if( rs->sr_ref[0].bv_val != NULL ) {
1280 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1286 #ifdef LDAP_CONNECTIONLESS
1287 if (op->o_conn && op->o_conn->c_is_udp)
1288 ber = op->o_res_ber;
1291 ber_init_w_nullc( ber, LBER_USE_DER );
1293 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1294 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1296 if( rc != -1 && rs->sr_ctrls != NULL ) {
1297 rc = send_ldap_controls( ber, rs->sr_ctrls );
1301 rc = ber_printf( ber, /*"{"*/ "N}" );
1306 LDAP_LOG( OPERATION, ERR,
1307 "send_search_reference: conn %lu "
1308 "ber_printf failed.\n", op->o_connid, 0, 0 );
1310 Debug( LDAP_DEBUG_ANY,
1311 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1314 #ifdef LDAP_CONNECTIONLESS
1315 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1317 ber_free_buf( ber );
1318 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1322 #ifdef LDAP_CONNECTIONLESS
1323 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1325 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1326 ber_free_buf( ber );
1328 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1329 num_bytes_sent += bytes;
1332 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1333 #ifdef LDAP_CONNECTIONLESS
1337 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1338 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1341 LDAP_LOG( OPERATION, ENTRY,
1342 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1344 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1361 *code = LDAP_SUCCESS;
1365 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1367 LDAP_LOG( OPERATION, INFO,
1368 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1370 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1378 while ( (s = strchr( s, '\n' )) != NULL ) {
1383 if ( (c = strchr( s, ':' )) != NULL ) {
1387 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1391 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1395 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1401 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1403 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",