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>
22 static char *v2ref( BerVarray ref, const char *text )
24 size_t len = 0, i = 0;
29 return ch_strdup(text);
37 if (text[len-1] != '\n') {
42 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
45 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
47 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
58 strcpy( v2+len, "Referral:" );
59 len += sizeof("Referral:");
61 for( i=0; ref[i].bv_val != NULL; i++ ) {
62 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
65 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
67 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
72 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
74 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
83 static ber_tag_t req2res( ber_tag_t tag )
88 case LDAP_REQ_COMPARE:
89 case LDAP_REQ_EXTENDED:
96 tag = LDAP_RES_DELETE;
99 case LDAP_REQ_ABANDON:
100 case LDAP_REQ_UNBIND:
104 case LDAP_REQ_SEARCH:
105 tag = LDAP_RES_SEARCH_RESULT;
115 static long send_ldap_ber(
121 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
123 /* write only one pdu at a time - wait til it's our turn */
124 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
126 /* lock the connection */
127 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
134 if ( connection_state_closing( conn ) ) {
135 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
136 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
141 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
148 * we got an error. if it's ewouldblock, we need to
149 * wait on the socket being writable. otherwise, figure
150 * it's a hard error and return.
154 LDAP_LOG( OPERATION, ERR,
155 "send_ldap_ber: conn %lu ber_flush failed err=%d (%s)\n",
156 conn ? conn->c_connid : 0, err, sock_errstr(err) );
158 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
159 err, sock_errstr(err), 0 );
162 if ( err != EWOULDBLOCK && err != EAGAIN ) {
163 connection_closing( conn );
165 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
166 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
171 /* wait for socket to be write-ready */
172 conn->c_writewaiter = 1;
173 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
174 slapd_set_write( sd, 1 );
176 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
177 conn->c_writewaiter = 0;
180 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
181 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
187 send_ldap_controls( BerElement *ber, LDAPControl **c )
190 if( c == NULL ) return 0;
192 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
193 if( rc == -1 ) return rc;
195 for( ; *c != NULL; c++) {
196 rc = ber_printf( ber, "{s" /*}*/, (*c)->ldctl_oid );
198 if( (*c)->ldctl_iscritical ) {
199 rc = ber_printf( ber, "b",
200 (ber_int_t) (*c)->ldctl_iscritical ) ;
201 if( rc == -1 ) return rc;
204 if( (*c)->ldctl_value.bv_val != NULL ) {
205 rc = ber_printf( ber, "O", &((*c)->ldctl_value));
206 if( rc == -1 ) return rc;
209 rc = ber_printf( ber, /*{*/"N}" );
210 if( rc == -1 ) return rc;
213 rc = ber_printf( ber, /*{*/"N}" );
229 struct berval *resdata,
230 struct berval *sasldata,
234 char berbuf[LBER_ELEMENT_SIZEOF];
235 BerElement *ber = (BerElement *)berbuf;
239 if (op->o_callback && op->o_callback->sc_response) {
240 op->o_callback->sc_response( conn, op, tag, msgid, err, matched,
241 text, ref, resoid, resdata, sasldata, ctrls );
245 #ifdef LDAP_CONNECTIONLESS
251 ber_init_w_nullc( ber, LBER_USE_DER );
254 LDAP_LOG( OPERATION, ENTRY,
255 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
258 Debug( LDAP_DEBUG_TRACE,
259 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
265 LDAP_LOG( OPERATION, ARGS,
266 "send_ldap_response: conn %lu ref=\"%s\"\n",
267 conn ? conn->c_connid : 0,
268 ref[0].bv_val ? ref[0].bv_val : "NULL" , 0 );
270 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
271 ref[0].bv_val ? ref[0].bv_val : "NULL",
276 #ifdef LDAP_CONNECTIONLESS
277 if (conn->c_is_udp && conn->c_protocol == LDAP_VERSION2) {
278 rc = ber_printf( ber, "t{ess" /*"}}"*/,
280 matched == NULL ? "" : matched,
281 text == NULL ? "" : text );
285 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
287 matched == NULL ? "" : matched,
288 text == NULL ? "" : text );
293 assert( err == LDAP_REFERRAL );
294 rc = ber_printf( ber, "t{W}",
295 LDAP_TAG_REFERRAL, ref );
297 assert( err != LDAP_REFERRAL );
301 if( rc != -1 && sasldata != NULL ) {
302 rc = ber_printf( ber, "tO",
303 LDAP_TAG_SASL_RES_CREDS, sasldata );
306 if( rc != -1 && resoid != NULL ) {
307 rc = ber_printf( ber, "ts",
308 LDAP_TAG_EXOP_RES_OID, resoid );
311 if( rc != -1 && resdata != NULL ) {
312 rc = ber_printf( ber, "tO",
313 LDAP_TAG_EXOP_RES_VALUE, resdata );
317 rc = ber_printf( ber, /*"{"*/ "N}" );
320 if( rc != -1 && ctrls != NULL ) {
321 rc = send_ldap_controls( ber, ctrls );
325 rc = ber_printf( ber, /*"{"*/ "N}" );
330 LDAP_LOG( OPERATION, ERR,
331 "send_ldap_response: conn %lu ber_printf failed\n",
332 conn ? conn->c_connid : 0, 0, 0 );
334 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
337 #ifdef LDAP_CONNECTIONLESS
338 if (conn->c_is_udp == 0)
345 bytes = send_ldap_ber( conn, ber );
346 #ifdef LDAP_CONNECTIONLESS
347 if (conn->c_is_udp == 0)
353 LDAP_LOG( OPERATION, ERR,
354 "send_ldap_response: conn %lu ber write failed\n",
355 conn ? conn->c_connid : 0, 0, 0 );
357 Debug( LDAP_DEBUG_ANY,
358 "send_ldap_response: ber write failed\n",
366 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)err );
367 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, ( matched != NULL ) ? (void *)ch_strdup( matched ) : NULL );
368 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, ( text != NULL ) ? (void *)ch_strdup( text ) : NULL );
369 #endif /* LDAP_SLAPI */
371 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
372 num_bytes_sent += bytes;
374 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
380 send_ldap_disconnect(
391 #define LDAP_UNSOLICITED_ERROR(e) \
392 ( (e) == LDAP_PROTOCOL_ERROR \
393 || (e) == LDAP_STRONG_AUTH_REQUIRED \
394 || (e) == LDAP_UNAVAILABLE )
396 assert( LDAP_UNSOLICITED_ERROR( err ) );
399 LDAP_LOG( OPERATION, ENTRY,
400 "send_ldap_disconnect: conn %lu %d:%s\n",
401 conn ? conn->c_connid : 0, err, text ? text : "" );
403 Debug( LDAP_DEBUG_TRACE,
404 "send_ldap_disconnect %d:%s\n",
405 err, text ? text : "", NULL );
409 if ( op->o_protocol < LDAP_VERSION3 ) {
411 tag = req2res( op->o_tag );
412 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
415 reqoid = LDAP_NOTICE_DISCONNECT;
416 tag = LDAP_RES_EXTENDED;
420 send_ldap_response( conn, op, tag, msgid,
421 err, NULL, text, NULL,
422 reqoid, NULL, NULL, NULL );
424 Statslog( LDAP_DEBUG_STATS,
425 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
426 op->o_connid, op->o_opid, tag, err, text ? text : "" );
430 slap_send_ldap_result(
444 assert( !LDAP_API_ERROR( err ) );
447 LDAP_LOG( OPERATION, ENTRY,
448 "send_ldap_result: conn %lu op=%lu p=%d\n",
449 op->o_connid, op->o_opid, op->o_protocol );
451 Debug( LDAP_DEBUG_TRACE,
452 "send_ldap_result: conn=%lu op=%lu p=%d\n",
453 op->o_connid, op->o_opid, op->o_protocol );
457 LDAP_LOG( OPERATION, ARGS,
458 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
459 err, matched ? matched : "", text ? text : "" );
461 Debug( LDAP_DEBUG_ARGS,
462 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
463 err, matched ? matched : "", text ? text : "" );
469 LDAP_LOG( OPERATION, ARGS,
470 "send_ldap_result: referral=\"%s\"\n",
471 ref[0].bv_val ? ref[0].bv_val : "NULL", 0, 0 );
473 Debug( LDAP_DEBUG_ARGS,
474 "send_ldap_result: referral=\"%s\"\n",
475 ref[0].bv_val ? ref[0].bv_val : "NULL",
480 assert( err != LDAP_PARTIAL_RESULTS );
482 if ( err == LDAP_REFERRAL ) {
483 if( op->o_noreferrals ) {
487 err = LDAP_NO_SUCH_OBJECT;
488 } else if ( op->o_protocol < LDAP_VERSION3 ) {
489 err = LDAP_PARTIAL_RESULTS;
493 if ( op->o_protocol < LDAP_VERSION3 ) {
494 tmp = v2ref( ref, text );
499 tag = req2res( op->o_tag );
500 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
502 send_ldap_response( conn, op, tag, msgid,
503 err, matched, text, ref,
504 NULL, NULL, NULL, ctrls );
506 Statslog( LDAP_DEBUG_STATS,
507 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
508 op->o_connid, op->o_opid, tag, err, text ? text : "" );
531 LDAP_LOG( OPERATION, ENTRY,
532 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
533 op->o_connid, err, cred ? cred->bv_len : -1 );
535 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
536 err, cred ? (long) cred->bv_len : -1, NULL );
540 tag = req2res( op->o_tag );
541 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
543 send_ldap_response( conn, op, tag, msgid,
544 err, matched, text, ref,
545 NULL, NULL, cred, ctrls );
549 slap_send_ldap_extended(
557 struct berval *rspdata,
565 LDAP_LOG( OPERATION, ENTRY,
566 "send_ldap_extended: err=%d oid=%s len=%ld\n",
567 err, rspoid ? rspoid : "",
568 rspdata != NULL ? rspdata->bv_len : 0 );
570 Debug( LDAP_DEBUG_TRACE,
571 "send_ldap_extended: err=%d oid=%s len=%ld\n",
573 rspoid ? rspoid : "",
574 rspdata != NULL ? rspdata->bv_len : 0 );
577 tag = req2res( op->o_tag );
578 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
580 send_ldap_response( conn, op, tag, msgid,
581 err, matched, text, refs,
582 rspoid, rspdata, NULL, ctrls );
586 slap_send_ldap_intermediate_resp(
594 struct berval *rspdata,
595 LDAPControl **ctrls )
600 LDAP_LOG( OPERATION, ENTRY,
601 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
602 err, rspoid ? rspoid : "",
603 rspdata != NULL ? rspdata->bv_len : 0 );
605 Debug( LDAP_DEBUG_TRACE,
606 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
608 rspoid ? rspoid : "",
609 rspdata != NULL ? rspdata->bv_len : 0 );
611 tag = LDAP_RES_INTERMEDIATE_RESP;
612 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
613 send_ldap_response( conn, op, tag, msgid,
614 err, matched, text, refs,
615 rspoid, rspdata, NULL, ctrls );
619 slap_send_search_result(
634 assert( !LDAP_API_ERROR( err ) );
636 if (op->o_callback && op->o_callback->sc_sresult) {
637 op->o_callback->sc_sresult(conn, op, err, matched, text, refs,
643 LDAP_LOG( OPERATION, ENTRY,
644 "send_search_result: err=%d matched=\"%s\" text=\"%s\"\n",
645 err, matched ? matched : "", text ? text : "" );
647 Debug( LDAP_DEBUG_TRACE,
648 "send_search_result: err=%d matched=\"%s\" text=\"%s\"\n",
649 err, matched ? matched : "", text ? text : "" );
653 assert( err != LDAP_PARTIAL_RESULTS );
655 if( op->o_protocol < LDAP_VERSION3 ) {
656 /* send references in search results */
657 if( err == LDAP_REFERRAL ) {
658 err = LDAP_PARTIAL_RESULTS;
661 tmp = v2ref( refs, text );
666 /* don't send references in search results */
667 assert( refs == NULL );
670 if( err == LDAP_REFERRAL ) {
675 tag = req2res( op->o_tag );
676 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
678 send_ldap_response( conn, op, tag, msgid,
679 err, matched, text, refs,
680 NULL, NULL, NULL, ctrls );
684 snprintf( nbuf, sizeof nbuf, "%d nentries=%d", err, nentries );
686 Statslog( LDAP_DEBUG_STATS,
687 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
688 op->o_connid, op->o_opid, tag, nbuf, text ? text : "" );
697 slap_send_search_entry(
702 AttributeName *attrs,
707 char berbuf[LBER_ELEMENT_SIZEOF];
708 BerElement *ber = (BerElement *)berbuf;
710 int i, j, rc=-1, bytes;
714 AccessControlState acl_state = ACL_STATE_INIT;
716 /* Support for computed attribute plugins */
717 computed_attr_context ctx;
721 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
723 /* a_flags: array of flags telling if the i-th element will be
724 * returned or filtered out
725 * e_flags: array of a_flags
727 char **e_flags = NULL;
729 if (op->o_callback && op->o_callback->sc_sendentry) {
730 return op->o_callback->sc_sendentry( be, conn, op, e, attrs,
735 LDAP_LOG( OPERATION, ENTRY,
736 "send_search_entry: conn %lu dn=\"%s\"%s\n",
737 op->o_connid, e->e_dn, attrsonly ? " (attrsOnly)" : "" );
739 Debug( LDAP_DEBUG_TRACE,
740 "=> send_search_entry: dn=\"%s\"%s\n",
741 e->e_dn, attrsonly ? " (attrsOnly)" : "", 0 );
744 if ( ! access_allowed( be, conn, op, e,
745 ad_entry, NULL, ACL_READ, NULL ) )
749 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
750 op->o_connid, e->e_dn, 0 );
752 Debug( LDAP_DEBUG_ACL,
753 "send_search_entry: access to entry not allowed\n",
762 #ifdef LDAP_CONNECTIONLESS
767 ber_init_w_nullc( ber, LBER_USE_DER );
769 #ifdef LDAP_CONNECTIONLESS
770 if (conn->c_is_udp && conn->c_protocol == LDAP_VERSION2) {
771 rc = ber_printf(ber, "t{0{" /*}}*/,
772 LDAP_RES_SEARCH_ENTRY, &e->e_name);
776 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
777 LDAP_RES_SEARCH_ENTRY, &e->e_name );
782 LDAP_LOG( OPERATION, ERR,
783 "send_search_entry: conn %lu ber_printf failed\n",
784 op->o_connid, 0, 0 );
786 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
789 #ifdef LDAP_CONNECTIONLESS
790 if (conn->c_is_udp == 0)
793 send_ldap_result( conn, op, LDAP_OTHER,
794 NULL, "encoding DN error", NULL, NULL );
798 /* check for special all user attributes ("*") type */
799 userattrs = ( attrs == NULL ) ? 1
800 : an_find( attrs, &AllUser );
802 /* check for special all operational attributes ("+") type */
803 opattrs = ( attrs == NULL ) ? 0
804 : an_find( attrs, &AllOper );
806 /* create an array of arrays of flags. Each flag corresponds
807 * to particular value of attribute and equals 1 if value matches
808 * to ValuesReturnFilter or 0 if not
810 if ( op->vrFilter != NULL ) {
814 for ( a = e->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
815 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
818 size = i * sizeof(char *) + k;
821 e_flags = SLAP_CALLOC ( 1, i * sizeof(char *) + k );
822 if( e_flags == NULL ) {
824 LDAP_LOG( OPERATION, ERR,
825 "send_search_entry: conn %lu SLAP_CALLOC failed\n",
826 conn ? conn->c_connid : 0, 0, 0 );
828 Debug( LDAP_DEBUG_ANY,
829 "send_search_entry: SLAP_CALLOC failed\n", 0, 0, 0 );
833 send_ldap_result( conn, op, LDAP_OTHER,
834 NULL, "memory error",
838 a_flags = (char *)(e_flags + i);
839 memset( a_flags, 0, k );
840 for ( a = e->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
841 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
842 e_flags[i] = a_flags;
846 rc = filter_matched_values(be, conn, op, e->e_attrs, &e_flags) ;
849 LDAP_LOG( OPERATION, ERR,
850 "send_search_entry: conn %lu matched values filtering failed\n",
851 conn ? conn->c_connid : 0, 0, 0 );
853 Debug( LDAP_DEBUG_ANY,
854 "matched values filtering failed\n", 0, 0, 0 );
856 #ifdef LDAP_CONNECTIONLESS
857 if (conn->c_is_udp == 0)
861 send_ldap_result( conn, op, LDAP_OTHER,
862 NULL, "matched values filtering error",
869 for ( a = e->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
870 AttributeDescription *desc = a->a_desc;
872 if ( attrs == NULL ) {
873 /* all attrs request, skip operational attributes */
874 if( is_at_operational( desc->ad_type ) ) {
879 /* specific attrs requested */
880 if ( is_at_operational( desc->ad_type ) ) {
881 if( !opattrs && !ad_inlist( desc, attrs ) ) {
886 if (!userattrs && !ad_inlist( desc, attrs ) ) {
892 if ( ! access_allowed( be, conn, op, e, desc, NULL,
893 ACL_READ, &acl_state ) )
897 "send_search_entry: conn %lu access to attribute %s not "
898 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
900 Debug( LDAP_DEBUG_ACL, "acl: "
901 "access to attribute %s not allowed\n",
902 desc->ad_cname.bv_val, 0, 0 );
907 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
909 LDAP_LOG( OPERATION, ERR,
910 "send_search_entry: conn %lu ber_printf failed\n",
911 op->o_connid, 0, 0 );
913 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
916 #ifdef LDAP_CONNECTIONLESS
917 if (conn->c_is_udp == 0)
920 send_ldap_result( conn, op, LDAP_OTHER,
921 NULL, "encoding description error", NULL, NULL );
926 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
927 if ( ! access_allowed( be, conn, op, e,
928 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
932 "send_search_entry: conn %lu "
933 "access to attribute %s, value %d not allowed\n",
934 op->o_connid, desc->ad_cname.bv_val, i );
936 Debug( LDAP_DEBUG_ACL,
937 "acl: access to attribute %s, "
938 "value %d not allowed\n",
939 desc->ad_cname.bv_val, i, 0 );
945 if ( op->vrFilter && e_flags[j][i] == 0 ){
949 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
951 LDAP_LOG( OPERATION, ERR,
952 "send_search_entry: conn %lu "
953 "ber_printf failed.\n", op->o_connid, 0, 0 );
955 Debug( LDAP_DEBUG_ANY,
956 "ber_printf failed\n", 0, 0, 0 );
959 #ifdef LDAP_CONNECTIONLESS
960 if (conn->c_is_udp == 0)
963 send_ldap_result( conn, op, LDAP_OTHER,
964 NULL, "encoding values error",
971 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
973 LDAP_LOG( OPERATION, ERR,
974 "send_search_entry: conn %lu ber_printf failed\n",
975 op->o_connid, 0, 0 );
977 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
980 #ifdef LDAP_CONNECTIONLESS
981 if (conn->c_is_udp == 0)
984 send_ldap_result( conn, op, LDAP_OTHER,
985 NULL, "encode end error", NULL, NULL );
990 /* eventually will loop through generated operational attributes */
991 /* only have subschemaSubentry implemented */
992 aa = backend_operational( be, conn, op, e, attrs, opattrs );
994 if ( aa != NULL && op->vrFilter != NULL ) {
998 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
999 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1002 size = i * sizeof(char *) + k;
1004 char *a_flags, **tmp;
1007 * Reuse previous memory - we likely need less space
1008 * for operational attributes
1010 tmp = SLAP_REALLOC ( e_flags, i * sizeof(char *) + k );
1011 if ( tmp == NULL ) {
1013 LDAP_LOG( OPERATION, ERR,
1014 "send_search_entry: conn %lu "
1015 "not enough memory "
1016 "for matched values filtering\n",
1017 conn ? conn->c_connid : 0, 0, 0);
1019 Debug( LDAP_DEBUG_ANY,
1020 "send_search_entry: conn %lu "
1021 "not enough memory "
1022 "for matched values filtering\n",
1023 conn ? conn->c_connid : 0, 0, 0 );
1027 send_ldap_result( conn, op, LDAP_NO_MEMORY,
1028 NULL, NULL, NULL, NULL );
1032 a_flags = (char *)(e_flags + i);
1033 memset( a_flags, 0, k );
1034 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1035 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1036 e_flags[i] = a_flags;
1039 rc = filter_matched_values(be, conn, op, aa, &e_flags) ;
1043 LDAP_LOG( OPERATION, ERR,
1044 "send_search_entry: conn %lu "
1045 "matched values filtering failed\n",
1046 conn ? conn->c_connid : 0, 0, 0);
1048 Debug( LDAP_DEBUG_ANY,
1049 "matched values filtering failed\n", 0, 0, 0 );
1051 #ifdef LDAP_CONNECTIONLESS
1052 if (conn->c_is_udp == 0)
1056 send_ldap_result( conn, op, LDAP_OTHER,
1057 NULL, "matched values filtering error",
1064 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
1065 AttributeDescription *desc = a->a_desc;
1067 if ( attrs == NULL ) {
1068 /* all attrs request, skip operational attributes */
1069 if( is_at_operational( desc->ad_type ) ) {
1074 /* specific attrs requested */
1075 if( is_at_operational( desc->ad_type ) ) {
1076 if( !opattrs && !ad_inlist( desc, attrs ) ) {
1080 if (!userattrs && !ad_inlist( desc, attrs ) )
1087 if ( ! access_allowed( be, conn, op, e, desc, NULL,
1088 ACL_READ, &acl_state ) )
1091 LDAP_LOG( ACL, INFO,
1092 "send_search_entry: conn %lu "
1093 "access to attribute %s not allowed\n",
1094 op->o_connid, desc->ad_cname.bv_val, 0 );
1096 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s "
1098 desc->ad_cname.bv_val, 0, 0 );
1104 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1107 LDAP_LOG( OPERATION, ERR,
1108 "send_search_entry: conn %lu "
1109 "ber_printf failed\n", op->o_connid, 0, 0 );
1111 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1114 #ifdef LDAP_CONNECTIONLESS
1115 if (conn->c_is_udp == 0)
1117 ber_free_buf( ber );
1118 send_ldap_result( conn, op, LDAP_OTHER,
1119 NULL, "encoding description error", NULL, NULL );
1125 if ( ! attrsonly ) {
1126 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1127 if ( ! access_allowed( be, conn, op, e,
1128 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1131 LDAP_LOG( ACL, INFO,
1132 "send_search_entry: conn %lu "
1133 "access to %s, value %d not allowed\n",
1134 op->o_connid, desc->ad_cname.bv_val, i );
1136 Debug( LDAP_DEBUG_ACL,
1137 "acl: access to attribute %s, "
1138 "value %d not allowed\n",
1139 desc->ad_cname.bv_val, i, 0 );
1145 if ( op->vrFilter && e_flags[j][i] == 0 ){
1149 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1151 LDAP_LOG( OPERATION, ERR,
1152 "send_search_entry: conn %lu ber_printf failed\n",
1153 op->o_connid, 0, 0 );
1155 Debug( LDAP_DEBUG_ANY,
1156 "ber_printf failed\n", 0, 0, 0 );
1159 #ifdef LDAP_CONNECTIONLESS
1160 if (conn->c_is_udp == 0)
1162 ber_free_buf( ber );
1163 send_ldap_result( conn, op, LDAP_OTHER,
1164 NULL, "encoding values error",
1173 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1175 LDAP_LOG( OPERATION, ERR,
1176 "send_search_entry: conn %lu ber_printf failed\n",
1177 op->o_connid, 0, 0 );
1179 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1182 #ifdef LDAP_CONNECTIONLESS
1183 if (conn->c_is_udp == 0)
1185 ber_free_buf( ber );
1186 send_ldap_result( conn, op, LDAP_OTHER,
1187 NULL, "encode end error", NULL, NULL );
1196 * First, setup the computed attribute context that is
1197 * passed to all plugins.
1199 ctx.cac_pb = op->o_pb;
1200 ctx.cac_attrs = attrs;
1201 ctx.cac_attrsonly = attrsonly;
1202 ctx.cac_userattrs = userattrs;
1203 ctx.cac_opattrs = opattrs;
1204 ctx.cac_acl_state = acl_state;
1205 ctx.cac_private = (void *)ber;
1208 * For each client requested attribute, call the plugins.
1210 if ( attrs != NULL ) {
1211 for ( anp = attrs; anp->an_name.bv_val != NULL; anp++ ) {
1212 rc = compute_evaluator( &ctx, anp->an_name.bv_val, e, slapi_x_compute_output_ber );
1219 * Technically we shouldn't be returning operational attributes
1220 * when the user requested only user attributes. We'll let the
1221 * plugin decide whether to be naughty or not.
1223 rc = compute_evaluator( &ctx, "*", e, slapi_x_compute_output_ber );
1226 ber_free_buf( ber );
1227 send_ldap_result( conn, op, LDAP_OTHER,
1228 NULL, "computed attribute error", NULL, NULL );
1231 #endif /* LDAP_SLAPI */
1240 rc = ber_printf( ber, /*{{*/ "}N}" );
1242 if( rc != -1 && ctrls != NULL ) {
1243 rc = send_ldap_controls( ber, ctrls );
1247 rc = ber_printf( ber, /*{*/ "N}" );
1252 LDAP_LOG( OPERATION, ERR,
1253 "send_search_entry: conn %lu ber_printf failed\n",
1254 op->o_connid, 0, 0 );
1256 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1259 #ifdef LDAP_CONNECTIONLESS
1260 if (conn->c_is_udp == 0)
1262 ber_free_buf( ber );
1263 send_ldap_result( conn, op, LDAP_OTHER,
1264 NULL, "encode entry end error", NULL, NULL );
1268 #ifdef LDAP_CONNECTIONLESS
1269 if (conn->c_is_udp == 0) {
1271 bytes = op->o_noop ? 0 : send_ldap_ber( conn, ber );
1272 ber_free_buf( ber );
1276 LDAP_LOG( OPERATION, ERR,
1277 "send_search_entry: conn %lu ber write failed.\n",
1278 op->o_connid, 0, 0 );
1280 Debug( LDAP_DEBUG_ANY,
1281 "send_search_entry: ber write failed\n",
1288 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1289 num_bytes_sent += bytes;
1292 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1294 #ifdef LDAP_CONNECTIONLESS
1298 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1299 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
1302 LDAP_LOG( OPERATION, ENTRY,
1303 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1305 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1311 if ( e_flags ) free( e_flags );
1316 slap_send_search_reference(
1322 LDAPControl **ctrls,
1326 char berbuf[LBER_ELEMENT_SIZEOF];
1327 BerElement *ber = (BerElement *)berbuf;
1331 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1332 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1334 if (op->o_callback && op->o_callback->sc_sendreference) {
1335 return op->o_callback->sc_sendreference( be, conn, op, e, refs, ctrls, v2refs );
1339 LDAP_LOG( OPERATION, ENTRY,
1340 "send_search_reference: conn %lu dn=\"%s\"\n",
1341 op->o_connid, e ? e->e_dn : "(null)", 0 );
1343 Debug( LDAP_DEBUG_TRACE,
1344 "=> send_search_reference: dn=\"%s\"\n",
1345 e ? e->e_dn : "(null)", 0, 0 );
1348 if ( e && ! access_allowed( be, conn, op, e,
1349 ad_entry, NULL, ACL_READ, NULL ) )
1352 LDAP_LOG( ACL, INFO,
1353 "send_search_reference: conn %lu "
1354 "access to entry %s not allowed\n",
1355 op->o_connid, e->e_dn, 0 );
1357 Debug( LDAP_DEBUG_ACL,
1358 "send_search_reference: access to entry not allowed\n",
1365 if ( e && ! access_allowed( be, conn, op, e,
1366 ad_ref, NULL, ACL_READ, NULL ) )
1369 LDAP_LOG( ACL, INFO,
1370 "send_search_reference: conn %lu access "
1371 "to reference not allowed.\n", op->o_connid, 0, 0 );
1373 Debug( LDAP_DEBUG_ACL,
1374 "send_search_reference: access "
1375 "to reference not allowed\n",
1382 if( op->o_noreferrals ) {
1384 LDAP_LOG( OPERATION, ERR,
1385 "send_search_reference: conn %lu noreferrals control in (%s).\n",
1386 op->o_connid, e->e_dn, 0 );
1388 Debug( LDAP_DEBUG_ANY,
1389 "send_search_reference: noreferrals control in (%s)\n",
1396 if( refs == NULL ) {
1398 LDAP_LOG( OPERATION, ERR,
1399 "send_search_reference: conn %lu null ref in (%s).\n",
1400 op->o_connid, e ? e->e_dn : "(null)", 0 );
1402 Debug( LDAP_DEBUG_ANY,
1403 "send_search_reference: null ref in (%s)\n",
1404 e ? e->e_dn : "(null)", 0, 0 );
1410 if( op->o_protocol < LDAP_VERSION3 ) {
1411 /* save the references for the result */
1412 if( refs[0].bv_val != NULL ) {
1413 if( value_add( v2refs, refs ) )
1419 #ifdef LDAP_CONNECTIONLESS
1421 ber = op->o_res_ber;
1424 ber_init_w_nullc( ber, LBER_USE_DER );
1426 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1427 LDAP_RES_SEARCH_REFERENCE, refs );
1429 if( rc != -1 && ctrls != NULL ) {
1430 rc = send_ldap_controls( ber, ctrls );
1434 rc = ber_printf( ber, /*"{"*/ "N}", op->o_msgid,
1435 LDAP_RES_SEARCH_REFERENCE, refs );
1440 LDAP_LOG( OPERATION, ERR,
1441 "send_search_reference: conn %lu "
1442 "ber_printf failed.\n", op->o_connid, 0, 0 );
1444 Debug( LDAP_DEBUG_ANY,
1445 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1448 #ifdef LDAP_CONNECTIONLESS
1449 if (conn->c_is_udp == 0)
1451 ber_free_buf( ber );
1452 send_ldap_result( conn, op, LDAP_OTHER,
1453 NULL, "encode DN error", NULL, NULL );
1457 #ifdef LDAP_CONNECTIONLESS
1458 if (conn->c_is_udp == 0) {
1460 bytes = op->o_noop ? 0 : send_ldap_ber( conn, ber );
1461 ber_free_buf( ber );
1463 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1464 num_bytes_sent += bytes;
1467 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1468 #ifdef LDAP_CONNECTIONLESS
1472 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1473 conn->c_connid, op->o_opid, e ? e->e_dn : "(null)", 0, 0 );
1476 LDAP_LOG( OPERATION, ENTRY,
1477 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1479 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1497 *code = LDAP_SUCCESS;
1501 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1503 LDAP_LOG( OPERATION, INFO,
1504 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1506 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1514 while ( (s = strchr( s, '\n' )) != NULL ) {
1519 if ( (c = strchr( s, ':' )) != NULL ) {
1523 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1527 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1531 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1537 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1539 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",