1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <ac/string.h>
13 #include <ac/unistd.h>
15 #include "ldap_defaults.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://") &&
83 strncasecmp( refs[i]->bv_val, "ldap://",
84 sizeof("ldap://")-1 ) == 0 )
87 for( j=sizeof("ldap://"); 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 acl = acl_get_applicable( be, op, e, a->a_type,
576 MAXREMATCHES, matches );
578 if ( ! acl_access_allowed( acl, a->a_type, be, conn, e,
579 NULL, op, ACL_READ, edn, matches ) )
584 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
585 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
587 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
588 NULL, "encoding type error", NULL, NULL );
593 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
594 if ( a->a_syntax & SYNTAX_DN &&
595 ! acl_access_allowed( acl, a->a_type, be, conn, e, a->a_vals[i], op,
596 ACL_READ, edn, matches) )
601 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
602 Debug( LDAP_DEBUG_ANY,
603 "ber_printf failed\n", 0, 0, 0 );
605 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
606 NULL, "encoding value error", NULL, NULL );
612 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
613 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
615 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
616 NULL, "encode end error", NULL, NULL );
621 #ifdef SLAPD_SCHEMA_DN
622 /* eventually will loop through generated operational attributes */
623 /* only have subschemaSubentry implemented */
624 a = backend_subschemasubentry( be );
627 regmatch_t matches[MAXREMATCHES];
629 if ( attrs == NULL ) {
630 /* all addrs request, skip operational attributes */
631 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
636 /* specific addrs requested */
637 if ( oc_check_operational_attr( a->a_type ) ) {
638 if( !opattrs && !charray_inlist( attrs, a->a_type ) )
643 if (!userattrs && !charray_inlist( attrs, a->a_type ) )
650 acl = acl_get_applicable( be, op, e, a->a_type,
651 MAXREMATCHES, matches );
653 if ( ! acl_access_allowed( acl, a->a_type, be, conn, e,
654 NULL, op, ACL_READ, edn, matches ) )
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 ( a->a_syntax & SYNTAX_DN &&
670 ! acl_access_allowed( acl, a->a_type, be, conn, e, a->a_vals[i], op,
671 ACL_READ, edn, matches) )
676 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
677 Debug( LDAP_DEBUG_ANY,
678 "ber_printf failed\n", 0, 0, 0 );
680 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
681 NULL, "encoding value error", NULL, NULL );
687 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
688 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
690 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
691 NULL, "encode end error", NULL, NULL );
697 rc = ber_printf( ber, /*{{{*/ "}}}" );
700 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
702 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
703 NULL, "encode entry end error", NULL, NULL );
707 bytes = send_ldap_ber( conn, ber );
711 Debug( LDAP_DEBUG_ANY,
712 "send_ldap_response: ber write failed\n",
717 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
718 num_bytes_sent += bytes;
721 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
723 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
724 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
726 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
735 send_search_reference(
740 struct berval **refs,
743 struct berval ***v2refs
750 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
752 if ( ! access_allowed( be, conn, op, e,
753 "entry", NULL, ACL_READ ) )
755 Debug( LDAP_DEBUG_ACL,
756 "send_search_reference: access to entry not allowed\n",
761 if ( ! access_allowed( be, conn, op, e,
762 "ref", NULL, ACL_READ ) )
764 Debug( LDAP_DEBUG_ACL,
765 "send_search_reference: access to reference not allowed\n",
771 Debug( LDAP_DEBUG_ANY,
772 "send_search_reference: null ref in (%s)\n",
777 if( op->o_protocol < LDAP_VERSION3 ) {
778 /* save the references for the result */
779 if( *refs == NULL ) {
780 value_add( v2refs, refs );
785 ber = ber_alloc_t( LBER_USE_DER );
788 Debug( LDAP_DEBUG_ANY,
789 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
790 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
791 NULL, "alloc BER error", NULL, NULL );
795 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
796 LDAP_RES_SEARCH_REFERENCE, refs );
799 Debug( LDAP_DEBUG_ANY,
800 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
802 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
803 NULL, "encode dn error", NULL, NULL );
807 bytes = send_ldap_ber( conn, ber );
810 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
811 num_bytes_sent += bytes;
814 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
816 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
817 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
819 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
836 *code = LDAP_SUCCESS;
840 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
841 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
848 while ( (s = strchr( s, '\n' )) != NULL ) {
853 if ( (c = strchr( s, ':' )) != NULL ) {
857 if ( strncasecmp( s, "code", 4 ) == 0 ) {
861 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
865 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
870 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",