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, "connection lost on write" );
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, "{e}", 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 ) {
303 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
305 rc = SLAP_CB_CONTINUE;
306 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
307 sc_next = op->o_callback->sc_next;
308 if ( op->o_callback->sc_response ) {
309 rc = op->o_callback->sc_response( op, rs );
310 if ( op->o_callback != *sc_prev ) {
311 *sc_prev = op->o_callback;
313 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
315 sc_prev = &op->o_callback->sc_next;
319 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
322 #ifdef LDAP_CONNECTIONLESS
323 if (op->o_conn && op->o_conn->c_is_udp)
328 ber_init_w_nullc( ber, LBER_USE_DER );
329 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
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 );
337 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
338 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
342 #ifdef LDAP_CONNECTIONLESS
343 if (op->o_conn && op->o_conn->c_is_udp &&
344 op->o_protocol == LDAP_VERSION2 )
346 rc = ber_printf( ber, "t{ess" /*"}"*/,
347 rs->sr_tag, rs->sr_err,
348 rs->sr_matched == NULL ? "" : rs->sr_matched,
349 rs->sr_text == NULL ? "" : rs->sr_text );
352 if ( rs->sr_type == REP_INTERMEDIATE ) {
353 rc = ber_printf( ber, "{it{" /*"}}"*/,
354 rs->sr_msgid, rs->sr_tag );
357 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
358 rs->sr_msgid, rs->sr_tag, rs->sr_err,
359 rs->sr_matched == NULL ? "" : rs->sr_matched,
360 rs->sr_text == NULL ? "" : rs->sr_text );
364 if ( rs->sr_ref != NULL ) {
365 assert( rs->sr_err == LDAP_REFERRAL );
366 rc = ber_printf( ber, "t{W}",
367 LDAP_TAG_REFERRAL, rs->sr_ref );
369 assert( rs->sr_err != LDAP_REFERRAL );
373 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
374 rc = ber_printf( ber, "tO",
375 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
379 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
381 if ( rs->sr_rspoid != NULL ) {
382 rc = ber_printf( ber, "ts",
383 rs->sr_type == REP_EXTENDED
384 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
387 if( rc != -1 && rs->sr_rspdata != NULL ) {
388 rc = ber_printf( ber, "tO",
389 rs->sr_type == REP_EXTENDED
390 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
396 rc = ber_printf( ber, /*"{"*/ "N}" );
400 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
404 rc = ber_printf( ber, /*"{"*/ "N}" );
407 #ifdef LDAP_CONNECTIONLESS
408 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
411 rc = ber_printf( ber, /*"{"*/ "N}" );
416 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
418 #ifdef LDAP_CONNECTIONLESS
419 if (!op->o_conn || op->o_conn->c_is_udp == 0)
428 bytes = send_ldap_ber( op->o_conn, ber );
429 #ifdef LDAP_CONNECTIONLESS
430 if (!op->o_conn || op->o_conn->c_is_udp == 0)
437 Debug( LDAP_DEBUG_ANY,
438 "send_ldap_response: ber write failed\n",
444 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
445 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
446 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
447 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
450 /* Tell caller that we did this for real, as opposed to being
451 * overridden by a callback
453 rc = SLAP_CB_CONTINUE;
456 if ( op->o_callback ) {
457 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
459 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
460 sc_next = op->o_callback->sc_next;
461 if ( op->o_callback->sc_cleanup ) {
462 (void)op->o_callback->sc_cleanup( op, rs );
463 if ( op->o_callback != *sc_prev ) {
464 *sc_prev = op->o_callback;
466 if ( !op->o_callback ) break;
468 sc_prev = &op->o_callback->sc_next;
474 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
475 free( (char *)rs->sr_matched );
476 rs->sr_matched = NULL;
479 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
480 ber_bvarray_free( rs->sr_ref );
489 send_ldap_disconnect( Operation *op, SlapReply *rs )
491 #define LDAP_UNSOLICITED_ERROR(e) \
492 ( (e) == LDAP_PROTOCOL_ERROR \
493 || (e) == LDAP_STRONG_AUTH_REQUIRED \
494 || (e) == LDAP_UNAVAILABLE )
496 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
498 rs->sr_type = REP_EXTENDED;
500 Debug( LDAP_DEBUG_TRACE,
501 "send_ldap_disconnect %d:%s\n",
502 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
504 if ( op->o_protocol < LDAP_VERSION3 ) {
505 rs->sr_rspoid = NULL;
506 rs->sr_tag = req2res( op->o_tag );
507 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
510 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
511 rs->sr_tag = LDAP_RES_EXTENDED;
515 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
516 Statslog( LDAP_DEBUG_STATS,
517 "%s DISCONNECT tag=%lu err=%d text=%s\n",
518 op->o_log_prefix, rs->sr_tag, rs->sr_err,
519 rs->sr_text ? rs->sr_text : "", 0 );
524 slap_send_ldap_result( Operation *op, SlapReply *rs )
527 const char *otext = rs->sr_text;
528 BerVarray oref = rs->sr_ref;
530 rs->sr_type = REP_RESULT;
532 /* Propagate Abandons so that cleanup callbacks can be processed */
533 if ( rs->sr_err == SLAPD_ABANDON )
536 assert( !LDAP_API_ERROR( rs->sr_err ) );
538 Debug( LDAP_DEBUG_TRACE,
539 "send_ldap_result: %s p=%d\n",
540 op->o_log_prefix, op->o_protocol, 0 );
542 Debug( LDAP_DEBUG_ARGS,
543 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
544 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
545 rs->sr_text ? rs->sr_text : "" );
549 Debug( LDAP_DEBUG_ARGS,
550 "send_ldap_result: referral=\"%s\"\n",
551 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
555 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
557 if ( rs->sr_err == LDAP_REFERRAL ) {
558 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
559 if( op->o_domain_scope ) {
563 if( rs->sr_ref == NULL ) {
564 rs->sr_err = LDAP_NO_SUCH_OBJECT;
565 } else if ( op->o_protocol < LDAP_VERSION3 ) {
566 rs->sr_err = LDAP_PARTIAL_RESULTS;
570 if ( op->o_protocol < LDAP_VERSION3 ) {
571 tmp = v2ref( rs->sr_ref, rs->sr_text );
576 rs->sr_tag = req2res( op->o_tag );
577 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
580 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
581 if ( op->o_tag == LDAP_REQ_SEARCH ) {
583 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
584 rs->sr_err, rs->sr_nentries );
586 Statslog( LDAP_DEBUG_STATS,
587 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
588 op->o_log_prefix, rs->sr_tag, nbuf,
589 rs->sr_text ? rs->sr_text : "", 0 );
591 Statslog( LDAP_DEBUG_STATS,
592 "%s RESULT tag=%lu err=%d text=%s\n",
593 op->o_log_prefix, rs->sr_tag, rs->sr_err,
594 rs->sr_text ? rs->sr_text : "", 0 );
598 if( tmp != NULL ) ch_free(tmp);
604 send_ldap_sasl( Operation *op, SlapReply *rs )
606 rs->sr_type = REP_SASL;
607 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
609 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
611 rs->sr_tag = req2res( op->o_tag );
612 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
614 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
615 Statslog( LDAP_DEBUG_STATS,
616 "%s RESULT tag=%lu err=%d text=%s\n",
617 op->o_log_prefix, rs->sr_tag, rs->sr_err,
618 rs->sr_text ? rs->sr_text : "", 0 );
623 slap_send_ldap_extended( Operation *op, SlapReply *rs )
625 rs->sr_type = REP_EXTENDED;
627 Debug( LDAP_DEBUG_TRACE,
628 "send_ldap_extended: err=%d oid=%s len=%ld\n",
630 rs->sr_rspoid ? rs->sr_rspoid : "",
631 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
633 rs->sr_tag = req2res( op->o_tag );
634 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
636 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
637 Statslog( LDAP_DEBUG_STATS,
638 "%s RESULT oid=%s err=%d text=%s\n",
639 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
640 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
645 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
647 rs->sr_type = REP_INTERMEDIATE;
648 Debug( LDAP_DEBUG_TRACE,
649 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
651 rs->sr_rspoid ? rs->sr_rspoid : "",
652 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
653 rs->sr_tag = LDAP_RES_INTERMEDIATE;
654 rs->sr_msgid = op->o_msgid;
655 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
656 Statslog( LDAP_DEBUG_STATS2,
657 "%s INTERM oid=%s\n",
659 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
664 slap_send_search_entry( Operation *op, SlapReply *rs )
666 BerElementBuffer berbuf;
667 BerElement *ber = (BerElement *) &berbuf;
669 int i, j, rc=-1, bytes;
672 AccessControlState acl_state = ACL_STATE_INIT;
674 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
676 /* a_flags: array of flags telling if the i-th element will be
677 * returned or filtered out
678 * e_flags: array of a_flags
680 char **e_flags = NULL;
682 rs->sr_type = REP_SEARCH;
684 /* eventually will loop through generated operational attribute types
685 * currently implemented types include:
686 * entryDN, subschemaSubentry, and hasSubordinates */
687 /* NOTE: moved before overlays callback circling because
688 * they may modify entry and other stuff in rs */
689 /* check for special all operational attributes ("+") type */
690 /* FIXME: maybe we could se this flag at the operation level;
691 * however, in principle the caller of send_search_entry() may
692 * change the attribute list at each call */
693 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
695 rc = backend_operational( op, rs );
700 if ( op->o_callback ) {
701 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
703 rc = SLAP_CB_CONTINUE;
704 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next )
706 sc_next = op->o_callback->sc_next;
707 if ( op->o_callback->sc_response ) {
708 rc = op->o_callback->sc_response( op, rs );
709 if ( op->o_callback != *sc_prev ) {
710 *sc_prev = op->o_callback;
712 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
714 sc_prev = &op->o_callback->sc_next;
718 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
721 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
722 op->o_connid, rs->sr_entry->e_name.bv_val,
723 op->ors_attrsonly ? " (attrsOnly)" : "" );
725 attrsonly = op->ors_attrsonly;
727 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
728 Debug( LDAP_DEBUG_ACL,
729 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
730 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
736 edn = rs->sr_entry->e_nname.bv_val;
738 if ( op->o_res_ber ) {
739 /* read back control or LDAP_CONNECTIONLESS */
744 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
745 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
747 ber_init2( ber, &bv, LBER_USE_DER );
748 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
751 #ifdef LDAP_CONNECTIONLESS
752 if ( op->o_conn && op->o_conn->c_is_udp ) {
754 if ( op->o_protocol == LDAP_VERSION2 ) {
755 rc = ber_printf(ber, "t{O{" /*}}*/,
756 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
758 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
759 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
763 if ( op->o_res_ber ) {
764 /* read back control */
765 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
767 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
768 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
772 Debug( LDAP_DEBUG_ANY,
773 "send_search_entry: conn %lu ber_printf failed\n",
774 op->o_connid, 0, 0 );
776 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
777 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
781 /* check for special all user attributes ("*") type */
782 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
784 /* create an array of arrays of flags. Each flag corresponds
785 * to particular value of attribute and equals 1 if value matches
786 * to ValuesReturnFilter or 0 if not
788 if ( op->o_vrFilter != NULL ) {
792 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
793 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
796 size = i * sizeof(char *) + k;
799 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
800 if( e_flags == NULL ) {
801 Debug( LDAP_DEBUG_ANY,
802 "send_search_entry: conn %lu slap_sl_calloc failed\n",
803 op->o_connid ? op->o_connid : 0, 0, 0 );
806 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
809 a_flags = (char *)(e_flags + i);
810 memset( a_flags, 0, k );
811 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
812 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
813 e_flags[i] = a_flags;
817 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
819 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
820 "conn %lu matched values filtering failed\n",
821 op->o_connid ? op->o_connid : 0, 0, 0 );
822 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
823 send_ldap_error( op, rs, LDAP_OTHER,
824 "matched values filtering error" );
830 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
831 AttributeDescription *desc = a->a_desc;
834 if ( rs->sr_attrs == NULL ) {
835 /* all user attrs request, skip operational attributes */
836 if( is_at_operational( desc->ad_type ) ) {
841 /* specific attrs requested */
842 if ( is_at_operational( desc->ad_type ) ) {
843 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
844 !ad_inlist( desc, rs->sr_attrs ) )
850 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
857 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
858 ACL_READ, &acl_state ) )
860 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
861 "conn %lu access to attribute %s not allowed\n",
862 op->o_connid, desc->ad_cname.bv_val, 0 );
866 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
867 Debug( LDAP_DEBUG_ANY,
868 "send_search_entry: conn %lu ber_printf failed\n",
869 op->o_connid, 0, 0 );
871 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
872 send_ldap_error( op, rs, LDAP_OTHER,
873 "encoding description error");
880 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
881 if ( ! access_allowed( op, rs->sr_entry,
882 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
884 Debug( LDAP_DEBUG_ACL,
885 "send_search_entry: conn %lu "
886 "access to attribute %s, value #%d not allowed\n",
887 op->o_connid, desc->ad_cname.bv_val, i );
892 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
899 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
900 Debug( LDAP_DEBUG_ANY,
901 "send_search_entry: conn %lu ber_printf failed\n",
902 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 description error");
910 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
911 Debug( LDAP_DEBUG_ANY,
912 "send_search_entry: conn %lu "
913 "ber_printf failed.\n", op->o_connid, 0, 0 );
915 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
916 send_ldap_error( op, rs, LDAP_OTHER,
917 "encoding values error" );
923 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
924 Debug( LDAP_DEBUG_ANY,
925 "send_search_entry: conn %lu ber_printf failed\n",
926 op->o_connid, 0, 0 );
928 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
929 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
934 /* NOTE: moved before overlays callback circling because
935 * they may modify entry and other stuff in rs */
936 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
940 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
941 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
944 size = i * sizeof(char *) + k;
946 char *a_flags, **tmp;
949 * Reuse previous memory - we likely need less space
950 * for operational attributes
952 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
955 Debug( LDAP_DEBUG_ANY,
956 "send_search_entry: conn %lu "
958 "for matched values filtering\n",
959 op->o_connid, 0, 0 );
960 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
961 send_ldap_error( op, rs, LDAP_OTHER,
962 "not enough memory for matched values filtering" );
966 a_flags = (char *)(e_flags + i);
967 memset( a_flags, 0, k );
968 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
969 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
970 e_flags[i] = a_flags;
973 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
976 Debug( LDAP_DEBUG_ANY,
977 "send_search_entry: conn %lu "
978 "matched values filtering failed\n",
979 op->o_connid ? op->o_connid : 0, 0, 0);
980 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
981 send_ldap_error( op, rs, LDAP_OTHER,
982 "matched values filtering error" );
988 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
989 AttributeDescription *desc = a->a_desc;
991 if ( rs->sr_attrs == NULL ) {
992 /* all user attrs request, skip operational attributes */
993 if( is_at_operational( desc->ad_type ) ) {
998 /* specific attrs requested */
999 if( is_at_operational( desc->ad_type ) ) {
1000 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1001 !ad_inlist( desc, rs->sr_attrs ) )
1006 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1012 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1013 ACL_READ, &acl_state ) )
1015 Debug( LDAP_DEBUG_ACL,
1016 "send_search_entry: conn %lu "
1017 "access to attribute %s not allowed\n",
1018 op->o_connid, desc->ad_cname.bv_val, 0 );
1023 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1025 Debug( LDAP_DEBUG_ANY,
1026 "send_search_entry: conn %lu "
1027 "ber_printf failed\n", op->o_connid, 0, 0 );
1029 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1030 send_ldap_error( op, rs, LDAP_OTHER,
1031 "encoding description error" );
1035 if ( ! attrsonly ) {
1036 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1037 if ( ! access_allowed( op, rs->sr_entry,
1038 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1040 Debug( LDAP_DEBUG_ACL,
1041 "send_search_entry: conn %lu "
1042 "access to %s, value %d not allowed\n",
1043 op->o_connid, desc->ad_cname.bv_val, i );
1048 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1052 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1053 Debug( LDAP_DEBUG_ANY,
1054 "send_search_entry: conn %lu ber_printf failed\n",
1055 op->o_connid, 0, 0 );
1057 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1058 send_ldap_error( op, rs, LDAP_OTHER,
1059 "encoding values error" );
1065 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1066 Debug( LDAP_DEBUG_ANY,
1067 "send_search_entry: conn %lu ber_printf failed\n",
1068 op->o_connid, 0, 0 );
1070 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1071 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1078 slap_sl_free( e_flags, op->o_tmpmemctx );
1082 rc = ber_printf( ber, /*{{*/ "}N}" );
1085 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1089 #ifdef LDAP_CONNECTIONLESS
1090 if( op->o_conn && op->o_conn->c_is_udp ) {
1091 if ( op->o_protocol != LDAP_VERSION2 ) {
1092 rc = ber_printf( ber, /*{*/ "N}" );
1096 if ( op->o_res_ber == NULL ) {
1097 rc = ber_printf( ber, /*{*/ "N}" );
1102 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1104 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1105 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1110 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1111 be_entry_release_rw( op, rs->sr_entry, 0 );
1112 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1113 rs->sr_entry = NULL;
1116 if ( op->o_res_ber == NULL ) {
1117 bytes = send_ldap_ber( op->o_conn, ber );
1118 ber_free_buf( ber );
1121 Debug( LDAP_DEBUG_ANY,
1122 "send_search_entry: conn %lu ber write failed.\n",
1123 op->o_connid, 0, 0 );
1130 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1131 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1132 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1133 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1134 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1137 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1138 op->o_log_prefix, edn, 0, 0, 0 );
1140 Debug( LDAP_DEBUG_TRACE,
1141 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1146 if ( op->o_callback ) {
1147 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1149 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1150 sc_next = op->o_callback->sc_next;
1151 if ( op->o_callback->sc_cleanup ) {
1152 (void)op->o_callback->sc_cleanup( op, rs );
1153 if ( op->o_callback != *sc_prev ) {
1154 *sc_prev = op->o_callback;
1156 if ( !op->o_callback ) break;
1158 sc_prev = &op->o_callback->sc_next;
1160 op->o_callback = sc;
1164 slap_sl_free( e_flags, op->o_tmpmemctx );
1167 if ( rs->sr_operational_attrs ) {
1168 attrs_free( rs->sr_operational_attrs );
1169 rs->sr_operational_attrs = NULL;
1171 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1173 /* FIXME: I think rs->sr_type should be explicitly set to
1174 * REP_SEARCH here. That's what it was when we entered this
1175 * function. send_ldap_error may have changed it, but we
1176 * should set it back so that the cleanup functions know
1177 * what they're doing.
1179 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1181 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1183 entry_free( rs->sr_entry );
1184 rs->sr_entry = NULL;
1185 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1192 slap_send_search_reference( Operation *op, SlapReply *rs )
1194 BerElementBuffer berbuf;
1195 BerElement *ber = (BerElement *) &berbuf;
1199 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1200 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1202 rs->sr_type = REP_SEARCHREF;
1203 if ( op->o_callback ) {
1204 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1206 rc = SLAP_CB_CONTINUE;
1207 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1208 sc_next = op->o_callback->sc_next;
1209 if ( op->o_callback->sc_response ) {
1210 rc = op->o_callback->sc_response( op, rs );
1211 if ( op->o_callback != *sc_prev ) {
1212 *sc_prev = op->o_callback;
1214 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1216 sc_prev = &op->o_callback->sc_next;
1219 op->o_callback = sc;
1220 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1223 Debug( LDAP_DEBUG_TRACE,
1224 "=> send_search_reference: dn=\"%s\"\n",
1225 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1227 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1228 ad_entry, NULL, ACL_READ, NULL ) )
1230 Debug( LDAP_DEBUG_ACL,
1231 "send_search_reference: access to entry not allowed\n",
1237 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1238 ad_ref, NULL, ACL_READ, NULL ) )
1240 Debug( LDAP_DEBUG_ACL,
1241 "send_search_reference: access "
1242 "to reference not allowed\n",
1248 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1249 if( op->o_domain_scope ) {
1250 Debug( LDAP_DEBUG_ANY,
1251 "send_search_reference: domainScope control in (%s)\n",
1252 rs->sr_entry->e_dn, 0, 0 );
1258 if( rs->sr_ref == NULL ) {
1259 Debug( LDAP_DEBUG_ANY,
1260 "send_search_reference: null ref in (%s)\n",
1261 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1266 if( op->o_protocol < LDAP_VERSION3 ) {
1268 /* save the references for the result */
1269 if( rs->sr_ref[0].bv_val != NULL ) {
1270 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1276 #ifdef LDAP_CONNECTIONLESS
1277 if( op->o_conn && op->o_conn->c_is_udp ) {
1278 ber = op->o_res_ber;
1282 ber_init_w_nullc( ber, LBER_USE_DER );
1283 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1286 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1287 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1290 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1294 rc = ber_printf( ber, /*"{"*/ "N}" );
1298 Debug( LDAP_DEBUG_ANY,
1299 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1301 #ifdef LDAP_CONNECTIONLESS
1302 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1304 ber_free_buf( ber );
1305 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1310 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1311 be_entry_release_rw( op, rs->sr_entry, 0 );
1312 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1313 rs->sr_entry = NULL;
1316 #ifdef LDAP_CONNECTIONLESS
1317 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1319 bytes = send_ldap_ber( op->o_conn, ber );
1320 ber_free_buf( ber );
1322 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1323 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1324 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1325 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1326 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1327 #ifdef LDAP_CONNECTIONLESS
1330 if ( rs->sr_ref != NULL ) {
1333 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1334 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1335 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1340 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1341 op->o_log_prefix, 0, 0, 0, 0 );
1344 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1347 if ( op->o_callback ) {
1348 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1350 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1351 sc_next = op->o_callback->sc_next;
1352 if ( op->o_callback->sc_cleanup ) {
1353 (void)op->o_callback->sc_cleanup( op, rs );
1354 if ( op->o_callback != *sc_prev ) {
1355 *sc_prev = op->o_callback;
1357 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1359 sc_prev = &op->o_callback->sc_next;
1361 op->o_callback = sc;
1377 *code = LDAP_SUCCESS;
1381 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1382 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1389 while ( (s = strchr( s, '\n' )) != NULL ) {
1394 if ( (c = strchr( s, ':' )) != NULL ) {
1398 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1402 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1406 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1411 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1421 int slap_read_controls(
1425 const struct berval *oid,
1426 LDAPControl **ctrl )
1430 BerElementBuffer berbuf;
1431 BerElement *ber = (BerElement *) &berbuf;
1435 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1436 oid->bv_val, e->e_dn, 0 );
1439 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1440 op->o_preread_attrs : op->o_postread_attrs;
1442 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1443 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1445 ber_init2( ber, &bv, LBER_USE_DER );
1446 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1448 /* create new operation */
1451 myop.o_res_ber = ber;
1452 myop.o_callback = NULL;
1454 rc = slap_send_search_entry( &myop, rs );
1457 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1459 if( rc == LBER_ERROR ) return LDAP_OTHER;
1461 c.ldctl_oid = oid->bv_val;
1462 c.ldctl_iscritical = 0;
1464 if ( *ctrl == NULL ) {
1466 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1468 /* retry: free previous try */
1469 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1473 return LDAP_SUCCESS;
1476 /* Map API errors to protocol errors... */
1478 slap_map_api2result( SlapReply *rs )
1480 switch(rs->sr_err) {
1481 case LDAP_SERVER_DOWN:
1482 return LDAP_UNAVAILABLE;
1483 case LDAP_LOCAL_ERROR:
1485 case LDAP_ENCODING_ERROR:
1486 case LDAP_DECODING_ERROR:
1487 return LDAP_PROTOCOL_ERROR;
1489 return LDAP_UNAVAILABLE;
1490 case LDAP_AUTH_UNKNOWN:
1491 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1492 case LDAP_FILTER_ERROR:
1493 rs->sr_text = "Filter error";
1495 case LDAP_USER_CANCELLED:
1496 rs->sr_text = "User cancelled";
1498 case LDAP_PARAM_ERROR:
1499 return LDAP_PROTOCOL_ERROR;
1500 case LDAP_NO_MEMORY:
1502 case LDAP_CONNECT_ERROR:
1503 return LDAP_UNAVAILABLE;
1504 case LDAP_NOT_SUPPORTED:
1505 return LDAP_UNWILLING_TO_PERFORM;
1506 case LDAP_CONTROL_NOT_FOUND:
1507 return LDAP_PROTOCOL_ERROR;
1508 case LDAP_NO_RESULTS_RETURNED:
1509 return LDAP_NO_SUCH_OBJECT;
1510 case LDAP_MORE_RESULTS_TO_RETURN:
1511 rs->sr_text = "More results to return";
1513 case LDAP_CLIENT_LOOP:
1514 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1515 return LDAP_LOOP_DETECT;
1517 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1524 slap_attr_flags( AttributeName *an )
1526 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1529 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1532 flags |= an_find( an, &AllOper )
1533 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1534 flags |= an_find( an, &AllUser )
1535 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;