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 static AttributeDescription *ref = NULL;
134 static const char *ref = "ref";
137 attr = attr_find( e->e_attrs, 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, "{V}", ref );
293 if( rc != -1 && sasldata != NULL ) {
294 rc = ber_printf( ber, "tO",
295 LDAP_TAG_SASL_RES_CREDS, sasldata );
298 if( rc != -1 && resoid != NULL ) {
299 rc = ber_printf( ber, "ts",
300 LDAP_TAG_EXOP_RES_OID, resoid );
303 if( rc != -1 && resdata != NULL ) {
304 rc = ber_printf( ber, "tO",
305 LDAP_TAG_EXOP_RES_VALUE, resdata );
309 rc = ber_printf( ber, "}}" );
314 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
320 bytes = send_ldap_ber( conn, ber );
324 Debug( LDAP_DEBUG_ANY,
325 "send_ldap_response: ber write failed\n",
330 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
331 num_bytes_sent += bytes;
333 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
339 send_ldap_disconnect(
350 #define LDAP_UNSOLICITED_ERROR(e) \
351 ( (e) == LDAP_PROTOCOL_ERROR \
352 || (e) == LDAP_STRONG_AUTH_REQUIRED \
353 || (e) == LDAP_UNAVAILABLE )
355 assert( LDAP_UNSOLICITED_ERROR( err ) );
357 Debug( LDAP_DEBUG_TRACE,
358 "send_ldap_disconnect %d:%s\n",
359 err, text ? text : "", NULL );
361 if ( op->o_protocol < LDAP_VERSION3 ) {
363 tag = req2res( op->o_tag );
364 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
367 reqoid = LDAP_NOTICE_DISCONNECT;
368 tag = LDAP_RES_EXTENDED;
372 #ifdef LDAP_CONNECTIONLESS
374 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
375 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
376 inet_ntoa(((struct sockaddr_in *)
377 &op->o_clientaddr)->sin_addr ),
378 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
383 send_ldap_response( conn, op, tag, msgid,
384 err, NULL, text, NULL,
385 reqoid, NULL, NULL, NULL );
387 Statslog( LDAP_DEBUG_STATS,
388 "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
389 (long) op->o_connid, (long) op->o_opid,
390 (long) tag, (long) err, text ? text : "" );
408 assert( !LDAP_API_ERROR( err ) );
410 Debug( LDAP_DEBUG_TRACE, "send_ldap_result: conn=%ld op=%ld p=%d\n",
411 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
412 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: %d:%s:%s\n",
413 err, matched ? matched : "", text ? text : "" );
415 assert( err != LDAP_PARTIAL_RESULTS );
417 if( op->o_tag != LDAP_REQ_SEARCH ) {
418 trim_refs_urls( ref );
421 if ( err == LDAP_REFERRAL ) {
423 err = LDAP_NO_SUCH_OBJECT;
424 } else if ( op->o_protocol < LDAP_VERSION3 ) {
425 err = LDAP_PARTIAL_RESULTS;
429 if ( op->o_protocol < LDAP_VERSION3 ) {
430 tmp = v2ref( ref, text );
435 tag = req2res( op->o_tag );
436 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
438 #ifdef LDAP_CONNECTIONLESS
440 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
441 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
442 inet_ntoa(((struct sockaddr_in *)
443 &op->o_clientaddr)->sin_addr ),
444 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
449 send_ldap_response( conn, op, tag, msgid,
450 err, matched, text, ref,
451 NULL, NULL, NULL, ctrls );
453 Statslog( LDAP_DEBUG_STATS,
454 "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
455 (long) op->o_connid, (long) op->o_opid,
456 (long) tag, (long) err, text ? text : "" );
478 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl %ld\n",
479 (long) err, NULL, NULL );
481 tag = req2res( op->o_tag );
482 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
484 #ifdef LDAP_CONNECTIONLESS
486 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
487 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
488 inet_ntoa(((struct sockaddr_in *)
489 &op->o_clientaddr)->sin_addr ),
490 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
495 send_ldap_response( conn, op, tag, msgid,
496 err, matched, text, ref,
497 NULL, NULL, cred, ctrls );
507 struct berval **refs,
509 struct berval *rspdata,
516 Debug( LDAP_DEBUG_TRACE,
517 "send_ldap_extended %ld:%s (%ld)\n",
519 rspoid ? rspoid : "",
520 rspdata != NULL ? (long) rspdata->bv_len : (long) 0 );
522 tag = req2res( op->o_tag );
523 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
525 #ifdef LDAP_CONNECTIONLESS
527 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
528 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
529 inet_ntoa(((struct sockaddr_in *)
530 &op->o_clientaddr)->sin_addr ),
531 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
536 send_ldap_response( conn, op, tag, msgid,
537 err, matched, text, refs,
538 rspoid, rspdata, NULL, ctrls );
549 struct berval **refs,
557 assert( !LDAP_API_ERROR( err ) );
559 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
560 err, matched ? matched : "", text ? text : "" );
562 assert( err != LDAP_PARTIAL_RESULTS );
564 trim_refs_urls( refs );
566 if( op->o_protocol < LDAP_VERSION3 ) {
567 /* send references in search results */
568 if( err == LDAP_REFERRAL ) {
569 err = LDAP_PARTIAL_RESULTS;
572 tmp = v2ref( refs, text );
576 /* don't send references in search results */
577 assert( refs == NULL );
580 if( err == LDAP_REFERRAL ) {
585 tag = req2res( op->o_tag );
586 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
588 #ifdef LDAP_CONNECTIONLESS
590 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
591 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
592 inet_ntoa(((struct sockaddr_in *)
593 &op->o_clientaddr)->sin_addr ),
594 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
599 send_ldap_response( conn, op, tag, msgid,
600 err, matched, text, refs,
601 NULL, NULL, NULL, ctrls );
603 Statslog( LDAP_DEBUG_STATS,
604 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
605 (long) op->o_connid, (long) op->o_opid,
606 (long) tag, (long) err, text ? text : "" );
631 #ifdef SLAPD_SCHEMA_NOT_COMPAT
632 AttributeDescription *entry = slap_schema.si_ad_entry;
634 static const char *entry = "entry";
637 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: \"%s\"\n", e->e_dn, 0, 0 );
639 if ( ! access_allowed( be, conn, op, e,
640 entry, NULL, ACL_READ ) )
642 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
649 ber = ber_alloc_t( LBER_USE_DER );
652 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
653 send_ldap_result( conn, op, LDAP_OTHER,
654 NULL, "BER allocation error", NULL, NULL );
658 rc = ber_printf( ber, "{it{s{", op->o_msgid,
659 LDAP_RES_SEARCH_ENTRY, e->e_dn );
662 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
664 send_ldap_result( conn, op, LDAP_OTHER,
665 NULL, "encoding DN error", NULL, NULL );
669 /* check for special all user attributes ("*") type */
670 userattrs = ( attrs == NULL ) ? 1
671 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
673 /* check for special all operational attributes ("+") type */
674 opattrs = ( attrs == NULL ) ? 0
675 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
677 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
678 #ifdef SLAPD_SCHEMA_NOT_COMPAT
679 AttributeDescription *desc = a->a_desc;
681 char *desc = a->a_type;
684 if ( attrs == NULL ) {
685 /* all addrs request, skip operational attributes */
686 #ifdef SLAPD_SCHEMA_NOT_COMPAT
687 if( is_at_operational( desc->ad_type ) )
689 if( oc_check_op_attr( desc ) )
696 /* specific addrs requested */
697 #ifdef SLAPD_SCHEMA_NOT_COMPAT
698 if ( is_at_operational( desc->ad_type ) )
700 if ( oc_check_op_attr( desc ) )
703 if( !opattrs && !ad_inlist( desc, attrs ) ) {
707 if (!userattrs && !ad_inlist( desc, attrs ) ) {
713 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
714 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
719 if (( rc = ber_printf( ber, "{s[" /*]}*/ , desc )) == -1 ) {
720 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
722 send_ldap_result( conn, op, LDAP_OTHER,
723 NULL, "encoding description error", NULL, NULL );
728 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
729 if ( ! access_allowed( be, conn, op, e,
730 desc, a->a_vals[i], ACL_READ ) )
732 Debug( LDAP_DEBUG_ACL,
733 "acl: access to attribute %s, value %d not allowed\n",
738 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
739 Debug( LDAP_DEBUG_ANY,
740 "ber_printf failed\n", 0, 0, 0 );
742 send_ldap_result( conn, op, LDAP_OTHER,
743 NULL, "encoding values error", NULL, NULL );
749 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
750 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
752 send_ldap_result( conn, op, LDAP_OTHER,
753 NULL, "encode end error", NULL, NULL );
758 /* eventually will loop through generated operational attributes */
759 /* only have subschemaSubentry implemented */
760 aa = backend_operational( be, e );
762 for (a = aa ; a == NULL; a = a->a_next ) {
763 #ifdef SLAPD_SCHEMA_NOT_COMPAT
764 AttributeDescription *desc = a->a_desc;
766 char *desc = a->a_type;
769 if ( attrs == NULL ) {
770 /* all addrs request, skip operational attributes */
771 #ifdef SLAPD_SCHEMA_NOT_COMPAT
772 if( is_at_operational( desc->ad_type ) )
774 if( oc_check_op_attr( desc ) )
781 /* specific addrs requested */
782 #ifdef SLAPD_SCHEMA_NOT_COMPAT
783 if( is_at_operational( desc->ad_type ) )
785 if( oc_check_op_attr( desc ) )
788 if( !opattrs && !ad_inlist( desc, attrs ) )
793 if (!userattrs && !ad_inlist( desc, attrs ) )
800 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
801 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
806 if (( rc = ber_printf( ber, "{s[" /*]}*/ , desc )) == -1 ) {
807 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
809 send_ldap_result( conn, op, LDAP_OTHER,
810 NULL, "encoding description error", NULL, NULL );
815 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
816 if ( ! access_allowed( be, conn, op, e,
817 desc, a->a_vals[i], ACL_READ ) )
819 Debug( LDAP_DEBUG_ACL,
820 "acl: access to attribute %s, value %d not allowed\n",
826 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
827 Debug( LDAP_DEBUG_ANY,
828 "ber_printf failed\n", 0, 0, 0 );
830 send_ldap_result( conn, op, LDAP_OTHER,
831 NULL, "encoding values error", NULL, NULL );
837 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
838 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
840 send_ldap_result( conn, op, LDAP_OTHER,
841 NULL, "encode end error", NULL, NULL );
848 rc = ber_printf( ber, /*{{{*/ "}}}" );
851 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
853 send_ldap_result( conn, op, LDAP_OTHER,
854 NULL, "encode entry end error", NULL, NULL );
858 bytes = send_ldap_ber( conn, ber );
862 Debug( LDAP_DEBUG_ANY,
863 "send_ldap_response: ber write failed\n",
868 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
869 num_bytes_sent += bytes;
872 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
874 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
875 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
877 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
886 send_search_reference(
891 struct berval **refs,
894 struct berval ***v2refs
901 #ifdef SLAPD_SCHEMA_NOT_COMPAT
902 static AttributeDescription *ref = NULL;
903 static AttributeDescription *entry = NULL;
905 static const char *ref = "ref";
906 static const char *entry = "entry";
909 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
911 if ( ! access_allowed( be, conn, op, e,
912 entry, NULL, ACL_READ ) )
914 Debug( LDAP_DEBUG_ACL,
915 "send_search_reference: access to entry not allowed\n",
920 if ( ! access_allowed( be, conn, op, e,
921 ref, NULL, ACL_READ ) )
923 Debug( LDAP_DEBUG_ACL,
924 "send_search_reference: access to reference not allowed\n",
930 Debug( LDAP_DEBUG_ANY,
931 "send_search_reference: null ref in (%s)\n",
936 if( op->o_protocol < LDAP_VERSION3 ) {
937 /* save the references for the result */
938 if( *refs != NULL ) {
939 value_add( v2refs, refs );
944 ber = ber_alloc_t( LBER_USE_DER );
947 Debug( LDAP_DEBUG_ANY,
948 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
949 send_ldap_result( conn, op, LDAP_OTHER,
950 NULL, "alloc BER error", NULL, NULL );
954 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
955 LDAP_RES_SEARCH_REFERENCE, refs );
958 Debug( LDAP_DEBUG_ANY,
959 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
961 send_ldap_result( conn, op, LDAP_OTHER,
962 NULL, "encode DN error", NULL, NULL );
966 bytes = send_ldap_ber( conn, ber );
969 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
970 num_bytes_sent += bytes;
973 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
975 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
976 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
978 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
995 *code = LDAP_SUCCESS;
999 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1000 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1007 while ( (s = strchr( s, '\n' )) != NULL ) {
1012 if ( (c = strchr( s, ':' )) != NULL ) {
1016 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1020 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1024 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1029 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",