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;
53 if (ref[i]->bv_val[ref[i]->bv_len-1] != '/')
61 static ber_tag_t req2res( ber_tag_t tag )
66 case LDAP_REQ_COMPARE:
67 case LDAP_REQ_EXTENDED:
74 tag = LDAP_RES_DELETE;
77 case LDAP_REQ_ABANDON:
83 tag = LDAP_RES_SEARCH_RESULT;
93 static void trim_refs_urls(
94 struct berval **refs )
98 if( refs == NULL ) return;
100 for( i=0; refs[i] != NULL; i++ ) {
101 if( refs[i]->bv_len > sizeof("ldap://")-1 &&
102 strncasecmp( refs[i]->bv_val, "ldap://",
103 sizeof("ldap://")-1 ) == 0 )
106 for( j=sizeof("ldap://")-1; j<refs[i]->bv_len ; j++ ) {
107 if( refs[i]->bv_val[j] == '/' ) {
108 refs[i]->bv_val[j] = '\0';
117 struct berval **get_entry_referrals(
124 struct berval **refs;
127 attr = attr_find( e->e_attrs, "ref" );
129 if( attr == NULL ) return NULL;
131 for( i=0; attr->a_vals[i] != NULL; i++ ) {
132 /* count references */
135 if( i < 1 ) return NULL;
137 refs = ch_malloc( i + 1 );
139 for( i=0, j=0; attr->a_vals[i] != NULL; i++ ) {
141 struct berval *ref = ber_bvdup( attr->a_vals[i] );
144 for( k=0; k<ref->bv_len; k++ ) {
145 if( isspace(ref->bv_val[k]) ) {
146 ref->bv_val[k] = '\0';
152 if( ref->bv_len > 0 ) {
163 ber_bvecfree( refs );
167 /* we should check that a referral value exists... */
172 static long send_ldap_ber(
178 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
180 /* write only one pdu at a time - wait til it's our turn */
181 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
183 /* lock the connection */
184 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
190 if ( connection_state_closing( conn ) ) {
191 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
192 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
197 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
204 * we got an error. if it's ewouldblock, we need to
205 * wait on the socket being writable. otherwise, figure
206 * it's a hard error and return.
209 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
210 err, STRERROR(err), 0 );
212 if ( err != EWOULDBLOCK && err != EAGAIN ) {
213 connection_closing( conn );
215 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
216 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
221 /* wait for socket to be write-ready */
222 conn->c_writewaiter = 1;
223 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
225 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
226 conn->c_writewaiter = 0;
229 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
230 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
254 assert( ctrls == NULL ); /* ctrls not implemented */
256 ber = ber_alloc_t( LBER_USE_DER );
258 Debug( LDAP_DEBUG_TRACE, "send_ldap_response: msgid=%ld tag=%ld err=%ld\n",
259 (long) msgid, (long) tag, (long) err );
262 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
266 #ifdef LDAP_CONNECTIONLESS
268 rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
269 err, matched ? matched : "", text ? text : "" );
273 rc = ber_printf( ber, "{it{ess",
275 matched == NULL ? "" : matched,
276 text == NULL ? "" : text );
278 if( rc != -1 && ref != NULL ) {
279 rc = ber_printf( ber, "{V}", ref );
282 if( rc != -1 && resoid != NULL ) {
283 rc = ber_printf( ber, "s", resoid );
286 if( rc != -1 && data != NULL ) {
287 rc = ber_printf( ber, "O", data );
291 rc = ber_printf( ber, "}}" );
296 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
302 bytes = send_ldap_ber( conn, ber );
306 Debug( LDAP_DEBUG_ANY,
307 "send_ldap_response: ber write failed\n",
312 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
313 num_bytes_sent += bytes;
315 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
321 send_ldap_disconnect(
332 #define LDAP_UNSOLICITED_ERROR(e) \
333 ( (e) == LDAP_PROTOCOL_ERROR \
334 || (e) == LDAP_STRONG_AUTH_REQUIRED \
335 || (e) == LDAP_UNAVAILABLE )
337 assert( LDAP_UNSOLICITED_ERROR( err ) );
339 Debug( LDAP_DEBUG_TRACE,
340 "send_ldap_disconnect %d:%s\n",
341 err, text ? text : "", NULL );
343 if ( op->o_protocol < LDAP_VERSION3 ) {
345 tag = req2res( op->o_tag );
346 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
349 reqoid = LDAP_NOTICE_DISCONNECT;
350 tag = LDAP_RES_EXTENDED;
354 #ifdef LDAP_CONNECTIONLESS
356 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
357 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
358 inet_ntoa(((struct sockaddr_in *)
359 &op->o_clientaddr)->sin_addr ),
360 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
364 send_ldap_response( conn, op, tag, msgid,
365 err, NULL, text, NULL,
366 reqoid, NULL, NULL );
368 Statslog( LDAP_DEBUG_STATS,
369 "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
370 (long) op->o_connid, (long) op->o_opid,
371 (long) tag, (long) err, text ? text : "" );
389 assert( !LDAP_API_ERROR( err ) );
391 Debug( LDAP_DEBUG_TRACE, "send_ldap_result: conn=%ld op=%ld p=%d\n",
392 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
393 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: %d:%s:%s\n",
394 err, matched ? matched : "", text ? text : "" );
396 assert( err != LDAP_PARTIAL_RESULTS );
398 if( op->o_tag != LDAP_REQ_SEARCH ) {
399 trim_refs_urls( ref );
402 if ( err == LDAP_REFERRAL ) {
404 err = LDAP_NO_SUCH_OBJECT;
405 } else if ( op->o_protocol < LDAP_VERSION3 ) {
406 err = LDAP_PARTIAL_RESULTS;
410 if ( op->o_protocol < LDAP_VERSION3 ) {
411 tmp = v2ref( ref, text );
416 tag = req2res( op->o_tag );
417 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
419 #ifdef LDAP_CONNECTIONLESS
421 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
422 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
423 inet_ntoa(((struct sockaddr_in *)
424 &op->o_clientaddr)->sin_addr ),
425 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
430 send_ldap_response( conn, op, tag, msgid,
431 err, matched, text, ref,
434 Statslog( LDAP_DEBUG_STATS,
435 "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
436 (long) op->o_connid, (long) op->o_opid,
437 (long) tag, (long) err, text ? text : "" );
457 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl %ld\n",
458 (long) err, NULL, NULL );
460 tag = req2res( op->o_tag );
461 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
463 #ifdef LDAP_CONNECTIONLESS
465 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
466 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
467 inet_ntoa(((struct sockaddr_in *)
468 &op->o_clientaddr)->sin_addr ),
469 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
474 send_ldap_response( conn, op, tag, msgid,
475 err, matched, text, NULL,
487 struct berval *rspdata
493 Debug( LDAP_DEBUG_TRACE,
494 "send_ldap_extended %ld:%s\n",
495 (long) err, rspoid ? rspoid : "", NULL );
497 tag = req2res( op->o_tag );
498 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
500 #ifdef LDAP_CONNECTIONLESS
502 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
503 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
504 inet_ntoa(((struct sockaddr_in *)
505 &op->o_clientaddr)->sin_addr ),
506 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
510 send_ldap_response( conn, op, tag, msgid,
511 err, matched, text, NULL,
512 rspoid, rspdata, NULL );
523 struct berval **refs,
531 assert( !LDAP_API_ERROR( err ) );
533 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
534 err, matched ? matched : "", text ? text : "" );
536 assert( err != LDAP_PARTIAL_RESULTS );
538 trim_refs_urls( refs );
540 if( op->o_protocol < LDAP_VERSION3 ) {
541 /* send references in search results */
542 if( err == LDAP_REFERRAL ) {
543 err = LDAP_PARTIAL_RESULTS;
546 tmp = v2ref( refs, text );
550 /* don't send references in search results */
551 assert( refs == NULL );
554 if( err == LDAP_REFERRAL ) {
559 tag = req2res( op->o_tag );
560 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
562 #ifdef LDAP_CONNECTIONLESS
564 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
565 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
566 inet_ntoa(((struct sockaddr_in *)
567 &op->o_clientaddr)->sin_addr ),
568 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
573 send_ldap_response( conn, op, tag, msgid,
574 err, matched, text, refs,
577 Statslog( LDAP_DEBUG_STATS,
578 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
579 (long) op->o_connid, (long) op->o_opid,
580 (long) tag, (long) err, text ? text : "" );
605 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: \"%s\"\n", e->e_dn, 0, 0 );
607 if ( ! access_allowed( be, conn, op, e,
608 "entry", NULL, ACL_READ ) )
610 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
617 ber = ber_alloc_t( LBER_USE_DER );
620 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
621 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
622 NULL, "allocating BER error", NULL, NULL );
626 rc = ber_printf( ber, "{it{s{", op->o_msgid,
627 LDAP_RES_SEARCH_ENTRY, e->e_dn );
630 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
632 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
633 NULL, "encoding dn error", NULL, NULL );
637 /* check for special all user attributes ("*") type */
638 userattrs = ( attrs == NULL ) ? 1
639 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
641 /* check for special all operational attributes ("+") type */
642 opattrs = ( attrs == NULL ) ? 0
643 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
645 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
646 if ( attrs == NULL ) {
647 /* all addrs request, skip operational attributes */
648 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
653 /* specific addrs requested */
654 if ( oc_check_operational_attr( a->a_type ) ) {
655 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
660 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
667 if ( ! access_allowed( be, conn, op, e,
668 a->a_type, NULL, ACL_READ ) )
670 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
675 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
676 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
678 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
679 NULL, "encoding type error", NULL, NULL );
684 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
685 if ( ! access_allowed( be, conn, op, e,
686 a->a_type, a->a_vals[i], ACL_READ ) )
688 Debug( LDAP_DEBUG_ACL,
689 "acl: access to attribute %s, value %d not allowed\n",
694 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
695 Debug( LDAP_DEBUG_ANY,
696 "ber_printf failed\n", 0, 0, 0 );
698 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
699 NULL, "encoding value error", NULL, NULL );
705 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
706 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
708 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
709 NULL, "encode end error", NULL, NULL );
714 #ifdef SLAPD_SCHEMA_DN
715 /* eventually will loop through generated operational attributes */
716 /* only have subschemaSubentry implemented */
717 a = backend_subschemasubentry( be );
720 if ( attrs == NULL ) {
721 /* all addrs request, skip operational attributes */
722 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
727 /* specific addrs requested */
728 if ( oc_check_operational_attr( a->a_type ) ) {
729 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
734 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
741 if ( ! access_allowed( be, conn, op, e,
742 a->a_type, NULL, ACL_READ ) )
744 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
749 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
750 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
752 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
753 NULL, "encoding type error", NULL, NULL );
758 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
759 if ( ! access_allowed( be, conn, op, e,
760 a->a_type, a->a_vals[i], ACL_READ ) )
762 Debug( LDAP_DEBUG_ACL,
763 "acl: access to attribute %s, value %d not allowed\n",
769 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
770 Debug( LDAP_DEBUG_ANY,
771 "ber_printf failed\n", 0, 0, 0 );
773 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
774 NULL, "encoding value error", NULL, NULL );
780 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
781 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
783 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
784 NULL, "encode end error", NULL, NULL );
790 rc = ber_printf( ber, /*{{{*/ "}}}" );
793 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
795 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
796 NULL, "encode entry end error", NULL, NULL );
800 bytes = send_ldap_ber( conn, ber );
804 Debug( LDAP_DEBUG_ANY,
805 "send_ldap_response: ber write failed\n",
810 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
811 num_bytes_sent += bytes;
814 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
816 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
817 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
819 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
828 send_search_reference(
833 struct berval **refs,
836 struct berval ***v2refs
843 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
845 if ( ! access_allowed( be, conn, op, e,
846 "entry", NULL, ACL_READ ) )
848 Debug( LDAP_DEBUG_ACL,
849 "send_search_reference: access to entry not allowed\n",
854 if ( ! access_allowed( be, conn, op, e,
855 "ref", NULL, ACL_READ ) )
857 Debug( LDAP_DEBUG_ACL,
858 "send_search_reference: access to reference not allowed\n",
864 Debug( LDAP_DEBUG_ANY,
865 "send_search_reference: null ref in (%s)\n",
870 if( op->o_protocol < LDAP_VERSION3 ) {
871 /* save the references for the result */
872 if( *refs != NULL ) {
873 value_add( v2refs, refs );
878 ber = ber_alloc_t( LBER_USE_DER );
881 Debug( LDAP_DEBUG_ANY,
882 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
883 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
884 NULL, "alloc BER error", NULL, NULL );
888 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
889 LDAP_RES_SEARCH_REFERENCE, refs );
892 Debug( LDAP_DEBUG_ANY,
893 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
895 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
896 NULL, "encode dn error", NULL, NULL );
900 bytes = send_ldap_ber( conn, ber );
903 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
904 num_bytes_sent += bytes;
907 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
909 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
910 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
912 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
929 *code = LDAP_SUCCESS;
933 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
934 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
941 while ( (s = strchr( s, '\n' )) != NULL ) {
946 if ( (c = strchr( s, ':' )) != NULL ) {
950 if ( strncasecmp( s, "code", 4 ) == 0 ) {
954 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
958 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
963 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",