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:") );
92 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
94 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
105 strcpy( v2+len, "Referral:" );
106 len += sizeof("Referral:");
108 for( i=0; ref[i].bv_val != NULL; i++ ) {
109 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
112 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
114 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
119 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
120 len += ref[i].bv_len;
121 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
130 static ber_tag_t req2res( ber_tag_t tag )
135 case LDAP_REQ_COMPARE:
136 case LDAP_REQ_EXTENDED:
137 case LDAP_REQ_MODIFY:
138 case LDAP_REQ_MODRDN:
142 case LDAP_REQ_DELETE:
143 tag = LDAP_RES_DELETE;
146 case LDAP_REQ_ABANDON:
147 case LDAP_REQ_UNBIND:
151 case LDAP_REQ_SEARCH:
152 tag = LDAP_RES_SEARCH_RESULT;
162 static long send_ldap_ber(
168 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
170 /* write only one pdu at a time - wait til it's our turn */
171 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
173 /* lock the connection */
174 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
181 if ( connection_state_closing( conn ) ) {
182 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
183 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
188 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
195 * we got an error. if it's ewouldblock, we need to
196 * wait on the socket being writable. otherwise, figure
197 * it's a hard error and return.
201 LDAP_LOG( OPERATION, ERR,
202 "send_ldap_ber: conn %lu ber_flush failed err=%d (%s)\n",
203 conn ? conn->c_connid : 0, err, sock_errstr(err) );
205 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
206 err, sock_errstr(err), 0 );
209 if ( err != EWOULDBLOCK && err != EAGAIN ) {
210 connection_closing( conn );
212 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
213 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
218 /* wait for socket to be write-ready */
219 conn->c_writewaiter = 1;
220 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
221 slapd_set_write( sd, 1 );
223 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
224 conn->c_writewaiter = 0;
227 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
228 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
234 send_ldap_control( BerElement *ber, LDAPControl *c )
240 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
242 if( c->ldctl_iscritical ) {
243 rc = ber_printf( ber, "b",
244 (ber_int_t) c->ldctl_iscritical ) ;
245 if( rc == -1 ) return rc;
248 if( c->ldctl_value.bv_val != NULL ) {
249 rc = ber_printf( ber, "O", &c->ldctl_value );
250 if( rc == -1 ) return rc;
253 rc = ber_printf( ber, /*{*/"N}" );
254 if( rc == -1 ) return rc;
260 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
264 LDAPControl **sctrls = NULL;
267 * Retrieve any additional controls that may be set by the
271 if ( o->o_pb && slapi_pblock_get( o->o_pb, SLAPI_RESCONTROLS, &sctrls ) != 0 ) {
275 if ( c == NULL && sctrls == NULL ) return 0;
277 if( c == NULL ) return 0;
278 #endif /* LDAP_SLAPI */
280 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
281 if( rc == -1 ) return rc;
285 #endif /* LDAP_SLAPI */
286 for( ; *c != NULL; c++) {
287 rc = send_ldap_control( ber, *c );
288 if( rc == -1 ) return rc;
292 if ( sctrls != NULL ) {
293 for ( c = sctrls; *c != NULL; c++ ) {
294 rc = send_ldap_control( ber, *c );
295 if( rc == -1 ) return rc;
298 #endif /* LDAP_SLAPI */
300 rc = ber_printf( ber, /*{*/"N}" );
310 BerElementBuffer berbuf;
311 BerElement *ber = (BerElement *) &berbuf;
312 int rc = LDAP_SUCCESS;
315 if ( op->o_callback ) {
317 slap_callback *sc = op->o_callback,
318 *sc_next = op->o_callback;
320 rc = SLAP_CB_CONTINUE;
321 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
322 sc_next = op->o_callback->sc_next;
323 if ( op->o_callback->sc_response ) {
324 rc = op->o_callback->sc_response( op, rs );
325 if ( first && op->o_callback == NULL ) {
328 if ( rc != SLAP_CB_CONTINUE ) break;
334 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
337 #ifdef LDAP_CONNECTIONLESS
338 if (op->o_conn && op->o_conn->c_is_udp)
343 ber_init_w_nullc( ber, LBER_USE_DER );
344 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
348 LDAP_LOG( OPERATION, ENTRY,
349 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
350 rs->sr_msgid, rs->sr_tag, rs->sr_err );
352 Debug( LDAP_DEBUG_TRACE,
353 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
354 rs->sr_msgid, rs->sr_tag, rs->sr_err );
359 LDAP_LOG( OPERATION, ARGS,
360 "send_ldap_response: conn %lu ref=\"%s\"\n",
362 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
364 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
365 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
370 #ifdef LDAP_CONNECTIONLESS
371 if (op->o_conn && op->o_conn->c_is_udp &&
372 op->o_protocol == LDAP_VERSION2 )
374 rc = ber_printf( ber, "t{ess" /*"}"*/,
375 rs->sr_tag, rs->sr_err,
376 rs->sr_matched == NULL ? "" : rs->sr_matched,
377 rs->sr_text == NULL ? "" : rs->sr_text );
380 if ( rs->sr_type == REP_INTERMEDIATE ) {
381 rc = ber_printf( ber, "{it{" /*"}}"*/,
382 rs->sr_msgid, rs->sr_tag );
385 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
386 rs->sr_msgid, rs->sr_tag, rs->sr_err,
387 rs->sr_matched == NULL ? "" : rs->sr_matched,
388 rs->sr_text == NULL ? "" : rs->sr_text );
392 if ( rs->sr_ref != NULL ) {
393 assert( rs->sr_err == LDAP_REFERRAL );
394 rc = ber_printf( ber, "t{W}",
395 LDAP_TAG_REFERRAL, rs->sr_ref );
397 assert( rs->sr_err != LDAP_REFERRAL );
401 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
402 rc = ber_printf( ber, "tO",
403 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
407 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
409 if ( rs->sr_rspoid != NULL ) {
410 rc = ber_printf( ber, "ts",
411 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
413 if( rc != -1 && rs->sr_rspdata != NULL ) {
414 rc = ber_printf( ber, "tO",
415 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
420 rc = ber_printf( ber, /*"{"*/ "N}" );
424 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
428 rc = ber_printf( ber, /*"{"*/ "N}" );
431 #ifdef LDAP_CONNECTIONLESS
432 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
435 rc = ber_printf( ber, /*"{"*/ "N}" );
441 LDAP_LOG( OPERATION, ERR,
442 "send_ldap_response: conn %lu ber_printf failed\n",
443 op->o_connid, 0, 0 );
445 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
448 #ifdef LDAP_CONNECTIONLESS
449 if (!op->o_conn || op->o_conn->c_is_udp == 0)
458 bytes = send_ldap_ber( op->o_conn, ber );
459 #ifdef LDAP_CONNECTIONLESS
460 if (!op->o_conn || op->o_conn->c_is_udp == 0)
468 LDAP_LOG( OPERATION, ERR,
469 "send_ldap_response: conn %lu ber write failed\n",
470 op->o_connid ? op->o_connid : 0, 0, 0 );
472 Debug( LDAP_DEBUG_ANY,
473 "send_ldap_response: ber write failed\n",
482 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
483 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
484 (void *)rs->sr_matched );
485 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
487 #endif /* LDAP_SLAPI */
489 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
490 num_bytes_sent += bytes;
492 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
495 /* Tell caller that we did this for real, as opposed to being
496 * overridden by a callback
498 rc = SLAP_CB_CONTINUE;
501 if ( op->o_callback ) {
503 slap_callback *sc = op->o_callback,
504 *sc_next = op->o_callback;
506 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
507 sc_next = op->o_callback->sc_next;
508 if ( op->o_callback->sc_cleanup ) {
509 (void)op->o_callback->sc_cleanup( op, rs );
510 if ( first && op->o_callback == NULL ) {
521 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
522 free( (char *)rs->sr_matched );
523 rs->sr_matched = NULL;
526 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
527 ber_bvarray_free( rs->sr_ref );
536 send_ldap_disconnect( Operation *op, SlapReply *rs )
538 #define LDAP_UNSOLICITED_ERROR(e) \
539 ( (e) == LDAP_PROTOCOL_ERROR \
540 || (e) == LDAP_STRONG_AUTH_REQUIRED \
541 || (e) == LDAP_UNAVAILABLE )
543 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
545 rs->sr_type = REP_EXTENDED;
548 LDAP_LOG( OPERATION, ENTRY,
549 "send_ldap_disconnect: conn %lu %d:%s\n",
550 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
552 Debug( LDAP_DEBUG_TRACE,
553 "send_ldap_disconnect %d:%s\n",
554 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
557 if ( op->o_protocol < LDAP_VERSION3 ) {
558 rs->sr_rspoid = NULL;
559 rs->sr_tag = req2res( op->o_tag );
560 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
563 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
564 rs->sr_tag = LDAP_RES_EXTENDED;
568 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
569 Statslog( LDAP_DEBUG_STATS,
570 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
571 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
572 rs->sr_text ? rs->sr_text : "" );
577 slap_send_ldap_result( Operation *op, SlapReply *rs )
580 const char *otext = rs->sr_text;
581 BerVarray oref = rs->sr_ref;
583 rs->sr_type = REP_RESULT;
585 assert( !LDAP_API_ERROR( rs->sr_err ));
588 LDAP_LOG( OPERATION, ENTRY,
589 "send_ldap_result: conn=%lu op=%lu p=%d\n",
590 op->o_connid, op->o_opid, op->o_protocol );
592 Debug( LDAP_DEBUG_TRACE,
593 "send_ldap_result: conn=%lu op=%lu p=%d\n",
594 op->o_connid, op->o_opid, op->o_protocol );
598 LDAP_LOG( OPERATION, ARGS,
599 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
600 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
601 rs->sr_text ? rs->sr_text : "" );
603 Debug( LDAP_DEBUG_ARGS,
604 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
605 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
606 rs->sr_text ? rs->sr_text : "" );
612 LDAP_LOG( OPERATION, ARGS,
613 "send_ldap_result: referral=\"%s\"\n",
614 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
616 Debug( LDAP_DEBUG_ARGS,
617 "send_ldap_result: referral=\"%s\"\n",
618 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
623 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
625 if ( rs->sr_err == LDAP_REFERRAL ) {
626 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
627 if( op->o_domain_scope ) {
631 if( rs->sr_ref == NULL ) {
632 rs->sr_err = LDAP_NO_SUCH_OBJECT;
633 } else if ( op->o_protocol < LDAP_VERSION3 ) {
634 rs->sr_err = LDAP_PARTIAL_RESULTS;
640 * Call pre-result plugins. To avoid infinite recursion plugins
641 * should just set SLAPI_RESULT_CODE rather than sending a
642 * result if they wish to change the result.
644 if ( op->o_pb != NULL ) {
645 slapi_int_pblock_set_operation( op->o_pb, op );
646 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
647 (void *)rs->sr_err );
648 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
649 (void *)rs->sr_text );
650 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
651 (void *)rs->sr_matched );
653 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
656 #endif /* LDAP_SLAPI */
658 if ( op->o_protocol < LDAP_VERSION3 ) {
659 tmp = v2ref( rs->sr_ref, rs->sr_text );
664 rs->sr_tag = req2res( op->o_tag );
665 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
667 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
668 if ( op->o_tag == LDAP_REQ_SEARCH ) {
670 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
671 rs->sr_err, rs->sr_nentries );
673 Statslog( LDAP_DEBUG_STATS,
674 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
675 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
676 rs->sr_text ? rs->sr_text : "" );
678 Statslog( LDAP_DEBUG_STATS,
679 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
680 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
681 rs->sr_text ? rs->sr_text : "" );
685 if( tmp != NULL ) ch_free(tmp);
691 send_ldap_sasl( Operation *op, SlapReply *rs )
693 rs->sr_type = REP_SASL;
695 LDAP_LOG( OPERATION, ENTRY,
696 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
697 op->o_connid, rs->sr_err,
698 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
700 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
702 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
705 rs->sr_tag = req2res( op->o_tag );
706 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
708 send_ldap_response( op, rs );
712 slap_send_ldap_extended( Operation *op, SlapReply *rs )
714 rs->sr_type = REP_EXTENDED;
717 LDAP_LOG( OPERATION, ENTRY,
718 "send_ldap_extended: err=%d oid=%s len=%ld\n",
719 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
720 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
722 Debug( LDAP_DEBUG_TRACE,
723 "send_ldap_extended: err=%d oid=%s len=%ld\n",
725 rs->sr_rspoid ? rs->sr_rspoid : "",
726 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
729 rs->sr_tag = req2res( op->o_tag );
730 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
732 send_ldap_response( op, rs );
736 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
738 rs->sr_type = REP_INTERMEDIATE;
740 LDAP_LOG( OPERATION, ENTRY,
741 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
742 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
743 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
745 Debug( LDAP_DEBUG_TRACE,
746 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
748 rs->sr_rspoid ? rs->sr_rspoid : "",
749 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
751 rs->sr_tag = LDAP_RES_INTERMEDIATE;
752 rs->sr_msgid = op->o_msgid;
753 send_ldap_response( op, rs );
757 slap_send_search_entry( Operation *op, SlapReply *rs )
759 BerElementBuffer berbuf;
760 BerElement *ber = (BerElement *) &berbuf;
762 int i, j, rc=-1, bytes;
765 AccessControlState acl_state = ACL_STATE_INIT;
767 /* Support for computed attribute plugins */
768 computed_attr_context ctx;
771 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
773 /* a_flags: array of flags telling if the i-th element will be
774 * returned or filtered out
775 * e_flags: array of a_flags
777 char **e_flags = NULL;
779 rs->sr_type = REP_SEARCH;
781 /* eventually will loop through generated operational attributes */
782 /* only subschemaSubentry and numSubordinates are implemented */
783 /* NOTE: moved before overlays callback circling because
784 * they may modify entry and other stuff in rs */
785 /* check for special all operational attributes ("+") type */
786 /* FIXME: maybe we could se this flag at the operation level;
787 * however, in principle the caller of send_search_entry() may
788 * change the attribute list at each call */
789 if ( rs->sr_attrs == NULL ) {
790 rs->sr_attr_flags = ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
793 rs->sr_attr_flags |= an_find( rs->sr_attrs, &AllOper ) ?
794 SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
795 rs->sr_attr_flags |= an_find( rs->sr_attrs, &AllUser ) ?
796 SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
799 rc = backend_operational( op, rs );
804 if ( op->o_callback ) {
806 slap_callback *sc = op->o_callback,
807 *sc_next = op->o_callback;
809 rc = SLAP_CB_CONTINUE;
810 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
811 sc_next = op->o_callback->sc_next;
812 if ( op->o_callback->sc_response ) {
813 rc = op->o_callback->sc_response( op, rs );
814 if ( first && op->o_callback == NULL ) {
817 if ( rc != SLAP_CB_CONTINUE ) break;
823 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
827 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
828 op->o_connid, rs->sr_entry->e_name.bv_val,
829 op->ors_attrsonly ? " (attrsOnly)" : "" );
831 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
832 op->o_connid, rs->sr_entry->e_name.bv_val,
833 op->ors_attrsonly ? " (attrsOnly)" : "" );
836 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
839 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
840 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
842 Debug( LDAP_DEBUG_ACL,
843 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
844 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
851 edn = rs->sr_entry->e_nname.bv_val;
853 if ( op->o_res_ber ) {
854 /* read back control or LDAP_CONNECTIONLESS */
860 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
861 bv.bv_len = siz + len;
862 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
864 ber_init2( ber, &bv, LBER_USE_DER );
865 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
868 #ifdef LDAP_CONNECTIONLESS
869 if ( op->o_conn && op->o_conn->c_is_udp ) {
871 if ( op->o_protocol == LDAP_VERSION2 ) {
872 rc = ber_printf(ber, "t{O{" /*}}*/,
873 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
875 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
876 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
880 if ( op->o_res_ber ) {
881 /* read back control */
882 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
884 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
885 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
890 LDAP_LOG( OPERATION, ERR,
891 "send_search_entry: conn %lu ber_printf failed\n",
892 op->o_connid, 0, 0 );
894 Debug( LDAP_DEBUG_ANY,
895 "send_search_entry: conn %lu ber_printf failed\n",
896 op->o_connid, 0, 0 );
899 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
900 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
904 /* check for special all user attributes ("*") type */
905 userattrs = ( rs->sr_attrs == NULL ) ? 1
906 : an_find( rs->sr_attrs, &AllUser );
908 /* create an array of arrays of flags. Each flag corresponds
909 * to particular value of attribute and equals 1 if value matches
910 * to ValuesReturnFilter or 0 if not
912 if ( op->o_vrFilter != NULL ) {
916 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
917 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
920 size = i * sizeof(char *) + k;
923 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
924 if( e_flags == NULL ) {
926 LDAP_LOG( OPERATION, ERR,
927 "send_search_entry: conn %lu slap_sl_calloc failed\n",
928 op->o_connid ? op->o_connid : 0, 0, 0 );
930 Debug( LDAP_DEBUG_ANY,
931 "send_search_entry: conn %lu slap_sl_calloc failed\n",
932 op->o_connid ? op->o_connid : 0, 0, 0 );
936 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
939 a_flags = (char *)(e_flags + i);
940 memset( a_flags, 0, k );
941 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
942 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
943 e_flags[i] = a_flags;
947 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
950 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
951 "conn %lu matched values filtering failed\n",
952 op->o_connid ? op->o_connid : 0, 0, 0 );
954 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
955 "conn %lu matched values filtering failed\n",
956 op->o_connid ? op->o_connid : 0, 0, 0 );
958 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
959 send_ldap_error( op, rs, LDAP_OTHER,
960 "matched values filtering error" );
966 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
967 AttributeDescription *desc = a->a_desc;
970 if ( rs->sr_attrs == NULL ) {
971 /* all attrs request, skip operational attributes */
972 if( is_at_operational( desc->ad_type ) ) {
977 /* specific attrs requested */
978 if ( is_at_operational( desc->ad_type ) ) {
979 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
980 !ad_inlist( desc, rs->sr_attrs ) )
986 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
993 if ( op->ors_attrsonly ) {
994 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
995 ACL_READ, &acl_state ) )
999 "send_search_entry: conn %lu access to attribute %s not "
1000 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
1002 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1003 "conn %lu access to attribute %s not allowed\n",
1004 op->o_connid, desc->ad_cname.bv_val, 0 );
1009 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1011 LDAP_LOG( OPERATION, ERR,
1012 "send_search_entry: conn %lu ber_printf failed\n",
1013 op->o_connid, 0, 0 );
1015 Debug( LDAP_DEBUG_ANY,
1016 "send_search_entry: conn %lu ber_printf failed\n",
1017 op->o_connid, 0, 0 );
1020 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1021 send_ldap_error( op, rs, LDAP_OTHER,
1022 "encoding description error");
1029 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1030 if ( ! access_allowed( op, rs->sr_entry,
1031 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1034 LDAP_LOG( ACL, INFO,
1035 "send_search_entry: conn %lu "
1036 "access to attribute %s, value #%d not allowed\n",
1037 op->o_connid, desc->ad_cname.bv_val, i );
1039 Debug( LDAP_DEBUG_ACL,
1040 "send_search_entry: conn %lu "
1041 "access to attribute %s, value #%d not allowed\n",
1042 op->o_connid, desc->ad_cname.bv_val, i );
1048 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1055 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1057 LDAP_LOG( OPERATION, ERR,
1058 "send_search_entry: conn %lu ber_printf failed\n",
1059 op->o_connid, 0, 0 );
1061 Debug( LDAP_DEBUG_ANY,
1062 "send_search_entry: conn %lu ber_printf failed\n",
1063 op->o_connid, 0, 0 );
1066 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1067 send_ldap_error( op, rs, LDAP_OTHER,
1068 "encoding description error");
1072 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1074 LDAP_LOG( OPERATION, ERR,
1075 "send_search_entry: conn %lu "
1076 "ber_printf failed.\n", op->o_connid, 0, 0 );
1078 Debug( LDAP_DEBUG_ANY,
1079 "send_search_entry: conn %lu "
1080 "ber_printf failed.\n", op->o_connid, 0, 0 );
1083 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1084 send_ldap_error( op, rs, LDAP_OTHER,
1085 "encoding values error" );
1091 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1093 LDAP_LOG( OPERATION, ERR,
1094 "send_search_entry: conn %lu ber_printf failed\n",
1095 op->o_connid, 0, 0 );
1097 Debug( LDAP_DEBUG_ANY,
1098 "send_search_entry: conn %lu ber_printf failed\n",
1099 op->o_connid, 0, 0 );
1102 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1103 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1108 /* NOTE: moved before overlays callback circling because
1109 * they may modify entry and other stuff in rs */
1110 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1114 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1115 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1118 size = i * sizeof(char *) + k;
1120 char *a_flags, **tmp;
1123 * Reuse previous memory - we likely need less space
1124 * for operational attributes
1126 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1128 if ( tmp == NULL ) {
1130 LDAP_LOG( OPERATION, ERR,
1131 "send_search_entry: conn %lu "
1132 "not enough memory "
1133 "for matched values filtering\n",
1134 op->o_connid, 0, 0);
1136 Debug( LDAP_DEBUG_ANY,
1137 "send_search_entry: conn %lu "
1138 "not enough memory "
1139 "for matched values filtering\n",
1140 op->o_connid, 0, 0 );
1142 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1143 send_ldap_error( op, rs, LDAP_OTHER,
1144 "not enough memory for matched values filtering" );
1148 a_flags = (char *)(e_flags + i);
1149 memset( a_flags, 0, k );
1150 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1151 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1152 e_flags[i] = a_flags;
1155 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1159 LDAP_LOG( OPERATION, ERR,
1160 "send_search_entry: conn %lu "
1161 "matched values filtering failed\n",
1162 op->o_connid ? op->o_connid : 0, 0, 0);
1164 Debug( LDAP_DEBUG_ANY,
1165 "send_search_entry: conn %lu "
1166 "matched values filtering failed\n",
1167 op->o_connid ? op->o_connid : 0, 0, 0);
1169 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1170 send_ldap_error( op, rs, LDAP_OTHER,
1171 "matched values filtering error" );
1177 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1178 AttributeDescription *desc = a->a_desc;
1180 if ( rs->sr_attrs == NULL ) {
1181 /* all attrs request, skip operational attributes */
1182 if( is_at_operational( desc->ad_type ) ) {
1187 /* specific attrs requested */
1188 if( is_at_operational( desc->ad_type ) ) {
1189 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1190 !ad_inlist( desc, rs->sr_attrs ) )
1195 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1201 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1202 ACL_READ, &acl_state ) )
1205 LDAP_LOG( ACL, INFO,
1206 "send_search_entry: conn %lu "
1207 "access to attribute %s not allowed\n",
1208 op->o_connid, desc->ad_cname.bv_val, 0 );
1210 Debug( LDAP_DEBUG_ACL,
1211 "send_search_entry: conn %lu "
1212 "access to attribute %s not allowed\n",
1213 op->o_connid, desc->ad_cname.bv_val, 0 );
1219 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1222 LDAP_LOG( OPERATION, ERR,
1223 "send_search_entry: conn %lu "
1224 "ber_printf failed\n", op->o_connid, 0, 0 );
1226 Debug( LDAP_DEBUG_ANY,
1227 "send_search_entry: conn %lu "
1228 "ber_printf failed\n", op->o_connid, 0, 0 );
1231 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1232 send_ldap_error( op, rs, LDAP_OTHER,
1233 "encoding description error" );
1237 if ( ! op->ors_attrsonly ) {
1238 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1239 if ( ! access_allowed( op, rs->sr_entry,
1240 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1243 LDAP_LOG( ACL, INFO,
1244 "send_search_entry: conn %lu "
1245 "access to %s, value %d not allowed\n",
1246 op->o_connid, desc->ad_cname.bv_val, i );
1248 Debug( LDAP_DEBUG_ACL,
1249 "send_search_entry: conn %lu "
1250 "access to %s, value %d not allowed\n",
1251 op->o_connid, desc->ad_cname.bv_val, i );
1257 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1261 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1263 LDAP_LOG( OPERATION, ERR,
1264 "send_search_entry: conn %lu ber_printf failed\n",
1265 op->o_connid, 0, 0 );
1267 Debug( LDAP_DEBUG_ANY,
1268 "send_search_entry: conn %lu ber_printf failed\n",
1269 op->o_connid, 0, 0 );
1272 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1273 send_ldap_error( op, rs, LDAP_OTHER,
1274 "encoding values error" );
1280 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1282 LDAP_LOG( OPERATION, ERR,
1283 "send_search_entry: conn %lu ber_printf failed\n",
1284 op->o_connid, 0, 0 );
1286 Debug( LDAP_DEBUG_ANY,
1287 "send_search_entry: conn %lu ber_printf failed\n",
1288 op->o_connid, 0, 0 );
1291 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1292 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1299 * First, setup the computed attribute context that is
1300 * passed to all plugins.
1303 ctx.cac_pb = op->o_pb;
1304 ctx.cac_attrs = rs->sr_attrs;
1305 ctx.cac_attrsonly = op->ors_attrsonly;
1306 ctx.cac_userattrs = userattrs;
1307 ctx.cac_opattrs = rs->sr_attr_flags;
1308 ctx.cac_acl_state = acl_state;
1309 ctx.cac_private = (void *)ber;
1312 * For each client requested attribute, call the plugins.
1314 if ( rs->sr_attrs != NULL ) {
1315 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1316 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1317 rs->sr_entry, slapi_int_compute_output_ber );
1318 if ( rc == 1 ) break;
1322 * Technically we shouldn't be returning operational attributes
1323 * when the user requested only user attributes. We'll let the
1324 * plugin decide whether to be naughty or not.
1326 rc = compute_evaluator( &ctx, "*",
1327 rs->sr_entry, slapi_int_compute_output_ber );
1330 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1331 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1335 #endif /* LDAP_SLAPI */
1339 slap_sl_free( e_flags, op->o_tmpmemctx );
1343 rc = ber_printf( ber, /*{{*/ "}N}" );
1346 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1350 #ifdef LDAP_CONNECTIONLESS
1351 if( op->o_conn && op->o_conn->c_is_udp ) {
1352 if ( op->o_protocol != LDAP_VERSION2 ) {
1353 rc = ber_printf( ber, /*{*/ "N}" );
1357 if ( op->o_res_ber == NULL ) {
1358 rc = ber_printf( ber, /*{*/ "N}" );
1364 LDAP_LOG( OPERATION, ERR,
1365 "send_search_entry: conn %lu ber_printf failed\n",
1366 op->o_connid, 0, 0 );
1368 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1371 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1372 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1377 if ( op->o_res_ber == NULL ) {
1378 bytes = send_ldap_ber( op->o_conn, ber );
1379 ber_free_buf( ber );
1383 LDAP_LOG( OPERATION, ERR,
1384 "send_search_entry: conn %lu ber write failed.\n",
1385 op->o_connid, 0, 0 );
1387 Debug( LDAP_DEBUG_ANY,
1388 "send_search_entry: conn %lu ber write failed.\n",
1389 op->o_connid, 0, 0 );
1397 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1398 num_bytes_sent += bytes;
1401 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1404 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1405 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1408 LDAP_LOG( OPERATION, ENTRY,
1409 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1411 Debug( LDAP_DEBUG_TRACE,
1412 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1418 if ( op->o_callback ) {
1420 slap_callback *sc = op->o_callback,
1421 *sc_next = op->o_callback;
1423 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1424 sc_next = op->o_callback->sc_next;
1425 if ( op->o_callback->sc_cleanup ) {
1426 (void)op->o_callback->sc_cleanup( op, rs );
1427 if ( first && op->o_callback == NULL ) {
1434 op->o_callback = sc;
1438 slap_sl_free( e_flags, op->o_tmpmemctx );
1441 if ( rs->sr_operational_attrs ) {
1442 attrs_free( rs->sr_operational_attrs );
1443 rs->sr_operational_attrs = NULL;
1445 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1447 /* FIXME: I think rs->sr_type should be explicitly set to
1448 * REP_SEARCH here. That's what it was when we entered this
1449 * function. send_ldap_error may have changed it, but we
1450 * should set it back so that the cleanup functions know
1451 * what they're doing.
1453 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1455 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1457 entry_free( rs->sr_entry );
1458 rs->sr_entry = NULL;
1459 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1466 slap_send_search_reference( Operation *op, SlapReply *rs )
1468 BerElementBuffer berbuf;
1469 BerElement *ber = (BerElement *) &berbuf;
1473 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1474 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1476 rs->sr_type = REP_SEARCHREF;
1477 if ( op->o_callback ) {
1479 slap_callback *sc = op->o_callback,
1480 *sc_next = op->o_callback;
1482 rc = SLAP_CB_CONTINUE;
1483 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1484 sc_next = op->o_callback->sc_next;
1485 if ( op->o_callback->sc_response ) {
1486 rc = op->o_callback->sc_response( op, rs );
1487 if ( first && op->o_callback == NULL ) {
1490 if ( rc != SLAP_CB_CONTINUE ) break;
1495 op->o_callback = sc;
1496 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1500 LDAP_LOG( OPERATION, ENTRY,
1501 "send_search_reference: conn %lu dn=\"%s\"\n",
1503 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1505 Debug( LDAP_DEBUG_TRACE,
1506 "=> send_search_reference: dn=\"%s\"\n",
1507 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1510 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1511 ad_entry, NULL, ACL_READ, NULL ) )
1514 LDAP_LOG( ACL, INFO,
1515 "send_search_reference: conn %lu "
1516 "access to entry %s not allowed\n",
1517 op->o_connid, rs->sr_entry->e_dn, 0 );
1519 Debug( LDAP_DEBUG_ACL,
1520 "send_search_reference: access to entry not allowed\n",
1527 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1528 ad_ref, NULL, ACL_READ, NULL ) )
1531 LDAP_LOG( ACL, INFO,
1532 "send_search_reference: conn %lu access "
1533 "to reference not allowed.\n", op->o_connid, 0, 0 );
1535 Debug( LDAP_DEBUG_ACL,
1536 "send_search_reference: access "
1537 "to reference not allowed\n",
1544 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1545 if( op->o_domain_scope ) {
1547 LDAP_LOG( OPERATION, ERR,
1548 "send_search_reference: conn %lu domainScope control in (%s).\n",
1549 op->o_connid, rs->sr_entry->e_dn, 0 );
1551 Debug( LDAP_DEBUG_ANY,
1552 "send_search_reference: domainScope control in (%s)\n",
1553 rs->sr_entry->e_dn, 0, 0 );
1560 if( rs->sr_ref == NULL ) {
1562 LDAP_LOG( OPERATION, ERR,
1563 "send_search_reference: conn %lu null ref in (%s).\n",
1564 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1566 Debug( LDAP_DEBUG_ANY,
1567 "send_search_reference: null ref in (%s)\n",
1568 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1574 if( op->o_protocol < LDAP_VERSION3 ) {
1576 /* save the references for the result */
1577 if( rs->sr_ref[0].bv_val != NULL ) {
1578 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1584 #ifdef LDAP_CONNECTIONLESS
1585 if( op->o_conn && op->o_conn->c_is_udp ) {
1586 ber = op->o_res_ber;
1590 ber_init_w_nullc( ber, LBER_USE_DER );
1591 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1594 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1595 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1598 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1602 rc = ber_printf( ber, /*"{"*/ "N}" );
1607 LDAP_LOG( OPERATION, ERR,
1608 "send_search_reference: conn %lu "
1609 "ber_printf failed.\n", op->o_connid, 0, 0 );
1611 Debug( LDAP_DEBUG_ANY,
1612 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1615 #ifdef LDAP_CONNECTIONLESS
1616 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1618 ber_free_buf( ber );
1619 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1623 #ifdef LDAP_CONNECTIONLESS
1624 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1626 bytes = send_ldap_ber( op->o_conn, ber );
1627 ber_free_buf( ber );
1629 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1630 num_bytes_sent += bytes;
1633 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1634 #ifdef LDAP_CONNECTIONLESS
1638 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1639 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1642 LDAP_LOG( OPERATION, ENTRY,
1643 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1645 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1649 if ( op->o_callback ) {
1651 slap_callback *sc = op->o_callback,
1652 *sc_next = op->o_callback;
1654 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1655 sc_next = op->o_callback->sc_next;
1656 if ( op->o_callback->sc_cleanup ) {
1657 (void)op->o_callback->sc_cleanup( op, rs );
1658 if ( first && op->o_callback == NULL ) {
1665 op->o_callback = sc;
1682 *code = LDAP_SUCCESS;
1686 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1688 LDAP_LOG( OPERATION, INFO,
1689 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1691 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1699 while ( (s = strchr( s, '\n' )) != NULL ) {
1704 if ( (c = strchr( s, ':' )) != NULL ) {
1708 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1712 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1716 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1722 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1724 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1735 int slap_read_controls(
1739 const struct berval *oid,
1740 LDAPControl **ctrl )
1744 BerElementBuffer berbuf;
1745 BerElement *ber = (BerElement *) &berbuf;
1751 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1752 oid->bv_val, e->e_dn, 0 );
1754 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1755 oid->bv_val, e->e_dn, 0 );
1759 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1760 op->o_preread_attrs : op->o_postread_attrs;
1762 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1763 bv.bv_len = siz + len;
1764 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1766 ber_init2( ber, &bv, LBER_USE_DER );
1767 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1769 /* create new operation */
1772 myop.o_res_ber = ber;
1774 rc = slap_send_search_entry( &myop, rs );
1777 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1779 if( rc == LBER_ERROR ) return LDAP_OTHER;
1781 c.ldctl_oid = oid->bv_val;
1782 c.ldctl_iscritical = 0;
1784 if ( ctrl == NULL ) {
1786 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1788 /* retry: free previous try */
1789 slap_sl_free( (*ctrl)->ldctl_value.bv_val, &op->o_tmpmemctx );
1793 return LDAP_SUCCESS;
1796 /* Map API errors to protocol errors... */
1798 slap_map_api2result( SlapReply *rs )
1800 switch(rs->sr_err) {
1801 case LDAP_SERVER_DOWN:
1802 return LDAP_UNAVAILABLE;
1803 case LDAP_LOCAL_ERROR:
1805 case LDAP_ENCODING_ERROR:
1806 case LDAP_DECODING_ERROR:
1807 return LDAP_PROTOCOL_ERROR;
1809 return LDAP_UNAVAILABLE;
1810 case LDAP_AUTH_UNKNOWN:
1811 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1812 case LDAP_FILTER_ERROR:
1813 rs->sr_text = "Filter error";
1815 case LDAP_USER_CANCELLED:
1816 rs->sr_text = "User cancelled";
1818 case LDAP_PARAM_ERROR:
1819 return LDAP_PROTOCOL_ERROR;
1820 case LDAP_NO_MEMORY:
1822 case LDAP_CONNECT_ERROR:
1823 return LDAP_UNAVAILABLE;
1824 case LDAP_NOT_SUPPORTED:
1825 return LDAP_UNWILLING_TO_PERFORM;
1826 case LDAP_CONTROL_NOT_FOUND:
1827 return LDAP_PROTOCOL_ERROR;
1828 case LDAP_NO_RESULTS_RETURNED:
1829 return LDAP_NO_SUCH_OBJECT;
1830 case LDAP_MORE_RESULTS_TO_RETURN:
1831 rs->sr_text = "More results to return";
1833 case LDAP_CLIENT_LOOP:
1834 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1835 return LDAP_LOOP_DETECT;
1837 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;