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 ( rs->sr_err == SLAPD_ABANDON ) {
335 if ( op->o_callback ) {
337 slap_callback *sc = op->o_callback,
338 *sc_next = op->o_callback;
340 rc = SLAP_CB_CONTINUE;
341 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
342 sc_next = op->o_callback->sc_next;
343 if ( op->o_callback->sc_response ) {
344 rc = op->o_callback->sc_response( op, rs );
345 if ( first && op->o_callback == NULL ) {
348 if ( rc != SLAP_CB_CONTINUE ) break;
354 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
357 #ifdef LDAP_CONNECTIONLESS
358 if (op->o_conn && op->o_conn->c_is_udp)
363 ber_init_w_nullc( ber, LBER_USE_DER );
364 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
367 Debug( LDAP_DEBUG_TRACE,
368 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
369 rs->sr_msgid, rs->sr_tag, rs->sr_err );
372 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
373 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
377 #ifdef LDAP_CONNECTIONLESS
378 if (op->o_conn && op->o_conn->c_is_udp &&
379 op->o_protocol == LDAP_VERSION2 )
381 rc = ber_printf( ber, "t{ess" /*"}"*/,
382 rs->sr_tag, rs->sr_err,
383 rs->sr_matched == NULL ? "" : rs->sr_matched,
384 rs->sr_text == NULL ? "" : rs->sr_text );
387 if ( rs->sr_type == REP_INTERMEDIATE ) {
388 rc = ber_printf( ber, "{it{" /*"}}"*/,
389 rs->sr_msgid, rs->sr_tag );
392 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
393 rs->sr_msgid, rs->sr_tag, rs->sr_err,
394 rs->sr_matched == NULL ? "" : rs->sr_matched,
395 rs->sr_text == NULL ? "" : rs->sr_text );
399 if ( rs->sr_ref != NULL ) {
400 assert( rs->sr_err == LDAP_REFERRAL );
401 rc = ber_printf( ber, "t{W}",
402 LDAP_TAG_REFERRAL, rs->sr_ref );
404 assert( rs->sr_err != LDAP_REFERRAL );
408 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
409 rc = ber_printf( ber, "tO",
410 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
414 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
416 if ( rs->sr_rspoid != NULL ) {
417 rc = ber_printf( ber, "ts",
418 rs->sr_type == REP_EXTENDED
419 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
422 if( rc != -1 && rs->sr_rspdata != NULL ) {
423 rc = ber_printf( ber, "tO",
424 rs->sr_type == REP_EXTENDED
425 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
431 rc = ber_printf( ber, /*"{"*/ "N}" );
435 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
439 rc = ber_printf( ber, /*"{"*/ "N}" );
442 #ifdef LDAP_CONNECTIONLESS
443 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
446 rc = ber_printf( ber, /*"{"*/ "N}" );
451 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
453 #ifdef LDAP_CONNECTIONLESS
454 if (!op->o_conn || op->o_conn->c_is_udp == 0)
463 bytes = send_ldap_ber( op->o_conn, ber );
464 #ifdef LDAP_CONNECTIONLESS
465 if (!op->o_conn || op->o_conn->c_is_udp == 0)
472 Debug( LDAP_DEBUG_ANY,
473 "send_ldap_response: ber write failed\n",
481 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
482 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
483 (void *)rs->sr_matched );
484 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
486 #endif /* LDAP_SLAPI */
488 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
489 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
490 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
491 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
494 /* Tell caller that we did this for real, as opposed to being
495 * overridden by a callback
497 rc = SLAP_CB_CONTINUE;
500 if ( op->o_callback ) {
502 slap_callback *sc = op->o_callback, *sc_next;
504 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
505 sc_next = op->o_callback->sc_next;
506 if ( op->o_callback->sc_cleanup ) {
507 (void)op->o_callback->sc_cleanup( op, rs );
508 if ( first && op->o_callback != sc ) {
518 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
519 free( (char *)rs->sr_matched );
520 rs->sr_matched = NULL;
523 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
524 ber_bvarray_free( rs->sr_ref );
533 send_ldap_disconnect( Operation *op, SlapReply *rs )
535 #define LDAP_UNSOLICITED_ERROR(e) \
536 ( (e) == LDAP_PROTOCOL_ERROR \
537 || (e) == LDAP_STRONG_AUTH_REQUIRED \
538 || (e) == LDAP_UNAVAILABLE )
540 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
542 rs->sr_type = REP_EXTENDED;
544 Debug( LDAP_DEBUG_TRACE,
545 "send_ldap_disconnect %d:%s\n",
546 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
548 if ( op->o_protocol < LDAP_VERSION3 ) {
549 rs->sr_rspoid = NULL;
550 rs->sr_tag = req2res( op->o_tag );
551 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
554 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
555 rs->sr_tag = LDAP_RES_EXTENDED;
559 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
560 Statslog( LDAP_DEBUG_STATS,
561 "%s DISCONNECT tag=%lu err=%d text=%s\n",
562 op->o_log_prefix, rs->sr_tag, rs->sr_err,
563 rs->sr_text ? rs->sr_text : "", 0 );
568 slap_send_ldap_result( Operation *op, SlapReply *rs )
571 const char *otext = rs->sr_text;
572 BerVarray oref = rs->sr_ref;
574 rs->sr_type = REP_RESULT;
576 /* Propagate Abandons so that cleanup callbacks can be processed */
577 if ( rs->sr_err == SLAPD_ABANDON )
580 assert( !LDAP_API_ERROR( rs->sr_err ) );
582 Debug( LDAP_DEBUG_TRACE,
583 "send_ldap_result: %s p=%d\n",
584 op->o_log_prefix, op->o_protocol, 0 );
586 Debug( LDAP_DEBUG_ARGS,
587 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
588 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
589 rs->sr_text ? rs->sr_text : "" );
593 Debug( LDAP_DEBUG_ARGS,
594 "send_ldap_result: referral=\"%s\"\n",
595 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
599 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
601 if ( rs->sr_err == LDAP_REFERRAL ) {
602 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
603 if( op->o_domain_scope ) {
607 if( rs->sr_ref == NULL ) {
608 rs->sr_err = LDAP_NO_SUCH_OBJECT;
609 } else if ( op->o_protocol < LDAP_VERSION3 ) {
610 rs->sr_err = LDAP_PARTIAL_RESULTS;
616 * Call pre-result plugins. To avoid infinite recursion plugins
617 * should just set SLAPI_RESULT_CODE rather than sending a
618 * result if they wish to change the result.
620 if ( op->o_callback == NULL && op->o_pb != NULL ) {
621 slapi_int_pblock_set_operation( op->o_pb, op );
622 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
623 (void *)rs->sr_err );
624 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
625 (void *)rs->sr_text );
626 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
627 (void *)rs->sr_matched );
629 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
632 #endif /* LDAP_SLAPI */
634 if ( op->o_protocol < LDAP_VERSION3 ) {
635 tmp = v2ref( rs->sr_ref, rs->sr_text );
640 rs->sr_tag = req2res( op->o_tag );
641 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
644 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
645 if ( op->o_tag == LDAP_REQ_SEARCH ) {
647 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
648 rs->sr_err, rs->sr_nentries );
650 Statslog( LDAP_DEBUG_STATS,
651 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
652 op->o_log_prefix, rs->sr_tag, nbuf,
653 rs->sr_text ? rs->sr_text : "", 0 );
655 Statslog( LDAP_DEBUG_STATS,
656 "%s RESULT tag=%lu err=%d text=%s\n",
657 op->o_log_prefix, rs->sr_tag, rs->sr_err,
658 rs->sr_text ? rs->sr_text : "", 0 );
662 if( tmp != NULL ) ch_free(tmp);
668 send_ldap_sasl( Operation *op, SlapReply *rs )
670 rs->sr_type = REP_SASL;
671 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
673 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
675 rs->sr_tag = req2res( op->o_tag );
676 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
678 send_ldap_response( op, rs );
682 slap_send_ldap_extended( Operation *op, SlapReply *rs )
684 rs->sr_type = REP_EXTENDED;
686 Debug( LDAP_DEBUG_TRACE,
687 "send_ldap_extended: err=%d oid=%s len=%ld\n",
689 rs->sr_rspoid ? rs->sr_rspoid : "",
690 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
692 rs->sr_tag = req2res( op->o_tag );
693 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
695 send_ldap_response( op, rs );
699 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
701 rs->sr_type = REP_INTERMEDIATE;
702 Debug( LDAP_DEBUG_TRACE,
703 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
705 rs->sr_rspoid ? rs->sr_rspoid : "",
706 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
707 rs->sr_tag = LDAP_RES_INTERMEDIATE;
708 rs->sr_msgid = op->o_msgid;
709 send_ldap_response( op, rs );
713 slap_send_search_entry( Operation *op, SlapReply *rs )
715 BerElementBuffer berbuf;
716 BerElement *ber = (BerElement *) &berbuf;
718 int i, j, rc=-1, bytes;
721 AccessControlState acl_state = ACL_STATE_INIT;
723 /* Support for computed attribute plugins */
724 computed_attr_context ctx;
727 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
729 /* a_flags: array of flags telling if the i-th element will be
730 * returned or filtered out
731 * e_flags: array of a_flags
733 char **e_flags = NULL;
735 rs->sr_type = REP_SEARCH;
737 /* eventually will loop through generated operational attribute types
738 * currently implemented types include:
739 * entryDN, subschemaSubentry, and hasSubordinates */
740 /* NOTE: moved before overlays callback circling because
741 * they may modify entry and other stuff in rs */
742 /* check for special all operational attributes ("+") type */
743 /* FIXME: maybe we could se this flag at the operation level;
744 * however, in principle the caller of send_search_entry() may
745 * change the attribute list at each call */
746 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
748 rc = backend_operational( op, rs );
753 if ( op->o_callback ) {
755 slap_callback *sc = op->o_callback,
756 *sc_next = op->o_callback;
758 rc = SLAP_CB_CONTINUE;
759 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
760 sc_next = op->o_callback->sc_next;
761 if ( op->o_callback->sc_response ) {
762 rc = op->o_callback->sc_response( op, rs );
763 if ( first && op->o_callback == NULL ) {
766 if ( rc != SLAP_CB_CONTINUE ) break;
772 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
775 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
776 op->o_connid, rs->sr_entry->e_name.bv_val,
777 op->ors_attrsonly ? " (attrsOnly)" : "" );
779 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
780 Debug( LDAP_DEBUG_ACL,
781 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
782 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
788 edn = rs->sr_entry->e_nname.bv_val;
790 if ( op->o_res_ber ) {
791 /* read back control or LDAP_CONNECTIONLESS */
796 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
797 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
799 ber_init2( ber, &bv, LBER_USE_DER );
800 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
803 #ifdef LDAP_CONNECTIONLESS
804 if ( op->o_conn && op->o_conn->c_is_udp ) {
806 if ( op->o_protocol == LDAP_VERSION2 ) {
807 rc = ber_printf(ber, "t{O{" /*}}*/,
808 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
810 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
811 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
815 if ( op->o_res_ber ) {
816 /* read back control */
817 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
819 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
820 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
824 Debug( LDAP_DEBUG_ANY,
825 "send_search_entry: conn %lu ber_printf failed\n",
826 op->o_connid, 0, 0 );
828 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
829 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
833 /* check for special all user attributes ("*") type */
834 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
836 /* create an array of arrays of flags. Each flag corresponds
837 * to particular value of attribute and equals 1 if value matches
838 * to ValuesReturnFilter or 0 if not
840 if ( op->o_vrFilter != NULL ) {
844 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
845 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
848 size = i * sizeof(char *) + k;
851 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
852 if( e_flags == NULL ) {
853 Debug( LDAP_DEBUG_ANY,
854 "send_search_entry: conn %lu slap_sl_calloc failed\n",
855 op->o_connid ? op->o_connid : 0, 0, 0 );
858 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
861 a_flags = (char *)(e_flags + i);
862 memset( a_flags, 0, k );
863 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
864 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
865 e_flags[i] = a_flags;
869 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
871 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
872 "conn %lu matched values filtering failed\n",
873 op->o_connid ? op->o_connid : 0, 0, 0 );
874 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
875 send_ldap_error( op, rs, LDAP_OTHER,
876 "matched values filtering error" );
882 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
883 AttributeDescription *desc = a->a_desc;
886 if ( rs->sr_attrs == NULL ) {
887 /* all attrs request, skip operational attributes */
888 if( is_at_operational( desc->ad_type ) ) {
893 /* specific attrs requested */
894 if ( is_at_operational( desc->ad_type ) ) {
895 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
896 !ad_inlist( desc, rs->sr_attrs ) )
902 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
909 if ( op->ors_attrsonly ) {
910 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
911 ACL_READ, &acl_state ) )
913 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
914 "conn %lu access to attribute %s not allowed\n",
915 op->o_connid, desc->ad_cname.bv_val, 0 );
919 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
920 Debug( LDAP_DEBUG_ANY,
921 "send_search_entry: conn %lu ber_printf failed\n",
922 op->o_connid, 0, 0 );
924 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
925 send_ldap_error( op, rs, LDAP_OTHER,
926 "encoding description error");
933 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
934 if ( ! access_allowed( op, rs->sr_entry,
935 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
937 Debug( LDAP_DEBUG_ACL,
938 "send_search_entry: conn %lu "
939 "access to attribute %s, value #%d not allowed\n",
940 op->o_connid, desc->ad_cname.bv_val, i );
945 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
952 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
953 Debug( LDAP_DEBUG_ANY,
954 "send_search_entry: conn %lu ber_printf failed\n",
955 op->o_connid, 0, 0 );
957 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
958 send_ldap_error( op, rs, LDAP_OTHER,
959 "encoding description error");
963 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
964 Debug( LDAP_DEBUG_ANY,
965 "send_search_entry: conn %lu "
966 "ber_printf failed.\n", op->o_connid, 0, 0 );
968 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
969 send_ldap_error( op, rs, LDAP_OTHER,
970 "encoding values error" );
976 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
977 Debug( LDAP_DEBUG_ANY,
978 "send_search_entry: conn %lu ber_printf failed\n",
979 op->o_connid, 0, 0 );
981 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
982 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
987 /* NOTE: moved before overlays callback circling because
988 * they may modify entry and other stuff in rs */
989 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
993 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
994 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
997 size = i * sizeof(char *) + k;
999 char *a_flags, **tmp;
1002 * Reuse previous memory - we likely need less space
1003 * for operational attributes
1005 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1007 if ( tmp == NULL ) {
1008 Debug( LDAP_DEBUG_ANY,
1009 "send_search_entry: conn %lu "
1010 "not enough memory "
1011 "for matched values filtering\n",
1012 op->o_connid, 0, 0 );
1013 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1014 send_ldap_error( op, rs, LDAP_OTHER,
1015 "not enough memory for matched values filtering" );
1019 a_flags = (char *)(e_flags + i);
1020 memset( a_flags, 0, k );
1021 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1022 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1023 e_flags[i] = a_flags;
1026 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1029 Debug( LDAP_DEBUG_ANY,
1030 "send_search_entry: conn %lu "
1031 "matched values filtering failed\n",
1032 op->o_connid ? op->o_connid : 0, 0, 0);
1033 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1034 send_ldap_error( op, rs, LDAP_OTHER,
1035 "matched values filtering error" );
1041 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1042 AttributeDescription *desc = a->a_desc;
1044 if ( rs->sr_attrs == NULL ) {
1045 /* all attrs request, skip operational attributes */
1046 if( is_at_operational( desc->ad_type ) ) {
1051 /* specific attrs requested */
1052 if( is_at_operational( desc->ad_type ) ) {
1053 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1054 !ad_inlist( desc, rs->sr_attrs ) )
1059 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1065 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1066 ACL_READ, &acl_state ) )
1068 Debug( LDAP_DEBUG_ACL,
1069 "send_search_entry: conn %lu "
1070 "access to attribute %s not allowed\n",
1071 op->o_connid, desc->ad_cname.bv_val, 0 );
1076 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1078 Debug( LDAP_DEBUG_ANY,
1079 "send_search_entry: conn %lu "
1080 "ber_printf failed\n", op->o_connid, 0, 0 );
1082 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1083 send_ldap_error( op, rs, LDAP_OTHER,
1084 "encoding description error" );
1088 if ( ! op->ors_attrsonly ) {
1089 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1090 if ( ! access_allowed( op, rs->sr_entry,
1091 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1093 Debug( LDAP_DEBUG_ACL,
1094 "send_search_entry: conn %lu "
1095 "access to %s, value %d not allowed\n",
1096 op->o_connid, desc->ad_cname.bv_val, i );
1101 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1105 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1106 Debug( LDAP_DEBUG_ANY,
1107 "send_search_entry: conn %lu ber_printf failed\n",
1108 op->o_connid, 0, 0 );
1110 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1111 send_ldap_error( op, rs, LDAP_OTHER,
1112 "encoding values error" );
1118 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1119 Debug( LDAP_DEBUG_ANY,
1120 "send_search_entry: conn %lu ber_printf failed\n",
1121 op->o_connid, 0, 0 );
1123 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1124 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1131 * First, setup the computed attribute context that is
1132 * passed to all plugins.
1135 ctx.cac_pb = op->o_pb;
1136 ctx.cac_attrs = rs->sr_attrs;
1137 ctx.cac_attrsonly = op->ors_attrsonly;
1138 ctx.cac_userattrs = userattrs;
1139 ctx.cac_opattrs = rs->sr_attr_flags;
1140 ctx.cac_acl_state = acl_state;
1141 ctx.cac_private = (void *)ber;
1144 * For each client requested attribute, call the plugins.
1146 if ( rs->sr_attrs != NULL ) {
1147 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1148 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1149 rs->sr_entry, slapi_int_compute_output_ber );
1150 if ( rc == 1 ) break;
1154 * Technically we shouldn't be returning operational attributes
1155 * when the user requested only user attributes. We'll let the
1156 * plugin decide whether to be naughty or not.
1158 rc = compute_evaluator( &ctx, "*",
1159 rs->sr_entry, slapi_int_compute_output_ber );
1162 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1163 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1167 #endif /* LDAP_SLAPI */
1171 slap_sl_free( e_flags, op->o_tmpmemctx );
1175 rc = ber_printf( ber, /*{{*/ "}N}" );
1178 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1182 #ifdef LDAP_CONNECTIONLESS
1183 if( op->o_conn && op->o_conn->c_is_udp ) {
1184 if ( op->o_protocol != LDAP_VERSION2 ) {
1185 rc = ber_printf( ber, /*{*/ "N}" );
1189 if ( op->o_res_ber == NULL ) {
1190 rc = ber_printf( ber, /*{*/ "N}" );
1195 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1197 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1198 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1203 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1204 be_entry_release_rw( op, rs->sr_entry, 0 );
1205 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1206 rs->sr_entry = NULL;
1209 if ( op->o_res_ber == NULL ) {
1210 bytes = send_ldap_ber( op->o_conn, ber );
1211 ber_free_buf( ber );
1214 Debug( LDAP_DEBUG_ANY,
1215 "send_search_entry: conn %lu ber write failed.\n",
1216 op->o_connid, 0, 0 );
1223 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1224 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1225 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1226 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1227 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1230 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1231 op->o_log_prefix, edn, 0, 0, 0 );
1233 Debug( LDAP_DEBUG_TRACE,
1234 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1239 if ( op->o_callback ) {
1241 slap_callback *sc = op->o_callback, *sc_next;
1243 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1244 sc_next = op->o_callback->sc_next;
1245 if ( op->o_callback->sc_cleanup ) {
1246 (void)op->o_callback->sc_cleanup( op, rs );
1247 if ( first && op->o_callback != sc ) {
1248 sc = op->o_callback;
1253 op->o_callback = sc;
1257 slap_sl_free( e_flags, op->o_tmpmemctx );
1260 if ( rs->sr_operational_attrs ) {
1261 attrs_free( rs->sr_operational_attrs );
1262 rs->sr_operational_attrs = NULL;
1264 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1266 /* FIXME: I think rs->sr_type should be explicitly set to
1267 * REP_SEARCH here. That's what it was when we entered this
1268 * function. send_ldap_error may have changed it, but we
1269 * should set it back so that the cleanup functions know
1270 * what they're doing.
1272 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1274 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1276 entry_free( rs->sr_entry );
1277 rs->sr_entry = NULL;
1278 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1285 slap_send_search_reference( Operation *op, SlapReply *rs )
1287 BerElementBuffer berbuf;
1288 BerElement *ber = (BerElement *) &berbuf;
1292 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1293 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1295 rs->sr_type = REP_SEARCHREF;
1296 if ( op->o_callback ) {
1298 slap_callback *sc = op->o_callback,
1299 *sc_next = op->o_callback;
1301 rc = SLAP_CB_CONTINUE;
1302 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1303 sc_next = op->o_callback->sc_next;
1304 if ( op->o_callback->sc_response ) {
1305 rc = op->o_callback->sc_response( op, rs );
1306 if ( first && op->o_callback == NULL ) {
1309 if ( rc != SLAP_CB_CONTINUE ) break;
1314 op->o_callback = sc;
1315 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1318 Debug( LDAP_DEBUG_TRACE,
1319 "=> send_search_reference: dn=\"%s\"\n",
1320 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1322 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1323 ad_entry, NULL, ACL_READ, NULL ) )
1325 Debug( LDAP_DEBUG_ACL,
1326 "send_search_reference: access to entry not allowed\n",
1332 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1333 ad_ref, NULL, ACL_READ, NULL ) )
1335 Debug( LDAP_DEBUG_ACL,
1336 "send_search_reference: access "
1337 "to reference not allowed\n",
1343 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1344 if( op->o_domain_scope ) {
1345 Debug( LDAP_DEBUG_ANY,
1346 "send_search_reference: domainScope control in (%s)\n",
1347 rs->sr_entry->e_dn, 0, 0 );
1353 if( rs->sr_ref == NULL ) {
1354 Debug( LDAP_DEBUG_ANY,
1355 "send_search_reference: null ref in (%s)\n",
1356 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1361 if( op->o_protocol < LDAP_VERSION3 ) {
1363 /* save the references for the result */
1364 if( rs->sr_ref[0].bv_val != NULL ) {
1365 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1371 #ifdef LDAP_CONNECTIONLESS
1372 if( op->o_conn && op->o_conn->c_is_udp ) {
1373 ber = op->o_res_ber;
1377 ber_init_w_nullc( ber, LBER_USE_DER );
1378 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1381 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1382 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1385 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1389 rc = ber_printf( ber, /*"{"*/ "N}" );
1393 Debug( LDAP_DEBUG_ANY,
1394 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1396 #ifdef LDAP_CONNECTIONLESS
1397 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1399 ber_free_buf( ber );
1400 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1405 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1406 be_entry_release_rw( op, rs->sr_entry, 0 );
1407 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1408 rs->sr_entry = NULL;
1411 #ifdef LDAP_CONNECTIONLESS
1412 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1414 bytes = send_ldap_ber( op->o_conn, ber );
1415 ber_free_buf( ber );
1417 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1418 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1419 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1420 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1421 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1422 #ifdef LDAP_CONNECTIONLESS
1426 Statslog( LDAP_DEBUG_STATS2, "%s REF dn=\"%s\"\n",
1427 op->o_log_prefix, rs->sr_entry ? rs->sr_entry->e_dn : "(null)",
1430 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1433 if ( op->o_callback ) {
1435 slap_callback *sc = op->o_callback, *sc_next;
1437 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1438 sc_next = op->o_callback->sc_next;
1439 if ( op->o_callback->sc_cleanup ) {
1440 (void)op->o_callback->sc_cleanup( op, rs );
1441 if ( first && op->o_callback != sc ) {
1442 sc = op->o_callback;
1447 op->o_callback = sc;
1463 *code = LDAP_SUCCESS;
1467 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1468 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1475 while ( (s = strchr( s, '\n' )) != NULL ) {
1480 if ( (c = strchr( s, ':' )) != NULL ) {
1484 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1488 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1492 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1497 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1507 int slap_read_controls(
1511 const struct berval *oid,
1512 LDAPControl **ctrl )
1516 BerElementBuffer berbuf;
1517 BerElement *ber = (BerElement *) &berbuf;
1521 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1522 oid->bv_val, e->e_dn, 0 );
1525 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1526 op->o_preread_attrs : op->o_postread_attrs;
1528 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1529 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1531 ber_init2( ber, &bv, LBER_USE_DER );
1532 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1534 /* create new operation */
1537 myop.o_res_ber = ber;
1539 rc = slap_send_search_entry( &myop, rs );
1542 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1544 if( rc == LBER_ERROR ) return LDAP_OTHER;
1546 c.ldctl_oid = oid->bv_val;
1547 c.ldctl_iscritical = 0;
1549 if ( ctrl == NULL ) {
1551 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1553 /* retry: free previous try */
1554 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1558 return LDAP_SUCCESS;
1561 /* Map API errors to protocol errors... */
1563 slap_map_api2result( SlapReply *rs )
1565 switch(rs->sr_err) {
1566 case LDAP_SERVER_DOWN:
1567 return LDAP_UNAVAILABLE;
1568 case LDAP_LOCAL_ERROR:
1570 case LDAP_ENCODING_ERROR:
1571 case LDAP_DECODING_ERROR:
1572 return LDAP_PROTOCOL_ERROR;
1574 return LDAP_UNAVAILABLE;
1575 case LDAP_AUTH_UNKNOWN:
1576 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1577 case LDAP_FILTER_ERROR:
1578 rs->sr_text = "Filter error";
1580 case LDAP_USER_CANCELLED:
1581 rs->sr_text = "User cancelled";
1583 case LDAP_PARAM_ERROR:
1584 return LDAP_PROTOCOL_ERROR;
1585 case LDAP_NO_MEMORY:
1587 case LDAP_CONNECT_ERROR:
1588 return LDAP_UNAVAILABLE;
1589 case LDAP_NOT_SUPPORTED:
1590 return LDAP_UNWILLING_TO_PERFORM;
1591 case LDAP_CONTROL_NOT_FOUND:
1592 return LDAP_PROTOCOL_ERROR;
1593 case LDAP_NO_RESULTS_RETURNED:
1594 return LDAP_NO_SUCH_OBJECT;
1595 case LDAP_MORE_RESULTS_TO_RETURN:
1596 rs->sr_text = "More results to return";
1598 case LDAP_CLIENT_LOOP:
1599 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1600 return LDAP_LOOP_DETECT;
1602 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1609 slap_attr_flags( AttributeName *an )
1611 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1614 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1617 flags |= an_find( an, &AllOper )
1618 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1619 flags |= an_find( an, &AllUser )
1620 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;