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;
390 tag = req2res( op->o_tag );
391 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
393 #ifdef LDAP_CONNECTIONLESS
395 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
396 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
397 inet_ntoa(((struct sockaddr_in *)
398 &op->o_clientaddr)->sin_addr ),
399 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
404 send_ldap_response( conn, op, tag, msgid,
405 err, matched, text, ref,
408 Statslog( LDAP_DEBUG_STATS,
409 "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
410 (long) op->o_connid, (long) op->o_opid,
411 (long) tag, (long) err, text ? text : "" );
426 struct berval **refs,
434 assert( !LDAP_API_ERROR( err ) );
436 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
437 err, matched ? matched : "", text ? text : "" );
439 assert( err != LDAP_PARTIAL_RESULTS );
441 trim_refs_urls( refs );
443 if( op->o_protocol < LDAP_VERSION3 ) {
444 /* send references in search results */
445 if( err == LDAP_REFERRAL ) {
446 err = LDAP_PARTIAL_RESULTS;
454 /* don't send references in search results */
455 assert( refs == NULL );
458 if( err == LDAP_REFERRAL ) {
463 tag = req2res( op->o_tag );
464 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
466 #ifdef LDAP_CONNECTIONLESS
468 ber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
469 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
470 inet_ntoa(((struct sockaddr_in *)
471 &op->o_clientaddr)->sin_addr ),
472 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
477 send_ldap_response( conn, op, tag, msgid,
478 err, matched, text, refs,
481 Statslog( LDAP_DEBUG_STATS,
482 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
483 (long) op->o_connid, (long) op->o_opid,
484 (long) tag, (long) err, text ? text : "" );
508 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: \"%s\"\n", e->e_dn, 0, 0 );
510 if ( ! access_allowed( be, conn, op, e,
511 "entry", NULL, ACL_READ ) )
513 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
520 ber = ber_alloc_t( LBER_USE_DER );
523 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
524 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
525 NULL, "allocating BER error", NULL, NULL );
529 rc = ber_printf( ber, "{it{s{", op->o_msgid,
530 LDAP_RES_SEARCH_ENTRY, e->e_dn );
533 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
535 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
536 NULL, "encoding dn error", NULL, NULL );
540 /* check for special all user attributes ("*") type */
541 userattrs = ( attrs == NULL ) ? 1
542 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
544 /* check for special all operational attributes ("+") type */
545 opattrs = ( attrs == NULL ) ? 0
546 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
548 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
549 regmatch_t matches[MAXREMATCHES];
551 if ( attrs == NULL ) {
552 /* all addrs request, skip operational attributes */
553 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
558 /* specific addrs requested */
559 if ( oc_check_operational_attr( a->a_type ) ) {
560 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
565 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
572 acl = acl_get_applicable( be, op, e, a->a_type,
573 MAXREMATCHES, matches );
575 if ( ! acl_access_allowed( acl, be, conn, e,
576 NULL, op, ACL_READ, edn, matches ) )
581 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
582 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
584 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
585 NULL, "encoding type error", NULL, NULL );
590 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
591 if ( a->a_syntax & SYNTAX_DN &&
592 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
593 ACL_READ, edn, matches) )
598 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
599 Debug( LDAP_DEBUG_ANY,
600 "ber_printf failed\n", 0, 0, 0 );
602 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
603 NULL, "encoding value error", NULL, NULL );
609 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
610 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
612 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
613 NULL, "encode end error", NULL, NULL );
618 #ifdef SLAPD_SCHEMA_DN
619 /* eventually will loop through generated operational attributes */
620 /* only have subschemaSubentry implemented */
621 a = backend_subschemasubentry( be );
624 regmatch_t matches[MAXREMATCHES];
626 if ( attrs == NULL ) {
627 /* all addrs request, skip operational attributes */
628 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
633 /* specific addrs requested */
634 if ( oc_check_operational_attr( a->a_type ) ) {
635 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
640 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
647 acl = acl_get_applicable( be, op, e, a->a_type,
648 MAXREMATCHES, matches );
650 if ( ! acl_access_allowed( acl, be, conn, e,
651 NULL, op, ACL_READ, edn, matches ) )
656 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
657 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
659 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
660 NULL, "encoding type error", NULL, NULL );
665 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
666 if ( a->a_syntax & SYNTAX_DN &&
667 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
668 ACL_READ, edn, matches) )
673 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
674 Debug( LDAP_DEBUG_ANY,
675 "ber_printf failed\n", 0, 0, 0 );
677 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
678 NULL, "encoding value error", NULL, NULL );
684 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
685 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
687 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
688 NULL, "encode end error", NULL, NULL );
694 rc = ber_printf( ber, /*{{{*/ "}}}" );
697 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
699 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
700 NULL, "encode entry end error", NULL, NULL );
704 bytes = send_ldap_ber( conn, ber );
707 Debug( LDAP_DEBUG_ANY,
708 "send_ldap_response: ber write failed\n",
713 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
714 num_bytes_sent += bytes;
717 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
719 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
720 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
722 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
731 send_search_reference(
736 struct berval **refs,
739 struct berval ***v2refs
746 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
748 if ( ! access_allowed( be, conn, op, e,
749 "entry", NULL, ACL_READ ) )
751 Debug( LDAP_DEBUG_ACL,
752 "send_search_reference: access to entry not allowed\n",
757 if ( ! access_allowed( be, conn, op, e,
758 "ref", NULL, ACL_READ ) )
760 Debug( LDAP_DEBUG_ACL,
761 "send_search_reference: access to reference not allowed\n",
767 Debug( LDAP_DEBUG_ANY,
768 "send_search_reference: null ref in (%s)\n",
773 if( op->o_protocol < LDAP_VERSION3 ) {
774 /* save the references for the result */
775 if( *refs == NULL ) {
776 value_add( v2refs, refs );
781 ber = ber_alloc_t( LBER_USE_DER );
784 Debug( LDAP_DEBUG_ANY,
785 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
786 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
787 NULL, "alloc BER error", NULL, NULL );
791 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
792 LDAP_RES_SEARCH_REFERENCE, refs );
795 Debug( LDAP_DEBUG_ANY,
796 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
798 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
799 NULL, "encode dn error", NULL, NULL );
803 bytes = send_ldap_ber( conn, ber );
805 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
806 num_bytes_sent += bytes;
809 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
811 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
812 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
814 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
831 *code = LDAP_SUCCESS;
835 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
836 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
843 while ( (s = strchr( s, '\n' )) != NULL ) {
848 if ( (c = strchr( s, ':' )) != NULL ) {
852 if ( strncasecmp( s, "code", 4 ) == 0 ) {
856 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
860 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
865 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",