1 /* result.c - routines to send ldap results, errors, and referrals */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
18 #include <ac/unistd.h>
22 static char *v2ref( struct berval **ref, const char *text )
24 size_t len = 0, i = 0;
30 return ch_strdup(text);
37 if (text[len-1] != '\n')
40 v2 = ch_malloc( len+i+sizeof("Referral:") );
46 strcpy( v2+len, "Referral:" );
47 len += sizeof("Referral:");
49 for( i=0; ref[i] != NULL; i++ ) {
50 v2 = ch_realloc( v2, len + ref[i]->bv_len + 1 );
52 AC_MEMCPY(&v2[len], ref[i]->bv_val, ref[i]->bv_len );
53 len += ref[i]->bv_len;
54 if (ref[i]->bv_val[ref[i]->bv_len-1] != '/')
62 static ber_tag_t req2res( ber_tag_t tag )
67 case LDAP_REQ_COMPARE:
68 case LDAP_REQ_EXTENDED:
75 tag = LDAP_RES_DELETE;
78 case LDAP_REQ_ABANDON:
84 tag = LDAP_RES_SEARCH_RESULT;
94 static void trim_refs_urls(
95 struct berval **refs )
99 if( refs == NULL ) return;
101 for( i=0; refs[i] != NULL; i++ ) {
102 if( refs[i]->bv_len > sizeof("ldap://")-1 &&
103 strncasecmp( refs[i]->bv_val, "ldap://",
104 sizeof("ldap://")-1 ) == 0 )
107 for( j=sizeof("ldap://")-1; j<refs[i]->bv_len ; j++ ) {
108 if( refs[i]->bv_val[j] == '/' ) {
109 refs[i]->bv_val[j] = '\0';
118 struct berval **get_entry_referrals(
125 struct berval **refs;
128 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
130 attr = attr_find( e->e_attrs, ad_ref );
132 if( attr == NULL ) return NULL;
134 for( i=0; attr->a_vals[i] != NULL; i++ ) {
135 /* count references */
138 if( i < 1 ) return NULL;
140 refs = ch_malloc( (i + 1) * sizeof(struct berval *));
142 for( i=0, j=0; attr->a_vals[i] != NULL; i++ ) {
144 struct berval *ref = ber_bvdup( attr->a_vals[i] );
147 for( k=0; k<ref->bv_len; k++ ) {
148 if( isspace(ref->bv_val[k]) ) {
149 ref->bv_val[k] = '\0';
155 if( ref->bv_len > 0 ) {
166 ber_bvecfree( refs );
170 /* we should check that a referral value exists... */
175 static long send_ldap_ber(
181 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
183 /* write only one pdu at a time - wait til it's our turn */
184 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
186 /* lock the connection */
187 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
194 if ( connection_state_closing( conn ) ) {
195 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
196 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
201 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
208 * we got an error. if it's ewouldblock, we need to
209 * wait on the socket being writable. otherwise, figure
210 * it's a hard error and return.
213 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
214 err, sock_errstr(err), 0 );
216 if ( err != EWOULDBLOCK && err != EAGAIN ) {
217 connection_closing( conn );
219 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
220 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
225 /* wait for socket to be write-ready */
226 conn->c_writewaiter = 1;
227 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
228 slapd_set_write( sd, 1 );
230 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
231 conn->c_writewaiter = 0;
234 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
235 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
251 struct berval *resdata,
252 struct berval *sasldata,
260 assert( ctrls == NULL ); /* ctrls not implemented */
262 ber = ber_alloc_t( LBER_USE_DER );
264 Debug( LDAP_DEBUG_TRACE, "send_ldap_response: msgid=%ld tag=%ld err=%ld\n",
265 (long) msgid, (long) tag, (long) err );
267 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=%s\n",
268 ref[0] && ref[0]->bv_val ? ref[0]->bv_val : "NULL",
273 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
278 rc = ber_printf( ber, "{it{ess",
280 matched == NULL ? "" : matched,
281 text == NULL ? "" : text );
285 assert( err == LDAP_REFERRAL );
286 rc = ber_printf( ber, "t{V}",
287 LDAP_TAG_REFERRAL, ref );
289 assert( err != LDAP_REFERRAL );
293 if( rc != -1 && sasldata != NULL ) {
294 rc = ber_printf( ber, "tO",
295 LDAP_TAG_SASL_RES_CREDS, sasldata );
298 if( rc != -1 && resoid != NULL ) {
299 rc = ber_printf( ber, "ts",
300 LDAP_TAG_EXOP_RES_OID, resoid );
303 if( rc != -1 && resdata != NULL ) {
304 rc = ber_printf( ber, "tO",
305 LDAP_TAG_EXOP_RES_VALUE, resdata );
309 rc = ber_printf( ber, "N}N}" );
314 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
320 bytes = send_ldap_ber( conn, ber );
324 Debug( LDAP_DEBUG_ANY,
325 "send_ldap_response: ber write failed\n",
330 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
331 num_bytes_sent += bytes;
333 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
339 send_ldap_disconnect(
350 #define LDAP_UNSOLICITED_ERROR(e) \
351 ( (e) == LDAP_PROTOCOL_ERROR \
352 || (e) == LDAP_STRONG_AUTH_REQUIRED \
353 || (e) == LDAP_UNAVAILABLE )
355 assert( LDAP_UNSOLICITED_ERROR( err ) );
357 Debug( LDAP_DEBUG_TRACE,
358 "send_ldap_disconnect %d:%s\n",
359 err, text ? text : "", NULL );
361 if ( op->o_protocol < LDAP_VERSION3 ) {
363 tag = req2res( op->o_tag );
364 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
367 reqoid = LDAP_NOTICE_DISCONNECT;
368 tag = LDAP_RES_EXTENDED;
372 send_ldap_response( conn, op, tag, msgid,
373 err, NULL, text, NULL,
374 reqoid, NULL, NULL, NULL );
376 Statslog( LDAP_DEBUG_STATS,
377 "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
378 (long) op->o_connid, (long) op->o_opid,
379 (long) tag, (long) err, text ? text : "" );
397 assert( !LDAP_API_ERROR( err ) );
399 Debug( LDAP_DEBUG_TRACE, "send_ldap_result: conn=%ld op=%ld p=%d\n",
400 (long) op->o_connid, (long) op->o_opid, op->o_protocol );
401 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: %d:%s:%s\n",
402 err, matched ? matched : "", text ? text : "" );
404 Debug( LDAP_DEBUG_ARGS, "send_ldap_result: referral: %s\n",
405 ref[0] && ref[0]->bv_val ? ref[0]->bv_val : "NULL",
409 assert( err != LDAP_PARTIAL_RESULTS );
411 if( op->o_tag != LDAP_REQ_SEARCH ) {
412 trim_refs_urls( ref );
415 if ( err == LDAP_REFERRAL ) {
417 err = LDAP_NO_SUCH_OBJECT;
418 } else if ( op->o_protocol < LDAP_VERSION3 ) {
419 err = LDAP_PARTIAL_RESULTS;
423 if ( op->o_protocol < LDAP_VERSION3 ) {
424 tmp = v2ref( ref, text );
429 tag = req2res( op->o_tag );
430 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
432 send_ldap_response( conn, op, tag, msgid,
433 err, matched, text, ref,
434 NULL, NULL, NULL, ctrls );
436 Statslog( LDAP_DEBUG_STATS,
437 "conn=%ld op=%ld RESULT tag=%lu err=%ld text=%s\n",
438 (long) op->o_connid, (long) op->o_opid,
439 (long) tag, (long) err, text ? text : "" );
461 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%ld len=%ld\n",
462 (long) err, cred ? cred->bv_len : -1, NULL );
464 tag = req2res( op->o_tag );
465 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
467 send_ldap_response( conn, op, tag, msgid,
468 err, matched, text, ref,
469 NULL, NULL, cred, ctrls );
479 struct berval **refs,
481 struct berval *rspdata,
488 Debug( LDAP_DEBUG_TRACE,
489 "send_ldap_extended %ld:%s (%ld)\n",
491 rspoid ? rspoid : "",
492 rspdata != NULL ? (long) rspdata->bv_len : (long) 0 );
494 tag = req2res( op->o_tag );
495 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
497 send_ldap_response( conn, op, tag, msgid,
498 err, matched, text, refs,
499 rspoid, rspdata, NULL, ctrls );
510 struct berval **refs,
518 assert( !LDAP_API_ERROR( err ) );
520 Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
521 err, matched ? matched : "", text ? text : "" );
523 assert( err != LDAP_PARTIAL_RESULTS );
525 trim_refs_urls( refs );
527 if( op->o_protocol < LDAP_VERSION3 ) {
528 /* send references in search results */
529 if( err == LDAP_REFERRAL ) {
530 err = LDAP_PARTIAL_RESULTS;
533 tmp = v2ref( refs, text );
537 /* don't send references in search results */
538 assert( refs == NULL );
541 if( err == LDAP_REFERRAL ) {
546 tag = req2res( op->o_tag );
547 msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;
549 send_ldap_response( conn, op, tag, msgid,
550 err, matched, text, refs,
551 NULL, NULL, NULL, ctrls );
553 Statslog( LDAP_DEBUG_STATS,
554 "conn=%ld op=%ld SEARCH RESULT tag=%lu err=%ld text=%s\n",
555 (long) op->o_connid, (long) op->o_opid,
556 (long) tag, (long) err, text ? text : "" );
581 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
583 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: \"%s\"\n", e->e_dn, 0, 0 );
585 if ( ! access_allowed( be, conn, op, e,
586 ad_entry, NULL, ACL_READ ) )
588 Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
595 ber = ber_alloc_t( LBER_USE_DER );
598 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
599 send_ldap_result( conn, op, LDAP_OTHER,
600 NULL, "BER allocation error", NULL, NULL );
604 rc = ber_printf( ber, "{it{s{" /*}}}*/, op->o_msgid,
605 LDAP_RES_SEARCH_ENTRY, e->e_dn );
608 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
610 send_ldap_result( conn, op, LDAP_OTHER,
611 NULL, "encoding DN error", NULL, NULL );
615 /* check for special all user attributes ("*") type */
616 userattrs = ( attrs == NULL ) ? 1
617 : charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES );
619 /* check for special all operational attributes ("+") type */
620 opattrs = ( attrs == NULL ) ? 0
621 : charray_inlist( attrs, LDAP_ALL_OPERATIONAL_ATTRIBUTES );
623 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
624 AttributeDescription *desc = a->a_desc;
625 char *type = desc->ad_cname->bv_val;
627 if ( attrs == NULL ) {
628 /* all addrs request, skip operational attributes */
629 if( is_at_operational( desc->ad_type ) )
635 /* specific addrs requested */
636 if ( is_at_operational( desc->ad_type ) )
638 if( !opattrs && !ad_inlist( desc, attrs ) ) {
642 if (!userattrs && !ad_inlist( desc, attrs ) ) {
648 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
649 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
650 desc->ad_cname->bv_val, 0, 0 );
654 if (( rc = ber_printf( ber, "{s[" /*]}*/ , type )) == -1 ) {
655 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
657 send_ldap_result( conn, op, LDAP_OTHER,
658 NULL, "encoding description error", NULL, NULL );
663 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
664 if ( ! access_allowed( be, conn, op, e,
665 desc, a->a_vals[i], ACL_READ ) )
667 Debug( LDAP_DEBUG_ACL,
668 "acl: access to attribute %s, value %d not allowed\n",
669 desc->ad_cname->bv_val, i, 0 );
673 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
674 Debug( LDAP_DEBUG_ANY,
675 "ber_printf failed\n", 0, 0, 0 );
677 send_ldap_result( conn, op, LDAP_OTHER,
678 NULL, "encoding values error", NULL, NULL );
684 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
685 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
687 send_ldap_result( conn, op, LDAP_OTHER,
688 NULL, "encode end error", NULL, NULL );
693 /* eventually will loop through generated operational attributes */
694 /* only have subschemaSubentry implemented */
695 aa = backend_operational( be, e );
697 for (a = aa ; a != NULL; a = a->a_next ) {
698 AttributeDescription *desc = a->a_desc;
700 if ( attrs == NULL ) {
701 /* all addrs request, skip operational attributes */
702 if( is_at_operational( desc->ad_type ) ) {
707 /* specific addrs requested */
708 if( is_at_operational( desc->ad_type ) ) {
709 if( !opattrs && !ad_inlist( desc, attrs ) )
714 if (!userattrs && !ad_inlist( desc, attrs ) )
721 if ( ! access_allowed( be, conn, op, e, desc, NULL, ACL_READ ) ) {
722 Debug( LDAP_DEBUG_ACL, "acl: access to attribute %s not allowed\n",
723 desc->ad_cname->bv_val, 0, 0 );
727 rc = ber_printf( ber, "{s[" /*]}*/ , desc->ad_cname->bv_val );
729 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
731 send_ldap_result( conn, op, LDAP_OTHER,
732 NULL, "encoding description error", NULL, NULL );
737 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
738 if ( ! access_allowed( be, conn, op, e,
739 desc, a->a_vals[i], ACL_READ ) )
741 Debug( LDAP_DEBUG_ACL,
742 "acl: access to attribute %s, value %d not allowed\n",
743 desc->ad_cname->bv_val, i, 0 );
748 if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
749 Debug( LDAP_DEBUG_ANY,
750 "ber_printf failed\n", 0, 0, 0 );
752 send_ldap_result( conn, op, LDAP_OTHER,
753 NULL, "encoding values error", NULL, NULL );
759 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
760 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
762 send_ldap_result( conn, op, LDAP_OTHER,
763 NULL, "encode end error", NULL, NULL );
770 rc = ber_printf( ber, /*{{{*/ "}N}N}" );
773 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
775 send_ldap_result( conn, op, LDAP_OTHER,
776 NULL, "encode entry end error", NULL, NULL );
780 bytes = send_ldap_ber( conn, ber );
784 Debug( LDAP_DEBUG_ANY,
785 "send_ldap_response: ber write failed\n",
790 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
791 num_bytes_sent += bytes;
794 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
796 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
797 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
799 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
808 send_search_reference(
813 struct berval **refs,
816 struct berval ***v2refs
823 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
824 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
826 Debug( LDAP_DEBUG_TRACE, "=> send_search_reference (%s)\n", e->e_dn, 0, 0 );
828 if ( ! access_allowed( be, conn, op, e,
829 ad_entry, NULL, ACL_READ ) )
831 Debug( LDAP_DEBUG_ACL,
832 "send_search_reference: access to entry not allowed\n",
837 if ( ! access_allowed( be, conn, op, e,
838 ad_ref, NULL, ACL_READ ) )
840 Debug( LDAP_DEBUG_ACL,
841 "send_search_reference: access to reference not allowed\n",
847 Debug( LDAP_DEBUG_ANY,
848 "send_search_reference: null ref in (%s)\n",
853 if( op->o_protocol < LDAP_VERSION3 ) {
854 /* save the references for the result */
855 if( *refs != NULL ) {
856 value_add( v2refs, refs );
861 ber = ber_alloc_t( LBER_USE_DER );
864 Debug( LDAP_DEBUG_ANY,
865 "send_search_reference: ber_alloc failed\n", 0, 0, 0 );
866 send_ldap_result( conn, op, LDAP_OTHER,
867 NULL, "alloc BER error", NULL, NULL );
871 rc = ber_printf( ber, "{it{V}N}", op->o_msgid,
872 LDAP_RES_SEARCH_REFERENCE, refs );
875 Debug( LDAP_DEBUG_ANY,
876 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
878 send_ldap_result( conn, op, LDAP_OTHER,
879 NULL, "encode DN error", NULL, NULL );
883 bytes = send_ldap_ber( conn, ber );
886 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
887 num_bytes_sent += bytes;
890 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
892 Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
893 (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
895 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
912 *code = LDAP_SUCCESS;
916 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
917 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
924 while ( (s = strchr( s, '\n' )) != NULL ) {
929 if ( (c = strchr( s, ':' )) != NULL ) {
933 if ( strncasecmp( s, "code", 4 ) == 0 ) {
937 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
941 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
946 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",