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;
481 if ( op->o_callback ) {
482 slap_callback *sc = op->o_callback;
483 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
484 if ( op->o_callback->sc_cleanup ) {
485 (void)op->o_callback->sc_cleanup( op, rs );
492 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
493 free( (char *)rs->sr_matched );
494 rs->sr_matched = NULL;
497 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
498 ber_bvarray_free( rs->sr_ref );
507 send_ldap_disconnect( Operation *op, SlapReply *rs )
509 #define LDAP_UNSOLICITED_ERROR(e) \
510 ( (e) == LDAP_PROTOCOL_ERROR \
511 || (e) == LDAP_STRONG_AUTH_REQUIRED \
512 || (e) == LDAP_UNAVAILABLE )
514 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
516 rs->sr_type = REP_EXTENDED;
519 LDAP_LOG( OPERATION, ENTRY,
520 "send_ldap_disconnect: conn %lu %d:%s\n",
521 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
523 Debug( LDAP_DEBUG_TRACE,
524 "send_ldap_disconnect %d:%s\n",
525 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
528 if ( op->o_protocol < LDAP_VERSION3 ) {
529 rs->sr_rspoid = NULL;
530 rs->sr_tag = req2res( op->o_tag );
531 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
534 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
535 rs->sr_tag = LDAP_RES_EXTENDED;
539 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
540 Statslog( LDAP_DEBUG_STATS,
541 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
542 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
543 rs->sr_text ? rs->sr_text : "" );
548 slap_send_ldap_result( Operation *op, SlapReply *rs )
551 const char *otext = rs->sr_text;
552 BerVarray oref = rs->sr_ref;
554 rs->sr_type = REP_RESULT;
556 assert( !LDAP_API_ERROR( rs->sr_err ));
559 LDAP_LOG( OPERATION, ENTRY,
560 "send_ldap_result: conn=%lu op=%lu p=%d\n",
561 op->o_connid, op->o_opid, op->o_protocol );
563 Debug( LDAP_DEBUG_TRACE,
564 "send_ldap_result: conn=%lu op=%lu p=%d\n",
565 op->o_connid, op->o_opid, op->o_protocol );
569 LDAP_LOG( OPERATION, ARGS,
570 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
571 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
572 rs->sr_text ? rs->sr_text : "" );
574 Debug( LDAP_DEBUG_ARGS,
575 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
576 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
577 rs->sr_text ? rs->sr_text : "" );
583 LDAP_LOG( OPERATION, ARGS,
584 "send_ldap_result: referral=\"%s\"\n",
585 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
587 Debug( LDAP_DEBUG_ARGS,
588 "send_ldap_result: referral=\"%s\"\n",
589 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
594 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
596 if ( rs->sr_err == LDAP_REFERRAL ) {
597 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
598 if( op->o_domain_scope ) {
602 if( rs->sr_ref == NULL ) {
603 rs->sr_err = LDAP_NO_SUCH_OBJECT;
604 } else if ( op->o_protocol < LDAP_VERSION3 ) {
605 rs->sr_err = LDAP_PARTIAL_RESULTS;
611 * Call pre-result plugins. To avoid infinite recursion plugins
612 * should just set SLAPI_RESULT_CODE rather than sending a
613 * result if they wish to change the result.
615 if ( op->o_pb != NULL ) {
616 slapi_int_pblock_set_operation( op->o_pb, op );
617 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
618 (void *)rs->sr_err );
619 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
620 (void *)rs->sr_text );
621 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
622 (void *)rs->sr_matched );
624 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
627 #endif /* LDAP_SLAPI */
629 if ( op->o_protocol < LDAP_VERSION3 ) {
630 tmp = v2ref( rs->sr_ref, rs->sr_text );
635 rs->sr_tag = req2res( op->o_tag );
636 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
638 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
639 if ( op->o_tag == LDAP_REQ_SEARCH ) {
641 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
642 rs->sr_err, rs->sr_nentries );
644 Statslog( LDAP_DEBUG_STATS,
645 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
646 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
647 rs->sr_text ? rs->sr_text : "" );
649 Statslog( LDAP_DEBUG_STATS,
650 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
651 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
652 rs->sr_text ? rs->sr_text : "" );
656 if( tmp != NULL ) ch_free(tmp);
662 send_ldap_sasl( Operation *op, SlapReply *rs )
664 rs->sr_type = REP_SASL;
666 LDAP_LOG( OPERATION, ENTRY,
667 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
668 op->o_connid, rs->sr_err,
669 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
671 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
673 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
676 rs->sr_tag = req2res( op->o_tag );
677 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
679 send_ldap_response( op, rs );
683 slap_send_ldap_extended( Operation *op, SlapReply *rs )
685 rs->sr_type = REP_EXTENDED;
688 LDAP_LOG( OPERATION, ENTRY,
689 "send_ldap_extended: err=%d oid=%s len=%ld\n",
690 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
691 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
693 Debug( LDAP_DEBUG_TRACE,
694 "send_ldap_extended: err=%d oid=%s len=%ld\n",
696 rs->sr_rspoid ? rs->sr_rspoid : "",
697 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
700 rs->sr_tag = req2res( op->o_tag );
701 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
703 send_ldap_response( op, rs );
707 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
709 rs->sr_type = REP_INTERMEDIATE;
711 LDAP_LOG( OPERATION, ENTRY,
712 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
713 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
714 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
716 Debug( LDAP_DEBUG_TRACE,
717 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
719 rs->sr_rspoid ? rs->sr_rspoid : "",
720 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
722 rs->sr_tag = LDAP_RES_INTERMEDIATE;
723 rs->sr_msgid = op->o_msgid;
724 send_ldap_response( op, rs );
728 slap_send_search_entry( Operation *op, SlapReply *rs )
730 BerElementBuffer berbuf;
731 BerElement *ber = (BerElement *) &berbuf;
733 int i, j, rc=-1, bytes;
737 AccessControlState acl_state = ACL_STATE_INIT;
739 /* Support for computed attribute plugins */
740 computed_attr_context ctx;
743 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
745 /* a_flags: array of flags telling if the i-th element will be
746 * returned or filtered out
747 * e_flags: array of a_flags
749 char **e_flags = NULL;
752 rs->sr_type = REP_SEARCH;
753 if (op->o_callback) {
754 slap_callback *sc = op->o_callback;
755 rc = SLAP_CB_CONTINUE;
756 for ( ; op->o_callback; ) {
757 if ( op->o_callback->sc_response ) {
758 rc = op->o_callback->sc_response( op, rs );
759 if ( rc != SLAP_CB_CONTINUE ) break;
761 op->o_callback = op->o_callback->sc_next;
764 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
767 e = rs->sr_mentry ? rs->sr_mentry : rs->sr_entry;
770 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
771 op->o_connid, e->e_name.bv_val,
772 op->ors_attrsonly ? " (attrsOnly)" : "" );
774 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
775 op->o_connid, e->e_name.bv_val,
776 op->ors_attrsonly ? " (attrsOnly)" : "" );
779 if ( !access_allowed( op, e, ad_entry, NULL, ACL_READ, NULL )) {
782 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
783 op->o_connid, e->e_name.bv_val, 0 );
785 Debug( LDAP_DEBUG_ACL,
786 "send_search_entry: access to entry not allowed\n",
794 edn = e->e_nname.bv_val;
796 if ( op->o_res_ber ) {
797 /* read back control or LDAP_CONNECTIONLESS */
804 entry_flatsize( e, &siz, &len, 0 );
805 bv.bv_len = siz + len;
806 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
808 ber_init2( ber, &bv, LBER_USE_DER );
809 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
812 #ifdef LDAP_CONNECTIONLESS
813 if ( op->o_conn && op->o_conn->c_is_udp ) {
815 if ( op->o_protocol == LDAP_VERSION2 ) {
816 rc = ber_printf(ber, "t{O{" /*}}*/,
817 LDAP_RES_SEARCH_ENTRY, &e->e_name );
819 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
820 LDAP_RES_SEARCH_ENTRY, &e->e_name );
824 if ( op->o_res_ber ) {
825 /* read back control */
826 rc = ber_printf( ber, "{O{" /*}}*/, &e->e_name );
828 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
829 LDAP_RES_SEARCH_ENTRY, &e->e_name );
834 LDAP_LOG( OPERATION, ERR,
835 "send_search_entry: conn %lu ber_printf failed\n",
836 op->o_connid, 0, 0 );
838 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
841 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
842 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
846 /* check for special all user attributes ("*") type */
847 userattrs = ( rs->sr_attrs == NULL ) ? 1
848 : an_find( rs->sr_attrs, &AllUser );
850 /* check for special all operational attributes ("+") type */
851 opattrs = ( rs->sr_attrs == NULL ) ? 0
852 : an_find( rs->sr_attrs, &AllOper );
854 /* create an array of arrays of flags. Each flag corresponds
855 * to particular value of attribute and equals 1 if value matches
856 * to ValuesReturnFilter or 0 if not
858 if ( op->o_vrFilter != NULL ) {
862 for ( a = e->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
863 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
866 size = i * sizeof(char *) + k;
869 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
870 if( e_flags == NULL ) {
872 LDAP_LOG( OPERATION, ERR,
873 "send_search_entry: conn %lu slap_sl_calloc failed\n",
874 op->o_connid ? op->o_connid : 0, 0, 0 );
876 Debug( LDAP_DEBUG_ANY,
877 "send_search_entry: slap_sl_calloc failed\n", 0, 0, 0 );
881 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
884 a_flags = (char *)(e_flags + i);
885 memset( a_flags, 0, k );
886 for ( a = e->e_attrs, i = 0; a != NULL; a = a->a_next, i++ ) {
887 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
888 e_flags[i] = a_flags;
892 rc = filter_matched_values(op, e->e_attrs, &e_flags) ;
895 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
896 "conn %lu matched values filtering failed\n",
897 op->o_connid ? op->o_connid : 0, 0, 0 );
899 Debug( LDAP_DEBUG_ANY,
900 "matched values filtering failed\n", 0, 0, 0 );
902 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
903 send_ldap_error( op, rs, LDAP_OTHER,
904 "matched values filtering error" );
910 for ( a = e->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
911 AttributeDescription *desc = a->a_desc;
914 if ( rs->sr_attrs == NULL ) {
915 /* all attrs request, skip operational attributes */
916 if( is_at_operational( desc->ad_type ) ) {
921 /* specific attrs requested */
922 if ( is_at_operational( desc->ad_type ) ) {
923 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
928 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
934 if ( op->ors_attrsonly ) {
935 if ( ! access_allowed( op, e, desc, NULL,
936 ACL_READ, &acl_state ) )
940 "send_search_entry: conn %lu access to attribute %s not "
941 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
943 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
944 "conn %lu access to attribute %s not allowed\n",
945 op->o_connid, desc->ad_cname.bv_val, 0 );
950 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
952 LDAP_LOG( OPERATION, ERR,
953 "send_search_entry: conn %lu ber_printf failed\n",
954 op->o_connid, 0, 0 );
956 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
959 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
960 send_ldap_error( op, rs, LDAP_OTHER,
961 "encoding description error");
968 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
969 if ( ! access_allowed( op, e,
970 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
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 );
978 Debug( LDAP_DEBUG_ACL,
979 "send_search_entry: conn %lu "
980 "access to attribute %s, value #%d not allowed\n",
981 op->o_connid, desc->ad_cname.bv_val, i );
987 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
994 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
996 LDAP_LOG( OPERATION, ERR,
997 "send_search_entry: conn %lu ber_printf failed\n",
998 op->o_connid, 0, 0 );
1000 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1003 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1004 send_ldap_error( op, rs, LDAP_OTHER,
1005 "encoding description error");
1009 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1011 LDAP_LOG( OPERATION, ERR,
1012 "send_search_entry: conn %lu "
1013 "ber_printf failed.\n", op->o_connid, 0, 0 );
1015 Debug( LDAP_DEBUG_ANY,
1016 "ber_printf failed\n", 0, 0, 0 );
1019 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1020 send_ldap_error( op, rs, LDAP_OTHER,
1021 "encoding values error" );
1027 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1029 LDAP_LOG( OPERATION, ERR,
1030 "send_search_entry: conn %lu ber_printf failed\n",
1031 op->o_connid, 0, 0 );
1033 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1036 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1037 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1042 /* eventually will loop through generated operational attributes */
1043 /* only subschemaSubentry and numSubordinates are implemented */
1044 aa = backend_operational( op, rs, opattrs );
1046 if ( aa != NULL && op->o_vrFilter != NULL ) {
1050 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1051 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1054 size = i * sizeof(char *) + k;
1056 char *a_flags, **tmp;
1059 * Reuse previous memory - we likely need less space
1060 * for operational attributes
1062 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1064 if ( tmp == NULL ) {
1066 LDAP_LOG( OPERATION, ERR,
1067 "send_search_entry: conn %lu "
1068 "not enough memory "
1069 "for matched values filtering\n",
1070 op->o_connid, 0, 0);
1072 Debug( LDAP_DEBUG_ANY,
1073 "send_search_entry: conn %lu "
1074 "not enough memory "
1075 "for matched values filtering\n",
1076 op->o_connid, 0, 0 );
1078 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1079 send_ldap_error( op, rs, LDAP_OTHER,
1080 "not enough memory for matched values filtering" );
1084 a_flags = (char *)(e_flags + i);
1085 memset( a_flags, 0, k );
1086 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1087 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1088 e_flags[i] = a_flags;
1091 rc = filter_matched_values(op, aa, &e_flags) ;
1095 LDAP_LOG( OPERATION, ERR,
1096 "send_search_entry: conn %lu "
1097 "matched values filtering failed\n",
1098 op->o_connid ? op->o_connid : 0, 0, 0);
1100 Debug( LDAP_DEBUG_ANY,
1101 "matched values filtering failed\n", 0, 0, 0 );
1103 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1104 send_ldap_error( op, rs, LDAP_OTHER,
1105 "matched values filtering error" );
1111 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
1112 AttributeDescription *desc = a->a_desc;
1114 if ( rs->sr_attrs == NULL ) {
1115 /* all attrs request, skip operational attributes */
1116 if( is_at_operational( desc->ad_type ) ) {
1121 /* specific attrs requested */
1122 if( is_at_operational( desc->ad_type ) ) {
1123 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1127 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1133 if ( ! access_allowed( op, e, desc, NULL,
1134 ACL_READ, &acl_state ) )
1137 LDAP_LOG( ACL, INFO,
1138 "send_search_entry: conn %lu "
1139 "access to attribute %s not allowed\n",
1140 op->o_connid, desc->ad_cname.bv_val, 0 );
1142 Debug( LDAP_DEBUG_ACL,
1143 "send_search_entry: conn %lu "
1144 "access to attribute %s not allowed\n",
1145 op->o_connid, desc->ad_cname.bv_val, 0 );
1151 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1154 LDAP_LOG( OPERATION, ERR,
1155 "send_search_entry: conn %lu "
1156 "ber_printf failed\n", op->o_connid, 0, 0 );
1158 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1161 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1162 send_ldap_error( op, rs, LDAP_OTHER,
1163 "encoding description error" );
1168 if ( ! op->ors_attrsonly ) {
1169 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1170 if ( ! access_allowed( op, e,
1171 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1174 LDAP_LOG( ACL, INFO,
1175 "send_search_entry: conn %lu "
1176 "access to %s, value %d not allowed\n",
1177 op->o_connid, desc->ad_cname.bv_val, i );
1179 Debug( LDAP_DEBUG_ACL,
1180 "send_search_entry: access to attribute %s, "
1181 "value %d not allowed\n",
1182 desc->ad_cname.bv_val, i, 0 );
1188 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1192 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1194 LDAP_LOG( OPERATION, ERR,
1195 "send_search_entry: conn %lu ber_printf failed\n",
1196 op->o_connid, 0, 0 );
1198 Debug( LDAP_DEBUG_ANY,
1199 "ber_printf failed\n", 0, 0, 0 );
1202 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1203 send_ldap_error( op, rs, LDAP_OTHER,
1204 "encoding values error" );
1211 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1213 LDAP_LOG( OPERATION, ERR,
1214 "send_search_entry: conn %lu ber_printf failed\n",
1215 op->o_connid, 0, 0 );
1217 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1220 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1221 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1229 * First, setup the computed attribute context that is
1230 * passed to all plugins.
1233 ctx.cac_pb = op->o_pb;
1234 ctx.cac_attrs = rs->sr_attrs;
1235 ctx.cac_attrsonly = op->ors_attrsonly;
1236 ctx.cac_userattrs = userattrs;
1237 ctx.cac_opattrs = opattrs;
1238 ctx.cac_acl_state = acl_state;
1239 ctx.cac_private = (void *)ber;
1242 * For each client requested attribute, call the plugins.
1244 if ( rs->sr_attrs != NULL ) {
1245 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1246 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1247 e, slapi_int_compute_output_ber );
1248 if ( rc == 1 ) break;
1252 * Technically we shouldn't be returning operational attributes
1253 * when the user requested only user attributes. We'll let the
1254 * plugin decide whether to be naughty or not.
1256 rc = compute_evaluator( &ctx, "*",
1257 e, slapi_int_compute_output_ber );
1260 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1261 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1265 #endif /* LDAP_SLAPI */
1269 slap_sl_free( e_flags, op->o_tmpmemctx );
1274 rc = ber_printf( ber, /*{{*/ "}N}" );
1277 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1281 #ifdef LDAP_CONNECTIONLESS
1282 if( op->o_conn && op->o_conn->c_is_udp ) {
1283 if ( op->o_protocol != LDAP_VERSION2 ) {
1284 rc = ber_printf( ber, /*{*/ "N}" );
1288 if ( op->o_res_ber == NULL ) {
1289 rc = ber_printf( ber, /*{*/ "N}" );
1295 LDAP_LOG( OPERATION, ERR,
1296 "send_search_entry: conn %lu ber_printf failed\n",
1297 op->o_connid, 0, 0 );
1299 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1302 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1303 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1308 if ( op->o_res_ber == NULL ) {
1309 bytes = send_ldap_ber( op->o_conn, ber );
1310 ber_free_buf( ber );
1314 LDAP_LOG( OPERATION, ERR,
1315 "send_search_entry: conn %lu ber write failed.\n",
1316 op->o_connid, 0, 0 );
1318 Debug( LDAP_DEBUG_ANY,
1319 "send_search_entry: ber write failed\n",
1328 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1329 num_bytes_sent += bytes;
1332 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1335 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1336 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
1339 LDAP_LOG( OPERATION, ENTRY,
1340 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1342 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1349 slap_sl_free( e_flags, op->o_tmpmemctx );
1352 if ( op->o_callback ) {
1353 slap_callback *sc = op->o_callback;
1354 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
1355 if ( op->o_callback->sc_cleanup ) {
1356 (void)op->o_callback->sc_cleanup( op, rs );
1359 op->o_callback = sc;
1362 /* FIXME: I think rs->sr_type should be explicitly set to
1363 * REP_SEARCH here. That's what it was when we entered this
1364 * function. send_ldap_error may have changed it, but we
1365 * should set it back so that the cleanup functions know
1366 * what they're doing.
1368 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1370 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1372 entry_free( rs->sr_entry );
1373 rs->sr_entry = NULL;
1374 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1381 slap_send_search_reference( Operation *op, SlapReply *rs )
1383 BerElementBuffer berbuf;
1384 BerElement *ber = (BerElement *) &berbuf;
1388 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1389 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1391 rs->sr_type = REP_SEARCHREF;
1392 if (op->o_callback) {
1393 slap_callback *sc = op->o_callback;
1394 rc = SLAP_CB_CONTINUE;
1395 for ( ; op->o_callback; ) {
1396 if ( op->o_callback->sc_response ) {
1397 rc = op->o_callback->sc_response( op, rs );
1398 if ( rc != SLAP_CB_CONTINUE ) break;
1400 op->o_callback = op->o_callback->sc_next;
1402 op->o_callback = sc;
1403 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1407 LDAP_LOG( OPERATION, ENTRY,
1408 "send_search_reference: conn %lu dn=\"%s\"\n",
1410 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1412 Debug( LDAP_DEBUG_TRACE,
1413 "=> send_search_reference: dn=\"%s\"\n",
1414 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1417 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1418 ad_entry, NULL, ACL_READ, NULL ) )
1421 LDAP_LOG( ACL, INFO,
1422 "send_search_reference: conn %lu "
1423 "access to entry %s not allowed\n",
1424 op->o_connid, rs->sr_entry->e_dn, 0 );
1426 Debug( LDAP_DEBUG_ACL,
1427 "send_search_reference: access to entry not allowed\n",
1434 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1435 ad_ref, NULL, ACL_READ, NULL ) )
1438 LDAP_LOG( ACL, INFO,
1439 "send_search_reference: conn %lu access "
1440 "to reference not allowed.\n", op->o_connid, 0, 0 );
1442 Debug( LDAP_DEBUG_ACL,
1443 "send_search_reference: access "
1444 "to reference not allowed\n",
1451 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1452 if( op->o_domain_scope ) {
1454 LDAP_LOG( OPERATION, ERR,
1455 "send_search_reference: conn %lu domainScope control in (%s).\n",
1456 op->o_connid, rs->sr_entry->e_dn, 0 );
1458 Debug( LDAP_DEBUG_ANY,
1459 "send_search_reference: domainScope control in (%s)\n",
1460 rs->sr_entry->e_dn, 0, 0 );
1467 if( rs->sr_ref == NULL ) {
1469 LDAP_LOG( OPERATION, ERR,
1470 "send_search_reference: conn %lu null ref in (%s).\n",
1471 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1473 Debug( LDAP_DEBUG_ANY,
1474 "send_search_reference: null ref in (%s)\n",
1475 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1481 if( op->o_protocol < LDAP_VERSION3 ) {
1483 /* save the references for the result */
1484 if( rs->sr_ref[0].bv_val != NULL ) {
1485 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1491 #ifdef LDAP_CONNECTIONLESS
1492 if( op->o_conn && op->o_conn->c_is_udp ) {
1493 ber = op->o_res_ber;
1497 ber_init_w_nullc( ber, LBER_USE_DER );
1498 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1501 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1502 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1505 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1509 rc = ber_printf( ber, /*"{"*/ "N}" );
1514 LDAP_LOG( OPERATION, ERR,
1515 "send_search_reference: conn %lu "
1516 "ber_printf failed.\n", op->o_connid, 0, 0 );
1518 Debug( LDAP_DEBUG_ANY,
1519 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1522 #ifdef LDAP_CONNECTIONLESS
1523 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1525 ber_free_buf( ber );
1526 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1530 #ifdef LDAP_CONNECTIONLESS
1531 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1533 bytes = send_ldap_ber( op->o_conn, ber );
1534 ber_free_buf( ber );
1536 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1537 num_bytes_sent += bytes;
1540 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1541 #ifdef LDAP_CONNECTIONLESS
1545 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1546 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1549 LDAP_LOG( OPERATION, ENTRY,
1550 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1552 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1556 if ( op->o_callback ) {
1557 slap_callback *sc = op->o_callback;
1558 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
1559 if ( op->o_callback->sc_cleanup ) {
1560 (void)op->o_callback->sc_cleanup( op, rs );
1563 op->o_callback = sc;
1580 *code = LDAP_SUCCESS;
1584 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1586 LDAP_LOG( OPERATION, INFO,
1587 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1589 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1597 while ( (s = strchr( s, '\n' )) != NULL ) {
1602 if ( (c = strchr( s, ':' )) != NULL ) {
1606 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1610 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1614 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1620 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1622 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1633 int slap_read_controls(
1637 const struct berval *oid,
1638 LDAPControl **ctrl )
1642 BerElementBuffer berbuf;
1643 BerElement *ber = (BerElement *) &berbuf;
1649 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1650 oid->bv_val, e->e_dn, 0 );
1652 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1653 oid->bv_val, e->e_dn, 0 );
1657 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1658 op->o_preread_attrs : op->o_postread_attrs;
1660 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1661 bv.bv_len = siz + len;
1662 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1664 ber_init2( ber, &bv, LBER_USE_DER );
1665 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1667 /* create new operation */
1670 myop.o_res_ber = ber;
1672 rc = slap_send_search_entry( &myop, rs );
1675 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1677 if( rc == LBER_ERROR ) return LDAP_OTHER;
1679 c.ldctl_oid = oid->bv_val;
1680 c.ldctl_iscritical = 0;
1682 if ( ctrl == NULL ) {
1684 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1686 /* retry: free previous try */
1687 slap_sl_free( (*ctrl)->ldctl_value.bv_val, &op->o_tmpmemctx );
1691 return LDAP_SUCCESS;
1694 /* Map API errors to protocol errors... */
1696 slap_map_api2result( SlapReply *rs )
1698 switch(rs->sr_err) {
1699 case LDAP_SERVER_DOWN:
1700 return LDAP_UNAVAILABLE;
1701 case LDAP_LOCAL_ERROR:
1703 case LDAP_ENCODING_ERROR:
1704 case LDAP_DECODING_ERROR:
1705 return LDAP_PROTOCOL_ERROR;
1707 return LDAP_UNAVAILABLE;
1708 case LDAP_AUTH_UNKNOWN:
1709 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1710 case LDAP_FILTER_ERROR:
1711 rs->sr_text = "Filter error";
1713 case LDAP_USER_CANCELLED:
1714 rs->sr_text = "User cancelled";
1716 case LDAP_PARAM_ERROR:
1717 return LDAP_PROTOCOL_ERROR;
1718 case LDAP_NO_MEMORY:
1720 case LDAP_CONNECT_ERROR:
1721 return LDAP_UNAVAILABLE;
1722 case LDAP_NOT_SUPPORTED:
1723 return LDAP_UNWILLING_TO_PERFORM;
1724 case LDAP_CONTROL_NOT_FOUND:
1725 return LDAP_PROTOCOL_ERROR;
1726 case LDAP_NO_RESULTS_RETURNED:
1727 return LDAP_NO_SUCH_OBJECT;
1728 case LDAP_MORE_RESULTS_TO_RETURN:
1729 rs->sr_text = "More results to return";
1731 case LDAP_CLIENT_LOOP:
1732 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1733 return LDAP_LOOP_DETECT;
1735 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;