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 #ifdef LDAP_CONTROL_NOREFERRALS
484 if( op->o_noreferrals ) {
489 err = LDAP_NO_SUCH_OBJECT;
490 } else if ( op->o_protocol < LDAP_VERSION3 ) {
491 err = LDAP_PARTIAL_RESULTS;
495 if ( op->o_protocol < LDAP_VERSION3 ) {
496 tmp = v2ref( ref, text );
501 tag = req2res( op->o_tag );
502 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
504 send_ldap_response( conn, op, tag, msgid,
505 err, matched, text, ref,
506 NULL, NULL, NULL, ctrls );
508 Statslog( LDAP_DEBUG_STATS,
509 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
510 op->o_connid, op->o_opid, tag, err, text ? text : "" );
533 LDAP_LOG( OPERATION, ENTRY,
534 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
535 op->o_connid, err, cred ? cred->bv_len : -1 );
537 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
538 err, cred ? (long) cred->bv_len : -1, NULL );
542 tag = req2res( op->o_tag );
543 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
545 send_ldap_response( conn, op, tag, msgid,
546 err, matched, text, ref,
547 NULL, NULL, cred, ctrls );
551 slap_send_ldap_extended(
559 struct berval *rspdata,
567 LDAP_LOG( OPERATION, ENTRY,
568 "send_ldap_extended: err=%d oid=%s len=%ld\n",
569 err, rspoid ? rspoid : "",
570 rspdata != NULL ? rspdata->bv_len : 0 );
572 Debug( LDAP_DEBUG_TRACE,
573 "send_ldap_extended: err=%d oid=%s len=%ld\n",
575 rspoid ? rspoid : "",
576 rspdata != NULL ? rspdata->bv_len : 0 );
579 tag = req2res( op->o_tag );
580 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
582 send_ldap_response( conn, op, tag, msgid,
583 err, matched, text, refs,
584 rspoid, rspdata, NULL, ctrls );
587 #ifdef LDAP_RES_INTERMEDIATE_RESP
589 slap_send_ldap_intermediate_resp(
597 struct berval *rspdata,
598 LDAPControl **ctrls )
603 LDAP_LOG( OPERATION, ENTRY,
604 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
605 err, rspoid ? rspoid : "",
606 rspdata != NULL ? rspdata->bv_len : 0 );
608 Debug( LDAP_DEBUG_TRACE,
609 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
611 rspoid ? rspoid : "",
612 rspdata != NULL ? rspdata->bv_len : 0 );
614 tag = LDAP_RES_INTERMEDIATE_RESP;
615 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
616 send_ldap_response( conn, op, tag, msgid,
617 err, matched, text, refs,
618 rspoid, rspdata, NULL, ctrls );
623 slap_send_search_result(
638 assert( !LDAP_API_ERROR( err ) );
640 if (op->o_callback && op->o_callback->sc_sresult) {
641 op->o_callback->sc_sresult(conn, op, err, matched, text, refs,
647 LDAP_LOG( OPERATION, ENTRY,
648 "send_search_result: err=%d matched=\"%s\" text=\"%s\"\n",
649 err, matched ? matched : "", text ? text : "" );
651 Debug( LDAP_DEBUG_TRACE,
652 "send_search_result: err=%d matched=\"%s\" text=\"%s\"\n",
653 err, matched ? matched : "", text ? text : "" );
657 assert( err != LDAP_PARTIAL_RESULTS );
659 if( op->o_protocol < LDAP_VERSION3 ) {
660 /* send references in search results */
661 if( err == LDAP_REFERRAL ) {
662 err = LDAP_PARTIAL_RESULTS;
665 tmp = v2ref( refs, text );
670 /* don't send references in search results */
671 assert( refs == NULL );
674 if( err == LDAP_REFERRAL ) {
679 tag = req2res( op->o_tag );
680 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
682 send_ldap_response( conn, op, tag, msgid,
683 err, matched, text, refs,
684 NULL, NULL, NULL, ctrls );
688 snprintf( nbuf, sizeof nbuf, "%d nentries=%d", err, nentries );
690 Statslog( LDAP_DEBUG_STATS,
691 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
692 op->o_connid, op->o_opid, tag, nbuf, text ? text : "" );
701 slap_send_search_entry(
706 AttributeName *attrs,
711 char berbuf[LBER_ELEMENT_SIZEOF];
712 BerElement *ber = (BerElement *)berbuf;
714 int i, j, rc=-1, bytes;
718 AccessControlState acl_state = ACL_STATE_INIT;
720 /* Support for computed attribute plugins */
721 computed_attr_context ctx;
725 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
727 /* a_flags: array of flags telling if the i-th element will be
728 * returned or filtered out
729 * e_flags: array of a_flags
731 char **e_flags = NULL;
733 if (op->o_callback && op->o_callback->sc_sendentry) {
734 return op->o_callback->sc_sendentry( be, conn, op, e, attrs,
739 LDAP_LOG( OPERATION, ENTRY,
740 "send_search_entry: conn %lu dn=\"%s\"%s\n",
741 op->o_connid, e->e_dn, attrsonly ? " (attrsOnly)" : "" );
743 Debug( LDAP_DEBUG_TRACE,
744 "=> send_search_entry: dn=\"%s\"%s\n",
745 e->e_dn, attrsonly ? " (attrsOnly)" : "", 0 );
748 if ( ! access_allowed( be, conn, op, e,
749 ad_entry, NULL, ACL_READ, NULL ) )
753 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
754 op->o_connid, e->e_dn, 0 );
756 Debug( LDAP_DEBUG_ACL,
757 "send_search_entry: access to entry not allowed\n",
766 #ifdef LDAP_CONNECTIONLESS
771 ber_init_w_nullc( ber, LBER_USE_DER );
773 #ifdef LDAP_CONNECTIONLESS
774 if (conn->c_is_udp && conn->c_protocol == LDAP_VERSION2) {
775 rc = ber_printf(ber, "t{0{" /*}}*/,
776 LDAP_RES_SEARCH_ENTRY, &e->e_name);
780 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
781 LDAP_RES_SEARCH_ENTRY, &e->e_name );
786 LDAP_LOG( OPERATION, ERR,
787 "send_search_entry: conn %lu ber_printf failed\n",
788 op->o_connid, 0, 0 );
790 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
793 #ifdef LDAP_CONNECTIONLESS
794 if (conn->c_is_udp == 0)
797 send_ldap_result( conn, op, LDAP_OTHER,
798 NULL, "encoding DN error", NULL, NULL );
802 /* check for special all user attributes ("*") type */
803 userattrs = ( attrs == NULL ) ? 1
804 : an_find( attrs, &AllUser );
806 /* check for special all operational attributes ("+") type */
807 opattrs = ( attrs == NULL ) ? 0
808 : an_find( attrs, &AllOper );
810 /* create an array of arrays of flags. Each flag corresponds
811 * to particular value of attribute and equals 1 if value matches
812 * to ValuesReturnFilter or 0 if not
814 if ( op->vrFilter != NULL ) {
818 for ( a = e->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
819 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
822 size = i * sizeof(char *) + k;
825 e_flags = SLAP_CALLOC ( 1, i * sizeof(char *) + k );
826 if( e_flags == NULL ) {
828 LDAP_LOG( OPERATION, ERR,
829 "send_search_entry: conn %lu SLAP_CALLOC failed\n",
830 conn ? conn->c_connid : 0, 0, 0 );
832 Debug( LDAP_DEBUG_ANY,
833 "send_search_entry: SLAP_CALLOC failed\n", 0, 0, 0 );
837 send_ldap_result( conn, op, LDAP_OTHER,
838 NULL, "memory error",
842 a_flags = (char *)(e_flags + i);
843 memset( a_flags, 0, k );
844 for ( a = e->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
845 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
846 e_flags[i] = a_flags;
850 rc = filter_matched_values(be, conn, op, e->e_attrs, &e_flags) ;
853 LDAP_LOG( OPERATION, ERR,
854 "send_search_entry: conn %lu matched values filtering failed\n",
855 conn ? conn->c_connid : 0, 0, 0 );
857 Debug( LDAP_DEBUG_ANY,
858 "matched values filtering failed\n", 0, 0, 0 );
860 #ifdef LDAP_CONNECTIONLESS
861 if (conn->c_is_udp == 0)
865 send_ldap_result( conn, op, LDAP_OTHER,
866 NULL, "matched values filtering error",
873 for ( a = e->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
874 AttributeDescription *desc = a->a_desc;
876 if ( attrs == NULL ) {
877 /* all attrs request, skip operational attributes */
878 if( is_at_operational( desc->ad_type ) ) {
883 /* specific attrs requested */
884 if ( is_at_operational( desc->ad_type ) ) {
885 if( !opattrs && !ad_inlist( desc, attrs ) ) {
890 if (!userattrs && !ad_inlist( desc, attrs ) ) {
896 if ( ! access_allowed( be, conn, op, e, desc, NULL,
897 ACL_READ, &acl_state ) )
901 "send_search_entry: conn %lu access to attribute %s not "
902 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
904 Debug( LDAP_DEBUG_ACL, "acl: "
905 "access to attribute %s not allowed\n",
906 desc->ad_cname.bv_val, 0, 0 );
911 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
913 LDAP_LOG( OPERATION, ERR,
914 "send_search_entry: conn %lu ber_printf failed\n",
915 op->o_connid, 0, 0 );
917 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
920 #ifdef LDAP_CONNECTIONLESS
921 if (conn->c_is_udp == 0)
924 send_ldap_result( conn, op, LDAP_OTHER,
925 NULL, "encoding description error", NULL, NULL );
930 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
931 if ( ! access_allowed( be, conn, op, e,
932 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
936 "send_search_entry: conn %lu "
937 "access to attribute %s, value %d not allowed\n",
938 op->o_connid, desc->ad_cname.bv_val, i );
940 Debug( LDAP_DEBUG_ACL,
941 "acl: access to attribute %s, "
942 "value %d not allowed\n",
943 desc->ad_cname.bv_val, i, 0 );
949 if ( op->vrFilter && e_flags[j][i] == 0 ){
953 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
955 LDAP_LOG( OPERATION, ERR,
956 "send_search_entry: conn %lu "
957 "ber_printf failed.\n", op->o_connid, 0, 0 );
959 Debug( LDAP_DEBUG_ANY,
960 "ber_printf failed\n", 0, 0, 0 );
963 #ifdef LDAP_CONNECTIONLESS
964 if (conn->c_is_udp == 0)
967 send_ldap_result( conn, op, LDAP_OTHER,
968 NULL, "encoding values error",
975 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
977 LDAP_LOG( OPERATION, ERR,
978 "send_search_entry: conn %lu ber_printf failed\n",
979 op->o_connid, 0, 0 );
981 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
984 #ifdef LDAP_CONNECTIONLESS
985 if (conn->c_is_udp == 0)
988 send_ldap_result( conn, op, LDAP_OTHER,
989 NULL, "encode end error", NULL, NULL );
994 /* eventually will loop through generated operational attributes */
995 /* only have subschemaSubentry implemented */
996 aa = backend_operational( be, conn, op, e, attrs, opattrs );
998 if ( aa != NULL && op->vrFilter != NULL ) {
1002 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1003 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1006 size = i * sizeof(char *) + k;
1008 char *a_flags, **tmp;
1011 * Reuse previous memory - we likely need less space
1012 * for operational attributes
1014 tmp = SLAP_REALLOC ( e_flags, i * sizeof(char *) + k );
1015 if ( tmp == NULL ) {
1017 LDAP_LOG( OPERATION, ERR,
1018 "send_search_entry: conn %lu "
1019 "not enough memory "
1020 "for matched values filtering\n",
1021 conn ? conn->c_connid : 0, 0, 0);
1023 Debug( LDAP_DEBUG_ANY,
1024 "send_search_entry: conn %lu "
1025 "not enough memory "
1026 "for matched values filtering\n",
1027 conn ? conn->c_connid : 0, 0, 0 );
1031 send_ldap_result( conn, op, LDAP_NO_MEMORY,
1032 NULL, NULL, NULL, NULL );
1036 a_flags = (char *)(e_flags + i);
1037 memset( a_flags, 0, k );
1038 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1039 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1040 e_flags[i] = a_flags;
1043 rc = filter_matched_values(be, conn, op, aa, &e_flags) ;
1047 LDAP_LOG( OPERATION, ERR,
1048 "send_search_entry: conn %lu "
1049 "matched values filtering failed\n",
1050 conn ? conn->c_connid : 0, 0, 0);
1052 Debug( LDAP_DEBUG_ANY,
1053 "matched values filtering failed\n", 0, 0, 0 );
1055 #ifdef LDAP_CONNECTIONLESS
1056 if (conn->c_is_udp == 0)
1060 send_ldap_result( conn, op, LDAP_OTHER,
1061 NULL, "matched values filtering error",
1068 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
1069 AttributeDescription *desc = a->a_desc;
1071 if ( attrs == NULL ) {
1072 /* all attrs request, skip operational attributes */
1073 if( is_at_operational( desc->ad_type ) ) {
1078 /* specific attrs requested */
1079 if( is_at_operational( desc->ad_type ) ) {
1080 if( !opattrs && !ad_inlist( desc, attrs ) ) {
1084 if (!userattrs && !ad_inlist( desc, attrs ) )
1091 if ( ! access_allowed( be, conn, op, e, desc, NULL,
1092 ACL_READ, &acl_state ) )
1095 LDAP_LOG( ACL, INFO,
1096 "send_search_entry: conn %lu "
1097 "access to attribute %s not allowed\n",
1098 op->o_connid, desc->ad_cname.bv_val, 0 );
1100 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s "
1102 desc->ad_cname.bv_val, 0, 0 );
1108 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1111 LDAP_LOG( OPERATION, ERR,
1112 "send_search_entry: conn %lu "
1113 "ber_printf failed\n", op->o_connid, 0, 0 );
1115 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1118 #ifdef LDAP_CONNECTIONLESS
1119 if (conn->c_is_udp == 0)
1121 ber_free_buf( ber );
1122 send_ldap_result( conn, op, LDAP_OTHER,
1123 NULL, "encoding description error", NULL, NULL );
1129 if ( ! attrsonly ) {
1130 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1131 if ( ! access_allowed( be, conn, op, e,
1132 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1135 LDAP_LOG( ACL, INFO,
1136 "send_search_entry: conn %lu "
1137 "access to %s, value %d not allowed\n",
1138 op->o_connid, desc->ad_cname.bv_val, i );
1140 Debug( LDAP_DEBUG_ACL,
1141 "acl: access to attribute %s, "
1142 "value %d not allowed\n",
1143 desc->ad_cname.bv_val, i, 0 );
1149 if ( op->vrFilter && e_flags[j][i] == 0 ){
1153 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1155 LDAP_LOG( OPERATION, ERR,
1156 "send_search_entry: conn %lu ber_printf failed\n",
1157 op->o_connid, 0, 0 );
1159 Debug( LDAP_DEBUG_ANY,
1160 "ber_printf failed\n", 0, 0, 0 );
1163 #ifdef LDAP_CONNECTIONLESS
1164 if (conn->c_is_udp == 0)
1166 ber_free_buf( ber );
1167 send_ldap_result( conn, op, LDAP_OTHER,
1168 NULL, "encoding values error",
1177 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1179 LDAP_LOG( OPERATION, ERR,
1180 "send_search_entry: conn %lu ber_printf failed\n",
1181 op->o_connid, 0, 0 );
1183 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1186 #ifdef LDAP_CONNECTIONLESS
1187 if (conn->c_is_udp == 0)
1189 ber_free_buf( ber );
1190 send_ldap_result( conn, op, LDAP_OTHER,
1191 NULL, "encode end error", NULL, NULL );
1200 * First, setup the computed attribute context that is
1201 * passed to all plugins.
1203 ctx.cac_pb = op->o_pb;
1204 ctx.cac_attrs = attrs;
1205 ctx.cac_attrsonly = attrsonly;
1206 ctx.cac_userattrs = userattrs;
1207 ctx.cac_opattrs = opattrs;
1208 ctx.cac_acl_state = acl_state;
1209 ctx.cac_private = (void *)ber;
1212 * For each client requested attribute, call the plugins.
1214 if ( attrs != NULL ) {
1215 for ( anp = attrs; anp->an_name.bv_val != NULL; anp++ ) {
1216 rc = compute_evaluator( &ctx, anp->an_name.bv_val, e, slapi_x_compute_output_ber );
1223 * Technically we shouldn't be returning operational attributes
1224 * when the user requested only user attributes. We'll let the
1225 * plugin decide whether to be naughty or not.
1227 rc = compute_evaluator( &ctx, "*", e, slapi_x_compute_output_ber );
1230 ber_free_buf( ber );
1231 send_ldap_result( conn, op, LDAP_OTHER,
1232 NULL, "computed attribute error", NULL, NULL );
1235 #endif /* LDAP_SLAPI */
1244 rc = ber_printf( ber, /*{{*/ "}N}" );
1246 if( rc != -1 && ctrls != NULL ) {
1247 rc = send_ldap_controls( ber, ctrls );
1251 rc = ber_printf( ber, /*{*/ "N}" );
1256 LDAP_LOG( OPERATION, ERR,
1257 "send_search_entry: conn %lu ber_printf failed\n",
1258 op->o_connid, 0, 0 );
1260 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1263 #ifdef LDAP_CONNECTIONLESS
1264 if (conn->c_is_udp == 0)
1266 ber_free_buf( ber );
1267 send_ldap_result( conn, op, LDAP_OTHER,
1268 NULL, "encode entry end error", NULL, NULL );
1272 #ifdef LDAP_CONNECTIONLESS
1273 if (conn->c_is_udp == 0) {
1275 bytes = op->o_noop ? 0 : send_ldap_ber( conn, ber );
1276 ber_free_buf( ber );
1280 LDAP_LOG( OPERATION, ERR,
1281 "send_search_entry: conn %lu ber write failed.\n",
1282 op->o_connid, 0, 0 );
1284 Debug( LDAP_DEBUG_ANY,
1285 "send_search_entry: ber write failed\n",
1292 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1293 num_bytes_sent += bytes;
1296 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1298 #ifdef LDAP_CONNECTIONLESS
1302 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1303 conn->c_connid, op->o_opid, e->e_dn, 0, 0 );
1306 LDAP_LOG( OPERATION, ENTRY,
1307 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1309 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1315 if ( e_flags ) free( e_flags );
1320 slap_send_search_reference(
1326 LDAPControl **ctrls,
1330 char berbuf[LBER_ELEMENT_SIZEOF];
1331 BerElement *ber = (BerElement *)berbuf;
1335 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1336 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1338 if (op->o_callback && op->o_callback->sc_sendreference) {
1339 return op->o_callback->sc_sendreference( be, conn, op, e, refs, ctrls, v2refs );
1343 LDAP_LOG( OPERATION, ENTRY,
1344 "send_search_reference: conn %lu dn=\"%s\"\n",
1345 op->o_connid, e ? e->e_dn : "(null)", 0 );
1347 Debug( LDAP_DEBUG_TRACE,
1348 "=> send_search_reference: dn=\"%s\"\n",
1349 e ? e->e_dn : "(null)", 0, 0 );
1352 if ( e && ! access_allowed( be, conn, op, e,
1353 ad_entry, NULL, ACL_READ, NULL ) )
1356 LDAP_LOG( ACL, INFO,
1357 "send_search_reference: conn %lu "
1358 "access to entry %s not allowed\n",
1359 op->o_connid, e->e_dn, 0 );
1361 Debug( LDAP_DEBUG_ACL,
1362 "send_search_reference: access to entry not allowed\n",
1369 if ( e && ! access_allowed( be, conn, op, e,
1370 ad_ref, NULL, ACL_READ, NULL ) )
1373 LDAP_LOG( ACL, INFO,
1374 "send_search_reference: conn %lu access "
1375 "to reference not allowed.\n", op->o_connid, 0, 0 );
1377 Debug( LDAP_DEBUG_ACL,
1378 "send_search_reference: access "
1379 "to reference not allowed\n",
1386 #ifdef LDAP_CONTROL_NOREFERRALS
1387 if( op->o_noreferrals ) {
1389 LDAP_LOG( OPERATION, ERR,
1390 "send_search_reference: conn %lu noreferrals control in (%s).\n",
1391 op->o_connid, e->e_dn, 0 );
1393 Debug( LDAP_DEBUG_ANY,
1394 "send_search_reference: noreferrals control in (%s)\n",
1402 if( refs == NULL ) {
1404 LDAP_LOG( OPERATION, ERR,
1405 "send_search_reference: conn %lu null ref in (%s).\n",
1406 op->o_connid, e ? e->e_dn : "(null)", 0 );
1408 Debug( LDAP_DEBUG_ANY,
1409 "send_search_reference: null ref in (%s)\n",
1410 e ? e->e_dn : "(null)", 0, 0 );
1416 if( op->o_protocol < LDAP_VERSION3 ) {
1417 /* save the references for the result */
1418 if( refs[0].bv_val != NULL ) {
1419 if( value_add( v2refs, refs ) )
1425 #ifdef LDAP_CONNECTIONLESS
1427 ber = op->o_res_ber;
1430 ber_init_w_nullc( ber, LBER_USE_DER );
1432 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1433 LDAP_RES_SEARCH_REFERENCE, refs );
1435 if( rc != -1 && ctrls != NULL ) {
1436 rc = send_ldap_controls( ber, ctrls );
1440 rc = ber_printf( ber, /*"{"*/ "N}", op->o_msgid,
1441 LDAP_RES_SEARCH_REFERENCE, refs );
1446 LDAP_LOG( OPERATION, ERR,
1447 "send_search_reference: conn %lu "
1448 "ber_printf failed.\n", op->o_connid, 0, 0 );
1450 Debug( LDAP_DEBUG_ANY,
1451 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1454 #ifdef LDAP_CONNECTIONLESS
1455 if (conn->c_is_udp == 0)
1457 ber_free_buf( ber );
1458 send_ldap_result( conn, op, LDAP_OTHER,
1459 NULL, "encode DN error", NULL, NULL );
1463 #ifdef LDAP_CONNECTIONLESS
1464 if (conn->c_is_udp == 0) {
1466 bytes = op->o_noop ? 0 : send_ldap_ber( conn, ber );
1467 ber_free_buf( ber );
1469 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1470 num_bytes_sent += bytes;
1473 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1474 #ifdef LDAP_CONNECTIONLESS
1478 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1479 conn->c_connid, op->o_opid, e ? e->e_dn : "(null)", 0, 0 );
1482 LDAP_LOG( OPERATION, ENTRY,
1483 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1485 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1503 *code = LDAP_SUCCESS;
1507 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1509 LDAP_LOG( OPERATION, INFO,
1510 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1512 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1520 while ( (s = strchr( s, '\n' )) != NULL ) {
1525 if ( (c = strchr( s, ':' )) != NULL ) {
1529 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1533 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1537 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1543 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1545 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",