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(
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 );
244 struct berval *resdata,
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 && resdata != NULL ) {
285 rc = ber_printf( ber, "O", resdata );
290 rc = ber_printf( ber, "}}" );
295 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
301 bytes = send_ldap_ber( conn, ber );
305 Debug( LDAP_DEBUG_ANY,
306 "send_ldap_response: ber write failed\n",
311 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
312 num_bytes_sent += bytes;
314 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
320 send_ldap_disconnect(
331 #define LDAP_UNSOLICITED_ERROR(e) \
332 ( (e) == LDAP_PROTOCOL_ERROR \
333 || (e) == LDAP_STRONG_AUTH_REQUIRED \
334 || (e) == LDAP_UNAVAILABLE )
336 assert( LDAP_UNSOLICITED_ERROR( err ) );
338 Debug( LDAP_DEBUG_TRACE,
339 "send_ldap_disconnect %d:%s\n",
340 err, text ? text : "", NULL );
342 if ( op->o_protocol < LDAP_VERSION3 ) {
344 tag = req2res( op->o_tag );
345 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
348 reqoid = LDAP_NOTICE_DISCONNECT;
349 tag = LDAP_RES_EXTENDED;
353 #ifdef LDAP_CONNECTIONLESS
355 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
356 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
357 inet_ntoa(((struct sockaddr_in *)
358 &op->o_clientaddr)->sin_addr ),
359 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
363 send_ldap_response( conn, op, tag, msgid,
364 err, NULL, text, NULL,
365 reqoid, NULL, NULL );
367 Statslog( LDAP_DEBUG_STATS,
368 "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
369 (long) op->o_connid, (long) op->o_opid,
370 (long) tag, (long) err, text ? text : "" );
388 assert( !LDAP_API_ERROR( err ) );
390 Debug( LDAP_DEBUG_TRACE, "send_ldap_result: conn=%ld op=%ld p=%d\n",
391 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
392 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: %d:%s:%s\n",
393 err, matched ? matched : "", text ? text : "" );
395 assert( err != LDAP_PARTIAL_RESULTS );
397 if( op->o_tag != LDAP_REQ_SEARCH ) {
398 trim_refs_urls( ref );
401 if ( err == LDAP_REFERRAL ) {
403 err = LDAP_NO_SUCH_OBJECT;
404 } else if ( op->o_protocol < LDAP_VERSION3 ) {
405 err = LDAP_PARTIAL_RESULTS;
409 if ( op->o_protocol < LDAP_VERSION3 ) {
410 tmp = v2ref( ref, text );
415 tag = req2res( op->o_tag );
416 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
418 #ifdef LDAP_CONNECTIONLESS
420 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
421 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
422 inet_ntoa(((struct sockaddr_in *)
423 &op->o_clientaddr)->sin_addr ),
424 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
429 send_ldap_response( conn, op, tag, msgid,
430 err, matched, text, ref,
433 Statslog( LDAP_DEBUG_STATS,
434 "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
435 (long) op->o_connid, (long) op->o_opid,
436 (long) tag, (long) err, text ? text : "" );
451 struct berval *rspdata
457 Debug( LDAP_DEBUG_TRACE,
458 "send_ldap_extended %d:%s\n",
459 err, rspoid ? rspoid : "", NULL );
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,
474 send_ldap_response( conn, op, tag, msgid,
475 err, matched, text, NULL,
476 rspoid, rspdata, NULL );
487 struct berval **refs,
495 assert( !LDAP_API_ERROR( err ) );
497 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
498 err, matched ? matched : "", text ? text : "" );
500 assert( err != LDAP_PARTIAL_RESULTS );
502 trim_refs_urls( refs );
504 if( op->o_protocol < LDAP_VERSION3 ) {
505 /* send references in search results */
506 if( err == LDAP_REFERRAL ) {
507 err = LDAP_PARTIAL_RESULTS;
510 tmp = v2ref( refs, text );
514 /* don't send references in search results */
515 assert( refs == NULL );
518 if( err == LDAP_REFERRAL ) {
523 tag = req2res( op->o_tag );
524 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
526 #ifdef LDAP_CONNECTIONLESS
528 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
529 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
530 inet_ntoa(((struct sockaddr_in *)
531 &op->o_clientaddr)->sin_addr ),
532 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
537 send_ldap_response( conn, op, tag, msgid,
538 err, matched, text, refs,
541 Statslog( LDAP_DEBUG_STATS,
542 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
543 (long) op->o_connid, (long) op->o_opid,
544 (long) tag, (long) err, text ? text : "" );
569 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: \"%s\"\n", e->e_dn, 0, 0 );
571 if ( ! access_allowed( be, conn, op, e,
572 "entry", NULL, ACL_READ ) )
574 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
581 ber = ber_alloc_t( LBER_USE_DER );
584 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
585 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
586 NULL, "allocating BER error", NULL, NULL );
590 rc = ber_printf( ber, "{it{s{", op->o_msgid,
591 LDAP_RES_SEARCH_ENTRY, e->e_dn );
594 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
596 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
597 NULL, "encoding dn error", NULL, NULL );
601 /* check for special all user attributes ("*") type */
602 userattrs = ( attrs == NULL ) ? 1
603 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
605 /* check for special all operational attributes ("+") type */
606 opattrs = ( attrs == NULL ) ? 0
607 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
609 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
610 if ( attrs == NULL ) {
611 /* all addrs request, skip operational attributes */
612 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
617 /* specific addrs requested */
618 if ( oc_check_operational_attr( a->a_type ) ) {
619 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
624 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
631 if ( ! access_allowed( be, conn, op, e,
632 a->a_type, NULL, ACL_READ ) )
634 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
639 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
640 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
642 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
643 NULL, "encoding type error", NULL, NULL );
648 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
649 if ( ! access_allowed( be, conn, op, e,
650 a->a_type, a->a_vals[i], ACL_READ ) )
652 Debug( LDAP_DEBUG_ACL,
653 "acl: access to attribute %s, value %d not allowed\n",
658 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
659 Debug( LDAP_DEBUG_ANY,
660 "ber_printf failed\n", 0, 0, 0 );
662 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
663 NULL, "encoding value error", NULL, NULL );
669 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
670 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
672 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
673 NULL, "encode end error", NULL, NULL );
678 #ifdef SLAPD_SCHEMA_DN
679 /* eventually will loop through generated operational attributes */
680 /* only have subschemaSubentry implemented */
681 a = backend_subschemasubentry( be );
684 if ( attrs == NULL ) {
685 /* all addrs request, skip operational attributes */
686 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
691 /* specific addrs requested */
692 if ( oc_check_operational_attr( a->a_type ) ) {
693 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
698 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
705 if ( ! access_allowed( be, conn, op, e,
706 a->a_type, NULL, ACL_READ ) )
708 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
713 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
714 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
716 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
717 NULL, "encoding type error", NULL, NULL );
722 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
723 if ( ! access_allowed( be, conn, op, e,
724 a->a_type, a->a_vals[i], ACL_READ ) )
726 Debug( LDAP_DEBUG_ACL,
727 "acl: access to attribute %s, value %d not allowed\n",
733 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
734 Debug( LDAP_DEBUG_ANY,
735 "ber_printf failed\n", 0, 0, 0 );
737 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
738 NULL, "encoding value error", NULL, NULL );
744 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
745 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
747 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
748 NULL, "encode end error", NULL, NULL );
754 rc = ber_printf( ber, /*{{{*/ "}}}" );
757 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
759 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
760 NULL, "encode entry end error", NULL, NULL );
764 bytes = send_ldap_ber( conn, ber );
768 Debug( LDAP_DEBUG_ANY,
769 "send_ldap_response: ber write failed\n",
774 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
775 num_bytes_sent += bytes;
778 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
780 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
781 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
783 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
792 send_search_reference(
797 struct berval **refs,
800 struct berval ***v2refs
807 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
809 if ( ! access_allowed( be, conn, op, e,
810 "entry", NULL, ACL_READ ) )
812 Debug( LDAP_DEBUG_ACL,
813 "send_search_reference: access to entry not allowed\n",
818 if ( ! access_allowed( be, conn, op, e,
819 "ref", NULL, ACL_READ ) )
821 Debug( LDAP_DEBUG_ACL,
822 "send_search_reference: access to reference not allowed\n",
828 Debug( LDAP_DEBUG_ANY,
829 "send_search_reference: null ref in (%s)\n",
834 if( op->o_protocol < LDAP_VERSION3 ) {
835 /* save the references for the result */
836 if( *refs == NULL ) {
837 value_add( v2refs, refs );
842 ber = ber_alloc_t( LBER_USE_DER );
845 Debug( LDAP_DEBUG_ANY,
846 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
847 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
848 NULL, "alloc BER error", NULL, NULL );
852 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
853 LDAP_RES_SEARCH_REFERENCE, refs );
856 Debug( LDAP_DEBUG_ANY,
857 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
859 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
860 NULL, "encode dn error", NULL, NULL );
864 bytes = send_ldap_ber( conn, ber );
867 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
868 num_bytes_sent += bytes;
871 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
873 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
874 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
876 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
893 *code = LDAP_SUCCESS;
897 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
898 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
905 while ( (s = strchr( s, '\n' )) != NULL ) {
910 if ( (c = strchr( s, ':' )) != NULL ) {
914 if ( strncasecmp( s, "code", 4 ) == 0 ) {
918 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
922 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
927 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",