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-2006 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 const struct berval slap_dummy_bv = BER_BVNULL;
43 int slap_null_cb( Operation *op, SlapReply *rs )
48 int slap_freeself_cb( Operation *op, SlapReply *rs )
50 assert( op->o_callback != NULL );
52 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
53 op->o_callback = NULL;
55 return SLAP_CB_CONTINUE;
58 int slap_replog_cb( Operation *op, SlapReply *rs )
60 if ( rs->sr_err == LDAP_SUCCESS ) {
63 return SLAP_CB_CONTINUE;
66 static char *v2ref( BerVarray ref, const char *text )
68 size_t len = 0, i = 0;
73 return ch_strdup(text);
81 if (text[len-1] != '\n') {
86 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
88 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
98 strcpy( v2+len, "Referral:" );
99 len += sizeof("Referral:");
101 for( i=0; ref[i].bv_val != NULL; i++ ) {
102 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
104 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
108 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
109 len += ref[i].bv_len;
110 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
120 slap_req2res( ber_tag_t tag )
125 case LDAP_REQ_COMPARE:
126 case LDAP_REQ_EXTENDED:
127 case LDAP_REQ_MODIFY:
128 case LDAP_REQ_MODRDN:
132 case LDAP_REQ_DELETE:
133 tag = LDAP_RES_DELETE;
136 case LDAP_REQ_ABANDON:
137 case LDAP_REQ_UNBIND:
141 case LDAP_REQ_SEARCH:
142 tag = LDAP_RES_SEARCH_RESULT;
152 static long send_ldap_ber(
158 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
160 /* write only one pdu at a time - wait til it's our turn */
161 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
163 /* lock the connection */
164 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
171 if ( connection_state_closing( conn ) ) {
172 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
173 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
178 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
185 * we got an error. if it's ewouldblock, we need to
186 * wait on the socket being writable. otherwise, figure
187 * it's a hard error and return.
190 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
191 err, sock_errstr(err), 0 );
193 if ( err != EWOULDBLOCK && err != EAGAIN ) {
194 connection_closing( conn, "connection lost on write" );
196 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
197 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
202 /* wait for socket to be write-ready */
203 conn->c_writewaiter = 1;
204 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
205 slapd_set_write( sd, 1 );
207 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
208 conn->c_writewaiter = 0;
211 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
212 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
218 send_ldap_control( BerElement *ber, LDAPControl *c )
224 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
226 if( c->ldctl_iscritical ) {
227 rc = ber_printf( ber, "b",
228 (ber_int_t) c->ldctl_iscritical ) ;
229 if( rc == -1 ) return rc;
232 if( c->ldctl_value.bv_val != NULL ) {
233 rc = ber_printf( ber, "O", &c->ldctl_value );
234 if( rc == -1 ) return rc;
237 rc = ber_printf( ber, /*{*/"N}" );
238 if( rc == -1 ) return rc;
244 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
251 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
252 if( rc == -1 ) return rc;
254 for( ; *c != NULL; c++) {
255 rc = send_ldap_control( ber, *c );
256 if( rc == -1 ) return rc;
260 /* this is a hack to avoid having to modify op->s_ctrls */
261 if( o->o_sortedresults ) {
262 BerElementBuffer berbuf;
263 BerElement *sber = (BerElement *) &berbuf;
265 BER_BVZERO( &sorted.ldctl_value );
266 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
267 sorted.ldctl_iscritical = 0;
269 ber_init2( sber, NULL, LBER_USE_DER );
271 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
273 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
277 (void) ber_free_buf( ber );
279 rc = send_ldap_control( ber, &sorted );
280 if( rc == -1 ) return rc;
284 rc = ber_printf( ber, /*{*/"N}" );
297 slap_callback *sc = op->o_callback, **sc_prev;
299 rc = SLAP_CB_CONTINUE;
300 for ( sc_prev = ≻ *sc_prev; ) {
301 slap_callback **sc_next = &(*sc_prev)->sc_next;
303 op->o_callback = *sc_prev;
304 if ( op->o_callback->sc_response ) {
305 rc = op->o_callback->sc_response( op, rs );
306 if ( op->o_callback == NULL ) {
307 /* the callback has been removed; repair the list */
310 } else if ( op->o_callback != *sc_prev ) {
311 /* a new callback has been inserted; repair the list */
312 *sc_next = op->o_callback;
313 sc_next = &op->o_callback;
315 if ( rc != SLAP_CB_CONTINUE ) break;
322 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
324 rc = SLAP_CB_CONTINUE;
325 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
326 sc_next = op->o_callback->sc_next;
327 if ( op->o_callback->sc_response ) {
328 slap_callback *sc2 = op->o_callback;
329 rc = op->o_callback->sc_response( op, rs );
330 if ( op->o_callback != sc2 ) {
331 *sc_prev = op->o_callback;
333 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
334 if ( op->o_callback != sc2 ) continue;
336 sc_prev = &op->o_callback->sc_next;
340 #endif /* ! NEW_CB */
351 slap_callback *sc = op->o_callback, **sc_prev;
353 for ( sc_prev = ≻ *sc_prev; ) {
354 slap_callback **sc_next = &(*sc_prev)->sc_next;
356 op->o_callback = *sc_prev;
357 if ( op->o_callback->sc_cleanup ) {
358 (void)op->o_callback->sc_cleanup( op, rs );
359 if ( op->o_callback == NULL ) {
360 /* the callback has been removed; repair the list */
363 } else if ( op->o_callback != *sc_prev ) {
364 /* a new callback has been inserted; repair the list */
365 *sc_next = op->o_callback;
366 sc_next = &op->o_callback;
368 /* don't care about the result; do all cleanup */
375 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
377 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
378 sc_next = op->o_callback->sc_next;
379 if ( op->o_callback->sc_cleanup ) {
380 slap_callback *sc2 = op->o_callback;
381 (void)op->o_callback->sc_cleanup( op, rs );
382 if ( op->o_callback != sc2 ) {
383 *sc_prev = op->o_callback;
385 if ( !op->o_callback ) break;
386 if ( op->o_callback != sc2 ) continue;
388 sc_prev = &op->o_callback->sc_next;
392 #endif /* ! NEW_CB */
402 BerElementBuffer berbuf;
403 BerElement *ber = (BerElement *) &berbuf;
404 int rc = LDAP_SUCCESS;
407 if ( rs->sr_err == SLAPD_ABANDON ) {
412 if ( op->o_callback ) {
413 rc = slap_response_loop( op, rs );
414 if ( rc != SLAP_CB_CONTINUE ) {
419 #ifdef LDAP_CONNECTIONLESS
420 if (op->o_conn && op->o_conn->c_is_udp)
425 ber_init_w_nullc( ber, LBER_USE_DER );
426 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
429 Debug( LDAP_DEBUG_TRACE,
430 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
431 rs->sr_msgid, rs->sr_tag, rs->sr_err );
434 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
435 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
439 #ifdef LDAP_CONNECTIONLESS
440 if (op->o_conn && op->o_conn->c_is_udp &&
441 op->o_protocol == LDAP_VERSION2 )
443 rc = ber_printf( ber, "t{ess" /*"}"*/,
444 rs->sr_tag, rs->sr_err,
445 rs->sr_matched == NULL ? "" : rs->sr_matched,
446 rs->sr_text == NULL ? "" : rs->sr_text );
449 if ( rs->sr_type == REP_INTERMEDIATE ) {
450 rc = ber_printf( ber, "{it{" /*"}}"*/,
451 rs->sr_msgid, rs->sr_tag );
454 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
455 rs->sr_msgid, rs->sr_tag, rs->sr_err,
456 rs->sr_matched == NULL ? "" : rs->sr_matched,
457 rs->sr_text == NULL ? "" : rs->sr_text );
461 if ( rs->sr_ref != NULL ) {
462 assert( rs->sr_err == LDAP_REFERRAL );
463 rc = ber_printf( ber, "t{W}",
464 LDAP_TAG_REFERRAL, rs->sr_ref );
466 assert( rs->sr_err != LDAP_REFERRAL );
470 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
471 rc = ber_printf( ber, "tO",
472 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
476 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
478 if ( rs->sr_rspoid != NULL ) {
479 rc = ber_printf( ber, "ts",
480 rs->sr_type == REP_EXTENDED
481 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
484 if( rc != -1 && rs->sr_rspdata != NULL ) {
485 rc = ber_printf( ber, "tO",
486 rs->sr_type == REP_EXTENDED
487 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
493 rc = ber_printf( ber, /*"{"*/ "N}" );
497 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
501 rc = ber_printf( ber, /*"{"*/ "N}" );
504 #ifdef LDAP_CONNECTIONLESS
505 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
508 rc = ber_printf( ber, /*"{"*/ "N}" );
513 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
515 #ifdef LDAP_CONNECTIONLESS
516 if (!op->o_conn || op->o_conn->c_is_udp == 0)
525 bytes = send_ldap_ber( op->o_conn, ber );
526 #ifdef LDAP_CONNECTIONLESS
527 if (!op->o_conn || op->o_conn->c_is_udp == 0)
534 Debug( LDAP_DEBUG_ANY,
535 "send_ldap_response: ber write failed\n",
541 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
542 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
543 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
544 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
547 /* Tell caller that we did this for real, as opposed to being
548 * overridden by a callback
550 rc = SLAP_CB_CONTINUE;
553 if ( op->o_callback ) {
554 (void)slap_cleanup_loop( op, rs );
557 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
558 free( (char *)rs->sr_matched );
559 rs->sr_matched = NULL;
562 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
563 ber_bvarray_free( rs->sr_ref );
572 send_ldap_disconnect( Operation *op, SlapReply *rs )
574 #define LDAP_UNSOLICITED_ERROR(e) \
575 ( (e) == LDAP_PROTOCOL_ERROR \
576 || (e) == LDAP_STRONG_AUTH_REQUIRED \
577 || (e) == LDAP_UNAVAILABLE )
579 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
581 rs->sr_type = REP_EXTENDED;
583 Debug( LDAP_DEBUG_TRACE,
584 "send_ldap_disconnect %d:%s\n",
585 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
587 if ( op->o_protocol < LDAP_VERSION3 ) {
588 rs->sr_rspoid = NULL;
589 rs->sr_tag = slap_req2res( op->o_tag );
590 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
593 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
594 rs->sr_tag = LDAP_RES_EXTENDED;
598 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
599 Statslog( LDAP_DEBUG_STATS,
600 "%s DISCONNECT tag=%lu err=%d text=%s\n",
601 op->o_log_prefix, rs->sr_tag, rs->sr_err,
602 rs->sr_text ? rs->sr_text : "", 0 );
607 slap_send_ldap_result( Operation *op, SlapReply *rs )
610 const char *otext = rs->sr_text;
611 BerVarray oref = rs->sr_ref;
613 rs->sr_type = REP_RESULT;
615 /* Propagate Abandons so that cleanup callbacks can be processed */
616 if ( rs->sr_err == SLAPD_ABANDON )
619 assert( !LDAP_API_ERROR( rs->sr_err ) );
621 Debug( LDAP_DEBUG_TRACE,
622 "send_ldap_result: %s p=%d\n",
623 op->o_log_prefix, op->o_protocol, 0 );
625 Debug( LDAP_DEBUG_ARGS,
626 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
627 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
628 rs->sr_text ? rs->sr_text : "" );
632 Debug( LDAP_DEBUG_ARGS,
633 "send_ldap_result: referral=\"%s\"\n",
634 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
638 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
640 if ( rs->sr_err == LDAP_REFERRAL ) {
641 if( op->o_domain_scope ) rs->sr_ref = NULL;
643 if( rs->sr_ref == NULL ) {
644 rs->sr_err = LDAP_NO_SUCH_OBJECT;
645 } else if ( op->o_protocol < LDAP_VERSION3 ) {
646 rs->sr_err = LDAP_PARTIAL_RESULTS;
650 if ( op->o_protocol < LDAP_VERSION3 ) {
651 tmp = v2ref( rs->sr_ref, rs->sr_text );
656 rs->sr_tag = slap_req2res( op->o_tag );
657 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
660 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
661 if ( op->o_tag == LDAP_REQ_SEARCH ) {
663 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
664 rs->sr_err, rs->sr_nentries );
666 Statslog( LDAP_DEBUG_STATS,
667 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
668 op->o_log_prefix, rs->sr_tag, nbuf,
669 rs->sr_text ? rs->sr_text : "", 0 );
671 Statslog( LDAP_DEBUG_STATS,
672 "%s RESULT tag=%lu err=%d text=%s\n",
673 op->o_log_prefix, rs->sr_tag, rs->sr_err,
674 rs->sr_text ? rs->sr_text : "", 0 );
678 if( tmp != NULL ) ch_free(tmp);
684 send_ldap_sasl( Operation *op, SlapReply *rs )
686 rs->sr_type = REP_SASL;
687 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
689 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
691 rs->sr_tag = slap_req2res( op->o_tag );
692 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
694 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
695 Statslog( LDAP_DEBUG_STATS,
696 "%s RESULT tag=%lu err=%d text=%s\n",
697 op->o_log_prefix, rs->sr_tag, rs->sr_err,
698 rs->sr_text ? rs->sr_text : "", 0 );
703 slap_send_ldap_extended( Operation *op, SlapReply *rs )
705 rs->sr_type = REP_EXTENDED;
707 Debug( LDAP_DEBUG_TRACE,
708 "send_ldap_extended: err=%d oid=%s len=%ld\n",
710 rs->sr_rspoid ? rs->sr_rspoid : "",
711 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
713 rs->sr_tag = slap_req2res( op->o_tag );
714 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
716 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
717 Statslog( LDAP_DEBUG_STATS,
718 "%s RESULT oid=%s err=%d text=%s\n",
719 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
720 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
725 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
727 rs->sr_type = REP_INTERMEDIATE;
728 Debug( LDAP_DEBUG_TRACE,
729 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
731 rs->sr_rspoid ? rs->sr_rspoid : "",
732 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
733 rs->sr_tag = LDAP_RES_INTERMEDIATE;
734 rs->sr_msgid = op->o_msgid;
735 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
736 Statslog( LDAP_DEBUG_STATS2,
737 "%s INTERM oid=%s\n",
739 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
746 * LDAP_SUCCESS entry sent
747 * LDAP_OTHER entry not sent (other)
748 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
749 * LDAP_UNAVAILABLE entry not sent (connection closed)
750 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
754 slap_send_search_entry( Operation *op, SlapReply *rs )
756 BerElementBuffer berbuf;
757 BerElement *ber = (BerElement *) &berbuf;
759 int i, j, rc = LDAP_UNAVAILABLE, bytes;
762 AccessControlState acl_state = ACL_STATE_INIT;
764 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
766 /* a_flags: array of flags telling if the i-th element will be
767 * returned or filtered out
768 * e_flags: array of a_flags
770 char **e_flags = NULL;
772 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
773 return LDAP_SIZELIMIT_EXCEEDED;
776 rs->sr_type = REP_SEARCH;
778 /* eventually will loop through generated operational attribute types
779 * currently implemented types include:
780 * entryDN, subschemaSubentry, and hasSubordinates */
781 /* NOTE: moved before overlays callback circling because
782 * they may modify entry and other stuff in rs */
783 /* check for special all operational attributes ("+") type */
784 /* FIXME: maybe we could set this flag at the operation level;
785 * however, in principle the caller of send_search_entry() may
786 * change the attribute list at each call */
787 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
789 rc = backend_operational( op, rs );
794 if ( op->o_callback ) {
795 rc = slap_response_loop( op, rs );
796 if ( rc != SLAP_CB_CONTINUE ) {
801 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
802 op->o_connid, rs->sr_entry->e_name.bv_val,
803 op->ors_attrsonly ? " (attrsOnly)" : "" );
805 attrsonly = op->ors_attrsonly;
807 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
808 Debug( LDAP_DEBUG_ACL,
809 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
810 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
812 rc = LDAP_INSUFFICIENT_ACCESS;
816 edn = rs->sr_entry->e_nname.bv_val;
818 if ( op->o_res_ber ) {
819 /* read back control or LDAP_CONNECTIONLESS */
824 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
825 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
827 ber_init2( ber, &bv, LBER_USE_DER );
828 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
831 #ifdef LDAP_CONNECTIONLESS
832 if ( op->o_conn && op->o_conn->c_is_udp ) {
834 if ( op->o_protocol == LDAP_VERSION2 ) {
835 rc = ber_printf(ber, "t{O{" /*}}*/,
836 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
838 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
839 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
843 if ( op->o_res_ber ) {
844 /* read back control */
845 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
847 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
848 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
852 Debug( LDAP_DEBUG_ANY,
853 "send_search_entry: conn %lu ber_printf failed\n",
854 op->o_connid, 0, 0 );
856 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
857 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
862 /* check for special all user attributes ("*") type */
863 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
865 /* create an array of arrays of flags. Each flag corresponds
866 * to particular value of attribute and equals 1 if value matches
867 * to ValuesReturnFilter or 0 if not
869 if ( op->o_vrFilter != NULL ) {
873 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
874 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
877 size = i * sizeof(char *) + k;
880 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
881 if( e_flags == NULL ) {
882 Debug( LDAP_DEBUG_ANY,
883 "send_search_entry: conn %lu slap_sl_calloc failed\n",
884 op->o_connid ? op->o_connid : 0, 0, 0 );
887 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
890 a_flags = (char *)(e_flags + i);
891 memset( a_flags, 0, k );
892 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
893 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
894 e_flags[i] = a_flags;
898 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
900 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
901 "conn %lu matched values filtering failed\n",
902 op->o_connid ? op->o_connid : 0, 0, 0 );
903 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
904 send_ldap_error( op, rs, LDAP_OTHER,
905 "matched values filtering error" );
912 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
913 AttributeDescription *desc = a->a_desc;
916 if ( rs->sr_attrs == NULL ) {
917 /* all user attrs request, skip operational attributes */
918 if( is_at_operational( desc->ad_type ) ) {
923 /* specific attrs requested */
924 if ( is_at_operational( desc->ad_type ) ) {
925 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
926 !ad_inlist( desc, rs->sr_attrs ) )
932 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
939 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
940 ACL_READ, &acl_state ) )
942 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
943 "conn %lu access to attribute %s not allowed\n",
944 op->o_connid, desc->ad_cname.bv_val, 0 );
948 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
949 Debug( LDAP_DEBUG_ANY,
950 "send_search_entry: conn %lu ber_printf failed\n",
951 op->o_connid, 0, 0 );
953 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
954 send_ldap_error( op, rs, LDAP_OTHER,
955 "encoding description error");
963 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
964 if ( ! access_allowed( op, rs->sr_entry,
965 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
967 Debug( LDAP_DEBUG_ACL,
968 "send_search_entry: conn %lu "
969 "access to attribute %s, value #%d not allowed\n",
970 op->o_connid, desc->ad_cname.bv_val, i );
975 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
982 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
983 Debug( LDAP_DEBUG_ANY,
984 "send_search_entry: conn %lu ber_printf failed\n",
985 op->o_connid, 0, 0 );
987 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
988 send_ldap_error( op, rs, LDAP_OTHER,
989 "encoding description error");
994 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
995 Debug( LDAP_DEBUG_ANY,
996 "send_search_entry: conn %lu "
997 "ber_printf failed.\n", op->o_connid, 0, 0 );
999 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1000 send_ldap_error( op, rs, LDAP_OTHER,
1001 "encoding values error" );
1008 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1009 Debug( LDAP_DEBUG_ANY,
1010 "send_search_entry: conn %lu ber_printf failed\n",
1011 op->o_connid, 0, 0 );
1013 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1014 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1020 /* NOTE: moved before overlays callback circling because
1021 * they may modify entry and other stuff in rs */
1022 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1026 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1027 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1030 size = i * sizeof(char *) + k;
1032 char *a_flags, **tmp;
1035 * Reuse previous memory - we likely need less space
1036 * for operational attributes
1038 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1040 if ( tmp == NULL ) {
1041 Debug( LDAP_DEBUG_ANY,
1042 "send_search_entry: conn %lu "
1043 "not enough memory "
1044 "for matched values filtering\n",
1045 op->o_connid, 0, 0 );
1046 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1047 send_ldap_error( op, rs, LDAP_OTHER,
1048 "not enough memory for matched values filtering" );
1052 a_flags = (char *)(e_flags + i);
1053 memset( a_flags, 0, k );
1054 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1055 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1056 e_flags[i] = a_flags;
1059 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1062 Debug( LDAP_DEBUG_ANY,
1063 "send_search_entry: conn %lu "
1064 "matched values filtering failed\n",
1065 op->o_connid ? op->o_connid : 0, 0, 0);
1066 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1067 send_ldap_error( op, rs, LDAP_OTHER,
1068 "matched values filtering error" );
1075 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1076 AttributeDescription *desc = a->a_desc;
1078 if ( rs->sr_attrs == NULL ) {
1079 /* all user attrs request, skip operational attributes */
1080 if( is_at_operational( desc->ad_type ) ) {
1085 /* specific attrs requested */
1086 if( is_at_operational( desc->ad_type ) ) {
1087 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1088 !ad_inlist( desc, rs->sr_attrs ) )
1093 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1099 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1100 ACL_READ, &acl_state ) )
1102 Debug( LDAP_DEBUG_ACL,
1103 "send_search_entry: conn %lu "
1104 "access to attribute %s not allowed\n",
1105 op->o_connid, desc->ad_cname.bv_val, 0 );
1110 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1112 Debug( LDAP_DEBUG_ANY,
1113 "send_search_entry: conn %lu "
1114 "ber_printf failed\n", op->o_connid, 0, 0 );
1116 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1117 send_ldap_error( op, rs, LDAP_OTHER,
1118 "encoding description error" );
1123 if ( ! attrsonly ) {
1124 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1125 if ( ! access_allowed( op, rs->sr_entry,
1126 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1128 Debug( LDAP_DEBUG_ACL,
1129 "send_search_entry: conn %lu "
1130 "access to %s, value %d not allowed\n",
1131 op->o_connid, desc->ad_cname.bv_val, i );
1136 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1140 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1141 Debug( LDAP_DEBUG_ANY,
1142 "send_search_entry: conn %lu ber_printf failed\n",
1143 op->o_connid, 0, 0 );
1145 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1146 send_ldap_error( op, rs, LDAP_OTHER,
1147 "encoding values error" );
1154 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1155 Debug( LDAP_DEBUG_ANY,
1156 "send_search_entry: conn %lu ber_printf failed\n",
1157 op->o_connid, 0, 0 );
1159 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1160 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1168 slap_sl_free( e_flags, op->o_tmpmemctx );
1172 rc = ber_printf( ber, /*{{*/ "}N}" );
1175 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1179 #ifdef LDAP_CONNECTIONLESS
1180 if( op->o_conn && op->o_conn->c_is_udp ) {
1181 if ( op->o_protocol != LDAP_VERSION2 ) {
1182 rc = ber_printf( ber, /*{*/ "N}" );
1186 if ( op->o_res_ber == NULL ) {
1187 rc = ber_printf( ber, /*{*/ "N}" );
1192 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1194 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1195 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1200 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1201 be_entry_release_rw( op, rs->sr_entry, 0 );
1202 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1203 rs->sr_entry = NULL;
1206 if ( op->o_res_ber == NULL ) {
1207 bytes = send_ldap_ber( op->o_conn, ber );
1208 ber_free_buf( ber );
1211 Debug( LDAP_DEBUG_ANY,
1212 "send_search_entry: conn %lu ber write failed.\n",
1213 op->o_connid, 0, 0 );
1215 rc = LDAP_UNAVAILABLE;
1220 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1221 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1222 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1223 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1224 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1227 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1228 op->o_log_prefix, edn, 0, 0, 0 );
1230 Debug( LDAP_DEBUG_TRACE,
1231 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1236 if ( op->o_callback ) {
1237 (void)slap_cleanup_loop( op, rs );
1241 slap_sl_free( e_flags, op->o_tmpmemctx );
1244 if ( rs->sr_operational_attrs ) {
1245 attrs_free( rs->sr_operational_attrs );
1246 rs->sr_operational_attrs = NULL;
1248 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1250 /* FIXME: I think rs->sr_type should be explicitly set to
1251 * REP_SEARCH here. That's what it was when we entered this
1252 * function. send_ldap_error may have changed it, but we
1253 * should set it back so that the cleanup functions know
1254 * what they're doing.
1256 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1258 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1260 entry_free( rs->sr_entry );
1261 rs->sr_entry = NULL;
1262 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1269 slap_send_search_reference( Operation *op, SlapReply *rs )
1271 BerElementBuffer berbuf;
1272 BerElement *ber = (BerElement *) &berbuf;
1276 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1277 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1279 rs->sr_type = REP_SEARCHREF;
1280 if ( op->o_callback ) {
1281 rc = slap_response_loop( op, rs );
1282 if ( rc != SLAP_CB_CONTINUE ) {
1287 Debug( LDAP_DEBUG_TRACE,
1288 "=> send_search_reference: dn=\"%s\"\n",
1289 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1291 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1292 ad_entry, NULL, ACL_READ, NULL ) )
1294 Debug( LDAP_DEBUG_ACL,
1295 "send_search_reference: access to entry not allowed\n",
1301 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1302 ad_ref, NULL, ACL_READ, NULL ) )
1304 Debug( LDAP_DEBUG_ACL,
1305 "send_search_reference: access "
1306 "to reference not allowed\n",
1312 if( op->o_domain_scope ) {
1313 Debug( LDAP_DEBUG_ANY,
1314 "send_search_reference: domainScope control in (%s)\n",
1315 rs->sr_entry->e_dn, 0, 0 );
1320 if( rs->sr_ref == NULL ) {
1321 Debug( LDAP_DEBUG_ANY,
1322 "send_search_reference: null ref in (%s)\n",
1323 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1328 if( op->o_protocol < LDAP_VERSION3 ) {
1330 /* save the references for the result */
1331 if( rs->sr_ref[0].bv_val != NULL ) {
1332 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1338 #ifdef LDAP_CONNECTIONLESS
1339 if( op->o_conn && op->o_conn->c_is_udp ) {
1340 ber = op->o_res_ber;
1344 ber_init_w_nullc( ber, LBER_USE_DER );
1345 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1348 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1349 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1352 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1356 rc = ber_printf( ber, /*"{"*/ "N}" );
1360 Debug( LDAP_DEBUG_ANY,
1361 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1363 #ifdef LDAP_CONNECTIONLESS
1364 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1366 ber_free_buf( ber );
1367 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1372 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1373 be_entry_release_rw( op, rs->sr_entry, 0 );
1374 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1375 rs->sr_entry = NULL;
1378 #ifdef LDAP_CONNECTIONLESS
1379 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1381 bytes = send_ldap_ber( op->o_conn, ber );
1382 ber_free_buf( ber );
1384 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1385 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1386 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1387 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1388 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1389 #ifdef LDAP_CONNECTIONLESS
1392 if ( rs->sr_ref != NULL ) {
1395 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1396 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1397 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1402 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1403 op->o_log_prefix, 0, 0, 0, 0 );
1406 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1409 if ( op->o_callback ) {
1410 (void)slap_cleanup_loop( op, rs );
1426 *code = LDAP_SUCCESS;
1430 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1431 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1438 while ( (s = strchr( s, '\n' )) != NULL ) {
1443 if ( (c = strchr( s, ':' )) != NULL ) {
1447 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1448 if ( c != NULL && lutil_atoi( code, c ) != 0 ) {
1451 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1455 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1461 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1471 int slap_read_controls(
1475 const struct berval *oid,
1476 LDAPControl **ctrl )
1480 BerElementBuffer berbuf;
1481 BerElement *ber = (BerElement *) &berbuf;
1485 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1486 oid->bv_val, e->e_dn, 0 );
1489 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1490 op->o_preread_attrs : op->o_postread_attrs;
1492 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1493 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1495 ber_init2( ber, &bv, LBER_USE_DER );
1496 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1498 /* create new operation */
1501 myop.o_res_ber = ber;
1502 myop.o_callback = NULL;
1503 myop.ors_slimit = 1;
1505 rc = slap_send_search_entry( &myop, rs );
1508 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1510 if( rc == LBER_ERROR ) return LDAP_OTHER;
1512 c.ldctl_oid = oid->bv_val;
1513 c.ldctl_iscritical = 0;
1515 if ( *ctrl == NULL ) {
1517 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1519 /* retry: free previous try */
1520 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1524 return LDAP_SUCCESS;
1527 /* Map API errors to protocol errors... */
1529 slap_map_api2result( SlapReply *rs )
1531 switch(rs->sr_err) {
1532 case LDAP_SERVER_DOWN:
1533 return LDAP_UNAVAILABLE;
1534 case LDAP_LOCAL_ERROR:
1536 case LDAP_ENCODING_ERROR:
1537 case LDAP_DECODING_ERROR:
1538 return LDAP_PROTOCOL_ERROR;
1540 return LDAP_UNAVAILABLE;
1541 case LDAP_AUTH_UNKNOWN:
1542 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1543 case LDAP_FILTER_ERROR:
1544 rs->sr_text = "Filter error";
1546 case LDAP_USER_CANCELLED:
1547 rs->sr_text = "User cancelled";
1549 case LDAP_PARAM_ERROR:
1550 return LDAP_PROTOCOL_ERROR;
1551 case LDAP_NO_MEMORY:
1553 case LDAP_CONNECT_ERROR:
1554 return LDAP_UNAVAILABLE;
1555 case LDAP_NOT_SUPPORTED:
1556 return LDAP_UNWILLING_TO_PERFORM;
1557 case LDAP_CONTROL_NOT_FOUND:
1558 return LDAP_PROTOCOL_ERROR;
1559 case LDAP_NO_RESULTS_RETURNED:
1560 return LDAP_NO_SUCH_OBJECT;
1561 case LDAP_MORE_RESULTS_TO_RETURN:
1562 rs->sr_text = "More results to return";
1564 case LDAP_CLIENT_LOOP:
1565 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1566 return LDAP_LOOP_DETECT;
1568 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1575 slap_attr_flags( AttributeName *an )
1577 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1580 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1583 flags |= an_find( an, &AllOper )
1584 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1585 flags |= an_find( an, &AllUser )
1586 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;