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 ) goto cleanup;
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 );
411 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
412 free( rs->sr_matched );
413 rs->sr_matched = NULL;
421 send_ldap_disconnect( Operation *op, SlapReply *rs )
423 #define LDAP_UNSOLICITED_ERROR(e) \
424 ( (e) == LDAP_PROTOCOL_ERROR \
425 || (e) == LDAP_STRONG_AUTH_REQUIRED \
426 || (e) == LDAP_UNAVAILABLE )
428 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
430 rs->sr_type = REP_EXTENDED;
433 LDAP_LOG( OPERATION, ENTRY,
434 "send_ldap_disconnect: conn %lu %d:%s\n",
435 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
437 Debug( LDAP_DEBUG_TRACE,
438 "send_ldap_disconnect %d:%s\n",
439 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
443 if ( op->o_protocol < LDAP_VERSION3 ) {
444 rs->sr_rspoid = NULL;
445 rs->sr_tag = req2res( op->o_tag );
446 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
449 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
450 rs->sr_tag = LDAP_RES_EXTENDED;
454 send_ldap_response( op, rs );
456 Statslog( LDAP_DEBUG_STATS,
457 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
458 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
462 slap_send_ldap_result( Operation *op, SlapReply *rs )
465 const char *otext = rs->sr_text;
466 BerVarray oref = rs->sr_ref;
468 rs->sr_type = REP_RESULT;
470 assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
473 LDAP_LOG( OPERATION, ENTRY,
474 "send_ldap_result: conn %lu op=%lu p=%d\n",
475 op->o_connid, op->o_opid, op->o_protocol );
477 Debug( LDAP_DEBUG_TRACE,
478 "send_ldap_result: conn=%lu op=%lu p=%d\n",
479 op->o_connid, op->o_opid, op->o_protocol );
483 LDAP_LOG( OPERATION, ARGS,
484 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
485 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
486 rs->sr_text ? rs->sr_text : "" );
488 Debug( LDAP_DEBUG_ARGS,
489 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
490 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
491 rs->sr_text ? rs->sr_text : "" );
497 LDAP_LOG( OPERATION, ARGS,
498 "send_ldap_result: referral=\"%s\"\n",
499 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
501 Debug( LDAP_DEBUG_ARGS,
502 "send_ldap_result: referral=\"%s\"\n",
503 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
508 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
510 if ( rs->sr_err == LDAP_REFERRAL ) {
511 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
512 if( op->o_domain_scope ) {
516 if( rs->sr_ref == NULL ) {
517 rs->sr_err = LDAP_NO_SUCH_OBJECT;
518 } else if ( op->o_protocol < LDAP_VERSION3 ) {
519 rs->sr_err = LDAP_PARTIAL_RESULTS;
525 * Call pre-result plugins. To avoid infinite recursion plugins
526 * should just set SLAPI_RESULT_CODE rather than sending a
527 * result if they wish to change the result.
530 slapi_x_pblock_set_operation( op->o_pb, op );
531 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
532 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
533 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
535 (void) doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN, op->o_pb );
537 #endif /* LDAP_SLAPI */
539 if ( op->o_protocol < LDAP_VERSION3 ) {
540 tmp = v2ref( rs->sr_ref, rs->sr_text );
545 rs->sr_tag = req2res( op->o_tag );
546 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
548 send_ldap_response( op, rs );
550 if ( op->o_tag == LDAP_REQ_SEARCH ) {
552 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
553 rs->sr_err, rs->sr_nentries );
555 Statslog( LDAP_DEBUG_STATS,
556 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
557 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
558 rs->sr_text ? rs->sr_text : "" );
560 Statslog( LDAP_DEBUG_STATS,
561 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
562 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
563 rs->sr_text ? rs->sr_text : "" );
566 if( tmp != NULL ) ch_free(tmp);
572 send_ldap_sasl( Operation *op, SlapReply *rs )
574 rs->sr_type = REP_SASL;
576 LDAP_LOG( OPERATION, ENTRY,
577 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
578 op->o_connid, rs->sr_err,
579 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
581 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
583 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
586 rs->sr_tag = req2res( op->o_tag );
587 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
589 send_ldap_response( op, rs );
593 slap_send_ldap_extended( Operation *op, SlapReply *rs )
595 rs->sr_type = REP_EXTENDED;
598 LDAP_LOG( OPERATION, ENTRY,
599 "send_ldap_extended: err=%d oid=%s len=%ld\n",
600 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
601 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
603 Debug( LDAP_DEBUG_TRACE,
604 "send_ldap_extended: err=%d oid=%s len=%ld\n",
606 rs->sr_rspoid ? rs->sr_rspoid : "",
607 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
610 rs->sr_tag = req2res( op->o_tag );
611 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
613 send_ldap_response( op, rs );
617 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
619 rs->sr_type = REP_INTERMEDIATE;
621 LDAP_LOG( OPERATION, ENTRY,
622 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
623 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
624 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
626 Debug( LDAP_DEBUG_TRACE,
627 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
629 rs->sr_rspoid ? rs->sr_rspoid : "",
630 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
632 rs->sr_tag = LDAP_RES_INTERMEDIATE;
633 rs->sr_msgid = op->o_msgid;
634 send_ldap_response( op, rs );
638 slap_send_search_entry( Operation *op, SlapReply *rs )
640 BerElementBuffer berbuf;
641 BerElement *ber = (BerElement *) &berbuf;
643 int i, j, rc=-1, bytes;
647 AccessControlState acl_state = ACL_STATE_INIT;
649 /* Support for computed attribute plugins */
650 computed_attr_context ctx;
653 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
655 /* a_flags: array of flags telling if the i-th element will be
656 * returned or filtered out
657 * e_flags: array of a_flags
659 char **e_flags = NULL;
661 rs->sr_type = REP_SEARCH;
662 if (op->o_callback && op->o_callback->sc_response) {
663 rc = op->o_callback->sc_response( op, rs );
664 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
668 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
669 op->o_connid, rs->sr_entry->e_name.bv_val,
670 op->ors_attrsonly ? " (attrsOnly)" : "" );
672 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
673 rs->sr_entry->e_name.bv_val,
674 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
677 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
680 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
681 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
683 Debug( LDAP_DEBUG_ACL,
684 "send_search_entry: access to entry not allowed\n",
692 edn = rs->sr_entry->e_nname.bv_val;
694 if ( op->o_res_ber ) {
695 /* read back control or LDAP_CONNECTIONLESS */
701 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
702 bv.bv_len = siz + len;
703 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
705 ber_init2( ber, &bv, LBER_USE_DER );
706 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
709 #ifdef LDAP_CONNECTIONLESS
710 if ( op->o_conn && op->o_conn->c_is_udp ) {
712 if ( op->o_protocol == LDAP_VERSION2 ) {
713 rc = ber_printf(ber, "t{O{" /*}}*/,
714 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
716 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
717 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
721 if ( op->o_res_ber ) {
722 /* read back control */
723 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
725 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
726 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
731 LDAP_LOG( OPERATION, ERR,
732 "send_search_entry: conn %lu ber_printf failed\n",
733 op->o_connid, 0, 0 );
735 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
738 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
739 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
743 /* check for special all user attributes ("*") type */
744 userattrs = ( rs->sr_attrs == NULL ) ? 1
745 : an_find( rs->sr_attrs, &AllUser );
747 /* check for special all operational attributes ("+") type */
748 opattrs = ( rs->sr_attrs == NULL ) ? 0
749 : an_find( rs->sr_attrs, &AllOper );
751 /* create an array of arrays of flags. Each flag corresponds
752 * to particular value of attribute and equals 1 if value matches
753 * to ValuesReturnFilter or 0 if not
755 if ( op->o_vrFilter != NULL ) {
759 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
760 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
763 size = i * sizeof(char *) + k;
766 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
767 if( e_flags == NULL ) {
769 LDAP_LOG( OPERATION, ERR,
770 "send_search_entry: conn %lu sl_calloc failed\n",
771 op->o_connid ? op->o_connid : 0, 0, 0 );
773 Debug( LDAP_DEBUG_ANY,
774 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
778 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
781 a_flags = (char *)(e_flags + i);
782 memset( a_flags, 0, k );
783 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
784 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
785 e_flags[i] = a_flags;
789 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
792 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
793 "conn %lu matched values filtering failed\n",
794 op->o_connid ? op->o_connid : 0, 0, 0 );
796 Debug( LDAP_DEBUG_ANY,
797 "matched values filtering failed\n", 0, 0, 0 );
799 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
800 send_ldap_error( op, rs, LDAP_OTHER,
801 "matched values filtering error" );
807 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
808 AttributeDescription *desc = a->a_desc;
810 if ( rs->sr_attrs == NULL ) {
811 /* all attrs request, skip operational attributes */
812 if( is_at_operational( desc->ad_type ) ) {
817 /* specific attrs requested */
818 if ( is_at_operational( desc->ad_type ) ) {
819 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
824 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
830 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
831 ACL_READ, &acl_state ) )
835 "send_search_entry: conn %lu access to attribute %s not "
836 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
838 Debug( LDAP_DEBUG_ACL, "acl: "
839 "access to attribute %s not allowed\n",
840 desc->ad_cname.bv_val, 0, 0 );
845 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
847 LDAP_LOG( OPERATION, ERR,
848 "send_search_entry: conn %lu ber_printf failed\n",
849 op->o_connid, 0, 0 );
851 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
854 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
855 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
859 if ( ! op->ors_attrsonly ) {
860 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
861 if ( ! access_allowed( op, rs->sr_entry,
862 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
866 "send_search_entry: conn %lu "
867 "access to attribute %s, value %d not allowed\n",
868 op->o_connid, desc->ad_cname.bv_val, i );
870 Debug( LDAP_DEBUG_ACL,
871 "acl: access to attribute %s, "
872 "value %d not allowed\n",
873 desc->ad_cname.bv_val, i, 0 );
879 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
883 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
885 LDAP_LOG( OPERATION, ERR,
886 "send_search_entry: conn %lu "
887 "ber_printf failed.\n", op->o_connid, 0, 0 );
889 Debug( LDAP_DEBUG_ANY,
890 "ber_printf failed\n", 0, 0, 0 );
893 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
894 send_ldap_error( op, rs, LDAP_OTHER,
895 "encoding values error" );
901 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
903 LDAP_LOG( OPERATION, ERR,
904 "send_search_entry: conn %lu ber_printf failed\n",
905 op->o_connid, 0, 0 );
907 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
910 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
911 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
916 /* eventually will loop through generated operational attributes */
917 /* only have subschemaSubentry and numSubordinates are implemented */
918 aa = backend_operational( op, rs, opattrs );
920 if ( aa != NULL && op->o_vrFilter != NULL ) {
924 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
925 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
928 size = i * sizeof(char *) + k;
930 char *a_flags, **tmp;
933 * Reuse previous memory - we likely need less space
934 * for operational attributes
936 tmp = sl_realloc( e_flags, i * sizeof(char *) + k,
940 LDAP_LOG( OPERATION, ERR,
941 "send_search_entry: conn %lu "
943 "for matched values filtering\n",
946 Debug( LDAP_DEBUG_ANY,
947 "send_search_entry: conn %lu "
949 "for matched values filtering\n",
950 op->o_connid, 0, 0 );
952 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
953 send_ldap_error( op, rs, LDAP_OTHER,
954 "not enough memory for matched values filtering" );
958 a_flags = (char *)(e_flags + i);
959 memset( a_flags, 0, k );
960 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
961 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
962 e_flags[i] = a_flags;
965 rc = filter_matched_values(op, aa, &e_flags) ;
969 LDAP_LOG( OPERATION, ERR,
970 "send_search_entry: conn %lu "
971 "matched values filtering failed\n",
972 op->o_connid ? op->o_connid : 0, 0, 0);
974 Debug( LDAP_DEBUG_ANY,
975 "matched values filtering failed\n", 0, 0, 0 );
977 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
978 send_ldap_error( op, rs, LDAP_OTHER,
979 "matched values filtering error" );
985 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
986 AttributeDescription *desc = a->a_desc;
988 if ( rs->sr_attrs == NULL ) {
989 /* all attrs request, skip operational attributes */
990 if( is_at_operational( desc->ad_type ) ) {
995 /* specific attrs requested */
996 if( is_at_operational( desc->ad_type ) ) {
997 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1001 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1007 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1008 ACL_READ, &acl_state ) )
1011 LDAP_LOG( ACL, INFO,
1012 "send_search_entry: conn %lu "
1013 "access to attribute %s not allowed\n",
1014 op->o_connid, desc->ad_cname.bv_val, 0 );
1016 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
1017 "not allowed\n", desc->ad_cname.bv_val, 0, 0 );
1023 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1026 LDAP_LOG( OPERATION, ERR,
1027 "send_search_entry: conn %lu "
1028 "ber_printf failed\n", op->o_connid, 0, 0 );
1030 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1033 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1034 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
1039 if ( ! op->ors_attrsonly ) {
1040 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1041 if ( ! access_allowed( op, rs->sr_entry,
1042 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1045 LDAP_LOG( ACL, INFO,
1046 "send_search_entry: conn %lu "
1047 "access to %s, value %d not allowed\n",
1048 op->o_connid, desc->ad_cname.bv_val, i );
1050 Debug( LDAP_DEBUG_ACL,
1051 "send_search_entry: access to attribute %s, "
1052 "value %d not allowed\n",
1053 desc->ad_cname.bv_val, i, 0 );
1059 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1063 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1065 LDAP_LOG( OPERATION, ERR,
1066 "send_search_entry: conn %lu ber_printf failed\n",
1067 op->o_connid, 0, 0 );
1069 Debug( LDAP_DEBUG_ANY,
1070 "ber_printf failed\n", 0, 0, 0 );
1073 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1074 send_ldap_error( op, rs, LDAP_OTHER,
1075 "encoding values error" );
1082 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1084 LDAP_LOG( OPERATION, ERR,
1085 "send_search_entry: conn %lu ber_printf failed\n",
1086 op->o_connid, 0, 0 );
1088 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1091 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1092 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1100 * First, setup the computed attribute context that is
1101 * passed to all plugins.
1104 ctx.cac_pb = op->o_pb;
1105 ctx.cac_attrs = rs->sr_attrs;
1106 ctx.cac_attrsonly = op->ors_attrsonly;
1107 ctx.cac_userattrs = userattrs;
1108 ctx.cac_opattrs = opattrs;
1109 ctx.cac_acl_state = acl_state;
1110 ctx.cac_private = (void *)ber;
1113 * For each client requested attribute, call the plugins.
1115 if ( rs->sr_attrs != NULL ) {
1116 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1117 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1118 rs->sr_entry, slapi_x_compute_output_ber );
1125 * Technically we shouldn't be returning operational attributes
1126 * when the user requested only user attributes. We'll let the
1127 * plugin decide whether to be naughty or not.
1129 rc = compute_evaluator( &ctx, "*",
1130 rs->sr_entry, slapi_x_compute_output_ber );
1133 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1134 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1138 #endif /* LDAP_SLAPI */
1142 sl_free( e_flags, op->o_tmpmemctx );
1147 rc = ber_printf( ber, /*{{*/ "}N}" );
1149 if( rc != -1 && rs->sr_ctrls != NULL ) {
1150 rc = send_ldap_controls( ber, rs->sr_ctrls );
1154 #ifdef LDAP_CONNECTIONLESS
1155 if( op->o_conn && op->o_conn->c_is_udp ) {
1156 if ( op->o_protocol != LDAP_VERSION2 ) {
1157 rc = ber_printf( ber, /*{*/ "N}" );
1161 if ( op->o_res_ber == NULL ) {
1162 rc = ber_printf( ber, /*{*/ "N}" );
1168 LDAP_LOG( OPERATION, ERR,
1169 "send_search_entry: conn %lu ber_printf failed\n",
1170 op->o_connid, 0, 0 );
1172 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1175 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1176 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1181 if ( op->o_res_ber == NULL ) {
1182 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1183 ber_free_buf( ber );
1187 LDAP_LOG( OPERATION, ERR,
1188 "send_search_entry: conn %lu ber write failed.\n",
1189 op->o_connid, 0, 0 );
1191 Debug( LDAP_DEBUG_ANY,
1192 "send_search_entry: ber write failed\n",
1201 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1202 num_bytes_sent += bytes;
1205 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1208 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1209 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1212 LDAP_LOG( OPERATION, ENTRY,
1213 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1215 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1221 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1223 && (rs->sr_flags & REP_ENTRY_MUSTBEFREED) )
1225 entry_free( rs->sr_entry );
1226 rs->sr_entry = NULL;
1227 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1230 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1236 slap_send_search_reference( Operation *op, SlapReply *rs )
1238 BerElementBuffer berbuf;
1239 BerElement *ber = (BerElement *) &berbuf;
1243 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1244 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1246 rs->sr_type = REP_SEARCHREF;
1247 if (op->o_callback && op->o_callback->sc_response) {
1248 rc = op->o_callback->sc_response( op, rs );
1249 if ( rc != SLAP_CB_CONTINUE ) return rc;
1253 LDAP_LOG( OPERATION, ENTRY,
1254 "send_search_reference: conn %lu dn=\"%s\"\n",
1256 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1258 Debug( LDAP_DEBUG_TRACE,
1259 "=> send_search_reference: dn=\"%s\"\n",
1260 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1263 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1264 ad_entry, NULL, ACL_READ, NULL ) )
1267 LDAP_LOG( ACL, INFO,
1268 "send_search_reference: conn %lu "
1269 "access to entry %s not allowed\n",
1270 op->o_connid, rs->sr_entry->e_dn, 0 );
1272 Debug( LDAP_DEBUG_ACL,
1273 "send_search_reference: access to entry not allowed\n",
1279 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1280 ad_ref, NULL, ACL_READ, NULL ) )
1283 LDAP_LOG( ACL, INFO,
1284 "send_search_reference: conn %lu access "
1285 "to reference not allowed.\n", op->o_connid, 0, 0 );
1287 Debug( LDAP_DEBUG_ACL,
1288 "send_search_reference: access "
1289 "to reference not allowed\n",
1295 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1296 if( op->o_domain_scope ) {
1298 LDAP_LOG( OPERATION, ERR,
1299 "send_search_reference: conn %lu domainScope control in (%s).\n",
1300 op->o_connid, rs->sr_entry->e_dn, 0 );
1302 Debug( LDAP_DEBUG_ANY,
1303 "send_search_reference: domainScope control in (%s)\n",
1304 rs->sr_entry->e_dn, 0, 0 );
1310 if( rs->sr_ref == NULL ) {
1312 LDAP_LOG( OPERATION, ERR,
1313 "send_search_reference: conn %lu null ref in (%s).\n",
1314 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1316 Debug( LDAP_DEBUG_ANY,
1317 "send_search_reference: null ref in (%s)\n",
1318 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1323 if( op->o_protocol < LDAP_VERSION3 ) {
1324 /* save the references for the result */
1325 if( rs->sr_ref[0].bv_val != NULL ) {
1326 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1332 #ifdef LDAP_CONNECTIONLESS
1333 if( op->o_conn && op->o_conn->c_is_udp ) {
1334 ber = op->o_res_ber;
1338 ber_init_w_nullc( ber, LBER_USE_DER );
1339 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1342 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1343 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1345 if( rc != -1 && rs->sr_ctrls != NULL ) {
1346 rc = send_ldap_controls( ber, rs->sr_ctrls );
1350 rc = ber_printf( ber, /*"{"*/ "N}" );
1355 LDAP_LOG( OPERATION, ERR,
1356 "send_search_reference: conn %lu "
1357 "ber_printf failed.\n", op->o_connid, 0, 0 );
1359 Debug( LDAP_DEBUG_ANY,
1360 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1363 #ifdef LDAP_CONNECTIONLESS
1364 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1366 ber_free_buf( ber );
1367 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1371 #ifdef LDAP_CONNECTIONLESS
1372 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1374 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1375 ber_free_buf( ber );
1377 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1378 num_bytes_sent += bytes;
1381 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1382 #ifdef LDAP_CONNECTIONLESS
1386 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1387 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1390 LDAP_LOG( OPERATION, ENTRY,
1391 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1393 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1410 *code = LDAP_SUCCESS;
1414 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1416 LDAP_LOG( OPERATION, INFO,
1417 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1419 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1427 while ( (s = strchr( s, '\n' )) != NULL ) {
1432 if ( (c = strchr( s, ':' )) != NULL ) {
1436 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1440 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1444 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1450 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1452 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1463 int slap_read_controls(
1467 const struct berval *oid,
1468 LDAPControl **ctrl )
1472 BerElementBuffer berbuf;
1473 BerElement *ber = (BerElement *) &berbuf;
1479 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1480 oid->bv_val, e->e_dn, 0 );
1482 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1483 oid->bv_val, e->e_dn, 0 );
1487 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1488 op->o_preread_attrs : op->o_postread_attrs;
1490 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1491 bv.bv_len = siz + len;
1492 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1494 ber_init2( ber, &bv, LBER_USE_DER );
1495 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1497 /* create new operation */
1500 myop.o_res_ber = ber;
1502 rc = slap_send_search_entry( &myop, rs );
1505 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1507 if( rc == LBER_ERROR ) return LDAP_OTHER;
1509 c.ldctl_oid = oid->bv_val;
1510 c.ldctl_iscritical = 0;
1512 *ctrl = sl_calloc( 1, sizeof(LDAPControl), NULL );
1514 return LDAP_SUCCESS;