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-2006 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 const struct berval slap_dummy_bv = BER_BVNULL;
43 int slap_null_cb( Operation *op, SlapReply *rs )
48 int slap_freeself_cb( Operation *op, SlapReply *rs )
50 assert( op->o_callback != NULL );
52 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
53 op->o_callback = NULL;
55 return SLAP_CB_CONTINUE;
58 int slap_replog_cb( Operation *op, SlapReply *rs )
60 if ( rs->sr_err == LDAP_SUCCESS ) {
63 return SLAP_CB_CONTINUE;
66 static char *v2ref( BerVarray ref, const char *text )
68 size_t len = 0, i = 0;
73 return ch_strdup(text);
81 if (text[len-1] != '\n') {
86 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
88 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
98 strcpy( v2+len, "Referral:" );
99 len += sizeof("Referral:");
101 for( i=0; ref[i].bv_val != NULL; i++ ) {
102 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
104 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
108 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
109 len += ref[i].bv_len;
110 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
120 slap_req2res( ber_tag_t tag )
125 case LDAP_REQ_COMPARE:
126 case LDAP_REQ_EXTENDED:
127 case LDAP_REQ_MODIFY:
128 case LDAP_REQ_MODRDN:
132 case LDAP_REQ_DELETE:
133 tag = LDAP_RES_DELETE;
136 case LDAP_REQ_ABANDON:
137 case LDAP_REQ_UNBIND:
141 case LDAP_REQ_SEARCH:
142 tag = LDAP_RES_SEARCH_RESULT;
152 static long send_ldap_ber(
158 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
160 /* write only one pdu at a time - wait til it's our turn */
161 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
163 /* lock the connection */
164 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
171 if ( connection_state_closing( conn ) ) {
172 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
173 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
178 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
185 * we got an error. if it's ewouldblock, we need to
186 * wait on the socket being writable. otherwise, figure
187 * it's a hard error and return.
190 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
191 err, sock_errstr(err), 0 );
193 if ( err != EWOULDBLOCK && err != EAGAIN ) {
194 connection_closing( conn, "connection lost on write" );
196 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
197 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
202 /* wait for socket to be write-ready */
203 conn->c_writewaiter = 1;
204 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
205 slapd_set_write( sd, 1 );
207 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
208 conn->c_writewaiter = 0;
211 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
212 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
218 send_ldap_control( BerElement *ber, LDAPControl *c )
224 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
226 if( c->ldctl_iscritical ) {
227 rc = ber_printf( ber, "b",
228 (ber_int_t) c->ldctl_iscritical ) ;
229 if( rc == -1 ) return rc;
232 if( c->ldctl_value.bv_val != NULL ) {
233 rc = ber_printf( ber, "O", &c->ldctl_value );
234 if( rc == -1 ) return rc;
237 rc = ber_printf( ber, /*{*/"N}" );
238 if( rc == -1 ) return rc;
244 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
251 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
252 if( rc == -1 ) return rc;
254 for( ; *c != NULL; c++) {
255 rc = send_ldap_control( ber, *c );
256 if( rc == -1 ) return rc;
260 /* this is a hack to avoid having to modify op->s_ctrls */
261 if( o->o_sortedresults ) {
262 BerElementBuffer berbuf;
263 BerElement *sber = (BerElement *) &berbuf;
265 BER_BVZERO( &sorted.ldctl_value );
266 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
267 sorted.ldctl_iscritical = 0;
269 ber_init2( sber, NULL, LBER_USE_DER );
271 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
273 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
277 (void) ber_free_buf( ber );
279 rc = send_ldap_control( ber, &sorted );
280 if( rc == -1 ) return rc;
284 rc = ber_printf( ber, /*{*/"N}" );
290 * slap_response_play()
292 * plays the callback list; rationale: a callback can
293 * - remove itself from the list, by setting op->o_callback = NULL;
294 * malloc()'ed callbacks should free themselves from inside the
295 * sc_response() function.
296 * - replace itself with another (list of) callback(s), by setting
297 * op->o_callback = a new (list of) callback(s); in this case, it
298 * is the callback's responsibility to to append existing subsequent
299 * callbacks to the end of the list that is passed to the sc_response()
301 * - modify the list of subsequent callbacks by modifying the value
302 * of the sc_next field from inside the sc_response() function; this
303 * case does not require any handling from inside slap_response_play()
305 * To stop execution of the playlist, the sc_response() function must return
306 * a value different from SLAP_SC_CONTINUE.
308 * The same applies to slap_cleanup_play(); only, there is no means to stop
309 * execution of the playlist, since all cleanup functions must be called.
319 slap_callback *sc = op->o_callback, **scp;
321 rc = SLAP_CB_CONTINUE;
322 for ( scp = ≻ *scp; ) {
323 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
325 op->o_callback = *scp;
326 if ( op->o_callback->sc_response ) {
327 rc = op->o_callback->sc_response( op, rs );
328 if ( op->o_callback == NULL ) {
329 /* the callback has been removed;
334 } else if ( op->o_callback != *scp ) {
335 /* a new callback has been inserted
336 * in place of the existing one; repair the list */
337 *scp = op->o_callback;
340 if ( rc != SLAP_CB_CONTINUE ) break;
346 #else /* ! LDAP_DEVEL */
347 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
349 rc = SLAP_CB_CONTINUE;
350 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
351 sc_next = op->o_callback->sc_next;
352 if ( op->o_callback->sc_response ) {
353 slap_callback *sc2 = op->o_callback;
354 rc = op->o_callback->sc_response( op, rs );
355 if ( op->o_callback != sc2 ) {
356 *sc_prev = op->o_callback;
358 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
359 if ( op->o_callback != sc2 ) continue;
361 sc_prev = &op->o_callback->sc_next;
365 #endif /* ! LDAP_DEVEL */
376 slap_callback *sc = op->o_callback, **scp;
378 for ( scp = ≻ *scp; ) {
379 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
381 op->o_callback = *scp;
382 if ( op->o_callback->sc_cleanup ) {
383 (void)op->o_callback->sc_cleanup( op, rs );
384 if ( op->o_callback == NULL ) {
385 /* the callback has been removed;
390 } else if ( op->o_callback != *scp ) {
391 /* a new callback has been inserted
392 * after the existing one; repair the list */
393 /* a new callback has been inserted
394 * in place of the existing one; repair the list */
395 *scp = op->o_callback;
398 /* don't care about the result; do all cleanup */
404 #else /* ! LDAP_DEVEL */
405 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
407 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
408 sc_next = op->o_callback->sc_next;
409 if ( op->o_callback->sc_cleanup ) {
410 slap_callback *sc2 = op->o_callback;
411 (void)op->o_callback->sc_cleanup( op, rs );
412 if ( op->o_callback != sc2 ) {
413 *sc_prev = op->o_callback;
415 if ( !op->o_callback ) break;
416 if ( op->o_callback != sc2 ) continue;
418 sc_prev = &op->o_callback->sc_next;
422 #endif /* ! LDAP_DEVEL */
432 BerElementBuffer berbuf;
433 BerElement *ber = (BerElement *) &berbuf;
434 int rc = LDAP_SUCCESS;
437 if ( rs->sr_err == SLAPD_ABANDON ) {
442 if ( op->o_callback ) {
443 rc = slap_response_play( op, rs );
444 if ( rc != SLAP_CB_CONTINUE ) {
449 #ifdef LDAP_CONNECTIONLESS
450 if (op->o_conn && op->o_conn->c_is_udp)
455 ber_init_w_nullc( ber, LBER_USE_DER );
456 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
459 Debug( LDAP_DEBUG_TRACE,
460 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
461 rs->sr_msgid, rs->sr_tag, rs->sr_err );
464 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
465 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
469 #ifdef LDAP_CONNECTIONLESS
470 if (op->o_conn && op->o_conn->c_is_udp &&
471 op->o_protocol == LDAP_VERSION2 )
473 rc = ber_printf( ber, "t{ess" /*"}"*/,
474 rs->sr_tag, rs->sr_err,
475 rs->sr_matched == NULL ? "" : rs->sr_matched,
476 rs->sr_text == NULL ? "" : rs->sr_text );
479 if ( rs->sr_type == REP_INTERMEDIATE ) {
480 rc = ber_printf( ber, "{it{" /*"}}"*/,
481 rs->sr_msgid, rs->sr_tag );
484 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
485 rs->sr_msgid, rs->sr_tag, rs->sr_err,
486 rs->sr_matched == NULL ? "" : rs->sr_matched,
487 rs->sr_text == NULL ? "" : rs->sr_text );
491 if ( rs->sr_ref != NULL ) {
492 assert( rs->sr_err == LDAP_REFERRAL );
493 rc = ber_printf( ber, "t{W}",
494 LDAP_TAG_REFERRAL, rs->sr_ref );
496 assert( rs->sr_err != LDAP_REFERRAL );
500 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
501 rc = ber_printf( ber, "tO",
502 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
506 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
508 if ( rs->sr_rspoid != NULL ) {
509 rc = ber_printf( ber, "ts",
510 rs->sr_type == REP_EXTENDED
511 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
514 if( rc != -1 && rs->sr_rspdata != NULL ) {
515 rc = ber_printf( ber, "tO",
516 rs->sr_type == REP_EXTENDED
517 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
523 rc = ber_printf( ber, /*"{"*/ "N}" );
527 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
531 rc = ber_printf( ber, /*"{"*/ "N}" );
534 #ifdef LDAP_CONNECTIONLESS
535 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
538 rc = ber_printf( ber, /*"{"*/ "N}" );
543 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
545 #ifdef LDAP_CONNECTIONLESS
546 if (!op->o_conn || op->o_conn->c_is_udp == 0)
555 bytes = send_ldap_ber( op->o_conn, ber );
556 #ifdef LDAP_CONNECTIONLESS
557 if (!op->o_conn || op->o_conn->c_is_udp == 0)
564 Debug( LDAP_DEBUG_ANY,
565 "send_ldap_response: ber write failed\n",
571 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
572 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
573 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
574 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
577 /* Tell caller that we did this for real, as opposed to being
578 * overridden by a callback
580 rc = SLAP_CB_CONTINUE;
583 if ( op->o_callback ) {
584 (void)slap_cleanup_play( op, rs );
587 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
588 free( (char *)rs->sr_matched );
589 rs->sr_matched = NULL;
592 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
593 ber_bvarray_free( rs->sr_ref );
602 send_ldap_disconnect( Operation *op, SlapReply *rs )
604 #define LDAP_UNSOLICITED_ERROR(e) \
605 ( (e) == LDAP_PROTOCOL_ERROR \
606 || (e) == LDAP_STRONG_AUTH_REQUIRED \
607 || (e) == LDAP_UNAVAILABLE )
609 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
611 rs->sr_type = REP_EXTENDED;
613 Debug( LDAP_DEBUG_TRACE,
614 "send_ldap_disconnect %d:%s\n",
615 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
617 if ( op->o_protocol < LDAP_VERSION3 ) {
618 rs->sr_rspoid = NULL;
619 rs->sr_tag = slap_req2res( op->o_tag );
620 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
623 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
624 rs->sr_tag = LDAP_RES_EXTENDED;
628 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
629 Statslog( LDAP_DEBUG_STATS,
630 "%s DISCONNECT tag=%lu err=%d text=%s\n",
631 op->o_log_prefix, rs->sr_tag, rs->sr_err,
632 rs->sr_text ? rs->sr_text : "", 0 );
637 slap_send_ldap_result( Operation *op, SlapReply *rs )
640 const char *otext = rs->sr_text;
641 BerVarray oref = rs->sr_ref;
643 rs->sr_type = REP_RESULT;
645 /* Propagate Abandons so that cleanup callbacks can be processed */
646 if ( rs->sr_err == SLAPD_ABANDON )
649 assert( !LDAP_API_ERROR( rs->sr_err ) );
651 Debug( LDAP_DEBUG_TRACE,
652 "send_ldap_result: %s p=%d\n",
653 op->o_log_prefix, op->o_protocol, 0 );
655 Debug( LDAP_DEBUG_ARGS,
656 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
657 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
658 rs->sr_text ? rs->sr_text : "" );
662 Debug( LDAP_DEBUG_ARGS,
663 "send_ldap_result: referral=\"%s\"\n",
664 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
668 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
670 if ( rs->sr_err == LDAP_REFERRAL ) {
671 if( op->o_domain_scope ) rs->sr_ref = NULL;
673 if( rs->sr_ref == NULL ) {
674 rs->sr_err = LDAP_NO_SUCH_OBJECT;
675 } else if ( op->o_protocol < LDAP_VERSION3 ) {
676 rs->sr_err = LDAP_PARTIAL_RESULTS;
680 if ( op->o_protocol < LDAP_VERSION3 ) {
681 tmp = v2ref( rs->sr_ref, rs->sr_text );
686 rs->sr_tag = slap_req2res( op->o_tag );
687 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
690 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
691 if ( op->o_tag == LDAP_REQ_SEARCH ) {
693 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
694 rs->sr_err, rs->sr_nentries );
696 Statslog( LDAP_DEBUG_STATS,
697 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
698 op->o_log_prefix, rs->sr_tag, nbuf,
699 rs->sr_text ? rs->sr_text : "", 0 );
701 Statslog( LDAP_DEBUG_STATS,
702 "%s RESULT tag=%lu err=%d text=%s\n",
703 op->o_log_prefix, rs->sr_tag, rs->sr_err,
704 rs->sr_text ? rs->sr_text : "", 0 );
708 if( tmp != NULL ) ch_free(tmp);
714 send_ldap_sasl( Operation *op, SlapReply *rs )
716 rs->sr_type = REP_SASL;
717 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
719 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
721 rs->sr_tag = slap_req2res( op->o_tag );
722 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
724 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
725 Statslog( LDAP_DEBUG_STATS,
726 "%s RESULT tag=%lu err=%d text=%s\n",
727 op->o_log_prefix, rs->sr_tag, rs->sr_err,
728 rs->sr_text ? rs->sr_text : "", 0 );
733 slap_send_ldap_extended( Operation *op, SlapReply *rs )
735 rs->sr_type = REP_EXTENDED;
737 Debug( LDAP_DEBUG_TRACE,
738 "send_ldap_extended: err=%d oid=%s len=%ld\n",
740 rs->sr_rspoid ? rs->sr_rspoid : "",
741 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
743 rs->sr_tag = slap_req2res( op->o_tag );
744 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
746 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
747 Statslog( LDAP_DEBUG_STATS,
748 "%s RESULT oid=%s err=%d text=%s\n",
749 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
750 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
755 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
757 rs->sr_type = REP_INTERMEDIATE;
758 Debug( LDAP_DEBUG_TRACE,
759 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
761 rs->sr_rspoid ? rs->sr_rspoid : "",
762 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
763 rs->sr_tag = LDAP_RES_INTERMEDIATE;
764 rs->sr_msgid = op->o_msgid;
765 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
766 Statslog( LDAP_DEBUG_STATS2,
767 "%s INTERM oid=%s\n",
769 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
776 * LDAP_SUCCESS entry sent
777 * LDAP_OTHER entry not sent (other)
778 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
779 * LDAP_UNAVAILABLE entry not sent (connection closed)
780 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
784 slap_send_search_entry( Operation *op, SlapReply *rs )
786 BerElementBuffer berbuf;
787 BerElement *ber = (BerElement *) &berbuf;
789 int i, j, rc = LDAP_UNAVAILABLE, bytes;
792 AccessControlState acl_state = ACL_STATE_INIT;
794 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
796 /* a_flags: array of flags telling if the i-th element will be
797 * returned or filtered out
798 * e_flags: array of a_flags
800 char **e_flags = NULL;
802 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
803 return LDAP_SIZELIMIT_EXCEEDED;
806 rs->sr_type = REP_SEARCH;
808 /* eventually will loop through generated operational attribute types
809 * currently implemented types include:
810 * entryDN, subschemaSubentry, and hasSubordinates */
811 /* NOTE: moved before overlays callback circling because
812 * they may modify entry and other stuff in rs */
813 /* check for special all operational attributes ("+") type */
814 /* FIXME: maybe we could set this flag at the operation level;
815 * however, in principle the caller of send_search_entry() may
816 * change the attribute list at each call */
817 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
819 rc = backend_operational( op, rs );
824 if ( op->o_callback ) {
825 rc = slap_response_play( op, rs );
826 if ( rc != SLAP_CB_CONTINUE ) {
831 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
832 op->o_connid, rs->sr_entry->e_name.bv_val,
833 op->ors_attrsonly ? " (attrsOnly)" : "" );
835 attrsonly = op->ors_attrsonly;
837 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
838 Debug( LDAP_DEBUG_ACL,
839 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
840 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
842 rc = LDAP_INSUFFICIENT_ACCESS;
846 edn = rs->sr_entry->e_nname.bv_val;
848 if ( op->o_res_ber ) {
849 /* read back control or LDAP_CONNECTIONLESS */
854 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
855 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
857 ber_init2( ber, &bv, LBER_USE_DER );
858 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
861 #ifdef LDAP_CONNECTIONLESS
862 if ( op->o_conn && op->o_conn->c_is_udp ) {
864 if ( op->o_protocol == LDAP_VERSION2 ) {
865 rc = ber_printf(ber, "t{O{" /*}}*/,
866 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
868 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
869 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
873 if ( op->o_res_ber ) {
874 /* read back control */
875 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
877 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
878 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
882 Debug( LDAP_DEBUG_ANY,
883 "send_search_entry: conn %lu ber_printf failed\n",
884 op->o_connid, 0, 0 );
886 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
887 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
892 /* check for special all user attributes ("*") type */
893 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
895 /* create an array of arrays of flags. Each flag corresponds
896 * to particular value of attribute and equals 1 if value matches
897 * to ValuesReturnFilter or 0 if not
899 if ( op->o_vrFilter != NULL ) {
903 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
904 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
907 size = i * sizeof(char *) + k;
910 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
911 if( e_flags == NULL ) {
912 Debug( LDAP_DEBUG_ANY,
913 "send_search_entry: conn %lu slap_sl_calloc failed\n",
914 op->o_connid ? op->o_connid : 0, 0, 0 );
917 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
920 a_flags = (char *)(e_flags + i);
921 memset( a_flags, 0, k );
922 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
923 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
924 e_flags[i] = a_flags;
928 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
930 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
931 "conn %lu matched values filtering failed\n",
932 op->o_connid ? op->o_connid : 0, 0, 0 );
933 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
934 send_ldap_error( op, rs, LDAP_OTHER,
935 "matched values filtering error" );
942 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
943 AttributeDescription *desc = a->a_desc;
946 if ( rs->sr_attrs == NULL ) {
947 /* all user attrs request, skip operational attributes */
948 if( is_at_operational( desc->ad_type ) ) {
953 /* specific attrs requested */
954 if ( is_at_operational( desc->ad_type ) ) {
955 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
956 !ad_inlist( desc, rs->sr_attrs ) )
962 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
969 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
970 ACL_READ, &acl_state ) )
972 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
973 "conn %lu access to attribute %s not allowed\n",
974 op->o_connid, desc->ad_cname.bv_val, 0 );
978 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
979 Debug( LDAP_DEBUG_ANY,
980 "send_search_entry: conn %lu ber_printf failed\n",
981 op->o_connid, 0, 0 );
983 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
984 send_ldap_error( op, rs, LDAP_OTHER,
985 "encoding description error");
993 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
994 if ( ! access_allowed( op, rs->sr_entry,
995 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
997 Debug( LDAP_DEBUG_ACL,
998 "send_search_entry: conn %lu "
999 "access to attribute %s, value #%d not allowed\n",
1000 op->o_connid, desc->ad_cname.bv_val, i );
1005 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1012 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1013 Debug( LDAP_DEBUG_ANY,
1014 "send_search_entry: conn %lu ber_printf failed\n",
1015 op->o_connid, 0, 0 );
1017 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1018 send_ldap_error( op, rs, LDAP_OTHER,
1019 "encoding description error");
1024 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
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 values error" );
1038 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1039 Debug( LDAP_DEBUG_ANY,
1040 "send_search_entry: conn %lu ber_printf failed\n",
1041 op->o_connid, 0, 0 );
1043 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1044 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1050 /* NOTE: moved before overlays callback circling because
1051 * they may modify entry and other stuff in rs */
1052 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1056 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1057 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1060 size = i * sizeof(char *) + k;
1062 char *a_flags, **tmp;
1065 * Reuse previous memory - we likely need less space
1066 * for operational attributes
1068 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1070 if ( tmp == NULL ) {
1071 Debug( LDAP_DEBUG_ANY,
1072 "send_search_entry: conn %lu "
1073 "not enough memory "
1074 "for matched values filtering\n",
1075 op->o_connid, 0, 0 );
1076 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1077 send_ldap_error( op, rs, LDAP_OTHER,
1078 "not enough memory for matched values filtering" );
1082 a_flags = (char *)(e_flags + i);
1083 memset( a_flags, 0, k );
1084 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1085 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1086 e_flags[i] = a_flags;
1089 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1092 Debug( LDAP_DEBUG_ANY,
1093 "send_search_entry: conn %lu "
1094 "matched values filtering failed\n",
1095 op->o_connid ? op->o_connid : 0, 0, 0);
1096 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1097 send_ldap_error( op, rs, LDAP_OTHER,
1098 "matched values filtering error" );
1105 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1106 AttributeDescription *desc = a->a_desc;
1108 if ( rs->sr_attrs == NULL ) {
1109 /* all user attrs request, skip operational attributes */
1110 if( is_at_operational( desc->ad_type ) ) {
1115 /* specific attrs requested */
1116 if( is_at_operational( desc->ad_type ) ) {
1117 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1118 !ad_inlist( desc, rs->sr_attrs ) )
1123 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1129 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1130 ACL_READ, &acl_state ) )
1132 Debug( LDAP_DEBUG_ACL,
1133 "send_search_entry: conn %lu "
1134 "access to attribute %s not allowed\n",
1135 op->o_connid, desc->ad_cname.bv_val, 0 );
1140 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1142 Debug( LDAP_DEBUG_ANY,
1143 "send_search_entry: conn %lu "
1144 "ber_printf failed\n", op->o_connid, 0, 0 );
1146 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1147 send_ldap_error( op, rs, LDAP_OTHER,
1148 "encoding description error" );
1153 if ( ! attrsonly ) {
1154 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1155 if ( ! access_allowed( op, rs->sr_entry,
1156 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1158 Debug( LDAP_DEBUG_ACL,
1159 "send_search_entry: conn %lu "
1160 "access to %s, value %d not allowed\n",
1161 op->o_connid, desc->ad_cname.bv_val, i );
1166 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1170 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1171 Debug( LDAP_DEBUG_ANY,
1172 "send_search_entry: conn %lu ber_printf failed\n",
1173 op->o_connid, 0, 0 );
1175 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1176 send_ldap_error( op, rs, LDAP_OTHER,
1177 "encoding values error" );
1184 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1185 Debug( LDAP_DEBUG_ANY,
1186 "send_search_entry: conn %lu ber_printf failed\n",
1187 op->o_connid, 0, 0 );
1189 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1190 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1198 slap_sl_free( e_flags, op->o_tmpmemctx );
1202 rc = ber_printf( ber, /*{{*/ "}N}" );
1205 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1209 #ifdef LDAP_CONNECTIONLESS
1210 if( op->o_conn && op->o_conn->c_is_udp ) {
1211 if ( op->o_protocol != LDAP_VERSION2 ) {
1212 rc = ber_printf( ber, /*{*/ "N}" );
1216 if ( op->o_res_ber == NULL ) {
1217 rc = ber_printf( ber, /*{*/ "N}" );
1222 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1224 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1225 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1230 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1231 be_entry_release_rw( op, rs->sr_entry, 0 );
1232 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1233 rs->sr_entry = NULL;
1236 if ( op->o_res_ber == NULL ) {
1237 bytes = send_ldap_ber( op->o_conn, ber );
1238 ber_free_buf( ber );
1241 Debug( LDAP_DEBUG_ANY,
1242 "send_search_entry: conn %lu ber write failed.\n",
1243 op->o_connid, 0, 0 );
1245 rc = LDAP_UNAVAILABLE;
1250 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1251 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1252 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1253 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1254 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1257 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1258 op->o_log_prefix, edn, 0, 0, 0 );
1260 Debug( LDAP_DEBUG_TRACE,
1261 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1266 if ( op->o_callback ) {
1267 (void)slap_cleanup_play( op, rs );
1271 slap_sl_free( e_flags, op->o_tmpmemctx );
1274 if ( rs->sr_operational_attrs ) {
1275 attrs_free( rs->sr_operational_attrs );
1276 rs->sr_operational_attrs = NULL;
1278 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1280 /* FIXME: I think rs->sr_type should be explicitly set to
1281 * REP_SEARCH here. That's what it was when we entered this
1282 * function. send_ldap_error may have changed it, but we
1283 * should set it back so that the cleanup functions know
1284 * what they're doing.
1286 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1288 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1290 entry_free( rs->sr_entry );
1291 rs->sr_entry = NULL;
1292 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1299 slap_send_search_reference( Operation *op, SlapReply *rs )
1301 BerElementBuffer berbuf;
1302 BerElement *ber = (BerElement *) &berbuf;
1306 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1307 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1309 rs->sr_type = REP_SEARCHREF;
1310 if ( op->o_callback ) {
1311 rc = slap_response_play( op, rs );
1312 if ( rc != SLAP_CB_CONTINUE ) {
1317 Debug( LDAP_DEBUG_TRACE,
1318 "=> send_search_reference: dn=\"%s\"\n",
1319 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1321 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1322 ad_entry, NULL, ACL_READ, NULL ) )
1324 Debug( LDAP_DEBUG_ACL,
1325 "send_search_reference: access to entry not allowed\n",
1331 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1332 ad_ref, NULL, ACL_READ, NULL ) )
1334 Debug( LDAP_DEBUG_ACL,
1335 "send_search_reference: access "
1336 "to reference not allowed\n",
1342 if( op->o_domain_scope ) {
1343 Debug( LDAP_DEBUG_ANY,
1344 "send_search_reference: domainScope control in (%s)\n",
1345 rs->sr_entry->e_dn, 0, 0 );
1350 if( rs->sr_ref == NULL ) {
1351 Debug( LDAP_DEBUG_ANY,
1352 "send_search_reference: null ref in (%s)\n",
1353 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1358 if( op->o_protocol < LDAP_VERSION3 ) {
1360 /* save the references for the result */
1361 if( rs->sr_ref[0].bv_val != NULL ) {
1362 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1368 #ifdef LDAP_CONNECTIONLESS
1369 if( op->o_conn && op->o_conn->c_is_udp ) {
1370 ber = op->o_res_ber;
1374 ber_init_w_nullc( ber, LBER_USE_DER );
1375 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1378 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1379 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1382 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1386 rc = ber_printf( ber, /*"{"*/ "N}" );
1390 Debug( LDAP_DEBUG_ANY,
1391 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1393 #ifdef LDAP_CONNECTIONLESS
1394 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1396 ber_free_buf( ber );
1397 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1402 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1403 be_entry_release_rw( op, rs->sr_entry, 0 );
1404 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1405 rs->sr_entry = NULL;
1408 #ifdef LDAP_CONNECTIONLESS
1409 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1411 bytes = send_ldap_ber( op->o_conn, ber );
1412 ber_free_buf( ber );
1414 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1415 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1416 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1417 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1418 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1419 #ifdef LDAP_CONNECTIONLESS
1422 if ( rs->sr_ref != NULL ) {
1425 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1426 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1427 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1432 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1433 op->o_log_prefix, 0, 0, 0, 0 );
1436 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1439 if ( op->o_callback ) {
1440 (void)slap_cleanup_play( op, rs );
1456 *code = LDAP_SUCCESS;
1460 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1461 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1468 while ( (s = strchr( s, '\n' )) != NULL ) {
1473 if ( (c = strchr( s, ':' )) != NULL ) {
1477 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1478 if ( c != NULL && lutil_atoi( code, c ) != 0 ) {
1481 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1485 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1491 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1501 int slap_read_controls(
1505 const struct berval *oid,
1506 LDAPControl **ctrl )
1510 BerElementBuffer berbuf;
1511 BerElement *ber = (BerElement *) &berbuf;
1515 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1516 oid->bv_val, e->e_dn, 0 );
1519 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1520 op->o_preread_attrs : op->o_postread_attrs;
1522 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1523 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1525 ber_init2( ber, &bv, LBER_USE_DER );
1526 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1528 /* create new operation */
1531 myop.o_res_ber = ber;
1532 myop.o_callback = NULL;
1533 myop.ors_slimit = 1;
1535 rc = slap_send_search_entry( &myop, rs );
1538 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1540 if( rc == LBER_ERROR ) return LDAP_OTHER;
1542 c.ldctl_oid = oid->bv_val;
1543 c.ldctl_iscritical = 0;
1545 if ( *ctrl == NULL ) {
1547 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1549 /* retry: free previous try */
1550 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1554 return LDAP_SUCCESS;
1557 /* Map API errors to protocol errors... */
1559 slap_map_api2result( SlapReply *rs )
1561 switch(rs->sr_err) {
1562 case LDAP_SERVER_DOWN:
1563 return LDAP_UNAVAILABLE;
1564 case LDAP_LOCAL_ERROR:
1566 case LDAP_ENCODING_ERROR:
1567 case LDAP_DECODING_ERROR:
1568 return LDAP_PROTOCOL_ERROR;
1570 return LDAP_UNAVAILABLE;
1571 case LDAP_AUTH_UNKNOWN:
1572 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1573 case LDAP_FILTER_ERROR:
1574 rs->sr_text = "Filter error";
1576 case LDAP_USER_CANCELLED:
1577 rs->sr_text = "User cancelled";
1579 case LDAP_PARAM_ERROR:
1580 return LDAP_PROTOCOL_ERROR;
1581 case LDAP_NO_MEMORY:
1583 case LDAP_CONNECT_ERROR:
1584 return LDAP_UNAVAILABLE;
1585 case LDAP_NOT_SUPPORTED:
1586 return LDAP_UNWILLING_TO_PERFORM;
1587 case LDAP_CONTROL_NOT_FOUND:
1588 return LDAP_PROTOCOL_ERROR;
1589 case LDAP_NO_RESULTS_RETURNED:
1590 return LDAP_NO_SUCH_OBJECT;
1591 case LDAP_MORE_RESULTS_TO_RETURN:
1592 rs->sr_text = "More results to return";
1594 case LDAP_CLIENT_LOOP:
1595 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1596 return LDAP_LOOP_DETECT;
1598 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1605 slap_attr_flags( AttributeName *an )
1607 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1610 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1613 flags |= an_find( an, &AllOper )
1614 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1615 flags |= an_find( an, &AllUser )
1616 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;