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}" );
232 struct berval *resdata,
233 struct berval *sasldata,
237 char berbuf[LBER_ELEMENT_SIZEOF];
238 BerElement *ber = (BerElement *)berbuf;
242 if (op->o_callback && op->o_callback->sc_response) {
243 op->o_callback->sc_response( conn, op, tag, msgid, err, matched,
244 text, ref, resoid, resdata, sasldata, ctrls );
248 #ifdef LDAP_CONNECTIONLESS
254 ber_init_w_nullc( ber, LBER_USE_DER );
257 LDAP_LOG( OPERATION, ENTRY,
258 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
261 Debug( LDAP_DEBUG_TRACE,
262 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
268 LDAP_LOG( OPERATION, ARGS,
269 "send_ldap_response: conn %lu ref=\"%s\"\n",
270 conn ? conn->c_connid : 0,
271 ref[0].bv_val ? ref[0].bv_val : "NULL" , 0 );
273 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
274 ref[0].bv_val ? ref[0].bv_val : "NULL",
279 #ifdef LDAP_CONNECTIONLESS
280 if (conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
281 rc = ber_printf( ber, "t{ess" /*"}}"*/,
283 matched == NULL ? "" : matched,
284 text == NULL ? "" : text );
288 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
290 matched == NULL ? "" : matched,
291 text == NULL ? "" : text );
296 assert( err == LDAP_REFERRAL );
297 rc = ber_printf( ber, "t{W}",
298 LDAP_TAG_REFERRAL, ref );
300 assert( err != LDAP_REFERRAL );
304 if( rc != -1 && sasldata != NULL ) {
305 rc = ber_printf( ber, "tO",
306 LDAP_TAG_SASL_RES_CREDS, sasldata );
309 if( rc != -1 && resoid != NULL ) {
310 rc = ber_printf( ber, "ts",
311 LDAP_TAG_EXOP_RES_OID, resoid );
314 if( rc != -1 && resdata != NULL ) {
315 rc = ber_printf( ber, "tO",
316 LDAP_TAG_EXOP_RES_VALUE, resdata );
320 rc = ber_printf( ber, /*"{"*/ "N}" );
323 if( rc != -1 && ctrls != NULL ) {
324 rc = send_ldap_controls( ber, ctrls );
328 rc = ber_printf( ber, /*"{"*/ "N}" );
331 #ifdef LDAP_CONNECTIONLESS
332 if( conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
333 rc = ber_printf( ber, /*"{"*/ "N}" );
339 LDAP_LOG( OPERATION, ERR,
340 "send_ldap_response: conn %lu ber_printf failed\n",
341 conn ? conn->c_connid : 0, 0, 0 );
343 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
346 #ifdef LDAP_CONNECTIONLESS
347 if (conn->c_is_udp == 0)
354 bytes = send_ldap_ber( conn, ber );
355 #ifdef LDAP_CONNECTIONLESS
356 if (conn->c_is_udp == 0)
362 LDAP_LOG( OPERATION, ERR,
363 "send_ldap_response: conn %lu ber write failed\n",
364 conn ? conn->c_connid : 0, 0, 0 );
366 Debug( LDAP_DEBUG_ANY,
367 "send_ldap_response: ber write failed\n",
375 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)err );
376 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, ( matched != NULL ) ? (void *)ch_strdup( matched ) : NULL );
377 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, ( text != NULL ) ? (void *)ch_strdup( text ) : NULL );
378 #endif /* LDAP_SLAPI */
380 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
381 num_bytes_sent += bytes;
383 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
389 send_ldap_disconnect(
400 #define LDAP_UNSOLICITED_ERROR(e) \
401 ( (e) == LDAP_PROTOCOL_ERROR \
402 || (e) == LDAP_STRONG_AUTH_REQUIRED \
403 || (e) == LDAP_UNAVAILABLE )
405 assert( LDAP_UNSOLICITED_ERROR( err ) );
408 LDAP_LOG( OPERATION, ENTRY,
409 "send_ldap_disconnect: conn %lu %d:%s\n",
410 conn ? conn->c_connid : 0, err, text ? text : "" );
412 Debug( LDAP_DEBUG_TRACE,
413 "send_ldap_disconnect %d:%s\n",
414 err, text ? text : "", NULL );
418 if ( op->o_protocol < LDAP_VERSION3 ) {
420 tag = req2res( op->o_tag );
421 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
424 reqoid = LDAP_NOTICE_DISCONNECT;
425 tag = LDAP_RES_EXTENDED;
429 send_ldap_response( conn, op, tag, msgid,
430 err, NULL, text, NULL,
431 reqoid, NULL, NULL, NULL );
433 Statslog( LDAP_DEBUG_STATS,
434 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
435 op->o_connid, op->o_opid, tag, err, text ? text : "" );
439 slap_send_ldap_result(
453 assert( !LDAP_API_ERROR( err ) );
456 LDAP_LOG( OPERATION, ENTRY,
457 "send_ldap_result: conn %lu op=%lu p=%d\n",
458 op->o_connid, op->o_opid, op->o_protocol );
460 Debug( LDAP_DEBUG_TRACE,
461 "send_ldap_result: conn=%lu op=%lu p=%d\n",
462 op->o_connid, op->o_opid, op->o_protocol );
466 LDAP_LOG( OPERATION, ARGS,
467 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
468 err, matched ? matched : "", text ? text : "" );
470 Debug( LDAP_DEBUG_ARGS,
471 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
472 err, matched ? matched : "", text ? text : "" );
478 LDAP_LOG( OPERATION, ARGS,
479 "send_ldap_result: referral=\"%s\"\n",
480 ref[0].bv_val ? ref[0].bv_val : "NULL", 0, 0 );
482 Debug( LDAP_DEBUG_ARGS,
483 "send_ldap_result: referral=\"%s\"\n",
484 ref[0].bv_val ? ref[0].bv_val : "NULL",
489 assert( err != LDAP_PARTIAL_RESULTS );
491 if ( err == LDAP_REFERRAL ) {
492 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
493 if( op->o_domain_scope ) {
498 err = LDAP_NO_SUCH_OBJECT;
499 } else if ( op->o_protocol < LDAP_VERSION3 ) {
500 err = LDAP_PARTIAL_RESULTS;
504 if ( op->o_protocol < LDAP_VERSION3 ) {
505 tmp = v2ref( ref, text );
510 tag = req2res( op->o_tag );
511 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
513 send_ldap_response( conn, op, tag, msgid,
514 err, matched, text, ref,
515 NULL, NULL, NULL, ctrls );
517 Statslog( LDAP_DEBUG_STATS,
518 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
519 op->o_connid, op->o_opid, tag, err, text ? text : "" );
542 LDAP_LOG( OPERATION, ENTRY,
543 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
544 op->o_connid, err, cred ? cred->bv_len : -1 );
546 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
547 err, cred ? (long) cred->bv_len : -1, NULL );
551 tag = req2res( op->o_tag );
552 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
554 send_ldap_response( conn, op, tag, msgid,
555 err, matched, text, ref,
556 NULL, NULL, cred, ctrls );
560 slap_send_ldap_extended(
568 struct berval *rspdata,
576 LDAP_LOG( OPERATION, ENTRY,
577 "send_ldap_extended: err=%d oid=%s len=%ld\n",
578 err, rspoid ? rspoid : "",
579 rspdata != NULL ? rspdata->bv_len : 0 );
581 Debug( LDAP_DEBUG_TRACE,
582 "send_ldap_extended: err=%d oid=%s len=%ld\n",
584 rspoid ? rspoid : "",
585 rspdata != NULL ? rspdata->bv_len : 0 );
588 tag = req2res( op->o_tag );
589 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
591 send_ldap_response( conn, op, tag, msgid,
592 err, matched, text, refs,
593 rspoid, rspdata, NULL, ctrls );
596 #ifdef LDAP_RES_INTERMEDIATE_RESP
598 slap_send_ldap_intermediate_resp(
606 struct berval *rspdata,
607 LDAPControl **ctrls )
612 LDAP_LOG( OPERATION, ENTRY,
613 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
614 err, rspoid ? rspoid : "",
615 rspdata != NULL ? rspdata->bv_len : 0 );
617 Debug( LDAP_DEBUG_TRACE,
618 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
620 rspoid ? rspoid : "",
621 rspdata != NULL ? rspdata->bv_len : 0 );
623 tag = LDAP_RES_INTERMEDIATE_RESP;
624 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
625 send_ldap_response( conn, op, tag, msgid,
626 err, matched, text, refs,
627 rspoid, rspdata, NULL, ctrls );
632 slap_send_search_result(
647 assert( !LDAP_API_ERROR( err ) );
649 if (op->o_callback && op->o_callback->sc_sresult) {
650 op->o_callback->sc_sresult(conn, op, err, matched, text, refs,
656 LDAP_LOG( OPERATION, ENTRY,
657 "send_search_result: err=%d matched=\"%s\" text=\"%s\"\n",
658 err, matched ? matched : "", text ? text : "" );
660 Debug( LDAP_DEBUG_TRACE,
661 "send_search_result: err=%d matched=\"%s\" text=\"%s\"\n",
662 err, matched ? matched : "", text ? text : "" );
666 assert( err != LDAP_PARTIAL_RESULTS );
668 if( op->o_protocol < LDAP_VERSION3 ) {
669 /* send references in search results */
670 if( err == LDAP_REFERRAL ) {
671 err = LDAP_PARTIAL_RESULTS;
674 tmp = v2ref( refs, text );
679 /* don't send references in search results */
680 assert( refs == NULL );
683 if( err == LDAP_REFERRAL ) {
688 tag = req2res( op->o_tag );
689 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
691 send_ldap_response( conn, op, tag, msgid,
692 err, matched, text, refs,
693 NULL, NULL, NULL, ctrls );
697 snprintf( nbuf, sizeof nbuf, "%d nentries=%d", err, nentries );
699 Statslog( LDAP_DEBUG_STATS,
700 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
701 op->o_connid, op->o_opid, tag, nbuf, text ? text : "" );
710 slap_send_search_entry(
715 AttributeName *attrs,
720 char berbuf[LBER_ELEMENT_SIZEOF];
721 BerElement *ber = (BerElement *)berbuf;
723 int i, j, rc=-1, bytes;
727 AccessControlState acl_state = ACL_STATE_INIT;
729 /* Support for computed attribute plugins */
730 computed_attr_context ctx;
734 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
736 /* a_flags: array of flags telling if the i-th element will be
737 * returned or filtered out
738 * e_flags: array of a_flags
740 char **e_flags = NULL;
742 if (op->o_callback && op->o_callback->sc_sendentry) {
743 return op->o_callback->sc_sendentry( be, conn, op, e, attrs,
748 LDAP_LOG( OPERATION, ENTRY,
749 "send_search_entry: conn %lu dn=\"%s\"%s\n",
750 op->o_connid, e->e_dn, attrsonly ? " (attrsOnly)" : "" );
752 Debug( LDAP_DEBUG_TRACE,
753 "=> send_search_entry: dn=\"%s\"%s\n",
754 e->e_dn, attrsonly ? " (attrsOnly)" : "", 0 );
757 if ( ! access_allowed( be, conn, op, e,
758 ad_entry, NULL, ACL_READ, NULL ) )
762 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
763 op->o_connid, e->e_dn, 0 );
765 Debug( LDAP_DEBUG_ACL,
766 "send_search_entry: access to entry not allowed\n",
775 #ifdef LDAP_CONNECTIONLESS
780 ber_init_w_nullc( ber, LBER_USE_DER );
782 #ifdef LDAP_CONNECTIONLESS
783 if (conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
784 rc = ber_printf(ber, "t{O{" /*}}*/,
785 LDAP_RES_SEARCH_ENTRY, &e->e_name);
789 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
790 LDAP_RES_SEARCH_ENTRY, &e->e_name );
795 LDAP_LOG( OPERATION, ERR,
796 "send_search_entry: conn %lu ber_printf failed\n",
797 op->o_connid, 0, 0 );
799 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
802 #ifdef LDAP_CONNECTIONLESS
803 if (conn->c_is_udp == 0)
806 send_ldap_result( conn, op, LDAP_OTHER,
807 NULL, "encoding DN error", NULL, NULL );
811 /* check for special all user attributes ("*") type */
812 userattrs = ( attrs == NULL ) ? 1
813 : an_find( attrs, &AllUser );
815 /* check for special all operational attributes ("+") type */
816 opattrs = ( attrs == NULL ) ? 0
817 : an_find( attrs, &AllOper );
819 /* create an array of arrays of flags. Each flag corresponds
820 * to particular value of attribute and equals 1 if value matches
821 * to ValuesReturnFilter or 0 if not
823 if ( op->vrFilter != NULL ) {
827 for ( a = e->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
828 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
831 size = i * sizeof(char *) + k;
834 e_flags = SLAP_CALLOC ( 1, i * sizeof(char *) + k );
835 if( e_flags == NULL ) {
837 LDAP_LOG( OPERATION, ERR,
838 "send_search_entry: conn %lu SLAP_CALLOC failed\n",
839 conn ? conn->c_connid : 0, 0, 0 );
841 Debug( LDAP_DEBUG_ANY,
842 "send_search_entry: SLAP_CALLOC failed\n", 0, 0, 0 );
846 send_ldap_result( conn, op, LDAP_OTHER,
847 NULL, "memory error",
851 a_flags = (char *)(e_flags + i);
852 memset( a_flags, 0, k );
853 for ( a = e->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
854 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
855 e_flags[i] = a_flags;
859 rc = filter_matched_values(be, conn, op, e->e_attrs, &e_flags) ;
862 LDAP_LOG( OPERATION, ERR,
863 "send_search_entry: conn %lu matched values filtering failed\n",
864 conn ? conn->c_connid : 0, 0, 0 );
866 Debug( LDAP_DEBUG_ANY,
867 "matched values filtering failed\n", 0, 0, 0 );
869 #ifdef LDAP_CONNECTIONLESS
870 if (conn->c_is_udp == 0)
874 send_ldap_result( conn, op, LDAP_OTHER,
875 NULL, "matched values filtering error",
882 for ( a = e->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
883 AttributeDescription *desc = a->a_desc;
885 if ( attrs == NULL ) {
886 /* all attrs request, skip operational attributes */
887 if( is_at_operational( desc->ad_type ) ) {
892 /* specific attrs requested */
893 if ( is_at_operational( desc->ad_type ) ) {
894 if( !opattrs && !ad_inlist( desc, attrs ) ) {
899 if (!userattrs && !ad_inlist( desc, attrs ) ) {
905 if ( ! access_allowed( be, conn, op, e, desc, NULL,
906 ACL_READ, &acl_state ) )
910 "send_search_entry: conn %lu access to attribute %s not "
911 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
913 Debug( LDAP_DEBUG_ACL, "acl: "
914 "access to attribute %s not allowed\n",
915 desc->ad_cname.bv_val, 0, 0 );
920 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
922 LDAP_LOG( OPERATION, ERR,
923 "send_search_entry: conn %lu ber_printf failed\n",
924 op->o_connid, 0, 0 );
926 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
929 #ifdef LDAP_CONNECTIONLESS
930 if (conn->c_is_udp == 0)
933 send_ldap_result( conn, op, LDAP_OTHER,
934 NULL, "encoding description error", NULL, NULL );
939 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
940 if ( ! access_allowed( be, conn, op, e,
941 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
945 "send_search_entry: conn %lu "
946 "access to attribute %s, value %d not allowed\n",
947 op->o_connid, desc->ad_cname.bv_val, i );
949 Debug( LDAP_DEBUG_ACL,
950 "acl: access to attribute %s, "
951 "value %d not allowed\n",
952 desc->ad_cname.bv_val, i, 0 );
958 if ( op->vrFilter && e_flags[j][i] == 0 ){
962 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
964 LDAP_LOG( OPERATION, ERR,
965 "send_search_entry: conn %lu "
966 "ber_printf failed.\n", op->o_connid, 0, 0 );
968 Debug( LDAP_DEBUG_ANY,
969 "ber_printf failed\n", 0, 0, 0 );
972 #ifdef LDAP_CONNECTIONLESS
973 if (conn->c_is_udp == 0)
976 send_ldap_result( conn, op, LDAP_OTHER,
977 NULL, "encoding values error",
984 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
986 LDAP_LOG( OPERATION, ERR,
987 "send_search_entry: conn %lu ber_printf failed\n",
988 op->o_connid, 0, 0 );
990 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
993 #ifdef LDAP_CONNECTIONLESS
994 if (conn->c_is_udp == 0)
997 send_ldap_result( conn, op, LDAP_OTHER,
998 NULL, "encode end error", NULL, NULL );
1003 /* eventually will loop through generated operational attributes */
1004 /* only have subschemaSubentry implemented */
1005 aa = backend_operational( be, conn, op, e, attrs, opattrs );
1007 if ( aa != NULL && op->vrFilter != NULL ) {
1011 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1012 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1015 size = i * sizeof(char *) + k;
1017 char *a_flags, **tmp;
1020 * Reuse previous memory - we likely need less space
1021 * for operational attributes
1023 tmp = SLAP_REALLOC ( e_flags, i * sizeof(char *) + k );
1024 if ( tmp == NULL ) {
1026 LDAP_LOG( OPERATION, ERR,
1027 "send_search_entry: conn %lu "
1028 "not enough memory "
1029 "for matched values filtering\n",
1030 conn ? conn->c_connid : 0, 0, 0);
1032 Debug( LDAP_DEBUG_ANY,
1033 "send_search_entry: conn %lu "
1034 "not enough memory "
1035 "for matched values filtering\n",
1036 conn ? conn->c_connid : 0, 0, 0 );
1040 send_ldap_result( conn, op, LDAP_NO_MEMORY,
1041 NULL, NULL, NULL, NULL );
1045 a_flags = (char *)(e_flags + i);
1046 memset( a_flags, 0, k );
1047 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1048 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1049 e_flags[i] = a_flags;
1052 rc = filter_matched_values(be, conn, op, aa, &e_flags) ;
1056 LDAP_LOG( OPERATION, ERR,
1057 "send_search_entry: conn %lu "
1058 "matched values filtering failed\n",
1059 conn ? conn->c_connid : 0, 0, 0);
1061 Debug( LDAP_DEBUG_ANY,
1062 "matched values filtering failed\n", 0, 0, 0 );
1064 #ifdef LDAP_CONNECTIONLESS
1065 if (conn->c_is_udp == 0)
1069 send_ldap_result( conn, op, LDAP_OTHER,
1070 NULL, "matched values filtering error",
1077 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
1078 AttributeDescription *desc = a->a_desc;
1080 if ( attrs == NULL ) {
1081 /* all attrs request, skip operational attributes */
1082 if( is_at_operational( desc->ad_type ) ) {
1087 /* specific attrs requested */
1088 if( is_at_operational( desc->ad_type ) ) {
1089 if( !opattrs && !ad_inlist( desc, attrs ) ) {
1093 if (!userattrs && !ad_inlist( desc, attrs ) )
1100 if ( ! access_allowed( be, conn, op, e, desc, NULL,
1101 ACL_READ, &acl_state ) )
1104 LDAP_LOG( ACL, INFO,
1105 "send_search_entry: conn %lu "
1106 "access to attribute %s not allowed\n",
1107 op->o_connid, desc->ad_cname.bv_val, 0 );
1109 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s "
1111 desc->ad_cname.bv_val, 0, 0 );
1117 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1120 LDAP_LOG( OPERATION, ERR,
1121 "send_search_entry: conn %lu "
1122 "ber_printf failed\n", op->o_connid, 0, 0 );
1124 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1127 #ifdef LDAP_CONNECTIONLESS
1128 if (conn->c_is_udp == 0)
1130 ber_free_buf( ber );
1131 send_ldap_result( conn, op, LDAP_OTHER,
1132 NULL, "encoding description error", NULL, NULL );
1138 if ( ! attrsonly ) {
1139 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1140 if ( ! access_allowed( be, conn, op, e,
1141 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1144 LDAP_LOG( ACL, INFO,
1145 "send_search_entry: conn %lu "
1146 "access to %s, value %d not allowed\n",
1147 op->o_connid, desc->ad_cname.bv_val, i );
1149 Debug( LDAP_DEBUG_ACL,
1150 "acl: access to attribute %s, "
1151 "value %d not allowed\n",
1152 desc->ad_cname.bv_val, i, 0 );
1158 if ( op->vrFilter && e_flags[j][i] == 0 ){
1162 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1164 LDAP_LOG( OPERATION, ERR,
1165 "send_search_entry: conn %lu ber_printf failed\n",
1166 op->o_connid, 0, 0 );
1168 Debug( LDAP_DEBUG_ANY,
1169 "ber_printf failed\n", 0, 0, 0 );
1172 #ifdef LDAP_CONNECTIONLESS
1173 if (conn->c_is_udp == 0)
1175 ber_free_buf( ber );
1176 send_ldap_result( conn, op, LDAP_OTHER,
1177 NULL, "encoding values error",
1186 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1188 LDAP_LOG( OPERATION, ERR,
1189 "send_search_entry: conn %lu ber_printf failed\n",
1190 op->o_connid, 0, 0 );
1192 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1195 #ifdef LDAP_CONNECTIONLESS
1196 if (conn->c_is_udp == 0)
1198 ber_free_buf( ber );
1199 send_ldap_result( conn, op, LDAP_OTHER,
1200 NULL, "encode end error", NULL, NULL );
1209 * First, setup the computed attribute context that is
1210 * passed to all plugins.
1212 ctx.cac_pb = op->o_pb;
1213 ctx.cac_attrs = attrs;
1214 ctx.cac_attrsonly = attrsonly;
1215 ctx.cac_userattrs = userattrs;
1216 ctx.cac_opattrs = opattrs;
1217 ctx.cac_acl_state = acl_state;
1218 ctx.cac_private = (void *)ber;
1221 * For each client requested attribute, call the plugins.
1223 if ( attrs != NULL ) {
1224 for ( anp = attrs; anp->an_name.bv_val != NULL; anp++ ) {
1225 rc = compute_evaluator( &ctx, anp->an_name.bv_val, e, slapi_x_compute_output_ber );
1232 * Technically we shouldn't be returning operational attributes
1233 * when the user requested only user attributes. We'll let the
1234 * plugin decide whether to be naughty or not.
1236 rc = compute_evaluator( &ctx, "*", e, slapi_x_compute_output_ber );
1239 ber_free_buf( ber );
1240 send_ldap_result( conn, op, LDAP_OTHER,
1241 NULL, "computed attribute error", NULL, NULL );
1244 #endif /* LDAP_SLAPI */
1253 rc = ber_printf( ber, /*{{*/ "}N}" );
1255 if( rc != -1 && ctrls != NULL ) {
1256 rc = send_ldap_controls( ber, ctrls );
1259 #ifdef LDAP_CONNECTIONLESS
1260 if( conn->c_is_udp && op->o_protocol == LDAP_VERSION2 ) {
1261 ; /* empty, skip following if */
1265 rc = ber_printf( ber, /*{*/ "N}" );
1270 LDAP_LOG( OPERATION, ERR,
1271 "send_search_entry: conn %lu ber_printf failed\n",
1272 op->o_connid, 0, 0 );
1274 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1277 #ifdef LDAP_CONNECTIONLESS
1278 if (conn->c_is_udp == 0)
1280 ber_free_buf( ber );
1281 send_ldap_result( conn, op, LDAP_OTHER,
1282 NULL, "encode entry end error", NULL, NULL );
1286 #ifdef LDAP_CONNECTIONLESS
1287 if (conn->c_is_udp == 0) {
1289 bytes = op->o_noop ? 0 : send_ldap_ber( conn, ber );
1290 ber_free_buf( ber );
1294 LDAP_LOG( OPERATION, ERR,
1295 "send_search_entry: conn %lu ber write failed.\n",
1296 op->o_connid, 0, 0 );
1298 Debug( LDAP_DEBUG_ANY,
1299 "send_search_entry: ber write failed\n",
1306 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1307 num_bytes_sent += bytes;
1310 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1312 #ifdef LDAP_CONNECTIONLESS
1316 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1317 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
1320 LDAP_LOG( OPERATION, ENTRY,
1321 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1323 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1329 if ( e_flags ) free( e_flags );
1334 slap_send_search_reference(
1340 LDAPControl **ctrls,
1344 char berbuf[LBER_ELEMENT_SIZEOF];
1345 BerElement *ber = (BerElement *)berbuf;
1349 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1350 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1352 if (op->o_callback && op->o_callback->sc_sendreference) {
1353 return op->o_callback->sc_sendreference( be, conn, op, e, refs, ctrls, v2refs );
1357 LDAP_LOG( OPERATION, ENTRY,
1358 "send_search_reference: conn %lu dn=\"%s\"\n",
1359 op->o_connid, e ? e->e_dn : "(null)", 0 );
1361 Debug( LDAP_DEBUG_TRACE,
1362 "=> send_search_reference: dn=\"%s\"\n",
1363 e ? e->e_dn : "(null)", 0, 0 );
1366 if ( e && ! access_allowed( be, conn, op, e,
1367 ad_entry, NULL, ACL_READ, NULL ) )
1370 LDAP_LOG( ACL, INFO,
1371 "send_search_reference: conn %lu "
1372 "access to entry %s not allowed\n",
1373 op->o_connid, e->e_dn, 0 );
1375 Debug( LDAP_DEBUG_ACL,
1376 "send_search_reference: access to entry not allowed\n",
1383 if ( e && ! access_allowed( be, conn, op, e,
1384 ad_ref, NULL, ACL_READ, NULL ) )
1387 LDAP_LOG( ACL, INFO,
1388 "send_search_reference: conn %lu access "
1389 "to reference not allowed.\n", op->o_connid, 0, 0 );
1391 Debug( LDAP_DEBUG_ACL,
1392 "send_search_reference: access "
1393 "to reference not allowed\n",
1400 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1401 if( op->o_domain_scope ) {
1403 LDAP_LOG( OPERATION, ERR,
1404 "send_search_reference: conn %lu domainScope control in (%s).\n",
1405 op->o_connid, e->e_dn, 0 );
1407 Debug( LDAP_DEBUG_ANY,
1408 "send_search_reference: domainScope control in (%s)\n",
1416 if( refs == NULL ) {
1418 LDAP_LOG( OPERATION, ERR,
1419 "send_search_reference: conn %lu null ref in (%s).\n",
1420 op->o_connid, e ? e->e_dn : "(null)", 0 );
1422 Debug( LDAP_DEBUG_ANY,
1423 "send_search_reference: null ref in (%s)\n",
1424 e ? e->e_dn : "(null)", 0, 0 );
1430 if( op->o_protocol < LDAP_VERSION3 ) {
1431 /* save the references for the result */
1432 if( refs[0].bv_val != NULL ) {
1433 if( value_add( v2refs, refs ) )
1439 #ifdef LDAP_CONNECTIONLESS
1441 ber = op->o_res_ber;
1444 ber_init_w_nullc( ber, LBER_USE_DER );
1446 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1447 LDAP_RES_SEARCH_REFERENCE, refs );
1449 if( rc != -1 && ctrls != NULL ) {
1450 rc = send_ldap_controls( ber, ctrls );
1454 rc = ber_printf( ber, /*"{"*/ "N}", op->o_msgid,
1455 LDAP_RES_SEARCH_REFERENCE, refs );
1460 LDAP_LOG( OPERATION, ERR,
1461 "send_search_reference: conn %lu "
1462 "ber_printf failed.\n", op->o_connid, 0, 0 );
1464 Debug( LDAP_DEBUG_ANY,
1465 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1468 #ifdef LDAP_CONNECTIONLESS
1469 if (conn->c_is_udp == 0)
1471 ber_free_buf( ber );
1472 send_ldap_result( conn, op, LDAP_OTHER,
1473 NULL, "encode DN error", NULL, NULL );
1477 #ifdef LDAP_CONNECTIONLESS
1478 if (conn->c_is_udp == 0) {
1480 bytes = op->o_noop ? 0 : send_ldap_ber( conn, ber );
1481 ber_free_buf( ber );
1483 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1484 num_bytes_sent += bytes;
1487 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1488 #ifdef LDAP_CONNECTIONLESS
1492 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1493 conn->c_connid, op->o_opid, e ? e->e_dn : "(null)", 0, 0 );
1496 LDAP_LOG( OPERATION, ENTRY,
1497 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1499 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1517 *code = LDAP_SUCCESS;
1521 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1523 LDAP_LOG( OPERATION, INFO,
1524 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1526 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1534 while ( (s = strchr( s, '\n' )) != NULL ) {
1539 if ( (c = strchr( s, ':' )) != NULL ) {
1543 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1547 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1551 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1557 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1559 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",