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 ) && ( rs->sr_err >= 0 ));
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 ( op->o_tag == LDAP_REQ_SEARCH ) {
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_entry( Operation *op, SlapReply *rs )
575 char berbuf[LBER_ELEMENT_SIZEOF];
576 BerElement *ber = (BerElement *)berbuf;
578 int i, j, rc=-1, bytes;
582 AccessControlState acl_state = ACL_STATE_INIT;
584 /* Support for computed attribute plugins */
585 computed_attr_context ctx;
589 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
591 /* a_flags: array of flags telling if the i-th element will be
592 * returned or filtered out
593 * e_flags: array of a_flags
595 char **e_flags = NULL;
597 rs->sr_type = REP_SEARCH;
598 if (op->o_callback && op->o_callback->sc_response) {
599 return op->o_callback->sc_response( op, rs );
603 LDAP_LOG( OPERATION, ENTRY,
604 "send_search_entry: conn %lu dn=\"%s\"%s\n",
605 op->o_connid, rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "" );
607 Debug( LDAP_DEBUG_TRACE,
608 "=> send_search_entry: dn=\"%s\"%s\n",
609 rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
612 if ( ! access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL ) )
616 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
617 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
619 Debug( LDAP_DEBUG_ACL,
620 "send_search_entry: access to entry not allowed\n",
627 edn = rs->sr_entry->e_nname.bv_val;
629 #ifdef LDAP_CONNECTIONLESS
630 if (op->o_conn && op->o_conn->c_is_udp)
634 ber_init_w_nullc( ber, LBER_USE_DER );
636 #ifdef LDAP_CONNECTIONLESS
637 if (op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
638 rc = ber_printf(ber, "t{O{" /*}}*/,
639 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name);
643 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
644 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
649 LDAP_LOG( OPERATION, ERR,
650 "send_search_entry: conn %lu ber_printf failed\n",
651 op->o_connid, 0, 0 );
653 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
656 #ifdef LDAP_CONNECTIONLESS
657 if (!op->o_conn || op->o_conn->c_is_udp == 0)
660 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
664 /* check for special all user attributes ("*") type */
665 userattrs = ( rs->sr_attrs == NULL ) ? 1
666 : an_find( rs->sr_attrs, &AllUser );
668 /* check for special all operational attributes ("+") type */
669 opattrs = ( rs->sr_attrs == NULL ) ? 0
670 : an_find( rs->sr_attrs, &AllOper );
672 /* create an array of arrays of flags. Each flag corresponds
673 * to particular value of attribute and equals 1 if value matches
674 * to ValuesReturnFilter or 0 if not
676 if ( op->vrFilter != NULL ) {
680 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
681 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
684 size = i * sizeof(char *) + k;
687 e_flags = SLAP_CALLOC ( 1, i * sizeof(char *) + k );
688 if( e_flags == NULL ) {
690 LDAP_LOG( OPERATION, ERR,
691 "send_search_entry: conn %lu SLAP_CALLOC failed\n",
692 op->o_connid : 0, 0, 0 );
694 Debug( LDAP_DEBUG_ANY,
695 "send_search_entry: SLAP_CALLOC failed\n", 0, 0, 0 );
699 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
702 a_flags = (char *)(e_flags + i);
703 memset( a_flags, 0, k );
704 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
705 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
706 e_flags[i] = a_flags;
710 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
713 LDAP_LOG( OPERATION, ERR,
714 "send_search_entry: conn %lu matched values filtering failed\n",
715 op->o_connid : 0, 0, 0 );
717 Debug( LDAP_DEBUG_ANY,
718 "matched values filtering failed\n", 0, 0, 0 );
720 #ifdef LDAP_CONNECTIONLESS
721 if (!op->o_conn || op->o_conn->c_is_udp == 0)
724 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
730 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
731 AttributeDescription *desc = a->a_desc;
733 if ( rs->sr_attrs == NULL ) {
734 /* all attrs request, skip operational attributes */
735 if( is_at_operational( desc->ad_type ) ) {
740 /* specific attrs requested */
741 if ( is_at_operational( desc->ad_type ) ) {
742 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
747 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
753 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
754 ACL_READ, &acl_state ) )
758 "send_search_entry: conn %lu access to attribute %s not "
759 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
761 Debug( LDAP_DEBUG_ACL, "acl: "
762 "access to attribute %s not allowed\n",
763 desc->ad_cname.bv_val, 0, 0 );
768 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
770 LDAP_LOG( OPERATION, ERR,
771 "send_search_entry: conn %lu ber_printf failed\n",
772 op->o_connid, 0, 0 );
774 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
777 #ifdef LDAP_CONNECTIONLESS
778 if (!op->o_conn || op->o_conn->c_is_udp == 0)
781 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
785 if ( ! op->ors_attrsonly ) {
786 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
787 if ( ! access_allowed( op, rs->sr_entry,
788 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
792 "send_search_entry: conn %lu "
793 "access to attribute %s, value %d not allowed\n",
794 op->o_connid, desc->ad_cname.bv_val, i );
796 Debug( LDAP_DEBUG_ACL,
797 "acl: access to attribute %s, "
798 "value %d not allowed\n",
799 desc->ad_cname.bv_val, i, 0 );
805 if ( op->vrFilter && e_flags[j][i] == 0 ){
809 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
811 LDAP_LOG( OPERATION, ERR,
812 "send_search_entry: conn %lu "
813 "ber_printf failed.\n", op->o_connid, 0, 0 );
815 Debug( LDAP_DEBUG_ANY,
816 "ber_printf failed\n", 0, 0, 0 );
819 #ifdef LDAP_CONNECTIONLESS
820 if (!op->o_conn || op->o_conn->c_is_udp == 0)
823 send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
829 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
831 LDAP_LOG( OPERATION, ERR,
832 "send_search_entry: conn %lu ber_printf failed\n",
833 op->o_connid, 0, 0 );
835 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
838 #ifdef LDAP_CONNECTIONLESS
839 if (!op->o_conn || op->o_conn->c_is_udp == 0)
842 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
847 /* eventually will loop through generated operational attributes */
848 /* only have subschemaSubentry implemented */
849 aa = backend_operational( op, rs, opattrs );
851 if ( aa != NULL && op->vrFilter != NULL ) {
855 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
856 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
859 size = i * sizeof(char *) + k;
861 char *a_flags, **tmp;
864 * Reuse previous memory - we likely need less space
865 * for operational attributes
867 tmp = SLAP_REALLOC ( e_flags, i * sizeof(char *) + k );
870 LDAP_LOG( OPERATION, ERR,
871 "send_search_entry: conn %lu "
873 "for matched values filtering\n",
876 Debug( LDAP_DEBUG_ANY,
877 "send_search_entry: conn %lu "
879 "for matched values filtering\n",
880 op->o_connid, 0, 0 );
884 send_ldap_error( op, rs, LDAP_OTHER, "not enough memory for matched values filtering" );
888 a_flags = (char *)(e_flags + i);
889 memset( a_flags, 0, k );
890 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
891 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
892 e_flags[i] = a_flags;
895 rc = filter_matched_values(op, aa, &e_flags) ;
899 LDAP_LOG( OPERATION, ERR,
900 "send_search_entry: conn %lu "
901 "matched values filtering failed\n",
902 op->o_connid : 0, 0, 0);
904 Debug( LDAP_DEBUG_ANY,
905 "matched values filtering failed\n", 0, 0, 0 );
907 #ifdef LDAP_CONNECTIONLESS
908 if (!op->o_conn || op->o_conn->c_is_udp == 0)
912 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
918 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
919 AttributeDescription *desc = a->a_desc;
921 if ( rs->sr_attrs == NULL ) {
922 /* all attrs request, skip operational attributes */
923 if( is_at_operational( desc->ad_type ) ) {
928 /* specific attrs requested */
929 if( is_at_operational( desc->ad_type ) ) {
930 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
934 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) )
941 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
942 ACL_READ, &acl_state ) )
946 "send_search_entry: conn %lu "
947 "access to attribute %s not allowed\n",
948 op->o_connid, desc->ad_cname.bv_val, 0 );
950 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
952 desc->ad_cname.bv_val, 0, 0 );
958 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
961 LDAP_LOG( OPERATION, ERR,
962 "send_search_entry: conn %lu "
963 "ber_printf failed\n", op->o_connid, 0, 0 );
965 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
968 #ifdef LDAP_CONNECTIONLESS
969 if (!op->o_conn || op->o_conn->c_is_udp == 0)
972 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
978 if ( ! op->ors_attrsonly ) {
979 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
980 if ( ! access_allowed( op, rs->sr_entry,
981 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
985 "send_search_entry: conn %lu "
986 "access to %s, value %d not allowed\n",
987 op->o_connid, desc->ad_cname.bv_val, i );
989 Debug( LDAP_DEBUG_ACL,
990 "send_search_entry: access to attribute %s, "
991 "value %d not allowed\n",
992 desc->ad_cname.bv_val, i, 0 );
998 if ( op->vrFilter && e_flags[j][i] == 0 ){
1002 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1004 LDAP_LOG( OPERATION, ERR,
1005 "send_search_entry: conn %lu ber_printf failed\n",
1006 op->o_connid, 0, 0 );
1008 Debug( LDAP_DEBUG_ANY,
1009 "ber_printf failed\n", 0, 0, 0 );
1012 #ifdef LDAP_CONNECTIONLESS
1013 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1015 ber_free_buf( ber );
1016 send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
1023 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1025 LDAP_LOG( OPERATION, ERR,
1026 "send_search_entry: conn %lu ber_printf failed\n",
1027 op->o_connid, 0, 0 );
1029 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1032 #ifdef LDAP_CONNECTIONLESS
1033 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1035 ber_free_buf( ber );
1036 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1045 * First, setup the computed attribute context that is
1046 * passed to all plugins.
1048 ctx.cac_pb = op->o_pb;
1049 ctx.cac_attrs = rs->sr_attrs;
1050 ctx.cac_attrsonly = op->ors_attrsonly;
1051 ctx.cac_userattrs = userattrs;
1052 ctx.cac_opattrs = opattrs;
1053 ctx.cac_acl_state = acl_state;
1054 ctx.cac_private = (void *)ber;
1057 * For each client requested attribute, call the plugins.
1059 if ( rs->sr_attrs != NULL ) {
1060 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1061 rc = compute_evaluator( &ctx, anp->an_name.bv_val, rs->sr_entry, slapi_x_compute_output_ber );
1068 * Technically we shouldn't be returning operational attributes
1069 * when the user requested only user attributes. We'll let the
1070 * plugin decide whether to be naughty or not.
1072 rc = compute_evaluator( &ctx, "*", rs->sr_entry, slapi_x_compute_output_ber );
1075 ber_free_buf( ber );
1076 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1079 #endif /* LDAP_SLAPI */
1088 rc = ber_printf( ber, /*{{*/ "}N}" );
1090 if( rc != -1 && rs->sr_ctrls != NULL ) {
1091 rc = send_ldap_controls( ber, rs->sr_ctrls );
1094 #ifdef LDAP_CONNECTIONLESS
1095 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 ) {
1096 ; /* empty, skip following if */
1100 rc = ber_printf( ber, /*{*/ "N}" );
1105 LDAP_LOG( OPERATION, ERR,
1106 "send_search_entry: conn %lu ber_printf failed\n",
1107 op->o_connid, 0, 0 );
1109 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1112 #ifdef LDAP_CONNECTIONLESS
1113 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1115 ber_free_buf( ber );
1116 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1120 #ifdef LDAP_CONNECTIONLESS
1121 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1123 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1124 ber_free_buf( ber );
1128 LDAP_LOG( OPERATION, ERR,
1129 "send_search_entry: conn %lu ber write failed.\n",
1130 op->o_connid, 0, 0 );
1132 Debug( LDAP_DEBUG_ANY,
1133 "send_search_entry: ber write failed\n",
1141 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1142 num_bytes_sent += bytes;
1145 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1147 #ifdef LDAP_CONNECTIONLESS
1151 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1152 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1155 LDAP_LOG( OPERATION, ENTRY,
1156 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1158 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1164 if ( e_flags ) free( e_flags );
1169 slap_send_search_reference( Operation *op, SlapReply *rs )
1171 char berbuf[LBER_ELEMENT_SIZEOF];
1172 BerElement *ber = (BerElement *)berbuf;
1176 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1177 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1179 rs->sr_type = REP_SEARCHREF;
1180 if (op->o_callback && op->o_callback->sc_response) {
1181 return op->o_callback->sc_response( op, rs );
1185 LDAP_LOG( OPERATION, ENTRY,
1186 "send_search_reference: conn %lu dn=\"%s\"\n",
1187 op->o_connid, rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1189 Debug( LDAP_DEBUG_TRACE,
1190 "=> send_search_reference: dn=\"%s\"\n",
1191 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1194 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1195 ad_entry, NULL, ACL_READ, NULL ) )
1198 LDAP_LOG( ACL, INFO,
1199 "send_search_reference: conn %lu "
1200 "access to entry %s not allowed\n",
1201 op->o_connid, rs->sr_entry->e_dn, 0 );
1203 Debug( LDAP_DEBUG_ACL,
1204 "send_search_reference: access to entry not allowed\n",
1211 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1212 ad_ref, NULL, ACL_READ, NULL ) )
1215 LDAP_LOG( ACL, INFO,
1216 "send_search_reference: conn %lu access "
1217 "to reference not allowed.\n", op->o_connid, 0, 0 );
1219 Debug( LDAP_DEBUG_ACL,
1220 "send_search_reference: access "
1221 "to reference not allowed\n",
1228 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1229 if( op->o_domain_scope ) {
1231 LDAP_LOG( OPERATION, ERR,
1232 "send_search_reference: conn %lu domainScope control in (%s).\n",
1233 op->o_connid, rs->sr_entry->e_dn, 0 );
1235 Debug( LDAP_DEBUG_ANY,
1236 "send_search_reference: domainScope control in (%s)\n",
1237 rs->sr_entry->e_dn, 0, 0 );
1244 if( rs->sr_ref == NULL ) {
1246 LDAP_LOG( OPERATION, ERR,
1247 "send_search_reference: conn %lu null ref in (%s).\n",
1248 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1250 Debug( LDAP_DEBUG_ANY,
1251 "send_search_reference: null ref in (%s)\n",
1252 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1258 if( op->o_protocol < LDAP_VERSION3 ) {
1259 /* save the references for the result */
1260 if( rs->sr_ref[0].bv_val != NULL ) {
1261 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1267 #ifdef LDAP_CONNECTIONLESS
1268 if (op->o_conn && op->o_conn->c_is_udp)
1269 ber = op->o_res_ber;
1272 ber_init_w_nullc( ber, LBER_USE_DER );
1274 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1275 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1277 if( rc != -1 && rs->sr_ctrls != NULL ) {
1278 rc = send_ldap_controls( ber, rs->sr_ctrls );
1282 rc = ber_printf( ber, /*"{"*/ "N}" );
1287 LDAP_LOG( OPERATION, ERR,
1288 "send_search_reference: conn %lu "
1289 "ber_printf failed.\n", op->o_connid, 0, 0 );
1291 Debug( LDAP_DEBUG_ANY,
1292 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1295 #ifdef LDAP_CONNECTIONLESS
1296 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1298 ber_free_buf( ber );
1299 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1303 #ifdef LDAP_CONNECTIONLESS
1304 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1306 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1307 ber_free_buf( ber );
1309 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1310 num_bytes_sent += bytes;
1313 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1314 #ifdef LDAP_CONNECTIONLESS
1318 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1319 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1322 LDAP_LOG( OPERATION, ENTRY,
1323 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1325 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1342 *code = LDAP_SUCCESS;
1346 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1348 LDAP_LOG( OPERATION, INFO,
1349 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1351 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1359 while ( (s = strchr( s, '\n' )) != NULL ) {
1364 if ( (c = strchr( s, ':' )) != NULL ) {
1368 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1372 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1376 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1382 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1384 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",