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;
591 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
593 /* a_flags: array of flags telling if the i-th element will be
594 * returned or filtered out
595 * e_flags: array of a_flags
597 char **e_flags = NULL;
599 rs->sr_type = REP_SEARCH;
600 if (op->o_callback && op->o_callback->sc_response) {
601 return op->o_callback->sc_response( op, rs );
605 LDAP_LOG( OPERATION, ENTRY,
606 "send_search_entry: conn %lu dn=\"%s\"%s\n",
607 op->o_connid, rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "" );
609 Debug( LDAP_DEBUG_TRACE,
610 "=> send_search_entry: dn=\"%s\"%s\n",
611 rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
614 if ( ! access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL ) )
618 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
619 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
621 Debug( LDAP_DEBUG_ACL,
622 "send_search_entry: access to entry not allowed\n",
629 edn = rs->sr_entry->e_nname.bv_val;
631 #ifdef LDAP_CONNECTIONLESS
632 if (op->o_conn && op->o_conn->c_is_udp)
636 ber_init_w_nullc( ber, LBER_USE_DER );
638 #ifdef LDAP_CONNECTIONLESS
639 if (op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
640 rc = ber_printf(ber, "t{O{" /*}}*/,
641 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name);
645 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
646 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
651 LDAP_LOG( OPERATION, ERR,
652 "send_search_entry: conn %lu ber_printf failed\n",
653 op->o_connid, 0, 0 );
655 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
658 #ifdef LDAP_CONNECTIONLESS
659 if (!op->o_conn || op->o_conn->c_is_udp == 0)
662 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
666 /* check for special all user attributes ("*") type */
667 userattrs = ( rs->sr_attrs == NULL ) ? 1
668 : an_find( rs->sr_attrs, &AllUser );
670 /* check for special all operational attributes ("+") type */
671 opattrs = ( rs->sr_attrs == NULL ) ? 0
672 : an_find( rs->sr_attrs, &AllOper );
674 /* create an array of arrays of flags. Each flag corresponds
675 * to particular value of attribute and equals 1 if value matches
676 * to ValuesReturnFilter or 0 if not
678 if ( op->vrFilter != NULL ) {
682 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
683 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
686 size = i * sizeof(char *) + k;
689 e_flags = SLAP_CALLOC ( 1, i * sizeof(char *) + k );
690 if( e_flags == NULL ) {
692 LDAP_LOG( OPERATION, ERR,
693 "send_search_entry: conn %lu SLAP_CALLOC failed\n",
694 op->o_connid : 0, 0, 0 );
696 Debug( LDAP_DEBUG_ANY,
697 "send_search_entry: SLAP_CALLOC failed\n", 0, 0, 0 );
701 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
704 a_flags = (char *)(e_flags + i);
705 memset( a_flags, 0, k );
706 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
707 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
708 e_flags[i] = a_flags;
712 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
715 LDAP_LOG( OPERATION, ERR,
716 "send_search_entry: conn %lu matched values filtering failed\n",
717 op->o_connid : 0, 0, 0 );
719 Debug( LDAP_DEBUG_ANY,
720 "matched values filtering failed\n", 0, 0, 0 );
722 #ifdef LDAP_CONNECTIONLESS
723 if (!op->o_conn || op->o_conn->c_is_udp == 0)
726 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
732 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
733 AttributeDescription *desc = a->a_desc;
735 if ( rs->sr_attrs == NULL ) {
736 /* all attrs request, skip operational attributes */
737 if( is_at_operational( desc->ad_type ) ) {
742 /* specific attrs requested */
743 if ( is_at_operational( desc->ad_type ) ) {
744 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
749 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
755 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
756 ACL_READ, &acl_state ) )
760 "send_search_entry: conn %lu access to attribute %s not "
761 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
763 Debug( LDAP_DEBUG_ACL, "acl: "
764 "access to attribute %s not allowed\n",
765 desc->ad_cname.bv_val, 0, 0 );
770 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
772 LDAP_LOG( OPERATION, ERR,
773 "send_search_entry: conn %lu ber_printf failed\n",
774 op->o_connid, 0, 0 );
776 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
779 #ifdef LDAP_CONNECTIONLESS
780 if (!op->o_conn || op->o_conn->c_is_udp == 0)
783 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
787 if ( ! op->ors_attrsonly ) {
788 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
789 if ( ! access_allowed( op, rs->sr_entry,
790 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
794 "send_search_entry: conn %lu "
795 "access to attribute %s, value %d not allowed\n",
796 op->o_connid, desc->ad_cname.bv_val, i );
798 Debug( LDAP_DEBUG_ACL,
799 "acl: access to attribute %s, "
800 "value %d not allowed\n",
801 desc->ad_cname.bv_val, i, 0 );
807 if ( op->vrFilter && e_flags[j][i] == 0 ){
811 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
813 LDAP_LOG( OPERATION, ERR,
814 "send_search_entry: conn %lu "
815 "ber_printf failed.\n", op->o_connid, 0, 0 );
817 Debug( LDAP_DEBUG_ANY,
818 "ber_printf failed\n", 0, 0, 0 );
821 #ifdef LDAP_CONNECTIONLESS
822 if (!op->o_conn || op->o_conn->c_is_udp == 0)
825 send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
831 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
833 LDAP_LOG( OPERATION, ERR,
834 "send_search_entry: conn %lu ber_printf failed\n",
835 op->o_connid, 0, 0 );
837 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
840 #ifdef LDAP_CONNECTIONLESS
841 if (!op->o_conn || op->o_conn->c_is_udp == 0)
844 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
849 /* eventually will loop through generated operational attributes */
850 /* only have subschemaSubentry implemented */
851 aa = backend_operational( op, rs, opattrs );
853 if ( aa != NULL && op->vrFilter != NULL ) {
857 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
858 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
861 size = i * sizeof(char *) + k;
863 char *a_flags, **tmp;
866 * Reuse previous memory - we likely need less space
867 * for operational attributes
869 tmp = SLAP_REALLOC ( e_flags, i * sizeof(char *) + k );
872 LDAP_LOG( OPERATION, ERR,
873 "send_search_entry: conn %lu "
875 "for matched values filtering\n",
878 Debug( LDAP_DEBUG_ANY,
879 "send_search_entry: conn %lu "
881 "for matched values filtering\n",
882 op->o_connid, 0, 0 );
886 send_ldap_error( op, rs, LDAP_OTHER, "not enough memory for matched values filtering" );
890 a_flags = (char *)(e_flags + i);
891 memset( a_flags, 0, k );
892 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
893 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
894 e_flags[i] = a_flags;
897 rc = filter_matched_values(op, aa, &e_flags) ;
901 LDAP_LOG( OPERATION, ERR,
902 "send_search_entry: conn %lu "
903 "matched values filtering failed\n",
904 op->o_connid : 0, 0, 0);
906 Debug( LDAP_DEBUG_ANY,
907 "matched values filtering failed\n", 0, 0, 0 );
909 #ifdef LDAP_CONNECTIONLESS
910 if (!op->o_conn || op->o_conn->c_is_udp == 0)
914 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
920 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
921 AttributeDescription *desc = a->a_desc;
923 if ( rs->sr_attrs == NULL ) {
924 /* all attrs request, skip operational attributes */
925 if( is_at_operational( desc->ad_type ) ) {
930 /* specific attrs requested */
931 if( is_at_operational( desc->ad_type ) ) {
932 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
936 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) )
943 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
944 ACL_READ, &acl_state ) )
948 "send_search_entry: conn %lu "
949 "access to attribute %s not allowed\n",
950 op->o_connid, desc->ad_cname.bv_val, 0 );
952 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
954 desc->ad_cname.bv_val, 0, 0 );
960 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
963 LDAP_LOG( OPERATION, ERR,
964 "send_search_entry: conn %lu "
965 "ber_printf failed\n", op->o_connid, 0, 0 );
967 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
970 #ifdef LDAP_CONNECTIONLESS
971 if (!op->o_conn || op->o_conn->c_is_udp == 0)
974 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
980 if ( ! op->ors_attrsonly ) {
981 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
982 if ( ! access_allowed( op, rs->sr_entry,
983 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
987 "send_search_entry: conn %lu "
988 "access to %s, value %d not allowed\n",
989 op->o_connid, desc->ad_cname.bv_val, i );
991 Debug( LDAP_DEBUG_ACL,
992 "send_search_entry: access to attribute %s, "
993 "value %d not allowed\n",
994 desc->ad_cname.bv_val, i, 0 );
1000 if ( op->vrFilter && e_flags[j][i] == 0 ){
1004 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1006 LDAP_LOG( OPERATION, ERR,
1007 "send_search_entry: conn %lu ber_printf failed\n",
1008 op->o_connid, 0, 0 );
1010 Debug( LDAP_DEBUG_ANY,
1011 "ber_printf failed\n", 0, 0, 0 );
1014 #ifdef LDAP_CONNECTIONLESS
1015 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1017 ber_free_buf( ber );
1018 send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
1025 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1027 LDAP_LOG( OPERATION, ERR,
1028 "send_search_entry: conn %lu ber_printf failed\n",
1029 op->o_connid, 0, 0 );
1031 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1034 #ifdef LDAP_CONNECTIONLESS
1035 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1037 ber_free_buf( ber );
1038 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1047 * First, setup the computed attribute context that is
1048 * passed to all plugins.
1050 ctx.cac_pb = op->o_pb;
1051 ctx.cac_attrs = rs->sr_attrs;
1052 ctx.cac_attrsonly = op->ors_attrsonly;
1053 ctx.cac_userattrs = userattrs;
1054 ctx.cac_opattrs = opattrs;
1055 ctx.cac_acl_state = acl_state;
1056 ctx.cac_private = (void *)ber;
1059 * For each client requested attribute, call the plugins.
1061 if ( rs->sr_attrs != NULL ) {
1062 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1063 rc = compute_evaluator( &ctx, anp->an_name.bv_val, rs->sr_entry, slapi_x_compute_output_ber );
1070 * Technically we shouldn't be returning operational attributes
1071 * when the user requested only user attributes. We'll let the
1072 * plugin decide whether to be naughty or not.
1074 rc = compute_evaluator( &ctx, "*", rs->sr_entry, slapi_x_compute_output_ber );
1077 ber_free_buf( ber );
1078 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1081 #endif /* LDAP_SLAPI */
1090 rc = ber_printf( ber, /*{{*/ "}N}" );
1092 if( rc != -1 && rs->sr_ctrls != NULL ) {
1093 rc = send_ldap_controls( ber, rs->sr_ctrls );
1096 #ifdef LDAP_CONNECTIONLESS
1097 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 ) {
1098 ; /* empty, skip following if */
1102 rc = ber_printf( ber, /*{*/ "N}" );
1107 LDAP_LOG( OPERATION, ERR,
1108 "send_search_entry: conn %lu ber_printf failed\n",
1109 op->o_connid, 0, 0 );
1111 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1114 #ifdef LDAP_CONNECTIONLESS
1115 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1117 ber_free_buf( ber );
1118 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1122 #ifdef LDAP_CONNECTIONLESS
1123 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1125 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1126 ber_free_buf( ber );
1130 LDAP_LOG( OPERATION, ERR,
1131 "send_search_entry: conn %lu ber write failed.\n",
1132 op->o_connid, 0, 0 );
1134 Debug( LDAP_DEBUG_ANY,
1135 "send_search_entry: ber write failed\n",
1143 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1144 num_bytes_sent += bytes;
1147 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1149 #ifdef LDAP_CONNECTIONLESS
1153 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1154 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1157 LDAP_LOG( OPERATION, ENTRY,
1158 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1160 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1166 if ( e_flags ) free( e_flags );
1171 slap_send_search_reference( Operation *op, SlapReply *rs )
1173 char berbuf[LBER_ELEMENT_SIZEOF];
1174 BerElement *ber = (BerElement *)berbuf;
1178 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1179 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1181 rs->sr_type = REP_SEARCHREF;
1182 if (op->o_callback && op->o_callback->sc_response) {
1183 return op->o_callback->sc_response( op, rs );
1187 LDAP_LOG( OPERATION, ENTRY,
1188 "send_search_reference: conn %lu dn=\"%s\"\n",
1189 op->o_connid, rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1191 Debug( LDAP_DEBUG_TRACE,
1192 "=> send_search_reference: dn=\"%s\"\n",
1193 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1196 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1197 ad_entry, NULL, ACL_READ, NULL ) )
1200 LDAP_LOG( ACL, INFO,
1201 "send_search_reference: conn %lu "
1202 "access to entry %s not allowed\n",
1203 op->o_connid, rs->sr_entry->e_dn, 0 );
1205 Debug( LDAP_DEBUG_ACL,
1206 "send_search_reference: access to entry not allowed\n",
1213 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1214 ad_ref, NULL, ACL_READ, NULL ) )
1217 LDAP_LOG( ACL, INFO,
1218 "send_search_reference: conn %lu access "
1219 "to reference not allowed.\n", op->o_connid, 0, 0 );
1221 Debug( LDAP_DEBUG_ACL,
1222 "send_search_reference: access "
1223 "to reference not allowed\n",
1230 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1231 if( op->o_domain_scope ) {
1233 LDAP_LOG( OPERATION, ERR,
1234 "send_search_reference: conn %lu domainScope control in (%s).\n",
1235 op->o_connid, rs->sr_entry->e_dn, 0 );
1237 Debug( LDAP_DEBUG_ANY,
1238 "send_search_reference: domainScope control in (%s)\n",
1239 rs->sr_entry->e_dn, 0, 0 );
1246 if( rs->sr_ref == NULL ) {
1248 LDAP_LOG( OPERATION, ERR,
1249 "send_search_reference: conn %lu null ref in (%s).\n",
1250 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1252 Debug( LDAP_DEBUG_ANY,
1253 "send_search_reference: null ref in (%s)\n",
1254 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1260 if( op->o_protocol < LDAP_VERSION3 ) {
1261 /* save the references for the result */
1262 if( rs->sr_ref[0].bv_val != NULL ) {
1263 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1269 #ifdef LDAP_CONNECTIONLESS
1270 if (op->o_conn && op->o_conn->c_is_udp)
1271 ber = op->o_res_ber;
1274 ber_init_w_nullc( ber, LBER_USE_DER );
1276 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1277 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1279 if( rc != -1 && rs->sr_ctrls != NULL ) {
1280 rc = send_ldap_controls( ber, rs->sr_ctrls );
1284 rc = ber_printf( ber, /*"{"*/ "N}" );
1289 LDAP_LOG( OPERATION, ERR,
1290 "send_search_reference: conn %lu "
1291 "ber_printf failed.\n", op->o_connid, 0, 0 );
1293 Debug( LDAP_DEBUG_ANY,
1294 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1297 #ifdef LDAP_CONNECTIONLESS
1298 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1300 ber_free_buf( ber );
1301 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1305 #ifdef LDAP_CONNECTIONLESS
1306 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1308 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1309 ber_free_buf( ber );
1311 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1312 num_bytes_sent += bytes;
1315 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1316 #ifdef LDAP_CONNECTIONLESS
1320 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1321 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1324 LDAP_LOG( OPERATION, ENTRY,
1325 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1327 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1344 *code = LDAP_SUCCESS;
1348 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1350 LDAP_LOG( OPERATION, INFO,
1351 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1353 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1361 while ( (s = strchr( s, '\n' )) != NULL ) {
1366 if ( (c = strchr( s, ':' )) != NULL ) {
1370 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1374 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1378 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1384 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1386 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",