server_privileges.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Server privileges and users manipulations
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * include common file
  10. */
  11. require_once 'libraries/common.inc.php';
  12. /**
  13. * functions implementation for this script
  14. */
  15. require_once 'libraries/display_change_password.lib.php';
  16. require_once 'libraries/server_privileges.lib.php';
  17. $cfgRelation = PMA_getRelationsParam();
  18. /**
  19. * Does the common work
  20. */
  21. $response = PMA_Response::getInstance();
  22. $header = $response->getHeader();
  23. $scripts = $header->getScripts();
  24. $scripts->addFile('server_privileges.js');
  25. if ((isset($_REQUEST['viewing_mode']) && $_REQUEST['viewing_mode'] == 'server')
  26. && $GLOBALS['cfgRelation']['menuswork']
  27. ) {
  28. include_once 'libraries/server_users.lib.php';
  29. $response->addHTML('<div>');
  30. $response->addHTML(PMA_getHtmlForSubMenusOnUsersPage('server_privileges.php'));
  31. }
  32. /**
  33. * Sets globals from $_POST patterns, for privileges and max_* vars
  34. */
  35. $post_patterns = array(
  36. '/_priv$/i',
  37. '/^max_/i'
  38. );
  39. foreach (array_keys($_POST) as $post_key) {
  40. foreach ($post_patterns as $one_post_pattern) {
  41. if (preg_match($one_post_pattern, $post_key)) {
  42. $GLOBALS[$post_key] = $_POST[$post_key];
  43. }
  44. }
  45. }
  46. require 'libraries/server_common.inc.php';
  47. /**
  48. * Messages are built using the message name
  49. */
  50. $strPrivDescAllPrivileges = __('Includes all privileges except GRANT.');
  51. $strPrivDescAlter = __('Allows altering the structure of existing tables.');
  52. $strPrivDescAlterRoutine = __('Allows altering and dropping stored routines.');
  53. $strPrivDescCreateDb = __('Allows creating new databases and tables.');
  54. $strPrivDescCreateRoutine = __('Allows creating stored routines.');
  55. $strPrivDescCreateTbl = __('Allows creating new tables.');
  56. $strPrivDescCreateTmpTable = __('Allows creating temporary tables.');
  57. $strPrivDescCreateUser = __('Allows creating, dropping and renaming user accounts.');
  58. $strPrivDescCreateView = __('Allows creating new views.');
  59. $strPrivDescDelete = __('Allows deleting data.');
  60. $strPrivDescDropDb = __('Allows dropping databases and tables.');
  61. $strPrivDescDropTbl = __('Allows dropping tables.');
  62. $strPrivDescEvent = __('Allows to set up events for the event scheduler.');
  63. $strPrivDescExecute = __('Allows executing stored routines.');
  64. $strPrivDescFile = __('Allows importing data from and exporting data into files.');
  65. $strPrivDescGrant = __(
  66. 'Allows adding users and privileges without reloading the privilege tables.'
  67. );
  68. $strPrivDescIndex = __('Allows creating and dropping indexes.');
  69. $strPrivDescInsert = __('Allows inserting and replacing data.');
  70. $strPrivDescLockTables = __('Allows locking tables for the current thread.');
  71. $strPrivDescMaxConnections = __(
  72. 'Limits the number of new connections the user may open per hour.'
  73. );
  74. $strPrivDescMaxQuestions = __(
  75. 'Limits the number of queries the user may send to the server per hour.'
  76. );
  77. $strPrivDescMaxUpdates = __(
  78. 'Limits the number of commands that change any table or database '
  79. . 'the user may execute per hour.'
  80. );
  81. $strPrivDescMaxUserConnections = __(
  82. 'Limits the number of simultaneous connections the user may have.'
  83. );
  84. $strPrivDescProcess = __('Allows viewing processes of all users.');
  85. $strPrivDescReferences = __('Has no effect in this MySQL version.');
  86. $strPrivDescReload = __(
  87. 'Allows reloading server settings and flushing the server\'s caches.'
  88. );
  89. $strPrivDescReplClient = __(
  90. 'Allows the user to ask where the slaves / masters are.'
  91. );
  92. $strPrivDescReplSlave = __('Needed for the replication slaves.');
  93. $strPrivDescSelect = __('Allows reading data.');
  94. $strPrivDescShowDb = __('Gives access to the complete list of databases.');
  95. $strPrivDescShowView = __('Allows performing SHOW CREATE VIEW queries.');
  96. $strPrivDescShutdown = __('Allows shutting down the server.');
  97. $strPrivDescSuper = __(
  98. 'Allows connecting, even if maximum number of connections is reached; '
  99. . 'required for most administrative operations like setting global variables '
  100. . 'or killing threads of other users.'
  101. );
  102. $strPrivDescTrigger = __('Allows creating and dropping triggers.');
  103. $strPrivDescUpdate = __('Allows changing data.');
  104. $strPrivDescUsage = __('No privileges.');
  105. $_add_user_error = false;
  106. /**
  107. * Get DB information: username, hostname, dbname,
  108. * tablename, db_and_table, dbname_is_wildcard
  109. */
  110. list(
  111. $username, $hostname, $dbname, $tablename,
  112. $db_and_table, $dbname_is_wildcard
  113. ) = PMA_getDataForDBInfo();
  114. /**
  115. * Checks if the user is allowed to do what he tries to...
  116. */
  117. if (! $is_superuser) {
  118. $response->addHTML(PMA_getHtmlForSubPageHeader('privileges', '', false));
  119. $response->addHTML(PMA_Message::error(__('No Privileges'))->getDisplay());
  120. exit;
  121. }
  122. /**
  123. * Checks if the user is using "Change Login Information / Copy User" dialog
  124. * only to update the password
  125. */
  126. if (isset($_REQUEST['change_copy']) && $username == $_REQUEST['old_username']
  127. && $hostname == $_REQUEST['old_hostname']
  128. ) {
  129. $response->addHTML(
  130. PMA_Message::error(__('Username and hostname didn\'t change.'))->getDisplay()
  131. );
  132. $response->isSuccess(false);
  133. exit;
  134. }
  135. /**
  136. * Changes / copies a user, part I
  137. */
  138. list($queries, $password) = PMA_getDataForChangeOrCopyUser();
  139. /**
  140. * Adds a user
  141. * (Changes / copies a user, part II)
  142. */
  143. list($ret_message, $ret_queries, $queries_for_display, $sql_query, $_add_user_error)
  144. = PMA_addUser(
  145. isset($dbname)? $dbname : null,
  146. isset($username)? $username : null,
  147. isset($hostname)? $hostname : null,
  148. isset($password)? $password : null,
  149. $cfgRelation['menuswork']
  150. );
  151. //update the old variables
  152. if (isset($ret_queries)) {
  153. $queries = $ret_queries;
  154. unset($ret_queries);
  155. }
  156. if (isset($ret_message)) {
  157. $message = $ret_message;
  158. unset($ret_message);
  159. }
  160. /**
  161. * Changes / copies a user, part III
  162. */
  163. if (isset($_REQUEST['change_copy'])) {
  164. $queries = PMA_getDbSpecificPrivsQueriesForChangeOrCopyUser(
  165. $queries, $username, $hostname
  166. );
  167. }
  168. /**
  169. * Updates privileges
  170. */
  171. if (! empty($_POST['update_privs'])) {
  172. list($sql_query, $message) = PMA_updatePrivileges(
  173. (isset($username) ? $username : ''),
  174. (isset($hostname) ? $hostname : ''),
  175. (isset($tablename) ? $tablename : ''),
  176. (isset($dbname) ? $dbname : '')
  177. );
  178. }
  179. /**
  180. * Assign users to user groups
  181. */
  182. if (! empty($_REQUEST['changeUserGroup']) && $cfgRelation['menuswork']) {
  183. PMA_setUserGroup($username, $_REQUEST['userGroup']);
  184. $message = PMA_Message::success();
  185. }
  186. /**
  187. * Revokes Privileges
  188. */
  189. if (isset($_REQUEST['revokeall'])) {
  190. list ($message, $sql_query) = PMA_getMessageAndSqlQueryForPrivilegesRevoke(
  191. $db_and_table,
  192. (isset($dbname) ? $dbname : ''),
  193. (isset($tablename) ? $tablename : ''),
  194. $username, $hostname
  195. );
  196. }
  197. /**
  198. * Updates the password
  199. */
  200. if (isset($_REQUEST['change_pw'])) {
  201. $message = PMA_updatePassword(
  202. $err_url, $username, $hostname
  203. );
  204. }
  205. /**
  206. * Deletes users
  207. * (Changes / copies a user, part IV)
  208. */
  209. if (isset($_REQUEST['delete'])
  210. || (isset($_REQUEST['change_copy']) && $_REQUEST['mode'] < 4)
  211. ) {
  212. $queries = PMA_getDataForDeleteUsers($queries);
  213. if (empty($_REQUEST['change_copy'])) {
  214. list($sql_query, $message) = PMA_deleteUser($queries);
  215. }
  216. }
  217. /**
  218. * Changes / copies a user, part V
  219. */
  220. if (isset($_REQUEST['change_copy'])) {
  221. $queries = PMA_getDataForQueries($queries, $queries_for_display);
  222. $message = PMA_Message::success();
  223. $sql_query = join("\n", $queries);
  224. }
  225. /**
  226. * Reloads the privilege tables into memory
  227. */
  228. $message_ret = PMA_updateMessageForReload();
  229. if (isset($message_ret)) {
  230. $message = $message_ret;
  231. unset($message_ret);
  232. }
  233. /**
  234. * If we are in an Ajax request for Create User/Edit User/Revoke User/
  235. * Flush Privileges, show $message and exit.
  236. */
  237. if ($GLOBALS['is_ajax_request']
  238. && empty($_REQUEST['ajax_page_request'])
  239. && ! isset($_REQUEST['export'])
  240. && (! isset($_REQUEST['submit_mult']) || $_REQUEST['submit_mult'] != 'export')
  241. && (! isset($_REQUEST['adduser']) || $_add_user_error)
  242. && (! isset($_REQUEST['initial']) || empty($_REQUEST['initial']))
  243. && ! isset($_REQUEST['showall'])
  244. && ! isset($_REQUEST['edit_user_dialog'])
  245. && ! isset($_REQUEST['edit_user_group_dialog'])
  246. && ! isset($_REQUEST['db_specific'])
  247. ) {
  248. $extra_data = PMA_getExtraDataForAjaxBehavior(
  249. (isset($password) ? $password : ''),
  250. (isset($sql_query) ? $sql_query : ''),
  251. (isset($hostname) ? $hostname : ''),
  252. (isset($username) ? $username : '')
  253. );
  254. if (! empty($message) && $message instanceof PMA_Message) {
  255. $response = PMA_Response::getInstance();
  256. $response->isSuccess($message->isSuccess());
  257. $response->addJSON('message', $message);
  258. $response->addJSON($extra_data);
  259. exit;
  260. }
  261. }
  262. /**
  263. * Displays the links
  264. */
  265. if (isset($_REQUEST['viewing_mode']) && $_REQUEST['viewing_mode'] == 'db') {
  266. $_REQUEST['db'] = $_REQUEST['checkprivsdb'];
  267. $url_query .= '&amp;goto=db_operations.php';
  268. // Gets the database structure
  269. $sub_part = '_structure';
  270. ob_start();
  271. include 'libraries/db_info.inc.php';
  272. $content = ob_get_contents();
  273. ob_end_clean();
  274. $response->addHTML($content . "\n");
  275. } else {
  276. if (! empty($GLOBALS['message'])) {
  277. $response->addHTML(PMA_Util::getMessage($GLOBALS['message']));
  278. unset($GLOBALS['message']);
  279. }
  280. }
  281. /**
  282. * Displays the page
  283. */
  284. $response->addHTML(
  285. PMA_getHtmlForUserGroupDialog(
  286. isset($username)? $username : null,
  287. $cfgRelation['menuswork']
  288. )
  289. );
  290. // export user definition
  291. if (isset($_REQUEST['export'])
  292. || (isset($_REQUEST['submit_mult']) && $_REQUEST['submit_mult'] == 'export')
  293. ) {
  294. list($title, $export) = PMA_getListForExportUserDefinition(
  295. isset($username) ? $username : null,
  296. isset($hostname) ? $hostname : null
  297. );
  298. unset($username, $hostname, $grants, $one_grant);
  299. $response = PMA_Response::getInstance();
  300. if ($GLOBALS['is_ajax_request']) {
  301. $response->addJSON('message', $export);
  302. $response->addJSON('title', $title);
  303. exit;
  304. } else {
  305. $response->addHTML("<h2>$title</h2>$export");
  306. }
  307. }
  308. if (isset($_REQUEST['adduser'])) {
  309. // Add user
  310. $response->addHTML(
  311. PMA_getHtmlForAddUser((isset($dbname) ? $dbname : ''))
  312. );
  313. } elseif (isset($_REQUEST['checkprivsdb'])) {
  314. if (isset($_REQUEST['checkprivstable'])) {
  315. // check the privileges for a particular table.
  316. $response->addHTML(
  317. PMA_getHtmlForSpecificTablePrivileges(
  318. $_REQUEST['checkprivsdb'], $_REQUEST['checkprivstable']
  319. )
  320. );
  321. } else {
  322. // check the privileges for a particular database.
  323. $response->addHTML(
  324. PMA_getHtmlForSpecificDbPrivileges($_REQUEST['checkprivsdb'])
  325. );
  326. }
  327. } else {
  328. if (! isset($username)) {
  329. // No username is given --> display the overview
  330. $response->addHTML(
  331. PMA_getHtmlForUserOverview($pmaThemeImage, $text_dir)
  332. );
  333. } else {
  334. // A user was selected -> display the user's properties
  335. // In an Ajax request, prevent cached values from showing
  336. if ($GLOBALS['is_ajax_request'] == true) {
  337. header('Cache-Control: no-cache');
  338. }
  339. $url_dbname = urlencode(
  340. str_replace(
  341. array('\_', '\%'),
  342. array('_', '%'), $_REQUEST['dbname']
  343. )
  344. );
  345. $response->addHTML(
  346. PMA_getHtmlForUserProperties(
  347. ((isset ($dbname_is_wildcard)) ? $dbname_is_wildcard : ''),
  348. $url_dbname, $username, $hostname,
  349. (isset($dbname) ? $dbname : ''),
  350. (isset($tablename) ? $tablename : '')
  351. )
  352. );
  353. }
  354. }
  355. if ((isset($_REQUEST['viewing_mode']) && $_REQUEST['viewing_mode'] == 'server')
  356. && $GLOBALS['cfgRelation']['menuswork']
  357. ) {
  358. $response->addHTML('</div>');
  359. }
  360. ?>