From 5ce4d14fc5676ea761713b66ddbd962616283b5f Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Wed, 8 Aug 2007 16:26:30 +0000 Subject: [PATCH] Prepare Privileges git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@313 7c3ca157-0c34-0410-bff1-cbf682f78f5c --- connect.inc.php | 3 +- design.inc.php | 4 +-- index.php | 2 ++ privileges.inc.php | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 privileges.inc.php diff --git a/connect.inc.php b/connect.inc.php index ded979f1..66126524 100644 --- a/connect.inc.php +++ b/connect.inc.php @@ -1,5 +1,5 @@ select_db($_GET["db"]) : isset($_GET["sql"]) || isset($_GET["dump"]) || isset($_GET["database"]) || isset($_GET["processlist"]))) { +if (!(strlen($_GET["db"]) ? $mysql->select_db($_GET["db"]) : isset($_GET["sql"]) || isset($_GET["dump"]) || isset($_GET["database"]) || isset($_GET["processlist"]) || isset($_GET["privileges"]))) { if (strlen($_GET["db"])) { unset($_SESSION["databases"][$_GET["server"]]); } @@ -9,6 +9,7 @@ if (!(strlen($_GET["db"]) ? $mysql->select_db($_GET["db"]) : isset($_GET["sql"]) } else { page_header(lang('Select database'), null); echo '

' . lang('Create new database') . "

\n"; + echo '

' . lang('Privileges') . "

\n"; echo '

' . lang('Process list') . "

\n"; echo "

" . lang('MySQL version') . ": $mysql->server_info " . lang('through PHP extension') . " " . (extension_loaded("mysqli") ? "MySQLi" : (extension_loaded("mysql") ? "MySQL" : "PDO")) . "

\n"; } diff --git a/design.inc.php b/design.inc.php index 95662ee4..354ccd0c 100644 --- a/design.inc.php +++ b/design.inc.php @@ -9,7 +9,7 @@ function page_header($title, $breadcrumb = array(), $title2 = "") { -<?php echo $title . (strlen($title2) ? ": " . htmlspecialchars($title2) : "") . " - " . lang('phpMinAdmin') . " 1.3.3-dev"; ?> +<?php echo $title . (strlen($title2) ? ": " . htmlspecialchars($title2) : "") . " - " . lang('phpMinAdmin') . " 1.4.0-dev"; ?> @@ -32,7 +32,7 @@ function page_header($title, $breadcrumb = array(), $title2 = "") { } foreach ($breadcrumb as $key => $val) { if (strlen($val)) { - echo '' . htmlspecialchars($val) . ' > '; + echo '' . htmlspecialchars($val) . ' > '; } } } diff --git a/index.php b/index.php index 53cc465b..9dd63f12 100644 --- a/index.php +++ b/index.php @@ -83,6 +83,8 @@ if (isset($_GET["dump"])) { include "./procedure.inc.php"; } elseif (isset($_GET["trigger"])) { include "./trigger.inc.php"; + } elseif (isset($_GET["privileges"])) { + include "./privileges.inc.php"; } elseif (isset($_GET["processlist"])) { include "./processlist.inc.php"; } else { diff --git a/privileges.inc.php b/privileges.inc.php new file mode 100644 index 00000000..22918a6a --- /dev/null +++ b/privileges.inc.php @@ -0,0 +1,90 @@ + lang('Privileges'))); + $privileges = array(); + $result = $mysql->query("SHOW PRIVILEGES"); + while ($row = $result->fetch_assoc()) { + foreach (explode(",", $row["Context"]) as $context) { + $privileges[$context][$row["Privilege"]] = $row["Comment"]; //! translation + } + } + $result->free(); + $privileges["Server Admin"] += $privileges["File access on server"]; + $privileges["Databases"]["Create routine"] = $privileges["Procedures"]["Create routine"]; + $privileges["Columns"] = array(); + foreach (array("Select", "Insert", "Update", "References") as $val) { + $privileges["Columns"][$val] = $privileges["Tables"][$val]; + } + unset($privileges["Server Admin"]["Usage"]); + unset($privileges["Procedures"]["Create routine"]); + unset($privileges["Functions"]["Create routine"]); + $grants = array(); + if (strlen($_GET["privileges"]) && ($result = $mysql->query("SHOW GRANTS FOR '" . $mysql->escape_string($_GET["name"]) . "'@'" . $mysql->escape_string($_GET["privileges"]) . "'"))) { //! Use information_schema for MySQL 5 - column names in column privileges are not escaped + while ($row = $result->fetch_row()) { + if (preg_match('~GRANT (.*) ON (.*) TO ~', $row[0], $match)) { //! escape part between ON and TO + preg_match_all('~ *([^(,]*[^ ,(])( *\\([^)]+\\))?~', $match[1], $matches, PREG_SET_ORDER); + foreach ($matches as $val) { + $grants["$match[2]$val[2]"][$val[1]] = true; + } + } + if (preg_match('~ WITH GRANT OPTION~', $row[0])) { //! don't check inside strings and identifiers + $grants[$match[2]]["GRANT OPTION"] = true; + } + } + $result->free(); + } + $grants[""] = true; + + foreach (array( + "Server Admin" => lang('Server'), + "Databases" => lang('Database'), + "Tables" => lang('Table'), + "Columns" => lang('Column'), + "Procedures" => lang('Procedure'), + "Functions" => lang('Function'), + ) as $key => $val) { + if ($privileges[$key]) { + echo "\n"; + echo ""; + if ($key != "Server Admin") { + echo ""; + } + foreach ($privileges[$key] as $privilege => $comment) { + echo ''; + } + echo "\n"; + foreach ($grants as $object => $grant) { + if ($key == "Server Admin" ? $object == (isset($grants["*.*"]) ? "*.*" : "") + : !$object || (substr($object, -1) == ")" || $key == "Columns" ? substr($object, -1) == ")" xor $key != "Columns" + : (preg_match('~PROCEDURE ~', $object) ? $key == "Procedures" + : (preg_match('~FUNCTION ~', $object) ? $key == "Functions" + : (substr($object, -1) == "*" || $key == "Tables" + ))))) { + echo ""; + if ($key != "Server Admin") { + echo '"; + } + foreach ($privileges[$key] as $privilege => $comment) { + echo ""; + } + echo "\n"; + } + } + echo "
$val' . htmlspecialchars($privilege) . '
\n"; + } + } + //! DROP USER, name, server, password +} else { + page_header(lang('Privileges')); + echo '

' . lang('Create user') . "

\n"; + //! use mysql database if possible (GRANTEE not properly escaped) or CURRENT_USER in MySQL 4 in case of insufficient privileges + $result = $mysql->query("SELECT DISTINCT GRANTEE FROM information_schema.USER_PRIVILEGES"); + echo "\n"; + echo "\n"; + while ($row = $result->fetch_row()) { + preg_match("~'((?:[^']+|'')*)'@'((?:[^']+|'')+)'~", $row[0], $match); + echo '\n"; + } + echo "
 " . lang('Username') . "" . lang('Server') . "
' . lang('edit') . '' . htmlspecialchars(str_replace("''", "'", $match[1])) . "" . htmlspecialchars(str_replace("''", "'", $match[2])) . "
\n"; + $result->free(); +}