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-2003 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>
44 int slap_null_cb( Operation *op, SlapReply *rs )
49 static char *v2ref( BerVarray ref, const char *text )
51 size_t len = 0, i = 0;
56 return ch_strdup(text);
64 if (text[len-1] != '\n') {
69 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
72 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
74 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
85 strcpy( v2+len, "Referral:" );
86 len += sizeof("Referral:");
88 for( i=0; ref[i].bv_val != NULL; i++ ) {
89 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
92 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
94 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
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] != '/') {
110 static ber_tag_t req2res( ber_tag_t tag )
115 case LDAP_REQ_COMPARE:
116 case LDAP_REQ_EXTENDED:
117 case LDAP_REQ_MODIFY:
118 case LDAP_REQ_MODRDN:
122 case LDAP_REQ_DELETE:
123 tag = LDAP_RES_DELETE;
126 case LDAP_REQ_ABANDON:
127 case LDAP_REQ_UNBIND:
131 case LDAP_REQ_SEARCH:
132 tag = LDAP_RES_SEARCH_RESULT;
142 static long send_ldap_ber(
148 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
150 /* write only one pdu at a time - wait til it's our turn */
151 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
153 /* lock the connection */
154 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
161 if ( connection_state_closing( conn ) ) {
162 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
163 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
168 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
175 * we got an error. if it's ewouldblock, we need to
176 * wait on the socket being writable. otherwise, figure
177 * it's a hard error and return.
181 LDAP_LOG( OPERATION, ERR,
182 "send_ldap_ber: conn %lu ber_flush failed err=%d (%s)\n",
183 conn ? conn->c_connid : 0, err, sock_errstr(err) );
185 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
186 err, sock_errstr(err), 0 );
189 if ( err != EWOULDBLOCK && err != EAGAIN ) {
190 connection_closing( conn );
192 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
193 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
198 /* wait for socket to be write-ready */
199 conn->c_writewaiter = 1;
200 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
201 slapd_set_write( sd, 1 );
203 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
204 conn->c_writewaiter = 0;
207 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
208 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
214 send_ldap_controls( BerElement *ber, LDAPControl **c )
217 if( c == NULL ) return 0;
219 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
220 if( rc == -1 ) return rc;
222 for( ; *c != NULL; c++) {
223 rc = ber_printf( ber, "{s" /*}*/, (*c)->ldctl_oid );
225 if( (*c)->ldctl_iscritical ) {
226 rc = ber_printf( ber, "b",
227 (ber_int_t) (*c)->ldctl_iscritical ) ;
228 if( rc == -1 ) return rc;
231 if( (*c)->ldctl_value.bv_val != NULL ) {
232 rc = ber_printf( ber, "O", &((*c)->ldctl_value));
233 if( rc == -1 ) return rc;
236 rc = ber_printf( ber, /*{*/"N}" );
237 if( rc == -1 ) return rc;
240 rc = ber_printf( ber, /*{*/"N}" );
250 BerElementBuffer berbuf;
251 BerElement *ber = (BerElement *) &berbuf;
255 if (op->o_callback) {
256 slap_callback *sc = op->o_callback;
257 for ( ; op->o_callback; ) {
258 if ( op->o_callback->sc_response ) {
259 rc = op->o_callback->sc_response( op, rs );
260 if ( rc != SLAP_CB_CONTINUE ) break;
262 op->o_callback = op->o_callback->sc_next;
265 if ( rc != SLAP_CB_CONTINUE ) goto cleanup;
268 #ifdef LDAP_CONNECTIONLESS
269 if (op->o_conn && op->o_conn->c_is_udp)
274 ber_init_w_nullc( ber, LBER_USE_DER );
275 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
279 LDAP_LOG( OPERATION, ENTRY,
280 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
281 rs->sr_msgid, rs->sr_tag, rs->sr_err );
283 Debug( LDAP_DEBUG_TRACE,
284 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
285 rs->sr_msgid, rs->sr_tag, rs->sr_err );
290 LDAP_LOG( OPERATION, ARGS,
291 "send_ldap_response: conn %lu ref=\"%s\"\n",
293 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
295 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
296 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
301 #ifdef LDAP_CONNECTIONLESS
302 if (op->o_conn && op->o_conn->c_is_udp &&
303 op->o_protocol == LDAP_VERSION2 )
305 rc = ber_printf( ber, "t{ess" /*"}}"*/,
306 rs->sr_tag, rs->sr_err,
307 rs->sr_matched == NULL ? "" : rs->sr_matched,
308 rs->sr_text == NULL ? "" : rs->sr_text );
311 if ( rs->sr_type == REP_INTERMEDIATE ) {
312 rc = ber_printf( ber, "{it{" /*"}}"*/,
313 rs->sr_msgid, rs->sr_tag );
316 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
317 rs->sr_msgid, rs->sr_tag, rs->sr_err,
318 rs->sr_matched == NULL ? "" : rs->sr_matched,
319 rs->sr_text == NULL ? "" : rs->sr_text );
323 if ( rs->sr_ref != NULL ) {
324 assert( rs->sr_err == LDAP_REFERRAL );
325 rc = ber_printf( ber, "t{W}",
326 LDAP_TAG_REFERRAL, rs->sr_ref );
328 assert( rs->sr_err != LDAP_REFERRAL );
332 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
333 rc = ber_printf( ber, "tO",
334 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
338 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
340 if ( rs->sr_rspoid != NULL ) {
341 rc = ber_printf( ber, "ts",
342 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
344 if( rc != -1 && rs->sr_rspdata != NULL ) {
345 rc = ber_printf( ber, "tO",
346 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
351 rc = ber_printf( ber, /*"{"*/ "N}" );
354 if( rc != -1 && rs->sr_ctrls != NULL ) {
355 rc = send_ldap_controls( ber, rs->sr_ctrls );
359 rc = ber_printf( ber, /*"{"*/ "N}" );
362 #ifdef LDAP_CONNECTIONLESS
363 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
364 rc = ber_printf( ber, /*"{"*/ "N}" );
370 LDAP_LOG( OPERATION, ERR,
371 "send_ldap_response: conn %lu ber_printf failed\n",
372 op->o_connid, 0, 0 );
374 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
377 #ifdef LDAP_CONNECTIONLESS
378 if (!op->o_conn || op->o_conn->c_is_udp == 0)
385 bytes = send_ldap_ber( op->o_conn, ber );
386 #ifdef LDAP_CONNECTIONLESS
387 if (!op->o_conn || op->o_conn->c_is_udp == 0)
393 LDAP_LOG( OPERATION, ERR,
394 "send_ldap_response: conn %lu ber write failed\n",
395 op->o_connid ? op->o_connid : 0, 0, 0 );
397 Debug( LDAP_DEBUG_ANY,
398 "send_ldap_response: ber write failed\n",
407 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
408 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
409 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
411 #endif /* LDAP_SLAPI */
413 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
414 num_bytes_sent += bytes;
416 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
419 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
420 free( (char *)rs->sr_matched );
421 rs->sr_matched = NULL;
424 if (op->o_callback) {
425 slap_callback *sc = op->o_callback;
426 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
427 if ( op->o_callback->sc_cleanup ) {
428 op->o_callback->sc_cleanup( op, rs );
439 send_ldap_disconnect( Operation *op, SlapReply *rs )
441 #define LDAP_UNSOLICITED_ERROR(e) \
442 ( (e) == LDAP_PROTOCOL_ERROR \
443 || (e) == LDAP_STRONG_AUTH_REQUIRED \
444 || (e) == LDAP_UNAVAILABLE )
446 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
448 rs->sr_type = REP_EXTENDED;
451 LDAP_LOG( OPERATION, ENTRY,
452 "send_ldap_disconnect: conn %lu %d:%s\n",
453 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
455 Debug( LDAP_DEBUG_TRACE,
456 "send_ldap_disconnect %d:%s\n",
457 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
461 if ( op->o_protocol < LDAP_VERSION3 ) {
462 rs->sr_rspoid = NULL;
463 rs->sr_tag = req2res( op->o_tag );
464 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
467 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
468 rs->sr_tag = LDAP_RES_EXTENDED;
472 send_ldap_response( op, rs );
474 Statslog( LDAP_DEBUG_STATS,
475 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
476 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
480 slap_send_ldap_result( Operation *op, SlapReply *rs )
483 const char *otext = rs->sr_text;
484 BerVarray oref = rs->sr_ref;
486 rs->sr_type = REP_RESULT;
488 assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
491 LDAP_LOG( OPERATION, ENTRY,
492 "send_ldap_result: conn %lu op=%lu p=%d\n",
493 op->o_connid, op->o_opid, op->o_protocol );
495 Debug( LDAP_DEBUG_TRACE,
496 "send_ldap_result: conn=%lu op=%lu p=%d\n",
497 op->o_connid, op->o_opid, op->o_protocol );
501 LDAP_LOG( OPERATION, ARGS,
502 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
503 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
504 rs->sr_text ? rs->sr_text : "" );
506 Debug( LDAP_DEBUG_ARGS,
507 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
508 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
509 rs->sr_text ? rs->sr_text : "" );
515 LDAP_LOG( OPERATION, ARGS,
516 "send_ldap_result: referral=\"%s\"\n",
517 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
519 Debug( LDAP_DEBUG_ARGS,
520 "send_ldap_result: referral=\"%s\"\n",
521 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
526 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
528 if ( rs->sr_err == LDAP_REFERRAL ) {
529 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
530 if( op->o_domain_scope ) {
534 if( rs->sr_ref == NULL ) {
535 rs->sr_err = LDAP_NO_SUCH_OBJECT;
536 } else if ( op->o_protocol < LDAP_VERSION3 ) {
537 rs->sr_err = LDAP_PARTIAL_RESULTS;
543 * Call pre-result plugins. To avoid infinite recursion plugins
544 * should just set SLAPI_RESULT_CODE rather than sending a
545 * result if they wish to change the result.
548 slapi_x_pblock_set_operation( op->o_pb, op );
549 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
550 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
551 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
553 (void) doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN, op->o_pb );
555 #endif /* LDAP_SLAPI */
557 if ( op->o_protocol < LDAP_VERSION3 ) {
558 tmp = v2ref( rs->sr_ref, rs->sr_text );
563 rs->sr_tag = req2res( op->o_tag );
564 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
566 send_ldap_response( op, rs );
568 if ( op->o_tag == LDAP_REQ_SEARCH ) {
570 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
571 rs->sr_err, rs->sr_nentries );
573 Statslog( LDAP_DEBUG_STATS,
574 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
575 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
576 rs->sr_text ? rs->sr_text : "" );
578 Statslog( LDAP_DEBUG_STATS,
579 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
580 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
581 rs->sr_text ? rs->sr_text : "" );
584 if( tmp != NULL ) ch_free(tmp);
590 send_ldap_sasl( Operation *op, SlapReply *rs )
592 rs->sr_type = REP_SASL;
594 LDAP_LOG( OPERATION, ENTRY,
595 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
596 op->o_connid, rs->sr_err,
597 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
599 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
601 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
604 rs->sr_tag = req2res( op->o_tag );
605 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
607 send_ldap_response( op, rs );
611 slap_send_ldap_extended( Operation *op, SlapReply *rs )
613 rs->sr_type = REP_EXTENDED;
616 LDAP_LOG( OPERATION, ENTRY,
617 "send_ldap_extended: err=%d oid=%s len=%ld\n",
618 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
619 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
621 Debug( LDAP_DEBUG_TRACE,
622 "send_ldap_extended: err=%d oid=%s len=%ld\n",
624 rs->sr_rspoid ? rs->sr_rspoid : "",
625 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
628 rs->sr_tag = req2res( op->o_tag );
629 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
631 send_ldap_response( op, rs );
635 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
637 rs->sr_type = REP_INTERMEDIATE;
639 LDAP_LOG( OPERATION, ENTRY,
640 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
641 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
642 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
644 Debug( LDAP_DEBUG_TRACE,
645 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
647 rs->sr_rspoid ? rs->sr_rspoid : "",
648 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
650 rs->sr_tag = LDAP_RES_INTERMEDIATE;
651 rs->sr_msgid = op->o_msgid;
652 send_ldap_response( op, rs );
656 slap_send_search_entry( Operation *op, SlapReply *rs )
658 BerElementBuffer berbuf;
659 BerElement *ber = (BerElement *) &berbuf;
661 int i, j, rc=-1, bytes;
665 AccessControlState acl_state = ACL_STATE_INIT;
667 /* Support for computed attribute plugins */
668 computed_attr_context ctx;
671 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
673 /* a_flags: array of flags telling if the i-th element will be
674 * returned or filtered out
675 * e_flags: array of a_flags
677 char **e_flags = NULL;
679 rs->sr_type = REP_SEARCH;
680 if (op->o_callback) {
681 slap_callback *sc = op->o_callback;
682 for ( ; op->o_callback; ) {
683 if ( op->o_callback->sc_response ) {
684 rc = op->o_callback->sc_response( op, rs );
685 if ( rc != SLAP_CB_CONTINUE ) break;
687 op->o_callback = op->o_callback->sc_next;
690 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
694 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
695 op->o_connid, rs->sr_entry->e_name.bv_val,
696 op->ors_attrsonly ? " (attrsOnly)" : "" );
698 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
699 rs->sr_entry->e_name.bv_val,
700 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
703 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
706 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
707 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
709 Debug( LDAP_DEBUG_ACL,
710 "send_search_entry: access to entry not allowed\n",
718 edn = rs->sr_entry->e_nname.bv_val;
720 if ( op->o_res_ber ) {
721 /* read back control or LDAP_CONNECTIONLESS */
727 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
728 bv.bv_len = siz + len;
729 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
731 ber_init2( ber, &bv, LBER_USE_DER );
732 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
735 #ifdef LDAP_CONNECTIONLESS
736 if ( op->o_conn && op->o_conn->c_is_udp ) {
738 if ( op->o_protocol == LDAP_VERSION2 ) {
739 rc = ber_printf(ber, "t{O{" /*}}*/,
740 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
742 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
743 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
747 if ( op->o_res_ber ) {
748 /* read back control */
749 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
751 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
752 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
757 LDAP_LOG( OPERATION, ERR,
758 "send_search_entry: conn %lu ber_printf failed\n",
759 op->o_connid, 0, 0 );
761 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
764 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
765 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
769 /* check for special all user attributes ("*") type */
770 userattrs = ( rs->sr_attrs == NULL ) ? 1
771 : an_find( rs->sr_attrs, &AllUser );
773 /* check for special all operational attributes ("+") type */
774 opattrs = ( rs->sr_attrs == NULL ) ? 0
775 : an_find( rs->sr_attrs, &AllOper );
777 /* create an array of arrays of flags. Each flag corresponds
778 * to particular value of attribute and equals 1 if value matches
779 * to ValuesReturnFilter or 0 if not
781 if ( op->o_vrFilter != NULL ) {
785 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
786 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
789 size = i * sizeof(char *) + k;
792 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
793 if( e_flags == NULL ) {
795 LDAP_LOG( OPERATION, ERR,
796 "send_search_entry: conn %lu sl_calloc failed\n",
797 op->o_connid ? op->o_connid : 0, 0, 0 );
799 Debug( LDAP_DEBUG_ANY,
800 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
804 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
807 a_flags = (char *)(e_flags + i);
808 memset( a_flags, 0, k );
809 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
810 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
811 e_flags[i] = a_flags;
815 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
818 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
819 "conn %lu matched values filtering failed\n",
820 op->o_connid ? op->o_connid : 0, 0, 0 );
822 Debug( LDAP_DEBUG_ANY,
823 "matched values filtering failed\n", 0, 0, 0 );
825 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
826 send_ldap_error( op, rs, LDAP_OTHER,
827 "matched values filtering error" );
833 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
834 AttributeDescription *desc = a->a_desc;
836 if ( rs->sr_attrs == NULL ) {
837 /* all attrs request, skip operational attributes */
838 if( is_at_operational( desc->ad_type ) ) {
843 /* specific attrs requested */
844 if ( is_at_operational( desc->ad_type ) ) {
845 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
850 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
856 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
857 ACL_READ, &acl_state ) )
861 "send_search_entry: conn %lu access to attribute %s not "
862 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
864 Debug( LDAP_DEBUG_ACL, "acl: "
865 "access to attribute %s not allowed\n",
866 desc->ad_cname.bv_val, 0, 0 );
871 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
873 LDAP_LOG( OPERATION, ERR,
874 "send_search_entry: conn %lu ber_printf failed\n",
875 op->o_connid, 0, 0 );
877 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
880 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
881 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
885 if ( ! op->ors_attrsonly ) {
886 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
887 if ( ! access_allowed( op, rs->sr_entry,
888 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
892 "send_search_entry: conn %lu "
893 "access to attribute %s, value %d not allowed\n",
894 op->o_connid, desc->ad_cname.bv_val, i );
896 Debug( LDAP_DEBUG_ACL,
897 "acl: access to attribute %s, "
898 "value %d not allowed\n",
899 desc->ad_cname.bv_val, i, 0 );
905 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
909 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
911 LDAP_LOG( OPERATION, ERR,
912 "send_search_entry: conn %lu "
913 "ber_printf failed.\n", op->o_connid, 0, 0 );
915 Debug( LDAP_DEBUG_ANY,
916 "ber_printf failed\n", 0, 0, 0 );
919 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
920 send_ldap_error( op, rs, LDAP_OTHER,
921 "encoding values error" );
927 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
929 LDAP_LOG( OPERATION, ERR,
930 "send_search_entry: conn %lu ber_printf failed\n",
931 op->o_connid, 0, 0 );
933 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
936 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
937 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
942 /* eventually will loop through generated operational attributes */
943 /* only have subschemaSubentry and numSubordinates are implemented */
944 aa = backend_operational( op, rs, opattrs );
946 if ( aa != NULL && op->o_vrFilter != NULL ) {
950 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
951 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
954 size = i * sizeof(char *) + k;
956 char *a_flags, **tmp;
959 * Reuse previous memory - we likely need less space
960 * for operational attributes
962 tmp = sl_realloc( e_flags, i * sizeof(char *) + k,
966 LDAP_LOG( OPERATION, ERR,
967 "send_search_entry: conn %lu "
969 "for matched values filtering\n",
972 Debug( LDAP_DEBUG_ANY,
973 "send_search_entry: conn %lu "
975 "for matched values filtering\n",
976 op->o_connid, 0, 0 );
978 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
979 send_ldap_error( op, rs, LDAP_OTHER,
980 "not enough memory for matched values filtering" );
984 a_flags = (char *)(e_flags + i);
985 memset( a_flags, 0, k );
986 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
987 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
988 e_flags[i] = a_flags;
991 rc = filter_matched_values(op, aa, &e_flags) ;
995 LDAP_LOG( OPERATION, ERR,
996 "send_search_entry: conn %lu "
997 "matched values filtering failed\n",
998 op->o_connid ? op->o_connid : 0, 0, 0);
1000 Debug( LDAP_DEBUG_ANY,
1001 "matched values filtering failed\n", 0, 0, 0 );
1003 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1004 send_ldap_error( op, rs, LDAP_OTHER,
1005 "matched values filtering error" );
1011 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
1012 AttributeDescription *desc = a->a_desc;
1014 if ( rs->sr_attrs == NULL ) {
1015 /* all attrs request, skip operational attributes */
1016 if( is_at_operational( desc->ad_type ) ) {
1021 /* specific attrs requested */
1022 if( is_at_operational( desc->ad_type ) ) {
1023 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1027 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1033 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1034 ACL_READ, &acl_state ) )
1037 LDAP_LOG( ACL, INFO,
1038 "send_search_entry: conn %lu "
1039 "access to attribute %s not allowed\n",
1040 op->o_connid, desc->ad_cname.bv_val, 0 );
1042 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
1043 "not allowed\n", desc->ad_cname.bv_val, 0, 0 );
1049 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1052 LDAP_LOG( OPERATION, ERR,
1053 "send_search_entry: conn %lu "
1054 "ber_printf failed\n", op->o_connid, 0, 0 );
1056 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1059 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1060 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
1065 if ( ! op->ors_attrsonly ) {
1066 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1067 if ( ! access_allowed( op, rs->sr_entry,
1068 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1071 LDAP_LOG( ACL, INFO,
1072 "send_search_entry: conn %lu "
1073 "access to %s, value %d not allowed\n",
1074 op->o_connid, desc->ad_cname.bv_val, i );
1076 Debug( LDAP_DEBUG_ACL,
1077 "send_search_entry: access to attribute %s, "
1078 "value %d not allowed\n",
1079 desc->ad_cname.bv_val, i, 0 );
1085 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1089 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1091 LDAP_LOG( OPERATION, ERR,
1092 "send_search_entry: conn %lu ber_printf failed\n",
1093 op->o_connid, 0, 0 );
1095 Debug( LDAP_DEBUG_ANY,
1096 "ber_printf failed\n", 0, 0, 0 );
1099 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1100 send_ldap_error( op, rs, LDAP_OTHER,
1101 "encoding values error" );
1108 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1110 LDAP_LOG( OPERATION, ERR,
1111 "send_search_entry: conn %lu ber_printf failed\n",
1112 op->o_connid, 0, 0 );
1114 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1117 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1118 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1126 * First, setup the computed attribute context that is
1127 * passed to all plugins.
1130 ctx.cac_pb = op->o_pb;
1131 ctx.cac_attrs = rs->sr_attrs;
1132 ctx.cac_attrsonly = op->ors_attrsonly;
1133 ctx.cac_userattrs = userattrs;
1134 ctx.cac_opattrs = opattrs;
1135 ctx.cac_acl_state = acl_state;
1136 ctx.cac_private = (void *)ber;
1139 * For each client requested attribute, call the plugins.
1141 if ( rs->sr_attrs != NULL ) {
1142 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1143 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1144 rs->sr_entry, slapi_x_compute_output_ber );
1151 * Technically we shouldn't be returning operational attributes
1152 * when the user requested only user attributes. We'll let the
1153 * plugin decide whether to be naughty or not.
1155 rc = compute_evaluator( &ctx, "*",
1156 rs->sr_entry, slapi_x_compute_output_ber );
1159 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1160 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1164 #endif /* LDAP_SLAPI */
1168 sl_free( e_flags, op->o_tmpmemctx );
1173 rc = ber_printf( ber, /*{{*/ "}N}" );
1175 if( rc != -1 && rs->sr_ctrls != NULL ) {
1176 rc = send_ldap_controls( ber, rs->sr_ctrls );
1180 #ifdef LDAP_CONNECTIONLESS
1181 if( op->o_conn && op->o_conn->c_is_udp ) {
1182 if ( op->o_protocol != LDAP_VERSION2 ) {
1183 rc = ber_printf( ber, /*{*/ "N}" );
1187 if ( op->o_res_ber == NULL ) {
1188 rc = ber_printf( ber, /*{*/ "N}" );
1194 LDAP_LOG( OPERATION, ERR,
1195 "send_search_entry: conn %lu ber_printf failed\n",
1196 op->o_connid, 0, 0 );
1198 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1201 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1202 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1207 if ( op->o_res_ber == NULL ) {
1208 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1209 ber_free_buf( ber );
1213 LDAP_LOG( OPERATION, ERR,
1214 "send_search_entry: conn %lu ber write failed.\n",
1215 op->o_connid, 0, 0 );
1217 Debug( LDAP_DEBUG_ANY,
1218 "send_search_entry: ber write failed\n",
1227 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1228 num_bytes_sent += bytes;
1231 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1234 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1235 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1238 LDAP_LOG( OPERATION, ENTRY,
1239 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1241 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1247 /* FIXME: I think rs->sr_type should be explicitly set to
1248 * REP_SEARCH here. That's what it was when we entered this
1249 * function. send_ldap_error may have changed it, but we
1250 * should set it back so that the cleanup functions know
1251 * what they're doing.
1253 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1255 && (rs->sr_flags & REP_ENTRY_MUSTBEFREED) )
1257 entry_free( rs->sr_entry );
1258 rs->sr_entry = NULL;
1259 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1262 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1264 if (op->o_callback) {
1265 slap_callback *sc = op->o_callback;
1266 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
1267 if ( op->o_callback->sc_cleanup ) {
1268 op->o_callback->sc_cleanup( op, rs );
1271 op->o_callback = sc;
1277 slap_send_search_reference( Operation *op, SlapReply *rs )
1279 BerElementBuffer berbuf;
1280 BerElement *ber = (BerElement *) &berbuf;
1284 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1285 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1287 rs->sr_type = REP_SEARCHREF;
1288 if (op->o_callback) {
1289 slap_callback *sc = op->o_callback;
1290 for ( ; op->o_callback; ) {
1291 if ( op->o_callback->sc_response ) {
1292 rc = op->o_callback->sc_response( op, rs );
1293 if ( rc != SLAP_CB_CONTINUE ) break;
1295 op->o_callback = op->o_callback->sc_next;
1297 op->o_callback = sc;
1298 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1302 LDAP_LOG( OPERATION, ENTRY,
1303 "send_search_reference: conn %lu dn=\"%s\"\n",
1305 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1307 Debug( LDAP_DEBUG_TRACE,
1308 "=> send_search_reference: dn=\"%s\"\n",
1309 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1312 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1313 ad_entry, NULL, ACL_READ, NULL ) )
1316 LDAP_LOG( ACL, INFO,
1317 "send_search_reference: conn %lu "
1318 "access to entry %s not allowed\n",
1319 op->o_connid, rs->sr_entry->e_dn, 0 );
1321 Debug( LDAP_DEBUG_ACL,
1322 "send_search_reference: access to entry not allowed\n",
1329 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1330 ad_ref, NULL, ACL_READ, NULL ) )
1333 LDAP_LOG( ACL, INFO,
1334 "send_search_reference: conn %lu access "
1335 "to reference not allowed.\n", op->o_connid, 0, 0 );
1337 Debug( LDAP_DEBUG_ACL,
1338 "send_search_reference: access "
1339 "to reference not allowed\n",
1346 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1347 if( op->o_domain_scope ) {
1349 LDAP_LOG( OPERATION, ERR,
1350 "send_search_reference: conn %lu domainScope control in (%s).\n",
1351 op->o_connid, rs->sr_entry->e_dn, 0 );
1353 Debug( LDAP_DEBUG_ANY,
1354 "send_search_reference: domainScope control in (%s)\n",
1355 rs->sr_entry->e_dn, 0, 0 );
1362 if( rs->sr_ref == NULL ) {
1364 LDAP_LOG( OPERATION, ERR,
1365 "send_search_reference: conn %lu null ref in (%s).\n",
1366 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1368 Debug( LDAP_DEBUG_ANY,
1369 "send_search_reference: null ref in (%s)\n",
1370 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1376 if( op->o_protocol < LDAP_VERSION3 ) {
1378 /* save the references for the result */
1379 if( rs->sr_ref[0].bv_val != NULL ) {
1380 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1386 #ifdef LDAP_CONNECTIONLESS
1387 if( op->o_conn && op->o_conn->c_is_udp ) {
1388 ber = op->o_res_ber;
1392 ber_init_w_nullc( ber, LBER_USE_DER );
1393 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1396 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1397 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1399 if( rc != -1 && rs->sr_ctrls != NULL ) {
1400 rc = send_ldap_controls( ber, rs->sr_ctrls );
1404 rc = ber_printf( ber, /*"{"*/ "N}" );
1409 LDAP_LOG( OPERATION, ERR,
1410 "send_search_reference: conn %lu "
1411 "ber_printf failed.\n", op->o_connid, 0, 0 );
1413 Debug( LDAP_DEBUG_ANY,
1414 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1417 #ifdef LDAP_CONNECTIONLESS
1418 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1420 ber_free_buf( ber );
1421 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1425 #ifdef LDAP_CONNECTIONLESS
1426 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1428 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1429 ber_free_buf( ber );
1431 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1432 num_bytes_sent += bytes;
1435 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1436 #ifdef LDAP_CONNECTIONLESS
1440 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1441 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1444 LDAP_LOG( OPERATION, ENTRY,
1445 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1447 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1451 if (op->o_callback) {
1452 slap_callback *sc = op->o_callback;
1453 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
1454 if ( op->o_callback->sc_cleanup ) {
1455 op->o_callback->sc_cleanup( op, rs );
1458 op->o_callback = sc;
1474 *code = LDAP_SUCCESS;
1478 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1480 LDAP_LOG( OPERATION, INFO,
1481 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1483 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1491 while ( (s = strchr( s, '\n' )) != NULL ) {
1496 if ( (c = strchr( s, ':' )) != NULL ) {
1500 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1504 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1508 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1514 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1516 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1527 int slap_read_controls(
1531 const struct berval *oid,
1532 LDAPControl **ctrl )
1536 BerElementBuffer berbuf;
1537 BerElement *ber = (BerElement *) &berbuf;
1543 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1544 oid->bv_val, e->e_dn, 0 );
1546 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1547 oid->bv_val, e->e_dn, 0 );
1551 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1552 op->o_preread_attrs : op->o_postread_attrs;
1554 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1555 bv.bv_len = siz + len;
1556 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1558 ber_init2( ber, &bv, LBER_USE_DER );
1559 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1561 /* create new operation */
1564 myop.o_res_ber = ber;
1566 rc = slap_send_search_entry( &myop, rs );
1569 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1571 if( rc == LBER_ERROR ) return LDAP_OTHER;
1573 c.ldctl_oid = oid->bv_val;
1574 c.ldctl_iscritical = 0;
1576 *ctrl = sl_calloc( 1, sizeof(LDAPControl), NULL );
1578 return LDAP_SUCCESS;