1 /* result.c - routines to send ldap results, errors, and referrals */
10 #include <ac/string.h>
12 #include <ac/unistd.h>
16 /* we need LBER internals */
17 #include "../../libraries/liblber/lber-int.h"
19 static char *v2ref( struct berval **ref )
24 if(ref == NULL) return NULL;
26 len = sizeof("Referral:");
27 v2 = ch_strdup("Referral:");
29 for( i=0; ref[i] != NULL; i++ ) {
30 v2 = ch_realloc( v2, len + ref[i]->bv_len + 1 );
32 memcpy(&v2[len], ref[i]->bv_val, ref[i]->bv_len );
33 len += ref[i]->bv_len;
40 static ber_tag_t req2res( ber_tag_t tag )
45 case LDAP_REQ_COMPARE:
46 case LDAP_REQ_EXTENDED:
53 tag = LDAP_RES_DELETE;
56 case LDAP_REQ_ABANDON:
62 tag = LDAP_RES_SEARCH_RESULT;
72 static void trim_refs_urls(
73 struct berval **refs )
77 if( refs == NULL ) return;
79 for( i=0; refs[i] != NULL; i++ ) {
80 if( refs[i]->bv_len > sizeof("ldap://") &&
81 strncasecmp( refs[i]->bv_val, "ldap://",
82 sizeof("ldap://")-1 ) == 0 )
85 for( j=sizeof("ldap://"); j<refs[i]->bv_len ; j++ ) {
86 if( refs[i]->bv_val[j] = '/' ) {
87 refs[i]->bv_val[j] = '\0';
96 struct berval **get_entry_referrals(
103 struct berval **refs;
106 attr = attr_find( e->e_attrs, "ref" );
108 if( attr == NULL ) return NULL;
110 for( i=0; attr->a_vals[i] != NULL; i++ ) {
111 /* count references */
114 if( i < 1 ) return NULL;
116 refs = ch_malloc( i + 1 );
118 for( i=0, j=0; attr->a_vals[i] != NULL; i++ ) {
120 struct berval *ref = ber_bvdup( attr->a_vals[i] );
123 for( k=0; k<ref->bv_len; k++ ) {
124 if( isspace(ref->bv_val[k]) ) {
125 ref->bv_val[k] = '\0';
131 if( ref->bv_len > 0 ) {
142 ber_bvecfree( refs );
146 /* we should check that a referral value exists... */
151 static long send_ldap_ber(
155 ber_len_t bytes = ber_pvt_ber_bytes( ber );
157 /* write only one pdu at a time - wait til it's our turn */
158 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
160 /* lock the connection */
161 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
167 if ( connection_state_closing( conn ) ) {
168 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
169 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
173 if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
180 * we got an error. if it's ewouldblock, we need to
181 * wait on the socket being writable. otherwise, figure
182 * it's a hard error and return.
185 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
186 err, err > -1 && err < sys_nerr ? sys_errlist[err]
189 if ( err != EWOULDBLOCK && err != EAGAIN ) {
190 connection_closing( conn );
192 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
193 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
197 /* wait for socket to be write-ready */
198 conn->c_writewaiter = 1;
199 slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
201 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
202 conn->c_writewaiter = 0;
205 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
206 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
222 struct berval *resdata,
230 assert( ctrls == NULL ); /* ctrls not implemented */
232 ber = ber_alloc_t( LBER_USE_DER );
234 Debug( LDAP_DEBUG_TRACE, "send_ldap_response: tag=%ld msgid=%ld err=%ld\n",
235 (long) tag, (long) msgid, (long) err );
238 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
242 #ifdef LDAP_CONNECTIONLESS
244 rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
245 err, matched ? matched : "", text ? text : "" );
249 rc = ber_printf( ber, "{it{ess",
251 matched == NULL ? "" : matched,
252 text == NULL ? "" : text );
254 if( rc != -1 && ref != NULL ) {
255 rc = ber_printf( ber, "{V}", ref );
258 if( rc != -1 && resoid != NULL ) {
259 rc = ber_printf( ber, "s", resoid );
262 if( rc != -1 && resdata != NULL ) {
263 rc = ber_printf( ber, "O", resdata );
268 rc = ber_printf( ber, "}}" );
273 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
278 bytes = send_ldap_ber( conn, ber );
281 Debug( LDAP_DEBUG_ANY,
282 "send_ldap_response: ber write failed\n",
287 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
288 num_bytes_sent += bytes;
290 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
296 send_ldap_disconnect(
307 #define LDAP_UNSOLICITED_ERROR(e) \
308 ( (e) == LDAP_PROTOCOL_ERROR \
309 || (e) == LDAP_STRONG_AUTH_REQUIRED \
310 || (e) == LDAP_UNAVAILABLE )
312 assert( LDAP_UNSOLICITED_ERROR( err ) );
314 Debug( LDAP_DEBUG_TRACE,
315 "send_ldap_disconnect %d:%s\n",
316 err, text ? text : "", NULL );
318 if ( op->o_protocol < LDAP_VERSION3 ) {
320 tag = req2res( op->o_tag );
321 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
324 reqoid = LDAP_NOTICE_DISCONNECT;
325 tag = LDAP_RES_EXTENDED;
329 #ifdef LDAP_CONNECTIONLESS
331 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
332 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
333 inet_ntoa(((struct sockaddr_in *)
334 &op->o_clientaddr)->sin_addr ),
335 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
339 send_ldap_response( conn, op, tag, msgid,
340 err, NULL, text, NULL,
341 reqoid, NULL, NULL );
343 Statslog( LDAP_DEBUG_STATS,
344 "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
345 (long) conn->c_connid, (long) op->o_opid,
346 (long) tag, (long) err, text );
364 assert( !LDAP_API_ERROR( err ) );
366 Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n",
367 err, matched ? matched : "", text ? text : "" );
369 assert( err != LDAP_PARTIAL_RESULTS );
371 if( op->o_tag != LDAP_REQ_SEARCH ) {
372 trim_refs_urls( ref );
375 if ( err == LDAP_REFERRAL ) {
377 err = LDAP_NO_SUCH_OBJECT;
378 } else if ( op->o_protocol < LDAP_VERSION3 ) {
379 err = LDAP_PARTIAL_RESULTS;
380 tmp = text = v2ref( ref );
385 tag = req2res( op->o_tag );
386 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
388 #ifdef LDAP_CONNECTIONLESS
390 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
391 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
392 inet_ntoa(((struct sockaddr_in *)
393 &op->o_clientaddr)->sin_addr ),
394 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
399 send_ldap_response( conn, op, tag, msgid,
400 err, matched, text, ref,
403 Statslog( LDAP_DEBUG_STATS,
404 "conn=%ld op=%ld RESULT err=%ld tag=%lu text=%s\n",
405 (long) conn->c_connid, (long) op->o_opid,
406 (long) err, (long) tag, text );
421 struct berval **refs,
429 assert( !LDAP_API_ERROR( err ) );
431 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
432 err, matched ? matched : "", text ? text : "" );
434 assert( err != LDAP_PARTIAL_RESULTS );
436 trim_refs_urls( refs );
438 if( op->o_protocol < LDAP_VERSION3 ) {
439 /* send references in search results */
440 if( err == LDAP_REFERRAL ) {
441 err = LDAP_PARTIAL_RESULTS;
442 tmp = text = v2ref( refs );
447 /* don't send references in search results */
448 if( err == LDAP_REFERRAL ) {
454 tag = req2res( op->o_tag );
455 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
457 #ifdef LDAP_CONNECTIONLESS
459 ber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
460 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
461 inet_ntoa(((struct sockaddr_in *)
462 &op->o_clientaddr)->sin_addr ),
463 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
468 send_ldap_response( conn, op, tag, msgid,
469 err, matched, text, refs,
472 Statslog( LDAP_DEBUG_STATS,
473 "conn=%ld op=%ld SEARCH RESULT err=%ld tag=%lu text=%s\n",
474 (long) conn->c_connid, (long) op->o_opid,
475 (long) err, (long) tag, text );
499 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
501 #if defined( SLAPD_SCHEMA_DN )
503 /* this could be backend specific */
505 val.bv_val = SLAPD_SCHEMA_DN;
506 val.bv_len = strlen( val.bv_val );
507 attr_merge( e, "subschemaSubentry", vals );
508 ldap_memfree( val.bv_val );
512 if ( ! access_allowed( be, conn, op, e,
513 "entry", NULL, ACL_READ ) )
515 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
522 ber = ber_alloc_t( LBER_USE_DER );
525 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
526 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
527 NULL, "allocating BER error", NULL, NULL );
531 rc = ber_printf( ber, "{it{s{", op->o_msgid,
532 LDAP_RES_SEARCH_ENTRY, e->e_dn );
535 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
537 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
538 NULL, "encoding dn error", NULL, NULL );
542 /* check for special all user attributes ("*") attribute */
543 allattrs = attrs == NULL
545 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
547 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
548 regmatch_t matches[MAXREMATCHES];
550 if ( attrs == NULL ) {
551 /* all addrs request, skip operational attributes */
552 if( !opattrs && oc_check_operational_attr( a->a_type ) ) {
557 /* specific addrs requested */
558 if ( !allattrs && !charray_inlist( attrs, a->a_type ) ) {
563 acl = acl_get_applicable( be, op, e, a->a_type,
564 MAXREMATCHES, matches );
566 if ( ! acl_access_allowed( acl, be, conn, e,
567 NULL, op, ACL_READ, edn, matches ) )
572 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
573 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
575 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
576 NULL, "encoding type error", NULL, NULL );
581 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
582 if ( a->a_syntax & SYNTAX_DN &&
583 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
584 ACL_READ, edn, matches) )
589 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
590 Debug( LDAP_DEBUG_ANY,
591 "ber_printf failed\n", 0, 0, 0 );
593 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
594 NULL, "encoding value error", NULL, NULL );
600 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
601 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
603 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
604 NULL, "encode end error", NULL, NULL );
609 rc = ber_printf( ber, /*{{{*/ "}}}" );
612 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
614 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
615 NULL, "encode entry end error", NULL, NULL );
619 bytes = send_ldap_ber( conn, ber );
622 Debug( LDAP_DEBUG_ANY,
623 "send_ldap_response: ber write failed\n",
628 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
629 num_bytes_sent += bytes;
632 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
634 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
635 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
637 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
646 send_search_reference(
651 struct berval **refs,
654 struct berval ***v2refs
661 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
663 if ( ! access_allowed( be, conn, op, e,
664 "entry", NULL, ACL_READ ) )
666 Debug( LDAP_DEBUG_ACL,
667 "send_search_reference: access to entry not allowed\n",
672 if ( ! access_allowed( be, conn, op, e,
673 "ref", NULL, ACL_READ ) )
675 Debug( LDAP_DEBUG_ACL,
676 "send_search_reference: access to reference not allowed\n",
682 Debug( LDAP_DEBUG_ANY,
683 "send_search_reference: null ref in (%s)\n",
688 if( op->o_protocol < LDAP_VERSION3 ) {
689 /* save the references for the result */
690 if( *refs == NULL ) {
691 value_add( v2refs, refs );
696 ber = ber_alloc_t( LBER_USE_DER );
699 Debug( LDAP_DEBUG_ANY,
700 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
701 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
702 NULL, "alloc BER error", NULL, NULL );
706 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
707 LDAP_RES_SEARCH_REFERENCE, refs );
710 Debug( LDAP_DEBUG_ANY,
711 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
713 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
714 NULL, "encode dn error", NULL, NULL );
718 bytes = send_ldap_ber( conn, ber );
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 );
746 *code = LDAP_SUCCESS;
750 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
751 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
758 while ( (s = strchr( s, '\n' )) != NULL ) {
763 if ( (c = strchr( s, ':' )) != NULL ) {
767 if ( strncasecmp( s, "code", 4 ) == 0 ) {
771 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
775 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
780 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",