1 /* result.c - routines to send ldap results, errors, and referrals */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
33 #include <ac/string.h>
36 #include <ac/unistd.h>
44 int slap_null_cb( Operation *op, SlapReply *rs )
49 static char *v2ref( BerVarray ref, const char *text )
51 size_t len = 0, i = 0;
56 return ch_strdup(text);
64 if (text[len-1] != '\n') {
69 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
72 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
74 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
85 strcpy( v2+len, "Referral:" );
86 len += sizeof("Referral:");
88 for( i=0; ref[i].bv_val != NULL; i++ ) {
89 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
92 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
94 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
99 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
100 len += ref[i].bv_len;
101 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
110 static ber_tag_t req2res( ber_tag_t tag )
115 case LDAP_REQ_COMPARE:
116 case LDAP_REQ_EXTENDED:
117 case LDAP_REQ_MODIFY:
118 case LDAP_REQ_MODRDN:
122 case LDAP_REQ_DELETE:
123 tag = LDAP_RES_DELETE;
126 case LDAP_REQ_ABANDON:
127 case LDAP_REQ_UNBIND:
131 case LDAP_REQ_SEARCH:
132 tag = LDAP_RES_SEARCH_RESULT;
142 static long send_ldap_ber(
148 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
150 /* write only one pdu at a time - wait til it's our turn */
151 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
153 /* lock the connection */
154 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
161 if ( connection_state_closing( conn ) ) {
162 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
163 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
168 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
175 * we got an error. if it's ewouldblock, we need to
176 * wait on the socket being writable. otherwise, figure
177 * it's a hard error and return.
181 LDAP_LOG( OPERATION, ERR,
182 "send_ldap_ber: conn %lu ber_flush failed err=%d (%s)\n",
183 conn ? conn->c_connid : 0, err, sock_errstr(err) );
185 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
186 err, sock_errstr(err), 0 );
189 if ( err != EWOULDBLOCK && err != EAGAIN ) {
190 connection_closing( conn );
192 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
193 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
198 /* wait for socket to be write-ready */
199 conn->c_writewaiter = 1;
200 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
201 slapd_set_write( sd, 1 );
203 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
204 conn->c_writewaiter = 0;
207 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
208 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
214 send_ldap_controls( BerElement *ber, LDAPControl **c )
217 if( c == NULL ) return 0;
219 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
220 if( rc == -1 ) return rc;
222 for( ; *c != NULL; c++) {
223 rc = ber_printf( ber, "{s" /*}*/, (*c)->ldctl_oid );
225 if( (*c)->ldctl_iscritical ) {
226 rc = ber_printf( ber, "b",
227 (ber_int_t) (*c)->ldctl_iscritical ) ;
228 if( rc == -1 ) return rc;
231 if( (*c)->ldctl_value.bv_val != NULL ) {
232 rc = ber_printf( ber, "O", &((*c)->ldctl_value));
233 if( rc == -1 ) return rc;
236 rc = ber_printf( ber, /*{*/"N}" );
237 if( rc == -1 ) return rc;
240 rc = ber_printf( ber, /*{*/"N}" );
250 BerElementBuffer berbuf;
251 BerElement *ber = (BerElement *) &berbuf;
255 if (op->o_callback && op->o_callback->sc_response) {
256 rc = op->o_callback->sc_response( op, rs );
257 if ( rc != SLAP_CB_CONTINUE ) return;
260 #ifdef LDAP_CONNECTIONLESS
261 if (op->o_conn && op->o_conn->c_is_udp)
266 ber_init_w_nullc( ber, LBER_USE_DER );
267 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
271 LDAP_LOG( OPERATION, ENTRY,
272 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
273 rs->sr_msgid, rs->sr_tag, rs->sr_err );
275 Debug( LDAP_DEBUG_TRACE,
276 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
277 rs->sr_msgid, rs->sr_tag, rs->sr_err );
282 LDAP_LOG( OPERATION, ARGS,
283 "send_ldap_response: conn %lu ref=\"%s\"\n",
285 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
287 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
288 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
293 #ifdef LDAP_CONNECTIONLESS
294 if (op->o_conn && op->o_conn->c_is_udp &&
295 op->o_protocol == LDAP_VERSION2 )
297 rc = ber_printf( ber, "t{ess" /*"}}"*/,
298 rs->sr_tag, rs->sr_err,
299 rs->sr_matched == NULL ? "" : rs->sr_matched,
300 rs->sr_text == NULL ? "" : rs->sr_text );
303 if ( rs->sr_type == REP_INTERMEDIATE ) {
304 rc = ber_printf( ber, "{it{" /*"}}"*/,
305 rs->sr_msgid, rs->sr_tag );
308 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
309 rs->sr_msgid, rs->sr_tag, rs->sr_err,
310 rs->sr_matched == NULL ? "" : rs->sr_matched,
311 rs->sr_text == NULL ? "" : rs->sr_text );
315 if ( rs->sr_ref != NULL ) {
316 assert( rs->sr_err == LDAP_REFERRAL );
317 rc = ber_printf( ber, "t{W}",
318 LDAP_TAG_REFERRAL, rs->sr_ref );
320 assert( rs->sr_err != LDAP_REFERRAL );
324 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
325 rc = ber_printf( ber, "tO",
326 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
330 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
332 if ( rs->sr_rspoid != NULL ) {
333 rc = ber_printf( ber, "ts",
334 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
336 if( rc != -1 && rs->sr_rspdata != NULL ) {
337 rc = ber_printf( ber, "tO",
338 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
343 rc = ber_printf( ber, /*"{"*/ "N}" );
346 if( rc != -1 && rs->sr_ctrls != NULL ) {
347 rc = send_ldap_controls( ber, rs->sr_ctrls );
351 rc = ber_printf( ber, /*"{"*/ "N}" );
354 #ifdef LDAP_CONNECTIONLESS
355 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
356 rc = ber_printf( ber, /*"{"*/ "N}" );
362 LDAP_LOG( OPERATION, ERR,
363 "send_ldap_response: conn %lu ber_printf failed\n",
364 op->o_connid, 0, 0 );
366 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
369 #ifdef LDAP_CONNECTIONLESS
370 if (!op->o_conn || op->o_conn->c_is_udp == 0)
377 bytes = send_ldap_ber( op->o_conn, ber );
378 #ifdef LDAP_CONNECTIONLESS
379 if (!op->o_conn || op->o_conn->c_is_udp == 0)
385 LDAP_LOG( OPERATION, ERR,
386 "send_ldap_response: conn %lu ber write failed\n",
387 op->o_connid ? op->o_connid : 0, 0, 0 );
389 Debug( LDAP_DEBUG_ANY,
390 "send_ldap_response: ber write failed\n",
399 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
400 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
401 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
403 #endif /* LDAP_SLAPI */
405 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
406 num_bytes_sent += bytes;
408 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
414 send_ldap_disconnect( Operation *op, SlapReply *rs )
416 #define LDAP_UNSOLICITED_ERROR(e) \
417 ( (e) == LDAP_PROTOCOL_ERROR \
418 || (e) == LDAP_STRONG_AUTH_REQUIRED \
419 || (e) == LDAP_UNAVAILABLE )
421 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
423 rs->sr_type = REP_EXTENDED;
426 LDAP_LOG( OPERATION, ENTRY,
427 "send_ldap_disconnect: conn %lu %d:%s\n",
428 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
430 Debug( LDAP_DEBUG_TRACE,
431 "send_ldap_disconnect %d:%s\n",
432 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
436 if ( op->o_protocol < LDAP_VERSION3 ) {
437 rs->sr_rspoid = NULL;
438 rs->sr_tag = req2res( op->o_tag );
439 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
442 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
443 rs->sr_tag = LDAP_RES_EXTENDED;
447 send_ldap_response( op, rs );
449 Statslog( LDAP_DEBUG_STATS,
450 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
451 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
455 slap_send_ldap_result( Operation *op, SlapReply *rs )
458 const char *otext = rs->sr_text;
459 BerVarray oref = rs->sr_ref;
461 rs->sr_type = REP_RESULT;
463 assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
466 LDAP_LOG( OPERATION, ENTRY,
467 "send_ldap_result: conn %lu op=%lu p=%d\n",
468 op->o_connid, op->o_opid, op->o_protocol );
470 Debug( LDAP_DEBUG_TRACE,
471 "send_ldap_result: conn=%lu op=%lu p=%d\n",
472 op->o_connid, op->o_opid, op->o_protocol );
476 LDAP_LOG( OPERATION, ARGS,
477 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
478 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
479 rs->sr_text ? rs->sr_text : "" );
481 Debug( LDAP_DEBUG_ARGS,
482 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
483 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
484 rs->sr_text ? rs->sr_text : "" );
490 LDAP_LOG( OPERATION, ARGS,
491 "send_ldap_result: referral=\"%s\"\n",
492 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
494 Debug( LDAP_DEBUG_ARGS,
495 "send_ldap_result: referral=\"%s\"\n",
496 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
501 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
503 if ( rs->sr_err == LDAP_REFERRAL ) {
504 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
505 if( op->o_domain_scope ) {
509 if( rs->sr_ref == NULL ) {
510 rs->sr_err = LDAP_NO_SUCH_OBJECT;
511 } else if ( op->o_protocol < LDAP_VERSION3 ) {
512 rs->sr_err = LDAP_PARTIAL_RESULTS;
518 * Call pre-result plugins. To avoid infinite recursion plugins
519 * should just set SLAPI_RESULT_CODE rather than sending a
520 * result if they wish to change the result.
523 slapi_x_pblock_set_operation( op->o_pb, op );
524 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
525 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
526 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
528 (void) doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN, op->o_pb );
530 #endif /* LDAP_SLAPI */
532 if ( op->o_protocol < LDAP_VERSION3 ) {
533 tmp = v2ref( rs->sr_ref, rs->sr_text );
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 );
543 if ( op->o_tag == LDAP_REQ_SEARCH ) {
545 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
546 rs->sr_err, rs->sr_nentries );
548 Statslog( LDAP_DEBUG_STATS,
549 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
550 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
551 rs->sr_text ? rs->sr_text : "" );
553 Statslog( LDAP_DEBUG_STATS,
554 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
555 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
556 rs->sr_text ? rs->sr_text : "" );
559 if( tmp != NULL ) ch_free(tmp);
565 send_ldap_sasl( Operation *op, SlapReply *rs )
567 rs->sr_type = REP_SASL;
569 LDAP_LOG( OPERATION, ENTRY,
570 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
571 op->o_connid, rs->sr_err,
572 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
574 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
576 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
579 rs->sr_tag = req2res( op->o_tag );
580 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
582 send_ldap_response( op, rs );
586 slap_send_ldap_extended( Operation *op, SlapReply *rs )
588 rs->sr_type = REP_EXTENDED;
591 LDAP_LOG( OPERATION, ENTRY,
592 "send_ldap_extended: err=%d oid=%s len=%ld\n",
593 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
594 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
596 Debug( LDAP_DEBUG_TRACE,
597 "send_ldap_extended: err=%d oid=%s len=%ld\n",
599 rs->sr_rspoid ? rs->sr_rspoid : "",
600 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
603 rs->sr_tag = req2res( op->o_tag );
604 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
606 send_ldap_response( op, rs );
610 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
612 rs->sr_type = REP_INTERMEDIATE;
614 LDAP_LOG( OPERATION, ENTRY,
615 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
616 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
617 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
619 Debug( LDAP_DEBUG_TRACE,
620 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
622 rs->sr_rspoid ? rs->sr_rspoid : "",
623 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
625 rs->sr_tag = LDAP_RES_INTERMEDIATE;
626 rs->sr_msgid = op->o_msgid;
627 send_ldap_response( op, rs );
631 slap_send_search_entry( Operation *op, SlapReply *rs )
633 BerElementBuffer berbuf;
634 BerElement *ber = (BerElement *) &berbuf;
636 int i, j, rc=-1, bytes;
640 AccessControlState acl_state = ACL_STATE_INIT;
642 /* Support for computed attribute plugins */
643 computed_attr_context ctx;
648 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
650 /* a_flags: array of flags telling if the i-th element will be
651 * returned or filtered out
652 * e_flags: array of a_flags
654 char **e_flags = NULL;
656 rs->sr_type = REP_SEARCH;
657 if (op->o_callback && op->o_callback->sc_response) {
658 rc = op->o_callback->sc_response( op, rs );
659 if ( rc != SLAP_CB_CONTINUE ) return rc;
663 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
664 op->o_connid, rs->sr_entry->e_name.bv_val,
665 op->ors_attrsonly ? " (attrsOnly)" : "" );
667 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
668 rs->sr_entry->e_name.bv_val,
669 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
672 mark = sl_mark( op->o_tmpmemctx );
674 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
677 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
678 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
680 Debug( LDAP_DEBUG_ACL,
681 "send_search_entry: access to entry not allowed\n",
685 sl_release( mark, op->o_tmpmemctx );
689 edn = rs->sr_entry->e_nname.bv_val;
691 if ( op->o_res_ber ) {
692 /* read back control or LDAP_CONNECTIONLESS */
698 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
699 bv.bv_len = siz + len;
700 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
702 ber_init2( ber, &bv, LBER_USE_DER );
703 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
706 #ifdef LDAP_CONNECTIONLESS
707 if ( op->o_conn && op->o_conn->c_is_udp ) {
709 if ( op->o_protocol == LDAP_VERSION2 ) {
710 rc = ber_printf(ber, "t{O{" /*}}*/,
711 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
713 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
714 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
718 if ( op->o_res_ber ) {
719 /* read back control */
720 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
722 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
723 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
728 LDAP_LOG( OPERATION, ERR,
729 "send_search_entry: conn %lu ber_printf failed\n",
730 op->o_connid, 0, 0 );
732 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
735 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
736 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
740 /* check for special all user attributes ("*") type */
741 userattrs = ( rs->sr_attrs == NULL ) ? 1
742 : an_find( rs->sr_attrs, &AllUser );
744 /* check for special all operational attributes ("+") type */
745 opattrs = ( rs->sr_attrs == NULL ) ? 0
746 : an_find( rs->sr_attrs, &AllOper );
748 /* create an array of arrays of flags. Each flag corresponds
749 * to particular value of attribute and equals 1 if value matches
750 * to ValuesReturnFilter or 0 if not
752 if ( op->o_vrFilter != NULL ) {
756 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
757 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
760 size = i * sizeof(char *) + k;
763 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
764 if( e_flags == NULL ) {
766 LDAP_LOG( OPERATION, ERR,
767 "send_search_entry: conn %lu sl_calloc failed\n",
768 op->o_connid ? op->o_connid : 0, 0, 0 );
770 Debug( LDAP_DEBUG_ANY,
771 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
775 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
778 a_flags = (char *)(e_flags + i);
779 memset( a_flags, 0, k );
780 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
781 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
782 e_flags[i] = a_flags;
786 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
789 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
790 "conn %lu matched values filtering failed\n",
791 op->o_connid ? op->o_connid : 0, 0, 0 );
793 Debug( LDAP_DEBUG_ANY,
794 "matched values filtering failed\n", 0, 0, 0 );
796 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
797 send_ldap_error( op, rs, LDAP_OTHER,
798 "matched values filtering error" );
804 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
805 AttributeDescription *desc = a->a_desc;
807 if ( rs->sr_attrs == NULL ) {
808 /* all attrs request, skip operational attributes */
809 if( is_at_operational( desc->ad_type ) ) {
814 /* specific attrs requested */
815 if ( is_at_operational( desc->ad_type ) ) {
816 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
821 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
827 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
828 ACL_READ, &acl_state ) )
832 "send_search_entry: conn %lu access to attribute %s not "
833 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
835 Debug( LDAP_DEBUG_ACL, "acl: "
836 "access to attribute %s not allowed\n",
837 desc->ad_cname.bv_val, 0, 0 );
842 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
844 LDAP_LOG( OPERATION, ERR,
845 "send_search_entry: conn %lu ber_printf failed\n",
846 op->o_connid, 0, 0 );
848 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
851 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
852 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
856 if ( ! op->ors_attrsonly ) {
857 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
858 if ( ! access_allowed( op, rs->sr_entry,
859 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
863 "send_search_entry: conn %lu "
864 "access to attribute %s, value %d not allowed\n",
865 op->o_connid, desc->ad_cname.bv_val, i );
867 Debug( LDAP_DEBUG_ACL,
868 "acl: access to attribute %s, "
869 "value %d not allowed\n",
870 desc->ad_cname.bv_val, i, 0 );
876 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
880 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
882 LDAP_LOG( OPERATION, ERR,
883 "send_search_entry: conn %lu "
884 "ber_printf failed.\n", op->o_connid, 0, 0 );
886 Debug( LDAP_DEBUG_ANY,
887 "ber_printf failed\n", 0, 0, 0 );
890 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
891 send_ldap_error( op, rs, LDAP_OTHER,
892 "encoding values error" );
898 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
900 LDAP_LOG( OPERATION, ERR,
901 "send_search_entry: conn %lu ber_printf failed\n",
902 op->o_connid, 0, 0 );
904 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
907 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
908 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
913 /* eventually will loop through generated operational attributes */
914 /* only have subschemaSubentry and numSubordinates are implemented */
915 aa = backend_operational( op, rs, opattrs );
917 if ( aa != NULL && op->o_vrFilter != NULL ) {
921 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
922 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
925 size = i * sizeof(char *) + k;
927 char *a_flags, **tmp;
930 * Reuse previous memory - we likely need less space
931 * for operational attributes
933 tmp = sl_realloc( e_flags, i * sizeof(char *) + k,
937 LDAP_LOG( OPERATION, ERR,
938 "send_search_entry: conn %lu "
940 "for matched values filtering\n",
943 Debug( LDAP_DEBUG_ANY,
944 "send_search_entry: conn %lu "
946 "for matched values filtering\n",
947 op->o_connid, 0, 0 );
949 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
950 send_ldap_error( op, rs, LDAP_OTHER,
951 "not enough memory for matched values filtering" );
955 a_flags = (char *)(e_flags + i);
956 memset( a_flags, 0, k );
957 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
958 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
959 e_flags[i] = a_flags;
962 rc = filter_matched_values(op, aa, &e_flags) ;
966 LDAP_LOG( OPERATION, ERR,
967 "send_search_entry: conn %lu "
968 "matched values filtering failed\n",
969 op->o_connid ? op->o_connid : 0, 0, 0);
971 Debug( LDAP_DEBUG_ANY,
972 "matched values filtering failed\n", 0, 0, 0 );
974 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
975 send_ldap_error( op, rs, LDAP_OTHER,
976 "matched values filtering error" );
982 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
983 AttributeDescription *desc = a->a_desc;
985 if ( rs->sr_attrs == NULL ) {
986 /* all attrs request, skip operational attributes */
987 if( is_at_operational( desc->ad_type ) ) {
992 /* specific attrs requested */
993 if( is_at_operational( desc->ad_type ) ) {
994 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
998 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1004 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1005 ACL_READ, &acl_state ) )
1008 LDAP_LOG( ACL, INFO,
1009 "send_search_entry: conn %lu "
1010 "access to attribute %s not allowed\n",
1011 op->o_connid, desc->ad_cname.bv_val, 0 );
1013 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
1014 "not allowed\n", desc->ad_cname.bv_val, 0, 0 );
1020 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1023 LDAP_LOG( OPERATION, ERR,
1024 "send_search_entry: conn %lu "
1025 "ber_printf failed\n", op->o_connid, 0, 0 );
1027 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1030 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1031 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
1036 if ( ! op->ors_attrsonly ) {
1037 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1038 if ( ! access_allowed( op, rs->sr_entry,
1039 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1042 LDAP_LOG( ACL, INFO,
1043 "send_search_entry: conn %lu "
1044 "access to %s, value %d not allowed\n",
1045 op->o_connid, desc->ad_cname.bv_val, i );
1047 Debug( LDAP_DEBUG_ACL,
1048 "send_search_entry: access to attribute %s, "
1049 "value %d not allowed\n",
1050 desc->ad_cname.bv_val, i, 0 );
1056 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1060 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1062 LDAP_LOG( OPERATION, ERR,
1063 "send_search_entry: conn %lu ber_printf failed\n",
1064 op->o_connid, 0, 0 );
1066 Debug( LDAP_DEBUG_ANY,
1067 "ber_printf failed\n", 0, 0, 0 );
1070 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1071 send_ldap_error( op, rs, LDAP_OTHER,
1072 "encoding values error" );
1079 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1081 LDAP_LOG( OPERATION, ERR,
1082 "send_search_entry: conn %lu ber_printf failed\n",
1083 op->o_connid, 0, 0 );
1085 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1088 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1089 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1097 * First, setup the computed attribute context that is
1098 * passed to all plugins.
1101 ctx.cac_pb = op->o_pb;
1102 ctx.cac_attrs = rs->sr_attrs;
1103 ctx.cac_attrsonly = op->ors_attrsonly;
1104 ctx.cac_userattrs = userattrs;
1105 ctx.cac_opattrs = opattrs;
1106 ctx.cac_acl_state = acl_state;
1107 ctx.cac_private = (void *)ber;
1110 * For each client requested attribute, call the plugins.
1112 if ( rs->sr_attrs != NULL ) {
1113 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1114 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1115 rs->sr_entry, slapi_x_compute_output_ber );
1122 * Technically we shouldn't be returning operational attributes
1123 * when the user requested only user attributes. We'll let the
1124 * plugin decide whether to be naughty or not.
1126 rc = compute_evaluator( &ctx, "*",
1127 rs->sr_entry, slapi_x_compute_output_ber );
1130 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1131 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1135 #endif /* LDAP_SLAPI */
1139 sl_free( e_flags, op->o_tmpmemctx );
1144 rc = ber_printf( ber, /*{{*/ "}N}" );
1146 if( rc != -1 && rs->sr_ctrls != NULL ) {
1147 rc = send_ldap_controls( ber, rs->sr_ctrls );
1151 #ifdef LDAP_CONNECTIONLESS
1152 if( op->o_conn && op->o_conn->c_is_udp ) {
1153 if ( op->o_protocol != LDAP_VERSION2 ) {
1154 rc = ber_printf( ber, /*{*/ "N}" );
1158 if ( op->o_res_ber == NULL ) {
1159 rc = ber_printf( ber, /*{*/ "N}" );
1165 LDAP_LOG( OPERATION, ERR,
1166 "send_search_entry: conn %lu ber_printf failed\n",
1167 op->o_connid, 0, 0 );
1169 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1172 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1173 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1174 sl_release( mark, op->o_tmpmemctx );
1178 if ( op->o_res_ber == NULL ) {
1179 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1180 ber_free_buf( ber );
1184 LDAP_LOG( OPERATION, ERR,
1185 "send_search_entry: conn %lu ber write failed.\n",
1186 op->o_connid, 0, 0 );
1188 Debug( LDAP_DEBUG_ANY,
1189 "send_search_entry: ber write failed\n",
1193 sl_release( mark, op->o_tmpmemctx );
1198 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1199 num_bytes_sent += bytes;
1202 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1205 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1206 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1209 LDAP_LOG( OPERATION, ENTRY,
1210 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1212 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1218 sl_release( mark, op->o_tmpmemctx );
1219 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1224 slap_send_search_reference( Operation *op, SlapReply *rs )
1226 BerElementBuffer berbuf;
1227 BerElement *ber = (BerElement *) &berbuf;
1232 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1233 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1235 rs->sr_type = REP_SEARCHREF;
1236 if (op->o_callback && op->o_callback->sc_response) {
1237 rc = op->o_callback->sc_response( op, rs );
1238 if ( rc != SLAP_CB_CONTINUE ) return rc;
1241 mark = sl_mark( op->o_tmpmemctx );
1244 LDAP_LOG( OPERATION, ENTRY,
1245 "send_search_reference: conn %lu dn=\"%s\"\n",
1247 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1249 Debug( LDAP_DEBUG_TRACE,
1250 "=> send_search_reference: dn=\"%s\"\n",
1251 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1254 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1255 ad_entry, NULL, ACL_READ, NULL ) )
1258 LDAP_LOG( ACL, INFO,
1259 "send_search_reference: conn %lu "
1260 "access to entry %s not allowed\n",
1261 op->o_connid, rs->sr_entry->e_dn, 0 );
1263 Debug( LDAP_DEBUG_ACL,
1264 "send_search_reference: access to entry not allowed\n",
1271 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1272 ad_ref, NULL, ACL_READ, NULL ) )
1275 LDAP_LOG( ACL, INFO,
1276 "send_search_reference: conn %lu access "
1277 "to reference not allowed.\n", op->o_connid, 0, 0 );
1279 Debug( LDAP_DEBUG_ACL,
1280 "send_search_reference: access "
1281 "to reference not allowed\n",
1288 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1289 if( op->o_domain_scope ) {
1291 LDAP_LOG( OPERATION, ERR,
1292 "send_search_reference: conn %lu domainScope control in (%s).\n",
1293 op->o_connid, rs->sr_entry->e_dn, 0 );
1295 Debug( LDAP_DEBUG_ANY,
1296 "send_search_reference: domainScope control in (%s)\n",
1297 rs->sr_entry->e_dn, 0, 0 );
1304 if( rs->sr_ref == NULL ) {
1306 LDAP_LOG( OPERATION, ERR,
1307 "send_search_reference: conn %lu null ref in (%s).\n",
1308 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1310 Debug( LDAP_DEBUG_ANY,
1311 "send_search_reference: null ref in (%s)\n",
1312 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1318 if( op->o_protocol < LDAP_VERSION3 ) {
1319 /* save the references for the result */
1320 if( rs->sr_ref[0].bv_val != NULL ) {
1321 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1328 #ifdef LDAP_CONNECTIONLESS
1329 if( op->o_conn && op->o_conn->c_is_udp ) {
1330 ber = op->o_res_ber;
1334 ber_init_w_nullc( ber, LBER_USE_DER );
1335 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1338 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1339 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1341 if( rc != -1 && rs->sr_ctrls != NULL ) {
1342 rc = send_ldap_controls( ber, rs->sr_ctrls );
1346 rc = ber_printf( ber, /*"{"*/ "N}" );
1351 LDAP_LOG( OPERATION, ERR,
1352 "send_search_reference: conn %lu "
1353 "ber_printf failed.\n", op->o_connid, 0, 0 );
1355 Debug( LDAP_DEBUG_ANY,
1356 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1359 #ifdef LDAP_CONNECTIONLESS
1360 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1362 ber_free_buf( ber );
1363 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1367 #ifdef LDAP_CONNECTIONLESS
1368 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1370 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1371 ber_free_buf( ber );
1373 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1374 num_bytes_sent += bytes;
1377 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1378 #ifdef LDAP_CONNECTIONLESS
1382 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1383 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1386 LDAP_LOG( OPERATION, ENTRY,
1387 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1389 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1393 sl_release( mark, op->o_tmpmemctx );
1408 *code = LDAP_SUCCESS;
1412 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1414 LDAP_LOG( OPERATION, INFO,
1415 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1417 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1425 while ( (s = strchr( s, '\n' )) != NULL ) {
1430 if ( (c = strchr( s, ':' )) != NULL ) {
1434 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1438 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1442 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1448 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1450 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1461 int slap_read_controls(
1465 const struct berval *oid,
1466 LDAPControl **ctrl )
1470 BerElementBuffer berbuf;
1471 BerElement *ber = (BerElement *) &berbuf;
1477 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1478 oid->bv_val, e->e_dn, 0 );
1480 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1481 oid->bv_val, e->e_dn, 0 );
1485 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1486 op->o_preread_attrs : op->o_postread_attrs;
1488 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1489 bv.bv_len = siz + len;
1490 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1492 ber_init2( ber, &bv, LBER_USE_DER );
1493 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1495 /* create new operation */
1498 myop.o_res_ber = ber;
1500 rc = slap_send_search_entry( &myop, rs );
1503 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1505 if( rc == LBER_ERROR ) return LDAP_OTHER;
1507 c.ldctl_oid = oid->bv_val;
1508 c.ldctl_iscritical = 0;
1510 *ctrl = sl_calloc( 1, sizeof(LDAPControl), NULL );
1512 return LDAP_SUCCESS;