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-2007 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 );
154 if ( connection_state_closing( conn ) ) {
155 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
156 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
161 if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
168 * we got an error. if it's ewouldblock, we need to
169 * wait on the socket being writable. otherwise, figure
170 * it's a hard error and return.
173 Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
174 err, sock_errstr(err), 0 );
176 if ( err != EWOULDBLOCK && err != EAGAIN ) {
177 connection_closing( conn, "connection lost on write" );
179 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
180 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
185 /* wait for socket to be write-ready */
186 conn->c_writewaiter = 1;
187 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
188 slapd_set_write( sd, 1 );
190 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
191 conn->c_writewaiter = 0;
194 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
195 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
201 send_ldap_control( BerElement *ber, LDAPControl *c )
207 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
209 if( c->ldctl_iscritical ) {
210 rc = ber_printf( ber, "b",
211 (ber_int_t) c->ldctl_iscritical ) ;
212 if( rc == -1 ) return rc;
215 if( c->ldctl_value.bv_val != NULL ) {
216 rc = ber_printf( ber, "O", &c->ldctl_value );
217 if( rc == -1 ) return rc;
220 rc = ber_printf( ber, /*{*/"N}" );
221 if( rc == -1 ) return rc;
227 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
234 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
235 if( rc == -1 ) return rc;
237 for( ; *c != NULL; c++) {
238 rc = send_ldap_control( ber, *c );
239 if( rc == -1 ) return rc;
242 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
243 /* this is a hack to avoid having to modify op->s_ctrls */
244 if( o->o_sortedresults ) {
245 BerElementBuffer berbuf;
246 BerElement *sber = (BerElement *) &berbuf;
248 BER_BVZERO( &sorted.ldctl_value );
249 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
250 sorted.ldctl_iscritical = 0;
252 ber_init2( sber, NULL, LBER_USE_DER );
254 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
256 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
260 (void) ber_free_buf( ber );
262 rc = send_ldap_control( ber, &sorted );
263 if( rc == -1 ) return rc;
267 rc = ber_printf( ber, /*{*/"N}" );
273 * slap_response_play()
275 * plays the callback list; rationale: a callback can
276 * - remove itself from the list, by setting op->o_callback = NULL;
277 * malloc()'ed callbacks should free themselves from inside the
278 * sc_response() function.
279 * - replace itself with another (list of) callback(s), by setting
280 * op->o_callback = a new (list of) callback(s); in this case, it
281 * is the callback's responsibility to to append existing subsequent
282 * callbacks to the end of the list that is passed to the sc_response()
284 * - modify the list of subsequent callbacks by modifying the value
285 * of the sc_next field from inside the sc_response() function; this
286 * case does not require any handling from inside slap_response_play()
288 * To stop execution of the playlist, the sc_response() function must return
289 * a value different from SLAP_SC_CONTINUE.
291 * The same applies to slap_cleanup_play(); only, there is no means to stop
292 * execution of the playlist, since all cleanup functions must be called.
301 slap_callback *sc = op->o_callback, **scp;
303 rc = SLAP_CB_CONTINUE;
304 for ( scp = ≻ *scp; ) {
305 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
307 op->o_callback = *scp;
308 if ( op->o_callback->sc_response ) {
309 rc = op->o_callback->sc_response( op, rs );
310 if ( op->o_callback == NULL ) {
311 /* the callback has been removed;
316 } else if ( op->o_callback != *scp ) {
317 /* a new callback has been inserted
318 * in place of the existing one; repair the list */
319 *scp = op->o_callback;
322 if ( rc != SLAP_CB_CONTINUE ) break;
336 slap_callback *sc = op->o_callback, **scp;
338 for ( scp = ≻ *scp; ) {
339 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
341 op->o_callback = *scp;
342 if ( op->o_callback->sc_cleanup ) {
343 (void)op->o_callback->sc_cleanup( op, rs );
344 if ( op->o_callback == NULL ) {
345 /* the callback has been removed;
350 } else if ( op->o_callback != *scp ) {
351 /* a new callback has been inserted
352 * after the existing one; repair the list */
353 /* a new callback has been inserted
354 * in place of the existing one; repair the list */
355 *scp = op->o_callback;
358 /* don't care about the result; do all cleanup */
372 BerElementBuffer berbuf;
373 BerElement *ber = (BerElement *) &berbuf;
374 int rc = LDAP_SUCCESS;
377 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon ) {
382 if ( op->o_callback ) {
383 rc = slap_response_play( op, rs );
384 if ( rc != SLAP_CB_CONTINUE ) {
389 #ifdef LDAP_CONNECTIONLESS
390 if (op->o_conn && op->o_conn->c_is_udp)
395 ber_init_w_nullc( ber, LBER_USE_DER );
396 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
399 Debug( LDAP_DEBUG_TRACE,
400 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
401 rs->sr_msgid, rs->sr_tag, rs->sr_err );
404 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
405 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
409 #ifdef LDAP_CONNECTIONLESS
410 if (op->o_conn && op->o_conn->c_is_udp &&
411 op->o_protocol == LDAP_VERSION2 )
413 rc = ber_printf( ber, "t{ess" /*"}"*/,
414 rs->sr_tag, rs->sr_err,
415 rs->sr_matched == NULL ? "" : rs->sr_matched,
416 rs->sr_text == NULL ? "" : rs->sr_text );
419 if ( rs->sr_type == REP_INTERMEDIATE ) {
420 rc = ber_printf( ber, "{it{" /*"}}"*/,
421 rs->sr_msgid, rs->sr_tag );
424 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
425 rs->sr_msgid, rs->sr_tag, rs->sr_err,
426 rs->sr_matched == NULL ? "" : rs->sr_matched,
427 rs->sr_text == NULL ? "" : rs->sr_text );
431 if ( rs->sr_ref != NULL ) {
432 assert( rs->sr_err == LDAP_REFERRAL );
433 rc = ber_printf( ber, "t{W}",
434 LDAP_TAG_REFERRAL, rs->sr_ref );
436 assert( rs->sr_err != LDAP_REFERRAL );
440 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
441 rc = ber_printf( ber, "tO",
442 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
446 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
448 if ( rs->sr_rspoid != NULL ) {
449 rc = ber_printf( ber, "ts",
450 rs->sr_type == REP_EXTENDED
451 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
454 if( rc != -1 && rs->sr_rspdata != NULL ) {
455 rc = ber_printf( ber, "tO",
456 rs->sr_type == REP_EXTENDED
457 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
463 rc = ber_printf( ber, /*"{"*/ "N}" );
467 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
471 rc = ber_printf( ber, /*"{"*/ "N}" );
474 #ifdef LDAP_CONNECTIONLESS
475 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
478 rc = ber_printf( ber, /*"{"*/ "N}" );
483 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
485 #ifdef LDAP_CONNECTIONLESS
486 if (!op->o_conn || op->o_conn->c_is_udp == 0)
495 bytes = send_ldap_ber( op->o_conn, ber );
496 #ifdef LDAP_CONNECTIONLESS
497 if (!op->o_conn || op->o_conn->c_is_udp == 0)
504 Debug( LDAP_DEBUG_ANY,
505 "send_ldap_response: ber write failed\n",
511 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
512 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
513 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
514 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
517 /* Tell caller that we did this for real, as opposed to being
518 * overridden by a callback
520 rc = SLAP_CB_CONTINUE;
523 if ( op->o_callback ) {
524 (void)slap_cleanup_play( op, rs );
527 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
528 free( (char *)rs->sr_matched );
529 rs->sr_matched = NULL;
532 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
533 ber_bvarray_free( rs->sr_ref );
542 send_ldap_disconnect( Operation *op, SlapReply *rs )
544 #define LDAP_UNSOLICITED_ERROR(e) \
545 ( (e) == LDAP_PROTOCOL_ERROR \
546 || (e) == LDAP_STRONG_AUTH_REQUIRED \
547 || (e) == LDAP_UNAVAILABLE )
549 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
551 rs->sr_type = REP_EXTENDED;
553 Debug( LDAP_DEBUG_TRACE,
554 "send_ldap_disconnect %d:%s\n",
555 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
557 if ( op->o_protocol < LDAP_VERSION3 ) {
558 rs->sr_rspoid = NULL;
559 rs->sr_tag = slap_req2res( op->o_tag );
560 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
563 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
564 rs->sr_tag = LDAP_RES_EXTENDED;
565 rs->sr_msgid = LDAP_RES_UNSOLICITED;
568 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
569 Statslog( LDAP_DEBUG_STATS,
570 "%s DISCONNECT tag=%lu err=%d text=%s\n",
571 op->o_log_prefix, rs->sr_tag, rs->sr_err,
572 rs->sr_text ? rs->sr_text : "", 0 );
577 slap_send_ldap_result( Operation *op, SlapReply *rs )
580 const char *otext = rs->sr_text;
581 BerVarray oref = rs->sr_ref;
583 rs->sr_type = REP_RESULT;
585 /* Propagate Abandons so that cleanup callbacks can be processed */
586 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
589 assert( !LDAP_API_ERROR( rs->sr_err ) );
591 Debug( LDAP_DEBUG_TRACE,
592 "send_ldap_result: %s p=%d\n",
593 op->o_log_prefix, op->o_protocol, 0 );
595 Debug( LDAP_DEBUG_ARGS,
596 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
597 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
598 rs->sr_text ? rs->sr_text : "" );
602 Debug( LDAP_DEBUG_ARGS,
603 "send_ldap_result: referral=\"%s\"\n",
604 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
608 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
610 if ( rs->sr_err == LDAP_REFERRAL ) {
611 if( op->o_domain_scope ) rs->sr_ref = NULL;
613 if( rs->sr_ref == NULL ) {
614 rs->sr_err = LDAP_NO_SUCH_OBJECT;
615 } else if ( op->o_protocol < LDAP_VERSION3 ) {
616 rs->sr_err = LDAP_PARTIAL_RESULTS;
620 if ( op->o_protocol < LDAP_VERSION3 ) {
621 tmp = v2ref( rs->sr_ref, rs->sr_text );
626 rs->sr_tag = slap_req2res( op->o_tag );
627 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
630 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
631 if ( op->o_tag == LDAP_REQ_SEARCH ) {
633 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
634 rs->sr_err, rs->sr_nentries );
636 Statslog( LDAP_DEBUG_STATS,
637 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
638 op->o_log_prefix, rs->sr_tag, nbuf,
639 rs->sr_text ? rs->sr_text : "", 0 );
641 Statslog( LDAP_DEBUG_STATS,
642 "%s RESULT tag=%lu err=%d text=%s\n",
643 op->o_log_prefix, rs->sr_tag, rs->sr_err,
644 rs->sr_text ? rs->sr_text : "", 0 );
648 if( tmp != NULL ) ch_free(tmp);
654 send_ldap_sasl( Operation *op, SlapReply *rs )
656 rs->sr_type = REP_SASL;
657 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
659 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
661 rs->sr_tag = slap_req2res( op->o_tag );
662 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
664 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
665 Statslog( LDAP_DEBUG_STATS,
666 "%s RESULT tag=%lu err=%d text=%s\n",
667 op->o_log_prefix, rs->sr_tag, rs->sr_err,
668 rs->sr_text ? rs->sr_text : "", 0 );
673 slap_send_ldap_extended( Operation *op, SlapReply *rs )
675 rs->sr_type = REP_EXTENDED;
677 Debug( LDAP_DEBUG_TRACE,
678 "send_ldap_extended: err=%d oid=%s len=%ld\n",
680 rs->sr_rspoid ? rs->sr_rspoid : "",
681 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
683 rs->sr_tag = slap_req2res( op->o_tag );
684 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
686 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
687 Statslog( LDAP_DEBUG_STATS,
688 "%s RESULT oid=%s err=%d text=%s\n",
689 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
690 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
695 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
697 rs->sr_type = REP_INTERMEDIATE;
698 Debug( LDAP_DEBUG_TRACE,
699 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
701 rs->sr_rspoid ? rs->sr_rspoid : "",
702 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
703 rs->sr_tag = LDAP_RES_INTERMEDIATE;
704 rs->sr_msgid = op->o_msgid;
705 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
706 Statslog( LDAP_DEBUG_STATS2,
707 "%s INTERM oid=%s\n",
709 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
716 * LDAP_SUCCESS entry sent
717 * LDAP_OTHER entry not sent (other)
718 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
719 * LDAP_UNAVAILABLE entry not sent (connection closed)
720 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
724 slap_send_search_entry( Operation *op, SlapReply *rs )
726 BerElementBuffer berbuf;
727 BerElement *ber = (BerElement *) &berbuf;
729 int i, j, rc = LDAP_UNAVAILABLE, bytes;
732 AccessControlState acl_state = ACL_STATE_INIT;
734 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
736 /* a_flags: array of flags telling if the i-th element will be
737 * returned or filtered out
738 * e_flags: array of a_flags
740 char **e_flags = NULL;
742 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
743 return LDAP_SIZELIMIT_EXCEEDED;
746 /* Every 64 entries, check for thread pool pause */
747 if (( rs->sr_nentries & 0x3f == 0x3f ) &&
748 ldap_pvt_thread_pool_query( &connection_pool,
749 LDAP_PVT_THREAD_POOL_PARAM_PAUSING, &i ) == 0 && i ) {
753 rs->sr_type = REP_SEARCH;
755 /* eventually will loop through generated operational attribute types
756 * currently implemented types include:
757 * entryDN, subschemaSubentry, and hasSubordinates */
758 /* NOTE: moved before overlays callback circling because
759 * they may modify entry and other stuff in rs */
760 /* check for special all operational attributes ("+") type */
761 /* FIXME: maybe we could set this flag at the operation level;
762 * however, in principle the caller of send_search_entry() may
763 * change the attribute list at each call */
764 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
766 rc = backend_operational( op, rs );
771 if ( op->o_callback ) {
772 rc = slap_response_play( op, rs );
773 if ( rc != SLAP_CB_CONTINUE ) {
778 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
779 op->o_connid, rs->sr_entry->e_name.bv_val,
780 op->ors_attrsonly ? " (attrsOnly)" : "" );
782 attrsonly = op->ors_attrsonly;
784 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
785 Debug( LDAP_DEBUG_ACL,
786 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
787 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
789 rc = LDAP_INSUFFICIENT_ACCESS;
793 edn = rs->sr_entry->e_nname.bv_val;
795 if ( op->o_res_ber ) {
796 /* read back control or LDAP_CONNECTIONLESS */
801 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
802 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
804 ber_init2( ber, &bv, LBER_USE_DER );
805 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
808 #ifdef LDAP_CONNECTIONLESS
809 if ( op->o_conn && op->o_conn->c_is_udp ) {
811 if ( op->o_protocol == LDAP_VERSION2 ) {
812 rc = ber_printf(ber, "t{O{" /*}}*/,
813 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
815 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
816 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
820 if ( op->o_res_ber ) {
821 /* read back control */
822 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
824 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
825 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
829 Debug( LDAP_DEBUG_ANY,
830 "send_search_entry: conn %lu ber_printf failed\n",
831 op->o_connid, 0, 0 );
833 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
834 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
839 /* check for special all user attributes ("*") type */
840 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
842 /* create an array of arrays of flags. Each flag corresponds
843 * to particular value of attribute and equals 1 if value matches
844 * to ValuesReturnFilter or 0 if not
846 if ( op->o_vrFilter != NULL ) {
850 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
851 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
854 size = i * sizeof(char *) + k;
857 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
858 if( e_flags == NULL ) {
859 Debug( LDAP_DEBUG_ANY,
860 "send_search_entry: conn %lu slap_sl_calloc failed\n",
861 op->o_connid ? op->o_connid : 0, 0, 0 );
864 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
867 a_flags = (char *)(e_flags + i);
868 memset( a_flags, 0, k );
869 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
870 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
871 e_flags[i] = a_flags;
875 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
877 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
878 "conn %lu matched values filtering failed\n",
879 op->o_connid ? op->o_connid : 0, 0, 0 );
880 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
881 send_ldap_error( op, rs, LDAP_OTHER,
882 "matched values filtering error" );
889 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
890 AttributeDescription *desc = a->a_desc;
893 if ( rs->sr_attrs == NULL ) {
894 /* all user attrs request, skip operational attributes */
895 if( is_at_operational( desc->ad_type ) ) {
900 /* specific attrs requested */
901 if ( is_at_operational( desc->ad_type ) ) {
902 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
903 !ad_inlist( desc, rs->sr_attrs ) )
909 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
916 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
917 ACL_READ, &acl_state ) )
919 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
920 "conn %lu access to attribute %s not allowed\n",
921 op->o_connid, desc->ad_cname.bv_val, 0 );
925 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
926 Debug( LDAP_DEBUG_ANY,
927 "send_search_entry: conn %lu ber_printf failed\n",
928 op->o_connid, 0, 0 );
930 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
931 send_ldap_error( op, rs, LDAP_OTHER,
932 "encoding description error");
940 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
941 if ( ! access_allowed( op, rs->sr_entry,
942 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
944 Debug( LDAP_DEBUG_ACL,
945 "send_search_entry: conn %lu "
946 "access to attribute %s, value #%d not allowed\n",
947 op->o_connid, desc->ad_cname.bv_val, i );
952 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
959 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
960 Debug( LDAP_DEBUG_ANY,
961 "send_search_entry: conn %lu ber_printf failed\n",
962 op->o_connid, 0, 0 );
964 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
965 send_ldap_error( op, rs, LDAP_OTHER,
966 "encoding description error");
971 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
972 Debug( LDAP_DEBUG_ANY,
973 "send_search_entry: conn %lu "
974 "ber_printf failed.\n", op->o_connid, 0, 0 );
976 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
977 send_ldap_error( op, rs, LDAP_OTHER,
978 "encoding values error" );
985 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
986 Debug( LDAP_DEBUG_ANY,
987 "send_search_entry: conn %lu ber_printf failed\n",
988 op->o_connid, 0, 0 );
990 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
991 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
997 /* NOTE: moved before overlays callback circling because
998 * they may modify entry and other stuff in rs */
999 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1003 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1004 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1007 size = i * sizeof(char *) + k;
1009 char *a_flags, **tmp;
1012 * Reuse previous memory - we likely need less space
1013 * for operational attributes
1015 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1017 if ( tmp == NULL ) {
1018 Debug( LDAP_DEBUG_ANY,
1019 "send_search_entry: conn %lu "
1020 "not enough memory "
1021 "for matched values filtering\n",
1022 op->o_connid, 0, 0 );
1023 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1024 send_ldap_error( op, rs, LDAP_OTHER,
1025 "not enough memory for matched values filtering" );
1029 a_flags = (char *)(e_flags + i);
1030 memset( a_flags, 0, k );
1031 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1032 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1033 e_flags[i] = a_flags;
1036 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1039 Debug( LDAP_DEBUG_ANY,
1040 "send_search_entry: conn %lu "
1041 "matched values filtering failed\n",
1042 op->o_connid ? op->o_connid : 0, 0, 0);
1043 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1044 send_ldap_error( op, rs, LDAP_OTHER,
1045 "matched values filtering error" );
1052 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1053 AttributeDescription *desc = a->a_desc;
1055 if ( rs->sr_attrs == NULL ) {
1056 /* all user attrs request, skip operational attributes */
1057 if( is_at_operational( desc->ad_type ) ) {
1062 /* specific attrs requested */
1063 if( is_at_operational( desc->ad_type ) ) {
1064 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1065 !ad_inlist( desc, rs->sr_attrs ) )
1070 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1076 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1077 ACL_READ, &acl_state ) )
1079 Debug( LDAP_DEBUG_ACL,
1080 "send_search_entry: conn %lu "
1081 "access to attribute %s not allowed\n",
1082 op->o_connid, desc->ad_cname.bv_val, 0 );
1087 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1089 Debug( LDAP_DEBUG_ANY,
1090 "send_search_entry: conn %lu "
1091 "ber_printf failed\n", op->o_connid, 0, 0 );
1093 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1094 send_ldap_error( op, rs, LDAP_OTHER,
1095 "encoding description error" );
1100 if ( ! attrsonly ) {
1101 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1102 if ( ! access_allowed( op, rs->sr_entry,
1103 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1105 Debug( LDAP_DEBUG_ACL,
1106 "send_search_entry: conn %lu "
1107 "access to %s, value %d not allowed\n",
1108 op->o_connid, desc->ad_cname.bv_val, i );
1113 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1117 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1118 Debug( LDAP_DEBUG_ANY,
1119 "send_search_entry: conn %lu ber_printf failed\n",
1120 op->o_connid, 0, 0 );
1122 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1123 send_ldap_error( op, rs, LDAP_OTHER,
1124 "encoding values error" );
1131 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1132 Debug( LDAP_DEBUG_ANY,
1133 "send_search_entry: conn %lu ber_printf failed\n",
1134 op->o_connid, 0, 0 );
1136 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1137 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1145 slap_sl_free( e_flags, op->o_tmpmemctx );
1149 rc = ber_printf( ber, /*{{*/ "}N}" );
1152 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1156 #ifdef LDAP_CONNECTIONLESS
1157 if( op->o_conn && op->o_conn->c_is_udp ) {
1158 if ( op->o_protocol != LDAP_VERSION2 ) {
1159 rc = ber_printf( ber, /*{*/ "N}" );
1163 if ( op->o_res_ber == NULL ) {
1164 rc = ber_printf( ber, /*{*/ "N}" );
1169 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1171 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1172 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1177 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1178 be_entry_release_rw( op, rs->sr_entry, 0 );
1179 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1180 rs->sr_entry = NULL;
1183 if ( op->o_res_ber == NULL ) {
1184 bytes = send_ldap_ber( op->o_conn, ber );
1185 ber_free_buf( ber );
1188 Debug( LDAP_DEBUG_ANY,
1189 "send_search_entry: conn %lu ber write failed.\n",
1190 op->o_connid, 0, 0 );
1192 rc = LDAP_UNAVAILABLE;
1197 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1198 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1199 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1200 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1201 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1204 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1205 op->o_log_prefix, edn, 0, 0, 0 );
1207 Debug( LDAP_DEBUG_TRACE,
1208 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1213 if ( op->o_callback ) {
1214 (void)slap_cleanup_play( op, rs );
1218 slap_sl_free( e_flags, op->o_tmpmemctx );
1221 if ( rs->sr_operational_attrs ) {
1222 attrs_free( rs->sr_operational_attrs );
1223 rs->sr_operational_attrs = NULL;
1225 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1227 /* FIXME: I think rs->sr_type should be explicitly set to
1228 * REP_SEARCH here. That's what it was when we entered this
1229 * function. send_ldap_error may have changed it, but we
1230 * should set it back so that the cleanup functions know
1231 * what they're doing.
1233 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1235 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1237 entry_free( rs->sr_entry );
1238 rs->sr_entry = NULL;
1239 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1246 slap_send_search_reference( Operation *op, SlapReply *rs )
1248 BerElementBuffer berbuf;
1249 BerElement *ber = (BerElement *) &berbuf;
1253 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1254 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1256 rs->sr_type = REP_SEARCHREF;
1257 if ( op->o_callback ) {
1258 rc = slap_response_play( op, rs );
1259 if ( rc != SLAP_CB_CONTINUE ) {
1264 Debug( LDAP_DEBUG_TRACE,
1265 "=> send_search_reference: dn=\"%s\"\n",
1266 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1268 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1269 ad_entry, NULL, ACL_READ, NULL ) )
1271 Debug( LDAP_DEBUG_ACL,
1272 "send_search_reference: access to entry not allowed\n",
1278 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1279 ad_ref, NULL, ACL_READ, NULL ) )
1281 Debug( LDAP_DEBUG_ACL,
1282 "send_search_reference: access "
1283 "to reference not allowed\n",
1289 if( op->o_domain_scope ) {
1290 Debug( LDAP_DEBUG_ANY,
1291 "send_search_reference: domainScope control in (%s)\n",
1292 rs->sr_entry->e_dn, 0, 0 );
1297 if( rs->sr_ref == NULL ) {
1298 Debug( LDAP_DEBUG_ANY,
1299 "send_search_reference: null ref in (%s)\n",
1300 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1305 if( op->o_protocol < LDAP_VERSION3 ) {
1307 /* save the references for the result */
1308 if( rs->sr_ref[0].bv_val != NULL ) {
1309 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1315 #ifdef LDAP_CONNECTIONLESS
1316 if( op->o_conn && op->o_conn->c_is_udp ) {
1317 ber = op->o_res_ber;
1321 ber_init_w_nullc( ber, LBER_USE_DER );
1322 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1325 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1326 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1329 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1333 rc = ber_printf( ber, /*"{"*/ "N}" );
1337 Debug( LDAP_DEBUG_ANY,
1338 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1340 #ifdef LDAP_CONNECTIONLESS
1341 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1343 ber_free_buf( ber );
1344 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1349 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1350 be_entry_release_rw( op, rs->sr_entry, 0 );
1351 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1352 rs->sr_entry = NULL;
1355 #ifdef LDAP_CONNECTIONLESS
1356 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1358 bytes = send_ldap_ber( op->o_conn, ber );
1359 ber_free_buf( ber );
1362 rc = LDAP_UNAVAILABLE;
1364 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1365 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1366 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1367 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1368 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1370 #ifdef LDAP_CONNECTIONLESS
1373 if ( rs->sr_ref != NULL ) {
1376 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1377 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1378 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1383 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1384 op->o_log_prefix, 0, 0, 0, 0 );
1387 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1390 if ( op->o_callback ) {
1391 (void)slap_cleanup_play( op, rs );
1407 *code = LDAP_SUCCESS;
1411 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1412 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1419 while ( (s = strchr( s, '\n' )) != NULL ) {
1424 if ( (c = strchr( s, ':' )) != NULL ) {
1428 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1433 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1439 while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1440 if ( c[ 0 ] == '\0' ) {
1441 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1447 retcode = strtol( c, &next, 10 );
1448 if ( next == NULL || next == c ) {
1449 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1455 while ( isspace( (unsigned char) next[ 0 ] ) ) next++;
1456 if ( next[ 0 ] != '\0' ) {
1457 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1463 /* FIXME: what if it's larger that max int? */
1464 *code = (int)retcode;
1466 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1470 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1475 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1485 int slap_read_controls(
1489 const struct berval *oid,
1490 LDAPControl **ctrl )
1494 BerElementBuffer berbuf;
1495 BerElement *ber = (BerElement *) &berbuf;
1499 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1500 oid->bv_val, e->e_dn, 0 );
1503 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1504 op->o_preread_attrs : op->o_postread_attrs;
1506 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1507 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1509 ber_init2( ber, &bv, LBER_USE_DER );
1510 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1512 /* create new operation */
1514 /* FIXME: o_bd needed for ACL */
1515 myop.o_bd = op->o_bd;
1516 myop.o_res_ber = ber;
1517 myop.o_callback = NULL;
1518 myop.ors_slimit = 1;
1520 rc = slap_send_search_entry( &myop, rs );
1523 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1525 if( rc == LBER_ERROR ) return LDAP_OTHER;
1527 c.ldctl_oid = oid->bv_val;
1528 c.ldctl_iscritical = 0;
1530 if ( *ctrl == NULL ) {
1532 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1534 /* retry: free previous try */
1535 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1539 return LDAP_SUCCESS;
1542 /* Map API errors to protocol errors... */
1544 slap_map_api2result( SlapReply *rs )
1546 switch(rs->sr_err) {
1547 case LDAP_SERVER_DOWN:
1548 return LDAP_UNAVAILABLE;
1549 case LDAP_LOCAL_ERROR:
1551 case LDAP_ENCODING_ERROR:
1552 case LDAP_DECODING_ERROR:
1553 return LDAP_PROTOCOL_ERROR;
1555 return LDAP_UNAVAILABLE;
1556 case LDAP_AUTH_UNKNOWN:
1557 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1558 case LDAP_FILTER_ERROR:
1559 rs->sr_text = "Filter error";
1561 case LDAP_USER_CANCELLED:
1562 rs->sr_text = "User cancelled";
1564 case LDAP_PARAM_ERROR:
1565 return LDAP_PROTOCOL_ERROR;
1566 case LDAP_NO_MEMORY:
1568 case LDAP_CONNECT_ERROR:
1569 return LDAP_UNAVAILABLE;
1570 case LDAP_NOT_SUPPORTED:
1571 return LDAP_UNWILLING_TO_PERFORM;
1572 case LDAP_CONTROL_NOT_FOUND:
1573 return LDAP_PROTOCOL_ERROR;
1574 case LDAP_NO_RESULTS_RETURNED:
1575 return LDAP_NO_SUCH_OBJECT;
1576 case LDAP_MORE_RESULTS_TO_RETURN:
1577 rs->sr_text = "More results to return";
1579 case LDAP_CLIENT_LOOP:
1580 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1581 return LDAP_LOOP_DETECT;
1583 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1590 slap_attr_flags( AttributeName *an )
1592 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1595 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1598 flags |= an_find( an, &AllOper )
1599 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1600 flags |= an_find( an, &AllUser )
1601 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;