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 )
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://")-1 &&
83 strncasecmp( refs[i]->bv_val, "ldap://",
84 sizeof("ldap://")-1 ) == 0 )
87 for( j=sizeof("ldap://")-1; 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 );
176 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
183 * we got an error. if it's ewouldblock, we need to
184 * wait on the socket being writable. otherwise, figure
185 * it's a hard error and return.
188 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
189 err, STRERROR(err), 0 );
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 );
200 /* wait for socket to be write-ready */
201 conn->c_writewaiter = 1;
202 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
204 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
205 conn->c_writewaiter = 0;
208 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
209 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
225 struct berval *resdata,
233 assert( ctrls == NULL ); /* ctrls not implemented */
235 ber = ber_alloc_t( LBER_USE_DER );
237 Debug( LDAP_DEBUG_TRACE, "send_ldap_response: msgid=%ld tag=%ld err=%ld\n",
238 (long) msgid, (long) tag, (long) err );
241 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
245 #ifdef LDAP_CONNECTIONLESS
247 rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
248 err, matched ? matched : "", text ? text : "" );
252 rc = ber_printf( ber, "{it{ess",
254 matched == NULL ? "" : matched,
255 text == NULL ? "" : text );
257 if( rc != -1 && ref != NULL ) {
258 rc = ber_printf( ber, "{V}", ref );
261 if( rc != -1 && resoid != NULL ) {
262 rc = ber_printf( ber, "s", resoid );
265 if( rc != -1 && resdata != NULL ) {
266 rc = ber_printf( ber, "O", resdata );
271 rc = ber_printf( ber, "}}" );
276 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
282 bytes = send_ldap_ber( conn, ber );
286 Debug( LDAP_DEBUG_ANY,
287 "send_ldap_response: ber write failed\n",
292 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
293 num_bytes_sent += bytes;
295 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
301 send_ldap_disconnect(
312 #define LDAP_UNSOLICITED_ERROR(e) \
313 ( (e) == LDAP_PROTOCOL_ERROR \
314 || (e) == LDAP_STRONG_AUTH_REQUIRED \
315 || (e) == LDAP_UNAVAILABLE )
317 assert( LDAP_UNSOLICITED_ERROR( err ) );
319 Debug( LDAP_DEBUG_TRACE,
320 "send_ldap_disconnect %d:%s\n",
321 err, text ? text : "", NULL );
323 if ( op->o_protocol < LDAP_VERSION3 ) {
325 tag = req2res( op->o_tag );
326 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
329 reqoid = LDAP_NOTICE_DISCONNECT;
330 tag = LDAP_RES_EXTENDED;
334 #ifdef LDAP_CONNECTIONLESS
336 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
337 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
338 inet_ntoa(((struct sockaddr_in *)
339 &op->o_clientaddr)->sin_addr ),
340 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
344 send_ldap_response( conn, op, tag, msgid,
345 err, NULL, text, NULL,
346 reqoid, NULL, NULL );
348 Statslog( LDAP_DEBUG_STATS,
349 "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
350 (long) op->o_connid, (long) op->o_opid,
351 (long) tag, (long) err, text ? text : "" );
369 assert( !LDAP_API_ERROR( err ) );
371 Debug( LDAP_DEBUG_TRACE, "send_ldap_result: conn=%ld op=%ld p=%d\n",
372 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
373 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: %d:%s:%s\n",
374 err, matched ? matched : "", text ? text : "" );
376 assert( err != LDAP_PARTIAL_RESULTS );
378 if( op->o_tag != LDAP_REQ_SEARCH ) {
379 trim_refs_urls( ref );
382 if ( err == LDAP_REFERRAL ) {
384 err = LDAP_NO_SUCH_OBJECT;
385 } else if ( op->o_protocol < LDAP_VERSION3 ) {
386 err = LDAP_PARTIAL_RESULTS;
393 tag = req2res( op->o_tag );
394 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
396 #ifdef LDAP_CONNECTIONLESS
398 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
399 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
400 inet_ntoa(((struct sockaddr_in *)
401 &op->o_clientaddr)->sin_addr ),
402 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
407 send_ldap_response( conn, op, tag, msgid,
408 err, matched, text, ref,
411 Statslog( LDAP_DEBUG_STATS,
412 "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
413 (long) op->o_connid, (long) op->o_opid,
414 (long) tag, (long) err, text ? text : "" );
429 struct berval **refs,
437 assert( !LDAP_API_ERROR( err ) );
439 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
440 err, matched ? matched : "", text ? text : "" );
442 assert( err != LDAP_PARTIAL_RESULTS );
444 trim_refs_urls( refs );
446 if( op->o_protocol < LDAP_VERSION3 ) {
447 /* send references in search results */
448 if( err == LDAP_REFERRAL ) {
449 err = LDAP_PARTIAL_RESULTS;
457 /* don't send references in search results */
458 assert( refs == NULL );
461 if( err == LDAP_REFERRAL ) {
466 tag = req2res( op->o_tag );
467 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
469 #ifdef LDAP_CONNECTIONLESS
471 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
472 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
473 inet_ntoa(((struct sockaddr_in *)
474 &op->o_clientaddr)->sin_addr ),
475 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
480 send_ldap_response( conn, op, tag, msgid,
481 err, matched, text, refs,
484 Statslog( LDAP_DEBUG_STATS,
485 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
486 (long) op->o_connid, (long) op->o_opid,
487 (long) tag, (long) err, text ? text : "" );
511 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: \"%s\"\n", e->e_dn, 0, 0 );
513 if ( ! access_allowed( be, conn, op, e,
514 "entry", NULL, ACL_READ ) )
516 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
523 ber = ber_alloc_t( LBER_USE_DER );
526 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
527 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
528 NULL, "allocating BER error", NULL, NULL );
532 rc = ber_printf( ber, "{it{s{", op->o_msgid,
533 LDAP_RES_SEARCH_ENTRY, e->e_dn );
536 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
538 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
539 NULL, "encoding dn error", NULL, NULL );
543 /* check for special all user attributes ("*") type */
544 userattrs = ( attrs == NULL ) ? 1
545 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
547 /* check for special all operational attributes ("+") type */
548 opattrs = ( attrs == NULL ) ? 0
549 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
551 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
552 regmatch_t matches[MAXREMATCHES];
554 if ( attrs == NULL ) {
555 /* all addrs request, skip operational attributes */
556 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
561 /* specific addrs requested */
562 if ( oc_check_operational_attr( a->a_type ) ) {
563 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
568 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
575 if ( ! access_allowed( be, conn, op, e,
576 a->a_type, NULL, ACL_READ ) )
578 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
583 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
584 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
586 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
587 NULL, "encoding type error", NULL, NULL );
592 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
593 if ( ! access_allowed( be, conn, op, e,
594 a->a_type, a->a_vals[i], ACL_READ ) )
596 Debug( LDAP_DEBUG_ACL,
597 "acl: access to attribute %s, value %d not allowed\n",
602 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
603 Debug( LDAP_DEBUG_ANY,
604 "ber_printf failed\n", 0, 0, 0 );
606 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
607 NULL, "encoding value error", NULL, NULL );
613 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
614 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
616 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
617 NULL, "encode end error", NULL, NULL );
622 #ifdef SLAPD_SCHEMA_DN
623 /* eventually will loop through generated operational attributes */
624 /* only have subschemaSubentry implemented */
625 a = backend_subschemasubentry( be );
628 regmatch_t matches[MAXREMATCHES];
630 if ( attrs == NULL ) {
631 /* all addrs request, skip operational attributes */
632 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
637 /* specific addrs requested */
638 if ( oc_check_operational_attr( a->a_type ) ) {
639 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
644 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
651 if ( ! access_allowed( be, conn, op, e,
652 a->a_type, NULL, ACL_READ ) )
654 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
659 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
660 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
662 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
663 NULL, "encoding type error", NULL, NULL );
668 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
669 if ( ! access_allowed( be, conn, op, e,
670 a->a_type, a->a_vals[i], ACL_READ ) )
672 Debug( LDAP_DEBUG_ACL,
673 "acl: access to attribute %s, value %d not allowed\n",
679 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
680 Debug( LDAP_DEBUG_ANY,
681 "ber_printf failed\n", 0, 0, 0 );
683 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
684 NULL, "encoding value error", NULL, NULL );
690 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
691 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
693 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
694 NULL, "encode end error", NULL, NULL );
700 rc = ber_printf( ber, /*{{{*/ "}}}" );
703 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
705 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
706 NULL, "encode entry end error", NULL, NULL );
710 bytes = send_ldap_ber( conn, ber );
714 Debug( LDAP_DEBUG_ANY,
715 "send_ldap_response: ber write failed\n",
720 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
721 num_bytes_sent += bytes;
724 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
726 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
727 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
729 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
738 send_search_reference(
743 struct berval **refs,
746 struct berval ***v2refs
753 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
755 if ( ! access_allowed( be, conn, op, e,
756 "entry", NULL, ACL_READ ) )
758 Debug( LDAP_DEBUG_ACL,
759 "send_search_reference: access to entry not allowed\n",
764 if ( ! access_allowed( be, conn, op, e,
765 "ref", NULL, ACL_READ ) )
767 Debug( LDAP_DEBUG_ACL,
768 "send_search_reference: access to reference not allowed\n",
774 Debug( LDAP_DEBUG_ANY,
775 "send_search_reference: null ref in (%s)\n",
780 if( op->o_protocol < LDAP_VERSION3 ) {
781 /* save the references for the result */
782 if( *refs == NULL ) {
783 value_add( v2refs, refs );
788 ber = ber_alloc_t( LBER_USE_DER );
791 Debug( LDAP_DEBUG_ANY,
792 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
793 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
794 NULL, "alloc BER error", NULL, NULL );
798 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
799 LDAP_RES_SEARCH_REFERENCE, refs );
802 Debug( LDAP_DEBUG_ANY,
803 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
805 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
806 NULL, "encode dn error", NULL, NULL );
810 bytes = send_ldap_ber( conn, ber );
813 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
814 num_bytes_sent += bytes;
817 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
819 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
820 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
822 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
839 *code = LDAP_SUCCESS;
843 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
844 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
851 while ( (s = strchr( s, '\n' )) != NULL ) {
856 if ( (c = strchr( s, ':' )) != NULL ) {
860 if ( strncasecmp( s, "code", 4 ) == 0 ) {
864 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
868 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
873 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",