diff --git a/contrib/babelfishpg_tsql/src/dbcmds.c b/contrib/babelfishpg_tsql/src/dbcmds.c index 74d7e7534a..ca1d6a4c44 100644 --- a/contrib/babelfishpg_tsql/src/dbcmds.c +++ b/contrib/babelfishpg_tsql/src/dbcmds.c @@ -42,6 +42,9 @@ #include "pltsql.h" #include "extendedproperty.h" +Oid sys_babelfish_db_seq_oid = InvalidOid; + +static Oid get_sys_babelfish_db_seq_oid(void); static bool have_createdb_privilege(void); static List *gen_createdb_subcmds(const char *schema, const char *dbo, @@ -57,6 +60,20 @@ static Oid do_create_bbf_db(const char *dbname, List *options, const char *owner static void create_bbf_db_internal(const char *dbname, List *options, const char *owner, int16 dbid); static void drop_related_bbf_namespace_entries(int16 dbid); +static Oid +get_sys_babelfish_db_seq_oid() +{ + if(!OidIsValid(sys_babelfish_db_seq_oid)) + { + RangeVar *sequence = makeRangeVarFromNameList(stringToQualifiedNameList("sys.babelfish_db_seq")); + Oid seqid = RangeVarGetRelid(sequence, NoLock, false); + + Assert(OidIsValid(seqid)); + sys_babelfish_db_seq_oid = seqid; + } + return sys_babelfish_db_seq_oid; +} + static bool have_createdb_privilege(void) { @@ -111,7 +128,6 @@ gen_createdb_subcmds(const char *schema, const char *dbo, const char *db_owner, /* create sysdatabases under current DB's DBO schema */ appendStringInfo(&query, "CREATE VIEW dummy.sysdatabases AS SELECT * FROM sys.sysdatabases; "); appendStringInfo(&query, "ALTER VIEW dummy.sysdatabases OWNER TO dummy; "); - appendStringInfo(&query, "GRANT SELECT ON dummy.sysdatabases TO dummy; "); /* create guest schema in the database. This has to be the last statement */ if (guest) @@ -120,9 +136,9 @@ gen_createdb_subcmds(const char *schema, const char *dbo, const char *db_owner, res = raw_parser(query.data, RAW_PARSE_DEFAULT); if (guest) - expected_stmt_num = list_length(logins) > 0 ? 10 : 9; + expected_stmt_num = list_length(logins) > 0 ? 9 : 8; else - expected_stmt_num = 7; + expected_stmt_num = 6; if (list_length(res) != expected_stmt_num) ereport(ERROR, @@ -166,9 +182,6 @@ gen_createdb_subcmds(const char *schema, const char *dbo, const char *db_owner, stmt = parsetree_nth_stmt(res, i++); update_AlterTableStmt(stmt, schema, db_owner); - stmt = parsetree_nth_stmt(res, i++); - update_GrantStmt(stmt, NULL, schema, db_owner); - if (guest) { stmt = parsetree_nth_stmt(res, i++); @@ -301,9 +314,12 @@ getAvailDbid(void) int16 dbid; int16 start = 0; + if(GetUserId() != get_role_oid("sysadmin", true)) + return InvalidDbid; + do { - dbid = DirectFunctionCall1(nextval, CStringGetTextDatum("sys.babelfish_db_seq")); + dbid = nextval_internal(get_sys_babelfish_db_seq_oid(), false); if (start == 0) start = dbid; else if (start == dbid) @@ -351,12 +367,8 @@ getDbidForLogicalDbRestore(Oid relid) * dbid while inserting into sysdatabases catalog. */ else - { - RangeVar *sequence = makeRangeVarFromNameList(stringToQualifiedNameList("sys.babelfish_db_seq")); - Oid seqid = RangeVarGetRelid(sequence, NoLock, false); + dbid = DirectFunctionCall1(currval_oid, get_sys_babelfish_db_seq_oid()); - dbid = DirectFunctionCall1(currval_oid, seqid); - } bbf_set_current_user(prev_current_user); return dbid; @@ -408,6 +420,9 @@ create_bbf_db_internal(const char *dbname, List *options, const char *owner, int const char *guest; const char *prev_current_user; int stmt_number = 0; + int save_sec_context; + bool is_set_userid; + Oid save_userid; /* TODO: Extract options */ @@ -519,16 +534,24 @@ create_bbf_db_internal(const char *dbname, List *options, const char *owner, int /* Run all subcommands */ foreach(parsetree_item, parsetree_list) { - Node *stmt = ((RawStmt *) lfirst(parsetree_item))->stmt; - PlannedStmt *wrapper; + Node *stmt = ((RawStmt *) lfirst(parsetree_item))->stmt; + PlannedStmt *wrapper; + is_set_userid = false; + if(stmt->type == T_CreateSchemaStmt || stmt->type == T_AlterTableStmt + || stmt->type == T_ViewStmt) + { + GetUserIdAndSecContext(&save_userid, &save_sec_context); + SetUserIdAndSecContext(get_role_oid(dbo_role, true), + save_sec_context | SECURITY_LOCAL_USERID_CHANGE); + is_set_userid = true; + } /* need to make a wrapper PlannedStmt */ wrapper = makeNode(PlannedStmt); wrapper->commandType = CMD_UTILITY; wrapper->canSetTag = false; wrapper->utilityStmt = stmt; wrapper->stmt_location = 0; - stmt_number++; if (guest && list_length(parsetree_list) == stmt_number) wrapper->stmt_len = 19; @@ -545,7 +568,9 @@ create_bbf_db_internal(const char *dbname, List *options, const char *owner, int None_Receiver, NULL); - /* make sure later steps can see the object created here */ + if(is_set_userid) + SetUserIdAndSecContext(save_userid, save_sec_context); + CommandCounterIncrement(); } set_cur_db(old_dbid, old_dbname); @@ -567,6 +592,9 @@ create_bbf_db_internal(const char *dbname, List *options, const char *owner, int } PG_CATCH(); { + if(is_set_userid) + SetUserIdAndSecContext(save_userid, save_sec_context); + /* Clean up. Restore previous state. */ bbf_set_current_user(prev_current_user); set_cur_db(old_dbid, old_dbname);