1 /* result.c - routines to send ldap results, errors, and referrals */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/string.h>
17 #include <ac/unistd.h>
25 static char *v2ref( BerVarray ref, const char *text )
27 size_t len = 0, i = 0;
32 return ch_strdup(text);
40 if (text[len-1] != '\n') {
45 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
48 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
50 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
61 strcpy( v2+len, "Referral:" );
62 len += sizeof("Referral:");
64 for( i=0; ref[i].bv_val != NULL; i++ ) {
65 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
68 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
70 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
75 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
77 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
86 static ber_tag_t req2res( ber_tag_t tag )
91 case LDAP_REQ_COMPARE:
92 case LDAP_REQ_EXTENDED:
99 tag = LDAP_RES_DELETE;
102 case LDAP_REQ_ABANDON:
103 case LDAP_REQ_UNBIND:
107 case LDAP_REQ_SEARCH:
108 tag = LDAP_RES_SEARCH_RESULT;
118 static long send_ldap_ber(
124 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
126 /* write only one pdu at a time - wait til it's our turn */
127 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
129 /* lock the connection */
130 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
137 if ( connection_state_closing( conn ) ) {
138 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
139 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
144 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
151 * we got an error. if it's ewouldblock, we need to
152 * wait on the socket being writable. otherwise, figure
153 * it's a hard error and return.
157 LDAP_LOG( OPERATION, ERR,
158 "send_ldap_ber: conn %lu ber_flush failed err=%d (%s)\n",
159 conn ? conn->c_connid : 0, err, sock_errstr(err) );
161 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
162 err, sock_errstr(err), 0 );
165 if ( err != EWOULDBLOCK && err != EAGAIN ) {
166 connection_closing( conn );
168 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
169 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
174 /* wait for socket to be write-ready */
175 conn->c_writewaiter = 1;
176 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
177 slapd_set_write( sd, 1 );
179 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
180 conn->c_writewaiter = 0;
183 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
184 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
190 send_ldap_controls( BerElement *ber, LDAPControl **c )
193 if( c == NULL ) return 0;
195 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
196 if( rc == -1 ) return rc;
198 for( ; *c != NULL; c++) {
199 rc = ber_printf( ber, "{s" /*}*/, (*c)->ldctl_oid );
201 if( (*c)->ldctl_iscritical ) {
202 rc = ber_printf( ber, "b",
203 (ber_int_t) (*c)->ldctl_iscritical ) ;
204 if( rc == -1 ) return rc;
207 if( (*c)->ldctl_value.bv_val != NULL ) {
208 rc = ber_printf( ber, "O", &((*c)->ldctl_value));
209 if( rc == -1 ) return rc;
212 rc = ber_printf( ber, /*{*/"N}" );
213 if( rc == -1 ) return rc;
216 rc = ber_printf( ber, /*{*/"N}" );
226 BerElementBuffer berbuf;
227 BerElement *ber = (BerElement *) &berbuf;
231 if (op->o_callback && op->o_callback->sc_response) {
232 rc = op->o_callback->sc_response( op, rs );
233 if ( rc != SLAP_CB_CONTINUE ) return;
236 #ifdef LDAP_CONNECTIONLESS
237 if (op->o_conn && op->o_conn->c_is_udp)
242 ber_init_w_nullc( ber, LBER_USE_DER );
243 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
247 LDAP_LOG( OPERATION, ENTRY,
248 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
249 rs->sr_msgid, rs->sr_tag, rs->sr_err );
251 Debug( LDAP_DEBUG_TRACE,
252 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
253 rs->sr_msgid, rs->sr_tag, rs->sr_err );
258 LDAP_LOG( OPERATION, ARGS,
259 "send_ldap_response: conn %lu ref=\"%s\"\n",
261 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
263 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
264 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
269 #ifdef LDAP_CONNECTIONLESS
270 if (op->o_conn && op->o_conn->c_is_udp &&
271 op->o_protocol == LDAP_VERSION2 )
273 rc = ber_printf( ber, "t{ess" /*"}}"*/,
274 rs->sr_tag, rs->sr_err,
275 rs->sr_matched == NULL ? "" : rs->sr_matched,
276 rs->sr_text == NULL ? "" : rs->sr_text );
279 if ( rs->sr_type == REP_INTERMEDIATE ) {
280 rc = ber_printf( ber, "{it{" /*"}}"*/,
281 rs->sr_msgid, rs->sr_tag );
284 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
285 rs->sr_msgid, rs->sr_tag, rs->sr_err,
286 rs->sr_matched == NULL ? "" : rs->sr_matched,
287 rs->sr_text == NULL ? "" : rs->sr_text );
291 if ( rs->sr_ref != NULL ) {
292 assert( rs->sr_err == LDAP_REFERRAL );
293 rc = ber_printf( ber, "t{W}",
294 LDAP_TAG_REFERRAL, rs->sr_ref );
296 assert( rs->sr_err != LDAP_REFERRAL );
300 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
301 rc = ber_printf( ber, "tO",
302 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
306 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
308 if ( rs->sr_rspoid != NULL ) {
309 rc = ber_printf( ber, "ts",
310 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
312 if( rc != -1 && rs->sr_rspdata != NULL ) {
313 rc = ber_printf( ber, "tO",
314 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
319 rc = ber_printf( ber, /*"{"*/ "N}" );
322 if( rc != -1 && rs->sr_ctrls != NULL ) {
323 rc = send_ldap_controls( ber, rs->sr_ctrls );
327 rc = ber_printf( ber, /*"{"*/ "N}" );
330 #ifdef LDAP_CONNECTIONLESS
331 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
332 rc = ber_printf( ber, /*"{"*/ "N}" );
338 LDAP_LOG( OPERATION, ERR,
339 "send_ldap_response: conn %lu ber_printf failed\n",
340 op->o_connid, 0, 0 );
342 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
345 #ifdef LDAP_CONNECTIONLESS
346 if (!op->o_conn || op->o_conn->c_is_udp == 0)
353 bytes = send_ldap_ber( op->o_conn, ber );
354 #ifdef LDAP_CONNECTIONLESS
355 if (!op->o_conn || op->o_conn->c_is_udp == 0)
361 LDAP_LOG( OPERATION, ERR,
362 "send_ldap_response: conn %lu ber write failed\n",
363 op->o_connid ? op->o_connid : 0, 0, 0 );
365 Debug( LDAP_DEBUG_ANY,
366 "send_ldap_response: ber write failed\n",
375 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
376 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
377 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
379 #endif /* LDAP_SLAPI */
381 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
382 num_bytes_sent += bytes;
384 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
390 send_ldap_disconnect( Operation *op, SlapReply *rs )
392 #define LDAP_UNSOLICITED_ERROR(e) \
393 ( (e) == LDAP_PROTOCOL_ERROR \
394 || (e) == LDAP_STRONG_AUTH_REQUIRED \
395 || (e) == LDAP_UNAVAILABLE )
397 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
399 rs->sr_type = REP_EXTENDED;
402 LDAP_LOG( OPERATION, ENTRY,
403 "send_ldap_disconnect: conn %lu %d:%s\n",
404 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
406 Debug( LDAP_DEBUG_TRACE,
407 "send_ldap_disconnect %d:%s\n",
408 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
412 if ( op->o_protocol < LDAP_VERSION3 ) {
413 rs->sr_rspoid = NULL;
414 rs->sr_tag = req2res( op->o_tag );
415 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
418 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
419 rs->sr_tag = LDAP_RES_EXTENDED;
423 send_ldap_response( op, rs );
425 Statslog( LDAP_DEBUG_STATS,
426 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
427 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
431 slap_send_ldap_result( Operation *op, SlapReply *rs )
434 const char *otext = rs->sr_text;
435 BerVarray oref = rs->sr_ref;
437 rs->sr_type = REP_RESULT;
439 assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
442 LDAP_LOG( OPERATION, ENTRY,
443 "send_ldap_result: conn %lu op=%lu p=%d\n",
444 op->o_connid, op->o_opid, op->o_protocol );
446 Debug( LDAP_DEBUG_TRACE,
447 "send_ldap_result: conn=%lu op=%lu p=%d\n",
448 op->o_connid, op->o_opid, op->o_protocol );
452 LDAP_LOG( OPERATION, ARGS,
453 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
454 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
455 rs->sr_text ? rs->sr_text : "" );
457 Debug( LDAP_DEBUG_ARGS,
458 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
459 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
460 rs->sr_text ? rs->sr_text : "" );
466 LDAP_LOG( OPERATION, ARGS,
467 "send_ldap_result: referral=\"%s\"\n",
468 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
470 Debug( LDAP_DEBUG_ARGS,
471 "send_ldap_result: referral=\"%s\"\n",
472 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
477 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
479 if ( rs->sr_err == LDAP_REFERRAL ) {
480 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
481 if( op->o_domain_scope ) {
485 if( rs->sr_ref == NULL ) {
486 rs->sr_err = LDAP_NO_SUCH_OBJECT;
487 } else if ( op->o_protocol < LDAP_VERSION3 ) {
488 rs->sr_err = LDAP_PARTIAL_RESULTS;
494 * Call pre-result plugins. To avoid infinite recursion plugins
495 * should just set SLAPI_RESULT_CODE rather than sending a
496 * result if they wish to change the result.
499 slapi_x_pblock_set_operation( op->o_pb, op );
500 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
501 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
502 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
504 (void) doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN, op->o_pb );
506 #endif /* LDAP_SLAPI */
508 if ( op->o_protocol < LDAP_VERSION3 ) {
509 tmp = v2ref( rs->sr_ref, rs->sr_text );
514 rs->sr_tag = req2res( op->o_tag );
515 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
517 send_ldap_response( op, rs );
519 if ( op->o_tag == LDAP_REQ_SEARCH ) {
521 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
522 rs->sr_err, rs->sr_nentries );
524 Statslog( LDAP_DEBUG_STATS,
525 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
526 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
527 rs->sr_text ? rs->sr_text : "" );
529 Statslog( LDAP_DEBUG_STATS,
530 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
531 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
532 rs->sr_text ? rs->sr_text : "" );
535 if( tmp != NULL ) ch_free(tmp);
541 send_ldap_sasl( Operation *op, SlapReply *rs )
543 rs->sr_type = REP_SASL;
545 LDAP_LOG( OPERATION, ENTRY,
546 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
547 op->o_connid, rs->sr_err,
548 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
550 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
552 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
555 rs->sr_tag = req2res( op->o_tag );
556 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
558 send_ldap_response( op, rs );
562 slap_send_ldap_extended( Operation *op, SlapReply *rs )
564 rs->sr_type = REP_EXTENDED;
567 LDAP_LOG( OPERATION, ENTRY,
568 "send_ldap_extended: err=%d oid=%s len=%ld\n",
569 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
570 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
572 Debug( LDAP_DEBUG_TRACE,
573 "send_ldap_extended: err=%d oid=%s len=%ld\n",
575 rs->sr_rspoid ? rs->sr_rspoid : "",
576 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
579 rs->sr_tag = req2res( op->o_tag );
580 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
582 send_ldap_response( op, rs );
586 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
588 rs->sr_type = REP_INTERMEDIATE;
590 LDAP_LOG( OPERATION, ENTRY,
591 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
592 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
593 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
595 Debug( LDAP_DEBUG_TRACE,
596 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
598 rs->sr_rspoid ? rs->sr_rspoid : "",
599 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
601 rs->sr_tag = LDAP_RES_INTERMEDIATE;
602 rs->sr_msgid = op->o_msgid;
603 send_ldap_response( op, rs );
607 slap_send_search_entry( Operation *op, SlapReply *rs )
609 BerElementBuffer berbuf;
610 BerElement *ber = (BerElement *) &berbuf;
612 int i, j, rc=-1, bytes;
616 AccessControlState acl_state = ACL_STATE_INIT;
618 /* Support for computed attribute plugins */
619 computed_attr_context ctx;
624 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
626 /* a_flags: array of flags telling if the i-th element will be
627 * returned or filtered out
628 * e_flags: array of a_flags
630 char **e_flags = NULL;
632 rs->sr_type = REP_SEARCH;
633 if (op->o_callback && op->o_callback->sc_response) {
634 rc = op->o_callback->sc_response( op, rs );
635 if ( rc != SLAP_CB_CONTINUE ) return rc;
639 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
640 op->o_connid, rs->sr_entry->e_name.bv_val,
641 op->ors_attrsonly ? " (attrsOnly)" : "" );
643 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
644 rs->sr_entry->e_name.bv_val,
645 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
648 mark = sl_mark( op->o_tmpmemctx );
650 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
653 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
654 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
656 Debug( LDAP_DEBUG_ACL,
657 "send_search_entry: access to entry not allowed\n",
661 sl_release( mark, op->o_tmpmemctx );
665 edn = rs->sr_entry->e_nname.bv_val;
667 if ( op->o_res_ber ) {
668 /* read back control or LDAP_CONNECTIONLESS */
674 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
675 bv.bv_len = siz + len;
676 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
678 ber_init2( ber, &bv, LBER_USE_DER );
679 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
682 #ifdef LDAP_CONNECTIONLESS
683 if ( op->o_conn && op->o_conn->c_is_udp ) {
685 if ( op->o_protocol == LDAP_VERSION2 ) {
686 rc = ber_printf(ber, "t{O{" /*}}*/,
687 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
689 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
690 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
694 if ( op->o_res_ber ) {
695 /* read back control */
696 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
698 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
699 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
704 LDAP_LOG( OPERATION, ERR,
705 "send_search_entry: conn %lu ber_printf failed\n",
706 op->o_connid, 0, 0 );
708 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
711 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
712 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
716 /* check for special all user attributes ("*") type */
717 userattrs = ( rs->sr_attrs == NULL ) ? 1
718 : an_find( rs->sr_attrs, &AllUser );
720 /* check for special all operational attributes ("+") type */
721 opattrs = ( rs->sr_attrs == NULL ) ? 0
722 : an_find( rs->sr_attrs, &AllOper );
724 /* create an array of arrays of flags. Each flag corresponds
725 * to particular value of attribute and equals 1 if value matches
726 * to ValuesReturnFilter or 0 if not
728 if ( op->o_vrFilter != NULL ) {
732 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
733 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
736 size = i * sizeof(char *) + k;
739 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
740 if( e_flags == NULL ) {
742 LDAP_LOG( OPERATION, ERR,
743 "send_search_entry: conn %lu sl_calloc failed\n",
744 op->o_connid ? op->o_connid : 0, 0, 0 );
746 Debug( LDAP_DEBUG_ANY,
747 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
751 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
754 a_flags = (char *)(e_flags + i);
755 memset( a_flags, 0, k );
756 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
757 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
758 e_flags[i] = a_flags;
762 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
765 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
766 "conn %lu matched values filtering failed\n",
767 op->o_connid ? op->o_connid : 0, 0, 0 );
769 Debug( LDAP_DEBUG_ANY,
770 "matched values filtering failed\n", 0, 0, 0 );
772 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
773 send_ldap_error( op, rs, LDAP_OTHER,
774 "matched values filtering error" );
780 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
781 AttributeDescription *desc = a->a_desc;
783 if ( rs->sr_attrs == NULL ) {
784 /* all attrs request, skip operational attributes */
785 if( is_at_operational( desc->ad_type ) ) {
790 /* specific attrs requested */
791 if ( is_at_operational( desc->ad_type ) ) {
792 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
797 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
803 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
804 ACL_READ, &acl_state ) )
808 "send_search_entry: conn %lu access to attribute %s not "
809 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
811 Debug( LDAP_DEBUG_ACL, "acl: "
812 "access to attribute %s not allowed\n",
813 desc->ad_cname.bv_val, 0, 0 );
818 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
820 LDAP_LOG( OPERATION, ERR,
821 "send_search_entry: conn %lu ber_printf failed\n",
822 op->o_connid, 0, 0 );
824 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
827 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
828 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
832 if ( ! op->ors_attrsonly ) {
833 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
834 if ( ! access_allowed( op, rs->sr_entry,
835 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
839 "send_search_entry: conn %lu "
840 "access to attribute %s, value %d not allowed\n",
841 op->o_connid, desc->ad_cname.bv_val, i );
843 Debug( LDAP_DEBUG_ACL,
844 "acl: access to attribute %s, "
845 "value %d not allowed\n",
846 desc->ad_cname.bv_val, i, 0 );
852 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
856 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
858 LDAP_LOG( OPERATION, ERR,
859 "send_search_entry: conn %lu "
860 "ber_printf failed.\n", op->o_connid, 0, 0 );
862 Debug( LDAP_DEBUG_ANY,
863 "ber_printf failed\n", 0, 0, 0 );
866 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
867 send_ldap_error( op, rs, LDAP_OTHER,
868 "encoding values error" );
874 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
876 LDAP_LOG( OPERATION, ERR,
877 "send_search_entry: conn %lu ber_printf failed\n",
878 op->o_connid, 0, 0 );
880 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
883 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
884 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
889 /* eventually will loop through generated operational attributes */
890 /* only have subschemaSubentry and numSubordinates are implemented */
891 aa = backend_operational( op, rs, opattrs );
893 if ( aa != NULL && op->o_vrFilter != NULL ) {
897 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
898 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
901 size = i * sizeof(char *) + k;
903 char *a_flags, **tmp;
906 * Reuse previous memory - we likely need less space
907 * for operational attributes
909 tmp = sl_realloc( e_flags, i * sizeof(char *) + k,
913 LDAP_LOG( OPERATION, ERR,
914 "send_search_entry: conn %lu "
916 "for matched values filtering\n",
919 Debug( LDAP_DEBUG_ANY,
920 "send_search_entry: conn %lu "
922 "for matched values filtering\n",
923 op->o_connid, 0, 0 );
925 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
926 send_ldap_error( op, rs, LDAP_OTHER,
927 "not enough memory for matched values filtering" );
931 a_flags = (char *)(e_flags + i);
932 memset( a_flags, 0, k );
933 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
934 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
935 e_flags[i] = a_flags;
938 rc = filter_matched_values(op, aa, &e_flags) ;
942 LDAP_LOG( OPERATION, ERR,
943 "send_search_entry: conn %lu "
944 "matched values filtering failed\n",
945 op->o_connid ? op->o_connid : 0, 0, 0);
947 Debug( LDAP_DEBUG_ANY,
948 "matched values filtering failed\n", 0, 0, 0 );
950 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
951 send_ldap_error( op, rs, LDAP_OTHER,
952 "matched values filtering error" );
958 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
959 AttributeDescription *desc = a->a_desc;
961 if ( rs->sr_attrs == NULL ) {
962 /* all attrs request, skip operational attributes */
963 if( is_at_operational( desc->ad_type ) ) {
968 /* specific attrs requested */
969 if( is_at_operational( desc->ad_type ) ) {
970 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
974 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
980 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
981 ACL_READ, &acl_state ) )
985 "send_search_entry: conn %lu "
986 "access to attribute %s not allowed\n",
987 op->o_connid, desc->ad_cname.bv_val, 0 );
989 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
990 "not allowed\n", desc->ad_cname.bv_val, 0, 0 );
996 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
999 LDAP_LOG( OPERATION, ERR,
1000 "send_search_entry: conn %lu "
1001 "ber_printf failed\n", op->o_connid, 0, 0 );
1003 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1006 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1007 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
1012 if ( ! op->ors_attrsonly ) {
1013 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1014 if ( ! access_allowed( op, rs->sr_entry,
1015 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1018 LDAP_LOG( ACL, INFO,
1019 "send_search_entry: conn %lu "
1020 "access to %s, value %d not allowed\n",
1021 op->o_connid, desc->ad_cname.bv_val, i );
1023 Debug( LDAP_DEBUG_ACL,
1024 "send_search_entry: access to attribute %s, "
1025 "value %d not allowed\n",
1026 desc->ad_cname.bv_val, i, 0 );
1032 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1036 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1038 LDAP_LOG( OPERATION, ERR,
1039 "send_search_entry: conn %lu ber_printf failed\n",
1040 op->o_connid, 0, 0 );
1042 Debug( LDAP_DEBUG_ANY,
1043 "ber_printf failed\n", 0, 0, 0 );
1046 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1047 send_ldap_error( op, rs, LDAP_OTHER,
1048 "encoding values error" );
1055 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1057 LDAP_LOG( OPERATION, ERR,
1058 "send_search_entry: conn %lu ber_printf failed\n",
1059 op->o_connid, 0, 0 );
1061 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1064 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1065 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1073 * First, setup the computed attribute context that is
1074 * passed to all plugins.
1077 ctx.cac_pb = op->o_pb;
1078 ctx.cac_attrs = rs->sr_attrs;
1079 ctx.cac_attrsonly = op->ors_attrsonly;
1080 ctx.cac_userattrs = userattrs;
1081 ctx.cac_opattrs = opattrs;
1082 ctx.cac_acl_state = acl_state;
1083 ctx.cac_private = (void *)ber;
1086 * For each client requested attribute, call the plugins.
1088 if ( rs->sr_attrs != NULL ) {
1089 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1090 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1091 rs->sr_entry, slapi_x_compute_output_ber );
1098 * Technically we shouldn't be returning operational attributes
1099 * when the user requested only user attributes. We'll let the
1100 * plugin decide whether to be naughty or not.
1102 rc = compute_evaluator( &ctx, "*",
1103 rs->sr_entry, slapi_x_compute_output_ber );
1106 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1107 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1111 #endif /* LDAP_SLAPI */
1115 sl_free( e_flags, op->o_tmpmemctx );
1120 rc = ber_printf( ber, /*{{*/ "}N}" );
1122 if( rc != -1 && rs->sr_ctrls != NULL ) {
1123 rc = send_ldap_controls( ber, rs->sr_ctrls );
1127 #ifdef LDAP_CONNECTIONLESS
1128 if( op->o_conn && op->o_conn->c_is_udp ) {
1129 if ( op->o_protocol != LDAP_VERSION2 ) {
1130 rc = ber_printf( ber, /*{*/ "N}" );
1134 if ( op->o_res_ber == NULL ) {
1135 rc = ber_printf( ber, /*{*/ "N}" );
1141 LDAP_LOG( OPERATION, ERR,
1142 "send_search_entry: conn %lu ber_printf failed\n",
1143 op->o_connid, 0, 0 );
1145 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1148 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1149 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1150 sl_release( mark, op->o_tmpmemctx );
1154 if ( op->o_res_ber == NULL ) {
1155 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1156 ber_free_buf( ber );
1160 LDAP_LOG( OPERATION, ERR,
1161 "send_search_entry: conn %lu ber write failed.\n",
1162 op->o_connid, 0, 0 );
1164 Debug( LDAP_DEBUG_ANY,
1165 "send_search_entry: ber write failed\n",
1169 sl_release( mark, op->o_tmpmemctx );
1174 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1175 num_bytes_sent += bytes;
1178 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1181 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1182 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1185 LDAP_LOG( OPERATION, ENTRY,
1186 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1188 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1194 sl_release( mark, op->o_tmpmemctx );
1195 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1200 slap_send_search_reference( Operation *op, SlapReply *rs )
1202 BerElementBuffer berbuf;
1203 BerElement *ber = (BerElement *) &berbuf;
1208 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1209 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1211 rs->sr_type = REP_SEARCHREF;
1212 if (op->o_callback && op->o_callback->sc_response) {
1213 rc = op->o_callback->sc_response( op, rs );
1214 if ( rc != SLAP_CB_CONTINUE ) return rc;
1217 mark = sl_mark( op->o_tmpmemctx );
1220 LDAP_LOG( OPERATION, ENTRY,
1221 "send_search_reference: conn %lu dn=\"%s\"\n",
1223 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1225 Debug( LDAP_DEBUG_TRACE,
1226 "=> send_search_reference: dn=\"%s\"\n",
1227 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1230 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1231 ad_entry, NULL, ACL_READ, NULL ) )
1234 LDAP_LOG( ACL, INFO,
1235 "send_search_reference: conn %lu "
1236 "access to entry %s not allowed\n",
1237 op->o_connid, rs->sr_entry->e_dn, 0 );
1239 Debug( LDAP_DEBUG_ACL,
1240 "send_search_reference: access to entry not allowed\n",
1247 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1248 ad_ref, NULL, ACL_READ, NULL ) )
1251 LDAP_LOG( ACL, INFO,
1252 "send_search_reference: conn %lu access "
1253 "to reference not allowed.\n", op->o_connid, 0, 0 );
1255 Debug( LDAP_DEBUG_ACL,
1256 "send_search_reference: access "
1257 "to reference not allowed\n",
1264 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1265 if( op->o_domain_scope ) {
1267 LDAP_LOG( OPERATION, ERR,
1268 "send_search_reference: conn %lu domainScope control in (%s).\n",
1269 op->o_connid, rs->sr_entry->e_dn, 0 );
1271 Debug( LDAP_DEBUG_ANY,
1272 "send_search_reference: domainScope control in (%s)\n",
1273 rs->sr_entry->e_dn, 0, 0 );
1280 if( rs->sr_ref == NULL ) {
1282 LDAP_LOG( OPERATION, ERR,
1283 "send_search_reference: conn %lu null ref in (%s).\n",
1284 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1286 Debug( LDAP_DEBUG_ANY,
1287 "send_search_reference: null ref in (%s)\n",
1288 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1294 if( op->o_protocol < LDAP_VERSION3 ) {
1295 /* save the references for the result */
1296 if( rs->sr_ref[0].bv_val != NULL ) {
1297 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1304 #ifdef LDAP_CONNECTIONLESS
1305 if( op->o_conn && op->o_conn->c_is_udp ) {
1306 ber = op->o_res_ber;
1310 ber_init_w_nullc( ber, LBER_USE_DER );
1311 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1314 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1315 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1317 if( rc != -1 && rs->sr_ctrls != NULL ) {
1318 rc = send_ldap_controls( ber, rs->sr_ctrls );
1322 rc = ber_printf( ber, /*"{"*/ "N}" );
1327 LDAP_LOG( OPERATION, ERR,
1328 "send_search_reference: conn %lu "
1329 "ber_printf failed.\n", op->o_connid, 0, 0 );
1331 Debug( LDAP_DEBUG_ANY,
1332 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1335 #ifdef LDAP_CONNECTIONLESS
1336 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1338 ber_free_buf( ber );
1339 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1343 #ifdef LDAP_CONNECTIONLESS
1344 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1346 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1347 ber_free_buf( ber );
1349 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1350 num_bytes_sent += bytes;
1353 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1354 #ifdef LDAP_CONNECTIONLESS
1358 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1359 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1362 LDAP_LOG( OPERATION, ENTRY,
1363 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1365 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1369 sl_release( mark, op->o_tmpmemctx );
1384 *code = LDAP_SUCCESS;
1388 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1390 LDAP_LOG( OPERATION, INFO,
1391 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1393 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1401 while ( (s = strchr( s, '\n' )) != NULL ) {
1406 if ( (c = strchr( s, ':' )) != NULL ) {
1410 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1414 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1418 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1424 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1426 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1437 int slap_read_controls(
1441 const struct berval *oid,
1442 LDAPControl **ctrl )
1446 BerElementBuffer berbuf;
1447 BerElement *ber = (BerElement *) &berbuf;
1453 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1454 oid->bv_val, e->e_dn, 0 );
1456 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1457 oid->bv_val, e->e_dn, 0 );
1461 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1462 op->o_preread_attrs : op->o_postread_attrs;
1464 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1465 bv.bv_len = siz + len;
1466 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1468 ber_init2( ber, &bv, LBER_USE_DER );
1469 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1471 /* create new operation */
1474 myop.o_res_ber = ber;
1476 rc = slap_send_search_entry( &myop, rs );
1479 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1481 if( rc == LBER_ERROR ) return LDAP_OTHER;
1483 c.ldctl_oid = oid->bv_val;
1484 c.ldctl_iscritical = 0;
1486 *ctrl = sl_calloc( 1, sizeof(LDAPControl), NULL );
1488 return LDAP_SUCCESS;