From 3c870e5c056876f12481f12a083f169cec4b7667 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Thu, 22 Aug 2024 14:26:02 +0500 Subject: [PATCH] Removed unused columns + Bug fixes * Removed unused columns from pgsql_users. * Fixed checksum computation for mysql_users. * Fixed saving of pgsql_users and pgsql_servers to disk during initial load of ProxySQL from the configuration file.. --- include/PgSQL_Authentication.h | 10 +-- include/ProxySQL_Admin_Tables_Definitions.h | 4 +- lib/PgSQL_Authentication.cpp | 44 +++------- lib/PgSQL_Protocol.cpp | 8 +- lib/ProxySQL_Admin.cpp | 91 +++++++++++++-------- lib/ProxySQL_Config.cpp | 24 ++---- 6 files changed, 85 insertions(+), 96 deletions(-) diff --git a/include/PgSQL_Authentication.h b/include/PgSQL_Authentication.h index 1a429c11d..9b053c714 100644 --- a/include/PgSQL_Authentication.h +++ b/include/PgSQL_Authentication.h @@ -17,7 +17,6 @@ struct _scram_keys { typedef struct _pgsql_account_details_t { char* username; char* password; - char* default_schema; char* attributes; char* comment; void* sha1_pass; @@ -27,7 +26,6 @@ typedef struct _pgsql_account_details_t { int max_connections; int num_connections_used; bool use_ssl; - bool schema_locked; bool transaction_persistent; bool fast_forward; bool __frontend; // this is used only during the dump @@ -76,12 +74,12 @@ class PgSQL_Authentication { public: PgSQL_Authentication(); ~PgSQL_Authentication(); - bool add(char *username, char *password, enum cred_username_type usertype, bool use_ssl, int default_hostgroup, char *default_schema, bool schema_locked, bool transaction_persistent, bool fast_forward, int max_connections, char* attributes, char *comment); + bool add(char *username, char *password, enum cred_username_type usertype, bool use_ssl, int default_hostgroup, bool transaction_persistent, bool fast_forward, int max_connections, char* attributes, char *comment); bool del(char *username, enum cred_username_type usertype, bool set_lock=true); bool reset(); void print_version(); bool exists(char *username); - char * lookup(char *username, enum cred_username_type usertype, bool *use_ssl, int *default_hostgroup, char **default_schema, bool *schema_locked, bool *transaction_persistent, bool *fast_forward, int *max_connections, void **sha1_pass, char **attributes); + char * lookup(char *username, enum cred_username_type usertype, bool *use_ssl, int *default_hostgroup, bool *transaction_persistent, bool *fast_forward, int *max_connections, void **sha1_pass, char **attributes); int dump_all_users(pgsql_account_details_t***, bool _complete=true); int increase_frontend_user_connections(char *username, int *mc=NULL); void decrease_frontend_user_connections(char *username); @@ -94,8 +92,8 @@ class PgSQL_Authentication { * @brief Computes the checksum for the 'pgsql_users' table contained in the supplied resultset. * It's UNSAFE to call this function with another resultset than the specified in @param doc. * @param resultset Assumed to be the result of hte following query against the Admin interface: - * - '"SELECT username, password, active, use_ssl, default_hostgroup, default_schema, - * schema_locked, transaction_persistent, fast_forward, backend, frontend, max_connections, + * - '"SELECT username, password, active, use_ssl, default_hostgroup, + * transaction_persistent, fast_forward, backend, frontend, max_connections, * attributes, comment FROM runtime_pgsql_users"' * The order isn't relevant in the query itself because ordering is performed while processing. * @param pgsql_users A 'unique_ptr' to be filled with the 'frontend' and 'backend' users found in the diff --git a/include/ProxySQL_Admin_Tables_Definitions.h b/include/ProxySQL_Admin_Tables_Definitions.h index 727d48665..42f65d6d3 100644 --- a/include/ProxySQL_Admin_Tables_Definitions.h +++ b/include/ProxySQL_Admin_Tables_Definitions.h @@ -267,7 +267,7 @@ // PgSQL Admin tables #define ADMIN_SQLITE_TABLE_PGSQL_SERVERS "CREATE TABLE pgsql_servers (hostgroup_id INT CHECK (hostgroup_id>=0) NOT NULL DEFAULT 0 , hostname VARCHAR NOT NULL , port INT CHECK (port >= 0 AND port <= 65535) NOT NULL DEFAULT 3306 , gtid_port INT CHECK ((gtid_port <> port OR gtid_port=0) AND gtid_port >= 0 AND gtid_port <= 65535) NOT NULL DEFAULT 0 , status VARCHAR CHECK (UPPER(status) IN ('ONLINE','SHUNNED','OFFLINE_SOFT', 'OFFLINE_HARD')) NOT NULL DEFAULT 'ONLINE' , weight INT CHECK (weight >= 0 AND weight <=10000000) NOT NULL DEFAULT 1 , compression INT CHECK (compression IN(0,1)) NOT NULL DEFAULT 0 , max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 1000 , max_replication_lag INT CHECK (max_replication_lag >= 0 AND max_replication_lag <= 126144000) NOT NULL DEFAULT 0 , use_ssl INT CHECK (use_ssl IN(0,1)) NOT NULL DEFAULT 0 , max_latency_ms INT UNSIGNED CHECK (max_latency_ms>=0) NOT NULL DEFAULT 0 , comment VARCHAR NOT NULL DEFAULT '' , PRIMARY KEY (hostgroup_id, hostname, port) )" -#define ADMIN_SQLITE_TABLE_PGSQL_USERS "CREATE TABLE pgsql_users (username VARCHAR NOT NULL , password VARCHAR , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , use_ssl INT CHECK (use_ssl IN (0,1)) NOT NULL DEFAULT 0 , default_hostgroup INT NOT NULL DEFAULT 0 , default_schema VARCHAR , schema_locked INT CHECK (schema_locked IN (0,1)) NOT NULL DEFAULT 0 , transaction_persistent INT CHECK (transaction_persistent IN (0,1)) NOT NULL DEFAULT 1 , fast_forward INT CHECK (fast_forward IN (0,1)) NOT NULL DEFAULT 0 , backend INT CHECK (backend IN (0,1)) NOT NULL DEFAULT 1 , frontend INT CHECK (frontend IN (0,1)) NOT NULL DEFAULT 1 , max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 10000 , attributes VARCHAR CHECK (JSON_VALID(attributes) OR attributes = '') NOT NULL DEFAULT '' , comment VARCHAR NOT NULL DEFAULT '' , PRIMARY KEY (username, backend) , UNIQUE (username, frontend))" +#define ADMIN_SQLITE_TABLE_PGSQL_USERS "CREATE TABLE pgsql_users (username VARCHAR NOT NULL , password VARCHAR , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , use_ssl INT CHECK (use_ssl IN (0,1)) NOT NULL DEFAULT 0 , default_hostgroup INT NOT NULL DEFAULT 0 , transaction_persistent INT CHECK (transaction_persistent IN (0,1)) NOT NULL DEFAULT 1 , fast_forward INT CHECK (fast_forward IN (0,1)) NOT NULL DEFAULT 0 , backend INT CHECK (backend IN (0,1)) NOT NULL DEFAULT 1 , frontend INT CHECK (frontend IN (0,1)) NOT NULL DEFAULT 1 , max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 10000 , attributes VARCHAR CHECK (JSON_VALID(attributes) OR attributes = '') NOT NULL DEFAULT '' , comment VARCHAR NOT NULL DEFAULT '' , PRIMARY KEY (username, backend) , UNIQUE (username, frontend))" #define ADMIN_SQLITE_TABLE_PGSQL_LDAP_MAPPING "CREATE TABLE pgsql_ldap_mapping (priority INTEGER CHECK (priority >= 1 AND priority <= 1000000) PRIMARY KEY , frontend_entity VARCHAR NOT NULL , backend_entity VARCHAR NOT NULL , comment VARCHAR NOT NULL DEFAULT '' , UNIQUE (frontend_entity))" #define ADMIN_SQLITE_TABLE_PGSQL_QUERY_RULES "CREATE TABLE pgsql_query_rules (rule_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 0 , username VARCHAR , schemaname VARCHAR , flagIN INT CHECK (flagIN >= 0) NOT NULL DEFAULT 0 , client_addr VARCHAR , proxy_addr VARCHAR , proxy_port INT CHECK (proxy_port >= 0 AND proxy_port <= 65535) , digest VARCHAR , match_digest VARCHAR , match_pattern VARCHAR , negate_match_pattern INT CHECK (negate_match_pattern IN (0,1)) NOT NULL DEFAULT 0 , re_modifiers VARCHAR DEFAULT 'CASELESS' , flagOUT INT CHECK (flagOUT >= 0) , replace_pattern VARCHAR CHECK(CASE WHEN replace_pattern IS NULL THEN 1 WHEN replace_pattern IS NOT NULL AND match_pattern IS NOT NULL THEN 1 ELSE 0 END) , destination_hostgroup INT DEFAULT NULL , cache_ttl INT CHECK(cache_ttl > 0) , cache_empty_result INT CHECK (cache_empty_result IN (0,1)) DEFAULT NULL , cache_timeout INT CHECK(cache_timeout >= 0) , reconnect INT CHECK (reconnect IN (0,1)) DEFAULT NULL , timeout INT UNSIGNED CHECK (timeout >= 0) , retries INT CHECK (retries>=0 AND retries <=1000) , delay INT UNSIGNED CHECK (delay >=0) , next_query_flagIN INT UNSIGNED , mirror_flagOUT INT UNSIGNED , mirror_hostgroup INT UNSIGNED , error_msg VARCHAR , OK_msg VARCHAR , sticky_conn INT CHECK (sticky_conn IN (0,1)) , multiplex INT CHECK (multiplex IN (0,1,2)) , gtid_from_hostgroup INT UNSIGNED , log INT CHECK (log IN (0,1)) , apply INT CHECK(apply IN (0,1)) NOT NULL DEFAULT 0 , attributes VARCHAR CHECK (JSON_VALID(attributes) OR attributes = '') NOT NULL DEFAULT '' , comment VARCHAR)" #define ADMIN_SQLITE_TABLE_PGSQL_FIREWALL_WHITELIST_USERS "CREATE TABLE pgsql_firewall_whitelist_users (active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , username VARCHAR NOT NULL , client_address VARCHAR NOT NULL , mode VARCHAR CHECK (mode IN ('OFF','DETECTING','PROTECTING')) NOT NULL DEFAULT ('OFF') , comment VARCHAR NOT NULL , PRIMARY KEY (username, client_address) )" @@ -278,7 +278,7 @@ #define ADMIN_SQLITE_TABLE_PGSQL_REPLICATION_HOSTGROUPS "CREATE TABLE pgsql_replication_hostgroups (writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY , reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND reader_hostgroup>=0) , check_type VARCHAR CHECK (LOWER(check_type) IN ('read_only','innodb_read_only','super_read_only','read_only|innodb_read_only','read_only&innodb_read_only')) NOT NULL DEFAULT 'read_only' , comment VARCHAR NOT NULL DEFAULT '', UNIQUE (reader_hostgroup))" #define ADMIN_SQLITE_TABLE_RUNTIME_PGSQL_SERVERS "CREATE TABLE runtime_pgsql_servers (hostgroup_id INT CHECK (hostgroup_id>=0) NOT NULL DEFAULT 0 , hostname VARCHAR NOT NULL , port INT CHECK (port >= 0 AND port <= 65535) NOT NULL DEFAULT 3306 , gtid_port INT CHECK ((gtid_port <> port OR gtid_port=0) AND gtid_port >= 0 AND gtid_port <= 65535) NOT NULL DEFAULT 0 , status VARCHAR CHECK (UPPER(status) IN ('ONLINE','SHUNNED','OFFLINE_SOFT', 'OFFLINE_HARD')) NOT NULL DEFAULT 'ONLINE' , weight INT CHECK (weight >= 0 AND weight <=10000000) NOT NULL DEFAULT 1 , compression INT CHECK (compression IN(0,1)) NOT NULL DEFAULT 0 , max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 1000 , max_replication_lag INT CHECK (max_replication_lag >= 0 AND max_replication_lag <= 126144000) NOT NULL DEFAULT 0 , use_ssl INT CHECK (use_ssl IN(0,1)) NOT NULL DEFAULT 0 , max_latency_ms INT UNSIGNED CHECK (max_latency_ms>=0) NOT NULL DEFAULT 0 , comment VARCHAR NOT NULL DEFAULT '' , PRIMARY KEY (hostgroup_id, hostname, port) )" -#define ADMIN_SQLITE_TABLE_RUNTIME_PGSQL_USERS "CREATE TABLE runtime_pgsql_users (username VARCHAR NOT NULL , password VARCHAR , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , use_ssl INT CHECK (use_ssl IN (0,1)) NOT NULL DEFAULT 0 , default_hostgroup INT NOT NULL DEFAULT 0 , default_schema VARCHAR , schema_locked INT CHECK (schema_locked IN (0,1)) NOT NULL DEFAULT 0 , transaction_persistent INT CHECK (transaction_persistent IN (0,1)) NOT NULL DEFAULT 1 , fast_forward INT CHECK (fast_forward IN (0,1)) NOT NULL DEFAULT 0 , backend INT CHECK (backend IN (0,1)) NOT NULL DEFAULT 1 , frontend INT CHECK (frontend IN (0,1)) NOT NULL DEFAULT 1 , max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 10000 , attributes VARCHAR CHECK (JSON_VALID(attributes) OR attributes = '') NOT NULL DEFAULT '', comment VARCHAR NOT NULL DEFAULT '' , PRIMARY KEY (username, backend) , UNIQUE (username, frontend))" +#define ADMIN_SQLITE_TABLE_RUNTIME_PGSQL_USERS "CREATE TABLE runtime_pgsql_users (username VARCHAR NOT NULL , password VARCHAR , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , use_ssl INT CHECK (use_ssl IN (0,1)) NOT NULL DEFAULT 0 , default_hostgroup INT NOT NULL DEFAULT 0 , transaction_persistent INT CHECK (transaction_persistent IN (0,1)) NOT NULL DEFAULT 1 , fast_forward INT CHECK (fast_forward IN (0,1)) NOT NULL DEFAULT 0 , backend INT CHECK (backend IN (0,1)) NOT NULL DEFAULT 1 , frontend INT CHECK (frontend IN (0,1)) NOT NULL DEFAULT 1 , max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 10000 , attributes VARCHAR CHECK (JSON_VALID(attributes) OR attributes = '') NOT NULL DEFAULT '', comment VARCHAR NOT NULL DEFAULT '' , PRIMARY KEY (username, backend) , UNIQUE (username, frontend))" #define ADMIN_SQLITE_TABLE_RUNTIME_PGSQL_LDAP_MAPPING "CREATE TABLE runtime_pgsql_ldap_mapping (priority INTEGER PRIMARY KEY NOT NULL , frontend_entity VARCHAR NOT NULL , backend_entity VARCHAR NOT NULL , comment VARCHAR NOT NULL DEFAULT '' , UNIQUE (frontend_entity))" #define ADMIN_SQLITE_TABLE_RUNTIME_PGSQL_QUERY_RULES "CREATE TABLE runtime_pgsql_query_rules (rule_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 0 , username VARCHAR , schemaname VARCHAR , flagIN INT CHECK (flagIN >= 0) NOT NULL DEFAULT 0 , client_addr VARCHAR , proxy_addr VARCHAR , proxy_port INT CHECK (proxy_port >= 0 AND proxy_port <= 65535), digest VARCHAR , match_digest VARCHAR , match_pattern VARCHAR , negate_match_pattern INT CHECK (negate_match_pattern IN (0,1)) NOT NULL DEFAULT 0 , re_modifiers VARCHAR DEFAULT 'CASELESS' , flagOUT INT CHECK (flagOUT >= 0), replace_pattern VARCHAR CHECK(CASE WHEN replace_pattern IS NULL THEN 1 WHEN replace_pattern IS NOT NULL AND match_pattern IS NOT NULL THEN 1 ELSE 0 END) , destination_hostgroup INT DEFAULT NULL , cache_ttl INT CHECK(cache_ttl > 0) , cache_empty_result INT CHECK (cache_empty_result IN (0,1)) DEFAULT NULL , cache_timeout INT CHECK(cache_timeout >= 0) , reconnect INT CHECK (reconnect IN (0,1)) DEFAULT NULL , timeout INT UNSIGNED CHECK (timeout >= 0) , retries INT CHECK (retries>=0 AND retries <=1000) , delay INT UNSIGNED CHECK (delay >=0) , next_query_flagIN INT UNSIGNED , mirror_flagOUT INT UNSIGNED , mirror_hostgroup INT UNSIGNED , error_msg VARCHAR , OK_msg VARCHAR , sticky_conn INT CHECK (sticky_conn IN (0,1)) , multiplex INT CHECK (multiplex IN (0,1,2)) , gtid_from_hostgroup INT UNSIGNED , log INT CHECK (log IN (0,1)) , apply INT CHECK(apply IN (0,1)) NOT NULL DEFAULT 0 , attributes VARCHAR CHECK (JSON_VALID(attributes) OR attributes = '') NOT NULL DEFAULT '' , comment VARCHAR)" #define ADMIN_SQLITE_TABLE_RUNTIME_PGSQL_FIREWALL_WHITELIST_USERS "CREATE TABLE runtime_pgsql_firewall_whitelist_users (active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 1 , username VARCHAR NOT NULL , client_address VARCHAR NOT NULL , mode VARCHAR CHECK (mode IN ('OFF','DETECTING','PROTECTING')) NOT NULL DEFAULT ('OFF') , comment VARCHAR NOT NULL , PRIMARY KEY (username, client_address) )" diff --git a/lib/PgSQL_Authentication.cpp b/lib/PgSQL_Authentication.cpp index 60a9b25ad..7f79bdf67 100644 --- a/lib/PgSQL_Authentication.cpp +++ b/lib/PgSQL_Authentication.cpp @@ -86,7 +86,7 @@ __loop_remove_inactives: #endif } -bool PgSQL_Authentication::add(char * username, char * password, enum cred_username_type usertype, bool use_ssl, int default_hostgroup, char *default_schema, bool schema_locked, bool transaction_persistent, bool fast_forward, int max_connections, char* attributes, char *comment) { +bool PgSQL_Authentication::add(char * username, char * password, enum cred_username_type usertype, bool use_ssl, int default_hostgroup, bool transaction_persistent, bool fast_forward, int max_connections, char* attributes, char *comment) { uint64_t hash1, hash2; SpookyHash myhash; myhash.Init(1,2); @@ -115,10 +115,6 @@ bool PgSQL_Authentication::add(char * username, char * password, enum cred_usern ad->sha1_pass=NULL; } } - if (strcmp(ad->default_schema,default_schema)) { - free(ad->default_schema); - ad->default_schema=strdup(default_schema); - } if (strcmp(ad->comment,comment)) { free(ad->comment); ad->comment=strdup(comment); @@ -180,7 +176,6 @@ bool PgSQL_Authentication::add(char * username, char * password, enum cred_usern } else { ad=(pgsql_account_details_t *)malloc(sizeof(pgsql_account_details_t )); ad->username=strdup(username); - ad->default_schema=strdup(default_schema); ad->comment=strdup(comment); ad->password=strdup(password); if (strlen(attributes)) { @@ -203,7 +198,6 @@ bool PgSQL_Authentication::add(char * username, char * password, enum cred_usern ad->use_ssl=use_ssl; ad->default_hostgroup=default_hostgroup; - ad->schema_locked=schema_locked; ad->transaction_persistent=transaction_persistent; ad->fast_forward=fast_forward; ad->max_connections=max_connections; @@ -237,7 +231,6 @@ unsigned int PgSQL_Authentication::memory_usage() { if (ado->username) ret += strlen(ado->username) + 1; if (ado->password) ret += strlen(ado->password) + 1; if (ado->sha1_pass) ret += SHA_DIGEST_LENGTH; - if (ado->default_schema) ret += strlen(ado->default_schema) + 1; if (ado->comment) ret += strlen(ado->comment) + 1; if (ado->attributes) ret += strlen(ado->attributes) + 1; } @@ -250,7 +243,6 @@ unsigned int PgSQL_Authentication::memory_usage() { if (ado->username) ret += strlen(ado->username) + 1; if (ado->password) ret += strlen(ado->password) + 1; if (ado->sha1_pass) ret += SHA_DIGEST_LENGTH; - if (ado->default_schema) ret += strlen(ado->default_schema) + 1; if (ado->comment) ret += strlen(ado->comment) + 1; if (ado->attributes) ret += strlen(ado->attributes) + 1; } @@ -293,7 +285,6 @@ int PgSQL_Authentication::dump_all_users(pgsql_account_details_t***ads, bool _co ad->default_hostgroup=ado->default_hostgroup; if (_complete==false) { ad->password=NULL; - ad->default_schema=NULL; ad->attributes=NULL; ad->comment=NULL; ad->num_connections_used=ado->num_connections_used; @@ -302,10 +293,8 @@ int PgSQL_Authentication::dump_all_users(pgsql_account_details_t***ads, bool _co ad->password=strdup(ado->password); ad->sha1_pass=NULL; ad->use_ssl=ado->use_ssl; - ad->default_schema=strdup(ado->default_schema); ad->attributes=strdup(ado->attributes); ad->comment=strdup(ado->comment); - ad->schema_locked=ado->schema_locked; ad->transaction_persistent=ado->transaction_persistent; ad->fast_forward=ado->fast_forward; ad->__frontend=1; @@ -324,10 +313,8 @@ int PgSQL_Authentication::dump_all_users(pgsql_account_details_t***ads, bool _co ad->sha1_pass=NULL; ad->use_ssl=ado->use_ssl; ad->default_hostgroup=ado->default_hostgroup; - ad->default_schema=strdup(ado->default_schema); ad->attributes=strdup(ado->attributes); ad->comment=strdup(ado->comment); - ad->schema_locked=ado->schema_locked; ad->transaction_persistent=ado->transaction_persistent; ad->fast_forward=ado->fast_forward; ad->max_connections=ado->max_connections; @@ -438,7 +425,6 @@ bool PgSQL_Authentication::del(char * username, enum cred_username_type usertype free(ad->username); free(ad->password); if (ad->sha1_pass) { free(ad->sha1_pass); ad->sha1_pass=NULL; } - free(ad->default_schema); free(ad->attributes); free(ad->comment); free(ad); @@ -507,7 +493,7 @@ bool PgSQL_Authentication::exists(char * username) { return ret; } -char * PgSQL_Authentication::lookup(char * username, enum cred_username_type usertype, bool *use_ssl, int *default_hostgroup, char **default_schema, bool *schema_locked, bool *transaction_persistent, bool *fast_forward, int *max_connections, void **sha1_pass, char **attributes) { +char * PgSQL_Authentication::lookup(char * username, enum cred_username_type usertype, bool *use_ssl, int *default_hostgroup, bool *transaction_persistent, bool *fast_forward, int *max_connections, void **sha1_pass, char **attributes) { char *ret=NULL; uint64_t hash1, hash2; SpookyHash myhash; @@ -529,8 +515,6 @@ char * PgSQL_Authentication::lookup(char * username, enum cred_username_type use ret=l_strdup(ad->password); if (use_ssl) *use_ssl=ad->use_ssl; if (default_hostgroup) *default_hostgroup=ad->default_hostgroup; - if (default_schema) *default_schema=l_strdup(ad->default_schema); - if (schema_locked) *schema_locked=ad->schema_locked; if (transaction_persistent) *transaction_persistent=ad->transaction_persistent; if (fast_forward) *fast_forward=ad->fast_forward; if (max_connections) *max_connections=ad->max_connections; @@ -569,7 +553,6 @@ bool PgSQL_Authentication::_reset(enum cred_username_type usertype) { free(ad->username); free(ad->password); if (ad->sha1_pass) { free(ad->sha1_pass); ad->sha1_pass=NULL; } - free(ad->default_schema); free(ad->comment); free(ad->attributes); //free(ad->scram_keys); @@ -610,14 +593,11 @@ static uint64_t compute_accounts_hash(const umap_pgauth& accs_map) { foundany = true; acc_map_hash.Update(&ad->use_ssl,sizeof(ad->use_ssl)); acc_map_hash.Update(&ad->default_hostgroup,sizeof(ad->default_hostgroup)); - acc_map_hash.Update(&ad->schema_locked,sizeof(ad->schema_locked)); acc_map_hash.Update(&ad->transaction_persistent,sizeof(ad->transaction_persistent)); acc_map_hash.Update(&ad->fast_forward,sizeof(ad->fast_forward)); acc_map_hash.Update(&ad->max_connections,sizeof(ad->max_connections)); acc_map_hash.Update(ad->username,strlen(ad->username)); acc_map_hash.Update(ad->password,strlen(ad->password)); - if (ad->default_schema) - acc_map_hash.Update(ad->default_schema,strlen(ad->default_schema)); if (ad->comment) acc_map_hash.Update(ad->comment,strlen(ad->comment)); if (ad->attributes) { @@ -676,15 +656,13 @@ static pair extract_accounts_details(MYSQL_RES* result acc_details->__active = true; acc_details->use_ssl = strcmp(row[2], "1") == 0 ? true : false; acc_details->default_hostgroup = atoi(row[3]); - acc_details->default_schema = row[4] ? row[4] : const_cast(""); - acc_details->schema_locked = strcmp(row[5], "1") == 0 ? true : false; - acc_details->transaction_persistent = strcmp(row[6], "1") == 0 ? true : false; - acc_details->fast_forward = strcmp(row[7], "1") == 0 ? true : false; - acc_details->__backend = strcmp(row[8], "1") == 0 ? true : false; - acc_details->__frontend = strcmp(row[9], "1") == 0 ? true : false; - acc_details->max_connections = atoi(row[10]); - acc_details->attributes = row[11] ? row[11] : const_cast(""); - acc_details->comment = row[12] ? row[12] : const_cast(""); + acc_details->transaction_persistent = strcmp(row[4], "1") == 0 ? true : false; + acc_details->fast_forward = strcmp(row[5], "1") == 0 ? true : false; + acc_details->__backend = strcmp(row[6], "1") == 0 ? true : false; + acc_details->__frontend = strcmp(row[7], "1") == 0 ? true : false; + acc_details->max_connections = atoi(row[8]); + acc_details->attributes = row[9] ? row[9] : const_cast(""); + acc_details->comment = row[10] ? row[10] : const_cast(""); return acc_details; }; @@ -699,12 +677,12 @@ static pair extract_accounts_details(MYSQL_RES* result myhash.Final(&u_hash, &_u_hash2); // is backend - if (strcmp(row[8], "1") == 0) { + if (strcmp(row[6], "1") == 0) { pgsql_account_details_t* acc_details = create_account_details(row); b_accs_map.insert({u_hash, acc_details}); } // is frontend - if (strcmp(row[9], "1") == 0) { + if (strcmp(row[7], "1") == 0) { pgsql_account_details_t* acc_details = create_account_details(row); f_accs_map.insert({u_hash, acc_details}); } diff --git a/lib/PgSQL_Protocol.cpp b/lib/PgSQL_Protocol.cpp index 1b4979b1b..d95b20a2b 100644 --- a/lib/PgSQL_Protocol.cpp +++ b/lib/PgSQL_Protocol.cpp @@ -702,7 +702,6 @@ EXECUTION_STATE PgSQL_Protocol::process_handshake_response_packet(unsigned char* char* pass = NULL; char* password = NULL; - char* default_schema = NULL; //char* db = NULL; char* attributes = NULL; void* sha1_pass = NULL; @@ -710,7 +709,6 @@ EXECUTION_STATE PgSQL_Protocol::process_handshake_response_packet(unsigned char* int default_hostgroup = -1; enum proxysql_session_type session_type = (*myds)->sess->session_type; bool using_password = false; - bool schema_locked; bool transaction_persistent = true; bool fast_forward = false; bool _ret_use_ssl = false; @@ -735,7 +733,7 @@ EXECUTION_STATE PgSQL_Protocol::process_handshake_response_packet(unsigned char* goto __exit_process_pkt_handshake_response; } - password = GloPgAuth->lookup((char*)user, USERNAME_FRONTEND, &_ret_use_ssl, &default_hostgroup, &default_schema, &schema_locked, &transaction_persistent, &fast_forward, &max_connections, &sha1_pass, &attributes); + password = GloPgAuth->lookup((char*)user, USERNAME_FRONTEND, &_ret_use_ssl, &default_hostgroup, &transaction_persistent, &fast_forward, &max_connections, &sha1_pass, &attributes); if (password) { #ifdef DEBUG @@ -748,9 +746,9 @@ EXECUTION_STATE PgSQL_Protocol::process_handshake_response_packet(unsigned char* free(tmp_pass); #endif // debug (*myds)->sess->default_hostgroup = default_hostgroup; - (*myds)->sess->default_schema = default_schema; // just the pointer is passed + //(*myds)->sess->default_schema = default_schema; // just the pointer is passed (*myds)->sess->user_attributes = attributes; // just the pointer is passed - (*myds)->sess->schema_locked = schema_locked; + //(*myds)->sess->schema_locked = schema_locked; (*myds)->sess->transaction_persistent = transaction_persistent; (*myds)->sess->session_fast_forward = false; // default if ((*myds)->sess->session_type == PROXYSQL_SESSION_PGSQL) { diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 9e576fa8f..7baf6f4c7 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -3334,7 +3334,7 @@ void ProxySQL_Admin::add_credentials(char *credentials, int hostgroup_id) { } } else if constexpr (pt == SERVER_TYPE_PGSQL) { if (GloPgAuth) { // this check if required if GloPgAuth doesn't exist yet - GloPgAuth->add(user, pass, USERNAME_FRONTEND, 0, hostgroup_id, (char*)"main", 0, 0, 0, 1000, (char*)"", (char*)""); + GloPgAuth->add(user, pass, USERNAME_FRONTEND, 0, hostgroup_id, 0, 0, 1000, (char*)"", (char*)""); } } @@ -5063,6 +5063,12 @@ void ProxySQL_Admin::__insert_or_replace_disktable_select_maintable() { BQE1(admindb, scheduler_tablenames, "", "INSERT OR REPLACE INTO disk.", " SELECT * FROM main."); BQE1(admindb, restapi_tablenames, "", "INSERT OR REPLACE INTO disk.", " SELECT * FROM main."); BQE1(admindb, proxysql_servers_tablenames, "", "INSERT OR REPLACE INTO disk.", " SELECT * FROM main."); + + BQE1(admindb, pgsql_servers_tablenames, "", "INSERT OR REPLACE INTO disk.", " SELECT * FROM main."); + BQE1(admindb, pgsql_query_rules_tablenames, "", "INSERT OR REPLACE INTO disk.", " SELECT * FROM main."); + admindb->execute("INSERT OR REPLACE INTO disk.pgsql_users SELECT * FROM main.pgsql_users"); + BQE1(admindb, pgsql_firewall_tablenames, "", "INSERT OR REPLACE INTO disk.", " SELECT * FROM main."); + #ifdef DEBUG admindb->execute("INSERT OR REPLACE INTO disk.debug_levels SELECT * FROM main.debug_levels"); admindb->execute("INSERT OR REPLACE INTO disk.debug_filters SELECT * FROM main.debug_filters"); @@ -5292,7 +5298,6 @@ void ProxySQL_Admin::__refresh_users( if (no_resultset_supplied) { uint64_t hash1 = GloMyAuth->get_runtime_checksum(); - hash1 += GloPgAuth->get_runtime_checksum(); if (GloMyLdapAuth) { hash1 += GloMyLdapAuth->get_ldap_mapping_runtime_checksum(); } @@ -5583,7 +5588,7 @@ SQLite3_result* ProxySQL_Admin::__add_active_users( if constexpr (pt == SERVER_TYPE_MYSQL) { str = (char*)"SELECT username,password,use_ssl,default_hostgroup,default_schema,schema_locked,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment FROM main.mysql_users WHERE active=1 AND default_hostgroup>=0"; } else if constexpr (pt == SERVER_TYPE_PGSQL) { - str = (char*)"SELECT username,password,use_ssl,default_hostgroup,default_schema,schema_locked,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment FROM main.pgsql_users WHERE active=1 AND default_hostgroup>=0"; + str = (char*)"SELECT username,password,use_ssl,default_hostgroup,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment FROM main.pgsql_users WHERE active=1 AND default_hostgroup>=0"; } admindb->execute_statement(str, &error, &cols, &affected_rows, &resultset); } else { @@ -5593,7 +5598,7 @@ SQLite3_result* ProxySQL_Admin::__add_active_users( if constexpr (pt == SERVER_TYPE_MYSQL) { str = (char*)"SELECT username,password,use_ssl,default_hostgroup,default_schema,schema_locked,transaction_persistent,fast_forward,max_connections,attributes,comment FROM main.mysql_users WHERE %s=1 AND active=1 AND default_hostgroup>=0 AND username='%s'"; } else if constexpr (pt == SERVER_TYPE_PGSQL) { - str = (char*)"SELECT username,password,use_ssl,default_hostgroup,default_schema,schema_locked,transaction_persistent,fast_forward,max_connections,attributes,comment FROM main.pgsql_users WHERE %s=1 AND active=1 AND default_hostgroup>=0 AND username='%s'"; + str = (char*)"SELECT username,password,use_ssl,default_hostgroup,transaction_persistent,fast_forward,max_connections,attributes,comment FROM main.pgsql_users WHERE %s=1 AND active=1 AND default_hostgroup>=0 AND username='%s'"; } query=(char *)malloc(strlen(str)+strlen(__user)+15); sprintf(query,str,(usertype==USERNAME_BACKEND ? "backend" : "frontend"),__user); @@ -5615,7 +5620,6 @@ SQLite3_result* ProxySQL_Admin::__add_active_users( } } - for (std::vector::iterator it = resultset->rows.begin() ; it != resultset->rows.end(); ++it) { SQLite3_row *r=*it; char *password=NULL; @@ -5631,25 +5635,47 @@ SQLite3_result* ProxySQL_Admin::__add_active_users( char* attributes = nullptr; char* comment = nullptr; - if (__user != nullptr) { - usertypes.push_back(usertype); + if constexpr (pt == SERVER_TYPE_MYSQL) { + if (__user != nullptr) { + usertypes.push_back(usertype); - max_connections = r->fields[8]; - attributes = r->fields[9]; - comment = r->fields[10]; - } else { - if (strcasecmp(r->fields[8], "1") == 0) { - usertypes.push_back(USERNAME_BACKEND); + max_connections = r->fields[8]; + attributes = r->fields[9]; + comment = r->fields[10]; } - if (strcasecmp(r->fields[9], "1") == 0) { - usertypes.push_back(USERNAME_FRONTEND); + else { + if (strcasecmp(r->fields[8], "1") == 0) { + usertypes.push_back(USERNAME_BACKEND); + } + if (strcasecmp(r->fields[9], "1") == 0) { + usertypes.push_back(USERNAME_FRONTEND); + } + + max_connections = r->fields[10]; + attributes = r->fields[11]; + comment = r->fields[12]; } + } else if constexpr (pt == SERVER_TYPE_PGSQL) { + if (__user != nullptr) { + usertypes.push_back(usertype); - max_connections = r->fields[10]; - attributes = r->fields[11]; - comment = r->fields[12]; - } + max_connections = r->fields[6]; + attributes = r->fields[7]; + comment = r->fields[8]; + } + else { + if (strcasecmp(r->fields[6], "1") == 0) { + usertypes.push_back(USERNAME_BACKEND); + } + if (strcasecmp(r->fields[7], "1") == 0) { + usertypes.push_back(USERNAME_FRONTEND); + } + max_connections = r->fields[8]; + attributes = r->fields[9]; + comment = r->fields[10]; + } + } for (const enum cred_username_type usertype : usertypes) { if constexpr (pt == SERVER_TYPE_MYSQL) { GloMyAuth->add( @@ -5673,10 +5699,8 @@ SQLite3_result* ProxySQL_Admin::__add_active_users( usertype, // backend/frontend (strcmp(r->fields[2], "1") == 0 ? true : false), // use_ssl atoi(r->fields[3]), // default_hostgroup - (r->fields[4] == NULL ? (char*)"" : r->fields[4]), //default_schema - (strcmp(r->fields[5], "1") == 0 ? true : false), // schema_locked - (strcmp(r->fields[6], "1") == 0 ? true : false), // transaction_persistent - (strcmp(r->fields[7], "1") == 0 ? true : false), // fast_forward + (strcmp(r->fields[4], "1") == 0 ? true : false), // transaction_persistent + (strcmp(r->fields[5], "1") == 0 ? true : false), // fast_forward (atoi(max_connections) > 0 ? atoi(max_connections) : 0), // max_connections (attributes == NULL ? (char*)"" : attributes), // attributes (comment == NULL ? (char*)"" : comment) //comment @@ -6047,10 +6071,10 @@ void ProxySQL_Admin::save_pgsql_users_runtime_to_database(bool _runtime) { // char *qfr=(char *)"REPLACE INTO runtime_pgsql_users(username,password,active,use_ssl,default_hostgroup,default_schema,schema_locked,transaction_persistent,fast_forward,backend,frontend,max_connections) VALUES('%s','%s',1,%d,%d,'%s',%d,%d,%d,COALESCE((SELECT backend FROM runtime_mysql_users WHERE username='%s' AND frontend=1),0),1,%d)"; // char *qbr=(char *)"REPLACE INTO runtime_pgsql_users(username,password,active,use_ssl,default_hostgroup,default_schema,schema_locked,transaction_persistent,fast_forward,backend,frontend,max_connections) VALUES('%s','%s',1,%d,%d,'%s',%d,%d,%d,1,COALESCE((SELECT frontend FROM runtime_mysql_users WHERE username='%s' AND backend=1),0),%d)"; - char* qf_stmt1 = (char*)"REPLACE INTO pgsql_users(username,password,active,use_ssl,default_hostgroup,default_schema,schema_locked,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment) VALUES(?1,?2,1,?3,?4,?5,?6,?7,?8,0,1,?9,?10,?11)"; - char* qb_stmt1 = (char*)"REPLACE INTO pgsql_users(username,password,active,use_ssl,default_hostgroup,default_schema,schema_locked,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment) VALUES(?1,?2,1,?3,?4,?5,?6,?7,?8,1,0,?9,?10,?11)"; - char* qfr_stmt1 = (char*)"REPLACE INTO runtime_pgsql_users(username,password,active,use_ssl,default_hostgroup,default_schema,schema_locked,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment) VALUES(?1,?2,1,?3,?4,?5,?6,?7,?8,0,1,?9,?10,?11)"; - char* qbr_stmt1 = (char*)"REPLACE INTO runtime_pgsql_users(username,password,active,use_ssl,default_hostgroup,default_schema,schema_locked,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment) VALUES(?1,?2,1,?3,?4,?5,?6,?7,?8,1,0,?9,?10,?11)"; + char* qf_stmt1 = (char*)"REPLACE INTO pgsql_users(username,password,active,use_ssl,default_hostgroup,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment) VALUES(?1,?2,1,?3,?4,?5,?6,0,1,?7,?8,?9)"; + char* qb_stmt1 = (char*)"REPLACE INTO pgsql_users(username,password,active,use_ssl,default_hostgroup,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment) VALUES(?1,?2,1,?3,?4,?5,?6,1,0,?7,?8,?9)"; + char* qfr_stmt1 = (char*)"REPLACE INTO runtime_pgsql_users(username,password,active,use_ssl,default_hostgroup,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment) VALUES(?1,?2,1,?3,?4,?5,?6,0,1,?7,?8,?9)"; + char* qbr_stmt1 = (char*)"REPLACE INTO runtime_pgsql_users(username,password,active,use_ssl,default_hostgroup,transaction_persistent,fast_forward,backend,frontend,max_connections,attributes,comment) VALUES(?1,?2,1,?3,?4,?5,?6,1,0,?7,?8,?9)"; num_users = GloPgAuth->dump_all_users(&ads); if (num_users == 0) return; char* q_stmt1_f = NULL; @@ -6115,20 +6139,17 @@ void ProxySQL_Admin::save_pgsql_users_runtime_to_database(bool _runtime) { rc = (*proxy_sqlite3_bind_text)(statement1, 2, ad->password, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, admindb); rc = (*proxy_sqlite3_bind_int64)(statement1, 3, ad->use_ssl); ASSERT_SQLITE_OK(rc, admindb); rc = (*proxy_sqlite3_bind_int64)(statement1, 4, ad->default_hostgroup); ASSERT_SQLITE_OK(rc, admindb); - rc = (*proxy_sqlite3_bind_text)(statement1, 5, ad->default_schema, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, admindb); - rc = (*proxy_sqlite3_bind_int64)(statement1, 6, ad->schema_locked); ASSERT_SQLITE_OK(rc, admindb); - rc = (*proxy_sqlite3_bind_int64)(statement1, 7, ad->transaction_persistent); ASSERT_SQLITE_OK(rc, admindb); - rc = (*proxy_sqlite3_bind_int64)(statement1, 8, ad->fast_forward); ASSERT_SQLITE_OK(rc, admindb); - rc = (*proxy_sqlite3_bind_int64)(statement1, 9, ad->max_connections); ASSERT_SQLITE_OK(rc, admindb); - rc = (*proxy_sqlite3_bind_text)(statement1, 10, ad->attributes, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, admindb); - rc = (*proxy_sqlite3_bind_text)(statement1, 11, ad->comment, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, admindb); + rc = (*proxy_sqlite3_bind_int64)(statement1, 5, ad->transaction_persistent); ASSERT_SQLITE_OK(rc, admindb); + rc = (*proxy_sqlite3_bind_int64)(statement1, 6, ad->fast_forward); ASSERT_SQLITE_OK(rc, admindb); + rc = (*proxy_sqlite3_bind_int64)(statement1, 7, ad->max_connections); ASSERT_SQLITE_OK(rc, admindb); + rc = (*proxy_sqlite3_bind_text)(statement1, 8, ad->attributes, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, admindb); + rc = (*proxy_sqlite3_bind_text)(statement1, 9, ad->comment, -1, SQLITE_TRANSIENT); ASSERT_SQLITE_OK(rc, admindb); SAFE_SQLITE3_STEP2(statement1); rc = (*proxy_sqlite3_clear_bindings)(statement1); ASSERT_SQLITE_OK(rc, admindb); rc = (*proxy_sqlite3_reset)(statement1); ASSERT_SQLITE_OK(rc, admindb); } free(ad->username); free(ad->password); // this is not initialized with dump_all_users( , false) - free(ad->default_schema); // this is not initialized with dump_all_users( , false) free(ad->comment); free(ad->attributes); free(ad); diff --git a/lib/ProxySQL_Config.cpp b/lib/ProxySQL_Config.cpp index aea8f2c14..a89125a4c 100644 --- a/lib/ProxySQL_Config.cpp +++ b/lib/ProxySQL_Config.cpp @@ -1633,15 +1633,13 @@ int ProxySQL_Config::Write_PgSQL_Users_to_configfile(std::string& data) { addField(data, "active", r->fields[2], ""); addField(data, "use_ssl", r->fields[3], ""); addField(data, "default_hostgroup", r->fields[4], ""); - addField(data, "default_schema", r->fields[5]); - addField(data, "schema_locked", r->fields[6], ""); - addField(data, "transaction_persistent", r->fields[7], ""); - addField(data, "fast_forward", r->fields[8], ""); - addField(data, "backend", r->fields[9], ""); - addField(data, "frontend", r->fields[10], ""); - addField(data, "max_connections", r->fields[11], ""); - addField(data, "attributes", r->fields[12]); - addField(data, "comment", r->fields[13]); + addField(data, "transaction_persistent", r->fields[5], ""); + addField(data, "fast_forward", r->fields[6], ""); + addField(data, "backend", r->fields[7], ""); + addField(data, "frontend", r->fields[8], ""); + addField(data, "max_connections", r->fields[9], ""); + addField(data, "attributes", r->fields[10]); + addField(data, "comment", r->fields[11]); data += "\t}"; isNext = true; } @@ -1664,7 +1662,7 @@ int ProxySQL_Config::Read_PgSQL_Users_from_configfile() { int i; int rows = 0; admindb->execute("PRAGMA foreign_keys = OFF"); - char* q = (char*)"INSERT OR REPLACE INTO pgsql_users (username, password, active, use_ssl, default_hostgroup, default_schema, schema_locked, transaction_persistent, fast_forward, max_connections, attributes, comment) VALUES ('%s', '%s', %d, %d, %d, '%s', %d, %d, %d, %d, '%s','%s')"; + char* q = (char*)"INSERT OR REPLACE INTO pgsql_users (username, password, active, use_ssl, default_hostgroup, transaction_persistent, fast_forward, max_connections, attributes, comment) VALUES ('%s', '%s', %d, %d, %d, %d, %d, %d, '%s','%s')"; for (i = 0; i < count; i++) { const Setting& user = pgsql_users[i]; std::string username; @@ -1672,8 +1670,6 @@ int ProxySQL_Config::Read_PgSQL_Users_from_configfile() { int active = 1; int use_ssl = 0; int default_hostgroup = 0; - std::string default_schema = ""; - int schema_locked = 0; int transaction_persistent = 1; int fast_forward = 0; int max_connections = 10000; @@ -1688,8 +1684,6 @@ int ProxySQL_Config::Read_PgSQL_Users_from_configfile() { user.lookupValue("active", active); user.lookupValue("use_ssl", use_ssl); //if (user.lookupValue("default_schema", default_schema)==false) default_schema=""; - user.lookupValue("default_schema", default_schema); - user.lookupValue("schema_locked", schema_locked); user.lookupValue("transaction_persistent", transaction_persistent); user.lookupValue("fast_forward", fast_forward); user.lookupValue("max_connections", max_connections); @@ -1698,7 +1692,7 @@ int ProxySQL_Config::Read_PgSQL_Users_from_configfile() { char* o1 = strdup(comment.c_str()); char* o = escape_string_single_quotes(o1, false); char* query = (char*)malloc(strlen(q) + strlen(username.c_str()) + strlen(password.c_str()) + strlen(o) + strlen(attributes.c_str()) + 128); - sprintf(query, q, username.c_str(), password.c_str(), active, use_ssl, default_hostgroup, default_schema.c_str(), schema_locked, transaction_persistent, fast_forward, max_connections, attributes.c_str(), o); + sprintf(query, q, username.c_str(), password.c_str(), active, use_ssl, default_hostgroup, transaction_persistent, fast_forward, max_connections, attributes.c_str(), o); admindb->execute(query); if (o != o1) free(o); free(o1);