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 );
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_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
526 free( (char *)rs->sr_matched );
527 rs->sr_matched = NULL;
530 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
531 ber_bvarray_free( rs->sr_ref );
540 send_ldap_disconnect( Operation *op, SlapReply *rs )
542 #define LDAP_UNSOLICITED_ERROR(e) \
543 ( (e) == LDAP_PROTOCOL_ERROR \
544 || (e) == LDAP_STRONG_AUTH_REQUIRED \
545 || (e) == LDAP_UNAVAILABLE )
547 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
549 rs->sr_type = REP_EXTENDED;
551 Debug( LDAP_DEBUG_TRACE,
552 "send_ldap_disconnect %d:%s\n",
553 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
555 if ( op->o_protocol < LDAP_VERSION3 ) {
556 rs->sr_rspoid = NULL;
557 rs->sr_tag = slap_req2res( op->o_tag );
558 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
561 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
562 rs->sr_tag = LDAP_RES_EXTENDED;
563 rs->sr_msgid = LDAP_RES_UNSOLICITED;
566 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
567 Statslog( LDAP_DEBUG_STATS,
568 "%s DISCONNECT tag=%lu err=%d text=%s\n",
569 op->o_log_prefix, rs->sr_tag, rs->sr_err,
570 rs->sr_text ? rs->sr_text : "", 0 );
575 slap_send_ldap_result( Operation *op, SlapReply *rs )
578 const char *otext = rs->sr_text;
579 BerVarray oref = rs->sr_ref;
581 rs->sr_type = REP_RESULT;
583 /* Propagate Abandons so that cleanup callbacks can be processed */
584 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
587 assert( !LDAP_API_ERROR( rs->sr_err ) );
589 Debug( LDAP_DEBUG_TRACE,
590 "send_ldap_result: %s p=%d\n",
591 op->o_log_prefix, op->o_protocol, 0 );
593 Debug( LDAP_DEBUG_ARGS,
594 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
595 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
596 rs->sr_text ? rs->sr_text : "" );
600 Debug( LDAP_DEBUG_ARGS,
601 "send_ldap_result: referral=\"%s\"\n",
602 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
606 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
608 if ( rs->sr_err == LDAP_REFERRAL ) {
609 if( op->o_domain_scope ) rs->sr_ref = NULL;
611 if( rs->sr_ref == NULL ) {
612 rs->sr_err = LDAP_NO_SUCH_OBJECT;
613 } else if ( op->o_protocol < LDAP_VERSION3 ) {
614 rs->sr_err = LDAP_PARTIAL_RESULTS;
618 if ( op->o_protocol < LDAP_VERSION3 ) {
619 tmp = v2ref( rs->sr_ref, rs->sr_text );
624 rs->sr_tag = slap_req2res( op->o_tag );
625 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
628 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
629 if ( op->o_tag == LDAP_REQ_SEARCH ) {
631 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
632 rs->sr_err, rs->sr_nentries );
634 Statslog( LDAP_DEBUG_STATS,
635 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
636 op->o_log_prefix, rs->sr_tag, nbuf,
637 rs->sr_text ? rs->sr_text : "", 0 );
639 Statslog( LDAP_DEBUG_STATS,
640 "%s RESULT tag=%lu err=%d text=%s\n",
641 op->o_log_prefix, rs->sr_tag, rs->sr_err,
642 rs->sr_text ? rs->sr_text : "", 0 );
646 if( tmp != NULL ) ch_free(tmp);
652 send_ldap_sasl( Operation *op, SlapReply *rs )
654 rs->sr_type = REP_SASL;
655 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
657 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
659 rs->sr_tag = slap_req2res( op->o_tag );
660 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
662 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
663 Statslog( LDAP_DEBUG_STATS,
664 "%s RESULT tag=%lu err=%d text=%s\n",
665 op->o_log_prefix, rs->sr_tag, rs->sr_err,
666 rs->sr_text ? rs->sr_text : "", 0 );
671 slap_send_ldap_extended( Operation *op, SlapReply *rs )
673 rs->sr_type = REP_EXTENDED;
675 Debug( LDAP_DEBUG_TRACE,
676 "send_ldap_extended: err=%d oid=%s len=%ld\n",
678 rs->sr_rspoid ? rs->sr_rspoid : "",
679 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
681 rs->sr_tag = slap_req2res( op->o_tag );
682 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
684 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
685 Statslog( LDAP_DEBUG_STATS,
686 "%s RESULT oid=%s err=%d text=%s\n",
687 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
688 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
693 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
695 rs->sr_type = REP_INTERMEDIATE;
696 Debug( LDAP_DEBUG_TRACE,
697 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
699 rs->sr_rspoid ? rs->sr_rspoid : "",
700 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
701 rs->sr_tag = LDAP_RES_INTERMEDIATE;
702 rs->sr_msgid = op->o_msgid;
703 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
704 Statslog( LDAP_DEBUG_STATS2,
705 "%s INTERM oid=%s\n",
707 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
714 * LDAP_SUCCESS entry sent
715 * LDAP_OTHER entry not sent (other)
716 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
717 * LDAP_UNAVAILABLE entry not sent (connection closed)
718 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
722 slap_send_search_entry( Operation *op, SlapReply *rs )
724 BerElementBuffer berbuf;
725 BerElement *ber = (BerElement *) &berbuf;
727 int i, j, rc = LDAP_UNAVAILABLE, bytes;
730 AccessControlState acl_state = ACL_STATE_INIT;
732 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
734 /* a_flags: array of flags telling if the i-th element will be
735 * returned or filtered out
736 * e_flags: array of a_flags
738 char **e_flags = NULL;
740 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
741 return LDAP_SIZELIMIT_EXCEEDED;
744 /* Every 64 entries, check for thread pool pause */
745 if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
746 ldap_pvt_thread_pool_query( &connection_pool,
747 LDAP_PVT_THREAD_POOL_PARAM_PAUSING, &i ) == 0 && i )
752 rs->sr_type = REP_SEARCH;
754 /* eventually will loop through generated operational attribute types
755 * currently implemented types include:
756 * entryDN, subschemaSubentry, and hasSubordinates */
757 /* NOTE: moved before overlays callback circling because
758 * they may modify entry and other stuff in rs */
759 /* check for special all operational attributes ("+") type */
760 /* FIXME: maybe we could set this flag at the operation level;
761 * however, in principle the caller of send_search_entry() may
762 * change the attribute list at each call */
763 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
765 rc = backend_operational( op, rs );
770 if ( op->o_callback ) {
771 rc = slap_response_play( op, rs );
772 if ( rc != SLAP_CB_CONTINUE ) {
777 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
778 op->o_connid, rs->sr_entry->e_name.bv_val,
779 op->ors_attrsonly ? " (attrsOnly)" : "" );
781 attrsonly = op->ors_attrsonly;
783 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
784 Debug( LDAP_DEBUG_ACL,
785 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
786 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
788 rc = LDAP_INSUFFICIENT_ACCESS;
792 edn = rs->sr_entry->e_nname.bv_val;
794 if ( op->o_res_ber ) {
795 /* read back control or LDAP_CONNECTIONLESS */
800 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
801 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
803 ber_init2( ber, &bv, LBER_USE_DER );
804 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
807 #ifdef LDAP_CONNECTIONLESS
808 if ( op->o_conn && op->o_conn->c_is_udp ) {
810 if ( op->o_protocol == LDAP_VERSION2 ) {
811 rc = ber_printf(ber, "t{O{" /*}}*/,
812 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
814 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
815 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
819 if ( op->o_res_ber ) {
820 /* read back control */
821 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
823 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
824 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
828 Debug( LDAP_DEBUG_ANY,
829 "send_search_entry: conn %lu ber_printf failed\n",
830 op->o_connid, 0, 0 );
832 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
833 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
838 /* check for special all user attributes ("*") type */
839 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
841 /* create an array of arrays of flags. Each flag corresponds
842 * to particular value of attribute and equals 1 if value matches
843 * to ValuesReturnFilter or 0 if not
845 if ( op->o_vrFilter != NULL ) {
849 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
850 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
853 size = i * sizeof(char *) + k;
856 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
857 if( e_flags == NULL ) {
858 Debug( LDAP_DEBUG_ANY,
859 "send_search_entry: conn %lu slap_sl_calloc failed\n",
860 op->o_connid ? op->o_connid : 0, 0, 0 );
863 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
866 a_flags = (char *)(e_flags + i);
867 memset( a_flags, 0, k );
868 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
869 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
870 e_flags[i] = a_flags;
874 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
876 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
877 "conn %lu matched values filtering failed\n",
878 op->o_connid ? op->o_connid : 0, 0, 0 );
879 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
880 send_ldap_error( op, rs, LDAP_OTHER,
881 "matched values filtering error" );
888 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
889 AttributeDescription *desc = a->a_desc;
892 if ( rs->sr_attrs == NULL ) {
893 /* all user attrs request, skip operational attributes */
894 if( is_at_operational( desc->ad_type ) ) {
899 /* specific attrs requested */
900 if ( is_at_operational( desc->ad_type ) ) {
901 /* if not explicitly requested */
902 if ( !ad_inlist( desc, rs->sr_attrs )) {
903 /* if not all op attrs requested, skip */
904 if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
906 /* if DSA-specific and replicating, skip */
907 if ( op->o_sync != SLAP_CONTROL_NONE &&
908 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
912 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
919 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
920 ACL_READ, &acl_state ) )
922 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
923 "conn %lu access to attribute %s not allowed\n",
924 op->o_connid, desc->ad_cname.bv_val, 0 );
928 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
929 Debug( LDAP_DEBUG_ANY,
930 "send_search_entry: conn %lu ber_printf failed\n",
931 op->o_connid, 0, 0 );
933 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
934 send_ldap_error( op, rs, LDAP_OTHER,
935 "encoding description error");
943 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
944 if ( ! access_allowed( op, rs->sr_entry,
945 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
947 Debug( LDAP_DEBUG_ACL,
948 "send_search_entry: conn %lu "
949 "access to attribute %s, value #%d not allowed\n",
950 op->o_connid, desc->ad_cname.bv_val, i );
955 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
962 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
963 Debug( LDAP_DEBUG_ANY,
964 "send_search_entry: conn %lu ber_printf failed\n",
965 op->o_connid, 0, 0 );
967 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
968 send_ldap_error( op, rs, LDAP_OTHER,
969 "encoding description error");
974 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
975 Debug( LDAP_DEBUG_ANY,
976 "send_search_entry: conn %lu "
977 "ber_printf failed.\n", op->o_connid, 0, 0 );
979 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
980 send_ldap_error( op, rs, LDAP_OTHER,
981 "encoding values error" );
988 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
989 Debug( LDAP_DEBUG_ANY,
990 "send_search_entry: conn %lu ber_printf failed\n",
991 op->o_connid, 0, 0 );
993 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
994 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1000 /* NOTE: moved before overlays callback circling because
1001 * they may modify entry and other stuff in rs */
1002 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1006 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1007 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1010 size = i * sizeof(char *) + k;
1012 char *a_flags, **tmp;
1015 * Reuse previous memory - we likely need less space
1016 * for operational attributes
1018 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1020 if ( tmp == NULL ) {
1021 Debug( LDAP_DEBUG_ANY,
1022 "send_search_entry: conn %lu "
1023 "not enough memory "
1024 "for matched values filtering\n",
1025 op->o_connid, 0, 0 );
1026 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1027 send_ldap_error( op, rs, LDAP_OTHER,
1028 "not enough memory for matched values filtering" );
1032 a_flags = (char *)(e_flags + i);
1033 memset( a_flags, 0, k );
1034 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1035 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1036 e_flags[i] = a_flags;
1039 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1042 Debug( LDAP_DEBUG_ANY,
1043 "send_search_entry: conn %lu "
1044 "matched values filtering failed\n",
1045 op->o_connid ? op->o_connid : 0, 0, 0);
1046 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1047 send_ldap_error( op, rs, LDAP_OTHER,
1048 "matched values filtering error" );
1055 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1056 AttributeDescription *desc = a->a_desc;
1058 if ( rs->sr_attrs == NULL ) {
1059 /* all user attrs request, skip operational attributes */
1060 if( is_at_operational( desc->ad_type ) ) {
1065 /* specific attrs requested */
1066 if( is_at_operational( desc->ad_type ) ) {
1067 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1068 !ad_inlist( desc, rs->sr_attrs ) )
1073 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1079 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1080 ACL_READ, &acl_state ) )
1082 Debug( LDAP_DEBUG_ACL,
1083 "send_search_entry: conn %lu "
1084 "access to attribute %s not allowed\n",
1085 op->o_connid, desc->ad_cname.bv_val, 0 );
1090 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1092 Debug( LDAP_DEBUG_ANY,
1093 "send_search_entry: conn %lu "
1094 "ber_printf failed\n", op->o_connid, 0, 0 );
1096 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1097 send_ldap_error( op, rs, LDAP_OTHER,
1098 "encoding description error" );
1103 if ( ! attrsonly ) {
1104 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1105 if ( ! access_allowed( op, rs->sr_entry,
1106 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1108 Debug( LDAP_DEBUG_ACL,
1109 "send_search_entry: conn %lu "
1110 "access to %s, value %d not allowed\n",
1111 op->o_connid, desc->ad_cname.bv_val, i );
1116 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1120 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1121 Debug( LDAP_DEBUG_ANY,
1122 "send_search_entry: conn %lu ber_printf failed\n",
1123 op->o_connid, 0, 0 );
1125 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1126 send_ldap_error( op, rs, LDAP_OTHER,
1127 "encoding values error" );
1134 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -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, "encode end error" );
1148 slap_sl_free( e_flags, op->o_tmpmemctx );
1152 rc = ber_printf( ber, /*{{*/ "}N}" );
1155 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1159 #ifdef LDAP_CONNECTIONLESS
1160 if( op->o_conn && op->o_conn->c_is_udp ) {
1161 if ( op->o_protocol != LDAP_VERSION2 ) {
1162 rc = ber_printf( ber, /*{*/ "N}" );
1166 if ( op->o_res_ber == NULL ) {
1167 rc = ber_printf( ber, /*{*/ "N}" );
1172 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1174 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1175 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1180 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1181 be_entry_release_rw( op, rs->sr_entry, 0 );
1182 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1183 rs->sr_entry = NULL;
1186 if ( op->o_res_ber == NULL ) {
1187 bytes = send_ldap_ber( op->o_conn, ber );
1188 ber_free_buf( ber );
1191 Debug( LDAP_DEBUG_ANY,
1192 "send_search_entry: conn %lu ber write failed.\n",
1193 op->o_connid, 0, 0 );
1195 rc = LDAP_UNAVAILABLE;
1200 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1201 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1202 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1203 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1204 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1207 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1208 op->o_log_prefix, edn, 0, 0, 0 );
1210 Debug( LDAP_DEBUG_TRACE,
1211 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1216 if ( op->o_callback ) {
1217 (void)slap_cleanup_play( op, rs );
1221 slap_sl_free( e_flags, op->o_tmpmemctx );
1224 if ( rs->sr_operational_attrs ) {
1225 attrs_free( rs->sr_operational_attrs );
1226 rs->sr_operational_attrs = NULL;
1228 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1230 /* FIXME: I think rs->sr_type should be explicitly set to
1231 * REP_SEARCH here. That's what it was when we entered this
1232 * function. send_ldap_error may have changed it, but we
1233 * should set it back so that the cleanup functions know
1234 * what they're doing.
1236 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1238 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1240 entry_free( rs->sr_entry );
1241 rs->sr_entry = NULL;
1242 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1249 slap_send_search_reference( Operation *op, SlapReply *rs )
1251 BerElementBuffer berbuf;
1252 BerElement *ber = (BerElement *) &berbuf;
1256 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1257 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1259 rs->sr_type = REP_SEARCHREF;
1260 if ( op->o_callback ) {
1261 rc = slap_response_play( op, rs );
1262 if ( rc != SLAP_CB_CONTINUE ) {
1267 Debug( LDAP_DEBUG_TRACE,
1268 "=> send_search_reference: dn=\"%s\"\n",
1269 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1271 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1272 ad_entry, NULL, ACL_READ, NULL ) )
1274 Debug( LDAP_DEBUG_ACL,
1275 "send_search_reference: access to entry not allowed\n",
1281 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1282 ad_ref, NULL, ACL_READ, NULL ) )
1284 Debug( LDAP_DEBUG_ACL,
1285 "send_search_reference: access "
1286 "to reference not allowed\n",
1292 if( op->o_domain_scope ) {
1293 Debug( LDAP_DEBUG_ANY,
1294 "send_search_reference: domainScope control in (%s)\n",
1295 rs->sr_entry->e_dn, 0, 0 );
1300 if( rs->sr_ref == NULL ) {
1301 Debug( LDAP_DEBUG_ANY,
1302 "send_search_reference: null ref in (%s)\n",
1303 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1308 if( op->o_protocol < LDAP_VERSION3 ) {
1310 /* save the references for the result */
1311 if( rs->sr_ref[0].bv_val != NULL ) {
1312 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1318 #ifdef LDAP_CONNECTIONLESS
1319 if( op->o_conn && op->o_conn->c_is_udp ) {
1320 ber = op->o_res_ber;
1324 ber_init_w_nullc( ber, LBER_USE_DER );
1325 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1328 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1329 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1332 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1336 rc = ber_printf( ber, /*"{"*/ "N}" );
1340 Debug( LDAP_DEBUG_ANY,
1341 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1343 #ifdef LDAP_CONNECTIONLESS
1344 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1346 ber_free_buf( ber );
1347 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1352 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1353 be_entry_release_rw( op, rs->sr_entry, 0 );
1354 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1355 rs->sr_entry = NULL;
1358 #ifdef LDAP_CONNECTIONLESS
1359 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1361 bytes = send_ldap_ber( op->o_conn, ber );
1362 ber_free_buf( ber );
1365 rc = LDAP_UNAVAILABLE;
1367 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1368 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1369 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1370 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1371 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1373 #ifdef LDAP_CONNECTIONLESS
1376 if ( rs->sr_ref != NULL ) {
1379 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1380 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1381 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1386 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1387 op->o_log_prefix, 0, 0, 0, 0 );
1390 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1393 if ( op->o_callback ) {
1394 (void)slap_cleanup_play( op, rs );
1410 *code = LDAP_SUCCESS;
1414 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1415 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1422 while ( (s = strchr( s, '\n' )) != NULL ) {
1427 if ( (c = strchr( s, ':' )) != NULL ) {
1431 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1436 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1442 while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1443 if ( c[ 0 ] == '\0' ) {
1444 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1450 retcode = strtol( c, &next, 10 );
1451 if ( next == NULL || next == c ) {
1452 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1458 while ( isspace( (unsigned char) next[ 0 ] ) ) next++;
1459 if ( next[ 0 ] != '\0' ) {
1460 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1466 /* FIXME: what if it's larger that max int? */
1467 *code = (int)retcode;
1469 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1473 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1478 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1488 int slap_read_controls(
1492 const struct berval *oid,
1493 LDAPControl **ctrl )
1497 BerElementBuffer berbuf;
1498 BerElement *ber = (BerElement *) &berbuf;
1502 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1503 oid->bv_val, e->e_dn, 0 );
1506 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1507 op->o_preread_attrs : op->o_postread_attrs;
1509 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1510 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1512 ber_init2( ber, &bv, LBER_USE_DER );
1513 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1515 /* create new operation */
1517 /* FIXME: o_bd needed for ACL */
1518 myop.o_bd = op->o_bd;
1519 myop.o_res_ber = ber;
1520 myop.o_callback = NULL;
1521 myop.ors_slimit = 1;
1523 rc = slap_send_search_entry( &myop, rs );
1526 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1528 if( rc == LBER_ERROR ) return LDAP_OTHER;
1530 c.ldctl_oid = oid->bv_val;
1531 c.ldctl_iscritical = 0;
1533 if ( *ctrl == NULL ) {
1535 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1537 /* retry: free previous try */
1538 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1542 return LDAP_SUCCESS;
1545 /* Map API errors to protocol errors... */
1547 slap_map_api2result( SlapReply *rs )
1549 switch(rs->sr_err) {
1550 case LDAP_SERVER_DOWN:
1551 return LDAP_UNAVAILABLE;
1552 case LDAP_LOCAL_ERROR:
1554 case LDAP_ENCODING_ERROR:
1555 case LDAP_DECODING_ERROR:
1556 return LDAP_PROTOCOL_ERROR;
1558 return LDAP_UNAVAILABLE;
1559 case LDAP_AUTH_UNKNOWN:
1560 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1561 case LDAP_FILTER_ERROR:
1562 rs->sr_text = "Filter error";
1564 case LDAP_USER_CANCELLED:
1565 rs->sr_text = "User cancelled";
1567 case LDAP_PARAM_ERROR:
1568 return LDAP_PROTOCOL_ERROR;
1569 case LDAP_NO_MEMORY:
1571 case LDAP_CONNECT_ERROR:
1572 return LDAP_UNAVAILABLE;
1573 case LDAP_NOT_SUPPORTED:
1574 return LDAP_UNWILLING_TO_PERFORM;
1575 case LDAP_CONTROL_NOT_FOUND:
1576 return LDAP_PROTOCOL_ERROR;
1577 case LDAP_NO_RESULTS_RETURNED:
1578 return LDAP_NO_SUCH_OBJECT;
1579 case LDAP_MORE_RESULTS_TO_RETURN:
1580 rs->sr_text = "More results to return";
1582 case LDAP_CLIENT_LOOP:
1583 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1584 return LDAP_LOOP_DETECT;
1586 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1593 slap_attr_flags( AttributeName *an )
1595 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1598 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1601 flags |= an_find( an, &AllOper )
1602 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1603 flags |= an_find( an, &AllUser )
1604 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;