*** client/mysqldump.c.orig 2014-03-15 04:07:26.000000000 +0900 --- client/mysqldump.c 2014-04-08 14:53:55.935813008 +0900 *************** *** 110,116 **** opt_include_master_host_port= 0, opt_events= 0, opt_comments_used= 0, opt_alltspcs=0, opt_notspcs= 0, opt_drop_trigger= 0, ! opt_secure_auth= 1; static my_bool insert_pat_inited= 0, debug_info_flag= 0, debug_check_flag= 0; static ulong opt_max_allowed_packet, opt_net_buffer_length; static MYSQL mysql_connection,*mysql=0; --- 110,116 ---- opt_include_master_host_port= 0, opt_events= 0, opt_comments_used= 0, opt_alltspcs=0, opt_notspcs= 0, opt_drop_trigger= 0, ! opt_secure_auth= 1, opt_grants= 0; static my_bool insert_pat_inited= 0, debug_info_flag= 0, debug_check_flag= 0; static ulong opt_max_allowed_packet, opt_net_buffer_length; static MYSQL mysql_connection,*mysql=0; *************** *** 512,517 **** --- 512,519 ---- {"secure-auth", OPT_SECURE_AUTH, "Refuse client connecting to server if it" " uses old (pre-4.1.1) protocol.", &opt_secure_auth, &opt_secure_auth, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0}, + {"grants", OPT_GRANTS, "Dump grants", &opt_grants, + &opt_grants, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, #include {"tab",'T', "Create tab-separated textfile for each table to given path. (Create .sql " *************** *** 5739,5744 **** --- 5741,5832 ---- die(EX_MYSQLERR, DYNAMIC_STR_ERROR_MSG); } + /* + dump_as_grants + -- retrieves list of all users's grants, and print out as GRANT statement + into the output (the dump). + + RETURN + 0 Success + 1 Error + */ + + static uint dump_as_grants() + { + char query_buff[QUERY_LENGTH]; + char *user, *host; + FILE *sql_file= md_result_file; + MYSQL_RES *users_res, *grants_res; + MYSQL_ROW users_row, grants_row; + + char db_cl_name[MY_CS_NAME_SIZE]; + + + DBUG_ENTER("dump_as_grants"); + + print_comment(sql_file, 0, + "\n--\n-- Dumping grants\n--\n"); + + if (lock_tables) + mysql_query(mysql, "LOCK TABLES mysql.columns_priv, mysql.db, mysql.procs_priv, mysql.proxies_priv, mysql.tables_priv, mysql.user READ"); + + if (mysql_query_with_error_report(mysql, &users_res, "SELECT user, host FROM mysql.user ORDER BY user, host")) + DBUG_RETURN(0); + + if (mysql_num_rows(users_res) > 0) + { + if (opt_xml) + fputs("\t\n", sql_file); + else + { + if (fetch_db_collation("mysql", db_cl_name, sizeof (db_cl_name))) + DBUG_RETURN(1); + } + } + + if (switch_character_set_results(mysql, "binary")) + DBUG_RETURN(1); + + while ((users_row= mysql_fetch_row(users_res)) != NULL) + { + user= users_row[0]; + host= users_row[1]; + DBUG_PRINT("info", ("retrieving GRANT for %s@%s", user, host)); + my_snprintf(query_buff, sizeof(query_buff), "SHOW GRANTS FOR '%s'@'%s'", + user, host); + + if (mysql_query_with_error_report(mysql, &grants_res, query_buff)) + DBUG_RETURN(1); + + while ((grants_row= mysql_fetch_row(grants_res)) != NULL) + { + if (opt_xml) + { + print_xml_row(sql_file, "grant", grants_res, &grants_row, + "Grant"); + continue; + } + fprintf(sql_file, "%s;\n", grants_row[0]); + } + mysql_free_result(grants_res); + + if (opt_xml) + { + fputs("\t\n", sql_file); + check_io(sql_file); + } + + if (switch_character_set_results(mysql, default_charset)) + DBUG_RETURN(1); + } + mysql_free_result(users_res); + + if (lock_tables) + (void) mysql_query_with_error_report(mysql, 0, "UNLOCK TABLES"); + DBUG_RETURN(0); + } + + static void dynstr_append_checked(DYNAMIC_STRING* dest, const char* src) { if (dynstr_append(dest, src)) *************** *** 5858,5863 **** --- 5946,5954 ---- if (opt_single_transaction && do_unlock_tables(mysql)) /* unlock but no commit! */ goto err; + if (opt_grants) + dump_as_grants(); + if (opt_alltspcs) dump_all_tablespaces(); *** client/client_priv.h.orig 2014-03-15 04:07:26.000000000 +0900 --- client/client_priv.h 2014-04-08 14:54:10.502866827 +0900 *************** *** 52,58 **** OPT_OPEN_FILES_LIMIT, OPT_SET_CHARSET, OPT_SET_GTID_PURGED, OPT_SERVER_ARG, OPT_STOP_POSITION, OPT_START_DATETIME, OPT_STOP_DATETIME, OPT_SIGINT_IGNORE, OPT_HEXBLOB, OPT_ORDER_BY_PRIMARY, OPT_COUNT, ! OPT_TRIGGERS, OPT_MYSQL_ONLY_PRINT, OPT_MYSQL_LOCK_DIRECTORY, OPT_USE_THREADS, --- 52,58 ---- OPT_OPEN_FILES_LIMIT, OPT_SET_CHARSET, OPT_SET_GTID_PURGED, OPT_SERVER_ARG, OPT_STOP_POSITION, OPT_START_DATETIME, OPT_STOP_DATETIME, OPT_SIGINT_IGNORE, OPT_HEXBLOB, OPT_ORDER_BY_PRIMARY, OPT_COUNT, ! OPT_TRIGGERS, OPT_GRANTS, OPT_MYSQL_ONLY_PRINT, OPT_MYSQL_LOCK_DIRECTORY, OPT_USE_THREADS,