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 assert( !LDAP_API_ERROR( rs->sr_err ) );
427 LDAP_LOG( OPERATION, ENTRY,
428 "send_ldap_result: conn %lu op=%lu p=%d\n",
429 op->o_connid, op->o_opid, op->o_protocol );
431 Debug( LDAP_DEBUG_TRACE,
432 "send_ldap_result: conn=%lu op=%lu p=%d\n",
433 op->o_connid, op->o_opid, op->o_protocol );
437 LDAP_LOG( OPERATION, ARGS,
438 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
439 rs->sr_err, rs->sr_matched ? rs->sr_matched : "", rs->sr_text ? rs->sr_text : "" );
441 Debug( LDAP_DEBUG_ARGS,
442 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
443 rs->sr_err, rs->sr_matched ? rs->sr_matched : "", rs->sr_text ? rs->sr_text : "" );
449 LDAP_LOG( OPERATION, ARGS,
450 "send_ldap_result: referral=\"%s\"\n",
451 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
453 Debug( LDAP_DEBUG_ARGS,
454 "send_ldap_result: referral=\"%s\"\n",
455 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
460 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
462 if ( rs->sr_err == LDAP_REFERRAL ) {
463 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
464 if( op->o_domain_scope ) {
468 if( rs->sr_ref == NULL ) {
469 rs->sr_err = LDAP_NO_SUCH_OBJECT;
470 } else if ( op->o_protocol < LDAP_VERSION3 ) {
471 rs->sr_err = LDAP_PARTIAL_RESULTS;
475 if ( op->o_protocol < LDAP_VERSION3 ) {
476 tmp = v2ref( rs->sr_ref, rs->sr_text );
481 rs->sr_tag = req2res( op->o_tag );
482 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
484 send_ldap_response( op, rs );
486 if ( rs->sr_type == REP_SRESULT ) {
488 snprintf( nbuf, sizeof nbuf, "%d nentries=%d", rs->sr_err, rs->sr_nentries );
490 Statslog( LDAP_DEBUG_STATS,
491 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
492 op->o_connid, op->o_opid, rs->sr_tag, nbuf, rs->sr_text ? rs->sr_text : "" );
494 Statslog( LDAP_DEBUG_STATS,
495 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
496 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
507 send_ldap_sasl( Operation *op, SlapReply *rs )
509 rs->sr_type = REP_SASL;
511 LDAP_LOG( OPERATION, ENTRY,
512 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
513 op->o_connid, rs->sr_err, rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
515 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
516 rs->sr_err, rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
519 rs->sr_tag = req2res( op->o_tag );
520 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
522 send_ldap_response( op, rs );
526 slap_send_ldap_extended( Operation *op, SlapReply *rs )
528 rs->sr_type = REP_EXTENDED;
531 LDAP_LOG( OPERATION, ENTRY,
532 "send_ldap_extended: err=%d oid=%s len=%ld\n",
533 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
534 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
536 Debug( LDAP_DEBUG_TRACE,
537 "send_ldap_extended: err=%d oid=%s len=%ld\n",
539 rs->sr_rspoid ? rs->sr_rspoid : "",
540 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
543 rs->sr_tag = req2res( op->o_tag );
544 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
546 send_ldap_response( op, rs );
549 #ifdef LDAP_RES_INTERMEDIATE_RESP
551 slap_send_ldap_intermediate_resp( Operation *op, SlapReply *rs )
553 rs->sr_type = REP_EXTENDED;
555 LDAP_LOG( OPERATION, ENTRY,
556 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
557 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
558 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
560 Debug( LDAP_DEBUG_TRACE,
561 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
563 rs->sr_rspoid ? rs->sr_rspoid : "",
564 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
566 rs->sr_tag = LDAP_RES_INTERMEDIATE_RESP;
567 rs->sr_msgid = op->o_msgid;
568 send_ldap_response( op, rs );
573 slap_send_search_result( Operation *op, SlapReply *rs )
575 rs->sr_type = REP_SRESULT;
576 slap_send_ldap_result( op, rs );
580 slap_send_search_entry( Operation *op, SlapReply *rs )
582 char berbuf[LBER_ELEMENT_SIZEOF];
583 BerElement *ber = (BerElement *)berbuf;
585 int i, j, rc=-1, bytes;
589 AccessControlState acl_state = ACL_STATE_INIT;
591 /* Support for computed attribute plugins */
592 computed_attr_context ctx;
596 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
598 /* a_flags: array of flags telling if the i-th element will be
599 * returned or filtered out
600 * e_flags: array of a_flags
602 char **e_flags = NULL;
604 rs->sr_type = REP_SEARCH;
605 if (op->o_callback && op->o_callback->sc_sendentry) {
606 return op->o_callback->sc_sendentry( op, rs );
610 LDAP_LOG( OPERATION, ENTRY,
611 "send_search_entry: conn %lu dn=\"%s\"%s\n",
612 op->o_connid, rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "" );
614 Debug( LDAP_DEBUG_TRACE,
615 "=> send_search_entry: dn=\"%s\"%s\n",
616 rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
619 if ( ! access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL ) )
623 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
624 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
626 Debug( LDAP_DEBUG_ACL,
627 "send_search_entry: access to entry not allowed\n",
634 edn = rs->sr_entry->e_nname.bv_val;
636 #ifdef LDAP_CONNECTIONLESS
637 if (op->o_conn && op->o_conn->c_is_udp)
641 ber_init_w_nullc( ber, LBER_USE_DER );
643 #ifdef LDAP_CONNECTIONLESS
644 if (op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
645 rc = ber_printf(ber, "t{O{" /*}}*/,
646 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name);
650 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
651 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
656 LDAP_LOG( OPERATION, ERR,
657 "send_search_entry: conn %lu ber_printf failed\n",
658 op->o_connid, 0, 0 );
660 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
663 #ifdef LDAP_CONNECTIONLESS
664 if (!op->o_conn || op->o_conn->c_is_udp == 0)
667 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
671 /* check for special all user attributes ("*") type */
672 userattrs = ( rs->sr_attrs == NULL ) ? 1
673 : an_find( rs->sr_attrs, &AllUser );
675 /* check for special all operational attributes ("+") type */
676 opattrs = ( rs->sr_attrs == NULL ) ? 0
677 : an_find( rs->sr_attrs, &AllOper );
679 /* create an array of arrays of flags. Each flag corresponds
680 * to particular value of attribute and equals 1 if value matches
681 * to ValuesReturnFilter or 0 if not
683 if ( op->vrFilter != NULL ) {
687 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
688 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
691 size = i * sizeof(char *) + k;
694 e_flags = SLAP_CALLOC ( 1, i * sizeof(char *) + k );
695 if( e_flags == NULL ) {
697 LDAP_LOG( OPERATION, ERR,
698 "send_search_entry: conn %lu SLAP_CALLOC failed\n",
699 op->o_connid : 0, 0, 0 );
701 Debug( LDAP_DEBUG_ANY,
702 "send_search_entry: SLAP_CALLOC failed\n", 0, 0, 0 );
706 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
709 a_flags = (char *)(e_flags + i);
710 memset( a_flags, 0, k );
711 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
712 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
713 e_flags[i] = a_flags;
717 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
720 LDAP_LOG( OPERATION, ERR,
721 "send_search_entry: conn %lu matched values filtering failed\n",
722 op->o_connid : 0, 0, 0 );
724 Debug( LDAP_DEBUG_ANY,
725 "matched values filtering failed\n", 0, 0, 0 );
727 #ifdef LDAP_CONNECTIONLESS
728 if (!op->o_conn || op->o_conn->c_is_udp == 0)
731 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
737 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
738 AttributeDescription *desc = a->a_desc;
740 if ( rs->sr_attrs == NULL ) {
741 /* all attrs request, skip operational attributes */
742 if( is_at_operational( desc->ad_type ) ) {
747 /* specific attrs requested */
748 if ( is_at_operational( desc->ad_type ) ) {
749 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
754 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
760 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
761 ACL_READ, &acl_state ) )
765 "send_search_entry: conn %lu access to attribute %s not "
766 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
768 Debug( LDAP_DEBUG_ACL, "acl: "
769 "access to attribute %s not allowed\n",
770 desc->ad_cname.bv_val, 0, 0 );
775 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
777 LDAP_LOG( OPERATION, ERR,
778 "send_search_entry: conn %lu ber_printf failed\n",
779 op->o_connid, 0, 0 );
781 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
784 #ifdef LDAP_CONNECTIONLESS
785 if (!op->o_conn || op->o_conn->c_is_udp == 0)
788 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
792 if ( ! op->ors_attrsonly ) {
793 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
794 if ( ! access_allowed( op, rs->sr_entry,
795 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
799 "send_search_entry: conn %lu "
800 "access to attribute %s, value %d not allowed\n",
801 op->o_connid, desc->ad_cname.bv_val, i );
803 Debug( LDAP_DEBUG_ACL,
804 "acl: access to attribute %s, "
805 "value %d not allowed\n",
806 desc->ad_cname.bv_val, i, 0 );
812 if ( op->vrFilter && e_flags[j][i] == 0 ){
816 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
818 LDAP_LOG( OPERATION, ERR,
819 "send_search_entry: conn %lu "
820 "ber_printf failed.\n", op->o_connid, 0, 0 );
822 Debug( LDAP_DEBUG_ANY,
823 "ber_printf failed\n", 0, 0, 0 );
826 #ifdef LDAP_CONNECTIONLESS
827 if (!op->o_conn || op->o_conn->c_is_udp == 0)
830 send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
836 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
838 LDAP_LOG( OPERATION, ERR,
839 "send_search_entry: conn %lu ber_printf failed\n",
840 op->o_connid, 0, 0 );
842 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
845 #ifdef LDAP_CONNECTIONLESS
846 if (!op->o_conn || op->o_conn->c_is_udp == 0)
849 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
854 /* eventually will loop through generated operational attributes */
855 /* only have subschemaSubentry implemented */
856 aa = backend_operational( op, rs, opattrs );
858 if ( aa != NULL && op->vrFilter != NULL ) {
862 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
863 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
866 size = i * sizeof(char *) + k;
868 char *a_flags, **tmp;
871 * Reuse previous memory - we likely need less space
872 * for operational attributes
874 tmp = SLAP_REALLOC ( e_flags, i * sizeof(char *) + k );
877 LDAP_LOG( OPERATION, ERR,
878 "send_search_entry: conn %lu "
880 "for matched values filtering\n",
883 Debug( LDAP_DEBUG_ANY,
884 "send_search_entry: conn %lu "
886 "for matched values filtering\n",
887 op->o_connid, 0, 0 );
891 send_ldap_error( op, rs, LDAP_OTHER, "not enough memory for matched values filtering" );
895 a_flags = (char *)(e_flags + i);
896 memset( a_flags, 0, k );
897 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
898 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
899 e_flags[i] = a_flags;
902 rc = filter_matched_values(op, aa, &e_flags) ;
906 LDAP_LOG( OPERATION, ERR,
907 "send_search_entry: conn %lu "
908 "matched values filtering failed\n",
909 op->o_connid : 0, 0, 0);
911 Debug( LDAP_DEBUG_ANY,
912 "matched values filtering failed\n", 0, 0, 0 );
914 #ifdef LDAP_CONNECTIONLESS
915 if (!op->o_conn || op->o_conn->c_is_udp == 0)
919 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
925 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
926 AttributeDescription *desc = a->a_desc;
928 if ( rs->sr_attrs == NULL ) {
929 /* all attrs request, skip operational attributes */
930 if( is_at_operational( desc->ad_type ) ) {
935 /* specific attrs requested */
936 if( is_at_operational( desc->ad_type ) ) {
937 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
941 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) )
948 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
949 ACL_READ, &acl_state ) )
953 "send_search_entry: conn %lu "
954 "access to attribute %s not allowed\n",
955 op->o_connid, desc->ad_cname.bv_val, 0 );
957 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
959 desc->ad_cname.bv_val, 0, 0 );
965 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
968 LDAP_LOG( OPERATION, ERR,
969 "send_search_entry: conn %lu "
970 "ber_printf failed\n", op->o_connid, 0, 0 );
972 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
975 #ifdef LDAP_CONNECTIONLESS
976 if (!op->o_conn || op->o_conn->c_is_udp == 0)
979 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
985 if ( ! op->ors_attrsonly ) {
986 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
987 if ( ! access_allowed( op, rs->sr_entry,
988 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
992 "send_search_entry: conn %lu "
993 "access to %s, value %d not allowed\n",
994 op->o_connid, desc->ad_cname.bv_val, i );
996 Debug( LDAP_DEBUG_ACL,
997 "send_search_entry: access to attribute %s, "
998 "value %d not allowed\n",
999 desc->ad_cname.bv_val, i, 0 );
1005 if ( op->vrFilter && e_flags[j][i] == 0 ){
1009 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1011 LDAP_LOG( OPERATION, ERR,
1012 "send_search_entry: conn %lu ber_printf failed\n",
1013 op->o_connid, 0, 0 );
1015 Debug( LDAP_DEBUG_ANY,
1016 "ber_printf failed\n", 0, 0, 0 );
1019 #ifdef LDAP_CONNECTIONLESS
1020 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1022 ber_free_buf( ber );
1023 send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
1030 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1032 LDAP_LOG( OPERATION, ERR,
1033 "send_search_entry: conn %lu ber_printf failed\n",
1034 op->o_connid, 0, 0 );
1036 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1039 #ifdef LDAP_CONNECTIONLESS
1040 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1042 ber_free_buf( ber );
1043 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1052 * First, setup the computed attribute context that is
1053 * passed to all plugins.
1055 ctx.cac_pb = op->o_pb;
1056 ctx.cac_attrs = rs->sr_attrs;
1057 ctx.cac_attrsonly = op->ors_attrsonly;
1058 ctx.cac_userattrs = userattrs;
1059 ctx.cac_opattrs = opattrs;
1060 ctx.cac_acl_state = acl_state;
1061 ctx.cac_private = (void *)ber;
1064 * For each client requested attribute, call the plugins.
1066 if ( rs->sr_attrs != NULL ) {
1067 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1068 rc = compute_evaluator( &ctx, anp->an_name.bv_val, rs->sr_entry, slapi_x_compute_output_ber );
1075 * Technically we shouldn't be returning operational attributes
1076 * when the user requested only user attributes. We'll let the
1077 * plugin decide whether to be naughty or not.
1079 rc = compute_evaluator( &ctx, "*", rs->sr_entry, slapi_x_compute_output_ber );
1082 ber_free_buf( ber );
1083 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1086 #endif /* LDAP_SLAPI */
1095 rc = ber_printf( ber, /*{{*/ "}N}" );
1097 if( rc != -1 && rs->sr_ctrls != NULL ) {
1098 rc = send_ldap_controls( ber, rs->sr_ctrls );
1101 #ifdef LDAP_CONNECTIONLESS
1102 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 ) {
1103 ; /* empty, skip following if */
1107 rc = ber_printf( ber, /*{*/ "N}" );
1112 LDAP_LOG( OPERATION, ERR,
1113 "send_search_entry: conn %lu ber_printf failed\n",
1114 op->o_connid, 0, 0 );
1116 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1119 #ifdef LDAP_CONNECTIONLESS
1120 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1122 ber_free_buf( ber );
1123 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1127 #ifdef LDAP_CONNECTIONLESS
1128 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1130 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1131 ber_free_buf( ber );
1135 LDAP_LOG( OPERATION, ERR,
1136 "send_search_entry: conn %lu ber write failed.\n",
1137 op->o_connid, 0, 0 );
1139 Debug( LDAP_DEBUG_ANY,
1140 "send_search_entry: ber write failed\n",
1147 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1148 num_bytes_sent += bytes;
1151 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1153 #ifdef LDAP_CONNECTIONLESS
1157 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1158 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1161 LDAP_LOG( OPERATION, ENTRY,
1162 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1164 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1170 if ( e_flags ) free( e_flags );
1175 slap_send_search_reference( Operation *op, SlapReply *rs )
1177 char berbuf[LBER_ELEMENT_SIZEOF];
1178 BerElement *ber = (BerElement *)berbuf;
1182 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1183 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1185 rs->sr_type = REP_SEARCHREF;
1186 if (op->o_callback && op->o_callback->sc_sendreference) {
1187 return op->o_callback->sc_sendreference( op, rs );
1191 LDAP_LOG( OPERATION, ENTRY,
1192 "send_search_reference: conn %lu dn=\"%s\"\n",
1193 op->o_connid, rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1195 Debug( LDAP_DEBUG_TRACE,
1196 "=> send_search_reference: dn=\"%s\"\n",
1197 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1200 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1201 ad_entry, NULL, ACL_READ, NULL ) )
1204 LDAP_LOG( ACL, INFO,
1205 "send_search_reference: conn %lu "
1206 "access to entry %s not allowed\n",
1207 op->o_connid, rs->sr_entry->e_dn, 0 );
1209 Debug( LDAP_DEBUG_ACL,
1210 "send_search_reference: access to entry not allowed\n",
1217 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1218 ad_ref, NULL, ACL_READ, NULL ) )
1221 LDAP_LOG( ACL, INFO,
1222 "send_search_reference: conn %lu access "
1223 "to reference not allowed.\n", op->o_connid, 0, 0 );
1225 Debug( LDAP_DEBUG_ACL,
1226 "send_search_reference: access "
1227 "to reference not allowed\n",
1234 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1235 if( op->o_domain_scope ) {
1237 LDAP_LOG( OPERATION, ERR,
1238 "send_search_reference: conn %lu domainScope control in (%s).\n",
1239 op->o_connid, rs->sr_entry->e_dn, 0 );
1241 Debug( LDAP_DEBUG_ANY,
1242 "send_search_reference: domainScope control in (%s)\n",
1243 rs->sr_entry->e_dn, 0, 0 );
1250 if( rs->sr_ref == NULL ) {
1252 LDAP_LOG( OPERATION, ERR,
1253 "send_search_reference: conn %lu null ref in (%s).\n",
1254 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1256 Debug( LDAP_DEBUG_ANY,
1257 "send_search_reference: null ref in (%s)\n",
1258 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1264 if( op->o_protocol < LDAP_VERSION3 ) {
1265 /* save the references for the result */
1266 if( rs->sr_ref[0].bv_val != NULL ) {
1267 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1273 #ifdef LDAP_CONNECTIONLESS
1274 if (op->o_conn && op->o_conn->c_is_udp)
1275 ber = op->o_res_ber;
1278 ber_init_w_nullc( ber, LBER_USE_DER );
1280 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1281 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1283 if( rc != -1 && rs->sr_ctrls != NULL ) {
1284 rc = send_ldap_controls( ber, rs->sr_ctrls );
1288 rc = ber_printf( ber, /*"{"*/ "N}" );
1293 LDAP_LOG( OPERATION, ERR,
1294 "send_search_reference: conn %lu "
1295 "ber_printf failed.\n", op->o_connid, 0, 0 );
1297 Debug( LDAP_DEBUG_ANY,
1298 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1301 #ifdef LDAP_CONNECTIONLESS
1302 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1304 ber_free_buf( ber );
1305 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1309 #ifdef LDAP_CONNECTIONLESS
1310 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1312 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1313 ber_free_buf( ber );
1315 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1316 num_bytes_sent += bytes;
1319 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1320 #ifdef LDAP_CONNECTIONLESS
1324 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1325 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1328 LDAP_LOG( OPERATION, ENTRY,
1329 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1331 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1348 *code = LDAP_SUCCESS;
1352 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1354 LDAP_LOG( OPERATION, INFO,
1355 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1357 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1365 while ( (s = strchr( s, '\n' )) != NULL ) {
1370 if ( (c = strchr( s, ':' )) != NULL ) {
1374 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1378 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1382 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1388 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1390 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",