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-2004 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>
41 #include "slapi/slapi.h"
44 int slap_null_cb( Operation *op, SlapReply *rs )
49 int slap_replog_cb( Operation *op, SlapReply *rs )
51 if ( rs->sr_err == LDAP_SUCCESS ) {
54 return SLAP_CB_CONTINUE;
57 static char *v2ref( BerVarray ref, const char *text )
59 size_t len = 0, i = 0;
64 return ch_strdup(text);
72 if (text[len-1] != '\n') {
77 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
80 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
82 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
93 strcpy( v2+len, "Referral:" );
94 len += sizeof("Referral:");
96 for( i=0; ref[i].bv_val != NULL; i++ ) {
97 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
100 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
102 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
107 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
108 len += ref[i].bv_len;
109 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
118 static ber_tag_t req2res( ber_tag_t tag )
123 case LDAP_REQ_COMPARE:
124 case LDAP_REQ_EXTENDED:
125 case LDAP_REQ_MODIFY:
126 case LDAP_REQ_MODRDN:
130 case LDAP_REQ_DELETE:
131 tag = LDAP_RES_DELETE;
134 case LDAP_REQ_ABANDON:
135 case LDAP_REQ_UNBIND:
139 case LDAP_REQ_SEARCH:
140 tag = LDAP_RES_SEARCH_RESULT;
150 static long send_ldap_ber(
156 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
158 /* write only one pdu at a time - wait til it's our turn */
159 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
161 /* lock the connection */
162 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
169 if ( connection_state_closing( conn ) ) {
170 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
171 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
176 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
183 * we got an error. if it's ewouldblock, we need to
184 * wait on the socket being writable. otherwise, figure
185 * it's a hard error and return.
189 LDAP_LOG( OPERATION, ERR,
190 "send_ldap_ber: conn %lu ber_flush failed err=%d (%s)\n",
191 conn ? conn->c_connid : 0, err, sock_errstr(err) );
193 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
194 err, sock_errstr(err), 0 );
197 if ( err != EWOULDBLOCK && err != EAGAIN ) {
198 connection_closing( conn );
200 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
201 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
206 /* wait for socket to be write-ready */
207 conn->c_writewaiter = 1;
208 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
209 slapd_set_write( sd, 1 );
211 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
212 conn->c_writewaiter = 0;
215 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
216 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
222 send_ldap_control( BerElement *ber, LDAPControl *c )
228 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
230 if( c->ldctl_iscritical ) {
231 rc = ber_printf( ber, "b",
232 (ber_int_t) c->ldctl_iscritical ) ;
233 if( rc == -1 ) return rc;
236 if( c->ldctl_value.bv_val != NULL ) {
237 rc = ber_printf( ber, "O", &c->ldctl_value );
238 if( rc == -1 ) return rc;
241 rc = ber_printf( ber, /*{*/"N}" );
242 if( rc == -1 ) return rc;
248 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
252 LDAPControl **sctrls = NULL;
255 * Retrieve any additional controls that may be set by the
259 if ( o->o_pb && slapi_pblock_get( o->o_pb, SLAPI_RESCONTROLS, &sctrls ) != 0 ) {
263 if ( c == NULL && sctrls == NULL ) return 0;
265 if( c == NULL ) return 0;
266 #endif /* LDAP_SLAPI */
268 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
269 if( rc == -1 ) return rc;
273 #endif /* LDAP_SLAPI */
274 for( ; *c != NULL; c++) {
275 rc = send_ldap_control( ber, *c );
276 if( rc == -1 ) return rc;
280 if ( sctrls != NULL ) {
281 for ( c = sctrls; *c != NULL; c++ ) {
282 rc = send_ldap_control( ber, *c );
283 if( rc == -1 ) return rc;
286 #endif /* LDAP_SLAPI */
288 rc = ber_printf( ber, /*{*/"N}" );
298 BerElementBuffer berbuf;
299 BerElement *ber = (BerElement *) &berbuf;
300 int rc = LDAP_SUCCESS;
303 if (op->o_callback) {
304 slap_callback *sc = op->o_callback;
305 rc = SLAP_CB_CONTINUE;
306 for ( ; op->o_callback; ) {
307 if ( op->o_callback->sc_response ) {
308 rc = op->o_callback->sc_response( op, rs );
309 if ( rc != SLAP_CB_CONTINUE ) break;
311 op->o_callback = op->o_callback->sc_next;
314 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
317 #ifdef LDAP_CONNECTIONLESS
318 if (op->o_conn && op->o_conn->c_is_udp)
323 ber_init_w_nullc( ber, LBER_USE_DER );
324 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
328 LDAP_LOG( OPERATION, ENTRY,
329 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
330 rs->sr_msgid, rs->sr_tag, rs->sr_err );
332 Debug( LDAP_DEBUG_TRACE,
333 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
334 rs->sr_msgid, rs->sr_tag, rs->sr_err );
339 LDAP_LOG( OPERATION, ARGS,
340 "send_ldap_response: conn %lu ref=\"%s\"\n",
342 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
344 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
345 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
350 #ifdef LDAP_CONNECTIONLESS
351 if (op->o_conn && op->o_conn->c_is_udp &&
352 op->o_protocol == LDAP_VERSION2 )
354 rc = ber_printf( ber, "t{ess" /*"}}"*/,
355 rs->sr_tag, rs->sr_err,
356 rs->sr_matched == NULL ? "" : rs->sr_matched,
357 rs->sr_text == NULL ? "" : rs->sr_text );
360 if ( rs->sr_type == REP_INTERMEDIATE ) {
361 rc = ber_printf( ber, "{it{" /*"}}"*/,
362 rs->sr_msgid, rs->sr_tag );
365 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
366 rs->sr_msgid, rs->sr_tag, rs->sr_err,
367 rs->sr_matched == NULL ? "" : rs->sr_matched,
368 rs->sr_text == NULL ? "" : rs->sr_text );
372 if ( rs->sr_ref != NULL ) {
373 assert( rs->sr_err == LDAP_REFERRAL );
374 rc = ber_printf( ber, "t{W}",
375 LDAP_TAG_REFERRAL, rs->sr_ref );
377 assert( rs->sr_err != LDAP_REFERRAL );
381 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
382 rc = ber_printf( ber, "tO",
383 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
387 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
389 if ( rs->sr_rspoid != NULL ) {
390 rc = ber_printf( ber, "ts",
391 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
393 if( rc != -1 && rs->sr_rspdata != NULL ) {
394 rc = ber_printf( ber, "tO",
395 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
400 rc = ber_printf( ber, /*"{"*/ "N}" );
404 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
408 rc = ber_printf( ber, /*"{"*/ "N}" );
411 #ifdef LDAP_CONNECTIONLESS
412 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
413 rc = ber_printf( ber, /*"{"*/ "N}" );
419 LDAP_LOG( OPERATION, ERR,
420 "send_ldap_response: conn %lu ber_printf failed\n",
421 op->o_connid, 0, 0 );
423 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
426 #ifdef LDAP_CONNECTIONLESS
427 if (!op->o_conn || op->o_conn->c_is_udp == 0)
434 bytes = send_ldap_ber( op->o_conn, ber );
435 #ifdef LDAP_CONNECTIONLESS
436 if (!op->o_conn || op->o_conn->c_is_udp == 0)
442 LDAP_LOG( OPERATION, ERR,
443 "send_ldap_response: conn %lu ber write failed\n",
444 op->o_connid ? op->o_connid : 0, 0, 0 );
446 Debug( LDAP_DEBUG_ANY,
447 "send_ldap_response: ber write failed\n",
456 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
457 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
458 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
460 #endif /* LDAP_SLAPI */
462 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
463 num_bytes_sent += bytes;
465 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
468 /* Tell caller that we did this for real, as opposed to being
469 * overridden by a callback
471 rc = SLAP_CB_CONTINUE;
473 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
474 free( (char *)rs->sr_matched );
475 rs->sr_matched = NULL;
479 if (op->o_callback) {
480 slap_callback *sc = op->o_callback;
481 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
482 if ( op->o_callback->sc_cleanup ) {
483 op->o_callback->sc_cleanup( op, rs );
494 send_ldap_disconnect( Operation *op, SlapReply *rs )
496 #define LDAP_UNSOLICITED_ERROR(e) \
497 ( (e) == LDAP_PROTOCOL_ERROR \
498 || (e) == LDAP_STRONG_AUTH_REQUIRED \
499 || (e) == LDAP_UNAVAILABLE )
501 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
503 rs->sr_type = REP_EXTENDED;
506 LDAP_LOG( OPERATION, ENTRY,
507 "send_ldap_disconnect: conn %lu %d:%s\n",
508 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
510 Debug( LDAP_DEBUG_TRACE,
511 "send_ldap_disconnect %d:%s\n",
512 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
516 if ( op->o_protocol < LDAP_VERSION3 ) {
517 rs->sr_rspoid = NULL;
518 rs->sr_tag = req2res( op->o_tag );
519 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
522 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
523 rs->sr_tag = LDAP_RES_EXTENDED;
527 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
528 Statslog( LDAP_DEBUG_STATS,
529 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
530 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
535 slap_send_ldap_result( Operation *op, SlapReply *rs )
538 const char *otext = rs->sr_text;
539 BerVarray oref = rs->sr_ref;
541 rs->sr_type = REP_RESULT;
543 assert( !LDAP_API_ERROR( rs->sr_err ));
546 LDAP_LOG( OPERATION, ENTRY,
547 "send_ldap_result: conn %lu op=%lu p=%d\n",
548 op->o_connid, op->o_opid, op->o_protocol );
550 Debug( LDAP_DEBUG_TRACE,
551 "send_ldap_result: conn=%lu op=%lu p=%d\n",
552 op->o_connid, op->o_opid, op->o_protocol );
556 LDAP_LOG( OPERATION, ARGS,
557 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
558 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
559 rs->sr_text ? rs->sr_text : "" );
561 Debug( LDAP_DEBUG_ARGS,
562 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
563 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
564 rs->sr_text ? rs->sr_text : "" );
570 LDAP_LOG( OPERATION, ARGS,
571 "send_ldap_result: referral=\"%s\"\n",
572 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
574 Debug( LDAP_DEBUG_ARGS,
575 "send_ldap_result: referral=\"%s\"\n",
576 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
581 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
583 if ( rs->sr_err == LDAP_REFERRAL ) {
584 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
585 if( op->o_domain_scope ) {
589 if( rs->sr_ref == NULL ) {
590 rs->sr_err = LDAP_NO_SUCH_OBJECT;
591 } else if ( op->o_protocol < LDAP_VERSION3 ) {
592 rs->sr_err = LDAP_PARTIAL_RESULTS;
598 * Call pre-result plugins. To avoid infinite recursion plugins
599 * should just set SLAPI_RESULT_CODE rather than sending a
600 * result if they wish to change the result.
602 if ( op->o_pb != NULL ) {
603 slapi_int_pblock_set_operation( op->o_pb, op );
604 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
605 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
606 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
608 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN, op->o_pb );
610 #endif /* LDAP_SLAPI */
612 if ( op->o_protocol < LDAP_VERSION3 ) {
613 tmp = v2ref( rs->sr_ref, rs->sr_text );
618 rs->sr_tag = req2res( op->o_tag );
619 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
621 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
622 if ( op->o_tag == LDAP_REQ_SEARCH ) {
624 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
625 rs->sr_err, rs->sr_nentries );
627 Statslog( LDAP_DEBUG_STATS,
628 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
629 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
630 rs->sr_text ? rs->sr_text : "" );
632 Statslog( LDAP_DEBUG_STATS,
633 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
634 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
635 rs->sr_text ? rs->sr_text : "" );
639 if( tmp != NULL ) ch_free(tmp);
645 send_ldap_sasl( Operation *op, SlapReply *rs )
647 rs->sr_type = REP_SASL;
649 LDAP_LOG( OPERATION, ENTRY,
650 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
651 op->o_connid, rs->sr_err,
652 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
654 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
656 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
659 rs->sr_tag = req2res( op->o_tag );
660 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
662 send_ldap_response( op, rs );
666 slap_send_ldap_extended( Operation *op, SlapReply *rs )
668 rs->sr_type = REP_EXTENDED;
671 LDAP_LOG( OPERATION, ENTRY,
672 "send_ldap_extended: err=%d oid=%s len=%ld\n",
673 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
674 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
676 Debug( LDAP_DEBUG_TRACE,
677 "send_ldap_extended: err=%d oid=%s len=%ld\n",
679 rs->sr_rspoid ? rs->sr_rspoid : "",
680 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
683 rs->sr_tag = req2res( op->o_tag );
684 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
686 send_ldap_response( op, rs );
690 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
692 rs->sr_type = REP_INTERMEDIATE;
694 LDAP_LOG( OPERATION, ENTRY,
695 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
696 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
697 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
699 Debug( LDAP_DEBUG_TRACE,
700 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
702 rs->sr_rspoid ? rs->sr_rspoid : "",
703 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
705 rs->sr_tag = LDAP_RES_INTERMEDIATE;
706 rs->sr_msgid = op->o_msgid;
707 send_ldap_response( op, rs );
711 slap_send_search_entry( Operation *op, SlapReply *rs )
713 BerElementBuffer berbuf;
714 BerElement *ber = (BerElement *) &berbuf;
716 int i, j, rc=-1, bytes;
720 AccessControlState acl_state = ACL_STATE_INIT;
722 /* Support for computed attribute plugins */
723 computed_attr_context ctx;
726 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
728 /* a_flags: array of flags telling if the i-th element will be
729 * returned or filtered out
730 * e_flags: array of a_flags
732 char **e_flags = NULL;
734 rs->sr_type = REP_SEARCH;
735 if (op->o_callback) {
736 slap_callback *sc = op->o_callback;
737 rc = SLAP_CB_CONTINUE;
738 for ( ; op->o_callback; ) {
739 if ( op->o_callback->sc_response ) {
740 rc = op->o_callback->sc_response( op, rs );
741 if ( rc != SLAP_CB_CONTINUE ) break;
743 op->o_callback = op->o_callback->sc_next;
746 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
750 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
751 op->o_connid, rs->sr_entry->e_name.bv_val,
752 op->ors_attrsonly ? " (attrsOnly)" : "" );
754 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
755 rs->sr_entry->e_name.bv_val,
756 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
759 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
762 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
763 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
765 Debug( LDAP_DEBUG_ACL,
766 "send_search_entry: access to entry not allowed\n",
774 edn = rs->sr_entry->e_nname.bv_val;
776 if ( op->o_res_ber ) {
777 /* read back control or LDAP_CONNECTIONLESS */
783 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
784 bv.bv_len = siz + len;
785 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
787 ber_init2( ber, &bv, LBER_USE_DER );
788 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
791 #ifdef LDAP_CONNECTIONLESS
792 if ( op->o_conn && op->o_conn->c_is_udp ) {
794 if ( op->o_protocol == LDAP_VERSION2 ) {
795 rc = ber_printf(ber, "t{O{" /*}}*/,
796 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
798 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
799 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
803 if ( op->o_res_ber ) {
804 /* read back control */
805 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
807 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
808 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
813 LDAP_LOG( OPERATION, ERR,
814 "send_search_entry: conn %lu ber_printf failed\n",
815 op->o_connid, 0, 0 );
817 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
820 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
821 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
825 /* check for special all user attributes ("*") type */
826 userattrs = ( rs->sr_attrs == NULL ) ? 1
827 : an_find( rs->sr_attrs, &AllUser );
829 /* check for special all operational attributes ("+") type */
830 opattrs = ( rs->sr_attrs == NULL ) ? 0
831 : an_find( rs->sr_attrs, &AllOper );
833 /* create an array of arrays of flags. Each flag corresponds
834 * to particular value of attribute and equals 1 if value matches
835 * to ValuesReturnFilter or 0 if not
837 if ( op->o_vrFilter != NULL ) {
841 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
842 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
845 size = i * sizeof(char *) + k;
848 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
849 if( e_flags == NULL ) {
851 LDAP_LOG( OPERATION, ERR,
852 "send_search_entry: conn %lu sl_calloc failed\n",
853 op->o_connid ? op->o_connid : 0, 0, 0 );
855 Debug( LDAP_DEBUG_ANY,
856 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
860 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
863 a_flags = (char *)(e_flags + i);
864 memset( a_flags, 0, k );
865 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
866 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
867 e_flags[i] = a_flags;
871 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
874 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
875 "conn %lu matched values filtering failed\n",
876 op->o_connid ? op->o_connid : 0, 0, 0 );
878 Debug( LDAP_DEBUG_ANY,
879 "matched values filtering failed\n", 0, 0, 0 );
881 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
882 send_ldap_error( op, rs, LDAP_OTHER,
883 "matched values filtering error" );
889 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
890 AttributeDescription *desc = a->a_desc;
892 if ( rs->sr_attrs == NULL ) {
893 /* all attrs request, skip operational attributes */
894 if( is_at_operational( desc->ad_type ) ) {
899 /* specific attrs requested */
900 if ( is_at_operational( desc->ad_type ) ) {
901 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
906 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
912 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
913 ACL_READ, &acl_state ) )
917 "send_search_entry: conn %lu access to attribute %s not "
918 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
920 Debug( LDAP_DEBUG_ACL, "acl: "
921 "access to attribute %s not allowed\n",
922 desc->ad_cname.bv_val, 0, 0 );
927 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
929 LDAP_LOG( OPERATION, ERR,
930 "send_search_entry: conn %lu ber_printf failed\n",
931 op->o_connid, 0, 0 );
933 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
936 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
937 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
941 if ( ! op->ors_attrsonly ) {
942 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
943 if ( ! access_allowed( op, rs->sr_entry,
944 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
948 "send_search_entry: conn %lu "
949 "access to attribute %s, value %d not allowed\n",
950 op->o_connid, desc->ad_cname.bv_val, i );
952 Debug( LDAP_DEBUG_ACL,
953 "acl: access to attribute %s, "
954 "value %d not allowed\n",
955 desc->ad_cname.bv_val, i, 0 );
961 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
965 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
967 LDAP_LOG( OPERATION, ERR,
968 "send_search_entry: conn %lu "
969 "ber_printf failed.\n", op->o_connid, 0, 0 );
971 Debug( LDAP_DEBUG_ANY,
972 "ber_printf failed\n", 0, 0, 0 );
975 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
976 send_ldap_error( op, rs, LDAP_OTHER,
977 "encoding values error" );
983 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
985 LDAP_LOG( OPERATION, ERR,
986 "send_search_entry: conn %lu ber_printf failed\n",
987 op->o_connid, 0, 0 );
989 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
992 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
993 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
998 /* eventually will loop through generated operational attributes */
999 /* only have subschemaSubentry and numSubordinates are implemented */
1000 aa = backend_operational( op, rs, opattrs );
1002 if ( aa != NULL && op->o_vrFilter != NULL ) {
1006 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1007 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1010 size = i * sizeof(char *) + k;
1012 char *a_flags, **tmp;
1015 * Reuse previous memory - we likely need less space
1016 * for operational attributes
1018 tmp = sl_realloc( e_flags, i * sizeof(char *) + k,
1020 if ( tmp == NULL ) {
1022 LDAP_LOG( OPERATION, ERR,
1023 "send_search_entry: conn %lu "
1024 "not enough memory "
1025 "for matched values filtering\n",
1026 op->o_connid, 0, 0);
1028 Debug( LDAP_DEBUG_ANY,
1029 "send_search_entry: conn %lu "
1030 "not enough memory "
1031 "for matched values filtering\n",
1032 op->o_connid, 0, 0 );
1034 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1035 send_ldap_error( op, rs, LDAP_OTHER,
1036 "not enough memory for matched values filtering" );
1040 a_flags = (char *)(e_flags + i);
1041 memset( a_flags, 0, k );
1042 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1043 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1044 e_flags[i] = a_flags;
1047 rc = filter_matched_values(op, aa, &e_flags) ;
1051 LDAP_LOG( OPERATION, ERR,
1052 "send_search_entry: conn %lu "
1053 "matched values filtering failed\n",
1054 op->o_connid ? op->o_connid : 0, 0, 0);
1056 Debug( LDAP_DEBUG_ANY,
1057 "matched values filtering failed\n", 0, 0, 0 );
1059 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1060 send_ldap_error( op, rs, LDAP_OTHER,
1061 "matched values filtering error" );
1067 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
1068 AttributeDescription *desc = a->a_desc;
1070 if ( rs->sr_attrs == NULL ) {
1071 /* all attrs request, skip operational attributes */
1072 if( is_at_operational( desc->ad_type ) ) {
1077 /* specific attrs requested */
1078 if( is_at_operational( desc->ad_type ) ) {
1079 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1083 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1089 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1090 ACL_READ, &acl_state ) )
1093 LDAP_LOG( ACL, INFO,
1094 "send_search_entry: conn %lu "
1095 "access to attribute %s not allowed\n",
1096 op->o_connid, desc->ad_cname.bv_val, 0 );
1098 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
1099 "not allowed\n", desc->ad_cname.bv_val, 0, 0 );
1105 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1108 LDAP_LOG( OPERATION, ERR,
1109 "send_search_entry: conn %lu "
1110 "ber_printf failed\n", op->o_connid, 0, 0 );
1112 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1115 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1116 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
1121 if ( ! op->ors_attrsonly ) {
1122 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1123 if ( ! access_allowed( op, rs->sr_entry,
1124 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1127 LDAP_LOG( ACL, INFO,
1128 "send_search_entry: conn %lu "
1129 "access to %s, value %d not allowed\n",
1130 op->o_connid, desc->ad_cname.bv_val, i );
1132 Debug( LDAP_DEBUG_ACL,
1133 "send_search_entry: access to attribute %s, "
1134 "value %d not allowed\n",
1135 desc->ad_cname.bv_val, i, 0 );
1141 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1145 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1147 LDAP_LOG( OPERATION, ERR,
1148 "send_search_entry: conn %lu ber_printf failed\n",
1149 op->o_connid, 0, 0 );
1151 Debug( LDAP_DEBUG_ANY,
1152 "ber_printf failed\n", 0, 0, 0 );
1155 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1156 send_ldap_error( op, rs, LDAP_OTHER,
1157 "encoding values error" );
1164 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1166 LDAP_LOG( OPERATION, ERR,
1167 "send_search_entry: conn %lu ber_printf failed\n",
1168 op->o_connid, 0, 0 );
1170 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1173 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1174 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1182 * First, setup the computed attribute context that is
1183 * passed to all plugins.
1186 ctx.cac_pb = op->o_pb;
1187 ctx.cac_attrs = rs->sr_attrs;
1188 ctx.cac_attrsonly = op->ors_attrsonly;
1189 ctx.cac_userattrs = userattrs;
1190 ctx.cac_opattrs = opattrs;
1191 ctx.cac_acl_state = acl_state;
1192 ctx.cac_private = (void *)ber;
1195 * For each client requested attribute, call the plugins.
1197 if ( rs->sr_attrs != NULL ) {
1198 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1199 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1200 rs->sr_entry, slapi_int_compute_output_ber );
1207 * Technically we shouldn't be returning operational attributes
1208 * when the user requested only user attributes. We'll let the
1209 * plugin decide whether to be naughty or not.
1211 rc = compute_evaluator( &ctx, "*",
1212 rs->sr_entry, slapi_int_compute_output_ber );
1215 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1216 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1220 #endif /* LDAP_SLAPI */
1224 sl_free( e_flags, op->o_tmpmemctx );
1229 rc = ber_printf( ber, /*{{*/ "}N}" );
1232 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1236 #ifdef LDAP_CONNECTIONLESS
1237 if( op->o_conn && op->o_conn->c_is_udp ) {
1238 if ( op->o_protocol != LDAP_VERSION2 ) {
1239 rc = ber_printf( ber, /*{*/ "N}" );
1243 if ( op->o_res_ber == NULL ) {
1244 rc = ber_printf( ber, /*{*/ "N}" );
1250 LDAP_LOG( OPERATION, ERR,
1251 "send_search_entry: conn %lu ber_printf failed\n",
1252 op->o_connid, 0, 0 );
1254 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1257 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1258 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1263 if ( op->o_res_ber == NULL ) {
1264 bytes = send_ldap_ber( op->o_conn, ber );
1265 ber_free_buf( ber );
1269 LDAP_LOG( OPERATION, ERR,
1270 "send_search_entry: conn %lu ber write failed.\n",
1271 op->o_connid, 0, 0 );
1273 Debug( LDAP_DEBUG_ANY,
1274 "send_search_entry: ber write failed\n",
1283 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1284 num_bytes_sent += bytes;
1287 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1290 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1291 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1294 LDAP_LOG( OPERATION, ENTRY,
1295 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1297 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1303 /* FIXME: I think rs->sr_type should be explicitly set to
1304 * REP_SEARCH here. That's what it was when we entered this
1305 * function. send_ldap_error may have changed it, but we
1306 * should set it back so that the cleanup functions know
1307 * what they're doing.
1309 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1311 && (rs->sr_flags & REP_ENTRY_MUSTBEFREED) )
1313 entry_free( rs->sr_entry );
1314 rs->sr_entry = NULL;
1315 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1318 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1320 if (op->o_callback) {
1321 slap_callback *sc = op->o_callback;
1322 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
1323 if ( op->o_callback->sc_cleanup ) {
1324 op->o_callback->sc_cleanup( op, rs );
1327 op->o_callback = sc;
1333 slap_send_search_reference( Operation *op, SlapReply *rs )
1335 BerElementBuffer berbuf;
1336 BerElement *ber = (BerElement *) &berbuf;
1340 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1341 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1343 rs->sr_type = REP_SEARCHREF;
1344 if (op->o_callback) {
1345 slap_callback *sc = op->o_callback;
1346 rc = SLAP_CB_CONTINUE;
1347 for ( ; op->o_callback; ) {
1348 if ( op->o_callback->sc_response ) {
1349 rc = op->o_callback->sc_response( op, rs );
1350 if ( rc != SLAP_CB_CONTINUE ) break;
1352 op->o_callback = op->o_callback->sc_next;
1354 op->o_callback = sc;
1355 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1359 LDAP_LOG( OPERATION, ENTRY,
1360 "send_search_reference: conn %lu dn=\"%s\"\n",
1362 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1364 Debug( LDAP_DEBUG_TRACE,
1365 "=> send_search_reference: dn=\"%s\"\n",
1366 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1369 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1370 ad_entry, NULL, ACL_READ, NULL ) )
1373 LDAP_LOG( ACL, INFO,
1374 "send_search_reference: conn %lu "
1375 "access to entry %s not allowed\n",
1376 op->o_connid, rs->sr_entry->e_dn, 0 );
1378 Debug( LDAP_DEBUG_ACL,
1379 "send_search_reference: access to entry not allowed\n",
1386 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1387 ad_ref, NULL, ACL_READ, NULL ) )
1390 LDAP_LOG( ACL, INFO,
1391 "send_search_reference: conn %lu access "
1392 "to reference not allowed.\n", op->o_connid, 0, 0 );
1394 Debug( LDAP_DEBUG_ACL,
1395 "send_search_reference: access "
1396 "to reference not allowed\n",
1403 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1404 if( op->o_domain_scope ) {
1406 LDAP_LOG( OPERATION, ERR,
1407 "send_search_reference: conn %lu domainScope control in (%s).\n",
1408 op->o_connid, rs->sr_entry->e_dn, 0 );
1410 Debug( LDAP_DEBUG_ANY,
1411 "send_search_reference: domainScope control in (%s)\n",
1412 rs->sr_entry->e_dn, 0, 0 );
1419 if( rs->sr_ref == NULL ) {
1421 LDAP_LOG( OPERATION, ERR,
1422 "send_search_reference: conn %lu null ref in (%s).\n",
1423 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1425 Debug( LDAP_DEBUG_ANY,
1426 "send_search_reference: null ref in (%s)\n",
1427 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1433 if( op->o_protocol < LDAP_VERSION3 ) {
1435 /* save the references for the result */
1436 if( rs->sr_ref[0].bv_val != NULL ) {
1437 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1443 #ifdef LDAP_CONNECTIONLESS
1444 if( op->o_conn && op->o_conn->c_is_udp ) {
1445 ber = op->o_res_ber;
1449 ber_init_w_nullc( ber, LBER_USE_DER );
1450 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1453 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1454 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1457 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1461 rc = ber_printf( ber, /*"{"*/ "N}" );
1466 LDAP_LOG( OPERATION, ERR,
1467 "send_search_reference: conn %lu "
1468 "ber_printf failed.\n", op->o_connid, 0, 0 );
1470 Debug( LDAP_DEBUG_ANY,
1471 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1474 #ifdef LDAP_CONNECTIONLESS
1475 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1477 ber_free_buf( ber );
1478 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1482 #ifdef LDAP_CONNECTIONLESS
1483 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1485 bytes = send_ldap_ber( op->o_conn, ber );
1486 ber_free_buf( ber );
1488 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1489 num_bytes_sent += bytes;
1492 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1493 #ifdef LDAP_CONNECTIONLESS
1497 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1498 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1501 LDAP_LOG( OPERATION, ENTRY,
1502 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1504 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1508 if (op->o_callback) {
1509 slap_callback *sc = op->o_callback;
1510 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
1511 if ( op->o_callback->sc_cleanup ) {
1512 op->o_callback->sc_cleanup( op, rs );
1515 op->o_callback = sc;
1531 *code = LDAP_SUCCESS;
1535 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1537 LDAP_LOG( OPERATION, INFO,
1538 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1540 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1548 while ( (s = strchr( s, '\n' )) != NULL ) {
1553 if ( (c = strchr( s, ':' )) != NULL ) {
1557 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1561 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1565 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1571 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1573 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1584 int slap_read_controls(
1588 const struct berval *oid,
1589 LDAPControl **ctrl )
1593 BerElementBuffer berbuf;
1594 BerElement *ber = (BerElement *) &berbuf;
1600 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1601 oid->bv_val, e->e_dn, 0 );
1603 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1604 oid->bv_val, e->e_dn, 0 );
1608 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1609 op->o_preread_attrs : op->o_postread_attrs;
1611 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1612 bv.bv_len = siz + len;
1613 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1615 ber_init2( ber, &bv, LBER_USE_DER );
1616 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1618 /* create new operation */
1621 myop.o_res_ber = ber;
1623 rc = slap_send_search_entry( &myop, rs );
1626 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1628 if( rc == LBER_ERROR ) return LDAP_OTHER;
1630 c.ldctl_oid = oid->bv_val;
1631 c.ldctl_iscritical = 0;
1633 *ctrl = sl_calloc( 1, sizeof(LDAPControl), NULL );
1635 return LDAP_SUCCESS;