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 *ref = slap_schema.si_ad_ref;
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;
680 char *type = desc->ad_cname->bv_val;
682 char *desc = a->a_type;
683 char *type = a->a_type;
686 if ( attrs == NULL ) {
687 /* all addrs request, skip operational attributes */
688 #ifdef SLAPD_SCHEMA_NOT_COMPAT
689 if( is_at_operational( desc->ad_type ) )
691 if( oc_check_op_attr( desc ) )
698 /* specific addrs requested */
699 #ifdef SLAPD_SCHEMA_NOT_COMPAT
700 if ( is_at_operational( desc->ad_type ) )
702 if ( oc_check_op_attr( desc ) )
705 if( !opattrs && !ad_inlist( desc, attrs ) ) {
709 if (!userattrs && !ad_inlist( desc, attrs ) ) {
715 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
716 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
721 if (( rc = ber_printf( ber, "{s[" /*]}*/ , type )) == -1 ) {
722 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
724 send_ldap_result( conn, op, LDAP_OTHER,
725 NULL, "encoding description error", NULL, NULL );
730 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
731 if ( ! access_allowed( be, conn, op, e,
732 desc, a->a_vals[i], ACL_READ ) )
734 Debug( LDAP_DEBUG_ACL,
735 "acl: access to attribute %s, value %d not allowed\n",
740 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
741 Debug( LDAP_DEBUG_ANY,
742 "ber_printf failed\n", 0, 0, 0 );
744 send_ldap_result( conn, op, LDAP_OTHER,
745 NULL, "encoding values error", NULL, NULL );
751 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
752 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
754 send_ldap_result( conn, op, LDAP_OTHER,
755 NULL, "encode end error", NULL, NULL );
760 /* eventually will loop through generated operational attributes */
761 /* only have subschemaSubentry implemented */
762 aa = backend_operational( be, e );
764 for (a = aa ; a == NULL; a = a->a_next ) {
765 #ifdef SLAPD_SCHEMA_NOT_COMPAT
766 AttributeDescription *desc = a->a_desc;
768 char *desc = a->a_type;
771 if ( attrs == NULL ) {
772 /* all addrs request, skip operational attributes */
773 #ifdef SLAPD_SCHEMA_NOT_COMPAT
774 if( is_at_operational( desc->ad_type ) )
776 if( oc_check_op_attr( desc ) )
783 /* specific addrs requested */
784 #ifdef SLAPD_SCHEMA_NOT_COMPAT
785 if( is_at_operational( desc->ad_type ) )
787 if( oc_check_op_attr( desc ) )
790 if( !opattrs && !ad_inlist( desc, attrs ) )
795 if (!userattrs && !ad_inlist( desc, attrs ) )
802 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
803 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
808 if (( rc = ber_printf( ber, "{s[" /*]}*/ , desc )) == -1 ) {
809 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
811 send_ldap_result( conn, op, LDAP_OTHER,
812 NULL, "encoding description error", NULL, NULL );
817 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
818 if ( ! access_allowed( be, conn, op, e,
819 desc, a->a_vals[i], ACL_READ ) )
821 Debug( LDAP_DEBUG_ACL,
822 "acl: access to attribute %s, value %d not allowed\n",
828 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
829 Debug( LDAP_DEBUG_ANY,
830 "ber_printf failed\n", 0, 0, 0 );
832 send_ldap_result( conn, op, LDAP_OTHER,
833 NULL, "encoding values error", NULL, NULL );
839 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
840 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
842 send_ldap_result( conn, op, LDAP_OTHER,
843 NULL, "encode end error", NULL, NULL );
850 rc = ber_printf( ber, /*{{{*/ "}}}" );
853 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
855 send_ldap_result( conn, op, LDAP_OTHER,
856 NULL, "encode entry end error", NULL, NULL );
860 bytes = send_ldap_ber( conn, ber );
864 Debug( LDAP_DEBUG_ANY,
865 "send_ldap_response: ber write failed\n",
870 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
871 num_bytes_sent += bytes;
874 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
876 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
877 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
879 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
888 send_search_reference(
893 struct berval **refs,
896 struct berval ***v2refs
903 #ifdef SLAPD_SCHEMA_NOT_COMPAT
904 AttributeDescription *ref = slap_schema.si_ad_ref;
905 AttributeDescription *entry = slap_schema.si_ad_entry;
907 static const char *ref = "ref";
908 static const char *entry = "entry";
911 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
913 if ( ! access_allowed( be, conn, op, e,
914 entry, NULL, ACL_READ ) )
916 Debug( LDAP_DEBUG_ACL,
917 "send_search_reference: access to entry not allowed\n",
922 if ( ! access_allowed( be, conn, op, e,
923 ref, NULL, ACL_READ ) )
925 Debug( LDAP_DEBUG_ACL,
926 "send_search_reference: access to reference not allowed\n",
932 Debug( LDAP_DEBUG_ANY,
933 "send_search_reference: null ref in (%s)\n",
938 if( op->o_protocol < LDAP_VERSION3 ) {
939 /* save the references for the result */
940 if( *refs != NULL ) {
941 value_add( v2refs, refs );
946 ber = ber_alloc_t( LBER_USE_DER );
949 Debug( LDAP_DEBUG_ANY,
950 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
951 send_ldap_result( conn, op, LDAP_OTHER,
952 NULL, "alloc BER error", NULL, NULL );
956 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
957 LDAP_RES_SEARCH_REFERENCE, refs );
960 Debug( LDAP_DEBUG_ANY,
961 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
963 send_ldap_result( conn, op, LDAP_OTHER,
964 NULL, "encode DN error", NULL, NULL );
968 bytes = send_ldap_ber( conn, ber );
971 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
972 num_bytes_sent += bytes;
975 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
977 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
978 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
980 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
997 *code = LDAP_SUCCESS;
1001 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1002 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1009 while ( (s = strchr( s, '\n' )) != NULL ) {
1014 if ( (c = strchr( s, ':' )) != NULL ) {
1018 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1022 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1026 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1031 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",