1 /* result.c - routines to send ldap results, errors, and referrals */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
33 #include <ac/string.h>
36 #include <ac/unistd.h>
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 int slap_replog_cb( Operation *op, SlapReply *rs )
59 if ( rs->sr_err == LDAP_SUCCESS ) {
62 return SLAP_CB_CONTINUE;
65 static char *v2ref( BerVarray ref, const char *text )
67 size_t len = 0, i = 0;
72 return ch_strdup(text);
80 if (text[len-1] != '\n') {
85 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
87 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
97 strcpy( v2+len, "Referral:" );
98 len += sizeof("Referral:");
100 for( i=0; ref[i].bv_val != NULL; i++ ) {
101 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
103 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
107 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
108 len += ref[i].bv_len;
109 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
119 slap_req2res( ber_tag_t tag )
124 case LDAP_REQ_COMPARE:
125 case LDAP_REQ_EXTENDED:
126 case LDAP_REQ_MODIFY:
127 case LDAP_REQ_MODRDN:
131 case LDAP_REQ_DELETE:
132 tag = LDAP_RES_DELETE;
135 case LDAP_REQ_ABANDON:
136 case LDAP_REQ_UNBIND:
140 case LDAP_REQ_SEARCH:
141 tag = LDAP_RES_SEARCH_RESULT;
151 static long send_ldap_ber(
157 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
159 /* write only one pdu at a time - wait til it's our turn */
160 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
162 /* lock the connection */
163 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
170 if ( connection_state_closing( conn ) ) {
171 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
172 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
177 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
184 * we got an error. if it's ewouldblock, we need to
185 * wait on the socket being writable. otherwise, figure
186 * it's a hard error and return.
189 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
190 err, sock_errstr(err), 0 );
192 if ( err != EWOULDBLOCK && err != EAGAIN ) {
193 connection_closing( conn, "connection lost on write" );
195 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
196 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
201 /* wait for socket to be write-ready */
202 conn->c_writewaiter = 1;
203 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
204 slapd_set_write( sd, 1 );
206 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
207 conn->c_writewaiter = 0;
210 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
211 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
217 send_ldap_control( BerElement *ber, LDAPControl *c )
223 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
225 if( c->ldctl_iscritical ) {
226 rc = ber_printf( ber, "b",
227 (ber_int_t) c->ldctl_iscritical ) ;
228 if( rc == -1 ) return rc;
231 if( c->ldctl_value.bv_val != NULL ) {
232 rc = ber_printf( ber, "O", &c->ldctl_value );
233 if( rc == -1 ) return rc;
236 rc = ber_printf( ber, /*{*/"N}" );
237 if( rc == -1 ) return rc;
243 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
250 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
251 if( rc == -1 ) return rc;
253 for( ; *c != NULL; c++) {
254 rc = send_ldap_control( ber, *c );
255 if( rc == -1 ) return rc;
259 /* this is a hack to avoid having to modify op->s_ctrls */
260 if( o->o_sortedresults ) {
261 BerElementBuffer berbuf;
262 BerElement *sber = (BerElement *) &berbuf;
264 BER_BVZERO( &sorted.ldctl_value );
265 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
266 sorted.ldctl_iscritical = 0;
268 ber_init2( sber, NULL, LBER_USE_DER );
270 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
272 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
276 (void) ber_free_buf( ber );
278 rc = send_ldap_control( ber, &sorted );
279 if( rc == -1 ) return rc;
283 rc = ber_printf( ber, /*{*/"N}" );
293 BerElementBuffer berbuf;
294 BerElement *ber = (BerElement *) &berbuf;
295 int rc = LDAP_SUCCESS;
298 if ( rs->sr_err == SLAPD_ABANDON ) {
303 if ( op->o_callback ) {
304 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
306 rc = SLAP_CB_CONTINUE;
307 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
308 sc_next = op->o_callback->sc_next;
309 if ( op->o_callback->sc_response ) {
310 rc = op->o_callback->sc_response( op, rs );
311 if ( op->o_callback != *sc_prev ) {
312 *sc_prev = op->o_callback;
314 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
316 sc_prev = &op->o_callback->sc_next;
320 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
323 #ifdef LDAP_CONNECTIONLESS
324 if (op->o_conn && op->o_conn->c_is_udp)
329 ber_init_w_nullc( ber, LBER_USE_DER );
330 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
333 Debug( LDAP_DEBUG_TRACE,
334 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
335 rs->sr_msgid, rs->sr_tag, rs->sr_err );
338 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
339 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
343 #ifdef LDAP_CONNECTIONLESS
344 if (op->o_conn && op->o_conn->c_is_udp &&
345 op->o_protocol == LDAP_VERSION2 )
347 rc = ber_printf( ber, "t{ess" /*"}"*/,
348 rs->sr_tag, rs->sr_err,
349 rs->sr_matched == NULL ? "" : rs->sr_matched,
350 rs->sr_text == NULL ? "" : rs->sr_text );
353 if ( rs->sr_type == REP_INTERMEDIATE ) {
354 rc = ber_printf( ber, "{it{" /*"}}"*/,
355 rs->sr_msgid, rs->sr_tag );
358 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
359 rs->sr_msgid, rs->sr_tag, rs->sr_err,
360 rs->sr_matched == NULL ? "" : rs->sr_matched,
361 rs->sr_text == NULL ? "" : rs->sr_text );
365 if ( rs->sr_ref != NULL ) {
366 assert( rs->sr_err == LDAP_REFERRAL );
367 rc = ber_printf( ber, "t{W}",
368 LDAP_TAG_REFERRAL, rs->sr_ref );
370 assert( rs->sr_err != LDAP_REFERRAL );
374 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
375 rc = ber_printf( ber, "tO",
376 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
380 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
382 if ( rs->sr_rspoid != NULL ) {
383 rc = ber_printf( ber, "ts",
384 rs->sr_type == REP_EXTENDED
385 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
388 if( rc != -1 && rs->sr_rspdata != NULL ) {
389 rc = ber_printf( ber, "tO",
390 rs->sr_type == REP_EXTENDED
391 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
397 rc = ber_printf( ber, /*"{"*/ "N}" );
401 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
405 rc = ber_printf( ber, /*"{"*/ "N}" );
408 #ifdef LDAP_CONNECTIONLESS
409 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
412 rc = ber_printf( ber, /*"{"*/ "N}" );
417 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
419 #ifdef LDAP_CONNECTIONLESS
420 if (!op->o_conn || op->o_conn->c_is_udp == 0)
429 bytes = send_ldap_ber( op->o_conn, ber );
430 #ifdef LDAP_CONNECTIONLESS
431 if (!op->o_conn || op->o_conn->c_is_udp == 0)
438 Debug( LDAP_DEBUG_ANY,
439 "send_ldap_response: ber write failed\n",
445 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
446 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
447 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
448 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
451 /* Tell caller that we did this for real, as opposed to being
452 * overridden by a callback
454 rc = SLAP_CB_CONTINUE;
457 if ( op->o_callback ) {
458 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
460 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
461 sc_next = op->o_callback->sc_next;
462 if ( op->o_callback->sc_cleanup ) {
463 (void)op->o_callback->sc_cleanup( op, rs );
464 if ( op->o_callback != *sc_prev ) {
465 *sc_prev = op->o_callback;
467 if ( !op->o_callback ) break;
469 sc_prev = &op->o_callback->sc_next;
475 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
476 free( (char *)rs->sr_matched );
477 rs->sr_matched = NULL;
480 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
481 ber_bvarray_free( rs->sr_ref );
490 send_ldap_disconnect( Operation *op, SlapReply *rs )
492 #define LDAP_UNSOLICITED_ERROR(e) \
493 ( (e) == LDAP_PROTOCOL_ERROR \
494 || (e) == LDAP_STRONG_AUTH_REQUIRED \
495 || (e) == LDAP_UNAVAILABLE )
497 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
499 rs->sr_type = REP_EXTENDED;
501 Debug( LDAP_DEBUG_TRACE,
502 "send_ldap_disconnect %d:%s\n",
503 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
505 if ( op->o_protocol < LDAP_VERSION3 ) {
506 rs->sr_rspoid = NULL;
507 rs->sr_tag = slap_req2res( op->o_tag );
508 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
511 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
512 rs->sr_tag = LDAP_RES_EXTENDED;
516 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
517 Statslog( LDAP_DEBUG_STATS,
518 "%s DISCONNECT tag=%lu err=%d text=%s\n",
519 op->o_log_prefix, rs->sr_tag, rs->sr_err,
520 rs->sr_text ? rs->sr_text : "", 0 );
525 slap_send_ldap_result( Operation *op, SlapReply *rs )
528 const char *otext = rs->sr_text;
529 BerVarray oref = rs->sr_ref;
531 rs->sr_type = REP_RESULT;
533 /* Propagate Abandons so that cleanup callbacks can be processed */
534 if ( rs->sr_err == SLAPD_ABANDON )
537 assert( !LDAP_API_ERROR( rs->sr_err ) );
539 Debug( LDAP_DEBUG_TRACE,
540 "send_ldap_result: %s p=%d\n",
541 op->o_log_prefix, op->o_protocol, 0 );
543 Debug( LDAP_DEBUG_ARGS,
544 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
545 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
546 rs->sr_text ? rs->sr_text : "" );
550 Debug( LDAP_DEBUG_ARGS,
551 "send_ldap_result: referral=\"%s\"\n",
552 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
556 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
558 if ( rs->sr_err == LDAP_REFERRAL ) {
559 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
560 if( op->o_domain_scope ) {
564 if( rs->sr_ref == NULL ) {
565 rs->sr_err = LDAP_NO_SUCH_OBJECT;
566 } else if ( op->o_protocol < LDAP_VERSION3 ) {
567 rs->sr_err = LDAP_PARTIAL_RESULTS;
571 if ( op->o_protocol < LDAP_VERSION3 ) {
572 tmp = v2ref( rs->sr_ref, rs->sr_text );
577 rs->sr_tag = slap_req2res( op->o_tag );
578 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
581 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
582 if ( op->o_tag == LDAP_REQ_SEARCH ) {
584 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
585 rs->sr_err, rs->sr_nentries );
587 Statslog( LDAP_DEBUG_STATS,
588 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
589 op->o_log_prefix, rs->sr_tag, nbuf,
590 rs->sr_text ? rs->sr_text : "", 0 );
592 Statslog( LDAP_DEBUG_STATS,
593 "%s RESULT tag=%lu err=%d text=%s\n",
594 op->o_log_prefix, rs->sr_tag, rs->sr_err,
595 rs->sr_text ? rs->sr_text : "", 0 );
599 if( tmp != NULL ) ch_free(tmp);
605 send_ldap_sasl( Operation *op, SlapReply *rs )
607 rs->sr_type = REP_SASL;
608 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
610 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
612 rs->sr_tag = slap_req2res( op->o_tag );
613 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
615 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
616 Statslog( LDAP_DEBUG_STATS,
617 "%s RESULT tag=%lu err=%d text=%s\n",
618 op->o_log_prefix, rs->sr_tag, rs->sr_err,
619 rs->sr_text ? rs->sr_text : "", 0 );
624 slap_send_ldap_extended( Operation *op, SlapReply *rs )
626 rs->sr_type = REP_EXTENDED;
628 Debug( LDAP_DEBUG_TRACE,
629 "send_ldap_extended: err=%d oid=%s len=%ld\n",
631 rs->sr_rspoid ? rs->sr_rspoid : "",
632 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
634 rs->sr_tag = slap_req2res( op->o_tag );
635 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
637 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
638 Statslog( LDAP_DEBUG_STATS,
639 "%s RESULT oid=%s err=%d text=%s\n",
640 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
641 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
646 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
648 rs->sr_type = REP_INTERMEDIATE;
649 Debug( LDAP_DEBUG_TRACE,
650 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
652 rs->sr_rspoid ? rs->sr_rspoid : "",
653 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
654 rs->sr_tag = LDAP_RES_INTERMEDIATE;
655 rs->sr_msgid = op->o_msgid;
656 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
657 Statslog( LDAP_DEBUG_STATS2,
658 "%s INTERM oid=%s\n",
660 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
665 slap_send_search_entry( Operation *op, SlapReply *rs )
667 BerElementBuffer berbuf;
668 BerElement *ber = (BerElement *) &berbuf;
670 int i, j, rc=-1, bytes;
673 AccessControlState acl_state = ACL_STATE_INIT;
675 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
677 /* a_flags: array of flags telling if the i-th element will be
678 * returned or filtered out
679 * e_flags: array of a_flags
681 char **e_flags = NULL;
683 rs->sr_type = REP_SEARCH;
685 /* eventually will loop through generated operational attribute types
686 * currently implemented types include:
687 * entryDN, subschemaSubentry, and hasSubordinates */
688 /* NOTE: moved before overlays callback circling because
689 * they may modify entry and other stuff in rs */
690 /* check for special all operational attributes ("+") type */
691 /* FIXME: maybe we could se this flag at the operation level;
692 * however, in principle the caller of send_search_entry() may
693 * change the attribute list at each call */
694 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
696 rc = backend_operational( op, rs );
701 if ( op->o_callback ) {
702 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
704 rc = SLAP_CB_CONTINUE;
705 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next )
707 sc_next = op->o_callback->sc_next;
708 if ( op->o_callback->sc_response ) {
709 rc = op->o_callback->sc_response( op, rs );
710 if ( op->o_callback != *sc_prev ) {
711 *sc_prev = op->o_callback;
713 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
715 sc_prev = &op->o_callback->sc_next;
719 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
722 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
723 op->o_connid, rs->sr_entry->e_name.bv_val,
724 op->ors_attrsonly ? " (attrsOnly)" : "" );
726 attrsonly = op->ors_attrsonly;
728 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
729 Debug( LDAP_DEBUG_ACL,
730 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
731 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
737 edn = rs->sr_entry->e_nname.bv_val;
739 if ( op->o_res_ber ) {
740 /* read back control or LDAP_CONNECTIONLESS */
745 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
746 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
748 ber_init2( ber, &bv, LBER_USE_DER );
749 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
752 #ifdef LDAP_CONNECTIONLESS
753 if ( op->o_conn && op->o_conn->c_is_udp ) {
755 if ( op->o_protocol == LDAP_VERSION2 ) {
756 rc = ber_printf(ber, "t{O{" /*}}*/,
757 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
759 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
760 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
764 if ( op->o_res_ber ) {
765 /* read back control */
766 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
768 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
769 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
773 Debug( LDAP_DEBUG_ANY,
774 "send_search_entry: conn %lu ber_printf failed\n",
775 op->o_connid, 0, 0 );
777 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
778 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
782 /* check for special all user attributes ("*") type */
783 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
785 /* create an array of arrays of flags. Each flag corresponds
786 * to particular value of attribute and equals 1 if value matches
787 * to ValuesReturnFilter or 0 if not
789 if ( op->o_vrFilter != NULL ) {
793 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
794 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
797 size = i * sizeof(char *) + k;
800 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
801 if( e_flags == NULL ) {
802 Debug( LDAP_DEBUG_ANY,
803 "send_search_entry: conn %lu slap_sl_calloc failed\n",
804 op->o_connid ? op->o_connid : 0, 0, 0 );
807 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
810 a_flags = (char *)(e_flags + i);
811 memset( a_flags, 0, k );
812 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
813 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
814 e_flags[i] = a_flags;
818 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
820 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
821 "conn %lu matched values filtering failed\n",
822 op->o_connid ? op->o_connid : 0, 0, 0 );
823 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
824 send_ldap_error( op, rs, LDAP_OTHER,
825 "matched values filtering error" );
831 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
832 AttributeDescription *desc = a->a_desc;
835 if ( rs->sr_attrs == NULL ) {
836 /* all user attrs request, skip operational attributes */
837 if( is_at_operational( desc->ad_type ) ) {
842 /* specific attrs requested */
843 if ( is_at_operational( desc->ad_type ) ) {
844 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
845 !ad_inlist( desc, rs->sr_attrs ) )
851 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
858 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
859 ACL_READ, &acl_state ) )
861 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
862 "conn %lu access to attribute %s not allowed\n",
863 op->o_connid, desc->ad_cname.bv_val, 0 );
867 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
868 Debug( LDAP_DEBUG_ANY,
869 "send_search_entry: conn %lu ber_printf failed\n",
870 op->o_connid, 0, 0 );
872 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
873 send_ldap_error( op, rs, LDAP_OTHER,
874 "encoding description error");
881 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
882 if ( ! access_allowed( op, rs->sr_entry,
883 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
885 Debug( LDAP_DEBUG_ACL,
886 "send_search_entry: conn %lu "
887 "access to attribute %s, value #%d not allowed\n",
888 op->o_connid, desc->ad_cname.bv_val, i );
893 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
900 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
901 Debug( LDAP_DEBUG_ANY,
902 "send_search_entry: conn %lu ber_printf failed\n",
903 op->o_connid, 0, 0 );
905 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
906 send_ldap_error( op, rs, LDAP_OTHER,
907 "encoding description error");
911 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
912 Debug( LDAP_DEBUG_ANY,
913 "send_search_entry: conn %lu "
914 "ber_printf failed.\n", op->o_connid, 0, 0 );
916 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
917 send_ldap_error( op, rs, LDAP_OTHER,
918 "encoding values error" );
924 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
925 Debug( LDAP_DEBUG_ANY,
926 "send_search_entry: conn %lu ber_printf failed\n",
927 op->o_connid, 0, 0 );
929 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
930 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
935 /* NOTE: moved before overlays callback circling because
936 * they may modify entry and other stuff in rs */
937 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
941 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
942 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
945 size = i * sizeof(char *) + k;
947 char *a_flags, **tmp;
950 * Reuse previous memory - we likely need less space
951 * for operational attributes
953 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
956 Debug( LDAP_DEBUG_ANY,
957 "send_search_entry: conn %lu "
959 "for matched values filtering\n",
960 op->o_connid, 0, 0 );
961 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
962 send_ldap_error( op, rs, LDAP_OTHER,
963 "not enough memory for matched values filtering" );
967 a_flags = (char *)(e_flags + i);
968 memset( a_flags, 0, k );
969 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
970 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
971 e_flags[i] = a_flags;
974 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
977 Debug( LDAP_DEBUG_ANY,
978 "send_search_entry: conn %lu "
979 "matched values filtering failed\n",
980 op->o_connid ? op->o_connid : 0, 0, 0);
981 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
982 send_ldap_error( op, rs, LDAP_OTHER,
983 "matched values filtering error" );
989 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
990 AttributeDescription *desc = a->a_desc;
992 if ( rs->sr_attrs == NULL ) {
993 /* all user attrs request, skip operational attributes */
994 if( is_at_operational( desc->ad_type ) ) {
999 /* specific attrs requested */
1000 if( is_at_operational( desc->ad_type ) ) {
1001 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1002 !ad_inlist( desc, rs->sr_attrs ) )
1007 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1013 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1014 ACL_READ, &acl_state ) )
1016 Debug( LDAP_DEBUG_ACL,
1017 "send_search_entry: conn %lu "
1018 "access to attribute %s not allowed\n",
1019 op->o_connid, desc->ad_cname.bv_val, 0 );
1024 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1026 Debug( LDAP_DEBUG_ANY,
1027 "send_search_entry: conn %lu "
1028 "ber_printf failed\n", op->o_connid, 0, 0 );
1030 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1031 send_ldap_error( op, rs, LDAP_OTHER,
1032 "encoding description error" );
1036 if ( ! attrsonly ) {
1037 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1038 if ( ! access_allowed( op, rs->sr_entry,
1039 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1041 Debug( LDAP_DEBUG_ACL,
1042 "send_search_entry: conn %lu "
1043 "access to %s, value %d not allowed\n",
1044 op->o_connid, desc->ad_cname.bv_val, i );
1049 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1053 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1054 Debug( LDAP_DEBUG_ANY,
1055 "send_search_entry: conn %lu ber_printf failed\n",
1056 op->o_connid, 0, 0 );
1058 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1059 send_ldap_error( op, rs, LDAP_OTHER,
1060 "encoding values error" );
1066 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1067 Debug( LDAP_DEBUG_ANY,
1068 "send_search_entry: conn %lu ber_printf failed\n",
1069 op->o_connid, 0, 0 );
1071 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1072 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1079 slap_sl_free( e_flags, op->o_tmpmemctx );
1083 rc = ber_printf( ber, /*{{*/ "}N}" );
1086 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1090 #ifdef LDAP_CONNECTIONLESS
1091 if( op->o_conn && op->o_conn->c_is_udp ) {
1092 if ( op->o_protocol != LDAP_VERSION2 ) {
1093 rc = ber_printf( ber, /*{*/ "N}" );
1097 if ( op->o_res_ber == NULL ) {
1098 rc = ber_printf( ber, /*{*/ "N}" );
1103 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1105 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1106 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1111 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1112 be_entry_release_rw( op, rs->sr_entry, 0 );
1113 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1114 rs->sr_entry = NULL;
1117 if ( op->o_res_ber == NULL ) {
1118 bytes = send_ldap_ber( op->o_conn, ber );
1119 ber_free_buf( ber );
1122 Debug( LDAP_DEBUG_ANY,
1123 "send_search_entry: conn %lu ber write failed.\n",
1124 op->o_connid, 0, 0 );
1131 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1132 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1133 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1134 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1135 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1138 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1139 op->o_log_prefix, edn, 0, 0, 0 );
1141 Debug( LDAP_DEBUG_TRACE,
1142 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1147 if ( op->o_callback ) {
1148 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1150 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1151 sc_next = op->o_callback->sc_next;
1152 if ( op->o_callback->sc_cleanup ) {
1153 (void)op->o_callback->sc_cleanup( op, rs );
1154 if ( op->o_callback != *sc_prev ) {
1155 *sc_prev = op->o_callback;
1157 if ( !op->o_callback ) break;
1159 sc_prev = &op->o_callback->sc_next;
1161 op->o_callback = sc;
1165 slap_sl_free( e_flags, op->o_tmpmemctx );
1168 if ( rs->sr_operational_attrs ) {
1169 attrs_free( rs->sr_operational_attrs );
1170 rs->sr_operational_attrs = NULL;
1172 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1174 /* FIXME: I think rs->sr_type should be explicitly set to
1175 * REP_SEARCH here. That's what it was when we entered this
1176 * function. send_ldap_error may have changed it, but we
1177 * should set it back so that the cleanup functions know
1178 * what they're doing.
1180 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1182 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1184 entry_free( rs->sr_entry );
1185 rs->sr_entry = NULL;
1186 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1193 slap_send_search_reference( Operation *op, SlapReply *rs )
1195 BerElementBuffer berbuf;
1196 BerElement *ber = (BerElement *) &berbuf;
1200 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1201 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1203 rs->sr_type = REP_SEARCHREF;
1204 if ( op->o_callback ) {
1205 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1207 rc = SLAP_CB_CONTINUE;
1208 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1209 sc_next = op->o_callback->sc_next;
1210 if ( op->o_callback->sc_response ) {
1211 rc = op->o_callback->sc_response( op, rs );
1212 if ( op->o_callback != *sc_prev ) {
1213 *sc_prev = op->o_callback;
1215 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1217 sc_prev = &op->o_callback->sc_next;
1220 op->o_callback = sc;
1221 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1224 Debug( LDAP_DEBUG_TRACE,
1225 "=> send_search_reference: dn=\"%s\"\n",
1226 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1228 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1229 ad_entry, NULL, ACL_READ, NULL ) )
1231 Debug( LDAP_DEBUG_ACL,
1232 "send_search_reference: access to entry not allowed\n",
1238 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1239 ad_ref, NULL, ACL_READ, NULL ) )
1241 Debug( LDAP_DEBUG_ACL,
1242 "send_search_reference: access "
1243 "to reference not allowed\n",
1249 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1250 if( op->o_domain_scope ) {
1251 Debug( LDAP_DEBUG_ANY,
1252 "send_search_reference: domainScope control in (%s)\n",
1253 rs->sr_entry->e_dn, 0, 0 );
1259 if( rs->sr_ref == NULL ) {
1260 Debug( LDAP_DEBUG_ANY,
1261 "send_search_reference: null ref in (%s)\n",
1262 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1267 if( op->o_protocol < LDAP_VERSION3 ) {
1269 /* save the references for the result */
1270 if( rs->sr_ref[0].bv_val != NULL ) {
1271 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1277 #ifdef LDAP_CONNECTIONLESS
1278 if( op->o_conn && op->o_conn->c_is_udp ) {
1279 ber = op->o_res_ber;
1283 ber_init_w_nullc( ber, LBER_USE_DER );
1284 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1287 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1288 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1291 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1295 rc = ber_printf( ber, /*"{"*/ "N}" );
1299 Debug( LDAP_DEBUG_ANY,
1300 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1302 #ifdef LDAP_CONNECTIONLESS
1303 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1305 ber_free_buf( ber );
1306 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1311 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1312 be_entry_release_rw( op, rs->sr_entry, 0 );
1313 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1314 rs->sr_entry = NULL;
1317 #ifdef LDAP_CONNECTIONLESS
1318 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1320 bytes = send_ldap_ber( op->o_conn, ber );
1321 ber_free_buf( ber );
1323 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1324 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1325 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1326 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1327 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1328 #ifdef LDAP_CONNECTIONLESS
1331 if ( rs->sr_ref != NULL ) {
1334 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1335 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1336 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1341 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1342 op->o_log_prefix, 0, 0, 0, 0 );
1345 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1348 if ( op->o_callback ) {
1349 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1351 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1352 sc_next = op->o_callback->sc_next;
1353 if ( op->o_callback->sc_cleanup ) {
1354 (void)op->o_callback->sc_cleanup( op, rs );
1355 if ( op->o_callback != *sc_prev ) {
1356 *sc_prev = op->o_callback;
1358 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1360 sc_prev = &op->o_callback->sc_next;
1362 op->o_callback = sc;
1378 *code = LDAP_SUCCESS;
1382 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1383 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1390 while ( (s = strchr( s, '\n' )) != NULL ) {
1395 if ( (c = strchr( s, ':' )) != NULL ) {
1399 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1403 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1407 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1412 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1422 int slap_read_controls(
1426 const struct berval *oid,
1427 LDAPControl **ctrl )
1431 BerElementBuffer berbuf;
1432 BerElement *ber = (BerElement *) &berbuf;
1436 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1437 oid->bv_val, e->e_dn, 0 );
1440 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1441 op->o_preread_attrs : op->o_postread_attrs;
1443 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1444 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1446 ber_init2( ber, &bv, LBER_USE_DER );
1447 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1449 /* create new operation */
1452 myop.o_res_ber = ber;
1453 myop.o_callback = NULL;
1455 rc = slap_send_search_entry( &myop, rs );
1458 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1460 if( rc == LBER_ERROR ) return LDAP_OTHER;
1462 c.ldctl_oid = oid->bv_val;
1463 c.ldctl_iscritical = 0;
1465 if ( *ctrl == NULL ) {
1467 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1469 /* retry: free previous try */
1470 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1474 return LDAP_SUCCESS;
1477 /* Map API errors to protocol errors... */
1479 slap_map_api2result( SlapReply *rs )
1481 switch(rs->sr_err) {
1482 case LDAP_SERVER_DOWN:
1483 return LDAP_UNAVAILABLE;
1484 case LDAP_LOCAL_ERROR:
1486 case LDAP_ENCODING_ERROR:
1487 case LDAP_DECODING_ERROR:
1488 return LDAP_PROTOCOL_ERROR;
1490 return LDAP_UNAVAILABLE;
1491 case LDAP_AUTH_UNKNOWN:
1492 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1493 case LDAP_FILTER_ERROR:
1494 rs->sr_text = "Filter error";
1496 case LDAP_USER_CANCELLED:
1497 rs->sr_text = "User cancelled";
1499 case LDAP_PARAM_ERROR:
1500 return LDAP_PROTOCOL_ERROR;
1501 case LDAP_NO_MEMORY:
1503 case LDAP_CONNECT_ERROR:
1504 return LDAP_UNAVAILABLE;
1505 case LDAP_NOT_SUPPORTED:
1506 return LDAP_UNWILLING_TO_PERFORM;
1507 case LDAP_CONTROL_NOT_FOUND:
1508 return LDAP_PROTOCOL_ERROR;
1509 case LDAP_NO_RESULTS_RETURNED:
1510 return LDAP_NO_SUCH_OBJECT;
1511 case LDAP_MORE_RESULTS_TO_RETURN:
1512 rs->sr_text = "More results to return";
1514 case LDAP_CLIENT_LOOP:
1515 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1516 return LDAP_LOOP_DETECT;
1518 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1525 slap_attr_flags( AttributeName *an )
1527 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1530 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1533 flags |= an_find( an, &AllOper )
1534 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1535 flags |= an_find( an, &AllUser )
1536 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;