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
258 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 */
275 for( ; *c != NULL; c++) {
276 rc = send_ldap_control( ber, *c );
277 if( rc == -1 ) return rc;
282 /* this is a hack to avoid having to modify op->s_ctrls */
283 if( o->o_sortedresults ) {
284 BerElementBuffer berbuf;
285 BerElement *sber = (BerElement *) &berbuf;
287 BER_BVZERO( &sorted.ldctl_value );
288 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
289 sorted.ldctl_iscritical = 0;
291 ber_init2( sber, NULL, LBER_USE_DER );
293 ber_printf( sber, "{i}", LDAP_UNWILLING_TO_PERFORM );
295 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
299 (void) ber_free_buf( ber );
301 rc = send_ldap_control( ber, &sorted );
302 if( rc == -1 ) return rc;
307 if ( sctrls != NULL ) {
308 for ( c = sctrls; *c != NULL; c++ ) {
309 rc = send_ldap_control( ber, *c );
310 if( rc == -1 ) return rc;
313 #endif /* LDAP_SLAPI */
315 rc = ber_printf( ber, /*{*/"N}" );
325 BerElementBuffer berbuf;
326 BerElement *ber = (BerElement *) &berbuf;
327 int rc = LDAP_SUCCESS;
330 if ( op->o_callback ) {
332 slap_callback *sc = op->o_callback,
333 *sc_next = op->o_callback;
335 rc = SLAP_CB_CONTINUE;
336 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
337 sc_next = op->o_callback->sc_next;
338 if ( op->o_callback->sc_response ) {
339 rc = op->o_callback->sc_response( op, rs );
340 if ( first && op->o_callback == NULL ) {
343 if ( rc != SLAP_CB_CONTINUE ) break;
349 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
352 #ifdef LDAP_CONNECTIONLESS
353 if (op->o_conn && op->o_conn->c_is_udp)
358 ber_init_w_nullc( ber, LBER_USE_DER );
359 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
362 Debug( LDAP_DEBUG_TRACE,
363 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
364 rs->sr_msgid, rs->sr_tag, rs->sr_err );
367 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
368 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
372 #ifdef LDAP_CONNECTIONLESS
373 if (op->o_conn && op->o_conn->c_is_udp &&
374 op->o_protocol == LDAP_VERSION2 )
376 rc = ber_printf( ber, "t{ess" /*"}"*/,
377 rs->sr_tag, rs->sr_err,
378 rs->sr_matched == NULL ? "" : rs->sr_matched,
379 rs->sr_text == NULL ? "" : rs->sr_text );
382 if ( rs->sr_type == REP_INTERMEDIATE ) {
383 rc = ber_printf( ber, "{it{" /*"}}"*/,
384 rs->sr_msgid, rs->sr_tag );
387 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
388 rs->sr_msgid, rs->sr_tag, rs->sr_err,
389 rs->sr_matched == NULL ? "" : rs->sr_matched,
390 rs->sr_text == NULL ? "" : rs->sr_text );
394 if ( rs->sr_ref != NULL ) {
395 assert( rs->sr_err == LDAP_REFERRAL );
396 rc = ber_printf( ber, "t{W}",
397 LDAP_TAG_REFERRAL, rs->sr_ref );
399 assert( rs->sr_err != LDAP_REFERRAL );
403 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
404 rc = ber_printf( ber, "tO",
405 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
409 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
411 if ( rs->sr_rspoid != NULL ) {
412 rc = ber_printf( ber, "ts",
413 rs->sr_type == REP_EXTENDED
414 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
417 if( rc != -1 && rs->sr_rspdata != NULL ) {
418 rc = ber_printf( ber, "tO",
419 rs->sr_type == REP_EXTENDED
420 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
426 rc = ber_printf( ber, /*"{"*/ "N}" );
430 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
434 rc = ber_printf( ber, /*"{"*/ "N}" );
437 #ifdef LDAP_CONNECTIONLESS
438 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
441 rc = ber_printf( ber, /*"{"*/ "N}" );
446 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
448 #ifdef LDAP_CONNECTIONLESS
449 if (!op->o_conn || op->o_conn->c_is_udp == 0)
458 bytes = send_ldap_ber( op->o_conn, ber );
459 #ifdef LDAP_CONNECTIONLESS
460 if (!op->o_conn || op->o_conn->c_is_udp == 0)
467 Debug( LDAP_DEBUG_ANY,
468 "send_ldap_response: ber write failed\n",
476 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
477 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
478 (void *)rs->sr_matched );
479 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
481 #endif /* LDAP_SLAPI */
483 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
484 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
485 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
486 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
489 /* Tell caller that we did this for real, as opposed to being
490 * overridden by a callback
492 rc = SLAP_CB_CONTINUE;
495 if ( op->o_callback ) {
497 slap_callback *sc = op->o_callback, *sc_next;
499 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
500 sc_next = op->o_callback->sc_next;
501 if ( op->o_callback->sc_cleanup ) {
502 (void)op->o_callback->sc_cleanup( op, rs );
503 if ( first && op->o_callback != sc ) {
513 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
514 free( (char *)rs->sr_matched );
515 rs->sr_matched = NULL;
518 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
519 ber_bvarray_free( rs->sr_ref );
528 send_ldap_disconnect( Operation *op, SlapReply *rs )
530 #define LDAP_UNSOLICITED_ERROR(e) \
531 ( (e) == LDAP_PROTOCOL_ERROR \
532 || (e) == LDAP_STRONG_AUTH_REQUIRED \
533 || (e) == LDAP_UNAVAILABLE )
535 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
537 rs->sr_type = REP_EXTENDED;
539 Debug( LDAP_DEBUG_TRACE,
540 "send_ldap_disconnect %d:%s\n",
541 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
543 if ( op->o_protocol < LDAP_VERSION3 ) {
544 rs->sr_rspoid = NULL;
545 rs->sr_tag = req2res( op->o_tag );
546 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
549 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
550 rs->sr_tag = LDAP_RES_EXTENDED;
554 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
555 Statslog( LDAP_DEBUG_STATS,
556 "%s DISCONNECT tag=%lu err=%d text=%s\n",
557 op->o_log_prefix, rs->sr_tag, rs->sr_err,
558 rs->sr_text ? rs->sr_text : "", 0 );
563 slap_send_ldap_result( Operation *op, SlapReply *rs )
566 const char *otext = rs->sr_text;
567 BerVarray oref = rs->sr_ref;
569 rs->sr_type = REP_RESULT;
571 assert( !LDAP_API_ERROR( rs->sr_err ));
573 Debug( LDAP_DEBUG_TRACE,
574 "send_ldap_result: %s p=%d\n",
575 op->o_log_prefix, op->o_protocol, 0 );
577 Debug( LDAP_DEBUG_ARGS,
578 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
579 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
580 rs->sr_text ? rs->sr_text : "" );
584 Debug( LDAP_DEBUG_ARGS,
585 "send_ldap_result: referral=\"%s\"\n",
586 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
590 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
592 if ( rs->sr_err == LDAP_REFERRAL ) {
593 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
594 if( op->o_domain_scope ) {
598 if( rs->sr_ref == NULL ) {
599 rs->sr_err = LDAP_NO_SUCH_OBJECT;
600 } else if ( op->o_protocol < LDAP_VERSION3 ) {
601 rs->sr_err = LDAP_PARTIAL_RESULTS;
607 * Call pre-result plugins. To avoid infinite recursion plugins
608 * should just set SLAPI_RESULT_CODE rather than sending a
609 * result if they wish to change the result.
611 if ( op->o_callback == NULL && op->o_pb != NULL ) {
612 slapi_int_pblock_set_operation( op->o_pb, op );
613 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
614 (void *)rs->sr_err );
615 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
616 (void *)rs->sr_text );
617 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
618 (void *)rs->sr_matched );
620 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
623 #endif /* LDAP_SLAPI */
625 if ( op->o_protocol < LDAP_VERSION3 ) {
626 tmp = v2ref( rs->sr_ref, rs->sr_text );
631 rs->sr_tag = req2res( op->o_tag );
632 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
634 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
635 if ( op->o_tag == LDAP_REQ_SEARCH ) {
637 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
638 rs->sr_err, rs->sr_nentries );
640 Statslog( LDAP_DEBUG_STATS,
641 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
642 op->o_log_prefix, rs->sr_tag, nbuf,
643 rs->sr_text ? rs->sr_text : "", 0 );
645 Statslog( LDAP_DEBUG_STATS,
646 "%s RESULT tag=%lu err=%d text=%s\n",
647 op->o_log_prefix, rs->sr_tag, rs->sr_err,
648 rs->sr_text ? rs->sr_text : "", 0 );
652 if( tmp != NULL ) ch_free(tmp);
658 send_ldap_sasl( Operation *op, SlapReply *rs )
660 rs->sr_type = REP_SASL;
661 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
663 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
665 rs->sr_tag = req2res( op->o_tag );
666 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
668 send_ldap_response( op, rs );
672 slap_send_ldap_extended( Operation *op, SlapReply *rs )
674 rs->sr_type = REP_EXTENDED;
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 );
682 rs->sr_tag = req2res( op->o_tag );
683 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
685 send_ldap_response( op, rs );
689 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
691 rs->sr_type = REP_INTERMEDIATE;
692 Debug( LDAP_DEBUG_TRACE,
693 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
695 rs->sr_rspoid ? rs->sr_rspoid : "",
696 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
697 rs->sr_tag = LDAP_RES_INTERMEDIATE;
698 rs->sr_msgid = op->o_msgid;
699 send_ldap_response( op, rs );
703 slap_send_search_entry( Operation *op, SlapReply *rs )
705 BerElementBuffer berbuf;
706 BerElement *ber = (BerElement *) &berbuf;
708 int i, j, rc=-1, bytes;
711 AccessControlState acl_state = ACL_STATE_INIT;
713 /* Support for computed attribute plugins */
714 computed_attr_context ctx;
717 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
719 /* a_flags: array of flags telling if the i-th element will be
720 * returned or filtered out
721 * e_flags: array of a_flags
723 char **e_flags = NULL;
725 rs->sr_type = REP_SEARCH;
727 /* eventually will loop through generated operational attribute types
728 * currently implemented types include:
729 * entryDN, subschemaSubentry, and hasSubordinates */
730 /* NOTE: moved before overlays callback circling because
731 * they may modify entry and other stuff in rs */
732 /* check for special all operational attributes ("+") type */
733 /* FIXME: maybe we could se this flag at the operation level;
734 * however, in principle the caller of send_search_entry() may
735 * change the attribute list at each call */
736 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
738 rc = backend_operational( op, rs );
743 if ( op->o_callback ) {
745 slap_callback *sc = op->o_callback,
746 *sc_next = op->o_callback;
748 rc = SLAP_CB_CONTINUE;
749 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
750 sc_next = op->o_callback->sc_next;
751 if ( op->o_callback->sc_response ) {
752 rc = op->o_callback->sc_response( op, rs );
753 if ( first && op->o_callback == NULL ) {
756 if ( rc != SLAP_CB_CONTINUE ) break;
762 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
765 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
766 op->o_connid, rs->sr_entry->e_name.bv_val,
767 op->ors_attrsonly ? " (attrsOnly)" : "" );
769 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
770 Debug( LDAP_DEBUG_ACL,
771 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
772 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
778 edn = rs->sr_entry->e_nname.bv_val;
780 if ( op->o_res_ber ) {
781 /* read back control or LDAP_CONNECTIONLESS */
787 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
788 bv.bv_len = siz + len;
789 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
791 ber_init2( ber, &bv, LBER_USE_DER );
792 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
795 #ifdef LDAP_CONNECTIONLESS
796 if ( op->o_conn && op->o_conn->c_is_udp ) {
798 if ( op->o_protocol == LDAP_VERSION2 ) {
799 rc = ber_printf(ber, "t{O{" /*}}*/,
800 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
802 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
803 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
807 if ( op->o_res_ber ) {
808 /* read back control */
809 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
811 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
812 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
816 Debug( LDAP_DEBUG_ANY,
817 "send_search_entry: conn %lu ber_printf failed\n",
818 op->o_connid, 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 = SLAP_USERATTRS( rs->sr_attr_flags );
828 /* create an array of arrays of flags. Each flag corresponds
829 * to particular value of attribute and equals 1 if value matches
830 * to ValuesReturnFilter or 0 if not
832 if ( op->o_vrFilter != NULL ) {
836 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
837 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
840 size = i * sizeof(char *) + k;
843 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
844 if( e_flags == NULL ) {
845 Debug( LDAP_DEBUG_ANY,
846 "send_search_entry: conn %lu slap_sl_calloc failed\n",
847 op->o_connid ? op->o_connid : 0, 0, 0 );
850 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
853 a_flags = (char *)(e_flags + i);
854 memset( a_flags, 0, k );
855 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
856 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
857 e_flags[i] = a_flags;
861 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
863 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
864 "conn %lu matched values filtering failed\n",
865 op->o_connid ? op->o_connid : 0, 0, 0 );
866 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
867 send_ldap_error( op, rs, LDAP_OTHER,
868 "matched values filtering error" );
874 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
875 AttributeDescription *desc = a->a_desc;
878 if ( rs->sr_attrs == NULL ) {
879 /* all attrs request, skip operational attributes */
880 if( is_at_operational( desc->ad_type ) ) {
885 /* specific attrs requested */
886 if ( is_at_operational( desc->ad_type ) ) {
887 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
888 !ad_inlist( desc, rs->sr_attrs ) )
894 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
901 if ( op->ors_attrsonly ) {
902 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
903 ACL_READ, &acl_state ) )
905 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
906 "conn %lu access to attribute %s not allowed\n",
907 op->o_connid, desc->ad_cname.bv_val, 0 );
911 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
912 Debug( LDAP_DEBUG_ANY,
913 "send_search_entry: conn %lu ber_printf failed\n",
914 op->o_connid, 0, 0 );
916 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
917 send_ldap_error( op, rs, LDAP_OTHER,
918 "encoding description error");
925 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
926 if ( ! access_allowed( op, rs->sr_entry,
927 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
929 Debug( LDAP_DEBUG_ACL,
930 "send_search_entry: conn %lu "
931 "access to attribute %s, value #%d not allowed\n",
932 op->o_connid, desc->ad_cname.bv_val, i );
937 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
944 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
945 Debug( LDAP_DEBUG_ANY,
946 "send_search_entry: conn %lu ber_printf failed\n",
947 op->o_connid, 0, 0 );
949 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
950 send_ldap_error( op, rs, LDAP_OTHER,
951 "encoding description error");
955 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
956 Debug( LDAP_DEBUG_ANY,
957 "send_search_entry: conn %lu "
958 "ber_printf failed.\n", op->o_connid, 0, 0 );
960 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
961 send_ldap_error( op, rs, LDAP_OTHER,
962 "encoding values error" );
968 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
969 Debug( LDAP_DEBUG_ANY,
970 "send_search_entry: conn %lu ber_printf failed\n",
971 op->o_connid, 0, 0 );
973 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
974 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
979 /* NOTE: moved before overlays callback circling because
980 * they may modify entry and other stuff in rs */
981 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
985 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
986 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
989 size = i * sizeof(char *) + k;
991 char *a_flags, **tmp;
994 * Reuse previous memory - we likely need less space
995 * for operational attributes
997 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1000 Debug( LDAP_DEBUG_ANY,
1001 "send_search_entry: conn %lu "
1002 "not enough memory "
1003 "for matched values filtering\n",
1004 op->o_connid, 0, 0 );
1005 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1006 send_ldap_error( op, rs, LDAP_OTHER,
1007 "not enough memory for matched values filtering" );
1011 a_flags = (char *)(e_flags + i);
1012 memset( a_flags, 0, k );
1013 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1014 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1015 e_flags[i] = a_flags;
1018 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1021 Debug( LDAP_DEBUG_ANY,
1022 "send_search_entry: conn %lu "
1023 "matched values filtering failed\n",
1024 op->o_connid ? op->o_connid : 0, 0, 0);
1025 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1026 send_ldap_error( op, rs, LDAP_OTHER,
1027 "matched values filtering error" );
1033 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1034 AttributeDescription *desc = a->a_desc;
1036 if ( rs->sr_attrs == NULL ) {
1037 /* all attrs request, skip operational attributes */
1038 if( is_at_operational( desc->ad_type ) ) {
1043 /* specific attrs requested */
1044 if( is_at_operational( desc->ad_type ) ) {
1045 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1046 !ad_inlist( desc, rs->sr_attrs ) )
1051 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1057 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1058 ACL_READ, &acl_state ) )
1060 Debug( LDAP_DEBUG_ACL,
1061 "send_search_entry: conn %lu "
1062 "access to attribute %s not allowed\n",
1063 op->o_connid, desc->ad_cname.bv_val, 0 );
1068 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1070 Debug( LDAP_DEBUG_ANY,
1071 "send_search_entry: conn %lu "
1072 "ber_printf failed\n", op->o_connid, 0, 0 );
1074 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1075 send_ldap_error( op, rs, LDAP_OTHER,
1076 "encoding description error" );
1080 if ( ! op->ors_attrsonly ) {
1081 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1082 if ( ! access_allowed( op, rs->sr_entry,
1083 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1085 Debug( LDAP_DEBUG_ACL,
1086 "send_search_entry: conn %lu "
1087 "access to %s, value %d not allowed\n",
1088 op->o_connid, desc->ad_cname.bv_val, i );
1093 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1097 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1098 Debug( LDAP_DEBUG_ANY,
1099 "send_search_entry: conn %lu ber_printf failed\n",
1100 op->o_connid, 0, 0 );
1102 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1103 send_ldap_error( op, rs, LDAP_OTHER,
1104 "encoding values error" );
1110 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1111 Debug( LDAP_DEBUG_ANY,
1112 "send_search_entry: conn %lu ber_printf failed\n",
1113 op->o_connid, 0, 0 );
1115 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1116 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1123 * First, setup the computed attribute context that is
1124 * passed to all plugins.
1127 ctx.cac_pb = op->o_pb;
1128 ctx.cac_attrs = rs->sr_attrs;
1129 ctx.cac_attrsonly = op->ors_attrsonly;
1130 ctx.cac_userattrs = userattrs;
1131 ctx.cac_opattrs = rs->sr_attr_flags;
1132 ctx.cac_acl_state = acl_state;
1133 ctx.cac_private = (void *)ber;
1136 * For each client requested attribute, call the plugins.
1138 if ( rs->sr_attrs != NULL ) {
1139 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1140 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1141 rs->sr_entry, slapi_int_compute_output_ber );
1142 if ( rc == 1 ) break;
1146 * Technically we shouldn't be returning operational attributes
1147 * when the user requested only user attributes. We'll let the
1148 * plugin decide whether to be naughty or not.
1150 rc = compute_evaluator( &ctx, "*",
1151 rs->sr_entry, slapi_int_compute_output_ber );
1154 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1155 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1159 #endif /* LDAP_SLAPI */
1163 slap_sl_free( e_flags, op->o_tmpmemctx );
1167 rc = ber_printf( ber, /*{{*/ "}N}" );
1170 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1174 #ifdef LDAP_CONNECTIONLESS
1175 if( op->o_conn && op->o_conn->c_is_udp ) {
1176 if ( op->o_protocol != LDAP_VERSION2 ) {
1177 rc = ber_printf( ber, /*{*/ "N}" );
1181 if ( op->o_res_ber == NULL ) {
1182 rc = ber_printf( ber, /*{*/ "N}" );
1187 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1189 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1190 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1195 if ( op->o_res_ber == NULL ) {
1196 bytes = send_ldap_ber( op->o_conn, ber );
1197 ber_free_buf( ber );
1200 Debug( LDAP_DEBUG_ANY,
1201 "send_search_entry: conn %lu ber write failed.\n",
1202 op->o_connid, 0, 0 );
1209 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1210 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1211 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1212 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1213 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1216 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1217 op->o_log_prefix, rs->sr_entry->e_dn, 0, 0, 0 );
1219 Debug( LDAP_DEBUG_TRACE,
1220 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1225 if ( op->o_callback ) {
1227 slap_callback *sc = op->o_callback, *sc_next;
1229 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1230 sc_next = op->o_callback->sc_next;
1231 if ( op->o_callback->sc_cleanup ) {
1232 (void)op->o_callback->sc_cleanup( op, rs );
1233 if ( first && op->o_callback != sc ) {
1234 sc = op->o_callback;
1239 op->o_callback = sc;
1243 slap_sl_free( e_flags, op->o_tmpmemctx );
1246 if ( rs->sr_operational_attrs ) {
1247 attrs_free( rs->sr_operational_attrs );
1248 rs->sr_operational_attrs = NULL;
1250 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1252 /* FIXME: I think rs->sr_type should be explicitly set to
1253 * REP_SEARCH here. That's what it was when we entered this
1254 * function. send_ldap_error may have changed it, but we
1255 * should set it back so that the cleanup functions know
1256 * what they're doing.
1258 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1260 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1262 entry_free( rs->sr_entry );
1263 rs->sr_entry = NULL;
1264 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1271 slap_send_search_reference( Operation *op, SlapReply *rs )
1273 BerElementBuffer berbuf;
1274 BerElement *ber = (BerElement *) &berbuf;
1278 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1279 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1281 rs->sr_type = REP_SEARCHREF;
1282 if ( op->o_callback ) {
1284 slap_callback *sc = op->o_callback,
1285 *sc_next = op->o_callback;
1287 rc = SLAP_CB_CONTINUE;
1288 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1289 sc_next = op->o_callback->sc_next;
1290 if ( op->o_callback->sc_response ) {
1291 rc = op->o_callback->sc_response( op, rs );
1292 if ( first && op->o_callback == NULL ) {
1295 if ( rc != SLAP_CB_CONTINUE ) break;
1300 op->o_callback = sc;
1301 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1304 Debug( LDAP_DEBUG_TRACE,
1305 "=> send_search_reference: dn=\"%s\"\n",
1306 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1308 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1309 ad_entry, NULL, ACL_READ, NULL ) )
1311 Debug( LDAP_DEBUG_ACL,
1312 "send_search_reference: access to entry not allowed\n",
1318 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1319 ad_ref, NULL, ACL_READ, NULL ) )
1321 Debug( LDAP_DEBUG_ACL,
1322 "send_search_reference: access "
1323 "to reference not allowed\n",
1329 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1330 if( op->o_domain_scope ) {
1331 Debug( LDAP_DEBUG_ANY,
1332 "send_search_reference: domainScope control in (%s)\n",
1333 rs->sr_entry->e_dn, 0, 0 );
1339 if( rs->sr_ref == NULL ) {
1340 Debug( LDAP_DEBUG_ANY,
1341 "send_search_reference: null ref in (%s)\n",
1342 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1347 if( op->o_protocol < LDAP_VERSION3 ) {
1349 /* save the references for the result */
1350 if( rs->sr_ref[0].bv_val != NULL ) {
1351 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1357 #ifdef LDAP_CONNECTIONLESS
1358 if( op->o_conn && op->o_conn->c_is_udp ) {
1359 ber = op->o_res_ber;
1363 ber_init_w_nullc( ber, LBER_USE_DER );
1364 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1367 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1368 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1371 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1375 rc = ber_printf( ber, /*"{"*/ "N}" );
1379 Debug( LDAP_DEBUG_ANY,
1380 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1382 #ifdef LDAP_CONNECTIONLESS
1383 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1385 ber_free_buf( ber );
1386 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1390 #ifdef LDAP_CONNECTIONLESS
1391 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1393 bytes = send_ldap_ber( op->o_conn, ber );
1394 ber_free_buf( ber );
1396 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1397 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1398 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1399 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1400 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1401 #ifdef LDAP_CONNECTIONLESS
1405 Statslog( LDAP_DEBUG_STATS2, "%s REF dn=\"%s\"\n",
1406 op->o_log_prefix, rs->sr_entry ? rs->sr_entry->e_dn : "(null)",
1409 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1412 if ( op->o_callback ) {
1414 slap_callback *sc = op->o_callback, *sc_next;
1416 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1417 sc_next = op->o_callback->sc_next;
1418 if ( op->o_callback->sc_cleanup ) {
1419 (void)op->o_callback->sc_cleanup( op, rs );
1420 if ( first && op->o_callback != sc ) {
1421 sc = op->o_callback;
1426 op->o_callback = sc;
1442 *code = LDAP_SUCCESS;
1446 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1447 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1454 while ( (s = strchr( s, '\n' )) != NULL ) {
1459 if ( (c = strchr( s, ':' )) != NULL ) {
1463 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1467 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1471 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1476 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1486 int slap_read_controls(
1490 const struct berval *oid,
1491 LDAPControl **ctrl )
1495 BerElementBuffer berbuf;
1496 BerElement *ber = (BerElement *) &berbuf;
1501 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1502 oid->bv_val, e->e_dn, 0 );
1505 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1506 op->o_preread_attrs : op->o_postread_attrs;
1508 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1509 bv.bv_len = siz + len;
1510 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1512 ber_init2( ber, &bv, LBER_USE_DER );
1513 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1515 /* create new operation */
1518 myop.o_res_ber = ber;
1520 rc = slap_send_search_entry( &myop, rs );
1523 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1525 if( rc == LBER_ERROR ) return LDAP_OTHER;
1527 c.ldctl_oid = oid->bv_val;
1528 c.ldctl_iscritical = 0;
1530 if ( ctrl == NULL ) {
1532 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1534 /* retry: free previous try */
1535 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1539 return LDAP_SUCCESS;
1542 /* Map API errors to protocol errors... */
1544 slap_map_api2result( SlapReply *rs )
1546 switch(rs->sr_err) {
1547 case LDAP_SERVER_DOWN:
1548 return LDAP_UNAVAILABLE;
1549 case LDAP_LOCAL_ERROR:
1551 case LDAP_ENCODING_ERROR:
1552 case LDAP_DECODING_ERROR:
1553 return LDAP_PROTOCOL_ERROR;
1555 return LDAP_UNAVAILABLE;
1556 case LDAP_AUTH_UNKNOWN:
1557 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1558 case LDAP_FILTER_ERROR:
1559 rs->sr_text = "Filter error";
1561 case LDAP_USER_CANCELLED:
1562 rs->sr_text = "User cancelled";
1564 case LDAP_PARAM_ERROR:
1565 return LDAP_PROTOCOL_ERROR;
1566 case LDAP_NO_MEMORY:
1568 case LDAP_CONNECT_ERROR:
1569 return LDAP_UNAVAILABLE;
1570 case LDAP_NOT_SUPPORTED:
1571 return LDAP_UNWILLING_TO_PERFORM;
1572 case LDAP_CONTROL_NOT_FOUND:
1573 return LDAP_PROTOCOL_ERROR;
1574 case LDAP_NO_RESULTS_RETURNED:
1575 return LDAP_NO_SUCH_OBJECT;
1576 case LDAP_MORE_RESULTS_TO_RETURN:
1577 rs->sr_text = "More results to return";
1579 case LDAP_CLIENT_LOOP:
1580 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1581 return LDAP_LOOP_DETECT;
1583 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1590 slap_attr_flags( AttributeName *an )
1592 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1595 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1598 flags |= an_find( an, &AllOper )
1599 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1600 flags |= an_find( an, &AllUser )
1601 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;