1 /* result.c - routines to send ldap results, errors, and referrals */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
18 #include <ac/unistd.h>
22 /* we need LBER internals */
23 #include "../../libraries/liblber/lber-int.h"
25 static char *v2ref( struct berval **ref, const char *text )
27 size_t len = 0, i = 0;
33 return ch_strdup(text);
40 if (text[len-1] != '\n')
43 v2 = ch_malloc( len+i+sizeof("Referral:") );
49 strcpy( v2+len, "Referral:" );
50 len += sizeof("Referral:");
52 for( i=0; ref[i] != NULL; i++ ) {
53 v2 = ch_realloc( v2, len + ref[i]->bv_len + 1 );
55 memcpy(&v2[len], ref[i]->bv_val, ref[i]->bv_len );
56 len += ref[i]->bv_len;
57 if (ref[i]->bv_val[ref[i]->bv_len-1] != '/')
65 static ber_tag_t req2res( ber_tag_t tag )
70 case LDAP_REQ_COMPARE:
71 case LDAP_REQ_EXTENDED:
78 tag = LDAP_RES_DELETE;
81 case LDAP_REQ_ABANDON:
87 tag = LDAP_RES_SEARCH_RESULT;
97 static void trim_refs_urls(
98 struct berval **refs )
102 if( refs == NULL ) return;
104 for( i=0; refs[i] != NULL; i++ ) {
105 if( refs[i]->bv_len > sizeof("ldap://")-1 &&
106 strncasecmp( refs[i]->bv_val, "ldap://",
107 sizeof("ldap://")-1 ) == 0 )
110 for( j=sizeof("ldap://")-1; j<refs[i]->bv_len ; j++ ) {
111 if( refs[i]->bv_val[j] == '/' ) {
112 refs[i]->bv_val[j] = '\0';
121 struct berval **get_entry_referrals(
128 struct berval **refs;
131 #ifdef SLAPD_SCHEMA_NOT_COMPAT
132 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
134 static const char *ad_ref = "ref";
137 attr = attr_find( e->e_attrs, ad_ref );
139 if( attr == NULL ) return NULL;
141 for( i=0; attr->a_vals[i] != NULL; i++ ) {
142 /* count references */
145 if( i < 1 ) return NULL;
147 refs = ch_malloc( (i + 1) * sizeof(struct berval *));
149 for( i=0, j=0; attr->a_vals[i] != NULL; i++ ) {
151 struct berval *ref = ber_bvdup( attr->a_vals[i] );
154 for( k=0; k<ref->bv_len; k++ ) {
155 if( isspace(ref->bv_val[k]) ) {
156 ref->bv_val[k] = '\0';
162 if( ref->bv_len > 0 ) {
173 ber_bvecfree( refs );
177 /* we should check that a referral value exists... */
182 static long send_ldap_ber(
188 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
190 /* write only one pdu at a time - wait til it's our turn */
191 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
193 /* lock the connection */
194 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
200 if ( connection_state_closing( conn ) ) {
201 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
202 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
207 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
214 * we got an error. if it's ewouldblock, we need to
215 * wait on the socket being writable. otherwise, figure
216 * it's a hard error and return.
219 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
220 err, sock_errstr(err), 0 );
222 if ( err != EWOULDBLOCK && err != EAGAIN ) {
223 connection_closing( conn );
225 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
226 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
231 /* wait for socket to be write-ready */
232 conn->c_writewaiter = 1;
233 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
235 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
236 conn->c_writewaiter = 0;
239 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
240 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
256 struct berval *resdata,
257 struct berval *sasldata,
265 assert( ctrls == NULL ); /* ctrls not implemented */
267 ber = ber_alloc_t( LBER_USE_DER );
269 Debug( LDAP_DEBUG_TRACE, "send_ldap_response: msgid=%ld tag=%ld err=%ld\n",
270 (long) msgid, (long) tag, (long) err );
273 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
277 #ifdef LDAP_CONNECTIONLESS
279 rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
280 err, matched ? matched : "", text ? text : "" );
284 rc = ber_printf( ber, "{it{ess",
286 matched == NULL ? "" : matched,
287 text == NULL ? "" : text );
289 if( rc != -1 && ref != NULL ) {
290 rc = ber_printf( ber, "t{V}",
291 LDAP_TAG_REFERRAL, ref );
294 if( rc != -1 && sasldata != NULL ) {
295 rc = ber_printf( ber, "tO",
296 LDAP_TAG_SASL_RES_CREDS, sasldata );
299 if( rc != -1 && resoid != NULL ) {
300 rc = ber_printf( ber, "ts",
301 LDAP_TAG_EXOP_RES_OID, resoid );
304 if( rc != -1 && resdata != NULL ) {
305 rc = ber_printf( ber, "tO",
306 LDAP_TAG_EXOP_RES_VALUE, resdata );
310 rc = ber_printf( ber, "}}" );
315 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
321 bytes = send_ldap_ber( conn, ber );
325 Debug( LDAP_DEBUG_ANY,
326 "send_ldap_response: ber write failed\n",
331 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
332 num_bytes_sent += bytes;
334 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
340 send_ldap_disconnect(
351 #define LDAP_UNSOLICITED_ERROR(e) \
352 ( (e) == LDAP_PROTOCOL_ERROR \
353 || (e) == LDAP_STRONG_AUTH_REQUIRED \
354 || (e) == LDAP_UNAVAILABLE )
356 assert( LDAP_UNSOLICITED_ERROR( err ) );
358 Debug( LDAP_DEBUG_TRACE,
359 "send_ldap_disconnect %d:%s\n",
360 err, text ? text : "", NULL );
362 if ( op->o_protocol < LDAP_VERSION3 ) {
364 tag = req2res( op->o_tag );
365 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
368 reqoid = LDAP_NOTICE_DISCONNECT;
369 tag = LDAP_RES_EXTENDED;
373 #ifdef LDAP_CONNECTIONLESS
375 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
376 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
377 inet_ntoa(((struct sockaddr_in *)
378 &op->o_clientaddr)->sin_addr ),
379 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
384 send_ldap_response( conn, op, tag, msgid,
385 err, NULL, text, NULL,
386 reqoid, NULL, NULL, NULL );
388 Statslog( LDAP_DEBUG_STATS,
389 "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
390 (long) op->o_connid, (long) op->o_opid,
391 (long) tag, (long) err, text ? text : "" );
409 assert( !LDAP_API_ERROR( err ) );
411 Debug( LDAP_DEBUG_TRACE, "send_ldap_result: conn=%ld op=%ld p=%d\n",
412 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
413 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: %d:%s:%s\n",
414 err, matched ? matched : "", text ? text : "" );
416 assert( err != LDAP_PARTIAL_RESULTS );
418 if( op->o_tag != LDAP_REQ_SEARCH ) {
419 trim_refs_urls( ref );
422 if ( err == LDAP_REFERRAL ) {
424 err = LDAP_NO_SUCH_OBJECT;
425 } else if ( op->o_protocol < LDAP_VERSION3 ) {
426 err = LDAP_PARTIAL_RESULTS;
430 if ( op->o_protocol < LDAP_VERSION3 ) {
431 tmp = v2ref( ref, text );
436 tag = req2res( op->o_tag );
437 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
439 #ifdef LDAP_CONNECTIONLESS
441 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
442 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
443 inet_ntoa(((struct sockaddr_in *)
444 &op->o_clientaddr)->sin_addr ),
445 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
450 send_ldap_response( conn, op, tag, msgid,
451 err, matched, text, ref,
452 NULL, NULL, NULL, ctrls );
454 Statslog( LDAP_DEBUG_STATS,
455 "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
456 (long) op->o_connid, (long) op->o_opid,
457 (long) tag, (long) err, text ? text : "" );
479 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl %ld\n",
480 (long) err, NULL, NULL );
482 tag = req2res( op->o_tag );
483 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
485 #ifdef LDAP_CONNECTIONLESS
487 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
488 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
489 inet_ntoa(((struct sockaddr_in *)
490 &op->o_clientaddr)->sin_addr ),
491 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
496 send_ldap_response( conn, op, tag, msgid,
497 err, matched, text, ref,
498 NULL, NULL, cred, ctrls );
508 struct berval **refs,
510 struct berval *rspdata,
517 Debug( LDAP_DEBUG_TRACE,
518 "send_ldap_extended %ld:%s (%ld)\n",
520 rspoid ? rspoid : "",
521 rspdata != NULL ? (long) rspdata->bv_len : (long) 0 );
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,
539 rspoid, rspdata, NULL, ctrls );
550 struct berval **refs,
558 assert( !LDAP_API_ERROR( err ) );
560 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
561 err, matched ? matched : "", text ? text : "" );
563 assert( err != LDAP_PARTIAL_RESULTS );
565 trim_refs_urls( refs );
567 if( op->o_protocol < LDAP_VERSION3 ) {
568 /* send references in search results */
569 if( err == LDAP_REFERRAL ) {
570 err = LDAP_PARTIAL_RESULTS;
573 tmp = v2ref( refs, text );
577 /* don't send references in search results */
578 assert( refs == NULL );
581 if( err == LDAP_REFERRAL ) {
586 tag = req2res( op->o_tag );
587 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
589 #ifdef LDAP_CONNECTIONLESS
591 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
592 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
593 inet_ntoa(((struct sockaddr_in *)
594 &op->o_clientaddr)->sin_addr ),
595 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
600 send_ldap_response( conn, op, tag, msgid,
601 err, matched, text, refs,
602 NULL, NULL, NULL, ctrls );
604 Statslog( LDAP_DEBUG_STATS,
605 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
606 (long) op->o_connid, (long) op->o_opid,
607 (long) tag, (long) err, text ? text : "" );
632 #ifdef SLAPD_SCHEMA_NOT_COMPAT
633 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
635 static const char *ad_entry = "entry";
638 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: \"%s\"\n", e->e_dn, 0, 0 );
640 if ( ! access_allowed( be, conn, op, e,
641 ad_entry, NULL, ACL_READ ) )
643 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
650 ber = ber_alloc_t( LBER_USE_DER );
653 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
654 send_ldap_result( conn, op, LDAP_OTHER,
655 NULL, "BER allocation error", NULL, NULL );
659 rc = ber_printf( ber, "{it{s{", op->o_msgid,
660 LDAP_RES_SEARCH_ENTRY, e->e_dn );
663 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
665 send_ldap_result( conn, op, LDAP_OTHER,
666 NULL, "encoding DN error", NULL, NULL );
670 /* check for special all user attributes ("*") type */
671 userattrs = ( attrs == NULL ) ? 1
672 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
674 /* check for special all operational attributes ("+") type */
675 opattrs = ( attrs == NULL ) ? 0
676 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
678 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
679 #ifdef SLAPD_SCHEMA_NOT_COMPAT
680 AttributeDescription *desc = a->a_desc;
681 char *type = desc->ad_cname->bv_val;
683 char *desc = a->a_type;
684 char *type = a->a_type;
687 if ( attrs == NULL ) {
688 /* all addrs request, skip operational attributes */
689 #ifdef SLAPD_SCHEMA_NOT_COMPAT
690 if( is_at_operational( desc->ad_type ) )
692 if( oc_check_op_attr( desc ) )
699 /* specific addrs requested */
700 #ifdef SLAPD_SCHEMA_NOT_COMPAT
701 if ( is_at_operational( desc->ad_type ) )
703 if ( oc_check_op_attr( desc ) )
706 if( !opattrs && !ad_inlist( desc, attrs ) ) {
710 if (!userattrs && !ad_inlist( desc, attrs ) ) {
716 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
717 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
722 if (( rc = ber_printf( ber, "{s[" /*]}*/ , type )) == -1 ) {
723 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
725 send_ldap_result( conn, op, LDAP_OTHER,
726 NULL, "encoding description error", NULL, NULL );
731 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
732 if ( ! access_allowed( be, conn, op, e,
733 desc, a->a_vals[i], ACL_READ ) )
735 Debug( LDAP_DEBUG_ACL,
736 "acl: access to attribute %s, value %d not allowed\n",
741 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
742 Debug( LDAP_DEBUG_ANY,
743 "ber_printf failed\n", 0, 0, 0 );
745 send_ldap_result( conn, op, LDAP_OTHER,
746 NULL, "encoding values error", NULL, NULL );
752 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
753 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
755 send_ldap_result( conn, op, LDAP_OTHER,
756 NULL, "encode end error", NULL, NULL );
761 /* eventually will loop through generated operational attributes */
762 /* only have subschemaSubentry implemented */
763 aa = backend_operational( be, e );
765 for (a = aa ; a == NULL; a = a->a_next ) {
766 #ifdef SLAPD_SCHEMA_NOT_COMPAT
767 AttributeDescription *desc = a->a_desc;
769 char *desc = a->a_type;
772 if ( attrs == NULL ) {
773 /* all addrs request, skip operational attributes */
774 #ifdef SLAPD_SCHEMA_NOT_COMPAT
775 if( is_at_operational( desc->ad_type ) )
777 if( oc_check_op_attr( desc ) )
784 /* specific addrs requested */
785 #ifdef SLAPD_SCHEMA_NOT_COMPAT
786 if( is_at_operational( desc->ad_type ) )
788 if( oc_check_op_attr( desc ) )
791 if( !opattrs && !ad_inlist( desc, attrs ) )
796 if (!userattrs && !ad_inlist( desc, attrs ) )
803 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
804 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
809 if (( rc = ber_printf( ber, "{s[" /*]}*/ , desc )) == -1 ) {
810 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
812 send_ldap_result( conn, op, LDAP_OTHER,
813 NULL, "encoding description error", NULL, NULL );
818 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
819 if ( ! access_allowed( be, conn, op, e,
820 desc, a->a_vals[i], ACL_READ ) )
822 Debug( LDAP_DEBUG_ACL,
823 "acl: access to attribute %s, value %d not allowed\n",
829 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
830 Debug( LDAP_DEBUG_ANY,
831 "ber_printf failed\n", 0, 0, 0 );
833 send_ldap_result( conn, op, LDAP_OTHER,
834 NULL, "encoding values error", NULL, NULL );
840 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
841 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
843 send_ldap_result( conn, op, LDAP_OTHER,
844 NULL, "encode end error", NULL, NULL );
851 rc = ber_printf( ber, /*{{{*/ "}}}" );
854 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
856 send_ldap_result( conn, op, LDAP_OTHER,
857 NULL, "encode entry end error", NULL, NULL );
861 bytes = send_ldap_ber( conn, ber );
865 Debug( LDAP_DEBUG_ANY,
866 "send_ldap_response: ber write failed\n",
871 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
872 num_bytes_sent += bytes;
875 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
877 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
878 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
880 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
889 send_search_reference(
894 struct berval **refs,
897 struct berval ***v2refs
904 #ifdef SLAPD_SCHEMA_NOT_COMPAT
905 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
906 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
908 static const char *ad_ref = "ref";
909 static const char *ad_entry = "entry";
912 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
914 if ( ! access_allowed( be, conn, op, e,
915 ad_entry, NULL, ACL_READ ) )
917 Debug( LDAP_DEBUG_ACL,
918 "send_search_reference: access to entry not allowed\n",
923 if ( ! access_allowed( be, conn, op, e,
924 ad_ref, NULL, ACL_READ ) )
926 Debug( LDAP_DEBUG_ACL,
927 "send_search_reference: access to reference not allowed\n",
933 Debug( LDAP_DEBUG_ANY,
934 "send_search_reference: null ref in (%s)\n",
939 if( op->o_protocol < LDAP_VERSION3 ) {
940 /* save the references for the result */
941 if( *refs != NULL ) {
942 value_add( v2refs, refs );
947 ber = ber_alloc_t( LBER_USE_DER );
950 Debug( LDAP_DEBUG_ANY,
951 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
952 send_ldap_result( conn, op, LDAP_OTHER,
953 NULL, "alloc BER error", NULL, NULL );
957 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
958 LDAP_RES_SEARCH_REFERENCE, refs );
961 Debug( LDAP_DEBUG_ANY,
962 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
964 send_ldap_result( conn, op, LDAP_OTHER,
965 NULL, "encode DN error", NULL, NULL );
969 bytes = send_ldap_ber( conn, ber );
972 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
973 num_bytes_sent += bytes;
976 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
978 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
979 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
981 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
998 *code = LDAP_SUCCESS;
1002 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1003 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1010 while ( (s = strchr( s, '\n' )) != NULL ) {
1015 if ( (c = strchr( s, ':' )) != NULL ) {
1019 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1023 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1027 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1032 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",