1 /* result.c - routines to send ldap results, errors, and referrals */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
33 #include <ac/string.h>
36 #include <ac/unistd.h>
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}" );
294 BerElementBuffer berbuf;
295 BerElement *ber = (BerElement *) &berbuf;
296 int rc = LDAP_SUCCESS;
299 if ( rs->sr_err == SLAPD_ABANDON ) {
304 if ( op->o_callback ) {
305 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
307 rc = SLAP_CB_CONTINUE;
308 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
309 sc_next = op->o_callback->sc_next;
310 if ( op->o_callback->sc_response ) {
311 slap_callback *sc2 = op->o_callback;
312 rc = op->o_callback->sc_response( op, rs );
313 if ( op->o_callback != sc2 ) {
314 *sc_prev = op->o_callback;
316 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
317 if ( op->o_callback != sc2 ) continue;
319 sc_prev = &op->o_callback->sc_next;
323 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
326 #ifdef LDAP_CONNECTIONLESS
327 if (op->o_conn && op->o_conn->c_is_udp)
332 ber_init_w_nullc( ber, LBER_USE_DER );
333 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
336 Debug( LDAP_DEBUG_TRACE,
337 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
338 rs->sr_msgid, rs->sr_tag, rs->sr_err );
341 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
342 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
346 #ifdef LDAP_CONNECTIONLESS
347 if (op->o_conn && op->o_conn->c_is_udp &&
348 op->o_protocol == LDAP_VERSION2 )
350 rc = ber_printf( ber, "t{ess" /*"}"*/,
351 rs->sr_tag, rs->sr_err,
352 rs->sr_matched == NULL ? "" : rs->sr_matched,
353 rs->sr_text == NULL ? "" : rs->sr_text );
356 if ( rs->sr_type == REP_INTERMEDIATE ) {
357 rc = ber_printf( ber, "{it{" /*"}}"*/,
358 rs->sr_msgid, rs->sr_tag );
361 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
362 rs->sr_msgid, rs->sr_tag, rs->sr_err,
363 rs->sr_matched == NULL ? "" : rs->sr_matched,
364 rs->sr_text == NULL ? "" : rs->sr_text );
368 if ( rs->sr_ref != NULL ) {
369 assert( rs->sr_err == LDAP_REFERRAL );
370 rc = ber_printf( ber, "t{W}",
371 LDAP_TAG_REFERRAL, rs->sr_ref );
373 assert( rs->sr_err != LDAP_REFERRAL );
377 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
378 rc = ber_printf( ber, "tO",
379 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
383 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
385 if ( rs->sr_rspoid != NULL ) {
386 rc = ber_printf( ber, "ts",
387 rs->sr_type == REP_EXTENDED
388 ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
391 if( rc != -1 && rs->sr_rspdata != NULL ) {
392 rc = ber_printf( ber, "tO",
393 rs->sr_type == REP_EXTENDED
394 ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
400 rc = ber_printf( ber, /*"{"*/ "N}" );
404 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
408 rc = ber_printf( ber, /*"{"*/ "N}" );
411 #ifdef LDAP_CONNECTIONLESS
412 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
415 rc = ber_printf( ber, /*"{"*/ "N}" );
420 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
422 #ifdef LDAP_CONNECTIONLESS
423 if (!op->o_conn || op->o_conn->c_is_udp == 0)
432 bytes = send_ldap_ber( op->o_conn, ber );
433 #ifdef LDAP_CONNECTIONLESS
434 if (!op->o_conn || op->o_conn->c_is_udp == 0)
441 Debug( LDAP_DEBUG_ANY,
442 "send_ldap_response: ber write failed\n",
448 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
449 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
450 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
451 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
454 /* Tell caller that we did this for real, as opposed to being
455 * overridden by a callback
457 rc = SLAP_CB_CONTINUE;
460 if ( op->o_callback ) {
461 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
463 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
464 sc_next = op->o_callback->sc_next;
465 if ( op->o_callback->sc_cleanup ) {
466 slap_callback *sc2 = op->o_callback;
467 (void)op->o_callback->sc_cleanup( op, rs );
468 if ( op->o_callback != sc2 ) {
469 *sc_prev = op->o_callback;
471 if ( !op->o_callback ) break;
472 if ( op->o_callback != sc2 ) continue;
474 sc_prev = &op->o_callback->sc_next;
480 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
481 free( (char *)rs->sr_matched );
482 rs->sr_matched = NULL;
485 if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
486 ber_bvarray_free( rs->sr_ref );
495 send_ldap_disconnect( Operation *op, SlapReply *rs )
497 #define LDAP_UNSOLICITED_ERROR(e) \
498 ( (e) == LDAP_PROTOCOL_ERROR \
499 || (e) == LDAP_STRONG_AUTH_REQUIRED \
500 || (e) == LDAP_UNAVAILABLE )
502 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
504 rs->sr_type = REP_EXTENDED;
506 Debug( LDAP_DEBUG_TRACE,
507 "send_ldap_disconnect %d:%s\n",
508 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
510 if ( op->o_protocol < LDAP_VERSION3 ) {
511 rs->sr_rspoid = NULL;
512 rs->sr_tag = slap_req2res( op->o_tag );
513 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
516 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
517 rs->sr_tag = LDAP_RES_EXTENDED;
521 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
522 Statslog( LDAP_DEBUG_STATS,
523 "%s DISCONNECT tag=%lu err=%d text=%s\n",
524 op->o_log_prefix, rs->sr_tag, rs->sr_err,
525 rs->sr_text ? rs->sr_text : "", 0 );
530 slap_send_ldap_result( Operation *op, SlapReply *rs )
533 const char *otext = rs->sr_text;
534 BerVarray oref = rs->sr_ref;
536 rs->sr_type = REP_RESULT;
538 /* Propagate Abandons so that cleanup callbacks can be processed */
539 if ( rs->sr_err == SLAPD_ABANDON )
542 assert( !LDAP_API_ERROR( rs->sr_err ) );
544 Debug( LDAP_DEBUG_TRACE,
545 "send_ldap_result: %s p=%d\n",
546 op->o_log_prefix, op->o_protocol, 0 );
548 Debug( LDAP_DEBUG_ARGS,
549 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
550 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
551 rs->sr_text ? rs->sr_text : "" );
555 Debug( LDAP_DEBUG_ARGS,
556 "send_ldap_result: referral=\"%s\"\n",
557 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
561 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
563 if ( rs->sr_err == LDAP_REFERRAL ) {
564 if( op->o_domain_scope ) rs->sr_ref = NULL;
566 if( rs->sr_ref == NULL ) {
567 rs->sr_err = LDAP_NO_SUCH_OBJECT;
568 } else if ( op->o_protocol < LDAP_VERSION3 ) {
569 rs->sr_err = LDAP_PARTIAL_RESULTS;
573 if ( op->o_protocol < LDAP_VERSION3 ) {
574 tmp = v2ref( rs->sr_ref, rs->sr_text );
579 rs->sr_tag = slap_req2res( op->o_tag );
580 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
583 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
584 if ( op->o_tag == LDAP_REQ_SEARCH ) {
586 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
587 rs->sr_err, rs->sr_nentries );
589 Statslog( LDAP_DEBUG_STATS,
590 "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
591 op->o_log_prefix, rs->sr_tag, nbuf,
592 rs->sr_text ? rs->sr_text : "", 0 );
594 Statslog( LDAP_DEBUG_STATS,
595 "%s RESULT tag=%lu err=%d text=%s\n",
596 op->o_log_prefix, rs->sr_tag, rs->sr_err,
597 rs->sr_text ? rs->sr_text : "", 0 );
601 if( tmp != NULL ) ch_free(tmp);
607 send_ldap_sasl( Operation *op, SlapReply *rs )
609 rs->sr_type = REP_SASL;
610 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
612 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
614 rs->sr_tag = slap_req2res( op->o_tag );
615 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
617 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
618 Statslog( LDAP_DEBUG_STATS,
619 "%s RESULT tag=%lu err=%d text=%s\n",
620 op->o_log_prefix, rs->sr_tag, rs->sr_err,
621 rs->sr_text ? rs->sr_text : "", 0 );
626 slap_send_ldap_extended( Operation *op, SlapReply *rs )
628 rs->sr_type = REP_EXTENDED;
630 Debug( LDAP_DEBUG_TRACE,
631 "send_ldap_extended: err=%d oid=%s len=%ld\n",
633 rs->sr_rspoid ? rs->sr_rspoid : "",
634 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
636 rs->sr_tag = slap_req2res( op->o_tag );
637 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
639 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
640 Statslog( LDAP_DEBUG_STATS,
641 "%s RESULT oid=%s err=%d text=%s\n",
642 op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
643 rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
648 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
650 rs->sr_type = REP_INTERMEDIATE;
651 Debug( LDAP_DEBUG_TRACE,
652 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
654 rs->sr_rspoid ? rs->sr_rspoid : "",
655 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
656 rs->sr_tag = LDAP_RES_INTERMEDIATE;
657 rs->sr_msgid = op->o_msgid;
658 if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
659 Statslog( LDAP_DEBUG_STATS2,
660 "%s INTERM oid=%s\n",
662 rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
669 * LDAP_SUCCESS entry sent
670 * LDAP_OTHER entry not sent (other)
671 * LDAP_INSUFFICIENT_ACCESS entry not sent (ACL)
672 * LDAP_UNAVAILABLE entry not sent (connection closed)
673 * LDAP_SIZELIMIT_EXCEEDED entry not sent (caller must send sizelimitExceeded)
677 slap_send_search_entry( Operation *op, SlapReply *rs )
679 BerElementBuffer berbuf;
680 BerElement *ber = (BerElement *) &berbuf;
682 int i, j, rc = LDAP_UNAVAILABLE, bytes;
685 AccessControlState acl_state = ACL_STATE_INIT;
687 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
689 /* a_flags: array of flags telling if the i-th element will be
690 * returned or filtered out
691 * e_flags: array of a_flags
693 char **e_flags = NULL;
695 if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
696 return LDAP_SIZELIMIT_EXCEEDED;
699 rs->sr_type = REP_SEARCH;
701 /* eventually will loop through generated operational attribute types
702 * currently implemented types include:
703 * entryDN, subschemaSubentry, and hasSubordinates */
704 /* NOTE: moved before overlays callback circling because
705 * they may modify entry and other stuff in rs */
706 /* check for special all operational attributes ("+") type */
707 /* FIXME: maybe we could set this flag at the operation level;
708 * however, in principle the caller of send_search_entry() may
709 * change the attribute list at each call */
710 rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
712 rc = backend_operational( op, rs );
717 if ( op->o_callback ) {
718 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
720 rc = SLAP_CB_CONTINUE;
721 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next )
723 sc_next = op->o_callback->sc_next;
724 if ( op->o_callback->sc_response ) {
725 slap_callback *sc2 = op->o_callback;
726 rc = op->o_callback->sc_response( op, rs );
727 if ( op->o_callback != sc2 ) {
728 *sc_prev = op->o_callback;
730 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
731 if ( op->o_callback != sc2 ) continue;
733 sc_prev = &op->o_callback->sc_next;
737 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
740 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
741 op->o_connid, rs->sr_entry->e_name.bv_val,
742 op->ors_attrsonly ? " (attrsOnly)" : "" );
744 attrsonly = op->ors_attrsonly;
746 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
747 Debug( LDAP_DEBUG_ACL,
748 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
749 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
751 rc = LDAP_INSUFFICIENT_ACCESS;
755 edn = rs->sr_entry->e_nname.bv_val;
757 if ( op->o_res_ber ) {
758 /* read back control or LDAP_CONNECTIONLESS */
763 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
764 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
766 ber_init2( ber, &bv, LBER_USE_DER );
767 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
770 #ifdef LDAP_CONNECTIONLESS
771 if ( op->o_conn && op->o_conn->c_is_udp ) {
773 if ( op->o_protocol == LDAP_VERSION2 ) {
774 rc = ber_printf(ber, "t{O{" /*}}*/,
775 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
777 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
778 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
782 if ( op->o_res_ber ) {
783 /* read back control */
784 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
786 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
787 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
791 Debug( LDAP_DEBUG_ANY,
792 "send_search_entry: conn %lu ber_printf failed\n",
793 op->o_connid, 0, 0 );
795 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
796 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
801 /* check for special all user attributes ("*") type */
802 userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
804 /* create an array of arrays of flags. Each flag corresponds
805 * to particular value of attribute and equals 1 if value matches
806 * to ValuesReturnFilter or 0 if not
808 if ( op->o_vrFilter != NULL ) {
812 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
813 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
816 size = i * sizeof(char *) + k;
819 e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
820 if( e_flags == NULL ) {
821 Debug( LDAP_DEBUG_ANY,
822 "send_search_entry: conn %lu slap_sl_calloc failed\n",
823 op->o_connid ? op->o_connid : 0, 0, 0 );
826 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
829 a_flags = (char *)(e_flags + i);
830 memset( a_flags, 0, k );
831 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
832 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
833 e_flags[i] = a_flags;
837 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
839 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
840 "conn %lu matched values filtering failed\n",
841 op->o_connid ? op->o_connid : 0, 0, 0 );
842 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
843 send_ldap_error( op, rs, LDAP_OTHER,
844 "matched values filtering error" );
851 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
852 AttributeDescription *desc = a->a_desc;
855 if ( rs->sr_attrs == NULL ) {
856 /* all user attrs request, skip operational attributes */
857 if( is_at_operational( desc->ad_type ) ) {
862 /* specific attrs requested */
863 if ( is_at_operational( desc->ad_type ) ) {
864 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
865 !ad_inlist( desc, rs->sr_attrs ) )
871 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
878 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
879 ACL_READ, &acl_state ) )
881 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
882 "conn %lu access to attribute %s not allowed\n",
883 op->o_connid, desc->ad_cname.bv_val, 0 );
887 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
888 Debug( LDAP_DEBUG_ANY,
889 "send_search_entry: conn %lu ber_printf failed\n",
890 op->o_connid, 0, 0 );
892 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
893 send_ldap_error( op, rs, LDAP_OTHER,
894 "encoding description error");
902 for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
903 if ( ! access_allowed( op, rs->sr_entry,
904 desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
906 Debug( LDAP_DEBUG_ACL,
907 "send_search_entry: conn %lu "
908 "access to attribute %s, value #%d not allowed\n",
909 op->o_connid, desc->ad_cname.bv_val, i );
914 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
921 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
922 Debug( LDAP_DEBUG_ANY,
923 "send_search_entry: conn %lu ber_printf failed\n",
924 op->o_connid, 0, 0 );
926 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
927 send_ldap_error( op, rs, LDAP_OTHER,
928 "encoding description error");
933 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
934 Debug( LDAP_DEBUG_ANY,
935 "send_search_entry: conn %lu "
936 "ber_printf failed.\n", op->o_connid, 0, 0 );
938 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
939 send_ldap_error( op, rs, LDAP_OTHER,
940 "encoding values error" );
947 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
948 Debug( LDAP_DEBUG_ANY,
949 "send_search_entry: conn %lu ber_printf failed\n",
950 op->o_connid, 0, 0 );
952 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
953 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
959 /* NOTE: moved before overlays callback circling because
960 * they may modify entry and other stuff in rs */
961 if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
965 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
966 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
969 size = i * sizeof(char *) + k;
971 char *a_flags, **tmp;
974 * Reuse previous memory - we likely need less space
975 * for operational attributes
977 tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
980 Debug( LDAP_DEBUG_ANY,
981 "send_search_entry: conn %lu "
983 "for matched values filtering\n",
984 op->o_connid, 0, 0 );
985 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
986 send_ldap_error( op, rs, LDAP_OTHER,
987 "not enough memory for matched values filtering" );
991 a_flags = (char *)(e_flags + i);
992 memset( a_flags, 0, k );
993 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
994 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
995 e_flags[i] = a_flags;
998 rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ;
1001 Debug( LDAP_DEBUG_ANY,
1002 "send_search_entry: conn %lu "
1003 "matched values filtering failed\n",
1004 op->o_connid ? op->o_connid : 0, 0, 0);
1005 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1006 send_ldap_error( op, rs, LDAP_OTHER,
1007 "matched values filtering error" );
1014 for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1015 AttributeDescription *desc = a->a_desc;
1017 if ( rs->sr_attrs == NULL ) {
1018 /* all user attrs request, skip operational attributes */
1019 if( is_at_operational( desc->ad_type ) ) {
1024 /* specific attrs requested */
1025 if( is_at_operational( desc->ad_type ) ) {
1026 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
1027 !ad_inlist( desc, rs->sr_attrs ) )
1032 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1038 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1039 ACL_READ, &acl_state ) )
1041 Debug( LDAP_DEBUG_ACL,
1042 "send_search_entry: conn %lu "
1043 "access to attribute %s not allowed\n",
1044 op->o_connid, desc->ad_cname.bv_val, 0 );
1049 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1051 Debug( LDAP_DEBUG_ANY,
1052 "send_search_entry: conn %lu "
1053 "ber_printf failed\n", op->o_connid, 0, 0 );
1055 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1056 send_ldap_error( op, rs, LDAP_OTHER,
1057 "encoding description error" );
1062 if ( ! attrsonly ) {
1063 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1064 if ( ! access_allowed( op, rs->sr_entry,
1065 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1067 Debug( LDAP_DEBUG_ACL,
1068 "send_search_entry: conn %lu "
1069 "access to %s, value %d not allowed\n",
1070 op->o_connid, desc->ad_cname.bv_val, i );
1075 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1079 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1080 Debug( LDAP_DEBUG_ANY,
1081 "send_search_entry: conn %lu ber_printf failed\n",
1082 op->o_connid, 0, 0 );
1084 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1085 send_ldap_error( op, rs, LDAP_OTHER,
1086 "encoding values error" );
1093 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1094 Debug( LDAP_DEBUG_ANY,
1095 "send_search_entry: conn %lu ber_printf failed\n",
1096 op->o_connid, 0, 0 );
1098 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1099 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1107 slap_sl_free( e_flags, op->o_tmpmemctx );
1111 rc = ber_printf( ber, /*{{*/ "}N}" );
1114 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1118 #ifdef LDAP_CONNECTIONLESS
1119 if( op->o_conn && op->o_conn->c_is_udp ) {
1120 if ( op->o_protocol != LDAP_VERSION2 ) {
1121 rc = ber_printf( ber, /*{*/ "N}" );
1125 if ( op->o_res_ber == NULL ) {
1126 rc = ber_printf( ber, /*{*/ "N}" );
1131 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1133 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1134 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1139 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1140 be_entry_release_rw( op, rs->sr_entry, 0 );
1141 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1142 rs->sr_entry = NULL;
1145 if ( op->o_res_ber == NULL ) {
1146 bytes = send_ldap_ber( op->o_conn, ber );
1147 ber_free_buf( ber );
1150 Debug( LDAP_DEBUG_ANY,
1151 "send_search_entry: conn %lu ber write failed.\n",
1152 op->o_connid, 0, 0 );
1154 rc = LDAP_UNAVAILABLE;
1159 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1160 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1161 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1162 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1163 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1166 Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1167 op->o_log_prefix, edn, 0, 0, 0 );
1169 Debug( LDAP_DEBUG_TRACE,
1170 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1175 if ( op->o_callback ) {
1176 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1178 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1179 sc_next = op->o_callback->sc_next;
1180 if ( op->o_callback->sc_cleanup ) {
1181 slap_callback *sc2 = op->o_callback;
1182 (void)op->o_callback->sc_cleanup( op, rs );
1183 if ( op->o_callback != sc2 ) {
1184 *sc_prev = op->o_callback;
1186 if ( !op->o_callback ) break;
1187 if ( op->o_callback != sc2 ) continue;
1189 sc_prev = &op->o_callback->sc_next;
1191 op->o_callback = sc;
1195 slap_sl_free( e_flags, op->o_tmpmemctx );
1198 if ( rs->sr_operational_attrs ) {
1199 attrs_free( rs->sr_operational_attrs );
1200 rs->sr_operational_attrs = NULL;
1202 rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1204 /* FIXME: I think rs->sr_type should be explicitly set to
1205 * REP_SEARCH here. That's what it was when we entered this
1206 * function. send_ldap_error may have changed it, but we
1207 * should set it back so that the cleanup functions know
1208 * what they're doing.
1210 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1212 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) )
1214 entry_free( rs->sr_entry );
1215 rs->sr_entry = NULL;
1216 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1223 slap_send_search_reference( Operation *op, SlapReply *rs )
1225 BerElementBuffer berbuf;
1226 BerElement *ber = (BerElement *) &berbuf;
1230 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1231 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1233 rs->sr_type = REP_SEARCHREF;
1234 if ( op->o_callback ) {
1235 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1237 rc = SLAP_CB_CONTINUE;
1238 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1239 sc_next = op->o_callback->sc_next;
1240 if ( op->o_callback->sc_response ) {
1241 slap_callback *sc2 = op->o_callback;
1242 rc = op->o_callback->sc_response( op, rs );
1243 if ( op->o_callback != sc2 ) {
1244 *sc_prev = op->o_callback;
1246 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1247 if ( op->o_callback != sc2 ) continue;
1249 sc_prev = &op->o_callback->sc_next;
1252 op->o_callback = sc;
1253 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1256 Debug( LDAP_DEBUG_TRACE,
1257 "=> send_search_reference: dn=\"%s\"\n",
1258 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1260 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1261 ad_entry, NULL, ACL_READ, NULL ) )
1263 Debug( LDAP_DEBUG_ACL,
1264 "send_search_reference: access to entry not allowed\n",
1270 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1271 ad_ref, NULL, ACL_READ, NULL ) )
1273 Debug( LDAP_DEBUG_ACL,
1274 "send_search_reference: access "
1275 "to reference not allowed\n",
1281 if( op->o_domain_scope ) {
1282 Debug( LDAP_DEBUG_ANY,
1283 "send_search_reference: domainScope control in (%s)\n",
1284 rs->sr_entry->e_dn, 0, 0 );
1289 if( rs->sr_ref == NULL ) {
1290 Debug( LDAP_DEBUG_ANY,
1291 "send_search_reference: null ref in (%s)\n",
1292 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1297 if( op->o_protocol < LDAP_VERSION3 ) {
1299 /* save the references for the result */
1300 if( rs->sr_ref[0].bv_val != NULL ) {
1301 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1307 #ifdef LDAP_CONNECTIONLESS
1308 if( op->o_conn && op->o_conn->c_is_udp ) {
1309 ber = op->o_res_ber;
1313 ber_init_w_nullc( ber, LBER_USE_DER );
1314 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1317 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1318 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1321 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1325 rc = ber_printf( ber, /*"{"*/ "N}" );
1329 Debug( LDAP_DEBUG_ANY,
1330 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1332 #ifdef LDAP_CONNECTIONLESS
1333 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1335 ber_free_buf( ber );
1336 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1341 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1342 be_entry_release_rw( op, rs->sr_entry, 0 );
1343 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1344 rs->sr_entry = NULL;
1347 #ifdef LDAP_CONNECTIONLESS
1348 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1350 bytes = send_ldap_ber( op->o_conn, ber );
1351 ber_free_buf( ber );
1353 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1354 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1355 ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1356 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1357 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1358 #ifdef LDAP_CONNECTIONLESS
1361 if ( rs->sr_ref != NULL ) {
1364 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1365 Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1366 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1371 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1372 op->o_log_prefix, 0, 0, 0, 0 );
1375 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1378 if ( op->o_callback ) {
1379 slap_callback *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1381 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1382 sc_next = op->o_callback->sc_next;
1383 if ( op->o_callback->sc_cleanup ) {
1384 slap_callback *sc2 = op->o_callback;
1385 (void)op->o_callback->sc_cleanup( op, rs );
1386 if ( op->o_callback != sc2 ) {
1387 *sc_prev = op->o_callback;
1389 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1390 if ( op->o_callback != sc2 ) continue;
1392 sc_prev = &op->o_callback->sc_next;
1394 op->o_callback = sc;
1410 *code = LDAP_SUCCESS;
1414 if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1415 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1422 while ( (s = strchr( s, '\n' )) != NULL ) {
1427 if ( (c = strchr( s, ':' )) != NULL ) {
1431 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1432 if ( c != NULL && lutil_atoi( code, c ) != 0 ) {
1435 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1439 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1445 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1455 int slap_read_controls(
1459 const struct berval *oid,
1460 LDAPControl **ctrl )
1464 BerElementBuffer berbuf;
1465 BerElement *ber = (BerElement *) &berbuf;
1469 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1470 oid->bv_val, e->e_dn, 0 );
1473 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1474 op->o_preread_attrs : op->o_postread_attrs;
1476 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1477 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1479 ber_init2( ber, &bv, LBER_USE_DER );
1480 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1482 /* create new operation */
1485 myop.o_res_ber = ber;
1486 myop.o_callback = NULL;
1487 myop.ors_slimit = 1;
1489 rc = slap_send_search_entry( &myop, rs );
1492 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1494 if( rc == LBER_ERROR ) return LDAP_OTHER;
1496 c.ldctl_oid = oid->bv_val;
1497 c.ldctl_iscritical = 0;
1499 if ( *ctrl == NULL ) {
1501 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1503 /* retry: free previous try */
1504 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1508 return LDAP_SUCCESS;
1511 /* Map API errors to protocol errors... */
1513 slap_map_api2result( SlapReply *rs )
1515 switch(rs->sr_err) {
1516 case LDAP_SERVER_DOWN:
1517 return LDAP_UNAVAILABLE;
1518 case LDAP_LOCAL_ERROR:
1520 case LDAP_ENCODING_ERROR:
1521 case LDAP_DECODING_ERROR:
1522 return LDAP_PROTOCOL_ERROR;
1524 return LDAP_UNAVAILABLE;
1525 case LDAP_AUTH_UNKNOWN:
1526 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1527 case LDAP_FILTER_ERROR:
1528 rs->sr_text = "Filter error";
1530 case LDAP_USER_CANCELLED:
1531 rs->sr_text = "User cancelled";
1533 case LDAP_PARAM_ERROR:
1534 return LDAP_PROTOCOL_ERROR;
1535 case LDAP_NO_MEMORY:
1537 case LDAP_CONNECT_ERROR:
1538 return LDAP_UNAVAILABLE;
1539 case LDAP_NOT_SUPPORTED:
1540 return LDAP_UNWILLING_TO_PERFORM;
1541 case LDAP_CONTROL_NOT_FOUND:
1542 return LDAP_PROTOCOL_ERROR;
1543 case LDAP_NO_RESULTS_RETURNED:
1544 return LDAP_NO_SUCH_OBJECT;
1545 case LDAP_MORE_RESULTS_TO_RETURN:
1546 rs->sr_text = "More results to return";
1548 case LDAP_CLIENT_LOOP:
1549 case LDAP_REFERRAL_LIMIT_EXCEEDED:
1550 return LDAP_LOOP_DETECT;
1552 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1559 slap_attr_flags( AttributeName *an )
1561 slap_mask_t flags = SLAP_ATTRS_UNDEFINED;
1564 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1567 flags |= an_find( an, &AllOper )
1568 ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1569 flags |= an_find( an, &AllUser )
1570 ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;