server_privileges.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. use PhpMyAdmin\Core;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Relation;
  11. use PhpMyAdmin\Response;
  12. use PhpMyAdmin\Server\Common;
  13. use PhpMyAdmin\Server\Privileges;
  14. use PhpMyAdmin\Server\Users;
  15. use PhpMyAdmin\Template;
  16. /**
  17. * include common file
  18. */
  19. require_once 'libraries/common.inc.php';
  20. /**
  21. * functions implementation for this script
  22. */
  23. require_once 'libraries/check_user_privileges.inc.php';
  24. $relation = new Relation();
  25. $cfgRelation = $relation->getRelationsParam();
  26. /**
  27. * Does the common work
  28. */
  29. $response = Response::getInstance();
  30. $header = $response->getHeader();
  31. $scripts = $header->getScripts();
  32. $scripts->addFile('server_privileges.js');
  33. $scripts->addFile('vendor/zxcvbn.js');
  34. if ((isset($_GET['viewing_mode'])
  35. && $_GET['viewing_mode'] == 'server')
  36. && $cfgRelation['menuswork']
  37. ) {
  38. $response->addHTML('<div>');
  39. $response->addHTML(Users::getHtmlForSubMenusOnUsersPage('server_privileges.php'));
  40. }
  41. /**
  42. * Sets globals from $_POST patterns, for privileges and max_* vars
  43. */
  44. $post_patterns = array(
  45. '/_priv$/i',
  46. '/^max_/i'
  47. );
  48. Core::setPostAsGlobal($post_patterns);
  49. require 'libraries/server_common.inc.php';
  50. /**
  51. * Messages are built using the message name
  52. */
  53. $strPrivDescAllPrivileges = __('Includes all privileges except GRANT.');
  54. $strPrivDescAlter = __('Allows altering the structure of existing tables.');
  55. $strPrivDescAlterRoutine = __('Allows altering and dropping stored routines.');
  56. $strPrivDescCreateDb = __('Allows creating new databases and tables.');
  57. $strPrivDescCreateRoutine = __('Allows creating stored routines.');
  58. $strPrivDescCreateTbl = __('Allows creating new tables.');
  59. $strPrivDescCreateTmpTable = __('Allows creating temporary tables.');
  60. $strPrivDescCreateUser = __('Allows creating, dropping and renaming user accounts.');
  61. $strPrivDescCreateView = __('Allows creating new views.');
  62. $strPrivDescDelete = __('Allows deleting data.');
  63. $strPrivDescDeleteHistoricalRows = __('Allows deleting historical rows.');
  64. $strPrivDescDropDb = __('Allows dropping databases and tables.');
  65. $strPrivDescDropTbl = __('Allows dropping tables.');
  66. $strPrivDescEvent = __('Allows to set up events for the event scheduler.');
  67. $strPrivDescExecute = __('Allows executing stored routines.');
  68. $strPrivDescFile = __('Allows importing data from and exporting data into files.');
  69. $strPrivDescGrantTbl = __(
  70. 'Allows user to give to other users or remove from other users the privileges '
  71. . 'that user possess yourself.'
  72. );
  73. $strPrivDescIndex = __('Allows creating and dropping indexes.');
  74. $strPrivDescInsert = __('Allows inserting and replacing data.');
  75. $strPrivDescLockTables = __('Allows locking tables for the current thread.');
  76. $strPrivDescMaxConnections = __(
  77. 'Limits the number of new connections the user may open per hour.'
  78. );
  79. $strPrivDescMaxQuestions = __(
  80. 'Limits the number of queries the user may send to the server per hour.'
  81. );
  82. $strPrivDescMaxUpdates = __(
  83. 'Limits the number of commands that change any table or database '
  84. . 'the user may execute per hour.'
  85. );
  86. $strPrivDescMaxUserConnections = __(
  87. 'Limits the number of simultaneous connections the user may have.'
  88. );
  89. $strPrivDescProcess = __('Allows viewing processes of all users.');
  90. $strPrivDescReferences = __('Has no effect in this MySQL version.');
  91. $strPrivDescReload = __(
  92. 'Allows reloading server settings and flushing the server\'s caches.'
  93. );
  94. $strPrivDescReplClient = __(
  95. 'Allows the user to ask where the slaves / masters are.'
  96. );
  97. $strPrivDescReplSlave = __('Needed for the replication slaves.');
  98. $strPrivDescSelect = __('Allows reading data.');
  99. $strPrivDescShowDb = __('Gives access to the complete list of databases.');
  100. $strPrivDescShowView = __('Allows performing SHOW CREATE VIEW queries.');
  101. $strPrivDescShutdown = __('Allows shutting down the server.');
  102. $strPrivDescSuper = __(
  103. 'Allows connecting, even if maximum number of connections is reached; '
  104. . 'required for most administrative operations like setting global variables '
  105. . 'or killing threads of other users.'
  106. );
  107. $strPrivDescTrigger = __('Allows creating and dropping triggers.');
  108. $strPrivDescUpdate = __('Allows changing data.');
  109. $strPrivDescUsage = __('No privileges.');
  110. $_add_user_error = false;
  111. /**
  112. * Get DB information: username, hostname, dbname,
  113. * tablename, db_and_table, dbname_is_wildcard
  114. */
  115. list(
  116. $username, $hostname, $dbname, $tablename, $routinename,
  117. $db_and_table, $dbname_is_wildcard
  118. ) = Privileges::getDataForDBInfo();
  119. /**
  120. * Checks if the user is allowed to do what he tries to...
  121. */
  122. if (!$GLOBALS['dbi']->isSuperuser() && !$GLOBALS['is_grantuser']
  123. && !$GLOBALS['is_createuser']
  124. ) {
  125. $response->addHTML(
  126. Template::get('server/sub_page_header')->render([
  127. 'type' => 'privileges',
  128. 'is_image' => false,
  129. ])
  130. );
  131. $response->addHTML(
  132. Message::error(__('No Privileges'))
  133. ->getDisplay()
  134. );
  135. exit;
  136. }
  137. if (! $GLOBALS['is_grantuser'] && !$GLOBALS['is_createuser']) {
  138. $response->addHTML(Message::notice(
  139. __('You do not have privileges to manipulate with the users!')
  140. )->getDisplay());
  141. }
  142. /**
  143. * Checks if the user is using "Change Login Information / Copy User" dialog
  144. * only to update the password
  145. */
  146. if (isset($_POST['change_copy']) && $username == $_POST['old_username']
  147. && $hostname == $_POST['old_hostname']
  148. ) {
  149. $response->addHTML(
  150. Message::error(
  151. __(
  152. "Username and hostname didn't change. "
  153. . "If you only want to change the password, "
  154. . "'Change password' tab should be used."
  155. )
  156. )->getDisplay()
  157. );
  158. $response->setRequestStatus(false);
  159. exit;
  160. }
  161. /**
  162. * Changes / copies a user, part I
  163. */
  164. list($queries, $password) = Privileges::getDataForChangeOrCopyUser();
  165. /**
  166. * Adds a user
  167. * (Changes / copies a user, part II)
  168. */
  169. list($ret_message, $ret_queries, $queries_for_display, $sql_query, $_add_user_error)
  170. = Privileges::addUser(
  171. isset($dbname)? $dbname : null,
  172. isset($username)? $username : null,
  173. isset($hostname)? $hostname : null,
  174. isset($password)? $password : null,
  175. $cfgRelation['menuswork']
  176. );
  177. //update the old variables
  178. if (isset($ret_queries)) {
  179. $queries = $ret_queries;
  180. unset($ret_queries);
  181. }
  182. if (isset($ret_message)) {
  183. $message = $ret_message;
  184. unset($ret_message);
  185. }
  186. /**
  187. * Changes / copies a user, part III
  188. */
  189. if (isset($_POST['change_copy'])) {
  190. $queries = Privileges::getDbSpecificPrivsQueriesForChangeOrCopyUser(
  191. $queries, $username, $hostname
  192. );
  193. }
  194. $itemType = '';
  195. if (! empty($routinename)) {
  196. $itemType = Privileges::getRoutineType($dbname, $routinename);
  197. }
  198. /**
  199. * Updates privileges
  200. */
  201. if (! empty($_POST['update_privs'])) {
  202. if (is_array($dbname)) {
  203. foreach ($dbname as $key => $db_name) {
  204. list($sql_query[$key], $message) = Privileges::updatePrivileges(
  205. (isset($username) ? $username : ''),
  206. (isset($hostname) ? $hostname : ''),
  207. (isset($tablename)
  208. ? $tablename
  209. : (isset($routinename) ? $routinename : '')),
  210. (isset($db_name) ? $db_name : ''),
  211. $itemType
  212. );
  213. }
  214. $sql_query = implode("\n", $sql_query);
  215. } else {
  216. list($sql_query, $message) = Privileges::updatePrivileges(
  217. (isset($username) ? $username : ''),
  218. (isset($hostname) ? $hostname : ''),
  219. (isset($tablename)
  220. ? $tablename
  221. : (isset($routinename) ? $routinename : '')),
  222. (isset($dbname) ? $dbname : ''),
  223. $itemType
  224. );
  225. }
  226. }
  227. /**
  228. * Assign users to user groups
  229. */
  230. if (! empty($_POST['changeUserGroup']) && $cfgRelation['menuswork']
  231. && $GLOBALS['dbi']->isSuperuser() && $GLOBALS['is_createuser']
  232. ) {
  233. Privileges::setUserGroup($username, $_POST['userGroup']);
  234. $message = Message::success();
  235. }
  236. /**
  237. * Revokes Privileges
  238. */
  239. if (isset($_POST['revokeall'])) {
  240. list ($message, $sql_query) = Privileges::getMessageAndSqlQueryForPrivilegesRevoke(
  241. (isset($dbname) ? $dbname : ''),
  242. (isset($tablename)
  243. ? $tablename
  244. : (isset($routinename) ? $routinename : '')),
  245. $username,
  246. $hostname,
  247. $itemType
  248. );
  249. }
  250. /**
  251. * Updates the password
  252. */
  253. if (isset($_POST['change_pw'])) {
  254. $message = Privileges::updatePassword(
  255. $err_url, $username, $hostname
  256. );
  257. }
  258. /**
  259. * Deletes users
  260. * (Changes / copies a user, part IV)
  261. */
  262. if (isset($_POST['delete'])
  263. || (isset($_POST['change_copy']) && $_POST['mode'] < 4)
  264. ) {
  265. $queries = Privileges::getDataForDeleteUsers($queries);
  266. if (empty($_POST['change_copy'])) {
  267. list($sql_query, $message) = Privileges::deleteUser($queries);
  268. }
  269. }
  270. /**
  271. * Changes / copies a user, part V
  272. */
  273. if (isset($_POST['change_copy'])) {
  274. $queries = Privileges::getDataForQueries($queries, $queries_for_display);
  275. $message = Message::success();
  276. $sql_query = join("\n", $queries);
  277. }
  278. /**
  279. * Reloads the privilege tables into memory
  280. */
  281. $message_ret = Privileges::updateMessageForReload();
  282. if (isset($message_ret)) {
  283. $message = $message_ret;
  284. unset($message_ret);
  285. }
  286. /**
  287. * If we are in an Ajax request for Create User/Edit User/Revoke User/
  288. * Flush Privileges, show $message and exit.
  289. */
  290. if ($response->isAjax()
  291. && empty($_REQUEST['ajax_page_request'])
  292. && ! isset($_GET['export'])
  293. && (! isset($_POST['submit_mult']) || $_POST['submit_mult'] != 'export')
  294. && ((! isset($_GET['initial']) || $_GET['initial'] === null
  295. || $_GET['initial'] === '')
  296. || (isset($_POST['delete']) && $_POST['delete'] === __('Go')))
  297. && ! isset($_GET['showall'])
  298. && ! isset($_GET['edit_user_group_dialog'])
  299. ) {
  300. $extra_data = Privileges::getExtraDataForAjaxBehavior(
  301. (isset($password) ? $password : ''),
  302. (isset($sql_query) ? $sql_query : ''),
  303. (isset($hostname) ? $hostname : ''),
  304. (isset($username) ? $username : '')
  305. );
  306. if (! empty($message) && $message instanceof Message) {
  307. $response->setRequestStatus($message->isSuccess());
  308. $response->addJSON('message', $message);
  309. $response->addJSON($extra_data);
  310. exit;
  311. }
  312. }
  313. /**
  314. * Displays the links
  315. */
  316. if (isset($_GET['viewing_mode']) && $_GET['viewing_mode'] == 'db') {
  317. $GLOBALS['db'] = $_REQUEST['db'] = $_GET['checkprivsdb'];
  318. $url_query .= '&amp;goto=db_operations.php';
  319. // Gets the database structure
  320. $sub_part = '_structure';
  321. ob_start();
  322. list(
  323. $tables,
  324. $num_tables,
  325. $total_num_tables,
  326. $sub_part,
  327. $is_show_stats,
  328. $db_is_system_schema,
  329. $tooltip_truename,
  330. $tooltip_aliasname,
  331. $pos
  332. ) = PhpMyAdmin\Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
  333. $content = ob_get_contents();
  334. ob_end_clean();
  335. $response->addHTML($content . "\n");
  336. } else {
  337. if (! empty($GLOBALS['message'])) {
  338. $response->addHTML(PhpMyAdmin\Util::getMessage($GLOBALS['message']));
  339. unset($GLOBALS['message']);
  340. }
  341. }
  342. /**
  343. * Displays the page
  344. */
  345. $response->addHTML(
  346. Privileges::getHtmlForUserGroupDialog(
  347. isset($username)? $username : null,
  348. $cfgRelation['menuswork']
  349. )
  350. );
  351. // export user definition
  352. if (isset($_GET['export'])
  353. || (isset($_POST['submit_mult']) && $_POST['submit_mult'] == 'export')
  354. ) {
  355. list($title, $export) = Privileges::getListForExportUserDefinition(
  356. isset($username) ? $username : null,
  357. isset($hostname) ? $hostname : null
  358. );
  359. unset($username, $hostname, $grants, $one_grant);
  360. if ($response->isAjax()) {
  361. $response->addJSON('message', $export);
  362. $response->addJSON('title', $title);
  363. exit;
  364. } else {
  365. $response->addHTML("<h2>$title</h2>$export");
  366. }
  367. }
  368. if (isset($_GET['adduser'])) {
  369. // Add user
  370. $response->addHTML(
  371. Privileges::getHtmlForAddUser((isset($dbname) ? $dbname : ''))
  372. );
  373. } elseif (isset($_GET['checkprivsdb'])) {
  374. if (isset($_GET['checkprivstable'])) {
  375. // check the privileges for a particular table.
  376. $response->addHTML(
  377. Privileges::getHtmlForSpecificTablePrivileges(
  378. $_GET['checkprivsdb'], $_GET['checkprivstable']
  379. )
  380. );
  381. } else {
  382. // check the privileges for a particular database.
  383. $response->addHTML(
  384. Privileges::getHtmlForSpecificDbPrivileges($_GET['checkprivsdb'])
  385. );
  386. }
  387. } else {
  388. if (isset($dbname) && ! is_array($dbname)) {
  389. $url_dbname = urlencode(
  390. str_replace(
  391. array('\_', '\%'),
  392. array('_', '%'),
  393. $dbname
  394. )
  395. );
  396. }
  397. if (! isset($username)) {
  398. // No username is given --> display the overview
  399. $response->addHTML(
  400. Privileges::getHtmlForUserOverview($pmaThemeImage, $text_dir)
  401. );
  402. } elseif (!empty($routinename)) {
  403. $response->addHTML(
  404. Privileges::getHtmlForRoutineSpecificPrivileges(
  405. $username, $hostname, $dbname, $routinename,
  406. (isset($url_dbname) ? $url_dbname : '')
  407. )
  408. );
  409. } else {
  410. // A user was selected -> display the user's properties
  411. // In an Ajax request, prevent cached values from showing
  412. if ($response->isAjax()) {
  413. header('Cache-Control: no-cache');
  414. }
  415. $response->addHTML(
  416. Privileges::getHtmlForUserProperties(
  417. (isset($dbname_is_wildcard) ? $dbname_is_wildcard : ''),
  418. (isset($url_dbname) ? $url_dbname : ''),
  419. $username, $hostname,
  420. (isset($dbname) ? $dbname : ''),
  421. (isset($tablename) ? $tablename : '')
  422. )
  423. );
  424. }
  425. }
  426. if ((isset($_GET['viewing_mode']) && $_GET['viewing_mode'] == 'server')
  427. && $GLOBALS['cfgRelation']['menuswork']
  428. ) {
  429. $response->addHTML('</div>');
  430. }