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 */
797 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
798 bv.bv_len = siz + len;
799 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
801 ber_init2( ber, &bv, LBER_USE_DER );
802 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
805 #ifdef LDAP_CONNECTIONLESS
806 if ( op->o_conn && op->o_conn->c_is_udp ) {
808 if ( op->o_protocol == LDAP_VERSION2 ) {
809 rc = ber_printf(ber, "t{O{" /*}}*/,
810 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
812 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
813 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
817 if ( op->o_res_ber ) {
818 /* read back control */
819 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
821 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
822 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
826 Debug( LDAP_DEBUG_ANY,
827 "send_search_entry: conn %lu ber_printf failed\n",
828 op->o_connid, 0, 0 );
830 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
831 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
835 /* check for special all user attributes ("*") type */
836 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
838 /* create an array of arrays of flags. Each flag corresponds
839 * to particular value of attribute and equals 1 if value matches
840 * to ValuesReturnFilter or 0 if not
842 if ( op->o_vrFilter != NULL ) {
846 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
847 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
850 size = i * sizeof(char *) + k;
853 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
854 if( e_flags == NULL ) {
855 Debug( LDAP_DEBUG_ANY,
856 "send_search_entry: conn %lu slap_sl_calloc failed\n",
857 op->o_connid ? op->o_connid : 0, 0, 0 );
860 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
863 a_flags = (char *)(e_flags + i);
864 memset( a_flags, 0, k );
865 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
866 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
867 e_flags[i] = a_flags;
871 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
873 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
874 "conn %lu matched values filtering failed\n",
875 op->o_connid ? op->o_connid : 0, 0, 0 );
876 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
877 send_ldap_error( op, rs, LDAP_OTHER,
878 "matched values filtering error" );
884 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
885 AttributeDescription *desc = a->a_desc;
888 if ( rs->sr_attrs == NULL ) {
889 /* all attrs request, skip operational attributes */
890 if( is_at_operational( desc->ad_type ) ) {
895 /* specific attrs requested */
896 if ( is_at_operational( desc->ad_type ) ) {
897 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
898 !ad_inlist( desc, rs->sr_attrs ) )
904 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
911 if ( op->ors_attrsonly ) {
912 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
913 ACL_READ, &acl_state ) )
915 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
916 "conn %lu access to attribute %s not allowed\n",
917 op->o_connid, desc->ad_cname.bv_val, 0 );
921 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
922 Debug( LDAP_DEBUG_ANY,
923 "send_search_entry: conn %lu ber_printf failed\n",
924 op->o_connid, 0, 0 );
926 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
927 send_ldap_error( op, rs, LDAP_OTHER,
928 "encoding description error");
935 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
936 if ( ! access_allowed( op, rs->sr_entry,
937 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
939 Debug( LDAP_DEBUG_ACL,
940 "send_search_entry: conn %lu "
941 "access to attribute %s, value #%d not allowed\n",
942 op->o_connid, desc->ad_cname.bv_val, i );
947 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
954 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
955 Debug( LDAP_DEBUG_ANY,
956 "send_search_entry: conn %lu ber_printf failed\n",
957 op->o_connid, 0, 0 );
959 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
960 send_ldap_error( op, rs, LDAP_OTHER,
961 "encoding description error");
965 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
966 Debug( LDAP_DEBUG_ANY,
967 "send_search_entry: conn %lu "
968 "ber_printf failed.\n", op->o_connid, 0, 0 );
970 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
971 send_ldap_error( op, rs, LDAP_OTHER,
972 "encoding values error" );
978 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
979 Debug( LDAP_DEBUG_ANY,
980 "send_search_entry: conn %lu ber_printf failed\n",
981 op->o_connid, 0, 0 );
983 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
984 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
989 /* NOTE: moved before overlays callback circling because
990 * they may modify entry and other stuff in rs */
991 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
995 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
996 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
999 size = i * sizeof(char *) + k;
1001 char *a_flags, **tmp;
1004 * Reuse previous memory - we likely need less space
1005 * for operational attributes
1007 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1009 if ( tmp == NULL ) {
1010 Debug( LDAP_DEBUG_ANY,
1011 "send_search_entry: conn %lu "
1012 "not enough memory "
1013 "for matched values filtering\n",
1014 op->o_connid, 0, 0 );
1015 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1016 send_ldap_error( op, rs, LDAP_OTHER,
1017 "not enough memory for matched values filtering" );
1021 a_flags = (char *)(e_flags + i);
1022 memset( a_flags, 0, k );
1023 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1024 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1025 e_flags[i] = a_flags;
1028 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1031 Debug( LDAP_DEBUG_ANY,
1032 "send_search_entry: conn %lu "
1033 "matched values filtering failed\n",
1034 op->o_connid ? op->o_connid : 0, 0, 0);
1035 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1036 send_ldap_error( op, rs, LDAP_OTHER,
1037 "matched values filtering error" );
1043 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1044 AttributeDescription *desc = a->a_desc;
1046 if ( rs->sr_attrs == NULL ) {
1047 /* all attrs request, skip operational attributes */
1048 if( is_at_operational( desc->ad_type ) ) {
1053 /* specific attrs requested */
1054 if( is_at_operational( desc->ad_type ) ) {
1055 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1056 !ad_inlist( desc, rs->sr_attrs ) )
1061 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1067 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1068 ACL_READ, &acl_state ) )
1070 Debug( LDAP_DEBUG_ACL,
1071 "send_search_entry: conn %lu "
1072 "access to attribute %s not allowed\n",
1073 op->o_connid, desc->ad_cname.bv_val, 0 );
1078 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1080 Debug( LDAP_DEBUG_ANY,
1081 "send_search_entry: conn %lu "
1082 "ber_printf failed\n", op->o_connid, 0, 0 );
1084 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1085 send_ldap_error( op, rs, LDAP_OTHER,
1086 "encoding description error" );
1090 if ( ! op->ors_attrsonly ) {
1091 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1092 if ( ! access_allowed( op, rs->sr_entry,
1093 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1095 Debug( LDAP_DEBUG_ACL,
1096 "send_search_entry: conn %lu "
1097 "access to %s, value %d not allowed\n",
1098 op->o_connid, desc->ad_cname.bv_val, i );
1103 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1107 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1108 Debug( LDAP_DEBUG_ANY,
1109 "send_search_entry: conn %lu ber_printf failed\n",
1110 op->o_connid, 0, 0 );
1112 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1113 send_ldap_error( op, rs, LDAP_OTHER,
1114 "encoding values error" );
1120 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1121 Debug( LDAP_DEBUG_ANY,
1122 "send_search_entry: conn %lu ber_printf failed\n",
1123 op->o_connid, 0, 0 );
1125 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1126 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1133 * First, setup the computed attribute context that is
1134 * passed to all plugins.
1137 ctx.cac_pb = op->o_pb;
1138 ctx.cac_attrs = rs->sr_attrs;
1139 ctx.cac_attrsonly = op->ors_attrsonly;
1140 ctx.cac_userattrs = userattrs;
1141 ctx.cac_opattrs = rs->sr_attr_flags;
1142 ctx.cac_acl_state = acl_state;
1143 ctx.cac_private = (void *)ber;
1146 * For each client requested attribute, call the plugins.
1148 if ( rs->sr_attrs != NULL ) {
1149 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1150 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1151 rs->sr_entry, slapi_int_compute_output_ber );
1152 if ( rc == 1 ) break;
1156 * Technically we shouldn't be returning operational attributes
1157 * when the user requested only user attributes. We'll let the
1158 * plugin decide whether to be naughty or not.
1160 rc = compute_evaluator( &ctx, "*",
1161 rs->sr_entry, slapi_int_compute_output_ber );
1164 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1165 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1169 #endif /* LDAP_SLAPI */
1173 slap_sl_free( e_flags, op->o_tmpmemctx );
1177 rc = ber_printf( ber, /*{{*/ "}N}" );
1180 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1184 #ifdef LDAP_CONNECTIONLESS
1185 if( op->o_conn && op->o_conn->c_is_udp ) {
1186 if ( op->o_protocol != LDAP_VERSION2 ) {
1187 rc = ber_printf( ber, /*{*/ "N}" );
1191 if ( op->o_res_ber == NULL ) {
1192 rc = ber_printf( ber, /*{*/ "N}" );
1197 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1199 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1200 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1205 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1206 be_entry_release_rw( op, rs->sr_entry, 0 );
1207 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1208 rs->sr_entry = NULL;
1211 if ( op->o_res_ber == NULL ) {
1212 bytes = send_ldap_ber( op->o_conn, ber );
1213 ber_free_buf( ber );
1216 Debug( LDAP_DEBUG_ANY,
1217 "send_search_entry: conn %lu ber write failed.\n",
1218 op->o_connid, 0, 0 );
1225 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1226 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1227 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1228 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1229 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1232 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1233 op->o_log_prefix, edn, 0, 0, 0 );
1235 Debug( LDAP_DEBUG_TRACE,
1236 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1241 if ( op->o_callback ) {
1243 slap_callback *sc = op->o_callback, *sc_next;
1245 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1246 sc_next = op->o_callback->sc_next;
1247 if ( op->o_callback->sc_cleanup ) {
1248 (void)op->o_callback->sc_cleanup( op, rs );
1249 if ( first && op->o_callback != sc ) {
1250 sc = op->o_callback;
1255 op->o_callback = sc;
1259 slap_sl_free( e_flags, op->o_tmpmemctx );
1262 if ( rs->sr_operational_attrs ) {
1263 attrs_free( rs->sr_operational_attrs );
1264 rs->sr_operational_attrs = NULL;
1266 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1268 /* FIXME: I think rs->sr_type should be explicitly set to
1269 * REP_SEARCH here. That's what it was when we entered this
1270 * function. send_ldap_error may have changed it, but we
1271 * should set it back so that the cleanup functions know
1272 * what they're doing.
1274 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1276 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1278 entry_free( rs->sr_entry );
1279 rs->sr_entry = NULL;
1280 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1287 slap_send_search_reference( Operation *op, SlapReply *rs )
1289 BerElementBuffer berbuf;
1290 BerElement *ber = (BerElement *) &berbuf;
1294 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1295 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1297 rs->sr_type = REP_SEARCHREF;
1298 if ( op->o_callback ) {
1300 slap_callback *sc = op->o_callback,
1301 *sc_next = op->o_callback;
1303 rc = SLAP_CB_CONTINUE;
1304 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1305 sc_next = op->o_callback->sc_next;
1306 if ( op->o_callback->sc_response ) {
1307 rc = op->o_callback->sc_response( op, rs );
1308 if ( first && op->o_callback == NULL ) {
1311 if ( rc != SLAP_CB_CONTINUE ) break;
1316 op->o_callback = sc;
1317 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1320 Debug( LDAP_DEBUG_TRACE,
1321 "=> send_search_reference: dn=\"%s\"\n",
1322 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1324 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1325 ad_entry, NULL, ACL_READ, NULL ) )
1327 Debug( LDAP_DEBUG_ACL,
1328 "send_search_reference: access to entry not allowed\n",
1334 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1335 ad_ref, NULL, ACL_READ, NULL ) )
1337 Debug( LDAP_DEBUG_ACL,
1338 "send_search_reference: access "
1339 "to reference not allowed\n",
1345 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1346 if( op->o_domain_scope ) {
1347 Debug( LDAP_DEBUG_ANY,
1348 "send_search_reference: domainScope control in (%s)\n",
1349 rs->sr_entry->e_dn, 0, 0 );
1355 if( rs->sr_ref == NULL ) {
1356 Debug( LDAP_DEBUG_ANY,
1357 "send_search_reference: null ref in (%s)\n",
1358 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1363 if( op->o_protocol < LDAP_VERSION3 ) {
1365 /* save the references for the result */
1366 if( rs->sr_ref[0].bv_val != NULL ) {
1367 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1373 #ifdef LDAP_CONNECTIONLESS
1374 if( op->o_conn && op->o_conn->c_is_udp ) {
1375 ber = op->o_res_ber;
1379 ber_init_w_nullc( ber, LBER_USE_DER );
1380 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1383 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1384 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1387 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1391 rc = ber_printf( ber, /*"{"*/ "N}" );
1395 Debug( LDAP_DEBUG_ANY,
1396 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1398 #ifdef LDAP_CONNECTIONLESS
1399 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1401 ber_free_buf( ber );
1402 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1406 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1407 be_entry_release_rw( op, rs->sr_entry, 0 );
1408 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1409 rs->sr_entry = NULL;
1412 #ifdef LDAP_CONNECTIONLESS
1413 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1415 bytes = send_ldap_ber( op->o_conn, ber );
1416 ber_free_buf( ber );
1418 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1419 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1420 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1421 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1422 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1423 #ifdef LDAP_CONNECTIONLESS
1427 Statslog( LDAP_DEBUG_STATS2, "%s REF dn=\"%s\"\n",
1428 op->o_log_prefix, rs->sr_entry ? rs->sr_entry->e_dn : "(null)",
1431 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1434 if ( op->o_callback ) {
1436 slap_callback *sc = op->o_callback, *sc_next;
1438 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1439 sc_next = op->o_callback->sc_next;
1440 if ( op->o_callback->sc_cleanup ) {
1441 (void)op->o_callback->sc_cleanup( op, rs );
1442 if ( first && op->o_callback != sc ) {
1443 sc = op->o_callback;
1448 op->o_callback = sc;
1464 *code = LDAP_SUCCESS;
1468 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1469 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1476 while ( (s = strchr( s, '\n' )) != NULL ) {
1481 if ( (c = strchr( s, ':' )) != NULL ) {
1485 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1489 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1493 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1498 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1508 int slap_read_controls(
1512 const struct berval *oid,
1513 LDAPControl **ctrl )
1517 BerElementBuffer berbuf;
1518 BerElement *ber = (BerElement *) &berbuf;
1523 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1524 oid->bv_val, e->e_dn, 0 );
1527 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1528 op->o_preread_attrs : op->o_postread_attrs;
1530 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1531 bv.bv_len = siz + len;
1532 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1534 ber_init2( ber, &bv, LBER_USE_DER );
1535 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1537 /* create new operation */
1540 myop.o_res_ber = ber;
1542 rc = slap_send_search_entry( &myop, rs );
1545 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1547 if( rc == LBER_ERROR ) return LDAP_OTHER;
1549 c.ldctl_oid = oid->bv_val;
1550 c.ldctl_iscritical = 0;
1552 if ( ctrl == NULL ) {
1554 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1556 /* retry: free previous try */
1557 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1561 return LDAP_SUCCESS;
1564 /* Map API errors to protocol errors... */
1566 slap_map_api2result( SlapReply *rs )
1568 switch(rs->sr_err) {
1569 case LDAP_SERVER_DOWN:
1570 return LDAP_UNAVAILABLE;
1571 case LDAP_LOCAL_ERROR:
1573 case LDAP_ENCODING_ERROR:
1574 case LDAP_DECODING_ERROR:
1575 return LDAP_PROTOCOL_ERROR;
1577 return LDAP_UNAVAILABLE;
1578 case LDAP_AUTH_UNKNOWN:
1579 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1580 case LDAP_FILTER_ERROR:
1581 rs->sr_text = "Filter error";
1583 case LDAP_USER_CANCELLED:
1584 rs->sr_text = "User cancelled";
1586 case LDAP_PARAM_ERROR:
1587 return LDAP_PROTOCOL_ERROR;
1588 case LDAP_NO_MEMORY:
1590 case LDAP_CONNECT_ERROR:
1591 return LDAP_UNAVAILABLE;
1592 case LDAP_NOT_SUPPORTED:
1593 return LDAP_UNWILLING_TO_PERFORM;
1594 case LDAP_CONTROL_NOT_FOUND:
1595 return LDAP_PROTOCOL_ERROR;
1596 case LDAP_NO_RESULTS_RETURNED:
1597 return LDAP_NO_SUCH_OBJECT;
1598 case LDAP_MORE_RESULTS_TO_RETURN:
1599 rs->sr_text = "More results to return";
1601 case LDAP_CLIENT_LOOP:
1602 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1603 return LDAP_LOOP_DETECT;
1605 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1612 slap_attr_flags( AttributeName *an )
1614 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1617 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1620 flags |= an_find( an, &AllOper )
1621 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1622 flags |= an_find( an, &AllUser )
1623 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;