]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/result.c
Fix prev commit
[openldap] / servers / slapd / result.c
index b134b9918d709db1af89006e2faf4e05fb26cf50..2f97e13ab40cbafeef42b0daefbef6f165ae9732 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2003 The OpenLDAP Foundation.
+ * Copyright 1998-2004 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -38,7 +38,7 @@
 #include "slap.h"
 
 #ifdef LDAP_SLAPI
-#include "slapi.h"
+#include "slapi/slapi.h"
 #endif
 
 int slap_null_cb( Operation *op, SlapReply *rs )
@@ -46,6 +46,14 @@ int slap_null_cb( Operation *op, SlapReply *rs )
        return 0;
 }
 
+int slap_replog_cb( Operation *op, SlapReply *rs )
+{
+       if ( rs->sr_err == LDAP_SUCCESS ) {
+               replog( op );
+       }
+       return SLAP_CB_CONTINUE;
+}
+
 static char *v2ref( BerVarray ref, const char *text )
 {
        size_t len = 0, i = 0;
@@ -211,49 +219,90 @@ static long send_ldap_ber(
 }
 
 static int
-send_ldap_controls( BerElement *ber, LDAPControl **c )
+send_ldap_control( BerElement *ber, LDAPControl *c )
+{
+       int rc;
+
+       assert( c != NULL );
+
+       rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
+
+       if( c->ldctl_iscritical ) {
+               rc = ber_printf( ber, "b",
+                       (ber_int_t) c->ldctl_iscritical ) ;
+               if( rc == -1 ) return rc;
+       }
+
+       if( c->ldctl_value.bv_val != NULL ) {
+               rc = ber_printf( ber, "O", &c->ldctl_value ); 
+               if( rc == -1 ) return rc;
+       }
+
+       rc = ber_printf( ber, /*{*/"N}" );
+       if( rc == -1 ) return rc;
+
+       return 0;
+}
+
+static int
+send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
 {
        int rc;
+#ifdef LDAP_SLAPI
+       LDAPControl **sctrls = NULL;
+
+       /*
+        * Retrieve any additional controls that may be set by the
+        * plugin.
+        */
+
+       if ( o->o_pb && slapi_pblock_get( o->o_pb, SLAPI_RESCONTROLS, &sctrls ) != 0 ) {
+               sctrls = NULL;
+       }
+
+       if ( c == NULL && sctrls == NULL ) return 0;
+#else
        if( c == NULL ) return 0;
+#endif /* LDAP_SLAPI */
 
        rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
        if( rc == -1 ) return rc;
 
+#ifdef LDAP_SLAPI
+       if ( c != NULL )
+#endif /* LDAP_SLAPI */
        for( ; *c != NULL; c++) {
-               rc = ber_printf( ber, "{s" /*}*/, (*c)->ldctl_oid );
-
-               if( (*c)->ldctl_iscritical ) {
-                       rc = ber_printf( ber, "b",
-                               (ber_int_t) (*c)->ldctl_iscritical ) ;
-                       if( rc == -1 ) return rc;
-               }
+               rc = send_ldap_control( ber, *c );
+               if( rc == -1 ) return rc;
+       }
 
-               if( (*c)->ldctl_value.bv_val != NULL ) {
-                       rc = ber_printf( ber, "O", &((*c)->ldctl_value)); 
+#ifdef LDAP_SLAPI
+       if ( sctrls != NULL ) {
+               for ( c = sctrls; *c != NULL; c++ ) {
+                       rc = send_ldap_control( ber, *c );
                        if( rc == -1 ) return rc;
                }
-
-               rc = ber_printf( ber, /*{*/"N}" );
-               if( rc == -1 ) return rc;
        }
+#endif /* LDAP_SLAPI */
 
        rc = ber_printf( ber, /*{*/"N}" );
 
        return rc;
 }
 
-void
+static int
 send_ldap_response(
        Operation *op,
        SlapReply *rs )
 {
        BerElementBuffer berbuf;
        BerElement      *ber = (BerElement *) &berbuf;
-       int             rc;
+       int             rc = LDAP_SUCCESS;
        long    bytes;
 
        if (op->o_callback) {
                slap_callback *sc = op->o_callback;
+               rc = SLAP_CB_CONTINUE;
                for ( ; op->o_callback; ) {
                        if ( op->o_callback->sc_response ) {
                                rc = op->o_callback->sc_response( op, rs );
@@ -262,9 +311,9 @@ send_ldap_response(
                        op->o_callback = op->o_callback->sc_next;
                }
                op->o_callback = sc;
-               if ( rc != SLAP_CB_CONTINUE ) goto cleanup;
+               if ( rc != SLAP_CB_CONTINUE ) goto clean2;
        }
-               
+
 #ifdef LDAP_CONNECTIONLESS
        if (op->o_conn && op->o_conn->c_is_udp)
                ber = op->o_res_ber;
@@ -351,8 +400,8 @@ send_ldap_response(
                rc = ber_printf( ber, /*"{"*/ "N}" );
        }
 
-       if( rc != -1 && rs->sr_ctrls != NULL ) {
-               rc = send_ldap_controls( ber, rs->sr_ctrls );
+       if( rc != -1 ) {
+               rc = send_ldap_controls( op, ber, rs->sr_ctrls );
        }
 
        if( rc != -1 ) {
@@ -415,12 +464,18 @@ send_ldap_response(
        num_pdu_sent++;
        ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
 
-cleanup:;
+cleanup:
+       /* Tell caller that we did this for real, as opposed to being
+        * overridden by a callback
+        */
+       rc = SLAP_CB_CONTINUE;
+
        if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
                free( (char *)rs->sr_matched );
                rs->sr_matched = NULL;
        }
 
+clean2:
        if (op->o_callback) {
                slap_callback *sc = op->o_callback;
                for ( ; op->o_callback; op->o_callback = op->o_callback->sc_next ) {
@@ -431,7 +486,7 @@ cleanup:;
                op->o_callback = sc;
        }
 
-       return;
+       return rc;
 }
 
 
@@ -469,11 +524,11 @@ send_ldap_disconnect( Operation   *op, SlapReply *rs )
                rs->sr_msgid = 0;
        }
 
-       send_ldap_response( op, rs );
-
-       Statslog( LDAP_DEBUG_STATS,
+       if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
+               Statslog( LDAP_DEBUG_STATS,
            "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
                op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
+       }
 }
 
 void
@@ -485,7 +540,7 @@ slap_send_ldap_result( Operation *op, SlapReply *rs )
 
        rs->sr_type = REP_RESULT;
 
-       assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
+       assert( !LDAP_API_ERROR( rs->sr_err ));
 
 #ifdef NEW_LOGGING
        LDAP_LOG( OPERATION, ENTRY, 
@@ -544,13 +599,13 @@ slap_send_ldap_result( Operation *op, SlapReply *rs )
         * should just set SLAPI_RESULT_CODE rather than sending a
         * result if they wish to change the result.
         */
-       if ( op->o_pb ) {
-               slapi_x_pblock_set_operation( op->o_pb, op );
+       if ( op->o_pb != NULL ) {
+               slapi_int_pblock_set_operation( op->o_pb, op );
                slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
                slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
                slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, (void *)rs->sr_matched );
 
-               (void) doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN, op->o_pb );
+               (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN, op->o_pb );
        }
 #endif /* LDAP_SLAPI */
 
@@ -563,22 +618,22 @@ slap_send_ldap_result( Operation *op, SlapReply *rs )
        rs->sr_tag = req2res( op->o_tag );
        rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
 
-       send_ldap_response( op, rs );
-
-       if ( op->o_tag == LDAP_REQ_SEARCH ) {
-               char nbuf[64];
-               snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
-                       rs->sr_err, rs->sr_nentries );
+       if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
+               if ( op->o_tag == LDAP_REQ_SEARCH ) {
+                       char nbuf[64];
+                       snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
+                               rs->sr_err, rs->sr_nentries );
 
-               Statslog( LDAP_DEBUG_STATS,
-               "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
-                       op->o_connid, op->o_opid, rs->sr_tag, nbuf,
-                       rs->sr_text ? rs->sr_text : "" );
-       } else {
-               Statslog( LDAP_DEBUG_STATS,
-                       "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
-                       op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
-                       rs->sr_text ? rs->sr_text : "" );
+                       Statslog( LDAP_DEBUG_STATS,
+                       "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
+                               op->o_connid, op->o_opid, rs->sr_tag, nbuf,
+                               rs->sr_text ? rs->sr_text : "" );
+               } else {
+                       Statslog( LDAP_DEBUG_STATS,
+                               "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
+                               op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
+                               rs->sr_text ? rs->sr_text : "" );
+               }
        }
 
        if( tmp != NULL ) ch_free(tmp);
@@ -679,6 +734,7 @@ slap_send_search_entry( Operation *op, SlapReply *rs )
        rs->sr_type = REP_SEARCH;
        if (op->o_callback) {
                slap_callback *sc = op->o_callback;
+               rc = SLAP_CB_CONTINUE;
                for ( ; op->o_callback; ) {
                        if ( op->o_callback->sc_response ) {
                                rc = op->o_callback->sc_response( op, rs );
@@ -832,6 +888,7 @@ slap_send_search_entry( Operation *op, SlapReply *rs )
 
        for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
                AttributeDescription *desc = a->a_desc;
+               int finish = 0;
 
                if ( rs->sr_attrs == NULL ) {
                        /* all attrs request, skip operational attributes */
@@ -853,39 +910,42 @@ slap_send_search_entry( Operation *op, SlapReply *rs )
                        }
                }
 
-               if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
-                       ACL_READ, &acl_state ) )
-               {
+               if ( op->ors_attrsonly ) {
+                       if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
+                               ACL_READ, &acl_state ) )
+                       {
 #ifdef NEW_LOGGING
-                       LDAP_LOG( ACL, INFO, 
-                               "send_search_entry: conn %lu  access to attribute %s not "
-                               "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
+                               LDAP_LOG( ACL, INFO, 
+                                       "send_search_entry: conn %lu  access to attribute %s not "
+                                       "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
 #else
-                       Debug( LDAP_DEBUG_ACL, "acl: "
-                               "access to attribute %s not allowed\n",
-                           desc->ad_cname.bv_val, 0, 0 );
+                               Debug( LDAP_DEBUG_ACL, "acl: "
+                                       "access to attribute %s not allowed\n",
+                                   desc->ad_cname.bv_val, 0, 0 );
 #endif
-                       continue;
-               }
+                               continue;
+                       }
 
-               if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
+                       if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
 #ifdef NEW_LOGGING
-                       LDAP_LOG( OPERATION, ERR, 
-                               "send_search_entry: conn %lu  ber_printf failed\n", 
-                               op->o_connid, 0, 0 );
+                               LDAP_LOG( OPERATION, ERR, 
+                                       "send_search_entry: conn %lu  ber_printf failed\n", 
+                                       op->o_connid, 0, 0 );
 #else
-                       Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
+                               Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
 #endif
 
-                       if ( op->o_res_ber == NULL ) ber_free_buf( ber );
-                       send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
-                       goto error_return;
-               }
+                               if ( op->o_res_ber == NULL ) ber_free_buf( ber );
+                               send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
+                               goto error_return;
+                       }
+                       finish = 1;
 
-               if ( ! op->ors_attrsonly ) {
-                       for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
+               } else {
+                       int first = 1;
+                       for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
                                if ( ! access_allowed( op, rs->sr_entry,
-                                       desc, &a->a_vals[i], ACL_READ, &acl_state ) )
+                                       desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
                                {
 #ifdef NEW_LOGGING
                                        LDAP_LOG( ACL, INFO, 
@@ -906,6 +966,23 @@ slap_send_search_entry( Operation *op, SlapReply *rs )
                                        continue;
                                }
 
+                               if ( first ) {
+                                       first = 0;
+                                       finish = 1;
+                                       if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
+#ifdef NEW_LOGGING
+                                               LDAP_LOG( OPERATION, ERR, 
+                                                       "send_search_entry: conn %lu  ber_printf failed\n", 
+                                                       op->o_connid, 0, 0 );
+#else
+                                               Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
+#endif
+
+                                               if ( op->o_res_ber == NULL ) ber_free_buf( ber );
+                                               send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
+                                               goto error_return;
+                                       }
+                               }
                                if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
 #ifdef NEW_LOGGING
                                        LDAP_LOG( OPERATION, ERR, 
@@ -924,7 +1001,7 @@ slap_send_search_entry( Operation *op, SlapReply *rs )
                        }
                }
 
-               if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
+               if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
 #ifdef NEW_LOGGING
                        LDAP_LOG( OPERATION, ERR, 
                                "send_search_entry: conn %lu ber_printf failed\n", 
@@ -1141,7 +1218,7 @@ slap_send_search_entry( Operation *op, SlapReply *rs )
                if ( rs->sr_attrs != NULL ) {
                        for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
                                rc = compute_evaluator( &ctx, anp->an_name.bv_val,
-                                       rs->sr_entry, slapi_x_compute_output_ber );
+                                       rs->sr_entry, slapi_int_compute_output_ber );
                                if ( rc == 1 ) {
                                        break;
                                }
@@ -1153,7 +1230,7 @@ slap_send_search_entry( Operation *op, SlapReply *rs )
                         * plugin decide whether to be naughty or not.
                         */
                        rc = compute_evaluator( &ctx, "*",
-                               rs->sr_entry, slapi_x_compute_output_ber );
+                               rs->sr_entry, slapi_int_compute_output_ber );
                }
                if ( rc == 1 ) {
                        if ( op->o_res_ber == NULL ) ber_free_buf( ber );
@@ -1172,8 +1249,8 @@ slap_send_search_entry( Operation *op, SlapReply *rs )
        attrs_free( aa );
        rc = ber_printf( ber, /*{{*/ "}N}" );
 
-       if( rc != -1 && rs->sr_ctrls != NULL ) {
-               rc = send_ldap_controls( ber, rs->sr_ctrls );
+       if( rc != -1 ) {
+               rc = send_ldap_controls( op, ber, rs->sr_ctrls );
        }
 
        if( rc != -1 ) {
@@ -1205,7 +1282,7 @@ slap_send_search_entry( Operation *op, SlapReply *rs )
        }
 
        if ( op->o_res_ber == NULL ) {
-               bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
+               bytes = send_ldap_ber( op->o_conn, ber );
                ber_free_buf( ber );
 
                if ( bytes < 0 ) {
@@ -1287,6 +1364,7 @@ slap_send_search_reference( Operation *op, SlapReply *rs )
        rs->sr_type = REP_SEARCHREF;
        if (op->o_callback) {
                slap_callback *sc = op->o_callback;
+               rc = SLAP_CB_CONTINUE;
                for ( ; op->o_callback; ) {
                        if ( op->o_callback->sc_response ) {
                                rc = op->o_callback->sc_response( op, rs );
@@ -1396,8 +1474,8 @@ slap_send_search_reference( Operation *op, SlapReply *rs )
        rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
                LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
 
-       if( rc != -1 && rs->sr_ctrls != NULL ) {
-               rc = send_ldap_controls( ber, rs->sr_ctrls );
+       if( rc != -1 ) {
+               rc = send_ldap_controls( op, ber, rs->sr_ctrls );
        }
 
        if( rc != -1 ) {
@@ -1425,7 +1503,7 @@ slap_send_search_reference( Operation *op, SlapReply *rs )
 #ifdef LDAP_CONNECTIONLESS
        if (!op->o_conn || op->o_conn->c_is_udp == 0) {
 #endif
-       bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
+       bytes = send_ldap_ber( op->o_conn, ber );
        ber_free_buf( ber );
 
        ldap_pvt_thread_mutex_lock( &num_sent_mutex );