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-2008 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>
40 const struct berval slap_dummy_bv = BER_BVNULL;
42 int slap_null_cb( Operation *op, SlapReply *rs )
47 int slap_freeself_cb( Operation *op, SlapReply *rs )
49 assert( op->o_callback != NULL );
51 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
52 op->o_callback = NULL;
54 return SLAP_CB_CONTINUE;
57 static char *v2ref( BerVarray ref, const char *text )
59 size_t len = 0, i = 0;
64 return ch_strdup(text);
72 if (text[len-1] != '\n') {
77 v2 = ch_malloc( len+i+sizeof("Referral:") );
85 strcpy( v2+len, "Referral:" );
86 len += sizeof("Referral:");
88 for( i=0; ref[i].bv_val != NULL; i++ ) {
89 v2 = ch_realloc( v2, len + ref[i].bv_len + 1 );
91 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
93 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
103 slap_req2res( ber_tag_t tag )
108 case LDAP_REQ_COMPARE:
109 case LDAP_REQ_EXTENDED:
110 case LDAP_REQ_MODIFY:
111 case LDAP_REQ_MODRDN:
115 case LDAP_REQ_DELETE:
116 tag = LDAP_RES_DELETE;
119 case LDAP_REQ_ABANDON:
120 case LDAP_REQ_UNBIND:
124 case LDAP_REQ_SEARCH:
125 tag = LDAP_RES_SEARCH_RESULT;
135 static long send_ldap_ber(
141 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
143 /* write only one pdu at a time - wait til it's our turn */
144 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
146 /* lock the connection */
147 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
153 if ( connection_state_closing( conn ) ) {
154 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
155 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
160 if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
167 * we got an error. if it's ewouldblock, we need to
168 * wait on the socket being writable. otherwise, figure
169 * it's a hard error and return.
172 Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
173 err, sock_errstr(err), 0 );
175 if ( err != EWOULDBLOCK && err != EAGAIN ) {
176 connection_closing( conn, "connection lost on write" );
178 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
179 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
184 /* wait for socket to be write-ready */
185 conn->c_writewaiter = 1;
186 slapd_set_write( conn->c_sd, 1 );
188 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
189 conn->c_writewaiter = 0;
192 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
193 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
199 send_ldap_control( BerElement *ber, LDAPControl *c )
205 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
207 if( c->ldctl_iscritical ) {
208 rc = ber_printf( ber, "b",
209 (ber_int_t) c->ldctl_iscritical ) ;
210 if( rc == -1 ) return rc;
213 if( c->ldctl_value.bv_val != NULL ) {
214 rc = ber_printf( ber, "O", &c->ldctl_value );
215 if( rc == -1 ) return rc;
218 rc = ber_printf( ber, /*{*/"N}" );
219 if( rc == -1 ) return rc;
225 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
232 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
233 if( rc == -1 ) return rc;
235 for( ; *c != NULL; c++) {
236 rc = send_ldap_control( ber, *c );
237 if( rc == -1 ) return rc;
240 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
241 /* this is a hack to avoid having to modify op->s_ctrls */
242 if( o->o_sortedresults ) {
243 BerElementBuffer berbuf;
244 BerElement *sber = (BerElement *) &berbuf;
246 BER_BVZERO( &sorted.ldctl_value );
247 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
248 sorted.ldctl_iscritical = 0;
250 ber_init2( sber, NULL, LBER_USE_DER );
252 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
254 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
258 (void) ber_free_buf( ber );
260 rc = send_ldap_control( ber, &sorted );
261 if( rc == -1 ) return rc;
265 rc = ber_printf( ber, /*{*/"N}" );
271 * slap_response_play()
273 * plays the callback list; rationale: a callback can
274 * - remove itself from the list, by setting op->o_callback = NULL;
275 * malloc()'ed callbacks should free themselves from inside the
276 * sc_response() function.
277 * - replace itself with another (list of) callback(s), by setting
278 * op->o_callback = a new (list of) callback(s); in this case, it
279 * is the callback's responsibility to to append existing subsequent
280 * callbacks to the end of the list that is passed to the sc_response()
282 * - modify the list of subsequent callbacks by modifying the value
283 * of the sc_next field from inside the sc_response() function; this
284 * case does not require any handling from inside slap_response_play()
286 * To stop execution of the playlist, the sc_response() function must return
287 * a value different from SLAP_SC_CONTINUE.
289 * The same applies to slap_cleanup_play(); only, there is no means to stop
290 * execution of the playlist, since all cleanup functions must be called.
299 slap_callback *sc = op->o_callback, **scp;
301 rc = SLAP_CB_CONTINUE;
302 for ( scp = ≻ *scp; ) {
303 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
305 op->o_callback = *scp;
306 if ( op->o_callback->sc_response ) {
307 rc = op->o_callback->sc_response( op, rs );
308 if ( op->o_callback == NULL ) {
309 /* the callback has been removed;
314 } else if ( op->o_callback != *scp ) {
315 /* a new callback has been inserted
316 * in place of the existing one; repair the list */
317 *scp = op->o_callback;
320 if ( rc != SLAP_CB_CONTINUE ) break;
334 slap_callback *sc = op->o_callback, **scp;
336 for ( scp = ≻ *scp; ) {
337 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
339 op->o_callback = *scp;
340 if ( op->o_callback->sc_cleanup ) {
341 (void)op->o_callback->sc_cleanup( op, rs );
342 if ( op->o_callback == NULL ) {
343 /* the callback has been removed;
348 } else if ( op->o_callback != *scp ) {
349 /* a new callback has been inserted
350 * after the existing one; repair the list */
351 /* a new callback has been inserted
352 * in place of the existing one; repair the list */
353 *scp = op->o_callback;
356 /* don't care about the result; do all cleanup */
370 BerElementBuffer berbuf;
371 BerElement *ber = (BerElement *) &berbuf;
372 int rc = LDAP_SUCCESS;
375 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon ) {
380 if ( op->o_callback ) {
381 rc = slap_response_play( op, rs );
382 if ( rc != SLAP_CB_CONTINUE ) {
387 #ifdef LDAP_CONNECTIONLESS
388 if (op->o_conn && op->o_conn->c_is_udp)
393 ber_init_w_nullc( ber, LBER_USE_DER );
394 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
397 Debug( LDAP_DEBUG_TRACE,
398 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
399 rs->sr_msgid, rs->sr_tag, rs->sr_err );
402 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
403 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
407 #ifdef LDAP_CONNECTIONLESS
408 if (op->o_conn && op->o_conn->c_is_udp &&
409 op->o_protocol == LDAP_VERSION2 )
411 rc = ber_printf( ber, "t{ess" /*"}"*/,
412 rs->sr_tag, rs->sr_err,
413 rs->sr_matched == NULL ? "" : rs->sr_matched,
414 rs->sr_text == NULL ? "" : rs->sr_text );
417 if ( rs->sr_type == REP_INTERMEDIATE ) {
418 rc = ber_printf( ber, "{it{" /*"}}"*/,
419 rs->sr_msgid, rs->sr_tag );
422 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
423 rs->sr_msgid, rs->sr_tag, rs->sr_err,
424 rs->sr_matched == NULL ? "" : rs->sr_matched,
425 rs->sr_text == NULL ? "" : rs->sr_text );
429 if ( rs->sr_ref != NULL ) {
430 assert( rs->sr_err == LDAP_REFERRAL );
431 rc = ber_printf( ber, "t{W}",
432 LDAP_TAG_REFERRAL, rs->sr_ref );
434 assert( rs->sr_err != LDAP_REFERRAL );
438 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
439 rc = ber_printf( ber, "tO",
440 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
444 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
446 if ( rs->sr_rspoid != NULL ) {
447 rc = ber_printf( ber, "ts",
448 rs->sr_type == REP_EXTENDED
449 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
452 if( rc != -1 && rs->sr_rspdata != NULL ) {
453 rc = ber_printf( ber, "tO",
454 rs->sr_type == REP_EXTENDED
455 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
461 rc = ber_printf( ber, /*"{"*/ "N}" );
465 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
469 rc = ber_printf( ber, /*"{"*/ "N}" );
472 #ifdef LDAP_CONNECTIONLESS
473 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
476 rc = ber_printf( ber, /*"{"*/ "N}" );
481 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
483 #ifdef LDAP_CONNECTIONLESS
484 if (!op->o_conn || op->o_conn->c_is_udp == 0)
493 bytes = send_ldap_ber( op->o_conn, ber );
494 #ifdef LDAP_CONNECTIONLESS
495 if (!op->o_conn || op->o_conn->c_is_udp == 0)
502 Debug( LDAP_DEBUG_ANY,
503 "send_ldap_response: ber write failed\n",
509 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
510 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
511 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
512 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
515 /* Tell caller that we did this for real, as opposed to being
516 * overridden by a callback
518 rc = SLAP_CB_CONTINUE;
521 if ( op->o_callback ) {
522 (void)slap_cleanup_play( op, rs );
525 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
526 rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
527 if ( rs->sr_matched ) {
528 free( (char *)rs->sr_matched );
529 rs->sr_matched = NULL;
533 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
534 rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
536 ber_bvarray_free( rs->sr_ref );
541 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
542 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
543 if ( rs->sr_ctrls ) {
544 slap_free_ctrls( op, rs->sr_ctrls );
554 send_ldap_disconnect( Operation *op, SlapReply *rs )
556 #define LDAP_UNSOLICITED_ERROR(e) \
557 ( (e) == LDAP_PROTOCOL_ERROR \
558 || (e) == LDAP_STRONG_AUTH_REQUIRED \
559 || (e) == LDAP_UNAVAILABLE )
561 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
563 rs->sr_type = REP_EXTENDED;
565 Debug( LDAP_DEBUG_TRACE,
566 "send_ldap_disconnect %d:%s\n",
567 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
569 if ( op->o_protocol < LDAP_VERSION3 ) {
570 rs->sr_rspoid = NULL;
571 rs->sr_tag = slap_req2res( op->o_tag );
572 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
575 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
576 rs->sr_tag = LDAP_RES_EXTENDED;
577 rs->sr_msgid = LDAP_RES_UNSOLICITED;
580 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
581 Statslog( LDAP_DEBUG_STATS,
582 "%s DISCONNECT tag=%lu err=%d text=%s\n",
583 op->o_log_prefix, rs->sr_tag, rs->sr_err,
584 rs->sr_text ? rs->sr_text : "", 0 );
589 slap_send_ldap_result( Operation *op, SlapReply *rs )
592 const char *otext = rs->sr_text;
593 BerVarray oref = rs->sr_ref;
595 rs->sr_type = REP_RESULT;
597 /* Propagate Abandons so that cleanup callbacks can be processed */
598 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
601 assert( !LDAP_API_ERROR( rs->sr_err ) );
603 Debug( LDAP_DEBUG_TRACE,
604 "send_ldap_result: %s p=%d\n",
605 op->o_log_prefix, op->o_protocol, 0 );
607 Debug( LDAP_DEBUG_ARGS,
608 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
609 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
610 rs->sr_text ? rs->sr_text : "" );
614 Debug( LDAP_DEBUG_ARGS,
615 "send_ldap_result: referral=\"%s\"\n",
616 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
620 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
622 if ( rs->sr_err == LDAP_REFERRAL ) {
623 if( op->o_domain_scope ) rs->sr_ref = NULL;
625 if( rs->sr_ref == NULL ) {
626 rs->sr_err = LDAP_NO_SUCH_OBJECT;
627 } else if ( op->o_protocol < LDAP_VERSION3 ) {
628 rs->sr_err = LDAP_PARTIAL_RESULTS;
632 if ( op->o_protocol < LDAP_VERSION3 ) {
633 tmp = v2ref( rs->sr_ref, rs->sr_text );
639 rs->sr_tag = slap_req2res( op->o_tag );
640 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
642 if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
643 if ( rs->sr_ref == NULL ) {
644 rs->sr_flags ^= REP_REF_MUSTBEFREED;
645 ber_bvarray_free( oref );
647 oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
650 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
651 if ( op->o_tag == LDAP_REQ_SEARCH ) {
652 Statslog( LDAP_DEBUG_STATS,
653 "%s SEARCH RESULT tag=%lu err=%d nentries=%d text=%s\n",
654 op->o_log_prefix, rs->sr_tag, rs->sr_err,
655 rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
657 Statslog( LDAP_DEBUG_STATS,
658 "%s RESULT tag=%lu err=%d text=%s\n",
659 op->o_log_prefix, rs->sr_tag, rs->sr_err,
660 rs->sr_text ? rs->sr_text : "", 0 );
664 if( tmp != NULL ) ch_free(tmp);
670 send_ldap_sasl( Operation *op, SlapReply *rs )
672 rs->sr_type = REP_SASL;
673 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
675 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
677 rs->sr_tag = slap_req2res( op->o_tag );
678 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
680 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
681 Statslog( LDAP_DEBUG_STATS,
682 "%s RESULT tag=%lu err=%d text=%s\n",
683 op->o_log_prefix, rs->sr_tag, rs->sr_err,
684 rs->sr_text ? rs->sr_text : "", 0 );
689 slap_send_ldap_extended( Operation *op, SlapReply *rs )
691 rs->sr_type = REP_EXTENDED;
693 Debug( LDAP_DEBUG_TRACE,
694 "send_ldap_extended: err=%d oid=%s len=%ld\n",
696 rs->sr_rspoid ? rs->sr_rspoid : "",
697 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
699 rs->sr_tag = slap_req2res( op->o_tag );
700 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
702 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
703 Statslog( LDAP_DEBUG_STATS,
704 "%s RESULT oid=%s err=%d text=%s\n",
705 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
706 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
711 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
713 rs->sr_type = REP_INTERMEDIATE;
714 Debug( LDAP_DEBUG_TRACE,
715 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
717 rs->sr_rspoid ? rs->sr_rspoid : "",
718 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
719 rs->sr_tag = LDAP_RES_INTERMEDIATE;
720 rs->sr_msgid = op->o_msgid;
721 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
722 Statslog( LDAP_DEBUG_STATS2,
723 "%s INTERM oid=%s\n",
725 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
732 * LDAP_SUCCESS entry sent
733 * LDAP_OTHER entry not sent (other)
734 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
735 * LDAP_UNAVAILABLE entry not sent (connection closed)
736 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
740 slap_send_search_entry( Operation *op, SlapReply *rs )
742 BerElementBuffer berbuf;
743 BerElement *ber = (BerElement *) &berbuf;
745 int i, j, rc = LDAP_UNAVAILABLE, bytes;
747 AccessControlState acl_state = ACL_STATE_INIT;
749 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
751 /* a_flags: array of flags telling if the i-th element will be
752 * returned or filtered out
753 * e_flags: array of a_flags
755 char **e_flags = NULL;
757 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
758 return LDAP_SIZELIMIT_EXCEEDED;
761 /* Every 64 entries, check for thread pool pause */
762 if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
763 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
768 rs->sr_type = REP_SEARCH;
770 /* eventually will loop through generated operational attribute types
771 * currently implemented types include:
772 * entryDN, subschemaSubentry, and hasSubordinates */
773 /* NOTE: moved before overlays callback circling because
774 * they may modify entry and other stuff in rs */
775 /* check for special all operational attributes ("+") type */
776 /* FIXME: maybe we could set this flag at the operation level;
777 * however, in principle the caller of send_search_entry() may
778 * change the attribute list at each call */
779 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
781 rc = backend_operational( op, rs );
786 if ( op->o_callback ) {
787 rc = slap_response_play( op, rs );
788 if ( rc != SLAP_CB_CONTINUE ) {
793 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
794 op->o_connid, rs->sr_entry->e_name.bv_val,
795 op->ors_attrsonly ? " (attrsOnly)" : "" );
797 attrsonly = op->ors_attrsonly;
799 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
800 Debug( LDAP_DEBUG_ACL,
801 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
802 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
804 rc = LDAP_INSUFFICIENT_ACCESS;
808 if ( op->o_res_ber ) {
809 /* read back control or LDAP_CONNECTIONLESS */
814 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
815 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
817 ber_init2( ber, &bv, LBER_USE_DER );
818 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
821 #ifdef LDAP_CONNECTIONLESS
822 if ( op->o_conn && op->o_conn->c_is_udp ) {
824 if ( op->o_protocol == LDAP_VERSION2 ) {
825 rc = ber_printf(ber, "t{O{" /*}}*/,
826 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
828 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
829 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
833 if ( op->o_res_ber ) {
834 /* read back control */
835 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
837 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
838 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
842 Debug( LDAP_DEBUG_ANY,
843 "send_search_entry: conn %lu ber_printf failed\n",
844 op->o_connid, 0, 0 );
846 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
847 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
852 /* check for special all user attributes ("*") type */
853 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
855 /* create an array of arrays of flags. Each flag corresponds
856 * to particular value of attribute and equals 1 if value matches
857 * to ValuesReturnFilter or 0 if not
859 if ( op->o_vrFilter != NULL ) {
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++ ) k++;
867 size = i * sizeof(char *) + k;
870 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
871 if( e_flags == NULL ) {
872 Debug( LDAP_DEBUG_ANY,
873 "send_search_entry: conn %lu slap_sl_calloc failed\n",
874 op->o_connid ? op->o_connid : 0, 0, 0 );
877 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
880 a_flags = (char *)(e_flags + i);
881 memset( a_flags, 0, k );
882 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
883 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
884 e_flags[i] = a_flags;
888 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
890 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
891 "conn %lu matched values filtering failed\n",
892 op->o_connid ? op->o_connid : 0, 0, 0 );
893 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
894 send_ldap_error( op, rs, LDAP_OTHER,
895 "matched values filtering error" );
902 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
903 AttributeDescription *desc = a->a_desc;
906 if ( rs->sr_attrs == NULL ) {
907 /* all user attrs request, skip operational attributes */
908 if( is_at_operational( desc->ad_type ) ) {
913 /* specific attrs requested */
914 if ( is_at_operational( desc->ad_type ) ) {
915 /* if not explicitly requested */
916 if ( !ad_inlist( desc, rs->sr_attrs )) {
917 /* if not all op attrs requested, skip */
918 if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
920 /* if DSA-specific and replicating, skip */
921 if ( op->o_sync != SLAP_CONTROL_NONE &&
922 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
926 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
933 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
934 ACL_READ, &acl_state ) )
936 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
937 "conn %lu access to attribute %s not allowed\n",
938 op->o_connid, desc->ad_cname.bv_val, 0 );
942 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
943 Debug( LDAP_DEBUG_ANY,
944 "send_search_entry: conn %lu ber_printf failed\n",
945 op->o_connid, 0, 0 );
947 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
948 send_ldap_error( op, rs, LDAP_OTHER,
949 "encoding description error");
957 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
958 if ( ! access_allowed( op, rs->sr_entry,
959 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
961 Debug( LDAP_DEBUG_ACL,
962 "send_search_entry: conn %lu "
963 "access to attribute %s, value #%d not allowed\n",
964 op->o_connid, desc->ad_cname.bv_val, i );
969 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
976 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -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,
983 "encoding description error");
988 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
989 Debug( LDAP_DEBUG_ANY,
990 "send_search_entry: conn %lu "
991 "ber_printf failed.\n", op->o_connid, 0, 0 );
993 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
994 send_ldap_error( op, rs, LDAP_OTHER,
995 "encoding values error" );
1002 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1003 Debug( LDAP_DEBUG_ANY,
1004 "send_search_entry: conn %lu ber_printf failed\n",
1005 op->o_connid, 0, 0 );
1007 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1008 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1014 /* NOTE: moved before overlays callback circling because
1015 * they may modify entry and other stuff in rs */
1016 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1020 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1021 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1024 size = i * sizeof(char *) + k;
1026 char *a_flags, **tmp;
1029 * Reuse previous memory - we likely need less space
1030 * for operational attributes
1032 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1034 if ( tmp == NULL ) {
1035 Debug( LDAP_DEBUG_ANY,
1036 "send_search_entry: conn %lu "
1037 "not enough memory "
1038 "for matched values filtering\n",
1039 op->o_connid, 0, 0 );
1040 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1041 send_ldap_error( op, rs, LDAP_OTHER,
1042 "not enough memory for matched values filtering" );
1046 a_flags = (char *)(e_flags + i);
1047 memset( a_flags, 0, k );
1048 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1049 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1050 e_flags[i] = a_flags;
1053 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1056 Debug( LDAP_DEBUG_ANY,
1057 "send_search_entry: conn %lu "
1058 "matched values filtering failed\n",
1059 op->o_connid ? op->o_connid : 0, 0, 0);
1060 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1061 send_ldap_error( op, rs, LDAP_OTHER,
1062 "matched values filtering error" );
1069 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1070 AttributeDescription *desc = a->a_desc;
1072 if ( rs->sr_attrs == NULL ) {
1073 /* all user attrs request, skip operational attributes */
1074 if( is_at_operational( desc->ad_type ) ) {
1079 /* specific attrs requested */
1080 if( is_at_operational( desc->ad_type ) ) {
1081 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1082 !ad_inlist( desc, rs->sr_attrs ) )
1087 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1093 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1094 ACL_READ, &acl_state ) )
1096 Debug( LDAP_DEBUG_ACL,
1097 "send_search_entry: conn %lu "
1098 "access to attribute %s not allowed\n",
1099 op->o_connid, desc->ad_cname.bv_val, 0 );
1104 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1106 Debug( LDAP_DEBUG_ANY,
1107 "send_search_entry: conn %lu "
1108 "ber_printf failed\n", 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 description error" );
1117 if ( ! attrsonly ) {
1118 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1119 if ( ! access_allowed( op, rs->sr_entry,
1120 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1122 Debug( LDAP_DEBUG_ACL,
1123 "send_search_entry: conn %lu "
1124 "access to %s, value %d not allowed\n",
1125 op->o_connid, desc->ad_cname.bv_val, i );
1130 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1134 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1135 Debug( LDAP_DEBUG_ANY,
1136 "send_search_entry: conn %lu ber_printf failed\n",
1137 op->o_connid, 0, 0 );
1139 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1140 send_ldap_error( op, rs, LDAP_OTHER,
1141 "encoding values error" );
1148 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1149 Debug( LDAP_DEBUG_ANY,
1150 "send_search_entry: conn %lu ber_printf failed\n",
1151 op->o_connid, 0, 0 );
1153 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1154 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1162 slap_sl_free( e_flags, op->o_tmpmemctx );
1166 rc = ber_printf( ber, /*{{*/ "}N}" );
1169 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1173 #ifdef LDAP_CONNECTIONLESS
1174 if( op->o_conn && op->o_conn->c_is_udp ) {
1175 if ( op->o_protocol != LDAP_VERSION2 ) {
1176 rc = ber_printf( ber, /*{*/ "N}" );
1180 if ( op->o_res_ber == NULL ) {
1181 rc = ber_printf( ber, /*{*/ "N}" );
1186 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1188 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1189 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1194 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1195 op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1197 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1198 be_entry_release_rw( op, rs->sr_entry, 0 );
1199 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1200 rs->sr_entry = NULL;
1203 if ( op->o_res_ber == NULL ) {
1204 bytes = send_ldap_ber( op->o_conn, ber );
1205 ber_free_buf( ber );
1208 Debug( LDAP_DEBUG_ANY,
1209 "send_search_entry: conn %lu ber write failed.\n",
1210 op->o_connid, 0, 0 );
1212 rc = LDAP_UNAVAILABLE;
1217 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1218 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1219 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1220 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1221 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1224 Debug( LDAP_DEBUG_TRACE,
1225 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1230 if ( op->o_callback ) {
1231 (void)slap_cleanup_play( op, rs );
1235 slap_sl_free( e_flags, op->o_tmpmemctx );
1238 if ( rs->sr_operational_attrs ) {
1239 attrs_free( rs->sr_operational_attrs );
1240 rs->sr_operational_attrs = NULL;
1242 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1244 /* FIXME: I think rs->sr_type should be explicitly set to
1245 * REP_SEARCH here. That's what it was when we entered this
1246 * function. send_ldap_error may have changed it, but we
1247 * should set it back so that the cleanup functions know
1248 * what they're doing.
1250 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1252 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1254 entry_free( rs->sr_entry );
1255 rs->sr_entry = NULL;
1256 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1263 slap_send_search_reference( Operation *op, SlapReply *rs )
1265 BerElementBuffer berbuf;
1266 BerElement *ber = (BerElement *) &berbuf;
1269 char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1271 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1272 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1274 rs->sr_type = REP_SEARCHREF;
1275 if ( op->o_callback ) {
1276 rc = slap_response_play( op, rs );
1277 if ( rc != SLAP_CB_CONTINUE ) {
1282 Debug( LDAP_DEBUG_TRACE,
1283 "=> send_search_reference: dn=\"%s\"\n",
1286 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1287 ad_entry, NULL, ACL_READ, NULL ) )
1289 Debug( LDAP_DEBUG_ACL,
1290 "send_search_reference: access to entry not allowed\n",
1296 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1297 ad_ref, NULL, ACL_READ, NULL ) )
1299 Debug( LDAP_DEBUG_ACL,
1300 "send_search_reference: access "
1301 "to reference not allowed\n",
1307 if( op->o_domain_scope ) {
1308 Debug( LDAP_DEBUG_ANY,
1309 "send_search_reference: domainScope control in (%s)\n",
1315 if( rs->sr_ref == NULL ) {
1316 Debug( LDAP_DEBUG_ANY,
1317 "send_search_reference: null ref in (%s)\n",
1323 if( op->o_protocol < LDAP_VERSION3 ) {
1325 /* save the references for the result */
1326 if( rs->sr_ref[0].bv_val != NULL ) {
1327 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1333 #ifdef LDAP_CONNECTIONLESS
1334 if( op->o_conn && op->o_conn->c_is_udp ) {
1335 ber = op->o_res_ber;
1339 ber_init_w_nullc( ber, LBER_USE_DER );
1340 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1343 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1344 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1347 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1351 rc = ber_printf( ber, /*"{"*/ "N}" );
1355 Debug( LDAP_DEBUG_ANY,
1356 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1358 #ifdef LDAP_CONNECTIONLESS
1359 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1361 ber_free_buf( ber );
1362 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1367 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1368 assert( rs->sr_entry != NULL );
1369 be_entry_release_rw( op, rs->sr_entry, 0 );
1370 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1371 rs->sr_entry = NULL;
1374 #ifdef LDAP_CONNECTIONLESS
1375 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1377 bytes = send_ldap_ber( op->o_conn, ber );
1378 ber_free_buf( ber );
1381 rc = LDAP_UNAVAILABLE;
1383 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1384 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1385 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1386 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1387 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_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_play( 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 ) {
1452 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1458 while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1459 if ( c[ 0 ] == '\0' ) {
1460 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1466 retcode = strtol( c, &next, 10 );
1467 if ( next == NULL || next == c ) {
1468 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1474 while ( isspace( (unsigned char) next[ 0 ] ) ) next++;
1475 if ( next[ 0 ] != '\0' ) {
1476 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1482 /* FIXME: what if it's larger that max int? */
1483 *code = (int)retcode;
1485 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1489 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1494 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1504 int slap_read_controls(
1508 const struct berval *oid,
1509 LDAPControl **ctrl )
1513 BerElementBuffer berbuf;
1514 BerElement *ber = (BerElement *) &berbuf;
1518 Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1519 op->o_log_prefix, oid->bv_val, e->e_dn );
1522 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1523 op->o_preread_attrs : op->o_postread_attrs;
1525 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1526 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1528 ber_init2( ber, &bv, LBER_USE_DER );
1529 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1531 /* create new operation */
1533 /* FIXME: o_bd needed for ACL */
1534 myop.o_bd = op->o_bd;
1535 myop.o_res_ber = ber;
1536 myop.o_callback = NULL;
1537 myop.ors_slimit = 1;
1539 rc = slap_send_search_entry( &myop, rs );
1542 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1544 if( rc == -1 ) 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;