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-2005 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>
40 const struct berval slap_dummy_bv = BER_BVNULL;
42 int slap_null_cb( Operation *op, SlapReply *rs )
47 int slap_freeself_cb( Operation *op, SlapReply *rs )
49 assert( op->o_callback != NULL );
51 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
52 op->o_callback = NULL;
54 return SLAP_CB_CONTINUE;
57 int slap_replog_cb( Operation *op, SlapReply *rs )
59 if ( rs->sr_err == LDAP_SUCCESS ) {
62 return SLAP_CB_CONTINUE;
65 static char *v2ref( BerVarray ref, const char *text )
67 size_t len = 0, i = 0;
72 return ch_strdup(text);
80 if (text[len-1] != '\n') {
85 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
87 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
97 strcpy( v2+len, "Referral:" );
98 len += sizeof("Referral:");
100 for( i=0; ref[i].bv_val != NULL; i++ ) {
101 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
103 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.
188 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
189 err, sock_errstr(err), 0 );
191 if ( err != EWOULDBLOCK && err != EAGAIN ) {
192 connection_closing( conn );
194 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
195 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
200 /* wait for socket to be write-ready */
201 conn->c_writewaiter = 1;
202 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
203 slapd_set_write( sd, 1 );
205 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
206 conn->c_writewaiter = 0;
209 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
210 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
216 send_ldap_control( BerElement *ber, LDAPControl *c )
222 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
224 if( c->ldctl_iscritical ) {
225 rc = ber_printf( ber, "b",
226 (ber_int_t) c->ldctl_iscritical ) ;
227 if( rc == -1 ) return rc;
230 if( c->ldctl_value.bv_val != NULL ) {
231 rc = ber_printf( ber, "O", &c->ldctl_value );
232 if( rc == -1 ) return rc;
235 rc = ber_printf( ber, /*{*/"N}" );
236 if( rc == -1 ) return rc;
242 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
249 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
250 if( rc == -1 ) return rc;
252 for( ; *c != NULL; c++) {
253 rc = send_ldap_control( ber, *c );
254 if( rc == -1 ) return rc;
258 /* this is a hack to avoid having to modify op->s_ctrls */
259 if( o->o_sortedresults ) {
260 BerElementBuffer berbuf;
261 BerElement *sber = (BerElement *) &berbuf;
263 BER_BVZERO( &sorted.ldctl_value );
264 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
265 sorted.ldctl_iscritical = 0;
267 ber_init2( sber, NULL, LBER_USE_DER );
269 ber_printf( sber, "{i}", LDAP_UNWILLING_TO_PERFORM );
271 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
275 (void) ber_free_buf( ber );
277 rc = send_ldap_control( ber, &sorted );
278 if( rc == -1 ) return rc;
282 rc = ber_printf( ber, /*{*/"N}" );
292 BerElementBuffer berbuf;
293 BerElement *ber = (BerElement *) &berbuf;
294 int rc = LDAP_SUCCESS;
297 if ( rs->sr_err == SLAPD_ABANDON ) {
302 if ( op->o_callback ) {
304 slap_callback *sc = op->o_callback,
305 *sc_next = op->o_callback;
307 rc = SLAP_CB_CONTINUE;
308 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
309 sc_next = op->o_callback->sc_next;
310 if ( op->o_callback->sc_response ) {
311 rc = op->o_callback->sc_response( op, rs );
312 if ( first && op->o_callback == NULL ) {
315 if ( rc != SLAP_CB_CONTINUE ) break;
321 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
324 #ifdef LDAP_CONNECTIONLESS
325 if (op->o_conn && op->o_conn->c_is_udp)
330 ber_init_w_nullc( ber, LBER_USE_DER );
331 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
334 Debug( LDAP_DEBUG_TRACE,
335 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
336 rs->sr_msgid, rs->sr_tag, rs->sr_err );
339 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
340 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
344 #ifdef LDAP_CONNECTIONLESS
345 if (op->o_conn && op->o_conn->c_is_udp &&
346 op->o_protocol == LDAP_VERSION2 )
348 rc = ber_printf( ber, "t{ess" /*"}"*/,
349 rs->sr_tag, rs->sr_err,
350 rs->sr_matched == NULL ? "" : rs->sr_matched,
351 rs->sr_text == NULL ? "" : rs->sr_text );
354 if ( rs->sr_type == REP_INTERMEDIATE ) {
355 rc = ber_printf( ber, "{it{" /*"}}"*/,
356 rs->sr_msgid, rs->sr_tag );
359 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
360 rs->sr_msgid, rs->sr_tag, rs->sr_err,
361 rs->sr_matched == NULL ? "" : rs->sr_matched,
362 rs->sr_text == NULL ? "" : rs->sr_text );
366 if ( rs->sr_ref != NULL ) {
367 assert( rs->sr_err == LDAP_REFERRAL );
368 rc = ber_printf( ber, "t{W}",
369 LDAP_TAG_REFERRAL, rs->sr_ref );
371 assert( rs->sr_err != LDAP_REFERRAL );
375 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
376 rc = ber_printf( ber, "tO",
377 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
381 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
383 if ( rs->sr_rspoid != NULL ) {
384 rc = ber_printf( ber, "ts",
385 rs->sr_type == REP_EXTENDED
386 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
389 if( rc != -1 && rs->sr_rspdata != NULL ) {
390 rc = ber_printf( ber, "tO",
391 rs->sr_type == REP_EXTENDED
392 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
398 rc = ber_printf( ber, /*"{"*/ "N}" );
402 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
406 rc = ber_printf( ber, /*"{"*/ "N}" );
409 #ifdef LDAP_CONNECTIONLESS
410 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
413 rc = ber_printf( ber, /*"{"*/ "N}" );
418 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
420 #ifdef LDAP_CONNECTIONLESS
421 if (!op->o_conn || op->o_conn->c_is_udp == 0)
430 bytes = send_ldap_ber( op->o_conn, ber );
431 #ifdef LDAP_CONNECTIONLESS
432 if (!op->o_conn || op->o_conn->c_is_udp == 0)
439 Debug( LDAP_DEBUG_ANY,
440 "send_ldap_response: ber write failed\n",
446 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
447 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
448 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
449 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
452 /* Tell caller that we did this for real, as opposed to being
453 * overridden by a callback
455 rc = SLAP_CB_CONTINUE;
458 if ( op->o_callback ) {
460 slap_callback *sc = op->o_callback, *sc_next;
462 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
463 sc_next = op->o_callback->sc_next;
464 if ( op->o_callback->sc_cleanup ) {
465 (void)op->o_callback->sc_cleanup( op, rs );
466 if ( first && op->o_callback != sc ) {
476 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
477 free( (char *)rs->sr_matched );
478 rs->sr_matched = NULL;
481 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
482 ber_bvarray_free( rs->sr_ref );
491 send_ldap_disconnect( Operation *op, SlapReply *rs )
493 #define LDAP_UNSOLICITED_ERROR(e) \
494 ( (e) == LDAP_PROTOCOL_ERROR \
495 || (e) == LDAP_STRONG_AUTH_REQUIRED \
496 || (e) == LDAP_UNAVAILABLE )
498 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
500 rs->sr_type = REP_EXTENDED;
502 Debug( LDAP_DEBUG_TRACE,
503 "send_ldap_disconnect %d:%s\n",
504 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
506 if ( op->o_protocol < LDAP_VERSION3 ) {
507 rs->sr_rspoid = NULL;
508 rs->sr_tag = req2res( op->o_tag );
509 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
512 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
513 rs->sr_tag = LDAP_RES_EXTENDED;
517 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
518 Statslog( LDAP_DEBUG_STATS,
519 "%s DISCONNECT tag=%lu err=%d text=%s\n",
520 op->o_log_prefix, rs->sr_tag, rs->sr_err,
521 rs->sr_text ? rs->sr_text : "", 0 );
526 slap_send_ldap_result( Operation *op, SlapReply *rs )
529 const char *otext = rs->sr_text;
530 BerVarray oref = rs->sr_ref;
532 rs->sr_type = REP_RESULT;
534 /* Propagate Abandons so that cleanup callbacks can be processed */
535 if ( rs->sr_err == SLAPD_ABANDON )
538 assert( !LDAP_API_ERROR( rs->sr_err ) );
540 Debug( LDAP_DEBUG_TRACE,
541 "send_ldap_result: %s p=%d\n",
542 op->o_log_prefix, op->o_protocol, 0 );
544 Debug( LDAP_DEBUG_ARGS,
545 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
546 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
547 rs->sr_text ? rs->sr_text : "" );
551 Debug( LDAP_DEBUG_ARGS,
552 "send_ldap_result: referral=\"%s\"\n",
553 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
557 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
559 if ( rs->sr_err == LDAP_REFERRAL ) {
560 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
561 if( op->o_domain_scope ) {
565 if( rs->sr_ref == NULL ) {
566 rs->sr_err = LDAP_NO_SUCH_OBJECT;
567 } else if ( op->o_protocol < LDAP_VERSION3 ) {
568 rs->sr_err = LDAP_PARTIAL_RESULTS;
572 if ( op->o_protocol < LDAP_VERSION3 ) {
573 tmp = v2ref( rs->sr_ref, rs->sr_text );
578 rs->sr_tag = req2res( op->o_tag );
579 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
582 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
583 if ( op->o_tag == LDAP_REQ_SEARCH ) {
585 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
586 rs->sr_err, rs->sr_nentries );
588 Statslog( LDAP_DEBUG_STATS,
589 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
590 op->o_log_prefix, rs->sr_tag, nbuf,
591 rs->sr_text ? rs->sr_text : "", 0 );
593 Statslog( LDAP_DEBUG_STATS,
594 "%s RESULT tag=%lu err=%d text=%s\n",
595 op->o_log_prefix, rs->sr_tag, rs->sr_err,
596 rs->sr_text ? rs->sr_text : "", 0 );
600 if( tmp != NULL ) ch_free(tmp);
606 send_ldap_sasl( Operation *op, SlapReply *rs )
608 rs->sr_type = REP_SASL;
609 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
611 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
613 rs->sr_tag = req2res( op->o_tag );
614 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
616 send_ldap_response( op, rs );
620 slap_send_ldap_extended( Operation *op, SlapReply *rs )
622 rs->sr_type = REP_EXTENDED;
624 Debug( LDAP_DEBUG_TRACE,
625 "send_ldap_extended: err=%d oid=%s len=%ld\n",
627 rs->sr_rspoid ? rs->sr_rspoid : "",
628 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
630 rs->sr_tag = req2res( op->o_tag );
631 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
633 send_ldap_response( op, rs );
637 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
639 rs->sr_type = REP_INTERMEDIATE;
640 Debug( LDAP_DEBUG_TRACE,
641 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
643 rs->sr_rspoid ? rs->sr_rspoid : "",
644 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
645 rs->sr_tag = LDAP_RES_INTERMEDIATE;
646 rs->sr_msgid = op->o_msgid;
647 send_ldap_response( op, rs );
651 slap_send_search_entry( Operation *op, SlapReply *rs )
653 BerElementBuffer berbuf;
654 BerElement *ber = (BerElement *) &berbuf;
656 int i, j, rc=-1, bytes;
659 AccessControlState acl_state = ACL_STATE_INIT;
661 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
663 /* a_flags: array of flags telling if the i-th element will be
664 * returned or filtered out
665 * e_flags: array of a_flags
667 char **e_flags = NULL;
669 rs->sr_type = REP_SEARCH;
671 /* eventually will loop through generated operational attribute types
672 * currently implemented types include:
673 * entryDN, subschemaSubentry, and hasSubordinates */
674 /* NOTE: moved before overlays callback circling because
675 * they may modify entry and other stuff in rs */
676 /* check for special all operational attributes ("+") type */
677 /* FIXME: maybe we could se this flag at the operation level;
678 * however, in principle the caller of send_search_entry() may
679 * change the attribute list at each call */
680 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
682 rc = backend_operational( op, rs );
687 if ( op->o_callback ) {
689 slap_callback *sc = op->o_callback,
690 *sc_next = op->o_callback;
692 rc = SLAP_CB_CONTINUE;
693 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
694 sc_next = op->o_callback->sc_next;
695 if ( op->o_callback->sc_response ) {
696 rc = op->o_callback->sc_response( op, rs );
697 if ( first && op->o_callback == NULL ) {
700 if ( rc != SLAP_CB_CONTINUE ) break;
706 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
709 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
710 op->o_connid, rs->sr_entry->e_name.bv_val,
711 op->ors_attrsonly ? " (attrsOnly)" : "" );
713 attrsonly = op->ors_attrsonly;
715 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
716 Debug( LDAP_DEBUG_ACL,
717 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
718 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
724 edn = rs->sr_entry->e_nname.bv_val;
726 if ( op->o_res_ber ) {
727 /* read back control or LDAP_CONNECTIONLESS */
732 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
733 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
735 ber_init2( ber, &bv, LBER_USE_DER );
736 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
739 #ifdef LDAP_CONNECTIONLESS
740 if ( op->o_conn && op->o_conn->c_is_udp ) {
742 if ( op->o_protocol == LDAP_VERSION2 ) {
743 rc = ber_printf(ber, "t{O{" /*}}*/,
744 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
746 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
747 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
751 if ( op->o_res_ber ) {
752 /* read back control */
753 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
755 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
756 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
760 Debug( LDAP_DEBUG_ANY,
761 "send_search_entry: conn %lu ber_printf failed\n",
762 op->o_connid, 0, 0 );
764 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
765 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
769 /* check for special all user attributes ("*") type */
770 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
772 /* create an array of arrays of flags. Each flag corresponds
773 * to particular value of attribute and equals 1 if value matches
774 * to ValuesReturnFilter or 0 if not
776 if ( op->o_vrFilter != NULL ) {
780 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
781 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
784 size = i * sizeof(char *) + k;
787 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
788 if( e_flags == NULL ) {
789 Debug( LDAP_DEBUG_ANY,
790 "send_search_entry: conn %lu slap_sl_calloc failed\n",
791 op->o_connid ? op->o_connid : 0, 0, 0 );
794 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
797 a_flags = (char *)(e_flags + i);
798 memset( a_flags, 0, k );
799 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
800 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
801 e_flags[i] = a_flags;
805 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
807 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
808 "conn %lu matched values filtering failed\n",
809 op->o_connid ? op->o_connid : 0, 0, 0 );
810 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
811 send_ldap_error( op, rs, LDAP_OTHER,
812 "matched values filtering error" );
818 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
819 AttributeDescription *desc = a->a_desc;
822 if ( rs->sr_attrs == NULL ) {
823 /* all attrs request, skip operational attributes */
824 if( is_at_operational( desc->ad_type ) ) {
829 /* specific attrs requested */
830 if ( is_at_operational( desc->ad_type ) ) {
831 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
832 !ad_inlist( desc, rs->sr_attrs ) )
838 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
846 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
847 ACL_READ, &acl_state ) )
849 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
850 "conn %lu access to attribute %s not allowed\n",
851 op->o_connid, desc->ad_cname.bv_val, 0 );
855 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
856 Debug( LDAP_DEBUG_ANY,
857 "send_search_entry: conn %lu ber_printf failed\n",
858 op->o_connid, 0, 0 );
860 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
861 send_ldap_error( op, rs, LDAP_OTHER,
862 "encoding description error");
869 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
870 if ( ! access_allowed( op, rs->sr_entry,
871 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
873 Debug( LDAP_DEBUG_ACL,
874 "send_search_entry: conn %lu "
875 "access to attribute %s, value #%d not allowed\n",
876 op->o_connid, desc->ad_cname.bv_val, i );
881 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
888 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
889 Debug( LDAP_DEBUG_ANY,
890 "send_search_entry: conn %lu ber_printf failed\n",
891 op->o_connid, 0, 0 );
893 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
894 send_ldap_error( op, rs, LDAP_OTHER,
895 "encoding description error");
899 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
900 Debug( LDAP_DEBUG_ANY,
901 "send_search_entry: conn %lu "
902 "ber_printf failed.\n", op->o_connid, 0, 0 );
904 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
905 send_ldap_error( op, rs, LDAP_OTHER,
906 "encoding values error" );
912 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
913 Debug( LDAP_DEBUG_ANY,
914 "send_search_entry: conn %lu ber_printf failed\n",
915 op->o_connid, 0, 0 );
917 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
918 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
923 /* NOTE: moved before overlays callback circling because
924 * they may modify entry and other stuff in rs */
925 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
929 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
930 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
933 size = i * sizeof(char *) + k;
935 char *a_flags, **tmp;
938 * Reuse previous memory - we likely need less space
939 * for operational attributes
941 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
944 Debug( LDAP_DEBUG_ANY,
945 "send_search_entry: conn %lu "
947 "for matched values filtering\n",
948 op->o_connid, 0, 0 );
949 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
950 send_ldap_error( op, rs, LDAP_OTHER,
951 "not enough memory for matched values filtering" );
955 a_flags = (char *)(e_flags + i);
956 memset( a_flags, 0, k );
957 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
958 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
959 e_flags[i] = a_flags;
962 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
965 Debug( LDAP_DEBUG_ANY,
966 "send_search_entry: conn %lu "
967 "matched values filtering failed\n",
968 op->o_connid ? op->o_connid : 0, 0, 0);
969 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
970 send_ldap_error( op, rs, LDAP_OTHER,
971 "matched values filtering error" );
977 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
978 AttributeDescription *desc = a->a_desc;
980 if ( rs->sr_attrs == NULL ) {
981 /* all attrs request, skip operational attributes */
982 if( is_at_operational( desc->ad_type ) ) {
987 /* specific attrs requested */
988 if( is_at_operational( desc->ad_type ) ) {
989 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
990 !ad_inlist( desc, rs->sr_attrs ) )
995 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1001 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1002 ACL_READ, &acl_state ) )
1004 Debug( LDAP_DEBUG_ACL,
1005 "send_search_entry: conn %lu "
1006 "access to attribute %s not allowed\n",
1007 op->o_connid, desc->ad_cname.bv_val, 0 );
1012 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1014 Debug( LDAP_DEBUG_ANY,
1015 "send_search_entry: conn %lu "
1016 "ber_printf failed\n", op->o_connid, 0, 0 );
1018 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1019 send_ldap_error( op, rs, LDAP_OTHER,
1020 "encoding description error" );
1024 if ( ! attrsonly ) {
1025 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1026 if ( ! access_allowed( op, rs->sr_entry,
1027 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1029 Debug( LDAP_DEBUG_ACL,
1030 "send_search_entry: conn %lu "
1031 "access to %s, value %d not allowed\n",
1032 op->o_connid, desc->ad_cname.bv_val, i );
1037 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1041 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1042 Debug( LDAP_DEBUG_ANY,
1043 "send_search_entry: conn %lu ber_printf failed\n",
1044 op->o_connid, 0, 0 );
1046 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1047 send_ldap_error( op, rs, LDAP_OTHER,
1048 "encoding values error" );
1054 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1055 Debug( LDAP_DEBUG_ANY,
1056 "send_search_entry: conn %lu ber_printf failed\n",
1057 op->o_connid, 0, 0 );
1059 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1060 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1067 slap_sl_free( e_flags, op->o_tmpmemctx );
1071 rc = ber_printf( ber, /*{{*/ "}N}" );
1074 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1078 #ifdef LDAP_CONNECTIONLESS
1079 if( op->o_conn && op->o_conn->c_is_udp ) {
1080 if ( op->o_protocol != LDAP_VERSION2 ) {
1081 rc = ber_printf( ber, /*{*/ "N}" );
1085 if ( op->o_res_ber == NULL ) {
1086 rc = ber_printf( ber, /*{*/ "N}" );
1091 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1093 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1094 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1099 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1100 be_entry_release_rw( op, rs->sr_entry, 0 );
1101 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1102 rs->sr_entry = NULL;
1105 if ( op->o_res_ber == NULL ) {
1106 bytes = send_ldap_ber( op->o_conn, ber );
1107 ber_free_buf( ber );
1110 Debug( LDAP_DEBUG_ANY,
1111 "send_search_entry: conn %lu ber write failed.\n",
1112 op->o_connid, 0, 0 );
1119 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1120 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1121 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1122 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1123 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1126 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1127 op->o_log_prefix, edn, 0, 0, 0 );
1129 Debug( LDAP_DEBUG_TRACE,
1130 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1135 if ( op->o_callback ) {
1137 slap_callback *sc = op->o_callback, *sc_next;
1139 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1140 sc_next = op->o_callback->sc_next;
1141 if ( op->o_callback->sc_cleanup ) {
1142 (void)op->o_callback->sc_cleanup( op, rs );
1143 if ( first && op->o_callback != sc ) {
1144 sc = op->o_callback;
1149 op->o_callback = sc;
1153 slap_sl_free( e_flags, op->o_tmpmemctx );
1156 if ( rs->sr_operational_attrs ) {
1157 attrs_free( rs->sr_operational_attrs );
1158 rs->sr_operational_attrs = NULL;
1160 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1162 /* FIXME: I think rs->sr_type should be explicitly set to
1163 * REP_SEARCH here. That's what it was when we entered this
1164 * function. send_ldap_error may have changed it, but we
1165 * should set it back so that the cleanup functions know
1166 * what they're doing.
1168 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1170 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1172 entry_free( rs->sr_entry );
1173 rs->sr_entry = NULL;
1174 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1181 slap_send_search_reference( Operation *op, SlapReply *rs )
1183 BerElementBuffer berbuf;
1184 BerElement *ber = (BerElement *) &berbuf;
1188 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1189 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1191 rs->sr_type = REP_SEARCHREF;
1192 if ( op->o_callback ) {
1194 slap_callback *sc = op->o_callback,
1195 *sc_next = op->o_callback;
1197 rc = SLAP_CB_CONTINUE;
1198 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1199 sc_next = op->o_callback->sc_next;
1200 if ( op->o_callback->sc_response ) {
1201 rc = op->o_callback->sc_response( op, rs );
1202 if ( first && op->o_callback == NULL ) {
1205 if ( rc != SLAP_CB_CONTINUE ) break;
1210 op->o_callback = sc;
1211 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1214 Debug( LDAP_DEBUG_TRACE,
1215 "=> send_search_reference: dn=\"%s\"\n",
1216 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1218 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1219 ad_entry, NULL, ACL_READ, NULL ) )
1221 Debug( LDAP_DEBUG_ACL,
1222 "send_search_reference: access to entry not allowed\n",
1228 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1229 ad_ref, NULL, ACL_READ, NULL ) )
1231 Debug( LDAP_DEBUG_ACL,
1232 "send_search_reference: access "
1233 "to reference not allowed\n",
1239 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1240 if( op->o_domain_scope ) {
1241 Debug( LDAP_DEBUG_ANY,
1242 "send_search_reference: domainScope control in (%s)\n",
1243 rs->sr_entry->e_dn, 0, 0 );
1249 if( rs->sr_ref == NULL ) {
1250 Debug( LDAP_DEBUG_ANY,
1251 "send_search_reference: null ref in (%s)\n",
1252 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1257 if( op->o_protocol < LDAP_VERSION3 ) {
1259 /* save the references for the result */
1260 if( rs->sr_ref[0].bv_val != NULL ) {
1261 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1267 #ifdef LDAP_CONNECTIONLESS
1268 if( op->o_conn && op->o_conn->c_is_udp ) {
1269 ber = op->o_res_ber;
1273 ber_init_w_nullc( ber, LBER_USE_DER );
1274 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1277 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1278 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1281 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1285 rc = ber_printf( ber, /*"{"*/ "N}" );
1289 Debug( LDAP_DEBUG_ANY,
1290 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1292 #ifdef LDAP_CONNECTIONLESS
1293 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1295 ber_free_buf( ber );
1296 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1301 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1302 be_entry_release_rw( op, rs->sr_entry, 0 );
1303 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1304 rs->sr_entry = NULL;
1307 #ifdef LDAP_CONNECTIONLESS
1308 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1310 bytes = send_ldap_ber( op->o_conn, ber );
1311 ber_free_buf( ber );
1313 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1314 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1315 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1316 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1317 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1318 #ifdef LDAP_CONNECTIONLESS
1322 Statslog( LDAP_DEBUG_STATS2, "%s REF dn=\"%s\"\n",
1323 op->o_log_prefix, rs->sr_entry ? rs->sr_entry->e_dn : "(null)",
1326 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1329 if ( op->o_callback ) {
1331 slap_callback *sc = op->o_callback, *sc_next;
1333 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1334 sc_next = op->o_callback->sc_next;
1335 if ( op->o_callback->sc_cleanup ) {
1336 (void)op->o_callback->sc_cleanup( op, rs );
1337 if ( first && op->o_callback != sc ) {
1338 sc = op->o_callback;
1343 op->o_callback = sc;
1359 *code = LDAP_SUCCESS;
1363 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1364 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1371 while ( (s = strchr( s, '\n' )) != NULL ) {
1376 if ( (c = strchr( s, ':' )) != NULL ) {
1380 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1384 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1388 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1393 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1403 int slap_read_controls(
1407 const struct berval *oid,
1408 LDAPControl **ctrl )
1412 BerElementBuffer berbuf;
1413 BerElement *ber = (BerElement *) &berbuf;
1417 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1418 oid->bv_val, e->e_dn, 0 );
1421 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1422 op->o_preread_attrs : op->o_postread_attrs;
1424 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1425 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1427 ber_init2( ber, &bv, LBER_USE_DER );
1428 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1430 /* create new operation */
1433 myop.o_res_ber = ber;
1434 myop.o_callback = NULL;
1436 rc = slap_send_search_entry( &myop, rs );
1439 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1441 if( rc == LBER_ERROR ) return LDAP_OTHER;
1443 c.ldctl_oid = oid->bv_val;
1444 c.ldctl_iscritical = 0;
1446 if ( *ctrl == NULL ) {
1448 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1450 /* retry: free previous try */
1451 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1455 return LDAP_SUCCESS;
1458 /* Map API errors to protocol errors... */
1460 slap_map_api2result( SlapReply *rs )
1462 switch(rs->sr_err) {
1463 case LDAP_SERVER_DOWN:
1464 return LDAP_UNAVAILABLE;
1465 case LDAP_LOCAL_ERROR:
1467 case LDAP_ENCODING_ERROR:
1468 case LDAP_DECODING_ERROR:
1469 return LDAP_PROTOCOL_ERROR;
1471 return LDAP_UNAVAILABLE;
1472 case LDAP_AUTH_UNKNOWN:
1473 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1474 case LDAP_FILTER_ERROR:
1475 rs->sr_text = "Filter error";
1477 case LDAP_USER_CANCELLED:
1478 rs->sr_text = "User cancelled";
1480 case LDAP_PARAM_ERROR:
1481 return LDAP_PROTOCOL_ERROR;
1482 case LDAP_NO_MEMORY:
1484 case LDAP_CONNECT_ERROR:
1485 return LDAP_UNAVAILABLE;
1486 case LDAP_NOT_SUPPORTED:
1487 return LDAP_UNWILLING_TO_PERFORM;
1488 case LDAP_CONTROL_NOT_FOUND:
1489 return LDAP_PROTOCOL_ERROR;
1490 case LDAP_NO_RESULTS_RETURNED:
1491 return LDAP_NO_SUCH_OBJECT;
1492 case LDAP_MORE_RESULTS_TO_RETURN:
1493 rs->sr_text = "More results to return";
1495 case LDAP_CLIENT_LOOP:
1496 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1497 return LDAP_LOOP_DETECT;
1499 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1506 slap_attr_flags( AttributeName *an )
1508 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1511 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1514 flags |= an_find( an, &AllOper )
1515 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1516 flags |= an_find( an, &AllUser )
1517 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;