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 = ch_malloc( len+i+sizeof("Referral:") );
94 strcpy( v2+len, "Referral:" );
95 len += sizeof("Referral:");
97 for( i=0; ref[i].bv_val != NULL; i++ ) {
98 v2 = ch_realloc( v2, len + ref[i].bv_len + 1 );
100 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
101 len += ref[i].bv_len;
102 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
112 slap_req2res( ber_tag_t tag )
117 case LDAP_REQ_COMPARE:
118 case LDAP_REQ_EXTENDED:
119 case LDAP_REQ_MODIFY:
120 case LDAP_REQ_MODRDN:
124 case LDAP_REQ_DELETE:
125 tag = LDAP_RES_DELETE;
128 case LDAP_REQ_ABANDON:
129 case LDAP_REQ_UNBIND:
133 case LDAP_REQ_SEARCH:
134 tag = LDAP_RES_SEARCH_RESULT;
144 static long send_ldap_ber(
150 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
152 /* write only one pdu at a time - wait til it's our turn */
153 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
155 /* lock the connection */
156 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
163 if ( connection_state_closing( conn ) ) {
164 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
165 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
170 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
177 * we got an error. if it's ewouldblock, we need to
178 * wait on the socket being writable. otherwise, figure
179 * it's a hard error and return.
182 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
183 err, sock_errstr(err), 0 );
185 if ( err != EWOULDBLOCK && err != EAGAIN ) {
186 connection_closing( conn, "connection lost on write" );
188 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
189 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
194 /* wait for socket to be write-ready */
195 conn->c_writewaiter = 1;
196 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
197 slapd_set_write( sd, 1 );
199 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
200 conn->c_writewaiter = 0;
203 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
204 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
210 send_ldap_control( BerElement *ber, LDAPControl *c )
216 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
218 if( c->ldctl_iscritical ) {
219 rc = ber_printf( ber, "b",
220 (ber_int_t) c->ldctl_iscritical ) ;
221 if( rc == -1 ) return rc;
224 if( c->ldctl_value.bv_val != NULL ) {
225 rc = ber_printf( ber, "O", &c->ldctl_value );
226 if( rc == -1 ) return rc;
229 rc = ber_printf( ber, /*{*/"N}" );
230 if( rc == -1 ) return rc;
236 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
243 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
244 if( rc == -1 ) return rc;
246 for( ; *c != NULL; c++) {
247 rc = send_ldap_control( ber, *c );
248 if( rc == -1 ) return rc;
252 /* this is a hack to avoid having to modify op->s_ctrls */
253 if( o->o_sortedresults ) {
254 BerElementBuffer berbuf;
255 BerElement *sber = (BerElement *) &berbuf;
257 BER_BVZERO( &sorted.ldctl_value );
258 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
259 sorted.ldctl_iscritical = 0;
261 ber_init2( sber, NULL, LBER_USE_DER );
263 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
265 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
269 (void) ber_free_buf( ber );
271 rc = send_ldap_control( ber, &sorted );
272 if( rc == -1 ) return rc;
276 rc = ber_printf( ber, /*{*/"N}" );
286 BerElementBuffer berbuf;
287 BerElement *ber = (BerElement *) &berbuf;
288 int rc = LDAP_SUCCESS;
291 if ( rs->sr_err == SLAPD_ABANDON ) {
296 if ( op->o_callback ) {
297 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
299 rc = SLAP_CB_CONTINUE;
300 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
301 sc_next = op->o_callback->sc_next;
302 if ( op->o_callback->sc_response ) {
303 slap_callback *sc2 = op->o_callback;
304 rc = op->o_callback->sc_response( op, rs );
305 if ( op->o_callback != sc2 ) {
306 *sc_prev = op->o_callback;
308 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
309 if ( op->o_callback != sc2 ) continue;
311 sc_prev = &op->o_callback->sc_next;
315 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
318 #ifdef LDAP_CONNECTIONLESS
319 if (op->o_conn && op->o_conn->c_is_udp)
324 ber_init_w_nullc( ber, LBER_USE_DER );
325 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
328 Debug( LDAP_DEBUG_TRACE,
329 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
330 rs->sr_msgid, rs->sr_tag, rs->sr_err );
333 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
334 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
338 #ifdef LDAP_CONNECTIONLESS
339 if (op->o_conn && op->o_conn->c_is_udp &&
340 op->o_protocol == LDAP_VERSION2 )
342 rc = ber_printf( ber, "t{ess" /*"}"*/,
343 rs->sr_tag, rs->sr_err,
344 rs->sr_matched == NULL ? "" : rs->sr_matched,
345 rs->sr_text == NULL ? "" : rs->sr_text );
348 if ( rs->sr_type == REP_INTERMEDIATE ) {
349 rc = ber_printf( ber, "{it{" /*"}}"*/,
350 rs->sr_msgid, rs->sr_tag );
353 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
354 rs->sr_msgid, rs->sr_tag, rs->sr_err,
355 rs->sr_matched == NULL ? "" : rs->sr_matched,
356 rs->sr_text == NULL ? "" : rs->sr_text );
360 if ( rs->sr_ref != NULL ) {
361 assert( rs->sr_err == LDAP_REFERRAL );
362 rc = ber_printf( ber, "t{W}",
363 LDAP_TAG_REFERRAL, rs->sr_ref );
365 assert( rs->sr_err != LDAP_REFERRAL );
369 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
370 rc = ber_printf( ber, "tO",
371 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
375 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
377 if ( rs->sr_rspoid != NULL ) {
378 rc = ber_printf( ber, "ts",
379 rs->sr_type == REP_EXTENDED
380 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
383 if( rc != -1 && rs->sr_rspdata != NULL ) {
384 rc = ber_printf( ber, "tO",
385 rs->sr_type == REP_EXTENDED
386 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
392 rc = ber_printf( ber, /*"{"*/ "N}" );
396 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
400 rc = ber_printf( ber, /*"{"*/ "N}" );
403 #ifdef LDAP_CONNECTIONLESS
404 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
407 rc = ber_printf( ber, /*"{"*/ "N}" );
412 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
414 #ifdef LDAP_CONNECTIONLESS
415 if (!op->o_conn || op->o_conn->c_is_udp == 0)
424 bytes = send_ldap_ber( op->o_conn, ber );
425 #ifdef LDAP_CONNECTIONLESS
426 if (!op->o_conn || op->o_conn->c_is_udp == 0)
433 Debug( LDAP_DEBUG_ANY,
434 "send_ldap_response: ber write failed\n",
440 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
441 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
442 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
443 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
446 /* Tell caller that we did this for real, as opposed to being
447 * overridden by a callback
449 rc = SLAP_CB_CONTINUE;
452 if ( op->o_callback ) {
453 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
455 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
456 sc_next = op->o_callback->sc_next;
457 if ( op->o_callback->sc_cleanup ) {
458 slap_callback *sc2 = op->o_callback;
459 (void)op->o_callback->sc_cleanup( op, rs );
460 if ( op->o_callback != sc2 ) {
461 *sc_prev = op->o_callback;
463 if ( !op->o_callback ) break;
464 if ( op->o_callback != sc2 ) continue;
466 sc_prev = &op->o_callback->sc_next;
472 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
473 free( (char *)rs->sr_matched );
474 rs->sr_matched = NULL;
477 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
478 ber_bvarray_free( rs->sr_ref );
487 send_ldap_disconnect( Operation *op, SlapReply *rs )
489 #define LDAP_UNSOLICITED_ERROR(e) \
490 ( (e) == LDAP_PROTOCOL_ERROR \
491 || (e) == LDAP_STRONG_AUTH_REQUIRED \
492 || (e) == LDAP_UNAVAILABLE )
494 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
496 rs->sr_type = REP_EXTENDED;
498 Debug( LDAP_DEBUG_TRACE,
499 "send_ldap_disconnect %d:%s\n",
500 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
502 if ( op->o_protocol < LDAP_VERSION3 ) {
503 rs->sr_rspoid = NULL;
504 rs->sr_tag = slap_req2res( op->o_tag );
505 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
508 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
509 rs->sr_tag = LDAP_RES_EXTENDED;
513 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
514 Statslog( LDAP_DEBUG_STATS,
515 "%s DISCONNECT tag=%lu err=%d text=%s\n",
516 op->o_log_prefix, rs->sr_tag, rs->sr_err,
517 rs->sr_text ? rs->sr_text : "", 0 );
522 slap_send_ldap_result( Operation *op, SlapReply *rs )
525 const char *otext = rs->sr_text;
526 BerVarray oref = rs->sr_ref;
528 rs->sr_type = REP_RESULT;
530 /* Propagate Abandons so that cleanup callbacks can be processed */
531 if ( rs->sr_err == SLAPD_ABANDON )
534 assert( !LDAP_API_ERROR( rs->sr_err ) );
536 Debug( LDAP_DEBUG_TRACE,
537 "send_ldap_result: %s p=%d\n",
538 op->o_log_prefix, op->o_protocol, 0 );
540 Debug( LDAP_DEBUG_ARGS,
541 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
542 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
543 rs->sr_text ? rs->sr_text : "" );
547 Debug( LDAP_DEBUG_ARGS,
548 "send_ldap_result: referral=\"%s\"\n",
549 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
553 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
555 if ( rs->sr_err == LDAP_REFERRAL ) {
556 if( op->o_domain_scope ) rs->sr_ref = NULL;
558 if( rs->sr_ref == NULL ) {
559 rs->sr_err = LDAP_NO_SUCH_OBJECT;
560 } else if ( op->o_protocol < LDAP_VERSION3 ) {
561 rs->sr_err = LDAP_PARTIAL_RESULTS;
565 if ( op->o_protocol < LDAP_VERSION3 ) {
566 tmp = v2ref( rs->sr_ref, rs->sr_text );
571 rs->sr_tag = slap_req2res( op->o_tag );
572 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
575 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
576 if ( op->o_tag == LDAP_REQ_SEARCH ) {
578 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
579 rs->sr_err, rs->sr_nentries );
581 Statslog( LDAP_DEBUG_STATS,
582 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
583 op->o_log_prefix, rs->sr_tag, nbuf,
584 rs->sr_text ? rs->sr_text : "", 0 );
586 Statslog( LDAP_DEBUG_STATS,
587 "%s RESULT tag=%lu err=%d text=%s\n",
588 op->o_log_prefix, rs->sr_tag, rs->sr_err,
589 rs->sr_text ? rs->sr_text : "", 0 );
593 if( tmp != NULL ) ch_free(tmp);
599 send_ldap_sasl( Operation *op, SlapReply *rs )
601 rs->sr_type = REP_SASL;
602 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
604 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
606 rs->sr_tag = slap_req2res( op->o_tag );
607 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
609 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
610 Statslog( LDAP_DEBUG_STATS,
611 "%s RESULT tag=%lu err=%d text=%s\n",
612 op->o_log_prefix, rs->sr_tag, rs->sr_err,
613 rs->sr_text ? rs->sr_text : "", 0 );
618 slap_send_ldap_extended( Operation *op, SlapReply *rs )
620 rs->sr_type = REP_EXTENDED;
622 Debug( LDAP_DEBUG_TRACE,
623 "send_ldap_extended: err=%d oid=%s len=%ld\n",
625 rs->sr_rspoid ? rs->sr_rspoid : "",
626 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
628 rs->sr_tag = slap_req2res( op->o_tag );
629 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
631 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
632 Statslog( LDAP_DEBUG_STATS,
633 "%s RESULT oid=%s err=%d text=%s\n",
634 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
635 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
640 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
642 rs->sr_type = REP_INTERMEDIATE;
643 Debug( LDAP_DEBUG_TRACE,
644 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
646 rs->sr_rspoid ? rs->sr_rspoid : "",
647 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
648 rs->sr_tag = LDAP_RES_INTERMEDIATE;
649 rs->sr_msgid = op->o_msgid;
650 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
651 Statslog( LDAP_DEBUG_STATS2,
652 "%s INTERM oid=%s\n",
654 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
661 * LDAP_SUCCESS entry sent
662 * LDAP_OTHER entry not sent (other)
663 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
664 * LDAP_UNAVAILABLE entry not sent (connection closed)
665 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
669 slap_send_search_entry( Operation *op, SlapReply *rs )
671 BerElementBuffer berbuf;
672 BerElement *ber = (BerElement *) &berbuf;
674 int i, j, rc = LDAP_UNAVAILABLE, bytes;
677 AccessControlState acl_state = ACL_STATE_INIT;
679 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
681 /* a_flags: array of flags telling if the i-th element will be
682 * returned or filtered out
683 * e_flags: array of a_flags
685 char **e_flags = NULL;
687 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
688 return LDAP_SIZELIMIT_EXCEEDED;
691 rs->sr_type = REP_SEARCH;
693 /* eventually will loop through generated operational attribute types
694 * currently implemented types include:
695 * entryDN, subschemaSubentry, and hasSubordinates */
696 /* NOTE: moved before overlays callback circling because
697 * they may modify entry and other stuff in rs */
698 /* check for special all operational attributes ("+") type */
699 /* FIXME: maybe we could se this flag at the operation level;
700 * however, in principle the caller of send_search_entry() may
701 * change the attribute list at each call */
702 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
704 rc = backend_operational( op, rs );
709 if ( op->o_callback ) {
710 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
712 rc = SLAP_CB_CONTINUE;
713 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next )
715 sc_next = op->o_callback->sc_next;
716 if ( op->o_callback->sc_response ) {
717 slap_callback *sc2 = op->o_callback;
718 rc = op->o_callback->sc_response( op, rs );
719 if ( op->o_callback != sc2 ) {
720 *sc_prev = op->o_callback;
722 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
723 if ( op->o_callback != sc2 ) continue;
725 sc_prev = &op->o_callback->sc_next;
729 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
732 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
733 op->o_connid, rs->sr_entry->e_name.bv_val,
734 op->ors_attrsonly ? " (attrsOnly)" : "" );
736 attrsonly = op->ors_attrsonly;
738 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
739 Debug( LDAP_DEBUG_ACL,
740 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
741 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
743 rc = LDAP_INSUFFICIENT_ACCESS;
747 edn = rs->sr_entry->e_nname.bv_val;
749 if ( op->o_res_ber ) {
750 /* read back control or LDAP_CONNECTIONLESS */
755 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
756 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
758 ber_init2( ber, &bv, LBER_USE_DER );
759 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
762 #ifdef LDAP_CONNECTIONLESS
763 if ( op->o_conn && op->o_conn->c_is_udp ) {
765 if ( op->o_protocol == LDAP_VERSION2 ) {
766 rc = ber_printf(ber, "t{O{" /*}}*/,
767 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
769 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
770 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
774 if ( op->o_res_ber ) {
775 /* read back control */
776 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
778 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
779 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
783 Debug( LDAP_DEBUG_ANY,
784 "send_search_entry: conn %lu ber_printf failed\n",
785 op->o_connid, 0, 0 );
787 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
788 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
793 /* check for special all user attributes ("*") type */
794 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
796 /* create an array of arrays of flags. Each flag corresponds
797 * to particular value of attribute and equals 1 if value matches
798 * to ValuesReturnFilter or 0 if not
800 if ( op->o_vrFilter != NULL ) {
804 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
805 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
808 size = i * sizeof(char *) + k;
811 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
812 if( e_flags == NULL ) {
813 Debug( LDAP_DEBUG_ANY,
814 "send_search_entry: conn %lu slap_sl_calloc failed\n",
815 op->o_connid ? op->o_connid : 0, 0, 0 );
818 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
821 a_flags = (char *)(e_flags + i);
822 memset( a_flags, 0, k );
823 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
824 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
825 e_flags[i] = a_flags;
829 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
831 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
832 "conn %lu matched values filtering failed\n",
833 op->o_connid ? op->o_connid : 0, 0, 0 );
834 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
835 send_ldap_error( op, rs, LDAP_OTHER,
836 "matched values filtering error" );
843 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
844 AttributeDescription *desc = a->a_desc;
847 if ( rs->sr_attrs == NULL ) {
848 /* all user attrs request, skip operational attributes */
849 if( is_at_operational( desc->ad_type ) ) {
854 /* specific attrs requested */
855 if ( is_at_operational( desc->ad_type ) ) {
856 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
857 !ad_inlist( desc, rs->sr_attrs ) )
863 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
870 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
871 ACL_READ, &acl_state ) )
873 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
874 "conn %lu access to attribute %s not allowed\n",
875 op->o_connid, desc->ad_cname.bv_val, 0 );
879 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
880 Debug( LDAP_DEBUG_ANY,
881 "send_search_entry: conn %lu ber_printf failed\n",
882 op->o_connid, 0, 0 );
884 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
885 send_ldap_error( op, rs, LDAP_OTHER,
886 "encoding description error");
894 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
895 if ( ! access_allowed( op, rs->sr_entry,
896 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
898 Debug( LDAP_DEBUG_ACL,
899 "send_search_entry: conn %lu "
900 "access to attribute %s, value #%d not allowed\n",
901 op->o_connid, desc->ad_cname.bv_val, i );
906 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
913 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
914 Debug( LDAP_DEBUG_ANY,
915 "send_search_entry: conn %lu ber_printf failed\n",
916 op->o_connid, 0, 0 );
918 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
919 send_ldap_error( op, rs, LDAP_OTHER,
920 "encoding description error");
925 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
926 Debug( LDAP_DEBUG_ANY,
927 "send_search_entry: conn %lu "
928 "ber_printf failed.\n", op->o_connid, 0, 0 );
930 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
931 send_ldap_error( op, rs, LDAP_OTHER,
932 "encoding values error" );
939 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
940 Debug( LDAP_DEBUG_ANY,
941 "send_search_entry: conn %lu ber_printf failed\n",
942 op->o_connid, 0, 0 );
944 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
945 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
951 /* NOTE: moved before overlays callback circling because
952 * they may modify entry and other stuff in rs */
953 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
957 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
958 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
961 size = i * sizeof(char *) + k;
963 char *a_flags, **tmp;
966 * Reuse previous memory - we likely need less space
967 * for operational attributes
969 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
972 Debug( LDAP_DEBUG_ANY,
973 "send_search_entry: conn %lu "
975 "for matched values filtering\n",
976 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 "not enough memory for matched values filtering" );
983 a_flags = (char *)(e_flags + i);
984 memset( a_flags, 0, k );
985 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
986 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
987 e_flags[i] = a_flags;
990 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
993 Debug( LDAP_DEBUG_ANY,
994 "send_search_entry: conn %lu "
995 "matched values filtering failed\n",
996 op->o_connid ? op->o_connid : 0, 0, 0);
997 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
998 send_ldap_error( op, rs, LDAP_OTHER,
999 "matched values filtering error" );
1006 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1007 AttributeDescription *desc = a->a_desc;
1009 if ( rs->sr_attrs == NULL ) {
1010 /* all user attrs request, skip operational attributes */
1011 if( is_at_operational( desc->ad_type ) ) {
1016 /* specific attrs requested */
1017 if( is_at_operational( desc->ad_type ) ) {
1018 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1019 !ad_inlist( desc, rs->sr_attrs ) )
1024 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1030 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1031 ACL_READ, &acl_state ) )
1033 Debug( LDAP_DEBUG_ACL,
1034 "send_search_entry: conn %lu "
1035 "access to attribute %s not allowed\n",
1036 op->o_connid, desc->ad_cname.bv_val, 0 );
1041 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1043 Debug( LDAP_DEBUG_ANY,
1044 "send_search_entry: conn %lu "
1045 "ber_printf failed\n", op->o_connid, 0, 0 );
1047 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1048 send_ldap_error( op, rs, LDAP_OTHER,
1049 "encoding description error" );
1054 if ( ! attrsonly ) {
1055 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1056 if ( ! access_allowed( op, rs->sr_entry,
1057 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1059 Debug( LDAP_DEBUG_ACL,
1060 "send_search_entry: conn %lu "
1061 "access to %s, value %d not allowed\n",
1062 op->o_connid, desc->ad_cname.bv_val, i );
1067 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1071 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1072 Debug( LDAP_DEBUG_ANY,
1073 "send_search_entry: conn %lu ber_printf failed\n",
1074 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 "encoding values error" );
1085 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1086 Debug( LDAP_DEBUG_ANY,
1087 "send_search_entry: conn %lu ber_printf failed\n",
1088 op->o_connid, 0, 0 );
1090 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1091 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1099 slap_sl_free( e_flags, op->o_tmpmemctx );
1103 rc = ber_printf( ber, /*{{*/ "}N}" );
1106 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1110 #ifdef LDAP_CONNECTIONLESS
1111 if( op->o_conn && op->o_conn->c_is_udp ) {
1112 if ( op->o_protocol != LDAP_VERSION2 ) {
1113 rc = ber_printf( ber, /*{*/ "N}" );
1117 if ( op->o_res_ber == NULL ) {
1118 rc = ber_printf( ber, /*{*/ "N}" );
1123 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1125 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1126 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1131 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1132 be_entry_release_rw( op, rs->sr_entry, 0 );
1133 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1134 rs->sr_entry = NULL;
1137 if ( op->o_res_ber == NULL ) {
1138 bytes = send_ldap_ber( op->o_conn, ber );
1139 ber_free_buf( ber );
1142 Debug( LDAP_DEBUG_ANY,
1143 "send_search_entry: conn %lu ber write failed.\n",
1144 op->o_connid, 0, 0 );
1146 rc = LDAP_UNAVAILABLE;
1151 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1152 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1153 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1154 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1155 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1158 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1159 op->o_log_prefix, edn, 0, 0, 0 );
1161 Debug( LDAP_DEBUG_TRACE,
1162 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1167 if ( op->o_callback ) {
1168 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1170 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1171 sc_next = op->o_callback->sc_next;
1172 if ( op->o_callback->sc_cleanup ) {
1173 slap_callback *sc2 = op->o_callback;
1174 (void)op->o_callback->sc_cleanup( op, rs );
1175 if ( op->o_callback != sc2 ) {
1176 *sc_prev = op->o_callback;
1178 if ( !op->o_callback ) break;
1179 if ( op->o_callback != sc2 ) continue;
1181 sc_prev = &op->o_callback->sc_next;
1183 op->o_callback = sc;
1187 slap_sl_free( e_flags, op->o_tmpmemctx );
1190 if ( rs->sr_operational_attrs ) {
1191 attrs_free( rs->sr_operational_attrs );
1192 rs->sr_operational_attrs = NULL;
1194 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1196 /* FIXME: I think rs->sr_type should be explicitly set to
1197 * REP_SEARCH here. That's what it was when we entered this
1198 * function. send_ldap_error may have changed it, but we
1199 * should set it back so that the cleanup functions know
1200 * what they're doing.
1202 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1204 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1206 entry_free( rs->sr_entry );
1207 rs->sr_entry = NULL;
1208 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1215 slap_send_search_reference( Operation *op, SlapReply *rs )
1217 BerElementBuffer berbuf;
1218 BerElement *ber = (BerElement *) &berbuf;
1222 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1223 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1225 rs->sr_type = REP_SEARCHREF;
1226 if ( op->o_callback ) {
1227 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1229 rc = SLAP_CB_CONTINUE;
1230 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1231 sc_next = op->o_callback->sc_next;
1232 if ( op->o_callback->sc_response ) {
1233 slap_callback *sc2 = op->o_callback;
1234 rc = op->o_callback->sc_response( op, rs );
1235 if ( op->o_callback != sc2 ) {
1236 *sc_prev = op->o_callback;
1238 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1239 if ( op->o_callback != sc2 ) continue;
1241 sc_prev = &op->o_callback->sc_next;
1244 op->o_callback = sc;
1245 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1248 Debug( LDAP_DEBUG_TRACE,
1249 "=> send_search_reference: dn=\"%s\"\n",
1250 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1252 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1253 ad_entry, NULL, ACL_READ, NULL ) )
1255 Debug( LDAP_DEBUG_ACL,
1256 "send_search_reference: access to entry not allowed\n",
1262 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1263 ad_ref, NULL, ACL_READ, NULL ) )
1265 Debug( LDAP_DEBUG_ACL,
1266 "send_search_reference: access "
1267 "to reference not allowed\n",
1273 if( op->o_domain_scope ) {
1274 Debug( LDAP_DEBUG_ANY,
1275 "send_search_reference: domainScope control in (%s)\n",
1276 rs->sr_entry->e_dn, 0, 0 );
1281 if( rs->sr_ref == NULL ) {
1282 Debug( LDAP_DEBUG_ANY,
1283 "send_search_reference: null ref in (%s)\n",
1284 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1289 if( op->o_protocol < LDAP_VERSION3 ) {
1291 /* save the references for the result */
1292 if( rs->sr_ref[0].bv_val != NULL ) {
1293 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1299 #ifdef LDAP_CONNECTIONLESS
1300 if( op->o_conn && op->o_conn->c_is_udp ) {
1301 ber = op->o_res_ber;
1305 ber_init_w_nullc( ber, LBER_USE_DER );
1306 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1309 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1310 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1313 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1317 rc = ber_printf( ber, /*"{"*/ "N}" );
1321 Debug( LDAP_DEBUG_ANY,
1322 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1324 #ifdef LDAP_CONNECTIONLESS
1325 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1327 ber_free_buf( ber );
1328 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1333 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1334 be_entry_release_rw( op, rs->sr_entry, 0 );
1335 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1336 rs->sr_entry = NULL;
1339 #ifdef LDAP_CONNECTIONLESS
1340 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1342 bytes = send_ldap_ber( op->o_conn, ber );
1343 ber_free_buf( ber );
1345 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1346 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1347 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1348 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1349 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1350 #ifdef LDAP_CONNECTIONLESS
1353 if ( rs->sr_ref != NULL ) {
1356 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1357 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1358 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1363 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1364 op->o_log_prefix, 0, 0, 0, 0 );
1367 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1370 if ( op->o_callback ) {
1371 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1373 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1374 sc_next = op->o_callback->sc_next;
1375 if ( op->o_callback->sc_cleanup ) {
1376 slap_callback *sc2 = op->o_callback;
1377 (void)op->o_callback->sc_cleanup( op, rs );
1378 if ( op->o_callback != sc2 ) {
1379 *sc_prev = op->o_callback;
1381 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1382 if ( op->o_callback != sc2 ) continue;
1384 sc_prev = &op->o_callback->sc_next;
1386 op->o_callback = sc;
1402 *code = LDAP_SUCCESS;
1406 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1407 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1414 while ( (s = strchr( s, '\n' )) != NULL ) {
1419 if ( (c = strchr( s, ':' )) != NULL ) {
1423 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1424 if ( c != NULL && lutil_atoi( code, c ) != 0 ) {
1427 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1431 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1437 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1447 int slap_read_controls(
1451 const struct berval *oid,
1452 LDAPControl **ctrl )
1456 BerElementBuffer berbuf;
1457 BerElement *ber = (BerElement *) &berbuf;
1461 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1462 oid->bv_val, e->e_dn, 0 );
1465 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1466 op->o_preread_attrs : op->o_postread_attrs;
1468 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1469 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1471 ber_init2( ber, &bv, LBER_USE_DER );
1472 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1474 /* create new operation */
1477 myop.o_res_ber = ber;
1478 myop.o_callback = NULL;
1479 myop.ors_slimit = 1;
1481 rc = slap_send_search_entry( &myop, rs );
1484 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1486 if( rc == LBER_ERROR ) return LDAP_OTHER;
1488 c.ldctl_oid = oid->bv_val;
1489 c.ldctl_iscritical = 0;
1491 if ( *ctrl == NULL ) {
1493 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1495 /* retry: free previous try */
1496 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1500 return LDAP_SUCCESS;
1503 /* Map API errors to protocol errors... */
1505 slap_map_api2result( SlapReply *rs )
1507 switch(rs->sr_err) {
1508 case LDAP_SERVER_DOWN:
1509 return LDAP_UNAVAILABLE;
1510 case LDAP_LOCAL_ERROR:
1512 case LDAP_ENCODING_ERROR:
1513 case LDAP_DECODING_ERROR:
1514 return LDAP_PROTOCOL_ERROR;
1516 return LDAP_UNAVAILABLE;
1517 case LDAP_AUTH_UNKNOWN:
1518 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1519 case LDAP_FILTER_ERROR:
1520 rs->sr_text = "Filter error";
1522 case LDAP_USER_CANCELLED:
1523 rs->sr_text = "User cancelled";
1525 case LDAP_PARAM_ERROR:
1526 return LDAP_PROTOCOL_ERROR;
1527 case LDAP_NO_MEMORY:
1529 case LDAP_CONNECT_ERROR:
1530 return LDAP_UNAVAILABLE;
1531 case LDAP_NOT_SUPPORTED:
1532 return LDAP_UNWILLING_TO_PERFORM;
1533 case LDAP_CONTROL_NOT_FOUND:
1534 return LDAP_PROTOCOL_ERROR;
1535 case LDAP_NO_RESULTS_RETURNED:
1536 return LDAP_NO_SUCH_OBJECT;
1537 case LDAP_MORE_RESULTS_TO_RETURN:
1538 rs->sr_text = "More results to return";
1540 case LDAP_CLIENT_LOOP:
1541 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1542 return LDAP_LOOP_DETECT;
1544 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1551 slap_attr_flags( AttributeName *an )
1553 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1556 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1559 flags |= an_find( an, &AllOper )
1560 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1561 flags |= an_find( an, &AllUser )
1562 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;