Skip to content

Commit

Permalink
Merge branch 'v3.0' into v3.0_compression
Browse files Browse the repository at this point in the history
  • Loading branch information
yashwantsahu20 committed Jan 16, 2025
2 parents 2d58074 + 261b529 commit ed30c5f
Show file tree
Hide file tree
Showing 20 changed files with 696 additions and 323 deletions.
47 changes: 47 additions & 0 deletions common_mk/openssl_flags.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
CUSTOM_OPENSSL_PATH ?=

OPENSSL_PACKAGE := openssl

ifeq ($(DISTRO),almalinux)
ifeq ($(CENTOSVER),8)
OPENSSL_PACKAGE := openssl3
endif
endif

$(info OPENSSL_PACKAGE: $(OPENSSL_PACKAGE))

# Use pkg-config to get the compiler and linker flags for OpenSSL if CUSTOM_OPENSSL_PATH is not set
ifeq ($(CUSTOM_OPENSSL_PATH),)
$(info No custom path specified.)
ifeq ($(OPENSSL_PACKAGE),openssl3)
SSL_IDIR := $(shell pkg-config --cflags $(OPENSSL_PACKAGE) | grep -oP "(?<=-I)[^ ]+")
SSL_LDIR := $(shell pkg-config --variable=libdir $(OPENSSL_PACKAGE))
LIB_SSL_PATH := $(shell find $(SSL_LDIR) -name "libssl.so.3" 2>/dev/null | head -n 1)
LIB_CRYPTO_PATH := $(shell find $(SSL_LDIR) -name "libcrypto.so.3" 2>/dev/null | head -n 1)
else
SSL_IDIR := $(shell export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1; export PKG_CONFIG_ALLOW_SYSTEM_LIBS=1; pkg-config --cflags $(OPENSSL_PACKAGE) | grep -oP "(?<=-I)[^ ]+")
SSL_LDIR := $(shell pkg-config --variable=libdir $(OPENSSL_PACKAGE))
LIB_SSL_PATH := $(shell find $(SSL_LDIR) -name "libssl.so" 2>/dev/null | head -n 1)
LIB_CRYPTO_PATH := $(shell find $(SSL_LDIR) -name "libcrypto.so" 2>/dev/null | head -n 1)
endif
else
SSL_IDIR := $(CUSTOM_OPENSSL_PATH)/include
SSL_LDIR := $(CUSTOM_OPENSSL_PATH)/lib64
LIB_SSL_PATH := $(shell find $(SSL_LDIR) -name "libssl.so" 2>/dev/null | head -n 1)
LIB_CRYPTO_PATH := $(shell find $(SSL_LDIR) -name "libcrypto.so" 2>/dev/null | head -n 1)
$(info Using custom OpenSSL path: $(CUSTOM_OPENSSL_PATH))
endif

# Check if required flags are set and provide feedback
ifneq ($(SSL_IDIR),)
ifneq ($(SSL_LDIR),)
$(info SSL_IDIR: $(SSL_IDIR))
$(info SSL_LDIR: $(SSL_LDIR))
$(info LIB_SSL_PATH: $(LIB_SSL_PATH))
$(info LIB_CRYPTO_PATH: $(LIB_CRYPTO_PATH))
else
$(error Warning: OpenSSL libraries directory (SSL_LDIR) not found. Exiting. Please ensure the correct path is set or install OpenSSL version 3.)
endif
else
$(error Warning: OpenSSL headers (SSL_IDIR) not found. Exiting. Please install OpenSSL version 3.)
endif
44 changes: 44 additions & 0 deletions common_mk/openssl_version_check.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
REQUIRED_OPENSSL_VERSION := 3.0.0

$(info OPENSSL_PACKAGE: $(OPENSSL_PACKAGE))

check_openssl_version:
@echo "Checking OpenSSL version..."
@if [ -n "$(CUSTOM_OPENSSL_PATH)" ]; then \
echo "Using custom OpenSSL path: $(CUSTOM_OPENSSL_PATH)"; \
header_path="$(CUSTOM_OPENSSL_PATH)/include/openssl/opensslv.h"; \
if [ ! -f "$$header_path" ]; then \
echo "OpenSSL header file not found at $$header_path"; \
exit 1; \
fi; \
version_number=$$(grep -oP '# define OPENSSL_VERSION_STR "\K[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?' $$header_path | tr -d '[:space:]'); \
if [ -z "$$version_number" ]; then \
echo "Failed to extract OPENSSL_VERSION_STR from $$header_path"; \
exit 1; \
fi; \
major=$$(echo $$version_number | cut -d'.' -f1); \
minor=$$(echo $$version_number | cut -d'.' -f2); \
patch=$$(echo $$version_number | cut -d'.' -f3); \
echo "Detected OpenSSL version from header: $$major.$$minor.$$patch"; \
required_major=3; \
required_minor=0; \
required_patch=0; \
if [ $$major -gt $$required_major ] || { [ $$major -eq $$required_major ] && { [ $$minor -gt $$required_minor ] || { [ $$minor -eq $$required_minor ] && [ $$patch -ge $$required_patch ]; }; }; }; then \
echo "OpenSSL version is valid."; \
else \
echo "OpenSSL version must be >= $(REQUIRED_OPENSSL_VERSION). Detected: $$major.$$minor.$$patch"; \
exit 1; \
fi; \
else \
echo "Using pkg-config to detect OpenSSL"; \
openssl_version=$$(pkg-config --modversion $(OPENSSL_PACKAGE) 2>/dev/null); \
if [ -z "$$openssl_version" ]; then \
echo "OpenSSL not found via pkg-config."; \
exit 1; \
fi; \
echo "Detected OpenSSL version from pkg-config: $$openssl_version"; \
if [ "$$(printf '%s\n' "$(REQUIRED_OPENSSL_VERSION)" "$$openssl_version" | sort -V | head -n1)" != "$(REQUIRED_OPENSSL_VERSION)" ]; then \
echo "OpenSSL version must be >= $(REQUIRED_OPENSSL_VERSION). Detected: $$openssl_version"; \
exit 1; \
fi; \
fi
37 changes: 2 additions & 35 deletions deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,41 +76,8 @@ endif

libinjection: libinjection/libinjection/src/libinjection.a

ssl_header_path := $(shell find /usr /usr/local /opt -name "ssl.h" -path "*/openssl/*" 2>/dev/null | head -n 1)
LIB_SSL_PATH := $(shell find /usr /usr/local /opt -name "libssl.so" 2>/dev/null | head -n 1)
LIB_CRYPTO_PATH := $(shell find /usr /usr/local /opt -name "libcrypto.so" 2>/dev/null | head -n 1)
ifeq ($(DISTRO),almalinux)
ifeq ($(CENTOSVER),8)
ssl_header_path := $(shell find /usr /usr/local /opt -name "ssl.h" -path "*/openssl3/*" 2>/dev/null | head -n 1)
LIB_SSL_PATH := $(shell find /usr /usr/local /opt -name "libssl.so.3" 2>/dev/null | head -n 1)
LIB_CRYPTO_PATH := $(shell find /usr /usr/local /opt -name "libcrypto.so.3" 2>/dev/null | head -n 1)
endif
else
endif
SSL_LDIR := $(dir $(LIB_SSL_PATH))

ifneq ($(ssl_header_path),)
SSL_IDIR := $(shell dirname $(shell dirname $(ssl_header_path)))
$(info Found OpenSSL headers at $(SSL_IDIR))
$(info OpenSSL lib full path is $(LIB_SSL_PATH))
$(info OpenSSL libs directory is $(SSL_LDIR))
else
$(error Warning: OpenSSL headers not found. exiting, please install openssl version 3.)
endif

OPENSSL_VERSION_3 := 3.0.0
check_openssl_version:
@if [[ "$(DISTRO)" = "almalinux" && "$(CENTOSVER)" = "8" ]]; then \
@current_version=$$(openssl3 version | awk '{print $$2}'); \
else \
@current_version=$$(openssl version | awk '{print $$2}'); \
fi; \
echo "Installed OpenSSL version: $$current_version"; \
compare_result=`printf "%s\n%s" "$(OPENSSL_VERSION_3)" "$$current_version" | sort -V | head -n 1`; \
if [ "$$compare_result" != "$(OPENSSL_VERSION_3)" ]; then \
echo "Error: Installed OpenSSL version must be $(OPENSSL_VERSION_3) or higher, Please upgrade OpenSSL."; \
exit 1; \
fi
include ../common_mk/openssl_flags.mk
include ../common_mk/openssl_version_check.mk

libhttpserver/libhttpserver/build/src/.libs/libhttpserver.a: libmicrohttpd/libmicrohttpd/src/microhttpd/.libs/libmicrohttpd.a re2/re2/obj/libre2.a
cd libhttpserver && rm -rf libhttpserver-*/ || true
Expand Down
5 changes: 4 additions & 1 deletion include/PgSQL_Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ class PgSQL_Connection_Placeholder {
bool AutocommitFalse_AndSavepoint();
bool MultiplexDisabled(bool check_delay_token = true);
bool IsKeepMultiplexEnabledVariables(char *query_digest_text);
void ProcessQueryAndSetStatusFlags(char *query_digest_text);
void optimize();
void close_mysql();

Expand All @@ -491,6 +490,7 @@ class PgSQL_Connection_Placeholder {
bool IsKnownActiveTransaction() { assert(0); return false; }
bool IsActiveTransaction() { assert(0); return false; }
PG_ASYNC_ST handler(short event) { assert(0); return ASYNC_IDLE; }
void ProcessQueryAndSetStatusFlags(char* query_digest_text);
/********* End of remove ******************/
};

Expand Down Expand Up @@ -617,6 +617,7 @@ class PgSQL_Connection : public PgSQL_Connection_Placeholder {
void optimize() {}
void update_bytes_recv(uint64_t bytes_recv);
void update_bytes_sent(uint64_t bytes_sent);
void ProcessQueryAndSetStatusFlags(char* query_digest_text);

inline const PGconn* get_pg_connection() const { return pgsql_conn; }
inline int get_pg_server_version() { return PQserverVersion(pgsql_conn); }
Expand Down Expand Up @@ -665,6 +666,8 @@ class PgSQL_Connection : public PgSQL_Connection_Placeholder {
// Handles the COPY OUT response from the server.
// Returns true if it consumes all buffer data, or false if the threshold for result size is reached
bool handle_copy_out(const PGresult* result, uint64_t* processed_bytes);
static void notice_handler_cb(void* arg, const PGresult* result);
static void unhandled_notice_cb(void* arg, const PGresult* result);
};

#endif /* __CLASS_PGSQL_CONNECTION_H */
17 changes: 10 additions & 7 deletions include/PgSQL_Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class PgSQL_Protocol;
#define PGSQL_QUERY_RESULT_ERROR 0x08
#define PGSQL_QUERY_RESULT_EMPTY 0x10
#define PGSQL_QUERY_RESULT_COPY_OUT 0x20
#define PGSQL_QUERY_RESULT_COPY_IN 0x30
#define PGSQL_QUERY_RESULT_NOTICE 0x40

class PgSQL_Query_Result {
public:
Expand Down Expand Up @@ -471,6 +471,8 @@ class PgSQL_Query_Result {
*/
unsigned int add_copy_out_response_end();

unsigned int add_notice(const PGresult* result);

/**
* @brief Retrieves the query result set and copies it to a PtrSizeArray.
*
Expand Down Expand Up @@ -829,26 +831,27 @@ class PgSQL_Protocol : public MySQL_Protocol {
unsigned int copy_command_completion_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result, bool extract_affected_rows);

/**
* @brief Copies an error message from a PGresult to a PgSQL_Query_Result.
* @brief Copies an error/notice message from a PGresult to a PgSQL_Query_Result.
*
* This function copies an error message from a `PGresult` object (typically
* obtained from libpq) to a `PgSQL_Query_Result` object. The error message
* contains information about an error that occurred during query execution.
* This function copies an error/notice message from a `PGresult` object (typically
* obtained from libpq) to a `PgSQL_Query_Result` object. The message
* contains information about an error/notice that occurred during query execution.
*
* @param send A boolean flag indicating whether to send the generated packet
* immediately or just generate it. (Currently not supported).
* @param pg_query_result A pointer to the `PgSQL_Query_Result` object where the
* error message will be copied.
* @param result A pointer to the `PGresult` object containing the error
* message to be copied.
*
* @param is_error A boolean flag indicating whether the message is an error or a notice.
*
* @return The number of bytes copied to the `PgSQL_Query_Result` object.
*
* @note This function extracts the various error fields (severity, code,
* message, detail, etc.) from the `PGresult` object and copies them
* to the `PgSQL_Query_Result` object.
*/
unsigned int copy_error_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result);
unsigned int copy_error_notice_to_PgSQL_Query_Result(bool send, PgSQL_Query_Result* pg_query_result, const PGresult* result, bool is_error);

/**
* @brief Copies an empty query response from a PGresult to a
Expand Down
21 changes: 1 addition & 20 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,7 @@ COREDUMPER_IDIR := $(COREDUMPER_DIR)/include
CURL_DIR := $(DEPS_PATH)/curl/curl
CURL_IDIR := $(CURL_DIR)/include

ssl_header_path := $(shell find /usr /usr/local /opt -name "ssl.h" -path "*/openssl/*" 2>/dev/null | head -n 1)
LIB_SSL_PATH := $(shell find /usr /usr/local /opt -name "libssl.so" 2>/dev/null | head -n 1)
LIB_CRYPTO_PATH := $(shell find /usr /usr/local /opt -name "libcrypto.so" 2>/dev/null | head -n 1)
ifeq ($(DISTRO),almalinux)
ifeq ($(CENTOSVER),8)
ssl_header_path := $(shell find /usr /usr/local /opt -name "ssl.h" -path "*/openssl3/*" 2>/dev/null | head -n 1)
LIB_SSL_PATH := $(shell find /usr /usr/local /opt -name "libssl.so.3" 2>/dev/null | head -n 1)
LIB_CRYPTO_PATH := $(shell find /usr /usr/local /opt -name "libcrypto.so.3" 2>/dev/null | head -n 1)endif
endif
endif
SSL_LDIR := $(dir $(LIB_SSL_PATH))

ifneq ($(ssl_header_path),)
SSL_IDIR := $(shell dirname $(shell dirname $(ssl_header_path)))
$(info Found OpenSSL headers at $(SSL_IDIR))
$(info OpenSSL lib full path is $(LIB_SSL_PATH))
$(info OpenSSL libs directory is $(SSL_LDIR))
else
$(error Warning: OpenSSL headers not found. exiting, please install openssl version 3.)
endif
include ../common_mk/openssl_flags.mk

EV_DIR := $(DEPS_PATH)/libev/libev/
EV_IDIR := $(EV_DIR)
Expand Down
4 changes: 4 additions & 0 deletions lib/MySQL_Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,10 @@ int MySQL_Protocol::PPHR_1(unsigned char *pkt, unsigned int len, bool& ret, MyPr

// this function was inline in process_pkt_handshake_response() , split for readibility
bool MySQL_Protocol::PPHR_2(unsigned char *pkt, unsigned int len, bool& ret, MyProt_tmp_auth_vars& vars1) { // process_pkt_handshake_response inner 2

// if packet length is less than 4, it's a malformed packet.
if ((len - sizeof(mysql_hdr)) < 4) return false;

vars1.capabilities = CPY4(pkt);
// see bug #2916. If CLIENT_MULTI_STATEMENTS is set by the client
// we enforce setting CLIENT_MULTI_RESULTS, this is the proper and expected
Expand Down
7 changes: 0 additions & 7 deletions lib/MySQL_Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3954,13 +3954,6 @@ int MySQL_Session::get_pkts_from_client(bool& wrong_pass, PtrSize_t& pkt) {
client_myds->PSarrayIN->remove_index(0,&pkt);
}

if (pkt.size <= sizeof(mysql_hdr)) {
proxy_debug(PROXY_DEBUG_MYSQL_COM, 5, "Malformed packet received\n");
l_free(pkt.size, pkt.ptr);
handler_ret = -1;
return handler_ret;
}

switch (status) {
case WAITING_CLIENT_DATA:
if (pkt.size==(0xFFFFFF+sizeof(mysql_hdr))) { // we are handling a multi-packet
Expand Down
Loading

0 comments on commit ed30c5f

Please sign in to comment.