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 char berbuf[LBER_ELEMENT_SIZEOF];
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",
374 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
375 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
376 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
377 #endif /* LDAP_SLAPI */
379 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
380 num_bytes_sent += bytes;
382 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
388 send_ldap_disconnect( Operation *op, SlapReply *rs )
390 #define LDAP_UNSOLICITED_ERROR(e) \
391 ( (e) == LDAP_PROTOCOL_ERROR \
392 || (e) == LDAP_STRONG_AUTH_REQUIRED \
393 || (e) == LDAP_UNAVAILABLE )
395 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
397 rs->sr_type = REP_EXTENDED;
400 LDAP_LOG( OPERATION, ENTRY,
401 "send_ldap_disconnect: conn %lu %d:%s\n",
402 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
404 Debug( LDAP_DEBUG_TRACE,
405 "send_ldap_disconnect %d:%s\n",
406 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
410 if ( op->o_protocol < LDAP_VERSION3 ) {
411 rs->sr_rspoid = NULL;
412 rs->sr_tag = req2res( op->o_tag );
413 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
416 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
417 rs->sr_tag = LDAP_RES_EXTENDED;
421 send_ldap_response( op, rs );
423 Statslog( LDAP_DEBUG_STATS,
424 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
425 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
429 slap_send_ldap_result( Operation *op, SlapReply *rs )
432 const char *otext = rs->sr_text;
433 BerVarray oref = rs->sr_ref;
435 rs->sr_type = REP_RESULT;
437 assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
440 LDAP_LOG( OPERATION, ENTRY,
441 "send_ldap_result: conn %lu op=%lu p=%d\n",
442 op->o_connid, op->o_opid, op->o_protocol );
444 Debug( LDAP_DEBUG_TRACE,
445 "send_ldap_result: conn=%lu op=%lu p=%d\n",
446 op->o_connid, op->o_opid, op->o_protocol );
450 LDAP_LOG( OPERATION, ARGS,
451 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
452 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
453 rs->sr_text ? rs->sr_text : "" );
455 Debug( LDAP_DEBUG_ARGS,
456 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
457 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
458 rs->sr_text ? rs->sr_text : "" );
464 LDAP_LOG( OPERATION, ARGS,
465 "send_ldap_result: referral=\"%s\"\n",
466 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
468 Debug( LDAP_DEBUG_ARGS,
469 "send_ldap_result: referral=\"%s\"\n",
470 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
475 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
477 if ( rs->sr_err == LDAP_REFERRAL ) {
478 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
479 if( op->o_domain_scope ) {
483 if( rs->sr_ref == NULL ) {
484 rs->sr_err = LDAP_NO_SUCH_OBJECT;
485 } else if ( op->o_protocol < LDAP_VERSION3 ) {
486 rs->sr_err = LDAP_PARTIAL_RESULTS;
492 * Call pre-result plugins. To avoid infinite recursion plugins
493 * should just set SLAPI_RESULT_CODE rather than sending a
494 * result if they wish to change the result.
496 slapi_x_pblock_set_operation( op->o_pb, op );
497 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
498 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
499 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
501 (void) doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN, op->o_pb );
502 #endif /* LDAP_SLAPI */
504 if ( op->o_protocol < LDAP_VERSION3 ) {
505 tmp = v2ref( rs->sr_ref, rs->sr_text );
510 rs->sr_tag = req2res( op->o_tag );
511 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
513 send_ldap_response( op, rs );
515 if ( op->o_tag == LDAP_REQ_SEARCH ) {
517 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
518 rs->sr_err, rs->sr_nentries );
520 Statslog( LDAP_DEBUG_STATS,
521 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
522 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
523 rs->sr_text ? rs->sr_text : "" );
525 Statslog( LDAP_DEBUG_STATS,
526 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
527 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
528 rs->sr_text ? rs->sr_text : "" );
531 if( tmp != NULL ) ch_free(tmp);
537 send_ldap_sasl( Operation *op, SlapReply *rs )
539 rs->sr_type = REP_SASL;
541 LDAP_LOG( OPERATION, ENTRY,
542 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
543 op->o_connid, rs->sr_err,
544 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
546 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
548 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
551 rs->sr_tag = req2res( op->o_tag );
552 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
554 send_ldap_response( op, rs );
558 slap_send_ldap_extended( Operation *op, SlapReply *rs )
560 rs->sr_type = REP_EXTENDED;
563 LDAP_LOG( OPERATION, ENTRY,
564 "send_ldap_extended: err=%d oid=%s len=%ld\n",
565 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
566 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
568 Debug( LDAP_DEBUG_TRACE,
569 "send_ldap_extended: err=%d oid=%s len=%ld\n",
571 rs->sr_rspoid ? rs->sr_rspoid : "",
572 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
575 rs->sr_tag = req2res( op->o_tag );
576 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
578 send_ldap_response( op, rs );
582 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
584 rs->sr_type = REP_INTERMEDIATE;
586 LDAP_LOG( OPERATION, ENTRY,
587 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
588 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
589 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
591 Debug( LDAP_DEBUG_TRACE,
592 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
594 rs->sr_rspoid ? rs->sr_rspoid : "",
595 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
597 rs->sr_tag = LDAP_RES_INTERMEDIATE;
598 rs->sr_msgid = op->o_msgid;
599 send_ldap_response( op, rs );
603 slap_send_search_entry( Operation *op, SlapReply *rs )
605 char berbuf[LBER_ELEMENT_SIZEOF];
606 BerElement *ber = (BerElement *)berbuf;
608 int i, j, rc=-1, bytes;
612 AccessControlState acl_state = ACL_STATE_INIT;
614 /* Support for computed attribute plugins */
615 computed_attr_context ctx;
620 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
622 /* a_flags: array of flags telling if the i-th element will be
623 * returned or filtered out
624 * e_flags: array of a_flags
626 char **e_flags = NULL;
628 rs->sr_type = REP_SEARCH;
629 if (op->o_callback && op->o_callback->sc_response) {
630 rc = op->o_callback->sc_response( op, rs );
631 if ( rc != SLAP_CB_CONTINUE ) return rc;
635 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
636 op->o_connid, rs->sr_entry->e_name.bv_val,
637 op->ors_attrsonly ? " (attrsOnly)" : "" );
639 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
640 rs->sr_entry->e_name.bv_val,
641 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
644 mark = sl_mark( op->o_tmpmemctx );
646 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
649 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
650 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
652 Debug( LDAP_DEBUG_ACL,
653 "send_search_entry: access to entry not allowed\n",
657 sl_release( mark, op->o_tmpmemctx );
661 edn = rs->sr_entry->e_nname.bv_val;
663 if ( op->o_res_ber ) {
664 /* read back control or LDAP_CONNECTIONLESS */
670 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
671 bv.bv_len = siz + len;
672 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
674 ber_init2( ber, &bv, LBER_USE_DER );
675 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
678 #ifdef LDAP_CONNECTIONLESS
679 if ( op->o_conn && op->o_conn->c_is_udp ) {
681 if ( op->o_protocol == LDAP_VERSION2 ) {
682 rc = ber_printf(ber, "t{O{" /*}}*/,
683 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
685 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
686 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
690 if ( op->o_res_ber ) {
691 /* read back control */
692 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
694 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
695 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
700 LDAP_LOG( OPERATION, ERR,
701 "send_search_entry: conn %lu ber_printf failed\n",
702 op->o_connid, 0, 0 );
704 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
707 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
708 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
712 /* check for special all user attributes ("*") type */
713 userattrs = ( rs->sr_attrs == NULL ) ? 1
714 : an_find( rs->sr_attrs, &AllUser );
716 /* check for special all operational attributes ("+") type */
717 opattrs = ( rs->sr_attrs == NULL ) ? 0
718 : an_find( rs->sr_attrs, &AllOper );
720 /* create an array of arrays of flags. Each flag corresponds
721 * to particular value of attribute and equals 1 if value matches
722 * to ValuesReturnFilter or 0 if not
724 if ( op->o_vrFilter != NULL ) {
728 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
729 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
732 size = i * sizeof(char *) + k;
735 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
736 if( e_flags == NULL ) {
738 LDAP_LOG( OPERATION, ERR,
739 "send_search_entry: conn %lu sl_calloc failed\n",
740 op->o_connid ? op->o_connid : 0, 0, 0 );
742 Debug( LDAP_DEBUG_ANY,
743 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
747 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
750 a_flags = (char *)(e_flags + i);
751 memset( a_flags, 0, k );
752 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
753 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
754 e_flags[i] = a_flags;
758 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
761 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
762 "conn %lu matched values filtering failed\n",
763 op->o_connid ? op->o_connid : 0, 0, 0 );
765 Debug( LDAP_DEBUG_ANY,
766 "matched values filtering failed\n", 0, 0, 0 );
768 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
769 send_ldap_error( op, rs, LDAP_OTHER,
770 "matched values filtering error" );
776 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
777 AttributeDescription *desc = a->a_desc;
779 if ( rs->sr_attrs == NULL ) {
780 /* all attrs request, skip operational attributes */
781 if( is_at_operational( desc->ad_type ) ) {
786 /* specific attrs requested */
787 if ( is_at_operational( desc->ad_type ) ) {
788 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
793 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
799 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
800 ACL_READ, &acl_state ) )
804 "send_search_entry: conn %lu access to attribute %s not "
805 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
807 Debug( LDAP_DEBUG_ACL, "acl: "
808 "access to attribute %s not allowed\n",
809 desc->ad_cname.bv_val, 0, 0 );
814 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
816 LDAP_LOG( OPERATION, ERR,
817 "send_search_entry: conn %lu ber_printf failed\n",
818 op->o_connid, 0, 0 );
820 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
823 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
824 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
828 if ( ! op->ors_attrsonly ) {
829 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
830 if ( ! access_allowed( op, rs->sr_entry,
831 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
835 "send_search_entry: conn %lu "
836 "access to attribute %s, value %d not allowed\n",
837 op->o_connid, desc->ad_cname.bv_val, i );
839 Debug( LDAP_DEBUG_ACL,
840 "acl: access to attribute %s, "
841 "value %d not allowed\n",
842 desc->ad_cname.bv_val, i, 0 );
848 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
852 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
854 LDAP_LOG( OPERATION, ERR,
855 "send_search_entry: conn %lu "
856 "ber_printf failed.\n", op->o_connid, 0, 0 );
858 Debug( LDAP_DEBUG_ANY,
859 "ber_printf failed\n", 0, 0, 0 );
862 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
863 send_ldap_error( op, rs, LDAP_OTHER,
864 "encoding values error" );
870 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
872 LDAP_LOG( OPERATION, ERR,
873 "send_search_entry: conn %lu ber_printf failed\n",
874 op->o_connid, 0, 0 );
876 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
879 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
880 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
885 /* eventually will loop through generated operational attributes */
886 /* only have subschemaSubentry and numSubordinates are implemented */
887 aa = backend_operational( op, rs, opattrs );
889 if ( aa != NULL && op->o_vrFilter != NULL ) {
893 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
894 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
897 size = i * sizeof(char *) + k;
899 char *a_flags, **tmp;
902 * Reuse previous memory - we likely need less space
903 * for operational attributes
905 tmp = sl_realloc( e_flags, i * sizeof(char *) + k,
909 LDAP_LOG( OPERATION, ERR,
910 "send_search_entry: conn %lu "
912 "for matched values filtering\n",
915 Debug( LDAP_DEBUG_ANY,
916 "send_search_entry: conn %lu "
918 "for matched values filtering\n",
919 op->o_connid, 0, 0 );
921 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
922 send_ldap_error( op, rs, LDAP_OTHER,
923 "not enough memory for matched values filtering" );
927 a_flags = (char *)(e_flags + i);
928 memset( a_flags, 0, k );
929 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
930 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
931 e_flags[i] = a_flags;
934 rc = filter_matched_values(op, aa, &e_flags) ;
938 LDAP_LOG( OPERATION, ERR,
939 "send_search_entry: conn %lu "
940 "matched values filtering failed\n",
941 op->o_connid ? op->o_connid : 0, 0, 0);
943 Debug( LDAP_DEBUG_ANY,
944 "matched values filtering failed\n", 0, 0, 0 );
946 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
947 send_ldap_error( op, rs, LDAP_OTHER,
948 "matched values filtering error" );
954 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
955 AttributeDescription *desc = a->a_desc;
957 if ( rs->sr_attrs == NULL ) {
958 /* all attrs request, skip operational attributes */
959 if( is_at_operational( desc->ad_type ) ) {
964 /* specific attrs requested */
965 if( is_at_operational( desc->ad_type ) ) {
966 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
970 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
976 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
977 ACL_READ, &acl_state ) )
981 "send_search_entry: conn %lu "
982 "access to attribute %s not allowed\n",
983 op->o_connid, desc->ad_cname.bv_val, 0 );
985 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
986 "not allowed\n", desc->ad_cname.bv_val, 0, 0 );
992 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
995 LDAP_LOG( OPERATION, ERR,
996 "send_search_entry: conn %lu "
997 "ber_printf failed\n", op->o_connid, 0, 0 );
999 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1002 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1003 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
1008 if ( ! op->ors_attrsonly ) {
1009 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1010 if ( ! access_allowed( op, rs->sr_entry,
1011 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1014 LDAP_LOG( ACL, INFO,
1015 "send_search_entry: conn %lu "
1016 "access to %s, value %d not allowed\n",
1017 op->o_connid, desc->ad_cname.bv_val, i );
1019 Debug( LDAP_DEBUG_ACL,
1020 "send_search_entry: access to attribute %s, "
1021 "value %d not allowed\n",
1022 desc->ad_cname.bv_val, i, 0 );
1028 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1032 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1034 LDAP_LOG( OPERATION, ERR,
1035 "send_search_entry: conn %lu ber_printf failed\n",
1036 op->o_connid, 0, 0 );
1038 Debug( LDAP_DEBUG_ANY,
1039 "ber_printf failed\n", 0, 0, 0 );
1042 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1043 send_ldap_error( op, rs, LDAP_OTHER,
1044 "encoding values error" );
1051 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1053 LDAP_LOG( OPERATION, ERR,
1054 "send_search_entry: conn %lu ber_printf failed\n",
1055 op->o_connid, 0, 0 );
1057 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1060 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1061 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1069 * First, setup the computed attribute context that is
1070 * passed to all plugins.
1072 ctx.cac_pb = op->o_pb;
1073 ctx.cac_attrs = rs->sr_attrs;
1074 ctx.cac_attrsonly = op->ors_attrsonly;
1075 ctx.cac_userattrs = userattrs;
1076 ctx.cac_opattrs = opattrs;
1077 ctx.cac_acl_state = acl_state;
1078 ctx.cac_private = (void *)ber;
1081 * For each client requested attribute, call the plugins.
1083 if ( rs->sr_attrs != NULL ) {
1084 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1085 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1086 rs->sr_entry, slapi_x_compute_output_ber );
1093 * Technically we shouldn't be returning operational attributes
1094 * when the user requested only user attributes. We'll let the
1095 * plugin decide whether to be naughty or not.
1097 rc = compute_evaluator( &ctx, "*",
1098 rs->sr_entry, slapi_x_compute_output_ber );
1101 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1102 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1105 #endif /* LDAP_SLAPI */
1109 sl_free( e_flags, op->o_tmpmemctx );
1114 rc = ber_printf( ber, /*{{*/ "}N}" );
1116 if( rc != -1 && rs->sr_ctrls != NULL ) {
1117 rc = send_ldap_controls( ber, rs->sr_ctrls );
1121 #ifdef LDAP_CONNECTIONLESS
1122 if( op->o_conn && op->o_conn->c_is_udp ) {
1123 if ( op->o_protocol != LDAP_VERSION2 ) {
1124 rc = ber_printf( ber, /*{*/ "N}" );
1128 if ( op->o_res_ber == NULL ) {
1129 rc = ber_printf( ber, /*{*/ "N}" );
1135 LDAP_LOG( OPERATION, ERR,
1136 "send_search_entry: conn %lu ber_printf failed\n",
1137 op->o_connid, 0, 0 );
1139 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1142 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1143 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1144 sl_release( mark, op->o_tmpmemctx );
1148 if ( op->o_res_ber == NULL ) {
1149 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1150 ber_free_buf( ber );
1154 LDAP_LOG( OPERATION, ERR,
1155 "send_search_entry: conn %lu ber write failed.\n",
1156 op->o_connid, 0, 0 );
1158 Debug( LDAP_DEBUG_ANY,
1159 "send_search_entry: ber write failed\n",
1163 sl_release( mark, op->o_tmpmemctx );
1168 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1169 num_bytes_sent += bytes;
1172 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1175 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1176 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1179 LDAP_LOG( OPERATION, ENTRY,
1180 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1182 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1188 sl_release( mark, op->o_tmpmemctx );
1189 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1194 slap_send_search_reference( Operation *op, SlapReply *rs )
1196 char berbuf[LBER_ELEMENT_SIZEOF];
1197 BerElement *ber = (BerElement *)berbuf;
1202 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1203 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1205 rs->sr_type = REP_SEARCHREF;
1206 if (op->o_callback && op->o_callback->sc_response) {
1207 rc = op->o_callback->sc_response( op, rs );
1208 if ( rc != SLAP_CB_CONTINUE ) return rc;
1211 mark = sl_mark( op->o_tmpmemctx );
1214 LDAP_LOG( OPERATION, ENTRY,
1215 "send_search_reference: conn %lu dn=\"%s\"\n",
1217 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1219 Debug( LDAP_DEBUG_TRACE,
1220 "=> send_search_reference: dn=\"%s\"\n",
1221 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1224 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1225 ad_entry, NULL, ACL_READ, NULL ) )
1228 LDAP_LOG( ACL, INFO,
1229 "send_search_reference: conn %lu "
1230 "access to entry %s not allowed\n",
1231 op->o_connid, rs->sr_entry->e_dn, 0 );
1233 Debug( LDAP_DEBUG_ACL,
1234 "send_search_reference: access to entry not allowed\n",
1241 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1242 ad_ref, NULL, ACL_READ, NULL ) )
1245 LDAP_LOG( ACL, INFO,
1246 "send_search_reference: conn %lu access "
1247 "to reference not allowed.\n", op->o_connid, 0, 0 );
1249 Debug( LDAP_DEBUG_ACL,
1250 "send_search_reference: access "
1251 "to reference not allowed\n",
1258 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1259 if( op->o_domain_scope ) {
1261 LDAP_LOG( OPERATION, ERR,
1262 "send_search_reference: conn %lu domainScope control in (%s).\n",
1263 op->o_connid, rs->sr_entry->e_dn, 0 );
1265 Debug( LDAP_DEBUG_ANY,
1266 "send_search_reference: domainScope control in (%s)\n",
1267 rs->sr_entry->e_dn, 0, 0 );
1274 if( rs->sr_ref == NULL ) {
1276 LDAP_LOG( OPERATION, ERR,
1277 "send_search_reference: conn %lu null ref in (%s).\n",
1278 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1280 Debug( LDAP_DEBUG_ANY,
1281 "send_search_reference: null ref in (%s)\n",
1282 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1288 if( op->o_protocol < LDAP_VERSION3 ) {
1289 /* save the references for the result */
1290 if( rs->sr_ref[0].bv_val != NULL ) {
1291 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1298 #ifdef LDAP_CONNECTIONLESS
1299 if( op->o_conn && op->o_conn->c_is_udp ) {
1300 ber = op->o_res_ber;
1304 ber_init_w_nullc( ber, LBER_USE_DER );
1305 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1308 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1309 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1311 if( rc != -1 && rs->sr_ctrls != NULL ) {
1312 rc = send_ldap_controls( ber, rs->sr_ctrls );
1316 rc = ber_printf( ber, /*"{"*/ "N}" );
1321 LDAP_LOG( OPERATION, ERR,
1322 "send_search_reference: conn %lu "
1323 "ber_printf failed.\n", op->o_connid, 0, 0 );
1325 Debug( LDAP_DEBUG_ANY,
1326 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1329 #ifdef LDAP_CONNECTIONLESS
1330 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1332 ber_free_buf( ber );
1333 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1337 #ifdef LDAP_CONNECTIONLESS
1338 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1340 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1341 ber_free_buf( ber );
1343 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1344 num_bytes_sent += bytes;
1347 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1348 #ifdef LDAP_CONNECTIONLESS
1352 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1353 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1356 LDAP_LOG( OPERATION, ENTRY,
1357 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1359 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1363 sl_release( mark, op->o_tmpmemctx );
1378 *code = LDAP_SUCCESS;
1382 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1384 LDAP_LOG( OPERATION, INFO,
1385 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1387 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1395 while ( (s = strchr( s, '\n' )) != NULL ) {
1400 if ( (c = strchr( s, ':' )) != NULL ) {
1404 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1408 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1412 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1418 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1420 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1431 int slap_read_controls(
1435 const struct berval *oid,
1436 LDAPControl **ctrl )
1440 char berbuf[LBER_ELEMENT_SIZEOF];
1441 BerElement *ber = (BerElement *) berbuf;
1447 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1448 oid->bv_val, e->e_dn, 0 );
1450 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1451 oid->bv_val, e->e_dn, 0 );
1455 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1456 op->o_preread_attrs : op->o_postread_attrs;
1458 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1459 bv.bv_len = siz + len;
1460 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1462 ber_init2( ber, &bv, LBER_USE_DER );
1463 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1465 /* create new operation */
1468 myop.o_res_ber = ber;
1470 rc = slap_send_search_entry( &myop, rs );
1473 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1475 if( rc == LBER_ERROR ) return LDAP_OTHER;
1477 c.ldctl_oid = oid->bv_val;
1478 c.ldctl_iscritical = 0;
1480 *ctrl = sl_calloc( 1, sizeof(LDAPControl), NULL );
1482 return LDAP_SUCCESS;