1 /* result.c - routines to send ldap results, errors, and referrals */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
33 #include <ac/string.h>
36 #include <ac/unistd.h>
44 int slap_null_cb( Operation *op, SlapReply *rs )
49 static char *v2ref( BerVarray ref, const char *text )
51 size_t len = 0, i = 0;
56 return ch_strdup(text);
64 if (text[len-1] != '\n') {
69 v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
72 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
74 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
85 strcpy( v2+len, "Referral:" );
86 len += sizeof("Referral:");
88 for( i=0; ref[i].bv_val != NULL; i++ ) {
89 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
92 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
94 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
99 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
100 len += ref[i].bv_len;
101 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
110 static ber_tag_t req2res( ber_tag_t tag )
115 case LDAP_REQ_COMPARE:
116 case LDAP_REQ_EXTENDED:
117 case LDAP_REQ_MODIFY:
118 case LDAP_REQ_MODRDN:
122 case LDAP_REQ_DELETE:
123 tag = LDAP_RES_DELETE;
126 case LDAP_REQ_ABANDON:
127 case LDAP_REQ_UNBIND:
131 case LDAP_REQ_SEARCH:
132 tag = LDAP_RES_SEARCH_RESULT;
142 static long send_ldap_ber(
148 ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
150 /* write only one pdu at a time - wait til it's our turn */
151 ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
153 /* lock the connection */
154 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
161 if ( connection_state_closing( conn ) ) {
162 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
163 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
168 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
175 * we got an error. if it's ewouldblock, we need to
176 * wait on the socket being writable. otherwise, figure
177 * it's a hard error and return.
181 LDAP_LOG( OPERATION, ERR,
182 "send_ldap_ber: conn %lu ber_flush failed err=%d (%s)\n",
183 conn ? conn->c_connid : 0, err, sock_errstr(err) );
185 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
186 err, sock_errstr(err), 0 );
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 );
198 /* wait for socket to be write-ready */
199 conn->c_writewaiter = 1;
200 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
201 slapd_set_write( sd, 1 );
203 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
204 conn->c_writewaiter = 0;
207 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
208 ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
214 send_ldap_control( BerElement *ber, LDAPControl *c )
220 rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
222 if( c->ldctl_iscritical ) {
223 rc = ber_printf( ber, "b",
224 (ber_int_t) c->ldctl_iscritical ) ;
225 if( rc == -1 ) return rc;
228 if( c->ldctl_value.bv_val != NULL ) {
229 rc = ber_printf( ber, "O", &c->ldctl_value );
230 if( rc == -1 ) return rc;
233 rc = ber_printf( ber, /*{*/"N}" );
234 if( rc == -1 ) return rc;
240 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
244 LDAPControl **sctrls = NULL;
247 * Retrieve any additional controls that may be set by the
251 if ( slapi_pblock_get( o->o_pb, SLAPI_RESCONTROLS, &sctrls ) != 0 ) {
255 if ( c == NULL && sctrls == NULL ) return 0;
257 if( c == NULL ) return 0;
258 #endif /* LDAP_SLAPI */
260 rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
261 if( rc == -1 ) return rc;
265 #endif /* LDAP_SLAPI */
266 for( ; *c != NULL; c++) {
267 rc = send_ldap_control( ber, *c );
268 if( rc == -1 ) return rc;
272 if ( sctrls != NULL ) {
273 for ( c = sctrls; *c != NULL; c++ ) {
274 rc = send_ldap_control( ber, *c );
275 if( rc == -1 ) return rc;
278 #endif /* LDAP_SLAPI */
280 rc = ber_printf( ber, /*{*/"N}" );
290 BerElementBuffer berbuf;
291 BerElement *ber = (BerElement *) &berbuf;
292 int rc = LDAP_SUCCESS;
295 if (op->o_callback) {
296 slap_callback *sc = op->o_callback;
297 rc = SLAP_CB_CONTINUE;
298 for ( ; op->o_callback; ) {
299 if ( op->o_callback->sc_response ) {
300 rc = op->o_callback->sc_response( op, rs );
301 if ( rc != SLAP_CB_CONTINUE ) break;
303 op->o_callback = op->o_callback->sc_next;
306 if ( rc != SLAP_CB_CONTINUE ) goto cleanup;
309 #ifdef LDAP_CONNECTIONLESS
310 if (op->o_conn && op->o_conn->c_is_udp)
315 ber_init_w_nullc( ber, LBER_USE_DER );
316 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
320 LDAP_LOG( OPERATION, ENTRY,
321 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
322 rs->sr_msgid, rs->sr_tag, rs->sr_err );
324 Debug( LDAP_DEBUG_TRACE,
325 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
326 rs->sr_msgid, rs->sr_tag, rs->sr_err );
331 LDAP_LOG( OPERATION, ARGS,
332 "send_ldap_response: conn %lu ref=\"%s\"\n",
334 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
336 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
337 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
342 #ifdef LDAP_CONNECTIONLESS
343 if (op->o_conn && op->o_conn->c_is_udp &&
344 op->o_protocol == LDAP_VERSION2 )
346 rc = ber_printf( ber, "t{ess" /*"}}"*/,
347 rs->sr_tag, rs->sr_err,
348 rs->sr_matched == NULL ? "" : rs->sr_matched,
349 rs->sr_text == NULL ? "" : rs->sr_text );
352 if ( rs->sr_type == REP_INTERMEDIATE ) {
353 rc = ber_printf( ber, "{it{" /*"}}"*/,
354 rs->sr_msgid, rs->sr_tag );
357 rc = ber_printf( ber, "{it{ess" /*"}}"*/,
358 rs->sr_msgid, rs->sr_tag, rs->sr_err,
359 rs->sr_matched == NULL ? "" : rs->sr_matched,
360 rs->sr_text == NULL ? "" : rs->sr_text );
364 if ( rs->sr_ref != NULL ) {
365 assert( rs->sr_err == LDAP_REFERRAL );
366 rc = ber_printf( ber, "t{W}",
367 LDAP_TAG_REFERRAL, rs->sr_ref );
369 assert( rs->sr_err != LDAP_REFERRAL );
373 if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
374 rc = ber_printf( ber, "tO",
375 LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
379 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
381 if ( rs->sr_rspoid != NULL ) {
382 rc = ber_printf( ber, "ts",
383 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
385 if( rc != -1 && rs->sr_rspdata != NULL ) {
386 rc = ber_printf( ber, "tO",
387 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
392 rc = ber_printf( ber, /*"{"*/ "N}" );
396 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
400 rc = ber_printf( ber, /*"{"*/ "N}" );
403 #ifdef LDAP_CONNECTIONLESS
404 if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
405 rc = ber_printf( ber, /*"{"*/ "N}" );
411 LDAP_LOG( OPERATION, ERR,
412 "send_ldap_response: conn %lu ber_printf failed\n",
413 op->o_connid, 0, 0 );
415 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
418 #ifdef LDAP_CONNECTIONLESS
419 if (!op->o_conn || op->o_conn->c_is_udp == 0)
426 bytes = send_ldap_ber( op->o_conn, ber );
427 #ifdef LDAP_CONNECTIONLESS
428 if (!op->o_conn || op->o_conn->c_is_udp == 0)
434 LDAP_LOG( OPERATION, ERR,
435 "send_ldap_response: conn %lu ber write failed\n",
436 op->o_connid ? op->o_connid : 0, 0, 0 );
438 Debug( LDAP_DEBUG_ANY,
439 "send_ldap_response: ber write failed\n",
448 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
449 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
450 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
452 #endif /* LDAP_SLAPI */
454 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
455 num_bytes_sent += bytes;
457 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
460 if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
461 free( (char *)rs->sr_matched );
462 rs->sr_matched = NULL;
465 if (op->o_callback) {
466 slap_callback *sc = op->o_callback;
467 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
468 if ( op->o_callback->sc_cleanup ) {
469 op->o_callback->sc_cleanup( op, rs );
480 send_ldap_disconnect( Operation *op, SlapReply *rs )
482 #define LDAP_UNSOLICITED_ERROR(e) \
483 ( (e) == LDAP_PROTOCOL_ERROR \
484 || (e) == LDAP_STRONG_AUTH_REQUIRED \
485 || (e) == LDAP_UNAVAILABLE )
487 assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
489 rs->sr_type = REP_EXTENDED;
492 LDAP_LOG( OPERATION, ENTRY,
493 "send_ldap_disconnect: conn %lu %d:%s\n",
494 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
496 Debug( LDAP_DEBUG_TRACE,
497 "send_ldap_disconnect %d:%s\n",
498 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
502 if ( op->o_protocol < LDAP_VERSION3 ) {
503 rs->sr_rspoid = NULL;
504 rs->sr_tag = req2res( op->o_tag );
505 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
508 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
509 rs->sr_tag = LDAP_RES_EXTENDED;
513 send_ldap_response( op, rs );
515 Statslog( LDAP_DEBUG_STATS,
516 "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
517 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
521 slap_send_ldap_result( Operation *op, SlapReply *rs )
524 const char *otext = rs->sr_text;
525 BerVarray oref = rs->sr_ref;
527 rs->sr_type = REP_RESULT;
529 assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
532 LDAP_LOG( OPERATION, ENTRY,
533 "send_ldap_result: conn %lu op=%lu p=%d\n",
534 op->o_connid, op->o_opid, op->o_protocol );
536 Debug( LDAP_DEBUG_TRACE,
537 "send_ldap_result: conn=%lu op=%lu p=%d\n",
538 op->o_connid, op->o_opid, op->o_protocol );
542 LDAP_LOG( OPERATION, ARGS,
543 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
544 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
545 rs->sr_text ? rs->sr_text : "" );
547 Debug( LDAP_DEBUG_ARGS,
548 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
549 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
550 rs->sr_text ? rs->sr_text : "" );
556 LDAP_LOG( OPERATION, ARGS,
557 "send_ldap_result: referral=\"%s\"\n",
558 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
560 Debug( LDAP_DEBUG_ARGS,
561 "send_ldap_result: referral=\"%s\"\n",
562 rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
567 assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
569 if ( rs->sr_err == LDAP_REFERRAL ) {
570 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
571 if( op->o_domain_scope ) {
575 if( rs->sr_ref == NULL ) {
576 rs->sr_err = LDAP_NO_SUCH_OBJECT;
577 } else if ( op->o_protocol < LDAP_VERSION3 ) {
578 rs->sr_err = LDAP_PARTIAL_RESULTS;
584 * Call pre-result plugins. To avoid infinite recursion plugins
585 * should just set SLAPI_RESULT_CODE rather than sending a
586 * result if they wish to change the result.
589 slapi_int_pblock_set_operation( op->o_pb, op );
590 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
591 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
592 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
594 (void) doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN, op->o_pb );
596 #endif /* LDAP_SLAPI */
598 if ( op->o_protocol < LDAP_VERSION3 ) {
599 tmp = v2ref( rs->sr_ref, rs->sr_text );
604 rs->sr_tag = req2res( op->o_tag );
605 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
607 send_ldap_response( op, rs );
609 if ( op->o_tag == LDAP_REQ_SEARCH ) {
611 snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
612 rs->sr_err, rs->sr_nentries );
614 Statslog( LDAP_DEBUG_STATS,
615 "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
616 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
617 rs->sr_text ? rs->sr_text : "" );
619 Statslog( LDAP_DEBUG_STATS,
620 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
621 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
622 rs->sr_text ? rs->sr_text : "" );
625 if( tmp != NULL ) ch_free(tmp);
631 send_ldap_sasl( Operation *op, SlapReply *rs )
633 rs->sr_type = REP_SASL;
635 LDAP_LOG( OPERATION, ENTRY,
636 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
637 op->o_connid, rs->sr_err,
638 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
640 Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
642 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
645 rs->sr_tag = req2res( op->o_tag );
646 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
648 send_ldap_response( op, rs );
652 slap_send_ldap_extended( Operation *op, SlapReply *rs )
654 rs->sr_type = REP_EXTENDED;
657 LDAP_LOG( OPERATION, ENTRY,
658 "send_ldap_extended: err=%d oid=%s len=%ld\n",
659 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
660 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
662 Debug( LDAP_DEBUG_TRACE,
663 "send_ldap_extended: err=%d oid=%s len=%ld\n",
665 rs->sr_rspoid ? rs->sr_rspoid : "",
666 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
669 rs->sr_tag = req2res( op->o_tag );
670 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
672 send_ldap_response( op, rs );
676 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
678 rs->sr_type = REP_INTERMEDIATE;
680 LDAP_LOG( OPERATION, ENTRY,
681 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
682 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
683 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
685 Debug( LDAP_DEBUG_TRACE,
686 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
688 rs->sr_rspoid ? rs->sr_rspoid : "",
689 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
691 rs->sr_tag = LDAP_RES_INTERMEDIATE;
692 rs->sr_msgid = op->o_msgid;
693 send_ldap_response( op, rs );
697 slap_send_search_entry( Operation *op, SlapReply *rs )
699 BerElementBuffer berbuf;
700 BerElement *ber = (BerElement *) &berbuf;
702 int i, j, rc=-1, bytes;
706 AccessControlState acl_state = ACL_STATE_INIT;
708 /* Support for computed attribute plugins */
709 computed_attr_context ctx;
712 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
714 /* a_flags: array of flags telling if the i-th element will be
715 * returned or filtered out
716 * e_flags: array of a_flags
718 char **e_flags = NULL;
720 rs->sr_type = REP_SEARCH;
721 if (op->o_callback) {
722 slap_callback *sc = op->o_callback;
723 rc = SLAP_CB_CONTINUE;
724 for ( ; op->o_callback; ) {
725 if ( op->o_callback->sc_response ) {
726 rc = op->o_callback->sc_response( op, rs );
727 if ( rc != SLAP_CB_CONTINUE ) break;
729 op->o_callback = op->o_callback->sc_next;
732 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
736 LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
737 op->o_connid, rs->sr_entry->e_name.bv_val,
738 op->ors_attrsonly ? " (attrsOnly)" : "" );
740 Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
741 rs->sr_entry->e_name.bv_val,
742 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
745 if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
748 "send_search_entry: conn %lu access to entry (%s) not allowed\n",
749 op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
751 Debug( LDAP_DEBUG_ACL,
752 "send_search_entry: access to entry not allowed\n",
760 edn = rs->sr_entry->e_nname.bv_val;
762 if ( op->o_res_ber ) {
763 /* read back control or LDAP_CONNECTIONLESS */
769 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
770 bv.bv_len = siz + len;
771 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
773 ber_init2( ber, &bv, LBER_USE_DER );
774 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
777 #ifdef LDAP_CONNECTIONLESS
778 if ( op->o_conn && op->o_conn->c_is_udp ) {
780 if ( op->o_protocol == LDAP_VERSION2 ) {
781 rc = ber_printf(ber, "t{O{" /*}}*/,
782 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
784 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
785 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
789 if ( op->o_res_ber ) {
790 /* read back control */
791 rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
793 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
794 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
799 LDAP_LOG( OPERATION, ERR,
800 "send_search_entry: conn %lu ber_printf failed\n",
801 op->o_connid, 0, 0 );
803 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
806 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
807 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
811 /* check for special all user attributes ("*") type */
812 userattrs = ( rs->sr_attrs == NULL ) ? 1
813 : an_find( rs->sr_attrs, &AllUser );
815 /* check for special all operational attributes ("+") type */
816 opattrs = ( rs->sr_attrs == NULL ) ? 0
817 : an_find( rs->sr_attrs, &AllOper );
819 /* create an array of arrays of flags. Each flag corresponds
820 * to particular value of attribute and equals 1 if value matches
821 * to ValuesReturnFilter or 0 if not
823 if ( op->o_vrFilter != NULL ) {
827 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
828 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
831 size = i * sizeof(char *) + k;
834 e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
835 if( e_flags == NULL ) {
837 LDAP_LOG( OPERATION, ERR,
838 "send_search_entry: conn %lu sl_calloc failed\n",
839 op->o_connid ? op->o_connid : 0, 0, 0 );
841 Debug( LDAP_DEBUG_ANY,
842 "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
846 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
849 a_flags = (char *)(e_flags + i);
850 memset( a_flags, 0, k );
851 for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
852 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
853 e_flags[i] = a_flags;
857 rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ;
860 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
861 "conn %lu matched values filtering failed\n",
862 op->o_connid ? op->o_connid : 0, 0, 0 );
864 Debug( LDAP_DEBUG_ANY,
865 "matched values filtering failed\n", 0, 0, 0 );
867 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
868 send_ldap_error( op, rs, LDAP_OTHER,
869 "matched values filtering error" );
875 for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
876 AttributeDescription *desc = a->a_desc;
878 if ( rs->sr_attrs == NULL ) {
879 /* all attrs request, skip operational attributes */
880 if( is_at_operational( desc->ad_type ) ) {
885 /* specific attrs requested */
886 if ( is_at_operational( desc->ad_type ) ) {
887 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
892 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
898 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
899 ACL_READ, &acl_state ) )
903 "send_search_entry: conn %lu access to attribute %s not "
904 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
906 Debug( LDAP_DEBUG_ACL, "acl: "
907 "access to attribute %s not allowed\n",
908 desc->ad_cname.bv_val, 0, 0 );
913 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
915 LDAP_LOG( OPERATION, ERR,
916 "send_search_entry: conn %lu ber_printf failed\n",
917 op->o_connid, 0, 0 );
919 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
922 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
923 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
927 if ( ! op->ors_attrsonly ) {
928 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
929 if ( ! access_allowed( op, rs->sr_entry,
930 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
934 "send_search_entry: conn %lu "
935 "access to attribute %s, value %d not allowed\n",
936 op->o_connid, desc->ad_cname.bv_val, i );
938 Debug( LDAP_DEBUG_ACL,
939 "acl: access to attribute %s, "
940 "value %d not allowed\n",
941 desc->ad_cname.bv_val, i, 0 );
947 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
951 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
953 LDAP_LOG( OPERATION, ERR,
954 "send_search_entry: conn %lu "
955 "ber_printf failed.\n", op->o_connid, 0, 0 );
957 Debug( LDAP_DEBUG_ANY,
958 "ber_printf failed\n", 0, 0, 0 );
961 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
962 send_ldap_error( op, rs, LDAP_OTHER,
963 "encoding values error" );
969 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
971 LDAP_LOG( OPERATION, ERR,
972 "send_search_entry: conn %lu ber_printf failed\n",
973 op->o_connid, 0, 0 );
975 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
978 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
979 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
984 /* eventually will loop through generated operational attributes */
985 /* only have subschemaSubentry and numSubordinates are implemented */
986 aa = backend_operational( op, rs, opattrs );
988 if ( aa != NULL && op->o_vrFilter != NULL ) {
992 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
993 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
996 size = i * sizeof(char *) + k;
998 char *a_flags, **tmp;
1001 * Reuse previous memory - we likely need less space
1002 * for operational attributes
1004 tmp = sl_realloc( e_flags, i * sizeof(char *) + k,
1006 if ( tmp == NULL ) {
1008 LDAP_LOG( OPERATION, ERR,
1009 "send_search_entry: conn %lu "
1010 "not enough memory "
1011 "for matched values filtering\n",
1012 op->o_connid, 0, 0);
1014 Debug( LDAP_DEBUG_ANY,
1015 "send_search_entry: conn %lu "
1016 "not enough memory "
1017 "for matched values filtering\n",
1018 op->o_connid, 0, 0 );
1020 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1021 send_ldap_error( op, rs, LDAP_OTHER,
1022 "not enough memory for matched values filtering" );
1026 a_flags = (char *)(e_flags + i);
1027 memset( a_flags, 0, k );
1028 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1029 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1030 e_flags[i] = a_flags;
1033 rc = filter_matched_values(op, aa, &e_flags) ;
1037 LDAP_LOG( OPERATION, ERR,
1038 "send_search_entry: conn %lu "
1039 "matched values filtering failed\n",
1040 op->o_connid ? op->o_connid : 0, 0, 0);
1042 Debug( LDAP_DEBUG_ANY,
1043 "matched values filtering failed\n", 0, 0, 0 );
1045 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1046 send_ldap_error( op, rs, LDAP_OTHER,
1047 "matched values filtering error" );
1053 for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
1054 AttributeDescription *desc = a->a_desc;
1056 if ( rs->sr_attrs == NULL ) {
1057 /* all attrs request, skip operational attributes */
1058 if( is_at_operational( desc->ad_type ) ) {
1063 /* specific attrs requested */
1064 if( is_at_operational( desc->ad_type ) ) {
1065 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1069 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1075 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1076 ACL_READ, &acl_state ) )
1079 LDAP_LOG( ACL, INFO,
1080 "send_search_entry: conn %lu "
1081 "access to attribute %s not allowed\n",
1082 op->o_connid, desc->ad_cname.bv_val, 0 );
1084 Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
1085 "not allowed\n", desc->ad_cname.bv_val, 0, 0 );
1091 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1094 LDAP_LOG( OPERATION, ERR,
1095 "send_search_entry: conn %lu "
1096 "ber_printf failed\n", op->o_connid, 0, 0 );
1098 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1101 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1102 send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
1107 if ( ! op->ors_attrsonly ) {
1108 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1109 if ( ! access_allowed( op, rs->sr_entry,
1110 desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1113 LDAP_LOG( ACL, INFO,
1114 "send_search_entry: conn %lu "
1115 "access to %s, value %d not allowed\n",
1116 op->o_connid, desc->ad_cname.bv_val, i );
1118 Debug( LDAP_DEBUG_ACL,
1119 "send_search_entry: access to attribute %s, "
1120 "value %d not allowed\n",
1121 desc->ad_cname.bv_val, i, 0 );
1127 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1131 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1133 LDAP_LOG( OPERATION, ERR,
1134 "send_search_entry: conn %lu ber_printf failed\n",
1135 op->o_connid, 0, 0 );
1137 Debug( LDAP_DEBUG_ANY,
1138 "ber_printf failed\n", 0, 0, 0 );
1141 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1142 send_ldap_error( op, rs, LDAP_OTHER,
1143 "encoding values error" );
1150 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1152 LDAP_LOG( OPERATION, ERR,
1153 "send_search_entry: conn %lu ber_printf failed\n",
1154 op->o_connid, 0, 0 );
1156 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1159 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1160 send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1168 * First, setup the computed attribute context that is
1169 * passed to all plugins.
1172 ctx.cac_pb = op->o_pb;
1173 ctx.cac_attrs = rs->sr_attrs;
1174 ctx.cac_attrsonly = op->ors_attrsonly;
1175 ctx.cac_userattrs = userattrs;
1176 ctx.cac_opattrs = opattrs;
1177 ctx.cac_acl_state = acl_state;
1178 ctx.cac_private = (void *)ber;
1181 * For each client requested attribute, call the plugins.
1183 if ( rs->sr_attrs != NULL ) {
1184 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1185 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1186 rs->sr_entry, slapi_int_compute_output_ber );
1193 * Technically we shouldn't be returning operational attributes
1194 * when the user requested only user attributes. We'll let the
1195 * plugin decide whether to be naughty or not.
1197 rc = compute_evaluator( &ctx, "*",
1198 rs->sr_entry, slapi_int_compute_output_ber );
1201 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1202 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1206 #endif /* LDAP_SLAPI */
1210 sl_free( e_flags, op->o_tmpmemctx );
1215 rc = ber_printf( ber, /*{{*/ "}N}" );
1218 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1222 #ifdef LDAP_CONNECTIONLESS
1223 if( op->o_conn && op->o_conn->c_is_udp ) {
1224 if ( op->o_protocol != LDAP_VERSION2 ) {
1225 rc = ber_printf( ber, /*{*/ "N}" );
1229 if ( op->o_res_ber == NULL ) {
1230 rc = ber_printf( ber, /*{*/ "N}" );
1236 LDAP_LOG( OPERATION, ERR,
1237 "send_search_entry: conn %lu ber_printf failed\n",
1238 op->o_connid, 0, 0 );
1240 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1243 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1244 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1249 if ( op->o_res_ber == NULL ) {
1250 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1251 ber_free_buf( ber );
1255 LDAP_LOG( OPERATION, ERR,
1256 "send_search_entry: conn %lu ber write failed.\n",
1257 op->o_connid, 0, 0 );
1259 Debug( LDAP_DEBUG_ANY,
1260 "send_search_entry: ber write failed\n",
1269 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1270 num_bytes_sent += bytes;
1273 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1276 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1277 op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1280 LDAP_LOG( OPERATION, ENTRY,
1281 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1283 Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1289 /* FIXME: I think rs->sr_type should be explicitly set to
1290 * REP_SEARCH here. That's what it was when we entered this
1291 * function. send_ldap_error may have changed it, but we
1292 * should set it back so that the cleanup functions know
1293 * what they're doing.
1295 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH
1297 && (rs->sr_flags & REP_ENTRY_MUSTBEFREED) )
1299 entry_free( rs->sr_entry );
1300 rs->sr_entry = NULL;
1301 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1304 if ( e_flags ) sl_free( e_flags, op->o_tmpmemctx );
1306 if (op->o_callback) {
1307 slap_callback *sc = op->o_callback;
1308 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
1309 if ( op->o_callback->sc_cleanup ) {
1310 op->o_callback->sc_cleanup( op, rs );
1313 op->o_callback = sc;
1319 slap_send_search_reference( Operation *op, SlapReply *rs )
1321 BerElementBuffer berbuf;
1322 BerElement *ber = (BerElement *) &berbuf;
1326 AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1327 AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1329 rs->sr_type = REP_SEARCHREF;
1330 if (op->o_callback) {
1331 slap_callback *sc = op->o_callback;
1332 rc = SLAP_CB_CONTINUE;
1333 for ( ; op->o_callback; ) {
1334 if ( op->o_callback->sc_response ) {
1335 rc = op->o_callback->sc_response( op, rs );
1336 if ( rc != SLAP_CB_CONTINUE ) break;
1338 op->o_callback = op->o_callback->sc_next;
1340 op->o_callback = sc;
1341 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1345 LDAP_LOG( OPERATION, ENTRY,
1346 "send_search_reference: conn %lu dn=\"%s\"\n",
1348 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1350 Debug( LDAP_DEBUG_TRACE,
1351 "=> send_search_reference: dn=\"%s\"\n",
1352 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1355 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1356 ad_entry, NULL, ACL_READ, NULL ) )
1359 LDAP_LOG( ACL, INFO,
1360 "send_search_reference: conn %lu "
1361 "access to entry %s not allowed\n",
1362 op->o_connid, rs->sr_entry->e_dn, 0 );
1364 Debug( LDAP_DEBUG_ACL,
1365 "send_search_reference: access to entry not allowed\n",
1372 if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1373 ad_ref, NULL, ACL_READ, NULL ) )
1376 LDAP_LOG( ACL, INFO,
1377 "send_search_reference: conn %lu access "
1378 "to reference not allowed.\n", op->o_connid, 0, 0 );
1380 Debug( LDAP_DEBUG_ACL,
1381 "send_search_reference: access "
1382 "to reference not allowed\n",
1389 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1390 if( op->o_domain_scope ) {
1392 LDAP_LOG( OPERATION, ERR,
1393 "send_search_reference: conn %lu domainScope control in (%s).\n",
1394 op->o_connid, rs->sr_entry->e_dn, 0 );
1396 Debug( LDAP_DEBUG_ANY,
1397 "send_search_reference: domainScope control in (%s)\n",
1398 rs->sr_entry->e_dn, 0, 0 );
1405 if( rs->sr_ref == NULL ) {
1407 LDAP_LOG( OPERATION, ERR,
1408 "send_search_reference: conn %lu null ref in (%s).\n",
1409 op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1411 Debug( LDAP_DEBUG_ANY,
1412 "send_search_reference: null ref in (%s)\n",
1413 rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1419 if( op->o_protocol < LDAP_VERSION3 ) {
1421 /* save the references for the result */
1422 if( rs->sr_ref[0].bv_val != NULL ) {
1423 if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1429 #ifdef LDAP_CONNECTIONLESS
1430 if( op->o_conn && op->o_conn->c_is_udp ) {
1431 ber = op->o_res_ber;
1435 ber_init_w_nullc( ber, LBER_USE_DER );
1436 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1439 rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1440 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1443 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1447 rc = ber_printf( ber, /*"{"*/ "N}" );
1452 LDAP_LOG( OPERATION, ERR,
1453 "send_search_reference: conn %lu "
1454 "ber_printf failed.\n", op->o_connid, 0, 0 );
1456 Debug( LDAP_DEBUG_ANY,
1457 "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1460 #ifdef LDAP_CONNECTIONLESS
1461 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1463 ber_free_buf( ber );
1464 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1468 #ifdef LDAP_CONNECTIONLESS
1469 if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1471 bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1472 ber_free_buf( ber );
1474 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1475 num_bytes_sent += bytes;
1478 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1479 #ifdef LDAP_CONNECTIONLESS
1483 Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1484 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1487 LDAP_LOG( OPERATION, ENTRY,
1488 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1490 Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1494 if (op->o_callback) {
1495 slap_callback *sc = op->o_callback;
1496 for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
1497 if ( op->o_callback->sc_cleanup ) {
1498 op->o_callback->sc_cleanup( op, rs );
1501 op->o_callback = sc;
1517 *code = LDAP_SUCCESS;
1521 if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1523 LDAP_LOG( OPERATION, INFO,
1524 "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1526 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1534 while ( (s = strchr( s, '\n' )) != NULL ) {
1539 if ( (c = strchr( s, ':' )) != NULL ) {
1543 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1547 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1551 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1557 LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1559 Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1570 int slap_read_controls(
1574 const struct berval *oid,
1575 LDAPControl **ctrl )
1579 BerElementBuffer berbuf;
1580 BerElement *ber = (BerElement *) &berbuf;
1586 LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1587 oid->bv_val, e->e_dn, 0 );
1589 Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1590 oid->bv_val, e->e_dn, 0 );
1594 rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1595 op->o_preread_attrs : op->o_postread_attrs;
1597 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1598 bv.bv_len = siz + len;
1599 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1601 ber_init2( ber, &bv, LBER_USE_DER );
1602 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1604 /* create new operation */
1607 myop.o_res_ber = ber;
1609 rc = slap_send_search_entry( &myop, rs );
1612 rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1614 if( rc == LBER_ERROR ) return LDAP_OTHER;
1616 c.ldctl_oid = oid->bv_val;
1617 c.ldctl_iscritical = 0;
1619 *ctrl = sl_calloc( 1, sizeof(LDAPControl), NULL );
1621 return LDAP_SUCCESS;