summaryrefslogtreecommitdiff
path: root/sql/sql_acl.cc
diff options
context:
space:
mode:
authorAlexander Nozdrin <alik@sun.com>2010-01-31 01:06:50 +0300
committerAlexander Nozdrin <alik@sun.com>2010-01-31 01:06:50 +0300
commitddc8765a9e35a020446fc28712751fe62200ea11 (patch)
tree64c1a58bb6f6f5516fbf930759b54e0081b98b90 /sql/sql_acl.cc
parent744d6cf57cfb9803a899a2d54e7407a27db96a27 (diff)
parent788c28aceb023702282bfbf372016da79f9ab49f (diff)
downloadmariadb-git-ddc8765a9e35a020446fc28712751fe62200ea11.tar.gz
Manual merge from mysql-trunk-merge.
Conflicts: - mysql-test/suite/rpl/r/rpl_binlog_grant.result - mysql-test/suite/rpl/r/rpl_sp.result - mysql-test/suite/rpl/t/rpl_binlog_grant.test - sql/sql_parse.cc - sql/sql_table.cc - sql/sql_test.cc
Diffstat (limited to 'sql/sql_acl.cc')
-rw-r--r--sql/sql_acl.cc124
1 files changed, 112 insertions, 12 deletions
diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc
index 012542a9467..8283123f51c 100644
--- a/sql/sql_acl.cc
+++ b/sql/sql_acl.cc
@@ -196,6 +196,7 @@ static bool compare_hostname(const acl_host_and_ip *host,const char *hostname,
const char *ip);
static my_bool acl_load(THD *thd, TABLE_LIST *tables);
static my_bool grant_load(THD *thd, TABLE_LIST *tables);
+static bool acl_write_bin_log(THD *thd, List <LEX_USER> &list, bool clear_error);
/*
Convert scrambled password to binary form, according to scramble type,
@@ -3270,7 +3271,8 @@ int mysql_table_grant(THD *thd, TABLE_LIST *table_list,
if (!result) /* success */
{
- result= write_bin_log(thd, TRUE, thd->query(), thd->query_length());
+ if (acl_write_bin_log(thd, user_list, TRUE))
+ result= -1;
}
mysql_rwlock_unlock(&LOCK_grant);
@@ -3446,8 +3448,7 @@ bool mysql_routine_grant(THD *thd, TABLE_LIST *table_list, bool is_proc,
if (write_to_binlog)
{
- if (write_bin_log(thd, FALSE, thd->query(), thd->query_length()))
- result= TRUE;
+ result|= acl_write_bin_log(thd, user_list, FALSE);
}
mysql_rwlock_unlock(&LOCK_grant);
@@ -3576,7 +3577,7 @@ bool mysql_grant(THD *thd, const char *db, List <LEX_USER> &list,
if (!result)
{
- result= write_bin_log(thd, TRUE, thd->query(), thd->query_length());
+ result= acl_write_bin_log(thd, list, TRUE);
}
mysql_rwlock_unlock(&LOCK_grant);
@@ -5755,9 +5756,9 @@ static int handle_grant_data(TABLE_LIST *tables, bool drop,
}
-static void append_user(String *str, LEX_USER *user)
+static void append_user(String *str, LEX_USER *user, bool comma= TRUE)
{
- if (str->length())
+ if (comma && str->length())
str->append(',');
str->append('\'');
str->append(user->user.str);
@@ -5766,6 +5767,65 @@ static void append_user(String *str, LEX_USER *user)
str->append('\'');
}
+/*
+ The operations(DROP, RENAME, REVOKE, GRANT) will cause inconsistency between
+ master and slave, when CURRENT_USER() is used. To solve this problem, we
+ construct a new binlog statement in which CURRENT_USER() is replaced by
+ the real user name and host name.
+ */
+static bool acl_write_bin_log(THD *thd, List <LEX_USER> &list, bool clear_error)
+{
+ String log_query;
+ LEX *lex= thd->lex;
+ List_iterator <LEX_USER> user_list(list);
+ LEX_USER *user, *tmp_user;
+
+ if (!mysql_bin_log.is_open())
+ return FALSE;
+
+ if (log_query.append(lex->stmt_begin, lex->stmt_user_begin - lex->stmt_begin))
+ return TRUE;
+ while ((tmp_user= user_list++))
+ {
+ if (!(user= get_current_user(thd, tmp_user)))
+ continue;
+
+ /*
+ No User, but a password?
+ They did GRANT ... TO CURRENT_USER() IDENTIFIED BY ... !
+ Get the current user, and shallow-copy the new password to them!
+ */
+ if (!tmp_user->user.str && tmp_user->password.str)
+ user->password= tmp_user->password;
+
+ if (log_query.append(" ", 1))
+ return TRUE;
+ append_user(&log_query, user, FALSE);
+ /* Only 'GRANT' have password */
+ if (user->password.str)
+ {
+ if (log_query.append(STRING_WITH_LEN(" IDENTIFIED BY ")) ||
+ log_query.append(STRING_WITH_LEN("PASSWORD ")) ||
+ log_query.append("'", 1) ||
+ log_query.append(user->password.str,
+ user->password.length) ||
+ log_query.append("'", 1))
+ return TRUE;
+ }
+ if (log_query.append(",", 1))
+ return TRUE;
+ }
+ /* It is binlogged only when at least one user is in the query */
+ if (log_query.c_ptr()[log_query.length()-1] == ',')
+ {
+ log_query.length(log_query.length()-1);
+ if (log_query.append(lex->stmt_user_end, lex->stmt_end - lex->stmt_user_end))
+ return TRUE;
+ return write_bin_log(thd, clear_error, log_query.c_ptr_safe(),
+ log_query.length()) != 0;
+ }
+ return FALSE;
+}
/*
Create a list of users.
@@ -5872,6 +5932,7 @@ bool mysql_drop_user(THD *thd, List <LEX_USER> &list)
{
int result;
String wrong_users;
+ String log_query;
LEX_USER *user_name, *tmp_user_name;
List_iterator <LEX_USER> user_list(list);
TABLE_LIST tables[GRANT_TABLES];
@@ -5901,6 +5962,7 @@ bool mysql_drop_user(THD *thd, List <LEX_USER> &list)
mysql_rwlock_wrlock(&LOCK_grant);
mysql_mutex_lock(&acl_cache->lock);
+ log_query.append(STRING_WITH_LEN("DROP USER"));
while ((tmp_user_name= user_list++))
{
if (!(user_name= get_current_user(thd, tmp_user_name)))
@@ -5908,6 +5970,17 @@ bool mysql_drop_user(THD *thd, List <LEX_USER> &list)
result= TRUE;
continue;
}
+
+ /*
+ The operation will cause inconsistency between master and slave, when
+ CURRENT_USER() is used. To solve this problem, we construct a new
+ binlog statement in which CURRENT_USER() is replaced by the real user
+ name and host name.
+ */
+ log_query.append(STRING_WITH_LEN(" "));
+ append_user(&log_query, user_name, FALSE);
+ log_query.append(STRING_WITH_LEN(","));
+
if (handle_grant_data(tables, 1, user_name, NULL) <= 0)
{
append_user(&wrong_users, user_name);
@@ -5926,7 +5999,13 @@ bool mysql_drop_user(THD *thd, List <LEX_USER> &list)
my_error(ER_CANNOT_USER, MYF(0), "DROP USER", wrong_users.c_ptr_safe());
if (some_users_deleted)
- result |= write_bin_log(thd, FALSE, thd->query(), thd->query_length());
+ {
+ if (log_query.c_ptr()[log_query.length()-1] == ',')
+ {
+ log_query.length(log_query.length()-1);
+ result|= write_bin_log(thd, FALSE, log_query.c_ptr_safe(), log_query.length());
+ }
+ }
mysql_rwlock_unlock(&LOCK_grant);
close_thread_tables(thd);
@@ -5954,6 +6033,7 @@ bool mysql_rename_user(THD *thd, List <LEX_USER> &list)
{
int result;
String wrong_users;
+ String log_query;
LEX_USER *user_from, *tmp_user_from;
LEX_USER *user_to, *tmp_user_to;
List_iterator <LEX_USER> user_list(list);
@@ -5981,6 +6061,7 @@ bool mysql_rename_user(THD *thd, List <LEX_USER> &list)
mysql_rwlock_wrlock(&LOCK_grant);
mysql_mutex_lock(&acl_cache->lock);
+ log_query.append(STRING_WITH_LEN("RENAME USER"));
while ((tmp_user_from= user_list++))
{
if (!(user_from= get_current_user(thd, tmp_user_from)))
@@ -5997,6 +6078,18 @@ bool mysql_rename_user(THD *thd, List <LEX_USER> &list)
DBUG_ASSERT(user_to != 0); /* Syntax enforces pairs of users. */
/*
+ The operation will cause inconsistency between master and slave, when
+ CURRENT_USER() is used. To solve this problem, we construct a new
+ binlog statement in which CURRENT_USER() is replaced by the real user
+ name and host name.
+ */
+ log_query.append(STRING_WITH_LEN(" "));
+ append_user(&log_query, user_from, FALSE);
+ log_query.append(STRING_WITH_LEN(" TO "));
+ append_user(&log_query, user_to, FALSE);
+ log_query.append(STRING_WITH_LEN(","));
+
+ /*
Search all in-memory structures and grant tables
for a mention of the new user name.
*/
@@ -6017,9 +6110,15 @@ bool mysql_rename_user(THD *thd, List <LEX_USER> &list)
if (result)
my_error(ER_CANNOT_USER, MYF(0), "RENAME USER", wrong_users.c_ptr_safe());
-
- if (some_users_renamed && mysql_bin_log.is_open())
- result |= write_bin_log(thd, FALSE, thd->query(), thd->query_length());
+
+ if (some_users_renamed)
+ {
+ if (log_query.c_ptr()[log_query.length()-1] == ',')
+ {
+ log_query.length(log_query.length()-1);
+ result|= write_bin_log(thd, FALSE, log_query.c_ptr_safe(), log_query.length());
+ }
+ }
mysql_rwlock_unlock(&LOCK_grant);
close_thread_tables(thd);
@@ -6209,8 +6308,9 @@ bool mysql_revoke_all(THD *thd, List <LEX_USER> &list)
mysql_mutex_unlock(&acl_cache->lock);
- int binlog_error=
- write_bin_log(thd, FALSE, thd->query(), thd->query_length());
+ int binlog_error= 0;
+ if (acl_write_bin_log(thd, list, FALSE))
+ binlog_error= 1;
mysql_rwlock_unlock(&LOCK_grant);
close_thread_tables(thd);