PageRenderTime 243ms CodeModel.GetById 93ms RepoModel.GetById 14ms app.codeStats 1ms

/system/application/third_party/adminer/adminer/user.inc.php

https://gitlab.com/sylver.gocloud/gocloudasia-college-system-framework
PHP | 185 lines | 167 code | 14 blank | 4 comment | 52 complexity | 02f0a7b5ed41699d64dfcbda89196d18 MD5 | raw file
  1. <?php
  2. $USER = $_GET["user"];
  3. $privileges = array("" => array("All privileges" => ""));
  4. foreach (get_rows("SHOW PRIVILEGES") as $row) {
  5. foreach (explode(",", ($row["Privilege"] == "Grant option" ? "" : $row["Context"])) as $context) {
  6. $privileges[$context][$row["Privilege"]] = $row["Comment"];
  7. }
  8. }
  9. $privileges["Server Admin"] += $privileges["File access on server"];
  10. $privileges["Databases"]["Create routine"] = $privileges["Procedures"]["Create routine"]; // MySQL bug #30305
  11. unset($privileges["Procedures"]["Create routine"]);
  12. $privileges["Columns"] = array();
  13. foreach (array("Select", "Insert", "Update", "References") as $val) {
  14. $privileges["Columns"][$val] = $privileges["Tables"][$val];
  15. }
  16. unset($privileges["Server Admin"]["Usage"]);
  17. foreach ($privileges["Tables"] as $key => $val) {
  18. unset($privileges["Databases"][$key]);
  19. }
  20. $new_grants = array();
  21. if ($_POST) {
  22. foreach ($_POST["objects"] as $key => $val) {
  23. $new_grants[$val] = (array) $new_grants[$val] + (array) $_POST["grants"][$key];
  24. }
  25. }
  26. $grants = array();
  27. $old_pass = "";
  28. if (isset($_GET["host"]) && ($result = $connection->query("SHOW GRANTS FOR " . q($USER) . "@" . q($_GET["host"])))) { //! use information_schema for MySQL 5 - column names in column privileges are not escaped
  29. while ($row = $result->fetch_row()) {
  30. if (preg_match('~GRANT (.*) ON (.*) TO ~', $row[0], $match) && preg_match_all('~ *([^(,]*[^ ,(])( *\\([^)]+\\))?~', $match[1], $matches, PREG_SET_ORDER)) { //! escape the part between ON and TO
  31. foreach ($matches as $val) {
  32. if ($val[1] != "USAGE") {
  33. $grants["$match[2]$val[2]"][$val[1]] = true;
  34. }
  35. if (preg_match('~ WITH GRANT OPTION~', $row[0])) { //! don't check inside strings and identifiers
  36. $grants["$match[2]$val[2]"]["GRANT OPTION"] = true;
  37. }
  38. }
  39. }
  40. if (preg_match("~ IDENTIFIED BY PASSWORD '([^']+)~", $row[0], $match)) {
  41. $old_pass = $match[1];
  42. }
  43. }
  44. }
  45. if ($_POST && !$error) {
  46. $old_user = (isset($_GET["host"]) ? q($USER) . "@" . q($_GET["host"]) : "''");
  47. if ($_POST["drop"]) {
  48. query_redirect("DROP USER $old_user", ME . "privileges=", lang('User has been dropped.'));
  49. } else {
  50. $new_user = q($_POST["user"]) . "@" . q($_POST["host"]); // if $_GET["host"] is not set then $new_user is always different
  51. $pass = $_POST["pass"];
  52. if ($pass != '' && !$_POST["hashed"]) {
  53. // compute hash in a separate query so that plain text password is not saved to history
  54. $pass = $connection->result("SELECT PASSWORD(" . q($pass) . ")");
  55. $error = !$pass;
  56. }
  57. $created = false;
  58. if (!$error) {
  59. if ($old_user != $new_user) {
  60. $created = queries(($connection->server_info < 5 ? "GRANT USAGE ON *.* TO" : "CREATE USER") . " $new_user IDENTIFIED BY PASSWORD " . q($pass));
  61. $error = !$created;
  62. } elseif ($pass != $old_pass) {
  63. queries("SET PASSWORD FOR $new_user = " . q($pass));
  64. }
  65. }
  66. if (!$error) {
  67. $revoke = array();
  68. foreach ($new_grants as $object => $grant) {
  69. if (isset($_GET["grant"])) {
  70. $grant = array_filter($grant);
  71. }
  72. $grant = array_keys($grant);
  73. if (isset($_GET["grant"])) {
  74. // no rights to mysql.user table
  75. $revoke = array_diff(array_keys(array_filter($new_grants[$object], 'strlen')), $grant);
  76. } elseif ($old_user == $new_user) {
  77. $old_grant = array_keys((array) $grants[$object]);
  78. $revoke = array_diff($old_grant, $grant);
  79. $grant = array_diff($grant, $old_grant);
  80. unset($grants[$object]);
  81. }
  82. if (preg_match('~^(.+)\\s*(\\(.*\\))?$~U', $object, $match) && (
  83. !grant("REVOKE", $revoke, $match[2], " ON $match[1] FROM $new_user") //! SQL injection
  84. || !grant("GRANT", $grant, $match[2], " ON $match[1] TO $new_user")
  85. )) {
  86. $error = true;
  87. break;
  88. }
  89. }
  90. }
  91. if (!$error && isset($_GET["host"])) {
  92. if ($old_user != $new_user) {
  93. queries("DROP USER $old_user");
  94. } elseif (!isset($_GET["grant"])) {
  95. foreach ($grants as $object => $revoke) {
  96. if (preg_match('~^(.+)(\\(.*\\))?$~U', $object, $match)) {
  97. grant("REVOKE", array_keys($revoke), $match[2], " ON $match[1] FROM $new_user");
  98. }
  99. }
  100. }
  101. }
  102. queries_redirect(ME . "privileges=", (isset($_GET["host"]) ? lang('User has been altered.') : lang('User has been created.')), !$error);
  103. if ($created) {
  104. // delete new user in case of an error
  105. $connection->query("DROP USER $new_user");
  106. }
  107. }
  108. }
  109. page_header((isset($_GET["host"]) ? lang('Username') . ": " . h("$USER@$_GET[host]") : lang('Create user')), $error, array("privileges" => array('', lang('Privileges'))));
  110. if ($_POST) {
  111. $row = $_POST;
  112. $grants = $new_grants;
  113. } else {
  114. $row = $_GET + array("host" => $connection->result("SELECT SUBSTRING_INDEX(CURRENT_USER, '@', -1)")); // create user on the same domain by default
  115. $row["pass"] = $old_pass;
  116. if ($old_pass != "") {
  117. $row["hashed"] = true;
  118. }
  119. $grants[(DB == "" || $grants ? "" : idf_escape(addcslashes(DB, "%_\\"))) . ".*"] = array();
  120. }
  121. ?>
  122. <form action="" method="post">
  123. <table cellspacing="0">
  124. <tr><th><?php echo lang('Server'); ?><td><input name="host" maxlength="60" value="<?php echo h($row["host"]); ?>" autocapitalize="off">
  125. <tr><th><?php echo lang('Username'); ?><td><input name="user" maxlength="16" value="<?php echo h($row["user"]); ?>" autocapitalize="off">
  126. <tr><th><?php echo lang('Password'); ?><td><input name="pass" id="pass" value="<?php echo h($row["pass"]); ?>">
  127. <?php if (!$row["hashed"]) { ?><script type="text/javascript">typePassword(document.getElementById('pass'));</script><?php } ?>
  128. <?php echo checkbox("hashed", 1, $row["hashed"], lang('Hashed'), "typePassword(this.form['pass'], this.checked);"); ?>
  129. </table>
  130. <?php
  131. //! MAX_* limits, REQUIRE
  132. echo "<table cellspacing='0'>\n";
  133. echo "<thead><tr><th colspan='2'>" . lang('Privileges') . doc_link(array('sql' => "grant.html#priv_level"));
  134. $i = 0;
  135. foreach ($grants as $object => $grant) {
  136. echo '<th>' . ($object != "*.*" ? "<input name='objects[$i]' value='" . h($object) . "' size='10' autocapitalize='off'>" : "<input type='hidden' name='objects[$i]' value='*.*' size='10'>*.*"); //! separate db, table, columns, PROCEDURE|FUNCTION, routine
  137. $i++;
  138. }
  139. echo "</thead>\n";
  140. foreach (array(
  141. "" => "",
  142. "Server Admin" => lang('Server'),
  143. "Databases" => lang('Database'),
  144. "Tables" => lang('Table'),
  145. "Columns" => lang('Column'),
  146. "Procedures" => lang('Routine'),
  147. ) as $context => $desc) {
  148. foreach ((array) $privileges[$context] as $privilege => $comment) {
  149. echo "<tr" . odd() . "><td" . ($desc ? ">$desc<td" : " colspan='2'") . ' lang="en" title="' . h($comment) . '">' . h($privilege);
  150. $i = 0;
  151. foreach ($grants as $object => $grant) {
  152. $name = "'grants[$i][" . h(strtoupper($privilege)) . "]'";
  153. $value = $grant[strtoupper($privilege)];
  154. if ($context == "Server Admin" && $object != (isset($grants["*.*"]) ? "*.*" : ".*")) {
  155. echo "<td>&nbsp;";
  156. } elseif (isset($_GET["grant"])) {
  157. echo "<td><select name=$name><option><option value='1'" . ($value ? " selected" : "") . ">" . lang('Grant') . "<option value='0'" . ($value == "0" ? " selected" : "") . ">" . lang('Revoke') . "</select>";
  158. } else {
  159. echo "<td align='center'><label class='block'><input type='checkbox' name=$name value='1'" . ($value ? " checked" : "") . ($privilege == "All privileges" ? " id='grants-$i-all'" : ($privilege == "Grant option" ? "" : " onclick=\"if (this.checked) formUncheck('grants-$i-all');\"")) . "></label>"; //! uncheck all except grant if all is checked
  160. }
  161. $i++;
  162. }
  163. }
  164. }
  165. echo "</table>\n";
  166. ?>
  167. <p>
  168. <input type="submit" value="<?php echo lang('Save'); ?>">
  169. <?php if (isset($_GET["host"])) { ?><input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"<?php echo confirm(); ?>><?php } ?>
  170. <input type="hidden" name="token" value="<?php echo $token; ?>">
  171. </form>