1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <ac/string.h>
13 #include <ac/unistd.h>
15 #include "ldap_defaults.h"
18 /* we need LBER internals */
19 #include "../../libraries/liblber/lber-int.h"
21 static char *v2ref( struct berval **ref )
26 if(ref == NULL) return NULL;
28 len = sizeof("Referral:");
29 v2 = ch_strdup("Referral:");
31 for( i=0; ref[i] != NULL; i++ ) {
32 v2 = ch_realloc( v2, len + ref[i]->bv_len + 1 );
34 memcpy(&v2[len], ref[i]->bv_val, ref[i]->bv_len );
35 len += ref[i]->bv_len;
42 static ber_tag_t req2res( ber_tag_t tag )
47 case LDAP_REQ_COMPARE:
48 case LDAP_REQ_EXTENDED:
55 tag = LDAP_RES_DELETE;
58 case LDAP_REQ_ABANDON:
64 tag = LDAP_RES_SEARCH_RESULT;
74 static void trim_refs_urls(
75 struct berval **refs )
79 if( refs == NULL ) return;
81 for( i=0; refs[i] != NULL; i++ ) {
82 if( refs[i]->bv_len > sizeof("ldap://") &&
83 strncasecmp( refs[i]->bv_val, "ldap://",
84 sizeof("ldap://")-1 ) == 0 )
87 for( j=sizeof("ldap://"); j<refs[i]->bv_len ; j++ ) {
88 if( refs[i]->bv_val[j] = '/' ) {
89 refs[i]->bv_val[j] = '\0';
98 struct berval **get_entry_referrals(
105 struct berval **refs;
108 attr = attr_find( e->e_attrs, "ref" );
110 if( attr == NULL ) return NULL;
112 for( i=0; attr->a_vals[i] != NULL; i++ ) {
113 /* count references */
116 if( i < 1 ) return NULL;
118 refs = ch_malloc( i + 1 );
120 for( i=0, j=0; attr->a_vals[i] != NULL; i++ ) {
122 struct berval *ref = ber_bvdup( attr->a_vals[i] );
125 for( k=0; k<ref->bv_len; k++ ) {
126 if( isspace(ref->bv_val[k]) ) {
127 ref->bv_val[k] = '\0';
133 if( ref->bv_len > 0 ) {
144 ber_bvecfree( refs );
148 /* we should check that a referral value exists... */
153 static long send_ldap_ber(
157 ber_len_t bytes = ber_pvt_ber_bytes( ber );
159 /* write only one pdu at a time - wait til it's our turn */
160 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
162 /* lock the connection */
163 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
169 if ( connection_state_closing( conn ) ) {
170 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
171 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
175 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
182 * we got an error. if it's ewouldblock, we need to
183 * wait on the socket being writable. otherwise, figure
184 * it's a hard error and return.
187 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
188 err, err > -1 && err < sys_nerr ? sys_errlist[err]
191 if ( err != EWOULDBLOCK && err != EAGAIN ) {
192 connection_closing( conn );
194 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
195 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
199 /* wait for socket to be write-ready */
200 conn->c_writewaiter = 1;
201 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
203 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
204 conn->c_writewaiter = 0;
207 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
208 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
224 struct berval *resdata,
232 assert( ctrls == NULL ); /* ctrls not implemented */
234 ber = ber_alloc_t( LBER_USE_DER );
236 Debug( LDAP_DEBUG_TRACE, "send_ldap_response: msgid=%ld tag=%ld err=%ld\n",
237 (long) msgid, (long) tag, (long) err );
240 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
244 #ifdef LDAP_CONNECTIONLESS
246 rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
247 err, matched ? matched : "", text ? text : "" );
251 rc = ber_printf( ber, "{it{ess",
253 matched == NULL ? "" : matched,
254 text == NULL ? "" : text );
256 if( rc != -1 && ref != NULL ) {
257 rc = ber_printf( ber, "{V}", ref );
260 if( rc != -1 && resoid != NULL ) {
261 rc = ber_printf( ber, "s", resoid );
264 if( rc != -1 && resdata != NULL ) {
265 rc = ber_printf( ber, "O", resdata );
270 rc = ber_printf( ber, "}}" );
275 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
280 bytes = send_ldap_ber( conn, ber );
283 Debug( LDAP_DEBUG_ANY,
284 "send_ldap_response: ber write failed\n",
289 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
290 num_bytes_sent += bytes;
292 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
298 send_ldap_disconnect(
309 #define LDAP_UNSOLICITED_ERROR(e) \
310 ( (e) == LDAP_PROTOCOL_ERROR \
311 || (e) == LDAP_STRONG_AUTH_REQUIRED \
312 || (e) == LDAP_UNAVAILABLE )
314 assert( LDAP_UNSOLICITED_ERROR( err ) );
316 Debug( LDAP_DEBUG_TRACE,
317 "send_ldap_disconnect %d:%s\n",
318 err, text ? text : "", NULL );
320 if ( op->o_protocol < LDAP_VERSION3 ) {
322 tag = req2res( op->o_tag );
323 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
326 reqoid = LDAP_NOTICE_DISCONNECT;
327 tag = LDAP_RES_EXTENDED;
331 #ifdef LDAP_CONNECTIONLESS
333 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
334 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
335 inet_ntoa(((struct sockaddr_in *)
336 &op->o_clientaddr)->sin_addr ),
337 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
341 send_ldap_response( conn, op, tag, msgid,
342 err, NULL, text, NULL,
343 reqoid, NULL, NULL );
345 Statslog( LDAP_DEBUG_STATS,
346 "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
347 (long) op->o_connid, (long) op->o_opid,
348 (long) tag, (long) err, text ? text : "" );
366 assert( !LDAP_API_ERROR( err ) );
368 Debug( LDAP_DEBUG_TRACE, "send_ldap_result: conn=%ld op=%ld p=%d\n",
369 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
370 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: %d:%s:%s\n",
371 err, matched ? matched : "", text ? text : "" );
373 assert( err != LDAP_PARTIAL_RESULTS );
375 if( op->o_tag != LDAP_REQ_SEARCH ) {
376 trim_refs_urls( ref );
379 if ( err == LDAP_REFERRAL ) {
381 err = LDAP_NO_SUCH_OBJECT;
382 } else if ( op->o_protocol < LDAP_VERSION3 ) {
383 err = LDAP_PARTIAL_RESULTS;
384 tmp = text = v2ref( ref );
389 tag = req2res( op->o_tag );
390 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
392 #ifdef LDAP_CONNECTIONLESS
394 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
395 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
396 inet_ntoa(((struct sockaddr_in *)
397 &op->o_clientaddr)->sin_addr ),
398 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
403 send_ldap_response( conn, op, tag, msgid,
404 err, matched, text, ref,
407 Statslog( LDAP_DEBUG_STATS,
408 "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
409 (long) op->o_connid, (long) op->o_opid,
410 (long) tag, (long) err, text ? text : "" );
425 struct berval **refs,
433 assert( !LDAP_API_ERROR( err ) );
435 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
436 err, matched ? matched : "", text ? text : "" );
438 assert( err != LDAP_PARTIAL_RESULTS );
440 trim_refs_urls( refs );
442 if( op->o_protocol < LDAP_VERSION3 ) {
443 /* send references in search results */
444 if( err == LDAP_REFERRAL ) {
445 err = LDAP_PARTIAL_RESULTS;
448 tmp = text = v2ref( refs );
452 /* don't send references in search results */
453 assert( refs == NULL );
456 if( err == LDAP_REFERRAL ) {
461 tag = req2res( op->o_tag );
462 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
464 #ifdef LDAP_CONNECTIONLESS
466 ber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
467 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
468 inet_ntoa(((struct sockaddr_in *)
469 &op->o_clientaddr)->sin_addr ),
470 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
475 send_ldap_response( conn, op, tag, msgid,
476 err, matched, text, refs,
479 Statslog( LDAP_DEBUG_STATS,
480 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
481 (long) op->o_connid, (long) op->o_opid,
482 (long) tag, (long) err, text ? text : "" );
506 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: \"%s\"\n", e->e_dn, 0, 0 );
508 if ( ! access_allowed( be, conn, op, e,
509 "entry", NULL, ACL_READ ) )
511 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
518 ber = ber_alloc_t( LBER_USE_DER );
521 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
522 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
523 NULL, "allocating BER error", NULL, NULL );
527 rc = ber_printf( ber, "{it{s{", op->o_msgid,
528 LDAP_RES_SEARCH_ENTRY, e->e_dn );
531 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
533 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
534 NULL, "encoding dn error", NULL, NULL );
538 /* check for special all user attributes ("*") type */
539 userattrs = ( attrs == NULL ) ? 1
540 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
542 /* check for special all operational attributes ("+") type */
543 opattrs = ( attrs == NULL ) ? 0
544 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
546 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
547 regmatch_t matches[MAXREMATCHES];
549 if ( attrs == NULL ) {
550 /* all addrs request, skip operational attributes */
551 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
556 /* specific addrs requested */
557 if ( oc_check_operational_attr( a->a_type ) ) {
558 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
563 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
570 acl = acl_get_applicable( be, op, e, a->a_type,
571 MAXREMATCHES, matches );
573 if ( ! acl_access_allowed( acl, be, conn, e,
574 NULL, op, ACL_READ, edn, matches ) )
579 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
580 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
582 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
583 NULL, "encoding type error", NULL, NULL );
588 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
589 if ( a->a_syntax & SYNTAX_DN &&
590 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
591 ACL_READ, edn, matches) )
596 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
597 Debug( LDAP_DEBUG_ANY,
598 "ber_printf failed\n", 0, 0, 0 );
600 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
601 NULL, "encoding value error", NULL, NULL );
607 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
608 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
610 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
611 NULL, "encode end error", NULL, NULL );
616 #ifdef SLAPD_SCHEMA_DN
617 /* eventually will loop through generated operational attributes */
618 /* only have subschemaSubentry implemented */
619 a = backend_subschemasubentry( be );
622 regmatch_t matches[MAXREMATCHES];
624 if ( attrs == NULL ) {
625 /* all addrs request, skip operational attributes */
626 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
631 /* specific addrs requested */
632 if ( oc_check_operational_attr( a->a_type ) ) {
633 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
638 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
645 acl = acl_get_applicable( be, op, e, a->a_type,
646 MAXREMATCHES, matches );
648 if ( ! acl_access_allowed( acl, be, conn, e,
649 NULL, op, ACL_READ, edn, matches ) )
654 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
655 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
657 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
658 NULL, "encoding type error", NULL, NULL );
663 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
664 if ( a->a_syntax & SYNTAX_DN &&
665 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
666 ACL_READ, edn, matches) )
671 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
672 Debug( LDAP_DEBUG_ANY,
673 "ber_printf failed\n", 0, 0, 0 );
675 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
676 NULL, "encoding value error", NULL, NULL );
682 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
683 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
685 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
686 NULL, "encode end error", NULL, NULL );
692 rc = ber_printf( ber, /*{{{*/ "}}}" );
695 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
697 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
698 NULL, "encode entry end error", NULL, NULL );
702 bytes = send_ldap_ber( conn, ber );
705 Debug( LDAP_DEBUG_ANY,
706 "send_ldap_response: ber write failed\n",
711 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
712 num_bytes_sent += bytes;
715 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
717 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
718 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
720 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
729 send_search_reference(
734 struct berval **refs,
737 struct berval ***v2refs
744 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
746 if ( ! access_allowed( be, conn, op, e,
747 "entry", NULL, ACL_READ ) )
749 Debug( LDAP_DEBUG_ACL,
750 "send_search_reference: access to entry not allowed\n",
755 if ( ! access_allowed( be, conn, op, e,
756 "ref", NULL, ACL_READ ) )
758 Debug( LDAP_DEBUG_ACL,
759 "send_search_reference: access to reference not allowed\n",
765 Debug( LDAP_DEBUG_ANY,
766 "send_search_reference: null ref in (%s)\n",
771 if( op->o_protocol < LDAP_VERSION3 ) {
772 /* save the references for the result */
773 if( *refs == NULL ) {
774 value_add( v2refs, refs );
779 ber = ber_alloc_t( LBER_USE_DER );
782 Debug( LDAP_DEBUG_ANY,
783 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
784 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
785 NULL, "alloc BER error", NULL, NULL );
789 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
790 LDAP_RES_SEARCH_REFERENCE, refs );
793 Debug( LDAP_DEBUG_ANY,
794 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
796 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
797 NULL, "encode dn error", NULL, NULL );
801 bytes = send_ldap_ber( conn, ber );
803 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
804 num_bytes_sent += bytes;
807 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
809 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
810 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
812 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
829 *code = LDAP_SUCCESS;
833 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
834 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
841 while ( (s = strchr( s, '\n' )) != NULL ) {
846 if ( (c = strchr( s, ':' )) != NULL ) {
850 if ( strncasecmp( s, "code", 4 ) == 0 ) {
854 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
858 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
863 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",