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, ( rs->sr_matched != NULL ) ? (void *)ch_strdup( rs->sr_matched ) : NULL );
376 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, ( rs->sr_text != NULL ) ? (void *)ch_strdup( rs->sr_text ) : NULL );
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 : "", rs->sr_text ? rs->sr_text : "" );
454 Debug( LDAP_DEBUG_ARGS,
455 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
456 rs->sr_err, rs->sr_matched ? rs->sr_matched : "", rs->sr_text ? rs->sr_text : "" );
462 LDAP_LOG( OPERATION, ARGS,
463 "send_ldap_result: referral=\"%s\"\n",
464 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
466 Debug( LDAP_DEBUG_ARGS,
467 "send_ldap_result: referral=\"%s\"\n",
468 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
473 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
475 if ( rs->sr_err == LDAP_REFERRAL ) {
476 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
477 if( op->o_domain_scope ) {
481 if( rs->sr_ref == NULL ) {
482 rs->sr_err = LDAP_NO_SUCH_OBJECT;
483 } else if ( op->o_protocol < LDAP_VERSION3 ) {
484 rs->sr_err = LDAP_PARTIAL_RESULTS;
488 if ( op->o_protocol < LDAP_VERSION3 ) {
489 tmp = v2ref( rs->sr_ref, rs->sr_text );
494 rs->sr_tag = req2res( op->o_tag );
495 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
497 send_ldap_response( op, rs );
499 if ( op->o_tag == LDAP_REQ_SEARCH ) {
501 snprintf( nbuf, sizeof nbuf, "%d nentries=%d", rs->sr_err, rs->sr_nentries );
503 Statslog( LDAP_DEBUG_STATS,
504 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
505 op->o_connid, op->o_opid, rs->sr_tag, nbuf, rs->sr_text ? rs->sr_text : "" );
507 Statslog( LDAP_DEBUG_STATS,
508 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
509 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
520 send_ldap_sasl( Operation *op, SlapReply *rs )
522 rs->sr_type = REP_SASL;
524 LDAP_LOG( OPERATION, ENTRY,
525 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
526 op->o_connid, rs->sr_err, rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
528 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
529 rs->sr_err, rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
532 rs->sr_tag = req2res( op->o_tag );
533 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
535 send_ldap_response( op, rs );
539 slap_send_ldap_extended( Operation *op, SlapReply *rs )
541 rs->sr_type = REP_EXTENDED;
544 LDAP_LOG( OPERATION, ENTRY,
545 "send_ldap_extended: err=%d oid=%s len=%ld\n",
546 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
547 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
549 Debug( LDAP_DEBUG_TRACE,
550 "send_ldap_extended: err=%d oid=%s len=%ld\n",
552 rs->sr_rspoid ? rs->sr_rspoid : "",
553 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
556 rs->sr_tag = req2res( op->o_tag );
557 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
559 send_ldap_response( op, rs );
563 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
565 rs->sr_type = REP_INTERMEDIATE;
567 LDAP_LOG( OPERATION, ENTRY,
568 "send_ldap_intermediate: 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_intermediate: 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 );
578 rs->sr_tag = LDAP_RES_INTERMEDIATE;
579 rs->sr_msgid = op->o_msgid;
580 send_ldap_response( op, rs );
584 slap_send_search_entry( Operation *op, SlapReply *rs )
586 char berbuf[LBER_ELEMENT_SIZEOF];
587 BerElement *ber = (BerElement *)berbuf;
589 int i, j, rc=-1, bytes;
593 AccessControlState acl_state = ACL_STATE_INIT;
595 /* Support for computed attribute plugins */
596 computed_attr_context ctx;
601 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
603 /* a_flags: array of flags telling if the i-th element will be
604 * returned or filtered out
605 * e_flags: array of a_flags
607 char **e_flags = NULL;
609 rs->sr_type = REP_SEARCH;
610 if (op->o_callback && op->o_callback->sc_response) {
611 rc = op->o_callback->sc_response( op, rs );
612 if ( rc != SLAP_CB_CONTINUE ) return rc;
616 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
617 op->o_connid, rs->sr_entry->e_name.bv_val,
618 op->ors_attrsonly ? " (attrsOnly)" : "" );
620 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
621 rs->sr_entry->e_name.bv_val,
622 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
625 mark = sl_mark( op->o_tmpmemctx );
627 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
630 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
631 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
633 Debug( LDAP_DEBUG_ACL,
634 "send_search_entry: access to entry not allowed\n",
638 sl_release( mark, op->o_tmpmemctx );
642 edn = rs->sr_entry->e_nname.bv_val;
644 #ifdef LDAP_CONNECTIONLESS
645 if (op->o_conn && op->o_conn->c_is_udp) {
653 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
654 bv.bv_len = siz + len;
655 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
657 ber_init2( ber, &bv, LBER_USE_DER );
658 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
661 #ifdef LDAP_CONNECTIONLESS
662 if (op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
663 rc = ber_printf(ber, "t{O{" /*}}*/,
664 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name);
668 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
669 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
674 LDAP_LOG( OPERATION, ERR,
675 "send_search_entry: conn %lu ber_printf failed\n",
676 op->o_connid, 0, 0 );
678 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
681 #ifdef LDAP_CONNECTIONLESS
682 if (!op->o_conn || op->o_conn->c_is_udp == 0)
685 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
689 /* check for special all user attributes ("*") type */
690 userattrs = ( rs->sr_attrs == NULL ) ? 1
691 : an_find( rs->sr_attrs, &AllUser );
693 /* check for special all operational attributes ("+") type */
694 opattrs = ( rs->sr_attrs == NULL ) ? 0
695 : an_find( rs->sr_attrs, &AllOper );
697 /* create an array of arrays of flags. Each flag corresponds
698 * to particular value of attribute and equals 1 if value matches
699 * to ValuesReturnFilter or 0 if not
701 if ( op->o_vrFilter != NULL ) {
705 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
706 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
709 size = i * sizeof(char *) + k;
712 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
713 if( e_flags == NULL ) {
715 LDAP_LOG( OPERATION, ERR,
716 "send_search_entry: conn %lu sl_calloc failed\n",
717 op->o_connid ? op->o_connid : 0, 0, 0 );
719 Debug( LDAP_DEBUG_ANY,
720 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
724 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
727 a_flags = (char *)(e_flags + i);
728 memset( a_flags, 0, k );
729 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
730 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
731 e_flags[i] = a_flags;
735 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
738 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
739 "conn %lu matched values filtering failed\n",
740 op->o_connid ? op->o_connid : 0, 0, 0 );
742 Debug( LDAP_DEBUG_ANY,
743 "matched values filtering failed\n", 0, 0, 0 );
745 #ifdef LDAP_CONNECTIONLESS
746 if (!op->o_conn || op->o_conn->c_is_udp == 0)
752 send_ldap_error( op, rs, LDAP_OTHER,
753 "matched values filtering error" );
759 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
760 AttributeDescription *desc = a->a_desc;
762 if ( rs->sr_attrs == NULL ) {
763 /* all attrs request, skip operational attributes */
764 if( is_at_operational( desc->ad_type ) ) {
769 /* specific attrs requested */
770 if ( is_at_operational( desc->ad_type ) ) {
771 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
776 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
782 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
783 ACL_READ, &acl_state ) )
787 "send_search_entry: conn %lu access to attribute %s not "
788 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
790 Debug( LDAP_DEBUG_ACL, "acl: "
791 "access to attribute %s not allowed\n",
792 desc->ad_cname.bv_val, 0, 0 );
797 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
799 LDAP_LOG( OPERATION, ERR,
800 "send_search_entry: conn %lu ber_printf failed\n",
801 op->o_connid, 0, 0 );
803 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
806 #ifdef LDAP_CONNECTIONLESS
807 if (!op->o_conn || op->o_conn->c_is_udp == 0)
810 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
814 if ( ! op->ors_attrsonly ) {
815 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
816 if ( ! access_allowed( op, rs->sr_entry,
817 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
821 "send_search_entry: conn %lu "
822 "access to attribute %s, value %d not allowed\n",
823 op->o_connid, desc->ad_cname.bv_val, i );
825 Debug( LDAP_DEBUG_ACL,
826 "acl: access to attribute %s, "
827 "value %d not allowed\n",
828 desc->ad_cname.bv_val, i, 0 );
834 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
838 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
840 LDAP_LOG( OPERATION, ERR,
841 "send_search_entry: conn %lu "
842 "ber_printf failed.\n", op->o_connid, 0, 0 );
844 Debug( LDAP_DEBUG_ANY,
845 "ber_printf failed\n", 0, 0, 0 );
848 #ifdef LDAP_CONNECTIONLESS
849 if (!op->o_conn || op->o_conn->c_is_udp == 0)
852 send_ldap_error( op, rs, LDAP_OTHER,
853 "encoding values error" );
859 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
861 LDAP_LOG( OPERATION, ERR,
862 "send_search_entry: conn %lu ber_printf failed\n",
863 op->o_connid, 0, 0 );
865 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
868 #ifdef LDAP_CONNECTIONLESS
869 if (!op->o_conn || op->o_conn->c_is_udp == 0)
872 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
877 /* eventually will loop through generated operational attributes */
878 /* only have subschemaSubentry implemented */
879 aa = backend_operational( op, rs, opattrs );
881 if ( aa != NULL && op->o_vrFilter != NULL ) {
885 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
886 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
889 size = i * sizeof(char *) + k;
891 char *a_flags, **tmp;
894 * Reuse previous memory - we likely need less space
895 * for operational attributes
897 tmp = sl_realloc( e_flags, i * sizeof(char *) + k,
901 LDAP_LOG( OPERATION, ERR,
902 "send_search_entry: conn %lu "
904 "for matched values filtering\n",
907 Debug( LDAP_DEBUG_ANY,
908 "send_search_entry: conn %lu "
910 "for matched values filtering\n",
911 op->o_connid, 0, 0 );
915 send_ldap_error( op, rs, LDAP_OTHER,
916 "not enough memory for matched values filtering" );
920 a_flags = (char *)(e_flags + i);
921 memset( a_flags, 0, k );
922 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
923 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
924 e_flags[i] = a_flags;
927 rc = filter_matched_values(op, aa, &e_flags) ;
931 LDAP_LOG( OPERATION, ERR,
932 "send_search_entry: conn %lu "
933 "matched values filtering failed\n",
934 op->o_connid ? op->o_connid : 0, 0, 0);
936 Debug( LDAP_DEBUG_ANY,
937 "matched values filtering failed\n", 0, 0, 0 );
939 #ifdef LDAP_CONNECTIONLESS
940 if (!op->o_conn || op->o_conn->c_is_udp == 0)
946 send_ldap_error( op, rs, LDAP_OTHER,
947 "matched values filtering error" );
953 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
954 AttributeDescription *desc = a->a_desc;
956 if ( rs->sr_attrs == NULL ) {
957 /* all attrs request, skip operational attributes */
958 if( is_at_operational( desc->ad_type ) ) {
963 /* specific attrs requested */
964 if( is_at_operational( desc->ad_type ) ) {
965 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
969 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
975 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
976 ACL_READ, &acl_state ) )
980 "send_search_entry: conn %lu "
981 "access to attribute %s not allowed\n",
982 op->o_connid, desc->ad_cname.bv_val, 0 );
984 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
985 "not allowed\n", desc->ad_cname.bv_val, 0, 0 );
991 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
994 LDAP_LOG( OPERATION, ERR,
995 "send_search_entry: conn %lu "
996 "ber_printf failed\n", op->o_connid, 0, 0 );
998 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1001 #ifdef LDAP_CONNECTIONLESS
1002 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1005 ber_free_buf( ber );
1007 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
1013 if ( ! op->ors_attrsonly ) {
1014 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1015 if ( ! access_allowed( op, rs->sr_entry,
1016 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1019 LDAP_LOG( ACL, INFO,
1020 "send_search_entry: conn %lu "
1021 "access to %s, value %d not allowed\n",
1022 op->o_connid, desc->ad_cname.bv_val, i );
1024 Debug( LDAP_DEBUG_ACL,
1025 "send_search_entry: access to attribute %s, "
1026 "value %d not allowed\n",
1027 desc->ad_cname.bv_val, i, 0 );
1033 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1037 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1039 LDAP_LOG( OPERATION, ERR,
1040 "send_search_entry: conn %lu ber_printf failed\n",
1041 op->o_connid, 0, 0 );
1043 Debug( LDAP_DEBUG_ANY,
1044 "ber_printf failed\n", 0, 0, 0 );
1047 #ifdef LDAP_CONNECTIONLESS
1048 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1051 ber_free_buf( ber );
1053 send_ldap_error( op, rs, LDAP_OTHER,
1054 "encoding values error" );
1061 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1063 LDAP_LOG( OPERATION, ERR,
1064 "send_search_entry: conn %lu ber_printf failed\n",
1065 op->o_connid, 0, 0 );
1067 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1070 #ifdef LDAP_CONNECTIONLESS
1071 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1074 ber_free_buf( ber );
1076 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1085 * First, setup the computed attribute context that is
1086 * passed to all plugins.
1088 ctx.cac_pb = op->o_pb;
1089 ctx.cac_attrs = rs->sr_attrs;
1090 ctx.cac_attrsonly = op->ors_attrsonly;
1091 ctx.cac_userattrs = userattrs;
1092 ctx.cac_opattrs = opattrs;
1093 ctx.cac_acl_state = acl_state;
1094 ctx.cac_private = (void *)ber;
1097 * For each client requested attribute, call the plugins.
1099 if ( rs->sr_attrs != NULL ) {
1100 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1101 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1102 rs->sr_entry, slapi_x_compute_output_ber );
1109 * Technically we shouldn't be returning operational attributes
1110 * when the user requested only user attributes. We'll let the
1111 * plugin decide whether to be naughty or not.
1113 rc = compute_evaluator( &ctx, "*",
1114 rs->sr_entry, slapi_x_compute_output_ber );
1117 ber_free_buf( ber );
1118 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1121 #endif /* LDAP_SLAPI */
1125 sl_free( e_flags, op->o_tmpmemctx );
1130 rc = ber_printf( ber, /*{{*/ "}N}" );
1132 if( rc != -1 && rs->sr_ctrls != NULL ) {
1133 rc = send_ldap_controls( ber, rs->sr_ctrls );
1136 #ifdef LDAP_CONNECTIONLESS
1137 if( op->o_conn && op->o_conn->c_is_udp &&
1138 op->o_protocol == LDAP_VERSION2 )
1140 ; /* empty, skip following if */
1144 rc = ber_printf( ber, /*{*/ "N}" );
1149 LDAP_LOG( OPERATION, ERR,
1150 "send_search_entry: conn %lu ber_printf failed\n",
1151 op->o_connid, 0, 0 );
1153 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1156 #ifdef LDAP_CONNECTIONLESS
1157 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1160 ber_free_buf( ber );
1162 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1163 sl_release( mark, op->o_tmpmemctx );
1167 #ifdef LDAP_CONNECTIONLESS
1168 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1171 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1172 ber_free_buf( ber );
1176 LDAP_LOG( OPERATION, ERR,
1177 "send_search_entry: conn %lu ber write failed.\n",
1178 op->o_connid, 0, 0 );
1180 Debug( LDAP_DEBUG_ANY,
1181 "send_search_entry: ber write failed\n",
1185 sl_release( mark, op->o_tmpmemctx );
1190 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1191 num_bytes_sent += bytes;
1194 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1197 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1198 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1201 LDAP_LOG( OPERATION, ENTRY,
1202 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1204 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1210 sl_release( mark, op->o_tmpmemctx );
1211 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1216 slap_send_search_reference( Operation *op, SlapReply *rs )
1218 char berbuf[LBER_ELEMENT_SIZEOF];
1219 BerElement *ber = (BerElement *)berbuf;
1224 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1225 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1227 rs->sr_type = REP_SEARCHREF;
1228 if (op->o_callback && op->o_callback->sc_response) {
1229 rc = op->o_callback->sc_response( op, rs );
1230 if ( rc != SLAP_CB_CONTINUE ) return rc;
1233 mark = sl_mark( op->o_tmpmemctx );
1236 LDAP_LOG( OPERATION, ENTRY,
1237 "send_search_reference: conn %lu dn=\"%s\"\n",
1238 op->o_connid, rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1240 Debug( LDAP_DEBUG_TRACE,
1241 "=> send_search_reference: dn=\"%s\"\n",
1242 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1245 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1246 ad_entry, NULL, ACL_READ, NULL ) )
1249 LDAP_LOG( ACL, INFO,
1250 "send_search_reference: conn %lu "
1251 "access to entry %s not allowed\n",
1252 op->o_connid, rs->sr_entry->e_dn, 0 );
1254 Debug( LDAP_DEBUG_ACL,
1255 "send_search_reference: access to entry not allowed\n",
1262 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1263 ad_ref, NULL, ACL_READ, NULL ) )
1266 LDAP_LOG( ACL, INFO,
1267 "send_search_reference: conn %lu access "
1268 "to reference not allowed.\n", op->o_connid, 0, 0 );
1270 Debug( LDAP_DEBUG_ACL,
1271 "send_search_reference: access "
1272 "to reference not allowed\n",
1279 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1280 if( op->o_domain_scope ) {
1282 LDAP_LOG( OPERATION, ERR,
1283 "send_search_reference: conn %lu domainScope control in (%s).\n",
1284 op->o_connid, rs->sr_entry->e_dn, 0 );
1286 Debug( LDAP_DEBUG_ANY,
1287 "send_search_reference: domainScope control in (%s)\n",
1288 rs->sr_entry->e_dn, 0, 0 );
1295 if( rs->sr_ref == NULL ) {
1297 LDAP_LOG( OPERATION, ERR,
1298 "send_search_reference: conn %lu null ref in (%s).\n",
1299 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1301 Debug( LDAP_DEBUG_ANY,
1302 "send_search_reference: null ref in (%s)\n",
1303 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1309 if( op->o_protocol < LDAP_VERSION3 ) {
1310 /* save the references for the result */
1311 if( rs->sr_ref[0].bv_val != NULL ) {
1312 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1319 #ifdef LDAP_CONNECTIONLESS
1320 if (op->o_conn && op->o_conn->c_is_udp)
1321 ber = op->o_res_ber;
1325 ber_init_w_nullc( ber, LBER_USE_DER );
1326 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1329 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1330 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1332 if( rc != -1 && rs->sr_ctrls != NULL ) {
1333 rc = send_ldap_controls( ber, rs->sr_ctrls );
1337 rc = ber_printf( ber, /*"{"*/ "N}" );
1342 LDAP_LOG( OPERATION, ERR,
1343 "send_search_reference: conn %lu "
1344 "ber_printf failed.\n", op->o_connid, 0, 0 );
1346 Debug( LDAP_DEBUG_ANY,
1347 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1350 #ifdef LDAP_CONNECTIONLESS
1351 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1353 ber_free_buf( ber );
1354 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1358 #ifdef LDAP_CONNECTIONLESS
1359 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1361 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1362 ber_free_buf( ber );
1364 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1365 num_bytes_sent += bytes;
1368 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1369 #ifdef LDAP_CONNECTIONLESS
1373 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1374 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1377 LDAP_LOG( OPERATION, ENTRY,
1378 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1380 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1384 sl_release( mark, op->o_tmpmemctx );
1399 *code = LDAP_SUCCESS;
1403 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1405 LDAP_LOG( OPERATION, INFO,
1406 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1408 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1416 while ( (s = strchr( s, '\n' )) != NULL ) {
1421 if ( (c = strchr( s, ':' )) != NULL ) {
1425 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1429 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1433 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1439 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1441 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",