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)
242 ber_init_w_nullc( ber, LBER_USE_DER );
243 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
247 LDAP_LOG( OPERATION, ENTRY,
248 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
249 rs->sr_msgid, rs->sr_tag, rs->sr_err );
251 Debug( LDAP_DEBUG_TRACE,
252 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
253 rs->sr_msgid, rs->sr_tag, rs->sr_err );
258 LDAP_LOG( OPERATION, ARGS,
259 "send_ldap_response: conn %lu ref=\"%s\"\n",
261 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
263 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
264 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
269 #ifdef LDAP_CONNECTIONLESS
270 if (op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
271 rc = ber_printf( ber, "t{ess" /*"}}"*/,
272 rs->sr_tag, rs->sr_err,
273 rs->sr_matched == NULL ? "" : rs->sr_matched,
274 rs->sr_text == NULL ? "" : rs->sr_text );
278 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
279 rs->sr_msgid, rs->sr_tag, rs->sr_err,
280 rs->sr_matched == NULL ? "" : rs->sr_matched,
281 rs->sr_text == NULL ? "" : rs->sr_text );
285 if ( rs->sr_ref != NULL ) {
286 assert( rs->sr_err == LDAP_REFERRAL );
287 rc = ber_printf( ber, "t{W}",
288 LDAP_TAG_REFERRAL, rs->sr_ref );
290 assert( rs->sr_err != LDAP_REFERRAL );
294 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
295 rc = ber_printf( ber, "tO",
296 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
299 if( rc != -1 && rs->sr_type == REP_EXTENDED ) {
300 if ( rs->sr_rspoid != NULL ) {
301 rc = ber_printf( ber, "ts",
302 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
304 if( rc != -1 && rs->sr_rspdata != NULL ) {
305 rc = ber_printf( ber, "tO",
306 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
311 rc = ber_printf( ber, /*"{"*/ "N}" );
314 if( rc != -1 && rs->sr_ctrls != NULL ) {
315 rc = send_ldap_controls( ber, rs->sr_ctrls );
319 rc = ber_printf( ber, /*"{"*/ "N}" );
322 #ifdef LDAP_CONNECTIONLESS
323 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
324 rc = ber_printf( ber, /*"{"*/ "N}" );
330 LDAP_LOG( OPERATION, ERR,
331 "send_ldap_response: conn %lu ber_printf failed\n",
332 op->o_connid, 0, 0 );
334 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
337 #ifdef LDAP_CONNECTIONLESS
338 if (!op->o_conn || op->o_conn->c_is_udp == 0)
345 bytes = send_ldap_ber( op->o_conn, ber );
346 #ifdef LDAP_CONNECTIONLESS
347 if (!op->o_conn || op->o_conn->c_is_udp == 0)
353 LDAP_LOG( OPERATION, ERR,
354 "send_ldap_response: conn %lu ber write failed\n",
355 op->o_connid ? op->o_connid : 0, 0, 0 );
357 Debug( LDAP_DEBUG_ANY,
358 "send_ldap_response: ber write failed\n",
366 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
367 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, ( rs->sr_matched != NULL ) ? (void *)ch_strdup( rs->sr_matched ) : NULL );
368 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, ( rs->sr_text != NULL ) ? (void *)ch_strdup( rs->sr_text ) : NULL );
369 #endif /* LDAP_SLAPI */
371 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
372 num_bytes_sent += bytes;
374 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
380 send_ldap_disconnect( Operation *op, SlapReply *rs )
382 #define LDAP_UNSOLICITED_ERROR(e) \
383 ( (e) == LDAP_PROTOCOL_ERROR \
384 || (e) == LDAP_STRONG_AUTH_REQUIRED \
385 || (e) == LDAP_UNAVAILABLE )
387 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
389 rs->sr_type = REP_EXTENDED;
392 LDAP_LOG( OPERATION, ENTRY,
393 "send_ldap_disconnect: conn %lu %d:%s\n",
394 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
396 Debug( LDAP_DEBUG_TRACE,
397 "send_ldap_disconnect %d:%s\n",
398 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
402 if ( op->o_protocol < LDAP_VERSION3 ) {
403 rs->sr_rspoid = NULL;
404 rs->sr_tag = req2res( op->o_tag );
405 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
408 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
409 rs->sr_tag = LDAP_RES_EXTENDED;
413 send_ldap_response( op, rs );
415 Statslog( LDAP_DEBUG_STATS,
416 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
417 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
421 slap_send_ldap_result( Operation *op, SlapReply *rs )
424 const char *otext = rs->sr_text;
425 BerVarray oref = rs->sr_ref;
427 rs->sr_type = REP_RESULT;
429 assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
432 LDAP_LOG( OPERATION, ENTRY,
433 "send_ldap_result: conn %lu op=%lu p=%d\n",
434 op->o_connid, op->o_opid, op->o_protocol );
436 Debug( LDAP_DEBUG_TRACE,
437 "send_ldap_result: conn=%lu op=%lu p=%d\n",
438 op->o_connid, op->o_opid, op->o_protocol );
442 LDAP_LOG( OPERATION, ARGS,
443 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
444 rs->sr_err, rs->sr_matched ? rs->sr_matched : "", rs->sr_text ? rs->sr_text : "" );
446 Debug( LDAP_DEBUG_ARGS,
447 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
448 rs->sr_err, rs->sr_matched ? rs->sr_matched : "", rs->sr_text ? rs->sr_text : "" );
454 LDAP_LOG( OPERATION, ARGS,
455 "send_ldap_result: referral=\"%s\"\n",
456 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
458 Debug( LDAP_DEBUG_ARGS,
459 "send_ldap_result: referral=\"%s\"\n",
460 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
465 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
467 if ( rs->sr_err == LDAP_REFERRAL ) {
468 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
469 if( op->o_domain_scope ) {
473 if( rs->sr_ref == NULL ) {
474 rs->sr_err = LDAP_NO_SUCH_OBJECT;
475 } else if ( op->o_protocol < LDAP_VERSION3 ) {
476 rs->sr_err = LDAP_PARTIAL_RESULTS;
480 if ( op->o_protocol < LDAP_VERSION3 ) {
481 tmp = v2ref( rs->sr_ref, rs->sr_text );
486 rs->sr_tag = req2res( op->o_tag );
487 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
489 send_ldap_response( op, rs );
491 if ( op->o_tag == LDAP_REQ_SEARCH ) {
493 snprintf( nbuf, sizeof nbuf, "%d nentries=%d", rs->sr_err, rs->sr_nentries );
495 Statslog( LDAP_DEBUG_STATS,
496 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
497 op->o_connid, op->o_opid, rs->sr_tag, nbuf, rs->sr_text ? rs->sr_text : "" );
499 Statslog( LDAP_DEBUG_STATS,
500 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
501 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
512 send_ldap_sasl( Operation *op, SlapReply *rs )
514 rs->sr_type = REP_SASL;
516 LDAP_LOG( OPERATION, ENTRY,
517 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
518 op->o_connid, rs->sr_err, rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
520 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
521 rs->sr_err, rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
524 rs->sr_tag = req2res( op->o_tag );
525 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
527 send_ldap_response( op, rs );
531 slap_send_ldap_extended( Operation *op, SlapReply *rs )
533 rs->sr_type = REP_EXTENDED;
536 LDAP_LOG( OPERATION, ENTRY,
537 "send_ldap_extended: err=%d oid=%s len=%ld\n",
538 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
539 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
541 Debug( LDAP_DEBUG_TRACE,
542 "send_ldap_extended: err=%d oid=%s len=%ld\n",
544 rs->sr_rspoid ? rs->sr_rspoid : "",
545 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
548 rs->sr_tag = req2res( op->o_tag );
549 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
551 send_ldap_response( op, rs );
554 #ifdef LDAP_RES_INTERMEDIATE
556 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
558 rs->sr_type = REP_EXTENDED;
560 LDAP_LOG( OPERATION, ENTRY,
561 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
562 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
563 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
565 Debug( LDAP_DEBUG_TRACE,
566 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
568 rs->sr_rspoid ? rs->sr_rspoid : "",
569 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
571 rs->sr_tag = LDAP_RES_INTERMEDIATE;
572 rs->sr_msgid = op->o_msgid;
573 send_ldap_response( op, rs );
578 slap_send_search_entry( Operation *op, SlapReply *rs )
580 char berbuf[LBER_ELEMENT_SIZEOF];
581 BerElement *ber = (BerElement *)berbuf;
583 int i, j, rc=-1, bytes;
587 AccessControlState acl_state = ACL_STATE_INIT;
589 /* Support for computed attribute plugins */
590 computed_attr_context ctx;
595 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
597 /* a_flags: array of flags telling if the i-th element will be
598 * returned or filtered out
599 * e_flags: array of a_flags
601 char **e_flags = NULL;
603 rs->sr_type = REP_SEARCH;
604 if (op->o_callback && op->o_callback->sc_response) {
605 return op->o_callback->sc_response( op, rs );
609 LDAP_LOG( OPERATION, ENTRY,
610 "send_search_entry: conn %lu dn=\"%s\"%s\n",
611 op->o_connid, rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "" );
613 Debug( LDAP_DEBUG_TRACE,
614 "=> send_search_entry: dn=\"%s\"%s\n",
615 rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
618 mark = sl_mark( op->o_tmpmemctx );
620 if ( ! access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL ) )
624 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
625 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
627 Debug( LDAP_DEBUG_ACL,
628 "send_search_entry: access to entry not allowed\n",
632 sl_release( mark, op->o_tmpmemctx );
636 edn = rs->sr_entry->e_nname.bv_val;
638 #ifdef LDAP_CONNECTIONLESS
639 if (op->o_conn && op->o_conn->c_is_udp)
647 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
648 bv.bv_len = siz + len;
649 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
651 ber_init2( ber, &bv, LBER_USE_DER );
652 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
655 #ifdef LDAP_CONNECTIONLESS
656 if (op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
657 rc = ber_printf(ber, "t{O{" /*}}*/,
658 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name);
662 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
663 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
668 LDAP_LOG( OPERATION, ERR,
669 "send_search_entry: conn %lu ber_printf failed\n",
670 op->o_connid, 0, 0 );
672 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
675 #ifdef LDAP_CONNECTIONLESS
676 if (!op->o_conn || op->o_conn->c_is_udp == 0)
679 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
683 /* check for special all user attributes ("*") type */
684 userattrs = ( rs->sr_attrs == NULL ) ? 1
685 : an_find( rs->sr_attrs, &AllUser );
687 /* check for special all operational attributes ("+") type */
688 opattrs = ( rs->sr_attrs == NULL ) ? 0
689 : an_find( rs->sr_attrs, &AllOper );
691 /* create an array of arrays of flags. Each flag corresponds
692 * to particular value of attribute and equals 1 if value matches
693 * to ValuesReturnFilter or 0 if not
695 if ( op->vrFilter != NULL ) {
699 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
700 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
703 size = i * sizeof(char *) + k;
706 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
707 if( e_flags == NULL ) {
709 LDAP_LOG( OPERATION, ERR,
710 "send_search_entry: conn %lu sl_calloc failed\n",
711 op->o_connid ? op->o_connid : 0, 0, 0 );
713 Debug( LDAP_DEBUG_ANY,
714 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
718 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
721 a_flags = (char *)(e_flags + i);
722 memset( a_flags, 0, k );
723 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
724 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
725 e_flags[i] = a_flags;
729 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
732 LDAP_LOG( OPERATION, ERR,
733 "send_search_entry: conn %lu matched values filtering failed\n",
734 op->o_connid ? op->o_connid : 0, 0, 0 );
736 Debug( LDAP_DEBUG_ANY,
737 "matched values filtering failed\n", 0, 0, 0 );
739 #ifdef LDAP_CONNECTIONLESS
740 if (!op->o_conn || op->o_conn->c_is_udp == 0)
743 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
749 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
750 AttributeDescription *desc = a->a_desc;
752 if ( rs->sr_attrs == NULL ) {
753 /* all attrs request, skip operational attributes */
754 if( is_at_operational( desc->ad_type ) ) {
759 /* specific attrs requested */
760 if ( is_at_operational( desc->ad_type ) ) {
761 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
766 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
772 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
773 ACL_READ, &acl_state ) )
777 "send_search_entry: conn %lu access to attribute %s not "
778 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
780 Debug( LDAP_DEBUG_ACL, "acl: "
781 "access to attribute %s not allowed\n",
782 desc->ad_cname.bv_val, 0, 0 );
787 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
789 LDAP_LOG( OPERATION, ERR,
790 "send_search_entry: conn %lu ber_printf failed\n",
791 op->o_connid, 0, 0 );
793 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
796 #ifdef LDAP_CONNECTIONLESS
797 if (!op->o_conn || op->o_conn->c_is_udp == 0)
800 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
804 if ( ! op->ors_attrsonly ) {
805 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
806 if ( ! access_allowed( op, rs->sr_entry,
807 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
811 "send_search_entry: conn %lu "
812 "access to attribute %s, value %d not allowed\n",
813 op->o_connid, desc->ad_cname.bv_val, i );
815 Debug( LDAP_DEBUG_ACL,
816 "acl: access to attribute %s, "
817 "value %d not allowed\n",
818 desc->ad_cname.bv_val, i, 0 );
824 if ( op->vrFilter && e_flags[j][i] == 0 ){
828 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
830 LDAP_LOG( OPERATION, ERR,
831 "send_search_entry: conn %lu "
832 "ber_printf failed.\n", op->o_connid, 0, 0 );
834 Debug( LDAP_DEBUG_ANY,
835 "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, "encoding values error" );
848 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
850 LDAP_LOG( OPERATION, ERR,
851 "send_search_entry: conn %lu ber_printf failed\n",
852 op->o_connid, 0, 0 );
854 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
857 #ifdef LDAP_CONNECTIONLESS
858 if (!op->o_conn || op->o_conn->c_is_udp == 0)
861 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
866 /* eventually will loop through generated operational attributes */
867 /* only have subschemaSubentry implemented */
868 aa = backend_operational( op, rs, opattrs );
870 if ( aa != NULL && op->vrFilter != NULL ) {
874 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
875 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
878 size = i * sizeof(char *) + k;
880 char *a_flags, **tmp;
883 * Reuse previous memory - we likely need less space
884 * for operational attributes
886 tmp = sl_realloc( e_flags, i * sizeof(char *) + k, op->o_tmpmemctx );
889 LDAP_LOG( OPERATION, ERR,
890 "send_search_entry: conn %lu "
892 "for matched values filtering\n",
895 Debug( LDAP_DEBUG_ANY,
896 "send_search_entry: conn %lu "
898 "for matched values filtering\n",
899 op->o_connid, 0, 0 );
903 send_ldap_error( op, rs, LDAP_OTHER, "not enough memory for matched values filtering" );
907 a_flags = (char *)(e_flags + i);
908 memset( a_flags, 0, k );
909 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
910 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
911 e_flags[i] = a_flags;
914 rc = filter_matched_values(op, aa, &e_flags) ;
918 LDAP_LOG( OPERATION, ERR,
919 "send_search_entry: conn %lu "
920 "matched values filtering failed\n",
921 op->o_connid ? op->o_connid : 0, 0, 0);
923 Debug( LDAP_DEBUG_ANY,
924 "matched values filtering failed\n", 0, 0, 0 );
926 #ifdef LDAP_CONNECTIONLESS
927 if (!op->o_conn || op->o_conn->c_is_udp == 0)
931 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
937 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
938 AttributeDescription *desc = a->a_desc;
940 if ( rs->sr_attrs == NULL ) {
941 /* all attrs request, skip operational attributes */
942 if( is_at_operational( desc->ad_type ) ) {
947 /* specific attrs requested */
948 if( is_at_operational( desc->ad_type ) ) {
949 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
953 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) )
960 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
961 ACL_READ, &acl_state ) )
965 "send_search_entry: conn %lu "
966 "access to attribute %s not allowed\n",
967 op->o_connid, desc->ad_cname.bv_val, 0 );
969 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
971 desc->ad_cname.bv_val, 0, 0 );
977 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
980 LDAP_LOG( OPERATION, ERR,
981 "send_search_entry: conn %lu "
982 "ber_printf failed\n", op->o_connid, 0, 0 );
984 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
987 #ifdef LDAP_CONNECTIONLESS
988 if (!op->o_conn || op->o_conn->c_is_udp == 0)
991 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
997 if ( ! op->ors_attrsonly ) {
998 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
999 if ( ! access_allowed( op, rs->sr_entry,
1000 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1003 LDAP_LOG( ACL, INFO,
1004 "send_search_entry: conn %lu "
1005 "access to %s, value %d not allowed\n",
1006 op->o_connid, desc->ad_cname.bv_val, i );
1008 Debug( LDAP_DEBUG_ACL,
1009 "send_search_entry: access to attribute %s, "
1010 "value %d not allowed\n",
1011 desc->ad_cname.bv_val, i, 0 );
1017 if ( op->vrFilter && e_flags[j][i] == 0 ){
1021 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1023 LDAP_LOG( OPERATION, ERR,
1024 "send_search_entry: conn %lu ber_printf failed\n",
1025 op->o_connid, 0, 0 );
1027 Debug( LDAP_DEBUG_ANY,
1028 "ber_printf failed\n", 0, 0, 0 );
1031 #ifdef LDAP_CONNECTIONLESS
1032 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1034 ber_free_buf( ber );
1035 send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
1042 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1044 LDAP_LOG( OPERATION, ERR,
1045 "send_search_entry: conn %lu ber_printf failed\n",
1046 op->o_connid, 0, 0 );
1048 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1051 #ifdef LDAP_CONNECTIONLESS
1052 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1054 ber_free_buf( ber );
1055 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1064 * First, setup the computed attribute context that is
1065 * passed to all plugins.
1067 ctx.cac_pb = op->o_pb;
1068 ctx.cac_attrs = rs->sr_attrs;
1069 ctx.cac_attrsonly = op->ors_attrsonly;
1070 ctx.cac_userattrs = userattrs;
1071 ctx.cac_opattrs = opattrs;
1072 ctx.cac_acl_state = acl_state;
1073 ctx.cac_private = (void *)ber;
1076 * For each client requested attribute, call the plugins.
1078 if ( rs->sr_attrs != NULL ) {
1079 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1080 rc = compute_evaluator( &ctx, anp->an_name.bv_val, rs->sr_entry, slapi_x_compute_output_ber );
1087 * Technically we shouldn't be returning operational attributes
1088 * when the user requested only user attributes. We'll let the
1089 * plugin decide whether to be naughty or not.
1091 rc = compute_evaluator( &ctx, "*", rs->sr_entry, slapi_x_compute_output_ber );
1094 ber_free_buf( ber );
1095 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1098 #endif /* LDAP_SLAPI */
1102 sl_free( e_flags, op->o_tmpmemctx );
1107 rc = ber_printf( ber, /*{{*/ "}N}" );
1109 if( rc != -1 && rs->sr_ctrls != NULL ) {
1110 rc = send_ldap_controls( ber, rs->sr_ctrls );
1113 #ifdef LDAP_CONNECTIONLESS
1114 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 ) {
1115 ; /* empty, skip following if */
1119 rc = ber_printf( ber, /*{*/ "N}" );
1124 LDAP_LOG( OPERATION, ERR,
1125 "send_search_entry: conn %lu ber_printf failed\n",
1126 op->o_connid, 0, 0 );
1128 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1131 #ifdef LDAP_CONNECTIONLESS
1132 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1134 ber_free_buf( ber );
1135 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1136 sl_release( mark, op->o_tmpmemctx );
1140 #ifdef LDAP_CONNECTIONLESS
1141 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1143 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1144 ber_free_buf( ber );
1148 LDAP_LOG( OPERATION, ERR,
1149 "send_search_entry: conn %lu ber write failed.\n",
1150 op->o_connid, 0, 0 );
1152 Debug( LDAP_DEBUG_ANY,
1153 "send_search_entry: ber write failed\n",
1157 sl_release( mark, op->o_tmpmemctx );
1162 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1163 num_bytes_sent += bytes;
1166 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1168 #ifdef LDAP_CONNECTIONLESS
1172 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1173 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1176 LDAP_LOG( OPERATION, ENTRY,
1177 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1179 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1185 sl_release( mark, op->o_tmpmemctx );
1186 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1191 slap_send_search_reference( Operation *op, SlapReply *rs )
1193 char berbuf[LBER_ELEMENT_SIZEOF];
1194 BerElement *ber = (BerElement *)berbuf;
1199 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1200 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1202 rs->sr_type = REP_SEARCHREF;
1203 if (op->o_callback && op->o_callback->sc_response) {
1204 return op->o_callback->sc_response( op, rs );
1207 mark = sl_mark( op->o_tmpmemctx );
1210 LDAP_LOG( OPERATION, ENTRY,
1211 "send_search_reference: conn %lu dn=\"%s\"\n",
1212 op->o_connid, rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1214 Debug( LDAP_DEBUG_TRACE,
1215 "=> send_search_reference: dn=\"%s\"\n",
1216 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1219 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1220 ad_entry, NULL, ACL_READ, NULL ) )
1223 LDAP_LOG( ACL, INFO,
1224 "send_search_reference: conn %lu "
1225 "access to entry %s not allowed\n",
1226 op->o_connid, rs->sr_entry->e_dn, 0 );
1228 Debug( LDAP_DEBUG_ACL,
1229 "send_search_reference: access to entry not allowed\n",
1236 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1237 ad_ref, NULL, ACL_READ, NULL ) )
1240 LDAP_LOG( ACL, INFO,
1241 "send_search_reference: conn %lu access "
1242 "to reference not allowed.\n", op->o_connid, 0, 0 );
1244 Debug( LDAP_DEBUG_ACL,
1245 "send_search_reference: access "
1246 "to reference not allowed\n",
1253 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1254 if( op->o_domain_scope ) {
1256 LDAP_LOG( OPERATION, ERR,
1257 "send_search_reference: conn %lu domainScope control in (%s).\n",
1258 op->o_connid, rs->sr_entry->e_dn, 0 );
1260 Debug( LDAP_DEBUG_ANY,
1261 "send_search_reference: domainScope control in (%s)\n",
1262 rs->sr_entry->e_dn, 0, 0 );
1269 if( rs->sr_ref == NULL ) {
1271 LDAP_LOG( OPERATION, ERR,
1272 "send_search_reference: conn %lu null ref in (%s).\n",
1273 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1275 Debug( LDAP_DEBUG_ANY,
1276 "send_search_reference: null ref in (%s)\n",
1277 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1283 if( op->o_protocol < LDAP_VERSION3 ) {
1284 /* save the references for the result */
1285 if( rs->sr_ref[0].bv_val != NULL ) {
1286 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1293 #ifdef LDAP_CONNECTIONLESS
1294 if (op->o_conn && op->o_conn->c_is_udp)
1295 ber = op->o_res_ber;
1299 ber_init_w_nullc( ber, LBER_USE_DER );
1300 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1303 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1304 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1306 if( rc != -1 && rs->sr_ctrls != NULL ) {
1307 rc = send_ldap_controls( ber, rs->sr_ctrls );
1311 rc = ber_printf( ber, /*"{"*/ "N}" );
1316 LDAP_LOG( OPERATION, ERR,
1317 "send_search_reference: conn %lu "
1318 "ber_printf failed.\n", op->o_connid, 0, 0 );
1320 Debug( LDAP_DEBUG_ANY,
1321 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1324 #ifdef LDAP_CONNECTIONLESS
1325 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1327 ber_free_buf( ber );
1328 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1332 #ifdef LDAP_CONNECTIONLESS
1333 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1335 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1336 ber_free_buf( ber );
1338 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1339 num_bytes_sent += bytes;
1342 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1343 #ifdef LDAP_CONNECTIONLESS
1347 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1348 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1351 LDAP_LOG( OPERATION, ENTRY,
1352 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1354 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1358 sl_release( mark, op->o_tmpmemctx );
1373 *code = LDAP_SUCCESS;
1377 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1379 LDAP_LOG( OPERATION, INFO,
1380 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1382 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1390 while ( (s = strchr( s, '\n' )) != NULL ) {
1395 if ( (c = strchr( s, ':' )) != NULL ) {
1399 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1403 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1407 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1413 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1415 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",