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) op->o_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: conn=%ld op=%ld p=%d\n",
367 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
368 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: %d:%s:%s\n",
369 err, matched ? matched : "", text ? text : "" );
371 assert( err != LDAP_PARTIAL_RESULTS );
373 if( op->o_tag != LDAP_REQ_SEARCH ) {
374 trim_refs_urls( ref );
377 if ( err == LDAP_REFERRAL ) {
379 err = LDAP_NO_SUCH_OBJECT;
380 } else if ( op->o_protocol < LDAP_VERSION3 ) {
381 err = LDAP_PARTIAL_RESULTS;
382 tmp = text = v2ref( ref );
387 tag = req2res( op->o_tag );
388 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
390 #ifdef LDAP_CONNECTIONLESS
392 ber_pvt_sb_udp_set_dst( conn->c_sb, &op->o_clientaddr );
393 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
394 inet_ntoa(((struct sockaddr_in *)
395 &op->o_clientaddr)->sin_addr ),
396 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
401 send_ldap_response( conn, op, tag, msgid,
402 err, matched, text, ref,
405 Statslog( LDAP_DEBUG_STATS,
406 "conn=%ld op=%ld RESULT err=%ld tag=%lu text=%s\n",
407 (long) op->o_connid, (long) op->o_opid,
408 (long) err, (long) tag, text );
423 struct berval **refs,
431 assert( !LDAP_API_ERROR( err ) );
433 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
434 err, matched ? matched : "", text ? text : "" );
436 assert( err != LDAP_PARTIAL_RESULTS );
438 trim_refs_urls( refs );
440 if( op->o_protocol < LDAP_VERSION3 ) {
441 /* send references in search results */
442 if( err == LDAP_REFERRAL ) {
443 err = LDAP_PARTIAL_RESULTS;
446 tmp = text = v2ref( refs );
450 /* don't send references in search results */
451 assert( refs == NULL );
454 if( err == LDAP_REFERRAL ) {
459 tag = req2res( op->o_tag );
460 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
462 #ifdef LDAP_CONNECTIONLESS
464 ber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
465 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
466 inet_ntoa(((struct sockaddr_in *)
467 &op->o_clientaddr)->sin_addr ),
468 ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
473 send_ldap_response( conn, op, tag, msgid,
474 err, matched, text, refs,
477 Statslog( LDAP_DEBUG_STATS,
478 "conn=%ld op=%ld SEARCH RESULT err=%ld tag=%lu text=%s\n",
479 (long) op->o_connid, (long) op->o_opid,
480 (long) err, (long) tag, text );
504 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
506 #if defined( SLAPD_SCHEMA_DN )
508 /* this could be backend specific */
510 val.bv_val = SLAPD_SCHEMA_DN;
511 val.bv_len = strlen( val.bv_val );
512 attr_merge( e, "subschemaSubentry", vals );
513 ldap_memfree( val.bv_val );
517 if ( ! access_allowed( be, conn, op, e,
518 "entry", NULL, ACL_READ ) )
520 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
527 ber = ber_alloc_t( LBER_USE_DER );
530 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
531 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
532 NULL, "allocating BER error", NULL, NULL );
536 rc = ber_printf( ber, "{it{s{", op->o_msgid,
537 LDAP_RES_SEARCH_ENTRY, e->e_dn );
540 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
542 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
543 NULL, "encoding dn error", NULL, NULL );
547 /* check for special all user attributes ("*") attribute */
548 allattrs = ( attrs == NULL ) ? 1
549 : charray_inlist( attrs, LDAP_ALL_USER_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 */
563 /* user requested all user attributes */
564 /* if operational, make sure it's in list */
566 if( oc_check_operational_attr( a->a_type )
567 && !charray_inlist( attrs, a->a_type ) )
572 } else if ( !charray_inlist( attrs, a->a_type ) ) {
577 acl = acl_get_applicable( be, op, e, a->a_type,
578 MAXREMATCHES, matches );
580 if ( ! acl_access_allowed( acl, be, conn, e,
581 NULL, op, ACL_READ, edn, matches ) )
586 if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
587 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
589 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
590 NULL, "encoding type error", NULL, NULL );
595 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
596 if ( a->a_syntax & SYNTAX_DN &&
597 ! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
598 ACL_READ, edn, matches) )
603 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
604 Debug( LDAP_DEBUG_ANY,
605 "ber_printf failed\n", 0, 0, 0 );
607 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
608 NULL, "encoding value error", NULL, NULL );
614 if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
615 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
617 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
618 NULL, "encode end error", NULL, NULL );
623 rc = ber_printf( ber, /*{{{*/ "}}}" );
626 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
628 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
629 NULL, "encode entry end error", NULL, NULL );
633 bytes = send_ldap_ber( conn, ber );
636 Debug( LDAP_DEBUG_ANY,
637 "send_ldap_response: ber write failed\n",
642 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
643 num_bytes_sent += bytes;
646 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
648 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
649 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
651 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
660 send_search_reference(
665 struct berval **refs,
668 struct berval ***v2refs
675 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );
677 if ( ! access_allowed( be, conn, op, e,
678 "entry", NULL, ACL_READ ) )
680 Debug( LDAP_DEBUG_ACL,
681 "send_search_reference: access to entry not allowed\n",
686 if ( ! access_allowed( be, conn, op, e,
687 "ref", NULL, ACL_READ ) )
689 Debug( LDAP_DEBUG_ACL,
690 "send_search_reference: access to reference not allowed\n",
696 Debug( LDAP_DEBUG_ANY,
697 "send_search_reference: null ref in (%s)\n",
702 if( op->o_protocol < LDAP_VERSION3 ) {
703 /* save the references for the result */
704 if( *refs == NULL ) {
705 value_add( v2refs, refs );
710 ber = ber_alloc_t( LBER_USE_DER );
713 Debug( LDAP_DEBUG_ANY,
714 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
715 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
716 NULL, "alloc BER error", NULL, NULL );
720 rc = ber_printf( ber, "{it{V}}", op->o_msgid,
721 LDAP_RES_SEARCH_REFERENCE, refs );
724 Debug( LDAP_DEBUG_ANY,
725 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
727 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
728 NULL, "encode dn error", NULL, NULL );
732 bytes = send_ldap_ber( conn, ber );
734 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
735 num_bytes_sent += bytes;
738 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
740 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
741 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
743 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
760 *code = LDAP_SUCCESS;
764 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
765 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
772 while ( (s = strchr( s, '\n' )) != NULL ) {
777 if ( (c = strchr( s, ':' )) != NULL ) {
781 if ( strncasecmp( s, "code", 4 ) == 0 ) {
785 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
789 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
794 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",