send_ldap_intermediate(Slapi_PBlock *pb, LDAPControl **ectrls, char *responseName, struct berval *responseValue)
{
    ber_tag_t tag;
    BerElement *ber;
    Slapi_Connection *connection;
    Slapi_Operation *operation;
    int rc = 0;
    int logit = 0;

    slapi_log_err(SLAPI_LOG_TRACE, "send_ldap_intermediate", "=>\n");
    slapi_pblock_get(pb, SLAPI_OPERATION, &operation);
    slapi_pblock_get(pb, SLAPI_CONNECTION, &connection);

    if (operation->o_status == SLAPI_OP_STATUS_RESULT_SENT) {
        return (rc); /* result already sent */
    }
    tag = LDAP_RES_INTERMEDIATE;
    if ((ber = der_alloc()) == NULL) {
        slapi_log_err(SLAPI_LOG_ERR, "send_ldap_intermediate", "ber_alloc failed\n");
        goto log_and_return;
    }
    /* add the intermediate message */
    rc = ber_printf(ber, "{it{", operation->o_msgid, tag);
    /* print responsename */
    rc = ber_printf(ber, "ts", LDAP_TAG_IM_RES_OID, responseName);
    /* print responsevalue */
    rc = ber_printf(ber, "tO", LDAP_TAG_IM_RES_VALUE, responseValue);


    if (rc != LBER_ERROR) {
        rc = ber_printf(ber, "}"); /* one more } to come */
    }

    if (ectrls != NULL && connection->c_ldapversion >= LDAP_VERSION3 && write_controls(ber, ectrls) != 0) {
        rc = (int)LBER_ERROR;
    }

    if (rc != LBER_ERROR) { /* end the LDAPMessage sequence */
        rc = ber_put_seq(ber);
    }
    if (rc == LBER_ERROR) {
        slapi_log_err(SLAPI_LOG_ERR, "send_ldap_intermediate", "ber_printf failed 0\n");
        ber_free(ber, 1 /* freebuf */);
        goto log_and_return;
    }

    /* write only one pdu at a time - wait til it's our turn */
    if (flush_ber(pb, connection, operation, ber, _LDAP_SEND_INTERMED) == 0) {
        logit = 1;
    }
log_and_return:
    /* operation->o_status = SLAPI_OP_STATUS_RESULT_SENT;
     * there could be multiple intermediate messages on
     * the same connection, unlike in send_result do not
     * set o_status
     */

    if (rc == LBER_ERROR) {
        rc = LDAP_OPERATIONS_ERROR;
    } else {
        rc = LDAP_SUCCESS;
    }

    if (logit && operation_is_flag_set(operation,
                                       OP_FLAG_ACTION_LOG_ACCESS)) {
        log_result(pb, operation, rc, tag, 0);
    }

    slapi_log_err(SLAPI_LOG_TRACE, "send_ldap_intermediate", "<= %d\n", rc);
    return rc;
}
send_ldap_intermediate(Slapi_PBlock *pb, LDAPControl **ectrls, char *responseName, struct berval *responseValue)
{
    ber_tag_t tag;
    BerElement *ber;
    Slapi_Connection *connection;
    Slapi_Operation *operation;
    int rc = 0;
    int logit = 0;

    slapi_log_err(SLAPI_LOG_TRACE, "send_ldap_intermediate", "=>\n");
    slapi_pblock_get(pb, SLAPI_OPERATION, &operation);
    slapi_pblock_get(pb, SLAPI_CONNECTION, &connection);

    if (operation->o_status == SLAPI_OP_STATUS_RESULT_SENT) {
        return (rc);  
    }
    tag = LDAP_RES_INTERMEDIATE;
    if ((ber = der_alloc()) == NULL) {
        slapi_log_err(SLAPI_LOG_ERR, "send_ldap_intermediate", "ber_alloc failed\n");
        goto log_and_return;
    }
     
    rc = ber_printf(ber, "{it{", operation->o_msgid, tag);
     
    rc = ber_printf(ber, "ts", LDAP_TAG_IM_RES_OID, responseName);
     
    rc = ber_printf(ber, "tO", LDAP_TAG_IM_RES_VALUE, responseValue);


    if (rc != LBER_ERROR) {
        rc = ber_printf(ber, "}");  
    }

    if (ectrls != NULL && connection->c_ldapversion >= LDAP_VERSION3 && write_controls(ber, ectrls) != 0) {
        rc = (int)LBER_ERROR;
    }

    if (rc != LBER_ERROR) {  
        rc = ber_put_seq(ber);
    }
    if (rc == LBER_ERROR) {
        slapi_log_err(SLAPI_LOG_ERR, "send_ldap_intermediate", "ber_printf failed 0\n");
        ber_free(ber, 1  );
        goto log_and_return;
    }

     
    if (flush_ber(pb, connection, operation, ber, _LDAP_SEND_INTERMED) == 0) {
        logit = 1;
    }
log_and_return:
     

    if (rc == LBER_ERROR) {
        rc = LDAP_OPERATIONS_ERROR;
    } else {
        rc = LDAP_SUCCESS;
    }

    if (logit && operation_is_flag_set(operation,
                                       OP_FLAG_ACTION_LOG_ACCESS)) {
        log_result(pb, operation, rc, tag, 0);
    }

    slapi_log_err(SLAPI_LOG_TRACE, "send_ldap_intermediate", "<= %d\n", rc);
    return rc;
}