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 rc = op->o_callback->sc_response( op, rs );
233 if ( rc != SLAP_CB_CONTINUE ) return;
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 &&
271 op->o_protocol == LDAP_VERSION2 )
273 rc = ber_printf( ber, "t{ess" /*"}}"*/,
274 rs->sr_tag, rs->sr_err,
275 rs->sr_matched == NULL ? "" : rs->sr_matched,
276 rs->sr_text == NULL ? "" : rs->sr_text );
279 if ( rs->sr_type == REP_INTERMEDIATE ) {
280 rc = ber_printf( ber, "{it{" /*"}}"*/,
281 rs->sr_msgid, rs->sr_tag );
284 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
285 rs->sr_msgid, rs->sr_tag, rs->sr_err,
286 rs->sr_matched == NULL ? "" : rs->sr_matched,
287 rs->sr_text == NULL ? "" : rs->sr_text );
291 if ( rs->sr_ref != NULL ) {
292 assert( rs->sr_err == LDAP_REFERRAL );
293 rc = ber_printf( ber, "t{W}",
294 LDAP_TAG_REFERRAL, rs->sr_ref );
296 assert( rs->sr_err != LDAP_REFERRAL );
300 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
301 rc = ber_printf( ber, "tO",
302 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
306 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
308 if ( rs->sr_rspoid != NULL ) {
309 rc = ber_printf( ber, "ts",
310 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
312 if( rc != -1 && rs->sr_rspdata != NULL ) {
313 rc = ber_printf( ber, "tO",
314 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
319 rc = ber_printf( ber, /*"{"*/ "N}" );
322 if( rc != -1 && rs->sr_ctrls != NULL ) {
323 rc = send_ldap_controls( ber, rs->sr_ctrls );
327 rc = ber_printf( ber, /*"{"*/ "N}" );
330 #ifdef LDAP_CONNECTIONLESS
331 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
332 rc = ber_printf( ber, /*"{"*/ "N}" );
338 LDAP_LOG( OPERATION, ERR,
339 "send_ldap_response: conn %lu ber_printf failed\n",
340 op->o_connid, 0, 0 );
342 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
345 #ifdef LDAP_CONNECTIONLESS
346 if (!op->o_conn || op->o_conn->c_is_udp == 0)
353 bytes = send_ldap_ber( op->o_conn, ber );
354 #ifdef LDAP_CONNECTIONLESS
355 if (!op->o_conn || op->o_conn->c_is_udp == 0)
361 LDAP_LOG( OPERATION, ERR,
362 "send_ldap_response: conn %lu ber write failed\n",
363 op->o_connid ? op->o_connid : 0, 0, 0 );
365 Debug( LDAP_DEBUG_ANY,
366 "send_ldap_response: ber write failed\n",
374 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
375 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, ( rs->sr_matched != NULL ) ? (void *)ch_strdup( rs->sr_matched ) : NULL );
376 /* RESULT_TEXT is dup'd by pblock_set */
377 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, rs->sr_text );
378 #endif /* LDAP_SLAPI */
380 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
381 num_bytes_sent += bytes;
383 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
389 send_ldap_disconnect( Operation *op, SlapReply *rs )
391 #define LDAP_UNSOLICITED_ERROR(e) \
392 ( (e) == LDAP_PROTOCOL_ERROR \
393 || (e) == LDAP_STRONG_AUTH_REQUIRED \
394 || (e) == LDAP_UNAVAILABLE )
396 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
398 rs->sr_type = REP_EXTENDED;
401 LDAP_LOG( OPERATION, ENTRY,
402 "send_ldap_disconnect: conn %lu %d:%s\n",
403 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
405 Debug( LDAP_DEBUG_TRACE,
406 "send_ldap_disconnect %d:%s\n",
407 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
411 if ( op->o_protocol < LDAP_VERSION3 ) {
412 rs->sr_rspoid = NULL;
413 rs->sr_tag = req2res( op->o_tag );
414 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
417 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
418 rs->sr_tag = LDAP_RES_EXTENDED;
422 send_ldap_response( op, rs );
424 Statslog( LDAP_DEBUG_STATS,
425 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
426 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
430 slap_send_ldap_result( Operation *op, SlapReply *rs )
433 const char *otext = rs->sr_text;
434 BerVarray oref = rs->sr_ref;
436 rs->sr_type = REP_RESULT;
438 assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
441 LDAP_LOG( OPERATION, ENTRY,
442 "send_ldap_result: conn %lu op=%lu p=%d\n",
443 op->o_connid, op->o_opid, op->o_protocol );
445 Debug( LDAP_DEBUG_TRACE,
446 "send_ldap_result: conn=%lu op=%lu p=%d\n",
447 op->o_connid, op->o_opid, op->o_protocol );
451 LDAP_LOG( OPERATION, ARGS,
452 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
453 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
454 rs->sr_text ? rs->sr_text : "" );
456 Debug( LDAP_DEBUG_ARGS,
457 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
458 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
459 rs->sr_text ? rs->sr_text : "" );
465 LDAP_LOG( OPERATION, ARGS,
466 "send_ldap_result: referral=\"%s\"\n",
467 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
469 Debug( LDAP_DEBUG_ARGS,
470 "send_ldap_result: referral=\"%s\"\n",
471 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
476 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
478 if ( rs->sr_err == LDAP_REFERRAL ) {
479 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
480 if( op->o_domain_scope ) {
484 if( rs->sr_ref == NULL ) {
485 rs->sr_err = LDAP_NO_SUCH_OBJECT;
486 } else if ( op->o_protocol < LDAP_VERSION3 ) {
487 rs->sr_err = LDAP_PARTIAL_RESULTS;
491 if ( op->o_protocol < LDAP_VERSION3 ) {
492 tmp = v2ref( rs->sr_ref, rs->sr_text );
497 rs->sr_tag = req2res( op->o_tag );
498 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
500 send_ldap_response( op, rs );
502 if ( op->o_tag == LDAP_REQ_SEARCH ) {
504 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
505 rs->sr_err, rs->sr_nentries );
507 Statslog( LDAP_DEBUG_STATS,
508 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
509 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
510 rs->sr_text ? rs->sr_text : "" );
512 Statslog( LDAP_DEBUG_STATS,
513 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
514 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
515 rs->sr_text ? rs->sr_text : "" );
518 if( tmp != NULL ) ch_free(tmp);
524 send_ldap_sasl( Operation *op, SlapReply *rs )
526 rs->sr_type = REP_SASL;
528 LDAP_LOG( OPERATION, ENTRY,
529 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
530 op->o_connid, rs->sr_err,
531 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
533 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
535 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
538 rs->sr_tag = req2res( op->o_tag );
539 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
541 send_ldap_response( op, rs );
545 slap_send_ldap_extended( Operation *op, SlapReply *rs )
547 rs->sr_type = REP_EXTENDED;
550 LDAP_LOG( OPERATION, ENTRY,
551 "send_ldap_extended: err=%d oid=%s len=%ld\n",
552 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
553 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
555 Debug( LDAP_DEBUG_TRACE,
556 "send_ldap_extended: err=%d oid=%s len=%ld\n",
558 rs->sr_rspoid ? rs->sr_rspoid : "",
559 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
562 rs->sr_tag = req2res( op->o_tag );
563 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
565 send_ldap_response( op, rs );
569 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
571 rs->sr_type = REP_INTERMEDIATE;
573 LDAP_LOG( OPERATION, ENTRY,
574 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
575 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
576 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
578 Debug( LDAP_DEBUG_TRACE,
579 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
581 rs->sr_rspoid ? rs->sr_rspoid : "",
582 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
584 rs->sr_tag = LDAP_RES_INTERMEDIATE;
585 rs->sr_msgid = op->o_msgid;
586 send_ldap_response( op, rs );
590 slap_send_search_entry( Operation *op, SlapReply *rs )
592 char berbuf[LBER_ELEMENT_SIZEOF];
593 BerElement *ber = (BerElement *)berbuf;
595 int i, j, rc=-1, bytes;
599 AccessControlState acl_state = ACL_STATE_INIT;
601 /* Support for computed attribute plugins */
602 computed_attr_context ctx;
607 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
609 /* a_flags: array of flags telling if the i-th element will be
610 * returned or filtered out
611 * e_flags: array of a_flags
613 char **e_flags = NULL;
615 rs->sr_type = REP_SEARCH;
616 if (op->o_callback && op->o_callback->sc_response) {
617 rc = op->o_callback->sc_response( op, rs );
618 if ( rc != SLAP_CB_CONTINUE ) return rc;
622 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
623 op->o_connid, rs->sr_entry->e_name.bv_val,
624 op->ors_attrsonly ? " (attrsOnly)" : "" );
626 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
627 rs->sr_entry->e_name.bv_val,
628 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
631 mark = sl_mark( op->o_tmpmemctx );
633 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
636 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
637 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
639 Debug( LDAP_DEBUG_ACL,
640 "send_search_entry: access to entry not allowed\n",
644 sl_release( mark, op->o_tmpmemctx );
648 edn = rs->sr_entry->e_nname.bv_val;
650 if ( op->o_res_ber ) {
651 /* read back control or LDAP_CONNECTIONLESS */
657 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
658 bv.bv_len = siz + len;
659 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
661 ber_init2( ber, &bv, LBER_USE_DER );
662 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
665 #ifdef LDAP_CONNECTIONLESS
666 if ( op->o_conn && op->o_conn->c_is_udp ) {
668 if ( op->o_protocol == LDAP_VERSION2 ) {
669 rc = ber_printf(ber, "t{O{" /*}}*/,
670 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
672 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
673 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
677 if ( op->o_res_ber ) {
678 /* read back control */
679 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
681 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
682 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
687 LDAP_LOG( OPERATION, ERR,
688 "send_search_entry: conn %lu ber_printf failed\n",
689 op->o_connid, 0, 0 );
691 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
694 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
695 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
699 /* check for special all user attributes ("*") type */
700 userattrs = ( rs->sr_attrs == NULL ) ? 1
701 : an_find( rs->sr_attrs, &AllUser );
703 /* check for special all operational attributes ("+") type */
704 opattrs = ( rs->sr_attrs == NULL ) ? 0
705 : an_find( rs->sr_attrs, &AllOper );
707 /* create an array of arrays of flags. Each flag corresponds
708 * to particular value of attribute and equals 1 if value matches
709 * to ValuesReturnFilter or 0 if not
711 if ( op->o_vrFilter != NULL ) {
715 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
716 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
719 size = i * sizeof(char *) + k;
722 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
723 if( e_flags == NULL ) {
725 LDAP_LOG( OPERATION, ERR,
726 "send_search_entry: conn %lu sl_calloc failed\n",
727 op->o_connid ? op->o_connid : 0, 0, 0 );
729 Debug( LDAP_DEBUG_ANY,
730 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
734 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
737 a_flags = (char *)(e_flags + i);
738 memset( a_flags, 0, k );
739 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
740 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
741 e_flags[i] = a_flags;
745 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
748 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
749 "conn %lu matched values filtering failed\n",
750 op->o_connid ? op->o_connid : 0, 0, 0 );
752 Debug( LDAP_DEBUG_ANY,
753 "matched values filtering failed\n", 0, 0, 0 );
755 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
756 send_ldap_error( op, rs, LDAP_OTHER,
757 "matched values filtering error" );
763 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
764 AttributeDescription *desc = a->a_desc;
766 if ( rs->sr_attrs == NULL ) {
767 /* all attrs request, skip operational attributes */
768 if( is_at_operational( desc->ad_type ) ) {
773 /* specific attrs requested */
774 if ( is_at_operational( desc->ad_type ) ) {
775 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
780 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
786 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
787 ACL_READ, &acl_state ) )
791 "send_search_entry: conn %lu access to attribute %s not "
792 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
794 Debug( LDAP_DEBUG_ACL, "acl: "
795 "access to attribute %s not allowed\n",
796 desc->ad_cname.bv_val, 0, 0 );
801 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
803 LDAP_LOG( OPERATION, ERR,
804 "send_search_entry: conn %lu ber_printf failed\n",
805 op->o_connid, 0, 0 );
807 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
810 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
811 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
815 if ( ! op->ors_attrsonly ) {
816 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
817 if ( ! access_allowed( op, rs->sr_entry,
818 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
822 "send_search_entry: conn %lu "
823 "access to attribute %s, value %d not allowed\n",
824 op->o_connid, desc->ad_cname.bv_val, i );
826 Debug( LDAP_DEBUG_ACL,
827 "acl: access to attribute %s, "
828 "value %d not allowed\n",
829 desc->ad_cname.bv_val, i, 0 );
835 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
839 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
841 LDAP_LOG( OPERATION, ERR,
842 "send_search_entry: conn %lu "
843 "ber_printf failed.\n", op->o_connid, 0, 0 );
845 Debug( LDAP_DEBUG_ANY,
846 "ber_printf failed\n", 0, 0, 0 );
849 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
850 send_ldap_error( op, rs, LDAP_OTHER,
851 "encoding values error" );
857 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
859 LDAP_LOG( OPERATION, ERR,
860 "send_search_entry: conn %lu ber_printf failed\n",
861 op->o_connid, 0, 0 );
863 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
866 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
867 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
872 /* eventually will loop through generated operational attributes */
873 /* only have subschemaSubentry and numSubordinates are implemented */
874 aa = backend_operational( op, rs, opattrs );
876 if ( aa != NULL && op->o_vrFilter != NULL ) {
880 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
881 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
884 size = i * sizeof(char *) + k;
886 char *a_flags, **tmp;
889 * Reuse previous memory - we likely need less space
890 * for operational attributes
892 tmp = sl_realloc( e_flags, i * sizeof(char *) + k,
896 LDAP_LOG( OPERATION, ERR,
897 "send_search_entry: conn %lu "
899 "for matched values filtering\n",
902 Debug( LDAP_DEBUG_ANY,
903 "send_search_entry: conn %lu "
905 "for matched values filtering\n",
906 op->o_connid, 0, 0 );
908 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
909 send_ldap_error( op, rs, LDAP_OTHER,
910 "not enough memory for matched values filtering" );
914 a_flags = (char *)(e_flags + i);
915 memset( a_flags, 0, k );
916 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
917 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
918 e_flags[i] = a_flags;
921 rc = filter_matched_values(op, aa, &e_flags) ;
925 LDAP_LOG( OPERATION, ERR,
926 "send_search_entry: conn %lu "
927 "matched values filtering failed\n",
928 op->o_connid ? op->o_connid : 0, 0, 0);
930 Debug( LDAP_DEBUG_ANY,
931 "matched values filtering failed\n", 0, 0, 0 );
933 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
934 send_ldap_error( op, rs, LDAP_OTHER,
935 "matched values filtering error" );
941 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
942 AttributeDescription *desc = a->a_desc;
944 if ( rs->sr_attrs == NULL ) {
945 /* all attrs request, skip operational attributes */
946 if( is_at_operational( desc->ad_type ) ) {
951 /* specific attrs requested */
952 if( is_at_operational( desc->ad_type ) ) {
953 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
957 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
963 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
964 ACL_READ, &acl_state ) )
968 "send_search_entry: conn %lu "
969 "access to attribute %s not allowed\n",
970 op->o_connid, desc->ad_cname.bv_val, 0 );
972 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
973 "not allowed\n", desc->ad_cname.bv_val, 0, 0 );
979 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
982 LDAP_LOG( OPERATION, ERR,
983 "send_search_entry: conn %lu "
984 "ber_printf failed\n", op->o_connid, 0, 0 );
986 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
989 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
990 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
995 if ( ! op->ors_attrsonly ) {
996 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
997 if ( ! access_allowed( op, rs->sr_entry,
998 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1001 LDAP_LOG( ACL, INFO,
1002 "send_search_entry: conn %lu "
1003 "access to %s, value %d not allowed\n",
1004 op->o_connid, desc->ad_cname.bv_val, i );
1006 Debug( LDAP_DEBUG_ACL,
1007 "send_search_entry: access to attribute %s, "
1008 "value %d not allowed\n",
1009 desc->ad_cname.bv_val, i, 0 );
1015 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1019 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1021 LDAP_LOG( OPERATION, ERR,
1022 "send_search_entry: conn %lu ber_printf failed\n",
1023 op->o_connid, 0, 0 );
1025 Debug( LDAP_DEBUG_ANY,
1026 "ber_printf failed\n", 0, 0, 0 );
1029 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1030 send_ldap_error( op, rs, LDAP_OTHER,
1031 "encoding values error" );
1038 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1040 LDAP_LOG( OPERATION, ERR,
1041 "send_search_entry: conn %lu ber_printf failed\n",
1042 op->o_connid, 0, 0 );
1044 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1047 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1048 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1056 * First, setup the computed attribute context that is
1057 * passed to all plugins.
1059 ctx.cac_pb = op->o_pb;
1060 ctx.cac_attrs = rs->sr_attrs;
1061 ctx.cac_attrsonly = op->ors_attrsonly;
1062 ctx.cac_userattrs = userattrs;
1063 ctx.cac_opattrs = opattrs;
1064 ctx.cac_acl_state = acl_state;
1065 ctx.cac_private = (void *)ber;
1068 * For each client requested attribute, call the plugins.
1070 if ( rs->sr_attrs != NULL ) {
1071 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1072 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1073 rs->sr_entry, slapi_x_compute_output_ber );
1080 * Technically we shouldn't be returning operational attributes
1081 * when the user requested only user attributes. We'll let the
1082 * plugin decide whether to be naughty or not.
1084 rc = compute_evaluator( &ctx, "*",
1085 rs->sr_entry, slapi_x_compute_output_ber );
1088 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1089 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1092 #endif /* LDAP_SLAPI */
1096 sl_free( e_flags, op->o_tmpmemctx );
1101 rc = ber_printf( ber, /*{{*/ "}N}" );
1103 if( rc != -1 && rs->sr_ctrls != NULL ) {
1104 rc = send_ldap_controls( ber, rs->sr_ctrls );
1108 #ifdef LDAP_CONNECTIONLESS
1109 if( op->o_conn && op->o_conn->c_is_udp ) {
1110 if ( op->o_protocol != LDAP_VERSION2 ) {
1111 rc = ber_printf( ber, /*{*/ "N}" );
1115 if ( op->o_res_ber == NULL ) {
1116 rc = ber_printf( ber, /*{*/ "N}" );
1122 LDAP_LOG( OPERATION, ERR,
1123 "send_search_entry: conn %lu ber_printf failed\n",
1124 op->o_connid, 0, 0 );
1126 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1129 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1130 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1131 sl_release( mark, op->o_tmpmemctx );
1135 if ( op->o_res_ber == NULL ) {
1136 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1137 ber_free_buf( ber );
1141 LDAP_LOG( OPERATION, ERR,
1142 "send_search_entry: conn %lu ber write failed.\n",
1143 op->o_connid, 0, 0 );
1145 Debug( LDAP_DEBUG_ANY,
1146 "send_search_entry: ber write failed\n",
1150 sl_release( mark, op->o_tmpmemctx );
1155 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1156 num_bytes_sent += bytes;
1159 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1162 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1163 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1166 LDAP_LOG( OPERATION, ENTRY,
1167 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1169 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1175 sl_release( mark, op->o_tmpmemctx );
1176 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1181 slap_send_search_reference( Operation *op, SlapReply *rs )
1183 char berbuf[LBER_ELEMENT_SIZEOF];
1184 BerElement *ber = (BerElement *)berbuf;
1189 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1190 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1192 rs->sr_type = REP_SEARCHREF;
1193 if (op->o_callback && op->o_callback->sc_response) {
1194 rc = op->o_callback->sc_response( op, rs );
1195 if ( rc != SLAP_CB_CONTINUE ) return rc;
1198 mark = sl_mark( op->o_tmpmemctx );
1201 LDAP_LOG( OPERATION, ENTRY,
1202 "send_search_reference: conn %lu dn=\"%s\"\n",
1204 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1206 Debug( LDAP_DEBUG_TRACE,
1207 "=> send_search_reference: dn=\"%s\"\n",
1208 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1211 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1212 ad_entry, NULL, ACL_READ, NULL ) )
1215 LDAP_LOG( ACL, INFO,
1216 "send_search_reference: conn %lu "
1217 "access to entry %s not allowed\n",
1218 op->o_connid, rs->sr_entry->e_dn, 0 );
1220 Debug( LDAP_DEBUG_ACL,
1221 "send_search_reference: access to entry not allowed\n",
1228 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1229 ad_ref, NULL, ACL_READ, NULL ) )
1232 LDAP_LOG( ACL, INFO,
1233 "send_search_reference: conn %lu access "
1234 "to reference not allowed.\n", op->o_connid, 0, 0 );
1236 Debug( LDAP_DEBUG_ACL,
1237 "send_search_reference: access "
1238 "to reference not allowed\n",
1245 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1246 if( op->o_domain_scope ) {
1248 LDAP_LOG( OPERATION, ERR,
1249 "send_search_reference: conn %lu domainScope control in (%s).\n",
1250 op->o_connid, rs->sr_entry->e_dn, 0 );
1252 Debug( LDAP_DEBUG_ANY,
1253 "send_search_reference: domainScope control in (%s)\n",
1254 rs->sr_entry->e_dn, 0, 0 );
1261 if( rs->sr_ref == NULL ) {
1263 LDAP_LOG( OPERATION, ERR,
1264 "send_search_reference: conn %lu null ref in (%s).\n",
1265 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1267 Debug( LDAP_DEBUG_ANY,
1268 "send_search_reference: null ref in (%s)\n",
1269 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1275 if( op->o_protocol < LDAP_VERSION3 ) {
1276 /* save the references for the result */
1277 if( rs->sr_ref[0].bv_val != NULL ) {
1278 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1285 #ifdef LDAP_CONNECTIONLESS
1286 if( op->o_conn && op->o_conn->c_is_udp ) {
1287 ber = op->o_res_ber;
1291 ber_init_w_nullc( ber, LBER_USE_DER );
1292 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1295 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1296 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1298 if( rc != -1 && rs->sr_ctrls != NULL ) {
1299 rc = send_ldap_controls( ber, rs->sr_ctrls );
1303 rc = ber_printf( ber, /*"{"*/ "N}" );
1308 LDAP_LOG( OPERATION, ERR,
1309 "send_search_reference: conn %lu "
1310 "ber_printf failed.\n", op->o_connid, 0, 0 );
1312 Debug( LDAP_DEBUG_ANY,
1313 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1316 #ifdef LDAP_CONNECTIONLESS
1317 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1319 ber_free_buf( ber );
1320 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1324 #ifdef LDAP_CONNECTIONLESS
1325 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1327 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1328 ber_free_buf( ber );
1330 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1331 num_bytes_sent += bytes;
1334 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1335 #ifdef LDAP_CONNECTIONLESS
1339 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1340 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1343 LDAP_LOG( OPERATION, ENTRY,
1344 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1346 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1350 sl_release( mark, op->o_tmpmemctx );
1365 *code = LDAP_SUCCESS;
1369 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1371 LDAP_LOG( OPERATION, INFO,
1372 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1374 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1382 while ( (s = strchr( s, '\n' )) != NULL ) {
1387 if ( (c = strchr( s, ':' )) != NULL ) {
1391 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1395 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1399 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1405 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1407 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1418 int slap_read_controls(
1422 const struct berval *oid,
1423 LDAPControl **ctrl )
1427 char berbuf[LBER_ELEMENT_SIZEOF];
1428 BerElement *ber = (BerElement *) berbuf;
1434 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1435 oid->bv_val, e->e_dn, 0 );
1437 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1438 oid->bv_val, e->e_dn, 0 );
1442 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1443 op->o_preread_attrs : op->o_postread_attrs;
1445 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1446 bv.bv_len = siz + len;
1447 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1449 ber_init2( ber, &bv, LBER_USE_DER );
1450 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1452 /* create new operation */
1455 myop.o_res_ber = ber;
1457 rc = slap_send_search_entry( &myop, rs );
1460 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1462 if( rc == LBER_ERROR ) return LDAP_OTHER;
1464 c.ldctl_oid = oid->bv_val;
1465 c.ldctl_iscritical = 0;
1467 *ctrl = sl_calloc( 1, sizeof(LDAPControl), NULL );
1469 return LDAP_SUCCESS;