1 /* result.c - routines to send ldap results, errors, and referrals */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
33 #include <ac/string.h>
36 #include <ac/unistd.h>
41 #include "slapi/slapi.h"
44 const struct berval slap_dummy_bv = BER_BVNULL;
46 int slap_null_cb( Operation *op, SlapReply *rs )
51 int slap_freeself_cb( Operation *op, SlapReply *rs )
53 assert( op->o_callback );
55 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
56 op->o_callback = NULL;
58 return SLAP_CB_CONTINUE;
61 int slap_replog_cb( Operation *op, SlapReply *rs )
63 if ( rs->sr_err == LDAP_SUCCESS ) {
66 return SLAP_CB_CONTINUE;
69 static char *v2ref( BerVarray ref, const char *text )
71 size_t len = 0, i = 0;
76 return ch_strdup(text);
84 if (text[len-1] != '\n') {
89 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
91 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
101 strcpy( v2+len, "Referral:" );
102 len += sizeof("Referral:");
104 for( i=0; ref[i].bv_val != NULL; i++ ) {
105 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
107 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
111 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
112 len += ref[i].bv_len;
113 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
122 static ber_tag_t req2res( ber_tag_t tag )
127 case LDAP_REQ_COMPARE:
128 case LDAP_REQ_EXTENDED:
129 case LDAP_REQ_MODIFY:
130 case LDAP_REQ_MODRDN:
134 case LDAP_REQ_DELETE:
135 tag = LDAP_RES_DELETE;
138 case LDAP_REQ_ABANDON:
139 case LDAP_REQ_UNBIND:
143 case LDAP_REQ_SEARCH:
144 tag = LDAP_RES_SEARCH_RESULT;
154 static long send_ldap_ber(
160 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
162 /* write only one pdu at a time - wait til it's our turn */
163 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
165 /* lock the connection */
166 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
173 if ( connection_state_closing( conn ) ) {
174 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
175 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
180 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
187 * we got an error. if it's ewouldblock, we need to
188 * wait on the socket being writable. otherwise, figure
189 * it's a hard error and return.
192 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
193 err, sock_errstr(err), 0 );
195 if ( err != EWOULDBLOCK && err != EAGAIN ) {
196 connection_closing( conn );
198 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
199 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
204 /* wait for socket to be write-ready */
205 conn->c_writewaiter = 1;
206 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
207 slapd_set_write( sd, 1 );
209 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
210 conn->c_writewaiter = 0;
213 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
214 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
220 send_ldap_control( BerElement *ber, LDAPControl *c )
226 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
228 if( c->ldctl_iscritical ) {
229 rc = ber_printf( ber, "b",
230 (ber_int_t) c->ldctl_iscritical ) ;
231 if( rc == -1 ) return rc;
234 if( c->ldctl_value.bv_val != NULL ) {
235 rc = ber_printf( ber, "O", &c->ldctl_value );
236 if( rc == -1 ) return rc;
239 rc = ber_printf( ber, /*{*/"N}" );
240 if( rc == -1 ) return rc;
246 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
250 LDAPControl **sctrls = NULL;
253 * Retrieve any additional controls that may be set by the
257 if ( o->o_pb && slapi_pblock_get( o->o_pb, SLAPI_RESCONTROLS, &sctrls ) != 0 ) {
261 if ( c == NULL && sctrls == NULL ) return 0;
263 if( c == NULL ) return 0;
264 #endif /* LDAP_SLAPI */
266 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
267 if( rc == -1 ) return rc;
271 #endif /* LDAP_SLAPI */
272 for( ; *c != NULL; c++) {
273 rc = send_ldap_control( ber, *c );
274 if( rc == -1 ) return rc;
278 if ( sctrls != NULL ) {
279 for ( c = sctrls; *c != NULL; c++ ) {
280 rc = send_ldap_control( ber, *c );
281 if( rc == -1 ) return rc;
284 #endif /* LDAP_SLAPI */
286 rc = ber_printf( ber, /*{*/"N}" );
296 BerElementBuffer berbuf;
297 BerElement *ber = (BerElement *) &berbuf;
298 int rc = LDAP_SUCCESS;
301 if ( op->o_callback ) {
303 slap_callback *sc = op->o_callback,
304 *sc_next = op->o_callback;
306 rc = SLAP_CB_CONTINUE;
307 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
308 sc_next = op->o_callback->sc_next;
309 if ( op->o_callback->sc_response ) {
310 rc = op->o_callback->sc_response( op, rs );
311 if ( first && op->o_callback == NULL ) {
314 if ( rc != SLAP_CB_CONTINUE ) break;
320 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
323 #ifdef LDAP_CONNECTIONLESS
324 if (op->o_conn && op->o_conn->c_is_udp)
329 ber_init_w_nullc( ber, LBER_USE_DER );
330 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
333 Debug( LDAP_DEBUG_TRACE,
334 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
335 rs->sr_msgid, rs->sr_tag, rs->sr_err );
338 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
339 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
343 #ifdef LDAP_CONNECTIONLESS
344 if (op->o_conn && op->o_conn->c_is_udp &&
345 op->o_protocol == LDAP_VERSION2 )
347 rc = ber_printf( ber, "t{ess" /*"}"*/,
348 rs->sr_tag, rs->sr_err,
349 rs->sr_matched == NULL ? "" : rs->sr_matched,
350 rs->sr_text == NULL ? "" : rs->sr_text );
353 if ( rs->sr_type == REP_INTERMEDIATE ) {
354 rc = ber_printf( ber, "{it{" /*"}}"*/,
355 rs->sr_msgid, rs->sr_tag );
358 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
359 rs->sr_msgid, rs->sr_tag, rs->sr_err,
360 rs->sr_matched == NULL ? "" : rs->sr_matched,
361 rs->sr_text == NULL ? "" : rs->sr_text );
365 if ( rs->sr_ref != NULL ) {
366 assert( rs->sr_err == LDAP_REFERRAL );
367 rc = ber_printf( ber, "t{W}",
368 LDAP_TAG_REFERRAL, rs->sr_ref );
370 assert( rs->sr_err != LDAP_REFERRAL );
374 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
375 rc = ber_printf( ber, "tO",
376 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
380 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
382 if ( rs->sr_rspoid != NULL ) {
383 rc = ber_printf( ber, "ts",
384 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
386 if( rc != -1 && rs->sr_rspdata != NULL ) {
387 rc = ber_printf( ber, "tO",
388 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
393 rc = ber_printf( ber, /*"{"*/ "N}" );
397 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
401 rc = ber_printf( ber, /*"{"*/ "N}" );
404 #ifdef LDAP_CONNECTIONLESS
405 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
408 rc = ber_printf( ber, /*"{"*/ "N}" );
413 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
415 #ifdef LDAP_CONNECTIONLESS
416 if (!op->o_conn || op->o_conn->c_is_udp == 0)
425 bytes = send_ldap_ber( op->o_conn, ber );
426 #ifdef LDAP_CONNECTIONLESS
427 if (!op->o_conn || op->o_conn->c_is_udp == 0)
434 Debug( LDAP_DEBUG_ANY,
435 "send_ldap_response: ber write failed\n",
443 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
444 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
445 (void *)rs->sr_matched );
446 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
448 #endif /* LDAP_SLAPI */
450 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
451 num_bytes_sent += bytes;
453 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
456 /* Tell caller that we did this for real, as opposed to being
457 * overridden by a callback
459 rc = SLAP_CB_CONTINUE;
462 if ( op->o_callback ) {
464 slap_callback *sc = op->o_callback,
465 *sc_next = op->o_callback;
467 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
468 sc_next = op->o_callback->sc_next;
469 if ( op->o_callback->sc_cleanup ) {
470 (void)op->o_callback->sc_cleanup( op, rs );
471 if ( first && op->o_callback == NULL ) {
482 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
483 free( (char *)rs->sr_matched );
484 rs->sr_matched = NULL;
487 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
488 ber_bvarray_free( rs->sr_ref );
497 send_ldap_disconnect( Operation *op, SlapReply *rs )
499 #define LDAP_UNSOLICITED_ERROR(e) \
500 ( (e) == LDAP_PROTOCOL_ERROR \
501 || (e) == LDAP_STRONG_AUTH_REQUIRED \
502 || (e) == LDAP_UNAVAILABLE )
504 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
506 rs->sr_type = REP_EXTENDED;
508 Debug( LDAP_DEBUG_TRACE,
509 "send_ldap_disconnect %d:%s\n",
510 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
512 if ( op->o_protocol < LDAP_VERSION3 ) {
513 rs->sr_rspoid = NULL;
514 rs->sr_tag = req2res( op->o_tag );
515 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
518 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
519 rs->sr_tag = LDAP_RES_EXTENDED;
523 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
524 Statslog( LDAP_DEBUG_STATS,
525 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
526 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
527 rs->sr_text ? rs->sr_text : "" );
532 slap_send_ldap_result( Operation *op, SlapReply *rs )
535 const char *otext = rs->sr_text;
536 BerVarray oref = rs->sr_ref;
538 rs->sr_type = REP_RESULT;
540 assert( !LDAP_API_ERROR( rs->sr_err ));
542 Debug( LDAP_DEBUG_TRACE,
543 "send_ldap_result: conn=%lu op=%lu p=%d\n",
544 op->o_connid, op->o_opid, op->o_protocol );
546 Debug( LDAP_DEBUG_ARGS,
547 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
548 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
549 rs->sr_text ? rs->sr_text : "" );
553 Debug( LDAP_DEBUG_ARGS,
554 "send_ldap_result: referral=\"%s\"\n",
555 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
559 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
561 if ( rs->sr_err == LDAP_REFERRAL ) {
562 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
563 if( op->o_domain_scope ) {
567 if( rs->sr_ref == NULL ) {
568 rs->sr_err = LDAP_NO_SUCH_OBJECT;
569 } else if ( op->o_protocol < LDAP_VERSION3 ) {
570 rs->sr_err = LDAP_PARTIAL_RESULTS;
576 * Call pre-result plugins. To avoid infinite recursion plugins
577 * should just set SLAPI_RESULT_CODE rather than sending a
578 * result if they wish to change the result.
580 if ( op->o_pb != NULL ) {
581 slapi_int_pblock_set_operation( op->o_pb, op );
582 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
583 (void *)rs->sr_err );
584 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
585 (void *)rs->sr_text );
586 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
587 (void *)rs->sr_matched );
589 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
592 #endif /* LDAP_SLAPI */
594 if ( op->o_protocol < LDAP_VERSION3 ) {
595 tmp = v2ref( rs->sr_ref, rs->sr_text );
600 rs->sr_tag = req2res( op->o_tag );
601 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
603 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
604 if ( op->o_tag == LDAP_REQ_SEARCH ) {
606 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
607 rs->sr_err, rs->sr_nentries );
609 Statslog( LDAP_DEBUG_STATS,
610 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
611 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
612 rs->sr_text ? rs->sr_text : "" );
614 Statslog( LDAP_DEBUG_STATS,
615 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
616 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
617 rs->sr_text ? rs->sr_text : "" );
621 if( tmp != NULL ) ch_free(tmp);
627 send_ldap_sasl( Operation *op, SlapReply *rs )
629 rs->sr_type = REP_SASL;
630 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
632 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
634 rs->sr_tag = req2res( op->o_tag );
635 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
637 send_ldap_response( op, rs );
641 slap_send_ldap_extended( Operation *op, SlapReply *rs )
643 rs->sr_type = REP_EXTENDED;
645 Debug( LDAP_DEBUG_TRACE,
646 "send_ldap_extended: err=%d oid=%s len=%ld\n",
648 rs->sr_rspoid ? rs->sr_rspoid : "",
649 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
651 rs->sr_tag = req2res( op->o_tag );
652 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
654 send_ldap_response( op, rs );
658 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
660 rs->sr_type = REP_INTERMEDIATE;
661 Debug( LDAP_DEBUG_TRACE,
662 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
664 rs->sr_rspoid ? rs->sr_rspoid : "",
665 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
666 rs->sr_tag = LDAP_RES_INTERMEDIATE;
667 rs->sr_msgid = op->o_msgid;
668 send_ldap_response( op, rs );
672 slap_send_search_entry( Operation *op, SlapReply *rs )
674 BerElementBuffer berbuf;
675 BerElement *ber = (BerElement *) &berbuf;
677 int i, j, rc=-1, bytes;
680 AccessControlState acl_state = ACL_STATE_INIT;
682 /* Support for computed attribute plugins */
683 computed_attr_context ctx;
686 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
688 /* a_flags: array of flags telling if the i-th element will be
689 * returned or filtered out
690 * e_flags: array of a_flags
692 char **e_flags = NULL;
694 rs->sr_type = REP_SEARCH;
696 /* eventually will loop through generated operational attribute types
697 * currently implemented types include:
698 * entryDN, subschemaSubentry, and hasSubordinates */
699 /* NOTE: moved before overlays callback circling because
700 * they may modify entry and other stuff in rs */
701 /* check for special all operational attributes ("+") type */
702 /* FIXME: maybe we could se this flag at the operation level;
703 * however, in principle the caller of send_search_entry() may
704 * change the attribute list at each call */
705 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
707 rc = backend_operational( op, rs );
712 if ( op->o_callback ) {
714 slap_callback *sc = op->o_callback,
715 *sc_next = op->o_callback;
717 rc = SLAP_CB_CONTINUE;
718 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
719 sc_next = op->o_callback->sc_next;
720 if ( op->o_callback->sc_response ) {
721 rc = op->o_callback->sc_response( op, rs );
722 if ( first && op->o_callback == NULL ) {
725 if ( rc != SLAP_CB_CONTINUE ) break;
731 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
734 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
735 op->o_connid, rs->sr_entry->e_name.bv_val,
736 op->ors_attrsonly ? " (attrsOnly)" : "" );
738 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
739 Debug( LDAP_DEBUG_ACL,
740 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
741 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
747 edn = rs->sr_entry->e_nname.bv_val;
749 if ( op->o_res_ber ) {
750 /* read back control or LDAP_CONNECTIONLESS */
756 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
757 bv.bv_len = siz + len;
758 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
760 ber_init2( ber, &bv, LBER_USE_DER );
761 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
764 #ifdef LDAP_CONNECTIONLESS
765 if ( op->o_conn && op->o_conn->c_is_udp ) {
767 if ( op->o_protocol == LDAP_VERSION2 ) {
768 rc = ber_printf(ber, "t{O{" /*}}*/,
769 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
771 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
772 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
776 if ( op->o_res_ber ) {
777 /* read back control */
778 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
780 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
781 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
785 Debug( LDAP_DEBUG_ANY,
786 "send_search_entry: conn %lu ber_printf failed\n",
787 op->o_connid, 0, 0 );
789 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
790 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
794 /* check for special all user attributes ("*") type */
795 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
797 /* create an array of arrays of flags. Each flag corresponds
798 * to particular value of attribute and equals 1 if value matches
799 * to ValuesReturnFilter or 0 if not
801 if ( op->o_vrFilter != NULL ) {
805 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
806 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
809 size = i * sizeof(char *) + k;
812 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
813 if( e_flags == NULL ) {
814 Debug( LDAP_DEBUG_ANY,
815 "send_search_entry: conn %lu slap_sl_calloc failed\n",
816 op->o_connid ? op->o_connid : 0, 0, 0 );
819 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
822 a_flags = (char *)(e_flags + i);
823 memset( a_flags, 0, k );
824 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
825 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
826 e_flags[i] = a_flags;
830 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
832 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
833 "conn %lu matched values filtering failed\n",
834 op->o_connid ? op->o_connid : 0, 0, 0 );
835 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
836 send_ldap_error( op, rs, LDAP_OTHER,
837 "matched values filtering error" );
843 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
844 AttributeDescription *desc = a->a_desc;
847 if ( rs->sr_attrs == NULL ) {
848 /* all attrs request, skip operational attributes */
849 if( is_at_operational( desc->ad_type ) ) {
854 /* specific attrs requested */
855 if ( is_at_operational( desc->ad_type ) ) {
856 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
857 !ad_inlist( desc, rs->sr_attrs ) )
863 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
870 if ( op->ors_attrsonly ) {
871 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
872 ACL_READ, &acl_state ) )
874 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
875 "conn %lu access to attribute %s not allowed\n",
876 op->o_connid, desc->ad_cname.bv_val, 0 );
880 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
881 Debug( LDAP_DEBUG_ANY,
882 "send_search_entry: conn %lu ber_printf failed\n",
883 op->o_connid, 0, 0 );
885 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
886 send_ldap_error( op, rs, LDAP_OTHER,
887 "encoding description error");
894 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
895 if ( ! access_allowed( op, rs->sr_entry,
896 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
898 Debug( LDAP_DEBUG_ACL,
899 "send_search_entry: conn %lu "
900 "access to attribute %s, value #%d not allowed\n",
901 op->o_connid, desc->ad_cname.bv_val, i );
906 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
913 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
914 Debug( LDAP_DEBUG_ANY,
915 "send_search_entry: conn %lu ber_printf failed\n",
916 op->o_connid, 0, 0 );
918 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
919 send_ldap_error( op, rs, LDAP_OTHER,
920 "encoding description error");
924 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
925 Debug( LDAP_DEBUG_ANY,
926 "send_search_entry: conn %lu "
927 "ber_printf failed.\n", op->o_connid, 0, 0 );
929 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
930 send_ldap_error( op, rs, LDAP_OTHER,
931 "encoding values error" );
937 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
938 Debug( LDAP_DEBUG_ANY,
939 "send_search_entry: conn %lu ber_printf failed\n",
940 op->o_connid, 0, 0 );
942 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
943 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
948 /* NOTE: moved before overlays callback circling because
949 * they may modify entry and other stuff in rs */
950 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
954 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
955 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
958 size = i * sizeof(char *) + k;
960 char *a_flags, **tmp;
963 * Reuse previous memory - we likely need less space
964 * for operational attributes
966 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
969 Debug( LDAP_DEBUG_ANY,
970 "send_search_entry: conn %lu "
972 "for matched values filtering\n",
973 op->o_connid, 0, 0 );
974 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
975 send_ldap_error( op, rs, LDAP_OTHER,
976 "not enough memory for matched values filtering" );
980 a_flags = (char *)(e_flags + i);
981 memset( a_flags, 0, k );
982 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
983 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
984 e_flags[i] = a_flags;
987 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
990 Debug( LDAP_DEBUG_ANY,
991 "send_search_entry: conn %lu "
992 "matched values filtering failed\n",
993 op->o_connid ? op->o_connid : 0, 0, 0);
994 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
995 send_ldap_error( op, rs, LDAP_OTHER,
996 "matched values filtering error" );
1002 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1003 AttributeDescription *desc = a->a_desc;
1005 if ( rs->sr_attrs == NULL ) {
1006 /* all attrs request, skip operational attributes */
1007 if( is_at_operational( desc->ad_type ) ) {
1012 /* specific attrs requested */
1013 if( is_at_operational( desc->ad_type ) ) {
1014 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1015 !ad_inlist( desc, rs->sr_attrs ) )
1020 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1026 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1027 ACL_READ, &acl_state ) )
1029 Debug( LDAP_DEBUG_ACL,
1030 "send_search_entry: conn %lu "
1031 "access to attribute %s not allowed\n",
1032 op->o_connid, desc->ad_cname.bv_val, 0 );
1037 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1039 Debug( LDAP_DEBUG_ANY,
1040 "send_search_entry: conn %lu "
1041 "ber_printf failed\n", op->o_connid, 0, 0 );
1043 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1044 send_ldap_error( op, rs, LDAP_OTHER,
1045 "encoding description error" );
1049 if ( ! op->ors_attrsonly ) {
1050 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1051 if ( ! access_allowed( op, rs->sr_entry,
1052 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1054 Debug( LDAP_DEBUG_ACL,
1055 "send_search_entry: conn %lu "
1056 "access to %s, value %d not allowed\n",
1057 op->o_connid, desc->ad_cname.bv_val, i );
1062 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1066 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1067 Debug( LDAP_DEBUG_ANY,
1068 "send_search_entry: conn %lu ber_printf failed\n",
1069 op->o_connid, 0, 0 );
1071 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1072 send_ldap_error( op, rs, LDAP_OTHER,
1073 "encoding values error" );
1079 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1080 Debug( LDAP_DEBUG_ANY,
1081 "send_search_entry: conn %lu ber_printf failed\n",
1082 op->o_connid, 0, 0 );
1084 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1085 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1092 * First, setup the computed attribute context that is
1093 * passed to all plugins.
1096 ctx.cac_pb = op->o_pb;
1097 ctx.cac_attrs = rs->sr_attrs;
1098 ctx.cac_attrsonly = op->ors_attrsonly;
1099 ctx.cac_userattrs = userattrs;
1100 ctx.cac_opattrs = rs->sr_attr_flags;
1101 ctx.cac_acl_state = acl_state;
1102 ctx.cac_private = (void *)ber;
1105 * For each client requested attribute, call the plugins.
1107 if ( rs->sr_attrs != NULL ) {
1108 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1109 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1110 rs->sr_entry, slapi_int_compute_output_ber );
1111 if ( rc == 1 ) break;
1115 * Technically we shouldn't be returning operational attributes
1116 * when the user requested only user attributes. We'll let the
1117 * plugin decide whether to be naughty or not.
1119 rc = compute_evaluator( &ctx, "*",
1120 rs->sr_entry, slapi_int_compute_output_ber );
1123 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1124 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1128 #endif /* LDAP_SLAPI */
1132 slap_sl_free( e_flags, op->o_tmpmemctx );
1136 rc = ber_printf( ber, /*{{*/ "}N}" );
1139 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1143 #ifdef LDAP_CONNECTIONLESS
1144 if( op->o_conn && op->o_conn->c_is_udp ) {
1145 if ( op->o_protocol != LDAP_VERSION2 ) {
1146 rc = ber_printf( ber, /*{*/ "N}" );
1150 if ( op->o_res_ber == NULL ) {
1151 rc = ber_printf( ber, /*{*/ "N}" );
1156 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1158 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1159 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1164 if ( op->o_res_ber == NULL ) {
1165 bytes = send_ldap_ber( op->o_conn, ber );
1166 ber_free_buf( ber );
1169 Debug( LDAP_DEBUG_ANY,
1170 "send_search_entry: conn %lu ber write failed.\n",
1171 op->o_connid, 0, 0 );
1178 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1179 num_bytes_sent += bytes;
1182 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1185 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1186 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1188 Debug( LDAP_DEBUG_TRACE,
1189 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1194 if ( op->o_callback ) {
1196 slap_callback *sc = op->o_callback,
1197 *sc_next = op->o_callback;
1199 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1200 sc_next = op->o_callback->sc_next;
1201 if ( op->o_callback->sc_cleanup ) {
1202 (void)op->o_callback->sc_cleanup( op, rs );
1203 if ( first && op->o_callback == NULL ) {
1210 op->o_callback = sc;
1214 slap_sl_free( e_flags, op->o_tmpmemctx );
1217 if ( rs->sr_operational_attrs ) {
1218 attrs_free( rs->sr_operational_attrs );
1219 rs->sr_operational_attrs = NULL;
1221 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1223 /* FIXME: I think rs->sr_type should be explicitly set to
1224 * REP_SEARCH here. That's what it was when we entered this
1225 * function. send_ldap_error may have changed it, but we
1226 * should set it back so that the cleanup functions know
1227 * what they're doing.
1229 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1231 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1233 entry_free( rs->sr_entry );
1234 rs->sr_entry = NULL;
1235 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1242 slap_send_search_reference( Operation *op, SlapReply *rs )
1244 BerElementBuffer berbuf;
1245 BerElement *ber = (BerElement *) &berbuf;
1249 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1250 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1252 rs->sr_type = REP_SEARCHREF;
1253 if ( op->o_callback ) {
1255 slap_callback *sc = op->o_callback,
1256 *sc_next = op->o_callback;
1258 rc = SLAP_CB_CONTINUE;
1259 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1260 sc_next = op->o_callback->sc_next;
1261 if ( op->o_callback->sc_response ) {
1262 rc = op->o_callback->sc_response( op, rs );
1263 if ( first && op->o_callback == NULL ) {
1266 if ( rc != SLAP_CB_CONTINUE ) break;
1271 op->o_callback = sc;
1272 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1275 Debug( LDAP_DEBUG_TRACE,
1276 "=> send_search_reference: dn=\"%s\"\n",
1277 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1279 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1280 ad_entry, NULL, ACL_READ, NULL ) )
1282 Debug( LDAP_DEBUG_ACL,
1283 "send_search_reference: access to entry not allowed\n",
1289 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1290 ad_ref, NULL, ACL_READ, NULL ) )
1292 Debug( LDAP_DEBUG_ACL,
1293 "send_search_reference: access "
1294 "to reference not allowed\n",
1300 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1301 if( op->o_domain_scope ) {
1302 Debug( LDAP_DEBUG_ANY,
1303 "send_search_reference: domainScope control in (%s)\n",
1304 rs->sr_entry->e_dn, 0, 0 );
1310 if( rs->sr_ref == NULL ) {
1311 Debug( LDAP_DEBUG_ANY,
1312 "send_search_reference: null ref in (%s)\n",
1313 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1318 if( op->o_protocol < LDAP_VERSION3 ) {
1320 /* save the references for the result */
1321 if( rs->sr_ref[0].bv_val != NULL ) {
1322 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1328 #ifdef LDAP_CONNECTIONLESS
1329 if( op->o_conn && op->o_conn->c_is_udp ) {
1330 ber = op->o_res_ber;
1334 ber_init_w_nullc( ber, LBER_USE_DER );
1335 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1338 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1339 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1342 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1346 rc = ber_printf( ber, /*"{"*/ "N}" );
1350 Debug( LDAP_DEBUG_ANY,
1351 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1353 #ifdef LDAP_CONNECTIONLESS
1354 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1356 ber_free_buf( ber );
1357 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1361 #ifdef LDAP_CONNECTIONLESS
1362 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1364 bytes = send_ldap_ber( op->o_conn, ber );
1365 ber_free_buf( ber );
1367 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1368 num_bytes_sent += bytes;
1371 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1372 #ifdef LDAP_CONNECTIONLESS
1376 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1377 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1379 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1382 if ( op->o_callback ) {
1384 slap_callback *sc = op->o_callback,
1385 *sc_next = op->o_callback;
1387 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1388 sc_next = op->o_callback->sc_next;
1389 if ( op->o_callback->sc_cleanup ) {
1390 (void)op->o_callback->sc_cleanup( op, rs );
1391 if ( first && op->o_callback == NULL ) {
1398 op->o_callback = sc;
1415 *code = LDAP_SUCCESS;
1419 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1420 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1427 while ( (s = strchr( s, '\n' )) != NULL ) {
1432 if ( (c = strchr( s, ':' )) != NULL ) {
1436 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1440 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1444 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1449 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1459 int slap_read_controls(
1463 const struct berval *oid,
1464 LDAPControl **ctrl )
1468 BerElementBuffer berbuf;
1469 BerElement *ber = (BerElement *) &berbuf;
1474 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1475 oid->bv_val, e->e_dn, 0 );
1478 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1479 op->o_preread_attrs : op->o_postread_attrs;
1481 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1482 bv.bv_len = siz + len;
1483 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1485 ber_init2( ber, &bv, LBER_USE_DER );
1486 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1488 /* create new operation */
1491 myop.o_res_ber = ber;
1493 rc = slap_send_search_entry( &myop, rs );
1496 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1498 if( rc == LBER_ERROR ) return LDAP_OTHER;
1500 c.ldctl_oid = oid->bv_val;
1501 c.ldctl_iscritical = 0;
1503 if ( ctrl == NULL ) {
1505 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1507 /* retry: free previous try */
1508 slap_sl_free( (*ctrl)->ldctl_value.bv_val, &op->o_tmpmemctx );
1512 return LDAP_SUCCESS;
1515 /* Map API errors to protocol errors... */
1517 slap_map_api2result( SlapReply *rs )
1519 switch(rs->sr_err) {
1520 case LDAP_SERVER_DOWN:
1521 return LDAP_UNAVAILABLE;
1522 case LDAP_LOCAL_ERROR:
1524 case LDAP_ENCODING_ERROR:
1525 case LDAP_DECODING_ERROR:
1526 return LDAP_PROTOCOL_ERROR;
1528 return LDAP_UNAVAILABLE;
1529 case LDAP_AUTH_UNKNOWN:
1530 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1531 case LDAP_FILTER_ERROR:
1532 rs->sr_text = "Filter error";
1534 case LDAP_USER_CANCELLED:
1535 rs->sr_text = "User cancelled";
1537 case LDAP_PARAM_ERROR:
1538 return LDAP_PROTOCOL_ERROR;
1539 case LDAP_NO_MEMORY:
1541 case LDAP_CONNECT_ERROR:
1542 return LDAP_UNAVAILABLE;
1543 case LDAP_NOT_SUPPORTED:
1544 return LDAP_UNWILLING_TO_PERFORM;
1545 case LDAP_CONTROL_NOT_FOUND:
1546 return LDAP_PROTOCOL_ERROR;
1547 case LDAP_NO_RESULTS_RETURNED:
1548 return LDAP_NO_SUCH_OBJECT;
1549 case LDAP_MORE_RESULTS_TO_RETURN:
1550 rs->sr_text = "More results to return";
1552 case LDAP_CLIENT_LOOP:
1553 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1554 return LDAP_LOOP_DETECT;
1556 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1563 slap_attr_flags( AttributeName *an )
1565 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1568 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1571 flags |= an_find( an, &AllOper ) ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1572 flags |= an_find( an, &AllUser ) ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;