1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <ac/signal.h>
11 #include <ac/string.h>
14 #include <ac/unistd.h>
18 /* we need LBER internals */
19 #include "../../libraries/liblber/lber-int.h"
21 static char *v2ref( struct berval **ref, const char *text )
23 size_t len = 0, i = 0;
29 return ch_strdup(text);
36 if (text[len-1] != '\n')
39 v2 = ch_malloc( len+i+sizeof("Referral:") );
45 strcpy( v2+len, "Referral:" );
46 len += sizeof("Referral:");
48 for( i=0; ref[i] != NULL; i++ ) {
49 v2 = ch_realloc( v2, len + ref[i]->bv_len + 1 );
51 memcpy(&v2[len], ref[i]->bv_val, ref[i]->bv_len );
52 len += ref[i]->bv_len + 1;
59 static ber_tag_t req2res( ber_tag_t tag )
64 case LDAP_REQ_COMPARE:
65 case LDAP_REQ_EXTENDED:
72 tag = LDAP_RES_DELETE;
75 case LDAP_REQ_ABANDON:
81 tag = LDAP_RES_SEARCH_RESULT;
91 static void trim_refs_urls(
92 struct berval **refs )
96 if( refs == NULL ) return;
98 for( i=0; refs[i] != NULL; i++ ) {
99 if( refs[i]->bv_len > sizeof("ldap://")-1 &&
100 strncasecmp( refs[i]->bv_val, "ldap://",
101 sizeof("ldap://")-1 ) == 0 )
104 for( j=sizeof("ldap://")-1; j<refs[i]->bv_len ; j++ ) {
105 if( refs[i]->bv_val[j] == '/' ) {
106 refs[i]->bv_val[j] = '\0';
115 struct berval **get_entry_referrals(
122 struct berval **refs;
125 attr = attr_find( e->e_attrs, "ref" );
127 if( attr == NULL ) return NULL;
129 for( i=0; attr->a_vals[i] != NULL; i++ ) {
130 /* count references */
133 if( i < 1 ) return NULL;
135 refs = ch_malloc( i + 1 );
137 for( i=0, j=0; attr->a_vals[i] != NULL; i++ ) {
139 struct berval *ref = ber_bvdup( attr->a_vals[i] );
142 for( k=0; k<ref->bv_len; k++ ) {
143 if( isspace(ref->bv_val[k]) ) {
144 ref->bv_val[k] = '\0';
150 if( ref->bv_len > 0 ) {
161 ber_bvecfree( refs );
165 /* we should check that a referral value exists... */
170 static long send_ldap_ber(
176 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
178 /* write only one pdu at a time - wait til it's our turn */
179 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
181 /* lock the connection */
182 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
188 if ( connection_state_closing( conn ) ) {
189 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
190 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
195 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
202 * we got an error. if it's ewouldblock, we need to
203 * wait on the socket being writable. otherwise, figure
204 * it's a hard error and return.
207 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
208 err, STRERROR(err), 0 );
210 if ( err != EWOULDBLOCK && err != EAGAIN ) {
211 connection_closing( conn );
213 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
214 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
219 /* wait for socket to be write-ready */
220 conn->c_writewaiter = 1;
221 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
223 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
224 conn->c_writewaiter = 0;
227 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
228 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
252 assert( ctrls == NULL ); /* ctrls not implemented */
254 ber = ber_alloc_t( LBER_USE_DER );
256 Debug( LDAP_DEBUG_TRACE, "send_ldap_response: msgid=%ld tag=%ld err=%ld\n",
257 (long) msgid, (long) tag, (long) err );
260 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
264 #ifdef LDAP_CONNECTIONLESS
266 rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
267 err, matched ? matched : "", text ? text : "" );
271 rc = ber_printf( ber, "{it{ess",
273 matched == NULL ? "" : matched,
274 text == NULL ? "" : text );
276 if( rc != -1 && ref != NULL ) {
277 rc = ber_printf( ber, "{V}", ref );
280 if( rc != -1 && resoid != NULL ) {
281 rc = ber_printf( ber, "s", resoid );
284 if( rc != -1 && data != NULL ) {
285 rc = ber_printf( ber, "O", data );
289 rc = ber_printf( ber, "}}" );
294 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
300 bytes = send_ldap_ber( conn, ber );
304 Debug( LDAP_DEBUG_ANY,
305 "send_ldap_response: ber write failed\n",
310 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
311 num_bytes_sent += bytes;
313 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
319 send_ldap_disconnect(
330 #define LDAP_UNSOLICITED_ERROR(e) \
331 ( (e) == LDAP_PROTOCOL_ERROR \
332 || (e) == LDAP_STRONG_AUTH_REQUIRED \
333 || (e) == LDAP_UNAVAILABLE )
335 assert( LDAP_UNSOLICITED_ERROR( err ) );
337 Debug( LDAP_DEBUG_TRACE,
338 "send_ldap_disconnect %d:%s\n",
339 err, text ? text : "", NULL );
341 if ( op->o_protocol < LDAP_VERSION3 ) {
343 tag = req2res( op->o_tag );
344 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
347 reqoid = LDAP_NOTICE_DISCONNECT;
348 tag = LDAP_RES_EXTENDED;
352 #ifdef LDAP_CONNECTIONLESS
354 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
355 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
356 inet_ntoa(((struct sockaddr_in *)
357 &op->o_clientaddr)->sin_addr ),
358 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
362 send_ldap_response( conn, op, tag, msgid,
363 err, NULL, text, NULL,
364 reqoid, NULL, NULL );
366 Statslog( LDAP_DEBUG_STATS,
367 "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
368 (long) op->o_connid, (long) op->o_opid,
369 (long) tag, (long) err, text ? text : "" );
387 assert( !LDAP_API_ERROR( err ) );
389 Debug( LDAP_DEBUG_TRACE, "send_ldap_result: conn=%ld op=%ld p=%d\n",
390 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
391 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: %d:%s:%s\n",
392 err, matched ? matched : "", text ? text : "" );
394 assert( err != LDAP_PARTIAL_RESULTS );
396 if( op->o_tag != LDAP_REQ_SEARCH ) {
397 trim_refs_urls( ref );
400 if ( err == LDAP_REFERRAL ) {
402 err = LDAP_NO_SUCH_OBJECT;
403 } else if ( op->o_protocol < LDAP_VERSION3 ) {
404 err = LDAP_PARTIAL_RESULTS;
408 if ( op->o_protocol < LDAP_VERSION3 ) {
409 tmp = v2ref( ref, text );
414 tag = req2res( op->o_tag );
415 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
417 #ifdef LDAP_CONNECTIONLESS
419 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
420 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
421 inet_ntoa(((struct sockaddr_in *)
422 &op->o_clientaddr)->sin_addr ),
423 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
428 send_ldap_response( conn, op, tag, msgid,
429 err, matched, text, ref,
432 Statslog( LDAP_DEBUG_STATS,
433 "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
434 (long) op->o_connid, (long) op->o_opid,
435 (long) tag, (long) err, text ? text : "" );
455 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl %ld\n",
456 (long) err, NULL, NULL );
458 tag = req2res( op->o_tag );
459 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
461 #ifdef LDAP_CONNECTIONLESS
463 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
464 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
465 inet_ntoa(((struct sockaddr_in *)
466 &op->o_clientaddr)->sin_addr ),
467 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
472 send_ldap_response( conn, op, tag, msgid,
473 err, matched, text, NULL,
485 struct berval *rspdata
491 Debug( LDAP_DEBUG_TRACE,
492 "send_ldap_extended %ld:%s\n",
493 (long) err, rspoid ? rspoid : "", NULL );
495 tag = req2res( op->o_tag );
496 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
498 #ifdef LDAP_CONNECTIONLESS
500 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
501 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
502 inet_ntoa(((struct sockaddr_in *)
503 &op->o_clientaddr)->sin_addr ),
504 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
508 send_ldap_response( conn, op, tag, msgid,
509 err, matched, text, NULL,
510 rspoid, rspdata, NULL );
521 struct berval **refs,
529 assert( !LDAP_API_ERROR( err ) );
531 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
532 err, matched ? matched : "", text ? text : "" );
534 assert( err != LDAP_PARTIAL_RESULTS );
536 trim_refs_urls( refs );
538 if( op->o_protocol < LDAP_VERSION3 ) {
539 /* send references in search results */
540 if( err == LDAP_REFERRAL ) {
541 err = LDAP_PARTIAL_RESULTS;
544 tmp = v2ref( refs, text );
548 /* don't send references in search results */
549 assert( refs == NULL );
552 if( err == LDAP_REFERRAL ) {
557 tag = req2res( op->o_tag );
558 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
560 #ifdef LDAP_CONNECTIONLESS
562 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
563 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
564 inet_ntoa(((struct sockaddr_in *)
565 &op->o_clientaddr)->sin_addr ),
566 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
571 send_ldap_response( conn, op, tag, msgid,
572 err, matched, text, refs,
575 Statslog( LDAP_DEBUG_STATS,
576 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
577 (long) op->o_connid, (long) op->o_opid,
578 (long) tag, (long) err, text ? text : "" );
603 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: \"%s\"\n", e->e_dn, 0, 0 );
605 if ( ! access_allowed( be, conn, op, e,
606 "entry", NULL, ACL_READ ) )
608 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
615 ber = ber_alloc_t( LBER_USE_DER );
618 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
619 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
620 NULL, "allocating BER error", NULL, NULL );
624 rc = ber_printf( ber, "{it{s{", op->o_msgid,
625 LDAP_RES_SEARCH_ENTRY, e->e_dn );
628 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
630 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
631 NULL, "encoding dn error", NULL, NULL );
635 /* check for special all user attributes ("*") type */
636 userattrs = ( attrs == NULL ) ? 1
637 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
639 /* check for special all operational attributes ("+") type */
640 opattrs = ( attrs == NULL ) ? 0
641 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
643 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
644 if ( attrs == NULL ) {
645 /* all addrs request, skip operational attributes */
646 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
651 /* specific addrs requested */
652 if ( oc_check_operational_attr( a->a_type ) ) {
653 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
658 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
665 if ( ! access_allowed( be, conn, op, e,
666 a->a_type, NULL, ACL_READ ) )
668 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
673 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
674 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
676 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
677 NULL, "encoding type error", NULL, NULL );
682 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
683 if ( ! access_allowed( be, conn, op, e,
684 a->a_type, a->a_vals[i], ACL_READ ) )
686 Debug( LDAP_DEBUG_ACL,
687 "acl: access to attribute %s, value %d not allowed\n",
692 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
693 Debug( LDAP_DEBUG_ANY,
694 "ber_printf failed\n", 0, 0, 0 );
696 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
697 NULL, "encoding value error", NULL, NULL );
703 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
704 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
706 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
707 NULL, "encode end error", NULL, NULL );
712 #ifdef SLAPD_SCHEMA_DN
713 /* eventually will loop through generated operational attributes */
714 /* only have subschemaSubentry implemented */
715 a = backend_subschemasubentry( be );
718 if ( attrs == NULL ) {
719 /* all addrs request, skip operational attributes */
720 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
725 /* specific addrs requested */
726 if ( oc_check_operational_attr( a->a_type ) ) {
727 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
732 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
739 if ( ! access_allowed( be, conn, op, e,
740 a->a_type, NULL, ACL_READ ) )
742 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
747 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
748 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
750 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
751 NULL, "encoding type error", NULL, NULL );
756 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
757 if ( ! access_allowed( be, conn, op, e,
758 a->a_type, a->a_vals[i], ACL_READ ) )
760 Debug( LDAP_DEBUG_ACL,
761 "acl: access to attribute %s, value %d not allowed\n",
767 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
768 Debug( LDAP_DEBUG_ANY,
769 "ber_printf failed\n", 0, 0, 0 );
771 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
772 NULL, "encoding value error", NULL, NULL );
778 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
779 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
781 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
782 NULL, "encode end error", NULL, NULL );
788 rc = ber_printf( ber, /*{{{*/ "}}}" );
791 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
793 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
794 NULL, "encode entry end error", NULL, NULL );
798 bytes = send_ldap_ber( conn, ber );
802 Debug( LDAP_DEBUG_ANY,
803 "send_ldap_response: ber write failed\n",
808 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
809 num_bytes_sent += bytes;
812 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
814 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
815 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
817 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
826 send_search_reference(
831 struct berval **refs,
834 struct berval ***v2refs
841 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
843 if ( ! access_allowed( be, conn, op, e,
844 "entry", NULL, ACL_READ ) )
846 Debug( LDAP_DEBUG_ACL,
847 "send_search_reference: access to entry not allowed\n",
852 if ( ! access_allowed( be, conn, op, e,
853 "ref", NULL, ACL_READ ) )
855 Debug( LDAP_DEBUG_ACL,
856 "send_search_reference: access to reference not allowed\n",
862 Debug( LDAP_DEBUG_ANY,
863 "send_search_reference: null ref in (%s)\n",
868 if( op->o_protocol < LDAP_VERSION3 ) {
869 /* save the references for the result */
870 if( *refs == NULL ) {
871 value_add( v2refs, refs );
876 ber = ber_alloc_t( LBER_USE_DER );
879 Debug( LDAP_DEBUG_ANY,
880 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
881 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
882 NULL, "alloc BER error", NULL, NULL );
886 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
887 LDAP_RES_SEARCH_REFERENCE, refs );
890 Debug( LDAP_DEBUG_ANY,
891 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
893 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
894 NULL, "encode dn error", NULL, NULL );
898 bytes = send_ldap_ber( conn, ber );
901 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
902 num_bytes_sent += bytes;
905 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
907 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
908 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
910 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
927 *code = LDAP_SUCCESS;
931 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
932 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
939 while ( (s = strchr( s, '\n' )) != NULL ) {
944 if ( (c = strchr( s, ':' )) != NULL ) {
948 if ( strncasecmp( s, "code", 4 ) == 0 ) {
952 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
956 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
961 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",