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>
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 = ch_malloc( len+i+sizeof("Referral:") );
93 strcpy( v2+len, "Referral:" );
94 len += sizeof("Referral:");
96 for( i=0; ref[i].bv_val != NULL; i++ ) {
97 v2 = ch_realloc( v2, len + ref[i].bv_len + 1 );
99 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
100 len += ref[i].bv_len;
101 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
111 slap_req2res( ber_tag_t tag )
116 case LDAP_REQ_COMPARE:
117 case LDAP_REQ_EXTENDED:
118 case LDAP_REQ_MODIFY:
119 case LDAP_REQ_MODRDN:
123 case LDAP_REQ_DELETE:
124 tag = LDAP_RES_DELETE;
127 case LDAP_REQ_ABANDON:
128 case LDAP_REQ_UNBIND:
132 case LDAP_REQ_SEARCH:
133 tag = LDAP_RES_SEARCH_RESULT;
143 static long send_ldap_ber(
149 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
151 /* write only one pdu at a time - wait til it's our turn */
152 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
154 /* lock the connection */
155 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
162 if ( connection_state_closing( conn ) ) {
163 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
164 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
169 if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
176 * we got an error. if it's ewouldblock, we need to
177 * wait on the socket being writable. otherwise, figure
178 * it's a hard error and return.
181 Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
182 err, sock_errstr(err), 0 );
184 if ( err != EWOULDBLOCK && err != EAGAIN ) {
185 connection_closing( conn, "connection lost on write" );
187 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
188 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
193 /* wait for socket to be write-ready */
194 conn->c_writewaiter = 1;
195 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
196 slapd_set_write( sd, 1 );
198 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
199 conn->c_writewaiter = 0;
202 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
203 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
209 send_ldap_control( BerElement *ber, LDAPControl *c )
215 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
217 if( c->ldctl_iscritical ) {
218 rc = ber_printf( ber, "b",
219 (ber_int_t) c->ldctl_iscritical ) ;
220 if( rc == -1 ) return rc;
223 if( c->ldctl_value.bv_val != NULL ) {
224 rc = ber_printf( ber, "O", &c->ldctl_value );
225 if( rc == -1 ) return rc;
228 rc = ber_printf( ber, /*{*/"N}" );
229 if( rc == -1 ) return rc;
235 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
242 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
243 if( rc == -1 ) return rc;
245 for( ; *c != NULL; c++) {
246 rc = send_ldap_control( ber, *c );
247 if( rc == -1 ) return rc;
250 #ifdef SLAP_SORTED_RESULTS
251 /* this is a hack to avoid having to modify op->s_ctrls */
252 if( o->o_sortedresults ) {
253 BerElementBuffer berbuf;
254 BerElement *sber = (BerElement *) &berbuf;
256 BER_BVZERO( &sorted.ldctl_value );
257 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
258 sorted.ldctl_iscritical = 0;
260 ber_init2( sber, NULL, LBER_USE_DER );
262 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
264 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
268 (void) ber_free_buf( ber );
270 rc = send_ldap_control( ber, &sorted );
271 if( rc == -1 ) return rc;
275 rc = ber_printf( ber, /*{*/"N}" );
281 * slap_response_play()
283 * plays the callback list; rationale: a callback can
284 * - remove itself from the list, by setting op->o_callback = NULL;
285 * malloc()'ed callbacks should free themselves from inside the
286 * sc_response() function.
287 * - replace itself with another (list of) callback(s), by setting
288 * op->o_callback = a new (list of) callback(s); in this case, it
289 * is the callback's responsibility to to append existing subsequent
290 * callbacks to the end of the list that is passed to the sc_response()
292 * - modify the list of subsequent callbacks by modifying the value
293 * of the sc_next field from inside the sc_response() function; this
294 * case does not require any handling from inside slap_response_play()
296 * To stop execution of the playlist, the sc_response() function must return
297 * a value different from SLAP_SC_CONTINUE.
299 * The same applies to slap_cleanup_play(); only, there is no means to stop
300 * execution of the playlist, since all cleanup functions must be called.
309 slap_callback *sc = op->o_callback, **scp;
311 rc = SLAP_CB_CONTINUE;
312 for ( scp = ≻ *scp; ) {
313 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
315 op->o_callback = *scp;
316 if ( op->o_callback->sc_response ) {
317 rc = op->o_callback->sc_response( op, rs );
318 if ( op->o_callback == NULL ) {
319 /* the callback has been removed;
324 } else if ( op->o_callback != *scp ) {
325 /* a new callback has been inserted
326 * in place of the existing one; repair the list */
327 *scp = op->o_callback;
330 if ( rc != SLAP_CB_CONTINUE ) break;
344 slap_callback *sc = op->o_callback, **scp;
346 for ( scp = ≻ *scp; ) {
347 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
349 op->o_callback = *scp;
350 if ( op->o_callback->sc_cleanup ) {
351 (void)op->o_callback->sc_cleanup( op, rs );
352 if ( op->o_callback == NULL ) {
353 /* the callback has been removed;
358 } else if ( op->o_callback != *scp ) {
359 /* a new callback has been inserted
360 * after the existing one; repair the list */
361 /* a new callback has been inserted
362 * in place of the existing one; repair the list */
363 *scp = op->o_callback;
366 /* don't care about the result; do all cleanup */
380 BerElementBuffer berbuf;
381 BerElement *ber = (BerElement *) &berbuf;
382 int rc = LDAP_SUCCESS;
385 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon ) {
390 if ( op->o_callback ) {
391 rc = slap_response_play( op, rs );
392 if ( rc != SLAP_CB_CONTINUE ) {
397 #ifdef LDAP_CONNECTIONLESS
398 if (op->o_conn && op->o_conn->c_is_udp)
403 ber_init_w_nullc( ber, LBER_USE_DER );
404 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
407 Debug( LDAP_DEBUG_TRACE,
408 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
409 rs->sr_msgid, rs->sr_tag, rs->sr_err );
412 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
413 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
417 #ifdef LDAP_CONNECTIONLESS
418 if (op->o_conn && op->o_conn->c_is_udp &&
419 op->o_protocol == LDAP_VERSION2 )
421 rc = ber_printf( ber, "t{ess" /*"}"*/,
422 rs->sr_tag, rs->sr_err,
423 rs->sr_matched == NULL ? "" : rs->sr_matched,
424 rs->sr_text == NULL ? "" : rs->sr_text );
427 if ( rs->sr_type == REP_INTERMEDIATE ) {
428 rc = ber_printf( ber, "{it{" /*"}}"*/,
429 rs->sr_msgid, rs->sr_tag );
432 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
433 rs->sr_msgid, rs->sr_tag, rs->sr_err,
434 rs->sr_matched == NULL ? "" : rs->sr_matched,
435 rs->sr_text == NULL ? "" : rs->sr_text );
439 if ( rs->sr_ref != NULL ) {
440 assert( rs->sr_err == LDAP_REFERRAL );
441 rc = ber_printf( ber, "t{W}",
442 LDAP_TAG_REFERRAL, rs->sr_ref );
444 assert( rs->sr_err != LDAP_REFERRAL );
448 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
449 rc = ber_printf( ber, "tO",
450 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
454 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
456 if ( rs->sr_rspoid != NULL ) {
457 rc = ber_printf( ber, "ts",
458 rs->sr_type == REP_EXTENDED
459 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
462 if( rc != -1 && rs->sr_rspdata != NULL ) {
463 rc = ber_printf( ber, "tO",
464 rs->sr_type == REP_EXTENDED
465 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
471 rc = ber_printf( ber, /*"{"*/ "N}" );
475 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
479 rc = ber_printf( ber, /*"{"*/ "N}" );
482 #ifdef LDAP_CONNECTIONLESS
483 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
486 rc = ber_printf( ber, /*"{"*/ "N}" );
491 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
493 #ifdef LDAP_CONNECTIONLESS
494 if (!op->o_conn || op->o_conn->c_is_udp == 0)
503 bytes = send_ldap_ber( op->o_conn, ber );
504 #ifdef LDAP_CONNECTIONLESS
505 if (!op->o_conn || op->o_conn->c_is_udp == 0)
512 Debug( LDAP_DEBUG_ANY,
513 "send_ldap_response: ber write failed\n",
519 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
520 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
521 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
522 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
525 /* Tell caller that we did this for real, as opposed to being
526 * overridden by a callback
528 rc = SLAP_CB_CONTINUE;
531 if ( op->o_callback ) {
532 (void)slap_cleanup_play( op, rs );
535 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
536 free( (char *)rs->sr_matched );
537 rs->sr_matched = NULL;
540 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
541 ber_bvarray_free( rs->sr_ref );
550 send_ldap_disconnect( Operation *op, SlapReply *rs )
552 #define LDAP_UNSOLICITED_ERROR(e) \
553 ( (e) == LDAP_PROTOCOL_ERROR \
554 || (e) == LDAP_STRONG_AUTH_REQUIRED \
555 || (e) == LDAP_UNAVAILABLE )
557 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
559 rs->sr_type = REP_EXTENDED;
561 Debug( LDAP_DEBUG_TRACE,
562 "send_ldap_disconnect %d:%s\n",
563 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
565 if ( op->o_protocol < LDAP_VERSION3 ) {
566 rs->sr_rspoid = NULL;
567 rs->sr_tag = slap_req2res( op->o_tag );
568 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
571 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
572 rs->sr_tag = LDAP_RES_EXTENDED;
576 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
577 Statslog( LDAP_DEBUG_STATS,
578 "%s DISCONNECT tag=%lu err=%d text=%s\n",
579 op->o_log_prefix, rs->sr_tag, rs->sr_err,
580 rs->sr_text ? rs->sr_text : "", 0 );
585 slap_send_ldap_result( Operation *op, SlapReply *rs )
588 const char *otext = rs->sr_text;
589 BerVarray oref = rs->sr_ref;
591 rs->sr_type = REP_RESULT;
593 /* Propagate Abandons so that cleanup callbacks can be processed */
594 if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
597 assert( !LDAP_API_ERROR( rs->sr_err ) );
599 Debug( LDAP_DEBUG_TRACE,
600 "send_ldap_result: %s p=%d\n",
601 op->o_log_prefix, op->o_protocol, 0 );
603 Debug( LDAP_DEBUG_ARGS,
604 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
605 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
606 rs->sr_text ? rs->sr_text : "" );
610 Debug( LDAP_DEBUG_ARGS,
611 "send_ldap_result: referral=\"%s\"\n",
612 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
616 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
618 if ( rs->sr_err == LDAP_REFERRAL ) {
619 if( op->o_domain_scope ) rs->sr_ref = NULL;
621 if( rs->sr_ref == NULL ) {
622 rs->sr_err = LDAP_NO_SUCH_OBJECT;
623 } else if ( op->o_protocol < LDAP_VERSION3 ) {
624 rs->sr_err = LDAP_PARTIAL_RESULTS;
628 if ( op->o_protocol < LDAP_VERSION3 ) {
629 tmp = v2ref( rs->sr_ref, rs->sr_text );
634 rs->sr_tag = slap_req2res( op->o_tag );
635 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
638 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
639 if ( op->o_tag == LDAP_REQ_SEARCH ) {
641 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
642 rs->sr_err, rs->sr_nentries );
644 Statslog( LDAP_DEBUG_STATS,
645 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
646 op->o_log_prefix, rs->sr_tag, nbuf,
647 rs->sr_text ? rs->sr_text : "", 0 );
649 Statslog( LDAP_DEBUG_STATS,
650 "%s RESULT tag=%lu err=%d text=%s\n",
651 op->o_log_prefix, rs->sr_tag, rs->sr_err,
652 rs->sr_text ? rs->sr_text : "", 0 );
656 if( tmp != NULL ) ch_free(tmp);
662 send_ldap_sasl( Operation *op, SlapReply *rs )
664 rs->sr_type = REP_SASL;
665 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
667 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
669 rs->sr_tag = slap_req2res( op->o_tag );
670 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
672 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
673 Statslog( LDAP_DEBUG_STATS,
674 "%s RESULT tag=%lu err=%d text=%s\n",
675 op->o_log_prefix, rs->sr_tag, rs->sr_err,
676 rs->sr_text ? rs->sr_text : "", 0 );
681 slap_send_ldap_extended( Operation *op, SlapReply *rs )
683 rs->sr_type = REP_EXTENDED;
685 Debug( LDAP_DEBUG_TRACE,
686 "send_ldap_extended: err=%d oid=%s len=%ld\n",
688 rs->sr_rspoid ? rs->sr_rspoid : "",
689 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
691 rs->sr_tag = slap_req2res( op->o_tag );
692 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
694 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
695 Statslog( LDAP_DEBUG_STATS,
696 "%s RESULT oid=%s err=%d text=%s\n",
697 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
698 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
703 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
705 rs->sr_type = REP_INTERMEDIATE;
706 Debug( LDAP_DEBUG_TRACE,
707 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
709 rs->sr_rspoid ? rs->sr_rspoid : "",
710 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
711 rs->sr_tag = LDAP_RES_INTERMEDIATE;
712 rs->sr_msgid = op->o_msgid;
713 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
714 Statslog( LDAP_DEBUG_STATS2,
715 "%s INTERM oid=%s\n",
717 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
724 * LDAP_SUCCESS entry sent
725 * LDAP_OTHER entry not sent (other)
726 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
727 * LDAP_UNAVAILABLE entry not sent (connection closed)
728 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
732 slap_send_search_entry( Operation *op, SlapReply *rs )
734 BerElementBuffer berbuf;
735 BerElement *ber = (BerElement *) &berbuf;
737 int i, j, rc = LDAP_UNAVAILABLE, bytes;
740 AccessControlState acl_state = ACL_STATE_INIT;
742 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
744 /* a_flags: array of flags telling if the i-th element will be
745 * returned or filtered out
746 * e_flags: array of a_flags
748 char **e_flags = NULL;
750 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
751 return LDAP_SIZELIMIT_EXCEEDED;
754 rs->sr_type = REP_SEARCH;
756 /* eventually will loop through generated operational attribute types
757 * currently implemented types include:
758 * entryDN, subschemaSubentry, and hasSubordinates */
759 /* NOTE: moved before overlays callback circling because
760 * they may modify entry and other stuff in rs */
761 /* check for special all operational attributes ("+") type */
762 /* FIXME: maybe we could set this flag at the operation level;
763 * however, in principle the caller of send_search_entry() may
764 * change the attribute list at each call */
765 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
767 rc = backend_operational( op, rs );
772 if ( op->o_callback ) {
773 rc = slap_response_play( op, rs );
774 if ( rc != SLAP_CB_CONTINUE ) {
779 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
780 op->o_connid, rs->sr_entry->e_name.bv_val,
781 op->ors_attrsonly ? " (attrsOnly)" : "" );
783 attrsonly = op->ors_attrsonly;
785 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
786 Debug( LDAP_DEBUG_ACL,
787 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
788 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
790 rc = LDAP_INSUFFICIENT_ACCESS;
794 edn = rs->sr_entry->e_nname.bv_val;
796 if ( op->o_res_ber ) {
797 /* read back control or LDAP_CONNECTIONLESS */
802 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
803 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
805 ber_init2( ber, &bv, LBER_USE_DER );
806 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
809 #ifdef LDAP_CONNECTIONLESS
810 if ( op->o_conn && op->o_conn->c_is_udp ) {
812 if ( op->o_protocol == LDAP_VERSION2 ) {
813 rc = ber_printf(ber, "t{O{" /*}}*/,
814 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
816 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
817 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
821 if ( op->o_res_ber ) {
822 /* read back control */
823 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
825 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
826 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
830 Debug( LDAP_DEBUG_ANY,
831 "send_search_entry: conn %lu ber_printf failed\n",
832 op->o_connid, 0, 0 );
834 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
835 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
840 /* check for special all user attributes ("*") type */
841 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
843 /* create an array of arrays of flags. Each flag corresponds
844 * to particular value of attribute and equals 1 if value matches
845 * to ValuesReturnFilter or 0 if not
847 if ( op->o_vrFilter != NULL ) {
851 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
852 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
855 size = i * sizeof(char *) + k;
858 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
859 if( e_flags == NULL ) {
860 Debug( LDAP_DEBUG_ANY,
861 "send_search_entry: conn %lu slap_sl_calloc failed\n",
862 op->o_connid ? op->o_connid : 0, 0, 0 );
865 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
868 a_flags = (char *)(e_flags + i);
869 memset( a_flags, 0, k );
870 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
871 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
872 e_flags[i] = a_flags;
876 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
878 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
879 "conn %lu matched values filtering failed\n",
880 op->o_connid ? op->o_connid : 0, 0, 0 );
881 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
882 send_ldap_error( op, rs, LDAP_OTHER,
883 "matched values filtering error" );
890 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
891 AttributeDescription *desc = a->a_desc;
894 if ( rs->sr_attrs == NULL ) {
895 /* all user attrs request, skip operational attributes */
896 if( is_at_operational( desc->ad_type ) ) {
901 /* specific attrs requested */
902 if ( is_at_operational( desc->ad_type ) ) {
903 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
904 !ad_inlist( desc, rs->sr_attrs ) )
910 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
917 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
918 ACL_READ, &acl_state ) )
920 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
921 "conn %lu access to attribute %s not allowed\n",
922 op->o_connid, desc->ad_cname.bv_val, 0 );
926 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
927 Debug( LDAP_DEBUG_ANY,
928 "send_search_entry: conn %lu ber_printf failed\n",
929 op->o_connid, 0, 0 );
931 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
932 send_ldap_error( op, rs, LDAP_OTHER,
933 "encoding description error");
941 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
942 if ( ! access_allowed( op, rs->sr_entry,
943 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
945 Debug( LDAP_DEBUG_ACL,
946 "send_search_entry: conn %lu "
947 "access to attribute %s, value #%d not allowed\n",
948 op->o_connid, desc->ad_cname.bv_val, i );
953 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
960 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
961 Debug( LDAP_DEBUG_ANY,
962 "send_search_entry: conn %lu ber_printf failed\n",
963 op->o_connid, 0, 0 );
965 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
966 send_ldap_error( op, rs, LDAP_OTHER,
967 "encoding description error");
972 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
973 Debug( LDAP_DEBUG_ANY,
974 "send_search_entry: conn %lu "
975 "ber_printf failed.\n", op->o_connid, 0, 0 );
977 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
978 send_ldap_error( op, rs, LDAP_OTHER,
979 "encoding values error" );
986 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
987 Debug( LDAP_DEBUG_ANY,
988 "send_search_entry: conn %lu ber_printf failed\n",
989 op->o_connid, 0, 0 );
991 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
992 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
998 /* NOTE: moved before overlays callback circling because
999 * they may modify entry and other stuff in rs */
1000 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1004 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1005 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1008 size = i * sizeof(char *) + k;
1010 char *a_flags, **tmp;
1013 * Reuse previous memory - we likely need less space
1014 * for operational attributes
1016 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1018 if ( tmp == NULL ) {
1019 Debug( LDAP_DEBUG_ANY,
1020 "send_search_entry: conn %lu "
1021 "not enough memory "
1022 "for matched values filtering\n",
1023 op->o_connid, 0, 0 );
1024 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1025 send_ldap_error( op, rs, LDAP_OTHER,
1026 "not enough memory for matched values filtering" );
1030 a_flags = (char *)(e_flags + i);
1031 memset( a_flags, 0, k );
1032 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1033 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1034 e_flags[i] = a_flags;
1037 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1040 Debug( LDAP_DEBUG_ANY,
1041 "send_search_entry: conn %lu "
1042 "matched values filtering failed\n",
1043 op->o_connid ? op->o_connid : 0, 0, 0);
1044 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1045 send_ldap_error( op, rs, LDAP_OTHER,
1046 "matched values filtering error" );
1053 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1054 AttributeDescription *desc = a->a_desc;
1056 if ( rs->sr_attrs == NULL ) {
1057 /* all user attrs request, skip operational attributes */
1058 if( is_at_operational( desc->ad_type ) ) {
1063 /* specific attrs requested */
1064 if( is_at_operational( desc->ad_type ) ) {
1065 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1066 !ad_inlist( desc, rs->sr_attrs ) )
1071 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1077 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1078 ACL_READ, &acl_state ) )
1080 Debug( LDAP_DEBUG_ACL,
1081 "send_search_entry: conn %lu "
1082 "access to attribute %s not allowed\n",
1083 op->o_connid, desc->ad_cname.bv_val, 0 );
1088 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1090 Debug( LDAP_DEBUG_ANY,
1091 "send_search_entry: conn %lu "
1092 "ber_printf failed\n", op->o_connid, 0, 0 );
1094 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1095 send_ldap_error( op, rs, LDAP_OTHER,
1096 "encoding description error" );
1101 if ( ! attrsonly ) {
1102 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1103 if ( ! access_allowed( op, rs->sr_entry,
1104 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1106 Debug( LDAP_DEBUG_ACL,
1107 "send_search_entry: conn %lu "
1108 "access to %s, value %d not allowed\n",
1109 op->o_connid, desc->ad_cname.bv_val, i );
1114 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1118 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1119 Debug( LDAP_DEBUG_ANY,
1120 "send_search_entry: conn %lu ber_printf failed\n",
1121 op->o_connid, 0, 0 );
1123 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1124 send_ldap_error( op, rs, LDAP_OTHER,
1125 "encoding values error" );
1132 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1133 Debug( LDAP_DEBUG_ANY,
1134 "send_search_entry: conn %lu ber_printf failed\n",
1135 op->o_connid, 0, 0 );
1137 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1138 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1146 slap_sl_free( e_flags, op->o_tmpmemctx );
1150 rc = ber_printf( ber, /*{{*/ "}N}" );
1153 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1157 #ifdef LDAP_CONNECTIONLESS
1158 if( op->o_conn && op->o_conn->c_is_udp ) {
1159 if ( op->o_protocol != LDAP_VERSION2 ) {
1160 rc = ber_printf( ber, /*{*/ "N}" );
1164 if ( op->o_res_ber == NULL ) {
1165 rc = ber_printf( ber, /*{*/ "N}" );
1170 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1172 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1173 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1178 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1179 be_entry_release_rw( op, rs->sr_entry, 0 );
1180 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1181 rs->sr_entry = NULL;
1184 if ( op->o_res_ber == NULL ) {
1185 bytes = send_ldap_ber( op->o_conn, ber );
1186 ber_free_buf( ber );
1189 Debug( LDAP_DEBUG_ANY,
1190 "send_search_entry: conn %lu ber write failed.\n",
1191 op->o_connid, 0, 0 );
1193 rc = LDAP_UNAVAILABLE;
1198 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1199 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1200 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1201 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1202 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1205 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1206 op->o_log_prefix, edn, 0, 0, 0 );
1208 Debug( LDAP_DEBUG_TRACE,
1209 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1214 if ( op->o_callback ) {
1215 (void)slap_cleanup_play( op, rs );
1219 slap_sl_free( e_flags, op->o_tmpmemctx );
1222 if ( rs->sr_operational_attrs ) {
1223 attrs_free( rs->sr_operational_attrs );
1224 rs->sr_operational_attrs = NULL;
1226 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1228 /* FIXME: I think rs->sr_type should be explicitly set to
1229 * REP_SEARCH here. That's what it was when we entered this
1230 * function. send_ldap_error may have changed it, but we
1231 * should set it back so that the cleanup functions know
1232 * what they're doing.
1234 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1236 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1238 entry_free( rs->sr_entry );
1239 rs->sr_entry = NULL;
1240 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1247 slap_send_search_reference( Operation *op, SlapReply *rs )
1249 BerElementBuffer berbuf;
1250 BerElement *ber = (BerElement *) &berbuf;
1254 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1255 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1257 rs->sr_type = REP_SEARCHREF;
1258 if ( op->o_callback ) {
1259 rc = slap_response_play( op, rs );
1260 if ( rc != SLAP_CB_CONTINUE ) {
1265 Debug( LDAP_DEBUG_TRACE,
1266 "=> send_search_reference: dn=\"%s\"\n",
1267 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1269 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1270 ad_entry, NULL, ACL_READ, NULL ) )
1272 Debug( LDAP_DEBUG_ACL,
1273 "send_search_reference: access to entry not allowed\n",
1279 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1280 ad_ref, NULL, ACL_READ, NULL ) )
1282 Debug( LDAP_DEBUG_ACL,
1283 "send_search_reference: access "
1284 "to reference not allowed\n",
1290 if( op->o_domain_scope ) {
1291 Debug( LDAP_DEBUG_ANY,
1292 "send_search_reference: domainScope control in (%s)\n",
1293 rs->sr_entry->e_dn, 0, 0 );
1298 if( rs->sr_ref == NULL ) {
1299 Debug( LDAP_DEBUG_ANY,
1300 "send_search_reference: null ref in (%s)\n",
1301 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1306 if( op->o_protocol < LDAP_VERSION3 ) {
1308 /* save the references for the result */
1309 if( rs->sr_ref[0].bv_val != NULL ) {
1310 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1316 #ifdef LDAP_CONNECTIONLESS
1317 if( op->o_conn && op->o_conn->c_is_udp ) {
1318 ber = op->o_res_ber;
1322 ber_init_w_nullc( ber, LBER_USE_DER );
1323 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1326 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1327 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1330 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1334 rc = ber_printf( ber, /*"{"*/ "N}" );
1338 Debug( LDAP_DEBUG_ANY,
1339 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1341 #ifdef LDAP_CONNECTIONLESS
1342 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1344 ber_free_buf( ber );
1345 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1350 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1351 be_entry_release_rw( op, rs->sr_entry, 0 );
1352 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1353 rs->sr_entry = NULL;
1356 #ifdef LDAP_CONNECTIONLESS
1357 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1359 bytes = send_ldap_ber( op->o_conn, ber );
1360 ber_free_buf( ber );
1362 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1363 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1364 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1365 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1366 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1367 #ifdef LDAP_CONNECTIONLESS
1370 if ( rs->sr_ref != NULL ) {
1373 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1374 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1375 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1380 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1381 op->o_log_prefix, 0, 0, 0, 0 );
1384 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1387 if ( op->o_callback ) {
1388 (void)slap_cleanup_play( op, rs );
1404 *code = LDAP_SUCCESS;
1408 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1409 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1416 while ( (s = strchr( s, '\n' )) != NULL ) {
1421 if ( (c = strchr( s, ':' )) != NULL ) {
1425 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1430 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1436 while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1437 if ( c[ 0 ] == '\0' ) {
1438 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1444 retcode = strtol( c, &next, 10 );
1445 if ( next == NULL || next == c ) {
1446 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1452 while ( isspace( (unsigned char) next[ 0 ] ) ) next++;
1453 if ( next[ 0 ] != '\0' ) {
1454 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1460 /* FIXME: what if it's larger that max int? */
1461 *code = (int)retcode;
1463 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1467 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1472 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1482 int slap_read_controls(
1486 const struct berval *oid,
1487 LDAPControl **ctrl )
1491 BerElementBuffer berbuf;
1492 BerElement *ber = (BerElement *) &berbuf;
1496 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1497 oid->bv_val, e->e_dn, 0 );
1500 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1501 op->o_preread_attrs : op->o_postread_attrs;
1503 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1504 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1506 ber_init2( ber, &bv, LBER_USE_DER );
1507 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1509 /* create new operation */
1512 myop.o_res_ber = ber;
1513 myop.o_callback = NULL;
1514 myop.ors_slimit = 1;
1516 rc = slap_send_search_entry( &myop, rs );
1519 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1521 if( rc == LBER_ERROR ) return LDAP_OTHER;
1523 c.ldctl_oid = oid->bv_val;
1524 c.ldctl_iscritical = 0;
1526 if ( *ctrl == NULL ) {
1528 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1530 /* retry: free previous try */
1531 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1535 return LDAP_SUCCESS;
1538 /* Map API errors to protocol errors... */
1540 slap_map_api2result( SlapReply *rs )
1542 switch(rs->sr_err) {
1543 case LDAP_SERVER_DOWN:
1544 return LDAP_UNAVAILABLE;
1545 case LDAP_LOCAL_ERROR:
1547 case LDAP_ENCODING_ERROR:
1548 case LDAP_DECODING_ERROR:
1549 return LDAP_PROTOCOL_ERROR;
1551 return LDAP_UNAVAILABLE;
1552 case LDAP_AUTH_UNKNOWN:
1553 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1554 case LDAP_FILTER_ERROR:
1555 rs->sr_text = "Filter error";
1557 case LDAP_USER_CANCELLED:
1558 rs->sr_text = "User cancelled";
1560 case LDAP_PARAM_ERROR:
1561 return LDAP_PROTOCOL_ERROR;
1562 case LDAP_NO_MEMORY:
1564 case LDAP_CONNECT_ERROR:
1565 return LDAP_UNAVAILABLE;
1566 case LDAP_NOT_SUPPORTED:
1567 return LDAP_UNWILLING_TO_PERFORM;
1568 case LDAP_CONTROL_NOT_FOUND:
1569 return LDAP_PROTOCOL_ERROR;
1570 case LDAP_NO_RESULTS_RETURNED:
1571 return LDAP_NO_SUCH_OBJECT;
1572 case LDAP_MORE_RESULTS_TO_RETURN:
1573 rs->sr_text = "More results to return";
1575 case LDAP_CLIENT_LOOP:
1576 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1577 return LDAP_LOOP_DETECT;
1579 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1586 slap_attr_flags( AttributeName *an )
1588 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1591 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1594 flags |= an_find( an, &AllOper )
1595 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1596 flags |= an_find( an, &AllUser )
1597 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;