home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2004 March / PCWMAR04.iso / Software / Resources / MySQL / Windows / data1.cab / Development / scripts / mysql_fix_privilege_tables.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  2003-10-20  |  6.8 KB  |  204 lines

  1. #!/bin/sh
  2.  
  3. root_password="$1"
  4. host="localhost"
  5. user="root"
  6.  
  7. if test -z $1 ; then
  8.   cmd="@bindir@/mysql -f --user=$user --host=$host mysql"
  9. else
  10.   root_password="$1"
  11.   cmd="@bindir@/mysql -f --user=$user --password=$root_password --host=$host mysql"
  12. fi
  13.  
  14. # Debian addition
  15. if [ "$1" = "--sql-only" ]; then
  16.   root_password=""
  17.   cmd="/usr/share/mysql/echo_stderr"
  18. fi
  19.  
  20. echo "This scripts updates the mysql.user, mysql.db, mysql.host and the"
  21. echo "mysql.func tables to MySQL 3.22.14 and above."
  22. echo ""
  23. echo "This is needed if you want to use the new GRANT functions,"
  24. echo "CREATE AGGREGATE FUNCTION or want to use the more secure passwords in 3.23"
  25. echo ""
  26. echo "If you get 'Access denied' errors, you should run this script again"
  27. echo "and give the MySQL root user password as an argument!"
  28.  
  29. echo "Converting all privilege tables to MyISAM format"
  30. $cmd <<END_OF_DATA
  31. ALTER TABLE user type=MyISAM;
  32. ALTER TABLE db type=MyISAM;
  33. ALTER TABLE host type=MyISAM;
  34. ALTER TABLE func type=MyISAM;
  35. ALTER TABLE columns_priv type=MyISAM;
  36. ALTER TABLE tables_priv type=MyISAM;
  37. END_OF_DATA
  38.  
  39.  
  40. # Fix old password format, add File_priv and func table
  41. echo ""
  42. echo "If your tables are already up to date or partially up to date you will"
  43. echo "get some warnings about 'Duplicated column name'. You can safely ignore these!"
  44.  
  45. $cmd <<END_OF_DATA
  46. alter table user change password password char(16) NOT NULL;
  47. alter table user add File_priv enum('N','Y') NOT NULL;
  48. CREATE TABLE if not exists func (
  49.   name char(64) DEFAULT '' NOT NULL,
  50.   ret tinyint(1) DEFAULT '0' NOT NULL,
  51.   dl char(128) DEFAULT '' NOT NULL,
  52.   type enum ('function','aggregate') NOT NULL,
  53.   PRIMARY KEY (name)
  54. );
  55. END_OF_DATA
  56. echo ""
  57.  
  58. # Add the new grant colums
  59.  
  60. echo "Creating Grant Alter and Index privileges if they don't exists"
  61. echo "You can ignore any Duplicate column errors"
  62. $cmd <<END_OF_DATA
  63. alter table user add Grant_priv enum('N','Y') NOT NULL,add References_priv enum('N','Y') NOT NULL,add Index_priv enum('N','Y') NOT NULL,add Alter_priv enum('N','Y') NOT NULL;
  64. alter table host add Grant_priv enum('N','Y') NOT NULL,add References_priv enum('N','Y') NOT NULL,add Index_priv enum('N','Y') NOT NULL,add Alter_priv enum('N','Y') NOT NULL;
  65. alter table db add Grant_priv enum('N','Y') NOT NULL,add References_priv enum('N','Y') NOT NULL,add Index_priv enum('N','Y') NOT NULL,add Alter_priv enum('N','Y') NOT NULL;
  66. END_OF_DATA
  67. res=$?
  68. echo ""
  69.  
  70. # If the new grant columns didn't exists, copy File -> Grant
  71. # and Create -> Alter, Index, References
  72.  
  73. if test $res = 0
  74. then
  75.   echo "Setting default privileges for the new grant, index and alter privileges"
  76.   $cmd <<END_OF_DATA
  77.   UPDATE user SET Grant_priv=File_priv,References_priv=Create_priv,Index_priv=Create_priv,Alter_priv=Create_priv;
  78.   UPDATE db SET References_priv=Create_priv,Index_priv=Create_priv,Alter_priv=Create_priv;
  79.   UPDATE host SET References_priv=Create_priv,Index_priv=Create_priv,Alter_priv=Create_priv;
  80. END_OF_DATA
  81.   echo ""
  82. fi
  83.  
  84. #
  85. # The second alter changes ssl_type to new 4.0.2 format
  86.  
  87. echo "Adding columns needed by GRANT .. REQUIRE (openssl)"
  88. echo "You can ignore any Duplicate column errors"
  89. $cmd <<END_OF_DATA
  90. ALTER TABLE user
  91. ADD ssl_type enum('','ANY','X509', 'SPECIFIED') NOT NULL,
  92. ADD ssl_cipher BLOB NOT NULL,
  93. ADD x509_issuer BLOB NOT NULL,
  94. ADD x509_subject BLOB NOT NULL;
  95. ALTER TABLE user MODIFY ssl_type enum('','ANY','X509', 'SPECIFIED') NOT NULL;
  96. END_OF_DATA
  97. echo ""
  98.  
  99. #
  100. # Create tables_priv and columns_priv if they don't exists
  101. #
  102.  
  103. echo "Creating the new table and column privilege tables"
  104.  
  105. $cmd <<END_OF_DATA
  106. CREATE TABLE IF NOT EXISTS tables_priv (
  107.   Host char(60) DEFAULT '' NOT NULL,
  108.   Db char(60) DEFAULT '' NOT NULL,
  109.   User char(16) DEFAULT '' NOT NULL,
  110.   Table_name char(60) DEFAULT '' NOT NULL,
  111.   Grantor char(77) DEFAULT '' NOT NULL,
  112.   Timestamp timestamp(14),
  113.   Table_priv set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter') DEFAULT '' NOT NULL,
  114.   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,
  115.   PRIMARY KEY (Host,Db,User,Table_name)
  116. );
  117. CREATE TABLE IF NOT EXISTS columns_priv (
  118.   Host char(60) DEFAULT '' NOT NULL,
  119.   Db char(60) DEFAULT '' NOT NULL,
  120.   User char(16) DEFAULT '' NOT NULL,
  121.   Table_name char(60) DEFAULT '' NOT NULL,
  122.   Column_name char(59) DEFAULT '' NOT NULL,
  123.   Timestamp timestamp(14),
  124.   Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL,
  125.   PRIMARY KEY (Host,Db,User,Table_name,Column_name)
  126. );
  127. END_OF_DATA
  128.  
  129. #
  130. # Name change of Type -> Column_priv from MySQL 3.22.12
  131. #
  132.  
  133. echo "Changing name of columns_priv.Type -> columns_priv.Column_priv"
  134. echo "You can ignore any Unknown column errors from this"
  135.  
  136. $cmd <<END_OF_DATA
  137. ALTER TABLE columns_priv change Type Column_priv set('Select','Insert','Update','References') DEFAULT '' NOT NULL;
  138. END_OF_DATA
  139. echo ""
  140.  
  141. #
  142. # Add the new 'type' column to the func table.
  143. #
  144.  
  145. echo "Fixing the func table"
  146. echo "You can ignore any Duplicate column errors"
  147.  
  148. $cmd <<EOF
  149. alter table func add type enum ('function','aggregate') NOT NULL;
  150. EOF
  151. echo ""
  152.  
  153. #
  154. # Change the user,db and host tables to MySQL 4.0 format
  155. #
  156.  
  157. echo "Adding new fields used by MySQL 4.0.2 to the privilege tables"
  158. echo "You can ignore any Duplicate column errors"
  159.  
  160. $cmd <<END_OF_DATA
  161. alter table user
  162. add Show_db_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER alter_priv,
  163. add Super_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Show_db_priv,
  164. add Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Super_priv,
  165. add Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Create_tmp_table_priv,
  166. add Execute_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Lock_tables_priv,
  167. add Repl_slave_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Execute_priv,
  168. add Repl_client_priv enum('N','Y') DEFAULT 'N' NOT NULL AFTER Repl_slave_priv;
  169. END_OF_DATA
  170.  
  171. if test $? -eq "0"
  172. then
  173.   # Convert privileges so that users have similar privileges as before
  174.   echo ""
  175.   echo "Updating new privileges in MySQL 4.0.2 from old ones"
  176.   $cmd <<END_OF_DATA
  177.   update user set show_db_priv= select_priv, super_priv=process_priv, execute_priv=process_priv, create_tmp_table_priv='Y', Lock_tables_priv='Y', Repl_slave_priv=file_priv, Repl_client_priv=file_priv where user<>"";
  178. END_OF_DATA
  179.   echo ""
  180. fi
  181.  
  182. # Add fields that can be used to limit number of questions and connections
  183. # for some users.
  184.  
  185. $cmd <<END_OF_DATA
  186. alter table user
  187. add max_questions int(11) NOT NULL AFTER x509_subject,
  188. add max_updates   int(11) unsigned NOT NULL AFTER max_questions,
  189. add max_connections int(11) unsigned NOT NULL AFTER max_updates;
  190. END_OF_DATA
  191.  
  192. #
  193. # Add Create_tmp_table_priv and Lock_tables_priv to db and host
  194. #
  195.  
  196. $cmd <<END_OF_DATA
  197. alter table db
  198. add Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,
  199. add Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL;
  200. alter table host
  201. add Create_tmp_table_priv enum('N','Y') DEFAULT 'N' NOT NULL,
  202. add Lock_tables_priv enum('N','Y') DEFAULT 'N' NOT NULL;
  203. END_OF_DATA
  204.