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 int slap_null_cb( Operation *op, SlapReply *rs )
49 int slap_replog_cb( Operation *op, SlapReply *rs )
51 if ( rs->sr_err == LDAP_SUCCESS ) {
54 return SLAP_CB_CONTINUE;
57 static char *v2ref( BerVarray ref, const char *text )
59 size_t len = 0, i = 0;
64 return ch_strdup(text);
72 if (text[len-1] != '\n') {
77 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
80 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
82 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
93 strcpy( v2+len, "Referral:" );
94 len += sizeof("Referral:");
96 for( i=0; ref[i].bv_val != NULL; i++ ) {
97 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
100 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
102 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] != '/') {
118 static ber_tag_t req2res( ber_tag_t tag )
123 case LDAP_REQ_COMPARE:
124 case LDAP_REQ_EXTENDED:
125 case LDAP_REQ_MODIFY:
126 case LDAP_REQ_MODRDN:
130 case LDAP_REQ_DELETE:
131 tag = LDAP_RES_DELETE;
134 case LDAP_REQ_ABANDON:
135 case LDAP_REQ_UNBIND:
139 case LDAP_REQ_SEARCH:
140 tag = LDAP_RES_SEARCH_RESULT;
150 static long send_ldap_ber(
156 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
158 /* write only one pdu at a time - wait til it's our turn */
159 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
161 /* lock the connection */
162 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
169 if ( connection_state_closing( conn ) ) {
170 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
171 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
176 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
183 * we got an error. if it's ewouldblock, we need to
184 * wait on the socket being writable. otherwise, figure
185 * it's a hard error and return.
189 LDAP_LOG( OPERATION, ERR,
190 "send_ldap_ber: conn %lu ber_flush failed err=%d (%s)\n",
191 conn ? conn->c_connid : 0, err, sock_errstr(err) );
193 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
194 err, sock_errstr(err), 0 );
197 if ( err != EWOULDBLOCK && err != EAGAIN ) {
198 connection_closing( conn );
200 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
201 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
206 /* wait for socket to be write-ready */
207 conn->c_writewaiter = 1;
208 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
209 slapd_set_write( sd, 1 );
211 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
212 conn->c_writewaiter = 0;
215 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
216 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
222 send_ldap_control( BerElement *ber, LDAPControl *c )
228 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
230 if( c->ldctl_iscritical ) {
231 rc = ber_printf( ber, "b",
232 (ber_int_t) c->ldctl_iscritical ) ;
233 if( rc == -1 ) return rc;
236 if( c->ldctl_value.bv_val != NULL ) {
237 rc = ber_printf( ber, "O", &c->ldctl_value );
238 if( rc == -1 ) return rc;
241 rc = ber_printf( ber, /*{*/"N}" );
242 if( rc == -1 ) return rc;
248 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
252 LDAPControl **sctrls = NULL;
255 * Retrieve any additional controls that may be set by the
259 if ( o->o_pb && slapi_pblock_get( o->o_pb, SLAPI_RESCONTROLS, &sctrls ) != 0 ) {
263 if ( c == NULL && sctrls == NULL ) return 0;
265 if( c == NULL ) return 0;
266 #endif /* LDAP_SLAPI */
268 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
269 if( rc == -1 ) return rc;
273 #endif /* LDAP_SLAPI */
274 for( ; *c != NULL; c++) {
275 rc = send_ldap_control( ber, *c );
276 if( rc == -1 ) return rc;
280 if ( sctrls != NULL ) {
281 for ( c = sctrls; *c != NULL; c++ ) {
282 rc = send_ldap_control( ber, *c );
283 if( rc == -1 ) return rc;
286 #endif /* LDAP_SLAPI */
288 rc = ber_printf( ber, /*{*/"N}" );
298 BerElementBuffer berbuf;
299 BerElement *ber = (BerElement *) &berbuf;
300 int rc = LDAP_SUCCESS;
303 if (op->o_callback) {
304 slap_callback *sc = op->o_callback;
305 rc = SLAP_CB_CONTINUE;
306 for ( ; op->o_callback; ) {
307 if ( op->o_callback->sc_response ) {
308 rc = op->o_callback->sc_response( op, rs );
309 if ( rc != SLAP_CB_CONTINUE ) break;
311 op->o_callback = op->o_callback->sc_next;
314 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
317 #ifdef LDAP_CONNECTIONLESS
318 if (op->o_conn && op->o_conn->c_is_udp)
323 ber_init_w_nullc( ber, LBER_USE_DER );
324 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
328 LDAP_LOG( OPERATION, ENTRY,
329 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
330 rs->sr_msgid, rs->sr_tag, rs->sr_err );
332 Debug( LDAP_DEBUG_TRACE,
333 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
334 rs->sr_msgid, rs->sr_tag, rs->sr_err );
339 LDAP_LOG( OPERATION, ARGS,
340 "send_ldap_response: conn %lu ref=\"%s\"\n",
342 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
344 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
345 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
350 #ifdef LDAP_CONNECTIONLESS
351 if (op->o_conn && op->o_conn->c_is_udp &&
352 op->o_protocol == LDAP_VERSION2 )
354 rc = ber_printf( ber, "t{ess" /*"}"*/,
355 rs->sr_tag, rs->sr_err,
356 rs->sr_matched == NULL ? "" : rs->sr_matched,
357 rs->sr_text == NULL ? "" : rs->sr_text );
360 if ( rs->sr_type == REP_INTERMEDIATE ) {
361 rc = ber_printf( ber, "{it{" /*"}}"*/,
362 rs->sr_msgid, rs->sr_tag );
365 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
366 rs->sr_msgid, rs->sr_tag, rs->sr_err,
367 rs->sr_matched == NULL ? "" : rs->sr_matched,
368 rs->sr_text == NULL ? "" : rs->sr_text );
372 if ( rs->sr_ref != NULL ) {
373 assert( rs->sr_err == LDAP_REFERRAL );
374 rc = ber_printf( ber, "t{W}",
375 LDAP_TAG_REFERRAL, rs->sr_ref );
377 assert( rs->sr_err != LDAP_REFERRAL );
381 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
382 rc = ber_printf( ber, "tO",
383 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
387 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
389 if ( rs->sr_rspoid != NULL ) {
390 rc = ber_printf( ber, "ts",
391 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
393 if( rc != -1 && rs->sr_rspdata != NULL ) {
394 rc = ber_printf( ber, "tO",
395 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
400 rc = ber_printf( ber, /*"{"*/ "N}" );
404 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
408 rc = ber_printf( ber, /*"{"*/ "N}" );
411 #ifdef LDAP_CONNECTIONLESS
412 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
415 rc = ber_printf( ber, /*"{"*/ "N}" );
421 LDAP_LOG( OPERATION, ERR,
422 "send_ldap_response: conn %lu ber_printf failed\n",
423 op->o_connid, 0, 0 );
425 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
428 #ifdef LDAP_CONNECTIONLESS
429 if (!op->o_conn || op->o_conn->c_is_udp == 0)
438 bytes = send_ldap_ber( op->o_conn, ber );
439 #ifdef LDAP_CONNECTIONLESS
440 if (!op->o_conn || op->o_conn->c_is_udp == 0)
448 LDAP_LOG( OPERATION, ERR,
449 "send_ldap_response: conn %lu ber write failed\n",
450 op->o_connid ? op->o_connid : 0, 0, 0 );
452 Debug( LDAP_DEBUG_ANY,
453 "send_ldap_response: ber write failed\n",
462 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
463 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
464 (void *)rs->sr_matched );
465 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
467 #endif /* LDAP_SLAPI */
469 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
470 num_bytes_sent += bytes;
472 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
475 /* Tell caller that we did this for real, as opposed to being
476 * overridden by a callback
478 rc = SLAP_CB_CONTINUE;
480 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
481 free( (char *)rs->sr_matched );
482 rs->sr_matched = NULL;
485 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
486 ber_bvarray_free( rs->sr_ref );
491 if (op->o_callback) {
492 slap_callback *sc = op->o_callback;
493 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
494 if ( op->o_callback->sc_cleanup ) {
495 op->o_callback->sc_cleanup( op, rs );
506 send_ldap_disconnect( Operation *op, SlapReply *rs )
508 #define LDAP_UNSOLICITED_ERROR(e) \
509 ( (e) == LDAP_PROTOCOL_ERROR \
510 || (e) == LDAP_STRONG_AUTH_REQUIRED \
511 || (e) == LDAP_UNAVAILABLE )
513 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
515 rs->sr_type = REP_EXTENDED;
518 LDAP_LOG( OPERATION, ENTRY,
519 "send_ldap_disconnect: conn %lu %d:%s\n",
520 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
522 Debug( LDAP_DEBUG_TRACE,
523 "send_ldap_disconnect %d:%s\n",
524 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
527 if ( op->o_protocol < LDAP_VERSION3 ) {
528 rs->sr_rspoid = NULL;
529 rs->sr_tag = req2res( op->o_tag );
530 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
533 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
534 rs->sr_tag = LDAP_RES_EXTENDED;
538 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
539 Statslog( LDAP_DEBUG_STATS,
540 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
541 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
542 rs->sr_text ? rs->sr_text : "" );
547 slap_send_ldap_result( Operation *op, SlapReply *rs )
550 const char *otext = rs->sr_text;
551 BerVarray oref = rs->sr_ref;
553 rs->sr_type = REP_RESULT;
555 assert( !LDAP_API_ERROR( rs->sr_err ));
558 LDAP_LOG( OPERATION, ENTRY,
559 "send_ldap_result: conn=%lu op=%lu p=%d\n",
560 op->o_connid, op->o_opid, op->o_protocol );
562 Debug( LDAP_DEBUG_TRACE,
563 "send_ldap_result: conn=%lu op=%lu p=%d\n",
564 op->o_connid, op->o_opid, op->o_protocol );
568 LDAP_LOG( OPERATION, ARGS,
569 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
570 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
571 rs->sr_text ? rs->sr_text : "" );
573 Debug( LDAP_DEBUG_ARGS,
574 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
575 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
576 rs->sr_text ? rs->sr_text : "" );
582 LDAP_LOG( OPERATION, ARGS,
583 "send_ldap_result: referral=\"%s\"\n",
584 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
586 Debug( LDAP_DEBUG_ARGS,
587 "send_ldap_result: referral=\"%s\"\n",
588 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
593 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
595 if ( rs->sr_err == LDAP_REFERRAL ) {
596 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
597 if( op->o_domain_scope ) {
601 if( rs->sr_ref == NULL ) {
602 rs->sr_err = LDAP_NO_SUCH_OBJECT;
603 } else if ( op->o_protocol < LDAP_VERSION3 ) {
604 rs->sr_err = LDAP_PARTIAL_RESULTS;
610 * Call pre-result plugins. To avoid infinite recursion plugins
611 * should just set SLAPI_RESULT_CODE rather than sending a
612 * result if they wish to change the result.
614 if ( op->o_pb != NULL ) {
615 slapi_int_pblock_set_operation( op->o_pb, op );
616 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
617 (void *)rs->sr_err );
618 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
619 (void *)rs->sr_text );
620 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
621 (void *)rs->sr_matched );
623 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
626 #endif /* LDAP_SLAPI */
628 if ( op->o_protocol < LDAP_VERSION3 ) {
629 tmp = v2ref( rs->sr_ref, rs->sr_text );
634 rs->sr_tag = 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 if ( op->o_tag == LDAP_REQ_SEARCH ) {
640 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
641 rs->sr_err, rs->sr_nentries );
643 Statslog( LDAP_DEBUG_STATS,
644 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
645 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
646 rs->sr_text ? rs->sr_text : "" );
648 Statslog( LDAP_DEBUG_STATS,
649 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
650 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
651 rs->sr_text ? rs->sr_text : "" );
655 if( tmp != NULL ) ch_free(tmp);
661 send_ldap_sasl( Operation *op, SlapReply *rs )
663 rs->sr_type = REP_SASL;
665 LDAP_LOG( OPERATION, ENTRY,
666 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
667 op->o_connid, rs->sr_err,
668 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
670 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
672 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
675 rs->sr_tag = req2res( op->o_tag );
676 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
678 send_ldap_response( op, rs );
682 slap_send_ldap_extended( Operation *op, SlapReply *rs )
684 rs->sr_type = REP_EXTENDED;
687 LDAP_LOG( OPERATION, ENTRY,
688 "send_ldap_extended: err=%d oid=%s len=%ld\n",
689 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
690 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
692 Debug( LDAP_DEBUG_TRACE,
693 "send_ldap_extended: err=%d oid=%s len=%ld\n",
695 rs->sr_rspoid ? rs->sr_rspoid : "",
696 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
699 rs->sr_tag = req2res( op->o_tag );
700 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
702 send_ldap_response( op, rs );
706 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
708 rs->sr_type = REP_INTERMEDIATE;
710 LDAP_LOG( OPERATION, ENTRY,
711 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
712 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
713 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
715 Debug( LDAP_DEBUG_TRACE,
716 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
718 rs->sr_rspoid ? rs->sr_rspoid : "",
719 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
721 rs->sr_tag = LDAP_RES_INTERMEDIATE;
722 rs->sr_msgid = op->o_msgid;
723 send_ldap_response( op, rs );
727 slap_send_search_entry( Operation *op, SlapReply *rs )
729 BerElementBuffer berbuf;
730 BerElement *ber = (BerElement *) &berbuf;
732 int i, j, rc=-1, bytes;
736 AccessControlState acl_state = ACL_STATE_INIT;
738 /* Support for computed attribute plugins */
739 computed_attr_context ctx;
742 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
744 /* a_flags: array of flags telling if the i-th element will be
745 * returned or filtered out
746 * e_flags: array of a_flags
748 char **e_flags = NULL;
750 rs->sr_type = REP_SEARCH;
751 if (op->o_callback) {
752 slap_callback *sc = op->o_callback;
753 rc = SLAP_CB_CONTINUE;
754 for ( ; op->o_callback; ) {
755 if ( op->o_callback->sc_response ) {
756 rc = op->o_callback->sc_response( op, rs );
757 if ( rc != SLAP_CB_CONTINUE ) break;
759 op->o_callback = op->o_callback->sc_next;
762 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
766 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
767 op->o_connid, rs->sr_entry->e_name.bv_val,
768 op->ors_attrsonly ? " (attrsOnly)" : "" );
770 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
771 rs->sr_entry->e_name.bv_val,
772 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
775 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
778 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
779 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
781 Debug( LDAP_DEBUG_ACL,
782 "send_search_entry: access to entry not allowed\n",
790 edn = rs->sr_entry->e_nname.bv_val;
792 if ( op->o_res_ber ) {
793 /* read back control or LDAP_CONNECTIONLESS */
799 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
800 bv.bv_len = siz + len;
801 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
803 ber_init2( ber, &bv, LBER_USE_DER );
804 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
807 #ifdef LDAP_CONNECTIONLESS
808 if ( op->o_conn && op->o_conn->c_is_udp ) {
810 if ( op->o_protocol == LDAP_VERSION2 ) {
811 rc = ber_printf(ber, "t{O{" /*}}*/,
812 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
814 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
815 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
819 if ( op->o_res_ber ) {
820 /* read back control */
821 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
823 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
824 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
829 LDAP_LOG( OPERATION, ERR,
830 "send_search_entry: conn %lu ber_printf failed\n",
831 op->o_connid, 0, 0 );
833 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
836 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
837 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
841 /* check for special all user attributes ("*") type */
842 userattrs = ( rs->sr_attrs == NULL ) ? 1
843 : an_find( rs->sr_attrs, &AllUser );
845 /* check for special all operational attributes ("+") type */
846 opattrs = ( rs->sr_attrs == NULL ) ? 0
847 : an_find( rs->sr_attrs, &AllOper );
849 /* create an array of arrays of flags. Each flag corresponds
850 * to particular value of attribute and equals 1 if value matches
851 * to ValuesReturnFilter or 0 if not
853 if ( op->o_vrFilter != NULL ) {
857 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
858 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
861 size = i * sizeof(char *) + k;
864 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
865 if( e_flags == NULL ) {
867 LDAP_LOG( OPERATION, ERR,
868 "send_search_entry: conn %lu slap_sl_calloc failed\n",
869 op->o_connid ? op->o_connid : 0, 0, 0 );
871 Debug( LDAP_DEBUG_ANY,
872 "send_search_entry: slap_sl_calloc failed\n", 0, 0, 0 );
876 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
879 a_flags = (char *)(e_flags + i);
880 memset( a_flags, 0, k );
881 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
882 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
883 e_flags[i] = a_flags;
887 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
890 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
891 "conn %lu matched values filtering failed\n",
892 op->o_connid ? op->o_connid : 0, 0, 0 );
894 Debug( LDAP_DEBUG_ANY,
895 "matched values filtering failed\n", 0, 0, 0 );
897 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
898 send_ldap_error( op, rs, LDAP_OTHER,
899 "matched values filtering error" );
905 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
906 AttributeDescription *desc = a->a_desc;
909 if ( rs->sr_attrs == NULL ) {
910 /* all attrs request, skip operational attributes */
911 if( is_at_operational( desc->ad_type ) ) {
916 /* specific attrs requested */
917 if ( is_at_operational( desc->ad_type ) ) {
918 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
923 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
929 if ( op->ors_attrsonly ) {
930 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
931 ACL_READ, &acl_state ) )
935 "send_search_entry: conn %lu access to attribute %s not "
936 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
938 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
939 "conn %lu access to attribute %s not allowed\n",
940 op->o_connid, desc->ad_cname.bv_val, 0 );
945 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
947 LDAP_LOG( OPERATION, ERR,
948 "send_search_entry: conn %lu ber_printf failed\n",
949 op->o_connid, 0, 0 );
951 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
954 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
955 send_ldap_error( op, rs, LDAP_OTHER,
956 "encoding description error");
963 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
964 if ( ! access_allowed( op, rs->sr_entry,
965 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
969 "send_search_entry: conn %lu "
970 "access to attribute %s, value #%d not allowed\n",
971 op->o_connid, desc->ad_cname.bv_val, i );
973 Debug( LDAP_DEBUG_ACL,
974 "send_search_entry: conn %lu "
975 "access to attribute %s, value #%d not allowed\n",
976 op->o_connid, desc->ad_cname.bv_val, i );
982 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
989 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
991 LDAP_LOG( OPERATION, ERR,
992 "send_search_entry: conn %lu ber_printf failed\n",
993 op->o_connid, 0, 0 );
995 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
998 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
999 send_ldap_error( op, rs, LDAP_OTHER,
1000 "encoding description error");
1004 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1006 LDAP_LOG( OPERATION, ERR,
1007 "send_search_entry: conn %lu "
1008 "ber_printf failed.\n", op->o_connid, 0, 0 );
1010 Debug( LDAP_DEBUG_ANY,
1011 "ber_printf failed\n", 0, 0, 0 );
1014 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1015 send_ldap_error( op, rs, LDAP_OTHER,
1016 "encoding values error" );
1022 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1024 LDAP_LOG( OPERATION, ERR,
1025 "send_search_entry: conn %lu ber_printf failed\n",
1026 op->o_connid, 0, 0 );
1028 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1031 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1032 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1037 /* eventually will loop through generated operational attributes */
1038 /* only subschemaSubentry and numSubordinates are implemented */
1039 aa = backend_operational( op, rs, opattrs );
1041 if ( aa != NULL && op->o_vrFilter != NULL ) {
1045 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1046 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1049 size = i * sizeof(char *) + k;
1051 char *a_flags, **tmp;
1054 * Reuse previous memory - we likely need less space
1055 * for operational attributes
1057 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1059 if ( tmp == NULL ) {
1061 LDAP_LOG( OPERATION, ERR,
1062 "send_search_entry: conn %lu "
1063 "not enough memory "
1064 "for matched values filtering\n",
1065 op->o_connid, 0, 0);
1067 Debug( LDAP_DEBUG_ANY,
1068 "send_search_entry: conn %lu "
1069 "not enough memory "
1070 "for matched values filtering\n",
1071 op->o_connid, 0, 0 );
1073 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1074 send_ldap_error( op, rs, LDAP_OTHER,
1075 "not enough memory for matched values filtering" );
1079 a_flags = (char *)(e_flags + i);
1080 memset( a_flags, 0, k );
1081 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1082 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1083 e_flags[i] = a_flags;
1086 rc = filter_matched_values(op, aa, &e_flags) ;
1090 LDAP_LOG( OPERATION, ERR,
1091 "send_search_entry: conn %lu "
1092 "matched values filtering failed\n",
1093 op->o_connid ? op->o_connid : 0, 0, 0);
1095 Debug( LDAP_DEBUG_ANY,
1096 "matched values filtering failed\n", 0, 0, 0 );
1098 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1099 send_ldap_error( op, rs, LDAP_OTHER,
1100 "matched values filtering error" );
1106 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
1107 AttributeDescription *desc = a->a_desc;
1109 if ( rs->sr_attrs == NULL ) {
1110 /* all attrs request, skip operational attributes */
1111 if( is_at_operational( desc->ad_type ) ) {
1116 /* specific attrs requested */
1117 if( is_at_operational( desc->ad_type ) ) {
1118 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1122 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1128 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1129 ACL_READ, &acl_state ) )
1132 LDAP_LOG( ACL, INFO,
1133 "send_search_entry: conn %lu "
1134 "access to attribute %s not allowed\n",
1135 op->o_connid, desc->ad_cname.bv_val, 0 );
1137 Debug( LDAP_DEBUG_ACL,
1138 "send_search_entry: conn %lu "
1139 "access to attribute %s not allowed\n",
1140 op->o_connid, desc->ad_cname.bv_val, 0 );
1146 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1149 LDAP_LOG( OPERATION, ERR,
1150 "send_search_entry: conn %lu "
1151 "ber_printf failed\n", op->o_connid, 0, 0 );
1153 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1156 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1157 send_ldap_error( op, rs, LDAP_OTHER,
1158 "encoding description error" );
1163 if ( ! op->ors_attrsonly ) {
1164 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1165 if ( ! access_allowed( op, rs->sr_entry,
1166 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1169 LDAP_LOG( ACL, INFO,
1170 "send_search_entry: conn %lu "
1171 "access to %s, value %d not allowed\n",
1172 op->o_connid, desc->ad_cname.bv_val, i );
1174 Debug( LDAP_DEBUG_ACL,
1175 "send_search_entry: access to attribute %s, "
1176 "value %d not allowed\n",
1177 desc->ad_cname.bv_val, i, 0 );
1183 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1187 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1189 LDAP_LOG( OPERATION, ERR,
1190 "send_search_entry: conn %lu ber_printf failed\n",
1191 op->o_connid, 0, 0 );
1193 Debug( LDAP_DEBUG_ANY,
1194 "ber_printf failed\n", 0, 0, 0 );
1197 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1198 send_ldap_error( op, rs, LDAP_OTHER,
1199 "encoding values error" );
1206 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1208 LDAP_LOG( OPERATION, ERR,
1209 "send_search_entry: conn %lu ber_printf failed\n",
1210 op->o_connid, 0, 0 );
1212 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1215 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1216 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1224 * First, setup the computed attribute context that is
1225 * passed to all plugins.
1228 ctx.cac_pb = op->o_pb;
1229 ctx.cac_attrs = rs->sr_attrs;
1230 ctx.cac_attrsonly = op->ors_attrsonly;
1231 ctx.cac_userattrs = userattrs;
1232 ctx.cac_opattrs = opattrs;
1233 ctx.cac_acl_state = acl_state;
1234 ctx.cac_private = (void *)ber;
1237 * For each client requested attribute, call the plugins.
1239 if ( rs->sr_attrs != NULL ) {
1240 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1241 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1242 rs->sr_entry, slapi_int_compute_output_ber );
1243 if ( rc == 1 ) break;
1247 * Technically we shouldn't be returning operational attributes
1248 * when the user requested only user attributes. We'll let the
1249 * plugin decide whether to be naughty or not.
1251 rc = compute_evaluator( &ctx, "*",
1252 rs->sr_entry, slapi_int_compute_output_ber );
1255 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1256 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1260 #endif /* LDAP_SLAPI */
1264 slap_sl_free( e_flags, op->o_tmpmemctx );
1269 rc = ber_printf( ber, /*{{*/ "}N}" );
1272 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1276 #ifdef LDAP_CONNECTIONLESS
1277 if( op->o_conn && op->o_conn->c_is_udp ) {
1278 if ( op->o_protocol != LDAP_VERSION2 ) {
1279 rc = ber_printf( ber, /*{*/ "N}" );
1283 if ( op->o_res_ber == NULL ) {
1284 rc = ber_printf( ber, /*{*/ "N}" );
1290 LDAP_LOG( OPERATION, ERR,
1291 "send_search_entry: conn %lu ber_printf failed\n",
1292 op->o_connid, 0, 0 );
1294 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1297 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1298 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1303 if ( op->o_res_ber == NULL ) {
1304 bytes = send_ldap_ber( op->o_conn, ber );
1305 ber_free_buf( ber );
1309 LDAP_LOG( OPERATION, ERR,
1310 "send_search_entry: conn %lu ber write failed.\n",
1311 op->o_connid, 0, 0 );
1313 Debug( LDAP_DEBUG_ANY,
1314 "send_search_entry: ber write failed\n",
1323 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1324 num_bytes_sent += bytes;
1327 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1330 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1331 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1334 LDAP_LOG( OPERATION, ENTRY,
1335 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1337 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1343 /* FIXME: I think rs->sr_type should be explicitly set to
1344 * REP_SEARCH here. That's what it was when we entered this
1345 * function. send_ldap_error may have changed it, but we
1346 * should set it back so that the cleanup functions know
1347 * what they're doing.
1349 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1351 && (rs->sr_flags & REP_ENTRY_MUSTBEFREED) )
1353 entry_free( rs->sr_entry );
1354 rs->sr_entry = NULL;
1355 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1358 if ( e_flags ) slap_sl_free( e_flags, op->o_tmpmemctx );
1360 if (op->o_callback) {
1361 slap_callback *sc = op->o_callback;
1362 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
1363 if ( op->o_callback->sc_cleanup ) {
1364 op->o_callback->sc_cleanup( op, rs );
1367 op->o_callback = sc;
1373 slap_send_search_reference( Operation *op, SlapReply *rs )
1375 BerElementBuffer berbuf;
1376 BerElement *ber = (BerElement *) &berbuf;
1380 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1381 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1383 rs->sr_type = REP_SEARCHREF;
1384 if (op->o_callback) {
1385 slap_callback *sc = op->o_callback;
1386 rc = SLAP_CB_CONTINUE;
1387 for ( ; op->o_callback; ) {
1388 if ( op->o_callback->sc_response ) {
1389 rc = op->o_callback->sc_response( op, rs );
1390 if ( rc != SLAP_CB_CONTINUE ) break;
1392 op->o_callback = op->o_callback->sc_next;
1394 op->o_callback = sc;
1395 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1399 LDAP_LOG( OPERATION, ENTRY,
1400 "send_search_reference: conn %lu dn=\"%s\"\n",
1402 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1404 Debug( LDAP_DEBUG_TRACE,
1405 "=> send_search_reference: dn=\"%s\"\n",
1406 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1409 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1410 ad_entry, NULL, ACL_READ, NULL ) )
1413 LDAP_LOG( ACL, INFO,
1414 "send_search_reference: conn %lu "
1415 "access to entry %s not allowed\n",
1416 op->o_connid, rs->sr_entry->e_dn, 0 );
1418 Debug( LDAP_DEBUG_ACL,
1419 "send_search_reference: access to entry not allowed\n",
1426 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1427 ad_ref, NULL, ACL_READ, NULL ) )
1430 LDAP_LOG( ACL, INFO,
1431 "send_search_reference: conn %lu access "
1432 "to reference not allowed.\n", op->o_connid, 0, 0 );
1434 Debug( LDAP_DEBUG_ACL,
1435 "send_search_reference: access "
1436 "to reference not allowed\n",
1443 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1444 if( op->o_domain_scope ) {
1446 LDAP_LOG( OPERATION, ERR,
1447 "send_search_reference: conn %lu domainScope control in (%s).\n",
1448 op->o_connid, rs->sr_entry->e_dn, 0 );
1450 Debug( LDAP_DEBUG_ANY,
1451 "send_search_reference: domainScope control in (%s)\n",
1452 rs->sr_entry->e_dn, 0, 0 );
1459 if( rs->sr_ref == NULL ) {
1461 LDAP_LOG( OPERATION, ERR,
1462 "send_search_reference: conn %lu null ref in (%s).\n",
1463 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1465 Debug( LDAP_DEBUG_ANY,
1466 "send_search_reference: null ref in (%s)\n",
1467 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1473 if( op->o_protocol < LDAP_VERSION3 ) {
1475 /* save the references for the result */
1476 if( rs->sr_ref[0].bv_val != NULL ) {
1477 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1483 #ifdef LDAP_CONNECTIONLESS
1484 if( op->o_conn && op->o_conn->c_is_udp ) {
1485 ber = op->o_res_ber;
1489 ber_init_w_nullc( ber, LBER_USE_DER );
1490 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1493 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1494 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1497 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1501 rc = ber_printf( ber, /*"{"*/ "N}" );
1506 LDAP_LOG( OPERATION, ERR,
1507 "send_search_reference: conn %lu "
1508 "ber_printf failed.\n", op->o_connid, 0, 0 );
1510 Debug( LDAP_DEBUG_ANY,
1511 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1514 #ifdef LDAP_CONNECTIONLESS
1515 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1517 ber_free_buf( ber );
1518 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1522 #ifdef LDAP_CONNECTIONLESS
1523 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1525 bytes = send_ldap_ber( op->o_conn, ber );
1526 ber_free_buf( ber );
1528 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1529 num_bytes_sent += bytes;
1532 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1533 #ifdef LDAP_CONNECTIONLESS
1537 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1538 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1541 LDAP_LOG( OPERATION, ENTRY,
1542 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1544 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1548 if (op->o_callback) {
1549 slap_callback *sc = op->o_callback;
1550 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
1551 if ( op->o_callback->sc_cleanup ) {
1552 op->o_callback->sc_cleanup( op, rs );
1555 op->o_callback = sc;
1571 *code = LDAP_SUCCESS;
1575 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1577 LDAP_LOG( OPERATION, INFO,
1578 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1580 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1588 while ( (s = strchr( s, '\n' )) != NULL ) {
1593 if ( (c = strchr( s, ':' )) != NULL ) {
1597 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1601 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1605 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1611 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1613 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1624 int slap_read_controls(
1628 const struct berval *oid,
1629 LDAPControl **ctrl )
1633 BerElementBuffer berbuf;
1634 BerElement *ber = (BerElement *) &berbuf;
1640 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1641 oid->bv_val, e->e_dn, 0 );
1643 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1644 oid->bv_val, e->e_dn, 0 );
1648 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1649 op->o_preread_attrs : op->o_postread_attrs;
1651 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1652 bv.bv_len = siz + len;
1653 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1655 ber_init2( ber, &bv, LBER_USE_DER );
1656 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1658 /* create new operation */
1661 myop.o_res_ber = ber;
1663 rc = slap_send_search_entry( &myop, rs );
1666 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1668 if( rc == LBER_ERROR ) return LDAP_OTHER;
1670 c.ldctl_oid = oid->bv_val;
1671 c.ldctl_iscritical = 0;
1673 if ( ctrl == NULL ) {
1675 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1677 /* retry: free previous try */
1678 slap_sl_free( (*ctrl)->ldctl_value.bv_val, &op->o_tmpmemctx );
1682 return LDAP_SUCCESS;
1685 /* Map API errors to protocol errors... */
1687 slap_map_api2result( SlapReply *rs )
1689 switch(rs->sr_err) {
1690 case LDAP_SERVER_DOWN:
1691 return LDAP_UNAVAILABLE;
1692 case LDAP_LOCAL_ERROR:
1694 case LDAP_ENCODING_ERROR:
1695 case LDAP_DECODING_ERROR:
1696 return LDAP_PROTOCOL_ERROR;
1698 return LDAP_UNAVAILABLE;
1699 case LDAP_AUTH_UNKNOWN:
1700 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1701 case LDAP_FILTER_ERROR:
1702 rs->sr_text = "Filter error";
1704 case LDAP_USER_CANCELLED:
1705 rs->sr_text = "User cancelled";
1707 case LDAP_PARAM_ERROR:
1708 return LDAP_PROTOCOL_ERROR;
1709 case LDAP_NO_MEMORY:
1711 case LDAP_CONNECT_ERROR:
1712 return LDAP_UNAVAILABLE;
1713 case LDAP_NOT_SUPPORTED:
1714 return LDAP_UNWILLING_TO_PERFORM;
1715 case LDAP_CONTROL_NOT_FOUND:
1716 return LDAP_PROTOCOL_ERROR;
1717 case LDAP_NO_RESULTS_RETURNED:
1718 return LDAP_NO_SUCH_OBJECT;
1719 case LDAP_MORE_RESULTS_TO_RETURN:
1720 rs->sr_text = "More results to return";
1722 case LDAP_CLIENT_LOOP:
1723 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1724 return LDAP_LOOP_DETECT;
1726 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;