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-2005 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 const struct berval slap_dummy_bv = BER_BVNULL;
46 int slap_null_cb( Operation *op, SlapReply *rs )
51 int slap_freeself_cb( Operation *op, SlapReply *rs )
53 assert( op->o_callback );
55 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
56 op->o_callback = NULL;
58 return SLAP_CB_CONTINUE;
61 int slap_replog_cb( Operation *op, SlapReply *rs )
63 if ( rs->sr_err == LDAP_SUCCESS ) {
66 return SLAP_CB_CONTINUE;
69 static char *v2ref( BerVarray ref, const char *text )
71 size_t len = 0, i = 0;
76 return ch_strdup(text);
84 if (text[len-1] != '\n') {
89 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
91 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
101 strcpy( v2+len, "Referral:" );
102 len += sizeof("Referral:");
104 for( i=0; ref[i].bv_val != NULL; i++ ) {
105 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
107 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
111 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
112 len += ref[i].bv_len;
113 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
122 static ber_tag_t req2res( ber_tag_t tag )
127 case LDAP_REQ_COMPARE:
128 case LDAP_REQ_EXTENDED:
129 case LDAP_REQ_MODIFY:
130 case LDAP_REQ_MODRDN:
134 case LDAP_REQ_DELETE:
135 tag = LDAP_RES_DELETE;
138 case LDAP_REQ_ABANDON:
139 case LDAP_REQ_UNBIND:
143 case LDAP_REQ_SEARCH:
144 tag = LDAP_RES_SEARCH_RESULT;
154 static long send_ldap_ber(
160 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
162 /* write only one pdu at a time - wait til it's our turn */
163 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
165 /* lock the connection */
166 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
173 if ( connection_state_closing( conn ) ) {
174 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
175 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
180 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
187 * we got an error. if it's ewouldblock, we need to
188 * wait on the socket being writable. otherwise, figure
189 * it's a hard error and return.
192 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
193 err, sock_errstr(err), 0 );
195 if ( err != EWOULDBLOCK && err != EAGAIN ) {
196 connection_closing( conn );
198 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
199 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
204 /* wait for socket to be write-ready */
205 conn->c_writewaiter = 1;
206 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
207 slapd_set_write( sd, 1 );
209 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
210 conn->c_writewaiter = 0;
213 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
214 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
220 send_ldap_control( BerElement *ber, LDAPControl *c )
226 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
228 if( c->ldctl_iscritical ) {
229 rc = ber_printf( ber, "b",
230 (ber_int_t) c->ldctl_iscritical ) ;
231 if( rc == -1 ) return rc;
234 if( c->ldctl_value.bv_val != NULL ) {
235 rc = ber_printf( ber, "O", &c->ldctl_value );
236 if( rc == -1 ) return rc;
239 rc = ber_printf( ber, /*{*/"N}" );
240 if( rc == -1 ) return rc;
246 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
250 LDAPControl **sctrls = NULL;
253 * Retrieve any additional controls that may be set by the
257 if ( o->o_pb && slapi_pblock_get( o->o_pb, SLAPI_RESCONTROLS, &sctrls ) != 0 ) {
261 if ( c == NULL && sctrls == NULL ) return 0;
263 if( c == NULL ) return 0;
264 #endif /* LDAP_SLAPI */
266 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
267 if( rc == -1 ) return rc;
271 #endif /* LDAP_SLAPI */
272 for( ; *c != NULL; c++) {
273 rc = send_ldap_control( ber, *c );
274 if( rc == -1 ) return rc;
278 if ( sctrls != NULL ) {
279 for ( c = sctrls; *c != NULL; c++ ) {
280 rc = send_ldap_control( ber, *c );
281 if( rc == -1 ) return rc;
284 #endif /* LDAP_SLAPI */
286 rc = ber_printf( ber, /*{*/"N}" );
296 BerElementBuffer berbuf;
297 BerElement *ber = (BerElement *) &berbuf;
298 int rc = LDAP_SUCCESS;
301 if ( op->o_callback ) {
303 slap_callback *sc = op->o_callback,
304 *sc_next = op->o_callback;
306 rc = SLAP_CB_CONTINUE;
307 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
308 sc_next = op->o_callback->sc_next;
309 if ( op->o_callback->sc_response ) {
310 rc = op->o_callback->sc_response( op, rs );
311 if ( first && op->o_callback == NULL ) {
314 if ( rc != SLAP_CB_CONTINUE ) break;
320 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
323 #ifdef LDAP_CONNECTIONLESS
324 if (op->o_conn && op->o_conn->c_is_udp)
329 ber_init_w_nullc( ber, LBER_USE_DER );
330 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
333 Debug( LDAP_DEBUG_TRACE,
334 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
335 rs->sr_msgid, rs->sr_tag, rs->sr_err );
338 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
339 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
343 #ifdef LDAP_CONNECTIONLESS
344 if (op->o_conn && op->o_conn->c_is_udp &&
345 op->o_protocol == LDAP_VERSION2 )
347 rc = ber_printf( ber, "t{ess" /*"}"*/,
348 rs->sr_tag, rs->sr_err,
349 rs->sr_matched == NULL ? "" : rs->sr_matched,
350 rs->sr_text == NULL ? "" : rs->sr_text );
353 if ( rs->sr_type == REP_INTERMEDIATE ) {
354 rc = ber_printf( ber, "{it{" /*"}}"*/,
355 rs->sr_msgid, rs->sr_tag );
358 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
359 rs->sr_msgid, rs->sr_tag, rs->sr_err,
360 rs->sr_matched == NULL ? "" : rs->sr_matched,
361 rs->sr_text == NULL ? "" : rs->sr_text );
365 if ( rs->sr_ref != NULL ) {
366 assert( rs->sr_err == LDAP_REFERRAL );
367 rc = ber_printf( ber, "t{W}",
368 LDAP_TAG_REFERRAL, rs->sr_ref );
370 assert( rs->sr_err != LDAP_REFERRAL );
374 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
375 rc = ber_printf( ber, "tO",
376 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
380 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
382 if ( rs->sr_rspoid != NULL ) {
383 rc = ber_printf( ber, "ts",
384 rs->sr_type == REP_EXTENDED
385 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
388 if( rc != -1 && rs->sr_rspdata != NULL ) {
389 rc = ber_printf( ber, "tO",
390 rs->sr_type == REP_EXTENDED
391 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
397 rc = ber_printf( ber, /*"{"*/ "N}" );
401 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
405 rc = ber_printf( ber, /*"{"*/ "N}" );
408 #ifdef LDAP_CONNECTIONLESS
409 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
412 rc = ber_printf( ber, /*"{"*/ "N}" );
417 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
419 #ifdef LDAP_CONNECTIONLESS
420 if (!op->o_conn || op->o_conn->c_is_udp == 0)
429 bytes = send_ldap_ber( op->o_conn, ber );
430 #ifdef LDAP_CONNECTIONLESS
431 if (!op->o_conn || op->o_conn->c_is_udp == 0)
438 Debug( LDAP_DEBUG_ANY,
439 "send_ldap_response: ber write failed\n",
447 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
448 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
449 (void *)rs->sr_matched );
450 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
452 #endif /* LDAP_SLAPI */
454 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
455 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
456 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, bytes );
457 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
460 /* Tell caller that we did this for real, as opposed to being
461 * overridden by a callback
463 rc = SLAP_CB_CONTINUE;
466 if ( op->o_callback ) {
468 slap_callback *sc = op->o_callback,
469 *sc_next = op->o_callback;
471 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
472 sc_next = op->o_callback->sc_next;
473 if ( op->o_callback->sc_cleanup ) {
474 (void)op->o_callback->sc_cleanup( op, rs );
475 if ( first && op->o_callback == NULL ) {
486 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
487 free( (char *)rs->sr_matched );
488 rs->sr_matched = NULL;
491 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
492 ber_bvarray_free( rs->sr_ref );
501 send_ldap_disconnect( Operation *op, SlapReply *rs )
503 #define LDAP_UNSOLICITED_ERROR(e) \
504 ( (e) == LDAP_PROTOCOL_ERROR \
505 || (e) == LDAP_STRONG_AUTH_REQUIRED \
506 || (e) == LDAP_UNAVAILABLE )
508 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
510 rs->sr_type = REP_EXTENDED;
512 Debug( LDAP_DEBUG_TRACE,
513 "send_ldap_disconnect %d:%s\n",
514 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 "%s DISCONNECT tag=%lu err=%d text=%s\n",
530 op->o_log_prefix, rs->sr_tag, rs->sr_err,
531 rs->sr_text ? rs->sr_text : "", 0 );
536 slap_send_ldap_result( Operation *op, SlapReply *rs )
539 const char *otext = rs->sr_text;
540 BerVarray oref = rs->sr_ref;
542 rs->sr_type = REP_RESULT;
544 assert( !LDAP_API_ERROR( rs->sr_err ));
546 Debug( LDAP_DEBUG_TRACE,
547 "send_ldap_result: %s p=%d\n",
548 op->o_log_prefix, op->o_protocol, 0 );
550 Debug( LDAP_DEBUG_ARGS,
551 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
552 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
553 rs->sr_text ? rs->sr_text : "" );
557 Debug( LDAP_DEBUG_ARGS,
558 "send_ldap_result: referral=\"%s\"\n",
559 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
563 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
565 if ( rs->sr_err == LDAP_REFERRAL ) {
566 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
567 if( op->o_domain_scope ) {
571 if( rs->sr_ref == NULL ) {
572 rs->sr_err = LDAP_NO_SUCH_OBJECT;
573 } else if ( op->o_protocol < LDAP_VERSION3 ) {
574 rs->sr_err = LDAP_PARTIAL_RESULTS;
580 * Call pre-result plugins. To avoid infinite recursion plugins
581 * should just set SLAPI_RESULT_CODE rather than sending a
582 * result if they wish to change the result.
584 if ( op->o_pb != NULL ) {
585 slapi_int_pblock_set_operation( op->o_pb, op );
586 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
587 (void *)rs->sr_err );
588 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
589 (void *)rs->sr_text );
590 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
591 (void *)rs->sr_matched );
593 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
596 #endif /* LDAP_SLAPI */
598 if ( op->o_protocol < LDAP_VERSION3 ) {
599 tmp = v2ref( rs->sr_ref, rs->sr_text );
604 rs->sr_tag = req2res( op->o_tag );
605 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
607 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
608 if ( op->o_tag == LDAP_REQ_SEARCH ) {
610 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
611 rs->sr_err, rs->sr_nentries );
613 Statslog( LDAP_DEBUG_STATS,
614 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
615 op->o_log_prefix, rs->sr_tag, nbuf,
616 rs->sr_text ? rs->sr_text : "", 0 );
618 Statslog( LDAP_DEBUG_STATS,
619 "%s RESULT tag=%lu err=%d text=%s\n",
620 op->o_log_prefix, rs->sr_tag, rs->sr_err,
621 rs->sr_text ? rs->sr_text : "", 0 );
625 if( tmp != NULL ) ch_free(tmp);
631 send_ldap_sasl( Operation *op, SlapReply *rs )
633 rs->sr_type = REP_SASL;
634 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
636 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
638 rs->sr_tag = req2res( op->o_tag );
639 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
641 send_ldap_response( op, rs );
645 slap_send_ldap_extended( Operation *op, SlapReply *rs )
647 rs->sr_type = REP_EXTENDED;
649 Debug( LDAP_DEBUG_TRACE,
650 "send_ldap_extended: err=%d oid=%s len=%ld\n",
652 rs->sr_rspoid ? rs->sr_rspoid : "",
653 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
655 rs->sr_tag = req2res( op->o_tag );
656 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
658 send_ldap_response( op, rs );
662 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
664 rs->sr_type = REP_INTERMEDIATE;
665 Debug( LDAP_DEBUG_TRACE,
666 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
668 rs->sr_rspoid ? rs->sr_rspoid : "",
669 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
670 rs->sr_tag = LDAP_RES_INTERMEDIATE;
671 rs->sr_msgid = op->o_msgid;
672 send_ldap_response( op, rs );
676 slap_send_search_entry( Operation *op, SlapReply *rs )
678 BerElementBuffer berbuf;
679 BerElement *ber = (BerElement *) &berbuf;
681 int i, j, rc=-1, bytes;
684 AccessControlState acl_state = ACL_STATE_INIT;
686 /* Support for computed attribute plugins */
687 computed_attr_context ctx;
690 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
692 /* a_flags: array of flags telling if the i-th element will be
693 * returned or filtered out
694 * e_flags: array of a_flags
696 char **e_flags = NULL;
698 rs->sr_type = REP_SEARCH;
700 /* eventually will loop through generated operational attribute types
701 * currently implemented types include:
702 * entryDN, subschemaSubentry, and hasSubordinates */
703 /* NOTE: moved before overlays callback circling because
704 * they may modify entry and other stuff in rs */
705 /* check for special all operational attributes ("+") type */
706 /* FIXME: maybe we could se this flag at the operation level;
707 * however, in principle the caller of send_search_entry() may
708 * change the attribute list at each call */
709 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
711 rc = backend_operational( op, rs );
716 if ( op->o_callback ) {
718 slap_callback *sc = op->o_callback,
719 *sc_next = op->o_callback;
721 rc = SLAP_CB_CONTINUE;
722 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
723 sc_next = op->o_callback->sc_next;
724 if ( op->o_callback->sc_response ) {
725 rc = op->o_callback->sc_response( op, rs );
726 if ( first && op->o_callback == NULL ) {
729 if ( rc != SLAP_CB_CONTINUE ) break;
735 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
738 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
739 op->o_connid, rs->sr_entry->e_name.bv_val,
740 op->ors_attrsonly ? " (attrsOnly)" : "" );
742 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
743 Debug( LDAP_DEBUG_ACL,
744 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
745 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
751 edn = rs->sr_entry->e_nname.bv_val;
753 if ( op->o_res_ber ) {
754 /* read back control or LDAP_CONNECTIONLESS */
760 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
761 bv.bv_len = siz + len;
762 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
764 ber_init2( ber, &bv, LBER_USE_DER );
765 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
768 #ifdef LDAP_CONNECTIONLESS
769 if ( op->o_conn && op->o_conn->c_is_udp ) {
771 if ( op->o_protocol == LDAP_VERSION2 ) {
772 rc = ber_printf(ber, "t{O{" /*}}*/,
773 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
775 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
776 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
780 if ( op->o_res_ber ) {
781 /* read back control */
782 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
784 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
785 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
789 Debug( LDAP_DEBUG_ANY,
790 "send_search_entry: conn %lu ber_printf failed\n",
791 op->o_connid, 0, 0 );
793 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
794 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
798 /* check for special all user attributes ("*") type */
799 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
801 /* create an array of arrays of flags. Each flag corresponds
802 * to particular value of attribute and equals 1 if value matches
803 * to ValuesReturnFilter or 0 if not
805 if ( op->o_vrFilter != NULL ) {
809 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
810 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
813 size = i * sizeof(char *) + k;
816 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
817 if( e_flags == NULL ) {
818 Debug( LDAP_DEBUG_ANY,
819 "send_search_entry: conn %lu slap_sl_calloc failed\n",
820 op->o_connid ? op->o_connid : 0, 0, 0 );
823 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
826 a_flags = (char *)(e_flags + i);
827 memset( a_flags, 0, k );
828 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
829 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
830 e_flags[i] = a_flags;
834 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
836 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
837 "conn %lu matched values filtering failed\n",
838 op->o_connid ? op->o_connid : 0, 0, 0 );
839 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
840 send_ldap_error( op, rs, LDAP_OTHER,
841 "matched values filtering error" );
847 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
848 AttributeDescription *desc = a->a_desc;
851 if ( rs->sr_attrs == NULL ) {
852 /* all attrs request, skip operational attributes */
853 if( is_at_operational( desc->ad_type ) ) {
858 /* specific attrs requested */
859 if ( is_at_operational( desc->ad_type ) ) {
860 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
861 !ad_inlist( desc, rs->sr_attrs ) )
867 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
874 if ( op->ors_attrsonly ) {
875 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
876 ACL_READ, &acl_state ) )
878 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
879 "conn %lu access to attribute %s not allowed\n",
880 op->o_connid, desc->ad_cname.bv_val, 0 );
884 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
885 Debug( LDAP_DEBUG_ANY,
886 "send_search_entry: conn %lu ber_printf failed\n",
887 op->o_connid, 0, 0 );
889 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
890 send_ldap_error( op, rs, LDAP_OTHER,
891 "encoding description error");
898 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
899 if ( ! access_allowed( op, rs->sr_entry,
900 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
902 Debug( LDAP_DEBUG_ACL,
903 "send_search_entry: conn %lu "
904 "access to attribute %s, value #%d not allowed\n",
905 op->o_connid, desc->ad_cname.bv_val, i );
910 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
917 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
918 Debug( LDAP_DEBUG_ANY,
919 "send_search_entry: conn %lu ber_printf failed\n",
920 op->o_connid, 0, 0 );
922 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
923 send_ldap_error( op, rs, LDAP_OTHER,
924 "encoding description error");
928 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
929 Debug( LDAP_DEBUG_ANY,
930 "send_search_entry: conn %lu "
931 "ber_printf failed.\n", op->o_connid, 0, 0 );
933 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
934 send_ldap_error( op, rs, LDAP_OTHER,
935 "encoding values error" );
941 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
942 Debug( LDAP_DEBUG_ANY,
943 "send_search_entry: conn %lu ber_printf failed\n",
944 op->o_connid, 0, 0 );
946 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
947 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
952 /* NOTE: moved before overlays callback circling because
953 * they may modify entry and other stuff in rs */
954 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
958 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
959 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
962 size = i * sizeof(char *) + k;
964 char *a_flags, **tmp;
967 * Reuse previous memory - we likely need less space
968 * for operational attributes
970 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
973 Debug( LDAP_DEBUG_ANY,
974 "send_search_entry: conn %lu "
976 "for matched values filtering\n",
977 op->o_connid, 0, 0 );
978 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
979 send_ldap_error( op, rs, LDAP_OTHER,
980 "not enough memory for matched values filtering" );
984 a_flags = (char *)(e_flags + i);
985 memset( a_flags, 0, k );
986 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
987 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
988 e_flags[i] = a_flags;
991 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
994 Debug( LDAP_DEBUG_ANY,
995 "send_search_entry: conn %lu "
996 "matched values filtering failed\n",
997 op->o_connid ? op->o_connid : 0, 0, 0);
998 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
999 send_ldap_error( op, rs, LDAP_OTHER,
1000 "matched values filtering error" );
1006 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1007 AttributeDescription *desc = a->a_desc;
1009 if ( rs->sr_attrs == NULL ) {
1010 /* all attrs request, skip operational attributes */
1011 if( is_at_operational( desc->ad_type ) ) {
1016 /* specific attrs requested */
1017 if( is_at_operational( desc->ad_type ) ) {
1018 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1019 !ad_inlist( desc, rs->sr_attrs ) )
1024 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1030 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1031 ACL_READ, &acl_state ) )
1033 Debug( LDAP_DEBUG_ACL,
1034 "send_search_entry: conn %lu "
1035 "access to attribute %s not allowed\n",
1036 op->o_connid, desc->ad_cname.bv_val, 0 );
1041 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1043 Debug( LDAP_DEBUG_ANY,
1044 "send_search_entry: conn %lu "
1045 "ber_printf failed\n", op->o_connid, 0, 0 );
1047 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1048 send_ldap_error( op, rs, LDAP_OTHER,
1049 "encoding description error" );
1053 if ( ! op->ors_attrsonly ) {
1054 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1055 if ( ! access_allowed( op, rs->sr_entry,
1056 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1058 Debug( LDAP_DEBUG_ACL,
1059 "send_search_entry: conn %lu "
1060 "access to %s, value %d not allowed\n",
1061 op->o_connid, desc->ad_cname.bv_val, i );
1066 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1070 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1071 Debug( LDAP_DEBUG_ANY,
1072 "send_search_entry: conn %lu ber_printf failed\n",
1073 op->o_connid, 0, 0 );
1075 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1076 send_ldap_error( op, rs, LDAP_OTHER,
1077 "encoding values error" );
1083 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1084 Debug( LDAP_DEBUG_ANY,
1085 "send_search_entry: conn %lu ber_printf failed\n",
1086 op->o_connid, 0, 0 );
1088 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1089 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1096 * First, setup the computed attribute context that is
1097 * passed to all plugins.
1100 ctx.cac_pb = op->o_pb;
1101 ctx.cac_attrs = rs->sr_attrs;
1102 ctx.cac_attrsonly = op->ors_attrsonly;
1103 ctx.cac_userattrs = userattrs;
1104 ctx.cac_opattrs = rs->sr_attr_flags;
1105 ctx.cac_acl_state = acl_state;
1106 ctx.cac_private = (void *)ber;
1109 * For each client requested attribute, call the plugins.
1111 if ( rs->sr_attrs != NULL ) {
1112 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1113 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1114 rs->sr_entry, slapi_int_compute_output_ber );
1115 if ( rc == 1 ) break;
1119 * Technically we shouldn't be returning operational attributes
1120 * when the user requested only user attributes. We'll let the
1121 * plugin decide whether to be naughty or not.
1123 rc = compute_evaluator( &ctx, "*",
1124 rs->sr_entry, slapi_int_compute_output_ber );
1127 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1128 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1132 #endif /* LDAP_SLAPI */
1136 slap_sl_free( e_flags, op->o_tmpmemctx );
1140 rc = ber_printf( ber, /*{{*/ "}N}" );
1143 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1147 #ifdef LDAP_CONNECTIONLESS
1148 if( op->o_conn && op->o_conn->c_is_udp ) {
1149 if ( op->o_protocol != LDAP_VERSION2 ) {
1150 rc = ber_printf( ber, /*{*/ "N}" );
1154 if ( op->o_res_ber == NULL ) {
1155 rc = ber_printf( ber, /*{*/ "N}" );
1160 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1162 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1163 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1168 if ( op->o_res_ber == NULL ) {
1169 bytes = send_ldap_ber( op->o_conn, ber );
1170 ber_free_buf( ber );
1173 Debug( LDAP_DEBUG_ANY,
1174 "send_search_entry: conn %lu ber write failed.\n",
1175 op->o_connid, 0, 0 );
1182 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1183 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, bytes );
1184 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1185 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1186 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1189 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1190 op->o_log_prefix, rs->sr_entry->e_dn, 0, 0, 0 );
1192 Debug( LDAP_DEBUG_TRACE,
1193 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1198 if ( op->o_callback ) {
1200 slap_callback *sc = op->o_callback,
1201 *sc_next = op->o_callback;
1203 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1204 sc_next = op->o_callback->sc_next;
1205 if ( op->o_callback->sc_cleanup ) {
1206 (void)op->o_callback->sc_cleanup( op, rs );
1207 if ( first && op->o_callback == NULL ) {
1214 op->o_callback = sc;
1218 slap_sl_free( e_flags, op->o_tmpmemctx );
1221 if ( rs->sr_operational_attrs ) {
1222 attrs_free( rs->sr_operational_attrs );
1223 rs->sr_operational_attrs = NULL;
1225 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1227 /* FIXME: I think rs->sr_type should be explicitly set to
1228 * REP_SEARCH here. That's what it was when we entered this
1229 * function. send_ldap_error may have changed it, but we
1230 * should set it back so that the cleanup functions know
1231 * what they're doing.
1233 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1235 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1237 entry_free( rs->sr_entry );
1238 rs->sr_entry = NULL;
1239 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1246 slap_send_search_reference( Operation *op, SlapReply *rs )
1248 BerElementBuffer berbuf;
1249 BerElement *ber = (BerElement *) &berbuf;
1253 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1254 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1256 rs->sr_type = REP_SEARCHREF;
1257 if ( op->o_callback ) {
1259 slap_callback *sc = op->o_callback,
1260 *sc_next = op->o_callback;
1262 rc = SLAP_CB_CONTINUE;
1263 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1264 sc_next = op->o_callback->sc_next;
1265 if ( op->o_callback->sc_response ) {
1266 rc = op->o_callback->sc_response( op, rs );
1267 if ( first && op->o_callback == NULL ) {
1270 if ( rc != SLAP_CB_CONTINUE ) break;
1275 op->o_callback = sc;
1276 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1279 Debug( LDAP_DEBUG_TRACE,
1280 "=> send_search_reference: dn=\"%s\"\n",
1281 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1283 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1284 ad_entry, NULL, ACL_READ, NULL ) )
1286 Debug( LDAP_DEBUG_ACL,
1287 "send_search_reference: access to entry not allowed\n",
1293 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1294 ad_ref, NULL, ACL_READ, NULL ) )
1296 Debug( LDAP_DEBUG_ACL,
1297 "send_search_reference: access "
1298 "to reference not allowed\n",
1304 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1305 if( op->o_domain_scope ) {
1306 Debug( LDAP_DEBUG_ANY,
1307 "send_search_reference: domainScope control in (%s)\n",
1308 rs->sr_entry->e_dn, 0, 0 );
1314 if( rs->sr_ref == NULL ) {
1315 Debug( LDAP_DEBUG_ANY,
1316 "send_search_reference: null ref in (%s)\n",
1317 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1322 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 );
1346 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1350 rc = ber_printf( ber, /*"{"*/ "N}" );
1354 Debug( LDAP_DEBUG_ANY,
1355 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1357 #ifdef LDAP_CONNECTIONLESS
1358 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1360 ber_free_buf( ber );
1361 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1365 #ifdef LDAP_CONNECTIONLESS
1366 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1368 bytes = send_ldap_ber( op->o_conn, ber );
1369 ber_free_buf( ber );
1371 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1372 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, bytes );
1373 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1374 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1375 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1376 #ifdef LDAP_CONNECTIONLESS
1380 Statslog( LDAP_DEBUG_STATS2, "%s REF dn=\"%s\"\n",
1381 op->o_log_prefix, rs->sr_entry ? rs->sr_entry->e_dn : "(null)",
1384 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1387 if ( op->o_callback ) {
1389 slap_callback *sc = op->o_callback,
1390 *sc_next = op->o_callback;
1392 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1393 sc_next = op->o_callback->sc_next;
1394 if ( op->o_callback->sc_cleanup ) {
1395 (void)op->o_callback->sc_cleanup( op, rs );
1396 if ( first && op->o_callback == NULL ) {
1403 op->o_callback = sc;
1420 *code = LDAP_SUCCESS;
1424 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1425 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1432 while ( (s = strchr( s, '\n' )) != NULL ) {
1437 if ( (c = strchr( s, ':' )) != NULL ) {
1441 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1445 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1449 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1454 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1464 int slap_read_controls(
1468 const struct berval *oid,
1469 LDAPControl **ctrl )
1473 BerElementBuffer berbuf;
1474 BerElement *ber = (BerElement *) &berbuf;
1479 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1480 oid->bv_val, e->e_dn, 0 );
1483 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1484 op->o_preread_attrs : op->o_postread_attrs;
1486 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1487 bv.bv_len = siz + len;
1488 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1490 ber_init2( ber, &bv, LBER_USE_DER );
1491 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1493 /* create new operation */
1496 myop.o_res_ber = ber;
1498 rc = slap_send_search_entry( &myop, rs );
1501 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1503 if( rc == LBER_ERROR ) return LDAP_OTHER;
1505 c.ldctl_oid = oid->bv_val;
1506 c.ldctl_iscritical = 0;
1508 if ( ctrl == NULL ) {
1510 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1512 /* retry: free previous try */
1513 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1517 return LDAP_SUCCESS;
1520 /* Map API errors to protocol errors... */
1522 slap_map_api2result( SlapReply *rs )
1524 switch(rs->sr_err) {
1525 case LDAP_SERVER_DOWN:
1526 return LDAP_UNAVAILABLE;
1527 case LDAP_LOCAL_ERROR:
1529 case LDAP_ENCODING_ERROR:
1530 case LDAP_DECODING_ERROR:
1531 return LDAP_PROTOCOL_ERROR;
1533 return LDAP_UNAVAILABLE;
1534 case LDAP_AUTH_UNKNOWN:
1535 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1536 case LDAP_FILTER_ERROR:
1537 rs->sr_text = "Filter error";
1539 case LDAP_USER_CANCELLED:
1540 rs->sr_text = "User cancelled";
1542 case LDAP_PARAM_ERROR:
1543 return LDAP_PROTOCOL_ERROR;
1544 case LDAP_NO_MEMORY:
1546 case LDAP_CONNECT_ERROR:
1547 return LDAP_UNAVAILABLE;
1548 case LDAP_NOT_SUPPORTED:
1549 return LDAP_UNWILLING_TO_PERFORM;
1550 case LDAP_CONTROL_NOT_FOUND:
1551 return LDAP_PROTOCOL_ERROR;
1552 case LDAP_NO_RESULTS_RETURNED:
1553 return LDAP_NO_SUCH_OBJECT;
1554 case LDAP_MORE_RESULTS_TO_RETURN:
1555 rs->sr_text = "More results to return";
1557 case LDAP_CLIENT_LOOP:
1558 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1559 return LDAP_LOOP_DETECT;
1561 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1568 slap_attr_flags( AttributeName *an )
1570 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1573 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1576 flags |= an_find( an, &AllOper ) ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1577 flags |= an_find( an, &AllUser ) ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;