From 06369249b4449c1faaa40245ec818e45d59ff508 Mon Sep 17 00:00:00 2001 From: Shard Gupta <40919925+shardgupta@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:37:48 +0530 Subject: [PATCH] =?UTF-8?q?Revert=20"Modify=20get=5F*=5Frole=5Fname()=20an?= =?UTF-8?q?d=20get=5F*=5Fschema=5Fname()=20APIs=20to=20always=20retur?= =?UTF-8?q?=E2=80=A6"=20(#2955)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contrib/babelfishpg_tsql/runtime/functions.c | 46 ++---- contrib/babelfishpg_tsql/src/catalog.c | 58 +------- contrib/babelfishpg_tsql/src/catalog.h | 1 - contrib/babelfishpg_tsql/src/dbcmds.c | 70 ++++----- contrib/babelfishpg_tsql/src/hooks.c | 8 +- contrib/babelfishpg_tsql/src/multidb.c | 146 +++++++++---------- contrib/babelfishpg_tsql/src/multidb.h | 12 +- contrib/babelfishpg_tsql/src/pl_exec-2.c | 52 +++---- contrib/babelfishpg_tsql/src/pl_exec.c | 6 +- contrib/babelfishpg_tsql/src/pl_handler.c | 27 ++-- contrib/babelfishpg_tsql/src/pltsql_utils.c | 2 +- contrib/babelfishpg_tsql/src/procedures.c | 27 ++-- contrib/babelfishpg_tsql/src/rolecmds.c | 56 +++---- contrib/babelfishpg_tsql/src/session.c | 17 +-- 14 files changed, 195 insertions(+), 333 deletions(-) diff --git a/contrib/babelfishpg_tsql/runtime/functions.c b/contrib/babelfishpg_tsql/runtime/functions.c index 93ad146a40..7fca673c2f 100644 --- a/contrib/babelfishpg_tsql/runtime/functions.c +++ b/contrib/babelfishpg_tsql/runtime/functions.c @@ -1305,15 +1305,11 @@ schema_id(PG_FUNCTION_ARGS) { char *db_name = get_cur_db_name(); const char *user = get_user_for_database(db_name); - char *guest_role_name = get_guest_role_name(db_name); + const char *guest_role_name = get_guest_role_name(db_name); if (!user) { pfree(db_name); - - if(guest_role_name) - pfree(guest_role_name); - PG_RETURN_NULL(); } else if ((guest_role_name && strcmp(user, guest_role_name) == 0)) @@ -1326,9 +1322,6 @@ schema_id(PG_FUNCTION_ARGS) physical_name = get_physical_schema_name(db_name, name); } pfree(db_name); - - if(guest_role_name) - pfree(guest_role_name); } else { @@ -2233,19 +2226,15 @@ object_id(PG_FUNCTION_ARGS) * name */ const char *user = get_user_for_database(db_name); - char *guest_role_name = get_guest_role_name(db_name); + const char *guest_role_name = get_guest_role_name(db_name); if (!user) { pfree(db_name); pfree(schema_name); pfree(object_name); - - if(guest_role_name) - pfree(guest_role_name); if (object_type) pfree(object_type); - PG_RETURN_NULL(); } else if ((guest_role_name && strcmp(user, guest_role_name) == 0)) @@ -2258,9 +2247,6 @@ object_id(PG_FUNCTION_ARGS) schema_name = get_authid_user_ext_schema_name((const char *) db_name, user); physical_schema_name = get_physical_schema_name(db_name, schema_name); } - - if(guest_role_name) - pfree(guest_role_name); } else { @@ -2693,18 +2679,14 @@ type_id(PG_FUNCTION_ARGS) if (!OidIsValid(result)) { /* find the default schema for current user and get physical schema name */ - const char *user = get_user_for_database(db_name); - char *guest_role_name = get_guest_role_name(db_name); + const char *user = get_user_for_database(db_name); + const char *guest_role_name = get_guest_role_name(db_name); if (!user) { pfree(db_name); pfree(schema_name); pfree(object_name); - - if(guest_role_name) - pfree(guest_role_name); - PG_RETURN_NULL(); } else if ((guest_role_name && strcmp(user, guest_role_name) == 0)) @@ -2717,9 +2699,6 @@ type_id(PG_FUNCTION_ARGS) schema_name = get_authid_user_ext_schema_name((const char *) db_name, user); physical_schema_name = get_physical_schema_name(db_name, schema_name); } - - if(guest_role_name) - pfree(guest_role_name); } else { @@ -2849,20 +2828,20 @@ replace_special_chars_fts(PG_FUNCTION_ARGS) Datum has_dbaccess(PG_FUNCTION_ARGS) { - char *db_name = text_to_cstring(PG_GETARG_TEXT_P(0)); + char *db_name = text_to_cstring(PG_GETARG_TEXT_P(0)); /* * Ensure the database name input argument is lower-case, as all Babel * table names are lower-case */ - char *lowercase_db_name = lowerstr(db_name); + char *lowercase_db_name = lowerstr(db_name); /* Also strip trailing whitespace to mimic SQL Server behaviour */ - int i; - char *user = NULL; - const char *login; - int16 db_id; - bool login_is_db_owner; + int i; + const char *user = NULL; + const char *login; + int16 db_id; + bool login_is_db_owner; i = strlen(lowercase_db_name); while (i > 0 && isspace((unsigned char) lowercase_db_name[i - 1])) @@ -2908,10 +2887,7 @@ has_dbaccess(PG_FUNCTION_ARGS) if (!user) PG_RETURN_INT32(0); else - { - pfree(user); PG_RETURN_INT32(1); - } } Datum diff --git a/contrib/babelfishpg_tsql/src/catalog.c b/contrib/babelfishpg_tsql/src/catalog.c index 88772a950d..1c2db7f299 100644 --- a/contrib/babelfishpg_tsql/src/catalog.c +++ b/contrib/babelfishpg_tsql/src/catalog.c @@ -3041,7 +3041,7 @@ update_user_catalog_for_guest(PG_FUNCTION_ARGS) bool guest_role_exists_for_db(const char *dbname) { - char *guest_role = get_guest_role_name(dbname); + const char *guest_role = get_guest_role_name(dbname); bool role_exists = false; HeapTuple tuple; @@ -3053,9 +3053,6 @@ guest_role_exists_for_db(const char *dbname) ReleaseSysCache(tuple); } - if(guest_role) - pfree(guest_role); - return role_exists; } @@ -3112,7 +3109,7 @@ get_login_for_user(Oid user_id, const char *physical_schema_name) static void create_guest_role_for_db(const char *dbname) { - char *guest = get_guest_role_name(dbname); + const char *guest = get_guest_role_name(dbname); const char *db_owner_role = get_db_owner_role_name(dbname); List *logins = NIL; List *res; @@ -3196,9 +3193,6 @@ create_guest_role_for_db(const char *dbname) SetConfigOption("createrole_self_grant", old_createrole_self_grant, PGC_USERSET, PGC_S_OVERRIDE); SetUserIdAndSecContext(save_userid, save_sec_context); set_cur_db(old_dbid, old_dbname); - - if(guest) - pfree(guest); } PG_END_TRY(); } @@ -4665,7 +4659,7 @@ update_babelfish_authid_user_ext_rename_db( Anum_bbf_authid_user_ext_orig_username, bbf_authid_user_ext_dsc, &isNull)); NameData rolename_namedata; - namestrcpy(&rolename_namedata, get_physical_user_name((char *)new_db_name, role_name, true, true)); + namestrcpy(&rolename_namedata, get_physical_user_name((char *)new_db_name, role_name, true)); list_of_roles_to_rename = lappend(list_of_roles_to_rename, pstrdup(role_name)); /* update rolname */ @@ -4944,8 +4938,8 @@ rename_tsql_db(char *old_db_name, char *new_db_name) (strlen(role) == 8 && strncmp(role, "db_owner", 8) == 0))) continue; - old_role_name = get_physical_user_name(old_db_name, role, true, true); - new_role_name = get_physical_user_name(new_db_name, role, true, true); + old_role_name = get_physical_user_name(old_db_name, role, true); + new_role_name = get_physical_user_name(new_db_name, role, true); exec_rename_db_util(old_role_name, new_role_name, false); } @@ -4983,48 +4977,6 @@ rename_tsql_db(char *old_db_name, char *new_db_name) CommitTransactionCommand(); } -/* - * user_exists_for_db - * returns true if the user/role exists in the sys.babelfish_authid_user_ext catalog, - * false otherwise. - */ -bool -user_exists_for_db(const char *db_name, const char *user_name) -{ - Relation bbf_authid_user_ext_rel; - HeapTuple tuple_user_ext; - ScanKeyData key[2]; - TableScanDesc scan; - NameData *rolname; - bool user_exists = false; - - bbf_authid_user_ext_rel = table_open(get_authid_user_ext_oid(), - RowExclusiveLock); - rolname = (NameData *) palloc0(NAMEDATALEN); - snprintf(rolname->data, NAMEDATALEN, "%s", user_name); - ScanKeyInit(&key[0], - Anum_bbf_authid_user_ext_rolname, - BTEqualStrategyNumber, F_NAMEEQ, - NameGetDatum(rolname)); - ScanKeyInit(&key[1], - Anum_bbf_authid_user_ext_database_name, - BTEqualStrategyNumber, F_TEXTEQ, - CStringGetTextDatum(db_name)); - - scan = table_beginscan_catalog(bbf_authid_user_ext_rel, 2, key); - - tuple_user_ext = heap_getnext(scan, ForwardScanDirection); - - if (HeapTupleIsValid(tuple_user_ext)) - { - user_exists = true; - } - - table_endscan(scan); - table_close(bbf_authid_user_ext_rel, RowExclusiveLock); - return user_exists; -} - /* * partition_function_id_exists * Returns true if provided function id is in use, false otherwise. diff --git a/contrib/babelfishpg_tsql/src/catalog.h b/contrib/babelfishpg_tsql/src/catalog.h index ff0248c9fb..2125582eec 100644 --- a/contrib/babelfishpg_tsql/src/catalog.h +++ b/contrib/babelfishpg_tsql/src/catalog.h @@ -162,7 +162,6 @@ extern List *update_babelfish_namespace_ext_nsp_name(int16 db_id, char *new_db_n extern List *update_babelfish_authid_user_ext_db_name(const char *old_db_name, const char *new_db_name); extern void rename_tsql_db(char *old_db_name, char *new_db_name); extern Oid get_login_for_user(Oid user_id, const char *physical_schema_name); -extern bool user_exists_for_db(const char *db_name, const char *user_name); /* MUST comply with babelfish_authid_user_ext table */ typedef struct FormData_authid_user_ext diff --git a/contrib/babelfishpg_tsql/src/dbcmds.c b/contrib/babelfishpg_tsql/src/dbcmds.c index e0ccfa321d..2e8a5533e2 100644 --- a/contrib/babelfishpg_tsql/src/dbcmds.c +++ b/contrib/babelfishpg_tsql/src/dbcmds.c @@ -431,25 +431,25 @@ check_database_collation_name(const char *database_collation_name) static void create_bbf_db_internal(ParseState *pstate, const char *dbname, List *options, const char *owner, int16 dbid) { - int16 old_dbid; - char *old_dbname; - Oid datdba; - Datum *new_record; - bool *new_record_nulls; - Relation sysdatabase_rel; - HeapTuple tuple; - List *parsetree_list; - ListCell *parsetree_item; - char *dbo_role = NULL; - NameData default_collation; - NameData owner_namedata; - int stmt_number = 0; - int save_sec_context; - bool is_set_userid = false; - Oid save_userid; - const char *old_createrole_self_grant; - ListCell *option; - const char *database_collation_name = NULL; + int16 old_dbid; + char *old_dbname; + Oid datdba; + Datum *new_record; + bool *new_record_nulls; + Relation sysdatabase_rel; + HeapTuple tuple; + List *parsetree_list; + ListCell *parsetree_item; + const char *dbo_role; + NameData default_collation; + NameData owner_namedata; + int stmt_number = 0; + int save_sec_context; + bool is_set_userid = false; + Oid save_userid; + const char *old_createrole_self_grant; + ListCell *option; + const char *database_collation_name = NULL; /* Check options */ foreach(option, options) @@ -608,9 +608,6 @@ create_bbf_db_internal(ParseState *pstate, const char *dbname, List *options, co } set_cur_db(old_dbid, old_dbname); add_fixed_user_roles_to_bbf_authid_user_ext(dbname); - - if(dbo_role) - pfree(dbo_role); } PG_FINALLY(); { @@ -625,18 +622,18 @@ create_bbf_db_internal(ParseState *pstate, const char *dbname, List *options, co void drop_bbf_db(const char *dbname, bool missing_ok, bool force_drop) { - volatile Relation sysdatabase_rel; - HeapTuple tuple; - Form_sysdatabases bbf_db; - int16 dbid; - char *dbo_role = NULL; - List *db_users_list; - List *parsetree_list; - ListCell *parsetree_item; - const char *prev_current_user; - int save_sec_context; - bool is_set_userid = false; - Oid save_userid; + volatile Relation sysdatabase_rel; + HeapTuple tuple; + Form_sysdatabases bbf_db; + int16 dbid; + const char *dbo_role; + List *db_users_list; + List *parsetree_list; + ListCell *parsetree_item; + const char *prev_current_user; + int save_sec_context; + bool is_set_userid = false; + Oid save_userid; if ((strlen(dbname) == 6 && (strncmp(dbname, "master", 6) == 0)) || ((strlen(dbname) == 6 && strncmp(dbname, "tempdb", 6) == 0)) || @@ -792,9 +789,6 @@ drop_bbf_db(const char *dbname, bool missing_ok, bool force_drop) } PG_END_TRY(); - if(dbo_role) - pfree(dbo_role); - /* Set current user back to previous user */ bbf_set_current_user(prev_current_user); } @@ -1076,7 +1070,7 @@ create_schema_if_not_exists(const uint16 dbid, * some reason guest role does not exist, then that is a bigger problem. * We skip creating the guest schema entirely instead of crashing though. */ - phys_role = get_physical_user_name((char *) dbname, (char *) owner_role, false, true); + phys_role = get_physical_user_name((char *) dbname, (char *) owner_role, false); if (!OidIsValid(get_role_oid(phys_role, true))) { ereport(LOG, diff --git a/contrib/babelfishpg_tsql/src/hooks.c b/contrib/babelfishpg_tsql/src/hooks.c index d6f1402e56..2d8561e5a9 100644 --- a/contrib/babelfishpg_tsql/src/hooks.c +++ b/contrib/babelfishpg_tsql/src/hooks.c @@ -5023,8 +5023,8 @@ get_local_schema_for_bbf_functions(Oid proc_nsp_oid) HeapTuple tuple; char *func_schema_name = NULL, *new_search_path = NULL; - char *func_dbo_schema = NULL; - const char *cur_dbname = get_cur_db_name(); + const char *func_dbo_schema, + *cur_dbname = get_cur_db_name(); tuple = SearchSysCache1(NAMESPACEOID, ObjectIdGetDatum(proc_nsp_oid)); @@ -5040,11 +5040,7 @@ get_local_schema_for_bbf_functions(Oid proc_nsp_oid) quote_identifier(func_dbo_schema)); ReleaseSysCache(tuple); - - if(func_dbo_schema) - pfree(func_dbo_schema); } - return new_search_path; } diff --git a/contrib/babelfishpg_tsql/src/multidb.c b/contrib/babelfishpg_tsql/src/multidb.c index 7ba241976f..033f2627f4 100644 --- a/contrib/babelfishpg_tsql/src/multidb.c +++ b/contrib/babelfishpg_tsql/src/multidb.c @@ -254,7 +254,7 @@ rewrite_object_refs(Node *stmt) * Try to get physical granted role name, see if it's an * existing db role */ - physical_role_name = get_physical_user_name(db_name, role_name, false, true); + physical_role_name = get_physical_user_name(db_name, role_name, false); if (get_role_oid(physical_role_name, true) == InvalidOid) break; @@ -273,7 +273,7 @@ rewrite_object_refs(Node *stmt) pfree(granted->priv_name); granted->priv_name = physical_role_name; - physical_principal_name = get_physical_user_name(db_name, principal_name, false, true); + physical_principal_name = get_physical_user_name(db_name, principal_name, false); pfree(grantee->rolename); grantee->rolename = physical_principal_name; @@ -343,7 +343,7 @@ rewrite_object_refs(Node *stmt) char *user_name; char *db_name = get_cur_db_name(); - user_name = get_physical_user_name(db_name, create_role->role, false, true); + user_name = get_physical_user_name(db_name, create_role->role, false); pfree(create_role->role); create_role->role = user_name; @@ -394,7 +394,7 @@ rewrite_object_refs(Node *stmt) (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Cannot alter the user %s", user_name))); - physical_user_name = get_physical_user_name(db_name, user_name, false, false); + physical_user_name = get_physical_user_name(db_name, user_name, false); pfree(alter_role->role->rolename); alter_role->role->rolename = physical_user_name; } @@ -1035,7 +1035,7 @@ rewrite_role_name(RoleSpec *role) { char *cur_db = get_cur_db_name(); - role->rolename = get_physical_user_name(cur_db, role->rolename, false, false); + role->rolename = get_physical_user_name(cur_db, role->rolename, false); } bool @@ -1271,7 +1271,7 @@ get_physical_schema_name(char *db_name, const char *schema_name) * Map the logical user name to its physical name in the database. */ char * -get_physical_user_name(char *db_name, char *user_name, bool suppress_db_error, bool suppress_role_error) +get_physical_user_name(char *db_name, char *user_name, bool suppress_error) { char *new_user_name; char *result; @@ -1284,7 +1284,7 @@ get_physical_user_name(char *db_name, char *user_name, bool suppress_db_error, b if (len == 0) return NULL; - if (!DbidIsValid(get_db_id(db_name)) && !suppress_db_error) + if (!DbidIsValid(get_db_id(db_name)) && !suppress_error) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database \"%s\" does not exist.", db_name))); @@ -1309,9 +1309,8 @@ get_physical_user_name(char *db_name, char *user_name, bool suppress_db_error, b (strlen(db_name) != 6 || (strncmp(db_name, "tempdb", 6) != 0)) && (strlen(db_name) != 4 || (strncmp(db_name, "msdb", 4) != 0))) { - if (((strlen(user_name) == 3 && strncmp(user_name, "dbo", 3) == 0) || - (strlen(user_name) == 8 && strncmp(user_name, "db_owner", 8) == 0)) - && (suppress_role_error || user_exists_for_db(db_name, new_user_name))) + if ((strlen(user_name) == 3 && strncmp(user_name, "dbo", 3) == 0) || + (strlen(user_name) == 8 && strncmp(user_name, "db_owner", 8) == 0)) { return new_user_name; } @@ -1325,117 +1324,116 @@ get_physical_user_name(char *db_name, char *user_name, bool suppress_db_error, b /* Truncate final result to 64 bytes */ truncate_tsql_identifier(result); - /* - * If the user or role is not found in the sys.babelfish_authid_user_ext - * catalog, then an error is thrown. The 'suppress_role_error' flag indicates if - * it is ok for the user or role to be absent from the catalog. - */ - if(!suppress_role_error && !user_exists_for_db(db_name, result)) - { - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("User or role \"%s\" does not exist", new_user_name))); - } - return result; } -char * +const char * get_dbo_schema_name(const char *dbname) { - char *name = palloc0(MAX_BBF_NAMEDATALEND); - - if ((0 == strcmp(dbname, "master")) || (0 == strcmp(dbname, "tempdb")) || (0 == strcmp(dbname, "msdb"))) - { - snprintf(name, MAX_BBF_NAMEDATALEND, "%s_dbo", dbname); - } - else if (SINGLE_DB == get_migration_mode()) - { - snprintf(name, MAX_BBF_NAMEDATALEND, "%s", "dbo"); - } + if (0 == strcmp(dbname, "master")) + return "master_dbo"; + if (0 == strcmp(dbname, "tempdb")) + return "tempdb_dbo"; + if (0 == strcmp(dbname, "msdb")) + return "msdb_dbo"; + if (SINGLE_DB == get_migration_mode()) + return "dbo"; else { + char *name = palloc0(MAX_BBF_NAMEDATALEND); + snprintf(name, MAX_BBF_NAMEDATALEND, "%s_dbo", dbname); truncate_identifier(name, strlen(name), false); + return name; } - return name; } -char * +const char * get_dbo_role_name(const char *dbname) { - char *name = palloc0(MAX_BBF_NAMEDATALEND); - - if ((0 == strcmp(dbname, "master")) || (0 == strcmp(dbname, "tempdb")) || (0 == strcmp(dbname, "msdb"))) - { - snprintf(name, MAX_BBF_NAMEDATALEND, "%s_dbo", dbname); - } - else if (SINGLE_DB == get_migration_mode()) - { - snprintf(name, MAX_BBF_NAMEDATALEND, "%s", "dbo"); - } + if (0 == strcmp(dbname, "master")) + return "master_dbo"; + if (0 == strcmp(dbname, "tempdb")) + return "tempdb_dbo"; + if (0 == strcmp(dbname, "msdb")) + return "msdb_dbo"; + if (SINGLE_DB == get_migration_mode()) + return "dbo"; else { + char *name = palloc0(MAX_BBF_NAMEDATALEND); + snprintf(name, MAX_BBF_NAMEDATALEND, "%s_dbo", dbname); truncate_identifier(name, strlen(name), false); + return name; } - return name; } -char * +const char * get_db_owner_name(const char *dbname) { - char *name = palloc0(MAX_BBF_NAMEDATALEND); - - if ((0 == strcmp(dbname, "master")) || (0 == strcmp(dbname, "tempdb")) || (0 == strcmp(dbname, "msdb"))) - { - snprintf(name, MAX_BBF_NAMEDATALEND, "%s_db_owner", dbname); - } - else if (SINGLE_DB == get_migration_mode()) - { - snprintf(name, MAX_BBF_NAMEDATALEND, "%s", "db_owner"); - } + if (0 == strcmp(dbname, "master")) + return "master_db_owner"; + if (0 == strcmp(dbname, "tempdb")) + return "tempdb_db_owner"; + if (0 == strcmp(dbname, "msdb")) + return "msdb_db_owner"; + if (SINGLE_DB == get_migration_mode()) + return "db_owner"; else { + char *name = palloc0(MAX_BBF_NAMEDATALEND); + snprintf(name, MAX_BBF_NAMEDATALEND, "%s_db_owner", dbname); truncate_identifier(name, strlen(name), false); + return name; } - return name; } -char * +const char * get_guest_role_name(const char *dbname) { - char *name = palloc0(MAX_BBF_NAMEDATALEND); + if (0 == strcmp(dbname, "master")) + return "master_guest"; + if (0 == strcmp(dbname, "tempdb")) + return "tempdb_guest"; + if (0 == strcmp(dbname, "msdb")) + return "msdb_guest"; /* * Always prefix with dbname regardless if single or multidb. Note that * dbo is an exception. */ - snprintf(name, MAX_BBF_NAMEDATALEND, "%s_guest", dbname); - truncate_identifier(name, strlen(name), false); - return name; + else + { + char *name = palloc0(MAX_BBF_NAMEDATALEND); + + snprintf(name, MAX_BBF_NAMEDATALEND, "%s_guest", dbname); + truncate_identifier(name, strlen(name), false); + return name; + } } -char * +const char * get_guest_schema_name(const char *dbname) { - char *name = palloc0(MAX_BBF_NAMEDATALEND); + if (0 == strcmp(dbname, "master")) + return "master_guest"; + if (0 == strcmp(dbname, "tempdb")) + return "tempdb_guest"; + if (0 == strcmp(dbname, "msdb")) + return "msdb_guest"; - if ((0 == strcmp(dbname, "master")) || (0 == strcmp(dbname, "tempdb")) || (0 == strcmp(dbname, "msdb"))) - { - snprintf(name, MAX_BBF_NAMEDATALEND, "%s_guest", dbname); - } - else if (SINGLE_DB == get_migration_mode()) - { - snprintf(name, MAX_BBF_NAMEDATALEND, "%s", "guest"); - } + if (SINGLE_DB == get_migration_mode()) + return "guest"; else { + char *name = palloc0(MAX_BBF_NAMEDATALEND); + snprintf(name, MAX_BBF_NAMEDATALEND, "%s_guest", dbname); truncate_identifier(name, strlen(name), false); + return name; } - return name; } bool diff --git a/contrib/babelfishpg_tsql/src/multidb.h b/contrib/babelfishpg_tsql/src/multidb.h index 3ea0d8b2cf..8312f9ade6 100644 --- a/contrib/babelfishpg_tsql/src/multidb.h +++ b/contrib/babelfishpg_tsql/src/multidb.h @@ -17,14 +17,14 @@ extern void rewrite_object_refs(Node *stmt); extern List* rewrite_plain_name(List *name); /* Value Strings */ /* helper functions */ -extern char *get_physical_user_name(char *db_name, char *user_name, bool suppress_error, bool missing_ok); +extern char *get_physical_user_name(char *db_name, char *user_name, bool suppress_error); extern char *get_physical_schema_name(char *db_name, const char *schema_name); extern char *get_physical_schema_name_by_mode(char *db_name, const char *schema_name, MigrationMode mode); -extern char *get_dbo_schema_name(const char *dbname); -extern char *get_dbo_role_name(const char *dbname); -extern char *get_db_owner_name(const char *dbname); -extern char *get_guest_role_name(const char *dbname); -extern char *get_guest_schema_name(const char *dbname); +extern const char *get_dbo_schema_name(const char *dbname); +extern const char *get_dbo_role_name(const char *dbname); +extern const char *get_db_owner_name(const char *dbname); +extern const char *get_guest_role_name(const char *dbname); +extern const char *get_guest_schema_name(const char *dbname); extern bool is_shared_schema(const char *name); extern void truncate_tsql_identifier(char *ident); extern bool physical_schema_name_exists(char *phys_schema_name); diff --git a/contrib/babelfishpg_tsql/src/pl_exec-2.c b/contrib/babelfishpg_tsql/src/pl_exec-2.c index 31cd7fee99..32ff3a3139 100644 --- a/contrib/babelfishpg_tsql/src/pl_exec-2.c +++ b/contrib/babelfishpg_tsql/src/pl_exec-2.c @@ -3207,31 +3207,31 @@ void exec_stmt_dbcc_checkident(PLtsql_stmt_dbcc *stmt) struct dbcc_checkident dbcc_stmt = stmt->dbcc_stmt_data.dbcc_checkident; Relation rel; TupleDesc tupdesc; - char *db_name = NULL; - char *max_identity_value_str = NULL; - char *query = NULL; - char *attname; - char *token; + char *db_name = NULL; + char *max_identity_value_str = NULL; + char *query = NULL; + char *attname; + char *token; const char *schema_name; - char *nsp_name = NULL; + const char *nsp_name; const char *user; - char *guest_role_name = NULL; - char *dbo_role_name = NULL; + const char *guest_role_name; + const char *dbo_role_name; const char *login; - int64 max_identity_value = 0; - int64 cur_identity_value = 0; - int attnum; - int rc = 0; - int64 reseed_value = 0; - Oid nsp_oid; - Oid table_oid; - Oid seqid = InvalidOid; - Oid current_user_id = GetUserId(); - volatile bool cur_value_is_null = true; - bool login_is_db_owner; + int64 max_identity_value = 0; + int64 cur_identity_value = 0; + int attnum; + int rc = 0; + int64 reseed_value = 0; + Oid nsp_oid; + Oid table_oid; + Oid seqid = InvalidOid; + Oid current_user_id = GetUserId(); + volatile bool cur_value_is_null = true; + bool login_is_db_owner; StringInfoData msg; - bool is_float_value; - bool is_cross_db = false; + bool is_float_value; + bool is_cross_db = false; if(dbcc_stmt.new_reseed_value) @@ -3327,11 +3327,6 @@ void exec_stmt_dbcc_checkident(PLtsql_stmt_dbcc *stmt) } pfree(db_name); - if(guest_role_name) - pfree(guest_role_name); - if(dbo_role_name) - pfree(dbo_role_name); - /* * get schema oid from physical schema name, it will return InvalidOid if * user don't have lookup access @@ -3384,9 +3379,6 @@ void exec_stmt_dbcc_checkident(PLtsql_stmt_dbcc *stmt) errmsg("'%s.%s' does not contain an identity column.", nsp_name, dbcc_stmt.table_name))); } - - if(nsp_name) - pfree(nsp_name); PG_TRY(); { @@ -3763,7 +3755,7 @@ exec_stmt_grantschema(PLtsql_execstate *estate, PLtsql_stmt_grantschema *stmt) Oid role_oid; bool is_public = 0 == strcmp(grantee_name, PUBLIC_ROLE_NAME); if (!is_public) - rolname = get_physical_user_name(dbname, grantee_name, false, true); + rolname = get_physical_user_name(dbname, grantee_name, false); else rolname = pstrdup(PUBLIC_ROLE_NAME); role_oid = get_role_oid(rolname, true); diff --git a/contrib/babelfishpg_tsql/src/pl_exec.c b/contrib/babelfishpg_tsql/src/pl_exec.c index 0e4ae16c80..e23ad310ea 100644 --- a/contrib/babelfishpg_tsql/src/pl_exec.c +++ b/contrib/babelfishpg_tsql/src/pl_exec.c @@ -10227,7 +10227,7 @@ reset_search_path(PLtsql_stmt_execsql *stmt, char **old_search_path, bool *reset char *cur_dbname = get_cur_db_name(); char *new_search_path; char *physical_schema; - char *dbo_schema = NULL; + const char *dbo_schema; top_es_entry = exec_state_call_stack->next; @@ -10378,10 +10378,6 @@ reset_search_path(PLtsql_stmt_execsql *stmt, char **old_search_path, bool *reset } pfree(cur_dbname); - - if(dbo_schema) - pfree(dbo_schema); - return false; } diff --git a/contrib/babelfishpg_tsql/src/pl_handler.c b/contrib/babelfishpg_tsql/src/pl_handler.c index 26d092bf14..cd294408cc 100644 --- a/contrib/babelfishpg_tsql/src/pl_handler.c +++ b/contrib/babelfishpg_tsql/src/pl_handler.c @@ -2924,7 +2924,7 @@ bbf_ProcessUtility(PlannedStmt *pstmt, } else if (isuser || isrole) { - char *db_owner_name; + const char *db_owner_name; db_owner_name = get_db_owner_name(get_cur_db_name()); if (!has_privs_of_role(GetUserId(),get_role_oid(db_owner_name, false))) @@ -3202,7 +3202,7 @@ bbf_ProcessUtility(PlannedStmt *pstmt, } else if (isuser || isrole) { - char *dbo_name; + const char *dbo_name; char *db_name; char *user_name; char *cur_user; @@ -3283,9 +3283,6 @@ bbf_ProcessUtility(PlannedStmt *pstmt, set_session_properties(db_name); pfree(cur_user); pfree(db_name); - - if(dbo_name) - pfree(dbo_name); return; } @@ -3334,17 +3331,17 @@ bbf_ProcessUtility(PlannedStmt *pstmt, { foreach(item, stmt->roles) { - RoleSpec *rolspec = lfirst(item); - char *user_name; - const char *db_principal_type = drop_user ? "user" : "role"; - char *db_owner_name; - int role_oid; - int rolename_len; + RoleSpec *rolspec = lfirst(item); + char *user_name; + const char *db_principal_type = drop_user ? "user" : "role"; + const char *db_owner_name; + int role_oid; + int rolename_len; bool is_tsql_db_principal = false; bool is_psql_db_principal = false; - Oid dbowner; + Oid dbowner; - user_name = get_physical_user_name(db_name, rolspec->rolename, false, true); + user_name = get_physical_user_name(db_name, rolspec->rolename, false); db_owner_name = get_db_owner_name(db_name); dbowner = get_role_oid(db_owner_name, false); role_oid = get_role_oid(user_name, true); @@ -3410,10 +3407,6 @@ bbf_ProcessUtility(PlannedStmt *pstmt, } pfree(rolspec->rolename); - - if(db_owner_name) - pfree(db_owner_name); - rolspec->rolename = user_name; } } diff --git a/contrib/babelfishpg_tsql/src/pltsql_utils.c b/contrib/babelfishpg_tsql/src/pltsql_utils.c index ccb5440b44..53dfc7ccf5 100644 --- a/contrib/babelfishpg_tsql/src/pltsql_utils.c +++ b/contrib/babelfishpg_tsql/src/pltsql_utils.c @@ -46,7 +46,7 @@ extern bool babelfish_dump_restore; extern char *get_cur_db_name(void); extern char *construct_unique_index_name(char *index_name, char *relation_name); extern char *get_physical_schema_name(char *db_name, const char *schema_name); -extern char *get_dbo_schema_name(const char *dbname); +extern const char *get_dbo_schema_name(const char *dbname); PG_FUNCTION_INFO_V1(split_identifier_internal); /* To cache oid of sys.varchar */ diff --git a/contrib/babelfishpg_tsql/src/procedures.c b/contrib/babelfishpg_tsql/src/procedures.c index 5f4297b9ac..27f21c6790 100644 --- a/contrib/babelfishpg_tsql/src/procedures.c +++ b/contrib/babelfishpg_tsql/src/procedures.c @@ -1704,16 +1704,13 @@ create_xp_qv_in_master_dbo_internal(PG_FUNCTION_ARGS) char *tempq = "CREATE OR REPLACE PROCEDURE %s.xp_qv(IN SYS.NVARCHAR(256), IN SYS.NVARCHAR(256))" "AS \'babelfishpg_tsql\', \'xp_qv_internal\' LANGUAGE C"; - char *dbo_scm = get_dbo_schema_name("master"); + const char *dbo_scm = get_dbo_schema_name("master"); if (dbo_scm == NULL) elog(ERROR, "Failed to retrieve dbo schema name"); query = psprintf(tempq, dbo_scm); - if(dbo_scm) - pfree(dbo_scm); - PG_TRY(); { if ((rc = SPI_connect()) != SPI_OK_CONNECT) @@ -1797,7 +1794,7 @@ create_xp_instance_regread_in_master_dbo_internal(PG_FUNCTION_ARGS) char *tempq2 = "CREATE OR REPLACE PROCEDURE %s.xp_instance_regread(IN p1 sys.nvarchar(512), IN p2 sys.sysname, IN p3 sys.nvarchar(512), INOUT out_param sys.nvarchar(512))" "AS \'babelfishpg_tsql\', \'xp_instance_regread_internal\' LANGUAGE C"; - char *dbo_scm = get_dbo_schema_name("master"); + const char *dbo_scm = get_dbo_schema_name("master"); if (dbo_scm == NULL) elog(ERROR, "Failed to retrieve dbo schema name"); @@ -1805,9 +1802,6 @@ create_xp_instance_regread_in_master_dbo_internal(PG_FUNCTION_ARGS) query = psprintf(tempq, dbo_scm); query2 = psprintf(tempq2, dbo_scm); - if(dbo_scm) - pfree(dbo_scm); - PG_TRY(); { if ((rc = SPI_connect()) != SPI_OK_CONNECT) @@ -2142,7 +2136,7 @@ sp_addrole(PG_FUNCTION_ARGS) errmsg("'%s' is not a valid name because it contains invalid characters.", rolname))); /* Map the logical role name to its physical name in the database. */ - physical_role_name = get_physical_user_name(get_cur_db_name(), lowercase_rolname, false, true); + physical_role_name = get_physical_user_name(get_cur_db_name(), lowercase_rolname, false); role_oid = get_role_oid(physical_role_name, true); /* Check if the user, group or role already exists */ @@ -2285,7 +2279,7 @@ sp_droprole(PG_FUNCTION_ARGS) errmsg("Name cannot be NULL."))); /* Map the logical role name to its physical name in the database. */ - physical_role_name = get_physical_user_name(get_cur_db_name(), lowercase_rolname, false, true); + physical_role_name = get_physical_user_name(get_cur_db_name(), lowercase_rolname, false); role_oid = get_role_oid(physical_role_name, true); /* Check if the role does not exists */ @@ -2435,7 +2429,7 @@ sp_addrolemember(PG_FUNCTION_ARGS) errmsg("Cannot make a role a member of itself."))); /* Map the logical member name to its physical name in the database. */ - physical_member_name = get_physical_user_name(get_cur_db_name(), lowercase_membername, false, true); + physical_member_name = get_physical_user_name(get_cur_db_name(), lowercase_membername, false); member_oid = get_role_oid(physical_member_name, true); /* @@ -2448,7 +2442,7 @@ sp_addrolemember(PG_FUNCTION_ARGS) errmsg("User or role '%s' does not exist in this database.", membername))); /* Map the logical role name to its physical name in the database. */ - physical_role_name = get_physical_user_name(get_cur_db_name(), lowercase_rolname, false, true); + physical_role_name = get_physical_user_name(get_cur_db_name(), lowercase_rolname, false); role_oid = get_role_oid(physical_role_name, true); /* Check if the role does not exists and given role name is an role */ @@ -2600,7 +2594,7 @@ sp_droprolemember(PG_FUNCTION_ARGS) errmsg("Name cannot be NULL."))); /* Map the logical role name to its physical name in the database. */ - physical_name = get_physical_user_name(get_cur_db_name(), lowercase_rolname, false, true); + physical_name = get_physical_user_name(get_cur_db_name(), lowercase_rolname, false); role_oid = get_role_oid(physical_name, true); /* Throw an error id the given role name doesn't exist or isn't a role */ @@ -2610,7 +2604,7 @@ sp_droprolemember(PG_FUNCTION_ARGS) errmsg("Cannot alter the role '%s', because it does not exist or you do not have permission.", rolname))); /* Map the logical member name to its physical name in the database. */ - physical_name = get_physical_user_name(get_cur_db_name(), lowercase_membername, false, true); + physical_name = get_physical_user_name(get_cur_db_name(), lowercase_membername, false); role_oid = get_role_oid(physical_name, true); /* @@ -3363,7 +3357,7 @@ sp_babelfish_volatility(PG_FUNCTION_ARGS) if (!strcmp(logical_schema_name, "")) { const char *user = get_user_for_database(db_name); - char *guest_role_name = get_guest_role_name(db_name); + const char *guest_role_name = get_guest_role_name(db_name); if (!user) ereport(ERROR, @@ -3381,9 +3375,6 @@ sp_babelfish_volatility(PG_FUNCTION_ARGS) physical_schema_name = get_physical_schema_name(db_name, logical_schema_name); pfree(logical_schema_name); } - - if(guest_role_name) - pfree(guest_role_name); } else { diff --git a/contrib/babelfishpg_tsql/src/rolecmds.c b/contrib/babelfishpg_tsql/src/rolecmds.c index a4f0d871ed..9cfbd25e4d 100644 --- a/contrib/babelfishpg_tsql/src/rolecmds.c +++ b/contrib/babelfishpg_tsql/src/rolecmds.c @@ -518,16 +518,13 @@ grant_guests_to_login(const char *login) &is_null); const char *db_name = TextDatumGetCString(db_name_datum); - char *guest_name = NULL; + const char *guest_name = NULL; if (guest_role_exists_for_db(db_name)) guest_name = get_guest_role_name(db_name); if (guest_name) - { guests = lappend(guests, make_accesspriv_node(guest_name)); - pfree(guest_name); - } tuple = heap_getnext(scan, ForwardScanDirection); } @@ -590,7 +587,7 @@ grant_revoke_dbo_to_login(const char* login, const char* db_name, bool is_grant) Node *stmt; PlannedStmt *wrapper; - char *dbo_role_name = get_dbo_role_name(db_name); + const char *dbo_role_name = get_dbo_role_name(db_name); initStringInfo(&query); @@ -642,9 +639,6 @@ grant_revoke_dbo_to_login(const char* login, const char* db_name, bool is_grant) CommandCounterIncrement(); pfree(query.data); - - if(dbo_role_name) - pfree(dbo_role_name); } static List * @@ -823,7 +817,7 @@ user_id(PG_FUNCTION_ARGS) if (!db_name) PG_RETURN_NULL(); - user_name = get_physical_user_name(db_name, user_input, false, true); + user_name = get_physical_user_name(db_name, user_input, false); if (!user_name) PG_RETURN_NULL(); @@ -1317,10 +1311,10 @@ add_existing_users_to_catalog(PG_FUNCTION_ARGS) while (HeapTupleIsValid(tuple)) { Datum db_name_datum; - const char *db_name; - char *dbo_role; - char *db_owner_role; - char *guest; + const char *db_name; + const char *dbo_role; + const char *db_owner_role; + const char *guest; db_name_datum = heap_getattr(tuple, Anum_sysdatabases_name, @@ -1337,13 +1331,9 @@ add_existing_users_to_catalog(PG_FUNCTION_ARGS) { dbo_list = lappend(dbo_list, make_rolespec_node(dbo_role)); add_to_bbf_authid_user_ext(dbo_role, "dbo", db_name, "dbo", NULL, false, true, false); - pfree(dbo_role); } if (db_owner_role) - { add_to_bbf_authid_user_ext(db_owner_role, "db_owner", db_name, NULL, NULL, true, true, false); - pfree(db_owner_role); - } if (guest) { /* @@ -1354,8 +1344,6 @@ add_existing_users_to_catalog(PG_FUNCTION_ARGS) add_to_bbf_authid_user_ext(guest, "guest", db_name, NULL, NULL, false, true, false); else add_to_bbf_authid_user_ext(guest, "guest", db_name, NULL, NULL, false, false, false); - - pfree(guest); } tuple = heap_getnext(scan, ForwardScanDirection); @@ -1523,7 +1511,7 @@ alter_bbf_authid_user_ext(AlterRoleStmt *stmt) /* update user name */ if (new_user_name) { - physical_name = get_physical_user_name(get_cur_db_name(), new_user_name, false, true); + physical_name = get_physical_user_name(get_cur_db_name(), new_user_name, false); namestrcpy(&physical_name_namedata, physical_name); new_record_user_ext[USER_EXT_ROLNAME] = NameGetDatum(&physical_name_namedata); @@ -1873,7 +1861,7 @@ role_id(PG_FUNCTION_ARGS) if (!get_cur_db_name()) PG_RETURN_NULL(); - role_name = get_physical_user_name(get_cur_db_name(), user_input, false, true); + role_name = get_physical_user_name(get_cur_db_name(), user_input, false); result = get_role_oid(role_name, true); @@ -1895,14 +1883,14 @@ is_rolemember(PG_FUNCTION_ARGS) Oid cur_user_oid = GetUserId(); Oid db_owner_oid; Oid dbo_role_oid; - char *role; - char *dc_role; - char *dc_principal = NULL; - char *physical_role_name; - char *physical_principal_name; - char *cur_db_name; - char *db_owner_name; - char *dbo_role_name; + char *role; + char *dc_role; + char *dc_principal = NULL; + char *physical_role_name; + char *physical_principal_name; + char *cur_db_name; + const char *db_owner_name; + const char *dbo_role_name; int idx; if (PG_ARGISNULL(0)) @@ -1914,7 +1902,7 @@ is_rolemember(PG_FUNCTION_ARGS) while (idx > 0 && isspace((unsigned char) role[idx - 1])) role[--idx] = '\0'; dc_role = downcase_identifier(role, strlen(role), false, false); - physical_role_name = get_physical_user_name(get_cur_db_name(), dc_role, false, true); + physical_role_name = get_physical_user_name(get_cur_db_name(), dc_role, false); role_oid = get_role_oid(physical_role_name, true); /* If principal name is NULL, take current user instead */ @@ -1929,7 +1917,7 @@ is_rolemember(PG_FUNCTION_ARGS) while (idx > 0 && isspace((unsigned char) principal[idx - 1])) principal[--idx] = '\0'; dc_principal = downcase_identifier(principal, strlen(principal), false, false); - physical_principal_name = get_physical_user_name(get_cur_db_name(), dc_principal, false, true); + physical_principal_name = get_physical_user_name(get_cur_db_name(), dc_principal, false); principal_oid = get_role_oid(physical_principal_name, true); } @@ -1967,12 +1955,6 @@ is_rolemember(PG_FUNCTION_ARGS) dbo_role_name = get_dbo_role_name(cur_db_name); db_owner_oid = get_role_oid(db_owner_name, false); dbo_role_oid = get_role_oid(dbo_role_name, false); - - if(db_owner_name) - pfree(db_owner_name); - if(dbo_role_name) - pfree(dbo_role_name); - if ((principal_oid == db_owner_oid) || (principal_oid == dbo_role_oid)) PG_RETURN_INT32(0); else if (is_member_of_role_nosuper(principal_oid, role_oid)) diff --git a/contrib/babelfishpg_tsql/src/session.c b/contrib/babelfishpg_tsql/src/session.c index 123cce5a7f..e6f11bcab6 100644 --- a/contrib/babelfishpg_tsql/src/session.c +++ b/contrib/babelfishpg_tsql/src/session.c @@ -165,11 +165,11 @@ set_cur_user_db_and_path(const char *db_name) static void set_search_path_for_user_schema(const char *db_name, const char *user) { - const char *path; - const char *buffer = "%s, \"$user\", sys, pg_catalog"; - char *physical_schema = NULL; - char *dbo_role_name = get_dbo_role_name(db_name); - char *guest_role_name = get_guest_role_name(db_name); + const char *path; + const char *buffer = "%s, \"$user\", sys, pg_catalog"; + const char *physical_schema; + const char *dbo_role_name = get_dbo_role_name(db_name); + const char *guest_role_name = get_guest_role_name(db_name); if ((dbo_role_name && strcmp(user, dbo_role_name) == 0)) { @@ -196,13 +196,6 @@ set_search_path_for_user_schema(const char *db_name, const char *user) path, PGC_SUSET, PGC_S_DATABASE_USER); - - if(dbo_role_name) - pfree(dbo_role_name); - if(guest_role_name) - pfree(guest_role_name); - if(physical_schema) - pfree(physical_schema); } /*