Skip to content

Commit

Permalink
Revert "Modify get_*_role_name() and get_*_schema_name() APIs to alwa…
Browse files Browse the repository at this point in the history
…ys retur…" (#2955)
  • Loading branch information
shardgupta authored Sep 23, 2024
1 parent 8c3c2a9 commit 0636924
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 333 deletions.
46 changes: 11 additions & 35 deletions contrib/babelfishpg_tsql/runtime/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
{
Expand Down Expand Up @@ -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))
Expand All @@ -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
{
Expand Down Expand Up @@ -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))
Expand All @@ -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
{
Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -2908,10 +2887,7 @@ has_dbaccess(PG_FUNCTION_ARGS)
if (!user)
PG_RETURN_INT32(0);
else
{
pfree(user);
PG_RETURN_INT32(1);
}
}

Datum
Expand Down
58 changes: 5 additions & 53 deletions contrib/babelfishpg_tsql/src/catalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -3053,9 +3053,6 @@ guest_role_exists_for_db(const char *dbname)
ReleaseSysCache(tuple);
}

if(guest_role)
pfree(guest_role);

return role_exists;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion contrib/babelfishpg_tsql/src/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 32 additions & 38 deletions contrib/babelfishpg_tsql/src/dbcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
{
Expand All @@ -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)) ||
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions contrib/babelfishpg_tsql/src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit 0636924

Please sign in to comment.