server_privileges.php 14 KB

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