server_privileges.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. /**
  8. *
  9. */
  10. require_once 'libraries/common.inc.php';
  11. /**
  12. * functions implementation for this script
  13. */
  14. require_once 'libraries/display_change_password.lib.php';
  15. require_once 'libraries/server_privileges.lib.php';
  16. /**
  17. * Does the common work
  18. */
  19. $response = PMA_Response::getInstance();
  20. $header = $response->getHeader();
  21. $scripts = $header->getScripts();
  22. $scripts->addFile('server_privileges.js');
  23. $_add_user_error = false;
  24. if (isset ($_REQUEST['username'])) {
  25. $username = $_REQUEST['username'];
  26. }
  27. if (isset ($_REQUEST['hostname'])) {
  28. $hostname = $_REQUEST['hostname'];
  29. }
  30. /**
  31. * Sets globals from $_POST patterns, for privileges and max_* vars
  32. */
  33. $post_patterns = array(
  34. '/_priv$/i',
  35. '/^max_/i'
  36. );
  37. foreach (array_keys($_POST) as $post_key) {
  38. foreach ($post_patterns as $one_post_pattern) {
  39. if (preg_match($one_post_pattern, $post_key)) {
  40. $GLOBALS[$post_key] = $_POST[$post_key];
  41. }
  42. }
  43. }
  44. require 'libraries/server_common.inc.php';
  45. $conditional_class = 'ajax';
  46. /**
  47. * Messages are built using the message name
  48. */
  49. $strPrivDescAllPrivileges = __('Includes all privileges except GRANT.');
  50. $strPrivDescAlter = __('Allows altering the structure of existing tables.');
  51. $strPrivDescAlterRoutine = __('Allows altering and dropping stored routines.');
  52. $strPrivDescCreateDb = __('Allows creating new databases and tables.');
  53. $strPrivDescCreateRoutine = __('Allows creating stored routines.');
  54. $strPrivDescCreateTbl = __('Allows creating new tables.');
  55. $strPrivDescCreateTmpTable = __('Allows creating temporary tables.');
  56. $strPrivDescCreateUser = __('Allows creating, dropping and renaming user accounts.');
  57. $strPrivDescCreateView = __('Allows creating new views.');
  58. $strPrivDescDelete = __('Allows deleting data.');
  59. $strPrivDescDropDb = __('Allows dropping databases and tables.');
  60. $strPrivDescDropTbl = __('Allows dropping tables.');
  61. $strPrivDescEvent = __('Allows to set up events for the event scheduler');
  62. $strPrivDescExecute = __('Allows executing stored routines.');
  63. $strPrivDescFile = __('Allows importing data from and exporting data into files.');
  64. $strPrivDescGrant = __('Allows adding users and privileges without reloading the privilege tables.');
  65. $strPrivDescIndex = __('Allows creating and dropping indexes.');
  66. $strPrivDescInsert = __('Allows inserting and replacing data.');
  67. $strPrivDescLockTables = __('Allows locking tables for the current thread.');
  68. $strPrivDescMaxConnections = __('Limits the number of new connections the user may open per hour.');
  69. $strPrivDescMaxQuestions = __('Limits the number of queries the user may send to the server per hour.');
  70. $strPrivDescMaxUpdates = __('Limits the number of commands that change any table or database the user may execute per hour.');
  71. $strPrivDescMaxUserConnections = __('Limits the number of simultaneous connections the user may have.');
  72. $strPrivDescProcess = __('Allows viewing processes of all users');
  73. $strPrivDescReferences = __('Has no effect in this MySQL version.');
  74. $strPrivDescReload = __('Allows reloading server settings and flushing the server\'s caches.');
  75. $strPrivDescReplClient = __('Allows the user to ask where the slaves / masters are.');
  76. $strPrivDescReplSlave = __('Needed for the replication slaves.');
  77. $strPrivDescSelect = __('Allows reading data.');
  78. $strPrivDescShowDb = __('Gives access to the complete list of databases.');
  79. $strPrivDescShowView = __('Allows performing SHOW CREATE VIEW queries.');
  80. $strPrivDescShutdown = __('Allows shutting down the server.');
  81. $strPrivDescSuper = __('Allows connecting, even if maximum number of connections is reached; required for most administrative operations like setting global variables or killing threads of other users.');
  82. $strPrivDescTrigger = __('Allows creating and dropping triggers');
  83. $strPrivDescUpdate = __('Allows changing data.');
  84. $strPrivDescUsage = __('No privileges.');
  85. /**
  86. * Checks if a dropdown box has been used for selecting a database / table
  87. */
  88. if (PMA_isValid($_REQUEST['pred_tablename'])) {
  89. $tablename = $_REQUEST['pred_tablename'];
  90. } elseif (PMA_isValid($_REQUEST['tablename'])) {
  91. $tablename = $_REQUEST['tablename'];
  92. } else {
  93. unset($tablename);
  94. }
  95. if (PMA_isValid($_REQUEST['pred_dbname'])) {
  96. $dbname = $_REQUEST['pred_dbname'];
  97. unset($pred_dbname);
  98. } elseif (PMA_isValid($_REQUEST['dbname'])) {
  99. $dbname = $_REQUEST['dbname'];
  100. } else {
  101. unset($dbname);
  102. unset($tablename);
  103. }
  104. if (isset($dbname)) {
  105. $unescaped_db = PMA_Util::unescapeMysqlWildcards($dbname);
  106. $db_and_table = PMA_Util::backquote($unescaped_db) . '.';
  107. if (isset($tablename)) {
  108. $db_and_table .= PMA_Util::backquote($tablename);
  109. } else {
  110. $db_and_table .= '*';
  111. }
  112. } else {
  113. $db_and_table = '*.*';
  114. }
  115. // check if given $dbname is a wildcard or not
  116. if (isset($dbname)) {
  117. //if (preg_match('/\\\\(?:_|%)/i', $dbname)) {
  118. if (preg_match('/(?<!\\\\)(?:_|%)/i', $dbname)) {
  119. $dbname_is_wildcard = true;
  120. } else {
  121. $dbname_is_wildcard = false;
  122. }
  123. }
  124. /**
  125. * Checks if the user is allowed to do what he tries to...
  126. */
  127. if (! $is_superuser) {
  128. $response->addHTML(
  129. '<h2>' . "\n"
  130. . PMA_Util::getIcon('b_usrlist.png')
  131. . __('Privileges') . "\n"
  132. . '</h2>' . "\n"
  133. );
  134. $response->addHTML(PMA_Message::error(__('No Privileges'))->getDisplay());
  135. exit;
  136. }
  137. /**
  138. * Changes / copies a user, part I
  139. */
  140. if (isset($_REQUEST['change_copy'])) {
  141. $user_host_condition = ' WHERE `User` = '
  142. . "'". PMA_Util::sqlAddSlashes($_REQUEST['old_username']) . "'"
  143. . ' AND `Host` = '
  144. . "'" . PMA_Util::sqlAddSlashes($_REQUEST['old_hostname']) . "';";
  145. $row = PMA_DBI_fetch_single_row(
  146. 'SELECT * FROM `mysql`.`user` ' . $user_host_condition
  147. );
  148. if (! $row) {
  149. PMA_Message::notice(__('No user found.'))->display();
  150. unset($_REQUEST['change_copy']);
  151. } else {
  152. extract($row, EXTR_OVERWRITE);
  153. // Recent MySQL versions have the field "Password" in mysql.user,
  154. // so the previous extract creates $Password but this script
  155. // uses $password
  156. if (! isset($password) && isset($Password)) {
  157. $password = $Password;
  158. }
  159. $queries = array();
  160. }
  161. }
  162. /**
  163. * Adds a user
  164. * (Changes / copies a user, part II)
  165. */
  166. if (isset($_REQUEST['adduser_submit']) || isset($_REQUEST['change_copy'])) {
  167. $sql_query = '';
  168. if ($_POST['pred_username'] == 'any') {
  169. $username = '';
  170. }
  171. switch ($_POST['pred_hostname']) {
  172. case 'any':
  173. $hostname = '%';
  174. break;
  175. case 'localhost':
  176. $hostname = 'localhost';
  177. break;
  178. case 'hosttable':
  179. $hostname = '';
  180. break;
  181. case 'thishost':
  182. $_user_name = PMA_DBI_fetch_value('SELECT USER()');
  183. $hostname = substr($_user_name, (strrpos($_user_name, '@') + 1));
  184. unset($_user_name);
  185. break;
  186. }
  187. $sql = "SELECT '1' FROM `mysql`.`user`"
  188. . " WHERE `User` = '" . PMA_Util::sqlAddSlashes($username) . "'"
  189. . " AND `Host` = '" . PMA_Util::sqlAddSlashes($hostname) . "';";
  190. if (PMA_DBI_fetch_value($sql) == 1) {
  191. $message = PMA_Message::error(__('The user %s already exists!'));
  192. $message->addParam('[em]\'' . $username . '\'@\'' . $hostname . '\'[/em]');
  193. $_REQUEST['adduser'] = true;
  194. $_add_user_error = true;
  195. } else {
  196. list($create_user_real, $create_user_show, $real_sql_query, $sql_query)
  197. = PMA_getSqlQueriesForDisplayAndAddUser(
  198. $username, $hostname, (isset ($password) ? $password : '')
  199. );
  200. if (empty($_REQUEST['change_copy'])) {
  201. $_error = false;
  202. if (isset($create_user_real)) {
  203. if (! PMA_DBI_try_query($create_user_real)) {
  204. $_error = true;
  205. }
  206. $sql_query = $create_user_show . $sql_query;
  207. }
  208. list($sql_query, $message) = PMA_addUserAndCreateDatabase(
  209. $_error, $real_sql_query, $sql_query, $username, $hostname,
  210. isset($dbname) ? $dbname : null
  211. );
  212. } else {
  213. if (isset($create_user_real)) {
  214. $queries[] = $create_user_real;
  215. }
  216. $queries[] = $real_sql_query;
  217. // we put the query containing the hidden password in
  218. // $queries_for_display, at the same position occupied
  219. // by the real query in $queries
  220. $tmp_count = count($queries);
  221. if (isset($create_user_real)) {
  222. $queries_for_display[$tmp_count - 2] = $create_user_show;
  223. }
  224. $queries_for_display[$tmp_count - 1] = $sql_query;
  225. }
  226. unset($res, $real_sql_query);
  227. }
  228. }
  229. /**
  230. * Changes / copies a user, part III
  231. */
  232. if (isset($_REQUEST['change_copy'])) {
  233. $queries = PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser(
  234. $queries, $username, $hostname
  235. );
  236. }
  237. /**
  238. * Updates privileges
  239. */
  240. if (! empty($_POST['update_privs'])) {
  241. list($sql_query, $message) = PMA_updatePrivileges(
  242. $username,
  243. $hostname,
  244. (isset($tablename) ? $tablename : ''),
  245. (isset($dbname) ? $dbname : '')
  246. );
  247. }
  248. /**
  249. * Revokes Privileges
  250. */
  251. if (isset($_REQUEST['revokeall'])) {
  252. list ($message, $sql_query) = PMA_getMessageAndSqlQueryForPrivilegesRevoke(
  253. $db_and_table,
  254. (isset($dbname) ? $dbname : ''),
  255. (isset($tablename) ? $tablename : ''),
  256. $username, $hostname
  257. );
  258. }
  259. /**
  260. * Updates the password
  261. */
  262. if (isset($_REQUEST['change_pw'])) {
  263. $message = PMA_getMessageForUpdatePassword(
  264. $err_url, $username, $hostname
  265. );
  266. }
  267. /**
  268. * Deletes users
  269. * (Changes / copies a user, part IV)
  270. */
  271. if (isset($_REQUEST['delete'])
  272. || (isset($_REQUEST['change_copy']) && $_REQUEST['mode'] < 4)
  273. ) {
  274. if (isset($_REQUEST['change_copy'])) {
  275. $selected_usr = array(
  276. $_REQUEST['old_username'] . '&amp;#27;' . $_REQUEST['old_hostname']
  277. );
  278. } else {
  279. $selected_usr = $_REQUEST['selected_usr'];
  280. $queries = array();
  281. }
  282. foreach ($selected_usr as $each_user) {
  283. list($this_user, $this_host) = explode('&amp;#27;', $each_user);
  284. $queries[] = '# '
  285. . sprintf(
  286. __('Deleting %s'),
  287. '\'' . $this_user . '\'@\'' . $this_host . '\''
  288. )
  289. . ' ...';
  290. $queries[] = 'DROP USER \''
  291. . PMA_Util::sqlAddSlashes($this_user)
  292. . '\'@\'' . PMA_Util::sqlAddSlashes($this_host) . '\';';
  293. if (isset($_REQUEST['drop_users_db'])) {
  294. $queries[] = 'DROP DATABASE IF EXISTS '
  295. . PMA_Util::backquote($this_user) . ';';
  296. $GLOBALS['reload'] = true;
  297. }
  298. }
  299. if (empty($_REQUEST['change_copy'])) {
  300. list($sql_query, $message) = PMA_deleteUser($queries);
  301. }
  302. }
  303. /**
  304. * Changes / copies a user, part V
  305. */
  306. if (isset($_REQUEST['change_copy'])) {
  307. $tmp_count = 0;
  308. foreach ($queries as $sql_query) {
  309. if ($sql_query{0} != '#') {
  310. PMA_DBI_query($sql_query);
  311. }
  312. // when there is a query containing a hidden password, take it
  313. // instead of the real query sent
  314. if (isset($queries_for_display[$tmp_count])) {
  315. $queries[$tmp_count] = $queries_for_display[$tmp_count];
  316. }
  317. $tmp_count++;
  318. }
  319. $message = PMA_Message::success();
  320. $sql_query = join("\n", $queries);
  321. }
  322. /**
  323. * Reloads the privilege tables into memory
  324. */
  325. if (isset($_REQUEST['flush_privileges'])) {
  326. $sql_query = 'FLUSH PRIVILEGES;';
  327. PMA_DBI_query($sql_query);
  328. $message = PMA_Message::success(__('The privileges were reloaded successfully.'));
  329. }
  330. /**
  331. * some standard links
  332. */
  333. list($link_edit, $link_revoke, $link_export)
  334. = PMA_getStandardLinks($conditional_class);
  335. /**
  336. * If we are in an Ajax request for Create User/Edit User/Revoke User/
  337. * Flush Privileges, show $message and exit.
  338. */
  339. if ($GLOBALS['is_ajax_request']
  340. && empty($_REQUEST['ajax_page_request'])
  341. && ! isset($_REQUEST['export'])
  342. && (! isset($_REQUEST['submit_mult']) || $_REQUEST['submit_mult'] != 'export')
  343. && (! isset($_REQUEST['adduser']) || $_add_user_error)
  344. && (! isset($_REQUEST['initial']) || empty($_REQUEST['initial']))
  345. && ! isset($_REQUEST['showall'])
  346. && ! isset($_REQUEST['edit_user_dialog'])
  347. && ! isset($_REQUEST['db_specific'])
  348. ) {
  349. $extra_data = PMA_getExtraDataForAjaxBehavior(
  350. (isset($password) ? $password : ''),
  351. $link_export,
  352. (isset($sql_query) ? $sql_query : ''),
  353. $link_edit,
  354. (isset($hostname) ? $hostname : ''),
  355. (isset($username) ? $username : '')
  356. );
  357. if (! empty($message) && $message instanceof PMA_Message) {
  358. $response = PMA_Response::getInstance();
  359. $response->isSuccess($message->isSuccess());
  360. $response->addJSON('message', $message);
  361. $response->addJSON($extra_data);
  362. exit;
  363. }
  364. }
  365. /**
  366. * Displays the links
  367. */
  368. if (isset($_REQUEST['viewing_mode']) && $_REQUEST['viewing_mode'] == 'db') {
  369. $_REQUEST['db'] = $_REQUEST['checkprivs'];
  370. $url_query .= '&amp;goto=db_operations.php';
  371. // Gets the database structure
  372. $sub_part = '_structure';
  373. ob_start();
  374. include 'libraries/db_info.inc.php';
  375. $content = ob_get_contents();
  376. ob_end_clean();
  377. $response->addHTML($content . "\n");
  378. } else {
  379. if (! empty($GLOBALS['message'])) {
  380. $response->addHTML(PMA_Util::getMessage($GLOBALS['message']));
  381. unset($GLOBALS['message']);
  382. }
  383. }
  384. /**
  385. * Displays the page
  386. */
  387. // export user definition
  388. if (isset($_REQUEST['export'])
  389. || (isset($_REQUEST['submit_mult']) && $_REQUEST['submit_mult'] == 'export')
  390. ) {
  391. list($title, $export) = PMA_getHtmlForExportUserDefinition(
  392. isset($username) ? $username : null,
  393. isset($hostname) ? $hostname : null
  394. );
  395. unset($username, $hostname, $grants, $one_grant);
  396. $response = PMA_Response::getInstance();
  397. if ($GLOBALS['is_ajax_request']) {
  398. $response->addJSON('message', $export);
  399. $response->addJSON('title', $title);
  400. exit;
  401. } else {
  402. $response->addHTML("<h2>$title</h2>$export");
  403. }
  404. }
  405. if (empty($_REQUEST['adduser'])
  406. && (! isset($_REQUEST['checkprivs'])
  407. || ! strlen($_REQUEST['checkprivs']))
  408. ) {
  409. if (! isset($username)) {
  410. // No username is given --> display the overview
  411. $response->addHTML(
  412. PMA_getHtmlForDisplayUserOverviewPage(
  413. $link_edit, $pmaThemeImage, $text_dir,
  414. $conditional_class, $link_export
  415. )
  416. );
  417. } else {
  418. // A user was selected -> display the user's properties
  419. // In an Ajax request, prevent cached values from showing
  420. if ($GLOBALS['is_ajax_request'] == true) {
  421. header('Cache-Control: no-cache');
  422. }
  423. $url_dbname = urlencode(
  424. str_replace(
  425. array('\_', '\%'),
  426. array('_', '%'), $_REQUEST['dbname']
  427. )
  428. );
  429. $response->addHTML(
  430. PMA_getHtmlForDisplayUserProperties(
  431. ((isset ($dbname_is_wildcard)) ? $dbname_is_wildcard : ''),
  432. $url_dbname, $username, $hostname, $link_edit, $link_revoke,
  433. (isset($dbname) ? $dbname : ''),
  434. (isset($tablename) ? $tablename : '')
  435. )
  436. );
  437. }
  438. } elseif (isset($_REQUEST['adduser'])) {
  439. // Add user
  440. $response->addHTML(
  441. PMA_getHtmlForAddUser((isset($dbname) ? $dbname : ''))
  442. );
  443. } else {
  444. // check the privileges for a particular database.
  445. $response->addHTML(
  446. PMA_getHtmlForSpecificDbPrivileges($link_edit, $conditional_class)
  447. );
  448. } // end if (empty($_REQUEST['adduser']) && empty($checkprivs))... elseif... else...
  449. ?>