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;
455 if( tmp != NULL ) text = tmp;
458 /* don't send references in search results */
459 assert( refs == NULL );
462 if( err == LDAP_REFERRAL ) {
467 tag = req2res( op->o_tag );
468 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
470 #ifdef LDAP_CONNECTIONLESS
472 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
473 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
474 inet_ntoa(((struct sockaddr_in *)
475 &op->o_clientaddr)->sin_addr ),
476 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
481 send_ldap_response( conn, op, tag, msgid,
482 err, matched, text, refs,
485 Statslog( LDAP_DEBUG_STATS,
486 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
487 (long) op->o_connid, (long) op->o_opid,
488 (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 if ( attrs == NULL ) {
553 /* all addrs request, skip operational attributes */
554 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
559 /* specific addrs requested */
560 if ( oc_check_operational_attr( a->a_type ) ) {
561 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
566 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
573 if ( ! access_allowed( be, conn, op, e,
574 a->a_type, NULL, ACL_READ ) )
576 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
581 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
582 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
584 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
585 NULL, "encoding type error", NULL, NULL );
590 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
591 if ( ! access_allowed( be, conn, op, e,
592 a->a_type, a->a_vals[i], ACL_READ ) )
594 Debug( LDAP_DEBUG_ACL,
595 "acl: access to attribute %s, value %d not allowed\n",
600 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
601 Debug( LDAP_DEBUG_ANY,
602 "ber_printf failed\n", 0, 0, 0 );
604 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
605 NULL, "encoding value error", NULL, NULL );
611 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
612 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
614 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
615 NULL, "encode end error", NULL, NULL );
620 #ifdef SLAPD_SCHEMA_DN
621 /* eventually will loop through generated operational attributes */
622 /* only have subschemaSubentry implemented */
623 a = backend_subschemasubentry( be );
626 if ( attrs == NULL ) {
627 /* all addrs request, skip operational attributes */
628 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
633 /* specific addrs requested */
634 if ( oc_check_operational_attr( a->a_type ) ) {
635 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
640 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
647 if ( ! access_allowed( be, conn, op, e,
648 a->a_type, NULL, ACL_READ ) )
650 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
655 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
656 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
658 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
659 NULL, "encoding type error", NULL, NULL );
664 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
665 if ( ! access_allowed( be, conn, op, e,
666 a->a_type, a->a_vals[i], ACL_READ ) )
668 Debug( LDAP_DEBUG_ACL,
669 "acl: access to attribute %s, value %d not allowed\n",
675 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
676 Debug( LDAP_DEBUG_ANY,
677 "ber_printf failed\n", 0, 0, 0 );
679 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
680 NULL, "encoding value error", NULL, NULL );
686 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
687 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
689 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
690 NULL, "encode end error", NULL, NULL );
696 rc = ber_printf( ber, /*{{{*/ "}}}" );
699 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
701 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
702 NULL, "encode entry end error", NULL, NULL );
706 bytes = send_ldap_ber( conn, ber );
710 Debug( LDAP_DEBUG_ANY,
711 "send_ldap_response: ber write failed\n",
716 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
717 num_bytes_sent += bytes;
720 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
722 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
723 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
725 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
734 send_search_reference(
739 struct berval **refs,
742 struct berval ***v2refs
749 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
751 if ( ! access_allowed( be, conn, op, e,
752 "entry", NULL, ACL_READ ) )
754 Debug( LDAP_DEBUG_ACL,
755 "send_search_reference: access to entry not allowed\n",
760 if ( ! access_allowed( be, conn, op, e,
761 "ref", NULL, ACL_READ ) )
763 Debug( LDAP_DEBUG_ACL,
764 "send_search_reference: access to reference not allowed\n",
770 Debug( LDAP_DEBUG_ANY,
771 "send_search_reference: null ref in (%s)\n",
776 if( op->o_protocol < LDAP_VERSION3 ) {
777 /* save the references for the result */
778 if( *refs == NULL ) {
779 value_add( v2refs, refs );
784 ber = ber_alloc_t( LBER_USE_DER );
787 Debug( LDAP_DEBUG_ANY,
788 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
789 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
790 NULL, "alloc BER error", NULL, NULL );
794 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
795 LDAP_RES_SEARCH_REFERENCE, refs );
798 Debug( LDAP_DEBUG_ANY,
799 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
801 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
802 NULL, "encode dn error", NULL, NULL );
806 bytes = send_ldap_ber( conn, ber );
809 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
810 num_bytes_sent += bytes;
813 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
815 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
816 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
818 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
835 *code = LDAP_SUCCESS;
839 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
840 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
847 while ( (s = strchr( s, '\n' )) != NULL ) {
852 if ( (c = strchr( s, ':' )) != NULL ) {
856 if ( strncasecmp( s, "code", 4 ) == 0 ) {
860 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
864 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
869 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",