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-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
33 #include <ac/string.h>
36 #include <ac/unistd.h>
41 #include "slapi/slapi.h"
44 const struct berval slap_dummy_bv = BER_BVNULL;
46 int slap_null_cb( Operation *op, SlapReply *rs )
51 int slap_freeself_cb( Operation *op, SlapReply *rs )
53 assert( op->o_callback );
55 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
56 op->o_callback = NULL;
58 return SLAP_CB_CONTINUE;
61 int slap_replog_cb( Operation *op, SlapReply *rs )
63 if ( rs->sr_err == LDAP_SUCCESS ) {
66 return SLAP_CB_CONTINUE;
69 static char *v2ref( BerVarray ref, const char *text )
71 size_t len = 0, i = 0;
76 return ch_strdup(text);
84 if (text[len-1] != '\n') {
89 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
91 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
101 strcpy( v2+len, "Referral:" );
102 len += sizeof("Referral:");
104 for( i=0; ref[i].bv_val != NULL; i++ ) {
105 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
107 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
111 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
112 len += ref[i].bv_len;
113 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
122 static ber_tag_t req2res( ber_tag_t tag )
127 case LDAP_REQ_COMPARE:
128 case LDAP_REQ_EXTENDED:
129 case LDAP_REQ_MODIFY:
130 case LDAP_REQ_MODRDN:
134 case LDAP_REQ_DELETE:
135 tag = LDAP_RES_DELETE;
138 case LDAP_REQ_ABANDON:
139 case LDAP_REQ_UNBIND:
143 case LDAP_REQ_SEARCH:
144 tag = LDAP_RES_SEARCH_RESULT;
154 static long send_ldap_ber(
160 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
162 /* write only one pdu at a time - wait til it's our turn */
163 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
165 /* lock the connection */
166 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
173 if ( connection_state_closing( conn ) ) {
174 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
175 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
180 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
187 * we got an error. if it's ewouldblock, we need to
188 * wait on the socket being writable. otherwise, figure
189 * it's a hard error and return.
192 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
193 err, sock_errstr(err), 0 );
195 if ( err != EWOULDBLOCK && err != EAGAIN ) {
196 connection_closing( conn );
198 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
199 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
204 /* wait for socket to be write-ready */
205 conn->c_writewaiter = 1;
206 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
207 slapd_set_write( sd, 1 );
209 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
210 conn->c_writewaiter = 0;
213 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
214 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
220 send_ldap_control( BerElement *ber, LDAPControl *c )
226 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
228 if( c->ldctl_iscritical ) {
229 rc = ber_printf( ber, "b",
230 (ber_int_t) c->ldctl_iscritical ) ;
231 if( rc == -1 ) return rc;
234 if( c->ldctl_value.bv_val != NULL ) {
235 rc = ber_printf( ber, "O", &c->ldctl_value );
236 if( rc == -1 ) return rc;
239 rc = ber_printf( ber, /*{*/"N}" );
240 if( rc == -1 ) return rc;
246 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
250 LDAPControl **sctrls = NULL;
253 * Retrieve any additional controls that may be set by the
257 if ( o->o_pb && slapi_pblock_get( o->o_pb, SLAPI_RESCONTROLS, &sctrls ) != 0 ) {
261 if ( c == NULL && sctrls == NULL ) return 0;
263 if( c == NULL ) return 0;
264 #endif /* LDAP_SLAPI */
266 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
267 if( rc == -1 ) return rc;
271 #endif /* LDAP_SLAPI */
272 for( ; *c != NULL; c++) {
273 rc = send_ldap_control( ber, *c );
274 if( rc == -1 ) return rc;
278 if ( sctrls != NULL ) {
279 for ( c = sctrls; *c != NULL; c++ ) {
280 rc = send_ldap_control( ber, *c );
281 if( rc == -1 ) return rc;
284 #endif /* LDAP_SLAPI */
286 rc = ber_printf( ber, /*{*/"N}" );
296 BerElementBuffer berbuf;
297 BerElement *ber = (BerElement *) &berbuf;
298 int rc = LDAP_SUCCESS;
301 if ( op->o_callback ) {
303 slap_callback *sc = op->o_callback,
304 *sc_next = op->o_callback;
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 ( first && op->o_callback == NULL ) {
314 if ( rc != SLAP_CB_CONTINUE ) break;
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 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
386 if( rc != -1 && rs->sr_rspdata != NULL ) {
387 rc = ber_printf( ber, "tO",
388 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
393 rc = ber_printf( ber, /*"{"*/ "N}" );
397 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
401 rc = ber_printf( ber, /*"{"*/ "N}" );
404 #ifdef LDAP_CONNECTIONLESS
405 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
408 rc = ber_printf( ber, /*"{"*/ "N}" );
413 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
415 #ifdef LDAP_CONNECTIONLESS
416 if (!op->o_conn || op->o_conn->c_is_udp == 0)
425 bytes = send_ldap_ber( op->o_conn, ber );
426 #ifdef LDAP_CONNECTIONLESS
427 if (!op->o_conn || op->o_conn->c_is_udp == 0)
434 Debug( LDAP_DEBUG_ANY,
435 "send_ldap_response: ber write failed\n",
443 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
444 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
445 (void *)rs->sr_matched );
446 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
448 #endif /* LDAP_SLAPI */
450 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
452 mpz_add_ui( slap_counters.sc_pdu, slap_counters.sc_pdu, 1 );
453 mpz_add_ui( slap_counters.sc_bytes, slap_counters.sc_bytes, bytes );
454 #else /* ! HAVE_GMP */
455 slap_counters.sc_bytes += bytes;
456 slap_counters.sc_pdu++;
457 #endif /* ! HAVE_GMP */
458 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
461 /* Tell caller that we did this for real, as opposed to being
462 * overridden by a callback
464 rc = SLAP_CB_CONTINUE;
467 if ( op->o_callback ) {
469 slap_callback *sc = op->o_callback,
470 *sc_next = op->o_callback;
472 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
473 sc_next = op->o_callback->sc_next;
474 if ( op->o_callback->sc_cleanup ) {
475 (void)op->o_callback->sc_cleanup( op, rs );
476 if ( first && op->o_callback == NULL ) {
487 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
488 free( (char *)rs->sr_matched );
489 rs->sr_matched = NULL;
492 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
493 ber_bvarray_free( rs->sr_ref );
502 send_ldap_disconnect( Operation *op, SlapReply *rs )
504 #define LDAP_UNSOLICITED_ERROR(e) \
505 ( (e) == LDAP_PROTOCOL_ERROR \
506 || (e) == LDAP_STRONG_AUTH_REQUIRED \
507 || (e) == LDAP_UNAVAILABLE )
509 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
511 rs->sr_type = REP_EXTENDED;
513 Debug( LDAP_DEBUG_TRACE,
514 "send_ldap_disconnect %d:%s\n",
515 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
517 if ( op->o_protocol < LDAP_VERSION3 ) {
518 rs->sr_rspoid = NULL;
519 rs->sr_tag = req2res( op->o_tag );
520 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
523 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
524 rs->sr_tag = LDAP_RES_EXTENDED;
528 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
529 Statslog( LDAP_DEBUG_STATS,
530 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
531 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
532 rs->sr_text ? rs->sr_text : "" );
537 slap_send_ldap_result( Operation *op, SlapReply *rs )
540 const char *otext = rs->sr_text;
541 BerVarray oref = rs->sr_ref;
543 rs->sr_type = REP_RESULT;
545 assert( !LDAP_API_ERROR( rs->sr_err ));
547 Debug( LDAP_DEBUG_TRACE,
548 "send_ldap_result: conn=%lu op=%lu p=%d\n",
549 op->o_connid, op->o_opid, op->o_protocol );
551 Debug( LDAP_DEBUG_ARGS,
552 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
553 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
554 rs->sr_text ? rs->sr_text : "" );
558 Debug( LDAP_DEBUG_ARGS,
559 "send_ldap_result: referral=\"%s\"\n",
560 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
564 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
566 if ( rs->sr_err == LDAP_REFERRAL ) {
567 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
568 if( op->o_domain_scope ) {
572 if( rs->sr_ref == NULL ) {
573 rs->sr_err = LDAP_NO_SUCH_OBJECT;
574 } else if ( op->o_protocol < LDAP_VERSION3 ) {
575 rs->sr_err = LDAP_PARTIAL_RESULTS;
581 * Call pre-result plugins. To avoid infinite recursion plugins
582 * should just set SLAPI_RESULT_CODE rather than sending a
583 * result if they wish to change the result.
585 if ( op->o_pb != NULL ) {
586 slapi_int_pblock_set_operation( op->o_pb, op );
587 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
588 (void *)rs->sr_err );
589 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
590 (void *)rs->sr_text );
591 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
592 (void *)rs->sr_matched );
594 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
597 #endif /* LDAP_SLAPI */
599 if ( op->o_protocol < LDAP_VERSION3 ) {
600 tmp = v2ref( rs->sr_ref, rs->sr_text );
605 rs->sr_tag = req2res( op->o_tag );
606 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
608 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
609 if ( op->o_tag == LDAP_REQ_SEARCH ) {
611 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
612 rs->sr_err, rs->sr_nentries );
614 Statslog( LDAP_DEBUG_STATS,
615 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
616 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
617 rs->sr_text ? rs->sr_text : "" );
619 Statslog( LDAP_DEBUG_STATS,
620 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
621 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
622 rs->sr_text ? rs->sr_text : "" );
626 if( tmp != NULL ) ch_free(tmp);
632 send_ldap_sasl( Operation *op, SlapReply *rs )
634 rs->sr_type = REP_SASL;
635 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
637 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
639 rs->sr_tag = req2res( op->o_tag );
640 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
642 send_ldap_response( op, rs );
646 slap_send_ldap_extended( Operation *op, SlapReply *rs )
648 rs->sr_type = REP_EXTENDED;
650 Debug( LDAP_DEBUG_TRACE,
651 "send_ldap_extended: err=%d oid=%s len=%ld\n",
653 rs->sr_rspoid ? rs->sr_rspoid : "",
654 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
656 rs->sr_tag = req2res( op->o_tag );
657 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
659 send_ldap_response( op, rs );
663 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
665 rs->sr_type = REP_INTERMEDIATE;
666 Debug( LDAP_DEBUG_TRACE,
667 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
669 rs->sr_rspoid ? rs->sr_rspoid : "",
670 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
671 rs->sr_tag = LDAP_RES_INTERMEDIATE;
672 rs->sr_msgid = op->o_msgid;
673 send_ldap_response( op, rs );
677 slap_send_search_entry( Operation *op, SlapReply *rs )
679 BerElementBuffer berbuf;
680 BerElement *ber = (BerElement *) &berbuf;
682 int i, j, rc=-1, bytes;
685 AccessControlState acl_state = ACL_STATE_INIT;
687 /* Support for computed attribute plugins */
688 computed_attr_context ctx;
691 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
693 /* a_flags: array of flags telling if the i-th element will be
694 * returned or filtered out
695 * e_flags: array of a_flags
697 char **e_flags = NULL;
699 rs->sr_type = REP_SEARCH;
701 /* eventually will loop through generated operational attribute types
702 * currently implemented types include:
703 * entryDN, subschemaSubentry, and hasSubordinates */
704 /* NOTE: moved before overlays callback circling because
705 * they may modify entry and other stuff in rs */
706 /* check for special all operational attributes ("+") type */
707 /* FIXME: maybe we could se this flag at the operation level;
708 * however, in principle the caller of send_search_entry() may
709 * change the attribute list at each call */
710 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
712 rc = backend_operational( op, rs );
717 if ( op->o_callback ) {
719 slap_callback *sc = op->o_callback,
720 *sc_next = op->o_callback;
722 rc = SLAP_CB_CONTINUE;
723 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
724 sc_next = op->o_callback->sc_next;
725 if ( op->o_callback->sc_response ) {
726 rc = op->o_callback->sc_response( op, rs );
727 if ( first && op->o_callback == NULL ) {
730 if ( rc != SLAP_CB_CONTINUE ) break;
736 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
739 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
740 op->o_connid, rs->sr_entry->e_name.bv_val,
741 op->ors_attrsonly ? " (attrsOnly)" : "" );
743 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
744 Debug( LDAP_DEBUG_ACL,
745 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
746 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
752 edn = rs->sr_entry->e_nname.bv_val;
754 if ( op->o_res_ber ) {
755 /* read back control or LDAP_CONNECTIONLESS */
761 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
762 bv.bv_len = siz + len;
763 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
765 ber_init2( ber, &bv, LBER_USE_DER );
766 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
769 #ifdef LDAP_CONNECTIONLESS
770 if ( op->o_conn && op->o_conn->c_is_udp ) {
772 if ( op->o_protocol == LDAP_VERSION2 ) {
773 rc = ber_printf(ber, "t{O{" /*}}*/,
774 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
776 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
777 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
781 if ( op->o_res_ber ) {
782 /* read back control */
783 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
785 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
786 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
790 Debug( LDAP_DEBUG_ANY,
791 "send_search_entry: conn %lu ber_printf failed\n",
792 op->o_connid, 0, 0 );
794 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
795 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
799 /* check for special all user attributes ("*") type */
800 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
802 /* create an array of arrays of flags. Each flag corresponds
803 * to particular value of attribute and equals 1 if value matches
804 * to ValuesReturnFilter or 0 if not
806 if ( op->o_vrFilter != NULL ) {
810 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
811 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
814 size = i * sizeof(char *) + k;
817 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
818 if( e_flags == NULL ) {
819 Debug( LDAP_DEBUG_ANY,
820 "send_search_entry: conn %lu slap_sl_calloc failed\n",
821 op->o_connid ? op->o_connid : 0, 0, 0 );
824 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
827 a_flags = (char *)(e_flags + i);
828 memset( a_flags, 0, k );
829 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
830 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
831 e_flags[i] = a_flags;
835 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
837 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
838 "conn %lu matched values filtering failed\n",
839 op->o_connid ? op->o_connid : 0, 0, 0 );
840 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
841 send_ldap_error( op, rs, LDAP_OTHER,
842 "matched values filtering error" );
848 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
849 AttributeDescription *desc = a->a_desc;
852 if ( rs->sr_attrs == NULL ) {
853 /* all attrs request, skip operational attributes */
854 if( is_at_operational( desc->ad_type ) ) {
859 /* specific attrs requested */
860 if ( is_at_operational( desc->ad_type ) ) {
861 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
862 !ad_inlist( desc, rs->sr_attrs ) )
868 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
875 if ( op->ors_attrsonly ) {
876 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
877 ACL_READ, &acl_state ) )
879 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
880 "conn %lu access to attribute %s not allowed\n",
881 op->o_connid, desc->ad_cname.bv_val, 0 );
885 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
886 Debug( LDAP_DEBUG_ANY,
887 "send_search_entry: conn %lu ber_printf failed\n",
888 op->o_connid, 0, 0 );
890 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
891 send_ldap_error( op, rs, LDAP_OTHER,
892 "encoding description error");
899 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
900 if ( ! access_allowed( op, rs->sr_entry,
901 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
903 Debug( LDAP_DEBUG_ACL,
904 "send_search_entry: conn %lu "
905 "access to attribute %s, value #%d not allowed\n",
906 op->o_connid, desc->ad_cname.bv_val, i );
911 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
918 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
919 Debug( LDAP_DEBUG_ANY,
920 "send_search_entry: conn %lu ber_printf failed\n",
921 op->o_connid, 0, 0 );
923 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
924 send_ldap_error( op, rs, LDAP_OTHER,
925 "encoding description error");
929 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
930 Debug( LDAP_DEBUG_ANY,
931 "send_search_entry: conn %lu "
932 "ber_printf failed.\n", op->o_connid, 0, 0 );
934 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
935 send_ldap_error( op, rs, LDAP_OTHER,
936 "encoding values error" );
942 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
943 Debug( LDAP_DEBUG_ANY,
944 "send_search_entry: conn %lu ber_printf failed\n",
945 op->o_connid, 0, 0 );
947 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
948 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
953 /* NOTE: moved before overlays callback circling because
954 * they may modify entry and other stuff in rs */
955 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
959 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
960 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
963 size = i * sizeof(char *) + k;
965 char *a_flags, **tmp;
968 * Reuse previous memory - we likely need less space
969 * for operational attributes
971 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
974 Debug( LDAP_DEBUG_ANY,
975 "send_search_entry: conn %lu "
977 "for matched values filtering\n",
978 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 "not enough memory for matched values filtering" );
985 a_flags = (char *)(e_flags + i);
986 memset( a_flags, 0, k );
987 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
988 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
989 e_flags[i] = a_flags;
992 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
995 Debug( LDAP_DEBUG_ANY,
996 "send_search_entry: conn %lu "
997 "matched values filtering failed\n",
998 op->o_connid ? op->o_connid : 0, 0, 0);
999 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1000 send_ldap_error( op, rs, LDAP_OTHER,
1001 "matched values filtering error" );
1007 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1008 AttributeDescription *desc = a->a_desc;
1010 if ( rs->sr_attrs == NULL ) {
1011 /* all attrs request, skip operational attributes */
1012 if( is_at_operational( desc->ad_type ) ) {
1017 /* specific attrs requested */
1018 if( is_at_operational( desc->ad_type ) ) {
1019 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1020 !ad_inlist( desc, rs->sr_attrs ) )
1025 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1031 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1032 ACL_READ, &acl_state ) )
1034 Debug( LDAP_DEBUG_ACL,
1035 "send_search_entry: conn %lu "
1036 "access to attribute %s not allowed\n",
1037 op->o_connid, desc->ad_cname.bv_val, 0 );
1042 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1044 Debug( LDAP_DEBUG_ANY,
1045 "send_search_entry: conn %lu "
1046 "ber_printf failed\n", op->o_connid, 0, 0 );
1048 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1049 send_ldap_error( op, rs, LDAP_OTHER,
1050 "encoding description error" );
1054 if ( ! op->ors_attrsonly ) {
1055 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1056 if ( ! access_allowed( op, rs->sr_entry,
1057 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1059 Debug( LDAP_DEBUG_ACL,
1060 "send_search_entry: conn %lu "
1061 "access to %s, value %d not allowed\n",
1062 op->o_connid, desc->ad_cname.bv_val, i );
1067 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1071 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1072 Debug( LDAP_DEBUG_ANY,
1073 "send_search_entry: conn %lu ber_printf failed\n",
1074 op->o_connid, 0, 0 );
1076 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1077 send_ldap_error( op, rs, LDAP_OTHER,
1078 "encoding values error" );
1084 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1085 Debug( LDAP_DEBUG_ANY,
1086 "send_search_entry: conn %lu ber_printf failed\n",
1087 op->o_connid, 0, 0 );
1089 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1090 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1097 * First, setup the computed attribute context that is
1098 * passed to all plugins.
1101 ctx.cac_pb = op->o_pb;
1102 ctx.cac_attrs = rs->sr_attrs;
1103 ctx.cac_attrsonly = op->ors_attrsonly;
1104 ctx.cac_userattrs = userattrs;
1105 ctx.cac_opattrs = rs->sr_attr_flags;
1106 ctx.cac_acl_state = acl_state;
1107 ctx.cac_private = (void *)ber;
1110 * For each client requested attribute, call the plugins.
1112 if ( rs->sr_attrs != NULL ) {
1113 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1114 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1115 rs->sr_entry, slapi_int_compute_output_ber );
1116 if ( rc == 1 ) break;
1120 * Technically we shouldn't be returning operational attributes
1121 * when the user requested only user attributes. We'll let the
1122 * plugin decide whether to be naughty or not.
1124 rc = compute_evaluator( &ctx, "*",
1125 rs->sr_entry, slapi_int_compute_output_ber );
1128 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1129 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1133 #endif /* LDAP_SLAPI */
1137 slap_sl_free( e_flags, op->o_tmpmemctx );
1141 rc = ber_printf( ber, /*{{*/ "}N}" );
1144 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1148 #ifdef LDAP_CONNECTIONLESS
1149 if( op->o_conn && op->o_conn->c_is_udp ) {
1150 if ( op->o_protocol != LDAP_VERSION2 ) {
1151 rc = ber_printf( ber, /*{*/ "N}" );
1155 if ( op->o_res_ber == NULL ) {
1156 rc = ber_printf( ber, /*{*/ "N}" );
1161 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1163 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1164 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1169 if ( op->o_res_ber == NULL ) {
1170 bytes = send_ldap_ber( op->o_conn, ber );
1171 ber_free_buf( ber );
1174 Debug( LDAP_DEBUG_ANY,
1175 "send_search_entry: conn %lu ber write failed.\n",
1176 op->o_connid, 0, 0 );
1183 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1185 mpz_add_ui( slap_counters.sc_bytes, slap_counters.sc_bytes, bytes );
1186 mpz_add_ui( slap_counters.sc_entries, slap_counters.sc_entries, 1 );
1187 mpz_add_ui( slap_counters.sc_pdu, slap_counters.sc_pdu, 1 );
1188 #else /* ! HAVE_GMP */
1189 slap_counters.sc_bytes += bytes;
1190 slap_counters.sc_entries++;
1191 slap_counters.sc_pdu++;
1192 #endif /* ! HAVE_GMP */
1193 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1196 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1197 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1199 Debug( LDAP_DEBUG_TRACE,
1200 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1205 if ( op->o_callback ) {
1207 slap_callback *sc = op->o_callback,
1208 *sc_next = op->o_callback;
1210 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1211 sc_next = op->o_callback->sc_next;
1212 if ( op->o_callback->sc_cleanup ) {
1213 (void)op->o_callback->sc_cleanup( op, rs );
1214 if ( first && op->o_callback == NULL ) {
1221 op->o_callback = sc;
1225 slap_sl_free( e_flags, op->o_tmpmemctx );
1228 if ( rs->sr_operational_attrs ) {
1229 attrs_free( rs->sr_operational_attrs );
1230 rs->sr_operational_attrs = NULL;
1232 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1234 /* FIXME: I think rs->sr_type should be explicitly set to
1235 * REP_SEARCH here. That's what it was when we entered this
1236 * function. send_ldap_error may have changed it, but we
1237 * should set it back so that the cleanup functions know
1238 * what they're doing.
1240 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1242 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1244 entry_free( rs->sr_entry );
1245 rs->sr_entry = NULL;
1246 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1253 slap_send_search_reference( Operation *op, SlapReply *rs )
1255 BerElementBuffer berbuf;
1256 BerElement *ber = (BerElement *) &berbuf;
1260 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1261 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1263 rs->sr_type = REP_SEARCHREF;
1264 if ( op->o_callback ) {
1266 slap_callback *sc = op->o_callback,
1267 *sc_next = op->o_callback;
1269 rc = SLAP_CB_CONTINUE;
1270 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1271 sc_next = op->o_callback->sc_next;
1272 if ( op->o_callback->sc_response ) {
1273 rc = op->o_callback->sc_response( op, rs );
1274 if ( first && op->o_callback == NULL ) {
1277 if ( rc != SLAP_CB_CONTINUE ) break;
1282 op->o_callback = sc;
1283 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1286 Debug( LDAP_DEBUG_TRACE,
1287 "=> send_search_reference: dn=\"%s\"\n",
1288 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1290 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1291 ad_entry, NULL, ACL_READ, NULL ) )
1293 Debug( LDAP_DEBUG_ACL,
1294 "send_search_reference: access to entry not allowed\n",
1300 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1301 ad_ref, NULL, ACL_READ, NULL ) )
1303 Debug( LDAP_DEBUG_ACL,
1304 "send_search_reference: access "
1305 "to reference not allowed\n",
1311 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1312 if( op->o_domain_scope ) {
1313 Debug( LDAP_DEBUG_ANY,
1314 "send_search_reference: domainScope control in (%s)\n",
1315 rs->sr_entry->e_dn, 0, 0 );
1321 if( rs->sr_ref == NULL ) {
1322 Debug( LDAP_DEBUG_ANY,
1323 "send_search_reference: null ref in (%s)\n",
1324 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1329 if( op->o_protocol < LDAP_VERSION3 ) {
1331 /* save the references for the result */
1332 if( rs->sr_ref[0].bv_val != NULL ) {
1333 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1339 #ifdef LDAP_CONNECTIONLESS
1340 if( op->o_conn && op->o_conn->c_is_udp ) {
1341 ber = op->o_res_ber;
1345 ber_init_w_nullc( ber, LBER_USE_DER );
1346 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1349 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1350 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1353 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1357 rc = ber_printf( ber, /*"{"*/ "N}" );
1361 Debug( LDAP_DEBUG_ANY,
1362 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1364 #ifdef LDAP_CONNECTIONLESS
1365 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1367 ber_free_buf( ber );
1368 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1372 #ifdef LDAP_CONNECTIONLESS
1373 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1375 bytes = send_ldap_ber( op->o_conn, ber );
1376 ber_free_buf( ber );
1378 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1380 mpz_add_ui( slap_counters.sc_bytes, slap_counters.sc_bytes, bytes );
1381 mpz_add_ui( slap_counters.sc_refs, slap_counters.sc_refs, 1 );
1382 mpz_add_ui( slap_counters.sc_pdu, slap_counters.sc_pdu, 1 );
1383 #else /* ! HAVE_GMP */
1384 slap_counters.sc_bytes += bytes;
1385 slap_counters.sc_refs++;
1386 slap_counters.sc_pdu++;
1387 #endif /* ! HAVE_GMP */
1388 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1389 #ifdef LDAP_CONNECTIONLESS
1393 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1394 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1396 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1399 if ( op->o_callback ) {
1401 slap_callback *sc = op->o_callback,
1402 *sc_next = op->o_callback;
1404 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1405 sc_next = op->o_callback->sc_next;
1406 if ( op->o_callback->sc_cleanup ) {
1407 (void)op->o_callback->sc_cleanup( op, rs );
1408 if ( first && op->o_callback == NULL ) {
1415 op->o_callback = sc;
1432 *code = LDAP_SUCCESS;
1436 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1437 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1444 while ( (s = strchr( s, '\n' )) != NULL ) {
1449 if ( (c = strchr( s, ':' )) != NULL ) {
1453 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1457 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1461 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1466 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1476 int slap_read_controls(
1480 const struct berval *oid,
1481 LDAPControl **ctrl )
1485 BerElementBuffer berbuf;
1486 BerElement *ber = (BerElement *) &berbuf;
1491 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1492 oid->bv_val, e->e_dn, 0 );
1495 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1496 op->o_preread_attrs : op->o_postread_attrs;
1498 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1499 bv.bv_len = siz + len;
1500 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1502 ber_init2( ber, &bv, LBER_USE_DER );
1503 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1505 /* create new operation */
1508 myop.o_res_ber = ber;
1510 rc = slap_send_search_entry( &myop, rs );
1513 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1515 if( rc == LBER_ERROR ) return LDAP_OTHER;
1517 c.ldctl_oid = oid->bv_val;
1518 c.ldctl_iscritical = 0;
1520 if ( ctrl == NULL ) {
1522 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1524 /* retry: free previous try */
1525 slap_sl_free( (*ctrl)->ldctl_value.bv_val, &op->o_tmpmemctx );
1529 return LDAP_SUCCESS;
1532 /* Map API errors to protocol errors... */
1534 slap_map_api2result( SlapReply *rs )
1536 switch(rs->sr_err) {
1537 case LDAP_SERVER_DOWN:
1538 return LDAP_UNAVAILABLE;
1539 case LDAP_LOCAL_ERROR:
1541 case LDAP_ENCODING_ERROR:
1542 case LDAP_DECODING_ERROR:
1543 return LDAP_PROTOCOL_ERROR;
1545 return LDAP_UNAVAILABLE;
1546 case LDAP_AUTH_UNKNOWN:
1547 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1548 case LDAP_FILTER_ERROR:
1549 rs->sr_text = "Filter error";
1551 case LDAP_USER_CANCELLED:
1552 rs->sr_text = "User cancelled";
1554 case LDAP_PARAM_ERROR:
1555 return LDAP_PROTOCOL_ERROR;
1556 case LDAP_NO_MEMORY:
1558 case LDAP_CONNECT_ERROR:
1559 return LDAP_UNAVAILABLE;
1560 case LDAP_NOT_SUPPORTED:
1561 return LDAP_UNWILLING_TO_PERFORM;
1562 case LDAP_CONTROL_NOT_FOUND:
1563 return LDAP_PROTOCOL_ERROR;
1564 case LDAP_NO_RESULTS_RETURNED:
1565 return LDAP_NO_SUCH_OBJECT;
1566 case LDAP_MORE_RESULTS_TO_RETURN:
1567 rs->sr_text = "More results to return";
1569 case LDAP_CLIENT_LOOP:
1570 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1571 return LDAP_LOOP_DETECT;
1573 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1580 slap_attr_flags( AttributeName *an )
1582 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1585 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1588 flags |= an_find( an, &AllOper ) ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1589 flags |= an_find( an, &AllUser ) ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;