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;
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(
174 ber_len_t bytes = ber_pvt_ber_bytes( ber );
176 /* write only one pdu at a time - wait til it's our turn */
177 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
179 /* lock the connection */
180 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
186 if ( connection_state_closing( conn ) ) {
187 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
188 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
193 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
200 * we got an error. if it's ewouldblock, we need to
201 * wait on the socket being writable. otherwise, figure
202 * it's a hard error and return.
205 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
206 err, STRERROR(err), 0 );
208 if ( err != EWOULDBLOCK && err != EAGAIN ) {
209 connection_closing( conn );
211 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
212 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
217 /* wait for socket to be write-ready */
218 conn->c_writewaiter = 1;
219 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
221 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
222 conn->c_writewaiter = 0;
225 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
226 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
242 struct berval *resdata,
250 assert( ctrls == NULL ); /* ctrls not implemented */
252 ber = ber_alloc_t( LBER_USE_DER );
254 Debug( LDAP_DEBUG_TRACE, "send_ldap_response: msgid=%ld tag=%ld err=%ld\n",
255 (long) msgid, (long) tag, (long) err );
258 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
262 #ifdef LDAP_CONNECTIONLESS
264 rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
265 err, matched ? matched : "", text ? text : "" );
269 rc = ber_printf( ber, "{it{ess",
271 matched == NULL ? "" : matched,
272 text == NULL ? "" : text );
274 if( rc != -1 && ref != NULL ) {
275 rc = ber_printf( ber, "{V}", ref );
278 if( rc != -1 && resoid != NULL ) {
279 rc = ber_printf( ber, "s", resoid );
282 if( rc != -1 && resdata != NULL ) {
283 rc = ber_printf( ber, "O", resdata );
288 rc = ber_printf( ber, "}}" );
293 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
299 bytes = send_ldap_ber( conn, ber );
303 Debug( LDAP_DEBUG_ANY,
304 "send_ldap_response: ber write failed\n",
309 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
310 num_bytes_sent += bytes;
312 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
318 send_ldap_disconnect(
329 #define LDAP_UNSOLICITED_ERROR(e) \
330 ( (e) == LDAP_PROTOCOL_ERROR \
331 || (e) == LDAP_STRONG_AUTH_REQUIRED \
332 || (e) == LDAP_UNAVAILABLE )
334 assert( LDAP_UNSOLICITED_ERROR( err ) );
336 Debug( LDAP_DEBUG_TRACE,
337 "send_ldap_disconnect %d:%s\n",
338 err, text ? text : "", NULL );
340 if ( op->o_protocol < LDAP_VERSION3 ) {
342 tag = req2res( op->o_tag );
343 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
346 reqoid = LDAP_NOTICE_DISCONNECT;
347 tag = LDAP_RES_EXTENDED;
351 #ifdef LDAP_CONNECTIONLESS
353 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
354 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
355 inet_ntoa(((struct sockaddr_in *)
356 &op->o_clientaddr)->sin_addr ),
357 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
361 send_ldap_response( conn, op, tag, msgid,
362 err, NULL, text, NULL,
363 reqoid, NULL, NULL );
365 Statslog( LDAP_DEBUG_STATS,
366 "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
367 (long) op->o_connid, (long) op->o_opid,
368 (long) tag, (long) err, text ? text : "" );
386 assert( !LDAP_API_ERROR( err ) );
388 Debug( LDAP_DEBUG_TRACE, "send_ldap_result: conn=%ld op=%ld p=%d\n",
389 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
390 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: %d:%s:%s\n",
391 err, matched ? matched : "", text ? text : "" );
393 assert( err != LDAP_PARTIAL_RESULTS );
395 if( op->o_tag != LDAP_REQ_SEARCH ) {
396 trim_refs_urls( ref );
399 if ( err == LDAP_REFERRAL ) {
401 err = LDAP_NO_SUCH_OBJECT;
402 } else if ( op->o_protocol < LDAP_VERSION3 ) {
403 err = LDAP_PARTIAL_RESULTS;
407 if ( op->o_protocol < LDAP_VERSION3 ) {
408 tmp = v2ref( ref, text );
413 tag = req2res( op->o_tag );
414 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
416 #ifdef LDAP_CONNECTIONLESS
418 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
419 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
420 inet_ntoa(((struct sockaddr_in *)
421 &op->o_clientaddr)->sin_addr ),
422 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
427 send_ldap_response( conn, op, tag, msgid,
428 err, matched, text, ref,
431 Statslog( LDAP_DEBUG_STATS,
432 "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
433 (long) op->o_connid, (long) op->o_opid,
434 (long) tag, (long) err, text ? text : "" );
449 struct berval **refs,
457 assert( !LDAP_API_ERROR( err ) );
459 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
460 err, matched ? matched : "", text ? text : "" );
462 assert( err != LDAP_PARTIAL_RESULTS );
464 trim_refs_urls( refs );
466 if( op->o_protocol < LDAP_VERSION3 ) {
467 /* send references in search results */
468 if( err == LDAP_REFERRAL ) {
469 err = LDAP_PARTIAL_RESULTS;
472 tmp = v2ref( refs, text );
476 /* don't send references in search results */
477 assert( refs == NULL );
480 if( err == LDAP_REFERRAL ) {
485 tag = req2res( op->o_tag );
486 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
488 #ifdef LDAP_CONNECTIONLESS
490 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
491 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
492 inet_ntoa(((struct sockaddr_in *)
493 &op->o_clientaddr)->sin_addr ),
494 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
499 send_ldap_response( conn, op, tag, msgid,
500 err, matched, text, refs,
503 Statslog( LDAP_DEBUG_STATS,
504 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
505 (long) op->o_connid, (long) op->o_opid,
506 (long) tag, (long) err, text ? text : "" );
531 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: \"%s\"\n", e->e_dn, 0, 0 );
533 if ( ! access_allowed( be, conn, op, e,
534 "entry", NULL, ACL_READ ) )
536 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
543 ber = ber_alloc_t( LBER_USE_DER );
546 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
547 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
548 NULL, "allocating BER error", NULL, NULL );
552 rc = ber_printf( ber, "{it{s{", op->o_msgid,
553 LDAP_RES_SEARCH_ENTRY, e->e_dn );
556 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
558 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
559 NULL, "encoding dn error", NULL, NULL );
563 /* check for special all user attributes ("*") type */
564 userattrs = ( attrs == NULL ) ? 1
565 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
567 /* check for special all operational attributes ("+") type */
568 opattrs = ( attrs == NULL ) ? 0
569 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
571 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
572 if ( attrs == NULL ) {
573 /* all addrs request, skip operational attributes */
574 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
579 /* specific addrs requested */
580 if ( oc_check_operational_attr( a->a_type ) ) {
581 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
586 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
593 if ( ! access_allowed( be, conn, op, e,
594 a->a_type, NULL, ACL_READ ) )
596 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
601 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
602 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
604 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
605 NULL, "encoding type error", NULL, NULL );
610 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
611 if ( ! access_allowed( be, conn, op, e,
612 a->a_type, a->a_vals[i], ACL_READ ) )
614 Debug( LDAP_DEBUG_ACL,
615 "acl: access to attribute %s, value %d not allowed\n",
620 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
621 Debug( LDAP_DEBUG_ANY,
622 "ber_printf failed\n", 0, 0, 0 );
624 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
625 NULL, "encoding value error", NULL, NULL );
631 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
632 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
634 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
635 NULL, "encode end error", NULL, NULL );
640 #ifdef SLAPD_SCHEMA_DN
641 /* eventually will loop through generated operational attributes */
642 /* only have subschemaSubentry implemented */
643 a = backend_subschemasubentry( be );
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",
695 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
696 Debug( LDAP_DEBUG_ANY,
697 "ber_printf failed\n", 0, 0, 0 );
699 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
700 NULL, "encoding value error", NULL, NULL );
706 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
707 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
709 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
710 NULL, "encode end error", NULL, NULL );
716 rc = ber_printf( ber, /*{{{*/ "}}}" );
719 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
721 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
722 NULL, "encode entry end error", NULL, NULL );
726 bytes = send_ldap_ber( conn, ber );
730 Debug( LDAP_DEBUG_ANY,
731 "send_ldap_response: ber write failed\n",
736 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
737 num_bytes_sent += bytes;
740 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
742 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
743 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
745 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
754 send_search_reference(
759 struct berval **refs,
762 struct berval ***v2refs
769 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
771 if ( ! access_allowed( be, conn, op, e,
772 "entry", NULL, ACL_READ ) )
774 Debug( LDAP_DEBUG_ACL,
775 "send_search_reference: access to entry not allowed\n",
780 if ( ! access_allowed( be, conn, op, e,
781 "ref", NULL, ACL_READ ) )
783 Debug( LDAP_DEBUG_ACL,
784 "send_search_reference: access to reference not allowed\n",
790 Debug( LDAP_DEBUG_ANY,
791 "send_search_reference: null ref in (%s)\n",
796 if( op->o_protocol < LDAP_VERSION3 ) {
797 /* save the references for the result */
798 if( *refs == NULL ) {
799 value_add( v2refs, refs );
804 ber = ber_alloc_t( LBER_USE_DER );
807 Debug( LDAP_DEBUG_ANY,
808 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
809 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
810 NULL, "alloc BER error", NULL, NULL );
814 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
815 LDAP_RES_SEARCH_REFERENCE, refs );
818 Debug( LDAP_DEBUG_ANY,
819 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
821 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
822 NULL, "encode dn error", NULL, NULL );
826 bytes = send_ldap_ber( conn, ber );
829 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
830 num_bytes_sent += bytes;
833 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
835 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
836 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
838 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
855 *code = LDAP_SUCCESS;
859 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
860 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
867 while ( (s = strchr( s, '\n' )) != NULL ) {
872 if ( (c = strchr( s, ':' )) != NULL ) {
876 if ( strncasecmp( s, "code", 4 ) == 0 ) {
880 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
884 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
889 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",