diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 831077a..7ecb760 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -52,6 +52,26 @@ #include "sql_db.h" #include "sql_array.h" +/* + Identifies tables in the table array. It's used in many places to make it + obvious which exact table is referenced, so reader don't have to search + through the code to understand what table was opened for example at index 1 + of the array. These enum values should be used as the array offset: + tables[GRANT_USER] = ... +*/ +enum GrantTable +{ + GRANT_USER= 0, + GRANT_DB, + GRANT_TABLES_PRIV, + GRANT_COLUMNS_PRIV, + GRANT_PROCS_PRIV, + GRANT_HOST, + GRANT_PROXIES_PRIV, + GRANT_TABLES_MAX // preserve as the last entry. +}; + + bool mysql_user_table_is_in_short_password_format= false; static const @@ -764,9 +784,10 @@ static my_bool acl_load(THD *thd, TABLE_LIST *tables) init_sql_alloc(&mem, ACL_ALLOC_BLOCK_SIZE, 0, MYF(0)); (void) my_init_dynamic_array(&acl_hosts,sizeof(ACL_HOST), 20, 50, MYF(0)); - if (tables[0].table) // "host" table may not exist (e.g. in MySQL 5.6.7+) + table= tables[GRANT_HOST].table; + if (table) // "host" table may not exist (e.g. in MySQL 5.6.7+) { - if (init_read_record(&read_record_info, thd, table= tables[0].table, + if (init_read_record(&read_record_info, thd, table, NULL, 1, 1, FALSE)) goto end; table->use_all_columns(); @@ -822,8 +843,8 @@ static my_bool acl_load(THD *thd, TABLE_LIST *tables) } freeze_size(&acl_hosts); - if (init_read_record(&read_record_info, thd, table=tables[1].table, - NULL, 1, 1, FALSE)) + table= tables[GRANT_USER].table; + if (init_read_record(&read_record_info, thd, table, NULL, 1, 1, FALSE)) goto end; table->use_all_columns(); (void) my_init_dynamic_array(&acl_users,sizeof(ACL_USER), 50, 100, MYF(0)); @@ -1023,8 +1044,8 @@ static my_bool acl_load(THD *thd, TABLE_LIST *tables) end_read_record(&read_record_info); freeze_size(&acl_users); - if (init_read_record(&read_record_info, thd, table=tables[2].table, - NULL, 1, 1, FALSE)) + table= tables[GRANT_DB].table; + if (init_read_record(&read_record_info, thd, table, NULL, 1, 1, FALSE)) goto end; table->use_all_columns(); (void) my_init_dynamic_array(&acl_dbs,sizeof(ACL_DB), 50, 100, MYF(0)); @@ -1091,10 +1112,10 @@ static my_bool acl_load(THD *thd, TABLE_LIST *tables) (void) my_init_dynamic_array(&acl_proxy_users, sizeof(ACL_PROXY_USER), 50, 100, MYF(0)); - if (tables[3].table) + table= tables[GRANT_PROXIES_PRIV].table; + if (table) { - if (init_read_record(&read_record_info, thd, table= tables[3].table, - NULL, 1, 1, FALSE)) + if (init_read_record(&read_record_info, thd, table, NULL, 1, 1, FALSE)) goto end; table->use_all_columns(); while (!(read_record_info.read_record(&read_record_info))) @@ -1174,7 +1195,7 @@ void acl_free(bool end) my_bool acl_reload(THD *thd) { - TABLE_LIST tables[4]; + TABLE_LIST tables[GRANT_TABLES_MAX]; DYNAMIC_ARRAY old_acl_hosts, old_acl_users, old_acl_dbs, old_acl_proxy_users; MEM_ROOT old_mem; bool old_initialized; @@ -1185,23 +1206,29 @@ my_bool acl_reload(THD *thd) To avoid deadlocks we should obtain table locks before obtaining acl_cache->lock mutex. */ - tables[0].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_HOST].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("host"), "host", TL_READ); - tables[1].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_USER].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("user"), "user", TL_READ); - tables[2].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_DB].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("db"), "db", TL_READ); - tables[3].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_PROXIES_PRIV].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("proxies_priv"), "proxies_priv", TL_READ); - tables[0].next_local= tables[0].next_global= tables + 1; - tables[1].next_local= tables[1].next_global= tables + 2; - tables[2].next_local= tables[2].next_global= tables + 3; - tables[0].open_type= tables[1].open_type= tables[2].open_type= - tables[3].open_type= OT_BASE_ONLY; - tables[0].open_strategy= tables[3].open_strategy= TABLE_LIST::OPEN_IF_EXISTS; - - if (open_and_lock_tables(thd, tables, FALSE, MYSQL_LOCK_IGNORE_TIMEOUT)) + tables[GRANT_HOST].next_local= tables[GRANT_HOST].next_global= + &tables[GRANT_USER]; + tables[GRANT_USER].next_local= tables[GRANT_USER].next_global= + &tables[GRANT_DB]; + tables[GRANT_DB].next_local= tables[GRANT_DB].next_global= + &tables[GRANT_PROXIES_PRIV]; + tables[GRANT_HOST].open_type= tables[GRANT_USER].open_type= + tables[GRANT_DB].open_type= tables[GRANT_PROXIES_PRIV].open_type= + OT_BASE_ONLY; + tables[GRANT_HOST].open_strategy= tables[GRANT_PROXIES_PRIV].open_strategy= + TABLE_LIST::OPEN_IF_EXISTS; + + if (open_and_lock_tables(thd, &tables[GRANT_HOST], FALSE, + MYSQL_LOCK_IGNORE_TIMEOUT)) { /* Execution might have been interrupted; only print the error message @@ -3589,7 +3616,7 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list, ulong column_priv= 0; List_iterator str_list (user_list); LEX_USER *Str, *tmp_Str; - TABLE_LIST tables[3]; + TABLE_LIST tables[GRANT_TABLES_MAX]; bool create_new_users=0; char *db_name, *table_name; DBUG_ENTER("mysql_table_grant"); @@ -3668,18 +3695,20 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list, /* open the mysql.tables_priv and mysql.columns_priv tables */ - tables[0].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_USER].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("user"), "user", TL_WRITE); - tables[1].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_TABLES_PRIV].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("tables_priv"), "tables_priv", TL_WRITE); - tables[2].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_COLUMNS_PRIV].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("columns_priv"), "columns_priv", TL_WRITE); - tables[0].next_local= tables[0].next_global= tables+1; + tables[GRANT_USER].next_local= tables[GRANT_USER].next_global= + &tables[GRANT_TABLES_PRIV]; /* Don't open column table if we don't need it ! */ if (column_priv || (revoke_grant && ((rights & COL_ACLS) || columns.elements))) - tables[1].next_local= tables[1].next_global= tables+2; + tables[GRANT_TABLES_PRIV].next_local= tables[GRANT_TABLES_PRIV].next_global= + &tables[GRANT_COLUMNS_PRIV]; #ifdef HAVE_REPLICATION /* @@ -3692,8 +3721,9 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list, The tables must be marked "updating" so that tables_ok() takes them into account in tests. */ - tables[0].updating= tables[1].updating= tables[2].updating= 1; - if (!(thd->spcont || rpl_filter->tables_ok(0, tables))) + tables[GRANT_USER].updating= tables[GRANT_TABLES_PRIV].updating= + tables[GRANT_COLUMNS_PRIV].updating= 1; + if (!(thd->spcont || rpl_filter->tables_ok(0, &tables[GRANT_USER]))) DBUG_RETURN(FALSE); } #endif @@ -3710,7 +3740,8 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list, this value corresponds to the statement being executed. */ thd->lex->sql_command= backup.sql_command; - if (open_and_lock_tables(thd, tables, FALSE, MYSQL_LOCK_IGNORE_TIMEOUT)) + if (open_and_lock_tables(thd, &tables[GRANT_USER], FALSE, + MYSQL_LOCK_IGNORE_TIMEOUT)) { // Should never happen thd->lex->restore_backup_query_tables_list(&backup); DBUG_RETURN(TRUE); /* purecov: deadcode */ @@ -3735,7 +3766,7 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list, continue; } /* Create user if needed */ - error=replace_user_table(thd, tables[0].table, *Str, + error=replace_user_table(thd, tables[GRANT_USER].table, *Str, 0, revoke_grant, create_new_users, test(thd->variables.sql_mode & MODE_NO_AUTO_CREATE_USER)); @@ -3806,17 +3837,17 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list, /* update table and columns */ - if (replace_table_table(thd, grant_table, tables[1].table, *Str, - db_name, table_name, + if (replace_table_table(thd, grant_table, tables[GRANT_TABLES_PRIV].table, + *Str, db_name, table_name, rights, column_priv, revoke_grant)) { /* Should only happen if table is crashed */ result= TRUE; /* purecov: deadcode */ } - else if (tables[2].table) + else if (tables[GRANT_COLUMNS_PRIV].table) { - if ((replace_column_table(grant_table, tables[2].table, *Str, - columns, + if ((replace_column_table(grant_table, tables[GRANT_COLUMNS_PRIV].table, + *Str, columns, db_name, table_name, rights, revoke_grant))) { @@ -3865,7 +3896,7 @@ bool mysql_routine_grant(THD *thd, TABLE_LIST *table_list, bool is_proc, { List_iterator str_list (user_list); LEX_USER *Str, *tmp_Str; - TABLE_LIST tables[2]; + TABLE_LIST tables[GRANT_TABLES_MAX]; bool create_new_users=0, result=0; char *db_name, *table_name; DBUG_ENTER("mysql_routine_grant"); @@ -3891,11 +3922,12 @@ bool mysql_routine_grant(THD *thd, TABLE_LIST *table_list, bool is_proc, /* open the mysql.user and mysql.procs_priv tables */ - tables[0].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_USER].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("user"), "user", TL_WRITE); - tables[1].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_PROCS_PRIV].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("procs_priv"), "procs_priv", TL_WRITE); - tables[0].next_local= tables[0].next_global= tables+1; + tables[GRANT_USER].next_local= tables[GRANT_USER].next_global= + &tables[GRANT_PROCS_PRIV]; #ifdef HAVE_REPLICATION /* @@ -3908,15 +3940,16 @@ bool mysql_routine_grant(THD *thd, TABLE_LIST *table_list, bool is_proc, The tables must be marked "updating" so that tables_ok() takes them into account in tests. */ - tables[0].updating= tables[1].updating= 1; - if (!(thd->spcont || rpl_filter->tables_ok(0, tables))) + tables[GRANT_USER].updating= tables[GRANT_PROCS_PRIV].updating= 1; + if (!(thd->spcont || rpl_filter->tables_ok(0, &tables[GRANT_USER]))) { DBUG_RETURN(FALSE); } } #endif - if (open_and_lock_tables(thd, tables, FALSE, MYSQL_LOCK_IGNORE_TIMEOUT)) + if (open_and_lock_tables(thd, &tables[GRANT_USER], FALSE, + MYSQL_LOCK_IGNORE_TIMEOUT)) DBUG_RETURN(TRUE); DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row()); @@ -3940,7 +3973,7 @@ bool mysql_routine_grant(THD *thd, TABLE_LIST *table_list, bool is_proc, continue; } /* Create user if needed */ - error=replace_user_table(thd, tables[0].table, *Str, + error=replace_user_table(thd, tables[GRANT_USER].table, *Str, 0, revoke_grant, create_new_users, test(thd->variables.sql_mode & MODE_NO_AUTO_CREATE_USER)); @@ -3975,8 +4008,8 @@ bool mysql_routine_grant(THD *thd, TABLE_LIST *table_list, bool is_proc, } } - if (replace_routine_table(thd, grant_name, tables[1].table, *Str, - db_name, table_name, is_proc, rights, + if (replace_routine_table(thd, grant_name, tables[GRANT_PROCS_PRIV].table, + *Str, db_name, table_name, is_proc, rights, revoke_grant) != 0) { result= TRUE; @@ -4006,7 +4039,7 @@ bool mysql_grant(THD *thd, const char *db, List &list, LEX_USER *Str, *tmp_Str, *proxied_user= NULL; char tmp_db[SAFE_NAME_LEN+1]; bool create_new_users=0; - TABLE_LIST tables[2]; + TABLE_LIST tables[GRANT_TABLES_MAX]; Rpl_filter *rpl_filter= thd->rpl_filter; DBUG_ENTER("mysql_grant"); @@ -4035,20 +4068,26 @@ bool mysql_grant(THD *thd, const char *db, List &list, } /* open the mysql.user and mysql.db or mysql.proxies_priv tables */ - tables[0].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_USER].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("user"), "user", TL_WRITE); + TABLE_LIST *next_table; if (is_proxy) - - tables[1].init_one_table(C_STRING_WITH_LEN("mysql"), + { + next_table= &tables[GRANT_PROXIES_PRIV]; + next_table->init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("proxies_priv"), "proxies_priv", TL_WRITE); + } else - tables[1].init_one_table(C_STRING_WITH_LEN("mysql"), + { + next_table= &tables[GRANT_DB]; + next_table->init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("db"), "db", TL_WRITE); - tables[0].next_local= tables[0].next_global= tables+1; + } + tables[GRANT_USER].next_local= tables[GRANT_USER].next_global= next_table; #ifdef HAVE_REPLICATION /* @@ -4061,13 +4100,14 @@ bool mysql_grant(THD *thd, const char *db, List &list, The tables must be marked "updating" so that tables_ok() takes them into account in tests. */ - tables[0].updating= tables[1].updating= 1; - if (!(thd->spcont || rpl_filter->tables_ok(0, tables))) + tables[GRANT_USER].updating= next_table->updating= 1; + if (!(thd->spcont || rpl_filter->tables_ok(0, &tables[GRANT_USER]))) DBUG_RETURN(FALSE); } #endif - if (open_and_lock_tables(thd, tables, FALSE, MYSQL_LOCK_IGNORE_TIMEOUT)) + if (open_and_lock_tables(thd, &tables[GRANT_USER], FALSE, + MYSQL_LOCK_IGNORE_TIMEOUT)) DBUG_RETURN(TRUE); /* purecov: deadcode */ DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row()); @@ -4097,7 +4137,7 @@ bool mysql_grant(THD *thd, const char *db, List &list, if (!tmp_Str->user.str && tmp_Str->password.str) Str->password= tmp_Str->password; - if (replace_user_table(thd, tables[0].table, *Str, + if (replace_user_table(thd, tables[GRANT_USER].table, *Str, (!db ? rights : 0), revoke_grant, create_new_users, test(thd->variables.sql_mode & MODE_NO_AUTO_CREATE_USER))) @@ -4107,7 +4147,7 @@ bool mysql_grant(THD *thd, const char *db, List &list, ulong db_rights= rights & DB_ACLS; if (db_rights == rights) { - if (replace_db_table(tables[1].table, db, *Str, db_rights, + if (replace_db_table(tables[GRANT_DB].table, db, *Str, db_rights, revoke_grant)) result= -1; } @@ -4119,7 +4159,8 @@ bool mysql_grant(THD *thd, const char *db, List &list, } else if (is_proxy) { - if (replace_proxies_priv_table (thd, tables[1].table, Str, proxied_user, + if (replace_proxies_priv_table (thd, tables[GRANT_PROXIES_PRIV].table, + Str, proxied_user, rights & GRANT_ACL ? TRUE : FALSE, revoke_grant)) result= -1; @@ -4312,8 +4353,8 @@ static my_bool grant_load(THD *thd, TABLE_LIST *tables) 0,0,0, (my_hash_get_key) get_grant_table, (my_hash_free_key) free_grant_table,0); - t_table = tables[0].table; - c_table = tables[1].table; + t_table = tables[GRANT_TABLES_PRIV].table; + c_table = tables[GRANT_COLUMNS_PRIV].table; if (t_table->file->ha_index_init(0, 1)) goto end_index_init; @@ -4440,7 +4481,7 @@ static my_bool grant_reload_procs_priv(THD *thd) my_bool grant_reload(THD *thd) { - TABLE_LIST tables[2]; + TABLE_LIST tables[GRANT_TABLES_MAX]; HASH old_column_priv_hash; MEM_ROOT old_mem; my_bool return_val= 1; @@ -4450,20 +4491,23 @@ my_bool grant_reload(THD *thd) if (!initialized) DBUG_RETURN(0); - tables[0].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_TABLES_PRIV].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("tables_priv"), "tables_priv", TL_READ); - tables[1].init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_COLUMNS_PRIV].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("columns_priv"), "columns_priv", TL_READ); - tables[0].next_local= tables[0].next_global= tables+1; - tables[0].open_type= tables[1].open_type= OT_BASE_ONLY; + tables[GRANT_TABLES_PRIV].next_local= tables[GRANT_TABLES_PRIV].next_global= + &tables[GRANT_COLUMNS_PRIV]; + tables[GRANT_TABLES_PRIV].open_type= tables[GRANT_COLUMNS_PRIV].open_type= + OT_BASE_ONLY; /* To avoid deadlocks we should obtain table locks before obtaining LOCK_grant rwlock. */ - if (open_and_lock_tables(thd, tables, FALSE, MYSQL_LOCK_IGNORE_TIMEOUT)) + if (open_and_lock_tables(thd, &tables[GRANT_TABLES_PRIV], FALSE, + MYSQL_LOCK_IGNORE_TIMEOUT)) goto end; mysql_rwlock_wrlock(&LOCK_grant); @@ -5737,14 +5781,10 @@ void get_mqh(const char *user, const char *host, USER_CONN *uc) SYNOPSIS open_grant_tables() thd The current thread. - tables (out) The 4 elements array for the opened tables. + tables (out) The array for the opened tables. DESCRIPTION - Tables are numbered as follows: - 0 user - 1 db - 2 tables_priv - 3 columns_priv + Tables are numbered by enum GrantTable. RETURN 1 Skip GRANT handling during replication. @@ -5752,7 +5792,6 @@ void get_mqh(const char *user, const char *host, USER_CONN *uc) < 0 Error. */ -#define GRANT_TABLES 6 int open_grant_tables(THD *thd, TABLE_LIST *tables) { DBUG_ENTER("open_grant_tables"); @@ -5763,29 +5802,34 @@ int open_grant_tables(THD *thd, TABLE_LIST *tables) DBUG_RETURN(-1); } - tables->init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_USER].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("user"), "user", TL_WRITE); - (tables+1)->init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_DB].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("db"), "db", TL_WRITE); - (tables+2)->init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_TABLES_PRIV].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("tables_priv"), "tables_priv", TL_WRITE); - (tables+3)->init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_COLUMNS_PRIV].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("columns_priv"), "columns_priv", TL_WRITE); - (tables+4)->init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_PROCS_PRIV].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("procs_priv"), "procs_priv", TL_WRITE); - (tables+5)->init_one_table(C_STRING_WITH_LEN("mysql"), + tables[GRANT_PROXIES_PRIV].init_one_table(C_STRING_WITH_LEN("mysql"), C_STRING_WITH_LEN("proxies_priv"), "proxies_priv", TL_WRITE); - tables[5].open_strategy= TABLE_LIST::OPEN_IF_EXISTS; - - tables->next_local= tables->next_global= tables + 1; - (tables+1)->next_local= (tables+1)->next_global= tables + 2; - (tables+2)->next_local= (tables+2)->next_global= tables + 3; - (tables+3)->next_local= (tables+3)->next_global= tables + 4; - (tables+4)->next_local= (tables+4)->next_global= tables + 5; + tables[GRANT_PROXIES_PRIV].open_strategy= TABLE_LIST::OPEN_IF_EXISTS; + + tables[GRANT_USER].next_local= tables[GRANT_USER].next_global= + &tables[GRANT_DB]; + tables[GRANT_DB].next_local= tables[GRANT_DB].next_global= + &tables[GRANT_TABLES_PRIV]; + tables[GRANT_TABLES_PRIV].next_local= tables[GRANT_TABLES_PRIV].next_global= + &tables[GRANT_COLUMNS_PRIV]; + tables[GRANT_COLUMNS_PRIV].next_local= tables[GRANT_COLUMNS_PRIV].next_global= + &tables[GRANT_PROCS_PRIV]; + tables[GRANT_PROCS_PRIV].next_local= tables[GRANT_PROCS_PRIV].next_global= + &tables[GRANT_PROXIES_PRIV]; #ifdef HAVE_REPLICATION /* @@ -5798,16 +5842,19 @@ int open_grant_tables(THD *thd, TABLE_LIST *tables) The tables must be marked "updating" so that tables_ok() takes them into account in tests. */ - tables[0].updating= tables[1].updating= tables[2].updating= - tables[3].updating= tables[4].updating= tables[5].updating= 1; - if (!(thd->spcont || rpl_filter->tables_ok(0, tables))) + tables[GRANT_USER].updating= tables[GRANT_DB].updating= + tables[GRANT_TABLES_PRIV].updating= tables[GRANT_COLUMNS_PRIV].updating= + tables[GRANT_PROCS_PRIV].updating= tables[GRANT_PROXIES_PRIV].updating= 1; + if (!(thd->spcont || rpl_filter->tables_ok(0, &tables[GRANT_USER]))) DBUG_RETURN(1); - tables[0].updating= tables[1].updating= tables[2].updating= - tables[3].updating= tables[4].updating= tables[5].updating= 0; + tables[GRANT_USER].updating= tables[GRANT_DB].updating= + tables[GRANT_TABLES_PRIV].updating= tables[GRANT_COLUMNS_PRIV].updating= + tables[GRANT_PROCS_PRIV].updating= tables[GRANT_PROXIES_PRIV].updating= 0; } #endif - if (open_and_lock_tables(thd, tables, FALSE, MYSQL_LOCK_IGNORE_TIMEOUT)) + if (open_and_lock_tables(thd, &tables[GRANT_USER], FALSE, + MYSQL_LOCK_IGNORE_TIMEOUT)) { // This should never happen DBUG_RETURN(-1); } @@ -5898,8 +5945,8 @@ static int modify_grant_table(TABLE *table, Field *host_field, SYNOPSIS handle_grant_table() - tables The array with the four open tables. - table_no The number of the table to handle (0..4). + tables The array tables opened by open_grant_tables + table_id Table to handle drop If user_from is to be dropped. user_from The the user to be searched/dropped/renamed. user_to The new name for the user if to be renamed, @@ -5912,12 +5959,6 @@ static int modify_grant_table(TABLE *table, Field *host_field, Delete from grant table if drop is true. Update in grant table if drop is false and user_to is not NULL. Search in grant table if drop is false and user_to is NULL. - Tables are numbered as follows: - 0 user - 1 db - 2 tables_priv - 3 columns_priv - 4 procs_priv RETURN > 0 At least one record matched. @@ -5925,14 +5966,15 @@ static int modify_grant_table(TABLE *table, Field *host_field, < 0 Error. */ -static int handle_grant_table(TABLE_LIST *tables, uint table_no, bool drop, - LEX_USER *user_from, LEX_USER *user_to) +static int handle_grant_table(TABLE_LIST *tables, enum GrantTable table_id, + bool drop, LEX_USER *user_from, LEX_USER *user_to) { int result= 0; int error; - TABLE *table= tables[table_no].table; + TABLE *table= tables[table_id].table; Field *host_field= table->field[0]; - Field *user_field= table->field[table_no && table_no != 5 ? 2 : 1]; + Field *user_field= table->field[table_id != GRANT_USER && + table_id != GRANT_PROXIES_PRIV ? 2 : 1]; char *host_str= user_from->host.str; char *user_str= user_from->user.str; const char *host; @@ -5943,7 +5985,7 @@ static int handle_grant_table(TABLE_LIST *tables, uint table_no, bool drop, THD *thd= current_thd; table->use_all_columns(); - if (! table_no) // mysql.user table + if (table_id == GRANT_USER) { /* The 'user' table has an unique index on (host, user). @@ -6015,7 +6057,7 @@ static int handle_grant_table(TABLE_LIST *tables, uint table_no, bool drop, user= ""; #ifdef EXTRA_DEBUG - if (table_no != 5) + if (table_id != GRANT_PROXIES_PRIV) { DBUG_PRINT("loop",("scan fields: '%s'@'%s' '%s' '%s' '%s'", user, host, @@ -6317,7 +6359,8 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, DBUG_ENTER("handle_grant_data"); /* Handle user table. */ - if ((found= handle_grant_table(tables, 0, drop, user_from, user_to)) < 0) + if ((found= handle_grant_table(tables, GRANT_USER, + drop, user_from, user_to)) < 0) { /* Handle of table failed, don't touch the in-memory array. */ result= -1; @@ -6335,7 +6378,8 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, } /* Handle db table. */ - if ((found= handle_grant_table(tables, 1, drop, user_from, user_to)) < 0) + if ((found= handle_grant_table(tables, GRANT_DB, drop, + user_from, user_to)) < 0) { /* Handle of table failed, don't touch the in-memory array. */ result= -1; @@ -6354,7 +6398,8 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, } /* Handle stored routines table. */ - if ((found= handle_grant_table(tables, 4, drop, user_from, user_to)) < 0) + if ((found= handle_grant_table(tables, GRANT_PROCS_PRIV, drop, + user_from, user_to)) < 0) { /* Handle of table failed, don't touch in-memory array. */ result= -1; @@ -6382,7 +6427,8 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, } /* Handle tables table. */ - if ((found= handle_grant_table(tables, 2, drop, user_from, user_to)) < 0) + if ((found= handle_grant_table(tables, GRANT_TABLES_PRIV, drop, + user_from, user_to)) < 0) { /* Handle of table failed, don't touch columns and in-memory array. */ result= -1; @@ -6398,7 +6444,8 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, } /* Handle columns table. */ - if ((found= handle_grant_table(tables, 3, drop, user_from, user_to)) < 0) + if ((found= handle_grant_table(tables, GRANT_COLUMNS_PRIV, drop, + user_from, user_to)) < 0) { /* Handle of table failed, don't touch the in-memory array. */ result= -1; @@ -6413,9 +6460,10 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop, } /* Handle proxies_priv table. */ - if (tables[5].table) + if (tables[GRANT_PROXIES_PRIV].table) { - if ((found= handle_grant_table(tables, 5, drop, user_from, user_to)) < 0) + if ((found= handle_grant_table(tables, GRANT_PROXIES_PRIV, drop, + user_from, user_to)) < 0) { /* Handle of table failed, don't touch the in-memory array. */ result= -1; @@ -6464,7 +6512,7 @@ bool mysql_create_user(THD *thd, List &list) String wrong_users; LEX_USER *user_name, *tmp_user_name; List_iterator user_list(list); - TABLE_LIST tables[GRANT_TABLES]; + TABLE_LIST tables[GRANT_TABLES_MAX]; bool some_users_created= FALSE; DBUG_ENTER("mysql_create_user"); @@ -6487,7 +6535,7 @@ bool mysql_create_user(THD *thd, List &list) Search all in-memory structures and grant tables for a mention of the new user name. */ - if (handle_grant_data(tables, 0, user_name, NULL)) + if (handle_grant_data(tables, false, user_name, NULL)) { append_user(&wrong_users, user_name); result= TRUE; @@ -6495,7 +6543,7 @@ bool mysql_create_user(THD *thd, List &list) } some_users_created= TRUE; - if (replace_user_table(thd, tables[0].table, *user_name, 0, 0, 1, 0)) + if (replace_user_table(thd, tables[GRANT_USER].table, *user_name, 0, 0, 1, 0)) { append_user(&wrong_users, user_name); result= TRUE; @@ -6534,7 +6582,7 @@ bool mysql_drop_user(THD *thd, List &list) String wrong_users; LEX_USER *user_name, *tmp_user_name; List_iterator user_list(list); - TABLE_LIST tables[GRANT_TABLES]; + TABLE_LIST tables[GRANT_TABLES_MAX]; bool some_users_deleted= FALSE; ulonglong old_sql_mode= thd->variables.sql_mode; DBUG_ENTER("mysql_drop_user"); @@ -6555,7 +6603,7 @@ bool mysql_drop_user(THD *thd, List &list) result= TRUE; continue; } - if (handle_grant_data(tables, 1, user_name, NULL) <= 0) + if (handle_grant_data(tables, true, user_name, NULL) <= 0) { append_user(&wrong_users, user_name); result= TRUE; @@ -6601,7 +6649,7 @@ bool mysql_rename_user(THD *thd, List &list) LEX_USER *user_from, *tmp_user_from; LEX_USER *user_to, *tmp_user_to; List_iterator user_list(list); - TABLE_LIST tables[GRANT_TABLES]; + TABLE_LIST tables[GRANT_TABLES_MAX]; bool some_users_renamed= FALSE; DBUG_ENTER("mysql_rename_user"); @@ -6633,8 +6681,8 @@ bool mysql_rename_user(THD *thd, List &list) Search all in-memory structures and grant tables for a mention of the new user name. */ - if (handle_grant_data(tables, 0, user_to, NULL) || - handle_grant_data(tables, 0, user_from, user_to) <= 0) + if (handle_grant_data(tables, false, user_to, NULL) || + handle_grant_data(tables, false, user_from, user_to) <= 0) { append_user(&wrong_users, user_from); result= TRUE; @@ -6678,7 +6726,7 @@ bool mysql_revoke_all(THD *thd, List &list) uint counter, revoked, is_proc; int result; ACL_DB *acl_db; - TABLE_LIST tables[GRANT_TABLES]; + TABLE_LIST tables[GRANT_TABLES_MAX]; DBUG_ENTER("mysql_revoke_all"); if ((result= open_grant_tables(thd, tables))) @@ -6704,7 +6752,7 @@ bool mysql_revoke_all(THD *thd, List &list) continue; } - if (replace_user_table(thd, tables[0].table, + if (replace_user_table(thd, tables[GRANT_USER].table, *lex_user, ~(ulong)0, 1, 0, 0)) { result= -1; @@ -6732,7 +6780,7 @@ bool mysql_revoke_all(THD *thd, List &list) if (!strcmp(lex_user->user.str,user) && !strcmp(lex_user->host.str, host)) { - if (!replace_db_table(tables[1].table, acl_db->db, *lex_user, + if (!replace_db_table(tables[GRANT_DB].table, acl_db->db, *lex_user, ~(ulong)0, 1)) { /* @@ -6764,7 +6812,8 @@ bool mysql_revoke_all(THD *thd, List &list) if (!strcmp(lex_user->user.str,user) && !strcmp(lex_user->host.str, host)) { - if (replace_table_table(thd,grant_table,tables[2].table,*lex_user, + if (replace_table_table(thd,grant_table,tables[GRANT_TABLES_PRIV].table, + *lex_user, grant_table->db, grant_table->tname, ~(ulong)0, 0, 1)) @@ -6779,7 +6828,8 @@ bool mysql_revoke_all(THD *thd, List &list) continue; } List columns; - if (!replace_column_table(grant_table,tables[3].table, *lex_user, + if (!replace_column_table(grant_table,tables[GRANT_COLUMNS_PRIV].table, + *lex_user, columns, grant_table->db, grant_table->tname, @@ -6810,11 +6860,12 @@ bool mysql_revoke_all(THD *thd, List &list) if (!strcmp(lex_user->user.str,user) && !strcmp(lex_user->host.str, host)) { - if (replace_routine_table(thd,grant_proc,tables[4].table,*lex_user, - grant_proc->db, - grant_proc->tname, - is_proc, - ~(ulong)0, 1) == 0) + if (replace_routine_table(thd,grant_proc,tables[GRANT_PROCS_PRIV].table, + *lex_user, + grant_proc->db, + grant_proc->tname, + is_proc, + ~(ulong)0, 1) == 0) { revoked= 1; continue; @@ -6923,7 +6974,7 @@ bool sp_revoke_privileges(THD *thd, const char *sp_db, const char *sp_name, { uint counter, revoked; int result; - TABLE_LIST tables[GRANT_TABLES]; + TABLE_LIST tables[GRANT_TABLES_MAX]; HASH *hash= is_proc ? &proc_priv_hash : &func_priv_hash; Silence_routine_definer_errors error_handler; DBUG_ENTER("sp_revoke_privileges"); @@ -6956,7 +7007,8 @@ bool sp_revoke_privileges(THD *thd, const char *sp_db, const char *sp_name, lex_user.host.length= grant_proc->host.hostname ? strlen(grant_proc->host.hostname) : 0; - if (replace_routine_table(thd,grant_proc,tables[4].table,lex_user, + if (replace_routine_table(thd,grant_proc,tables[GRANT_PROCS_PRIV].table, + lex_user, grant_proc->db, grant_proc->tname, is_proc, ~(ulong)0, 1) == 0) {